2 * DASH demux plugin for GStreamer
6 * Copyright (C) 2012 Orange
9 * David Corvoysier <david.corvoysier@orange.com>
10 * Hamid Zakari <hamid.zakari@gmail.com>
12 * Copyright (C) 2013 Smart TV Alliance
13 * Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>, Collabora Ltd.
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Library General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Library General Public License for more details.
25 * You should have received a copy of the GNU Library General Public
26 * License along with this library (COPYING); if not, write to the
27 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
31 * SECTION:element-dashdemux
34 * DASH demuxer element.
35 * ## Example launch line
37 * gst-launch-1.0 playbin uri="http://www-itec.uni-klu.ac.at/ftp/datasets/mmsys12/RedBullPlayStreets/redbull_4s/RedBullPlayStreets_4s_isoffmain_DIS_23009_1_v_2_1c2_2011_08_30.mpd"
41 /* Implementation notes:
43 * The following section describes how dashdemux works internally.
47 * dashdemux is a "fake" demux, as unlike traditional demux elements, it
48 * doesn't split data streams contained in an envelope to expose them to
49 * downstream decoding elements.
51 * Instead, it parses an XML file called a manifest to identify a set of
52 * individual stream fragments it needs to fetch and expose to the actual
53 * demux elements that will handle them (this behavior is sometimes
54 * referred as the "demux after a demux" scenario).
56 * For a given section of content, several representations corresponding
57 * to different bitrates may be available: dashdemux will select the most
58 * appropriate representation based on local conditions (typically the
59 * available bandwidth and the amount of buffering available, capped by
60 * a maximum allowed bitrate).
62 * The representation selection algorithm can be configured using
63 * specific properties: max bitrate, min/max buffering, bandwidth ratio.
68 * dashdemux has a single sink pad that accepts the data corresponding
69 * to the manifest, typically fetched from an HTTP or file source.
71 * dashdemux exposes the streams it recreates based on the fragments it
72 * fetches through dedicated src pads corresponding to the caps of the
73 * fragments container (ISOBMFF/MP4 or MPEG2TS).
75 * During playback, new representations will typically be exposed as a
76 * new set of pads (see 'Switching between representations' below).
78 * Fragments downloading is performed using a dedicated task that fills
79 * an internal queue. Another task is in charge of popping fragments
80 * from the queue and pushing them downstream.
82 * Switching between representations:
84 * Decodebin supports scenarios allowing to seamlessly switch from one
85 * stream to another inside the same "decoding chain".
87 * To achieve that, it combines the elements it autoplugged in chains
88 * and groups, allowing only one decoding group to be active at a given
89 * time for a given chain.
91 * A chain can signal decodebin that it is complete by sending a
92 * no-more-pads event, but even after that new pads can be added to
93 * create new subgroups, providing that a new no-more-pads event is sent.
95 * We take advantage of that to dynamically create a new decoding group
96 * in order to select a different representation during playback.
98 * Typically, assuming that each fragment contains both audio and video,
99 * the following tree would be created:
102 * |_ group "Representation set 1"
103 * | |_ chain "Qt Demux 0"
104 * | |_ group "Stream 0"
107 * |_ group "Representation set 2"
108 * |_ chain "Qt Demux 1"
109 * |_ group "Stream 1"
113 * Or, if audio and video are contained in separate fragments:
116 * |_ group "Representation set 1"
117 * | |_ chain "Qt Demux 0"
118 * | | |_ group "Stream 0"
119 * | | |_ chain "H264"
120 * | |_ chain "Qt Demux 1"
121 * | |_ group "Stream 1"
123 * |_ group "Representation set 2"
124 * |_ chain "Qt Demux 3"
125 * | |_ group "Stream 2"
127 * |_ chain "Qt Demux 4"
128 * |_ group "Stream 3"
131 * In both cases, when switching from Set 1 to Set 2 an EOS is sent on
132 * each end pad corresponding to Rep 0, triggering the "drain" state to
133 * propagate upstream.
134 * Once both EOS have been processed, the "Set 1" group is completely
135 * drained, and decodebin2 will switch to the "Set 2" group.
137 * Note: nothing can be pushed to the new decoding group before the
138 * old one has been drained, which means that in order to be able to
139 * adapt quickly to bandwidth changes, we will not be able to rely
140 * on downstream buffering, and will instead manage an internal queue.
151 #include <inttypes.h>
153 #include <gst/base/gsttypefindhelper.h>
154 #include <gst/tag/tag.h>
155 #include <gst/net/gstnet.h>
156 #include "gst/gst-i18n-plugin.h"
157 #include "gstdashdemux.h"
158 #include "gstdash_debug.h"
160 static GstStaticPadTemplate gst_dash_demux_videosrc_template =
161 GST_STATIC_PAD_TEMPLATE ("video_%02u",
164 GST_STATIC_CAPS_ANY);
166 static GstStaticPadTemplate gst_dash_demux_audiosrc_template =
167 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
170 GST_STATIC_CAPS_ANY);
172 static GstStaticPadTemplate gst_dash_demux_subtitlesrc_template =
173 GST_STATIC_PAD_TEMPLATE ("subtitle_%02u",
176 GST_STATIC_CAPS_ANY);
178 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
181 GST_STATIC_CAPS ("application/dash+xml"));
183 GST_DEBUG_CATEGORY (gst_dash_demux_debug);
184 #define GST_CAT_DEFAULT gst_dash_demux_debug
190 PROP_MAX_BUFFERING_TIME,
191 PROP_BANDWIDTH_USAGE,
193 PROP_MAX_VIDEO_WIDTH,
194 PROP_MAX_VIDEO_HEIGHT,
195 PROP_MAX_VIDEO_FRAMERATE,
196 PROP_PRESENTATION_DELAY,
200 /* Default values for properties */
201 #define DEFAULT_MAX_BUFFERING_TIME 30 /* in seconds */
202 #define DEFAULT_BANDWIDTH_USAGE 0.8f /* 0 to 1 */
203 #define DEFAULT_MAX_BITRATE 0 /* in bit/s */
204 #define DEFAULT_MAX_VIDEO_WIDTH 0
205 #define DEFAULT_MAX_VIDEO_HEIGHT 0
206 #define DEFAULT_MAX_VIDEO_FRAMERATE_N 0
207 #define DEFAULT_MAX_VIDEO_FRAMERATE_D 1
208 #define DEFAULT_PRESENTATION_DELAY "10s" /* 10s */
210 /* Clock drift compensation for live streams */
211 #define SLOW_CLOCK_UPDATE_INTERVAL (1000000 * 30 * 60) /* 30 minutes */
212 #define FAST_CLOCK_UPDATE_INTERVAL (1000000 * 30) /* 30 seconds */
213 #define SUPPORTED_CLOCK_FORMATS (GST_MPD_UTCTIMING_TYPE_NTP | GST_MPD_UTCTIMING_TYPE_HTTP_HEAD | GST_MPD_UTCTIMING_TYPE_HTTP_XSDATE | GST_MPD_UTCTIMING_TYPE_HTTP_ISO | GST_MPD_UTCTIMING_TYPE_HTTP_NTP)
214 #define NTP_TO_UNIX_EPOCH G_GUINT64_CONSTANT(2208988800) /* difference (in seconds) between NTP epoch and Unix epoch */
216 struct _GstDashDemuxClockDrift
218 GMutex clock_lock; /* used to protect access to struct */
221 /* @clock_compensation: amount (in usecs) to add to client's idea of
222 now to map it to the server's idea of now */
223 GTimeSpan clock_compensation;
229 guint64 start_offset, end_offset;
230 /* TODO: Timestamp and duration */
231 } GstDashStreamSyncSample;
234 static void gst_dash_demux_set_property (GObject * object, guint prop_id,
235 const GValue * value, GParamSpec * pspec);
236 static void gst_dash_demux_get_property (GObject * object, guint prop_id,
237 GValue * value, GParamSpec * pspec);
238 static void gst_dash_demux_dispose (GObject * obj);
240 /* GstAdaptiveDemux */
241 static GstClockTime gst_dash_demux_get_duration (GstAdaptiveDemux * ademux);
242 static gboolean gst_dash_demux_is_live (GstAdaptiveDemux * ademux);
243 static void gst_dash_demux_reset (GstAdaptiveDemux * ademux);
244 static gboolean gst_dash_demux_process_manifest (GstAdaptiveDemux * ademux,
246 static gboolean gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
248 gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream);
249 static GstFlowReturn gst_dash_demux_stream_seek (GstAdaptiveDemuxStream *
250 stream, gboolean forward, GstSeekFlags flags, GstClockTime ts,
251 GstClockTime * final_ts);
252 static gboolean gst_dash_demux_stream_has_next_fragment (GstAdaptiveDemuxStream
255 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream);
257 gst_dash_demux_stream_advance_subfragment (GstAdaptiveDemuxStream * stream);
258 static gboolean gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream *
259 stream, guint64 bitrate);
260 static gint64 gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux *
262 static GstFlowReturn gst_dash_demux_update_manifest_data (GstAdaptiveDemux *
263 demux, GstBuffer * buf);
265 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
267 static void gst_dash_demux_advance_period (GstAdaptiveDemux * demux);
268 static gboolean gst_dash_demux_has_next_period (GstAdaptiveDemux * demux);
269 static GstFlowReturn gst_dash_demux_data_received (GstAdaptiveDemux * demux,
270 GstAdaptiveDemuxStream * stream, GstBuffer * buffer);
272 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
273 GstAdaptiveDemuxStream * stream);
275 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
276 GstAdaptiveDemuxStream * stream);
277 static gboolean gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream *
281 static gboolean gst_dash_demux_setup_all_streams (GstDashDemux * demux);
282 static void gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream);
284 static GstCaps *gst_dash_demux_get_input_caps (GstDashDemux * demux,
285 GstActiveStream * stream);
286 static GstPad *gst_dash_demux_create_pad (GstDashDemux * demux,
287 GstActiveStream * stream);
288 static GstDashDemuxClockDrift *gst_dash_demux_clock_drift_new (GstDashDemux *
290 static void gst_dash_demux_clock_drift_free (GstDashDemuxClockDrift *);
291 static gboolean gst_dash_demux_poll_clock_drift (GstDashDemux * demux);
292 static GTimeSpan gst_dash_demux_get_clock_compensation (GstDashDemux * demux);
293 static GDateTime *gst_dash_demux_get_server_now_utc (GstDashDemux * demux);
295 #define SIDX(s) (&(s)->sidx_parser.sidx)
297 static inline GstSidxBoxEntry *
298 SIDX_ENTRY (GstDashDemuxStream * s, gint i)
300 g_assert (i < SIDX (s)->entries_count);
301 return &(SIDX (s)->entries[(i)]);
304 #define SIDX_CURRENT_ENTRY(s) SIDX_ENTRY(s, SIDX(s)->entry_index)
306 static void gst_dash_demux_send_content_protection_event (gpointer cp_data,
309 #define gst_dash_demux_parent_class parent_class
310 G_DEFINE_TYPE_WITH_CODE (GstDashDemux, gst_dash_demux, GST_TYPE_ADAPTIVE_DEMUX,
311 GST_DEBUG_CATEGORY_INIT (gst_dash_demux_debug, "dashdemux", 0,
316 gst_dash_demux_dispose (GObject * obj)
318 GstDashDemux *demux = GST_DASH_DEMUX (obj);
320 gst_dash_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
323 gst_mpd_client_free (demux->client);
324 demux->client = NULL;
327 g_mutex_clear (&demux->client_lock);
329 gst_dash_demux_clock_drift_free (demux->clock_drift);
330 demux->clock_drift = NULL;
331 g_free (demux->default_presentation_delay);
332 G_OBJECT_CLASS (parent_class)->dispose (obj);
336 gst_dash_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
339 GstDashDemux *self = GST_DASH_DEMUX (demux);
342 GTimeSpan stream_now;
343 GstClockTime seg_duration;
345 if (self->client->mpd_node->availabilityStartTime == NULL)
348 seg_duration = gst_mpd_client_get_maximum_segment_duration (self->client);
349 now = gst_dash_demux_get_server_now_utc (self);
351 gst_date_time_to_g_date_time (self->client->
352 mpd_node->availabilityStartTime);
353 stream_now = g_date_time_difference (now, mstart);
354 g_date_time_unref (now);
355 g_date_time_unref (mstart);
360 *stop = stream_now * GST_USECOND;
361 if (self->client->mpd_node->timeShiftBufferDepth == GST_MPD_DURATION_NONE) {
365 *stop - (self->client->mpd_node->timeShiftBufferDepth * GST_MSECOND);
370 /* As defined in 5.3.9.5.3 of the DASH specification, a segment does
371 not become available until the sum of:
372 * the value of the MPD@availabilityStartTime,
373 * the PeriodStart time of the containing Period
374 * the MPD start time of the Media Segment, and
375 * the MPD duration of the Media Segment.
376 Therefore we need to subtract the media segment duration from the stop
379 *stop -= seg_duration;
384 gst_dash_demux_get_presentation_offset (GstAdaptiveDemux * demux,
385 GstAdaptiveDemuxStream * stream)
387 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
388 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
390 return gst_mpd_parser_get_stream_presentation_offset (dashdemux->client,
395 gst_dash_demux_get_period_start_time (GstAdaptiveDemux * demux)
397 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
399 return gst_mpd_parser_get_period_start_time (dashdemux->client);
403 gst_dash_demux_class_init (GstDashDemuxClass * klass)
405 GObjectClass *gobject_class;
406 GstElementClass *gstelement_class;
407 GstAdaptiveDemuxClass *gstadaptivedemux_class;
409 gobject_class = (GObjectClass *) klass;
410 gstelement_class = (GstElementClass *) klass;
411 gstadaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
413 gobject_class->set_property = gst_dash_demux_set_property;
414 gobject_class->get_property = gst_dash_demux_get_property;
415 gobject_class->dispose = gst_dash_demux_dispose;
417 #ifndef GST_REMOVE_DEPRECATED
418 g_object_class_install_property (gobject_class, PROP_MAX_BUFFERING_TIME,
419 g_param_spec_uint ("max-buffering-time", "Maximum buffering time",
420 "Maximum number of seconds of buffer accumulated during playback"
422 2, G_MAXUINT, DEFAULT_MAX_BUFFERING_TIME,
423 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
425 g_object_class_install_property (gobject_class, PROP_BANDWIDTH_USAGE,
426 g_param_spec_float ("bandwidth-usage",
427 "Bandwidth usage [0..1]",
428 "Percentage of the available bandwidth to use when "
429 "selecting representations (deprecated)",
430 0, 1, DEFAULT_BANDWIDTH_USAGE,
431 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
434 g_object_class_install_property (gobject_class, PROP_MAX_BITRATE,
435 g_param_spec_uint ("max-bitrate", "Max bitrate",
436 "Max of bitrate supported by target video decoder (0 = no maximum)",
437 0, G_MAXUINT, DEFAULT_MAX_BITRATE,
438 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
440 g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_WIDTH,
441 g_param_spec_uint ("max-video-width", "Max video width",
442 "Max video width to select (0 = no maximum)",
443 0, G_MAXUINT, DEFAULT_MAX_VIDEO_WIDTH,
444 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
446 g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_HEIGHT,
447 g_param_spec_uint ("max-video-height", "Max video height",
448 "Max video height to select (0 = no maximum)",
449 0, G_MAXUINT, DEFAULT_MAX_VIDEO_HEIGHT,
450 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
452 g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_FRAMERATE,
453 gst_param_spec_fraction ("max-video-framerate", "Max video framerate",
454 "Max video framerate to select (0/1 = no maximum)",
455 0, 1, G_MAXINT, 1, DEFAULT_MAX_VIDEO_FRAMERATE_N,
456 DEFAULT_MAX_VIDEO_FRAMERATE_D,
457 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
459 g_object_class_install_property (gobject_class, PROP_PRESENTATION_DELAY,
460 g_param_spec_string ("presentation-delay", "Presentation delay",
461 "Default presentation delay (in seconds, milliseconds or fragments) (e.g. 12s, 2500ms, 3f)",
462 DEFAULT_PRESENTATION_DELAY,
463 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
465 gst_element_class_add_static_pad_template (gstelement_class,
466 &gst_dash_demux_audiosrc_template);
467 gst_element_class_add_static_pad_template (gstelement_class,
468 &gst_dash_demux_videosrc_template);
469 gst_element_class_add_static_pad_template (gstelement_class,
470 &gst_dash_demux_subtitlesrc_template);
472 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
474 gst_element_class_set_static_metadata (gstelement_class,
476 "Codec/Demuxer/Adaptive",
477 "Dynamic Adaptive Streaming over HTTP demuxer",
478 "David Corvoysier <david.corvoysier@orange.com>\n\
479 Hamid Zakari <hamid.zakari@gmail.com>\n\
480 Gianluca Gennari <gennarone@gmail.com>");
483 gstadaptivedemux_class->get_duration = gst_dash_demux_get_duration;
484 gstadaptivedemux_class->is_live = gst_dash_demux_is_live;
485 gstadaptivedemux_class->reset = gst_dash_demux_reset;
486 gstadaptivedemux_class->seek = gst_dash_demux_seek;
488 gstadaptivedemux_class->process_manifest = gst_dash_demux_process_manifest;
489 gstadaptivedemux_class->update_manifest_data =
490 gst_dash_demux_update_manifest_data;
491 gstadaptivedemux_class->get_manifest_update_interval =
492 gst_dash_demux_get_manifest_update_interval;
494 gstadaptivedemux_class->has_next_period = gst_dash_demux_has_next_period;
495 gstadaptivedemux_class->advance_period = gst_dash_demux_advance_period;
496 gstadaptivedemux_class->stream_has_next_fragment =
497 gst_dash_demux_stream_has_next_fragment;
498 gstadaptivedemux_class->stream_advance_fragment =
499 gst_dash_demux_stream_advance_fragment;
500 gstadaptivedemux_class->stream_get_fragment_waiting_time =
501 gst_dash_demux_stream_get_fragment_waiting_time;
502 gstadaptivedemux_class->stream_seek = gst_dash_demux_stream_seek;
503 gstadaptivedemux_class->stream_select_bitrate =
504 gst_dash_demux_stream_select_bitrate;
505 gstadaptivedemux_class->stream_update_fragment_info =
506 gst_dash_demux_stream_update_fragment_info;
507 gstadaptivedemux_class->stream_free = gst_dash_demux_stream_free;
508 gstadaptivedemux_class->get_live_seek_range =
509 gst_dash_demux_get_live_seek_range;
510 gstadaptivedemux_class->get_presentation_offset =
511 gst_dash_demux_get_presentation_offset;
512 gstadaptivedemux_class->get_period_start_time =
513 gst_dash_demux_get_period_start_time;
515 gstadaptivedemux_class->start_fragment = gst_dash_demux_stream_fragment_start;
516 gstadaptivedemux_class->finish_fragment =
517 gst_dash_demux_stream_fragment_finished;
518 gstadaptivedemux_class->data_received = gst_dash_demux_data_received;
519 gstadaptivedemux_class->need_another_chunk =
520 gst_dash_demux_need_another_chunk;
524 gst_dash_demux_init (GstDashDemux * demux)
527 demux->max_buffering_time = DEFAULT_MAX_BUFFERING_TIME * GST_SECOND;
528 demux->max_bitrate = DEFAULT_MAX_BITRATE;
529 demux->max_video_width = DEFAULT_MAX_VIDEO_WIDTH;
530 demux->max_video_height = DEFAULT_MAX_VIDEO_HEIGHT;
531 demux->max_video_framerate_n = DEFAULT_MAX_VIDEO_FRAMERATE_N;
532 demux->max_video_framerate_d = DEFAULT_MAX_VIDEO_FRAMERATE_D;
533 demux->default_presentation_delay = g_strdup (DEFAULT_PRESENTATION_DELAY);
535 g_mutex_init (&demux->client_lock);
537 gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
538 sizeof (GstDashDemuxStream));
542 gst_dash_demux_set_property (GObject * object, guint prop_id,
543 const GValue * value, GParamSpec * pspec)
545 GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
546 GstDashDemux *demux = GST_DASH_DEMUX (object);
549 case PROP_MAX_BUFFERING_TIME:
550 demux->max_buffering_time = g_value_get_uint (value) * GST_SECOND;
552 case PROP_BANDWIDTH_USAGE:
553 adaptivedemux->bitrate_limit = g_value_get_float (value);
555 case PROP_MAX_BITRATE:
556 demux->max_bitrate = g_value_get_uint (value);
558 case PROP_MAX_VIDEO_WIDTH:
559 demux->max_video_width = g_value_get_uint (value);
561 case PROP_MAX_VIDEO_HEIGHT:
562 demux->max_video_height = g_value_get_uint (value);
564 case PROP_MAX_VIDEO_FRAMERATE:
565 demux->max_video_framerate_n = gst_value_get_fraction_numerator (value);
566 demux->max_video_framerate_d = gst_value_get_fraction_denominator (value);
568 case PROP_PRESENTATION_DELAY:
569 g_free (demux->default_presentation_delay);
570 demux->default_presentation_delay = g_value_dup_string (value);
573 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
579 gst_dash_demux_get_property (GObject * object, guint prop_id, GValue * value,
582 GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
583 GstDashDemux *demux = GST_DASH_DEMUX (object);
586 case PROP_MAX_BUFFERING_TIME:
587 g_value_set_uint (value, demux->max_buffering_time / GST_SECOND);
589 case PROP_BANDWIDTH_USAGE:
590 g_value_set_float (value, adaptivedemux->bitrate_limit);
592 case PROP_MAX_BITRATE:
593 g_value_set_uint (value, demux->max_bitrate);
595 case PROP_MAX_VIDEO_WIDTH:
596 g_value_set_uint (value, demux->max_video_width);
598 case PROP_MAX_VIDEO_HEIGHT:
599 g_value_set_uint (value, demux->max_video_height);
601 case PROP_MAX_VIDEO_FRAMERATE:
602 gst_value_set_fraction (value, demux->max_video_framerate_n,
603 demux->max_video_framerate_d);
605 case PROP_PRESENTATION_DELAY:
606 if (demux->default_presentation_delay == NULL)
607 g_value_set_static_string (value, "");
609 g_value_set_string (value, demux->default_presentation_delay);
612 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
618 gst_dash_demux_setup_mpdparser_streams (GstDashDemux * demux,
619 GstMpdClient * client)
621 gboolean has_streams = FALSE;
622 GList *adapt_sets, *iter;
624 adapt_sets = gst_mpd_client_get_adaptation_sets (client);
625 for (iter = adapt_sets; iter; iter = g_list_next (iter)) {
626 GstAdaptationSetNode *adapt_set_node = iter->data;
628 gst_mpd_client_setup_streaming (client, adapt_set_node);
633 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, ("Manifest has no playable "
634 "streams"), ("No streams could be activated from the manifest"));
640 gst_dash_demux_setup_all_streams (GstDashDemux * demux)
644 GST_DEBUG_OBJECT (demux, "Setting up streams for period %d",
645 gst_mpd_client_get_period_index (demux->client));
647 /* clean old active stream list, if any */
648 gst_active_streams_free (demux->client);
650 if (!gst_dash_demux_setup_mpdparser_streams (demux, demux->client)) {
654 GST_DEBUG_OBJECT (demux, "Creating stream objects");
655 for (i = 0; i < gst_mpdparser_get_nb_active_stream (demux->client); i++) {
656 GstDashDemuxStream *stream;
657 GstActiveStream *active_stream;
662 GstTagList *tags = NULL;
664 active_stream = gst_mpdparser_get_active_stream_by_index (demux->client, i);
665 if (active_stream == NULL)
668 if (demux->trickmode_no_audio
669 && active_stream->mimeType == GST_STREAM_AUDIO) {
670 GST_DEBUG_OBJECT (demux,
671 "Skipping audio stream %d because of TRICKMODE_NO_AUDIO flag", i);
675 srcpad = gst_dash_demux_create_pad (demux, active_stream);
679 caps = gst_dash_demux_get_input_caps (demux, active_stream);
680 GST_LOG_OBJECT (demux, "Creating stream %d %" GST_PTR_FORMAT, i, caps);
681 #ifdef TIZEN_FEATURE_ADAPTIVE_MODIFICATION
682 caps = gst_caps_make_writable (caps);
683 gst_caps_set_simple (caps, "adaptive_stream", G_TYPE_BOOLEAN, TRUE,
684 "dash_stream", G_TYPE_BOOLEAN, TRUE, NULL);
685 GST_LOG_OBJECT (demux,
686 "*** modified caps for dash streaming = %" GST_PTR_FORMAT, caps);
689 if (active_stream->cur_adapt_set) {
690 GstAdaptationSetNode *adp_set = active_stream->cur_adapt_set;
691 lang = adp_set->lang;
693 /* Fallback to the language in ContentComponent node */
697 for (it = adp_set->ContentComponents; it; it = it->next) {
698 GstContentComponentNode *cc_node = it->data;
700 lang = cc_node->lang;
706 #ifdef TIZEN_FEATURE_ADAPTIVE_MODIFICATION
707 if (active_stream->mimeType == GST_STREAM_VIDEO
708 && adp_set->VariantInfo != NULL) {
709 GST_LOG_OBJECT (demux,
710 "post msg about video active stream variant info");
712 gst_element_post_message (GST_ELEMENT_CAST (demux),
713 gst_message_new_element (GST_OBJECT_CAST (demux),
714 gst_structure_new (GST_ADAPTIVE_DEMUX_VARIANT_MESSAGE_NAME,
715 "video-variant-info", G_TYPE_POINTER, adp_set->VariantInfo,
722 if (gst_tag_check_language_code (lang))
723 tags = gst_tag_list_new (GST_TAG_LANGUAGE_CODE, lang, NULL);
725 tags = gst_tag_list_new (GST_TAG_LANGUAGE_NAME, lang, NULL);
728 stream = (GstDashDemuxStream *)
729 gst_adaptive_demux_stream_new (GST_ADAPTIVE_DEMUX_CAST (demux), srcpad);
730 stream->active_stream = active_stream;
731 s = gst_caps_get_structure (caps, 0);
733 gst_mpd_client_has_isoff_ondemand_profile (demux->client);
734 stream->is_isobmff = gst_structure_has_name (s, "video/quicktime")
735 || gst_structure_has_name (s, "audio/x-m4a");
736 stream->first_sync_sample_always_after_moof = TRUE;
737 if (stream->is_isobmff
738 || gst_mpd_client_has_isoff_ondemand_profile (demux->client))
739 stream->adapter = gst_adapter_new ();
740 gst_adaptive_demux_stream_set_caps (GST_ADAPTIVE_DEMUX_STREAM_CAST (stream),
743 gst_adaptive_demux_stream_set_tags (GST_ADAPTIVE_DEMUX_STREAM_CAST
746 stream->pending_seek_ts = GST_CLOCK_TIME_NONE;
747 stream->sidx_position = GST_CLOCK_TIME_NONE;
748 if (active_stream->cur_adapt_set &&
749 active_stream->cur_adapt_set->RepresentationBase &&
750 active_stream->cur_adapt_set->RepresentationBase->ContentProtection) {
751 GST_DEBUG_OBJECT (demux, "Adding ContentProtection events to source pad");
752 g_list_foreach (active_stream->cur_adapt_set->RepresentationBase->
753 ContentProtection, gst_dash_demux_send_content_protection_event,
757 gst_isoff_sidx_parser_init (&stream->sidx_parser);
764 gst_dash_demux_send_content_protection_event (gpointer data, gpointer userdata)
766 GstDescriptorType *cp = (GstDescriptorType *) data;
767 GstDashDemuxStream *stream = (GstDashDemuxStream *) userdata;
773 if (cp->schemeIdUri == NULL)
776 GST_TRACE_OBJECT (stream, "check schemeIdUri %s", cp->schemeIdUri);
777 /* RFC 2141 states: The leading "urn:" sequence is case-insensitive */
778 schemeIdUri = g_ascii_strdown (cp->schemeIdUri, -1);
779 if (g_str_has_prefix (schemeIdUri, "urn:uuid:")) {
780 pssi_len = strlen (cp->value);
781 pssi = gst_buffer_new_wrapped (g_memdup (cp->value, pssi_len), pssi_len);
782 GST_LOG_OBJECT (stream, "Queuing Protection event on source pad");
783 /* RFC 4122 states that the hex part of a UUID is in lower case,
784 * but some streams seem to ignore this and use upper case for the
785 * protection system ID */
786 event = gst_event_new_protection (cp->schemeIdUri + 9, pssi, "dash/mpd");
787 gst_adaptive_demux_stream_queue_event ((GstAdaptiveDemuxStream *) stream,
789 gst_buffer_unref (pssi);
791 g_free (schemeIdUri);
795 gst_dash_demux_get_duration (GstAdaptiveDemux * ademux)
797 GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
799 g_return_val_if_fail (demux->client != NULL, GST_CLOCK_TIME_NONE);
801 return gst_mpd_client_get_media_presentation_duration (demux->client);
805 gst_dash_demux_is_live (GstAdaptiveDemux * ademux)
807 GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
809 g_return_val_if_fail (demux->client != NULL, FALSE);
811 return gst_mpd_client_is_live (demux->client);
815 gst_dash_demux_setup_streams (GstAdaptiveDemux * demux)
817 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
819 GstDateTime *now = NULL;
822 /* setup video, audio and subtitle streams, starting from first Period if
825 if (gst_mpd_client_is_live (dashdemux->client)) {
827 if (dashdemux->client->mpd_node->availabilityStartTime == NULL) {
829 GST_ERROR_OBJECT (demux, "MPD does not have availabilityStartTime");
832 if (dashdemux->clock_drift == NULL) {
835 gst_mpd_client_get_utc_timing_sources (dashdemux->client,
836 SUPPORTED_CLOCK_FORMATS, NULL);
838 GST_DEBUG_OBJECT (dashdemux, "Found a supported UTCTiming element");
839 dashdemux->clock_drift = gst_dash_demux_clock_drift_new (dashdemux);
840 gst_dash_demux_poll_clock_drift (dashdemux);
843 /* get period index for period encompassing the current time */
844 g_now = gst_dash_demux_get_server_now_utc (dashdemux);
845 now = gst_date_time_new_from_g_date_time (g_now);
846 if (dashdemux->client->mpd_node->suggestedPresentationDelay != -1) {
847 GstDateTime *target = gst_mpd_client_add_time_difference (now,
848 dashdemux->client->mpd_node->suggestedPresentationDelay * -1000);
849 gst_date_time_unref (now);
851 } else if (dashdemux->default_presentation_delay) {
853 gst_mpd_client_parse_default_presentation_delay (dashdemux->client,
854 dashdemux->default_presentation_delay);
855 GstDateTime *target = gst_mpd_client_add_time_difference (now,
857 gst_date_time_unref (now);
861 gst_mpd_client_get_period_index_at_time (dashdemux->client, now);
862 if (period_idx == G_MAXUINT) {
863 #ifndef GST_DISABLE_GST_DEBUG
864 gchar *date_str = gst_date_time_to_iso8601_string (now);
865 GST_DEBUG_OBJECT (demux, "Unable to find live period active at %s",
874 if (!gst_mpd_client_set_period_index (dashdemux->client, period_idx) ||
875 !gst_dash_demux_setup_all_streams (dashdemux)) {
880 /* If stream is live, try to find the segment that
881 * is closest to current time */
882 if (gst_mpd_client_is_live (dashdemux->client)) {
885 GST_DEBUG_OBJECT (demux, "Seeking to current time of day for live stream ");
887 gnow = gst_date_time_to_g_date_time (now);
888 gst_mpd_client_seek_to_time (dashdemux->client, gnow);
889 g_date_time_unref (gnow);
891 GST_DEBUG_OBJECT (demux, "Seeking to first segment for on-demand stream ");
893 /* start playing from the first segment */
894 gst_mpd_client_seek_to_first_segment (dashdemux->client);
899 gst_date_time_unref (now);
904 gst_dash_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
906 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
907 gboolean ret = FALSE;
911 if (dashdemux->client)
912 gst_mpd_client_free (dashdemux->client);
913 dashdemux->client = gst_mpd_client_new ();
914 gst_mpd_client_set_uri_downloader (dashdemux->client, demux->downloader);
916 dashdemux->client->mpd_uri = g_strdup (demux->manifest_uri);
917 dashdemux->client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
919 GST_DEBUG_OBJECT (demux, "Fetched MPD file at URI: %s (base: %s)",
920 dashdemux->client->mpd_uri,
921 GST_STR_NULL (dashdemux->client->mpd_base_uri));
923 if (gst_buffer_map (buf, &mapinfo, GST_MAP_READ)) {
924 manifest = (gchar *) mapinfo.data;
925 if (gst_mpd_parse (dashdemux->client, manifest, mapinfo.size)) {
926 if (gst_mpd_client_setup_media_presentation (dashdemux->client, 0, 0,
930 GST_ELEMENT_ERROR (demux, STREAM, DECODE,
931 ("Incompatible manifest file."), (NULL));
934 gst_buffer_unmap (buf, &mapinfo);
936 GST_WARNING_OBJECT (demux, "Failed to map manifest buffer");
940 ret = gst_dash_demux_setup_streams (demux);
946 gst_dash_demux_create_pad (GstDashDemux * demux, GstActiveStream * stream)
949 GstPadTemplate *tmpl;
952 switch (stream->mimeType) {
953 case GST_STREAM_AUDIO:
954 name = g_strdup_printf ("audio_%02u", demux->n_audio_streams++);
955 tmpl = gst_static_pad_template_get (&gst_dash_demux_audiosrc_template);
957 case GST_STREAM_VIDEO:
958 name = g_strdup_printf ("video_%02u", demux->n_video_streams++);
959 tmpl = gst_static_pad_template_get (&gst_dash_demux_videosrc_template);
961 case GST_STREAM_APPLICATION:
962 if (gst_mpd_client_active_stream_contains_subtitles (stream)) {
963 name = g_strdup_printf ("subtitle_%02u", demux->n_subtitle_streams++);
965 gst_static_pad_template_get (&gst_dash_demux_subtitlesrc_template);
971 g_assert_not_reached ();
975 /* Create and activate new pads */
976 pad = gst_pad_new_from_template (tmpl, name);
978 gst_object_unref (tmpl);
980 gst_pad_set_active (pad, TRUE);
981 GST_INFO_OBJECT (demux, "Creating srcpad %s:%s", GST_DEBUG_PAD_NAME (pad));
986 gst_dash_demux_reset (GstAdaptiveDemux * ademux)
988 GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
990 GST_DEBUG_OBJECT (demux, "Resetting demux");
992 demux->end_of_period = FALSE;
993 demux->end_of_manifest = FALSE;
996 gst_mpd_client_free (demux->client);
997 demux->client = NULL;
999 gst_dash_demux_clock_drift_free (demux->clock_drift);
1000 demux->clock_drift = NULL;
1001 demux->client = gst_mpd_client_new ();
1002 gst_mpd_client_set_uri_downloader (demux->client, ademux->downloader);
1004 demux->n_audio_streams = 0;
1005 demux->n_video_streams = 0;
1006 demux->n_subtitle_streams = 0;
1008 demux->trickmode_no_audio = FALSE;
1009 demux->allow_trickmode_key_units = TRUE;
1013 gst_dash_demux_get_video_input_caps (GstDashDemux * demux,
1014 GstActiveStream * stream)
1016 guint width = 0, height = 0;
1017 gint fps_num = 0, fps_den = 1;
1018 gboolean have_fps = FALSE;
1019 GstCaps *caps = NULL;
1024 /* if bitstreamSwitching is true we dont need to swich pads on resolution change */
1025 if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
1026 width = gst_mpd_client_get_video_stream_width (stream);
1027 height = gst_mpd_client_get_video_stream_height (stream);
1029 gst_mpd_client_get_video_stream_framerate (stream, &fps_num, &fps_den);
1031 caps = gst_mpd_client_get_stream_caps (stream);
1035 if (width > 0 && height > 0) {
1036 gst_caps_set_simple (caps, "width", G_TYPE_INT, width, "height",
1037 G_TYPE_INT, height, NULL);
1041 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fps_num,
1049 gst_dash_demux_get_audio_input_caps (GstDashDemux * demux,
1050 GstActiveStream * stream)
1052 guint rate = 0, channels = 0;
1053 GstCaps *caps = NULL;
1058 /* if bitstreamSwitching is true we dont need to swich pads on rate/channels change */
1059 if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
1060 channels = gst_mpd_client_get_audio_stream_num_channels (stream);
1061 rate = gst_mpd_client_get_audio_stream_rate (stream);
1063 caps = gst_mpd_client_get_stream_caps (stream);
1068 gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
1071 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
1078 gst_dash_demux_get_application_input_caps (GstDashDemux * demux,
1079 GstActiveStream * stream)
1081 GstCaps *caps = NULL;
1086 caps = gst_mpd_client_get_stream_caps (stream);
1094 gst_dash_demux_get_input_caps (GstDashDemux * demux, GstActiveStream * stream)
1096 switch (stream->mimeType) {
1097 case GST_STREAM_VIDEO:
1098 return gst_dash_demux_get_video_input_caps (demux, stream);
1099 case GST_STREAM_AUDIO:
1100 return gst_dash_demux_get_audio_input_caps (demux, stream);
1101 case GST_STREAM_APPLICATION:
1102 return gst_dash_demux_get_application_input_caps (demux, stream);
1104 return GST_CAPS_NONE;
1109 gst_dash_demux_stream_update_headers_info (GstAdaptiveDemuxStream * stream)
1111 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1112 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1115 gst_mpd_client_get_next_header (dashdemux->client,
1116 &path, dashstream->index,
1117 &stream->fragment.header_range_start, &stream->fragment.header_range_end);
1120 stream->fragment.header_uri =
1121 gst_uri_join_strings (gst_mpdparser_get_baseURL (dashdemux->client,
1122 dashstream->index), path);
1127 gst_mpd_client_get_next_header_index (dashdemux->client,
1128 &path, dashstream->index,
1129 &stream->fragment.index_range_start, &stream->fragment.index_range_end);
1132 stream->fragment.index_uri =
1133 gst_uri_join_strings (gst_mpdparser_get_baseURL (dashdemux->client,
1134 dashstream->index), path);
1139 static GstFlowReturn
1140 gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream)
1142 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1143 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1145 GstMediaFragmentInfo fragment;
1148 gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
1150 isombff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1152 /* Reset chunk size if any */
1153 stream->fragment.chunk_size = 0;
1155 if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream) && isombff) {
1156 gst_dash_demux_stream_update_headers_info (stream);
1157 /* sidx entries may not be available in here */
1158 if (stream->fragment.index_uri
1159 && dashstream->sidx_position != GST_CLOCK_TIME_NONE) {
1160 /* request only the index to be downloaded as we need to reposition the
1161 * stream to a subsegment */
1166 if (dashstream->moof_sync_samples
1167 && GST_ADAPTIVE_DEMUX (dashdemux)->
1168 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1169 GstDashStreamSyncSample *sync_sample =
1170 &g_array_index (dashstream->moof_sync_samples, GstDashStreamSyncSample,
1171 dashstream->current_sync_sample);
1173 gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1176 stream->fragment.uri = fragment.uri;
1177 stream->fragment.timestamp = GST_CLOCK_TIME_NONE;
1178 stream->fragment.duration = GST_CLOCK_TIME_NONE;
1179 stream->fragment.range_start = sync_sample->start_offset;
1180 stream->fragment.range_end = sync_sample->end_offset;
1185 if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
1186 dashstream->index, &ts)) {
1187 if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream)) {
1188 gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
1189 gst_dash_demux_stream_update_headers_info (stream);
1192 gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1195 stream->fragment.uri = fragment.uri;
1196 /* If mpd does not specify indexRange (i.e., null index_uri),
1197 * sidx entries may not be available until download it */
1198 if (isombff && dashstream->sidx_position != GST_CLOCK_TIME_NONE
1199 && SIDX (dashstream)->entries) {
1200 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
1201 stream->fragment.range_start =
1202 dashstream->sidx_base_offset + entry->offset;
1203 stream->fragment.timestamp = entry->pts;
1204 stream->fragment.duration = entry->duration;
1205 if (stream->demux->segment.rate < 0.0) {
1206 stream->fragment.range_end =
1207 stream->fragment.range_start + entry->size - 1;
1209 stream->fragment.range_end = fragment.range_end;
1212 stream->fragment.timestamp = fragment.timestamp;
1213 stream->fragment.duration = fragment.duration;
1214 stream->fragment.range_start =
1215 MAX (fragment.range_start, dashstream->sidx_base_offset);
1216 stream->fragment.range_end = fragment.range_end;
1222 return GST_FLOW_EOS;
1226 gst_dash_demux_index_entry_search (GstSidxBoxEntry * entry, GstClockTime * ts,
1229 GstClockTime entry_ts = entry->pts + entry->duration;
1230 if (entry_ts <= *ts)
1232 else if (entry->pts > *ts)
1238 static GstFlowReturn
1239 gst_dash_demux_stream_sidx_seek (GstDashDemuxStream * dashstream,
1240 gboolean forward, GstSeekFlags flags, GstClockTime ts,
1241 GstClockTime * final_ts)
1243 GstSidxBox *sidx = SIDX (dashstream);
1244 GstSidxBoxEntry *entry;
1245 gint idx = sidx->entries_count;
1246 GstFlowReturn ret = GST_FLOW_OK;
1248 if (sidx->entries_count == 0)
1249 return GST_FLOW_EOS;
1252 gst_util_array_binary_search (sidx->entries, sidx->entries_count,
1253 sizeof (GstSidxBoxEntry),
1254 (GCompareDataFunc) gst_dash_demux_index_entry_search,
1255 GST_SEARCH_MODE_EXACT, &ts, NULL);
1257 /* No exact match found, nothing in our index
1258 * This is usually a bug or broken stream, as the seeking code already
1259 * makes sure that we're in the correct period and segment, and only need
1260 * to find the correct place inside the segment. Allow for some rounding
1261 * errors and inaccuracies here though */
1263 GstSidxBoxEntry *last_entry = &sidx->entries[sidx->entries_count - 1];
1265 GST_WARNING_OBJECT (dashstream->parent.pad, "Couldn't find SIDX entry");
1267 if (ts < sidx->entries[0].pts
1268 && ts + 250 * GST_MSECOND >= sidx->entries[0].pts)
1269 entry = &sidx->entries[0];
1270 else if (ts >= last_entry->pts + last_entry->duration &&
1271 ts < last_entry->pts + last_entry->duration + 250 * GST_MSECOND)
1275 return GST_FLOW_EOS;
1277 idx = entry - sidx->entries;
1279 /* FIXME in reverse mode, if we are exactly at a fragment start it makes more
1280 * sense to start from the end of the previous fragment */
1281 if (!forward && idx > 0 && entry->pts == ts) {
1283 entry = &sidx->entries[idx];
1286 /* Now entry->pts <= ts < entry->pts + entry->duration, need to adjust for
1288 if ((flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST) {
1289 if (idx + 1 < sidx->entries_count
1290 && sidx->entries[idx + 1].pts - ts < ts - sidx->entries[idx].pts)
1292 } else if ((forward && (flags & GST_SEEK_FLAG_SNAP_AFTER)) || (!forward
1293 && (flags & GST_SEEK_FLAG_SNAP_BEFORE))) {
1294 if (idx + 1 < sidx->entries_count && entry->pts < ts)
1298 g_assert (sidx->entry_index < sidx->entries_count);
1300 sidx->entry_index = idx;
1301 dashstream->sidx_position = sidx->entries[idx].pts;
1304 *final_ts = dashstream->sidx_position;
1309 static GstFlowReturn
1310 gst_dash_demux_stream_seek (GstAdaptiveDemuxStream * stream, gboolean forward,
1311 GstSeekFlags flags, GstClockTime ts, GstClockTime * final_ts)
1313 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1314 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1315 gint last_index, last_repeat;
1316 gboolean is_isobmff;
1318 last_index = dashstream->active_stream->segment_index;
1319 last_repeat = dashstream->active_stream->segment_repeat_index;
1321 if (dashstream->adapter)
1322 gst_adapter_clear (dashstream->adapter);
1323 dashstream->current_offset = -1;
1324 dashstream->current_index_header_or_data = 0;
1326 dashstream->isobmff_parser.current_fourcc = 0;
1327 dashstream->isobmff_parser.current_start_offset = 0;
1328 dashstream->isobmff_parser.current_size = 0;
1330 if (dashstream->moof)
1331 gst_isoff_moof_box_free (dashstream->moof);
1332 dashstream->moof = NULL;
1333 if (dashstream->moof_sync_samples)
1334 g_array_free (dashstream->moof_sync_samples, TRUE);
1335 dashstream->moof_sync_samples = NULL;
1336 dashstream->current_sync_sample = -1;
1338 is_isobmff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1340 if (!gst_mpd_client_stream_seek (dashdemux->client, dashstream->active_stream,
1342 is_isobmff ? (flags & (~(GST_SEEK_FLAG_SNAP_BEFORE |
1343 GST_SEEK_FLAG_SNAP_AFTER))) : flags, ts, final_ts)) {
1344 return GST_FLOW_EOS;
1348 GstClockTime period_start, offset;
1350 period_start = gst_mpd_parser_get_period_start_time (dashdemux->client);
1352 gst_mpd_parser_get_stream_presentation_offset (dashdemux->client,
1355 if (G_UNLIKELY (ts < period_start))
1358 ts += offset - period_start;
1360 if (last_index != dashstream->active_stream->segment_index ||
1361 last_repeat != dashstream->active_stream->segment_repeat_index) {
1362 GST_LOG_OBJECT (stream->pad,
1363 "Segment index was changed, reset sidx parser");
1364 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1365 dashstream->sidx_base_offset = 0;
1366 dashstream->allow_sidx = TRUE;
1369 if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1370 if (gst_dash_demux_stream_sidx_seek (dashstream, forward, flags, ts,
1371 final_ts) != GST_FLOW_OK) {
1372 GST_ERROR_OBJECT (stream->pad, "Couldn't find position in sidx");
1373 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1374 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1376 dashstream->pending_seek_ts = GST_CLOCK_TIME_NONE;
1378 /* no index yet, seek when we have it */
1379 /* FIXME - the final_ts won't be correct here */
1380 dashstream->pending_seek_ts = ts;
1388 gst_dash_demux_stream_has_next_sync_sample (GstAdaptiveDemuxStream * stream)
1390 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1392 if (dashstream->moof_sync_samples
1393 && GST_ADAPTIVE_DEMUX (stream->demux)->
1394 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1395 if (stream->demux->segment.rate > 0.0) {
1396 if (dashstream->current_sync_sample + 1 <
1397 dashstream->moof_sync_samples->len)
1400 if (dashstream->current_sync_sample >= 1)
1408 gst_dash_demux_stream_has_next_subfragment (GstAdaptiveDemuxStream * stream)
1410 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1411 GstSidxBox *sidx = SIDX (dashstream);
1413 if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1414 if (stream->demux->segment.rate > 0.0) {
1415 if (sidx->entry_index + 1 < sidx->entries_count)
1418 if (sidx->entry_index >= 1)
1426 gst_dash_demux_stream_advance_sync_sample (GstAdaptiveDemuxStream * stream)
1428 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1429 gboolean fragment_finished = FALSE;
1431 if (dashstream->moof_sync_samples
1432 && GST_ADAPTIVE_DEMUX (stream->demux)->
1433 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1434 if (stream->demux->segment.rate > 0.0) {
1435 dashstream->current_sync_sample++;
1436 if (dashstream->current_sync_sample >= dashstream->moof_sync_samples->len) {
1437 fragment_finished = TRUE;
1440 if (dashstream->current_sync_sample == -1) {
1441 dashstream->current_sync_sample =
1442 dashstream->moof_sync_samples->len - 1;
1443 } else if (dashstream->current_sync_sample == 0) {
1444 dashstream->current_sync_sample = -1;
1445 fragment_finished = TRUE;
1447 dashstream->current_sync_sample--;
1452 GST_DEBUG_OBJECT (stream->pad,
1453 "Advancing sync sample #%d fragment_finished:%d",
1454 dashstream->current_sync_sample, fragment_finished);
1456 if (!fragment_finished)
1457 stream->discont = TRUE;
1459 return !fragment_finished;
1463 gst_dash_demux_stream_advance_subfragment (GstAdaptiveDemuxStream * stream)
1465 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1467 GstSidxBox *sidx = SIDX (dashstream);
1468 gboolean fragment_finished = TRUE;
1470 if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1471 if (stream->demux->segment.rate > 0.0) {
1472 gint idx = ++sidx->entry_index;
1473 if (idx < sidx->entries_count) {
1474 fragment_finished = FALSE;
1477 if (idx == sidx->entries_count)
1478 dashstream->sidx_position =
1479 sidx->entries[idx - 1].pts + sidx->entries[idx - 1].duration;
1481 dashstream->sidx_position = sidx->entries[idx].pts;
1483 gint idx = --sidx->entry_index;
1486 fragment_finished = FALSE;
1487 dashstream->sidx_position = sidx->entries[idx].pts;
1489 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1494 GST_DEBUG_OBJECT (stream->pad, "New sidx index: %d / %d. "
1495 "Finished fragment: %d", sidx->entry_index, sidx->entries_count,
1498 return !fragment_finished;
1502 gst_dash_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
1504 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1505 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1507 if (dashstream->moof_sync_samples
1508 && GST_ADAPTIVE_DEMUX (dashdemux)->
1509 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1510 if (gst_dash_demux_stream_has_next_sync_sample (stream))
1514 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)) {
1515 if (gst_dash_demux_stream_has_next_subfragment (stream))
1519 return gst_mpd_client_has_next_segment (dashdemux->client,
1520 dashstream->active_stream, stream->demux->segment.rate > 0.0);
1523 static GstFlowReturn
1524 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream)
1526 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1527 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1529 GST_DEBUG_OBJECT (stream->pad, "Advance fragment");
1531 /* If downloading only keyframes, switch to the next one or fall through */
1532 if (dashstream->moof_sync_samples
1533 && GST_ADAPTIVE_DEMUX (dashdemux)->
1534 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1535 if (gst_dash_demux_stream_advance_sync_sample (stream))
1539 dashstream->isobmff_parser.current_fourcc = 0;
1540 dashstream->isobmff_parser.current_start_offset = 0;
1541 dashstream->isobmff_parser.current_size = 0;
1543 if (dashstream->moof)
1544 gst_isoff_moof_box_free (dashstream->moof);
1545 dashstream->moof = NULL;
1546 if (dashstream->moof_sync_samples)
1547 g_array_free (dashstream->moof_sync_samples, TRUE);
1548 dashstream->moof_sync_samples = NULL;
1549 dashstream->current_sync_sample = -1;
1551 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)) {
1552 if (gst_dash_demux_stream_advance_subfragment (stream))
1556 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1557 gst_isoff_sidx_parser_init (&dashstream->sidx_parser);
1558 dashstream->sidx_base_offset = 0;
1559 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1560 dashstream->allow_sidx = TRUE;
1561 if (dashstream->adapter)
1562 gst_adapter_clear (dashstream->adapter);
1564 return gst_mpd_client_advance_segment (dashdemux->client,
1565 dashstream->active_stream, stream->demux->segment.rate > 0.0);
1569 gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream * stream,
1572 GstActiveStream *active_stream = NULL;
1573 GList *rep_list = NULL;
1575 GstAdaptiveDemux *base_demux = stream->demux;
1576 GstDashDemux *demux = GST_DASH_DEMUX_CAST (stream->demux);
1577 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1578 gboolean ret = FALSE;
1580 active_stream = dashstream->active_stream;
1581 if (active_stream == NULL) {
1585 /* retrieve representation list */
1586 if (active_stream->cur_adapt_set)
1587 rep_list = active_stream->cur_adapt_set->Representations;
1592 GST_DEBUG_OBJECT (stream->pad,
1593 "Trying to change to bitrate: %" G_GUINT64_FORMAT, bitrate);
1595 if (active_stream->mimeType == GST_STREAM_VIDEO && demux->max_bitrate) {
1596 bitrate = MIN (demux->max_bitrate, bitrate);
1599 #ifdef TIZEN_FEATURE_ADAPTIVE_MODIFICATION
1600 bitrate = (base_demux->max_bandwidth > DEFAULT_ADAPTIVE_VARIANT)?
1601 (guint64)(base_demux->max_bandwidth):(bitrate);
1602 GST_DEBUG_OBJECT (stream->pad,
1603 "Trying to change to bitrate under : %" G_GUINT64_FORMAT, bitrate);
1605 /* get representation index with current max_bandwidth */
1606 if ((base_demux->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) ||
1607 ABS (base_demux->segment.rate) <= 1.0) {
1609 gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list, (gint)bitrate,
1610 demux->max_video_width, demux->max_video_height);
1613 gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list,
1614 (gint)(bitrate / ABS (base_demux->segment.rate)), demux->max_video_width,
1615 demux->max_video_height);
1618 /* get representation index with current max_bandwidth */
1619 if ((base_demux->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) ||
1620 ABS (base_demux->segment.rate) <= 1.0) {
1622 gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list, bitrate,
1623 demux->max_video_width, demux->max_video_height,
1624 demux->max_video_framerate_n, demux->max_video_framerate_d);
1627 gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list,
1628 bitrate / ABS (base_demux->segment.rate), demux->max_video_width,
1629 demux->max_video_height, demux->max_video_framerate_n,
1630 demux->max_video_framerate_d);
1634 /* if no representation has the required bandwidth, take the lowest one */
1635 if (new_index == -1)
1636 new_index = gst_mpdparser_get_rep_idx_with_min_bandwidth (rep_list);
1638 if (new_index != active_stream->representation_idx) {
1639 GstRepresentationNode *rep = g_list_nth_data (rep_list, new_index);
1640 GST_INFO_OBJECT (demux, "Changing representation idx: %d %d %u",
1641 dashstream->index, new_index, rep->bandwidth);
1642 if (gst_mpd_client_setup_representation (demux->client, active_stream, rep)) {
1645 GST_INFO_OBJECT (demux, "Switching bitrate to %d",
1646 active_stream->cur_representation->bandwidth);
1647 caps = gst_dash_demux_get_input_caps (demux, active_stream);
1648 gst_adaptive_demux_stream_set_caps (stream, caps);
1652 GST_WARNING_OBJECT (demux, "Can not switch representation, aborting...");
1657 if (gst_mpd_client_has_isoff_ondemand_profile (demux->client)
1658 && SIDX (dashstream)->entries) {
1659 /* store our current position to change to the same one in a different
1660 * representation if needed */
1661 if (SIDX (dashstream)->entry_index < SIDX (dashstream)->entries_count)
1662 dashstream->sidx_position = SIDX_CURRENT_ENTRY (dashstream)->pts;
1663 else if (SIDX (dashstream)->entry_index >=
1664 SIDX (dashstream)->entries_count)
1665 dashstream->sidx_position =
1666 SIDX_ENTRY (dashstream,
1667 SIDX (dashstream)->entries_count - 1)->pts + SIDX_ENTRY (dashstream,
1668 SIDX (dashstream)->entries_count - 1)->duration;
1670 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1672 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1675 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1676 dashstream->sidx_base_offset = 0;
1677 dashstream->allow_sidx = TRUE;
1679 /* Reset ISOBMFF box parsing state */
1680 dashstream->isobmff_parser.current_fourcc = 0;
1681 dashstream->isobmff_parser.current_start_offset = 0;
1682 dashstream->isobmff_parser.current_size = 0;
1684 dashstream->current_offset = -1;
1685 dashstream->current_index_header_or_data = 0;
1687 if (dashstream->adapter)
1688 gst_adapter_clear (dashstream->adapter);
1690 if (dashstream->moof)
1691 gst_isoff_moof_box_free (dashstream->moof);
1692 dashstream->moof = NULL;
1693 if (dashstream->moof_sync_samples)
1694 g_array_free (dashstream->moof_sync_samples, TRUE);
1695 dashstream->moof_sync_samples = NULL;
1696 dashstream->current_sync_sample = -1;
1703 #define SEEK_UPDATES_PLAY_POSITION(r, start_type, stop_type) \
1704 ((r >= 0 && start_type != GST_SEEK_TYPE_NONE) || \
1705 (r < 0 && stop_type != GST_SEEK_TYPE_NONE))
1708 gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
1713 GstSeekType start_type, stop_type;
1716 GstClockTime current_pos, target_pos;
1717 guint current_period;
1718 GstStreamPeriod *period;
1719 GList *iter, *streams = NULL;
1720 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1721 gboolean trickmode_no_audio;
1723 gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
1726 if (!SEEK_UPDATES_PLAY_POSITION (rate, start_type, stop_type)) {
1727 /* nothing to do if we don't have to update the current position */
1731 if (demux->segment.rate > 0.0) {
1732 target_pos = (GstClockTime) start;
1734 target_pos = (GstClockTime) stop;
1737 /* select the requested Period in the Media Presentation */
1738 if (!gst_mpd_client_setup_media_presentation (dashdemux->client, target_pos,
1743 for (list = g_list_first (dashdemux->client->periods); list;
1744 list = g_list_next (list)) {
1745 period = list->data;
1746 current_pos = period->start;
1747 current_period = period->number;
1748 GST_DEBUG_OBJECT (demux, "Looking at period %u) start:%"
1749 GST_TIME_FORMAT " - duration:%"
1750 GST_TIME_FORMAT ") for position %" GST_TIME_FORMAT,
1751 current_period, GST_TIME_ARGS (current_pos),
1752 GST_TIME_ARGS (period->duration), GST_TIME_ARGS (target_pos));
1753 if (current_pos <= target_pos
1754 && target_pos <= current_pos + period->duration) {
1759 GST_WARNING_OBJECT (demux, "Could not find seeked Period");
1763 trickmode_no_audio = ! !(flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO);
1765 streams = demux->streams;
1766 if (current_period != gst_mpd_client_get_period_index (dashdemux->client)) {
1767 GST_DEBUG_OBJECT (demux, "Seeking to Period %d", current_period);
1769 /* clean old active stream list, if any */
1770 gst_active_streams_free (dashdemux->client);
1771 dashdemux->trickmode_no_audio = trickmode_no_audio;
1773 /* setup video, audio and subtitle streams, starting from the new Period */
1774 if (!gst_mpd_client_set_period_index (dashdemux->client, current_period)
1775 || !gst_dash_demux_setup_all_streams (dashdemux))
1777 streams = demux->next_streams;
1778 } else if (dashdemux->trickmode_no_audio != trickmode_no_audio) {
1779 /* clean old active stream list, if any */
1780 gst_active_streams_free (dashdemux->client);
1781 dashdemux->trickmode_no_audio = trickmode_no_audio;
1783 /* setup video, audio and subtitle streams, starting from the new Period */
1784 if (!gst_dash_demux_setup_all_streams (dashdemux))
1786 streams = demux->next_streams;
1789 /* Update the current sequence on all streams */
1790 for (iter = streams; iter; iter = g_list_next (iter)) {
1791 GstAdaptiveDemuxStream *stream = iter->data;
1793 if (gst_dash_demux_stream_seek (stream, rate >= 0, 0, target_pos,
1794 NULL) != GST_FLOW_OK)
1802 gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
1804 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1805 return MIN (dashdemux->client->mpd_node->minimumUpdatePeriod * 1000,
1806 SLOW_CLOCK_UPDATE_INTERVAL);
1809 static GstFlowReturn
1810 gst_dash_demux_update_manifest_data (GstAdaptiveDemux * demux,
1813 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1814 GstMpdClient *new_client = NULL;
1817 GST_DEBUG_OBJECT (demux, "Updating manifest file from URL");
1819 /* parse the manifest file */
1820 new_client = gst_mpd_client_new ();
1821 gst_mpd_client_set_uri_downloader (new_client, demux->downloader);
1822 new_client->mpd_uri = g_strdup (demux->manifest_uri);
1823 new_client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
1824 gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
1826 if (gst_mpd_parse (new_client, (gchar *) mapinfo.data, mapinfo.size)) {
1827 const gchar *period_id;
1830 GList *streams_iter;
1833 /* prepare the new manifest and try to transfer the stream position
1834 * status from the old manifest client */
1836 GST_DEBUG_OBJECT (demux, "Updating manifest");
1838 period_id = gst_mpd_client_get_period_id (dashdemux->client);
1839 period_idx = gst_mpd_client_get_period_index (dashdemux->client);
1841 /* setup video, audio and subtitle streams, starting from current Period */
1842 if (!gst_mpd_client_setup_media_presentation (new_client, -1,
1843 (period_id ? -1 : period_idx), period_id)) {
1848 if (!gst_mpd_client_set_period_id (new_client, period_id)) {
1849 GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
1850 gst_mpd_client_free (new_client);
1851 gst_buffer_unmap (buffer, &mapinfo);
1852 return GST_FLOW_EOS;
1855 if (!gst_mpd_client_set_period_index (new_client, period_idx)) {
1856 GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
1857 gst_mpd_client_free (new_client);
1858 gst_buffer_unmap (buffer, &mapinfo);
1859 return GST_FLOW_EOS;
1863 if (!gst_dash_demux_setup_mpdparser_streams (dashdemux, new_client)) {
1864 GST_ERROR_OBJECT (demux, "Failed to setup streams on manifest " "update");
1865 gst_mpd_client_free (new_client);
1866 gst_buffer_unmap (buffer, &mapinfo);
1867 return GST_FLOW_ERROR;
1870 /* If no pads have been exposed yet, need to use those */
1872 if (demux->streams == NULL) {
1873 if (demux->prepared_streams) {
1874 streams = demux->prepared_streams;
1877 streams = demux->streams;
1880 /* update the streams to play from the next segment */
1881 for (iter = streams, streams_iter = new_client->active_streams;
1882 iter && streams_iter;
1883 iter = g_list_next (iter), streams_iter = g_list_next (streams_iter)) {
1884 GstDashDemuxStream *demux_stream = iter->data;
1885 GstActiveStream *new_stream = streams_iter->data;
1889 GST_DEBUG_OBJECT (demux,
1890 "Stream of index %d is missing from manifest update",
1891 demux_stream->index);
1892 gst_mpd_client_free (new_client);
1893 gst_buffer_unmap (buffer, &mapinfo);
1894 return GST_FLOW_EOS;
1897 if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
1898 demux_stream->index, &ts)
1899 || gst_mpd_client_get_last_fragment_timestamp_end (dashdemux->client,
1900 demux_stream->index, &ts)) {
1902 /* Due to rounding when doing the timescale conversions it might happen
1903 * that the ts falls back to a previous segment, leading the same data
1904 * to be downloaded twice. We try to work around this by always adding
1905 * 10 microseconds to get back to the correct segment. The errors are
1906 * usually on the order of nanoseconds so it should be enough.
1908 GST_DEBUG_OBJECT (GST_ADAPTIVE_DEMUX_STREAM_PAD (demux_stream),
1909 "Current position: %" GST_TIME_FORMAT ", updating to %"
1910 GST_TIME_FORMAT, GST_TIME_ARGS (ts),
1911 GST_TIME_ARGS (ts + (10 * GST_USECOND)));
1912 ts += 10 * GST_USECOND;
1913 gst_mpd_client_stream_seek (new_client, new_stream,
1914 demux->segment.rate >= 0, 0, ts, NULL);
1917 demux_stream->active_stream = new_stream;
1920 gst_mpd_client_free (dashdemux->client);
1921 dashdemux->client = new_client;
1923 GST_DEBUG_OBJECT (demux, "Manifest file successfully updated");
1924 if (dashdemux->clock_drift) {
1925 gst_dash_demux_poll_clock_drift (dashdemux);
1928 /* In most cases, this will happen if we set a wrong url in the
1929 * source element and we have received the 404 HTML response instead of
1931 GST_WARNING_OBJECT (demux, "Error parsing the manifest.");
1932 gst_mpd_client_free (new_client);
1933 gst_buffer_unmap (buffer, &mapinfo);
1934 return GST_FLOW_ERROR;
1937 gst_buffer_unmap (buffer, &mapinfo);
1943 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
1946 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1947 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1948 GstDateTime *segmentAvailability;
1949 GstActiveStream *active_stream = dashstream->active_stream;
1951 segmentAvailability =
1952 gst_mpd_client_get_next_segment_availability_start_time
1953 (dashdemux->client, active_stream);
1955 if (segmentAvailability) {
1957 GstDateTime *cur_time;
1960 gst_date_time_new_from_g_date_time
1961 (gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST
1964 gst_mpd_client_calculate_time_difference (cur_time,
1965 segmentAvailability);
1966 gst_date_time_unref (segmentAvailability);
1967 gst_date_time_unref (cur_time);
1968 /* subtract the server's clock drift, so that if the server's
1969 time is behind our idea of UTC, we need to sleep for longer
1970 before requesting a fragment */
1972 gst_dash_demux_get_clock_compensation (dashdemux) * GST_USECOND;
1978 gst_dash_demux_has_next_period (GstAdaptiveDemux * demux)
1980 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1982 if (demux->segment.rate >= 0)
1983 return gst_mpd_client_has_next_period (dashdemux->client);
1985 return gst_mpd_client_has_previous_period (dashdemux->client);
1989 gst_dash_demux_advance_period (GstAdaptiveDemux * demux)
1991 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1993 if (demux->segment.rate >= 0) {
1994 if (!gst_mpd_client_set_period_index (dashdemux->client,
1995 gst_mpd_client_get_period_index (dashdemux->client) + 1)) {
2000 if (!gst_mpd_client_set_period_index (dashdemux->client,
2001 gst_mpd_client_get_period_index (dashdemux->client) - 1)) {
2007 gst_dash_demux_setup_all_streams (dashdemux);
2008 gst_mpd_client_seek_to_first_segment (dashdemux->client);
2012 _gst_buffer_split (GstBuffer * buffer, gint offset, gsize size)
2014 GstBuffer *newbuf = gst_buffer_copy_region (buffer,
2015 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_META
2016 | GST_BUFFER_COPY_MEMORY, offset, size == -1 ? size : size - offset);
2018 gst_buffer_resize (buffer, 0, offset);
2024 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
2025 GstAdaptiveDemuxStream * stream)
2027 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2028 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2030 dashstream->current_index_header_or_data = 0;
2031 dashstream->current_offset = -1;
2033 /* We need to mark every first buffer of a key unit as discont,
2034 * and also every first buffer of a moov and moof. This ensures
2035 * that qtdemux takes note of our buffer offsets for each of those
2036 * buffers instead of keeping track of them itself from the first
2037 * buffer. We need offsets to be consistent between moof and mdat
2039 if (dashstream->is_isobmff && dashdemux->allow_trickmode_key_units
2040 && (demux->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS)
2041 && dashstream->active_stream->mimeType == GST_STREAM_VIDEO)
2042 stream->discont = TRUE;
2047 static GstFlowReturn
2048 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
2049 GstAdaptiveDemuxStream * stream)
2051 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2052 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2054 /* We need to mark every first buffer of a key unit as discont,
2055 * and also every first buffer of a moov and moof. This ensures
2056 * that qtdemux takes note of our buffer offsets for each of those
2057 * buffers instead of keeping track of them itself from the first
2058 * buffer. We need offsets to be consistent between moof and mdat
2060 if (dashstream->is_isobmff && dashdemux->allow_trickmode_key_units
2061 && (demux->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS)
2062 && dashstream->active_stream->mimeType == GST_STREAM_VIDEO)
2063 stream->discont = TRUE;
2065 /* Only handle fragment advancing specifically for SIDX if we're not
2066 * in key unit mode */
2067 if (!(dashstream->moof_sync_samples
2068 && GST_ADAPTIVE_DEMUX (dashdemux)->
2069 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS)
2070 && gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)
2071 && dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
2072 /* fragment is advanced on data_received when byte limits are reached */
2073 if (dashstream->pending_seek_ts != GST_CLOCK_TIME_NONE) {
2074 if (SIDX (dashstream)->entry_index < SIDX (dashstream)->entries_count)
2076 } else if (gst_dash_demux_stream_has_next_subfragment (stream)) {
2081 if (G_UNLIKELY (stream->downloading_header || stream->downloading_index))
2084 return gst_adaptive_demux_stream_advance_fragment (demux, stream,
2085 stream->fragment.duration);
2089 gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream * stream)
2091 GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
2092 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2094 /* We're chunked downloading for ISOBMFF in KEY_UNITS mode for the actual
2095 * fragment until we parsed the moof and arrived at the mdat. 8192 is a
2096 * random guess for the moof size
2098 if (dashstream->is_isobmff
2099 && (GST_ADAPTIVE_DEMUX (stream->demux)->
2100 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS)
2101 && dashstream->active_stream->mimeType == GST_STREAM_VIDEO
2102 && !stream->downloading_header && !stream->downloading_index
2103 && dashdemux->allow_trickmode_key_units) {
2104 if (dashstream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT) {
2105 /* Need to download the moof first to know anything */
2107 stream->fragment.chunk_size = 8192;
2108 /* Do we have the first fourcc already or are we in the middle */
2109 if (dashstream->isobmff_parser.current_fourcc == 0) {
2110 stream->fragment.chunk_size += dashstream->moof_average_size;
2111 if (dashstream->first_sync_sample_always_after_moof)
2112 stream->fragment.chunk_size +=
2113 dashstream->first_sync_sample_average_size;
2116 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2117 dashstream->sidx_parser.sidx.entries) {
2118 guint64 sidx_end_offset =
2119 dashstream->sidx_base_offset +
2120 SIDX_CURRENT_ENTRY (dashstream)->offset +
2121 SIDX_CURRENT_ENTRY (dashstream)->size;
2122 guint64 downloaded_end_offset =
2123 dashstream->current_offset +
2124 gst_adapter_available (dashstream->adapter);
2126 if (stream->fragment.chunk_size +
2127 downloaded_end_offset > sidx_end_offset) {
2128 stream->fragment.chunk_size = sidx_end_offset - downloaded_end_offset;
2131 } else if (dashstream->moof && dashstream->moof_sync_samples) {
2132 /* Have the moof, either we're done now or we want to download the
2133 * directly following sync sample */
2134 if (dashstream->first_sync_sample_after_moof
2135 && dashstream->current_sync_sample == 0) {
2136 GstDashStreamSyncSample *sync_sample =
2137 &g_array_index (dashstream->moof_sync_samples,
2138 GstDashStreamSyncSample, 0);
2139 guint64 end_offset = sync_sample->end_offset + 1;
2140 guint64 downloaded_end_offset =
2141 dashstream->current_offset +
2142 gst_adapter_available (dashstream->adapter);
2144 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2145 dashstream->sidx_parser.sidx.entries) {
2146 guint64 sidx_end_offset =
2147 dashstream->sidx_base_offset +
2148 SIDX_CURRENT_ENTRY (dashstream)->offset +
2149 SIDX_CURRENT_ENTRY (dashstream)->size;
2151 if (end_offset > sidx_end_offset) {
2152 end_offset = sidx_end_offset;
2156 if (downloaded_end_offset < end_offset) {
2157 stream->fragment.chunk_size = end_offset - downloaded_end_offset;
2159 stream->fragment.chunk_size = 0;
2162 stream->fragment.chunk_size = 0;
2165 /* Have moof but can't do key-units mode, just download until the end */
2166 stream->fragment.chunk_size = -1;
2169 /* We might've decided that we can't allow key-unit only
2170 * trickmodes while doing chunked downloading. In that case
2171 * just download from here to the end now */
2172 if (dashstream->moof
2173 && (GST_ADAPTIVE_DEMUX (stream->demux)->
2174 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS))
2175 stream->fragment.chunk_size = -1;
2177 stream->fragment.chunk_size = 0;
2180 return stream->fragment.chunk_size != 0;
2183 static GstFlowReturn
2184 gst_dash_demux_parse_isobmff (GstAdaptiveDemux * demux,
2185 GstDashDemuxStream * dash_stream, gboolean * sidx_seek_needed)
2187 GstAdaptiveDemuxStream *stream = (GstAdaptiveDemuxStream *) dash_stream;
2188 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2192 GstByteReader reader;
2195 guint64 size, buffer_offset;
2197 *sidx_seek_needed = FALSE;
2199 /* This must not be called when we're in the mdat. We only look at the mdat
2200 * header and then stop parsing the boxes as we're only interested in the
2201 * metadata! Handling mdat is the job of the surrounding code, as well as
2202 * stopping or starting the next fragment when mdat is over (=> sidx)
2204 g_assert (dash_stream->isobmff_parser.current_fourcc !=
2205 GST_ISOFF_FOURCC_MDAT);
2207 available = gst_adapter_available (dash_stream->adapter);
2208 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2209 buffer_offset = dash_stream->current_offset;
2211 /* Always at the start of a box here */
2212 g_assert (dash_stream->isobmff_parser.current_size == 0);
2214 /* At the start of a box => Parse it */
2215 gst_buffer_map (buffer, &map, GST_MAP_READ);
2216 gst_byte_reader_init (&reader, map.data, map.size);
2218 /* While there are more boxes left to parse ... */
2219 dash_stream->isobmff_parser.current_start_offset = buffer_offset;
2221 dash_stream->isobmff_parser.current_fourcc = 0;
2222 dash_stream->isobmff_parser.current_size = 0;
2224 if (!gst_isoff_parse_box_header (&reader, &fourcc, NULL, &header_size,
2229 dash_stream->isobmff_parser.current_fourcc = fourcc;
2231 /* We assume this is mdat, anything else with "size until end"
2232 * does not seem to make sense */
2233 g_assert (dash_stream->isobmff_parser.current_fourcc ==
2234 GST_ISOFF_FOURCC_MDAT);
2235 dash_stream->isobmff_parser.current_size = -1;
2239 dash_stream->isobmff_parser.current_size = size;
2241 /* Do we have the complete box or are at MDAT */
2242 if (gst_byte_reader_get_remaining (&reader) < size - header_size ||
2243 dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MDAT) {
2244 /* Reset byte reader to the beginning of the box */
2245 gst_byte_reader_set_pos (&reader,
2246 gst_byte_reader_get_pos (&reader) - header_size);
2250 GST_LOG_OBJECT (stream->pad,
2251 "box %" GST_FOURCC_FORMAT " at offset %" G_GUINT64_FORMAT " size %"
2252 G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc),
2253 dash_stream->isobmff_parser.current_start_offset, size);
2255 if (dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MOOF) {
2256 GstByteReader sub_reader;
2258 /* Only allow SIDX before the very first moof */
2259 dash_stream->allow_sidx = FALSE;
2261 g_assert (dash_stream->moof == NULL);
2262 g_assert (dash_stream->moof_sync_samples == NULL);
2263 gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
2264 dash_stream->moof = gst_isoff_moof_box_parse (&sub_reader);
2265 dash_stream->moof_offset =
2266 dash_stream->isobmff_parser.current_start_offset;
2267 dash_stream->moof_size = size;
2268 dash_stream->current_sync_sample = -1;
2270 if (dash_stream->moof_average_size) {
2271 if (dash_stream->moof_average_size < size)
2272 dash_stream->moof_average_size =
2273 (size * 3 + dash_stream->moof_average_size) / 4;
2275 dash_stream->moof_average_size =
2276 (size + dash_stream->moof_average_size + 3) / 4;
2278 dash_stream->moof_average_size = size;
2280 } else if (dash_stream->isobmff_parser.current_fourcc ==
2281 GST_ISOFF_FOURCC_SIDX &&
2282 gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2283 dash_stream->allow_sidx) {
2284 GstByteReader sub_reader;
2285 GstIsoffParserResult res;
2288 dash_stream->sidx_base_offset =
2289 dash_stream->isobmff_parser.current_start_offset + size;
2290 dash_stream->allow_sidx = FALSE;
2292 gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
2295 gst_isoff_sidx_parser_parse (&dash_stream->sidx_parser, &sub_reader,
2298 if (res == GST_ISOFF_PARSER_DONE) {
2299 guint64 first_offset = dash_stream->sidx_parser.sidx.first_offset;
2300 GstSidxBox *sidx = SIDX (dash_stream);
2304 GST_LOG_OBJECT (stream->pad,
2305 "non-zero sidx first offset %" G_GUINT64_FORMAT, first_offset);
2306 dash_stream->sidx_base_offset += first_offset;
2309 for (i = 0; i < sidx->entries_count; i++) {
2310 GstSidxBoxEntry *entry = &sidx->entries[i];
2312 if (entry->ref_type != 0) {
2313 GST_FIXME_OBJECT (stream->pad, "SIDX ref_type 1 not supported yet");
2314 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
2315 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2320 /* We might've cleared the index above */
2321 if (sidx->entries_count > 0) {
2322 if (GST_CLOCK_TIME_IS_VALID (dash_stream->pending_seek_ts)) {
2323 /* FIXME, preserve seek flags */
2324 if (gst_dash_demux_stream_sidx_seek (dash_stream,
2325 demux->segment.rate >= 0, 0, dash_stream->pending_seek_ts,
2326 NULL) != GST_FLOW_OK) {
2327 GST_ERROR_OBJECT (stream->pad, "Couldn't find position in sidx");
2328 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
2329 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2331 dash_stream->pending_seek_ts = GST_CLOCK_TIME_NONE;
2334 if (dash_stream->sidx_position == GST_CLOCK_TIME_NONE) {
2335 SIDX (dash_stream)->entry_index = 0;
2337 if (gst_dash_demux_stream_sidx_seek (dash_stream,
2338 demux->segment.rate >= 0, GST_SEEK_FLAG_SNAP_BEFORE,
2339 dash_stream->sidx_position, NULL) != GST_FLOW_OK) {
2340 GST_ERROR_OBJECT (stream->pad,
2341 "Couldn't find position in sidx");
2342 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
2343 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2346 dash_stream->sidx_position =
2347 SIDX (dash_stream)->entries[SIDX (dash_stream)->
2352 if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED &&
2353 SIDX (dash_stream)->entry_index != 0) {
2354 /* Need to jump to the requested SIDX entry. Push everything up to
2355 * the SIDX box below and let the caller handle everything else */
2356 *sidx_seek_needed = TRUE;
2361 gst_byte_reader_skip (&reader, size - header_size);
2364 dash_stream->isobmff_parser.current_fourcc = 0;
2365 dash_stream->isobmff_parser.current_start_offset += size;
2366 dash_stream->isobmff_parser.current_size = 0;
2367 } while (gst_byte_reader_get_remaining (&reader) > 0);
2369 gst_buffer_unmap (buffer, &map);
2371 /* mdat? Push all we have and wait for it to be over */
2372 if (dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MDAT) {
2375 GST_LOG_OBJECT (stream->pad,
2376 "box %" GST_FOURCC_FORMAT " at offset %" G_GUINT64_FORMAT " size %"
2377 G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc),
2378 dash_stream->isobmff_parser.current_start_offset,
2379 dash_stream->isobmff_parser.current_size);
2381 /* At mdat. Move the start of the mdat to the adapter and have everything
2382 * else be pushed. We parsed all header boxes at this point and are not
2383 * supposed to be called again until the next moof */
2384 pending = _gst_buffer_split (buffer, gst_byte_reader_get_pos (&reader), -1);
2385 gst_adapter_push (dash_stream->adapter, pending);
2386 dash_stream->current_offset += gst_byte_reader_get_pos (&reader);
2387 dash_stream->isobmff_parser.current_size = 0;
2389 GST_BUFFER_OFFSET (buffer) = buffer_offset;
2390 GST_BUFFER_OFFSET_END (buffer) =
2391 buffer_offset + gst_buffer_get_size (buffer);
2392 return gst_adaptive_demux_stream_push_buffer (stream, buffer);
2393 } else if (gst_byte_reader_get_pos (&reader) != 0) {
2396 /* Multiple complete boxes and no mdat? Push them and keep the remainder,
2397 * which is the start of the next box if any remainder */
2399 pending = _gst_buffer_split (buffer, gst_byte_reader_get_pos (&reader), -1);
2400 gst_adapter_push (dash_stream->adapter, pending);
2401 dash_stream->current_offset += gst_byte_reader_get_pos (&reader);
2402 dash_stream->isobmff_parser.current_size = 0;
2404 GST_BUFFER_OFFSET (buffer) = buffer_offset;
2405 GST_BUFFER_OFFSET_END (buffer) =
2406 buffer_offset + gst_buffer_get_size (buffer);
2407 return gst_adaptive_demux_stream_push_buffer (stream, buffer);
2410 /* Not even a single complete, non-mdat box, wait */
2411 dash_stream->isobmff_parser.current_size = 0;
2412 gst_adapter_push (dash_stream->adapter, buffer);
2418 gst_dash_demux_find_sync_samples (GstAdaptiveDemux * demux,
2419 GstAdaptiveDemuxStream * stream)
2421 GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
2422 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
2424 guint32 track_id = 0;
2425 guint64 prev_traf_end;
2426 gboolean trex_sample_flags = FALSE;
2428 if (!dash_stream->moof) {
2429 dashdemux->allow_trickmode_key_units = FALSE;
2433 dash_stream->current_sync_sample = -1;
2434 dash_stream->moof_sync_samples =
2435 g_array_new (FALSE, FALSE, sizeof (GstDashStreamSyncSample));
2437 prev_traf_end = dash_stream->moof_offset;
2439 /* generate table of keyframes and offsets */
2440 for (i = 0; i < dash_stream->moof->traf->len; i++) {
2441 GstTrafBox *traf = &g_array_index (dash_stream->moof->traf, GstTrafBox, i);
2442 guint64 traf_offset = 0, prev_trun_end;
2446 track_id = traf->tfhd.track_id;
2447 } else if (track_id != traf->tfhd.track_id) {
2448 GST_ERROR_OBJECT (stream->pad,
2449 "moof with trafs of different track ids (%u != %u)", track_id,
2450 traf->tfhd.track_id);
2451 g_array_free (dash_stream->moof_sync_samples, TRUE);
2452 dash_stream->moof_sync_samples = NULL;
2453 dashdemux->allow_trickmode_key_units = FALSE;
2457 if (traf->tfhd.flags & GST_TFHD_FLAGS_BASE_DATA_OFFSET_PRESENT) {
2458 traf_offset = traf->tfhd.base_data_offset;
2459 } else if (traf->tfhd.flags & GST_TFHD_FLAGS_DEFAULT_BASE_IS_MOOF) {
2460 traf_offset = dash_stream->moof_offset;
2462 traf_offset = prev_traf_end;
2465 prev_trun_end = traf_offset;
2467 for (j = 0; j < traf->trun->len; j++) {
2468 GstTrunBox *trun = &g_array_index (traf->trun, GstTrunBox, j);
2469 guint64 trun_offset, prev_sample_end;
2472 if (trun->flags & GST_TRUN_FLAGS_DATA_OFFSET_PRESENT) {
2473 trun_offset = traf_offset + trun->data_offset;
2475 trun_offset = prev_trun_end;
2478 prev_sample_end = trun_offset;
2479 for (k = 0; k < trun->samples->len; k++) {
2480 GstTrunSample *sample =
2481 &g_array_index (trun->samples, GstTrunSample, k);
2482 guint64 sample_offset;
2483 guint32 sample_flags;
2485 guint32 sample_duration;
2488 sample_offset = prev_sample_end;
2490 if (trun->flags & GST_TRUN_FLAGS_SAMPLE_FLAGS_PRESENT) {
2491 sample_flags = sample->sample_flags;
2492 } else if ((trun->flags & GST_TRUN_FLAGS_FIRST_SAMPLE_FLAGS_PRESENT)
2494 sample_flags = trun->first_sample_flags;
2496 tfhd.flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_FLAGS_PRESENT) {
2497 sample_flags = traf->tfhd.default_sample_flags;
2499 trex_sample_flags = TRUE;
2504 if (trun->flags & GST_TRUN_FLAGS_SAMPLE_DURATION_PRESENT) {
2505 sample_duration = sample->sample_duration;
2507 tfhd.flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_DURATION_PRESENT) {
2508 sample_duration = traf->tfhd.default_sample_duration;
2510 GST_FIXME_OBJECT (stream->pad,
2511 "Sample duration given by trex - can't download only keyframes");
2512 g_array_free (dash_stream->moof_sync_samples, TRUE);
2513 dash_stream->moof_sync_samples = NULL;
2518 if (trun->flags & GST_TRUN_FLAGS_SAMPLE_SIZE_PRESENT) {
2519 prev_sample_end += sample->sample_size;
2521 tfhd.flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_SIZE_PRESENT) {
2522 prev_sample_end += traf->tfhd.default_sample_size;
2524 GST_FIXME_OBJECT (stream->pad,
2525 "Sample size given by trex - can't download only keyframes");
2526 g_array_free (dash_stream->moof_sync_samples, TRUE);
2527 dash_stream->moof_sync_samples = NULL;
2528 dashdemux->allow_trickmode_key_units = FALSE;
2532 /* Non-non-sync sample aka sync sample */
2533 if (!GST_ISOFF_SAMPLE_FLAGS_SAMPLE_IS_NON_SYNC_SAMPLE (sample_flags) ||
2534 GST_ISOFF_SAMPLE_FLAGS_SAMPLE_DEPENDS_ON (sample_flags) == 2) {
2535 GstDashStreamSyncSample sync_sample =
2536 { sample_offset, prev_sample_end - 1 };
2537 /* TODO: need timestamps so we can decide to download or not */
2538 g_array_append_val (dash_stream->moof_sync_samples, sync_sample);
2542 prev_trun_end = prev_sample_end;
2545 prev_traf_end = prev_trun_end;
2548 if (trex_sample_flags) {
2549 if (dash_stream->moof_sync_samples->len > 0) {
2550 GST_LOG_OBJECT (stream->pad,
2551 "Some sample flags given by trex but still found sync samples");
2553 GST_FIXME_OBJECT (stream->pad,
2554 "Sample flags given by trex - can't download only keyframes");
2555 g_array_free (dash_stream->moof_sync_samples, TRUE);
2556 dash_stream->moof_sync_samples = NULL;
2557 dashdemux->allow_trickmode_key_units = FALSE;
2562 if (dash_stream->moof_sync_samples->len == 0) {
2563 GST_LOG_OBJECT (stream->pad, "No sync samples found in fragment");
2564 g_array_free (dash_stream->moof_sync_samples, TRUE);
2565 dash_stream->moof_sync_samples = NULL;
2566 dashdemux->allow_trickmode_key_units = FALSE;
2571 GstDashStreamSyncSample *sync_sample =
2572 &g_array_index (dash_stream->moof_sync_samples, GstDashStreamSyncSample,
2574 guint size = sync_sample->end_offset + 1 - sync_sample->start_offset;
2576 if (dash_stream->first_sync_sample_average_size) {
2577 if (dash_stream->first_sync_sample_average_size < size)
2578 dash_stream->first_sync_sample_average_size =
2579 (size * 3 + dash_stream->first_sync_sample_average_size) / 4;
2581 dash_stream->first_sync_sample_average_size =
2582 (size + dash_stream->first_sync_sample_average_size * 3) / 4;
2584 dash_stream->first_sync_sample_average_size = size;
2587 if (dash_stream->moof_offset + dash_stream->moof_size + 8 <
2588 sync_sample->start_offset) {
2589 dash_stream->first_sync_sample_after_moof = FALSE;
2590 dash_stream->first_sync_sample_always_after_moof = FALSE;
2592 dash_stream->first_sync_sample_after_moof =
2593 (dash_stream->moof_sync_samples->len == 1
2594 || demux->segment.rate > 0.0);
2602 static GstFlowReturn
2603 gst_dash_demux_handle_isobmff (GstAdaptiveDemux * demux,
2604 GstAdaptiveDemuxStream * stream)
2606 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
2607 GstFlowReturn ret = GST_FLOW_OK;
2609 gboolean sidx_advance = FALSE;
2611 /* We parse all ISOBMFF boxes of a (sub)fragment until the mdat. This covers
2612 * at least moov, moof and sidx boxes. Once mdat is received we just output
2613 * everything until the next (sub)fragment */
2614 if (dash_stream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT) {
2615 gboolean sidx_seek_needed = FALSE;
2617 ret = gst_dash_demux_parse_isobmff (demux, dash_stream, &sidx_seek_needed);
2618 if (ret != GST_FLOW_OK)
2621 /* Go to selected segment if needed here */
2622 if (sidx_seek_needed && !stream->downloading_index)
2623 return GST_ADAPTIVE_DEMUX_FLOW_END_OF_FRAGMENT;
2625 /* No mdat yet, let's get called again with the next boxes */
2626 if (dash_stream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT)
2629 /* Here we end up only if we're right at the mdat start */
2631 /* Jump to the next sync sample. As we're doing chunked downloading
2632 * here, just drop data until our chunk is over so we can reuse the
2633 * HTTP connection instead of having to create a new one or
2634 * reuse the data if the sync sample follows the moof */
2635 if (dash_stream->active_stream->mimeType == GST_STREAM_VIDEO
2636 && gst_dash_demux_find_sync_samples (demux, stream) &&
2637 GST_ADAPTIVE_DEMUX (stream->demux)->
2638 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
2640 if (dash_stream->first_sync_sample_after_moof) {
2641 /* If we're here, don't throw away data but collect sync
2642 * sample while we're at it below. We're doing chunked
2643 * downloading so might need to adjust the next chunk size for
2645 dash_stream->current_sync_sample = 0;
2649 if (gst_adapter_available (dash_stream->adapter) == 0)
2652 /* We have some data from the mdat available in the adapter, handle it
2653 * below in the push code */
2655 /* Somewhere in the middle of the mdat */
2659 if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
2660 guint64 sidx_end_offset =
2661 dash_stream->sidx_base_offset +
2662 SIDX_CURRENT_ENTRY (dash_stream)->offset +
2663 SIDX_CURRENT_ENTRY (dash_stream)->size;
2664 gboolean has_next = gst_dash_demux_stream_has_next_subfragment (stream);
2667 /* Need to handle everything in the adapter according to the parsed SIDX
2668 * and advance subsegments accordingly */
2670 available = gst_adapter_available (dash_stream->adapter);
2671 if (dash_stream->current_offset + available < sidx_end_offset) {
2672 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2674 if (!has_next && sidx_end_offset <= dash_stream->current_offset) {
2675 /* Drain all bytes, since there might be trailing bytes at the end of subfragment */
2676 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2678 if (sidx_end_offset <= dash_stream->current_offset) {
2679 /* This means a corrupted stream or a bug: ignoring bugs, it
2680 * should only happen if the SIDX index is corrupt */
2681 GST_ERROR_OBJECT (stream->pad, "Invalid SIDX state");
2682 gst_adapter_clear (dash_stream->adapter);
2683 return GST_FLOW_ERROR;
2686 gst_adapter_take_buffer (dash_stream->adapter,
2687 sidx_end_offset - dash_stream->current_offset);
2688 sidx_advance = TRUE;
2693 /* Take it all and handle it further below */
2695 gst_adapter_take_buffer (dash_stream->adapter,
2696 gst_adapter_available (dash_stream->adapter));
2698 /* Attention: All code paths below need to update dash_stream->current_offset */
2701 /* We're actually running in key-units trick mode */
2702 if (dash_stream->active_stream->mimeType == GST_STREAM_VIDEO
2703 && dash_stream->moof_sync_samples
2704 && GST_ADAPTIVE_DEMUX (stream->demux)->
2705 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
2707 if (dash_stream->current_sync_sample == -1) {
2708 /* We're doing chunked downloading and wait for finishing the current
2709 * chunk so we can jump to the first keyframe */
2710 dash_stream->current_offset += gst_buffer_get_size (buffer);
2711 gst_buffer_unref (buffer);
2714 GstDashStreamSyncSample *sync_sample =
2715 &g_array_index (dash_stream->moof_sync_samples,
2716 GstDashStreamSyncSample, dash_stream->current_sync_sample);
2717 guint64 end_offset =
2718 dash_stream->current_offset + gst_buffer_get_size (buffer);
2720 /* Make sure to not download too much, this should only happen for
2721 * the very first keyframe if it follows the moof */
2722 if (dash_stream->current_offset >= sync_sample->end_offset + 1) {
2723 dash_stream->current_offset += gst_buffer_get_size (buffer);
2724 gst_buffer_unref (buffer);
2726 } else if (end_offset > sync_sample->end_offset + 1) {
2728 sync_sample->end_offset + 1 - dash_stream->current_offset;
2729 GstBuffer *sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0,
2731 gst_buffer_unref (buffer);
2737 GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
2738 dash_stream->current_offset += gst_buffer_get_size (buffer);
2739 GST_BUFFER_OFFSET_END (buffer) = dash_stream->current_offset;
2741 ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
2742 if (ret != GST_FLOW_OK)
2747 gst_adaptive_demux_stream_advance_fragment (demux, stream,
2748 SIDX_CURRENT_ENTRY (dash_stream)->duration);
2749 if (ret != GST_FLOW_OK)
2752 /* If we still have data available, recurse and use it up if possible */
2753 if (gst_adapter_available (dash_stream->adapter) > 0)
2754 return gst_dash_demux_handle_isobmff (demux, stream);
2760 static GstFlowReturn
2761 gst_dash_demux_data_received (GstAdaptiveDemux * demux,
2762 GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
2764 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
2765 GstFlowReturn ret = GST_FLOW_OK;
2766 guint index_header_or_data;
2768 if (stream->downloading_index)
2769 index_header_or_data = 1;
2770 else if (stream->downloading_header)
2771 index_header_or_data = 2;
2773 index_header_or_data = 3;
2775 if (dash_stream->current_index_header_or_data != index_header_or_data) {
2776 /* Clear pending data */
2777 if (gst_adapter_available (dash_stream->adapter) != 0)
2778 GST_ERROR_OBJECT (stream->pad,
2779 "Had pending SIDX data after switch between index/header/data");
2780 gst_adapter_clear (dash_stream->adapter);
2781 dash_stream->current_index_header_or_data = index_header_or_data;
2782 dash_stream->current_offset = -1;
2785 if (dash_stream->current_offset == -1)
2786 dash_stream->current_offset =
2787 GST_BUFFER_OFFSET_IS_VALID (buffer) ? GST_BUFFER_OFFSET (buffer) : 0;
2789 gst_adapter_push (dash_stream->adapter, buffer);
2792 if (dash_stream->is_isobmff || stream->downloading_index) {
2793 /* SIDX index is also ISOBMMF */
2794 ret = gst_dash_demux_handle_isobmff (demux, stream);
2795 } else if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
2798 /* Not ISOBMFF but had a SIDX index. Does this even exist or work? */
2799 while (ret == GST_FLOW_OK
2800 && ((available = gst_adapter_available (dash_stream->adapter)) > 0)) {
2801 gboolean advance = FALSE;
2802 guint64 sidx_end_offset =
2803 dash_stream->sidx_base_offset +
2804 SIDX_CURRENT_ENTRY (dash_stream)->offset +
2805 SIDX_CURRENT_ENTRY (dash_stream)->size;
2806 gboolean has_next = gst_dash_demux_stream_has_next_subfragment (stream);
2808 if (dash_stream->current_offset + available < sidx_end_offset) {
2809 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2811 if (!has_next && sidx_end_offset <= dash_stream->current_offset) {
2812 /* Drain all bytes, since there might be trailing bytes at the end of subfragment */
2813 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2815 if (sidx_end_offset <= dash_stream->current_offset) {
2816 /* This means a corrupted stream or a bug: ignoring bugs, it
2817 * should only happen if the SIDX index is corrupt */
2818 GST_ERROR_OBJECT (stream->pad, "Invalid SIDX state");
2819 gst_adapter_clear (dash_stream->adapter);
2820 ret = GST_FLOW_ERROR;
2824 gst_adapter_take_buffer (dash_stream->adapter,
2825 sidx_end_offset - dash_stream->current_offset);
2831 GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
2832 GST_BUFFER_OFFSET_END (buffer) =
2833 GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer);
2834 dash_stream->current_offset = GST_BUFFER_OFFSET_END (buffer);
2836 ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
2840 GstFlowReturn new_ret;
2842 gst_adaptive_demux_stream_advance_fragment (demux, stream,
2843 SIDX_CURRENT_ENTRY (dash_stream)->duration);
2845 /* only overwrite if it was OK before */
2846 if (ret == GST_FLOW_OK)
2854 /* this should be the main header, just push it all */
2855 buffer = gst_adapter_take_buffer (dash_stream->adapter,
2856 gst_adapter_available (dash_stream->adapter));
2858 GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
2859 GST_BUFFER_OFFSET_END (buffer) =
2860 GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer);
2861 dash_stream->current_offset = GST_BUFFER_OFFSET_END (buffer);
2863 ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
2870 gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream)
2872 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
2874 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2875 if (dash_stream->adapter)
2876 g_object_unref (dash_stream->adapter);
2877 if (dash_stream->moof)
2878 gst_isoff_moof_box_free (dash_stream->moof);
2879 if (dash_stream->moof_sync_samples)
2880 g_array_free (dash_stream->moof_sync_samples, TRUE);
2883 static GstDashDemuxClockDrift *
2884 gst_dash_demux_clock_drift_new (GstDashDemux * demux)
2886 GstDashDemuxClockDrift *clock_drift;
2888 clock_drift = g_slice_new0 (GstDashDemuxClockDrift);
2889 g_mutex_init (&clock_drift->clock_lock);
2890 clock_drift->next_update =
2891 GST_TIME_AS_USECONDS (gst_adaptive_demux_get_monotonic_time
2892 (GST_ADAPTIVE_DEMUX_CAST (demux)));
2897 gst_dash_demux_clock_drift_free (GstDashDemuxClockDrift * clock_drift)
2900 g_mutex_lock (&clock_drift->clock_lock);
2901 if (clock_drift->ntp_clock)
2902 g_object_unref (clock_drift->ntp_clock);
2903 g_mutex_unlock (&clock_drift->clock_lock);
2904 g_mutex_clear (&clock_drift->clock_lock);
2905 g_slice_free (GstDashDemuxClockDrift, clock_drift);
2910 * The value attribute of the UTCTiming element contains a white-space
2911 * separated list of servers that are recommended to be used in
2912 * combination with the NTP protocol as defined in IETF RFC 5905 for
2913 * getting the appropriate time.
2915 * The DASH standard does not specify which version of NTP. This
2916 * function only works with NTPv4 servers.
2918 static GstDateTime *
2919 gst_dash_demux_poll_ntp_server (GstDashDemuxClockDrift * clock_drift,
2922 GstClockTime ntp_clock_time;
2923 GDateTime *dt, *dt2;
2925 if (!clock_drift->ntp_clock) {
2926 GResolver *resolver;
2931 resolver = g_resolver_get_default ();
2932 /* We don't round-robin NTP servers. If the manifest specifies multiple
2933 NTP time servers, select one at random */
2934 clock_drift->selected_url = g_random_int_range (0, g_strv_length (urls));
2935 GST_DEBUG ("Connecting to NTP time server %s",
2936 urls[clock_drift->selected_url]);
2937 inet_addrs = g_resolver_lookup_by_name (resolver,
2938 urls[clock_drift->selected_url], NULL, &err);
2939 g_object_unref (resolver);
2940 if (!inet_addrs || g_list_length (inet_addrs) == 0) {
2941 GST_ERROR ("Failed to resolve hostname of NTP server: %s",
2942 err ? (err->message) : "unknown error");
2944 g_resolver_free_addresses (inet_addrs);
2950 g_inet_address_to_string ((GInetAddress
2951 *) (g_list_first (inet_addrs)->data));
2952 clock_drift->ntp_clock = gst_ntp_clock_new ("dashntp", ip_addr, 123, 0);
2954 g_resolver_free_addresses (inet_addrs);
2955 if (!clock_drift->ntp_clock) {
2956 GST_ERROR ("Failed to create NTP clock");
2959 if (!gst_clock_wait_for_sync (clock_drift->ntp_clock, 5 * GST_SECOND)) {
2960 g_object_unref (clock_drift->ntp_clock);
2961 clock_drift->ntp_clock = NULL;
2962 GST_ERROR ("Failed to lock to NTP clock");
2966 ntp_clock_time = gst_clock_get_time (clock_drift->ntp_clock);
2967 if (ntp_clock_time == GST_CLOCK_TIME_NONE) {
2968 GST_ERROR ("Failed to get time from NTP clock");
2971 ntp_clock_time -= NTP_TO_UNIX_EPOCH * GST_SECOND;
2972 dt = g_date_time_new_from_unix_utc (ntp_clock_time / GST_SECOND);
2974 GST_ERROR ("Failed to create GstDateTime");
2978 gst_util_uint64_scale (ntp_clock_time % GST_SECOND, 1000000, GST_SECOND);
2979 dt2 = g_date_time_add (dt, ntp_clock_time);
2980 g_date_time_unref (dt);
2981 return gst_date_time_new_from_g_date_time (dt2);
2984 struct Rfc5322TimeZone
2991 Parse an RFC5322 (section 3.3) date-time from the Date: field in the
2993 See https://tools.ietf.org/html/rfc5322#section-3.3
2995 static GstDateTime *
2996 gst_dash_demux_parse_http_head (GstDashDemuxClockDrift * clock_drift,
2997 GstFragment * download)
2999 static const gchar *months[] = { NULL, "Jan", "Feb", "Mar", "Apr",
3000 "May", "Jun", "Jul", "Aug",
3001 "Sep", "Oct", "Nov", "Dec", NULL
3003 static const struct Rfc5322TimeZone timezones[] = {
3018 GstDateTime *value = NULL;
3019 const GstStructure *response_headers;
3020 const gchar *http_date;
3024 gint year = -1, month = -1, day = -1, hour = -1, minute = -1, second = -1;
3027 gfloat tzoffset = 0;
3028 gboolean parsed_tz = FALSE;
3030 g_return_val_if_fail (download != NULL, NULL);
3031 g_return_val_if_fail (download->headers != NULL, NULL);
3033 val = gst_structure_get_value (download->headers, "response-headers");
3037 response_headers = gst_value_get_structure (val);
3038 http_date = gst_structure_get_string (response_headers, "Date");
3043 /* skip optional text version of day of the week */
3044 pos = strchr (http_date, ',');
3050 sscanf (pos, "%02d %3s %04d %02d:%02d:%02d %5s", &day, monthstr, &year,
3051 &hour, &minute, &second, zone);
3056 for (i = 1; months[i]; ++i) {
3057 if (g_ascii_strncasecmp (months[i], monthstr, strlen (months[i])) == 0) {
3062 for (i = 0; timezones[i].name && !parsed_tz; ++i) {
3063 if (g_ascii_strncasecmp (timezones[i].name, z,
3064 strlen (timezones[i].name)) == 0) {
3065 tzoffset = timezones[i].tzoffset;
3071 gboolean neg = FALSE;
3072 /* check if it is in the form +-HHMM */
3073 if (*z == '+' || *z == '-') {
3076 else if (*z == '-') {
3080 ret = sscanf (z, "%02d%02d", &hh, &mm);
3083 tzoffset += mm / 60.0;
3085 tzoffset = -tzoffset;
3090 /* Accept year in both 2 digit or 4 digit format */
3094 if (month > 0 && parsed_tz) {
3095 value = gst_date_time_new (tzoffset,
3096 year, month, day, hour, minute, second);
3102 The timing information is contained in the message body of the HTTP
3103 response and contains a time value formatted according to NTP timestamp
3104 format in IETF RFC 5905.
3107 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3108 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3112 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3114 NTP Timestamp Format
3116 static GstDateTime *
3117 gst_dash_demux_parse_http_ntp (GstDashDemuxClockDrift * clock_drift,
3122 GDateTime *dt, *dt2;
3125 /* See https://tools.ietf.org/html/rfc5905#page-12 for details of
3126 the NTP Timestamp Format */
3127 gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
3128 if (mapinfo.size != 8) {
3129 gst_buffer_unmap (buffer, &mapinfo);
3132 seconds = GST_READ_UINT32_BE (mapinfo.data);
3133 fraction = GST_READ_UINT32_BE (mapinfo.data + 4);
3134 gst_buffer_unmap (buffer, &mapinfo);
3135 fraction = gst_util_uint64_scale (fraction, 1000000,
3136 G_GUINT64_CONSTANT (1) << 32);
3137 /* subtract constant to convert from 1900 based time to 1970 based time */
3138 seconds -= NTP_TO_UNIX_EPOCH;
3139 dt = g_date_time_new_from_unix_utc (seconds);
3140 dt2 = g_date_time_add (dt, fraction);
3141 g_date_time_unref (dt);
3142 return gst_date_time_new_from_g_date_time (dt2);
3146 The timing information is contained in the message body of the
3147 HTTP response and contains a time value formatted according to
3148 xs:dateTime as defined in W3C XML Schema Part 2: Datatypes specification.
3150 static GstDateTime *
3151 gst_dash_demux_parse_http_xsdate (GstDashDemuxClockDrift * clock_drift,
3154 GstDateTime *value = NULL;
3157 /* the string from the server might not be zero terminated */
3158 if (gst_buffer_map (buffer, &mapinfo, GST_MAP_READ)) {
3160 str = g_strndup ((const gchar *) mapinfo.data, mapinfo.size);
3161 gst_buffer_unmap (buffer, &mapinfo);
3162 value = gst_date_time_new_from_iso8601_string (str);
3169 gst_dash_demux_poll_clock_drift (GstDashDemux * demux)
3171 GstDashDemuxClockDrift *clock_drift;
3172 GDateTime *start = NULL, *end;
3173 GstBuffer *buffer = NULL;
3174 GstDateTime *value = NULL;
3175 gboolean ret = FALSE;
3177 GstMPDUTCTimingType method;
3180 g_return_val_if_fail (demux != NULL, FALSE);
3181 g_return_val_if_fail (demux->clock_drift != NULL, FALSE);
3182 clock_drift = demux->clock_drift;
3184 GST_TIME_AS_USECONDS (gst_adaptive_demux_get_monotonic_time
3185 (GST_ADAPTIVE_DEMUX_CAST (demux)));
3186 if (now < clock_drift->next_update) {
3187 /*TODO: If a fragment fails to download in adaptivedemux, it waits
3188 for a manifest reload before another attempt to fetch a fragment.
3189 Section 10.8.6 of the DVB-DASH standard states that the DASH client
3190 shall refresh the manifest and resynchronise to one of the time sources.
3192 Currently the fact that the manifest refresh follows a download failure
3193 does not make it into dashdemux. */
3196 urls = gst_mpd_client_get_utc_timing_sources (demux->client,
3197 SUPPORTED_CLOCK_FORMATS, &method);
3201 /* Update selected_url just in case the number of URLs in the UTCTiming
3202 element has shrunk since the last poll */
3203 clock_drift->selected_url = clock_drift->selected_url % g_strv_length (urls);
3204 g_mutex_lock (&clock_drift->clock_lock);
3206 if (method == GST_MPD_UTCTIMING_TYPE_NTP) {
3207 value = gst_dash_demux_poll_ntp_server (clock_drift, urls);
3209 GST_ERROR_OBJECT (demux, "Failed to fetch time from NTP server %s",
3210 urls[clock_drift->selected_url]);
3211 g_mutex_unlock (&clock_drift->clock_lock);
3216 gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3218 GstFragment *download;
3219 #ifdef TIZEN_FEATURE_ADAPTIVE_MODIFICATION
3220 GST_DEBUG_OBJECT (demux, "Fetching current time from %s",
3221 urls[clock_drift->selected_url]);
3222 download = gst_uri_downloader_fetch_uri (GST_ADAPTIVE_DEMUX_CAST
3223 (demux)->downloader, urls[clock_drift->selected_url], NULL, NULL, NULL,
3224 DEFAULT_ADAPTIVE_RETRY, DEFAULT_ADAPTIVE_TIMEOUT, TRUE, FALSE, TRUE,
3227 gint64 range_start = 0, range_end = -1;
3228 GST_DEBUG_OBJECT (demux, "Fetching current time from %s",
3229 urls[clock_drift->selected_url]);
3230 if (method == GST_MPD_UTCTIMING_TYPE_HTTP_HEAD) {
3234 gst_uri_downloader_fetch_uri_with_range (GST_ADAPTIVE_DEMUX_CAST
3235 (demux)->downloader, urls[clock_drift->selected_url], NULL, TRUE, TRUE,
3236 TRUE, range_start, range_end, NULL);
3239 if (method == GST_MPD_UTCTIMING_TYPE_HTTP_HEAD && download->headers) {
3240 value = gst_dash_demux_parse_http_head (clock_drift, download);
3242 buffer = gst_fragment_get_buffer (download);
3244 g_object_unref (download);
3247 g_mutex_unlock (&clock_drift->clock_lock);
3248 if (!value && !buffer) {
3249 GST_ERROR_OBJECT (demux, "Failed to fetch time from %s",
3250 urls[clock_drift->selected_url]);
3253 end = gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3254 if (!value && method == GST_MPD_UTCTIMING_TYPE_HTTP_NTP) {
3255 value = gst_dash_demux_parse_http_ntp (clock_drift, buffer);
3256 } else if (!value) {
3257 /* GST_MPD_UTCTIMING_TYPE_HTTP_XSDATE or GST_MPD_UTCTIMING_TYPE_HTTP_ISO */
3258 value = gst_dash_demux_parse_http_xsdate (clock_drift, buffer);
3261 gst_buffer_unref (buffer);
3263 GTimeSpan download_duration = g_date_time_difference (end, start);
3264 GDateTime *client_now, *server_now;
3265 /* We don't know when the server sampled its clock, but we know
3266 it must have been before "end" and probably after "start".
3267 A reasonable estimate is to use (start+end)/2
3269 client_now = g_date_time_add (start, download_duration / 2);
3270 server_now = gst_date_time_to_g_date_time (value);
3271 /* If gst_date_time_new_from_iso8601_string is given an unsupported
3272 ISO 8601 format, it can return a GstDateTime that is not valid,
3273 which causes gst_date_time_to_g_date_time to return NULL */
3275 g_mutex_lock (&clock_drift->clock_lock);
3276 clock_drift->clock_compensation =
3277 g_date_time_difference (server_now, client_now);
3278 g_mutex_unlock (&clock_drift->clock_lock);
3279 GST_DEBUG_OBJECT (demux,
3280 "Difference between client and server clocks is %lfs",
3281 ((double) clock_drift->clock_compensation) / 1000000.0);
3282 g_date_time_unref (server_now);
3285 GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
3287 g_date_time_unref (client_now);
3288 gst_date_time_unref (value);
3290 GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
3292 g_date_time_unref (end);
3295 g_date_time_unref (start);
3296 /* if multiple URLs were specified, use a simple round-robin to
3298 g_mutex_lock (&clock_drift->clock_lock);
3299 if (method == GST_MPD_UTCTIMING_TYPE_NTP) {
3300 clock_drift->next_update = now + FAST_CLOCK_UPDATE_INTERVAL;
3302 clock_drift->selected_url =
3303 (1 + clock_drift->selected_url) % g_strv_length (urls);
3305 clock_drift->next_update = now + SLOW_CLOCK_UPDATE_INTERVAL;
3307 clock_drift->next_update = now + FAST_CLOCK_UPDATE_INTERVAL;
3310 g_mutex_unlock (&clock_drift->clock_lock);
3315 gst_dash_demux_get_clock_compensation (GstDashDemux * demux)
3318 if (demux->clock_drift) {
3319 g_mutex_lock (&demux->clock_drift->clock_lock);
3320 rv = demux->clock_drift->clock_compensation;
3321 g_mutex_unlock (&demux->clock_drift->clock_lock);
3323 GST_LOG_OBJECT (demux, "Clock drift %" GST_STIME_FORMAT, GST_STIME_ARGS (rv));
3328 gst_dash_demux_get_server_now_utc (GstDashDemux * demux)
3330 GDateTime *client_now;
3331 GDateTime *server_now;
3334 gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3336 g_date_time_add (client_now,
3337 gst_dash_demux_get_clock_compensation (demux));
3338 g_date_time_unref (client_now);