1 /* GStreamer Muxer bin that splits output stream by size/time
2 * Copyright (C) <2014> Jan Schmidt <jan@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-splitmuxsink
22 * @short_description: Muxer wrapper for splitting output stream by size or time
24 * This element wraps a muxer and a sink, and starts a new file when the mux
25 * contents are about to cross a threshold of maximum size of maximum time,
26 * splitting at video keyframe boundaries. Exactly one input video stream
27 * can be muxed, with as many accompanying audio and subtitle streams as
30 * By default, it uses mp4mux and filesink, but they can be changed via
31 * the 'muxer' and 'sink' properties.
33 * The minimum file size is 1 GOP, however - so limits may be overrun if the
34 * distance between any 2 keyframes is larger than the limits.
36 * If a video stream is available, the splitting process is driven by the video
37 * stream contents, and the video stream must contain closed GOPs for the output
38 * file parts to be played individually correctly. In the absence of a video
39 * stream, the first available stream is used as reference for synchronization.
42 * <title>Example pipelines</title>
44 * gst-launch-1.0 -e v4l2src num-buffers=500 ! video/x-raw,width=320,height=240 ! videoconvert ! queue ! timeoverlay ! x264enc key-int-max=10 ! h264parse ! splitmuxsink location=video%02d.mov max-size-time=10000000000 max-size-bytes=1000000
46 * Records a video stream captured from a v4l2 device and muxes it into
47 * ISO mp4 files, splitting as needed to limit size/duration to 10 seconds
48 * and 1MB maximum size.
57 #include "gstsplitmuxsink.h"
59 GST_DEBUG_CATEGORY_STATIC (splitmux_debug);
60 #define GST_CAT_DEFAULT splitmux_debug
62 #define GST_SPLITMUX_LOCK(s) g_mutex_lock(&(s)->lock)
63 #define GST_SPLITMUX_UNLOCK(s) g_mutex_unlock(&(s)->lock)
64 #define GST_SPLITMUX_WAIT(s) g_cond_wait (&(s)->data_cond, &(s)->lock)
65 #define GST_SPLITMUX_BROADCAST(s) g_cond_broadcast (&(s)->data_cond)
78 #define DEFAULT_MAX_SIZE_TIME 0
79 #define DEFAULT_MAX_SIZE_BYTES 0
80 #define DEFAULT_MUXER_OVERHEAD 0.02
81 #define DEFAULT_MUXER "mp4mux"
82 #define DEFAULT_SINK "filesink"
86 SIGNAL_FORMAT_LOCATION,
90 static guint signals[SIGNAL_LAST];
92 static GstStaticPadTemplate video_sink_template =
93 GST_STATIC_PAD_TEMPLATE ("video",
97 static GstStaticPadTemplate audio_sink_template =
98 GST_STATIC_PAD_TEMPLATE ("audio_%u",
101 GST_STATIC_CAPS_ANY);
102 static GstStaticPadTemplate subtitle_sink_template =
103 GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
106 GST_STATIC_CAPS_ANY);
108 static GQuark PAD_CONTEXT;
113 PAD_CONTEXT = g_quark_from_static_string ("pad-context");
116 #define gst_splitmux_sink_parent_class parent_class
117 G_DEFINE_TYPE_EXTENDED (GstSplitMuxSink, gst_splitmux_sink, GST_TYPE_BIN, 0,
120 static gboolean create_elements (GstSplitMuxSink * splitmux);
121 static gboolean create_sink (GstSplitMuxSink * splitmux);
122 static void gst_splitmux_sink_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_splitmux_sink_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
126 static void gst_splitmux_sink_dispose (GObject * object);
127 static void gst_splitmux_sink_finalize (GObject * object);
129 static GstPad *gst_splitmux_sink_request_new_pad (GstElement * element,
130 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
131 static void gst_splitmux_sink_release_pad (GstElement * element, GstPad * pad);
133 static GstStateChangeReturn gst_splitmux_sink_change_state (GstElement *
134 element, GstStateChange transition);
136 static void bus_handler (GstBin * bin, GstMessage * msg);
137 static void set_next_filename (GstSplitMuxSink * splitmux);
138 static void start_next_fragment (GstSplitMuxSink * splitmux);
139 static void check_queue_length (GstSplitMuxSink * splitmux, MqStreamCtx * ctx);
140 static void mq_stream_ctx_unref (MqStreamCtx * ctx);
143 mq_stream_buf_new (void)
145 return g_slice_new0 (MqStreamBuf);
149 mq_stream_buf_free (MqStreamBuf * data)
151 g_slice_free (MqStreamBuf, data);
155 gst_splitmux_sink_class_init (GstSplitMuxSinkClass * klass)
157 GObjectClass *gobject_class = (GObjectClass *) klass;
158 GstElementClass *gstelement_class = (GstElementClass *) klass;
159 GstBinClass *gstbin_class = (GstBinClass *) klass;
161 gobject_class->set_property = gst_splitmux_sink_set_property;
162 gobject_class->get_property = gst_splitmux_sink_get_property;
163 gobject_class->dispose = gst_splitmux_sink_dispose;
164 gobject_class->finalize = gst_splitmux_sink_finalize;
166 gst_element_class_set_static_metadata (gstelement_class,
167 "Split Muxing Bin", "Generic/Bin/Muxer",
168 "Convenience bin that muxes incoming streams into multiple time/size limited files",
169 "Jan Schmidt <jan@centricular.com>");
171 gst_element_class_add_pad_template (gstelement_class,
172 gst_static_pad_template_get (&video_sink_template));
173 gst_element_class_add_pad_template (gstelement_class,
174 gst_static_pad_template_get (&audio_sink_template));
175 gst_element_class_add_pad_template (gstelement_class,
176 gst_static_pad_template_get (&subtitle_sink_template));
178 gstelement_class->change_state =
179 GST_DEBUG_FUNCPTR (gst_splitmux_sink_change_state);
180 gstelement_class->request_new_pad =
181 GST_DEBUG_FUNCPTR (gst_splitmux_sink_request_new_pad);
182 gstelement_class->release_pad =
183 GST_DEBUG_FUNCPTR (gst_splitmux_sink_release_pad);
185 gstbin_class->handle_message = bus_handler;
187 g_object_class_install_property (gobject_class, PROP_LOCATION,
188 g_param_spec_string ("location", "File Output Pattern",
189 "Format string pattern for the location of the files to write (e.g. video%05d.mp4)",
190 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (gobject_class, PROP_MUXER_OVERHEAD,
192 g_param_spec_double ("mux-overhead", "Muxing Overhead",
193 "Extra size overhead of muxing (0.02 = 2%)", 0.0, 1.0,
194 DEFAULT_MUXER_OVERHEAD,
195 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
198 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
199 "Max. amount of time per file (in ns, 0=disable)", 0, G_MAXUINT64,
200 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
202 g_param_spec_uint64 ("max-size-bytes", "Max. size bytes",
203 "Max. amount of data per file (in bytes, 0=disable)", 0, G_MAXUINT64,
204 DEFAULT_MAX_SIZE_BYTES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (gobject_class, PROP_MUXER,
207 g_param_spec_object ("muxer", "Muxer",
208 "The muxer element to use (NULL = default mp4mux)",
209 GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 g_object_class_install_property (gobject_class, PROP_SINK,
211 g_param_spec_object ("sink", "Sink",
212 "The sink element (or element chain) to use (NULL = default filesink)",
213 GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 * GstSplitMuxSink::format-location:
217 * @splitmux: the #GstSplitMuxSink
218 * @fragment_id: the sequence number of the file to be created
220 * Returns: the location to be used for the next output file
222 signals[SIGNAL_FORMAT_LOCATION] =
223 g_signal_new ("format-location", G_TYPE_FROM_CLASS (klass),
224 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_STRING, 1, G_TYPE_UINT);
228 gst_splitmux_sink_init (GstSplitMuxSink * splitmux)
230 g_mutex_init (&splitmux->lock);
231 g_cond_init (&splitmux->data_cond);
233 splitmux->mux_overhead = DEFAULT_MUXER_OVERHEAD;
234 splitmux->threshold_time = DEFAULT_MAX_SIZE_TIME;
235 splitmux->threshold_bytes = DEFAULT_MAX_SIZE_BYTES;
237 GST_OBJECT_FLAG_SET (splitmux, GST_ELEMENT_FLAG_SINK);
241 gst_splitmux_reset (GstSplitMuxSink * splitmux)
244 gst_bin_remove (GST_BIN (splitmux), splitmux->mq);
246 gst_bin_remove (GST_BIN (splitmux), splitmux->muxer);
247 if (splitmux->active_sink)
248 gst_bin_remove (GST_BIN (splitmux), splitmux->active_sink);
250 splitmux->sink = splitmux->active_sink = splitmux->muxer = splitmux->mq =
255 gst_splitmux_sink_dispose (GObject * object)
257 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (object);
259 G_OBJECT_CLASS (parent_class)->dispose (object);
261 /* Calling parent dispose invalidates all child pointers */
262 splitmux->sink = splitmux->active_sink = splitmux->muxer = splitmux->mq =
267 gst_splitmux_sink_finalize (GObject * object)
269 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (object);
270 g_cond_clear (&splitmux->data_cond);
271 g_mutex_clear (&splitmux->lock);
272 if (splitmux->provided_sink)
273 gst_object_unref (splitmux->provided_sink);
274 if (splitmux->provided_muxer)
275 gst_object_unref (splitmux->provided_muxer);
277 g_free (splitmux->location);
279 /* Make sure to free any un-released contexts */
280 g_list_foreach (splitmux->contexts, (GFunc) mq_stream_ctx_unref, NULL);
281 g_list_free (splitmux->contexts);
283 G_OBJECT_CLASS (parent_class)->finalize (object);
287 gst_splitmux_sink_set_property (GObject * object, guint prop_id,
288 const GValue * value, GParamSpec * pspec)
290 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (object);
294 GST_OBJECT_LOCK (splitmux);
295 g_free (splitmux->location);
296 splitmux->location = g_value_dup_string (value);
297 GST_OBJECT_UNLOCK (splitmux);
300 case PROP_MAX_SIZE_BYTES:
301 GST_OBJECT_LOCK (splitmux);
302 splitmux->threshold_bytes = g_value_get_uint64 (value);
303 GST_OBJECT_UNLOCK (splitmux);
305 case PROP_MAX_SIZE_TIME:
306 GST_OBJECT_LOCK (splitmux);
307 splitmux->threshold_time = g_value_get_uint64 (value);
308 GST_OBJECT_UNLOCK (splitmux);
310 case PROP_MUXER_OVERHEAD:
311 GST_OBJECT_LOCK (splitmux);
312 splitmux->mux_overhead = g_value_get_double (value);
313 GST_OBJECT_UNLOCK (splitmux);
316 GST_OBJECT_LOCK (splitmux);
317 if (splitmux->provided_sink)
318 gst_object_unref (splitmux->provided_sink);
319 splitmux->provided_sink = g_value_dup_object (value);
320 GST_OBJECT_UNLOCK (splitmux);
323 GST_OBJECT_LOCK (splitmux);
324 if (splitmux->provided_muxer)
325 gst_object_unref (splitmux->provided_muxer);
326 splitmux->provided_muxer = g_value_dup_object (value);
327 GST_OBJECT_UNLOCK (splitmux);
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 gst_splitmux_sink_get_property (GObject * object, guint prop_id,
337 GValue * value, GParamSpec * pspec)
339 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (object);
343 GST_OBJECT_LOCK (splitmux);
344 g_value_set_string (value, splitmux->location);
345 GST_OBJECT_UNLOCK (splitmux);
347 case PROP_MAX_SIZE_BYTES:
348 GST_OBJECT_LOCK (splitmux);
349 g_value_set_uint64 (value, splitmux->threshold_bytes);
350 GST_OBJECT_UNLOCK (splitmux);
352 case PROP_MAX_SIZE_TIME:
353 GST_OBJECT_LOCK (splitmux);
354 g_value_set_uint64 (value, splitmux->threshold_time);
355 GST_OBJECT_UNLOCK (splitmux);
357 case PROP_MUXER_OVERHEAD:
358 GST_OBJECT_LOCK (splitmux);
359 g_value_set_double (value, splitmux->mux_overhead);
360 GST_OBJECT_UNLOCK (splitmux);
363 GST_OBJECT_LOCK (splitmux);
364 g_value_set_object (value, splitmux->provided_sink);
365 GST_OBJECT_UNLOCK (splitmux);
368 GST_OBJECT_LOCK (splitmux);
369 g_value_set_object (value, splitmux->provided_muxer);
370 GST_OBJECT_UNLOCK (splitmux);
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
379 mq_sink_to_src (GstElement * mq, GstPad * sink_pad)
381 gchar *tmp, *sinkname, *srcname;
384 sinkname = gst_pad_get_name (sink_pad);
386 srcname = g_strdup_printf ("src_%s", tmp);
388 mq_src = gst_element_get_static_pad (mq, srcname);
397 get_pads_from_mq (GstSplitMuxSink * splitmux, GstPad ** sink_pad,
403 /* Request a pad from multiqueue, then connect this one, then
404 * discover the corresponding output pad and return both */
405 mq_sink = gst_element_get_request_pad (splitmux->mq, "sink_%u");
409 mq_src = mq_sink_to_src (splitmux->mq, mq_sink);
419 gst_element_release_request_pad (splitmux->mq, mq_sink);
424 mq_stream_ctx_new (GstSplitMuxSink * splitmux)
428 ctx = g_new0 (MqStreamCtx, 1);
429 g_atomic_int_set (&ctx->refcount, 1);
430 ctx->splitmux = splitmux;
431 gst_segment_init (&ctx->in_segment, GST_FORMAT_UNDEFINED);
432 gst_segment_init (&ctx->out_segment, GST_FORMAT_UNDEFINED);
433 ctx->in_running_time = ctx->out_running_time = 0;
434 g_queue_init (&ctx->queued_bufs);
439 mq_stream_ctx_free (MqStreamCtx * ctx)
441 g_queue_foreach (&ctx->queued_bufs, (GFunc) mq_stream_buf_free, NULL);
442 g_queue_clear (&ctx->queued_bufs);
447 mq_stream_ctx_unref (MqStreamCtx * ctx)
449 if (g_atomic_int_dec_and_test (&ctx->refcount))
450 mq_stream_ctx_free (ctx);
454 mq_stream_ctx_ref (MqStreamCtx * ctx)
456 g_atomic_int_inc (&ctx->refcount);
460 _pad_block_destroy_sink_notify (MqStreamCtx * ctx)
462 ctx->sink_pad_block_id = 0;
463 mq_stream_ctx_unref (ctx);
467 _pad_block_destroy_src_notify (MqStreamCtx * ctx)
469 ctx->src_pad_block_id = 0;
470 mq_stream_ctx_unref (ctx);
473 /* Called with lock held, drops the lock to send EOS to the
477 send_eos (GstSplitMuxSink * splitmux, MqStreamCtx * ctx)
482 eos = gst_event_new_eos ();
483 pad = gst_pad_get_peer (ctx->srcpad);
487 GST_INFO_OBJECT (splitmux, "Sending EOS on %" GST_PTR_FORMAT, pad);
488 GST_SPLITMUX_UNLOCK (splitmux);
489 gst_pad_send_event (pad, eos);
490 GST_SPLITMUX_LOCK (splitmux);
492 gst_object_unref (pad);
495 /* Called with splitmux lock held to check if this output
496 * context needs to sleep to wait for the release of the
497 * next GOP, or to send EOS to close out the current file
500 complete_or_wait_on_out (GstSplitMuxSink * splitmux, MqStreamCtx * ctx)
504 GST_LOG_OBJECT (ctx->srcpad,
505 "Checking running time %" GST_TIME_FORMAT " against max %"
506 GST_TIME_FORMAT, GST_TIME_ARGS (ctx->out_running_time),
507 GST_TIME_ARGS (splitmux->max_out_running_time));
509 if (splitmux->max_out_running_time == GST_CLOCK_TIME_NONE ||
510 ctx->out_running_time < splitmux->max_out_running_time) {
511 splitmux->have_muxed_something = TRUE;
515 if (ctx->flushing || splitmux->state == SPLITMUX_STATE_STOPPED)
518 if (splitmux->state == SPLITMUX_STATE_ENDING_FILE) {
519 if (ctx->out_eos == FALSE) {
520 send_eos (splitmux, ctx);
523 } else if (splitmux->state == SPLITMUX_STATE_START_NEXT_FRAGMENT) {
524 start_next_fragment (splitmux);
528 GST_INFO_OBJECT (ctx->srcpad,
529 "Sleeping for running time %"
530 GST_TIME_FORMAT " (max %" GST_TIME_FORMAT ")",
531 GST_TIME_ARGS (ctx->out_running_time),
532 GST_TIME_ARGS (splitmux->max_out_running_time));
533 ctx->out_blocked = TRUE;
534 /* Expand the mq if needed before sleeping */
535 check_queue_length (splitmux, ctx);
536 GST_SPLITMUX_WAIT (splitmux);
537 ctx->out_blocked = FALSE;
538 GST_INFO_OBJECT (ctx->srcpad,
539 "Woken for new max running time %" GST_TIME_FORMAT,
540 GST_TIME_ARGS (splitmux->max_out_running_time));
544 static GstPadProbeReturn
545 handle_mq_output (GstPad * pad, GstPadProbeInfo * info, MqStreamCtx * ctx)
547 GstSplitMuxSink *splitmux = ctx->splitmux;
548 MqStreamBuf *buf_info = NULL;
550 GST_LOG_OBJECT (pad, "Fired probe type 0x%x\n", info->type);
552 /* FIXME: Handle buffer lists, until then make it clear they won't work */
553 if (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST) {
554 g_warning ("Buffer list handling not implemented");
555 return GST_PAD_PROBE_DROP;
557 if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
558 GstEvent *event = gst_pad_probe_info_get_event (info);
560 GST_LOG_OBJECT (pad, "Event %" GST_PTR_FORMAT, event);
562 switch (GST_EVENT_TYPE (event)) {
563 case GST_EVENT_SEGMENT:
564 gst_event_copy_segment (event, &ctx->out_segment);
566 case GST_EVENT_FLUSH_STOP:
567 GST_SPLITMUX_LOCK (splitmux);
568 gst_segment_init (&ctx->out_segment, GST_FORMAT_UNDEFINED);
569 g_queue_foreach (&ctx->queued_bufs, (GFunc) mq_stream_buf_free, NULL);
570 g_queue_clear (&ctx->queued_bufs);
571 ctx->flushing = FALSE;
572 GST_SPLITMUX_UNLOCK (splitmux);
574 case GST_EVENT_FLUSH_START:
575 GST_SPLITMUX_LOCK (splitmux);
576 GST_LOG_OBJECT (pad, "Flush start");
577 ctx->flushing = TRUE;
578 GST_SPLITMUX_BROADCAST (splitmux);
579 GST_SPLITMUX_UNLOCK (splitmux);
582 GST_SPLITMUX_LOCK (splitmux);
583 if (splitmux->state == SPLITMUX_STATE_STOPPED)
586 GST_SPLITMUX_UNLOCK (splitmux);
591 gst_event_parse_gap (event, &gap_ts, NULL);
592 if (gap_ts == GST_CLOCK_TIME_NONE)
595 GST_SPLITMUX_LOCK (splitmux);
597 gap_ts = gst_segment_to_running_time (&ctx->out_segment,
598 GST_FORMAT_TIME, gap_ts);
600 GST_LOG_OBJECT (pad, "Have GAP w/ ts %" GST_TIME_FORMAT,
601 GST_TIME_ARGS (gap_ts));
603 if (splitmux->state == SPLITMUX_STATE_STOPPED)
605 ctx->out_running_time = gap_ts;
606 complete_or_wait_on_out (splitmux, ctx);
607 GST_SPLITMUX_UNLOCK (splitmux);
613 return GST_PAD_PROBE_PASS;
616 /* Allow everything through until the configured next stopping point */
617 GST_SPLITMUX_LOCK (splitmux);
619 buf_info = g_queue_pop_tail (&ctx->queued_bufs);
620 if (buf_info == NULL)
621 /* Can only happen due to a poorly timed flush */
624 /* If we have popped a keyframe, decrement the queued_gop count */
625 if (buf_info->keyframe && splitmux->queued_gops > 0)
626 splitmux->queued_gops--;
628 ctx->out_running_time = buf_info->run_ts;
630 GST_LOG_OBJECT (splitmux,
631 "Pad %" GST_PTR_FORMAT " buffer with TS %" GST_TIME_FORMAT
632 " size %" G_GSIZE_FORMAT,
633 pad, GST_TIME_ARGS (ctx->out_running_time), buf_info->buf_size);
635 complete_or_wait_on_out (splitmux, ctx);
637 if (splitmux->muxed_out_time == GST_CLOCK_TIME_NONE ||
638 splitmux->muxed_out_time < buf_info->run_ts)
639 splitmux->muxed_out_time = buf_info->run_ts;
641 splitmux->muxed_out_bytes += buf_info->buf_size;
643 #ifndef GST_DISABLE_GST_DEBUG
645 GstBuffer *buf = gst_pad_probe_info_get_buffer (info);
646 GST_LOG_OBJECT (pad, "Returning to pass buffer %" GST_PTR_FORMAT
647 " run ts %" GST_TIME_FORMAT, buf,
648 GST_TIME_ARGS (ctx->out_running_time));
652 GST_SPLITMUX_UNLOCK (splitmux);
654 mq_stream_buf_free (buf_info);
656 return GST_PAD_PROBE_PASS;
659 GST_SPLITMUX_UNLOCK (splitmux);
660 return GST_PAD_PROBE_DROP;
664 resend_sticky (GstPad * pad, GstEvent ** event, GstPad * peer)
666 return gst_pad_send_event (peer, gst_event_ref (*event));
670 restart_context (MqStreamCtx * ctx, GstSplitMuxSink * splitmux)
672 GstPad *peer = gst_pad_get_peer (ctx->srcpad);
674 gst_pad_sticky_events_foreach (ctx->srcpad,
675 (GstPadStickyEventsForeachFunction) (resend_sticky), peer);
678 ctx->out_eos = FALSE;
680 gst_object_unref (peer);
683 /* Called with lock held when a fragment
684 * reaches EOS and it is time to restart
688 start_next_fragment (GstSplitMuxSink * splitmux)
690 /* 1 change to new file */
691 gst_element_set_state (splitmux->muxer, GST_STATE_NULL);
692 gst_element_set_state (splitmux->active_sink, GST_STATE_NULL);
694 set_next_filename (splitmux);
696 gst_element_sync_state_with_parent (splitmux->active_sink);
697 gst_element_sync_state_with_parent (splitmux->muxer);
699 g_list_foreach (splitmux->contexts, (GFunc) restart_context, splitmux);
701 /* Switch state and go back to processing */
702 splitmux->state = SPLITMUX_STATE_COLLECTING_GOP_START;
704 if (!splitmux->reference_ctx->in_eos) {
705 splitmux->max_out_running_time = splitmux->reference_ctx->in_running_time;
707 splitmux->max_out_running_time = GST_CLOCK_TIME_NONE;
708 splitmux->have_muxed_something = FALSE;
710 splitmux->have_muxed_something =
711 (splitmux->reference_ctx->in_running_time > splitmux->muxed_out_time);
713 /* Store the overflow parameters as the basis for the next fragment */
714 splitmux->mux_start_time = splitmux->muxed_out_time;
715 splitmux->mux_start_bytes = splitmux->muxed_out_bytes;
717 GST_DEBUG_OBJECT (splitmux,
718 "Restarting flow for new fragment. New running time %" GST_TIME_FORMAT,
719 GST_TIME_ARGS (splitmux->max_out_running_time));
721 GST_SPLITMUX_BROADCAST (splitmux);
725 bus_handler (GstBin * bin, GstMessage * message)
727 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (bin);
729 switch (GST_MESSAGE_TYPE (message)) {
730 case GST_MESSAGE_EOS:
731 /* If the state is draining out the current file, drop this EOS */
732 GST_SPLITMUX_LOCK (splitmux);
733 if (splitmux->state == SPLITMUX_STATE_ENDING_FILE &&
734 splitmux->max_out_running_time != GST_CLOCK_TIME_NONE) {
735 GST_DEBUG_OBJECT (splitmux, "Caught EOS at end of fragment, dropping");
736 splitmux->state = SPLITMUX_STATE_START_NEXT_FRAGMENT;
737 GST_SPLITMUX_BROADCAST (splitmux);
739 gst_message_unref (message);
740 GST_SPLITMUX_UNLOCK (splitmux);
743 GST_SPLITMUX_UNLOCK (splitmux);
749 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
752 /* Called with splitmux lock held */
753 /* Called when entering ProcessingCompleteGop state
754 * Assess if mq contents overflowed the current file
755 * -> If yes, need to switch to new file
756 * -> if no, set max_out_running_time to let this GOP in and
757 * go to COLLECTING_GOP_START state
760 handle_gathered_gop (GstSplitMuxSink * splitmux)
763 gsize queued_bytes = 0;
764 GstClockTime queued_time = 0;
766 /* Assess if the multiqueue contents overflowed the current file */
767 for (cur = g_list_first (splitmux->contexts);
768 cur != NULL; cur = g_list_next (cur)) {
769 MqStreamCtx *tmpctx = (MqStreamCtx *) (cur->data);
770 if (tmpctx->in_running_time > queued_time)
771 queued_time = tmpctx->in_running_time;
772 queued_bytes += tmpctx->in_bytes;
775 g_assert (queued_bytes >= splitmux->mux_start_bytes);
776 g_assert (queued_time >= splitmux->mux_start_time);
778 queued_bytes -= splitmux->mux_start_bytes;
779 queued_time -= splitmux->mux_start_time;
781 /* Expand queued bytes estimate by muxer overhead */
782 queued_bytes += (queued_bytes * splitmux->mux_overhead);
784 GST_LOG_OBJECT (splitmux, "mq at TS %" GST_TIME_FORMAT
785 " bytes %" G_GSIZE_FORMAT, GST_TIME_ARGS (queued_time), queued_bytes);
787 /* Check for overrun - have we output at least one byte and overrun
788 * either threshold? */
789 if (splitmux->have_muxed_something &&
790 ((splitmux->threshold_bytes > 0 &&
791 queued_bytes >= splitmux->threshold_bytes) ||
792 (splitmux->threshold_time > 0 &&
793 queued_time >= splitmux->threshold_time))) {
795 splitmux->state = SPLITMUX_STATE_ENDING_FILE;
797 GST_INFO_OBJECT (splitmux,
798 "mq overflowed since last, draining out. max out TS is %"
799 GST_TIME_FORMAT, GST_TIME_ARGS (splitmux->max_out_running_time));
800 GST_SPLITMUX_BROADCAST (splitmux);
804 GST_LOG_OBJECT (splitmux,
805 "This GOP didn't overflow the fragment. Bytes sent %" G_GSIZE_FORMAT
806 " queued %" G_GSIZE_FORMAT " time %" GST_TIME_FORMAT " Continuing.",
807 splitmux->muxed_out_bytes - splitmux->mux_start_bytes,
808 queued_bytes, GST_TIME_ARGS (queued_time));
810 /* Wake everyone up to push this one GOP, then sleep */
811 splitmux->state = SPLITMUX_STATE_COLLECTING_GOP_START;
812 splitmux->have_muxed_something = TRUE;
814 if (!splitmux->reference_ctx->in_eos)
815 splitmux->max_out_running_time = splitmux->reference_ctx->in_running_time;
817 splitmux->max_out_running_time = GST_CLOCK_TIME_NONE;
819 GST_LOG_OBJECT (splitmux, "Waking output for complete GOP, TS %"
820 GST_TIME_FORMAT, GST_TIME_ARGS (splitmux->max_out_running_time));
821 GST_SPLITMUX_BROADCAST (splitmux);
826 /* Called with splitmux lock held */
827 /* Called from each input pad when it is has all the pieces
828 * for a GOP or EOS, starting with the reference pad which has set the
829 * splitmux->max_in_running_time
832 check_completed_gop (GstSplitMuxSink * splitmux, MqStreamCtx * ctx)
835 gboolean ready = TRUE;
836 GstClockTime current_max_in_running_time;
838 if (splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE) {
839 /* Iterate each pad, and check that the input running time is at least
840 * up to the reference running time, and if so handle the collected GOP */
841 GST_LOG_OBJECT (splitmux, "Checking GOP collected, ctx %p", ctx);
842 for (cur = g_list_first (splitmux->contexts);
843 cur != NULL; cur = g_list_next (cur)) {
844 MqStreamCtx *tmpctx = (MqStreamCtx *) (cur->data);
846 GST_LOG_OBJECT (splitmux,
847 "Context %p (src pad %" GST_PTR_FORMAT ") TS %" GST_TIME_FORMAT
848 " EOS %d", tmpctx, tmpctx->srcpad,
849 GST_TIME_ARGS (tmpctx->in_running_time), tmpctx->in_eos);
851 if (tmpctx->in_running_time < splitmux->max_in_running_time &&
853 GST_LOG_OBJECT (splitmux,
854 "Context %p (src pad %" GST_PTR_FORMAT ") not ready. We'll sleep",
855 tmpctx, tmpctx->srcpad);
861 GST_DEBUG_OBJECT (splitmux,
862 "Collected GOP is complete. Processing (ctx %p)", ctx);
863 /* All pads have a complete GOP, release it into the multiqueue */
864 handle_gathered_gop (splitmux);
868 /* Some pad is not yet ready, or GOP is being pushed
869 * either way, sleep and wait to get woken */
870 current_max_in_running_time = splitmux->max_in_running_time;
871 while ((splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE ||
872 splitmux->state == SPLITMUX_STATE_START_NEXT_FRAGMENT) &&
874 (current_max_in_running_time == splitmux->max_in_running_time)) {
876 GST_LOG_OBJECT (splitmux, "Sleeping for %s (ctx %p)",
877 splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE ?
878 "GOP complete" : "EOF draining", ctx);
879 GST_SPLITMUX_WAIT (splitmux);
881 GST_LOG_OBJECT (splitmux, "Done waiting for complete GOP (ctx %p)", ctx);
886 check_queue_length (GstSplitMuxSink * splitmux, MqStreamCtx * ctx)
889 guint cur_len = g_queue_get_length (&ctx->queued_bufs);
891 GST_DEBUG_OBJECT (ctx->sinkpad,
892 "Checking queue length len %u cur_max %u queued gops %u",
893 cur_len, splitmux->mq_max_buffers, splitmux->queued_gops);
895 if (cur_len >= splitmux->mq_max_buffers) {
896 gboolean allow_grow = FALSE;
898 /* If collecting a GOP and this pad might block,
899 * and there isn't already a pending GOP in the queue
902 if (splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE &&
903 ctx->in_running_time < splitmux->max_in_running_time &&
904 splitmux->queued_gops <= 1) {
906 } else if (splitmux->state == SPLITMUX_STATE_COLLECTING_GOP_START &&
907 ctx->is_reference && splitmux->queued_gops <= 1) {
912 for (cur = g_list_first (splitmux->contexts);
913 cur != NULL; cur = g_list_next (cur)) {
914 MqStreamCtx *tmpctx = (MqStreamCtx *) (cur->data);
915 GST_DEBUG_OBJECT (tmpctx->sinkpad,
916 " len %u out_blocked %d",
917 g_queue_get_length (&tmpctx->queued_bufs), tmpctx->out_blocked);
918 /* If another stream is starving, grow */
919 if (tmpctx != ctx && g_queue_get_length (&tmpctx->queued_bufs) < 1) {
926 splitmux->mq_max_buffers = cur_len + 1;
928 GST_INFO_OBJECT (splitmux,
929 "Multiqueue overrun - enlarging to %u buffers ctx %p",
930 splitmux->mq_max_buffers, ctx);
932 g_object_set (splitmux->mq, "max-size-buffers",
933 splitmux->mq_max_buffers, NULL);
938 static GstPadProbeReturn
939 handle_mq_input (GstPad * pad, GstPadProbeInfo * info, MqStreamCtx * ctx)
941 GstSplitMuxSink *splitmux = ctx->splitmux;
943 MqStreamBuf *buf_info = NULL;
946 gboolean keyframe = FALSE;
948 GST_LOG_OBJECT (pad, "Fired probe type 0x%x", info->type);
950 /* FIXME: Handle buffer lists, until then make it clear they won't work */
951 if (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST) {
952 g_warning ("Buffer list handling not implemented");
953 return GST_PAD_PROBE_DROP;
955 if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
956 GstEvent *event = gst_pad_probe_info_get_event (info);
957 switch (GST_EVENT_TYPE (event)) {
958 case GST_EVENT_SEGMENT:
959 gst_event_copy_segment (event, &ctx->in_segment);
961 case GST_EVENT_FLUSH_STOP:
962 GST_SPLITMUX_LOCK (splitmux);
963 gst_segment_init (&ctx->in_segment, GST_FORMAT_UNDEFINED);
966 ctx->in_running_time = 0;
967 GST_SPLITMUX_UNLOCK (splitmux);
970 GST_SPLITMUX_LOCK (splitmux);
973 if (splitmux->state == SPLITMUX_STATE_STOPPED)
976 if (ctx->is_reference) {
977 GST_INFO_OBJECT (splitmux, "Got Reference EOS. Finishing up");
978 /* Act as if this is a new keyframe with infinite timestamp */
979 splitmux->max_in_running_time = GST_CLOCK_TIME_NONE;
980 splitmux->state = SPLITMUX_STATE_WAITING_GOP_COMPLETE;
981 /* Wake up other input pads to collect this GOP */
982 GST_SPLITMUX_BROADCAST (splitmux);
983 check_completed_gop (splitmux, ctx);
984 } else if (splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE) {
985 /* If we are waiting for a GOP to be completed (ie, for aux
986 * pads to catch up), then this pad is complete, so check
987 * if the whole GOP is.
989 check_completed_gop (splitmux, ctx);
991 GST_SPLITMUX_UNLOCK (splitmux);
996 return GST_PAD_PROBE_PASS;
999 buf = gst_pad_probe_info_get_buffer (info);
1000 ctx->in_running_time = gst_segment_to_running_time (&ctx->in_segment,
1001 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
1002 buf_info = mq_stream_buf_new ();
1004 if (GST_BUFFER_PTS_IS_VALID (buf))
1005 ts = GST_BUFFER_PTS (buf);
1007 ts = GST_BUFFER_DTS (buf);
1009 GST_SPLITMUX_LOCK (splitmux);
1011 if (splitmux->state == SPLITMUX_STATE_STOPPED)
1014 /* If this buffer has a timestamp, advance the input timestamp of the
1016 if (GST_CLOCK_TIME_IS_VALID (ts)) {
1017 GstClockTime running_time =
1018 gst_segment_to_running_time (&ctx->in_segment, GST_FORMAT_TIME,
1019 GST_BUFFER_TIMESTAMP (buf));
1021 if (GST_CLOCK_TIME_IS_VALID (running_time) &&
1022 (ctx->in_running_time == GST_CLOCK_TIME_NONE
1023 || running_time > ctx->in_running_time))
1024 ctx->in_running_time = running_time;
1027 /* Try to make sure we have a valid running time */
1028 if (!GST_CLOCK_TIME_IS_VALID (ctx->in_running_time)) {
1029 ctx->in_running_time =
1030 gst_segment_to_running_time (&ctx->in_segment, GST_FORMAT_TIME,
1031 ctx->in_segment.start);
1034 buf_info->run_ts = ctx->in_running_time;
1035 buf_info->buf_size = gst_buffer_get_size (buf);
1037 /* Update total input byte counter for overflow detect */
1038 ctx->in_bytes += buf_info->buf_size;
1040 /* initialize mux_start_time */
1041 if (ctx->is_reference && splitmux->mux_start_time == 0)
1042 splitmux->mux_start_time = buf_info->run_ts;
1044 GST_DEBUG_OBJECT (pad, "Buf TS %" GST_TIME_FORMAT
1045 " total in_bytes %" G_GSIZE_FORMAT,
1046 GST_TIME_ARGS (buf_info->run_ts), ctx->in_bytes);
1053 switch (splitmux->state) {
1054 case SPLITMUX_STATE_COLLECTING_GOP_START:
1055 if (ctx->is_reference) {
1056 /* If a keyframe, we have a complete GOP */
1057 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT) ||
1058 !GST_CLOCK_TIME_IS_VALID (ctx->in_running_time) ||
1059 splitmux->max_in_running_time >= ctx->in_running_time) {
1060 /* Pass this buffer through */
1064 GST_INFO_OBJECT (pad,
1065 "Have keyframe with running time %" GST_TIME_FORMAT,
1066 GST_TIME_ARGS (ctx->in_running_time));
1068 splitmux->state = SPLITMUX_STATE_WAITING_GOP_COMPLETE;
1069 splitmux->max_in_running_time = ctx->in_running_time;
1070 /* Wake up other input pads to collect this GOP */
1071 GST_SPLITMUX_BROADCAST (splitmux);
1072 check_completed_gop (splitmux, ctx);
1074 /* We're still waiting for a keyframe on the reference pad, sleep */
1075 GST_LOG_OBJECT (pad, "Sleeping for GOP start");
1076 GST_SPLITMUX_WAIT (splitmux);
1077 GST_LOG_OBJECT (pad, "Done sleeping for GOP start state now %d",
1081 case SPLITMUX_STATE_WAITING_GOP_COMPLETE:
1082 /* After a GOP start is found, this buffer might complete the GOP */
1083 /* If we overran the target timestamp, it might be time to process
1084 * the GOP, otherwise bail out for more data
1086 GST_LOG_OBJECT (pad,
1087 "Checking TS %" GST_TIME_FORMAT " against max %" GST_TIME_FORMAT,
1088 GST_TIME_ARGS (ctx->in_running_time),
1089 GST_TIME_ARGS (splitmux->max_in_running_time));
1091 if (ctx->in_running_time < splitmux->max_in_running_time) {
1096 GST_LOG_OBJECT (pad,
1097 "Collected last packet of GOP. Checking other pads");
1098 check_completed_gop (splitmux, ctx);
1100 case SPLITMUX_STATE_ENDING_FILE:
1101 case SPLITMUX_STATE_START_NEXT_FRAGMENT:
1102 /* A fragment is ending, wait until that's done before continuing */
1103 GST_DEBUG_OBJECT (pad, "Sleeping for fragment restart");
1104 GST_SPLITMUX_WAIT (splitmux);
1105 GST_DEBUG_OBJECT (pad,
1106 "Done sleeping for fragment restart state now %d", splitmux->state);
1112 } while (loop_again);
1115 splitmux->queued_gops++;
1116 buf_info->keyframe = TRUE;
1119 /* Now add this buffer to the queue just before returning */
1120 g_queue_push_head (&ctx->queued_bufs, buf_info);
1122 /* Check the buffer will fit in the mq */
1123 check_queue_length (splitmux, ctx);
1125 GST_LOG_OBJECT (pad, "Returning to queue buffer %" GST_PTR_FORMAT
1126 " run ts %" GST_TIME_FORMAT, buf, GST_TIME_ARGS (ctx->in_running_time));
1128 GST_SPLITMUX_UNLOCK (splitmux);
1129 return GST_PAD_PROBE_PASS;
1132 GST_SPLITMUX_UNLOCK (splitmux);
1134 mq_stream_buf_free (buf_info);
1135 return GST_PAD_PROBE_PASS;
1139 gst_splitmux_sink_request_new_pad (GstElement * element,
1140 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1142 GstSplitMuxSink *splitmux = (GstSplitMuxSink *) element;
1143 GstPadTemplate *mux_template = NULL;
1145 GstPad *mq_sink, *mq_src;
1147 gboolean is_video = FALSE;
1150 GST_DEBUG_OBJECT (element, "templ:%s, name:%s", templ->name_template, name);
1152 GST_SPLITMUX_LOCK (splitmux);
1153 if (!create_elements (splitmux))
1156 if (templ->name_template) {
1157 if (g_str_equal (templ->name_template, "video")) {
1158 /* FIXME: Look for a pad template with matching caps, rather than by name */
1160 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS
1161 (splitmux->muxer), "video_%u");
1166 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS
1167 (splitmux->muxer), templ->name_template);
1169 if (mux_template == NULL) {
1170 /* Fallback to find sink pad templates named 'sink_%d' (mpegtsmux) */
1172 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS
1173 (splitmux->muxer), "sink_%d");
1177 res = gst_element_request_pad (splitmux->muxer, mux_template, name, caps);
1182 gname = g_strdup ("video");
1183 else if (name == NULL)
1184 gname = gst_pad_get_name (res);
1186 gname = g_strdup (name);
1188 if (!get_pads_from_mq (splitmux, &mq_sink, &mq_src)) {
1189 gst_element_release_request_pad (splitmux->muxer, res);
1190 gst_object_unref (GST_OBJECT (res));
1194 if (gst_pad_link (mq_src, res) != GST_PAD_LINK_OK) {
1195 gst_element_release_request_pad (splitmux->muxer, res);
1196 gst_object_unref (GST_OBJECT (res));
1197 gst_element_release_request_pad (splitmux->mq, mq_sink);
1198 gst_object_unref (GST_OBJECT (mq_sink));
1202 gst_object_unref (GST_OBJECT (res));
1204 ctx = mq_stream_ctx_new (splitmux);
1205 ctx->srcpad = mq_src;
1206 ctx->sinkpad = mq_sink;
1208 mq_stream_ctx_ref (ctx);
1209 ctx->src_pad_block_id =
1210 gst_pad_add_probe (mq_src, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
1211 (GstPadProbeCallback) handle_mq_output, ctx, (GDestroyNotify)
1212 _pad_block_destroy_src_notify);
1213 if (is_video && splitmux->reference_ctx != NULL) {
1214 splitmux->reference_ctx->is_reference = FALSE;
1215 splitmux->reference_ctx = NULL;
1217 if (splitmux->reference_ctx == NULL) {
1218 splitmux->reference_ctx = ctx;
1219 ctx->is_reference = TRUE;
1222 res = gst_ghost_pad_new (gname, mq_sink);
1223 g_object_set_qdata ((GObject *) (res), PAD_CONTEXT, ctx);
1225 mq_stream_ctx_ref (ctx);
1226 ctx->sink_pad_block_id =
1227 gst_pad_add_probe (mq_sink, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
1228 (GstPadProbeCallback) handle_mq_input, ctx, (GDestroyNotify)
1229 _pad_block_destroy_sink_notify);
1231 GST_DEBUG_OBJECT (splitmux, "Request pad %" GST_PTR_FORMAT
1232 " is mq pad %" GST_PTR_FORMAT, res, mq_sink);
1234 splitmux->contexts = g_list_prepend (splitmux->contexts, ctx);
1238 gst_object_unref (mq_sink);
1239 gst_object_unref (mq_src);
1241 gst_pad_set_active (res, TRUE);
1242 gst_element_add_pad (element, res);
1243 GST_SPLITMUX_UNLOCK (splitmux);
1247 GST_SPLITMUX_UNLOCK (splitmux);
1252 gst_splitmux_sink_release_pad (GstElement * element, GstPad * pad)
1254 GstSplitMuxSink *splitmux = (GstSplitMuxSink *) element;
1255 GstPad *mqsink, *mqsrc, *muxpad;
1257 (MqStreamCtx *) (g_object_get_qdata ((GObject *) (pad), PAD_CONTEXT));
1259 GST_SPLITMUX_LOCK (splitmux);
1261 if (splitmux->muxer == NULL || splitmux->mq == NULL)
1262 goto fail; /* Elements don't exist yet - nothing to release */
1264 GST_INFO_OBJECT (pad, "releasing request pad");
1266 mqsink = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1267 mqsrc = mq_sink_to_src (splitmux->mq, mqsink);
1268 muxpad = gst_pad_get_peer (mqsrc);
1270 /* Remove the context from our consideration */
1271 splitmux->contexts = g_list_remove (splitmux->contexts, ctx);
1273 if (ctx->sink_pad_block_id)
1274 gst_pad_remove_probe (ctx->sinkpad, ctx->sink_pad_block_id);
1276 if (ctx->src_pad_block_id)
1277 gst_pad_remove_probe (ctx->srcpad, ctx->src_pad_block_id);
1279 /* Can release the context now */
1280 mq_stream_ctx_unref (ctx);
1282 /* Release and free the mq input */
1283 gst_element_release_request_pad (splitmux->mq, mqsink);
1285 /* Release and free the muxer input */
1286 gst_element_release_request_pad (splitmux->muxer, muxpad);
1288 gst_object_unref (mqsink);
1289 gst_object_unref (mqsrc);
1290 gst_object_unref (muxpad);
1292 gst_element_remove_pad (element, pad);
1295 GST_SPLITMUX_UNLOCK (splitmux);
1299 create_element (GstSplitMuxSink * splitmux,
1300 const gchar * factory, const gchar * name)
1302 GstElement *ret = gst_element_factory_make (factory, name);
1304 g_warning ("Failed to create %s - splitmuxsink will not work", name);
1308 if (!gst_bin_add (GST_BIN (splitmux), ret)) {
1309 g_warning ("Could not add %s element - splitmuxsink will not work", name);
1310 gst_object_unref (ret);
1318 create_elements (GstSplitMuxSink * splitmux)
1320 /* Create internal elements */
1321 if (splitmux->mq == NULL) {
1323 create_element (splitmux, "multiqueue", "multiqueue")) == NULL)
1326 splitmux->mq_max_buffers = 5;
1327 /* No bytes or time limit, we limit buffers manually */
1328 g_object_set (splitmux->mq, "max-size-bytes", 0, "max-size-time",
1329 (guint64) 0, "max-size-buffers", splitmux->mq_max_buffers, NULL);
1332 if (splitmux->muxer == NULL) {
1333 GstElement *provided_muxer = NULL;
1335 GST_OBJECT_LOCK (splitmux);
1336 if (splitmux->provided_muxer != NULL)
1337 provided_muxer = gst_object_ref (splitmux->provided_muxer);
1338 GST_OBJECT_UNLOCK (splitmux);
1340 if (provided_muxer == NULL) {
1341 if ((splitmux->muxer =
1342 create_element (splitmux, "mp4mux", "muxer")) == NULL)
1345 if (!gst_bin_add (GST_BIN (splitmux), provided_muxer)) {
1346 g_warning ("Could not add muxer element - splitmuxsink will not work");
1347 gst_object_unref (provided_muxer);
1351 splitmux->muxer = provided_muxer;
1352 gst_object_unref (provided_muxer);
1362 find_sink (GstElement * e)
1364 GstElement *res = NULL;
1366 gboolean done = FALSE;
1367 GValue data = { 0, };
1369 if (!GST_IS_BIN (e))
1372 iter = gst_bin_iterate_sinks (GST_BIN (e));
1374 switch (gst_iterator_next (iter, &data)) {
1375 case GST_ITERATOR_OK:
1377 GstElement *child = g_value_get_object (&data);
1378 if (g_object_class_find_property (G_OBJECT_GET_CLASS (child),
1379 "location") != NULL) {
1383 g_value_reset (&data);
1386 case GST_ITERATOR_RESYNC:
1387 gst_iterator_resync (iter);
1389 case GST_ITERATOR_DONE:
1392 case GST_ITERATOR_ERROR:
1393 g_assert_not_reached ();
1397 g_value_unset (&data);
1398 gst_iterator_free (iter);
1404 create_sink (GstSplitMuxSink * splitmux)
1406 GstElement *provided_sink = NULL;
1408 g_return_val_if_fail (splitmux->active_sink == NULL, TRUE);
1410 GST_OBJECT_LOCK (splitmux);
1411 if (splitmux->provided_sink != NULL)
1412 provided_sink = gst_object_ref (splitmux->provided_sink);
1413 GST_OBJECT_UNLOCK (splitmux);
1415 if (provided_sink == NULL) {
1416 if ((splitmux->sink =
1417 create_element (splitmux, DEFAULT_SINK, "sink")) == NULL)
1419 splitmux->active_sink = splitmux->sink;
1421 if (!gst_bin_add (GST_BIN (splitmux), provided_sink)) {
1422 g_warning ("Could not add sink elements - splitmuxsink will not work");
1423 gst_object_unref (provided_sink);
1427 splitmux->active_sink = provided_sink;
1429 /* The bin holds a ref now, we can drop our tmp ref */
1430 gst_object_unref (provided_sink);
1432 /* Find the sink element */
1433 splitmux->sink = find_sink (splitmux->active_sink);
1434 if (splitmux->sink == NULL) {
1436 ("Could not locate sink element in provided sink - splitmuxsink will not work");
1441 if (!gst_element_link (splitmux->muxer, splitmux->active_sink)) {
1442 g_warning ("Failed to link muxer and sink- splitmuxsink will not work");
1452 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
1455 set_next_filename (GstSplitMuxSink * splitmux)
1457 gchar *fname = NULL;
1459 g_signal_emit (splitmux, signals[SIGNAL_FORMAT_LOCATION], 0,
1460 splitmux->fragment_id, &fname);
1463 fname = splitmux->location ?
1464 g_strdup_printf (splitmux->location, splitmux->fragment_id) : NULL;
1467 GST_INFO_OBJECT (splitmux, "Setting file to %s", fname);
1468 g_object_set (splitmux->sink, "location", fname, NULL);
1471 splitmux->fragment_id++;
1475 static GstStateChangeReturn
1476 gst_splitmux_sink_change_state (GstElement * element, GstStateChange transition)
1478 GstStateChangeReturn ret;
1479 GstSplitMuxSink *splitmux = (GstSplitMuxSink *) element;
1481 switch (transition) {
1482 case GST_STATE_CHANGE_NULL_TO_READY:{
1483 GST_SPLITMUX_LOCK (splitmux);
1484 if (!create_elements (splitmux) || !create_sink (splitmux)) {
1485 ret = GST_STATE_CHANGE_FAILURE;
1486 GST_SPLITMUX_UNLOCK (splitmux);
1489 GST_SPLITMUX_UNLOCK (splitmux);
1490 splitmux->fragment_id = 0;
1491 set_next_filename (splitmux);
1494 case GST_STATE_CHANGE_READY_TO_PAUSED:{
1495 GST_SPLITMUX_LOCK (splitmux);
1496 /* Start by collecting one input on each pad */
1497 splitmux->state = SPLITMUX_STATE_COLLECTING_GOP_START;
1498 splitmux->max_in_running_time = 0;
1499 splitmux->muxed_out_time = splitmux->mux_start_time = 0;
1500 splitmux->muxed_out_bytes = splitmux->mux_start_bytes = 0;
1501 GST_SPLITMUX_UNLOCK (splitmux);
1504 case GST_STATE_CHANGE_PAUSED_TO_READY:
1505 case GST_STATE_CHANGE_READY_TO_NULL:
1506 GST_SPLITMUX_LOCK (splitmux);
1507 splitmux->state = SPLITMUX_STATE_STOPPED;
1508 /* Wake up any blocked threads */
1509 GST_LOG_OBJECT (splitmux,
1510 "State change -> NULL or READY. Waking threads");
1511 GST_SPLITMUX_BROADCAST (splitmux);
1512 GST_SPLITMUX_UNLOCK (splitmux);
1518 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1519 if (ret == GST_STATE_CHANGE_FAILURE)
1522 switch (transition) {
1523 case GST_STATE_CHANGE_READY_TO_NULL:
1524 GST_SPLITMUX_LOCK (splitmux);
1525 splitmux->fragment_id = 0;
1526 gst_splitmux_reset (splitmux);
1527 GST_SPLITMUX_UNLOCK (splitmux);
1535 if (transition == GST_STATE_CHANGE_NULL_TO_READY &&
1536 ret == GST_STATE_CHANGE_FAILURE) {
1537 /* Cleanup elements on failed transition out of NULL */
1538 gst_splitmux_reset (splitmux);
1544 register_splitmuxsink (GstPlugin * plugin)
1546 GST_DEBUG_CATEGORY_INIT (splitmux_debug, "splitmuxsink", 0,
1547 "Split File Muxing Sink");
1549 return gst_element_register (plugin, "splitmuxsink", GST_RANK_NONE,
1550 GST_TYPE_SPLITMUX_SINK);