0cc6ab0f0528ad561d7adee8284d1f2fd8295de6
[platform/upstream/gstreamer.git] / ext / ogg / gstoggmux.c
1 /* OGG muxer plugin for GStreamer
2  * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3  * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:element-oggmux
23  * @see_also: <link linkend="gst-plugins-base-plugins-oggdemux">oggdemux</link>
24  *
25  * This element merges streams (audio and video) into ogg files.
26  *
27  * <refsect2>
28  * <title>Example pipelines</title>
29  * |[
30  * gst-launch v4l2src num-buffers=500 ! video/x-raw-yuv,width=320,height=240 ! ffmpegcolorspace ! theoraenc ! oggmux ! filesink location=video.ogg
31  * ]| Encodes a video stream captured from a v4l2-compatible camera to Ogg/Theora
32  * (the encoding will stop automatically after 500 frames)
33  * </refsect2>
34  *
35  * Last reviewed on 2008-02-06 (0.10.17)
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <gst/gst.h>
43 #include <gst/base/gstcollectpads.h>
44 #include <gst/tag/tag.h>
45
46 #include "gstoggmux.h"
47
48 /* memcpy - if someone knows a way to get rid of it, please speak up
49  * note: the ogg docs even say you need this... */
50 #include <string.h>
51 #include <time.h>
52 #include <stdlib.h>             /* rand, srand, atoi */
53
54 GST_DEBUG_CATEGORY_STATIC (gst_ogg_mux_debug);
55 #define GST_CAT_DEFAULT gst_ogg_mux_debug
56
57 /* This isn't generally what you'd want with an end-time macro, because
58    technically the end time of a buffer with invalid duration is invalid. But
59    for sorting ogg pages this is what we want. */
60 #define GST_BUFFER_END_TIME(buf) \
61     (GST_BUFFER_DURATION_IS_VALID (buf) \
62     ? GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf) \
63     : GST_BUFFER_TIMESTAMP (buf))
64
65 #define GST_GP_FORMAT "[gp %8" G_GINT64_FORMAT "]"
66 #define GST_GP_CAST(_gp) ((gint64) _gp)
67
68 typedef enum
69 {
70   GST_OGG_FLAG_BOS = GST_ELEMENT_FLAG_LAST,
71   GST_OGG_FLAG_EOS
72 }
73 GstOggFlag;
74
75 /* OggMux signals and args */
76 enum
77 {
78   /* FILL ME */
79   LAST_SIGNAL
80 };
81
82 /* set to 0.5 seconds by default */
83 #define DEFAULT_MAX_DELAY       G_GINT64_CONSTANT(500000000)
84 #define DEFAULT_MAX_PAGE_DELAY  G_GINT64_CONSTANT(500000000)
85 #define DEFAULT_MAX_TOLERANCE   G_GINT64_CONSTANT(40000000)
86 enum
87 {
88   ARG_0,
89   ARG_MAX_DELAY,
90   ARG_MAX_PAGE_DELAY,
91   ARG_MAX_TOLERANCE
92 };
93
94 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
95     GST_PAD_SRC,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS ("application/ogg")
98     );
99
100 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
101     GST_PAD_SINK,
102     GST_PAD_REQUEST,
103     GST_STATIC_CAPS ("video/x-theora; "
104         "audio/x-vorbis; audio/x-flac; audio/x-speex; audio/x-celt; "
105         "application/x-ogm-video; application/x-ogm-audio; video/x-dirac; "
106         "video/x-smoke; video/x-vp8; text/x-cmml, encoded = (boolean) TRUE; "
107         "subtitle/x-kate; application/x-kate")
108     );
109
110 static void gst_ogg_mux_finalize (GObject * object);
111
112 static GstFlowReturn
113 gst_ogg_mux_collected (GstCollectPads * pads, GstOggMux * ogg_mux);
114 static gboolean gst_ogg_mux_handle_src_event (GstPad * pad, GstEvent * event);
115 static GstPad *gst_ogg_mux_request_new_pad (GstElement * element,
116     GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
117 static void gst_ogg_mux_release_pad (GstElement * element, GstPad * pad);
118
119 static void gst_ogg_mux_set_property (GObject * object,
120     guint prop_id, const GValue * value, GParamSpec * pspec);
121 static void gst_ogg_mux_get_property (GObject * object,
122     guint prop_id, GValue * value, GParamSpec * pspec);
123 static GstStateChangeReturn gst_ogg_mux_change_state (GstElement * element,
124     GstStateChange transition);
125
126 /*static guint gst_ogg_mux_signals[LAST_SIGNAL] = { 0 }; */
127 #define gst_ogg_mux_parent_class parent_class
128 G_DEFINE_TYPE_WITH_CODE (GstOggMux, gst_ogg_mux, GST_TYPE_ELEMENT,
129     G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL));
130
131 static void
132 gst_ogg_mux_class_init (GstOggMuxClass * klass)
133 {
134   GObjectClass *gobject_class;
135   GstElementClass *gstelement_class;
136
137   gobject_class = (GObjectClass *) klass;
138   gstelement_class = (GstElementClass *) klass;
139
140   gobject_class->finalize = gst_ogg_mux_finalize;
141   gobject_class->get_property = gst_ogg_mux_get_property;
142   gobject_class->set_property = gst_ogg_mux_set_property;
143
144   gst_element_class_add_pad_template (gstelement_class,
145       gst_static_pad_template_get (&src_factory));
146   gst_element_class_add_pad_template (gstelement_class,
147       gst_static_pad_template_get (&sink_factory));
148
149   gst_element_class_set_details_simple (gstelement_class,
150       "Ogg muxer", "Codec/Muxer",
151       "mux ogg streams (info about ogg: http://xiph.org)",
152       "Wim Taymans <wim@fluendo.com>");
153
154   gstelement_class->request_new_pad = gst_ogg_mux_request_new_pad;
155   gstelement_class->release_pad = gst_ogg_mux_release_pad;
156
157   g_object_class_install_property (gobject_class, ARG_MAX_DELAY,
158       g_param_spec_uint64 ("max-delay", "Max delay",
159           "Maximum delay in multiplexing streams", 0, G_MAXUINT64,
160           DEFAULT_MAX_DELAY,
161           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162   g_object_class_install_property (gobject_class, ARG_MAX_PAGE_DELAY,
163       g_param_spec_uint64 ("max-page-delay", "Max page delay",
164           "Maximum delay for sending out a page", 0, G_MAXUINT64,
165           DEFAULT_MAX_PAGE_DELAY,
166           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167   g_object_class_install_property (gobject_class, ARG_MAX_TOLERANCE,
168       g_param_spec_uint64 ("max-tolerance", "Max time tolerance",
169           "Maximum timestamp difference for maintaining perfect granules",
170           0, G_MAXUINT64, DEFAULT_MAX_TOLERANCE,
171           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172
173   gstelement_class->change_state = gst_ogg_mux_change_state;
174
175 }
176
177 #if 0
178 static const GstEventMask *
179 gst_ogg_mux_get_sink_event_masks (GstPad * pad)
180 {
181   static const GstEventMask gst_ogg_mux_sink_event_masks[] = {
182     {GST_EVENT_EOS, 0},
183     {GST_EVENT_DISCONTINUOUS, 0},
184     {0,}
185   };
186
187   return gst_ogg_mux_sink_event_masks;
188 }
189 #endif
190
191 static void
192 gst_ogg_mux_clear (GstOggMux * ogg_mux)
193 {
194   ogg_mux->pulling = NULL;
195   ogg_mux->need_headers = TRUE;
196   ogg_mux->delta_pad = NULL;
197   ogg_mux->offset = 0;
198   ogg_mux->next_ts = 0;
199   ogg_mux->last_ts = GST_CLOCK_TIME_NONE;
200 }
201
202 static void
203 gst_ogg_mux_init (GstOggMux * ogg_mux)
204 {
205   GstElementClass *klass = GST_ELEMENT_GET_CLASS (ogg_mux);
206
207   ogg_mux->srcpad =
208       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
209           "src"), "src");
210   gst_pad_set_event_function (ogg_mux->srcpad, gst_ogg_mux_handle_src_event);
211   gst_element_add_pad (GST_ELEMENT (ogg_mux), ogg_mux->srcpad);
212
213   GST_OBJECT_FLAG_SET (GST_ELEMENT (ogg_mux), GST_OGG_FLAG_BOS);
214
215   /* seed random number generator for creation of serial numbers */
216   srand (time (NULL));
217
218   ogg_mux->collect = gst_collect_pads_new ();
219   gst_collect_pads_set_function (ogg_mux->collect,
220       (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_ogg_mux_collected),
221       ogg_mux);
222
223   ogg_mux->max_delay = DEFAULT_MAX_DELAY;
224   ogg_mux->max_page_delay = DEFAULT_MAX_PAGE_DELAY;
225   ogg_mux->max_tolerance = DEFAULT_MAX_TOLERANCE;
226
227   gst_ogg_mux_clear (ogg_mux);
228 }
229
230 static void
231 gst_ogg_mux_finalize (GObject * object)
232 {
233   GstOggMux *ogg_mux;
234
235   ogg_mux = GST_OGG_MUX (object);
236
237   if (ogg_mux->collect) {
238     gst_object_unref (ogg_mux->collect);
239     ogg_mux->collect = NULL;
240   }
241
242   G_OBJECT_CLASS (parent_class)->finalize (object);
243 }
244
245 static void
246 gst_ogg_mux_ogg_pad_destroy_notify (GstCollectData * data)
247 {
248   GstOggPadData *oggpad = (GstOggPadData *) data;
249   GstBuffer *buf;
250
251   ogg_stream_clear (&oggpad->map.stream);
252   gst_caps_replace (&oggpad->map.caps, NULL);
253
254   if (oggpad->pagebuffers) {
255     while ((buf = g_queue_pop_head (oggpad->pagebuffers)) != NULL) {
256       gst_buffer_unref (buf);
257     }
258     g_queue_free (oggpad->pagebuffers);
259     oggpad->pagebuffers = NULL;
260   }
261 }
262
263 static GstPadLinkReturn
264 gst_ogg_mux_sinkconnect (GstPad * pad, GstPad * peer)
265 {
266   GstOggMux *ogg_mux;
267
268   ogg_mux = GST_OGG_MUX (gst_pad_get_parent (pad));
269
270   GST_DEBUG_OBJECT (ogg_mux, "sinkconnect triggered on %s", GST_PAD_NAME (pad));
271
272   gst_object_unref (ogg_mux);
273
274   return GST_PAD_LINK_OK;
275 }
276
277 static gboolean
278 gst_ogg_mux_sink_event (GstPad * pad, GstEvent * event)
279 {
280   GstOggMux *ogg_mux = GST_OGG_MUX (gst_pad_get_parent (pad));
281   GstOggPadData *ogg_pad = (GstOggPadData *) gst_pad_get_element_private (pad);
282   gboolean ret = FALSE;
283
284   GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
285
286   switch (GST_EVENT_TYPE (event)) {
287     case GST_EVENT_SEGMENT:
288     {
289       const GstSegment *segment;
290
291       gst_event_parse_segment (event, &segment);
292
293       /* We don't support non time NEWSEGMENT events */
294       if (segment->format != GST_FORMAT_TIME) {
295         gst_event_unref (event);
296         event = NULL;
297         break;
298       }
299
300       gst_segment_copy_into (segment, &ogg_pad->segment);
301       break;
302     }
303     case GST_EVENT_FLUSH_STOP:{
304       gst_segment_init (&ogg_pad->segment, GST_FORMAT_TIME);
305       break;
306     }
307     default:
308       break;
309   }
310
311   /* now GstCollectPads can take care of the rest, e.g. EOS */
312   if (event != NULL)
313     ret = ogg_pad->collect_event (pad, event);
314
315   gst_object_unref (ogg_mux);
316   return ret;
317 }
318
319 static gboolean
320 gst_ogg_mux_is_serialno_present (GstOggMux * ogg_mux, guint32 serialno)
321 {
322   GSList *walk;
323
324   walk = ogg_mux->collect->data;
325   while (walk) {
326     GstOggPadData *pad = (GstOggPadData *) walk->data;
327     if (pad->map.serialno == serialno)
328       return TRUE;
329     walk = walk->next;
330   }
331
332   return FALSE;
333 }
334
335 static guint32
336 gst_ogg_mux_generate_serialno (GstOggMux * ogg_mux)
337 {
338   guint32 serialno;
339
340   do {
341     serialno = g_random_int_range (0, G_MAXINT32);
342   } while (gst_ogg_mux_is_serialno_present (ogg_mux, serialno));
343
344   return serialno;
345 }
346
347 static GstPad *
348 gst_ogg_mux_request_new_pad (GstElement * element,
349     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
350 {
351   GstOggMux *ogg_mux;
352   GstPad *newpad;
353   GstElementClass *klass;
354
355   g_return_val_if_fail (templ != NULL, NULL);
356
357   if (templ->direction != GST_PAD_SINK)
358     goto wrong_direction;
359
360   g_return_val_if_fail (GST_IS_OGG_MUX (element), NULL);
361   ogg_mux = GST_OGG_MUX (element);
362
363   klass = GST_ELEMENT_GET_CLASS (element);
364
365   if (templ != gst_element_class_get_pad_template (klass, "sink_%d"))
366     goto wrong_template;
367
368   {
369     gint serial;
370     gchar *name;
371
372     if (req_name == NULL || strlen (req_name) < 6) {
373       /* no name given when requesting the pad, use random serial number */
374       serial = gst_ogg_mux_generate_serialno (ogg_mux);
375     } else {
376       /* parse serial number from requested padname */
377       serial = atoi (&req_name[5]);
378     }
379     /* create new pad with the name */
380     GST_DEBUG_OBJECT (ogg_mux, "Creating new pad for serial %d", serial);
381     name = g_strdup_printf ("sink_%d", serial);
382     newpad = gst_pad_new_from_template (templ, name);
383     g_free (name);
384
385     /* construct our own wrapper data structure for the pad to
386      * keep track of its status */
387     {
388       GstOggPadData *oggpad;
389
390       oggpad = (GstOggPadData *)
391           gst_collect_pads_add_pad_full (ogg_mux->collect, newpad,
392           sizeof (GstOggPadData), gst_ogg_mux_ogg_pad_destroy_notify);
393       ogg_mux->active_pads++;
394
395       oggpad->map.serialno = serial;
396       ogg_stream_init (&oggpad->map.stream, oggpad->map.serialno);
397       oggpad->packetno = 0;
398       oggpad->pageno = 0;
399       oggpad->eos = FALSE;
400       /* we assume there will be some control data first for this pad */
401       oggpad->state = GST_OGG_PAD_STATE_CONTROL;
402       oggpad->new_page = TRUE;
403       oggpad->first_delta = FALSE;
404       oggpad->prev_delta = FALSE;
405       oggpad->data_pushed = FALSE;
406       oggpad->pagebuffers = g_queue_new ();
407       oggpad->map.headers = NULL;
408       oggpad->map.queued = NULL;
409       oggpad->next_granule = 0;
410       oggpad->keyframe_granule = -1;
411
412       gst_segment_init (&oggpad->segment, GST_FORMAT_TIME);
413
414       oggpad->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
415       gst_pad_set_event_function (newpad,
416           GST_DEBUG_FUNCPTR (gst_ogg_mux_sink_event));
417     }
418   }
419
420   /* setup some pad functions */
421   gst_pad_set_link_function (newpad, gst_ogg_mux_sinkconnect);
422
423   /* dd the pad to the element */
424   gst_element_add_pad (element, newpad);
425
426   return newpad;
427
428   /* ERRORS */
429 wrong_direction:
430   {
431     g_warning ("ogg_mux: request pad that is not a SINK pad\n");
432     return NULL;
433   }
434 wrong_template:
435   {
436     g_warning ("ogg_mux: this is not our template!\n");
437     return NULL;
438   }
439 }
440
441 static void
442 gst_ogg_mux_release_pad (GstElement * element, GstPad * pad)
443 {
444   GstOggMux *ogg_mux;
445
446   ogg_mux = GST_OGG_MUX (gst_pad_get_parent (pad));
447
448   gst_collect_pads_remove_pad (ogg_mux->collect, pad);
449   gst_element_remove_pad (element, pad);
450
451   gst_object_unref (ogg_mux);
452 }
453
454 /* handle events */
455 static gboolean
456 gst_ogg_mux_handle_src_event (GstPad * pad, GstEvent * event)
457 {
458   GstEventType type;
459
460   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
461
462   switch (type) {
463     case GST_EVENT_SEEK:
464       /* disable seeking for now */
465       return FALSE;
466     default:
467       break;
468   }
469
470   return gst_pad_event_default (pad, event);
471 }
472
473 static GstBuffer *
474 gst_ogg_mux_buffer_from_page (GstOggMux * mux, ogg_page * page, gboolean delta)
475 {
476   GstBuffer *buffer;
477
478   /* allocate space for header and body */
479   buffer = gst_buffer_new_and_alloc (page->header_len + page->body_len);
480   gst_buffer_fill (buffer, 0, page->header, page->header_len);
481   gst_buffer_fill (buffer, page->header_len, page->body, page->body_len);
482
483   /* Here we set granulepos as our OFFSET_END to give easy direct access to
484    * this value later. Before we push it, we reset this to OFFSET + SIZE
485    * (see gst_ogg_mux_push_buffer). */
486   GST_BUFFER_OFFSET_END (buffer) = ogg_page_granulepos (page);
487   if (delta)
488     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
489
490   GST_LOG_OBJECT (mux, GST_GP_FORMAT
491       " created buffer %p from ogg page",
492       GST_GP_CAST (ogg_page_granulepos (page)), buffer);
493
494   return buffer;
495 }
496
497 static GstFlowReturn
498 gst_ogg_mux_push_buffer (GstOggMux * mux, GstBuffer * buffer,
499     GstOggPadData * oggpad)
500 {
501   /* fix up OFFSET and OFFSET_END again */
502   GST_BUFFER_OFFSET (buffer) = mux->offset;
503   mux->offset += gst_buffer_get_size (buffer);
504   GST_BUFFER_OFFSET_END (buffer) = mux->offset;
505
506   /* Ensure we have monotonically increasing timestamps in the output. */
507   if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
508     gint64 run_time = GST_BUFFER_TIMESTAMP (buffer);
509     if (mux->last_ts != GST_CLOCK_TIME_NONE && run_time < mux->last_ts)
510       GST_BUFFER_TIMESTAMP (buffer) = mux->last_ts;
511     else
512       mux->last_ts = run_time;
513   }
514
515   return gst_pad_push (mux->srcpad, buffer);
516 }
517
518 /* if all queues have at least one page, dequeue the page with the lowest
519  * timestamp */
520 static gboolean
521 gst_ogg_mux_dequeue_page (GstOggMux * mux, GstFlowReturn * flowret)
522 {
523   GSList *walk;
524   GstOggPadData *opad = NULL;   /* "oldest" pad */
525   GstClockTime oldest = GST_CLOCK_TIME_NONE;
526   GstBuffer *buf = NULL;
527   gboolean ret = FALSE;
528
529   *flowret = GST_FLOW_OK;
530
531   walk = mux->collect->data;
532   while (walk) {
533     GstOggPadData *pad = (GstOggPadData *) walk->data;
534
535     /* We need each queue to either be at EOS, or have one or more pages
536      * available with a set granulepos (i.e. not -1), otherwise we don't have
537      * enough data yet to determine which stream needs to go next for correct
538      * time ordering. */
539     if (pad->pagebuffers->length == 0) {
540       if (pad->eos) {
541         GST_LOG_OBJECT (pad->collect.pad,
542             "pad is EOS, skipping for dequeue decision");
543       } else {
544         GST_LOG_OBJECT (pad->collect.pad,
545             "no pages in this queue, can't dequeue");
546         return FALSE;
547       }
548     } else {
549       /* We then need to check for a non-negative granulepos */
550       int i;
551       gboolean valid = FALSE;
552
553       for (i = 0; i < pad->pagebuffers->length; i++) {
554         buf = g_queue_peek_nth (pad->pagebuffers, i);
555         /* Here we check the OFFSET_END, which is actually temporarily the
556          * granulepos value for this buffer */
557         if (GST_BUFFER_OFFSET_END (buf) != -1) {
558           valid = TRUE;
559           break;
560         }
561       }
562       if (!valid) {
563         GST_LOG_OBJECT (pad->collect.pad,
564             "No page timestamps in queue, can't dequeue");
565         return FALSE;
566       }
567     }
568
569     walk = g_slist_next (walk);
570   }
571
572   walk = mux->collect->data;
573   while (walk) {
574     GstOggPadData *pad = (GstOggPadData *) walk->data;
575
576     /* any page with a granulepos of -1 can be pushed immediately.
577      * TODO: it CAN be, but it seems silly to do so? */
578     buf = g_queue_peek_head (pad->pagebuffers);
579     while (buf && GST_BUFFER_OFFSET_END (buf) == -1) {
580       GST_LOG_OBJECT (pad->collect.pad, "[gp        -1] pushing page");
581       g_queue_pop_head (pad->pagebuffers);
582       *flowret = gst_ogg_mux_push_buffer (mux, buf, pad);
583       buf = g_queue_peek_head (pad->pagebuffers);
584       ret = TRUE;
585     }
586
587     if (buf) {
588       /* if no oldest buffer yet, take this one */
589       if (oldest == GST_CLOCK_TIME_NONE) {
590         GST_LOG_OBJECT (mux, "no oldest yet, taking buffer %p from pad %"
591             GST_PTR_FORMAT " with gp time %" GST_TIME_FORMAT,
592             buf, pad->collect.pad, GST_TIME_ARGS (GST_BUFFER_OFFSET (buf)));
593         oldest = GST_BUFFER_OFFSET (buf);
594         opad = pad;
595       } else {
596         /* if we have an oldest, compare with this one */
597         if (GST_BUFFER_OFFSET (buf) < oldest) {
598           GST_LOG_OBJECT (mux, "older buffer %p, taking from pad %"
599               GST_PTR_FORMAT " with gp time %" GST_TIME_FORMAT,
600               buf, pad->collect.pad, GST_TIME_ARGS (GST_BUFFER_OFFSET (buf)));
601           oldest = GST_BUFFER_OFFSET (buf);
602           opad = pad;
603         }
604       }
605     }
606     walk = g_slist_next (walk);
607   }
608
609   if (oldest != GST_CLOCK_TIME_NONE) {
610     g_assert (opad);
611     buf = g_queue_pop_head (opad->pagebuffers);
612     GST_LOG_OBJECT (opad->collect.pad,
613         GST_GP_FORMAT " pushing oldest page buffer %p (granulepos time %"
614         GST_TIME_FORMAT ")", GST_BUFFER_OFFSET_END (buf), buf,
615         GST_TIME_ARGS (GST_BUFFER_OFFSET (buf)));
616     *flowret = gst_ogg_mux_push_buffer (mux, buf, opad);
617     ret = TRUE;
618   }
619
620   return ret;
621 }
622
623 /* put the given ogg page on a per-pad queue, timestamping it correctly.
624  * after that, dequeue and push as many pages as possible.
625  * Caller should make sure:
626  * pad->timestamp     was set with the timestamp of the first packet put
627  *                    on the page
628  * pad->timestamp_end was set with the timestamp + duration of the last packet
629  *                    put on the page
630  * pad->gp_time       was set with the time matching the gp of the last
631  *                    packet put on the page
632  *
633  * will also reset timestamp and timestamp_end, so caller func can restart
634  * counting.
635  */
636 static GstFlowReturn
637 gst_ogg_mux_pad_queue_page (GstOggMux * mux, GstOggPadData * pad,
638     ogg_page * page, gboolean delta)
639 {
640   GstFlowReturn ret;
641   GstBuffer *buffer = gst_ogg_mux_buffer_from_page (mux, page, delta);
642
643   /* take the timestamp of the first packet on this page */
644   GST_BUFFER_TIMESTAMP (buffer) = pad->timestamp;
645   GST_BUFFER_DURATION (buffer) = pad->timestamp_end - pad->timestamp;
646   /* take the gp time of the last completed packet on this page */
647   GST_BUFFER_OFFSET (buffer) = pad->gp_time;
648
649   /* the next page will start where the current page's end time leaves off */
650   pad->timestamp = pad->timestamp_end;
651
652   g_queue_push_tail (pad->pagebuffers, buffer);
653   GST_LOG_OBJECT (pad->collect.pad, GST_GP_FORMAT
654       " queued buffer page %p (gp time %"
655       GST_TIME_FORMAT ", timestamp %" GST_TIME_FORMAT
656       "), %d page buffers queued", GST_GP_CAST (ogg_page_granulepos (page)),
657       buffer, GST_TIME_ARGS (GST_BUFFER_OFFSET (buffer)),
658       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
659       g_queue_get_length (pad->pagebuffers));
660
661   while (gst_ogg_mux_dequeue_page (mux, &ret)) {
662     if (ret != GST_FLOW_OK)
663       break;
664   }
665
666   return ret;
667 }
668
669 /*
670  * Given two pads, compare the buffers queued on it.
671  * Returns:
672  *  0 if they have an equal priority
673  * -1 if the first is better
674  *  1 if the second is better
675  * Priority decided by: a) validity, b) older timestamp, c) smaller number
676  * of muxed pages
677  */
678 static gint
679 gst_ogg_mux_compare_pads (GstOggMux * ogg_mux, GstOggPadData * first,
680     GstOggPadData * second)
681 {
682   guint64 firsttime, secondtime;
683
684   /* if the first pad doesn't contain anything or is even NULL, return
685    * the second pad as best candidate and vice versa */
686   if (first == NULL)
687     return 1;
688   if (second == NULL)
689     return -1;
690
691   /* no timestamp on first buffer, it must go first */
692   firsttime = GST_BUFFER_TIMESTAMP (first->buffer);
693   if (firsttime == GST_CLOCK_TIME_NONE)
694     return -1;
695
696   /* no timestamp on second buffer, it must go first */
697   secondtime = GST_BUFFER_TIMESTAMP (second->buffer);
698   if (secondtime == GST_CLOCK_TIME_NONE)
699     return 1;
700
701   /* first buffer has higher timestamp, second one should go first */
702   if (secondtime < firsttime)
703     return 1;
704   /* second buffer has higher timestamp, first one should go first */
705   else if (secondtime > firsttime)
706     return -1;
707   else {
708     /* buffers with equal timestamps, prefer the pad that has the
709      * least number of pages muxed */
710     if (second->pageno < first->pageno)
711       return 1;
712     else if (second->pageno > first->pageno)
713       return -1;
714   }
715
716   /* same priority if all of the above failed */
717   return 0;
718 }
719
720 static GstBuffer *
721 gst_ogg_mux_decorate_buffer (GstOggMux * ogg_mux, GstOggPadData * pad,
722     GstBuffer * buf)
723 {
724   GstClockTime time;
725   gint64 duration, granule, limit;
726   GstClockTime next_time;
727   GstClockTimeDiff diff;
728   ogg_packet packet;
729   gsize size;
730
731   /* ensure messing with metadata is ok */
732   buf = gst_buffer_make_writable (buf);
733
734   /* convert time to running time, so we need no longer bother about that */
735   time = GST_BUFFER_TIMESTAMP (buf);
736   if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
737     time = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME, time);
738     if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
739       gst_buffer_unref (buf);
740       return NULL;
741     } else {
742       GST_BUFFER_TIMESTAMP (buf) = time;
743     }
744   }
745
746   /* now come up with granulepos stuff corresponding to time */
747   if (!pad->have_type ||
748       pad->map.granulerate_n <= 0 || pad->map.granulerate_d <= 0)
749     goto no_granule;
750
751   packet.packet = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
752   packet.bytes = size;
753   duration = gst_ogg_stream_get_packet_duration (&pad->map, &packet);
754   gst_buffer_unmap (buf, packet.packet, size);
755
756   /* give up if no duration can be determined, relying on upstream */
757   if (G_UNLIKELY (duration < 0)) {
758     /* well, if some day we really could handle sparse input ... */
759     if (pad->map.is_sparse) {
760       limit = 1;
761       diff = 2;
762       goto resync;
763     }
764     GST_WARNING_OBJECT (pad->collect.pad,
765         "failed to determine packet duration");
766     goto no_granule;
767   }
768
769   GST_LOG_OBJECT (pad->collect.pad, "buffer ts %" GST_TIME_FORMAT
770       ", duration %" GST_TIME_FORMAT ", granule duration %" G_GINT64_FORMAT,
771       GST_TIME_ARGS (time), GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
772       duration);
773
774   /* determine granule corresponding to time,
775    * using the inverse of oggdemux' granule -> time */
776
777   /* see if interpolated granule matches good enough */
778   granule = pad->next_granule;
779   next_time = gst_ogg_stream_granule_to_time (&pad->map, pad->next_granule);
780   diff = GST_CLOCK_DIFF (next_time, time);
781
782   /* we tolerate deviation up to configured or within granule granularity */
783   limit = gst_ogg_stream_granule_to_time (&pad->map, 1) / 2;
784   limit = MAX (limit, ogg_mux->max_tolerance);
785
786   GST_LOG_OBJECT (pad->collect.pad, "expected granule %" G_GINT64_FORMAT " == "
787       "time %" GST_TIME_FORMAT " --> ts diff %" GST_TIME_FORMAT
788       " < tolerance %" GST_TIME_FORMAT " (?)",
789       granule, GST_TIME_ARGS (next_time), GST_TIME_ARGS (ABS (diff)),
790       GST_TIME_ARGS (limit));
791
792 resync:
793   /* if not good enough, determine granule based on time */
794   if (diff > limit || diff < -limit) {
795     granule = gst_util_uint64_scale_round (time, pad->map.granulerate_n,
796         GST_SECOND * pad->map.granulerate_d);
797     GST_DEBUG_OBJECT (pad->collect.pad,
798         "resyncing to determined granule %" G_GINT64_FORMAT, granule);
799   }
800
801   if (pad->map.is_ogm || pad->map.is_sparse) {
802     pad->next_granule = granule;
803   } else {
804     granule += duration;
805     pad->next_granule = granule;
806   }
807
808   /* track previous keyframe */
809   if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT))
810     pad->keyframe_granule = granule;
811
812   /* determine corresponding time and granulepos */
813   GST_BUFFER_OFFSET (buf) = gst_ogg_stream_granule_to_time (&pad->map, granule);
814   GST_BUFFER_OFFSET_END (buf) =
815       gst_ogg_stream_granule_to_granulepos (&pad->map, granule,
816       pad->keyframe_granule);
817
818   return buf;
819
820   /* ERRORS */
821 no_granule:
822   {
823     GST_DEBUG_OBJECT (pad->collect.pad, "could not determine granulepos, "
824         "falling back to upstream provided metadata");
825     return buf;
826   }
827 }
828
829
830 /* make sure at least one buffer is queued on all pads, two if possible
831  * 
832  * if pad->buffer == NULL, pad->next_buffer !=  NULL, then
833  *   we do not know if the buffer is the last or not
834  * if pad->buffer != NULL, pad->next_buffer != NULL, then
835  *   pad->buffer is not the last buffer for the pad
836  * if pad->buffer != NULL, pad->next_buffer == NULL, then
837  *   pad->buffer if the last buffer for the pad
838  * 
839  * returns a pointer to an oggpad that holds the best buffer, or
840  * NULL when no pad was usable. "best" means the buffer marked
841  * with the lowest timestamp. If best->buffer == NULL then either
842  * we're at EOS (popped = FALSE), or a buffer got dropped, so retry. */
843 static GstOggPadData *
844 gst_ogg_mux_queue_pads (GstOggMux * ogg_mux, gboolean * popped)
845 {
846   GstOggPadData *bestpad = NULL;
847   GSList *walk;
848
849   *popped = FALSE;
850
851   /* try to make sure we have a buffer from each usable pad first */
852   walk = ogg_mux->collect->data;
853   while (walk) {
854     GstOggPadData *pad;
855     GstCollectData *data;
856
857     data = (GstCollectData *) walk->data;
858     pad = (GstOggPadData *) data;
859
860     walk = g_slist_next (walk);
861
862     GST_LOG_OBJECT (data->pad, "looking at pad for buffer");
863
864     /* try to get a new buffer for this pad if needed and possible */
865     if (pad->buffer == NULL) {
866       GstBuffer *buf;
867
868       buf = gst_collect_pads_pop (ogg_mux->collect, data);
869       GST_LOG_OBJECT (data->pad, "popped buffer %" GST_PTR_FORMAT, buf);
870
871       /* On EOS we get a NULL buffer */
872       if (buf != NULL) {
873         *popped = TRUE;
874
875         if (ogg_mux->delta_pad == NULL &&
876             GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT))
877           ogg_mux->delta_pad = pad;
878
879         /* if we need headers */
880         if (pad->state == GST_OGG_PAD_STATE_CONTROL) {
881           /* and we have one */
882           ogg_packet packet;
883           gboolean is_header;
884           gsize size;
885
886           packet.packet = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
887           packet.bytes = size;
888
889           /* if we're not yet in data mode, ensure we're setup on the first packet */
890           if (!pad->have_type) {
891             GstCaps *caps;
892
893             /* Use headers in caps, if any; this will allow us to be resilient
894              * to starting streams on the fly, and some streams (like VP8
895              * at least) do not send headers packets, as other muxers don't
896              * expect/need them. */
897             caps = gst_pad_get_current_caps (GST_PAD_CAST (data->pad));
898             pad->have_type =
899                 gst_ogg_stream_setup_map_from_caps_headers (&pad->map, caps);
900
901             if (!pad->have_type) {
902               /* fallback on the packet */
903               pad->have_type = gst_ogg_stream_setup_map (&pad->map, &packet);
904             }
905             if (!pad->have_type) {
906               GST_ERROR_OBJECT (pad, "mapper didn't recognise input stream "
907                   "(pad caps: %" GST_PTR_FORMAT ")", caps);
908             } else {
909               GST_DEBUG_OBJECT (pad, "caps detected: %" GST_PTR_FORMAT,
910                   pad->map.caps);
911             }
912             if (caps)
913               gst_caps_unref (caps);
914           }
915
916           if (pad->have_type)
917             is_header = gst_ogg_stream_packet_is_header (&pad->map, &packet);
918           else                  /* fallback (FIXME 0.11: remove IN_CAPS hack) */
919             is_header = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
920
921           gst_buffer_unmap (buf, packet.packet, size);
922
923           if (is_header) {
924             GST_DEBUG_OBJECT (ogg_mux,
925                 "got header buffer in control state, ignoring");
926             /* just ignore */
927             pad->map.n_header_packets_seen++;
928             gst_buffer_unref (buf);
929             buf = NULL;
930           } else {
931             GST_DEBUG_OBJECT (ogg_mux,
932                 "got data buffer in control state, switching to data mode");
933             /* this is a data buffer so switch to data state */
934             pad->state = GST_OGG_PAD_STATE_DATA;
935
936             /* check if this type of stream allows generating granulepos
937              * metadata here, if not, upstream will have to provide */
938             if (gst_ogg_stream_granule_to_granulepos (&pad->map, 1, 1) < 0) {
939               GST_WARNING_OBJECT (data->pad, "can not generate metadata; "
940                   "relying on upstream");
941               /* disable metadata code path, otherwise not used anyway */
942               pad->map.granulerate_n = 0;
943             }
944           }
945         }
946
947         /* so now we should have a real data packet;
948          * see that it is properly decorated */
949         if (G_LIKELY (buf)) {
950           buf = gst_ogg_mux_decorate_buffer (ogg_mux, pad, buf);
951           if (G_UNLIKELY (!buf))
952             GST_DEBUG_OBJECT (data->pad, "buffer clipped");
953         }
954       }
955
956       pad->buffer = buf;
957     }
958
959     /* we should have a buffer now, see if it is the best pad to
960      * pull on */
961     if (pad->buffer) {
962       if (gst_ogg_mux_compare_pads (ogg_mux, bestpad, pad) > 0) {
963         GST_LOG_OBJECT (data->pad,
964             "new best pad, with buffer %" GST_PTR_FORMAT, pad->buffer);
965
966         bestpad = pad;
967       }
968     }
969   }
970
971   return bestpad;
972 }
973
974 static GList *
975 gst_ogg_mux_get_headers (GstOggPadData * pad)
976 {
977   GList *res = NULL;
978   GstStructure *structure;
979   GstCaps *caps;
980   GstPad *thepad;
981
982   thepad = pad->collect.pad;
983
984   GST_LOG_OBJECT (thepad, "getting headers");
985
986   caps = gst_pad_get_negotiated_caps (thepad);
987   if (caps != NULL) {
988     const GValue *streamheader;
989
990     structure = gst_caps_get_structure (caps, 0);
991     streamheader = gst_structure_get_value (structure, "streamheader");
992     if (streamheader != NULL) {
993       GST_LOG_OBJECT (thepad, "got header");
994       if (G_VALUE_TYPE (streamheader) == GST_TYPE_ARRAY) {
995         GArray *bufarr = g_value_peek_pointer (streamheader);
996         gint i;
997
998         GST_LOG_OBJECT (thepad, "got fixed list");
999
1000         for (i = 0; i < bufarr->len; i++) {
1001           GValue *bufval = &g_array_index (bufarr, GValue, i);
1002
1003           GST_LOG_OBJECT (thepad, "item %d", i);
1004           if (G_VALUE_TYPE (bufval) == GST_TYPE_BUFFER) {
1005             GstBuffer *buf = g_value_peek_pointer (bufval);
1006
1007             GST_LOG_OBJECT (thepad, "adding item %d to header list", i);
1008
1009             gst_buffer_ref (buf);
1010             res = g_list_append (res, buf);
1011           }
1012         }
1013       } else {
1014         GST_LOG_OBJECT (thepad, "streamheader is not fixed list");
1015       }
1016
1017       /* Start a new page for every CMML buffer */
1018       if (gst_structure_has_name (structure, "text/x-cmml"))
1019         pad->always_flush_page = TRUE;
1020     } else if (gst_structure_has_name (structure, "video/x-dirac")) {
1021       res = g_list_append (res, pad->buffer);
1022       pad->buffer = NULL;
1023       pad->always_flush_page = TRUE;
1024     } else {
1025       GST_LOG_OBJECT (thepad, "caps don't have streamheader");
1026     }
1027     gst_caps_unref (caps);
1028   } else {
1029     GST_LOG_OBJECT (thepad, "got empty caps as negotiated format");
1030   }
1031   return res;
1032 }
1033
1034 static GstCaps *
1035 gst_ogg_mux_set_header_on_caps (GstCaps * caps, GList * buffers)
1036 {
1037   GstStructure *structure;
1038   GValue array = { 0 };
1039   GList *walk = buffers;
1040
1041   caps = gst_caps_make_writable (caps);
1042
1043   structure = gst_caps_get_structure (caps, 0);
1044
1045   /* put buffers in a fixed list */
1046   g_value_init (&array, GST_TYPE_ARRAY);
1047
1048   while (walk) {
1049     GstBuffer *buf = GST_BUFFER (walk->data);
1050     GstBuffer *copy;
1051     GValue value = { 0 };
1052
1053     walk = walk->next;
1054
1055     /* mark buffer */
1056     GST_LOG ("Setting IN_CAPS on buffer of length %d",
1057         gst_buffer_get_size (buf));
1058     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
1059
1060     g_value_init (&value, GST_TYPE_BUFFER);
1061     copy = gst_buffer_copy (buf);
1062     gst_value_set_buffer (&value, copy);
1063     gst_buffer_unref (copy);
1064     gst_value_array_append_value (&array, &value);
1065     g_value_unset (&value);
1066   }
1067   gst_structure_set_value (structure, "streamheader", &array);
1068   g_value_unset (&array);
1069
1070   return caps;
1071 }
1072
1073 /*
1074  * For each pad we need to write out one (small) header in one
1075  * page that allows decoders to identify the type of the stream.
1076  * After that we need to write out all extra info for the decoders.
1077  * In the case of a codec that also needs data as configuration, we can
1078  * find that info in the streamcaps. 
1079  * After writing the headers we must start a new page for the data.
1080  */
1081 static GstFlowReturn
1082 gst_ogg_mux_send_headers (GstOggMux * mux)
1083 {
1084   GSList *walk;
1085   GList *hbufs, *hwalk;
1086   GstCaps *caps;
1087   GstFlowReturn ret;
1088
1089   hbufs = NULL;
1090   ret = GST_FLOW_OK;
1091
1092   GST_LOG_OBJECT (mux, "collecting headers");
1093
1094   walk = mux->collect->data;
1095   while (walk) {
1096     GstOggPadData *pad;
1097     GstPad *thepad;
1098
1099     pad = (GstOggPadData *) walk->data;
1100     thepad = pad->collect.pad;
1101
1102     walk = g_slist_next (walk);
1103
1104     GST_LOG_OBJECT (mux, "looking at pad %s:%s", GST_DEBUG_PAD_NAME (thepad));
1105
1106     /* if the pad has no buffer, we don't care */
1107     if (pad->buffer == NULL)
1108       continue;
1109
1110     /* now figure out the headers */
1111     pad->map.headers = gst_ogg_mux_get_headers (pad);
1112   }
1113
1114   GST_LOG_OBJECT (mux, "creating BOS pages");
1115   walk = mux->collect->data;
1116   while (walk) {
1117     GstOggPadData *pad;
1118     GstBuffer *buf;
1119     ogg_packet packet;
1120     ogg_page page;
1121     GstPad *thepad;
1122     GstCaps *caps;
1123     GstStructure *structure;
1124     GstBuffer *hbuf;
1125     gsize size;
1126
1127     pad = (GstOggPadData *) walk->data;
1128     thepad = pad->collect.pad;
1129     caps = gst_pad_get_negotiated_caps (thepad);
1130     structure = gst_caps_get_structure (caps, 0);
1131
1132     walk = walk->next;
1133
1134     pad->packetno = 0;
1135
1136     GST_LOG_OBJECT (thepad, "looping over headers");
1137
1138     if (pad->map.headers) {
1139       buf = GST_BUFFER (pad->map.headers->data);
1140       pad->map.headers = g_list_remove (pad->map.headers, buf);
1141     } else if (pad->buffer) {
1142       buf = pad->buffer;
1143       gst_buffer_ref (buf);
1144     } else {
1145       /* fixme -- should be caught in the previous list traversal. */
1146       GST_OBJECT_LOCK (thepad);
1147       g_critical ("No headers or buffers on pad %s:%s",
1148           GST_DEBUG_PAD_NAME (thepad));
1149       GST_OBJECT_UNLOCK (thepad);
1150       continue;
1151     }
1152
1153     /* create a packet from the buffer */
1154     packet.packet = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
1155     packet.bytes = size;
1156     packet.granulepos = GST_BUFFER_OFFSET_END (buf);
1157     if (packet.granulepos == -1)
1158       packet.granulepos = 0;
1159     /* mark BOS and packet number */
1160     packet.b_o_s = (pad->packetno == 0);
1161     packet.packetno = pad->packetno++;
1162     /* mark EOS */
1163     packet.e_o_s = 0;
1164
1165     /* swap the packet in */
1166     ogg_stream_packetin (&pad->map.stream, &packet);
1167
1168     gst_buffer_unmap (buf, packet.packet, size);
1169     gst_buffer_unref (buf);
1170
1171     GST_LOG_OBJECT (thepad, "flushing out BOS page");
1172     if (!ogg_stream_flush (&pad->map.stream, &page))
1173       g_critical ("Could not flush BOS page");
1174
1175     hbuf = gst_ogg_mux_buffer_from_page (mux, &page, FALSE);
1176
1177     GST_LOG_OBJECT (mux, "swapped out page with mime type %s",
1178         gst_structure_get_name (structure));
1179
1180     /* quick hack: put Theora, VP8 and Dirac video pages at the front.
1181      * Ideally, we would have a settable enum for which Ogg
1182      * profile we work with, and order based on that.
1183      * (FIXME: if there is more than one video stream, shouldn't we only put
1184      * one's BOS into the first page, followed by an audio stream's BOS, and
1185      * only then followed by the remaining video and audio streams?) */
1186     if (gst_structure_has_name (structure, "video/x-theora")) {
1187       GST_DEBUG_OBJECT (thepad, "putting %s page at the front", "Theora");
1188       hbufs = g_list_prepend (hbufs, hbuf);
1189     } else if (gst_structure_has_name (structure, "video/x-dirac")) {
1190       GST_DEBUG_OBJECT (thepad, "putting %s page at the front", "Dirac");
1191       hbufs = g_list_prepend (hbufs, hbuf);
1192       pad->always_flush_page = TRUE;
1193     } else if (gst_structure_has_name (structure, "video/x-vp8")) {
1194       GST_DEBUG_OBJECT (thepad, "putting %s page at the front", "VP8");
1195       hbufs = g_list_prepend (hbufs, hbuf);
1196     } else {
1197       hbufs = g_list_append (hbufs, hbuf);
1198     }
1199     gst_caps_unref (caps);
1200   }
1201
1202   GST_LOG_OBJECT (mux, "creating next headers");
1203   walk = mux->collect->data;
1204   while (walk) {
1205     GstOggPadData *pad;
1206     GstPad *thepad;
1207
1208     pad = (GstOggPadData *) walk->data;
1209     thepad = pad->collect.pad;
1210
1211     walk = walk->next;
1212
1213     GST_LOG_OBJECT (mux, "looping over headers for pad %s:%s",
1214         GST_DEBUG_PAD_NAME (thepad));
1215
1216     hwalk = pad->map.headers;
1217     while (hwalk) {
1218       GstBuffer *buf = GST_BUFFER (hwalk->data);
1219       ogg_packet packet;
1220       ogg_page page;
1221       gsize size;
1222
1223       hwalk = hwalk->next;
1224
1225       /* create a packet from the buffer */
1226       packet.packet = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
1227       packet.bytes = size;
1228       packet.granulepos = GST_BUFFER_OFFSET_END (buf);
1229       if (packet.granulepos == -1)
1230         packet.granulepos = 0;
1231       /* mark BOS and packet number */
1232       packet.b_o_s = (pad->packetno == 0);
1233       packet.packetno = pad->packetno++;
1234       /* mark EOS */
1235       packet.e_o_s = 0;
1236
1237       /* swap the packet in */
1238       ogg_stream_packetin (&pad->map.stream, &packet);
1239       gst_buffer_unmap (buf, packet.packet, size);
1240       gst_buffer_unref (buf);
1241
1242       /* if last header, flush page */
1243       if (hwalk == NULL) {
1244         GST_LOG_OBJECT (mux,
1245             "flushing page as packet %" G_GUINT64_FORMAT " is first or "
1246             "last packet", (guint64) packet.packetno);
1247         while (ogg_stream_flush (&pad->map.stream, &page)) {
1248           GstBuffer *hbuf = gst_ogg_mux_buffer_from_page (mux, &page, FALSE);
1249
1250           GST_LOG_OBJECT (mux, "swapped out page");
1251           hbufs = g_list_append (hbufs, hbuf);
1252         }
1253       } else {
1254         GST_LOG_OBJECT (mux, "try to swap out page");
1255         /* just try to swap out a page then */
1256         while (ogg_stream_pageout (&pad->map.stream, &page) > 0) {
1257           GstBuffer *hbuf = gst_ogg_mux_buffer_from_page (mux, &page, FALSE);
1258
1259           GST_LOG_OBJECT (mux, "swapped out page");
1260           hbufs = g_list_append (hbufs, hbuf);
1261         }
1262       }
1263     }
1264     g_list_free (pad->map.headers);
1265     pad->map.headers = NULL;
1266   }
1267   /* hbufs holds all buffers for the headers now */
1268
1269   /* create caps with the buffers */
1270   caps = gst_pad_get_caps (mux->srcpad, NULL);
1271   if (caps) {
1272     caps = gst_ogg_mux_set_header_on_caps (caps, hbufs);
1273     gst_pad_set_caps (mux->srcpad, caps);
1274     gst_caps_unref (caps);
1275   }
1276   /* and send the buffers */
1277   while (hbufs != NULL) {
1278     GstBuffer *buf = GST_BUFFER (hbufs->data);
1279
1280     hbufs = g_list_delete_link (hbufs, hbufs);
1281
1282     if ((ret = gst_ogg_mux_push_buffer (mux, buf, NULL)) != GST_FLOW_OK)
1283       break;
1284   }
1285   /* free any remaining nodes/buffers in case we couldn't push them */
1286   g_list_foreach (hbufs, (GFunc) gst_mini_object_unref, NULL);
1287   g_list_free (hbufs);
1288
1289   return ret;
1290 }
1291
1292 /* this function is called to process data on the best pending pad.
1293  *
1294  * basic idea:
1295  *
1296  * 1) store the selected pad and keep on pulling until we fill a
1297  *    complete ogg page or the ogg page is filled above the max-delay
1298  *    threshold. This is needed because the ogg spec says that
1299  *    you should fill a complete page with data from the same logical
1300  *    stream. When the page is filled, go back to 1).
1301  * 2) before filling a page, read ahead one more buffer to see if this
1302  *    packet is the last of the stream. We need to do this because the ogg
1303  *    spec mandates that the last packet should have the EOS flag set before
1304  *    sending it to ogg. if pad->buffer is NULL we need to wait to find out
1305  *    whether there are any more buffers.
1306  * 3) pages get queued on a per-pad queue. Every time a page is queued, a
1307  *    dequeue is called, which will dequeue the oldest page on any pad, provided
1308  *    that ALL pads have at least one marked page in the queue (or remaining
1309  *    pads are at EOS)
1310  */
1311 static GstFlowReturn
1312 gst_ogg_mux_process_best_pad (GstOggMux * ogg_mux, GstOggPadData * best)
1313 {
1314   GstFlowReturn ret = GST_FLOW_OK;
1315   gboolean delta_unit;
1316   gint64 granulepos = 0;
1317   GstClockTime timestamp, gp_time;
1318   GstBuffer *next_buf;
1319
1320   GST_LOG_OBJECT (ogg_mux, "best pad %" GST_PTR_FORMAT
1321       ", currently pulling from %" GST_PTR_FORMAT, best->collect.pad,
1322       ogg_mux->pulling);
1323
1324   next_buf = gst_collect_pads_peek (ogg_mux->collect, &best->collect);
1325   if (next_buf) {
1326     best->eos = FALSE;
1327     gst_buffer_unref (next_buf);
1328   } else {
1329     best->eos = TRUE;
1330   }
1331
1332   /* if we were already pulling from one pad, but the new "best" buffer is
1333    * from another pad, we need to check if we have reason to flush a page
1334    * for the pad we were pulling from before */
1335   if (ogg_mux->pulling && best &&
1336       ogg_mux->pulling != best && ogg_mux->pulling->buffer) {
1337     GstOggPadData *pad = ogg_mux->pulling;
1338     GstClockTime last_ts = GST_BUFFER_END_TIME (pad->buffer);
1339
1340     /* if the next packet in the current page is going to make the page
1341      * too long, we need to flush */
1342     if (last_ts > ogg_mux->next_ts + ogg_mux->max_delay) {
1343       ogg_page page;
1344
1345       GST_LOG_OBJECT (pad->collect.pad,
1346           GST_GP_FORMAT " stored packet %" G_GINT64_FORMAT
1347           " will make page too long, flushing",
1348           GST_BUFFER_OFFSET_END (pad->buffer),
1349           (gint64) pad->map.stream.packetno);
1350
1351       while (ogg_stream_flush (&pad->map.stream, &page)) {
1352         /* end time of this page is the timestamp of the next buffer */
1353         ogg_mux->pulling->timestamp_end = GST_BUFFER_TIMESTAMP (pad->buffer);
1354         /* Place page into the per-pad queue */
1355         ret = gst_ogg_mux_pad_queue_page (ogg_mux, pad, &page,
1356             pad->first_delta);
1357         /* increment the page number counter */
1358         pad->pageno++;
1359         /* mark other pages as delta */
1360         pad->first_delta = TRUE;
1361       }
1362       pad->new_page = TRUE;
1363       ogg_mux->pulling = NULL;
1364     }
1365   }
1366
1367   /* if we don't know which pad to pull on, use the best one */
1368   if (ogg_mux->pulling == NULL) {
1369     ogg_mux->pulling = best;
1370     GST_LOG_OBJECT (ogg_mux->pulling->collect.pad, "pulling from best pad");
1371
1372     /* remember timestamp and gp time of first buffer for this new pad */
1373     if (ogg_mux->pulling != NULL) {
1374       ogg_mux->next_ts = GST_BUFFER_TIMESTAMP (ogg_mux->pulling->buffer);
1375       GST_LOG_OBJECT (ogg_mux->pulling->collect.pad, "updated times, next ts %"
1376           GST_TIME_FORMAT, GST_TIME_ARGS (ogg_mux->next_ts));
1377     } else {
1378       /* no pad to pull on, send EOS */
1379       gst_pad_push_event (ogg_mux->srcpad, gst_event_new_eos ());
1380       return GST_FLOW_WRONG_STATE;
1381     }
1382   }
1383
1384   if (ogg_mux->need_headers) {
1385     ret = gst_ogg_mux_send_headers (ogg_mux);
1386     ogg_mux->need_headers = FALSE;
1387   }
1388
1389   /* we are pulling from a pad, continue to do so until a page
1390    * has been filled and queued */
1391   if (ogg_mux->pulling != NULL) {
1392     ogg_packet packet;
1393     ogg_page page;
1394     GstBuffer *buf, *tmpbuf;
1395     GstOggPadData *pad = ogg_mux->pulling;
1396     gint64 duration;
1397     gboolean force_flush;
1398     gsize size;
1399
1400     GST_LOG_OBJECT (ogg_mux->pulling->collect.pad, "pulling from pad");
1401
1402     /* now see if we have a buffer */
1403     buf = pad->buffer;
1404     if (buf == NULL) {
1405       GST_DEBUG_OBJECT (ogg_mux, "pad was EOS");
1406       ogg_mux->pulling = NULL;
1407       return GST_FLOW_OK;
1408     }
1409
1410     delta_unit = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
1411     duration = GST_BUFFER_DURATION (buf);
1412
1413     /* if the current "next timestamp" on the pad is unset, then this is the
1414      * first packet on the new page.  Update our pad's page timestamp */
1415     if (ogg_mux->pulling->timestamp == GST_CLOCK_TIME_NONE) {
1416       ogg_mux->pulling->timestamp = GST_BUFFER_TIMESTAMP (buf);
1417       GST_LOG_OBJECT (ogg_mux->pulling->collect.pad,
1418           "updated pad timestamp to %" GST_TIME_FORMAT,
1419           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1420     }
1421     /* create a packet from the buffer */
1422     packet.packet = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
1423     packet.bytes = size;
1424     packet.granulepos = GST_BUFFER_OFFSET_END (buf);
1425     if (packet.granulepos == -1)
1426       packet.granulepos = 0;
1427     /* mark BOS and packet number */
1428     packet.b_o_s = (pad->packetno == 0);
1429     packet.packetno = pad->packetno++;
1430     GST_LOG_OBJECT (pad->collect.pad, GST_GP_FORMAT
1431         " packet %" G_GINT64_FORMAT " (%ld bytes) created from buffer",
1432         GST_GP_CAST (packet.granulepos), (gint64) packet.packetno,
1433         packet.bytes);
1434
1435     packet.e_o_s = best->eos ? 1 : 0;
1436     tmpbuf = NULL;
1437
1438     /* we flush when we see a new keyframe */
1439     force_flush = (pad->prev_delta && !delta_unit) || pad->always_flush_page;
1440     if (duration != -1) {
1441       pad->duration += duration;
1442       /* if page duration exceeds max, flush page */
1443       if (pad->duration > ogg_mux->max_page_delay) {
1444         force_flush = TRUE;
1445         pad->duration = 0;
1446       }
1447     }
1448
1449     if (GST_BUFFER_IS_DISCONT (buf)) {
1450       if (pad->data_pushed) {
1451         GST_LOG_OBJECT (pad->collect.pad, "got discont");
1452         packet.packetno++;
1453         /* No public API for this; hack things in */
1454         pad->map.stream.pageno++;
1455         force_flush = TRUE;
1456       } else {
1457         GST_LOG_OBJECT (pad->collect.pad, "discont at stream start");
1458       }
1459     }
1460
1461     /* flush the currently built page if necessary */
1462     if (force_flush) {
1463       GST_LOG_OBJECT (pad->collect.pad,
1464           GST_GP_FORMAT " forced flush of page before this packet",
1465           GST_BUFFER_OFFSET_END (pad->buffer));
1466       while (ogg_stream_flush (&pad->map.stream, &page)) {
1467         /* end time of this page is the timestamp of the next buffer */
1468         ogg_mux->pulling->timestamp_end = GST_BUFFER_TIMESTAMP (pad->buffer);
1469         ret = gst_ogg_mux_pad_queue_page (ogg_mux, pad, &page,
1470             pad->first_delta);
1471
1472         /* increment the page number counter */
1473         pad->pageno++;
1474         /* mark other pages as delta */
1475         pad->first_delta = TRUE;
1476       }
1477       pad->new_page = TRUE;
1478     }
1479
1480     /* if this is the first packet of a new page figure out the delta flag */
1481     if (pad->new_page) {
1482       if (delta_unit) {
1483         /* mark the page as delta */
1484         pad->first_delta = TRUE;
1485       } else {
1486         /* got a keyframe */
1487         if (ogg_mux->delta_pad == pad) {
1488           /* if we get it on the pad with deltaunits,
1489            * we mark the page as non delta */
1490           pad->first_delta = FALSE;
1491         } else if (ogg_mux->delta_pad != NULL) {
1492           /* if there are pads with delta frames, we
1493            * must mark this one as delta */
1494           pad->first_delta = TRUE;
1495         } else {
1496           pad->first_delta = FALSE;
1497         }
1498       }
1499       pad->new_page = FALSE;
1500     }
1501
1502     /* save key unit to track delta->key unit transitions */
1503     pad->prev_delta = delta_unit;
1504
1505     /* swap the packet in */
1506     if (packet.e_o_s == 1)
1507       GST_DEBUG_OBJECT (pad->collect.pad, "swapping in EOS packet");
1508     if (packet.b_o_s == 1)
1509       GST_DEBUG_OBJECT (pad->collect.pad, "swapping in BOS packet");
1510
1511     ogg_stream_packetin (&pad->map.stream, &packet);
1512     gst_buffer_unmap (buf, packet.packet, size);
1513     pad->data_pushed = TRUE;
1514
1515     gp_time = GST_BUFFER_OFFSET (pad->buffer);
1516     granulepos = GST_BUFFER_OFFSET_END (pad->buffer);
1517     timestamp = GST_BUFFER_TIMESTAMP (pad->buffer);
1518
1519     GST_LOG_OBJECT (pad->collect.pad,
1520         GST_GP_FORMAT " packet %" G_GINT64_FORMAT ", gp time %"
1521         GST_TIME_FORMAT ", timestamp %" GST_TIME_FORMAT " packetin'd",
1522         granulepos, (gint64) packet.packetno, GST_TIME_ARGS (gp_time),
1523         GST_TIME_ARGS (timestamp));
1524     /* don't need the old buffer anymore */
1525     gst_buffer_unref (pad->buffer);
1526     /* store new readahead buffer */
1527     pad->buffer = tmpbuf;
1528
1529     /* let ogg write out the pages now. The packet we got could end
1530      * up in more than one page so we need to write them all */
1531     if (ogg_stream_pageout (&pad->map.stream, &page) > 0) {
1532       /* we have a new page, so we need to timestamp it correctly.
1533        * if this fresh packet ends on this page, then the page's granulepos
1534        * comes from that packet, and we should set this buffer's timestamp */
1535
1536       GST_LOG_OBJECT (pad->collect.pad,
1537           GST_GP_FORMAT " packet %" G_GINT64_FORMAT ", time %"
1538           GST_TIME_FORMAT ") caused new page",
1539           granulepos, (gint64) packet.packetno, GST_TIME_ARGS (timestamp));
1540       GST_LOG_OBJECT (pad->collect.pad,
1541           GST_GP_FORMAT " new page %ld",
1542           GST_GP_CAST (ogg_page_granulepos (&page)), pad->map.stream.pageno);
1543
1544       if (ogg_page_granulepos (&page) == granulepos) {
1545         /* the packet we streamed in finishes on the current page,
1546          * because the page's granulepos is the granulepos of the last
1547          * packet completed on that page,
1548          * so update the timestamp that we will give to the page */
1549         GST_LOG_OBJECT (pad->collect.pad,
1550             GST_GP_FORMAT
1551             " packet finishes on current page, updating gp time to %"
1552             GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (gp_time));
1553         pad->gp_time = gp_time;
1554       } else {
1555         GST_LOG_OBJECT (pad->collect.pad,
1556             GST_GP_FORMAT
1557             " packet spans beyond current page, keeping old gp time %"
1558             GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (pad->gp_time));
1559       }
1560
1561       /* push the page */
1562       /* end time of this page is the timestamp of the next buffer */
1563       pad->timestamp_end = timestamp;
1564       ret = gst_ogg_mux_pad_queue_page (ogg_mux, pad, &page, pad->first_delta);
1565       pad->pageno++;
1566       /* mark next pages as delta */
1567       pad->first_delta = TRUE;
1568
1569       /* use an inner loop here to flush the remaining pages and
1570        * mark them as delta frames as well */
1571       while (ogg_stream_pageout (&pad->map.stream, &page) > 0) {
1572         if (ogg_page_granulepos (&page) == granulepos) {
1573           /* the page has taken up the new packet completely, which means
1574            * the packet ends the page and we can update the gp time
1575            * before pushing out */
1576           pad->gp_time = gp_time;
1577         }
1578
1579         /* we have a complete page now, we can push the page
1580          * and make sure to pull on a new pad the next time around */
1581         ret = gst_ogg_mux_pad_queue_page (ogg_mux, pad, &page,
1582             pad->first_delta);
1583         /* increment the page number counter */
1584         pad->pageno++;
1585       }
1586       /* need a new page as well */
1587       pad->new_page = TRUE;
1588       pad->duration = 0;
1589       /* we're done pulling on this pad, make sure to choose a new
1590        * pad for pulling in the next iteration */
1591       ogg_mux->pulling = NULL;
1592     }
1593
1594     /* Update the gp time, if necessary, since any future page will have at
1595      * least this gp time.
1596      */
1597     if (pad->gp_time < gp_time) {
1598       pad->gp_time = gp_time;
1599       GST_LOG_OBJECT (pad->collect.pad,
1600           "Updated running gp time of pad %" GST_PTR_FORMAT
1601           " to %" GST_TIME_FORMAT, pad->collect.pad, GST_TIME_ARGS (gp_time));
1602     }
1603   }
1604
1605   return ret;
1606 }
1607
1608 /* all_pads_eos:
1609  *
1610  * Checks if all pads are EOS'd by peeking.
1611  *
1612  * Returns TRUE if all pads are EOS.
1613  */
1614 static gboolean
1615 all_pads_eos (GstCollectPads * pads)
1616 {
1617   GSList *walk;
1618
1619   walk = pads->data;
1620   while (walk) {
1621     GstOggPadData *oggpad = (GstOggPadData *) walk->data;
1622
1623     GST_DEBUG ("oggpad %p eos %d", oggpad, oggpad->eos);
1624
1625     if (oggpad->eos == FALSE)
1626       return FALSE;
1627
1628     walk = g_slist_next (walk);
1629   }
1630
1631   return TRUE;
1632 }
1633
1634 /* This function is called when there is data on all pads.
1635  * 
1636  * It finds a pad to pull on, this is done by looking at the buffers
1637  * to decide which one to use, and using the 'oldest' one first. It then calls
1638  * gst_ogg_mux_process_best_pad() to process as much data as possible.
1639  * 
1640  * If all the pads have received EOS, it flushes out all data by continually
1641  * getting the best pad and calling gst_ogg_mux_process_best_pad() until they
1642  * are all empty, and then sends EOS.
1643  */
1644 static GstFlowReturn
1645 gst_ogg_mux_collected (GstCollectPads * pads, GstOggMux * ogg_mux)
1646 {
1647   GstOggPadData *best;
1648   GstFlowReturn ret;
1649   gboolean popped;
1650
1651   GST_LOG_OBJECT (ogg_mux, "collected");
1652
1653   /* queue buffers on all pads; find a buffer with the lowest timestamp */
1654   best = gst_ogg_mux_queue_pads (ogg_mux, &popped);
1655
1656   if (popped)
1657     return GST_FLOW_OK;
1658
1659   if (best == NULL || best->buffer == NULL) {
1660     /* This is not supposed to happen */
1661     return GST_FLOW_ERROR;
1662   }
1663
1664   ret = gst_ogg_mux_process_best_pad (ogg_mux, best);
1665
1666   if (best->eos && all_pads_eos (pads)) {
1667     gst_pad_push_event (ogg_mux->srcpad, gst_event_new_eos ());
1668     return GST_FLOW_UNEXPECTED;
1669   }
1670
1671   return ret;
1672 }
1673
1674 static void
1675 gst_ogg_mux_get_property (GObject * object,
1676     guint prop_id, GValue * value, GParamSpec * pspec)
1677 {
1678   GstOggMux *ogg_mux;
1679
1680   ogg_mux = GST_OGG_MUX (object);
1681
1682   switch (prop_id) {
1683     case ARG_MAX_DELAY:
1684       g_value_set_uint64 (value, ogg_mux->max_delay);
1685       break;
1686     case ARG_MAX_PAGE_DELAY:
1687       g_value_set_uint64 (value, ogg_mux->max_page_delay);
1688       break;
1689     case ARG_MAX_TOLERANCE:
1690       g_value_set_uint64 (value, ogg_mux->max_tolerance);
1691       break;
1692     default:
1693       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1694       break;
1695   }
1696 }
1697
1698 static void
1699 gst_ogg_mux_set_property (GObject * object,
1700     guint prop_id, const GValue * value, GParamSpec * pspec)
1701 {
1702   GstOggMux *ogg_mux;
1703
1704   ogg_mux = GST_OGG_MUX (object);
1705
1706   switch (prop_id) {
1707     case ARG_MAX_DELAY:
1708       ogg_mux->max_delay = g_value_get_uint64 (value);
1709       break;
1710     case ARG_MAX_PAGE_DELAY:
1711       ogg_mux->max_page_delay = g_value_get_uint64 (value);
1712       break;
1713     case ARG_MAX_TOLERANCE:
1714       ogg_mux->max_tolerance = g_value_get_uint64 (value);
1715       break;
1716     default:
1717       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1718       break;
1719   }
1720 }
1721
1722 /* reset all variables in the ogg pads. */
1723 static void
1724 gst_ogg_mux_init_collectpads (GstCollectPads * collect)
1725 {
1726   GSList *walk;
1727
1728   walk = collect->data;
1729   while (walk) {
1730     GstOggPadData *oggpad = (GstOggPadData *) walk->data;
1731
1732     ogg_stream_init (&oggpad->map.stream, oggpad->map.serialno);
1733     oggpad->packetno = 0;
1734     oggpad->pageno = 0;
1735     oggpad->eos = FALSE;
1736     /* we assume there will be some control data first for this pad */
1737     oggpad->state = GST_OGG_PAD_STATE_CONTROL;
1738     oggpad->new_page = TRUE;
1739     oggpad->first_delta = FALSE;
1740     oggpad->prev_delta = FALSE;
1741     oggpad->data_pushed = FALSE;
1742     oggpad->pagebuffers = g_queue_new ();
1743
1744     gst_segment_init (&oggpad->segment, GST_FORMAT_TIME);
1745
1746     walk = g_slist_next (walk);
1747   }
1748 }
1749
1750 /* Clear all buffers from the collectpads object */
1751 static void
1752 gst_ogg_mux_clear_collectpads (GstCollectPads * collect)
1753 {
1754   GSList *walk;
1755
1756   for (walk = collect->data; walk; walk = g_slist_next (walk)) {
1757     GstOggPadData *oggpad = (GstOggPadData *) walk->data;
1758     GstBuffer *buf;
1759
1760     ogg_stream_clear (&oggpad->map.stream);
1761
1762     while ((buf = g_queue_pop_head (oggpad->pagebuffers)) != NULL) {
1763       gst_buffer_unref (buf);
1764     }
1765     g_queue_free (oggpad->pagebuffers);
1766     oggpad->pagebuffers = NULL;
1767
1768     if (oggpad->buffer) {
1769       gst_buffer_unref (oggpad->buffer);
1770       oggpad->buffer = NULL;
1771     }
1772
1773     gst_segment_init (&oggpad->segment, GST_FORMAT_TIME);
1774   }
1775 }
1776
1777 static GstStateChangeReturn
1778 gst_ogg_mux_change_state (GstElement * element, GstStateChange transition)
1779 {
1780   GstOggMux *ogg_mux;
1781   GstStateChangeReturn ret;
1782
1783   ogg_mux = GST_OGG_MUX (element);
1784
1785   switch (transition) {
1786     case GST_STATE_CHANGE_NULL_TO_READY:
1787       break;
1788     case GST_STATE_CHANGE_READY_TO_PAUSED:
1789       gst_ogg_mux_clear (ogg_mux);
1790       gst_ogg_mux_init_collectpads (ogg_mux->collect);
1791       gst_collect_pads_start (ogg_mux->collect);
1792       break;
1793     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1794       break;
1795     case GST_STATE_CHANGE_PAUSED_TO_READY:
1796       gst_collect_pads_stop (ogg_mux->collect);
1797       break;
1798     default:
1799       break;
1800   }
1801
1802   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1803
1804   switch (transition) {
1805     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1806       break;
1807     case GST_STATE_CHANGE_PAUSED_TO_READY:
1808       gst_ogg_mux_clear_collectpads (ogg_mux->collect);
1809       break;
1810     case GST_STATE_CHANGE_READY_TO_NULL:
1811       break;
1812     default:
1813       break;
1814   }
1815
1816   return ret;
1817 }
1818
1819 gboolean
1820 gst_ogg_mux_plugin_init (GstPlugin * plugin)
1821 {
1822   GST_DEBUG_CATEGORY_INIT (gst_ogg_mux_debug, "oggmux", 0, "ogg muxer");
1823
1824   return gst_element_register (plugin, "oggmux", GST_RANK_PRIMARY,
1825       GST_TYPE_OGG_MUX);
1826 }