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>
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.
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.
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.
22 * SECTION:element-oggmux
23 * @see_also: <link linkend="gst-plugins-base-plugins-oggdemux">oggdemux</link>
25 * This element merges streams (audio and video) into ogg files.
28 * <title>Example pipelines</title>
30 * gst-launch v4l2src num-buffers=500 ! video/x-raw,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)
35 * Last reviewed on 2008-02-06 (0.10.17)
43 #include <gst/base/gstcollectpads.h>
44 #include <gst/tag/tag.h>
46 #include "gstoggmux.h"
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... */
52 #include <stdlib.h> /* rand, srand, atoi */
54 GST_DEBUG_CATEGORY_STATIC (gst_ogg_mux_debug);
55 #define GST_CAT_DEFAULT gst_ogg_mux_debug
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))
65 #define GST_GP_FORMAT "[gp %8" G_GINT64_FORMAT "]"
66 #define GST_GP_CAST(_gp) ((gint64) _gp)
70 GST_OGG_FLAG_BOS = GST_ELEMENT_FLAG_LAST,
75 /* OggMux signals and args */
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)
94 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
97 GST_STATIC_CAPS ("application/ogg")
100 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
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")
110 static void gst_ogg_mux_finalize (GObject * object);
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);
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);
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));
132 gst_ogg_mux_class_init (GstOggMuxClass * klass)
134 GObjectClass *gobject_class;
135 GstElementClass *gstelement_class;
137 gobject_class = (GObjectClass *) klass;
138 gstelement_class = (GstElementClass *) klass;
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;
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));
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>");
154 gstelement_class->request_new_pad = gst_ogg_mux_request_new_pad;
155 gstelement_class->release_pad = gst_ogg_mux_release_pad;
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,
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));
173 gstelement_class->change_state = gst_ogg_mux_change_state;
178 static const GstEventMask *
179 gst_ogg_mux_get_sink_event_masks (GstPad * pad)
181 static const GstEventMask gst_ogg_mux_sink_event_masks[] = {
183 {GST_EVENT_DISCONTINUOUS, 0},
187 return gst_ogg_mux_sink_event_masks;
192 gst_ogg_mux_clear (GstOggMux * ogg_mux)
194 ogg_mux->pulling = NULL;
195 ogg_mux->need_headers = TRUE;
196 ogg_mux->delta_pad = NULL;
198 ogg_mux->next_ts = 0;
199 ogg_mux->last_ts = GST_CLOCK_TIME_NONE;
203 gst_ogg_mux_init (GstOggMux * ogg_mux)
205 GstElementClass *klass = GST_ELEMENT_GET_CLASS (ogg_mux);
208 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
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);
213 GST_OBJECT_FLAG_SET (GST_ELEMENT (ogg_mux), GST_OGG_FLAG_BOS);
215 /* seed random number generator for creation of serial numbers */
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),
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;
227 gst_ogg_mux_clear (ogg_mux);
231 gst_ogg_mux_finalize (GObject * object)
235 ogg_mux = GST_OGG_MUX (object);
237 if (ogg_mux->collect) {
238 gst_object_unref (ogg_mux->collect);
239 ogg_mux->collect = NULL;
242 G_OBJECT_CLASS (parent_class)->finalize (object);
246 gst_ogg_mux_ogg_pad_destroy_notify (GstCollectData * data)
248 GstOggPadData *oggpad = (GstOggPadData *) data;
251 ogg_stream_clear (&oggpad->map.stream);
252 gst_caps_replace (&oggpad->map.caps, NULL);
254 if (oggpad->pagebuffers) {
255 while ((buf = g_queue_pop_head (oggpad->pagebuffers)) != NULL) {
256 gst_buffer_unref (buf);
258 g_queue_free (oggpad->pagebuffers);
259 oggpad->pagebuffers = NULL;
263 static GstPadLinkReturn
264 gst_ogg_mux_sinkconnect (GstPad * pad, GstPad * peer)
268 ogg_mux = GST_OGG_MUX (gst_pad_get_parent (pad));
270 GST_DEBUG_OBJECT (ogg_mux, "sinkconnect triggered on %s", GST_PAD_NAME (pad));
272 gst_object_unref (ogg_mux);
274 return GST_PAD_LINK_OK;
278 gst_ogg_mux_sink_event (GstPad * pad, GstEvent * event)
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;
284 GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
286 switch (GST_EVENT_TYPE (event)) {
287 case GST_EVENT_SEGMENT:
289 const GstSegment *segment;
291 gst_event_parse_segment (event, &segment);
293 /* We don't support non time NEWSEGMENT events */
294 if (segment->format != GST_FORMAT_TIME) {
295 gst_event_unref (event);
300 gst_segment_copy_into (segment, &ogg_pad->segment);
303 case GST_EVENT_FLUSH_STOP:{
304 gst_segment_init (&ogg_pad->segment, GST_FORMAT_TIME);
311 /* now GstCollectPads can take care of the rest, e.g. EOS */
313 ret = ogg_pad->collect_event (pad, event);
315 gst_object_unref (ogg_mux);
320 gst_ogg_mux_is_serialno_present (GstOggMux * ogg_mux, guint32 serialno)
324 walk = ogg_mux->collect->data;
326 GstOggPadData *pad = (GstOggPadData *) walk->data;
327 if (pad->map.serialno == serialno)
336 gst_ogg_mux_generate_serialno (GstOggMux * ogg_mux)
341 serialno = g_random_int_range (0, G_MAXINT32);
342 } while (gst_ogg_mux_is_serialno_present (ogg_mux, serialno));
348 gst_ogg_mux_request_new_pad (GstElement * element,
349 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
353 GstElementClass *klass;
355 g_return_val_if_fail (templ != NULL, NULL);
357 if (templ->direction != GST_PAD_SINK)
358 goto wrong_direction;
360 g_return_val_if_fail (GST_IS_OGG_MUX (element), NULL);
361 ogg_mux = GST_OGG_MUX (element);
363 klass = GST_ELEMENT_GET_CLASS (element);
365 if (templ != gst_element_class_get_pad_template (klass, "sink_%d"))
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);
376 /* parse serial number from requested padname */
377 serial = atoi (&req_name[5]);
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);
385 /* construct our own wrapper data structure for the pad to
386 * keep track of its status */
388 GstOggPadData *oggpad;
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++;
395 oggpad->map.serialno = serial;
396 ogg_stream_init (&oggpad->map.stream, oggpad->map.serialno);
397 oggpad->packetno = 0;
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;
412 gst_segment_init (&oggpad->segment, GST_FORMAT_TIME);
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));
420 /* setup some pad functions */
421 gst_pad_set_link_function (newpad, gst_ogg_mux_sinkconnect);
423 /* dd the pad to the element */
424 gst_element_add_pad (element, newpad);
431 g_warning ("ogg_mux: request pad that is not a SINK pad\n");
436 g_warning ("ogg_mux: this is not our template!\n");
442 gst_ogg_mux_release_pad (GstElement * element, GstPad * pad)
446 ogg_mux = GST_OGG_MUX (gst_pad_get_parent (pad));
448 gst_collect_pads_remove_pad (ogg_mux->collect, pad);
449 gst_element_remove_pad (element, pad);
451 gst_object_unref (ogg_mux);
456 gst_ogg_mux_handle_src_event (GstPad * pad, GstEvent * event)
460 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
464 /* disable seeking for now */
470 return gst_pad_event_default (pad, event);
474 gst_ogg_mux_buffer_from_page (GstOggMux * mux, ogg_page * page, gboolean delta)
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);
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);
488 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
490 GST_LOG_OBJECT (mux, GST_GP_FORMAT
491 " created buffer %p from ogg page",
492 GST_GP_CAST (ogg_page_granulepos (page)), buffer);
498 gst_ogg_mux_push_buffer (GstOggMux * mux, GstBuffer * buffer,
499 GstOggPadData * oggpad)
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;
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;
512 mux->last_ts = run_time;
515 return gst_pad_push (mux->srcpad, buffer);
518 /* if all queues have at least one page, dequeue the page with the lowest
521 gst_ogg_mux_dequeue_page (GstOggMux * mux, GstFlowReturn * flowret)
524 GstOggPadData *opad = NULL; /* "oldest" pad */
525 GstClockTime oldest = GST_CLOCK_TIME_NONE;
526 GstBuffer *buf = NULL;
527 gboolean ret = FALSE;
529 *flowret = GST_FLOW_OK;
531 walk = mux->collect->data;
533 GstOggPadData *pad = (GstOggPadData *) walk->data;
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
539 if (pad->pagebuffers->length == 0) {
541 GST_LOG_OBJECT (pad->collect.pad,
542 "pad is EOS, skipping for dequeue decision");
544 GST_LOG_OBJECT (pad->collect.pad,
545 "no pages in this queue, can't dequeue");
549 /* We then need to check for a non-negative granulepos */
551 gboolean valid = FALSE;
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) {
563 GST_LOG_OBJECT (pad->collect.pad,
564 "No page timestamps in queue, can't dequeue");
569 walk = g_slist_next (walk);
572 walk = mux->collect->data;
574 GstOggPadData *pad = (GstOggPadData *) walk->data;
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);
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);
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);
606 walk = g_slist_next (walk);
609 if (oldest != GST_CLOCK_TIME_NONE) {
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);
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
628 * pad->timestamp_end was set with the timestamp + duration of the last packet
630 * pad->gp_time was set with the time matching the gp of the last
631 * packet put on the page
633 * will also reset timestamp and timestamp_end, so caller func can restart
637 gst_ogg_mux_pad_queue_page (GstOggMux * mux, GstOggPadData * pad,
638 ogg_page * page, gboolean delta)
641 GstBuffer *buffer = gst_ogg_mux_buffer_from_page (mux, page, delta);
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;
649 /* the next page will start where the current page's end time leaves off */
650 pad->timestamp = pad->timestamp_end;
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));
661 while (gst_ogg_mux_dequeue_page (mux, &ret)) {
662 if (ret != GST_FLOW_OK)
670 * Given two pads, compare the buffers queued on it.
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
679 gst_ogg_mux_compare_pads (GstOggMux * ogg_mux, GstOggPadData * first,
680 GstOggPadData * second)
682 guint64 firsttime, secondtime;
684 /* if the first pad doesn't contain anything or is even NULL, return
685 * the second pad as best candidate and vice versa */
691 /* no timestamp on first buffer, it must go first */
692 firsttime = GST_BUFFER_TIMESTAMP (first->buffer);
693 if (firsttime == GST_CLOCK_TIME_NONE)
696 /* no timestamp on second buffer, it must go first */
697 secondtime = GST_BUFFER_TIMESTAMP (second->buffer);
698 if (secondtime == GST_CLOCK_TIME_NONE)
701 /* first buffer has higher timestamp, second one should go first */
702 if (secondtime < firsttime)
704 /* second buffer has higher timestamp, first one should go first */
705 else if (secondtime > firsttime)
708 /* buffers with equal timestamps, prefer the pad that has the
709 * least number of pages muxed */
710 if (second->pageno < first->pageno)
712 else if (second->pageno > first->pageno)
716 /* same priority if all of the above failed */
721 gst_ogg_mux_decorate_buffer (GstOggMux * ogg_mux, GstOggPadData * pad,
725 gint64 duration, granule, limit;
726 GstClockTime next_time;
727 GstClockTimeDiff diff;
731 /* ensure messing with metadata is ok */
732 buf = gst_buffer_make_writable (buf);
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);
742 GST_BUFFER_TIMESTAMP (buf) = time;
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)
751 packet.packet = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
753 duration = gst_ogg_stream_get_packet_duration (&pad->map, &packet);
754 gst_buffer_unmap (buf, packet.packet, size);
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) {
764 GST_WARNING_OBJECT (pad->collect.pad,
765 "failed to determine packet duration");
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)),
774 /* determine granule corresponding to time,
775 * using the inverse of oggdemux' granule -> time */
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);
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);
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));
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);
801 if (pad->map.is_ogm || pad->map.is_sparse) {
802 pad->next_granule = granule;
805 pad->next_granule = granule;
808 /* track previous keyframe */
809 if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT))
810 pad->keyframe_granule = granule;
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);
823 GST_DEBUG_OBJECT (pad->collect.pad, "could not determine granulepos, "
824 "falling back to upstream provided metadata");
830 /* make sure at least one buffer is queued on all pads, two if possible
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
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)
846 GstOggPadData *bestpad = NULL;
851 /* try to make sure we have a buffer from each usable pad first */
852 walk = ogg_mux->collect->data;
855 GstCollectData *data;
857 data = (GstCollectData *) walk->data;
858 pad = (GstOggPadData *) data;
860 walk = g_slist_next (walk);
862 GST_LOG_OBJECT (data->pad, "looking at pad for buffer");
864 /* try to get a new buffer for this pad if needed and possible */
865 if (pad->buffer == NULL) {
868 buf = gst_collect_pads_pop (ogg_mux->collect, data);
869 GST_LOG_OBJECT (data->pad, "popped buffer %" GST_PTR_FORMAT, buf);
871 /* On EOS we get a NULL buffer */
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;
879 /* if we need headers */
880 if (pad->state == GST_OGG_PAD_STATE_CONTROL) {
881 /* and we have one */
886 packet.packet = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
889 /* if we're not yet in data mode, ensure we're setup on the first packet */
890 if (!pad->have_type) {
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));
899 gst_ogg_stream_setup_map_from_caps_headers (&pad->map, caps);
901 if (!pad->have_type) {
902 /* fallback on the packet */
903 pad->have_type = gst_ogg_stream_setup_map (&pad->map, &packet);
905 if (!pad->have_type) {
906 GST_ERROR_OBJECT (pad, "mapper didn't recognise input stream "
907 "(pad caps: %" GST_PTR_FORMAT ")", caps);
909 GST_DEBUG_OBJECT (pad, "caps detected: %" GST_PTR_FORMAT,
913 gst_caps_unref (caps);
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);
921 gst_buffer_unmap (buf, packet.packet, size);
924 GST_DEBUG_OBJECT (ogg_mux,
925 "got header buffer in control state, ignoring");
927 pad->map.n_header_packets_seen++;
928 gst_buffer_unref (buf);
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;
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;
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");
959 /* we should have a buffer now, see if it is the best pad to
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);
975 gst_ogg_mux_get_headers (GstOggPadData * pad)
978 GstStructure *structure;
982 thepad = pad->collect.pad;
984 GST_LOG_OBJECT (thepad, "getting headers");
986 caps = gst_pad_get_negotiated_caps (thepad);
988 const GValue *streamheader;
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);
998 GST_LOG_OBJECT (thepad, "got fixed list");
1000 for (i = 0; i < bufarr->len; i++) {
1001 GValue *bufval = &g_array_index (bufarr, GValue, i);
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);
1007 GST_LOG_OBJECT (thepad, "adding item %d to header list", i);
1009 gst_buffer_ref (buf);
1010 res = g_list_append (res, buf);
1014 GST_LOG_OBJECT (thepad, "streamheader is not fixed list");
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);
1023 pad->always_flush_page = TRUE;
1025 GST_LOG_OBJECT (thepad, "caps don't have streamheader");
1027 gst_caps_unref (caps);
1029 GST_LOG_OBJECT (thepad, "got empty caps as negotiated format");
1035 gst_ogg_mux_set_header_on_caps (GstCaps * caps, GList * buffers)
1037 GstStructure *structure;
1038 GValue array = { 0 };
1039 GList *walk = buffers;
1041 caps = gst_caps_make_writable (caps);
1043 structure = gst_caps_get_structure (caps, 0);
1045 /* put buffers in a fixed list */
1046 g_value_init (&array, GST_TYPE_ARRAY);
1049 GstBuffer *buf = GST_BUFFER (walk->data);
1051 GValue value = { 0 };
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);
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);
1067 gst_structure_set_value (structure, "streamheader", &array);
1068 g_value_unset (&array);
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.
1081 static GstFlowReturn
1082 gst_ogg_mux_send_headers (GstOggMux * mux)
1085 GList *hbufs, *hwalk;
1092 GST_LOG_OBJECT (mux, "collecting headers");
1094 walk = mux->collect->data;
1099 pad = (GstOggPadData *) walk->data;
1100 thepad = pad->collect.pad;
1102 walk = g_slist_next (walk);
1104 GST_LOG_OBJECT (mux, "looking at pad %s:%s", GST_DEBUG_PAD_NAME (thepad));
1106 /* if the pad has no buffer, we don't care */
1107 if (pad->buffer == NULL)
1110 /* now figure out the headers */
1111 pad->map.headers = gst_ogg_mux_get_headers (pad);
1114 GST_LOG_OBJECT (mux, "creating BOS pages");
1115 walk = mux->collect->data;
1123 GstStructure *structure;
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);
1136 GST_LOG_OBJECT (thepad, "looping over headers");
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) {
1143 gst_buffer_ref (buf);
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);
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++;
1165 /* swap the packet in */
1166 ogg_stream_packetin (&pad->map.stream, &packet);
1168 gst_buffer_unmap (buf, packet.packet, size);
1169 gst_buffer_unref (buf);
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");
1175 hbuf = gst_ogg_mux_buffer_from_page (mux, &page, FALSE);
1177 GST_LOG_OBJECT (mux, "swapped out page with mime type %s",
1178 gst_structure_get_name (structure));
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);
1197 hbufs = g_list_append (hbufs, hbuf);
1199 gst_caps_unref (caps);
1202 GST_LOG_OBJECT (mux, "creating next headers");
1203 walk = mux->collect->data;
1208 pad = (GstOggPadData *) walk->data;
1209 thepad = pad->collect.pad;
1213 GST_LOG_OBJECT (mux, "looping over headers for pad %s:%s",
1214 GST_DEBUG_PAD_NAME (thepad));
1216 hwalk = pad->map.headers;
1218 GstBuffer *buf = GST_BUFFER (hwalk->data);
1223 hwalk = hwalk->next;
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++;
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);
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);
1250 GST_LOG_OBJECT (mux, "swapped out page");
1251 hbufs = g_list_append (hbufs, hbuf);
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);
1259 GST_LOG_OBJECT (mux, "swapped out page");
1260 hbufs = g_list_append (hbufs, hbuf);
1264 g_list_free (pad->map.headers);
1265 pad->map.headers = NULL;
1267 /* hbufs holds all buffers for the headers now */
1269 /* create caps with the buffers */
1270 caps = gst_pad_get_caps (mux->srcpad, NULL);
1272 caps = gst_ogg_mux_set_header_on_caps (caps, hbufs);
1273 gst_pad_set_caps (mux->srcpad, caps);
1274 gst_caps_unref (caps);
1276 /* and send the buffers */
1277 while (hbufs != NULL) {
1278 GstBuffer *buf = GST_BUFFER (hbufs->data);
1280 hbufs = g_list_delete_link (hbufs, hbufs);
1282 if ((ret = gst_ogg_mux_push_buffer (mux, buf, NULL)) != GST_FLOW_OK)
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);
1292 /* this function is called to process data on the best pending pad.
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
1311 static GstFlowReturn
1312 gst_ogg_mux_process_best_pad (GstOggMux * ogg_mux, GstOggPadData * best)
1314 GstFlowReturn ret = GST_FLOW_OK;
1315 gboolean delta_unit;
1316 gint64 granulepos = 0;
1317 GstClockTime timestamp, gp_time;
1318 GstBuffer *next_buf;
1320 GST_LOG_OBJECT (ogg_mux, "best pad %" GST_PTR_FORMAT
1321 ", currently pulling from %" GST_PTR_FORMAT, best->collect.pad,
1324 next_buf = gst_collect_pads_peek (ogg_mux->collect, &best->collect);
1327 gst_buffer_unref (next_buf);
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);
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) {
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);
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,
1357 /* increment the page number counter */
1359 /* mark other pages as delta */
1360 pad->first_delta = TRUE;
1362 pad->new_page = TRUE;
1363 ogg_mux->pulling = NULL;
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");
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));
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;
1384 if (ogg_mux->need_headers) {
1385 ret = gst_ogg_mux_send_headers (ogg_mux);
1386 ogg_mux->need_headers = FALSE;
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) {
1394 GstBuffer *buf, *tmpbuf;
1395 GstOggPadData *pad = ogg_mux->pulling;
1397 gboolean force_flush;
1400 GST_LOG_OBJECT (ogg_mux->pulling->collect.pad, "pulling from pad");
1402 /* now see if we have a buffer */
1405 GST_DEBUG_OBJECT (ogg_mux, "pad was EOS");
1406 ogg_mux->pulling = NULL;
1410 delta_unit = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
1411 duration = GST_BUFFER_DURATION (buf);
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)));
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,
1435 packet.e_o_s = best->eos ? 1 : 0;
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) {
1449 if (GST_BUFFER_IS_DISCONT (buf)) {
1450 if (pad->data_pushed) {
1451 GST_LOG_OBJECT (pad->collect.pad, "got discont");
1453 /* No public API for this; hack things in */
1454 pad->map.stream.pageno++;
1457 GST_LOG_OBJECT (pad->collect.pad, "discont at stream start");
1461 /* flush the currently built page if necessary */
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,
1472 /* increment the page number counter */
1474 /* mark other pages as delta */
1475 pad->first_delta = TRUE;
1477 pad->new_page = TRUE;
1480 /* if this is the first packet of a new page figure out the delta flag */
1481 if (pad->new_page) {
1483 /* mark the page as delta */
1484 pad->first_delta = TRUE;
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;
1496 pad->first_delta = FALSE;
1499 pad->new_page = FALSE;
1502 /* save key unit to track delta->key unit transitions */
1503 pad->prev_delta = delta_unit;
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");
1511 ogg_stream_packetin (&pad->map.stream, &packet);
1512 gst_buffer_unmap (buf, packet.packet, size);
1513 pad->data_pushed = TRUE;
1515 gp_time = GST_BUFFER_OFFSET (pad->buffer);
1516 granulepos = GST_BUFFER_OFFSET_END (pad->buffer);
1517 timestamp = GST_BUFFER_TIMESTAMP (pad->buffer);
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;
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 */
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);
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,
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;
1555 GST_LOG_OBJECT (pad->collect.pad,
1557 " packet spans beyond current page, keeping old gp time %"
1558 GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (pad->gp_time));
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);
1566 /* mark next pages as delta */
1567 pad->first_delta = TRUE;
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;
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,
1583 /* increment the page number counter */
1586 /* need a new page as well */
1587 pad->new_page = TRUE;
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;
1594 /* Update the gp time, if necessary, since any future page will have at
1595 * least this gp time.
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));
1610 * Checks if all pads are EOS'd by peeking.
1612 * Returns TRUE if all pads are EOS.
1615 all_pads_eos (GstCollectPads * pads)
1621 GstOggPadData *oggpad = (GstOggPadData *) walk->data;
1623 GST_DEBUG ("oggpad %p eos %d", oggpad, oggpad->eos);
1625 if (oggpad->eos == FALSE)
1628 walk = g_slist_next (walk);
1634 /* This function is called when there is data on all pads.
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.
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.
1644 static GstFlowReturn
1645 gst_ogg_mux_collected (GstCollectPads * pads, GstOggMux * ogg_mux)
1647 GstOggPadData *best;
1651 GST_LOG_OBJECT (ogg_mux, "collected");
1653 /* queue buffers on all pads; find a buffer with the lowest timestamp */
1654 best = gst_ogg_mux_queue_pads (ogg_mux, &popped);
1659 if (best == NULL || best->buffer == NULL) {
1660 /* This is not supposed to happen */
1661 return GST_FLOW_ERROR;
1664 ret = gst_ogg_mux_process_best_pad (ogg_mux, best);
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;
1675 gst_ogg_mux_get_property (GObject * object,
1676 guint prop_id, GValue * value, GParamSpec * pspec)
1680 ogg_mux = GST_OGG_MUX (object);
1684 g_value_set_uint64 (value, ogg_mux->max_delay);
1686 case ARG_MAX_PAGE_DELAY:
1687 g_value_set_uint64 (value, ogg_mux->max_page_delay);
1689 case ARG_MAX_TOLERANCE:
1690 g_value_set_uint64 (value, ogg_mux->max_tolerance);
1693 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1699 gst_ogg_mux_set_property (GObject * object,
1700 guint prop_id, const GValue * value, GParamSpec * pspec)
1704 ogg_mux = GST_OGG_MUX (object);
1708 ogg_mux->max_delay = g_value_get_uint64 (value);
1710 case ARG_MAX_PAGE_DELAY:
1711 ogg_mux->max_page_delay = g_value_get_uint64 (value);
1713 case ARG_MAX_TOLERANCE:
1714 ogg_mux->max_tolerance = g_value_get_uint64 (value);
1717 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1722 /* reset all variables in the ogg pads. */
1724 gst_ogg_mux_init_collectpads (GstCollectPads * collect)
1728 walk = collect->data;
1730 GstOggPadData *oggpad = (GstOggPadData *) walk->data;
1732 ogg_stream_init (&oggpad->map.stream, oggpad->map.serialno);
1733 oggpad->packetno = 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 ();
1744 gst_segment_init (&oggpad->segment, GST_FORMAT_TIME);
1746 walk = g_slist_next (walk);
1750 /* Clear all buffers from the collectpads object */
1752 gst_ogg_mux_clear_collectpads (GstCollectPads * collect)
1756 for (walk = collect->data; walk; walk = g_slist_next (walk)) {
1757 GstOggPadData *oggpad = (GstOggPadData *) walk->data;
1760 ogg_stream_clear (&oggpad->map.stream);
1762 while ((buf = g_queue_pop_head (oggpad->pagebuffers)) != NULL) {
1763 gst_buffer_unref (buf);
1765 g_queue_free (oggpad->pagebuffers);
1766 oggpad->pagebuffers = NULL;
1768 if (oggpad->buffer) {
1769 gst_buffer_unref (oggpad->buffer);
1770 oggpad->buffer = NULL;
1773 gst_segment_init (&oggpad->segment, GST_FORMAT_TIME);
1777 static GstStateChangeReturn
1778 gst_ogg_mux_change_state (GstElement * element, GstStateChange transition)
1781 GstStateChangeReturn ret;
1783 ogg_mux = GST_OGG_MUX (element);
1785 switch (transition) {
1786 case GST_STATE_CHANGE_NULL_TO_READY:
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);
1793 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1795 case GST_STATE_CHANGE_PAUSED_TO_READY:
1796 gst_collect_pads_stop (ogg_mux->collect);
1802 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1804 switch (transition) {
1805 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1807 case GST_STATE_CHANGE_PAUSED_TO_READY:
1808 gst_ogg_mux_clear_collectpads (ogg_mux->collect);
1810 case GST_STATE_CHANGE_READY_TO_NULL:
1820 gst_ogg_mux_plugin_init (GstPlugin * plugin)
1822 GST_DEBUG_CATEGORY_INIT (gst_ogg_mux_debug, "oggmux", 0, "ogg muxer");
1824 return gst_element_register (plugin, "oggmux", GST_RANK_PRIMARY,