gstoggdemux.c 27.6 KB
Newer Older
1 2 3
/* GStreamer
 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
 *
4
 * gstoggdemux.c: ogg stream demuxer
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <ogg/ogg.h>
27 28
/* memcpy - if someone knows a way to get rid of it, please speak up 
 * note: the ogg docs even say you ned this... */
29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <string.h> 

GST_DEBUG_CATEGORY_STATIC (gst_ogg_demux_debug);
#define GST_CAT_DEFAULT gst_ogg_demux_debug

#define GST_TYPE_OGG_DEMUX (gst_ogg_demux_get_type()) 
#define GST_OGG_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_OGG_DEMUX, GstOggDemux))
#define GST_OGG_DEMUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_OGG_DEMUX, GstOggDemux))
#define GST_IS_OGG_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_OGG_DEMUX))
#define GST_IS_OGG_DEMUX_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_OGG_DEMUX))

typedef struct _GstOggDemux GstOggDemux;
typedef struct _GstOggDemuxClass GstOggDemuxClass;

43 44 45 46 47 48 49 50 51 52 53 54 55 56
typedef enum {
  /* just because you shouldn't make a valid enum value 0 */
  GST_OGG_STATE_INAVLID,
  /* just started, we need to decide if we should do setup */
  GST_OGG_STATE_START,
  /* setup is analyzing the stream, getting lengths and so on */
  GST_OGG_STATE_SETUP,
  /* after a seek, during resyncing */
  GST_OGG_STATE_SEEK,
  /* normal playback */
  GST_OGG_STATE_PLAY
} GstOggState;

/* all information needed for one ogg stream */
57 58 59
typedef struct {
  GstPad *		pad; /* reference for this pad is held by element we belong to */

60
  gint			serial;
61 62
  ogg_stream_state	stream;
  guint64		offset; /* end offset of last buffer */
63
  guint64		known_offset; /* last known offset */
64 65
  gint64		packetno; /* number of next expected packet */
  
66
  guint64		length;	/* length of stream or 0 */
67 68 69
  glong			pages; /* number of pages in stream or 0 */

  guint			flags;
70 71
} GstOggPad;

72 73 74 75 76 77
typedef enum {
  GST_OGG_PAD_NEEDS_DISCONT	= (1 << 0),
  GST_OGG_PAD_NEEDS_FLUSH	= (1 << 1)
}
GstOggPadFlags;

78 79 80 81 82
/* all information needed for one ogg chain (relevant for chained bitstreams) */
typedef struct {
  GSList *		pads; /* list of GstOggPad */
} GstOggChain;
#define CURRENT_CHAIN(ogg) (&g_array_index ((ogg)->chains, GstOggChain, (ogg)->current_chain))
83 84 85 86 87 88 89
#define FOR_PAD_IN_CURRENT_CHAIN(ogg, _pad, ...) G_STMT_START{			\
  GSList *_walk;							      	\
  for (_walk = CURRENT_CHAIN (ogg)->pads; _walk; _walk = g_slist_next (_walk)) { \
    GstOggPad *_pad = (GstOggPad *) _walk->data;				\
    __VA_ARGS__									\
  }										\
}G_STMT_END
90 91 92 93 94 95 96

typedef enum {
  GST_OGG_FLAG_BOS = GST_ELEMENT_FLAG_LAST,
  GST_OGG_FLAG_EOS,
  GST_OGG_FLAG_WAIT_FOR_DISCONT
} GstOggFlag;

97 98 99
struct _GstOggDemux {
  GstElement		element;

100
  /* pad */
101
  GstPad *		sinkpad;
102 103 104 105 106 107 108
  
  /* state */
  GstOggState		state;
  GArray *		chains;
  gint			current_chain;
  guint			flags;
  
109 110
  /* ogg stuff */
  ogg_sync_state	sync;
111 112 113 114
 
  /* seeking */
  GstOggPad *		seek_pad;
  guint64		seek_to;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
};

struct _GstOggDemuxClass {
  GstElementClass parent_class;
};

/* elementfactory information */
static GstElementDetails gst_ogg_demux_details = GST_ELEMENT_DETAILS (
  "ogg demuxer",
  "Codec/Demuxer",
  "demux ogg streams (info about ogg: http://xiph.org)",
  "Benjamin Otte <in7y118@public.uni-hamburg.de>"
);


/* signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  /* FILL ME */
};

David Schleef's avatar
David Schleef committed
141 142
static GstStaticPadTemplate ogg_demux_src_template_factory =
GST_STATIC_PAD_TEMPLATE (
143 144 145
  "src",
  GST_PAD_SRC,
  GST_PAD_SOMETIMES,
David Schleef's avatar
David Schleef committed
146 147 148 149 150
  GST_STATIC_CAPS_ANY
);

static GstStaticPadTemplate ogg_demux_sink_template_factory =
GST_STATIC_PAD_TEMPLATE (
151 152 153
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
David Schleef's avatar
David Schleef committed
154 155
  GST_STATIC_CAPS ("application/ogg")
);
156 157


158
static void		gst_ogg_demux_finalize		(GObject *		object);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

static gboolean		gst_ogg_demux_src_event		(GstPad *		pad, 
							 GstEvent *		event);
static const GstEventMask* gst_ogg_demux_get_event_masks (GstPad *		pad);
static const GstQueryType* gst_ogg_demux_get_query_types (GstPad *		pad);

static gboolean		gst_ogg_demux_src_query		(GstPad *		pad,
							 GstQueryType		type,
							 GstFormat *		format, 
							 gint64 *		value);

static void		gst_ogg_demux_chain		(GstPad *		pad,
							 GstData *		buffer);

static GstElementStateReturn gst_ogg_demux_change_state	(GstElement *		element);

static GstOggPad *	gst_ogg_pad_new			(GstOggDemux *		ogg,
							 int			serial_no);
static void		gst_ogg_pad_remove		(GstOggDemux *		ogg,
							 GstOggPad *		ogg_pad);
static void		gst_ogg_pad_reset		(GstOggDemux *		ogg,
							 GstOggPad *		pad);
static void		gst_ogg_demux_push		(GstOggDemux *		ogg,
							 ogg_page *		page);
static void		gst_ogg_pad_push		(GstOggDemux *		ogg,
							 GstOggPad *		ogg_pad);

static GstCaps *	gst_ogg_type_find		(ogg_packet *		packet);
  
188
static void		gst_ogg_print			(GstOggDemux *		demux);
189

190 191 192 193 194 195
#define GST_OGG_SET_STATE(ogg, new_state) G_STMT_START{				\
  GST_DEBUG_OBJECT (ogg, "setting state to %s", G_STRINGIFY (new_state));	\
  ogg->state = new_state;							\
}G_STMT_END
	
GST_BOILERPLATE (GstOggDemux, gst_ogg_demux, GstElement, GST_TYPE_ELEMENT)
196 197 198 199 200 201 202 203 204

static void
gst_ogg_demux_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
  
  gst_element_class_set_details (element_class, &gst_ogg_demux_details);

  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
205
      gst_static_pad_template_get (&ogg_demux_sink_template_factory));
206
  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
207
      gst_static_pad_template_get (&ogg_demux_src_template_factory));
208 209
}
static void
210
gst_ogg_demux_class_init (GstOggDemuxClass *klass)
211
{
212 213
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
214 215 216
  
  gstelement_class->change_state = gst_ogg_demux_change_state;

217
  gobject_class->finalize = gst_ogg_demux_finalize;
218 219
}
static void
220
gst_ogg_demux_init (GstOggDemux *ogg)
221 222 223
{
  /* create the sink pad */
  ogg->sinkpad = gst_pad_new_from_template(
David Schleef's avatar
David Schleef committed
224
      gst_static_pad_template_get (&ogg_demux_sink_template_factory), "sink");
225 226 227 228
  gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
  gst_pad_set_chain_function (ogg->sinkpad, GST_DEBUG_FUNCPTR (gst_ogg_demux_chain));

  /* initalize variables */
229 230
  GST_OGG_SET_STATE (ogg, GST_OGG_STATE_START);
  ogg->chains = g_array_new (TRUE, TRUE, sizeof (GstOggChain));
231
  ogg->current_chain = -1;
232 233 234 235
  
  GST_FLAG_SET (ogg, GST_ELEMENT_EVENT_AWARE);
}
static void
236
gst_ogg_demux_finalize (GObject *object)
237 238 239 240 241
{
  GstOggDemux *ogg;

  ogg = GST_OGG_DEMUX (object);

242 243 244 245
  /* chains are removed when going to READY */
  g_assert (ogg->current_chain == -1);
  g_assert (ogg->chains->len == 0);
  g_array_free (ogg->chains, TRUE);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

static const GstEventMask*
gst_ogg_demux_get_event_masks (GstPad *pad)
{
  static const GstEventMask gst_ogg_demux_src_event_masks[] = {
    { GST_EVENT_SEEK, GST_SEEK_METHOD_SET |
                      GST_SEEK_FLAG_FLUSH },
    { 0, }
  };
  return gst_ogg_demux_src_event_masks;
}
static const GstQueryType*
gst_ogg_demux_get_query_types (GstPad *pad)
{
  static const GstQueryType gst_ogg_demux_src_query_types[] = {
    GST_QUERY_TOTAL,
    GST_QUERY_POSITION,
    0
  };
  return gst_ogg_demux_src_query_types;
}

269 270 271 272 273 274 275 276 277 278 279 280 281 282
static GstOggPad *
gst_ogg_get_pad_by_pad (GstOggDemux *ogg, GstPad *pad)
{
  GSList *walk;
  GstOggPad *cur;
  
  for (walk = CURRENT_CHAIN (ogg)->pads; walk; walk = g_slist_next (walk)) {
    cur = (GstOggPad *) walk->data;
    if (cur->pad == pad)
      return cur;
  }
  return NULL;
}

283 284 285 286 287
static gboolean
gst_ogg_demux_src_query (GstPad *pad, GstQueryType type,
		       GstFormat *format, gint64 *value)
{
  gboolean res = FALSE;
288 289
  GstOggDemux *ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
  GstOggPad *cur = gst_ogg_get_pad_by_pad (ogg, pad);
290 291 292

  switch (type) {
    case GST_QUERY_TOTAL: {
293
      if (*format == GST_FORMAT_DEFAULT) {
294
	*value = cur->length;
295 296
	res = TRUE;
      }
297 298 299
      break;
    }
    case GST_QUERY_POSITION:
300
      if (*format == GST_FORMAT_DEFAULT && cur->length != 0) {
301
	*value = cur->known_offset;
302 303
	res = TRUE;
      }
304 305 306 307 308 309 310
      break;
    default:
      break;
  }
  return res;
}

311 312 313 314 315 316
/* The current seeking implementation is the most simple I could come up with:
 * - when seeking forwards, just discard data until desired position is reached
 * - when seeking backwards, seek to beginning and seek forward from there
 * Anyone is free to improve this algorithm as it is quite stupid and probably
 * really slow.
 */
317 318 319 320
static gboolean
gst_ogg_demux_src_event (GstPad *pad, GstEvent *event)
{
  GstOggDemux *ogg;
321
  GstOggPad *cur;
322 323

  ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
324 325 326
  cur = gst_ogg_get_pad_by_pad (ogg, pad);
  /* FIXME: optimize this so events from inactive chains work? 
   * in theory there shouldn't be an exisiting pad for inactive chains */
327
  if (cur == NULL) goto error;
328
  
329 330
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:
331 332 333
    {
      gint64 offset;
      if (GST_EVENT_SEEK_FORMAT (event) != GST_FORMAT_DEFAULT)
334
	goto error;
335 336 337 338
      offset = GST_EVENT_SEEK_OFFSET (event);
      switch (GST_EVENT_SEEK_METHOD (event)) {
	case GST_SEEK_METHOD_END:
	  if (cur->length == 0 || offset > 0)
339
	    goto error;
340 341 342 343 344 345 346 347 348
	  offset = cur->length + offset;
	  break;
	case GST_SEEK_METHOD_CUR:
	  offset += cur->known_offset;
	  break;
	case GST_SEEK_METHOD_SET:
	  break;
	default:
	  g_warning ("invalid seek method in seek event");
349
	  goto error;
350 351
      }
      if (offset < cur->known_offset) {
352
	GstEvent *restart = gst_event_new_seek (GST_FORMAT_BYTES | GST_SEEK_METHOD_SET | GST_EVENT_SEEK_FLAGS (event), 0);
353
	if (!gst_pad_send_event (GST_PAD_PEER (ogg->sinkpad), restart))
354 355 356 357 358 359
	  goto error;
      } else {
	FOR_PAD_IN_CURRENT_CHAIN (ogg, pad,
	  if (GST_PAD_IS_USABLE (pad->pad))
	    gst_pad_push (pad->pad, GST_DATA (gst_event_new (GST_EVENT_FLUSH)));
	);
360 361
      }

362
      GST_OGG_SET_STATE (ogg, GST_OGG_STATE_SEEK);
363 364 365
      FOR_PAD_IN_CURRENT_CHAIN (ogg, pad,
	pad->flags |= GST_OGG_PAD_NEEDS_DISCONT;
      );
366
      GST_DEBUG_OBJECT (ogg, "initiating seeking to offset %"G_GUINT64_FORMAT, offset);
367 368 369 370 371
      ogg->seek_pad = cur; 
      ogg->seek_to = offset;
      gst_event_unref (event);
      return TRUE;
    }
372
    default:
373
      return gst_pad_event_default (pad, event);
374 375
  }

376 377 378
  g_assert_not_reached ();
  
error:
379 380 381
  gst_event_unref (event);
  return FALSE;
}
382 383 384 385 386 387 388

static void
gst_ogg_start_playing (GstOggDemux *ogg)
{
  GST_DEBUG_OBJECT (ogg, "got EOS in setup, changing to playback now");
  if (!gst_pad_send_event (GST_PAD_PEER (ogg->sinkpad),
      gst_event_new_seek (GST_FORMAT_BYTES | GST_SEEK_METHOD_SET, 0))) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
389
    GST_ELEMENT_ERROR (ogg, CORE, SEEK, (NULL), 
390 391 392 393 394 395 396 397 398
	("cannot seek to start after EOS"));
  }
  ogg->current_chain = 0;
  GST_FLAG_UNSET (ogg, GST_OGG_FLAG_EOS);
  GST_FLAG_SET (ogg, GST_OGG_FLAG_WAIT_FOR_DISCONT);
  GST_OGG_SET_STATE (ogg, GST_OGG_STATE_PLAY);
  gst_ogg_print (ogg);
}

399 400 401 402 403 404 405
static void
gst_ogg_demux_handle_event (GstPad *pad, GstEvent *event)
{
  GstOggDemux *ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
  
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_DISCONTINUOUS:
406 407 408 409
      GST_DEBUG_OBJECT (ogg, "got a discont event");
      ogg_sync_reset (&ogg->sync);
      gst_event_unref (event);
      GST_FLAG_UNSET (ogg, GST_OGG_FLAG_WAIT_FOR_DISCONT);
410 411 412
      FOR_PAD_IN_CURRENT_CHAIN (ogg, pad,
	pad->flags |= GST_OGG_PAD_NEEDS_DISCONT;
      );
413 414
      break;
    case GST_EVENT_EOS:
415 416 417 418 419 420 421 422 423 424 425
      if (ogg->state == GST_OGG_STATE_SETUP) {
	gst_ogg_start_playing (ogg);
      } else {
	guint i;
	GSList *walk;
	GST_DEBUG_OBJECT (ogg, "got EOS");
	ogg->current_chain = -1;
	for (i = 0; i < ogg->chains->len; i++) {
	  GstOggChain *chain = &g_array_index (ogg->chains, GstOggChain, i);
	  for (walk = chain->pads; walk; walk = g_slist_next (walk)) {
	    GstOggPad *pad = (GstOggPad *) walk->data;
426 427
	    if (pad->pad && GST_PAD_IS_USABLE (pad->pad)) {
              gst_data_ref (GST_DATA (event));
428
	      gst_pad_push (pad->pad, GST_DATA (event));
429
            }
430 431 432
	  }
	}
	gst_element_set_eos (GST_ELEMENT (ogg));
433
      }
434
      gst_event_unref (event);
435 436 437 438 439 440 441
      break;
    default:
      gst_pad_event_default (pad, event);
      break;
  }
  return;
}
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

/* get the pad with the given serial in the current stream or NULL if none */
static GstOggPad *
gst_ogg_pad_get_in_current_chain (GstOggDemux *ogg, int serial)
{
  GSList *walk;
  
  if (ogg->current_chain == -1) return NULL;
  g_return_val_if_fail (ogg->current_chain < ogg->chains->len, NULL);

  for (walk = CURRENT_CHAIN (ogg)->pads; walk; walk = g_slist_next (walk)) {
    GstOggPad *pad = (GstOggPad *) walk->data;
    if (pad->serial == serial)
      return pad;
  }
  return NULL;
}

static void
gst_ogg_add_chain (GstOggDemux *ogg)
{
  GST_LOG_OBJECT (ogg, "adding chain %u", ogg->chains->len);
  ogg->current_chain = ogg->chains->len;
  g_array_set_size (ogg->chains, ogg->chains->len + 1);
}

468 469 470 471 472 473
static void
gst_ogg_demux_chain (GstPad *pad, GstData *buffer)
{
  GstOggDemux *ogg;
  guint8 *data;
  int pageout_ret = 1;
474
  guint64 offset_end;
475 476 477 478 479 480 481 482 483

  /* handle events */
  if (GST_IS_EVENT (buffer)) {
    gst_ogg_demux_handle_event (pad, GST_EVENT (buffer));
    return;
  }

  ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));

484 485 486 487 488 489 490
  if (GST_FLAG_IS_SET (ogg, GST_OGG_FLAG_WAIT_FOR_DISCONT)) {
    GST_LOG_OBJECT (ogg, "waiting for discont event, discarding buffer");
    gst_data_unref (buffer);
    return; 
  }

  GST_LOG_OBJECT (ogg, "queueing buffer %p with offset %llu", buffer, GST_BUFFER_OFFSET (buffer));
491 492 493 494
  data = (guint8 *) ogg_sync_buffer(&ogg->sync, GST_BUFFER_SIZE (buffer));
  memcpy (data, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
  if (ogg_sync_wrote (&ogg->sync, GST_BUFFER_SIZE (buffer)) != 0) {
    gst_data_unref (buffer);
495
    GST_ELEMENT_ERROR (ogg, LIBRARY, TOO_LAZY, (NULL), ("ogg_sync_wrote failed"));
496 497
    return;
  }
498 499 500
  offset_end = GST_BUFFER_OFFSET_IS_VALID (buffer) ?
      GST_BUFFER_OFFSET (buffer) + GST_BUFFER_SIZE (buffer) :
      (guint64) -1;
501 502 503 504 505 506 507
  gst_data_unref (buffer);
  while (pageout_ret != 0) {
    ogg_page page;
    
    pageout_ret = ogg_sync_pageout (&ogg->sync, &page);
    switch (pageout_ret) {
      case -1:
508 509
	/* FIXME: need some kind of discont here, we don't know any values to send though,
	 * we only have the END_OFFSET */
510 511
	break;
      case 0:
512 513 514 515 516 517 518 519 520 521
	if (ogg->state == GST_OGG_STATE_SETUP) {
	  guint64 length;
	  GstFormat format = GST_FORMAT_BYTES;
	  if (!gst_pad_query (GST_PAD_PEER (ogg->sinkpad), GST_QUERY_TOTAL, &format, &length))
	    length = 0;
	  if (length <= offset_end) {
	    gst_ogg_start_playing (ogg);
	    goto out;
	  }
	}
522 523
	break;
      case 1:
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
	GST_LOG_OBJECT (ogg, "processing ogg page (serial %d, packet %ld, granule pos %llu",
	    ogg_page_serialno (&page), ogg_page_pageno (&page), ogg_page_granulepos (&page));
	switch (ogg->state) {
	  case GST_OGG_STATE_SETUP:
	    if (ogg_page_eos (&page)) {
	      GstOggPad *cur = gst_ogg_pad_get_in_current_chain (ogg, ogg_page_serialno (&page));
	      GST_FLAG_SET (ogg, GST_OGG_FLAG_EOS);
	      if (!cur) {
		GST_ERROR_OBJECT (ogg, "unknown serial %d", ogg_page_serialno (&page));
	      } else {
		cur->pages = ogg_page_pageno (&page);
		cur->length = ogg_page_granulepos (&page);
	      }
	    } else {
	      if (GST_FLAG_IS_SET (ogg, GST_OGG_FLAG_EOS) && ogg_page_bos (&page)) {
		gst_ogg_add_chain (ogg);
	      }
	      GST_FLAG_UNSET (ogg, GST_OGG_FLAG_EOS);
	    }
	    if (ogg_page_bos (&page)) {
	      if (gst_ogg_pad_get_in_current_chain (ogg, ogg_page_serialno (&page))) {
		GST_ERROR_OBJECT (ogg, "multiple BOS page for serial %d (page %ld)",
		    ogg_page_serialno (&page), ogg_page_pageno (&page));
	      } else {
		GstOggPad *pad = gst_ogg_pad_new (ogg, ogg_page_serialno (&page));
		CURRENT_CHAIN (ogg)->pads = g_slist_prepend (CURRENT_CHAIN (ogg)->pads, pad);
	      }
	      GST_FLAG_SET (ogg, GST_OGG_FLAG_BOS);
	    } else {
	      GST_FLAG_UNSET (ogg, GST_OGG_FLAG_BOS);
	    }
	    break;
	  case GST_OGG_STATE_START:
	    if (gst_pad_send_event (GST_PAD_PEER (ogg->sinkpad),
		gst_event_new_seek (GST_FORMAT_BYTES | GST_SEEK_METHOD_END, 0))) {
	      GST_OGG_SET_STATE (ogg, GST_OGG_STATE_SETUP);
	      GST_DEBUG_OBJECT (ogg, "stream can seek, try setup now");
	      if (!gst_pad_send_event (GST_PAD_PEER (ogg->sinkpad),
		  gst_event_new_seek (GST_FORMAT_BYTES | GST_SEEK_METHOD_SET, 0))) {
563
		GST_ELEMENT_ERROR (ogg, CORE, SEEK, (NULL), 
564 565 566 567 568 569
		    ("stream can seek to end, but not to start. Can't handle that."));
	      }
	      gst_ogg_add_chain (ogg);
	      GST_FLAG_SET (ogg, GST_OGG_FLAG_WAIT_FOR_DISCONT);
	      goto out;
	    }
570
	    gst_ogg_add_chain (ogg);
571 572 573 574 575 576 577 578 579 580
	    GST_OGG_SET_STATE (ogg, GST_OGG_STATE_PLAY);
	    /* fall through */
	  case GST_OGG_STATE_SEEK:
	  case GST_OGG_STATE_PLAY:
	    gst_ogg_demux_push (ogg, &page);
	    break;
	  default:
	    g_assert_not_reached ();
	    break;
	}
581 582 583 584 585 586 587
	break;
      default:
	GST_WARNING_OBJECT (ogg, "unknown return value %d from ogg_sync_pageout", pageout_ret);
	pageout_ret = 0;
	break;
    }
  }
588
out:
589 590 591 592 593
  return;
}
static GstOggPad *
gst_ogg_pad_new (GstOggDemux *ogg, int serial)
{
594
  GstOggPad *ret = g_new0 (GstOggPad, 1);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

  ret->serial = serial;
  if (ogg_stream_init (&ret->stream, serial) != 0) {
    GST_ERROR_OBJECT (ogg, "Could not initialize ogg_stream struct for serial %d.", serial);
    g_free (ret);
    return NULL;
  }
  GST_LOG_OBJECT (ogg, "created new ogg src %p for stream with serial %d", ret, serial);

  return ret;
}
static void
gst_ogg_pad_remove (GstOggDemux *ogg, GstOggPad *pad)
{
  if (pad->pad) {
610 611 612 613 614 615
    /* FIXME:
     * we do it in the EOS signal already - EOS handling needs to be better thought out.
     * Correct way would be pushing EOS on eos page, but scheduler doesn't like that
    if (GST_PAD_IS_USEABLE (pad->pad))
      gst_pad_push (pad->pad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
    */
616 617 618 619 620 621 622 623 624 625
    gst_element_remove_pad (GST_ELEMENT (ogg), pad->pad);
  }
  if (ogg_stream_clear (&pad->stream) != 0)
    GST_ERROR_OBJECT (ogg, "ogg_stream_clear (serial %d) did not return 0, ignoring this error", pad->serial);
  GST_LOG_OBJECT (ogg, "free ogg src %p for stream with serial %d", pad, pad->serial);
  g_free (pad);
}
static void
gst_ogg_demux_push (GstOggDemux *ogg, ogg_page* page)
{
626
  GSList *walk;
627 628 629
  GstOggPad *cur;

  /* find the stream */
630
  for (walk = CURRENT_CHAIN (ogg)->pads; walk; walk = g_slist_next (walk)) {
631 632 633 634 635 636 637 638 639 640
    cur = (GstOggPad *) walk->data;
    if (cur->serial == ogg_page_serialno (page)) {
      goto br;
    }
  }
  cur = NULL;
br:
  /* now we either have a stream (cur) or not */
  if (ogg_page_bos (page)) {
    if (cur) {
641 642 643 644
      GST_DEBUG_OBJECT (ogg, "ogg page declared as BOS while stream %d already existed."
	  "Possibly a seek happened.", cur->serial);
    } else if (cur) {
      GST_DEBUG_OBJECT (ogg, "reactivating deactivated stream %d.", cur->serial); 
645 646 647 648
    } else {
      /* FIXME: monitor if we are still in creation stage? */
      cur = gst_ogg_pad_new (ogg, ogg_page_serialno (page));
      if (!cur) {
649
	GST_ELEMENT_ERROR (ogg, LIBRARY, TOO_LAZY, (NULL), ("Creating ogg_stream struct failed."));
650 651 652 653 654
	return;
      }
      if (ogg->current_chain == -1) {
	/* add new one at the end */
	gst_ogg_add_chain (ogg);
655
      }
656
      CURRENT_CHAIN (ogg)->pads = g_slist_prepend (CURRENT_CHAIN (ogg)->pads, cur);
657 658 659
    }
  }
  if (cur == NULL) {
660
    GST_ELEMENT_ERROR (ogg, STREAM, DECODE, (NULL), ("invalid ogg stream serial no"));
661 662 663 664 665 666 667
    return;
  }
  if (ogg_stream_pagein (&cur->stream, page) != 0) {
    GST_WARNING_OBJECT (ogg, "ogg stream choked on page (serial %d), resetting stream", cur->serial);
    gst_ogg_pad_reset (ogg, cur);
    return;
  }
668 669 670 671 672 673 674 675
  switch (ogg->state) {
    case GST_OGG_STATE_SEEK:
      GST_LOG_OBJECT (ogg, "in seek - offset now: %"G_GUINT64_FORMAT" (pad %d) - desired offset %"
	  G_GUINT64_FORMAT" (pad %d)", cur->known_offset, cur->serial, 
	  ogg->seek_to, ogg->seek_pad->serial);
      if (cur == ogg->seek_pad) {
	if (ogg_page_granulepos (page) > ogg->seek_to) {
	  GST_OGG_SET_STATE (ogg, GST_OGG_STATE_PLAY);
676
          GST_DEBUG_OBJECT (ogg, "ended seek at offset %"G_GUINT64_FORMAT" (requested  %"G_GUINT64_FORMAT, cur->known_offset, ogg->seek_to);
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
	  ogg->seek_pad = NULL;
	  ogg->seek_to = 0;
	}
      }
      /* fallthrough */
    case GST_OGG_STATE_PLAY:
      cur->known_offset = ogg_page_granulepos (page);
      gst_ogg_pad_push (ogg, cur);
      break;
    default:
      g_assert_not_reached ();
      break;
  }
  if (ogg_page_eos (page)) {
    GST_DEBUG_OBJECT (ogg, "got EOS for stream with serial %d, sending EOS now",
	cur->serial);
693 694 695 696
#if 0
  /* Removing pads while PLAYING doesn't work with current schedulers */
  /* remove from list, as this will never be called again */
    gst_ogg_pad_remove (ogg, cur);
697 698 699
  /* this is also not possible because sending EOS this way confuses the scheduler */
    gst_pad_push (cur->pad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
#else
700
#endif
701
  }
702 703 704 705 706 707
}
static void
gst_ogg_pad_push (GstOggDemux *ogg, GstOggPad *pad)
{
  ogg_packet packet;
  int ret;
708
  GstBuffer *buf;
709 710 711 712 713 714 715 716 717 718

  while (TRUE) {
    ret = ogg_stream_packetout (&pad->stream, &packet);
    switch (ret) {
      case 0:
	return;
      case -1:
	gst_ogg_pad_reset (ogg, pad);
	break;
      case 1: {
719
	/* only push data when playing, not during seek or similar */
720 721
	if (ogg->state != GST_OGG_STATE_PLAY)
	  continue;
722 723
	if (!pad->pad) {
	  GstCaps *caps = gst_ogg_type_find (&packet);
724
	  gchar *name = g_strdup_printf ("serial_%d", pad->serial);
725
	  
726
	  if (caps == NULL) {
727 728
	    GST_WARNING_OBJECT (ogg, "couldn't find caps for stream with serial %d", pad->serial);
	    caps = gst_caps_new_simple ("application/octet-stream", NULL);
729
	  }
David Schleef's avatar
David Schleef committed
730 731 732
	  pad->pad = gst_pad_new_from_template (
	      gst_static_pad_template_get (&ogg_demux_src_template_factory),
	      name);
733 734 735 736 737
	  g_free (name);
	  gst_pad_set_event_function (pad->pad, GST_DEBUG_FUNCPTR (gst_ogg_demux_src_event));
	  gst_pad_set_event_mask_function (pad->pad, GST_DEBUG_FUNCPTR (gst_ogg_demux_get_event_masks));
	  gst_pad_set_query_function (pad->pad, GST_DEBUG_FUNCPTR (gst_ogg_demux_src_query));
	  gst_pad_set_query_type_function (pad->pad, GST_DEBUG_FUNCPTR (gst_ogg_demux_get_query_types));
738 739
	  gst_pad_use_explicit_caps (pad->pad);
	  gst_pad_set_explicit_caps (pad->pad, caps);
740 741 742
	  gst_pad_set_active (pad->pad, TRUE);
	  gst_element_add_pad (GST_ELEMENT (ogg), pad->pad);
	}
743 744 745 746 747 748 749 750 751 752 753 754
	/* check for discont */
	if (packet.packetno != pad->packetno++) {
	  pad->flags |= GST_OGG_PAD_NEEDS_DISCONT;
	  pad->packetno = packet.packetno + 1;
	}
	/* send discont if needed */
	if ((pad->flags & GST_OGG_PAD_NEEDS_DISCONT) && GST_PAD_IS_USABLE (pad->pad)) {
	  GstEvent *event = gst_event_new_discontinuous (FALSE, 
	    GST_FORMAT_DEFAULT, pad->known_offset); /* FIXME: this might be wrong because we can only use the last known offset */
	  gst_pad_push (pad->pad, GST_DATA (event));
	  pad->flags &= (~GST_OGG_PAD_NEEDS_DISCONT);
	};
755 756
	/* optimization: use a bufferpool containing the ogg packet? */
	buf = gst_pad_alloc_buffer (pad->pad, GST_BUFFER_OFFSET_NONE, packet.bytes);
757
	memcpy (buf->data, packet.packet, packet.bytes);
758 759 760 761 762 763 764
	if (pad->offset != -1)
	  GST_BUFFER_OFFSET (buf) = pad->offset;
	if (packet.granulepos != -1)
	  GST_BUFFER_OFFSET_END (buf) = packet.granulepos;
	pad->offset = packet.granulepos;
	if (GST_PAD_IS_USABLE (pad->pad))
	  gst_pad_push (pad->pad, GST_DATA (buf));
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
	break;
      }
      default:
	GST_ERROR_OBJECT (ogg, "invalid return value %d for ogg_stream_packetout, resetting stream", ret);
	gst_ogg_pad_reset (ogg, pad);
	break;
    }
  }
}
static void
gst_ogg_pad_reset (GstOggDemux *ogg, GstOggPad *pad)
{
  ogg_stream_reset (&pad->stream);
  pad->offset = GST_BUFFER_OFFSET_NONE;
  /* FIXME: need a discont here */
}
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798

static void
gst_ogg_chains_clear (GstOggDemux *ogg)
{
  gint i;
  GSList *walk;
  
  for (i = ogg->chains->len - 1; i >= 0; i--) {
    GstOggChain *cur = &g_array_index (ogg->chains, GstOggChain, i);
    for (walk = cur->pads; walk; walk = g_slist_next (walk)) {
      gst_ogg_pad_remove (ogg, (GstOggPad *) walk->data);
    }
    g_slist_free (cur->pads);
    g_array_remove_index (ogg->chains, i);
  }
  ogg->current_chain = -1;
}

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
static GstElementStateReturn
gst_ogg_demux_change_state (GstElement *element)
{
  GstOggDemux *ogg;

  ogg = GST_OGG_DEMUX (element);

  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_NULL_TO_READY:
      ogg_sync_init (&ogg->sync);
      break;
    case GST_STATE_READY_TO_PAUSED:
      ogg_sync_reset (&ogg->sync);
      break;
    case GST_STATE_PAUSED_TO_PLAYING:
      break;
    case GST_STATE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_PAUSED_TO_READY:
818 819 820 821
      gst_ogg_chains_clear (ogg);
      GST_OGG_SET_STATE (ogg, GST_OGG_STATE_START);
      ogg->seek_pad = NULL;
      ogg->seek_to = 0;
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
      break;
    case GST_STATE_READY_TO_NULL:
      ogg_sync_clear (&ogg->sync);
      break;
    default:
      g_assert_not_reached ();
      break;
  }

  return parent_class->change_state (element);
}

/*** typefinding **************************************************************/
/* ogg supports its own typefinding because the ogg spec defines that the first
 * packet of an ogg stream must identify the stream. Therefore ogg can use a
 * simplified approach at typefinding.
 */
typedef struct {
  ogg_packet *	packet;
  guint		best_probability;
  GstCaps *	caps;
} OggTypeFind;
static guint8 *
ogg_find_peek (gpointer data, gint64 offset, guint size)
{
  OggTypeFind *find = (OggTypeFind *) data;
  
  if (offset + size <= find->packet->bytes) {
    return ((guint8 *) find->packet->packet) + offset;
  } else {
    return NULL;
  }
}
static void
David Schleef's avatar
David Schleef committed
856
ogg_find_suggest (gpointer data, guint probability, const GstCaps *caps)
857 858 859 860
{
  OggTypeFind *find = (OggTypeFind *) data;
  
  if (probability > find->best_probability) {
David Schleef's avatar
David Schleef committed
861
    gst_caps_replace (&find->caps, gst_caps_copy (caps));
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    find->best_probability = probability;
  }
}
static GstCaps *
gst_ogg_type_find (ogg_packet *packet)
{
  GstTypeFind gst_find;
  OggTypeFind find;
  GList *walk, *type_list = NULL;
  
  walk = type_list = gst_type_find_factory_get_list ();
  
  find.packet = packet;
  find.best_probability = 0;
  find.caps = NULL;
  gst_find.data = &find;
  gst_find.peek = ogg_find_peek;
  gst_find.suggest = ogg_find_suggest;
  
  while (walk) {
    GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (walk->data);
    
    gst_type_find_factory_call_function (factory, &gst_find);
    if (find.best_probability >= GST_TYPE_FIND_MAXIMUM)
      break;
    walk = g_list_next (walk);
  }
  
  if (find.best_probability > 0)
    return find.caps;

  return NULL;
}

static gboolean
plugin_init (GstPlugin *plugin)
{
  GST_DEBUG_CATEGORY_INIT (gst_ogg_demux_debug, "oggdemux", 0, "ogg demuxer");

  return gst_element_register (plugin, "oggdemux", GST_RANK_PRIMARY, GST_TYPE_OGG_DEMUX);
}
GST_PLUGIN_DEFINE (
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
  "ogg",
  "ogg stream manipulation (info about ogg: http://xiph.org)",
  plugin_init,
  VERSION,
  GST_LICENSE,
  GST_PACKAGE,
  GST_ORIGIN
)
914 915 916 917 918 919 920 921 922 923

/* prints all info about the element */
static void
gst_ogg_print (GstOggDemux *ogg)
{
  guint i;
  GSList *walk;
  
  for (i = 0; i < ogg->chains->len; i++) {
    GstOggChain *chain = &g_array_index (ogg->chains, GstOggChain, i);
924
    GST_INFO_OBJECT (ogg, "chain %d (%u streams):", i, g_slist_length (chain->pads));
925 926
    for (walk = chain->pads; walk; walk = g_slist_next (walk)) {
      GstOggPad *pad = (GstOggPad *) walk->data;
927 928 929
      GST_INFO_OBJECT (ogg, "  stream %d:", pad->serial);
      GST_INFO_OBJECT (ogg, "    length %"G_GUINT64_FORMAT, pad->length);
      GST_INFO_OBJECT (ogg, "    pages %ld", pad->pages);
930 931 932
    }
  }
}