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 * is required, 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 * The splitting process is driven by the video stream contents, and
37 * the video stream must contain closed GOPs for the output file parts
38 * to be played individually correctly.
41 * <title>Example pipelines</title>
43 * 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
45 * Records a video stream captured from a v4l2 device and muxes it into
46 * ISO mp4 files, splitting as needed to limit size/duration to 10 seconds
47 * and 1MB maximum size.
56 #include "gstsplitmuxsink.h"
58 GST_DEBUG_CATEGORY_STATIC (splitmux_debug);
59 #define GST_CAT_DEFAULT splitmux_debug
61 #define GST_SPLITMUX_LOCK(s) g_mutex_lock(&(s)->lock)
62 #define GST_SPLITMUX_UNLOCK(s) g_mutex_unlock(&(s)->lock)
63 #define GST_SPLITMUX_WAIT(s) g_cond_wait (&(s)->data_cond, &(s)->lock)
64 #define GST_SPLITMUX_BROADCAST(s) g_cond_broadcast (&(s)->data_cond)
77 #define DEFAULT_MAX_SIZE_TIME 0
78 #define DEFAULT_MAX_SIZE_BYTES 0
79 #define DEFAULT_MUXER_OVERHEAD 0.02
80 #define DEFAULT_MUXER "mp4mux"
81 #define DEFAULT_SINK "filesink"
83 static GstStaticPadTemplate video_sink_template =
84 GST_STATIC_PAD_TEMPLATE ("video",
88 static GstStaticPadTemplate audio_sink_template =
89 GST_STATIC_PAD_TEMPLATE ("audio_%u",
93 static GstStaticPadTemplate subtitle_sink_template =
94 GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
99 static GQuark PAD_CONTEXT;
104 PAD_CONTEXT = g_quark_from_static_string ("pad-context");
107 #define gst_splitmux_sink_parent_class parent_class
108 G_DEFINE_TYPE_EXTENDED (GstSplitMuxSink, gst_splitmux_sink, GST_TYPE_BIN, 0,
111 static gboolean create_elements (GstSplitMuxSink * splitmux);
112 static gboolean create_sink (GstSplitMuxSink * splitmux);
113 static void gst_splitmux_sink_set_property (GObject * object, guint prop_id,
114 const GValue * value, GParamSpec * pspec);
115 static void gst_splitmux_sink_get_property (GObject * object, guint prop_id,
116 GValue * value, GParamSpec * pspec);
117 static void gst_splitmux_sink_dispose (GObject * object);
118 static void gst_splitmux_sink_finalize (GObject * object);
120 static GstPad *gst_splitmux_sink_request_new_pad (GstElement * element,
121 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
122 static void gst_splitmux_sink_release_pad (GstElement * element, GstPad * pad);
124 static GstStateChangeReturn gst_splitmux_sink_change_state (GstElement *
125 element, GstStateChange transition);
127 static void bus_handler (GstBin * bin, GstMessage * msg);
128 static void set_next_filename (GstSplitMuxSink * splitmux);
129 static void start_next_fragment (GstSplitMuxSink * splitmux);
130 static void check_queue_length (GstSplitMuxSink * splitmux, MqStreamCtx * ctx);
131 static void mq_stream_ctx_unref (MqStreamCtx * ctx);
134 mq_stream_buf_new (void)
136 return g_slice_new0 (MqStreamBuf);
140 mq_stream_buf_free (MqStreamBuf * data)
142 g_slice_free (MqStreamBuf, data);
146 gst_splitmux_sink_class_init (GstSplitMuxSinkClass * klass)
148 GObjectClass *gobject_class = (GObjectClass *) klass;
149 GstElementClass *gstelement_class = (GstElementClass *) klass;
150 GstBinClass *gstbin_class = (GstBinClass *) klass;
152 gobject_class->set_property = gst_splitmux_sink_set_property;
153 gobject_class->get_property = gst_splitmux_sink_get_property;
154 gobject_class->dispose = gst_splitmux_sink_dispose;
155 gobject_class->finalize = gst_splitmux_sink_finalize;
157 gst_element_class_set_static_metadata (gstelement_class,
158 "Split Muxing Bin", "Generic/Bin/Muxer",
159 "Convenience bin that muxes incoming streams into multiple time/size limited files",
160 "Jan Schmidt <jan@centricular.com>");
162 gst_element_class_add_pad_template (gstelement_class,
163 gst_static_pad_template_get (&video_sink_template));
164 gst_element_class_add_pad_template (gstelement_class,
165 gst_static_pad_template_get (&audio_sink_template));
166 gst_element_class_add_pad_template (gstelement_class,
167 gst_static_pad_template_get (&subtitle_sink_template));
169 gstelement_class->change_state =
170 GST_DEBUG_FUNCPTR (gst_splitmux_sink_change_state);
171 gstelement_class->request_new_pad =
172 GST_DEBUG_FUNCPTR (gst_splitmux_sink_request_new_pad);
173 gstelement_class->release_pad =
174 GST_DEBUG_FUNCPTR (gst_splitmux_sink_release_pad);
176 gstbin_class->handle_message = bus_handler;
178 g_object_class_install_property (gobject_class, PROP_LOCATION,
179 g_param_spec_string ("location", "File Output Pattern",
180 "Format string pattern for the location of the files to write (e.g. video%05d.mp4)",
181 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182 g_object_class_install_property (gobject_class, PROP_MUXER_OVERHEAD,
183 g_param_spec_double ("mux-overhead", "Muxing Overhead",
184 "Extra size overhead of muxing (0.02 = 2%)", 0.0, 1.0,
185 DEFAULT_MUXER_OVERHEAD,
186 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
188 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
189 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
190 "Max. amount of time per file (in ns, 0=disable)", 0, G_MAXUINT64,
191 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
193 g_param_spec_uint64 ("max-size-bytes", "Max. size bytes",
194 "Max. amount of data per file (in bytes, 0=disable)", 0, G_MAXUINT64,
195 DEFAULT_MAX_SIZE_BYTES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (gobject_class, PROP_MUXER,
198 g_param_spec_object ("muxer", "Muxer",
199 "The muxer element to use (NULL = default mp4mux)",
200 GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201 g_object_class_install_property (gobject_class, PROP_SINK,
202 g_param_spec_object ("sink", "Sink",
203 "The sink element (or element chain) to use (NULL = default filesink)",
204 GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208 gst_splitmux_sink_init (GstSplitMuxSink * splitmux)
210 g_mutex_init (&splitmux->lock);
211 g_cond_init (&splitmux->data_cond);
213 splitmux->mux_overhead = DEFAULT_MUXER_OVERHEAD;
214 splitmux->threshold_time = DEFAULT_MAX_SIZE_TIME;
215 splitmux->threshold_bytes = DEFAULT_MAX_SIZE_BYTES;
217 GST_OBJECT_FLAG_SET (splitmux, GST_ELEMENT_FLAG_SINK);
221 gst_splitmux_reset (GstSplitMuxSink * splitmux)
224 gst_bin_remove (GST_BIN (splitmux), splitmux->mq);
226 gst_bin_remove (GST_BIN (splitmux), splitmux->muxer);
227 if (splitmux->active_sink)
228 gst_bin_remove (GST_BIN (splitmux), splitmux->active_sink);
230 splitmux->sink = splitmux->active_sink = splitmux->muxer = splitmux->mq =
235 gst_splitmux_sink_dispose (GObject * object)
237 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (object);
239 G_OBJECT_CLASS (parent_class)->dispose (object);
241 /* Calling parent dispose invalidates all child pointers */
242 splitmux->sink = splitmux->active_sink = splitmux->muxer = splitmux->mq =
247 gst_splitmux_sink_finalize (GObject * object)
249 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (object);
250 g_cond_clear (&splitmux->data_cond);
251 g_mutex_clear (&splitmux->lock);
252 if (splitmux->provided_sink)
253 gst_object_unref (splitmux->provided_sink);
254 if (splitmux->provided_muxer)
255 gst_object_unref (splitmux->provided_muxer);
257 g_free (splitmux->location);
259 /* Make sure to free any un-released contexts */
260 g_list_foreach (splitmux->contexts, (GFunc) mq_stream_ctx_unref, NULL);
261 g_list_free (splitmux->contexts);
263 G_OBJECT_CLASS (parent_class)->finalize (object);
267 gst_splitmux_sink_set_property (GObject * object, guint prop_id,
268 const GValue * value, GParamSpec * pspec)
270 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (object);
274 GST_OBJECT_LOCK (splitmux);
275 g_free (splitmux->location);
276 splitmux->location = g_value_dup_string (value);
277 GST_OBJECT_UNLOCK (splitmux);
280 case PROP_MAX_SIZE_BYTES:
281 GST_OBJECT_LOCK (splitmux);
282 splitmux->threshold_bytes = g_value_get_uint64 (value);
283 GST_OBJECT_UNLOCK (splitmux);
285 case PROP_MAX_SIZE_TIME:
286 GST_OBJECT_LOCK (splitmux);
287 splitmux->threshold_time = g_value_get_uint64 (value);
288 GST_OBJECT_UNLOCK (splitmux);
290 case PROP_MUXER_OVERHEAD:
291 GST_OBJECT_LOCK (splitmux);
292 splitmux->mux_overhead = g_value_get_double (value);
293 GST_OBJECT_UNLOCK (splitmux);
296 GST_OBJECT_LOCK (splitmux);
297 if (splitmux->provided_sink)
298 gst_object_unref (splitmux->provided_sink);
299 splitmux->provided_sink = g_value_dup_object (value);
300 GST_OBJECT_UNLOCK (splitmux);
303 GST_OBJECT_LOCK (splitmux);
304 if (splitmux->provided_muxer)
305 gst_object_unref (splitmux->provided_muxer);
306 splitmux->provided_muxer = g_value_dup_object (value);
307 GST_OBJECT_UNLOCK (splitmux);
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 gst_splitmux_sink_get_property (GObject * object, guint prop_id,
317 GValue * value, GParamSpec * pspec)
319 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (object);
323 GST_OBJECT_LOCK (splitmux);
324 g_value_set_string (value, splitmux->location);
325 GST_OBJECT_UNLOCK (splitmux);
327 case PROP_MAX_SIZE_BYTES:
328 GST_OBJECT_LOCK (splitmux);
329 g_value_set_uint64 (value, splitmux->threshold_bytes);
330 GST_OBJECT_UNLOCK (splitmux);
332 case PROP_MAX_SIZE_TIME:
333 GST_OBJECT_LOCK (splitmux);
334 g_value_set_uint64 (value, splitmux->threshold_time);
335 GST_OBJECT_UNLOCK (splitmux);
337 case PROP_MUXER_OVERHEAD:
338 GST_OBJECT_LOCK (splitmux);
339 g_value_set_double (value, splitmux->mux_overhead);
340 GST_OBJECT_UNLOCK (splitmux);
343 GST_OBJECT_LOCK (splitmux);
344 g_value_set_object (value, splitmux->provided_sink);
345 GST_OBJECT_UNLOCK (splitmux);
348 GST_OBJECT_LOCK (splitmux);
349 g_value_set_object (value, splitmux->provided_muxer);
350 GST_OBJECT_UNLOCK (splitmux);
353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359 mq_sink_to_src (GstElement * mq, GstPad * sink_pad)
361 gchar *tmp, *sinkname, *srcname;
364 sinkname = gst_pad_get_name (sink_pad);
366 srcname = g_strdup_printf ("src_%s", tmp);
368 mq_src = gst_element_get_static_pad (mq, srcname);
377 get_pads_from_mq (GstSplitMuxSink * splitmux, GstPad ** sink_pad,
383 /* Request a pad from multiqueue, then connect this one, then
384 * discover the corresponding output pad and return both */
385 mq_sink = gst_element_get_request_pad (splitmux->mq, "sink_%u");
389 mq_src = mq_sink_to_src (splitmux->mq, mq_sink);
399 gst_element_release_request_pad (splitmux->mq, mq_sink);
404 mq_stream_ctx_new (GstSplitMuxSink * splitmux)
408 ctx = g_new0 (MqStreamCtx, 1);
409 g_atomic_int_set (&ctx->refcount, 1);
410 ctx->splitmux = splitmux;
411 gst_segment_init (&ctx->in_segment, GST_FORMAT_UNDEFINED);
412 gst_segment_init (&ctx->out_segment, GST_FORMAT_UNDEFINED);
413 ctx->in_running_time = ctx->out_running_time = 0;
414 g_queue_init (&ctx->queued_bufs);
419 mq_stream_ctx_free (MqStreamCtx * ctx)
421 g_queue_foreach (&ctx->queued_bufs, (GFunc) mq_stream_buf_free, NULL);
422 g_queue_clear (&ctx->queued_bufs);
427 mq_stream_ctx_unref (MqStreamCtx * ctx)
429 if (g_atomic_int_dec_and_test (&ctx->refcount))
430 mq_stream_ctx_free (ctx);
434 mq_stream_ctx_ref (MqStreamCtx * ctx)
436 g_atomic_int_inc (&ctx->refcount);
440 _pad_block_destroy_sink_notify (MqStreamCtx * ctx)
442 ctx->sink_pad_block_id = 0;
443 mq_stream_ctx_unref (ctx);
447 _pad_block_destroy_src_notify (MqStreamCtx * ctx)
449 ctx->src_pad_block_id = 0;
450 mq_stream_ctx_unref (ctx);
453 /* Called with lock held, drops the lock to send EOS to the
457 send_eos (GstSplitMuxSink * splitmux, MqStreamCtx * ctx)
462 eos = gst_event_new_eos ();
463 pad = gst_pad_get_peer (ctx->srcpad);
467 GST_INFO_OBJECT (splitmux, "Sending EOS on %" GST_PTR_FORMAT, pad);
468 GST_SPLITMUX_UNLOCK (splitmux);
469 gst_pad_send_event (pad, eos);
470 GST_SPLITMUX_LOCK (splitmux);
472 gst_object_unref (pad);
475 /* Called with splitmux lock held to check if this output
476 * context needs to sleep to wait for the release of the
477 * next GOP, or to send EOS to close out the current file
480 complete_or_wait_on_out (GstSplitMuxSink * splitmux, MqStreamCtx * ctx)
484 GST_LOG_OBJECT (ctx->srcpad,
485 "Checking running time %" GST_TIME_FORMAT " against max %"
486 GST_TIME_FORMAT, GST_TIME_ARGS (ctx->out_running_time),
487 GST_TIME_ARGS (splitmux->max_out_running_time));
489 if (splitmux->max_out_running_time == GST_CLOCK_TIME_NONE ||
490 ctx->out_running_time < splitmux->max_out_running_time)
493 if (ctx->flushing || splitmux->state == SPLITMUX_STATE_STOPPED)
496 if (splitmux->state == SPLITMUX_STATE_ENDING_FILE) {
497 if (ctx->out_eos == FALSE) {
498 send_eos (splitmux, ctx);
501 } else if (splitmux->state == SPLITMUX_STATE_START_NEXT_FRAGMENT) {
502 start_next_fragment (splitmux);
506 GST_INFO_OBJECT (ctx->srcpad,
507 "Sleeping for running time %"
508 GST_TIME_FORMAT " (max %" GST_TIME_FORMAT ")",
509 GST_TIME_ARGS (ctx->out_running_time),
510 GST_TIME_ARGS (splitmux->max_out_running_time));
511 ctx->out_blocked = TRUE;
512 /* Expand the mq if needed before sleeping */
513 check_queue_length (splitmux, ctx);
514 GST_SPLITMUX_WAIT (splitmux);
515 ctx->out_blocked = FALSE;
516 GST_INFO_OBJECT (ctx->srcpad,
517 "Woken for new max running time %" GST_TIME_FORMAT,
518 GST_TIME_ARGS (splitmux->max_out_running_time));
522 static GstPadProbeReturn
523 handle_mq_output (GstPad * pad, GstPadProbeInfo * info, MqStreamCtx * ctx)
525 GstSplitMuxSink *splitmux = ctx->splitmux;
526 MqStreamBuf *buf_info = NULL;
528 GST_LOG_OBJECT (pad, "Fired probe type 0x%x\n", info->type);
530 /* FIXME: Handle buffer lists, until then make it clear they won't work */
531 if (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST) {
532 g_warning ("Buffer list handling not implemented");
533 return GST_PAD_PROBE_DROP;
535 if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
536 GstEvent *event = gst_pad_probe_info_get_event (info);
538 GST_LOG_OBJECT (pad, "Event %" GST_PTR_FORMAT, event);
540 switch (GST_EVENT_TYPE (event)) {
541 case GST_EVENT_SEGMENT:
542 gst_event_copy_segment (event, &ctx->out_segment);
544 case GST_EVENT_FLUSH_STOP:
545 GST_SPLITMUX_LOCK (splitmux);
546 gst_segment_init (&ctx->out_segment, GST_FORMAT_UNDEFINED);
547 g_queue_foreach (&ctx->queued_bufs, (GFunc) mq_stream_buf_free, NULL);
548 g_queue_clear (&ctx->queued_bufs);
549 ctx->flushing = FALSE;
550 GST_SPLITMUX_UNLOCK (splitmux);
552 case GST_EVENT_FLUSH_START:
553 GST_SPLITMUX_LOCK (splitmux);
554 GST_LOG_OBJECT (pad, "Flush start");
555 ctx->flushing = TRUE;
556 GST_SPLITMUX_BROADCAST (splitmux);
557 GST_SPLITMUX_UNLOCK (splitmux);
560 GST_SPLITMUX_LOCK (splitmux);
561 if (splitmux->state == SPLITMUX_STATE_STOPPED)
564 GST_SPLITMUX_UNLOCK (splitmux);
569 gst_event_parse_gap (event, &gap_ts, NULL);
570 if (gap_ts == GST_CLOCK_TIME_NONE)
573 GST_SPLITMUX_LOCK (splitmux);
575 gap_ts = gst_segment_to_running_time (&ctx->out_segment,
576 GST_FORMAT_TIME, gap_ts);
578 GST_LOG_OBJECT (pad, "Have GAP w/ ts %" GST_TIME_FORMAT,
579 GST_TIME_ARGS (gap_ts));
581 if (splitmux->state == SPLITMUX_STATE_STOPPED)
583 ctx->out_running_time = gap_ts;
584 complete_or_wait_on_out (splitmux, ctx);
585 GST_SPLITMUX_UNLOCK (splitmux);
591 return GST_PAD_PROBE_PASS;
594 /* Allow everything through until the configured next stopping point */
595 GST_SPLITMUX_LOCK (splitmux);
597 buf_info = g_queue_pop_tail (&ctx->queued_bufs);
598 if (buf_info == NULL)
599 /* Can only happen due to a poorly timed flush */
602 /* If we have popped a keyframe, decrement the queued_gop count */
603 if (buf_info->keyframe && splitmux->queued_gops > 0)
604 splitmux->queued_gops--;
606 ctx->out_running_time = buf_info->run_ts;
608 GST_LOG_OBJECT (splitmux,
609 "Pad %" GST_PTR_FORMAT " buffer with TS %" GST_TIME_FORMAT
610 " size %" G_GSIZE_FORMAT,
611 pad, GST_TIME_ARGS (ctx->out_running_time), buf_info->buf_size);
613 complete_or_wait_on_out (splitmux, ctx);
615 if (splitmux->muxed_out_time == GST_CLOCK_TIME_NONE ||
616 splitmux->muxed_out_time < buf_info->run_ts)
617 splitmux->muxed_out_time = buf_info->run_ts;
619 splitmux->muxed_out_bytes += buf_info->buf_size;
621 #ifndef GST_DISABLE_GST_DEBUG
623 GstBuffer *buf = gst_pad_probe_info_get_buffer (info);
624 GST_LOG_OBJECT (pad, "Returning to pass buffer %" GST_PTR_FORMAT
625 " run ts %" GST_TIME_FORMAT, buf,
626 GST_TIME_ARGS (ctx->out_running_time));
630 GST_SPLITMUX_UNLOCK (splitmux);
632 mq_stream_buf_free (buf_info);
634 return GST_PAD_PROBE_PASS;
637 GST_SPLITMUX_UNLOCK (splitmux);
638 return GST_PAD_PROBE_DROP;
642 resend_sticky (GstPad * pad, GstEvent ** event, GstPad * peer)
644 return gst_pad_send_event (peer, gst_event_ref (*event));
648 restart_context (MqStreamCtx * ctx, GstSplitMuxSink * splitmux)
650 GstPad *peer = gst_pad_get_peer (ctx->srcpad);
652 gst_pad_sticky_events_foreach (ctx->srcpad,
653 (GstPadStickyEventsForeachFunction) (resend_sticky), peer);
656 ctx->out_eos = FALSE;
658 gst_object_unref (peer);
661 /* Called with lock held when a fragment
662 * reaches EOS and it is time to restart
666 start_next_fragment (GstSplitMuxSink * splitmux)
668 /* 1 change to new file */
669 gst_element_set_state (splitmux->muxer, GST_STATE_NULL);
670 gst_element_set_state (splitmux->active_sink, GST_STATE_NULL);
672 set_next_filename (splitmux);
674 gst_element_sync_state_with_parent (splitmux->active_sink);
675 gst_element_sync_state_with_parent (splitmux->muxer);
677 g_list_foreach (splitmux->contexts, (GFunc) restart_context, splitmux);
679 /* Switch state and go back to processing */
680 splitmux->state = SPLITMUX_STATE_COLLECTING_GOP_START;
681 if (!splitmux->video_ctx->in_eos)
682 splitmux->max_out_running_time = splitmux->video_ctx->in_running_time;
684 splitmux->max_out_running_time = GST_CLOCK_TIME_NONE;
686 /* Store the overflow parameters as the basis for the next fragment */
687 splitmux->mux_start_time = splitmux->muxed_out_time;
688 splitmux->mux_start_bytes = splitmux->muxed_out_bytes;
690 GST_DEBUG_OBJECT (splitmux,
691 "Restarting flow for new fragment. New running time %" GST_TIME_FORMAT,
692 GST_TIME_ARGS (splitmux->max_out_running_time));
694 GST_SPLITMUX_BROADCAST (splitmux);
698 bus_handler (GstBin * bin, GstMessage * message)
700 GstSplitMuxSink *splitmux = GST_SPLITMUX_SINK (bin);
702 switch (GST_MESSAGE_TYPE (message)) {
703 case GST_MESSAGE_EOS:
704 /* If the state is draining out the current file, drop this EOS */
705 GST_SPLITMUX_LOCK (splitmux);
706 if (splitmux->state == SPLITMUX_STATE_ENDING_FILE &&
707 splitmux->max_out_running_time != GST_CLOCK_TIME_NONE) {
708 GST_DEBUG_OBJECT (splitmux, "Caught EOS at end of fragment, dropping");
709 splitmux->state = SPLITMUX_STATE_START_NEXT_FRAGMENT;
710 GST_SPLITMUX_BROADCAST (splitmux);
712 gst_message_unref (message);
713 GST_SPLITMUX_UNLOCK (splitmux);
716 GST_SPLITMUX_UNLOCK (splitmux);
722 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
725 /* Called with splitmux lock held */
726 /* Called when entering ProcessingCompleteGop state
727 * Assess if mq contents overflowed the current file
728 * -> If yes, need to switch to new file
729 * -> if no, set max_out_running_time to let this GOP in and
730 * go to COLLECTING_GOP_START state
733 handle_gathered_gop (GstSplitMuxSink * splitmux)
736 gsize queued_bytes = 0;
737 GstClockTime queued_time = 0;
739 /* Assess if the multiqueue contents overflowed the current file */
740 for (cur = g_list_first (splitmux->contexts);
741 cur != NULL; cur = g_list_next (cur)) {
742 MqStreamCtx *tmpctx = (MqStreamCtx *) (cur->data);
743 if (tmpctx->in_running_time > queued_time)
744 queued_time = tmpctx->in_running_time;
745 queued_bytes += tmpctx->in_bytes;
748 g_assert (queued_bytes >= splitmux->mux_start_bytes);
749 g_assert (queued_time >= splitmux->mux_start_time);
751 queued_bytes -= splitmux->mux_start_bytes;
752 queued_time -= splitmux->mux_start_time;
754 /* Expand queued bytes estimate by muxer overhead */
755 queued_bytes += (queued_bytes * splitmux->mux_overhead);
757 GST_LOG_OBJECT (splitmux, "mq at TS %" GST_TIME_FORMAT
758 " bytes %" G_GSIZE_FORMAT, GST_TIME_ARGS (queued_time), queued_bytes);
760 /* Check for overrun - have we output at least one byte and overrun
761 * either threshold? */
762 if ((splitmux->mux_start_bytes < splitmux->muxed_out_bytes) &&
763 ((splitmux->threshold_bytes > 0 &&
764 queued_bytes >= splitmux->threshold_bytes) ||
765 (splitmux->threshold_time > 0 &&
766 queued_time >= splitmux->threshold_time))) {
768 splitmux->state = SPLITMUX_STATE_ENDING_FILE;
770 GST_INFO_OBJECT (splitmux,
771 "mq overflowed since last, draining out. max out TS is %"
772 GST_TIME_FORMAT, GST_TIME_ARGS (splitmux->max_out_running_time));
773 GST_SPLITMUX_BROADCAST (splitmux);
777 GST_LOG_OBJECT (splitmux,
778 "This GOP didn't overflow the fragment. Bytes sent %" G_GSIZE_FORMAT
779 " queued %" G_GSIZE_FORMAT " time %" GST_TIME_FORMAT " Continuing.",
780 splitmux->muxed_out_bytes - splitmux->mux_start_bytes,
781 queued_bytes, GST_TIME_ARGS (queued_time));
783 /* Wake everyone up to push this one GOP, then sleep */
784 splitmux->state = SPLITMUX_STATE_COLLECTING_GOP_START;
785 if (!splitmux->video_ctx->in_eos)
786 splitmux->max_out_running_time = splitmux->video_ctx->in_running_time;
788 splitmux->max_out_running_time = GST_CLOCK_TIME_NONE;
790 GST_LOG_OBJECT (splitmux, "Waking output for complete GOP, TS %"
791 GST_TIME_FORMAT, GST_TIME_ARGS (splitmux->max_out_running_time));
792 GST_SPLITMUX_BROADCAST (splitmux);
797 /* Called with splitmux lock held */
798 /* Called from each input pad when it is has all the pieces
799 * for a GOP or EOS, starting with the video pad which has set the
800 * splitmux->max_in_running_time
803 check_completed_gop (GstSplitMuxSink * splitmux, MqStreamCtx * ctx)
806 gboolean ready = TRUE;
808 if (splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE) {
809 /* Iterate each pad, and check that the input running time is at least
810 * up to the video runnning time, and if so handle the collected GOP */
811 GST_LOG_OBJECT (splitmux, "Checking GOP collected, ctx %p", ctx);
812 for (cur = g_list_first (splitmux->contexts);
813 cur != NULL; cur = g_list_next (cur)) {
814 MqStreamCtx *tmpctx = (MqStreamCtx *) (cur->data);
816 GST_LOG_OBJECT (splitmux,
817 "Context %p (src pad %" GST_PTR_FORMAT ") TS %" GST_TIME_FORMAT
818 " EOS %d", tmpctx, tmpctx->srcpad,
819 GST_TIME_ARGS (tmpctx->in_running_time), tmpctx->in_eos);
821 if (tmpctx->in_running_time < splitmux->max_in_running_time &&
823 GST_LOG_OBJECT (splitmux,
824 "Context %p (src pad %" GST_PTR_FORMAT ") not ready. We'll sleep",
825 tmpctx, tmpctx->srcpad);
831 GST_DEBUG_OBJECT (splitmux,
832 "Collected GOP is complete. Processing (ctx %p)", ctx);
833 /* All pads have a complete GOP, release it into the multiqueue */
834 handle_gathered_gop (splitmux);
838 /* Some pad is not yet ready, or GOP is being pushed
839 * either way, sleep and wait to get woken */
840 while ((splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE ||
841 splitmux->state == SPLITMUX_STATE_START_NEXT_FRAGMENT) &&
844 GST_LOG_OBJECT (splitmux, "Sleeping for %s (ctx %p)",
845 splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE ?
846 "GOP complete" : "EOF draining", ctx);
847 GST_SPLITMUX_WAIT (splitmux);
849 GST_LOG_OBJECT (splitmux, "Done waiting for complete GOP (ctx %p)", ctx);
854 check_queue_length (GstSplitMuxSink * splitmux, MqStreamCtx * ctx)
857 guint cur_len = g_queue_get_length (&ctx->queued_bufs);
859 GST_DEBUG_OBJECT (ctx->sinkpad,
860 "Checking queue length len %u cur_max %u queued gops %u",
861 cur_len, splitmux->mq_max_buffers, splitmux->queued_gops);
863 if (cur_len >= splitmux->mq_max_buffers) {
864 gboolean allow_grow = FALSE;
866 /* If collecting a GOP and this pad might block,
867 * and there isn't already a pending GOP in the queue
870 if (splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE &&
871 ctx->in_running_time < splitmux->max_in_running_time &&
872 splitmux->queued_gops <= 1) {
874 } else if (splitmux->state == SPLITMUX_STATE_COLLECTING_GOP_START &&
880 for (cur = g_list_first (splitmux->contexts);
881 cur != NULL; cur = g_list_next (cur)) {
882 MqStreamCtx *tmpctx = (MqStreamCtx *) (cur->data);
883 GST_DEBUG_OBJECT (tmpctx->sinkpad,
884 " len %u out_blocked %d",
885 g_queue_get_length (&tmpctx->queued_bufs), tmpctx->out_blocked);
886 /* If another stream is starving, grow */
887 if (tmpctx != ctx && g_queue_get_length (&tmpctx->queued_bufs) < 1) {
894 splitmux->mq_max_buffers = cur_len + 1;
896 GST_INFO_OBJECT (splitmux,
897 "Multiqueue overrun - enlarging to %u buffers ctx %p",
898 splitmux->mq_max_buffers, ctx);
900 g_object_set (splitmux->mq, "max-size-buffers",
901 splitmux->mq_max_buffers, NULL);
906 static GstPadProbeReturn
907 handle_mq_input (GstPad * pad, GstPadProbeInfo * info, MqStreamCtx * ctx)
909 GstSplitMuxSink *splitmux = ctx->splitmux;
911 MqStreamBuf *buf_info = NULL;
914 gboolean keyframe = FALSE;
916 GST_LOG_OBJECT (pad, "Fired probe type 0x%x\n", info->type);
918 /* FIXME: Handle buffer lists, until then make it clear they won't work */
919 if (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST) {
920 g_warning ("Buffer list handling not implemented");
921 return GST_PAD_PROBE_DROP;
923 if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
924 GstEvent *event = gst_pad_probe_info_get_event (info);
925 switch (GST_EVENT_TYPE (event)) {
926 case GST_EVENT_SEGMENT:
927 gst_event_copy_segment (event, &ctx->in_segment);
929 case GST_EVENT_FLUSH_STOP:
930 GST_SPLITMUX_LOCK (splitmux);
931 gst_segment_init (&ctx->in_segment, GST_FORMAT_UNDEFINED);
934 ctx->in_running_time = 0;
935 GST_SPLITMUX_UNLOCK (splitmux);
938 GST_SPLITMUX_LOCK (splitmux);
941 if (splitmux->state == SPLITMUX_STATE_STOPPED)
945 GST_INFO_OBJECT (splitmux, "Got Video EOS. Finishing up");
946 /* Act as if this is a new keyframe with infinite timestamp */
947 splitmux->max_in_running_time = GST_CLOCK_TIME_NONE;
948 splitmux->state = SPLITMUX_STATE_WAITING_GOP_COMPLETE;
949 /* Wake up other input pads to collect this GOP */
950 GST_SPLITMUX_BROADCAST (splitmux);
951 check_completed_gop (splitmux, ctx);
952 } else if (splitmux->state == SPLITMUX_STATE_WAITING_GOP_COMPLETE) {
953 /* If we are waiting for a GOP to be completed (ie, for aux
954 * pads to catch up), then this pad is complete, so check
955 * if the whole GOP is.
957 check_completed_gop (splitmux, ctx);
959 GST_SPLITMUX_UNLOCK (splitmux);
964 return GST_PAD_PROBE_PASS;
967 buf = gst_pad_probe_info_get_buffer (info);
968 ctx->in_running_time = gst_segment_to_running_time (&ctx->in_segment,
969 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
970 buf_info = mq_stream_buf_new ();
972 if (GST_BUFFER_PTS_IS_VALID (buf))
973 ts = GST_BUFFER_PTS (buf);
975 ts = GST_BUFFER_DTS (buf);
977 GST_SPLITMUX_LOCK (splitmux);
979 if (splitmux->state == SPLITMUX_STATE_STOPPED)
982 /* If this buffer has a timestamp, advance the input timestamp of the
984 if (GST_CLOCK_TIME_IS_VALID (ts)) {
985 GstClockTime running_time =
986 gst_segment_to_running_time (&ctx->in_segment, GST_FORMAT_TIME,
987 GST_BUFFER_TIMESTAMP (buf));
989 if (GST_CLOCK_TIME_IS_VALID (running_time) &&
990 (ctx->in_running_time == GST_CLOCK_TIME_NONE
991 || running_time > ctx->in_running_time))
992 ctx->in_running_time = running_time;
995 /* Try to make sure we have a valid running time */
996 if (!GST_CLOCK_TIME_IS_VALID (ctx->in_running_time)) {
997 ctx->in_running_time =
998 gst_segment_to_running_time (&ctx->in_segment, GST_FORMAT_TIME,
999 ctx->in_segment.start);
1002 buf_info->run_ts = ctx->in_running_time;
1003 buf_info->buf_size = gst_buffer_get_size (buf);
1005 /* Update total input byte counter for overflow detect */
1006 ctx->in_bytes += buf_info->buf_size;
1008 GST_DEBUG_OBJECT (pad, "Buf TS %" GST_TIME_FORMAT
1009 " total in_bytes %" G_GSIZE_FORMAT,
1010 GST_TIME_ARGS (buf_info->run_ts), ctx->in_bytes);
1017 switch (splitmux->state) {
1018 case SPLITMUX_STATE_COLLECTING_GOP_START:
1019 if (ctx->is_video) {
1020 /* If a keyframe, we have a complete GOP */
1021 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT) ||
1022 !GST_CLOCK_TIME_IS_VALID (ctx->in_running_time) ||
1023 splitmux->max_in_running_time >= ctx->in_running_time) {
1024 /* Pass this buffer through */
1028 GST_INFO_OBJECT (pad,
1029 "Have keyframe with running time %" GST_TIME_FORMAT,
1030 GST_TIME_ARGS (ctx->in_running_time));
1032 splitmux->state = SPLITMUX_STATE_WAITING_GOP_COMPLETE;
1033 splitmux->max_in_running_time = ctx->in_running_time;
1034 /* Wake up other input pads to collect this GOP */
1035 GST_SPLITMUX_BROADCAST (splitmux);
1036 check_completed_gop (splitmux, ctx);
1038 /* We're still waiting for a keyframe on the video pad, sleep */
1039 GST_LOG_OBJECT (pad, "Sleeping for GOP start");
1040 GST_SPLITMUX_WAIT (splitmux);
1041 GST_LOG_OBJECT (pad, "Done sleeping for GOP start state now %d",
1045 case SPLITMUX_STATE_WAITING_GOP_COMPLETE:
1046 /* After a GOP start is found, this buffer might complete the GOP */
1047 /* If we overran the target timestamp, it might be time to process
1048 * the GOP, otherwise bail out for more data
1050 GST_LOG_OBJECT (pad,
1051 "Checking TS %" GST_TIME_FORMAT " against max %" GST_TIME_FORMAT,
1052 GST_TIME_ARGS (ctx->in_running_time),
1053 GST_TIME_ARGS (splitmux->max_in_running_time));
1055 if (ctx->in_running_time < splitmux->max_in_running_time) {
1060 GST_LOG_OBJECT (pad,
1061 "Collected last packet of GOP. Checking other pads");
1062 check_completed_gop (splitmux, ctx);
1064 case SPLITMUX_STATE_ENDING_FILE:
1065 case SPLITMUX_STATE_START_NEXT_FRAGMENT:
1066 /* A fragment is ending, wait until that's done before continuing */
1067 GST_DEBUG_OBJECT (pad, "Sleeping for fragment restart");
1068 GST_SPLITMUX_WAIT (splitmux);
1069 GST_DEBUG_OBJECT (pad,
1070 "Done sleeping for fragment restart state now %d", splitmux->state);
1076 } while (loop_again);
1079 splitmux->queued_gops++;
1080 buf_info->keyframe = TRUE;
1083 /* Now add this buffer to the queue just before returning */
1084 g_queue_push_head (&ctx->queued_bufs, buf_info);
1086 /* Check the buffer will fit in the mq */
1087 check_queue_length (splitmux, ctx);
1089 GST_LOG_OBJECT (pad, "Returning to queue buffer %" GST_PTR_FORMAT
1090 " run ts %" GST_TIME_FORMAT, buf, GST_TIME_ARGS (ctx->in_running_time));
1092 GST_SPLITMUX_UNLOCK (splitmux);
1093 return GST_PAD_PROBE_PASS;
1096 GST_SPLITMUX_UNLOCK (splitmux);
1098 mq_stream_buf_free (buf_info);
1099 return GST_PAD_PROBE_PASS;
1103 gst_splitmux_sink_request_new_pad (GstElement * element,
1104 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1106 GstSplitMuxSink *splitmux = (GstSplitMuxSink *) element;
1107 GstPadTemplate *mux_template = NULL;
1109 GstPad *mq_sink, *mq_src;
1111 gboolean is_video = FALSE;
1114 GST_DEBUG_OBJECT (element, "templ:%s, name:%s", templ->name_template, name);
1116 GST_SPLITMUX_LOCK (splitmux);
1117 if (!create_elements (splitmux))
1120 if (templ->name_template) {
1121 if (g_str_equal (templ->name_template, "video")) {
1122 /* FIXME: Look for a pad template with matching caps, rather than by name */
1124 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS
1125 (splitmux->muxer), "video_%u");
1130 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS
1131 (splitmux->muxer), templ->name_template);
1135 res = gst_element_request_pad (splitmux->muxer, mux_template, name, caps);
1140 gname = g_strdup ("video");
1141 else if (name == NULL)
1142 gname = gst_pad_get_name (res);
1144 gname = g_strdup (name);
1146 if (!get_pads_from_mq (splitmux, &mq_sink, &mq_src)) {
1147 gst_element_release_request_pad (splitmux->muxer, res);
1148 gst_object_unref (GST_OBJECT (res));
1152 if (gst_pad_link (mq_src, res) != GST_PAD_LINK_OK) {
1153 gst_element_release_request_pad (splitmux->muxer, res);
1154 gst_object_unref (GST_OBJECT (res));
1155 gst_element_release_request_pad (splitmux->mq, mq_sink);
1156 gst_object_unref (GST_OBJECT (mq_sink));
1160 gst_object_unref (GST_OBJECT (res));
1162 ctx = mq_stream_ctx_new (splitmux);
1163 ctx->is_video = is_video;
1164 ctx->srcpad = mq_src;
1165 ctx->sinkpad = mq_sink;
1167 mq_stream_ctx_ref (ctx);
1168 ctx->src_pad_block_id =
1169 gst_pad_add_probe (mq_src, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
1170 (GstPadProbeCallback) handle_mq_output, ctx, (GDestroyNotify)
1171 _pad_block_destroy_src_notify);
1173 splitmux->video_ctx = ctx;
1175 res = gst_ghost_pad_new (gname, mq_sink);
1176 g_object_set_qdata ((GObject *) (res), PAD_CONTEXT, ctx);
1178 mq_stream_ctx_ref (ctx);
1179 ctx->sink_pad_block_id =
1180 gst_pad_add_probe (mq_sink, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
1181 (GstPadProbeCallback) handle_mq_input, ctx, (GDestroyNotify)
1182 _pad_block_destroy_sink_notify);
1184 GST_DEBUG_OBJECT (splitmux, "Request pad %" GST_PTR_FORMAT
1185 " is mq pad %" GST_PTR_FORMAT, res, mq_sink);
1187 splitmux->contexts = g_list_prepend (splitmux->contexts, ctx);
1191 gst_object_unref (mq_sink);
1192 gst_object_unref (mq_src);
1194 gst_pad_set_active (res, TRUE);
1195 gst_element_add_pad (element, res);
1196 GST_SPLITMUX_UNLOCK (splitmux);
1200 GST_SPLITMUX_UNLOCK (splitmux);
1205 gst_splitmux_sink_release_pad (GstElement * element, GstPad * pad)
1207 GstSplitMuxSink *splitmux = (GstSplitMuxSink *) element;
1208 GstPad *mqsink, *mqsrc, *muxpad;
1210 (MqStreamCtx *) (g_object_get_qdata ((GObject *) (pad), PAD_CONTEXT));
1212 GST_SPLITMUX_LOCK (splitmux);
1214 if (splitmux->muxer == NULL || splitmux->mq == NULL)
1215 goto fail; /* Elements don't exist yet - nothing to release */
1217 GST_INFO_OBJECT (pad, "releasing request pad");
1219 mqsink = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1220 mqsrc = mq_sink_to_src (splitmux->mq, mqsink);
1221 muxpad = gst_pad_get_peer (mqsrc);
1223 /* Remove the context from our consideration */
1224 splitmux->contexts = g_list_remove (splitmux->contexts, ctx);
1226 if (ctx->sink_pad_block_id)
1227 gst_pad_remove_probe (ctx->sinkpad, ctx->sink_pad_block_id);
1229 if (ctx->src_pad_block_id)
1230 gst_pad_remove_probe (ctx->srcpad, ctx->src_pad_block_id);
1232 /* Can release the context now */
1233 mq_stream_ctx_unref (ctx);
1235 /* Release and free the mq input */
1236 gst_element_release_request_pad (splitmux->mq, mqsink);
1238 /* Release and free the muxer input */
1239 gst_element_release_request_pad (splitmux->muxer, muxpad);
1241 gst_object_unref (mqsink);
1242 gst_object_unref (mqsrc);
1243 gst_object_unref (muxpad);
1245 gst_element_remove_pad (element, pad);
1248 GST_SPLITMUX_UNLOCK (splitmux);
1252 create_element (GstSplitMuxSink * splitmux,
1253 const gchar * factory, const gchar * name)
1255 GstElement *ret = gst_element_factory_make (factory, name);
1257 g_warning ("Failed to create %s - splitmuxsink will not work", name);
1261 if (!gst_bin_add (GST_BIN (splitmux), ret)) {
1262 g_warning ("Could not add %s element - splitmuxsink will not work", name);
1263 gst_object_unref (ret);
1271 create_elements (GstSplitMuxSink * splitmux)
1273 /* Create internal elements */
1274 if (splitmux->mq == NULL) {
1276 create_element (splitmux, "multiqueue", "multiqueue")) == NULL)
1279 splitmux->mq_max_buffers = 5;
1280 /* No bytes or time limit, we limit buffers manually */
1281 g_object_set (splitmux->mq, "max-size-bytes", 0, "max-size-time",
1282 (guint64) 0, "max-size-buffers", splitmux->mq_max_buffers, NULL);
1285 if (splitmux->muxer == NULL) {
1286 GstElement *provided_muxer = NULL;
1288 GST_OBJECT_LOCK (splitmux);
1289 if (splitmux->provided_muxer != NULL)
1290 provided_muxer = gst_object_ref (splitmux->provided_muxer);
1291 GST_OBJECT_UNLOCK (splitmux);
1293 if (provided_muxer == NULL) {
1294 if ((splitmux->muxer =
1295 create_element (splitmux, "mp4mux", "muxer")) == NULL)
1298 if (!gst_bin_add (GST_BIN (splitmux), provided_muxer)) {
1299 g_warning ("Could not add muxer element - splitmuxsink will not work");
1300 gst_object_unref (provided_muxer);
1304 splitmux->muxer = provided_muxer;
1305 gst_object_unref (provided_muxer);
1315 find_sink (GstElement * e)
1317 GstElement *res = NULL;
1319 gboolean done = FALSE;
1320 GValue data = { 0, };
1322 if (!GST_IS_BIN (e))
1325 iter = gst_bin_iterate_sinks (GST_BIN (e));
1327 switch (gst_iterator_next (iter, &data)) {
1328 case GST_ITERATOR_OK:
1330 GstElement *child = g_value_get_object (&data);
1331 if (g_object_class_find_property (G_OBJECT_GET_CLASS (child),
1332 "location") != NULL) {
1336 g_value_reset (&data);
1339 case GST_ITERATOR_RESYNC:
1340 gst_iterator_resync (iter);
1342 case GST_ITERATOR_DONE:
1345 case GST_ITERATOR_ERROR:
1346 g_assert_not_reached ();
1350 g_value_unset (&data);
1351 gst_iterator_free (iter);
1357 create_sink (GstSplitMuxSink * splitmux)
1359 GstElement *provided_sink = NULL;
1361 g_return_val_if_fail (splitmux->active_sink == NULL, TRUE);
1363 GST_OBJECT_LOCK (splitmux);
1364 if (splitmux->provided_sink != NULL)
1365 provided_sink = gst_object_ref (splitmux->provided_sink);
1366 GST_OBJECT_UNLOCK (splitmux);
1368 if (provided_sink == NULL) {
1369 if ((splitmux->sink =
1370 create_element (splitmux, DEFAULT_SINK, "sink")) == NULL)
1372 splitmux->active_sink = splitmux->sink;
1374 if (!gst_bin_add (GST_BIN (splitmux), provided_sink)) {
1375 g_warning ("Could not add sink elements - splitmuxsink will not work");
1376 gst_object_unref (provided_sink);
1380 splitmux->active_sink = provided_sink;
1382 /* The bin holds a ref now, we can drop our tmp ref */
1383 gst_object_unref (provided_sink);
1385 /* Find the sink element */
1386 splitmux->sink = find_sink (splitmux->active_sink);
1387 if (splitmux->sink == NULL) {
1389 ("Could not locate sink element in provided sink - splitmuxsink will not work");
1394 if (!gst_element_link (splitmux->muxer, splitmux->active_sink)) {
1395 g_warning ("Failed to link muxer and sink- splitmuxsink will not work");
1405 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
1408 set_next_filename (GstSplitMuxSink * splitmux)
1410 if (splitmux->location) {
1413 fname = g_strdup_printf (splitmux->location, splitmux->fragment_id);
1414 GST_INFO_OBJECT (splitmux, "Setting file to %s", fname);
1415 g_object_set (splitmux->sink, "location", fname, NULL);
1418 splitmux->fragment_id++;
1422 static GstStateChangeReturn
1423 gst_splitmux_sink_change_state (GstElement * element, GstStateChange transition)
1425 GstStateChangeReturn ret;
1426 GstSplitMuxSink *splitmux = (GstSplitMuxSink *) element;
1428 switch (transition) {
1429 case GST_STATE_CHANGE_NULL_TO_READY:{
1430 GST_SPLITMUX_LOCK (splitmux);
1431 if (!create_elements (splitmux) || !create_sink (splitmux)) {
1432 ret = GST_STATE_CHANGE_FAILURE;
1433 GST_SPLITMUX_UNLOCK (splitmux);
1436 GST_SPLITMUX_UNLOCK (splitmux);
1437 splitmux->fragment_id = 0;
1438 set_next_filename (splitmux);
1441 case GST_STATE_CHANGE_READY_TO_PAUSED:{
1442 GST_SPLITMUX_LOCK (splitmux);
1443 /* Start by collecting one input on each pad */
1444 splitmux->state = SPLITMUX_STATE_COLLECTING_GOP_START;
1445 splitmux->max_in_running_time = 0;
1446 splitmux->muxed_out_time = splitmux->mux_start_time = 0;
1447 splitmux->muxed_out_bytes = splitmux->mux_start_bytes = 0;
1448 GST_SPLITMUX_UNLOCK (splitmux);
1451 case GST_STATE_CHANGE_PAUSED_TO_READY:
1452 case GST_STATE_CHANGE_READY_TO_NULL:
1453 GST_SPLITMUX_LOCK (splitmux);
1454 splitmux->state = SPLITMUX_STATE_STOPPED;
1455 /* Wake up any blocked threads */
1456 GST_LOG_OBJECT (splitmux,
1457 "State change -> NULL or READY. Waking threads");
1458 GST_SPLITMUX_BROADCAST (splitmux);
1459 GST_SPLITMUX_UNLOCK (splitmux);
1465 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1466 if (ret == GST_STATE_CHANGE_FAILURE)
1469 switch (transition) {
1470 case GST_STATE_CHANGE_READY_TO_NULL:
1471 GST_SPLITMUX_LOCK (splitmux);
1472 splitmux->fragment_id = 0;
1473 gst_splitmux_reset (splitmux);
1474 GST_SPLITMUX_UNLOCK (splitmux);
1482 if (transition == GST_STATE_CHANGE_NULL_TO_READY &&
1483 ret == GST_STATE_CHANGE_FAILURE) {
1484 /* Cleanup elements on failed transition out of NULL */
1485 gst_splitmux_reset (splitmux);
1491 register_splitmuxsink (GstPlugin * plugin)
1493 GST_DEBUG_CATEGORY_INIT (splitmux_debug, "splitmuxsink", 0,
1494 "Split File Muxing Sink");
1496 return gst_element_register (plugin, "splitmuxsink", GST_RANK_NONE,
1497 GST_TYPE_SPLITMUX_SINK);