2 * Copyright (C) 2019 Stéphane Cerveau <scerveau@collabora.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-dashsink
24 * Dynamic Adaptive Streaming over HTTP sink/server
26 * ## Example launch line
28 * gst-launch-1.0 dashsink name=dashsink audiotestsrc is-live=true ! avenc_aac ! dashsink.audio_0 videotestsrc is-live=true ! x264enc ! dashsink.video_0
33 /* Implementation notes:
35 * The following section describes how dashsink works internally.
39 * This element aims to generate the Media Pressentation Description XML based file
40 * used as DASH content in addition to the necessary media fragments.
41 * Based on splitmuxsink branches to generate the media fragments,
42 * the element will generate a new adaptation set for each media type (video/audio/test)
43 * and a new representation for each additional stream for a media type.
44 * ,----------------dashsink------------------,
45 * ; ,----------splitmuxsink--------------, ;
46 * ,-videotestsrc-, ,-x264enc-, ; ; ,-Queue-, ,-mpegtsmux-, ,-filesink-, ; ;
47 * ; o--o o---o--o ; o-o o-o , ; ;
48 * '--------------' '---------' ; ; '-------' '---------' '----------' ; ;
49 * ; '------------------------------------' ;
51 * ; ,----------splitmuxsink--------------, ;
52 * ,-audiotestsrc-, ,-avenc_aac-, ; ; ,-Queue-, ,-mpegtsmux-, ,-filesink-, ; ;
53 * ; o--o o-o--o o-o o-o ; ; ;
54 * '--------------' '-----------' ; ; '-------' '---------' '----------' ; ;
55 * ; '------------------------------------' ;
56 * ' -----------------------------------------'
59 * | |_ Video Adaptation Set
60 * | | |_ Representation 1 - Container/Codec - bitrate X
61 * | |_ Representation 2 - Container/Codec - bitrate Y
62 * | |_ Audio Adaptation Set
63 * | |_ Representation 1 - Container/Codec - bitrate X
64 * | |_ Representation 2 - Container/Codec - bitrate Y
66 * This element is able to generate static or dynamic MPD with multiple adaptation sets,
67 * multiple representations and multiple periods for three kind of
68 * media streams (Video/Audio/Text).
70 * It supports any kind of stream input codec
71 * which can be encapsulated in Transport Stream (MPEG-TS) or ISO media format (MP4).
72 * The current implementation is generating compliant MPDs for both static and dynamic
73 * profiles with https://conformance.dashif.org/
77 * The fragments during the DASH generation does not look reliable enough to be used as
78 * a production solution. Some additional or fine tuning work needs to be performed to address
79 * these issues, especially for MP4 fragments.
87 #include "gstdashsink.h"
88 #include "gstmpdparser.h"
89 #include <gst/pbutils/pbutils.h>
90 #include <gst/video/video.h>
91 #include <glib/gstdio.h>
96 GST_DEBUG_CATEGORY_STATIC (gst_dash_sink_debug);
97 #define GST_CAT_DEFAULT gst_dash_sink_debug
100 * GstDashSinkMuxerType:
101 * @GST_DASH_SINK_MUXER_TS: Use mpegtsmux
102 * @GST_DASH_SINK_MUXER_MP4: Use mp4mux
108 GST_DASH_SINK_MUXER_TS = 0,
109 GST_DASH_SINK_MUXER_MP4 = 1,
110 } GstDashSinkMuxerType;
112 typedef struct _DashSinkMuxer
114 GstDashSinkMuxerType type;
115 const gchar *element_name;
116 const gchar *mimetype;
117 const gchar *file_ext;
120 #define GST_TYPE_DASH_SINK_MUXER (gst_dash_sink_muxer_get_type())
122 gst_dash_sink_muxer_get_type (void)
124 static GType dash_sink_muxer_type = 0;
125 static const GEnumValue muxer_type[] = {
126 {GST_DASH_SINK_MUXER_TS, "Use mpegtsmux", "ts"},
127 {GST_DASH_SINK_MUXER_MP4, "Use mp4mux", "mp4"},
131 if (!dash_sink_muxer_type) {
132 dash_sink_muxer_type =
133 g_enum_register_static ("GstDashSinkMuxerType", muxer_type);
135 return dash_sink_muxer_type;
138 static const DashSinkMuxer dash_muxer_list[] = {
140 GST_DASH_SINK_MUXER_TS,
145 GST_DASH_SINK_MUXER_MP4,
151 #define DEFAULT_SEGMENT_LIST_TPL "_%05d"
152 #define DEFAULT_SEGMENT_TEMPLATE_TPL "_%d"
153 #define DEFAULT_MPD_FILENAME "dash.mpd"
154 #define DEFAULT_MPD_ROOT_PATH NULL
155 #define DEFAULT_TARGET_DURATION 15
156 #define DEFAULT_SEND_KEYFRAME_REQUESTS TRUE
157 #define DEFAULT_MPD_NAMESPACE "urn:mpeg:dash:schema:mpd:2011"
158 #define DEFAULT_MPD_PROFILES "urn:mpeg:dash:profile:isoff-main:2011"
159 #define DEFAULT_MPD_SCHEMA_LOCATION "urn:mpeg:DASH:schema:MPD:2011 DASH-MPD.xsd"
160 #define DEFAULT_MPD_USE_SEGMENT_LIST FALSE
161 #define DEFAULT_MPD_MIN_BUFFER_TIME 2000
162 #define DEFAULT_MPD_PERIOD_DURATION GST_CLOCK_TIME_NONE
164 #define DEFAULT_DASH_SINK_MUXER GST_DASH_SINK_MUXER_TS
168 ADAPTATION_SET_ID_VIDEO = 1,
169 ADAPTATION_SET_ID_AUDIO,
170 ADAPTATION_SET_ID_SUBTITLE,
178 PROP_TARGET_DURATION,
179 PROP_SEND_KEYFRAME_REQUESTS,
180 PROP_USE_SEGMENT_LIST,
183 PROP_MPD_MINIMUM_UPDATE_PERIOD,
184 PROP_MPD_MIN_BUFFER_TIME,
186 PROP_MPD_PERIOD_DURATION,
191 SIGNAL_GET_PLAYLIST_STREAM,
192 SIGNAL_GET_FRAGMENT_STREAM,
196 static guint signals[SIGNAL_LAST];
200 DASH_SINK_STREAM_TYPE_VIDEO = 0,
201 DASH_SINK_STREAM_TYPE_AUDIO,
202 DASH_SINK_STREAM_TYPE_SUBTITLE,
203 DASH_SINK_STREAM_TYPE_UNKNOWN,
204 } GstDashSinkStreamType;
206 typedef struct _GstDashSinkStreamVideoInfo
210 } GstDashSinkStreamVideoInfo;
212 typedef struct _GstDashSinkStreamAudioInfo
216 } GstDashSinkStreamAudioInfo;
218 typedef struct GstDashSinkStreamSubtitleInfo
221 } GstDashSinkStreamSubtitleInfo;
223 typedef union _GstDashSinkStreamInfo
225 GstDashSinkStreamVideoInfo video;
226 GstDashSinkStreamAudioInfo audio;
227 GstDashSinkStreamSubtitleInfo subtitle;
228 } GstDashSinkStreamInfo;
236 gchar *mpd_root_path;
239 GstDashSinkMuxerType muxer;
240 GstMPDClient *mpd_client;
241 gchar *current_period_id;
242 gint target_duration;
243 GstClockTime running_time;
244 gboolean send_keyframe_requests;
245 gboolean use_segment_list;
247 gchar *segment_file_tpl;
250 guint64 minimum_update_period;
251 guint64 min_buffer_time;
252 gint64 period_duration;
255 typedef struct _GstDashSinkStream
258 GstDashSinkStreamType type;
261 GstElement *splitmuxsink;
262 gint adaptation_set_id;
263 gchar *representation_id;
264 gchar *current_segment_location;
265 gint current_segment_id;
266 gint next_segment_id;
270 GstClockTime current_running_time_start;
271 GstDashSinkStreamInfo info;
272 GstElement *giostreamsink;
275 static GstStaticPadTemplate video_sink_template =
276 GST_STATIC_PAD_TEMPLATE ("video_%u",
279 GST_STATIC_CAPS_ANY);
282 static GstStaticPadTemplate audio_sink_template =
283 GST_STATIC_PAD_TEMPLATE ("audio_%u",
286 GST_STATIC_CAPS_ANY);
287 static GstStaticPadTemplate subtitle_sink_template =
288 GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
291 GST_STATIC_CAPS_ANY);
293 #define gst_dash_sink_parent_class parent_class
294 G_DEFINE_TYPE_WITH_CODE (GstDashSink, gst_dash_sink, GST_TYPE_BIN,
295 GST_DEBUG_CATEGORY_INIT (gst_dash_sink_debug, "dashsink", 0, "DashSink"));
296 GST_ELEMENT_REGISTER_DEFINE (dashsink, "dashsink", GST_RANK_NONE,
297 gst_dash_sink_get_type ());
299 static void gst_dash_sink_set_property (GObject * object, guint prop_id,
300 const GValue * value, GParamSpec * spec);
301 static void gst_dash_sink_get_property (GObject * object, guint prop_id,
302 GValue * value, GParamSpec * spec);
303 static void gst_dash_sink_handle_message (GstBin * bin, GstMessage * message);
304 static void gst_dash_sink_reset (GstDashSink * sink);
305 static GstStateChangeReturn
306 gst_dash_sink_change_state (GstElement * element, GstStateChange trans);
307 static GstPad *gst_dash_sink_request_new_pad (GstElement * element,
308 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
309 static void gst_dash_sink_release_pad (GstElement * element, GstPad * pad);
312 static GstDashSinkStream *
313 gst_dash_sink_stream_from_pad (GList * streams, GstPad * pad)
316 GstDashSinkStream *stream = NULL;
317 for (l = streams; l != NULL; l = l->next) {
319 if (stream->pad == pad)
325 static GstDashSinkStream *
326 gst_dash_sink_stream_from_splitmuxsink (GList * streams, GstElement * element)
329 GstDashSinkStream *stream = NULL;
330 for (l = streams; l != NULL; l = l->next) {
332 if (stream->splitmuxsink == element)
339 gst_dash_sink_stream_get_next_name (GList * streams, GstDashSinkStreamType type)
343 GstDashSinkStream *stream = NULL;
346 for (l = streams; l != NULL; l = l->next) {
348 if (stream->type == type)
353 case DASH_SINK_STREAM_TYPE_VIDEO:
354 name = g_strdup_printf ("video_%d", count);
356 case DASH_SINK_STREAM_TYPE_AUDIO:
357 name = g_strdup_printf ("audio_%d", count);
359 case DASH_SINK_STREAM_TYPE_SUBTITLE:
360 name = g_strdup_printf ("sub_%d", count);
363 name = g_strdup_printf ("unknown_%d", count);
370 gst_dash_sink_stream_free (gpointer s)
372 GstDashSinkStream *stream = (GstDashSinkStream *) s;
373 g_object_unref (stream->sink);
374 g_free (stream->current_segment_location);
375 g_free (stream->representation_id);
376 g_free (stream->mimetype);
377 g_free (stream->codec);
383 gst_dash_sink_dispose (GObject * object)
385 GstDashSink *sink = GST_DASH_SINK (object);
387 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
391 gst_dash_sink_finalize (GObject * object)
393 GstDashSink *sink = GST_DASH_SINK (object);
395 g_free (sink->mpd_filename);
396 g_free (sink->mpd_root_path);
397 g_free (sink->mpd_profiles);
398 if (sink->mpd_client)
399 gst_mpd_client_free (sink->mpd_client);
400 g_mutex_clear (&sink->mpd_lock);
402 g_list_free_full (sink->streams, gst_dash_sink_stream_free);
404 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) sink);
407 /* Default implementations for the signal handlers */
408 static GOutputStream *
409 gst_dash_sink_get_playlist_stream (GstDashSink * sink, const gchar * location)
411 GFile *file = g_file_new_for_path (location);
412 GOutputStream *ostream;
416 G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE,
417 G_FILE_CREATE_REPLACE_DESTINATION, NULL, &err));
419 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
420 (("Got no output stream for playlist '%s': %s."), location,
421 err->message), (NULL));
422 g_clear_error (&err);
425 g_object_unref (file);
430 static GOutputStream *
431 gst_dash_sink_get_fragment_stream (GstDashSink * sink, const gchar * location)
433 GFile *file = g_file_new_for_path (location);
434 GOutputStream *ostream;
438 G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE,
439 G_FILE_CREATE_REPLACE_DESTINATION, NULL, &err));
441 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
442 (("Got no output stream for fragment '%s': %s."), location,
443 err->message), (NULL));
444 g_clear_error (&err);
447 g_object_unref (file);
453 gst_dash_sink_class_init (GstDashSinkClass * klass)
455 GObjectClass *gobject_class;
456 GstElementClass *element_class;
457 GstBinClass *bin_class;
459 gobject_class = (GObjectClass *) klass;
460 element_class = GST_ELEMENT_CLASS (klass);
461 bin_class = GST_BIN_CLASS (klass);
463 gst_element_class_add_static_pad_template (element_class,
464 &video_sink_template);
465 gst_element_class_add_static_pad_template (element_class,
466 &audio_sink_template);
467 gst_element_class_add_static_pad_template (element_class,
468 &subtitle_sink_template);
470 gst_element_class_set_static_metadata (element_class,
472 "Dynamic Adaptive Streaming over HTTP sink",
473 "Stéphane Cerveau <scerveau@collabora.com>");
475 element_class->change_state = GST_DEBUG_FUNCPTR (gst_dash_sink_change_state);
476 element_class->request_new_pad =
477 GST_DEBUG_FUNCPTR (gst_dash_sink_request_new_pad);
478 element_class->release_pad = GST_DEBUG_FUNCPTR (gst_dash_sink_release_pad);
480 bin_class->handle_message = gst_dash_sink_handle_message;
482 gobject_class->dispose = gst_dash_sink_dispose;
483 gobject_class->finalize = gst_dash_sink_finalize;
484 gobject_class->set_property = gst_dash_sink_set_property;
485 gobject_class->get_property = gst_dash_sink_get_property;
487 g_object_class_install_property (gobject_class, PROP_MPD_FILENAME,
488 g_param_spec_string ("mpd-filename", "MPD filename",
489 "filename of the mpd to write", DEFAULT_MPD_FILENAME,
490 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
491 g_object_class_install_property (gobject_class, PROP_MPD_ROOT_PATH,
492 g_param_spec_string ("mpd-root-path", "MPD Root Path",
493 "Path where the MPD and its fragents will be written",
494 DEFAULT_MPD_ROOT_PATH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
495 g_object_class_install_property (gobject_class, PROP_MPD_BASEURL,
496 g_param_spec_string ("mpd-baseurl", "MPD BaseURL",
497 "BaseURL to set in the MPD", DEFAULT_MPD_ROOT_PATH,
498 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
499 g_object_class_install_property (gobject_class, PROP_TARGET_DURATION,
500 g_param_spec_uint ("target-duration", "Target duration",
501 "The target duration in seconds of a segment/file. "
502 "(0 - disabled, useful for management of segment duration by the "
503 "streaming server)", 0, G_MAXUINT, DEFAULT_TARGET_DURATION,
504 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
505 g_object_class_install_property (gobject_class, PROP_SEND_KEYFRAME_REQUESTS,
506 g_param_spec_boolean ("send-keyframe-requests", "Send Keyframe Requests",
507 "Send keyframe requests to ensure correct fragmentation. If this is disabled "
508 "then the input must have keyframes in regular intervals",
509 DEFAULT_SEND_KEYFRAME_REQUESTS,
510 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
511 g_object_class_install_property (gobject_class, PROP_USE_SEGMENT_LIST,
512 g_param_spec_boolean ("use-segment-list", "Use segment list",
513 "Use segment list instead of segment template to create the segments",
514 DEFAULT_MPD_USE_SEGMENT_LIST,
515 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
516 g_object_class_install_property (gobject_class, PROP_MPD_DYNAMIC,
517 g_param_spec_boolean ("dynamic", "dynamic", "Provides a dynamic mpd",
518 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
519 g_object_class_install_property (gobject_class, PROP_MUXER,
520 g_param_spec_enum ("muxer", "Muxer",
521 "Muxer type to be used by dashsink to generate the fragment",
522 GST_TYPE_DASH_SINK_MUXER, DEFAULT_DASH_SINK_MUXER,
523 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
524 g_object_class_install_property (gobject_class,
525 PROP_MPD_MINIMUM_UPDATE_PERIOD,
526 g_param_spec_uint64 ("minimum-update-period", "Minimum update period",
527 "Provides to the manifest a minimum update period in milliseconds", 0,
528 G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
529 g_object_class_install_property (gobject_class,
530 PROP_MPD_MIN_BUFFER_TIME,
531 g_param_spec_uint64 ("min-buffer-time", "Mininim buffer time",
532 "Provides to the manifest a minimum buffer time in milliseconds", 0,
533 G_MAXUINT64, DEFAULT_MPD_MIN_BUFFER_TIME,
534 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
535 g_object_class_install_property (gobject_class,
536 PROP_MPD_PERIOD_DURATION,
537 g_param_spec_uint64 ("period-duration", "period duration",
538 "Provides the explicit duration of a period in milliseconds", 0,
539 G_MAXUINT64, DEFAULT_MPD_PERIOD_DURATION,
540 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
543 * GstDashSink::get-playlist-stream:
544 * @sink: the #GstDashSink
545 * @location: Location for the playlist file
547 * Returns: #GOutputStream for writing the playlist file.
551 signals[SIGNAL_GET_PLAYLIST_STREAM] =
552 g_signal_new_class_handler ("get-playlist-stream",
553 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
554 G_CALLBACK (gst_dash_sink_get_playlist_stream), NULL, NULL, NULL,
555 G_TYPE_OUTPUT_STREAM, 1, G_TYPE_STRING);
558 * GstDashSink::get-fragment-stream:
559 * @sink: the #GstDashSink
560 * @location: Location for the fragment file
562 * Returns: #GOutputStream for writing the fragment file.
566 signals[SIGNAL_GET_FRAGMENT_STREAM] =
567 g_signal_new_class_handler ("get-fragment-stream",
568 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
569 G_CALLBACK (gst_dash_sink_get_fragment_stream), NULL, NULL, NULL,
570 G_TYPE_OUTPUT_STREAM, 1, G_TYPE_STRING);
572 gst_type_mark_as_plugin_api (GST_TYPE_DASH_SINK_MUXER, 0);
576 on_format_location (GstElement * splitmuxsink, guint fragment_id,
577 GstDashSinkStream * dash_stream)
579 GOutputStream *stream = NULL;
580 GstDashSink *sink = dash_stream->sink;
581 gchar *segment_tpl_path;
583 dash_stream->current_segment_id = dash_stream->next_segment_id;
584 g_free (dash_stream->current_segment_location);
585 if (sink->use_segment_list)
586 dash_stream->current_segment_location =
587 g_strdup_printf ("%s" DEFAULT_SEGMENT_LIST_TPL ".%s",
588 dash_stream->representation_id, dash_stream->current_segment_id,
589 dash_muxer_list[sink->muxer].file_ext);
591 dash_stream->current_segment_location =
592 g_strdup_printf ("%s" DEFAULT_SEGMENT_TEMPLATE_TPL ".%s",
593 dash_stream->representation_id, dash_stream->current_segment_id,
594 dash_muxer_list[sink->muxer].file_ext);
596 dash_stream->next_segment_id++;
598 if (sink->mpd_root_path)
600 g_build_path (G_DIR_SEPARATOR_S, sink->mpd_root_path,
601 dash_stream->current_segment_location, NULL);
603 segment_tpl_path = g_strdup (dash_stream->current_segment_location);
606 g_signal_emit (sink, signals[SIGNAL_GET_FRAGMENT_STREAM], 0, segment_tpl_path,
610 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
611 (("Got no output stream for fragment '%s'."), segment_tpl_path),
614 g_object_set (dash_stream->giostreamsink, "stream", stream, NULL);
617 g_object_unref (stream);
619 g_free (segment_tpl_path);
625 gst_dash_sink_add_splitmuxsink (GstDashSink * sink, GstDashSinkStream * stream)
628 gst_element_factory_make (dash_muxer_list[sink->muxer].element_name,
631 if (sink->muxer == GST_DASH_SINK_MUXER_MP4)
632 g_object_set (mux, "fragment-duration", sink->target_duration * GST_MSECOND,
635 g_return_val_if_fail (mux != NULL, FALSE);
637 stream->splitmuxsink = gst_element_factory_make ("splitmuxsink", NULL);
638 if (!stream->splitmuxsink) {
639 gst_object_unref (mux);
642 stream->giostreamsink = gst_element_factory_make ("giostreamsink", NULL);
643 if (!stream->giostreamsink) {
644 gst_object_unref (stream->splitmuxsink);
645 gst_object_unref (mux);
649 gst_bin_add (GST_BIN (sink), stream->splitmuxsink);
651 if (!sink->use_segment_list)
652 stream->current_segment_id = 1;
654 stream->current_segment_id = 0;
655 stream->next_segment_id = stream->current_segment_id;
657 g_object_set (stream->splitmuxsink, "location", NULL,
658 "max-size-time", ((GstClockTime) sink->target_duration * GST_SECOND),
659 "send-keyframe-requests", TRUE, "muxer", mux, "sink",
660 stream->giostreamsink, "reset-muxer", FALSE, "send-keyframe-requests",
661 sink->send_keyframe_requests, NULL);
663 g_signal_connect (stream->splitmuxsink, "format-location",
664 G_CALLBACK (on_format_location), stream);
670 gst_dash_sink_init (GstDashSink * sink)
672 sink->mpd_filename = g_strdup (DEFAULT_MPD_FILENAME);
673 sink->mpd_root_path = g_strdup (DEFAULT_MPD_ROOT_PATH);
674 sink->mpd_client = NULL;
676 sink->target_duration = DEFAULT_TARGET_DURATION;
677 sink->send_keyframe_requests = DEFAULT_SEND_KEYFRAME_REQUESTS;
678 sink->mpd_profiles = g_strdup (DEFAULT_MPD_PROFILES);
679 sink->use_segment_list = DEFAULT_MPD_USE_SEGMENT_LIST;
681 sink->min_buffer_time = DEFAULT_MPD_MIN_BUFFER_TIME;
682 sink->period_duration = DEFAULT_MPD_PERIOD_DURATION;
684 g_mutex_init (&sink->mpd_lock);
686 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_FLAG_SINK);
688 gst_dash_sink_reset (sink);
692 gst_dash_sink_reset (GstDashSink * sink)
698 gst_dash_sink_get_stream_metadata (GstDashSink * sink,
699 GstDashSinkStream * stream)
702 GstCaps *caps = gst_pad_get_current_caps (stream->pad);
704 GST_DEBUG_OBJECT (sink, "stream caps %s", gst_caps_to_string (caps));
705 s = gst_caps_get_structure (caps, 0);
707 switch (stream->type) {
708 case DASH_SINK_STREAM_TYPE_VIDEO:
710 gst_structure_get_int (s, "width", &stream->info.video.width);
711 gst_structure_get_int (s, "height", &stream->info.video.height);
712 g_free (stream->codec);
714 g_strdup (gst_mpd_helper_get_video_codec_from_mime (caps));
717 case DASH_SINK_STREAM_TYPE_AUDIO:
719 gst_structure_get_int (s, "channels", &stream->info.audio.channels);
720 gst_structure_get_int (s, "rate", &stream->info.audio.rate);
721 g_free (stream->codec);
723 g_strdup (gst_mpd_helper_get_audio_codec_from_mime (caps));
726 case DASH_SINK_STREAM_TYPE_SUBTITLE:
734 gst_caps_unref (caps);
738 gst_dash_sink_generate_mpd_content (GstDashSink * sink,
739 GstDashSinkStream * stream)
741 if (!sink->mpd_client) {
743 sink->mpd_client = gst_mpd_client_new ();
744 /* Add or set root node with stream ids */
745 gst_mpd_client_set_root_node (sink->mpd_client,
746 "profiles", sink->mpd_profiles,
747 "default-namespace", DEFAULT_MPD_NAMESPACE,
748 "min-buffer-time", sink->min_buffer_time, NULL);
749 if (sink->is_dynamic) {
750 GstDateTime *now = gst_date_time_new_now_utc ();
751 gst_mpd_client_set_root_node (sink->mpd_client,
752 "type", GST_MPD_FILE_TYPE_DYNAMIC,
753 "availability-start-time", now, "publish-time", now, NULL);
754 gst_date_time_unref (now);
756 if (sink->minimum_update_period)
757 gst_mpd_client_set_root_node (sink->mpd_client,
758 "minimum-update-period", sink->minimum_update_period, NULL);
759 if (sink->mpd_baseurl)
760 gst_mpd_client_add_baseurl_node (sink->mpd_client, "url",
761 sink->mpd_baseurl, NULL);
762 /* Add or set period node with stream ids
763 * TODO support multiple period
765 sink->current_period_id =
766 gst_mpd_client_set_period_node (sink->mpd_client,
767 sink->current_period_id, NULL);
768 for (l = sink->streams; l != NULL; l = l->next) {
769 GstDashSinkStream *stream = (GstDashSinkStream *) l->data;
770 /* Add or set adaptation_set node with stream ids
771 * AdaptationSet per stream type
773 gst_mpd_client_set_adaptation_set_node (sink->mpd_client,
774 sink->current_period_id, stream->adaptation_set_id, NULL);
775 /* Add or set representation node with stream ids */
776 gst_mpd_client_set_representation_node (sink->mpd_client,
777 sink->current_period_id, stream->adaptation_set_id,
778 stream->representation_id, "bandwidth", stream->bitrate, "mime-type",
779 stream->mimetype, "codecs", stream->codec, NULL);
780 /* Set specific to stream type */
781 if (stream->type == DASH_SINK_STREAM_TYPE_VIDEO) {
782 gst_mpd_client_set_adaptation_set_node (sink->mpd_client,
783 sink->current_period_id, stream->adaptation_set_id, "content-type",
785 gst_mpd_client_set_representation_node (sink->mpd_client,
786 sink->current_period_id, stream->adaptation_set_id,
787 stream->representation_id, "width", stream->info.video.width,
788 "height", stream->info.video.height, NULL);
789 } else if (stream->type == DASH_SINK_STREAM_TYPE_AUDIO) {
790 gst_mpd_client_set_adaptation_set_node (sink->mpd_client,
791 sink->current_period_id, stream->adaptation_set_id, "content-type",
793 gst_mpd_client_set_representation_node (sink->mpd_client,
794 sink->current_period_id, stream->adaptation_set_id,
795 stream->representation_id, "audio-sampling-rate",
796 stream->info.audio.rate, NULL);
798 if (sink->use_segment_list) {
799 /* Add a default segment list */
800 gst_mpd_client_set_segment_list (sink->mpd_client,
801 sink->current_period_id, stream->adaptation_set_id,
802 stream->representation_id, "duration", sink->target_duration, NULL);
804 gchar *media_segment_template =
805 g_strconcat (stream->representation_id, "_$Number$",
806 ".", dash_muxer_list[sink->muxer].file_ext, NULL);
807 gst_mpd_client_set_segment_template (sink->mpd_client,
808 sink->current_period_id, stream->adaptation_set_id,
809 stream->representation_id, "media", media_segment_template,
810 "duration", sink->target_duration, NULL);
811 g_free (media_segment_template);
816 if (sink->use_segment_list) {
817 GST_INFO_OBJECT (sink, "Add segment URL: %s",
818 stream->current_segment_location);
819 gst_mpd_client_add_segment_url (sink->mpd_client, sink->current_period_id,
820 stream->adaptation_set_id, stream->representation_id, "media",
821 stream->current_segment_location, NULL);
823 if (!sink->is_dynamic) {
824 if (sink->period_duration != DEFAULT_MPD_PERIOD_DURATION)
825 gst_mpd_client_set_period_node (sink->mpd_client,
826 sink->current_period_id, "duration", sink->period_duration, NULL);
828 gst_mpd_client_set_period_node (sink->mpd_client,
829 sink->current_period_id, "duration",
830 gst_util_uint64_scale (sink->running_time, 1, GST_MSECOND), NULL);
832 if (!sink->minimum_update_period) {
833 if (sink->period_duration != DEFAULT_MPD_PERIOD_DURATION)
834 gst_mpd_client_set_root_node (sink->mpd_client,
835 "media-presentation-duration", sink->period_duration, NULL);
837 gst_mpd_client_set_root_node (sink->mpd_client,
838 "media-presentation-duration",
839 gst_util_uint64_scale (sink->running_time, 1, GST_MSECOND), NULL);
845 gst_dash_sink_write_mpd_file (GstDashSink * sink,
846 GstDashSinkStream * current_stream)
848 char *mpd_content = NULL;
850 GError *error = NULL;
851 gchar *mpd_filepath = NULL;
852 GOutputStream *file_stream = NULL;
853 gsize bytes_to_write;
855 g_mutex_lock (&sink->mpd_lock);
856 gst_dash_sink_generate_mpd_content (sink, current_stream);
857 if (!gst_mpd_client_get_xml_content (sink->mpd_client, &mpd_content, &size))
859 g_mutex_unlock (&sink->mpd_lock);
861 if (sink->mpd_root_path)
863 g_build_path (G_DIR_SEPARATOR_S, sink->mpd_root_path,
864 sink->mpd_filename, NULL);
866 mpd_filepath = g_strdup (sink->mpd_filename);
867 GST_DEBUG_OBJECT (sink, "a new mpd content is available: %s", mpd_content);
868 GST_DEBUG_OBJECT (sink, "write mpd to %s", mpd_filepath);
870 g_signal_emit (sink, signals[SIGNAL_GET_PLAYLIST_STREAM], 0, mpd_filepath,
873 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
874 (("Got no output stream for fragment '%s'."), mpd_filepath), (NULL));
877 bytes_to_write = strlen (mpd_content);
878 if (!g_output_stream_write_all (file_stream, mpd_content, bytes_to_write,
879 NULL, NULL, &error)) {
880 GST_ERROR ("Failed to write mpd content: %s", error->message);
881 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
882 (("Failed to write playlist '%s'."), error->message), (NULL));
883 g_error_free (error);
887 g_free (mpd_content);
888 g_free (mpd_filepath);
889 g_object_unref (file_stream);
893 gst_dash_sink_handle_message (GstBin * bin, GstMessage * message)
895 GstDashSink *sink = GST_DASH_SINK (bin);
896 GstDashSinkStream *stream = NULL;
897 switch (message->type) {
898 case GST_MESSAGE_ELEMENT:
900 const GstStructure *s = gst_message_get_structure (message);
901 GST_DEBUG_OBJECT (sink, "Received message with name %s",
902 gst_structure_get_name (s));
904 gst_dash_sink_stream_from_splitmuxsink (sink->streams,
905 GST_ELEMENT (message->src));
907 if (gst_structure_has_name (s, "splitmuxsink-fragment-opened")) {
908 gst_dash_sink_get_stream_metadata (sink, stream);
909 gst_structure_get_clock_time (s, "running-time",
910 &stream->current_running_time_start);
911 } else if (gst_structure_has_name (s, "splitmuxsink-fragment-closed")) {
912 GstClockTime running_time;
913 gst_structure_get_clock_time (s, "running-time", &running_time);
914 if (sink->running_time < running_time)
915 sink->running_time = running_time;
916 gst_dash_sink_write_mpd_file (sink, stream);
921 case GST_MESSAGE_EOS:{
922 gst_dash_sink_write_mpd_file (sink, NULL);
929 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
932 static GstPadProbeReturn
933 _dash_sink_buffers_probe (GstPad * pad, GstPadProbeInfo * probe_info,
936 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (probe_info);
937 GstDashSinkStream *stream = (GstDashSinkStream *) user_data;
939 if (GST_BUFFER_DURATION (buffer))
941 gst_buffer_get_size (buffer) * GST_SECOND /
942 GST_BUFFER_DURATION (buffer);
944 return GST_PAD_PROBE_OK;
948 gst_dash_sink_request_new_pad (GstElement * element, GstPadTemplate * templ,
949 const gchar * pad_name, const GstCaps * caps)
951 GstDashSink *sink = GST_DASH_SINK (element);
952 GstDashSinkStream *stream = NULL;
955 const gchar *split_pad_name = pad_name;
957 stream = g_new0 (GstDashSinkStream, 1);
958 stream->sink = g_object_ref (sink);
959 if (g_str_has_prefix (templ->name_template, "video")) {
960 stream->type = DASH_SINK_STREAM_TYPE_VIDEO;
961 stream->adaptation_set_id = ADAPTATION_SET_ID_VIDEO;
962 split_pad_name = "video";
963 } else if (g_str_has_prefix (templ->name_template, "audio")) {
964 stream->type = DASH_SINK_STREAM_TYPE_AUDIO;
965 stream->adaptation_set_id = ADAPTATION_SET_ID_AUDIO;
966 } else if (g_str_has_prefix (templ->name_template, "subtitle")) {
967 stream->type = DASH_SINK_STREAM_TYPE_SUBTITLE;
968 stream->adaptation_set_id = ADAPTATION_SET_ID_SUBTITLE;
972 stream->representation_id = g_strdup (pad_name);
974 stream->representation_id =
975 gst_dash_sink_stream_get_next_name (sink->streams, stream->type);
978 stream->mimetype = g_strdup (dash_muxer_list[sink->muxer].mimetype);
981 if (!gst_dash_sink_add_splitmuxsink (sink, stream)) {
982 GST_ERROR_OBJECT (sink,
983 "Unable to create splitmuxsink element for pad template name %s",
984 templ->name_template);
985 gst_dash_sink_stream_free (stream);
989 peer = gst_element_request_pad_simple (stream->splitmuxsink, split_pad_name);
991 GST_ERROR_OBJECT (sink, "Unable to request pad name %s", split_pad_name);
995 pad = gst_ghost_pad_new_from_template (pad_name, peer, templ);
996 gst_pad_set_active (pad, TRUE);
997 gst_element_add_pad (element, pad);
998 gst_object_unref (peer);
1002 stream->buffer_probe = gst_pad_add_probe (stream->pad,
1003 GST_PAD_PROBE_TYPE_BUFFER, _dash_sink_buffers_probe, stream, NULL);
1005 sink->streams = g_list_append (sink->streams, stream);
1006 GST_DEBUG_OBJECT (sink, "Adding a new stream with id %s",
1007 stream->representation_id);
1014 gst_dash_sink_release_pad (GstElement * element, GstPad * pad)
1016 GstDashSink *sink = GST_DASH_SINK (element);
1018 GstDashSinkStream *stream =
1019 gst_dash_sink_stream_from_pad (sink->streams, pad);
1021 g_return_if_fail (stream != NULL);
1023 peer = gst_pad_get_peer (pad);
1025 gst_element_release_request_pad (stream->splitmuxsink, pad);
1026 gst_object_unref (peer);
1029 if (stream->buffer_probe > 0) {
1030 gst_pad_remove_probe (pad, stream->buffer_probe);
1031 stream->buffer_probe = 0;
1034 gst_object_ref (pad);
1035 gst_element_remove_pad (element, pad);
1036 gst_pad_set_active (pad, FALSE);
1040 gst_object_unref (pad);
1043 static GstStateChangeReturn
1044 gst_dash_sink_change_state (GstElement * element, GstStateChange trans)
1046 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1047 GstDashSink *sink = GST_DASH_SINK (element);
1050 case GST_STATE_CHANGE_NULL_TO_READY:
1051 if (!g_list_length (sink->streams)) {
1052 return GST_STATE_CHANGE_FAILURE;
1059 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
1062 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1064 case GST_STATE_CHANGE_PAUSED_TO_READY:
1065 case GST_STATE_CHANGE_READY_TO_NULL:
1066 gst_dash_sink_reset (sink);
1076 gst_dash_sink_set_property (GObject * object, guint prop_id,
1077 const GValue * value, GParamSpec * pspec)
1079 GstDashSink *sink = GST_DASH_SINK (object);
1082 case PROP_MPD_FILENAME:
1083 g_free (sink->mpd_filename);
1084 sink->mpd_filename = g_value_dup_string (value);
1086 case PROP_MPD_ROOT_PATH:
1087 g_free (sink->mpd_root_path);
1088 sink->mpd_root_path = g_value_dup_string (value);
1090 case PROP_MPD_BASEURL:
1091 g_free (sink->mpd_baseurl);
1092 sink->mpd_baseurl = g_value_dup_string (value);
1094 case PROP_TARGET_DURATION:
1095 sink->target_duration = g_value_get_uint (value);
1097 case PROP_SEND_KEYFRAME_REQUESTS:
1098 sink->send_keyframe_requests = g_value_get_boolean (value);
1100 case PROP_USE_SEGMENT_LIST:
1101 sink->use_segment_list = g_value_get_boolean (value);
1103 case PROP_MPD_DYNAMIC:
1104 sink->is_dynamic = g_value_get_boolean (value);
1107 sink->muxer = g_value_get_enum (value);
1109 case PROP_MPD_MINIMUM_UPDATE_PERIOD:
1110 sink->minimum_update_period = g_value_get_uint64 (value);
1112 case PROP_MPD_MIN_BUFFER_TIME:
1113 sink->min_buffer_time = g_value_get_uint64 (value);
1115 case PROP_MPD_PERIOD_DURATION:
1116 sink->period_duration = g_value_get_uint64 (value);
1119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1125 gst_dash_sink_get_property (GObject * object, guint prop_id,
1126 GValue * value, GParamSpec * pspec)
1128 GstDashSink *sink = GST_DASH_SINK (object);
1131 case PROP_MPD_FILENAME:
1132 g_value_set_string (value, sink->mpd_filename);
1134 case PROP_MPD_ROOT_PATH:
1135 g_value_set_string (value, sink->mpd_root_path);
1137 case PROP_MPD_BASEURL:
1138 g_value_set_string (value, sink->mpd_baseurl);
1140 case PROP_TARGET_DURATION:
1141 g_value_set_uint (value, sink->target_duration);
1143 case PROP_SEND_KEYFRAME_REQUESTS:
1144 g_value_set_boolean (value, sink->send_keyframe_requests);
1146 case PROP_USE_SEGMENT_LIST:
1147 g_value_set_boolean (value, sink->use_segment_list);
1149 case PROP_MPD_DYNAMIC:
1150 g_value_set_boolean (value, sink->is_dynamic);
1153 g_value_set_enum (value, sink->muxer);
1155 case PROP_MPD_MINIMUM_UPDATE_PERIOD:
1156 g_value_set_uint64 (value, sink->minimum_update_period);
1158 case PROP_MPD_MIN_BUFFER_TIME:
1159 g_value_set_uint64 (value, sink->min_buffer_time);
1161 case PROP_MPD_PERIOD_DURATION:
1162 g_value_set_uint64 (value, sink->period_duration);
1165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);