gstgnomevfssrc.c 33.9 KB
Newer Older
1 2 3 4
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *                    2001 Bastien Nocera <hadess@hadess.net>
5
 *                    2002 Kristian Rietveld <kris@gtk.org>
6
 *                    2002,2003 Colin Walters <walters@gnu.org>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * gnomevfssrc.c:
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#define BROKEN_SIG 1
/*#undef BROKEN_SIG */

#include <sys/types.h>
30 31 32 33 34
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
35 36 37 38
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
Wim Taymans's avatar
Wim Taymans committed
39 40
#include <errno.h>
#include <string.h>
41 42 43

#include <gst/gst.h>
#include <libgnomevfs/gnome-vfs.h>
44 45
/* gnome-vfs.h doesn't include the following header, which we need: */
#include <libgnomevfs/gnome-vfs-standard-callbacks.h>
46 47 48 49 50 51 52 53 54 55 56 57 58 59

GstElementDetails gst_gnomevfssrc_details;

#define GST_TYPE_GNOMEVFSSRC \
  (gst_gnomevfssrc_get_type())
#define GST_GNOMEVFSSRC(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GNOMEVFSSRC,GstGnomeVFSSrc))
#define GST_GNOMEVFSSRC_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_GNOMEVFSSRC,GstGnomeVFSSrcClass))
#define GST_IS_GNOMEVFSSRC(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_GNOMEVFSSRC))
#define GST_IS_GNOMEVFSSRC_CLASS(obj) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_GNOMEVFSSRC))

60 61
static GStaticMutex count_lock = G_STATIC_MUTEX_INIT;
static gint ref_count = 0;
62
static gboolean vfs_owner = FALSE;
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
typedef enum {
	GST_GNOMEVFSSRC_OPEN = GST_ELEMENT_FLAG_LAST,

	GST_GNOMEVFSSRC_FLAG_LAST = GST_ELEMENT_FLAG_LAST + 2,
} GstGnomeVFSSrcFlags;

typedef struct _GstGnomeVFSSrc GstGnomeVFSSrc;
typedef struct _GstGnomeVFSSrcClass GstGnomeVFSSrcClass;

struct _GstGnomeVFSSrc {
	GstElement element;
	/* pads */
	GstPad *srcpad;

	/* filename */
	gchar *filename;
	/* is it a local file ? */
	gboolean is_local;
	/* uri */
	GnomeVFSURI *uri;

	/* handle */
	GnomeVFSHandle *handle;
87 88
	/* Seek stuff */
	gboolean need_flush;
89 90 91 92 93 94 95 96 97 98 99 100 101

	/* local filename */
	gchar *local_name;
	/* fd for local file fallback */
	gint fd;
	/* mmap */
	guchar *map;			/* where the file is mapped to */

	/* details for fallback synchronous read */
	GnomeVFSFileSize size;
	GnomeVFSFileOffset curoffset;	/* current offset in file */
	gulong bytes_per_read;		/* bytes per read */
	gboolean new_seek;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

	/* icecast/audiocast metadata extraction handling */
	gboolean iradio_mode;
	gboolean iradio_callbacks_pushed;

	gint icy_metaint;
	GnomeVFSFileSize icy_count;

	gchar *iradio_name;
	gchar *iradio_genre;
	gchar *iradio_url;
	gchar *iradio_title;

	GThread *audiocast_thread;
	GList *audiocast_notify_queue;
	GMutex *audiocast_queue_mutex;
118
	GMutex *audiocast_udpdata_mutex;
119 120 121 122
	gint audiocast_thread_die_infd;
	gint audiocast_thread_die_outfd;
	gint audiocast_port;
	gint audiocast_fd;
123 124 125 126 127 128
};

struct _GstGnomeVFSSrcClass {
	GstElementClass parent_class;
};

129
/* elementfactory information */
130 131 132
GstElementDetails gst_gnomevfssrc_details = {
	"GnomeVFS Source",
	"Source/File",
133
	"LGPL",
134 135 136 137 138 139
	"Read from any GnomeVFS file",
	VERSION,
	"Bastien Nocera <hadess@hadess.net>",
	"(C) 2001",
};

Wim Taymans's avatar
Wim Taymans committed
140
GST_PAD_FORMATS_FUNCTION (gst_gnomevfssrc_get_formats,
141 142 143 144
	GST_FORMAT_BYTES
)

GST_PAD_QUERY_TYPE_FUNCTION (gst_gnomevfssrc_get_query_types,
Wim Taymans's avatar
Wim Taymans committed
145 146
	GST_QUERY_TOTAL,
	GST_QUERY_POSITION
147 148
)

Wim Taymans's avatar
Wim Taymans committed
149
GST_PAD_EVENT_MASK_FUNCTION (gst_gnomevfssrc_get_event_mask,
150 151 152 153 154 155 156 157
  { GST_EVENT_SEEK, GST_SEEK_METHOD_CUR |
                    GST_SEEK_METHOD_SET |
                    GST_SEEK_METHOD_END |
                    GST_SEEK_FLAG_FLUSH },
  { GST_EVENT_FLUSH, 0 },
  { GST_EVENT_SIZE, 0 }
)

158 159 160 161 162 163 164 165 166
/* GnomeVFSSrc signals and args */
enum {
	LAST_SIGNAL
};

enum {
	ARG_0,
	ARG_LOCATION,
	ARG_BYTESPERREAD,
167 168 169 170 171
	ARG_IRADIO_MODE,
	ARG_IRADIO_NAME,
	ARG_IRADIO_GENRE,
	ARG_IRADIO_URL,
	ARG_IRADIO_TITLE,
172 173 174 175
};

GType gst_gnomevfssrc_get_type(void);

Wim Taymans's avatar
Wim Taymans committed
176 177
static void 		gst_gnomevfssrc_class_init	(GstGnomeVFSSrcClass *klass);
static void 		gst_gnomevfssrc_init		(GstGnomeVFSSrc *gnomevfssrc);
178
static void 		gst_gnomevfssrc_dispose		(GObject *object);
179

Wim Taymans's avatar
Wim Taymans committed
180 181 182 183
static void 		gst_gnomevfssrc_set_property	(GObject *object, guint prop_id,
							 const GValue *value, GParamSpec *pspec);
static void 		gst_gnomevfssrc_get_property	(GObject *object, guint prop_id,
							 GValue *value, GParamSpec *pspec);
184

Wim Taymans's avatar
Wim Taymans committed
185
static GstBuffer*	gst_gnomevfssrc_get		(GstPad *pad);
186

Wim Taymans's avatar
Wim Taymans committed
187 188
static GstElementStateReturn 
			gst_gnomevfssrc_change_state	(GstElement *element);
189

Wim Taymans's avatar
Wim Taymans committed
190 191 192
static void 		gst_gnomevfssrc_close_file	(GstGnomeVFSSrc *src);
static gboolean 	gst_gnomevfssrc_open_file	(GstGnomeVFSSrc *src);
static gboolean 	gst_gnomevfssrc_srcpad_event 	(GstPad *pad, GstEvent *event);
Wim Taymans's avatar
Wim Taymans committed
193
static gboolean 	gst_gnomevfssrc_srcpad_query 	(GstPad *pad, GstQueryType type,
Wim Taymans's avatar
Wim Taymans committed
194
		              				 GstFormat *format, gint64 *value);
195 196 197 198 199 200 201

static int audiocast_init(GstGnomeVFSSrc *src);
static int audiocast_register_listener(gint *port, gint *fd);
static void audiocast_do_notifications(GstGnomeVFSSrc *src);
static gpointer audiocast_thread_run(GstGnomeVFSSrc *src);
static void audiocast_thread_kill(GstGnomeVFSSrc *src);

202 203 204 205 206 207 208 209
static GstElementClass *parent_class = NULL;

GType gst_gnomevfssrc_get_type(void)
{
	static GType gnomevfssrc_type = 0;

	if (!gnomevfssrc_type) {
		static const GTypeInfo gnomevfssrc_info = {
210 211 212 213 214 215 216 217
			sizeof(GstGnomeVFSSrcClass),      NULL,
			NULL,
			(GClassInitFunc) gst_gnomevfssrc_class_init,
			NULL,
			NULL,
			sizeof(GstGnomeVFSSrc),
			0,
			(GInstanceInitFunc) gst_gnomevfssrc_init,
218 219
		};
		gnomevfssrc_type =
220 221 222 223
			g_type_register_static(GST_TYPE_ELEMENT,
					"GstGnomeVFSSrc",
					&gnomevfssrc_info,
					0);
224 225 226 227 228 229 230 231 232 233 234 235 236 237
	}
	return gnomevfssrc_type;
}

static void gst_gnomevfssrc_class_init(GstGnomeVFSSrcClass *klass)
{
	GObjectClass *gobject_class;
	GstElementClass *gstelement_class;

	gobject_class = (GObjectClass *) klass;
	gstelement_class = (GstElementClass *) klass;

	parent_class = g_type_class_ref(GST_TYPE_ELEMENT);

238
      	gst_element_class_install_std_props (
239 240 241 242 243
               GST_ELEMENT_CLASS (klass),
               "bytesperread", ARG_BYTESPERREAD, G_PARAM_READWRITE,
               "location",     ARG_LOCATION,     G_PARAM_READWRITE,
               NULL);

244 245
	gobject_class->dispose         = gst_gnomevfssrc_dispose;

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	/* icecast stuff */
	g_object_class_install_property (gobject_class,
					 ARG_IRADIO_MODE,
					 g_param_spec_boolean ("iradio-mode",
							       "iradio-mode",
							       "Enable internet radio mode (extraction of icecast/audiocast metadata)",
							       FALSE,
							       G_PARAM_READWRITE));
	g_object_class_install_property (gobject_class,
					 ARG_IRADIO_NAME,
					 g_param_spec_string ("iradio-name",
							      "iradio-name",
							      "Name of the stream",
							      NULL,
							      G_PARAM_READABLE));
	g_object_class_install_property (gobject_class,
					 ARG_IRADIO_GENRE,
					 g_param_spec_string ("iradio-genre",
							      "iradio-genre",
							      "Genre of the stream",

							      NULL,
							      G_PARAM_READABLE));
	g_object_class_install_property (gobject_class,
					 ARG_IRADIO_URL,
					 g_param_spec_string ("iradio-url",
							      "iradio-url",
							      "Homepage URL for radio stream",
							      NULL,
							      G_PARAM_READABLE));
	g_object_class_install_property (gobject_class,
					 ARG_IRADIO_TITLE,
					 g_param_spec_string ("iradio-title",
							      "iradio-title",
							      "Name of currently playing song",
							      NULL,
							      G_PARAM_READABLE));

284 285 286 287 288 289 290 291 292 293
	gstelement_class->set_property = gst_gnomevfssrc_set_property;
	gstelement_class->get_property = gst_gnomevfssrc_get_property;

	gstelement_class->change_state = gst_gnomevfssrc_change_state;
}

static void gst_gnomevfssrc_init(GstGnomeVFSSrc *gnomevfssrc)
{
	gnomevfssrc->srcpad = gst_pad_new("src", GST_PAD_SRC);
	gst_pad_set_get_function(gnomevfssrc->srcpad, gst_gnomevfssrc_get);
294 295
	gst_pad_set_event_mask_function (gnomevfssrc->srcpad, 
			gst_gnomevfssrc_get_event_mask);
296 297
	gst_pad_set_event_function (gnomevfssrc->srcpad,
			gst_gnomevfssrc_srcpad_event);
298 299
	gst_pad_set_query_type_function (gnomevfssrc->srcpad, 
			gst_gnomevfssrc_get_query_types);
Wim Taymans's avatar
Wim Taymans committed
300 301
	gst_pad_set_query_function (gnomevfssrc->srcpad,
			gst_gnomevfssrc_srcpad_query);
302 303
	gst_pad_set_formats_function (gnomevfssrc->srcpad, 
			gst_gnomevfssrc_get_formats);
304 305 306 307 308 309 310 311 312 313
	gst_element_add_pad(GST_ELEMENT(gnomevfssrc), gnomevfssrc->srcpad);

	gnomevfssrc->filename = NULL;
	gnomevfssrc->is_local = FALSE;
	gnomevfssrc->uri = NULL;
	gnomevfssrc->handle = NULL;
	gnomevfssrc->fd = 0;
	gnomevfssrc->curoffset = 0;
	gnomevfssrc->bytes_per_read = 4096;
	gnomevfssrc->new_seek = FALSE;
314

315 316 317 318 319 320 321 322 323 324
	gnomevfssrc->icy_metaint = 0;

	gnomevfssrc->iradio_mode = FALSE;
	gnomevfssrc->iradio_callbacks_pushed = FALSE;
	gnomevfssrc->icy_count = 0;
	gnomevfssrc->iradio_name = NULL;
	gnomevfssrc->iradio_genre = NULL;
	gnomevfssrc->iradio_url = NULL;
	gnomevfssrc->iradio_title = NULL;

325
	gnomevfssrc->audiocast_udpdata_mutex = g_mutex_new(); 
326 327 328 329
	gnomevfssrc->audiocast_queue_mutex = g_mutex_new(); 
	gnomevfssrc->audiocast_notify_queue = NULL;
	gnomevfssrc->audiocast_thread = NULL;
		
330 331 332 333 334
	g_static_mutex_lock (&count_lock);
	if (ref_count == 0) {
		/* gnome vfs engine init */
		if (gnome_vfs_initialized() == FALSE) {
			gnome_vfs_init();
335
			vfs_owner = TRUE;
336 337 338 339 340 341 342 343 344 345 346
		}
	}
	ref_count++;
	g_static_mutex_unlock (&count_lock);
}

static void
gst_gnomevfssrc_dispose (GObject *object)
{
	g_static_mutex_lock (&count_lock);
	ref_count--;
347
	if (ref_count == 0 && vfs_owner) {
348 349 350 351 352 353 354
		if (gnome_vfs_initialized() == TRUE) {
			gnome_vfs_shutdown();
		}
	}
	g_static_mutex_unlock (&count_lock);

  	G_OBJECT_CLASS (parent_class)->dispose (object);
355 356 357 358 359 360
}


static void gst_gnomevfssrc_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	GstGnomeVFSSrc *src;
361 362
        const gchar *location;
        gchar cwd[PATH_MAX];
363 364 365 366 367 368 369 370 371 372 373 374

	/* it's not null if we got it, but it might not be ours */
	g_return_if_fail(GST_IS_GNOMEVFSSRC(object));

	src = GST_GNOMEVFSSRC(object);

	switch (prop_id) {
	case ARG_LOCATION:
		/* the element must be stopped or paused in order to do this */
		g_return_if_fail((GST_STATE(src) < GST_STATE_PLAYING)
				 || (GST_STATE(src) == GST_STATE_PAUSED));

375
		g_free(src->filename);
376 377 378

		/* clear the filename if we get a NULL (is that possible?) */
		if (g_value_get_string (value) == NULL) {
Wim Taymans's avatar
Wim Taymans committed
379
			gst_element_set_state(GST_ELEMENT(object), GST_STATE_NULL);
380 381 382
			src->filename = NULL;
		} else {
			/* otherwise set the new filename */
383
                        location = g_value_get_string (value);
384
                        /* if it's not a proper uri, default to file: -- this
385 386
                         * is a crude test */
                        if (!strchr (location, ':'))
387 388 389 390
			{
				gchar *newloc = gnome_vfs_escape_path_string(location);
                                if (*newloc == '/')
                                        src->filename = g_strdup_printf ("file://%s", newloc);
391
                                else
392 393 394
                                        src->filename = g_strdup_printf ("file://%s/%s", getcwd(cwd, PATH_MAX), newloc);
				g_free(newloc);
			}
395 396
                        else
                                src->filename = g_strdup (g_value_get_string (value));
397 398 399 400 401 402 403 404 405 406 407
		}

		if ((GST_STATE(src) == GST_STATE_PAUSED)
		    && (src->filename != NULL)) {
			gst_gnomevfssrc_close_file(src);
			gst_gnomevfssrc_open_file(src);
		}
		break;
	case ARG_BYTESPERREAD:
		src->bytes_per_read = g_value_get_int (value);
		break;
408 409 410
	case ARG_IRADIO_MODE:
		src->iradio_mode = g_value_get_boolean (value);
		break;
411
	default:
412
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
		break;
	}
}

static void gst_gnomevfssrc_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	GstGnomeVFSSrc *src;

	/* it's not null if we got it, but it might not be ours */
	g_return_if_fail(GST_IS_GNOMEVFSSRC(object));

	src = GST_GNOMEVFSSRC(object);

	switch (prop_id) {
	case ARG_LOCATION:
		g_value_set_string (value, src->filename);
		break;
	case ARG_BYTESPERREAD:
		g_value_set_int (value, src->bytes_per_read);
		break;
433 434 435 436 437 438 439 440 441 442 443 444 445
	case ARG_IRADIO_MODE:
		g_value_set_boolean (value, src->iradio_mode);
		break;
	case ARG_IRADIO_NAME:
		g_value_set_string (value, src->iradio_name);
		break;
	case ARG_IRADIO_GENRE:
		g_value_set_string (value, src->iradio_genre);
		break;
	case ARG_IRADIO_URL:
		g_value_set_string (value, src->iradio_url);
		break;
	case ARG_IRADIO_TITLE:
446
		g_mutex_lock(src->audiocast_udpdata_mutex);
447
		g_value_set_string (value, src->iradio_title);
448
		g_mutex_unlock(src->audiocast_udpdata_mutex);
449
		break;
450 451 452 453 454 455
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
/*
 * icecast/audiocast metadata extraction support code
 */

static int audiocast_init(GstGnomeVFSSrc *src)
{
	int pipefds[2];
	GError *error = NULL;
	if (!src->iradio_mode)
		return TRUE;
	GST_DEBUG (0,"audiocast: registering listener");
	if (audiocast_register_listener(&src->audiocast_port, &src->audiocast_fd) < 0)
	{
		gst_element_error(GST_ELEMENT(src),
				  "opening vfs file \"%s\" (%s)",
				  src->filename, 
				  "unable to register UDP port");
		close(src->audiocast_fd);
		return FALSE;
	}
	GST_DEBUG (0,"audiocast: creating pipe");
	src->audiocast_notify_queue = NULL;
	if (pipe(pipefds) < 0)
	{
		close(src->audiocast_fd);
		return FALSE;
	}
	src->audiocast_thread_die_infd = pipefds[0];
	src->audiocast_thread_die_outfd = pipefds[1];
	GST_DEBUG (0,"audiocast: creating audiocast thread");
	src->audiocast_thread = g_thread_create((GThreadFunc) audiocast_thread_run, src, TRUE, &error);
	if (error != NULL) {
		gst_element_error(GST_ELEMENT(src),
				  "opening vfs file \"%s\" (unable to create thread: %s)",
				  src->filename, 
				  error->message);
		close(src->audiocast_fd);
		return FALSE;
	}
	return TRUE;
}

static int audiocast_register_listener(gint *port, gint *fd)
{
	struct sockaddr_in sin;
	int sock;
	socklen_t sinlen = sizeof (struct sockaddr_in);

	GST_DEBUG (0,"audiocast: estabilishing UDP listener");

	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
		goto lose;
	
	memset(&sin, 0, sinlen);
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = g_htonl(INADDR_ANY);
			
	if (bind(sock, (struct sockaddr *)&sin, sinlen) < 0)
		goto lose_and_close;

	memset(&sin, 0, sinlen);
	if (getsockname(sock, (struct sockaddr *)&sin, &sinlen) < 0)
		goto lose_and_close;

	GST_DEBUG (0,"audiocast: listening on local %s:%d", inet_ntoa(sin.sin_addr), g_ntohs(sin.sin_port));

	*port = g_ntohs(sin.sin_port);
	*fd = sock;

	return 0;
lose_and_close:
	close(sock);
lose:
	return -1;
}

static void audiocast_do_notifications(GstGnomeVFSSrc *src)
{
	/* Send any pending notifications we got from the UDP thread. */
	if (src->iradio_mode)
	{
		GList *entry;
		g_mutex_lock(src->audiocast_queue_mutex);
		for (entry = src->audiocast_notify_queue; entry; entry = entry->next)
			g_object_notify(G_OBJECT (src), (const gchar *) entry->data);
		g_list_free(src->audiocast_notify_queue);
		src->audiocast_notify_queue = NULL;
		g_mutex_unlock(src->audiocast_queue_mutex);
	}		
}

static gpointer audiocast_thread_run(GstGnomeVFSSrc *src)
{
	char buf[1025], **lines;
	char *valptr;
	gsize len;
	fd_set fdset, readset;
	struct sockaddr_in from;
	socklen_t fromlen = sizeof(struct sockaddr_in);

	FD_ZERO(&fdset);

	FD_SET(src->audiocast_fd, &fdset);
	FD_SET(src->audiocast_thread_die_infd, &fdset);

	while (1)
	{
		GST_DEBUG (0,"audiocast thread: dropping into select");
		readset = fdset;
		if (select(FD_SETSIZE, &readset, NULL, NULL, NULL) < 0) {
			perror("select");
			return NULL;
		}
		if (FD_ISSET(src->audiocast_thread_die_infd, &readset)) {
			char buf[1];
			GST_DEBUG (0,"audiocast thread: got die character");
			read(src->audiocast_thread_die_infd, buf, 1);
			close(src->audiocast_thread_die_infd);
			close(src->audiocast_fd);
			return NULL;
		}
		GST_DEBUG (0,"audiocast thread: reading data");
		len = recvfrom(src->audiocast_fd, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &from, &fromlen);
		if (len < 0 && errno == EAGAIN)
			continue;
		else if (len >= 0)
		{
			int i;
			buf[len] = '\0';
			lines = g_strsplit(buf, "\n", 0);
			if (!lines)
				continue;
	
			for (i = 0; lines[i]; i++) {
				while ((lines[i][strlen(lines[i]) - 1] == '\n') ||
				       (lines[i][strlen(lines[i]) - 1] == '\r'))
					lines[i][strlen(lines[i]) - 1] = '\0';
			
				valptr = strchr(lines[i], ':');
			
				if (!valptr)
					continue;
				else
					valptr++;
		
				g_strstrip(valptr);
				if (!strlen(valptr))
					continue;
		
				if (!strncmp(lines[i], "x-audiocast-streamtitle", 23)) {
606
					g_mutex_lock(src->audiocast_udpdata_mutex);
607 608 609
					if (src->iradio_title)
						g_free(src->iradio_title);
					src->iradio_title = g_strdup(valptr);
610
					g_mutex_unlock(src->audiocast_udpdata_mutex);
611 612 613 614
					g_mutex_lock(src->audiocast_queue_mutex);
					src->audiocast_notify_queue = g_list_append(src->audiocast_notify_queue, "iradio-title");
					GST_DEBUG(0,"audiocast title: %s\n", src->iradio_title);
					g_mutex_unlock(src->audiocast_queue_mutex);
615 616 617 618 619 620 621 622 623 624
				} else if (!strncmp(lines[i], "x-audiocast-streamurl", 21)) {
					g_mutex_lock(src->audiocast_udpdata_mutex);
					if (src->iradio_url)
						g_free(src->iradio_url);
					src->iradio_url = g_strdup(valptr);
					g_mutex_unlock(src->audiocast_udpdata_mutex);
					g_mutex_lock(src->audiocast_queue_mutex);
					src->audiocast_notify_queue = g_list_append(src->audiocast_notify_queue, "iradio-url");
					GST_DEBUG(0,"audiocast url: %s\n", src->iradio_title);
					g_mutex_unlock(src->audiocast_queue_mutex);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
				} else if (!strncmp(lines[i], "x-audiocast-udpseqnr", 20)) {
					gchar outbuf[120];
					sprintf(outbuf, "x-audiocast-ack: %ld \r\n", atol(valptr));
					if (sendto(src->audiocast_fd, outbuf, strlen(outbuf), 0, (struct sockaddr *) &from, fromlen) <= 0) {
						fprintf(stderr, "Error sending response to server: %s\n", strerror(errno));
						continue;
					}
					GST_DEBUG(0,"sent audiocast ack: %s\n", outbuf);
				}
			}
			g_strfreev(lines);
		}
	}
	return NULL;
}

static void audiocast_thread_kill(GstGnomeVFSSrc *src)
{
	if (!src->audiocast_thread)
		return;
	
	/*
	  We rely on this hack to kill the
	  audiocast thread.  If we get icecast
	  metadata, then we don't need the
	  audiocast metadata too.
	*/
	GST_DEBUG (0,"audiocast: writing die character");
	write(src->audiocast_thread_die_outfd, "q", 1);
	close(src->audiocast_thread_die_outfd);
	GST_DEBUG (0,"audiocast: joining thread");
	g_thread_join(src->audiocast_thread);
	src->audiocast_thread = NULL;
}

static void
gst_gnomevfssrc_send_additional_headers_callback (gconstpointer in,
						  gsize         in_size,
						  gpointer      out,
						  gsize         out_size,
						  gpointer      callback_data)
{
	GstGnomeVFSSrc *src = GST_GNOMEVFSSRC (callback_data);
	GnomeVFSModuleCallbackAdditionalHeadersOut *out_args =
                (GnomeVFSModuleCallbackAdditionalHeadersOut *)out;

	if (!src->iradio_mode)
		return;
	GST_DEBUG(0,"sending headers\n");

        out_args->headers = g_list_append (out_args->headers,
                                           g_strdup ("icy-metadata:1\r\n"));
        out_args->headers = g_list_append (out_args->headers,
                                           g_strdup_printf ("x-audiocast-udpport: %d\r\n", src->audiocast_port));
}

static void
gst_gnomevfssrc_received_headers_callback (gconstpointer in,
					   gsize         in_size,
					   gpointer      out,
					   gsize         out_size,
					   gpointer      callback_data)
{
	GList *i;
	gint icy_metaint;
	GstGnomeVFSSrc *src = GST_GNOMEVFSSRC (callback_data);
        GnomeVFSModuleCallbackReceivedHeadersIn *in_args =
                (GnomeVFSModuleCallbackReceivedHeadersIn *)in;

	if (!src->iradio_mode)
		return;

        for (i = in_args->headers; i; i = i->next) {
		char *data = (char *) i->data;
		char *key;
		char *value = strchr(data, ':');
		if (!value)
			continue;

		value++;
		g_strstrip(value);
		if (!strlen(value))
			continue;

		/* Icecast stuff */
                if (!strncmp (data, "icy-metaint:", 12)) /* ugh */
		{ 
                        sscanf (data + 12, "%d", &icy_metaint);
			src->icy_metaint = icy_metaint;
			GST_DEBUG (0,"got icy-metaint, killing audiocast thread");
			audiocast_thread_kill(src);
			continue;
		}

		if (!strncmp(data, "icy-", 4))
			key = data + 4;
		else if (!strncmp(data, "x-audiocast-", 12))
			key = data + 12;
		else
			continue;

726
		GST_DEBUG (0,"key: %s", key);
727 728 729 730 731 732 733 734 735 736
                if (!strncmp (key, "name", 4)) {
			if (src->iradio_name)
				g_free (src->iradio_name);
			src->iradio_name = g_strdup (value);
			g_object_notify (G_OBJECT (src), "iradio-name");
                } else if (!strncmp (key, "genre", 5)) {
			if (src->iradio_genre)
				g_free (src->iradio_genre);
			src->iradio_genre = g_strdup (value);
			g_object_notify (G_OBJECT (src), "iradio-genre");
737
                } else if (!strncmp (key, "url", 3)) {
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
			if (src->iradio_url)
				g_free (src->iradio_url);
			src->iradio_url = g_strdup (value);
			g_object_notify (G_OBJECT (src), "iradio-url");
		}
        }
}

static void
gst_gnomevfssrc_push_callbacks (GstGnomeVFSSrc *gnomevfssrc)
{
	if (gnomevfssrc->iradio_callbacks_pushed)
		return;

	GST_DEBUG (0,"pushing callbacks");
	gnome_vfs_module_callback_push (GNOME_VFS_MODULE_CALLBACK_HTTP_SEND_ADDITIONAL_HEADERS,
					gst_gnomevfssrc_send_additional_headers_callback,
					gnomevfssrc,
					NULL);
	gnome_vfs_module_callback_push (GNOME_VFS_MODULE_CALLBACK_HTTP_RECEIVED_HEADERS,
					gst_gnomevfssrc_received_headers_callback,
					gnomevfssrc,
					NULL);

	gnomevfssrc->iradio_callbacks_pushed = TRUE;
}

static void
gst_gnomevfssrc_pop_callbacks (GstGnomeVFSSrc *gnomevfssrc)
{
	if (!gnomevfssrc->iradio_callbacks_pushed)
		return;

	GST_DEBUG (0,"popping callbacks");
	gnome_vfs_module_callback_pop (GNOME_VFS_MODULE_CALLBACK_HTTP_SEND_ADDITIONAL_HEADERS);
	gnome_vfs_module_callback_pop (GNOME_VFS_MODULE_CALLBACK_HTTP_RECEIVED_HEADERS);
}

static void
gst_gnomevfssrc_get_icy_metadata (GstGnomeVFSSrc *src)
{
	GnomeVFSFileSize length = 0;
        GnomeVFSResult res;
        gint metadata_length;
        guchar foobyte;
        guchar *data;
        guchar *pos;
	gchar **tags;
	int i;

	GST_DEBUG (0,"reading icecast metadata");

        while (length == 0) {
                res = gnome_vfs_read (src->handle, &foobyte, 1, &length);
                if (res != GNOME_VFS_OK)
                        return;
        }

        metadata_length = foobyte * 16;

        if (metadata_length == 0)
                return;

	data = g_new (gchar, metadata_length + 1);
        pos = data;

        while (pos - data < metadata_length) {
                res = gnome_vfs_read (src->handle, pos,
                                      metadata_length - (pos - data),
                                      &length);
		/* FIXME: better error handling here? */
                if (res != GNOME_VFS_OK) {
                        g_free (data);
                        return;
                }

                pos += length;
        }

        data[metadata_length] = 0;
	tags = g_strsplit(data, "';", 0);
	
	for (i = 0; tags[i]; i++)
	{
		if (!g_strncasecmp(tags[i], "StreamTitle=", 12))
		{
			if (src->iradio_title)
				g_free (src->iradio_title);
			src->iradio_title = g_strdup(tags[i]+13);
			GST_DEBUG(0, "sending notification on icecast title");
			g_object_notify (G_OBJECT (src), "iradio-title");
		}
830 831 832 833 834 835 836 837
		if (!g_strncasecmp(tags[i], "StreamUrl=", 10))
		{
			if (src->iradio_url)
				g_free (src->iradio_url);
			src->iradio_url = g_strdup(tags[i]+11);
			GST_DEBUG(0, "sending notification on icecast url");
			g_object_notify (G_OBJECT (src), "iradio-url");
		}
838 839 840 841 842 843 844
	}

	g_strfreev(tags);
}

/* end of icecast/audiocast metadata extraction support code */

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
/**
 * gst_gnomevfssrc_get:
 * @pad: #GstPad to push a buffer from
 *
 * Push a new buffer from the gnomevfssrc at the current offset.
 */
static GstBuffer *gst_gnomevfssrc_get(GstPad *pad)
{
	GstGnomeVFSSrc *src;
	GnomeVFSResult result = 0;
	GstBuffer *buf;
	GnomeVFSFileSize readbytes;

	g_return_val_if_fail(pad != NULL, NULL);
	src = GST_GNOMEVFSSRC(gst_pad_get_parent(pad));
	g_return_val_if_fail(GST_FLAG_IS_SET(src, GST_GNOMEVFSSRC_OPEN),
			     NULL);

	/* deal with EOF state */
	if ((src->curoffset >= src->size) && (src->size != 0))
	{
Wim Taymans's avatar
Wim Taymans committed
866
		gst_element_set_eos (GST_ELEMENT (src));
Wim Taymans's avatar
Wim Taymans committed
867
		return GST_BUFFER (gst_event_new (GST_EVENT_EOS));
868 869 870 871 872 873 874
	}

	/* create the buffer */
	/* FIXME: should eventually use a bufferpool for this */
	buf = gst_buffer_new();
	g_return_val_if_fail(buf, NULL);

875 876
	audiocast_do_notifications(src);

877 878 879 880 881 882 883 884
	/* read it in from the file */
	if (src->is_local)
	{
		/* simply set the buffer to point to the correct region of the
		 * file */
		GST_BUFFER_DATA (buf) = src->map + src->curoffset;
		GST_BUFFER_OFFSET (buf) = src->curoffset;
		GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
885
		GST_BUFFER_FLAG_SET (buf, GST_BUFFER_READONLY);
886 887 888 889 890 891 892 893 894 895

		if ((src->curoffset + src->bytes_per_read) > src->size)
		{
			GST_BUFFER_SIZE (buf) = src->size - src->curoffset;
		} else {
			GST_BUFFER_SIZE (buf) = src->bytes_per_read;
		}

		if (src->new_seek)
		{
Wim Taymans's avatar
Wim Taymans committed
896 897
			GstEvent *event;

898
			gst_buffer_unref (buf);
899
			GST_DEBUG (0,"new seek %" G_GINT64_FORMAT, src->curoffset);
900
			src->new_seek = FALSE;
Wim Taymans's avatar
Wim Taymans committed
901 902 903 904 905

			GST_DEBUG (GST_CAT_EVENT, "gnomevfssrc sending discont");
			event = gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, src->curoffset, NULL);
			src->need_flush = FALSE;
			return GST_BUFFER (event);
906
		}
Wim Taymans's avatar
Wim Taymans committed
907 908 909

		src->curoffset += GST_BUFFER_SIZE (buf);

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
	} else if (src->iradio_mode && src->icy_metaint > 0) {
		GST_BUFFER_DATA (buf) = g_malloc0 (src->icy_metaint);
		g_return_val_if_fail (GST_BUFFER_DATA (buf) != NULL, NULL);

		result = gnome_vfs_read (src->handle, GST_BUFFER_DATA (buf),
					 src->icy_metaint - src->icy_count,
					 &readbytes);

		/* EOS? */
		if (readbytes == 0) {
			gst_buffer_unref (buf);
			gst_element_set_eos (GST_ELEMENT (src));

			return GST_BUFFER (gst_event_new (GST_EVENT_EOS));
		}
Wim Taymans's avatar
Wim Taymans committed
925

926 927 928 929 930 931 932 933 934 935
		src->icy_count += readbytes;
		GST_BUFFER_OFFSET (buf) = src->curoffset;
		GST_BUFFER_SIZE (buf) = readbytes;
		src->curoffset += readbytes;

		if (src->icy_count == src->icy_metaint) {
			gst_gnomevfssrc_get_icy_metadata (src);
			src->icy_count = 0;
		}

936 937 938 939 940 941 942
	} else {
		/* allocate the space for the buffer data */
		GST_BUFFER_DATA(buf) = g_malloc(src->bytes_per_read);
		g_return_val_if_fail(GST_BUFFER_DATA(buf) != NULL, NULL);

		if (src->new_seek)
		{
Wim Taymans's avatar
Wim Taymans committed
943 944
			GstEvent *event;

945
			gst_buffer_unref (buf);
946
			GST_DEBUG (0,"new seek %" G_GINT64_FORMAT, src->curoffset);
947
			src->new_seek = FALSE;
Wim Taymans's avatar
Wim Taymans committed
948 949 950 951

			GST_DEBUG (GST_CAT_EVENT, "gnomevfssrc sending discont");
			event = gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, src->curoffset, NULL);
			src->need_flush = FALSE;
952

Wim Taymans's avatar
Wim Taymans committed
953
			return GST_BUFFER (event);
954 955 956 957 958
		}

		result = gnome_vfs_read(src->handle, GST_BUFFER_DATA(buf),
				   src->bytes_per_read, &readbytes);

959
		GST_DEBUG(0, "read: %s, readbytes: %" G_GINT64_FORMAT,
960 961 962 963 964
				gnome_vfs_result_to_string(result), readbytes);
		/* deal with EOS */
		if (readbytes == 0)
		{
			gst_buffer_unref(buf);
Wim Taymans's avatar
Wim Taymans committed
965

Wim Taymans's avatar
Wim Taymans committed
966
			gst_element_set_eos (GST_ELEMENT (src));
Wim Taymans's avatar
Wim Taymans committed
967 968

			return GST_BUFFER (gst_event_new (GST_EVENT_EOS));
969 970 971 972 973 974 975
		}
		
		GST_BUFFER_OFFSET(buf) = src->curoffset;
		GST_BUFFER_SIZE(buf) = readbytes;
		src->curoffset += readbytes;
	}

Wim Taymans's avatar
Wim Taymans committed
976 977
	GST_BUFFER_TIMESTAMP (buf) = -1;

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	/* we're done, return the buffer */
	return buf;
}

/* open the file and mmap it, necessary to go to READY state */
static gboolean gst_gnomevfssrc_open_file(GstGnomeVFSSrc *src)
{
	GnomeVFSResult result;

	g_return_val_if_fail(!GST_FLAG_IS_SET(src, GST_GNOMEVFSSRC_OPEN),
			     FALSE);

	/* create the uri */
	src->uri = gnome_vfs_uri_new(src->filename);
	if (!src->uri) {
		gst_element_error(GST_ELEMENT(src),
Wim Taymans's avatar
Wim Taymans committed
994 995
				  "creating uri \"%s\" (%s)",
				  	src->filename, strerror (errno));
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
		return FALSE;
	}

	/* open the file using open() if the file is local */
	src->is_local = gnome_vfs_uri_is_local(src->uri);

	if (src->is_local) {
		src->local_name =
			gnome_vfs_get_local_path_from_uri(src->filename);
		src->fd = open(src->local_name, O_RDONLY);
		if (src->fd < 0)
		{
			gst_element_error(GST_ELEMENT(src),
Wim Taymans's avatar
Wim Taymans committed
1009 1010
					  "opening local file \"%s\" (%s)",
						src->filename, strerror (errno));
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
			return FALSE;
		}

		/* find the file length */
		src->size = lseek (src->fd, 0, SEEK_END);
		lseek (src->fd, 0, SEEK_SET);
		src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED,
				src->fd, 0);
		madvise (src->map,src->size, 2);
		/* collapse state if that failed */
		if (src->map == NULL)
		{
			gst_gnomevfssrc_close_file(src);
			gst_element_error (GST_ELEMENT (src),"mmapping file");
			return FALSE;
		}

		src->new_seek = TRUE;
	} else {
1030 1031 1032 1033 1034 1035

		if (!audiocast_init(src))
			return FALSE;
			
		gst_gnomevfssrc_push_callbacks (src);

1036 1037 1038 1039 1040
		result =
		    gnome_vfs_open_uri(&(src->handle), src->uri,
				       GNOME_VFS_OPEN_READ);
		if (result != GNOME_VFS_OK)
		{
1041 1042
			gst_gnomevfssrc_pop_callbacks (src);
			audiocast_thread_kill(src);
1043
			gst_element_error(GST_ELEMENT(src),
Wim Taymans's avatar
Wim Taymans committed
1044 1045 1046
				  "opening vfs file \"%s\" (%s)",
				      src->filename, 
				      gnome_vfs_result_to_string(result));
1047 1048 1049
			return FALSE;
		}

1050 1051 1052 1053
		/* find the file length (but skip it in iradio mode,
		 * since it will require a separate request, and we
		 * know the length is undefined anyways) */
		if (!src->iradio_mode)
1054 1055 1056 1057 1058 1059 1060
		{
			GnomeVFSResult size_result;
			GnomeVFSFileInfo *info;

			info = gnome_vfs_file_info_new ();
			size_result = gnome_vfs_get_file_info_uri(src->uri,
					info, GNOME_VFS_FILE_INFO_DEFAULT);
1061

1062 1063 1064 1065 1066 1067 1068
			if (size_result != GNOME_VFS_OK)
				src->size = 0;
			else
				src->size = info->size;

			gnome_vfs_file_info_unref(info);
		}
1069 1070 1071
		else
			src->size = 0;

1072
		GST_DEBUG(0, "size %" G_GINT64_FORMAT, src->size);
1073

1074 1075
		audiocast_do_notifications(src);
	
1076
		GST_DEBUG(0, "open %s", gnome_vfs_result_to_string(result));
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
	}

	GST_FLAG_SET(src, GST_GNOMEVFSSRC_OPEN);

	return TRUE;
}

/* unmap and close the file */
static void gst_gnomevfssrc_close_file(GstGnomeVFSSrc *src)
{
	g_return_if_fail(GST_FLAG_IS_SET(src, GST_GNOMEVFSSRC_OPEN));

1089 1090 1091
	gst_gnomevfssrc_pop_callbacks (src);
	audiocast_thread_kill(src);

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	/* close the file */
	if (src->is_local)
	{
		/* unmap the file from memory */
		munmap (src->map, src->size);
		close(src->fd);
	} else {
		gnome_vfs_close(src->handle);
	}

	/* zero out a lot of our state */
	src->is_local = FALSE;
1104
	gnome_vfs_uri_unref(src->uri);
1105
	g_free(src->local_name);
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	src->fd = 0;
	src->map = NULL;
	src->size = 0;
	src->curoffset = 0;
	src->new_seek = FALSE;

	GST_FLAG_UNSET(src, GST_GNOMEVFSSRC_OPEN);
}

static GstElementStateReturn gst_gnomevfssrc_change_state(GstElement *element)
{
	g_return_val_if_fail(GST_IS_GNOMEVFSSRC(element),
			     GST_STATE_FAILURE);

1120
	switch (GST_STATE_TRANSITION (element)) {
Wim Taymans's avatar
Wim Taymans committed
1121
	case GST_STATE_READY_TO_PAUSED:
1122 1123
		if (!GST_FLAG_IS_SET(element, GST_GNOMEVFSSRC_OPEN)) {
			if (!gst_gnomevfssrc_open_file
1124
					(GST_GNOMEVFSSRC(element)))
1125 1126
				return GST_STATE_FAILURE;
		}
1127
		break;
Wim Taymans's avatar
Wim Taymans committed
1128
	case GST_STATE_PAUSED_TO_READY:
1129 1130
		if (GST_FLAG_IS_SET(element, GST_GNOMEVFSSRC_OPEN))
			gst_gnomevfssrc_close_file(GST_GNOMEVFSSRC
1131 1132
					(element));
		break;
Wim Taymans's avatar
Wim Taymans committed
1133 1134
	case GST_STATE_NULL_TO_READY:
	case GST_STATE_READY_TO_NULL:
1135 1136
	default:
		break;
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	}

	if (GST_ELEMENT_CLASS(parent_class)->change_state)
		return GST_ELEMENT_CLASS(parent_class)->
		    change_state(element);

	return GST_STATE_SUCCESS;
}

static gboolean plugin_init(GModule *module, GstPlugin *plugin)
{
	GstElementFactory *factory;

	/* create an elementfactory for the aasink element */
	factory =
1152
	    gst_element_factory_new("gnomevfssrc", GST_TYPE_GNOMEVFSSRC,
1153 1154 1155 1156 1157 1158 1159 1160
				   &gst_gnomevfssrc_details);
	g_return_val_if_fail(factory != NULL, FALSE);

	gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));

	return TRUE;
}

1161

Wim Taymans's avatar
Wim Taymans committed
1162
static gboolean
Wim Taymans's avatar
Wim Taymans committed
1163
gst_gnomevfssrc_srcpad_query (GstPad *pad, GstQueryType type,
Wim Taymans's avatar
Wim Taymans committed
1164 1165 1166 1167 1168
		              GstFormat *format, gint64 *value)
{
	GstGnomeVFSSrc *src = GST_GNOMEVFSSRC (gst_pad_get_parent (pad));

	switch (type) {
Wim Taymans's avatar
Wim Taymans committed
1169
	case GST_QUERY_TOTAL:
Wim Taymans's avatar
Wim Taymans committed
1170 1171 1172 1173 1174
		if (*format != GST_FORMAT_BYTES) {
			return FALSE;
		}
		*value = src->size;
		break;
Wim Taymans's avatar
Wim Taymans committed
1175
	case GST_QUERY_POSITION:
Wim Taymans's avatar
Wim Taymans committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		if (*format != GST_FORMAT_BYTES) {
			return FALSE;
		}
		*value = src->curoffset;
		break;
	default:
		return FALSE;
		break;
	}
	return TRUE;
} 

1188 1189 1190 1191 1192 1193 1194
static gboolean
gst_gnomevfssrc_srcpad_event (GstPad *pad, GstEvent *event)
{
	GstGnomeVFSSrc *src = GST_GNOMEVFSSRC(GST_PAD_PARENT(pad));

	switch (GST_EVENT_TYPE (event)) {
	case GST_EVENT_SEEK:
Wim Taymans's avatar
Wim Taymans committed
1195
        {
Wim Taymans's avatar
Wim Taymans committed
1196 1197
		gint64 desired_offset;

Wim Taymans's avatar
Wim Taymans committed
1198
		if (GST_EVENT_SEEK_FORMAT (event) != GST_FORMAT_BYTES) {
1199
			gst_event_unref (event);
Wim Taymans's avatar
Wim Taymans committed
1200 1201 1202 1203
			return FALSE;
		}
		switch (GST_EVENT_SEEK_METHOD (event)) {
		case GST_SEEK_METHOD_SET:
Wim Taymans's avatar
Wim Taymans committed
1204
			desired_offset = (guint64) GST_EVENT_SEEK_OFFSET (event);
1205
			break;
Wim Taymans's avatar
Wim Taymans committed
1206
		case GST_SEEK_METHOD_CUR:
Wim Taymans's avatar
Wim Taymans committed
1207
			desired_offset = src->curoffset + GST_EVENT_SEEK_OFFSET (event);
1208
			break;
Wim Taymans's avatar
Wim Taymans committed
1209
		case GST_SEEK_METHOD_END:
Wim Taymans's avatar
Wim Taymans committed
1210
			desired_offset = src->size - ABS (GST_EVENT_SEEK_OFFSET (event));
1211 1212
			break;
		default:
1213
			gst_event_unref (event);
1214 1215 1216
			return FALSE;
			break;
		}
Wim Taymans's avatar
Wim Taymans committed
1217 1218 1219 1220 1221 1222 1223 1224 1225
		if (!src->is_local) {
			GnomeVFSResult result;

			result = gnome_vfs_seek(src->handle,
					GNOME_VFS_SEEK_START, desired_offset);
			GST_DEBUG(0, "new_seek: %s",
					gnome_vfs_result_to_string(result));
			
			if (result != GNOME_VFS_OK) {
1226
				gst_event_unref (event);
Wim Taymans's avatar
Wim Taymans committed
1227 1228 1229 1230 1231
				return FALSE;
			}
		}
		src->curoffset = desired_offset;
		src->new_seek = TRUE;
Wim Taymans's avatar
Wim Taymans committed
1232
		src->need_flush = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH;
1233
		break;
Wim Taymans's avatar
Wim Taymans committed
1234
	}
Wim Taymans's avatar
Wim Taymans committed
1235 1236
	case GST_EVENT_SIZE:
		if (GST_EVENT_SIZE_FORMAT (event) != GST_FORMAT_BYTES) {
1237
			gst_event_unref (event);
Wim Taymans's avatar
Wim Taymans committed
1238 1239 1240 1241 1242 1243
			return FALSE;
		}
		src->bytes_per_read = GST_EVENT_SIZE_VALUE (event);
		g_object_notify (G_OBJECT (src), "bytesperread");
		break;

1244 1245 1246 1247
	case GST_EVENT_FLUSH:
		src->need_flush = TRUE;
		break;
	default:
1248
		gst_event_unref (event);
1249 1250 1251
		return FALSE;
		break;
	}
1252
	gst_event_unref (event);
1253 1254 1255 1256

	return TRUE;
}

1257 1258 1259 1260 1261 1262
GstPluginDesc plugin_desc = {
	GST_VERSION_MAJOR,
	GST_VERSION_MINOR,
	"gnomevfssrc",
	plugin_init
};
1263 1264 1265 1266 1267 1268

/*
  Local Variables:
  c-file-style: "linux"
  End:
*/