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.
143 * Keyframe trick-mode implementation:
145 * When requested (with GST_SEEK_FLAG_TRICKMODE_KEY_UNIT) and if the format
146 * is supported (ISOBMFF profiles), dashdemux can download only keyframes
147 * in order to provide fast forward/reverse playback without exceeding the
148 * available bandwidth/cpu/memory usage.
150 * This is done in two parts:
151 * 1) Parsing ISOBMFF atoms to detect the location of keyframes and only
152 * download/push those.
153 * 2) Deciding what the ideal next keyframe to download is in order to
154 * provide as many keyframes as possible without rebuffering.
156 * * Keyframe-only downloads:
158 * For each beginning of fragment, the fragment header will be parsed in
159 * gst_dash_demux_parse_isobmff() and then the information (offset, pts...)
160 * of each keyframe will be stored in moof_sync_samples.
162 * gst_dash_demux_stream_update_fragment_info() will specify the range
163 * start and end of the current keyframe, which will cause GstAdaptiveDemux
164 * to do a new upstream range request.
166 * When advancing, if there are still some keyframes in the current
167 * fragment, gst_dash_demux_stream_advance_fragment() will call
168 * gst_dash_demux_stream_advance_sync_sample() which decides what the next
169 * keyframe to get will be (it can be in reverse order for example, or
170 * might not be the *next* keyframe but one further as explained below).
172 * If no more keyframes are available in the current fragment, dash will
173 * advance to the next fragment (just like in the normal case) or to a
174 * fragment much further away (as explained below).
177 * * Deciding the optimal "next" keyframe/fragment to download:
179 * The main reason for doing keyframe-only downloads is for trick-modes
180 * (i.e. being able to do fast reverse/forward playback with limited
181 * bandwidth/cpu/memory).
183 * Downloading all keyframes might not be the optimal solution, especially
184 * at high playback rates, since the time taken to download the keyframe
185 * might exceed the available running time between two displayed frames
186 * (i.e. all frames would end up arriving late). This would cause severe
189 * Note: The values specified below can be in either the segment running
190 * time or in absolute values. Where position values need to be converted
191 * to segment running time the "running_time(val)" notation is used, and
192 * where running time need ot be converted to segment poisition the
193 * "position(val)" notation is used.
195 * The goal instead is to be able to download/display as many frames as
196 * possible for a given playback rate. For that the implementation will
198 * * The requested playback rate and segment
199 * * The average time to request and download a keyframe (in running time)
200 * * The current position of dashdemux in the stream
201 * * The current downstream (i.e. sink) position (in running time)
203 * To reach this goal we consider that there is some amount of buffering
204 * (in time) between dashdemux and the display sink. While we do not know
205 * the exact amount of buffering available, a safe and reasonable assertion
206 * is that there is at least a second (in running time).
208 * The average time to request and fully download a keyframe (with or
209 * without fragment header) is obtained by averaging the
210 * GstAdaptiveDemuxStream->last_download_time and is stored in
211 * GstDashDemuxStream->average_download_time. Those values include the
212 * network latency and full download time, which are more interesting and
213 * correct than just bitrates (with small download sizes, the impact of the
214 * network latency is much higher).
216 * The current position is calculated based on the fragment timestamp and
217 * the current keyframe index within that fragment. It is stored in
218 * GstDashDemuxStream->actual_position.
220 * The downstream position of the pipeline is obtained via QoS events and
221 * is stored in GstAdaptiveDemux (note: it's a running time value).
223 * The estimated buffering level between dashdemux and downstream is
225 * buffering_level = running_time(actual_position) - qos_earliest_time
227 * In order to avoid rebuffering, we want to ensure that the next keyframe
228 * (including potential fragment header) we request will be download, demuxed
229 * and decoded in time so that it is not late. That next keyframe time is
230 * called the "target_time" and is calculated whenever we have finished
231 * pushing a keyframe downstream.
233 * One simple observation at this point is that we *need* to make sure that
234 * the target time is chosen such that:
235 * running_time(target_time) > qos_earliest_time + average_download_time
237 * i.e. we chose a target time which will be greater than the time at which
238 * downstream will be once we request and download the keyframe (otherwise
239 * we're guaranteed to be late).
241 * This would provide the highest number of displayed frames per
242 * second, but it is just a *minimal* value and is not enough as-is,
243 * since it doesn't take into account the following items which could
244 * cause frames to arrive late (and therefore rebuffering):
245 * * Network jitter (i.e. by how much the download time can fluctuate)
247 * * Different keyframe sizes (and therefore download time)
250 * Instead, we adjust the target time calculation based on the
253 * The smaller the buffering level is (i.e. the closer we are between
254 * current and downstream), the more aggressively we skip forward (and
255 * guarantee the keyframe will be downloaded, decoded and displayed in
256 * time). And the higher the buffering level, the least aggresivelly
257 * we need to skip forward (and therefore display more frames per
260 * Right now the threshold for aggressive switching is set to 3
261 * average_download_time. Below that buffering level we set the target time
262 * to at least 3 average_download_time distance beyond the
265 * If we are above that buffering level we set the target time to:
266 * position(running_time(position) + average_download_time)
268 * The logic is therefore:
270 * Calculate target_time
271 * Advance to keyframe/fragment for that target_time
272 * Adaptivedemux downloads that keyframe/fragment
283 #include <inttypes.h>
285 #include <gst/base/gsttypefindhelper.h>
286 #include <gst/tag/tag.h>
287 #include <gst/net/gstnet.h>
288 #include <glib/gi18n-lib.h>
289 #include "gstdashdemux.h"
290 #include "gstdash_debug.h"
292 static GstStaticPadTemplate gst_dash_demux_videosrc_template =
293 GST_STATIC_PAD_TEMPLATE ("video_%02u",
296 GST_STATIC_CAPS_ANY);
298 static GstStaticPadTemplate gst_dash_demux_audiosrc_template =
299 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
302 GST_STATIC_CAPS_ANY);
304 static GstStaticPadTemplate gst_dash_demux_subtitlesrc_template =
305 GST_STATIC_PAD_TEMPLATE ("subtitle_%02u",
308 GST_STATIC_CAPS_ANY);
310 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
313 GST_STATIC_CAPS ("application/dash+xml"));
315 GST_DEBUG_CATEGORY (gst_dash_demux_debug);
316 #define GST_CAT_DEFAULT gst_dash_demux_debug
322 PROP_MAX_BUFFERING_TIME,
323 PROP_BANDWIDTH_USAGE,
325 PROP_MAX_VIDEO_WIDTH,
326 PROP_MAX_VIDEO_HEIGHT,
327 PROP_MAX_VIDEO_FRAMERATE,
328 PROP_PRESENTATION_DELAY,
332 /* Default values for properties */
333 #define DEFAULT_MAX_BUFFERING_TIME 30 /* in seconds */
334 #define DEFAULT_BANDWIDTH_USAGE 0.8f /* 0 to 1 */
335 #define DEFAULT_MAX_BITRATE 0 /* in bit/s */
336 #define DEFAULT_MAX_VIDEO_WIDTH 0
337 #define DEFAULT_MAX_VIDEO_HEIGHT 0
338 #define DEFAULT_MAX_VIDEO_FRAMERATE_N 0
339 #define DEFAULT_MAX_VIDEO_FRAMERATE_D 1
340 #define DEFAULT_PRESENTATION_DELAY "10s" /* 10s */
342 /* Clock drift compensation for live streams */
343 #define SLOW_CLOCK_UPDATE_INTERVAL (1000000 * 30 * 60) /* 30 minutes */
344 #define FAST_CLOCK_UPDATE_INTERVAL (1000000 * 30) /* 30 seconds */
345 #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)
346 #define NTP_TO_UNIX_EPOCH G_GUINT64_CONSTANT(2208988800) /* difference (in seconds) between NTP epoch and Unix epoch */
348 struct _GstDashDemuxClockDrift
350 GMutex clock_lock; /* used to protect access to struct */
353 /* @clock_compensation: amount (in usecs) to add to client's idea of
354 now to map it to the server's idea of now */
355 GTimeSpan clock_compensation;
361 guint64 start_offset, end_offset;
362 /* TODO: Timestamp and duration */
363 } GstDashStreamSyncSample;
366 static void gst_dash_demux_set_property (GObject * object, guint prop_id,
367 const GValue * value, GParamSpec * pspec);
368 static void gst_dash_demux_get_property (GObject * object, guint prop_id,
369 GValue * value, GParamSpec * pspec);
370 static void gst_dash_demux_dispose (GObject * obj);
372 /* GstAdaptiveDemux */
373 static GstClockTime gst_dash_demux_get_duration (GstAdaptiveDemux * ademux);
374 static gboolean gst_dash_demux_is_live (GstAdaptiveDemux * ademux);
375 static void gst_dash_demux_reset (GstAdaptiveDemux * ademux);
376 static gboolean gst_dash_demux_process_manifest (GstAdaptiveDemux * ademux,
378 static gboolean gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
380 gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream);
381 static GstFlowReturn gst_dash_demux_stream_seek (GstAdaptiveDemuxStream *
382 stream, gboolean forward, GstSeekFlags flags, GstClockTime ts,
383 GstClockTime * final_ts);
384 static gboolean gst_dash_demux_stream_has_next_fragment (GstAdaptiveDemuxStream
387 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream);
389 gst_dash_demux_stream_advance_subfragment (GstAdaptiveDemuxStream * stream);
390 static gboolean gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream *
391 stream, guint64 bitrate);
392 static gint64 gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux *
394 static GstFlowReturn gst_dash_demux_update_manifest_data (GstAdaptiveDemux *
395 demux, GstBuffer * buf);
397 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
399 static void gst_dash_demux_advance_period (GstAdaptiveDemux * demux);
400 static gboolean gst_dash_demux_has_next_period (GstAdaptiveDemux * demux);
401 static GstFlowReturn gst_dash_demux_data_received (GstAdaptiveDemux * demux,
402 GstAdaptiveDemuxStream * stream, GstBuffer * buffer);
404 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
405 GstAdaptiveDemuxStream * stream);
407 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
408 GstAdaptiveDemuxStream * stream);
409 static gboolean gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream *
413 static gboolean gst_dash_demux_setup_all_streams (GstDashDemux * demux);
414 static void gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream);
416 static GstCaps *gst_dash_demux_get_input_caps (GstDashDemux * demux,
417 GstActiveStream * stream);
418 static GstPad *gst_dash_demux_create_pad (GstDashDemux * demux,
419 GstActiveStream * stream);
420 static GstDashDemuxClockDrift *gst_dash_demux_clock_drift_new (GstDashDemux *
422 static void gst_dash_demux_clock_drift_free (GstDashDemuxClockDrift *);
423 static gboolean gst_dash_demux_poll_clock_drift (GstDashDemux * demux);
424 static GTimeSpan gst_dash_demux_get_clock_compensation (GstDashDemux * demux);
425 static GDateTime *gst_dash_demux_get_server_now_utc (GstDashDemux * demux);
427 #define SIDX(s) (&(s)->sidx_parser.sidx)
429 static inline GstSidxBoxEntry *
430 SIDX_ENTRY (GstDashDemuxStream * s, gint i)
432 g_assert (i < SIDX (s)->entries_count);
433 return &(SIDX (s)->entries[(i)]);
436 #define SIDX_CURRENT_ENTRY(s) SIDX_ENTRY(s, SIDX(s)->entry_index)
438 static void gst_dash_demux_send_content_protection_event (gpointer cp_data,
441 #define gst_dash_demux_parent_class parent_class
442 G_DEFINE_TYPE_WITH_CODE (GstDashDemux, gst_dash_demux, GST_TYPE_ADAPTIVE_DEMUX,
443 GST_DEBUG_CATEGORY_INIT (gst_dash_demux_debug, "dashdemux", 0,
446 GST_ELEMENT_REGISTER_DEFINE (dashdemux, "dashdemux", GST_RANK_PRIMARY,
447 GST_TYPE_DASH_DEMUX);
450 gst_dash_demux_dispose (GObject * obj)
452 GstDashDemux *demux = GST_DASH_DEMUX (obj);
454 gst_dash_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
457 gst_mpd_client_free (demux->client);
458 demux->client = NULL;
461 g_mutex_clear (&demux->client_lock);
463 gst_dash_demux_clock_drift_free (demux->clock_drift);
464 demux->clock_drift = NULL;
465 g_free (demux->default_presentation_delay);
466 G_OBJECT_CLASS (parent_class)->dispose (obj);
470 gst_dash_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
473 GstDashDemux *self = GST_DASH_DEMUX (demux);
476 GTimeSpan stream_now;
477 GstClockTime seg_duration;
479 if (self->client->mpd_root_node->availabilityStartTime == NULL)
482 seg_duration = gst_mpd_client_get_maximum_segment_duration (self->client);
483 now = gst_dash_demux_get_server_now_utc (self);
485 gst_date_time_to_g_date_time (self->client->mpd_root_node->
486 availabilityStartTime);
487 stream_now = g_date_time_difference (now, mstart);
488 g_date_time_unref (now);
489 g_date_time_unref (mstart);
494 *stop = stream_now * GST_USECOND;
495 if (self->client->mpd_root_node->timeShiftBufferDepth ==
496 GST_MPD_DURATION_NONE) {
501 (self->client->mpd_root_node->timeShiftBufferDepth * GST_MSECOND);
506 /* As defined in 5.3.9.5.3 of the DASH specification, a segment does
507 not become available until the sum of:
508 * the value of the MPD@availabilityStartTime,
509 * the PeriodStart time of the containing Period
510 * the MPD start time of the Media Segment, and
511 * the MPD duration of the Media Segment.
512 Therefore we need to subtract the media segment duration from the stop
515 *stop -= seg_duration;
520 gst_dash_demux_get_presentation_offset (GstAdaptiveDemux * demux,
521 GstAdaptiveDemuxStream * stream)
523 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
524 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
526 return gst_mpd_client_get_stream_presentation_offset (dashdemux->client,
531 gst_dash_demux_get_period_start_time (GstAdaptiveDemux * demux)
533 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
535 return gst_mpd_client_get_period_start_time (dashdemux->client);
539 gst_dash_demux_class_init (GstDashDemuxClass * klass)
541 GObjectClass *gobject_class;
542 GstElementClass *gstelement_class;
543 GstAdaptiveDemuxClass *gstadaptivedemux_class;
545 gobject_class = (GObjectClass *) klass;
546 gstelement_class = (GstElementClass *) klass;
547 gstadaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
549 gobject_class->set_property = gst_dash_demux_set_property;
550 gobject_class->get_property = gst_dash_demux_get_property;
551 gobject_class->dispose = gst_dash_demux_dispose;
553 #ifndef GST_REMOVE_DEPRECATED
554 g_object_class_install_property (gobject_class, PROP_MAX_BUFFERING_TIME,
555 g_param_spec_uint ("max-buffering-time", "Maximum buffering time",
556 "Maximum number of seconds of buffer accumulated during playback"
558 2, G_MAXUINT, DEFAULT_MAX_BUFFERING_TIME,
559 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
561 g_object_class_install_property (gobject_class, PROP_BANDWIDTH_USAGE,
562 g_param_spec_float ("bandwidth-usage",
563 "Bandwidth usage [0..1]",
564 "Percentage of the available bandwidth to use when "
565 "selecting representations (deprecated)",
566 0, 1, DEFAULT_BANDWIDTH_USAGE,
567 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
570 g_object_class_install_property (gobject_class, PROP_MAX_BITRATE,
571 g_param_spec_uint ("max-bitrate", "Max bitrate",
572 "Max of bitrate supported by target video decoder (0 = no maximum)",
573 0, G_MAXUINT, DEFAULT_MAX_BITRATE,
574 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
576 g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_WIDTH,
577 g_param_spec_uint ("max-video-width", "Max video width",
578 "Max video width to select (0 = no maximum)",
579 0, G_MAXUINT, DEFAULT_MAX_VIDEO_WIDTH,
580 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
582 g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_HEIGHT,
583 g_param_spec_uint ("max-video-height", "Max video height",
584 "Max video height to select (0 = no maximum)",
585 0, G_MAXUINT, DEFAULT_MAX_VIDEO_HEIGHT,
586 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
588 g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_FRAMERATE,
589 gst_param_spec_fraction ("max-video-framerate", "Max video framerate",
590 "Max video framerate to select (0/1 = no maximum)",
591 0, 1, G_MAXINT, 1, DEFAULT_MAX_VIDEO_FRAMERATE_N,
592 DEFAULT_MAX_VIDEO_FRAMERATE_D,
593 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
595 g_object_class_install_property (gobject_class, PROP_PRESENTATION_DELAY,
596 g_param_spec_string ("presentation-delay", "Presentation delay",
597 "Default presentation delay (in seconds, milliseconds or fragments) (e.g. 12s, 2500ms, 3f)",
598 DEFAULT_PRESENTATION_DELAY,
599 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
601 gst_element_class_add_static_pad_template (gstelement_class,
602 &gst_dash_demux_audiosrc_template);
603 gst_element_class_add_static_pad_template (gstelement_class,
604 &gst_dash_demux_videosrc_template);
605 gst_element_class_add_static_pad_template (gstelement_class,
606 &gst_dash_demux_subtitlesrc_template);
608 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
610 gst_element_class_set_static_metadata (gstelement_class,
612 "Codec/Demuxer/Adaptive",
613 "Dynamic Adaptive Streaming over HTTP demuxer",
614 "David Corvoysier <david.corvoysier@orange.com>\n\
615 Hamid Zakari <hamid.zakari@gmail.com>\n\
616 Gianluca Gennari <gennarone@gmail.com>");
619 gstadaptivedemux_class->get_duration = gst_dash_demux_get_duration;
620 gstadaptivedemux_class->is_live = gst_dash_demux_is_live;
621 gstadaptivedemux_class->reset = gst_dash_demux_reset;
622 gstadaptivedemux_class->seek = gst_dash_demux_seek;
624 gstadaptivedemux_class->process_manifest = gst_dash_demux_process_manifest;
625 gstadaptivedemux_class->update_manifest_data =
626 gst_dash_demux_update_manifest_data;
627 gstadaptivedemux_class->get_manifest_update_interval =
628 gst_dash_demux_get_manifest_update_interval;
630 gstadaptivedemux_class->has_next_period = gst_dash_demux_has_next_period;
631 gstadaptivedemux_class->advance_period = gst_dash_demux_advance_period;
632 gstadaptivedemux_class->stream_has_next_fragment =
633 gst_dash_demux_stream_has_next_fragment;
634 gstadaptivedemux_class->stream_advance_fragment =
635 gst_dash_demux_stream_advance_fragment;
636 gstadaptivedemux_class->stream_get_fragment_waiting_time =
637 gst_dash_demux_stream_get_fragment_waiting_time;
638 gstadaptivedemux_class->stream_seek = gst_dash_demux_stream_seek;
639 gstadaptivedemux_class->stream_select_bitrate =
640 gst_dash_demux_stream_select_bitrate;
641 gstadaptivedemux_class->stream_update_fragment_info =
642 gst_dash_demux_stream_update_fragment_info;
643 gstadaptivedemux_class->stream_free = gst_dash_demux_stream_free;
644 gstadaptivedemux_class->get_live_seek_range =
645 gst_dash_demux_get_live_seek_range;
646 gstadaptivedemux_class->get_presentation_offset =
647 gst_dash_demux_get_presentation_offset;
648 gstadaptivedemux_class->get_period_start_time =
649 gst_dash_demux_get_period_start_time;
651 gstadaptivedemux_class->start_fragment = gst_dash_demux_stream_fragment_start;
652 gstadaptivedemux_class->finish_fragment =
653 gst_dash_demux_stream_fragment_finished;
654 gstadaptivedemux_class->data_received = gst_dash_demux_data_received;
655 gstadaptivedemux_class->need_another_chunk =
656 gst_dash_demux_need_another_chunk;
660 gst_dash_demux_init (GstDashDemux * demux)
663 demux->max_buffering_time = DEFAULT_MAX_BUFFERING_TIME * GST_SECOND;
664 demux->max_bitrate = DEFAULT_MAX_BITRATE;
665 demux->max_video_width = DEFAULT_MAX_VIDEO_WIDTH;
666 demux->max_video_height = DEFAULT_MAX_VIDEO_HEIGHT;
667 demux->max_video_framerate_n = DEFAULT_MAX_VIDEO_FRAMERATE_N;
668 demux->max_video_framerate_d = DEFAULT_MAX_VIDEO_FRAMERATE_D;
669 demux->default_presentation_delay = g_strdup (DEFAULT_PRESENTATION_DELAY);
671 g_mutex_init (&demux->client_lock);
673 gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
674 sizeof (GstDashDemuxStream));
678 gst_dash_demux_set_property (GObject * object, guint prop_id,
679 const GValue * value, GParamSpec * pspec)
681 GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
682 GstDashDemux *demux = GST_DASH_DEMUX (object);
685 case PROP_MAX_BUFFERING_TIME:
686 demux->max_buffering_time = g_value_get_uint (value) * GST_SECOND;
688 case PROP_BANDWIDTH_USAGE:
689 adaptivedemux->bitrate_limit = g_value_get_float (value);
691 case PROP_MAX_BITRATE:
692 demux->max_bitrate = g_value_get_uint (value);
694 case PROP_MAX_VIDEO_WIDTH:
695 demux->max_video_width = g_value_get_uint (value);
697 case PROP_MAX_VIDEO_HEIGHT:
698 demux->max_video_height = g_value_get_uint (value);
700 case PROP_MAX_VIDEO_FRAMERATE:
701 demux->max_video_framerate_n = gst_value_get_fraction_numerator (value);
702 demux->max_video_framerate_d = gst_value_get_fraction_denominator (value);
704 case PROP_PRESENTATION_DELAY:
705 g_free (demux->default_presentation_delay);
706 demux->default_presentation_delay = g_value_dup_string (value);
709 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
715 gst_dash_demux_get_property (GObject * object, guint prop_id, GValue * value,
718 GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
719 GstDashDemux *demux = GST_DASH_DEMUX (object);
722 case PROP_MAX_BUFFERING_TIME:
723 g_value_set_uint (value, demux->max_buffering_time / GST_SECOND);
725 case PROP_BANDWIDTH_USAGE:
726 g_value_set_float (value, adaptivedemux->bitrate_limit);
728 case PROP_MAX_BITRATE:
729 g_value_set_uint (value, demux->max_bitrate);
731 case PROP_MAX_VIDEO_WIDTH:
732 g_value_set_uint (value, demux->max_video_width);
734 case PROP_MAX_VIDEO_HEIGHT:
735 g_value_set_uint (value, demux->max_video_height);
737 case PROP_MAX_VIDEO_FRAMERATE:
738 gst_value_set_fraction (value, demux->max_video_framerate_n,
739 demux->max_video_framerate_d);
741 case PROP_PRESENTATION_DELAY:
742 if (demux->default_presentation_delay == NULL)
743 g_value_set_static_string (value, "");
745 g_value_set_string (value, demux->default_presentation_delay);
748 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
754 gst_dash_demux_setup_mpdparser_streams (GstDashDemux * demux,
755 GstMPDClient * client)
757 gboolean has_streams = FALSE;
758 GList *adapt_sets, *iter;
760 adapt_sets = gst_mpd_client_get_adaptation_sets (client);
761 for (iter = adapt_sets; iter; iter = g_list_next (iter)) {
762 GstMPDAdaptationSetNode *adapt_set_node = iter->data;
764 gst_mpd_client_setup_streaming (client, adapt_set_node);
769 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, ("Manifest has no playable "
770 "streams"), ("No streams could be activated from the manifest"));
776 gst_dash_demux_setup_all_streams (GstDashDemux * demux)
780 GST_DEBUG_OBJECT (demux, "Setting up streams for period %d",
781 gst_mpd_client_get_period_index (demux->client));
783 /* clean old active stream list, if any */
784 gst_mpd_client_active_streams_free (demux->client);
786 if (!gst_dash_demux_setup_mpdparser_streams (demux, demux->client)) {
790 GST_DEBUG_OBJECT (demux, "Creating stream objects");
791 for (i = 0; i < gst_mpd_client_get_nb_active_stream (demux->client); i++) {
792 GstDashDemuxStream *stream;
793 GstActiveStream *active_stream;
798 GstTagList *tags = NULL;
801 gst_mpd_client_get_active_stream_by_index (demux->client, i);
802 if (active_stream == NULL)
805 if (demux->trickmode_no_audio
806 && active_stream->mimeType == GST_STREAM_AUDIO) {
807 GST_DEBUG_OBJECT (demux,
808 "Skipping audio stream %d because of TRICKMODE_NO_AUDIO flag", i);
812 srcpad = gst_dash_demux_create_pad (demux, active_stream);
816 caps = gst_dash_demux_get_input_caps (demux, active_stream);
817 GST_LOG_OBJECT (demux, "Creating stream %d %" GST_PTR_FORMAT, i, caps);
819 if (active_stream->cur_adapt_set) {
820 GstMPDAdaptationSetNode *adp_set = active_stream->cur_adapt_set;
821 lang = adp_set->lang;
823 /* Fallback to the language in ContentComponent node */
827 for (it = adp_set->ContentComponents; it; it = it->next) {
828 GstMPDContentComponentNode *cc_node = it->data;
830 lang = cc_node->lang;
838 if (gst_tag_check_language_code (lang))
839 tags = gst_tag_list_new (GST_TAG_LANGUAGE_CODE, lang, NULL);
841 tags = gst_tag_list_new (GST_TAG_LANGUAGE_NAME, lang, NULL);
844 stream = (GstDashDemuxStream *)
845 gst_adaptive_demux_stream_new (GST_ADAPTIVE_DEMUX_CAST (demux), srcpad);
846 stream->active_stream = active_stream;
848 if (active_stream->cur_representation) {
849 stream->last_representation_id =
850 g_strdup (stream->active_stream->cur_representation->id);
852 stream->last_representation_id = NULL;
855 s = gst_caps_get_structure (caps, 0);
857 gst_mpd_client_has_isoff_ondemand_profile (demux->client);
858 stream->is_isobmff = gst_structure_has_name (s, "video/quicktime")
859 || gst_structure_has_name (s, "audio/x-m4a");
860 stream->first_sync_sample_always_after_moof = TRUE;
861 stream->adapter = gst_adapter_new ();
862 gst_adaptive_demux_stream_set_caps (GST_ADAPTIVE_DEMUX_STREAM_CAST (stream),
865 gst_adaptive_demux_stream_set_tags (GST_ADAPTIVE_DEMUX_STREAM_CAST
868 stream->pending_seek_ts = GST_CLOCK_TIME_NONE;
869 stream->sidx_position = GST_CLOCK_TIME_NONE;
870 stream->actual_position = GST_CLOCK_TIME_NONE;
871 stream->target_time = GST_CLOCK_TIME_NONE;
872 /* Set a default average keyframe download time of a quarter of a second */
873 stream->average_download_time = 250 * GST_MSECOND;
875 if (active_stream->cur_adapt_set &&
876 GST_MPD_REPRESENTATION_BASE_NODE (active_stream->
877 cur_adapt_set)->ContentProtection) {
878 GST_DEBUG_OBJECT (demux, "Adding ContentProtection events to source pad");
879 g_list_foreach (GST_MPD_REPRESENTATION_BASE_NODE
880 (active_stream->cur_adapt_set)->ContentProtection,
881 gst_dash_demux_send_content_protection_event, stream);
884 gst_isoff_sidx_parser_init (&stream->sidx_parser);
891 gst_dash_demux_send_content_protection_event (gpointer data, gpointer userdata)
893 GstMPDDescriptorTypeNode *cp = (GstMPDDescriptorTypeNode *) data;
894 GstDashDemuxStream *stream = (GstDashDemuxStream *) userdata;
899 GstPad *pad = GST_ADAPTIVE_DEMUX_STREAM_PAD (stream);
901 if (cp->schemeIdUri == NULL)
904 GST_TRACE_OBJECT (pad, "check schemeIdUri %s", cp->schemeIdUri);
905 /* RFC 2141 states: The leading "urn:" sequence is case-insensitive */
906 schemeIdUri = g_ascii_strdown (cp->schemeIdUri, -1);
907 if (g_str_has_prefix (schemeIdUri, "urn:uuid:")) {
908 pssi_len = strlen (cp->value);
909 pssi = gst_buffer_new_memdup (cp->value, pssi_len);
910 /* RFC 4122 states that the hex part of a UUID is in lower case,
911 * but some streams seem to ignore this and use upper case for the
912 * protection system ID */
913 event = gst_event_new_protection (cp->schemeIdUri + 9, pssi, "dash/mpd");
915 "Queueing protection event %" GST_PTR_FORMAT " on source pad", event);
916 gst_adaptive_demux_stream_queue_event ((GstAdaptiveDemuxStream *) stream,
918 gst_buffer_unref (pssi);
920 g_free (schemeIdUri);
924 gst_dash_demux_get_duration (GstAdaptiveDemux * ademux)
926 GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
928 g_return_val_if_fail (demux->client != NULL, GST_CLOCK_TIME_NONE);
930 return gst_mpd_client_get_media_presentation_duration (demux->client);
934 gst_dash_demux_is_live (GstAdaptiveDemux * ademux)
936 GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
938 g_return_val_if_fail (demux->client != NULL, FALSE);
940 return gst_mpd_client_is_live (demux->client);
944 gst_dash_demux_setup_streams (GstAdaptiveDemux * demux)
946 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
948 GstDateTime *now = NULL;
951 /* setup video, audio and subtitle streams, starting from first Period if
954 if (gst_mpd_client_is_live (dashdemux->client)) {
956 if (dashdemux->client->mpd_root_node->availabilityStartTime == NULL) {
958 GST_ERROR_OBJECT (demux, "MPD does not have availabilityStartTime");
961 if (dashdemux->clock_drift == NULL) {
964 gst_mpd_client_get_utc_timing_sources (dashdemux->client,
965 SUPPORTED_CLOCK_FORMATS, NULL);
967 GST_DEBUG_OBJECT (dashdemux, "Found a supported UTCTiming element");
968 dashdemux->clock_drift = gst_dash_demux_clock_drift_new (dashdemux);
969 gst_dash_demux_poll_clock_drift (dashdemux);
972 /* get period index for period encompassing the current time */
973 g_now = gst_dash_demux_get_server_now_utc (dashdemux);
974 now = gst_date_time_new_from_g_date_time (g_now);
975 if (dashdemux->client->mpd_root_node->suggestedPresentationDelay != -1) {
976 GstDateTime *target = gst_mpd_client_add_time_difference (now,
977 dashdemux->client->mpd_root_node->suggestedPresentationDelay * -1000);
978 gst_date_time_unref (now);
980 } else if (dashdemux->default_presentation_delay) {
982 gst_mpd_client_parse_default_presentation_delay (dashdemux->client,
983 dashdemux->default_presentation_delay);
984 GstDateTime *target = gst_mpd_client_add_time_difference (now,
986 gst_date_time_unref (now);
990 gst_mpd_client_get_period_index_at_time (dashdemux->client, now);
991 if (period_idx == G_MAXUINT) {
992 #ifndef GST_DISABLE_GST_DEBUG
993 gchar *date_str = gst_date_time_to_iso8601_string (now);
994 GST_DEBUG_OBJECT (demux, "Unable to find live period active at %s",
1003 if (!gst_mpd_client_set_period_index (dashdemux->client, period_idx) ||
1004 !gst_dash_demux_setup_all_streams (dashdemux)) {
1009 /* If stream is live, try to find the segment that
1010 * is closest to current time */
1011 if (gst_mpd_client_is_live (dashdemux->client)) {
1014 GST_DEBUG_OBJECT (demux, "Seeking to current time of day for live stream ");
1016 gnow = gst_date_time_to_g_date_time (now);
1017 gst_mpd_client_seek_to_time (dashdemux->client, gnow);
1018 g_date_time_unref (gnow);
1020 GST_DEBUG_OBJECT (demux, "Seeking to first segment for on-demand stream ");
1022 /* start playing from the first segment */
1023 gst_mpd_client_seek_to_first_segment (dashdemux->client);
1028 gst_date_time_unref (now);
1033 gst_dash_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
1035 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1036 gboolean ret = FALSE;
1040 if (dashdemux->client)
1041 gst_mpd_client_free (dashdemux->client);
1042 dashdemux->client = gst_mpd_client_new ();
1043 gst_mpd_client_set_uri_downloader (dashdemux->client, demux->downloader);
1045 dashdemux->client->mpd_uri = g_strdup (demux->manifest_uri);
1046 dashdemux->client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
1048 GST_DEBUG_OBJECT (demux, "Fetched MPD file at URI: %s (base: %s)",
1049 dashdemux->client->mpd_uri,
1050 GST_STR_NULL (dashdemux->client->mpd_base_uri));
1052 if (gst_buffer_map (buf, &mapinfo, GST_MAP_READ)) {
1053 manifest = (gchar *) mapinfo.data;
1054 if (gst_mpd_client_parse (dashdemux->client, manifest, mapinfo.size)) {
1055 if (gst_mpd_client_setup_media_presentation (dashdemux->client, 0, 0,
1059 GST_ELEMENT_ERROR (demux, STREAM, DECODE,
1060 ("Incompatible manifest file."), (NULL));
1063 gst_buffer_unmap (buf, &mapinfo);
1065 GST_WARNING_OBJECT (demux, "Failed to map manifest buffer");
1069 ret = gst_dash_demux_setup_streams (demux);
1075 gst_dash_demux_create_pad (GstDashDemux * demux, GstActiveStream * stream)
1078 GstPadTemplate *tmpl;
1081 switch (stream->mimeType) {
1082 case GST_STREAM_AUDIO:
1083 name = g_strdup_printf ("audio_%02u", demux->n_audio_streams++);
1084 tmpl = gst_static_pad_template_get (&gst_dash_demux_audiosrc_template);
1086 case GST_STREAM_VIDEO:
1087 name = g_strdup_printf ("video_%02u", demux->n_video_streams++);
1088 tmpl = gst_static_pad_template_get (&gst_dash_demux_videosrc_template);
1090 case GST_STREAM_APPLICATION:
1091 if (gst_mpd_client_active_stream_contains_subtitles (stream)) {
1092 name = g_strdup_printf ("subtitle_%02u", demux->n_subtitle_streams++);
1094 gst_static_pad_template_get (&gst_dash_demux_subtitlesrc_template);
1100 g_assert_not_reached ();
1104 /* Create and activate new pads */
1105 pad = gst_pad_new_from_template (tmpl, name);
1107 gst_object_unref (tmpl);
1109 gst_pad_set_active (pad, TRUE);
1110 GST_INFO_OBJECT (demux, "Creating srcpad %s:%s", GST_DEBUG_PAD_NAME (pad));
1115 gst_dash_demux_reset (GstAdaptiveDemux * ademux)
1117 GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
1119 GST_DEBUG_OBJECT (demux, "Resetting demux");
1121 demux->end_of_period = FALSE;
1122 demux->end_of_manifest = FALSE;
1124 if (demux->client) {
1125 gst_mpd_client_free (demux->client);
1126 demux->client = NULL;
1128 gst_dash_demux_clock_drift_free (demux->clock_drift);
1129 demux->clock_drift = NULL;
1130 demux->client = gst_mpd_client_new ();
1131 gst_mpd_client_set_uri_downloader (demux->client, ademux->downloader);
1133 demux->n_audio_streams = 0;
1134 demux->n_video_streams = 0;
1135 demux->n_subtitle_streams = 0;
1137 demux->trickmode_no_audio = FALSE;
1138 demux->allow_trickmode_key_units = TRUE;
1142 gst_dash_demux_get_video_input_caps (GstDashDemux * demux,
1143 GstActiveStream * stream)
1145 guint width = 0, height = 0;
1146 gint fps_num = 0, fps_den = 1;
1147 gboolean have_fps = FALSE;
1148 GstCaps *caps = NULL;
1153 /* if bitstreamSwitching is true we don't need to switch pads on resolution change */
1154 if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
1155 width = gst_mpd_client_get_video_stream_width (stream);
1156 height = gst_mpd_client_get_video_stream_height (stream);
1158 gst_mpd_client_get_video_stream_framerate (stream, &fps_num, &fps_den);
1160 caps = gst_mpd_client_get_stream_caps (stream);
1164 if (width > 0 && height > 0) {
1165 gst_caps_set_simple (caps, "width", G_TYPE_INT, width, "height",
1166 G_TYPE_INT, height, NULL);
1170 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fps_num,
1178 gst_dash_demux_get_audio_input_caps (GstDashDemux * demux,
1179 GstActiveStream * stream)
1181 guint rate = 0, channels = 0;
1182 GstCaps *caps = NULL;
1187 /* if bitstreamSwitching is true we don't need to switch pads on rate/channels change */
1188 if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
1189 channels = gst_mpd_client_get_audio_stream_num_channels (stream);
1190 rate = gst_mpd_client_get_audio_stream_rate (stream);
1192 caps = gst_mpd_client_get_stream_caps (stream);
1197 gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
1200 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
1207 gst_dash_demux_get_application_input_caps (GstDashDemux * demux,
1208 GstActiveStream * stream)
1210 GstCaps *caps = NULL;
1215 caps = gst_mpd_client_get_stream_caps (stream);
1223 gst_dash_demux_get_input_caps (GstDashDemux * demux, GstActiveStream * stream)
1225 switch (stream->mimeType) {
1226 case GST_STREAM_VIDEO:
1227 return gst_dash_demux_get_video_input_caps (demux, stream);
1228 case GST_STREAM_AUDIO:
1229 return gst_dash_demux_get_audio_input_caps (demux, stream);
1230 case GST_STREAM_APPLICATION:
1231 return gst_dash_demux_get_application_input_caps (demux, stream);
1233 return GST_CAPS_NONE;
1238 gst_dash_demux_stream_update_headers_info (GstAdaptiveDemuxStream * stream)
1240 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1241 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1244 gst_mpd_client_get_next_header (dashdemux->client,
1245 &path, dashstream->index,
1246 &stream->fragment.header_range_start, &stream->fragment.header_range_end);
1249 stream->fragment.header_uri =
1250 gst_uri_join_strings (gst_mpd_client_get_baseURL (dashdemux->client,
1251 dashstream->index), path);
1256 gst_mpd_client_get_next_header_index (dashdemux->client,
1257 &path, dashstream->index,
1258 &stream->fragment.index_range_start, &stream->fragment.index_range_end);
1261 stream->fragment.index_uri =
1262 gst_uri_join_strings (gst_mpd_client_get_baseURL (dashdemux->client,
1263 dashstream->index), path);
1268 static GstFlowReturn
1269 gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream)
1271 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1272 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1274 GstMediaFragmentInfo fragment;
1277 gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
1279 isombff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1281 /* Reset chunk size if any */
1282 stream->fragment.chunk_size = 0;
1283 dashstream->current_fragment_keyframe_distance = GST_CLOCK_TIME_NONE;
1285 if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream) && isombff) {
1286 gst_dash_demux_stream_update_headers_info (stream);
1287 /* sidx entries may not be available in here */
1288 if (stream->fragment.index_uri
1289 && dashstream->sidx_position != GST_CLOCK_TIME_NONE) {
1290 /* request only the index to be downloaded as we need to reposition the
1291 * stream to a subsegment */
1296 if (dashstream->moof_sync_samples
1297 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
1298 GstDashStreamSyncSample *sync_sample =
1299 &g_array_index (dashstream->moof_sync_samples, GstDashStreamSyncSample,
1300 dashstream->current_sync_sample);
1302 gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1305 if (isombff && dashstream->sidx_position != GST_CLOCK_TIME_NONE
1306 && SIDX (dashstream)->entries) {
1307 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
1308 dashstream->current_fragment_timestamp = fragment.timestamp = entry->pts;
1309 dashstream->current_fragment_duration = fragment.duration =
1312 dashstream->current_fragment_timestamp = fragment.timestamp;
1313 dashstream->current_fragment_duration = fragment.duration;
1316 dashstream->current_fragment_keyframe_distance =
1317 fragment.duration / dashstream->moof_sync_samples->len;
1318 dashstream->actual_position =
1319 fragment.timestamp +
1320 dashstream->current_sync_sample *
1321 dashstream->current_fragment_keyframe_distance;
1322 if (stream->segment.rate < 0.0)
1323 dashstream->actual_position +=
1324 dashstream->current_fragment_keyframe_distance;
1325 dashstream->actual_position =
1326 MIN (dashstream->actual_position,
1327 fragment.timestamp + fragment.duration);
1329 stream->fragment.uri = fragment.uri;
1330 stream->fragment.timestamp = GST_CLOCK_TIME_NONE;
1331 stream->fragment.duration = GST_CLOCK_TIME_NONE;
1332 stream->fragment.range_start = sync_sample->start_offset;
1333 stream->fragment.range_end = sync_sample->end_offset;
1335 GST_DEBUG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
1336 GST_TIME_ARGS (dashstream->actual_position));
1341 if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
1342 dashstream->index, &ts)) {
1343 /* For live streams, check whether the underlying representation changed
1344 * (due to a manifest update with no matching representation) */
1345 if (gst_mpd_client_is_live (dashdemux->client)
1346 && !GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream)) {
1347 if (dashstream->active_stream
1348 && dashstream->active_stream->cur_representation) {
1349 /* id specifies an identifier for this Representation. The
1350 * identifier shall be unique within a Period unless the
1351 * Representation is functionally identically to another
1352 * Representation in the same Period. */
1353 if (g_strcmp0 (dashstream->active_stream->cur_representation->id,
1354 dashstream->last_representation_id)) {
1356 stream->need_header = TRUE;
1358 GST_INFO_OBJECT (dashdemux,
1359 "Representation changed from %s to %s - updating to bitrate %d",
1360 GST_STR_NULL (dashstream->last_representation_id),
1361 GST_STR_NULL (dashstream->active_stream->cur_representation->id),
1362 dashstream->active_stream->cur_representation->bandwidth);
1365 gst_dash_demux_get_input_caps (dashdemux,
1366 dashstream->active_stream);
1367 gst_adaptive_demux_stream_set_caps (stream, caps);
1369 /* Update the stored last representation id */
1370 g_free (dashstream->last_representation_id);
1371 dashstream->last_representation_id =
1372 g_strdup (dashstream->active_stream->cur_representation->id);
1375 g_free (dashstream->last_representation_id);
1376 dashstream->last_representation_id = NULL;
1380 if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream)) {
1381 gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
1382 gst_dash_demux_stream_update_headers_info (stream);
1385 gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1388 stream->fragment.uri = fragment.uri;
1389 /* If mpd does not specify indexRange (i.e., null index_uri),
1390 * sidx entries may not be available until download it */
1391 if (isombff && dashstream->sidx_position != GST_CLOCK_TIME_NONE
1392 && SIDX (dashstream)->entries) {
1393 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
1394 stream->fragment.range_start =
1395 dashstream->sidx_base_offset + entry->offset;
1396 dashstream->actual_position = stream->fragment.timestamp = entry->pts;
1397 dashstream->current_fragment_timestamp = stream->fragment.timestamp =
1399 dashstream->current_fragment_duration = stream->fragment.duration =
1401 if (stream->demux->segment.rate < 0.0) {
1402 stream->fragment.range_end =
1403 stream->fragment.range_start + entry->size - 1;
1404 dashstream->actual_position += entry->duration;
1406 stream->fragment.range_end = fragment.range_end;
1409 dashstream->actual_position = stream->fragment.timestamp =
1411 dashstream->current_fragment_timestamp = fragment.timestamp;
1412 dashstream->current_fragment_duration = stream->fragment.duration =
1414 if (stream->demux->segment.rate < 0.0)
1415 dashstream->actual_position += fragment.duration;
1416 stream->fragment.range_start =
1417 MAX (fragment.range_start, dashstream->sidx_base_offset);
1418 stream->fragment.range_end = fragment.range_end;
1421 GST_DEBUG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
1422 GST_TIME_ARGS (dashstream->actual_position));
1427 return GST_FLOW_EOS;
1431 gst_dash_demux_index_entry_search (GstSidxBoxEntry * entry, GstClockTime * ts,
1434 GstClockTime entry_ts = entry->pts + entry->duration;
1435 if (entry_ts <= *ts)
1437 else if (entry->pts > *ts)
1443 static GstFlowReturn
1444 gst_dash_demux_stream_sidx_seek (GstDashDemuxStream * dashstream,
1445 gboolean forward, GstSeekFlags flags, GstClockTime ts,
1446 GstClockTime * final_ts)
1448 GstSidxBox *sidx = SIDX (dashstream);
1449 GstSidxBoxEntry *entry;
1450 gint idx = sidx->entries_count;
1451 GstFlowReturn ret = GST_FLOW_OK;
1453 if (sidx->entries_count == 0)
1454 return GST_FLOW_EOS;
1457 gst_util_array_binary_search (sidx->entries, sidx->entries_count,
1458 sizeof (GstSidxBoxEntry),
1459 (GCompareDataFunc) gst_dash_demux_index_entry_search,
1460 GST_SEARCH_MODE_EXACT, &ts, NULL);
1462 /* No exact match found, nothing in our index
1463 * This is usually a bug or broken stream, as the seeking code already
1464 * makes sure that we're in the correct period and segment, and only need
1465 * to find the correct place inside the segment. Allow for some rounding
1466 * errors and inaccuracies here though */
1468 GstSidxBoxEntry *last_entry = &sidx->entries[sidx->entries_count - 1];
1470 GST_WARNING_OBJECT (dashstream->parent.pad, "Couldn't find SIDX entry");
1472 if (ts < sidx->entries[0].pts
1473 && ts + 250 * GST_MSECOND >= sidx->entries[0].pts)
1474 entry = &sidx->entries[0];
1475 else if (ts >= last_entry->pts + last_entry->duration &&
1476 ts < last_entry->pts + last_entry->duration + 250 * GST_MSECOND)
1480 return GST_FLOW_EOS;
1482 idx = entry - sidx->entries;
1484 /* FIXME in reverse mode, if we are exactly at a fragment start it makes more
1485 * sense to start from the end of the previous fragment */
1486 if (!forward && idx > 0 && entry->pts == ts) {
1488 entry = &sidx->entries[idx];
1491 /* Now entry->pts <= ts < entry->pts + entry->duration, need to adjust for
1493 if ((flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST) {
1494 if (idx + 1 < sidx->entries_count
1495 && sidx->entries[idx + 1].pts - ts < ts - sidx->entries[idx].pts)
1497 } else if ((forward && (flags & GST_SEEK_FLAG_SNAP_AFTER)) || (!forward
1498 && (flags & GST_SEEK_FLAG_SNAP_BEFORE))) {
1499 if (idx + 1 < sidx->entries_count && entry->pts < ts)
1503 g_assert (sidx->entry_index < sidx->entries_count);
1505 sidx->entry_index = idx;
1506 dashstream->sidx_position = sidx->entries[idx].pts;
1509 *final_ts = dashstream->sidx_position;
1514 static GstFlowReturn
1515 gst_dash_demux_stream_seek (GstAdaptiveDemuxStream * stream, gboolean forward,
1516 GstSeekFlags flags, GstClockTime ts, GstClockTime * final_ts)
1518 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1519 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1520 gint last_index, last_repeat;
1521 gboolean is_isobmff;
1523 last_index = dashstream->active_stream->segment_index;
1524 last_repeat = dashstream->active_stream->segment_repeat_index;
1526 if (dashstream->adapter)
1527 gst_adapter_clear (dashstream->adapter);
1528 dashstream->current_offset = -1;
1529 dashstream->current_index_header_or_data = 0;
1531 dashstream->isobmff_parser.current_fourcc = 0;
1532 dashstream->isobmff_parser.current_start_offset = 0;
1533 dashstream->isobmff_parser.current_size = 0;
1535 if (dashstream->moof)
1536 gst_isoff_moof_box_free (dashstream->moof);
1537 dashstream->moof = NULL;
1538 if (dashstream->moof_sync_samples)
1539 g_array_free (dashstream->moof_sync_samples, TRUE);
1540 dashstream->moof_sync_samples = NULL;
1541 dashstream->current_sync_sample = -1;
1542 dashstream->target_time = GST_CLOCK_TIME_NONE;
1544 is_isobmff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1546 if (!gst_mpd_client_stream_seek (dashdemux->client, dashstream->active_stream,
1548 is_isobmff ? (flags & (~(GST_SEEK_FLAG_SNAP_BEFORE |
1549 GST_SEEK_FLAG_SNAP_AFTER))) : flags, ts, final_ts)) {
1550 return GST_FLOW_EOS;
1554 GstClockTime period_start, offset;
1556 period_start = gst_mpd_client_get_period_start_time (dashdemux->client);
1558 gst_mpd_client_get_stream_presentation_offset (dashdemux->client,
1561 if (G_UNLIKELY (ts < period_start))
1564 ts += offset - period_start;
1566 if (last_index != dashstream->active_stream->segment_index ||
1567 last_repeat != dashstream->active_stream->segment_repeat_index) {
1568 GST_LOG_OBJECT (stream->pad,
1569 "Segment index was changed, reset sidx parser");
1570 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1571 dashstream->sidx_base_offset = 0;
1572 dashstream->allow_sidx = TRUE;
1575 if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1576 if (gst_dash_demux_stream_sidx_seek (dashstream, forward, flags, ts,
1577 final_ts) != GST_FLOW_OK) {
1578 GST_ERROR_OBJECT (stream->pad, "Couldn't find position in sidx");
1579 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1580 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1582 dashstream->pending_seek_ts = GST_CLOCK_TIME_NONE;
1584 /* no index yet, seek when we have it */
1585 /* FIXME - the final_ts won't be correct here */
1586 dashstream->pending_seek_ts = ts;
1590 stream->discont = TRUE;
1596 gst_dash_demux_stream_has_next_sync_sample (GstAdaptiveDemuxStream * stream)
1598 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1600 if (dashstream->moof_sync_samples &&
1601 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)) {
1602 if (stream->demux->segment.rate > 0.0) {
1603 if (dashstream->current_sync_sample + 1 <
1604 dashstream->moof_sync_samples->len)
1607 if (dashstream->current_sync_sample >= 1)
1615 gst_dash_demux_stream_has_next_subfragment (GstAdaptiveDemuxStream * stream)
1617 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1618 GstSidxBox *sidx = SIDX (dashstream);
1620 if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1621 if (stream->demux->segment.rate > 0.0) {
1622 if (sidx->entry_index + 1 < sidx->entries_count)
1625 if (sidx->entry_index >= 1)
1633 gst_dash_demux_stream_advance_sync_sample (GstAdaptiveDemuxStream * stream,
1634 GstClockTime target_time)
1636 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1637 gboolean fragment_finished = FALSE;
1640 if (GST_CLOCK_TIME_IS_VALID (target_time)) {
1641 GST_LOG_OBJECT (stream->pad,
1642 "target_time:%" GST_TIME_FORMAT " fragment ts %" GST_TIME_FORMAT
1643 " average keyframe dist: %" GST_TIME_FORMAT
1644 " current keyframe dist: %" GST_TIME_FORMAT
1645 " fragment duration:%" GST_TIME_FORMAT,
1646 GST_TIME_ARGS (target_time),
1647 GST_TIME_ARGS (dashstream->current_fragment_timestamp),
1648 GST_TIME_ARGS (dashstream->keyframe_average_distance),
1649 GST_TIME_ARGS (dashstream->current_fragment_keyframe_distance),
1650 GST_TIME_ARGS (stream->fragment.duration));
1652 if (stream->demux->segment.rate > 0.0) {
1655 dashstream->current_fragment_timestamp) /
1656 dashstream->current_fragment_keyframe_distance;
1658 /* Prevent getting stuck in a loop due to rounding errors */
1659 if (idx == dashstream->current_sync_sample)
1662 GstClockTime end_time =
1663 dashstream->current_fragment_timestamp +
1664 dashstream->current_fragment_duration;
1666 if (end_time < target_time) {
1667 idx = dashstream->moof_sync_samples->len;
1671 target_time) / dashstream->current_fragment_keyframe_distance;
1672 if (idx == dashstream->moof_sync_samples->len) {
1673 dashstream->current_sync_sample = -1;
1674 fragment_finished = TRUE;
1677 idx = dashstream->moof_sync_samples->len - 1 - idx;
1680 /* Prevent getting stuck in a loop due to rounding errors */
1681 if (idx == dashstream->current_sync_sample) {
1683 dashstream->current_sync_sample = -1;
1684 fragment_finished = TRUE;
1693 GST_DEBUG_OBJECT (stream->pad,
1694 "Advancing sync sample #%d target #%d",
1695 dashstream->current_sync_sample, idx);
1697 if (idx != -1 && idx >= dashstream->moof_sync_samples->len) {
1698 dashstream->current_sync_sample = -1;
1699 fragment_finished = TRUE;
1703 if (stream->demux->segment.rate > 0.0) {
1704 /* Try to get the sync sample for the target time */
1706 dashstream->current_sync_sample = idx;
1708 dashstream->current_sync_sample++;
1709 if (dashstream->current_sync_sample >= dashstream->moof_sync_samples->len) {
1710 fragment_finished = TRUE;
1715 dashstream->current_sync_sample = idx;
1716 } else if (dashstream->current_sync_sample == -1) {
1717 dashstream->current_sync_sample = dashstream->moof_sync_samples->len - 1;
1718 } else if (dashstream->current_sync_sample == 0) {
1719 dashstream->current_sync_sample = -1;
1720 fragment_finished = TRUE;
1722 dashstream->current_sync_sample--;
1727 GST_DEBUG_OBJECT (stream->pad,
1728 "Advancing sync sample #%d fragment_finished:%d",
1729 dashstream->current_sync_sample, fragment_finished);
1731 if (!fragment_finished)
1732 stream->discont = TRUE;
1734 return !fragment_finished;
1738 gst_dash_demux_stream_advance_subfragment (GstAdaptiveDemuxStream * stream)
1740 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1742 GstSidxBox *sidx = SIDX (dashstream);
1743 gboolean fragment_finished = TRUE;
1745 if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1746 if (stream->demux->segment.rate > 0.0) {
1747 gint idx = ++sidx->entry_index;
1748 if (idx < sidx->entries_count) {
1749 fragment_finished = FALSE;
1752 if (idx == sidx->entries_count)
1753 dashstream->sidx_position =
1754 sidx->entries[idx - 1].pts + sidx->entries[idx - 1].duration;
1756 dashstream->sidx_position = sidx->entries[idx].pts;
1758 gint idx = --sidx->entry_index;
1761 fragment_finished = FALSE;
1762 dashstream->sidx_position = sidx->entries[idx].pts;
1764 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1769 GST_DEBUG_OBJECT (stream->pad, "New sidx index: %d / %d. "
1770 "Finished fragment: %d", sidx->entry_index, sidx->entries_count,
1773 return !fragment_finished;
1777 gst_dash_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
1779 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1780 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1782 if (dashstream->moof_sync_samples &&
1783 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
1784 if (gst_dash_demux_stream_has_next_sync_sample (stream))
1788 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)) {
1789 if (gst_dash_demux_stream_has_next_subfragment (stream))
1793 return gst_mpd_client_has_next_segment (dashdemux->client,
1794 dashstream->active_stream, stream->demux->segment.rate > 0.0);
1797 /* The goal here is to figure out, once we have pushed a keyframe downstream,
1798 * what the next ideal keyframe to download is.
1800 * This is done based on:
1801 * * the current internal position (i.e. actual_position)
1802 * * the reported downstream position (QoS feedback)
1803 * * the average keyframe download time (average_download_time)
1806 gst_dash_demux_stream_get_target_time (GstDashDemux * dashdemux,
1807 GstAdaptiveDemuxStream * stream, GstClockTime cur_position,
1808 GstClockTime min_skip)
1810 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1811 GstClockTime cur_running, min_running, min_position;
1812 GstClockTimeDiff diff;
1813 GstClockTime ret = cur_position;
1814 GstClockTime deadline;
1815 GstClockTime upstream_earliest_time;
1816 GstClockTime earliest_time = GST_CLOCK_TIME_NONE;
1818 g_assert (min_skip > 0);
1820 /* minimum stream position we have to skip to */
1821 if (stream->segment.rate > 0)
1822 min_position = cur_position + min_skip;
1823 else if (cur_position < min_skip)
1826 min_position = cur_position - min_skip;
1828 /* Use current clock time or the QoS earliest time, whichever is further in
1829 * the future. The QoS time is only updated on every QoS event and
1830 * especially not if e.g. a videodecoder or converter drops a frame further
1833 * We only use the times if we ever received a QoS event since the last
1834 * flush, as otherwise base_time and clock might not be correct because of a
1835 * still pre-rolling sink
1837 upstream_earliest_time =
1838 gst_adaptive_demux_get_qos_earliest_time ((GstAdaptiveDemux *) dashdemux);
1839 if (upstream_earliest_time != GST_CLOCK_TIME_NONE) {
1842 clock = gst_element_get_clock (GST_ELEMENT_CAST (dashdemux));
1845 GstClockTime base_time;
1846 GstClockTime now_time;
1848 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (dashdemux));
1849 now_time = gst_clock_get_time (clock);
1850 if (now_time > base_time)
1851 now_time -= base_time;
1855 gst_object_unref (clock);
1857 earliest_time = MAX (now_time, upstream_earliest_time);
1859 earliest_time = upstream_earliest_time;
1863 /* our current position in running time */
1865 gst_segment_to_running_time (&stream->segment, GST_FORMAT_TIME,
1868 /* the minimum position we have to skip to in running time */
1870 gst_segment_to_running_time (&stream->segment, GST_FORMAT_TIME,
1873 GST_DEBUG_OBJECT (stream->pad,
1874 "position: current %" GST_TIME_FORMAT " min next %" GST_TIME_FORMAT,
1875 GST_TIME_ARGS (cur_position), GST_TIME_ARGS (min_position));
1876 GST_DEBUG_OBJECT (stream->pad,
1877 "running time: current %" GST_TIME_FORMAT " min next %" GST_TIME_FORMAT
1878 " earliest %" GST_TIME_FORMAT, GST_TIME_ARGS (cur_running),
1879 GST_TIME_ARGS (min_running), GST_TIME_ARGS (earliest_time));
1881 /* Take configured maximum video bandwidth and framerate into account */
1883 GstClockTime min_run_dist, min_frame_dist, diff = 0;
1884 guint max_fps_n, max_fps_d;
1886 min_run_dist = min_skip / ABS (stream->segment.rate);
1888 if (dashdemux->max_video_framerate_n != 0) {
1889 max_fps_n = dashdemux->max_video_framerate_n;
1890 max_fps_d = dashdemux->max_video_framerate_d;
1892 /* more than 10 fps is not very useful if we're skipping anyway */
1897 min_frame_dist = gst_util_uint64_scale_ceil (GST_SECOND,
1898 max_fps_d, max_fps_n);
1900 GST_DEBUG_OBJECT (stream->pad,
1901 "Have max framerate %d/%d - Min dist %" GST_TIME_FORMAT
1902 ", min requested dist %" GST_TIME_FORMAT,
1903 max_fps_n, max_fps_d,
1904 GST_TIME_ARGS (min_run_dist), GST_TIME_ARGS (min_frame_dist));
1905 if (min_frame_dist > min_run_dist)
1906 diff = MAX (diff, min_frame_dist - min_run_dist);
1908 if (dashdemux->max_bitrate != 0) {
1909 guint64 max_bitrate = gst_util_uint64_scale_ceil (GST_SECOND,
1910 8 * dashstream->keyframe_average_size,
1911 dashstream->keyframe_average_distance) * ABS (stream->segment.rate);
1913 if (max_bitrate > dashdemux->max_bitrate) {
1914 min_frame_dist = gst_util_uint64_scale_ceil (GST_SECOND,
1915 8 * dashstream->keyframe_average_size,
1916 dashdemux->max_bitrate) * ABS (stream->segment.rate);
1918 GST_DEBUG_OBJECT (stream->pad,
1919 "Have max bitrate %u - Min dist %" GST_TIME_FORMAT
1920 ", min requested dist %" GST_TIME_FORMAT, dashdemux->max_bitrate,
1921 GST_TIME_ARGS (min_run_dist), GST_TIME_ARGS (min_frame_dist));
1922 if (min_frame_dist > min_run_dist)
1923 diff = MAX (diff, min_frame_dist - min_run_dist);
1928 GST_DEBUG_OBJECT (stream->pad,
1929 "Skipping further ahead by %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1930 min_running += diff;
1934 if (earliest_time == GST_CLOCK_TIME_NONE) {
1935 GstClockTime run_key_dist;
1938 dashstream->keyframe_average_distance / ABS (stream->segment.rate);
1940 /* If we don't have downstream information (such as at startup or
1941 * without live sinks), just get the next time by taking the minimum
1942 * amount we have to skip ahead
1943 * Except if it takes us longer to download */
1944 if (run_key_dist > dashstream->average_download_time)
1946 gst_segment_position_from_running_time (&stream->segment,
1947 GST_FORMAT_TIME, min_running);
1949 ret = gst_segment_position_from_running_time (&stream->segment,
1951 min_running - run_key_dist + dashstream->average_download_time);
1953 GST_DEBUG_OBJECT (stream->pad,
1954 "Advancing to %" GST_TIME_FORMAT " (was %" GST_TIME_FORMAT ")",
1955 GST_TIME_ARGS (ret), GST_TIME_ARGS (min_position));
1960 /* Figure out the difference, in running time, between where we are and
1961 * where downstream is */
1962 diff = min_running - earliest_time;
1963 GST_LOG_OBJECT (stream->pad,
1964 "min_running %" GST_TIME_FORMAT " diff %" GST_STIME_FORMAT
1965 " average_download %" GST_TIME_FORMAT, GST_TIME_ARGS (min_running),
1966 GST_STIME_ARGS (diff), GST_TIME_ARGS (dashstream->average_download_time));
1968 /* Have at least 500ms or 3 keyframes safety between current position and downstream */
1969 deadline = MAX (500 * GST_MSECOND, 3 * dashstream->average_download_time);
1971 /* The furthest away we are from the current position, the least we need to advance */
1972 if (diff < 0 || diff < deadline) {
1973 /* Force skipping (but not more than 1s ahead) */
1975 gst_segment_position_from_running_time (&stream->segment,
1976 GST_FORMAT_TIME, earliest_time + MIN (deadline, GST_SECOND));
1977 GST_DEBUG_OBJECT (stream->pad,
1978 "MUST SKIP to at least %" GST_TIME_FORMAT " (was %" GST_TIME_FORMAT ")",
1979 GST_TIME_ARGS (ret), GST_TIME_ARGS (min_position));
1980 } else if (diff < 4 * dashstream->average_download_time) {
1981 /* Go forward a bit less aggressively (and at most 1s forward) */
1982 ret = gst_segment_position_from_running_time (&stream->segment,
1983 GST_FORMAT_TIME, min_running + MIN (GST_SECOND,
1984 2 * dashstream->average_download_time));
1985 GST_DEBUG_OBJECT (stream->pad,
1986 "MUST SKIP to at least %" GST_TIME_FORMAT " (was %" GST_TIME_FORMAT ")",
1987 GST_TIME_ARGS (ret), GST_TIME_ARGS (min_position));
1989 /* Get the next position satisfying the download time */
1990 ret = gst_segment_position_from_running_time (&stream->segment,
1991 GST_FORMAT_TIME, min_running);
1992 GST_DEBUG_OBJECT (stream->pad,
1993 "Advance to %" GST_TIME_FORMAT " (was %" GST_TIME_FORMAT ")",
1994 GST_TIME_ARGS (ret), GST_TIME_ARGS (min_position));
2000 GstClockTime cur_skip =
2001 (cur_position < ret) ? ret - cur_position : cur_position - ret;
2003 if (dashstream->average_skip_size == 0) {
2004 dashstream->average_skip_size = cur_skip;
2006 dashstream->average_skip_size =
2007 (cur_skip + 3 * dashstream->average_skip_size) / 4;
2010 if (dashstream->average_skip_size >
2011 cur_skip + dashstream->keyframe_average_distance
2012 && dashstream->average_skip_size > min_skip) {
2013 if (stream->segment.rate > 0)
2014 ret = cur_position + dashstream->average_skip_size;
2015 else if (cur_position > dashstream->average_skip_size)
2016 ret = cur_position - dashstream->average_skip_size;
2025 static GstFlowReturn
2026 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream)
2028 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2029 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
2030 GstClockTime target_time = GST_CLOCK_TIME_NONE;
2031 GstClockTime previous_position;
2034 GST_DEBUG_OBJECT (stream->pad, "Advance fragment");
2036 /* Update download statistics */
2037 if (dashstream->moof_sync_samples &&
2038 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux) &&
2039 GST_CLOCK_TIME_IS_VALID (stream->last_download_time)) {
2040 if (GST_CLOCK_TIME_IS_VALID (dashstream->average_download_time)) {
2041 dashstream->average_download_time =
2042 (3 * dashstream->average_download_time +
2043 stream->last_download_time) / 4;
2045 dashstream->average_download_time = stream->last_download_time;
2048 GST_DEBUG_OBJECT (stream->pad,
2049 "Download time last: %" GST_TIME_FORMAT " average: %" GST_TIME_FORMAT,
2050 GST_TIME_ARGS (stream->last_download_time),
2051 GST_TIME_ARGS (dashstream->average_download_time));
2054 previous_position = dashstream->actual_position;
2056 /* Update internal position */
2057 if (GST_CLOCK_TIME_IS_VALID (dashstream->actual_position)) {
2059 if (dashstream->moof_sync_samples
2060 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
2061 GST_LOG_OBJECT (stream->pad, "current sync sample #%d",
2062 dashstream->current_sync_sample);
2063 if (dashstream->current_sync_sample == -1) {
2065 } else if (dashstream->current_sync_sample <
2066 dashstream->moof_sync_samples->len) {
2067 dur = dashstream->current_fragment_keyframe_distance;
2069 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2070 dashstream->sidx_position != GST_CLOCK_TIME_NONE
2071 && SIDX (dashstream)->entries) {
2072 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
2073 dur = entry->duration;
2076 dashstream->current_fragment_timestamp +
2077 dashstream->current_fragment_duration -
2078 dashstream->actual_position;
2081 } else if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2082 dashstream->sidx_position != GST_CLOCK_TIME_NONE
2083 && SIDX (dashstream)->entries) {
2084 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
2085 dur = entry->duration;
2087 dur = stream->fragment.duration;
2090 if (dashstream->moof_sync_samples
2091 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
2092 /* We just downloaded the header, we actually use the previous
2093 * target_time now as it was not used up yet */
2094 if (dashstream->current_sync_sample == -1)
2095 target_time = dashstream->target_time;
2098 gst_dash_demux_stream_get_target_time (dashdemux, stream,
2099 dashstream->actual_position, dur);
2100 dashstream->actual_position = target_time;
2102 /* Adjust based on direction */
2103 if (stream->demux->segment.rate > 0.0)
2104 dashstream->actual_position += dur;
2105 else if (dashstream->actual_position >= dur)
2106 dashstream->actual_position -= dur;
2108 dashstream->actual_position = 0;
2111 GST_DEBUG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
2112 GST_TIME_ARGS (dashstream->actual_position));
2114 dashstream->target_time = target_time;
2116 GST_DEBUG_OBJECT (stream->pad, "target_time: %" GST_TIME_FORMAT,
2117 GST_TIME_ARGS (target_time));
2119 /* If downloading only keyframes, switch to the next one or fall through */
2120 if (dashstream->moof_sync_samples &&
2121 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
2122 if (gst_dash_demux_stream_advance_sync_sample (stream, target_time))
2126 dashstream->isobmff_parser.current_fourcc = 0;
2127 dashstream->isobmff_parser.current_start_offset = 0;
2128 dashstream->isobmff_parser.current_size = 0;
2130 if (dashstream->moof)
2131 gst_isoff_moof_box_free (dashstream->moof);
2132 dashstream->moof = NULL;
2133 if (dashstream->moof_sync_samples)
2134 g_array_free (dashstream->moof_sync_samples, TRUE);
2135 dashstream->moof_sync_samples = NULL;
2136 dashstream->current_sync_sample = -1;
2138 /* Check if we just need to 'advance' to the next fragment, or if we
2139 * need to skip by more. */
2140 if (GST_CLOCK_TIME_IS_VALID (target_time)
2141 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux) &&
2142 dashstream->active_stream->mimeType == GST_STREAM_VIDEO) {
2143 GstClockTime actual_ts;
2144 GstSeekFlags flags = 0;
2146 /* Key-unit trick mode, seek to fragment containing target time
2148 * We first try seeking without snapping. As above code to skip keyframes
2149 * in the current fragment was not successful, we should go at least one
2150 * fragment ahead. Due to rounding errors we could end up at the same
2151 * fragment again here, in which case we retry seeking with the SNAP_AFTER
2154 * We don't always set that flag as we would then end up one further
2155 * fragment in the future in all good cases.
2159 gst_dash_demux_stream_seek (stream, (stream->segment.rate > 0), flags,
2160 target_time, &actual_ts);
2162 if (ret != GST_FLOW_OK) {
2163 GST_WARNING_OBJECT (stream->pad, "Failed to seek to %" GST_TIME_FORMAT,
2164 GST_TIME_ARGS (target_time));
2169 /* Retry with skipping ahead */
2170 flags |= GST_SEEK_FLAG_SNAP_AFTER;
2174 GST_DEBUG_OBJECT (stream->pad,
2175 "Skipped to %" GST_TIME_FORMAT " (wanted %" GST_TIME_FORMAT ", was %"
2176 GST_TIME_FORMAT ")", GST_TIME_ARGS (actual_ts),
2177 GST_TIME_ARGS (target_time), GST_TIME_ARGS (previous_position));
2179 if ((stream->segment.rate > 0 && actual_ts <= previous_position) ||
2180 (stream->segment.rate < 0 && actual_ts >= previous_position)) {
2185 /* Retry with forcing skipping ahead */
2186 flags |= GST_SEEK_FLAG_SNAP_AFTER;
2195 /* Normal mode, advance to the next fragment */
2196 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)) {
2197 if (gst_dash_demux_stream_advance_subfragment (stream))
2201 if (dashstream->adapter)
2202 gst_adapter_clear (dashstream->adapter);
2204 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
2205 dashstream->sidx_base_offset = 0;
2206 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
2207 dashstream->allow_sidx = TRUE;
2209 ret = gst_mpd_client_advance_segment (dashdemux->client,
2210 dashstream->active_stream, stream->demux->segment.rate > 0.0);
2216 gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream * stream,
2219 GstActiveStream *active_stream = NULL;
2220 GList *rep_list = NULL;
2222 GstAdaptiveDemux *base_demux = stream->demux;
2223 GstDashDemux *demux = GST_DASH_DEMUX_CAST (stream->demux);
2224 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2225 gboolean ret = FALSE;
2227 active_stream = dashstream->active_stream;
2228 if (active_stream == NULL) {
2232 /* In key-frame trick mode don't change bitrates */
2233 if (GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (demux)) {
2234 GST_DEBUG_OBJECT (demux, "In key-frame trick mode, not changing bitrates");
2238 /* retrieve representation list */
2239 if (active_stream->cur_adapt_set)
2240 rep_list = active_stream->cur_adapt_set->Representations;
2245 GST_DEBUG_OBJECT (stream->pad,
2246 "Trying to change to bitrate: %" G_GUINT64_FORMAT, bitrate);
2248 if (active_stream->mimeType == GST_STREAM_VIDEO && demux->max_bitrate) {
2249 bitrate = MIN (demux->max_bitrate, bitrate);
2252 /* get representation index with current max_bandwidth */
2253 if (GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (base_demux) ||
2254 ABS (base_demux->segment.rate) <= 1.0) {
2256 gst_mpd_client_get_rep_idx_with_max_bandwidth (rep_list, bitrate,
2257 demux->max_video_width, demux->max_video_height,
2258 demux->max_video_framerate_n, demux->max_video_framerate_d);
2261 gst_mpd_client_get_rep_idx_with_max_bandwidth (rep_list,
2262 bitrate / ABS (base_demux->segment.rate), demux->max_video_width,
2263 demux->max_video_height, demux->max_video_framerate_n,
2264 demux->max_video_framerate_d);
2267 /* if no representation has the required bandwidth, take the lowest one */
2268 if (new_index == -1)
2269 new_index = gst_mpd_client_get_rep_idx_with_min_bandwidth (rep_list);
2271 if (new_index != active_stream->representation_idx) {
2272 GstMPDRepresentationNode *rep = g_list_nth_data (rep_list, new_index);
2273 GST_INFO_OBJECT (demux, "Changing representation idx: %d %d %u",
2274 dashstream->index, new_index, rep->bandwidth);
2275 if (gst_mpd_client_setup_representation (demux->client, active_stream, rep)) {
2278 GST_INFO_OBJECT (demux, "Switching bitrate to %d",
2279 active_stream->cur_representation->bandwidth);
2280 caps = gst_dash_demux_get_input_caps (demux, active_stream);
2281 gst_adaptive_demux_stream_set_caps (stream, caps);
2284 /* Update the stored last representation id */
2285 g_free (dashstream->last_representation_id);
2286 dashstream->last_representation_id =
2287 g_strdup (active_stream->cur_representation->id);
2289 GST_WARNING_OBJECT (demux, "Can not switch representation, aborting...");
2294 if (gst_mpd_client_has_isoff_ondemand_profile (demux->client)
2295 && SIDX (dashstream)->entries) {
2296 /* store our current position to change to the same one in a different
2297 * representation if needed */
2298 if (SIDX (dashstream)->entry_index < SIDX (dashstream)->entries_count)
2299 dashstream->sidx_position = SIDX_CURRENT_ENTRY (dashstream)->pts;
2300 else if (SIDX (dashstream)->entry_index >=
2301 SIDX (dashstream)->entries_count)
2302 dashstream->sidx_position =
2303 SIDX_ENTRY (dashstream,
2304 SIDX (dashstream)->entries_count - 1)->pts + SIDX_ENTRY (dashstream,
2305 SIDX (dashstream)->entries_count - 1)->duration;
2307 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
2309 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
2312 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
2313 dashstream->sidx_base_offset = 0;
2314 dashstream->allow_sidx = TRUE;
2316 /* Reset ISOBMFF box parsing state */
2317 dashstream->isobmff_parser.current_fourcc = 0;
2318 dashstream->isobmff_parser.current_start_offset = 0;
2319 dashstream->isobmff_parser.current_size = 0;
2321 dashstream->current_offset = -1;
2322 dashstream->current_index_header_or_data = 0;
2324 if (dashstream->adapter)
2325 gst_adapter_clear (dashstream->adapter);
2327 if (dashstream->moof)
2328 gst_isoff_moof_box_free (dashstream->moof);
2329 dashstream->moof = NULL;
2330 if (dashstream->moof_sync_samples)
2331 g_array_free (dashstream->moof_sync_samples, TRUE);
2332 dashstream->moof_sync_samples = NULL;
2333 dashstream->current_sync_sample = -1;
2334 dashstream->target_time = GST_CLOCK_TIME_NONE;
2341 #define SEEK_UPDATES_PLAY_POSITION(r, start_type, stop_type) \
2342 ((r >= 0 && start_type != GST_SEEK_TYPE_NONE) || \
2343 (r < 0 && stop_type != GST_SEEK_TYPE_NONE))
2346 gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
2351 GstSeekType start_type, stop_type;
2354 GstClockTime current_pos, target_pos;
2355 guint current_period;
2356 GstStreamPeriod *period;
2357 GList *iter, *streams = NULL;
2358 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2359 gboolean trickmode_no_audio;
2361 gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
2364 if (!SEEK_UPDATES_PLAY_POSITION (rate, start_type, stop_type)) {
2365 /* nothing to do if we don't have to update the current position */
2369 if (demux->segment.rate > 0.0) {
2370 target_pos = (GstClockTime) start;
2372 target_pos = (GstClockTime) stop;
2375 /* select the requested Period in the Media Presentation */
2376 if (!gst_mpd_client_setup_media_presentation (dashdemux->client, target_pos,
2381 for (list = g_list_first (dashdemux->client->periods); list;
2382 list = g_list_next (list)) {
2383 period = list->data;
2384 current_pos = period->start;
2385 current_period = period->number;
2386 GST_DEBUG_OBJECT (demux, "Looking at period %u) start:%"
2387 GST_TIME_FORMAT " - duration:%"
2388 GST_TIME_FORMAT ") for position %" GST_TIME_FORMAT,
2389 current_period, GST_TIME_ARGS (current_pos),
2390 GST_TIME_ARGS (period->duration), GST_TIME_ARGS (target_pos));
2391 if (current_pos <= target_pos
2392 && target_pos <= current_pos + period->duration) {
2397 GST_WARNING_OBJECT (demux, "Could not find seeked Period");
2401 trickmode_no_audio = ! !(flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO);
2403 streams = demux->streams;
2404 if (current_period != gst_mpd_client_get_period_index (dashdemux->client)) {
2405 GST_DEBUG_OBJECT (demux, "Seeking to Period %d", current_period);
2407 /* clean old active stream list, if any */
2408 gst_mpd_client_active_streams_free (dashdemux->client);
2409 dashdemux->trickmode_no_audio = trickmode_no_audio;
2411 /* setup video, audio and subtitle streams, starting from the new Period */
2412 if (!gst_mpd_client_set_period_index (dashdemux->client, current_period)
2413 || !gst_dash_demux_setup_all_streams (dashdemux))
2415 streams = demux->next_streams;
2416 } else if (dashdemux->trickmode_no_audio != trickmode_no_audio) {
2417 /* clean old active stream list, if any */
2418 gst_mpd_client_active_streams_free (dashdemux->client);
2419 dashdemux->trickmode_no_audio = trickmode_no_audio;
2421 /* setup video, audio and subtitle streams, starting from the new Period */
2422 if (!gst_dash_demux_setup_all_streams (dashdemux))
2424 streams = demux->next_streams;
2427 /* Update the current sequence on all streams */
2428 for (iter = streams; iter; iter = g_list_next (iter)) {
2429 GstAdaptiveDemuxStream *stream = iter->data;
2430 GstDashDemuxStream *dashstream = iter->data;
2432 dashstream->average_skip_size = 0;
2433 if (gst_dash_demux_stream_seek (stream, rate >= 0, 0, target_pos,
2434 NULL) != GST_FLOW_OK)
2442 gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
2444 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2445 return MIN (dashdemux->client->mpd_root_node->minimumUpdatePeriod * 1000,
2446 SLOW_CLOCK_UPDATE_INTERVAL);
2449 static GstFlowReturn
2450 gst_dash_demux_update_manifest_data (GstAdaptiveDemux * demux,
2453 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2454 GstMPDClient *new_client = NULL;
2457 GST_DEBUG_OBJECT (demux, "Updating manifest file from URL");
2459 /* parse the manifest file */
2460 new_client = gst_mpd_client_new ();
2461 gst_mpd_client_set_uri_downloader (new_client, demux->downloader);
2462 new_client->mpd_uri = g_strdup (demux->manifest_uri);
2463 new_client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
2464 gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
2466 if (gst_mpd_client_parse (new_client, (gchar *) mapinfo.data, mapinfo.size)) {
2467 const gchar *period_id;
2470 GList *streams_iter;
2473 /* prepare the new manifest and try to transfer the stream position
2474 * status from the old manifest client */
2476 GST_DEBUG_OBJECT (demux, "Updating manifest");
2478 period_id = gst_mpd_client_get_period_id (dashdemux->client);
2479 period_idx = gst_mpd_client_get_period_index (dashdemux->client);
2481 /* setup video, audio and subtitle streams, starting from current Period */
2482 if (!gst_mpd_client_setup_media_presentation (new_client, -1,
2483 (period_id ? -1 : period_idx), period_id)) {
2488 if (!gst_mpd_client_set_period_id (new_client, period_id)) {
2489 GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
2490 gst_mpd_client_free (new_client);
2491 gst_buffer_unmap (buffer, &mapinfo);
2492 return GST_FLOW_EOS;
2495 if (!gst_mpd_client_set_period_index (new_client, period_idx)) {
2496 GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
2497 gst_mpd_client_free (new_client);
2498 gst_buffer_unmap (buffer, &mapinfo);
2499 return GST_FLOW_EOS;
2503 if (!gst_dash_demux_setup_mpdparser_streams (dashdemux, new_client)) {
2504 GST_ERROR_OBJECT (demux, "Failed to setup streams on manifest " "update");
2505 gst_mpd_client_free (new_client);
2506 gst_buffer_unmap (buffer, &mapinfo);
2507 return GST_FLOW_ERROR;
2510 /* If no pads have been exposed yet, need to use those */
2512 if (demux->streams == NULL) {
2513 if (demux->prepared_streams) {
2514 streams = demux->prepared_streams;
2517 streams = demux->streams;
2520 /* update the streams to preserve the current representation if there is one,
2521 * and to play from the next segment */
2522 for (iter = streams, streams_iter = new_client->active_streams;
2523 iter && streams_iter;
2524 iter = g_list_next (iter), streams_iter = g_list_next (streams_iter)) {
2525 GstDashDemuxStream *demux_stream = iter->data;
2526 GstActiveStream *new_stream = streams_iter->data;
2530 GST_DEBUG_OBJECT (demux,
2531 "Stream of index %d is missing from manifest update",
2532 demux_stream->index);
2533 gst_mpd_client_free (new_client);
2534 gst_buffer_unmap (buffer, &mapinfo);
2535 return GST_FLOW_EOS;
2538 if (new_stream->cur_adapt_set
2539 && demux_stream->last_representation_id != NULL) {
2541 GList *rep_list = new_stream->cur_adapt_set->Representations;
2542 GstMPDRepresentationNode *rep_node =
2543 gst_mpd_client_get_representation_with_id (rep_list,
2544 demux_stream->last_representation_id);
2545 if (rep_node != NULL) {
2546 if (gst_mpd_client_setup_representation (new_client, new_stream,
2548 GST_DEBUG_OBJECT (GST_ADAPTIVE_DEMUX_STREAM_PAD (demux_stream),
2549 "Found and set up matching representation %s in new manifest",
2550 demux_stream->last_representation_id);
2552 GST_ERROR_OBJECT (GST_ADAPTIVE_DEMUX_STREAM_PAD (demux_stream),
2553 "Failed to set up representation %s in new manifest",
2554 demux_stream->last_representation_id);
2555 gst_mpd_client_free (new_client);
2556 gst_buffer_unmap (buffer, &mapinfo);
2557 return GST_FLOW_EOS;
2560 /* If we failed to find the current representation,
2561 * then update_fragment_info() will reconfigure to the
2562 * new settings after the current download finishes */
2563 GST_WARNING_OBJECT (GST_ADAPTIVE_DEMUX_STREAM_PAD (demux_stream),
2564 "Failed to find representation %s in new manifest",
2565 demux_stream->last_representation_id);
2569 if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
2570 demux_stream->index, &ts)
2571 || gst_mpd_client_get_last_fragment_timestamp_end (dashdemux->client,
2572 demux_stream->index, &ts)) {
2574 /* Due to rounding when doing the timescale conversions it might happen
2575 * that the ts falls back to a previous segment, leading the same data
2576 * to be downloaded twice. We try to work around this by always adding
2577 * 10 microseconds to get back to the correct segment. The errors are
2578 * usually on the order of nanoseconds so it should be enough.
2581 /* _get_next_fragment_timestamp() returned relative timestamp to
2582 * corresponding period start, but _client_stream_seek expects absolute
2584 ts += gst_mpd_client_get_period_start_time (dashdemux->client);
2586 GST_DEBUG_OBJECT (GST_ADAPTIVE_DEMUX_STREAM_PAD (demux_stream),
2587 "Current position: %" GST_TIME_FORMAT ", updating to %"
2588 GST_TIME_FORMAT, GST_TIME_ARGS (ts),
2589 GST_TIME_ARGS (ts + (10 * GST_USECOND)));
2590 ts += 10 * GST_USECOND;
2591 gst_mpd_client_stream_seek (new_client, new_stream,
2592 demux->segment.rate >= 0, 0, ts, NULL);
2595 demux_stream->active_stream = new_stream;
2598 gst_mpd_client_free (dashdemux->client);
2599 dashdemux->client = new_client;
2601 GST_DEBUG_OBJECT (demux, "Manifest file successfully updated");
2602 if (dashdemux->clock_drift) {
2603 gst_dash_demux_poll_clock_drift (dashdemux);
2606 /* In most cases, this will happen if we set a wrong url in the
2607 * source element and we have received the 404 HTML response instead of
2609 GST_WARNING_OBJECT (demux, "Error parsing the manifest.");
2610 gst_mpd_client_free (new_client);
2611 gst_buffer_unmap (buffer, &mapinfo);
2612 return GST_FLOW_ERROR;
2615 gst_buffer_unmap (buffer, &mapinfo);
2621 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
2624 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
2625 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2626 GstDateTime *segmentAvailability;
2627 GstActiveStream *active_stream = dashstream->active_stream;
2629 segmentAvailability =
2630 gst_mpd_client_get_next_segment_availability_start_time
2631 (dashdemux->client, active_stream);
2633 if (segmentAvailability) {
2635 GstDateTime *cur_time;
2638 gst_date_time_new_from_g_date_time
2639 (gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST
2642 gst_mpd_client_calculate_time_difference (cur_time,
2643 segmentAvailability);
2644 gst_date_time_unref (segmentAvailability);
2645 gst_date_time_unref (cur_time);
2646 /* subtract the server's clock drift, so that if the server's
2647 time is behind our idea of UTC, we need to sleep for longer
2648 before requesting a fragment */
2650 gst_dash_demux_get_clock_compensation (dashdemux) * GST_USECOND;
2656 gst_dash_demux_has_next_period (GstAdaptiveDemux * demux)
2658 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2660 if (demux->segment.rate >= 0)
2661 return gst_mpd_client_has_next_period (dashdemux->client);
2663 return gst_mpd_client_has_previous_period (dashdemux->client);
2667 gst_dash_demux_advance_period (GstAdaptiveDemux * demux)
2669 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2671 if (demux->segment.rate >= 0) {
2672 if (!gst_mpd_client_set_period_index (dashdemux->client,
2673 gst_mpd_client_get_period_index (dashdemux->client) + 1)) {
2678 if (!gst_mpd_client_set_period_index (dashdemux->client,
2679 gst_mpd_client_get_period_index (dashdemux->client) - 1)) {
2685 gst_dash_demux_setup_all_streams (dashdemux);
2686 gst_mpd_client_seek_to_first_segment (dashdemux->client);
2690 _gst_buffer_split (GstBuffer * buffer, gint offset, gsize size)
2692 GstBuffer *newbuf = gst_buffer_copy_region (buffer,
2693 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_META
2694 | GST_BUFFER_COPY_MEMORY, offset, size == -1 ? size : size - offset);
2696 gst_buffer_resize (buffer, 0, offset);
2702 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
2703 GstAdaptiveDemuxStream * stream)
2705 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2706 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2708 GST_LOG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
2709 GST_TIME_ARGS (dashstream->actual_position));
2711 dashstream->current_index_header_or_data = 0;
2712 dashstream->current_offset = -1;
2714 /* We need to mark every first buffer of a key unit as discont,
2715 * and also every first buffer of a moov and moof. This ensures
2716 * that qtdemux takes note of our buffer offsets for each of those
2717 * buffers instead of keeping track of them itself from the first
2718 * buffer. We need offsets to be consistent between moof and mdat
2720 if (dashstream->is_isobmff && dashdemux->allow_trickmode_key_units
2721 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (demux)
2722 && dashstream->active_stream->mimeType == GST_STREAM_VIDEO)
2723 stream->discont = TRUE;
2728 static GstFlowReturn
2729 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
2730 GstAdaptiveDemuxStream * stream)
2732 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2733 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2735 /* We need to mark every first buffer of a key unit as discont,
2736 * and also every first buffer of a moov and moof. This ensures
2737 * that qtdemux takes note of our buffer offsets for each of those
2738 * buffers instead of keeping track of them itself from the first
2739 * buffer. We need offsets to be consistent between moof and mdat
2741 if (dashstream->is_isobmff && dashdemux->allow_trickmode_key_units
2742 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (demux)
2743 && dashstream->active_stream->mimeType == GST_STREAM_VIDEO)
2744 stream->discont = TRUE;
2746 /* Only handle fragment advancing specifically for SIDX if we're not
2747 * in key unit mode */
2748 if (!(dashstream->moof_sync_samples
2749 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux))
2750 && gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)
2751 && dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
2752 /* fragment is advanced on data_received when byte limits are reached */
2753 if (dashstream->pending_seek_ts != GST_CLOCK_TIME_NONE) {
2754 if (SIDX (dashstream)->entry_index < SIDX (dashstream)->entries_count)
2756 } else if (gst_dash_demux_stream_has_next_subfragment (stream)) {
2761 if (G_UNLIKELY (stream->downloading_header || stream->downloading_index))
2764 return gst_adaptive_demux_stream_advance_fragment (demux, stream,
2765 stream->fragment.duration);
2769 gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream * stream)
2771 GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
2772 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2774 /* We're chunked downloading for ISOBMFF in KEY_UNITS mode for the actual
2775 * fragment until we parsed the moof and arrived at the mdat. 8192 is a
2776 * random guess for the moof size
2778 if (dashstream->is_isobmff
2779 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)
2780 && dashstream->active_stream->mimeType == GST_STREAM_VIDEO
2781 && !stream->downloading_header && !stream->downloading_index
2782 && dashdemux->allow_trickmode_key_units) {
2783 if (dashstream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT) {
2784 /* Need to download the moof first to know anything */
2786 stream->fragment.chunk_size = 8192;
2787 /* Do we have the first fourcc already or are we in the middle */
2788 if (dashstream->isobmff_parser.current_fourcc == 0) {
2789 stream->fragment.chunk_size += dashstream->moof_average_size;
2790 if (dashstream->first_sync_sample_always_after_moof) {
2791 gboolean first = FALSE;
2792 /* Check if we'll really need that first sample */
2793 if (GST_CLOCK_TIME_IS_VALID (dashstream->target_time)) {
2795 ((dashstream->target_time -
2796 dashstream->current_fragment_timestamp) /
2797 dashstream->keyframe_average_distance) == 0 ? TRUE : FALSE;
2798 } else if (stream->segment.rate > 0) {
2803 stream->fragment.chunk_size += dashstream->keyframe_average_size;
2807 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2808 dashstream->sidx_parser.sidx.entries) {
2809 guint64 sidx_start_offset =
2810 dashstream->sidx_base_offset +
2811 SIDX_CURRENT_ENTRY (dashstream)->offset;
2812 guint64 sidx_end_offset =
2813 sidx_start_offset + SIDX_CURRENT_ENTRY (dashstream)->size;
2814 guint64 downloaded_end_offset;
2816 if (dashstream->current_offset == GST_CLOCK_TIME_NONE) {
2817 downloaded_end_offset = sidx_start_offset;
2819 downloaded_end_offset =
2820 dashstream->current_offset +
2821 gst_adapter_available (dashstream->adapter);
2824 downloaded_end_offset = MAX (downloaded_end_offset, sidx_start_offset);
2826 if (stream->fragment.chunk_size +
2827 downloaded_end_offset > sidx_end_offset) {
2828 stream->fragment.chunk_size = sidx_end_offset - downloaded_end_offset;
2831 } else if (dashstream->moof && dashstream->moof_sync_samples) {
2832 /* Have the moof, either we're done now or we want to download the
2833 * directly following sync sample */
2834 if (dashstream->first_sync_sample_after_moof
2835 && dashstream->current_sync_sample == 0) {
2836 GstDashStreamSyncSample *sync_sample =
2837 &g_array_index (dashstream->moof_sync_samples,
2838 GstDashStreamSyncSample, 0);
2839 guint64 end_offset = sync_sample->end_offset + 1;
2840 guint64 downloaded_end_offset;
2842 downloaded_end_offset =
2843 dashstream->current_offset +
2844 gst_adapter_available (dashstream->adapter);
2846 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2847 dashstream->sidx_parser.sidx.entries) {
2848 guint64 sidx_end_offset =
2849 dashstream->sidx_base_offset +
2850 SIDX_CURRENT_ENTRY (dashstream)->offset +
2851 SIDX_CURRENT_ENTRY (dashstream)->size;
2853 if (end_offset > sidx_end_offset) {
2854 end_offset = sidx_end_offset;
2858 if (downloaded_end_offset < end_offset) {
2859 stream->fragment.chunk_size = end_offset - downloaded_end_offset;
2861 stream->fragment.chunk_size = 0;
2864 stream->fragment.chunk_size = 0;
2867 /* Have moof but can't do key-units mode, just download until the end */
2868 stream->fragment.chunk_size = -1;
2871 /* We might've decided that we can't allow key-unit only
2872 * trickmodes while doing chunked downloading. In that case
2873 * just download from here to the end now */
2874 if (dashstream->moof
2875 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)) {
2876 stream->fragment.chunk_size = -1;
2878 stream->fragment.chunk_size = 0;
2882 return stream->fragment.chunk_size != 0;
2885 static GstFlowReturn
2886 gst_dash_demux_parse_isobmff (GstAdaptiveDemux * demux,
2887 GstDashDemuxStream * dash_stream, gboolean * sidx_seek_needed)
2889 GstAdaptiveDemuxStream *stream = (GstAdaptiveDemuxStream *) dash_stream;
2890 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2894 GstByteReader reader;
2897 guint64 size, buffer_offset;
2899 *sidx_seek_needed = FALSE;
2901 /* This must not be called when we're in the mdat. We only look at the mdat
2902 * header and then stop parsing the boxes as we're only interested in the
2903 * metadata! Handling mdat is the job of the surrounding code, as well as
2904 * stopping or starting the next fragment when mdat is over (=> sidx)
2906 g_assert (dash_stream->isobmff_parser.current_fourcc !=
2907 GST_ISOFF_FOURCC_MDAT);
2909 available = gst_adapter_available (dash_stream->adapter);
2910 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2911 buffer_offset = dash_stream->current_offset;
2913 /* Always at the start of a box here */
2914 g_assert (dash_stream->isobmff_parser.current_size == 0);
2916 /* At the start of a box => Parse it */
2917 gst_buffer_map (buffer, &map, GST_MAP_READ);
2918 gst_byte_reader_init (&reader, map.data, map.size);
2920 /* While there are more boxes left to parse ... */
2921 dash_stream->isobmff_parser.current_start_offset = buffer_offset;
2923 dash_stream->isobmff_parser.current_fourcc = 0;
2924 dash_stream->isobmff_parser.current_size = 0;
2926 if (!gst_isoff_parse_box_header (&reader, &fourcc, NULL, &header_size,
2931 dash_stream->isobmff_parser.current_fourcc = fourcc;
2933 /* We assume this is mdat, anything else with "size until end"
2934 * does not seem to make sense */
2935 g_assert (dash_stream->isobmff_parser.current_fourcc ==
2936 GST_ISOFF_FOURCC_MDAT);
2937 dash_stream->isobmff_parser.current_size = -1;
2941 dash_stream->isobmff_parser.current_size = size;
2943 /* Do we have the complete box or are at MDAT */
2944 if (gst_byte_reader_get_remaining (&reader) < size - header_size ||
2945 dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MDAT) {
2946 /* Reset byte reader to the beginning of the box */
2947 gst_byte_reader_set_pos (&reader,
2948 gst_byte_reader_get_pos (&reader) - header_size);
2952 GST_LOG_OBJECT (stream->pad,
2953 "box %" GST_FOURCC_FORMAT " at offset %" G_GUINT64_FORMAT " size %"
2954 G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc),
2955 dash_stream->isobmff_parser.current_start_offset, size);
2957 if (dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MOOF) {
2958 GstByteReader sub_reader;
2960 /* Only allow SIDX before the very first moof */
2961 dash_stream->allow_sidx = FALSE;
2963 g_assert (dash_stream->moof == NULL);
2964 g_assert (dash_stream->moof_sync_samples == NULL);
2965 gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
2966 dash_stream->moof = gst_isoff_moof_box_parse (&sub_reader);
2967 dash_stream->moof_offset =
2968 dash_stream->isobmff_parser.current_start_offset;
2969 dash_stream->moof_size = size;
2970 dash_stream->current_sync_sample = -1;
2972 if (dash_stream->moof_average_size) {
2973 if (dash_stream->moof_average_size < size)
2974 dash_stream->moof_average_size =
2975 (size * 3 + dash_stream->moof_average_size) / 4;
2977 dash_stream->moof_average_size =
2978 (size + dash_stream->moof_average_size + 3) / 4;
2980 dash_stream->moof_average_size = size;
2982 } else if (dash_stream->isobmff_parser.current_fourcc ==
2983 GST_ISOFF_FOURCC_SIDX &&
2984 gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2985 dash_stream->allow_sidx) {
2986 GstByteReader sub_reader;
2987 GstIsoffParserResult res;
2990 dash_stream->sidx_base_offset =
2991 dash_stream->isobmff_parser.current_start_offset + size;
2992 dash_stream->allow_sidx = FALSE;
2994 gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
2997 gst_isoff_sidx_parser_parse (&dash_stream->sidx_parser, &sub_reader,
3000 if (res == GST_ISOFF_PARSER_DONE) {
3001 guint64 first_offset = dash_stream->sidx_parser.sidx.first_offset;
3002 GstSidxBox *sidx = SIDX (dash_stream);
3006 GST_LOG_OBJECT (stream->pad,
3007 "non-zero sidx first offset %" G_GUINT64_FORMAT, first_offset);
3008 dash_stream->sidx_base_offset += first_offset;
3011 for (i = 0; i < sidx->entries_count; i++) {
3012 GstSidxBoxEntry *entry = &sidx->entries[i];
3014 if (entry->ref_type != 0) {
3015 GST_FIXME_OBJECT (stream->pad, "SIDX ref_type 1 not supported yet");
3016 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
3017 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
3022 /* We might've cleared the index above */
3023 if (sidx->entries_count > 0) {
3024 if (GST_CLOCK_TIME_IS_VALID (dash_stream->pending_seek_ts)) {
3025 /* FIXME, preserve seek flags */
3026 if (gst_dash_demux_stream_sidx_seek (dash_stream,
3027 demux->segment.rate >= 0, 0, dash_stream->pending_seek_ts,
3028 NULL) != GST_FLOW_OK) {
3029 GST_ERROR_OBJECT (stream->pad, "Couldn't find position in sidx");
3030 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
3031 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
3033 dash_stream->pending_seek_ts = GST_CLOCK_TIME_NONE;
3036 if (dash_stream->sidx_position == GST_CLOCK_TIME_NONE) {
3037 SIDX (dash_stream)->entry_index = 0;
3039 if (gst_dash_demux_stream_sidx_seek (dash_stream,
3040 demux->segment.rate >= 0, GST_SEEK_FLAG_SNAP_BEFORE,
3041 dash_stream->sidx_position, NULL) != GST_FLOW_OK) {
3042 GST_ERROR_OBJECT (stream->pad,
3043 "Couldn't find position in sidx");
3044 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
3045 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
3048 dash_stream->sidx_position =
3049 SIDX (dash_stream)->entries[SIDX (dash_stream)->entry_index].
3054 if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED &&
3055 SIDX (dash_stream)->entry_index != 0) {
3056 /* Need to jump to the requested SIDX entry. Push everything up to
3057 * the SIDX box below and let the caller handle everything else */
3058 *sidx_seek_needed = TRUE;
3063 gst_byte_reader_skip (&reader, size - header_size);
3066 dash_stream->isobmff_parser.current_fourcc = 0;
3067 dash_stream->isobmff_parser.current_start_offset += size;
3068 dash_stream->isobmff_parser.current_size = 0;
3069 } while (gst_byte_reader_get_remaining (&reader) > 0);
3071 gst_buffer_unmap (buffer, &map);
3073 /* mdat? Push all we have and wait for it to be over */
3074 if (dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MDAT) {
3077 GST_LOG_OBJECT (stream->pad,
3078 "box %" GST_FOURCC_FORMAT " at offset %" G_GUINT64_FORMAT " size %"
3079 G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc),
3080 dash_stream->isobmff_parser.current_start_offset,
3081 dash_stream->isobmff_parser.current_size);
3083 /* At mdat. Move the start of the mdat to the adapter and have everything
3084 * else be pushed. We parsed all header boxes at this point and are not
3085 * supposed to be called again until the next moof */
3086 pending = _gst_buffer_split (buffer, gst_byte_reader_get_pos (&reader), -1);
3087 gst_adapter_push (dash_stream->adapter, pending);
3088 dash_stream->current_offset += gst_byte_reader_get_pos (&reader);
3089 dash_stream->isobmff_parser.current_size = 0;
3091 GST_BUFFER_OFFSET (buffer) = buffer_offset;
3092 GST_BUFFER_OFFSET_END (buffer) =
3093 buffer_offset + gst_buffer_get_size (buffer);
3094 return gst_adaptive_demux_stream_push_buffer (stream, buffer);
3095 } else if (gst_byte_reader_get_pos (&reader) != 0) {
3098 /* Multiple complete boxes and no mdat? Push them and keep the remainder,
3099 * which is the start of the next box if any remainder */
3101 pending = _gst_buffer_split (buffer, gst_byte_reader_get_pos (&reader), -1);
3102 gst_adapter_push (dash_stream->adapter, pending);
3103 dash_stream->current_offset += gst_byte_reader_get_pos (&reader);
3104 dash_stream->isobmff_parser.current_size = 0;
3106 GST_BUFFER_OFFSET (buffer) = buffer_offset;
3107 GST_BUFFER_OFFSET_END (buffer) =
3108 buffer_offset + gst_buffer_get_size (buffer);
3109 return gst_adaptive_demux_stream_push_buffer (stream, buffer);
3112 /* Not even a single complete, non-mdat box, wait */
3113 dash_stream->isobmff_parser.current_size = 0;
3114 gst_adapter_push (dash_stream->adapter, buffer);
3120 gst_dash_demux_find_sync_samples (GstAdaptiveDemux * demux,
3121 GstAdaptiveDemuxStream * stream)
3123 GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
3124 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3126 guint32 track_id = 0;
3127 guint64 prev_traf_end;
3128 gboolean trex_sample_flags = FALSE;
3130 if (!dash_stream->moof) {
3131 dashdemux->allow_trickmode_key_units = FALSE;
3135 dash_stream->current_sync_sample = -1;
3136 dash_stream->moof_sync_samples =
3137 g_array_new (FALSE, FALSE, sizeof (GstDashStreamSyncSample));
3139 prev_traf_end = dash_stream->moof_offset;
3141 /* generate table of keyframes and offsets */
3142 for (i = 0; i < dash_stream->moof->traf->len; i++) {
3143 GstTrafBox *traf = &g_array_index (dash_stream->moof->traf, GstTrafBox, i);
3144 guint64 traf_offset = 0, prev_trun_end;
3148 track_id = traf->tfhd.track_id;
3149 } else if (track_id != traf->tfhd.track_id) {
3150 GST_ERROR_OBJECT (stream->pad,
3151 "moof with trafs of different track ids (%u != %u)", track_id,
3152 traf->tfhd.track_id);
3153 g_array_free (dash_stream->moof_sync_samples, TRUE);
3154 dash_stream->moof_sync_samples = NULL;
3155 dashdemux->allow_trickmode_key_units = FALSE;
3159 if (traf->tfhd.flags & GST_TFHD_FLAGS_BASE_DATA_OFFSET_PRESENT) {
3160 traf_offset = traf->tfhd.base_data_offset;
3161 } else if (traf->tfhd.flags & GST_TFHD_FLAGS_DEFAULT_BASE_IS_MOOF) {
3162 traf_offset = dash_stream->moof_offset;
3164 traf_offset = prev_traf_end;
3167 prev_trun_end = traf_offset;
3169 for (j = 0; j < traf->trun->len; j++) {
3170 GstTrunBox *trun = &g_array_index (traf->trun, GstTrunBox, j);
3171 guint64 trun_offset, prev_sample_end;
3174 if (trun->flags & GST_TRUN_FLAGS_DATA_OFFSET_PRESENT) {
3175 trun_offset = traf_offset + trun->data_offset;
3177 trun_offset = prev_trun_end;
3180 prev_sample_end = trun_offset;
3181 for (k = 0; k < trun->samples->len; k++) {
3182 GstTrunSample *sample =
3183 &g_array_index (trun->samples, GstTrunSample, k);
3184 guint64 sample_offset;
3185 guint32 sample_flags;
3187 guint32 sample_duration;
3190 sample_offset = prev_sample_end;
3192 if (trun->flags & GST_TRUN_FLAGS_SAMPLE_FLAGS_PRESENT) {
3193 sample_flags = sample->sample_flags;
3194 } else if ((trun->flags & GST_TRUN_FLAGS_FIRST_SAMPLE_FLAGS_PRESENT)
3196 sample_flags = trun->first_sample_flags;
3197 } else if (traf->tfhd.
3198 flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_FLAGS_PRESENT) {
3199 sample_flags = traf->tfhd.default_sample_flags;
3201 trex_sample_flags = TRUE;
3206 if (trun->flags & GST_TRUN_FLAGS_SAMPLE_DURATION_PRESENT) {
3207 sample_duration = sample->sample_duration;
3208 } else if (traf->tfhd.
3209 flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_DURATION_PRESENT) {
3210 sample_duration = traf->tfhd.default_sample_duration;
3212 GST_FIXME_OBJECT (stream->pad,
3213 "Sample duration given by trex - can't download only keyframes");
3214 g_array_free (dash_stream->moof_sync_samples, TRUE);
3215 dash_stream->moof_sync_samples = NULL;
3220 if (trun->flags & GST_TRUN_FLAGS_SAMPLE_SIZE_PRESENT) {
3221 prev_sample_end += sample->sample_size;
3222 } else if (traf->tfhd.
3223 flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_SIZE_PRESENT) {
3224 prev_sample_end += traf->tfhd.default_sample_size;
3226 GST_FIXME_OBJECT (stream->pad,
3227 "Sample size given by trex - can't download only keyframes");
3228 g_array_free (dash_stream->moof_sync_samples, TRUE);
3229 dash_stream->moof_sync_samples = NULL;
3230 dashdemux->allow_trickmode_key_units = FALSE;
3234 /* Non-non-sync sample aka sync sample */
3235 if (!GST_ISOFF_SAMPLE_FLAGS_SAMPLE_IS_NON_SYNC_SAMPLE (sample_flags) ||
3236 GST_ISOFF_SAMPLE_FLAGS_SAMPLE_DEPENDS_ON (sample_flags) == 2) {
3237 GstDashStreamSyncSample sync_sample =
3238 { sample_offset, prev_sample_end - 1 };
3239 /* TODO: need timestamps so we can decide to download or not */
3240 g_array_append_val (dash_stream->moof_sync_samples, sync_sample);
3244 prev_trun_end = prev_sample_end;
3247 prev_traf_end = prev_trun_end;
3250 if (trex_sample_flags) {
3251 if (dash_stream->moof_sync_samples->len > 0) {
3252 GST_LOG_OBJECT (stream->pad,
3253 "Some sample flags given by trex but still found sync samples");
3255 GST_FIXME_OBJECT (stream->pad,
3256 "Sample flags given by trex - can't download only keyframes");
3257 g_array_free (dash_stream->moof_sync_samples, TRUE);
3258 dash_stream->moof_sync_samples = NULL;
3259 dashdemux->allow_trickmode_key_units = FALSE;
3264 if (dash_stream->moof_sync_samples->len == 0) {
3265 GST_LOG_OBJECT (stream->pad, "No sync samples found in fragment");
3266 g_array_free (dash_stream->moof_sync_samples, TRUE);
3267 dash_stream->moof_sync_samples = NULL;
3268 dashdemux->allow_trickmode_key_units = FALSE;
3273 GstDashStreamSyncSample *sync_sample;
3276 GstClockTime current_keyframe_distance;
3278 for (i = 0; i < dash_stream->moof_sync_samples->len; i++) {
3280 &g_array_index (dash_stream->moof_sync_samples,
3281 GstDashStreamSyncSample, i);
3282 size = sync_sample->end_offset + 1 - sync_sample->start_offset;
3284 if (dash_stream->keyframe_average_size) {
3285 /* Over-estimate the keyframe size */
3286 if (dash_stream->keyframe_average_size < size)
3287 dash_stream->keyframe_average_size =
3288 (size * 3 + dash_stream->keyframe_average_size) / 4;
3290 dash_stream->keyframe_average_size =
3291 (size + dash_stream->keyframe_average_size * 3) / 4;
3293 dash_stream->keyframe_average_size = size;
3297 if (dash_stream->moof_offset + dash_stream->moof_size + 8 <
3298 sync_sample->start_offset) {
3299 dash_stream->first_sync_sample_after_moof = FALSE;
3300 dash_stream->first_sync_sample_always_after_moof = FALSE;
3302 dash_stream->first_sync_sample_after_moof =
3303 (dash_stream->moof_sync_samples->len == 1
3304 || demux->segment.rate > 0.0);
3309 g_assert (stream->fragment.duration != 0);
3310 g_assert (stream->fragment.duration != GST_CLOCK_TIME_NONE);
3312 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)
3313 && dash_stream->sidx_position != GST_CLOCK_TIME_NONE
3314 && SIDX (dash_stream)->entries) {
3315 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dash_stream);
3316 current_keyframe_distance =
3317 entry->duration / dash_stream->moof_sync_samples->len;
3319 current_keyframe_distance =
3320 stream->fragment.duration / dash_stream->moof_sync_samples->len;
3322 dash_stream->current_fragment_keyframe_distance = current_keyframe_distance;
3324 if (dash_stream->keyframe_average_distance) {
3325 /* Under-estimate the keyframe distance */
3326 if (dash_stream->keyframe_average_distance > current_keyframe_distance)
3327 dash_stream->keyframe_average_distance =
3328 (dash_stream->keyframe_average_distance * 3 +
3329 current_keyframe_distance) / 4;
3331 dash_stream->keyframe_average_distance =
3332 (dash_stream->keyframe_average_distance +
3333 current_keyframe_distance * 3) / 4;
3335 dash_stream->keyframe_average_distance = current_keyframe_distance;
3338 GST_DEBUG_OBJECT (stream->pad,
3339 "average keyframe sample size: %" G_GUINT64_FORMAT,
3340 dash_stream->keyframe_average_size);
3341 GST_DEBUG_OBJECT (stream->pad,
3342 "average keyframe distance: %" GST_TIME_FORMAT " (%" GST_TIME_FORMAT
3343 ")", GST_TIME_ARGS (dash_stream->keyframe_average_distance),
3344 GST_TIME_ARGS (current_keyframe_distance));
3345 GST_DEBUG_OBJECT (stream->pad, "first sync sample after moof: %d",
3346 dash_stream->first_sync_sample_after_moof);
3353 static GstFlowReturn
3354 gst_dash_demux_handle_isobmff (GstAdaptiveDemux * demux,
3355 GstAdaptiveDemuxStream * stream)
3357 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3358 GstFlowReturn ret = GST_FLOW_OK;
3360 gboolean sidx_advance = FALSE;
3362 /* We parse all ISOBMFF boxes of a (sub)fragment until the mdat. This covers
3363 * at least moov, moof and sidx boxes. Once mdat is received we just output
3364 * everything until the next (sub)fragment */
3365 if (dash_stream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT) {
3366 gboolean sidx_seek_needed = FALSE;
3368 ret = gst_dash_demux_parse_isobmff (demux, dash_stream, &sidx_seek_needed);
3369 if (ret != GST_FLOW_OK)
3372 /* Go to selected segment if needed here */
3373 if (sidx_seek_needed && !stream->downloading_index)
3374 return GST_ADAPTIVE_DEMUX_FLOW_END_OF_FRAGMENT;
3376 /* No mdat yet, let's get called again with the next boxes */
3377 if (dash_stream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT)
3380 /* Here we end up only if we're right at the mdat start */
3382 /* Jump to the next sync sample. As we're doing chunked downloading
3383 * here, just drop data until our chunk is over so we can reuse the
3384 * HTTP connection instead of having to create a new one or
3385 * reuse the data if the sync sample follows the moof */
3386 if (dash_stream->active_stream->mimeType == GST_STREAM_VIDEO
3387 && gst_dash_demux_find_sync_samples (demux, stream) &&
3388 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)) {
3391 if (GST_CLOCK_TIME_IS_VALID (dash_stream->target_time)) {
3393 (dash_stream->target_time -
3394 dash_stream->current_fragment_timestamp) /
3395 dash_stream->current_fragment_keyframe_distance;
3396 } else if (stream->segment.rate > 0) {
3400 GST_DEBUG_OBJECT (stream->pad, "target %" GST_TIME_FORMAT " idx %d",
3401 GST_TIME_ARGS (dash_stream->target_time), idx);
3402 /* Figure out target time */
3404 if (dash_stream->first_sync_sample_after_moof && idx == 0) {
3405 /* If we're here, don't throw away data but collect sync
3406 * sample while we're at it below. We're doing chunked
3407 * downloading so might need to adjust the next chunk size for
3409 dash_stream->current_sync_sample = 0;
3410 GST_DEBUG_OBJECT (stream->pad, "Using first keyframe after header");
3414 if (gst_adapter_available (dash_stream->adapter) == 0)
3417 /* We have some data from the mdat available in the adapter, handle it
3418 * below in the push code */
3420 /* Somewhere in the middle of the mdat */
3424 if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
3425 guint64 sidx_end_offset =
3426 dash_stream->sidx_base_offset +
3427 SIDX_CURRENT_ENTRY (dash_stream)->offset +
3428 SIDX_CURRENT_ENTRY (dash_stream)->size;
3429 gboolean has_next = gst_dash_demux_stream_has_next_subfragment (stream);
3432 /* Need to handle everything in the adapter according to the parsed SIDX
3433 * and advance subsegments accordingly */
3435 available = gst_adapter_available (dash_stream->adapter);
3436 if (dash_stream->current_offset + available < sidx_end_offset) {
3437 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
3439 if (!has_next && sidx_end_offset <= dash_stream->current_offset) {
3440 /* Drain all bytes, since there might be trailing bytes at the end of subfragment */
3441 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
3443 if (sidx_end_offset <= dash_stream->current_offset) {
3444 /* This means a corrupted stream or a bug: ignoring bugs, it
3445 * should only happen if the SIDX index is corrupt */
3446 GST_ERROR_OBJECT (stream->pad, "Invalid SIDX state");
3447 gst_adapter_clear (dash_stream->adapter);
3448 return GST_FLOW_ERROR;
3451 gst_adapter_take_buffer (dash_stream->adapter,
3452 sidx_end_offset - dash_stream->current_offset);
3453 sidx_advance = TRUE;
3458 /* Take it all and handle it further below */
3460 gst_adapter_take_buffer (dash_stream->adapter,
3461 gst_adapter_available (dash_stream->adapter));
3463 /* Attention: All code paths below need to update dash_stream->current_offset */
3466 /* We're actually running in key-units trick mode */
3467 if (dash_stream->active_stream->mimeType == GST_STREAM_VIDEO
3468 && dash_stream->moof_sync_samples
3469 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)) {
3470 if (dash_stream->current_sync_sample == -1) {
3471 /* We're doing chunked downloading and wait for finishing the current
3472 * chunk so we can jump to the first keyframe */
3473 dash_stream->current_offset += gst_buffer_get_size (buffer);
3474 gst_buffer_unref (buffer);
3477 GstDashStreamSyncSample *sync_sample =
3478 &g_array_index (dash_stream->moof_sync_samples,
3479 GstDashStreamSyncSample, dash_stream->current_sync_sample);
3480 guint64 end_offset =
3481 dash_stream->current_offset + gst_buffer_get_size (buffer);
3483 /* Make sure to not download too much, this should only happen for
3484 * the very first keyframe if it follows the moof */
3485 if (dash_stream->current_offset >= sync_sample->end_offset + 1) {
3486 dash_stream->current_offset += gst_buffer_get_size (buffer);
3487 gst_buffer_unref (buffer);
3489 } else if (end_offset > sync_sample->end_offset + 1) {
3491 sync_sample->end_offset + 1 - dash_stream->current_offset;
3492 GstBuffer *sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0,
3494 gst_buffer_unref (buffer);
3500 GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
3501 dash_stream->current_offset += gst_buffer_get_size (buffer);
3502 GST_BUFFER_OFFSET_END (buffer) = dash_stream->current_offset;
3504 ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
3505 if (ret != GST_FLOW_OK)
3510 gst_adaptive_demux_stream_advance_fragment (demux, stream,
3511 SIDX_CURRENT_ENTRY (dash_stream)->duration);
3512 if (ret != GST_FLOW_OK)
3515 /* If we still have data available, recurse and use it up if possible */
3516 if (gst_adapter_available (dash_stream->adapter) > 0)
3517 return gst_dash_demux_handle_isobmff (demux, stream);
3523 static GstFlowReturn
3524 gst_dash_demux_data_received (GstAdaptiveDemux * demux,
3525 GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
3527 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3528 GstFlowReturn ret = GST_FLOW_OK;
3529 guint index_header_or_data;
3531 if (stream->downloading_index)
3532 index_header_or_data = 1;
3533 else if (stream->downloading_header)
3534 index_header_or_data = 2;
3536 index_header_or_data = 3;
3538 if (dash_stream->current_index_header_or_data != index_header_or_data) {
3539 /* Clear pending data */
3540 if (gst_adapter_available (dash_stream->adapter) != 0)
3541 GST_ERROR_OBJECT (stream->pad,
3542 "Had pending SIDX data after switch between index/header/data");
3543 gst_adapter_clear (dash_stream->adapter);
3544 dash_stream->current_index_header_or_data = index_header_or_data;
3545 dash_stream->current_offset = -1;
3548 if (dash_stream->current_offset == -1)
3549 dash_stream->current_offset =
3550 GST_BUFFER_OFFSET_IS_VALID (buffer) ? GST_BUFFER_OFFSET (buffer) : 0;
3552 gst_adapter_push (dash_stream->adapter, buffer);
3555 if (dash_stream->is_isobmff || stream->downloading_index) {
3556 /* SIDX index is also ISOBMMF */
3557 ret = gst_dash_demux_handle_isobmff (demux, stream);
3558 } else if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
3561 /* Not ISOBMFF but had a SIDX index. Does this even exist or work? */
3562 while (ret == GST_FLOW_OK
3563 && ((available = gst_adapter_available (dash_stream->adapter)) > 0)) {
3564 gboolean advance = FALSE;
3565 guint64 sidx_end_offset =
3566 dash_stream->sidx_base_offset +
3567 SIDX_CURRENT_ENTRY (dash_stream)->offset +
3568 SIDX_CURRENT_ENTRY (dash_stream)->size;
3569 gboolean has_next = gst_dash_demux_stream_has_next_subfragment (stream);
3571 if (dash_stream->current_offset + available < sidx_end_offset) {
3572 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
3574 if (!has_next && sidx_end_offset <= dash_stream->current_offset) {
3575 /* Drain all bytes, since there might be trailing bytes at the end of subfragment */
3576 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
3578 if (sidx_end_offset <= dash_stream->current_offset) {
3579 /* This means a corrupted stream or a bug: ignoring bugs, it
3580 * should only happen if the SIDX index is corrupt */
3581 GST_ERROR_OBJECT (stream->pad, "Invalid SIDX state");
3582 gst_adapter_clear (dash_stream->adapter);
3583 ret = GST_FLOW_ERROR;
3587 gst_adapter_take_buffer (dash_stream->adapter,
3588 sidx_end_offset - dash_stream->current_offset);
3594 GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
3595 GST_BUFFER_OFFSET_END (buffer) =
3596 GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer);
3597 dash_stream->current_offset = GST_BUFFER_OFFSET_END (buffer);
3599 ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
3603 GstFlowReturn new_ret;
3605 gst_adaptive_demux_stream_advance_fragment (demux, stream,
3606 SIDX_CURRENT_ENTRY (dash_stream)->duration);
3608 /* only overwrite if it was OK before */
3609 if (ret == GST_FLOW_OK)
3617 /* this should be the main header, just push it all */
3618 buffer = gst_adapter_take_buffer (dash_stream->adapter,
3619 gst_adapter_available (dash_stream->adapter));
3621 GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
3622 GST_BUFFER_OFFSET_END (buffer) =
3623 GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer);
3624 dash_stream->current_offset = GST_BUFFER_OFFSET_END (buffer);
3626 ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
3633 gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream)
3635 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3637 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
3638 if (dash_stream->adapter)
3639 g_object_unref (dash_stream->adapter);
3640 if (dash_stream->moof)
3641 gst_isoff_moof_box_free (dash_stream->moof);
3642 if (dash_stream->moof_sync_samples)
3643 g_array_free (dash_stream->moof_sync_samples, TRUE);
3644 g_free (dash_stream->last_representation_id);
3647 static GstDashDemuxClockDrift *
3648 gst_dash_demux_clock_drift_new (GstDashDemux * demux)
3650 GstDashDemuxClockDrift *clock_drift;
3652 clock_drift = g_slice_new0 (GstDashDemuxClockDrift);
3653 g_mutex_init (&clock_drift->clock_lock);
3654 clock_drift->next_update =
3655 GST_TIME_AS_USECONDS (gst_adaptive_demux_get_monotonic_time
3656 (GST_ADAPTIVE_DEMUX_CAST (demux)));
3661 gst_dash_demux_clock_drift_free (GstDashDemuxClockDrift * clock_drift)
3664 g_mutex_lock (&clock_drift->clock_lock);
3665 if (clock_drift->ntp_clock)
3666 g_object_unref (clock_drift->ntp_clock);
3667 g_mutex_unlock (&clock_drift->clock_lock);
3668 g_mutex_clear (&clock_drift->clock_lock);
3669 g_slice_free (GstDashDemuxClockDrift, clock_drift);
3674 * The value attribute of the UTCTiming element contains a white-space
3675 * separated list of servers that are recommended to be used in
3676 * combination with the NTP protocol as defined in IETF RFC 5905 for
3677 * getting the appropriate time.
3679 * The DASH standard does not specify which version of NTP. This
3680 * function only works with NTPv4 servers.
3682 static GstDateTime *
3683 gst_dash_demux_poll_ntp_server (GstDashDemuxClockDrift * clock_drift,
3686 GstClockTime ntp_clock_time;
3687 GDateTime *dt, *dt2;
3689 if (!clock_drift->ntp_clock) {
3690 GResolver *resolver;
3695 resolver = g_resolver_get_default ();
3696 /* We don't round-robin NTP servers. If the manifest specifies multiple
3697 NTP time servers, select one at random */
3698 clock_drift->selected_url = g_random_int_range (0, g_strv_length (urls));
3699 GST_DEBUG ("Connecting to NTP time server %s",
3700 urls[clock_drift->selected_url]);
3701 inet_addrs = g_resolver_lookup_by_name (resolver,
3702 urls[clock_drift->selected_url], NULL, &err);
3703 g_object_unref (resolver);
3704 if (!inet_addrs || g_list_length (inet_addrs) == 0) {
3705 GST_ERROR ("Failed to resolve hostname of NTP server: %s",
3706 err ? (err->message) : "unknown error");
3708 g_resolver_free_addresses (inet_addrs);
3714 g_inet_address_to_string ((GInetAddress
3715 *) (g_list_first (inet_addrs)->data));
3716 clock_drift->ntp_clock = gst_ntp_clock_new ("dashntp", ip_addr, 123, 0);
3718 g_resolver_free_addresses (inet_addrs);
3719 if (!clock_drift->ntp_clock) {
3720 GST_ERROR ("Failed to create NTP clock");
3723 if (!gst_clock_wait_for_sync (clock_drift->ntp_clock, 5 * GST_SECOND)) {
3724 g_object_unref (clock_drift->ntp_clock);
3725 clock_drift->ntp_clock = NULL;
3726 GST_ERROR ("Failed to lock to NTP clock");
3730 ntp_clock_time = gst_clock_get_time (clock_drift->ntp_clock);
3731 if (ntp_clock_time == GST_CLOCK_TIME_NONE) {
3732 GST_ERROR ("Failed to get time from NTP clock");
3735 ntp_clock_time -= NTP_TO_UNIX_EPOCH * GST_SECOND;
3736 dt = g_date_time_new_from_unix_utc (ntp_clock_time / GST_SECOND);
3738 GST_ERROR ("Failed to create GstDateTime");
3742 gst_util_uint64_scale (ntp_clock_time % GST_SECOND, 1000000, GST_SECOND);
3743 dt2 = g_date_time_add (dt, ntp_clock_time);
3744 g_date_time_unref (dt);
3745 return gst_date_time_new_from_g_date_time (dt2);
3748 struct Rfc5322TimeZone
3755 Parse an RFC5322 (section 3.3) date-time from the Date: field in the
3757 See https://tools.ietf.org/html/rfc5322#section-3.3
3759 static GstDateTime *
3760 gst_dash_demux_parse_http_head (GstDashDemuxClockDrift * clock_drift,
3761 GstFragment * download)
3763 static const gchar *months[] = { NULL, "Jan", "Feb", "Mar", "Apr",
3764 "May", "Jun", "Jul", "Aug",
3765 "Sep", "Oct", "Nov", "Dec", NULL
3767 static const struct Rfc5322TimeZone timezones[] = {
3782 GstDateTime *value = NULL;
3783 const GstStructure *response_headers;
3784 const gchar *http_date;
3788 gint year = -1, month = -1, day = -1, hour = -1, minute = -1, second = -1;
3791 gfloat tzoffset = 0;
3792 gboolean parsed_tz = FALSE;
3794 g_return_val_if_fail (download != NULL, NULL);
3795 g_return_val_if_fail (download->headers != NULL, NULL);
3797 val = gst_structure_get_value (download->headers, "response-headers");
3801 response_headers = gst_value_get_structure (val);
3802 http_date = gst_structure_get_string (response_headers, "Date");
3807 /* skip optional text version of day of the week */
3808 pos = strchr (http_date, ',');
3814 sscanf (pos, "%02d %3s %04d %02d:%02d:%02d %5s", &day, monthstr, &year,
3815 &hour, &minute, &second, zone);
3820 for (i = 1; months[i]; ++i) {
3821 if (g_ascii_strncasecmp (months[i], monthstr, strlen (months[i])) == 0) {
3826 for (i = 0; timezones[i].name && !parsed_tz; ++i) {
3827 if (g_ascii_strncasecmp (timezones[i].name, z,
3828 strlen (timezones[i].name)) == 0) {
3829 tzoffset = timezones[i].tzoffset;
3835 gboolean neg = FALSE;
3836 /* check if it is in the form +-HHMM */
3837 if (*z == '+' || *z == '-') {
3840 else if (*z == '-') {
3844 ret = sscanf (z, "%02d%02d", &hh, &mm);
3847 tzoffset += mm / 60.0;
3849 tzoffset = -tzoffset;
3854 /* Accept year in both 2 digit or 4 digit format */
3858 if (month > 0 && parsed_tz) {
3859 value = gst_date_time_new (tzoffset,
3860 year, month, day, hour, minute, second);
3866 The timing information is contained in the message body of the HTTP
3867 response and contains a time value formatted according to NTP timestamp
3868 format in IETF RFC 5905.
3871 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
3872 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3874 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3876 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3878 NTP Timestamp Format
3880 static GstDateTime *
3881 gst_dash_demux_parse_http_ntp (GstDashDemuxClockDrift * clock_drift,
3886 GDateTime *dt, *dt2;
3889 /* See https://tools.ietf.org/html/rfc5905#page-12 for details of
3890 the NTP Timestamp Format */
3891 gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
3892 if (mapinfo.size != 8) {
3893 gst_buffer_unmap (buffer, &mapinfo);
3896 seconds = GST_READ_UINT32_BE (mapinfo.data);
3897 fraction = GST_READ_UINT32_BE (mapinfo.data + 4);
3898 gst_buffer_unmap (buffer, &mapinfo);
3899 fraction = gst_util_uint64_scale (fraction, 1000000,
3900 G_GUINT64_CONSTANT (1) << 32);
3901 /* subtract constant to convert from 1900 based time to 1970 based time */
3902 seconds -= NTP_TO_UNIX_EPOCH;
3903 dt = g_date_time_new_from_unix_utc (seconds);
3904 dt2 = g_date_time_add (dt, fraction);
3905 g_date_time_unref (dt);
3906 return gst_date_time_new_from_g_date_time (dt2);
3910 The timing information is contained in the message body of the
3911 HTTP response and contains a time value formatted according to
3912 xs:dateTime as defined in W3C XML Schema Part 2: Datatypes specification.
3914 static GstDateTime *
3915 gst_dash_demux_parse_http_xsdate (GstDashDemuxClockDrift * clock_drift,
3918 GstDateTime *value = NULL;
3921 /* the string from the server might not be zero terminated */
3922 if (gst_buffer_map (buffer, &mapinfo, GST_MAP_READ)) {
3924 str = g_strndup ((const gchar *) mapinfo.data, mapinfo.size);
3925 gst_buffer_unmap (buffer, &mapinfo);
3926 value = gst_date_time_new_from_iso8601_string (str);
3933 gst_dash_demux_poll_clock_drift (GstDashDemux * demux)
3935 GstDashDemuxClockDrift *clock_drift;
3936 GDateTime *start = NULL, *end;
3937 GstBuffer *buffer = NULL;
3938 GstDateTime *value = NULL;
3939 gboolean ret = FALSE;
3941 GstMPDUTCTimingType method;
3944 g_return_val_if_fail (demux != NULL, FALSE);
3945 g_return_val_if_fail (demux->clock_drift != NULL, FALSE);
3946 clock_drift = demux->clock_drift;
3948 GST_TIME_AS_USECONDS (gst_adaptive_demux_get_monotonic_time
3949 (GST_ADAPTIVE_DEMUX_CAST (demux)));
3950 if (now < clock_drift->next_update) {
3951 /*TODO: If a fragment fails to download in adaptivedemux, it waits
3952 for a manifest reload before another attempt to fetch a fragment.
3953 Section 10.8.6 of the DVB-DASH standard states that the DASH client
3954 shall refresh the manifest and resynchronise to one of the time sources.
3956 Currently the fact that the manifest refresh follows a download failure
3957 does not make it into dashdemux. */
3960 urls = gst_mpd_client_get_utc_timing_sources (demux->client,
3961 SUPPORTED_CLOCK_FORMATS, &method);
3965 /* Update selected_url just in case the number of URLs in the UTCTiming
3966 element has shrunk since the last poll */
3967 clock_drift->selected_url = clock_drift->selected_url % g_strv_length (urls);
3968 g_mutex_lock (&clock_drift->clock_lock);
3970 if (method == GST_MPD_UTCTIMING_TYPE_NTP) {
3971 value = gst_dash_demux_poll_ntp_server (clock_drift, urls);
3973 GST_ERROR_OBJECT (demux, "Failed to fetch time from NTP server %s",
3974 urls[clock_drift->selected_url]);
3975 g_mutex_unlock (&clock_drift->clock_lock);
3980 gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3982 GstFragment *download;
3983 gint64 range_start = 0, range_end = -1;
3984 GST_DEBUG_OBJECT (demux, "Fetching current time from %s",
3985 urls[clock_drift->selected_url]);
3986 if (method == GST_MPD_UTCTIMING_TYPE_HTTP_HEAD) {
3990 gst_uri_downloader_fetch_uri_with_range (GST_ADAPTIVE_DEMUX_CAST
3991 (demux)->downloader, urls[clock_drift->selected_url], NULL, TRUE, TRUE,
3992 TRUE, range_start, range_end, NULL);
3994 if (method == GST_MPD_UTCTIMING_TYPE_HTTP_HEAD && download->headers) {
3995 value = gst_dash_demux_parse_http_head (clock_drift, download);
3997 buffer = gst_fragment_get_buffer (download);
3999 g_object_unref (download);
4002 g_mutex_unlock (&clock_drift->clock_lock);
4003 if (!value && !buffer) {
4004 GST_ERROR_OBJECT (demux, "Failed to fetch time from %s",
4005 urls[clock_drift->selected_url]);
4008 end = gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
4009 if (!value && method == GST_MPD_UTCTIMING_TYPE_HTTP_NTP) {
4010 value = gst_dash_demux_parse_http_ntp (clock_drift, buffer);
4011 } else if (!value) {
4012 /* GST_MPD_UTCTIMING_TYPE_HTTP_XSDATE or GST_MPD_UTCTIMING_TYPE_HTTP_ISO */
4013 value = gst_dash_demux_parse_http_xsdate (clock_drift, buffer);
4016 gst_buffer_unref (buffer);
4018 GTimeSpan download_duration = g_date_time_difference (end, start);
4019 GDateTime *client_now, *server_now;
4020 /* We don't know when the server sampled its clock, but we know
4021 it must have been before "end" and probably after "start".
4022 A reasonable estimate is to use (start+end)/2
4024 client_now = g_date_time_add (start, download_duration / 2);
4025 server_now = gst_date_time_to_g_date_time (value);
4026 /* If gst_date_time_new_from_iso8601_string is given an unsupported
4027 ISO 8601 format, it can return a GstDateTime that is not valid,
4028 which causes gst_date_time_to_g_date_time to return NULL */
4030 g_mutex_lock (&clock_drift->clock_lock);
4031 clock_drift->clock_compensation =
4032 g_date_time_difference (server_now, client_now);
4033 g_mutex_unlock (&clock_drift->clock_lock);
4034 GST_DEBUG_OBJECT (demux,
4035 "Difference between client and server clocks is %lfs",
4036 ((double) clock_drift->clock_compensation) / 1000000.0);
4037 g_date_time_unref (server_now);
4040 GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
4042 g_date_time_unref (client_now);
4043 gst_date_time_unref (value);
4045 GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
4047 g_date_time_unref (end);
4050 g_date_time_unref (start);
4051 /* if multiple URLs were specified, use a simple round-robin to
4053 g_mutex_lock (&clock_drift->clock_lock);
4054 if (method == GST_MPD_UTCTIMING_TYPE_NTP) {
4055 clock_drift->next_update = now + FAST_CLOCK_UPDATE_INTERVAL;
4057 clock_drift->selected_url =
4058 (1 + clock_drift->selected_url) % g_strv_length (urls);
4060 clock_drift->next_update = now + SLOW_CLOCK_UPDATE_INTERVAL;
4062 clock_drift->next_update = now + FAST_CLOCK_UPDATE_INTERVAL;
4065 g_mutex_unlock (&clock_drift->clock_lock);
4070 gst_dash_demux_get_clock_compensation (GstDashDemux * demux)
4073 if (demux->clock_drift) {
4074 g_mutex_lock (&demux->clock_drift->clock_lock);
4075 rv = demux->clock_drift->clock_compensation;
4076 g_mutex_unlock (&demux->clock_drift->clock_lock);
4078 GST_LOG_OBJECT (demux, "Clock drift %" GST_STIME_FORMAT, GST_STIME_ARGS (rv));
4083 gst_dash_demux_get_server_now_utc (GstDashDemux * demux)
4085 GDateTime *client_now;
4086 GDateTime *server_now;
4089 gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
4091 g_date_time_add (client_now,
4092 gst_dash_demux_get_clock_compensation (demux));
4093 g_date_time_unref (client_now);