dashdemux: Preserve current representation on live manifest updates
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / dash / gstdashdemux.c
1 /*
2  * DASH demux plugin for GStreamer
3  *
4  * gstdashdemux.c
5  *
6  * Copyright (C) 2012 Orange
7  *
8  * Authors:
9  *   David Corvoysier <david.corvoysier@orange.com>
10  *   Hamid Zakari <hamid.zakari@gmail.com>
11  *
12  * Copyright (C) 2013 Smart TV Alliance
13  *  Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>, Collabora Ltd.
14  *
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.
19  *
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.
24  *
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.
29  */
30 /**
31  * SECTION:element-dashdemux
32  * @title: dashdemux
33  *
34  * DASH demuxer element.
35  * ## Example launch line
36  * |[
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"
38  * ]|
39  */
40
41 /* Implementation notes:
42  *
43  * The following section describes how dashdemux works internally.
44  *
45  * Introduction:
46  *
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.
50  *
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).
55  *
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).
61  *
62  * The representation selection algorithm can be configured using
63  * specific properties: max bitrate, min/max buffering, bandwidth ratio.
64  *
65  *
66  * General Design:
67  *
68  * dashdemux has a single sink pad that accepts the data corresponding
69  * to the manifest, typically fetched from an HTTP or file source.
70  *
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).
74  *
75  * During playback, new representations will typically be exposed as a
76  * new set of pads (see 'Switching between representations' below).
77  *
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.
81  *
82  * Switching between representations:
83  *
84  * Decodebin supports scenarios allowing to seamlessly switch from one
85  * stream to another inside the same "decoding chain".
86  *
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.
90  *
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.
94  *
95  * We take advantage of that to dynamically create a new decoding group
96  * in order to select a different representation during playback.
97  *
98  * Typically, assuming that each fragment contains both audio and video,
99  * the following tree would be created:
100  *
101  * chain "DASH Demux"
102  * |_ group "Representation set 1"
103  * |   |_ chain "Qt Demux 0"
104  * |       |_ group "Stream 0"
105  * |           |_ chain "H264"
106  * |           |_ chain "AAC"
107  * |_ group "Representation set 2"
108  *     |_ chain "Qt Demux 1"
109  *         |_ group "Stream 1"
110  *             |_ chain "H264"
111  *             |_ chain "AAC"
112  *
113  * Or, if audio and video are contained in separate fragments:
114  *
115  * chain "DASH Demux"
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"
122  * |           |_ chain "AAC"
123  * |_ group "Representation set 2"
124  *     |_ chain "Qt Demux 3"
125  *     |   |_ group "Stream 2"
126  *     |       |_ chain "H264"
127  *     |_ chain "Qt Demux 4"
128  *         |_ group "Stream 3"
129  *             |_ chain "AAC"
130  *
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.
136  *
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.
141  *
142  *
143  * Keyframe trick-mode implementation:
144  *
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.
149  *
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.
155  *
156  * * Keyframe-only downloads:
157  *
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.
161  *
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.
165  *
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).
171  *
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).
175  *
176  *
177  * * Deciding the optimal "next" keyframe/fragment to download:
178  *
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).
182  *
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
187  * rebuffering.
188  *
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.
194  *
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
197  * take into account:
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)
202  *
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).
207  *
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).
215  *
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.
219  *
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).
222  *
223  * The estimated buffering level between dashdemux and downstream is
224  * therefore:
225  *   buffering_level = running_time(actual_position) - qos_earliest_time
226  *
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.
232  *
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
236  *
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).
240  *
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)
246  * * Network stalling
247  * * Different keyframe sizes (and therefore download time)
248  * * Decoding speed
249  *
250  * Instead, we adjust the target time calculation based on the
251  * buffering_level.
252  *
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
258  * second).
259  *
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
263  * qos_earliest_time.
264  *
265  * If we are above that buffering level we set the target time to:
266  *      position(running_time(position) + average_download_time)
267  *
268  * The logic is therefore:
269  * WHILE(!EOS)
270  *   Calculate target_time
271  *   Advance to keyframe/fragment for that target_time
272  *   Adaptivedemux downloads that keyframe/fragment
273  *
274  */
275
276 #ifdef HAVE_CONFIG_H
277 #  include "config.h"
278 #endif
279
280 #include <string.h>
281 #include <stdio.h>
282 #include <stdlib.h>
283 #include <inttypes.h>
284 #include <gio/gio.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"
291
292 static GstStaticPadTemplate gst_dash_demux_videosrc_template =
293 GST_STATIC_PAD_TEMPLATE ("video_%02u",
294     GST_PAD_SRC,
295     GST_PAD_SOMETIMES,
296     GST_STATIC_CAPS_ANY);
297
298 static GstStaticPadTemplate gst_dash_demux_audiosrc_template =
299 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
300     GST_PAD_SRC,
301     GST_PAD_SOMETIMES,
302     GST_STATIC_CAPS_ANY);
303
304 static GstStaticPadTemplate gst_dash_demux_subtitlesrc_template =
305 GST_STATIC_PAD_TEMPLATE ("subtitle_%02u",
306     GST_PAD_SRC,
307     GST_PAD_SOMETIMES,
308     GST_STATIC_CAPS_ANY);
309
310 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
311     GST_PAD_SINK,
312     GST_PAD_ALWAYS,
313     GST_STATIC_CAPS ("application/dash+xml"));
314
315 GST_DEBUG_CATEGORY (gst_dash_demux_debug);
316 #define GST_CAT_DEFAULT gst_dash_demux_debug
317
318 enum
319 {
320   PROP_0,
321
322   PROP_MAX_BUFFERING_TIME,
323   PROP_BANDWIDTH_USAGE,
324   PROP_MAX_BITRATE,
325   PROP_MAX_VIDEO_WIDTH,
326   PROP_MAX_VIDEO_HEIGHT,
327   PROP_MAX_VIDEO_FRAMERATE,
328   PROP_PRESENTATION_DELAY,
329   PROP_LAST
330 };
331
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 */
341
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 */
347
348 struct _GstDashDemuxClockDrift
349 {
350   GMutex clock_lock;            /* used to protect access to struct */
351   guint selected_url;
352   gint64 next_update;
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;
356   GstClock *ntp_clock;
357 };
358
359 typedef struct
360 {
361   guint64 start_offset, end_offset;
362   /* TODO: Timestamp and duration */
363 } GstDashStreamSyncSample;
364
365 /* GObject */
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);
371
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,
377     GstBuffer * buf);
378 static gboolean gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
379 static GstFlowReturn
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
385     * stream);
386 static GstFlowReturn
387 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream);
388 static gboolean
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 *
393     demux);
394 static GstFlowReturn gst_dash_demux_update_manifest_data (GstAdaptiveDemux *
395     demux, GstBuffer * buf);
396 static gint64
397 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
398     stream);
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);
403 static gboolean
404 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
405     GstAdaptiveDemuxStream * stream);
406 static GstFlowReturn
407 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
408     GstAdaptiveDemuxStream * stream);
409 static gboolean gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream *
410     stream);
411
412 /* GstDashDemux */
413 static gboolean gst_dash_demux_setup_all_streams (GstDashDemux * demux);
414 static void gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream);
415
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 *
421     demux);
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);
426
427 #define SIDX(s) (&(s)->sidx_parser.sidx)
428
429 static inline GstSidxBoxEntry *
430 SIDX_ENTRY (GstDashDemuxStream * s, gint i)
431 {
432   g_assert (i < SIDX (s)->entries_count);
433   return &(SIDX (s)->entries[(i)]);
434 }
435
436 #define SIDX_CURRENT_ENTRY(s) SIDX_ENTRY(s, SIDX(s)->entry_index)
437
438 static void gst_dash_demux_send_content_protection_event (gpointer cp_data,
439     gpointer stream);
440
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,
444         "dashdemux element")
445     );
446 GST_ELEMENT_REGISTER_DEFINE (dashdemux, "dashdemux", GST_RANK_PRIMARY,
447     GST_TYPE_DASH_DEMUX);
448
449 static void
450 gst_dash_demux_dispose (GObject * obj)
451 {
452   GstDashDemux *demux = GST_DASH_DEMUX (obj);
453
454   gst_dash_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
455
456   if (demux->client) {
457     gst_mpd_client_free (demux->client);
458     demux->client = NULL;
459   }
460
461   g_mutex_clear (&demux->client_lock);
462
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);
467 }
468
469 static gboolean
470 gst_dash_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
471     gint64 * stop)
472 {
473   GstDashDemux *self = GST_DASH_DEMUX (demux);
474   GDateTime *now;
475   GDateTime *mstart;
476   GTimeSpan stream_now;
477   GstClockTime seg_duration;
478
479   if (self->client->mpd_root_node->availabilityStartTime == NULL)
480     return FALSE;
481
482   seg_duration = gst_mpd_client_get_maximum_segment_duration (self->client);
483   now = gst_dash_demux_get_server_now_utc (self);
484   mstart =
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);
490
491   if (stream_now <= 0)
492     return FALSE;
493
494   *stop = stream_now * GST_USECOND;
495   if (self->client->mpd_root_node->timeShiftBufferDepth ==
496       GST_MPD_DURATION_NONE) {
497     *start = 0;
498   } else {
499     *start =
500         *stop -
501         (self->client->mpd_root_node->timeShiftBufferDepth * GST_MSECOND);
502     if (*start < 0)
503       *start = 0;
504   }
505
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
513      time.
514    */
515   *stop -= seg_duration;
516   return TRUE;
517 }
518
519 static GstClockTime
520 gst_dash_demux_get_presentation_offset (GstAdaptiveDemux * demux,
521     GstAdaptiveDemuxStream * stream)
522 {
523   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
524   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
525
526   return gst_mpd_client_get_stream_presentation_offset (dashdemux->client,
527       dashstream->index);
528 }
529
530 static GstClockTime
531 gst_dash_demux_get_period_start_time (GstAdaptiveDemux * demux)
532 {
533   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
534
535   return gst_mpd_client_get_period_start_time (dashdemux->client);
536 }
537
538 static void
539 gst_dash_demux_class_init (GstDashDemuxClass * klass)
540 {
541   GObjectClass *gobject_class;
542   GstElementClass *gstelement_class;
543   GstAdaptiveDemuxClass *gstadaptivedemux_class;
544
545   gobject_class = (GObjectClass *) klass;
546   gstelement_class = (GstElementClass *) klass;
547   gstadaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
548
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;
552
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"
557           "(deprecated)",
558           2, G_MAXUINT, DEFAULT_MAX_BUFFERING_TIME,
559           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
560
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));
568 #endif
569
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));
575
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));
581
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));
587
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));
594
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));
600
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);
607
608   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
609
610   gst_element_class_set_static_metadata (gstelement_class,
611       "DASH Demuxer",
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>");
617
618
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;
623
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;
629
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;
650
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;
657 }
658
659 static void
660 gst_dash_demux_init (GstDashDemux * demux)
661 {
662   /* Properties */
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);
670
671   g_mutex_init (&demux->client_lock);
672
673   gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
674       sizeof (GstDashDemuxStream));
675 }
676
677 static void
678 gst_dash_demux_set_property (GObject * object, guint prop_id,
679     const GValue * value, GParamSpec * pspec)
680 {
681   GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
682   GstDashDemux *demux = GST_DASH_DEMUX (object);
683
684   switch (prop_id) {
685     case PROP_MAX_BUFFERING_TIME:
686       demux->max_buffering_time = g_value_get_uint (value) * GST_SECOND;
687       break;
688     case PROP_BANDWIDTH_USAGE:
689       adaptivedemux->bitrate_limit = g_value_get_float (value);
690       break;
691     case PROP_MAX_BITRATE:
692       demux->max_bitrate = g_value_get_uint (value);
693       break;
694     case PROP_MAX_VIDEO_WIDTH:
695       demux->max_video_width = g_value_get_uint (value);
696       break;
697     case PROP_MAX_VIDEO_HEIGHT:
698       demux->max_video_height = g_value_get_uint (value);
699       break;
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);
703       break;
704     case PROP_PRESENTATION_DELAY:
705       g_free (demux->default_presentation_delay);
706       demux->default_presentation_delay = g_value_dup_string (value);
707       break;
708     default:
709       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
710       break;
711   }
712 }
713
714 static void
715 gst_dash_demux_get_property (GObject * object, guint prop_id, GValue * value,
716     GParamSpec * pspec)
717 {
718   GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
719   GstDashDemux *demux = GST_DASH_DEMUX (object);
720
721   switch (prop_id) {
722     case PROP_MAX_BUFFERING_TIME:
723       g_value_set_uint (value, demux->max_buffering_time / GST_SECOND);
724       break;
725     case PROP_BANDWIDTH_USAGE:
726       g_value_set_float (value, adaptivedemux->bitrate_limit);
727       break;
728     case PROP_MAX_BITRATE:
729       g_value_set_uint (value, demux->max_bitrate);
730       break;
731     case PROP_MAX_VIDEO_WIDTH:
732       g_value_set_uint (value, demux->max_video_width);
733       break;
734     case PROP_MAX_VIDEO_HEIGHT:
735       g_value_set_uint (value, demux->max_video_height);
736       break;
737     case PROP_MAX_VIDEO_FRAMERATE:
738       gst_value_set_fraction (value, demux->max_video_framerate_n,
739           demux->max_video_framerate_d);
740       break;
741     case PROP_PRESENTATION_DELAY:
742       if (demux->default_presentation_delay == NULL)
743         g_value_set_static_string (value, "");
744       else
745         g_value_set_string (value, demux->default_presentation_delay);
746       break;
747     default:
748       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
749       break;
750   }
751 }
752
753 static gboolean
754 gst_dash_demux_setup_mpdparser_streams (GstDashDemux * demux,
755     GstMPDClient * client)
756 {
757   gboolean has_streams = FALSE;
758   GList *adapt_sets, *iter;
759
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;
763
764     gst_mpd_client_setup_streaming (client, adapt_set_node);
765     has_streams = TRUE;
766   }
767
768   if (!has_streams) {
769     GST_ELEMENT_ERROR (demux, STREAM, DEMUX, ("Manifest has no playable "
770             "streams"), ("No streams could be activated from the manifest"));
771   }
772   return has_streams;
773 }
774
775 static gboolean
776 gst_dash_demux_setup_all_streams (GstDashDemux * demux)
777 {
778   guint i;
779
780   GST_DEBUG_OBJECT (demux, "Setting up streams for period %d",
781       gst_mpd_client_get_period_index (demux->client));
782
783   /* clean old active stream list, if any */
784   gst_mpd_client_active_streams_free (demux->client);
785
786   if (!gst_dash_demux_setup_mpdparser_streams (demux, demux->client)) {
787     return FALSE;
788   }
789
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;
794     GstCaps *caps;
795     GstStructure *s;
796     GstPad *srcpad;
797     gchar *lang = NULL;
798     GstTagList *tags = NULL;
799
800     active_stream =
801         gst_mpd_client_get_active_stream_by_index (demux->client, i);
802     if (active_stream == NULL)
803       continue;
804
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);
809       continue;
810     }
811
812     srcpad = gst_dash_demux_create_pad (demux, active_stream);
813     if (srcpad == NULL)
814       continue;
815
816     caps = gst_dash_demux_get_input_caps (demux, active_stream);
817     GST_LOG_OBJECT (demux, "Creating stream %d %" GST_PTR_FORMAT, i, caps);
818
819     if (active_stream->cur_adapt_set) {
820       GstMPDAdaptationSetNode *adp_set = active_stream->cur_adapt_set;
821       lang = adp_set->lang;
822
823       /* Fallback to the language in ContentComponent node */
824       if (lang == NULL) {
825         GList *it;
826
827         for (it = adp_set->ContentComponents; it; it = it->next) {
828           GstMPDContentComponentNode *cc_node = it->data;
829           if (cc_node->lang) {
830             lang = cc_node->lang;
831             break;
832           }
833         }
834       }
835     }
836
837     if (lang) {
838       if (gst_tag_check_language_code (lang))
839         tags = gst_tag_list_new (GST_TAG_LANGUAGE_CODE, lang, NULL);
840       else
841         tags = gst_tag_list_new (GST_TAG_LANGUAGE_NAME, lang, NULL);
842     }
843
844     stream = (GstDashDemuxStream *)
845         gst_adaptive_demux_stream_new (GST_ADAPTIVE_DEMUX_CAST (demux), srcpad);
846     stream->active_stream = active_stream;
847
848     if (active_stream->cur_representation) {
849       stream->last_representation_id =
850           g_strdup (stream->active_stream->cur_representation->id);
851     } else {
852       stream->last_representation_id = NULL;
853     }
854
855     s = gst_caps_get_structure (caps, 0);
856     stream->allow_sidx =
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),
863         caps);
864     if (tags)
865       gst_adaptive_demux_stream_set_tags (GST_ADAPTIVE_DEMUX_STREAM_CAST
866           (stream), tags);
867     stream->index = i;
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;
874
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);
882     }
883
884     gst_isoff_sidx_parser_init (&stream->sidx_parser);
885   }
886
887   return TRUE;
888 }
889
890 static void
891 gst_dash_demux_send_content_protection_event (gpointer data, gpointer userdata)
892 {
893   GstMPDDescriptorTypeNode *cp = (GstMPDDescriptorTypeNode *) data;
894   GstDashDemuxStream *stream = (GstDashDemuxStream *) userdata;
895   GstEvent *event;
896   GstBuffer *pssi;
897   glong pssi_len;
898   gchar *schemeIdUri;
899   GstPad *pad = GST_ADAPTIVE_DEMUX_STREAM_PAD (stream);
900
901   if (cp->schemeIdUri == NULL)
902     return;
903
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");
914     GST_LOG_OBJECT (pad,
915         "Queueing protection event %" GST_PTR_FORMAT " on source pad", event);
916     gst_adaptive_demux_stream_queue_event ((GstAdaptiveDemuxStream *) stream,
917         event);
918     gst_buffer_unref (pssi);
919   }
920   g_free (schemeIdUri);
921 }
922
923 static GstClockTime
924 gst_dash_demux_get_duration (GstAdaptiveDemux * ademux)
925 {
926   GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
927
928   g_return_val_if_fail (demux->client != NULL, GST_CLOCK_TIME_NONE);
929
930   return gst_mpd_client_get_media_presentation_duration (demux->client);
931 }
932
933 static gboolean
934 gst_dash_demux_is_live (GstAdaptiveDemux * ademux)
935 {
936   GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
937
938   g_return_val_if_fail (demux->client != NULL, FALSE);
939
940   return gst_mpd_client_is_live (demux->client);
941 }
942
943 static gboolean
944 gst_dash_demux_setup_streams (GstAdaptiveDemux * demux)
945 {
946   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
947   gboolean ret = TRUE;
948   GstDateTime *now = NULL;
949   guint period_idx;
950
951   /* setup video, audio and subtitle streams, starting from first Period if
952    * non-live */
953   period_idx = 0;
954   if (gst_mpd_client_is_live (dashdemux->client)) {
955     GDateTime *g_now;
956     if (dashdemux->client->mpd_root_node->availabilityStartTime == NULL) {
957       ret = FALSE;
958       GST_ERROR_OBJECT (demux, "MPD does not have availabilityStartTime");
959       goto done;
960     }
961     if (dashdemux->clock_drift == NULL) {
962       gchar **urls;
963       urls =
964           gst_mpd_client_get_utc_timing_sources (dashdemux->client,
965           SUPPORTED_CLOCK_FORMATS, NULL);
966       if (urls) {
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);
970       }
971     }
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);
979       now = target;
980     } else if (dashdemux->default_presentation_delay) {
981       gint64 dfp =
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,
985           dfp * -1000);
986       gst_date_time_unref (now);
987       now = target;
988     }
989     period_idx =
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",
995           date_str);
996       g_free (date_str);
997 #endif
998       ret = FALSE;
999       goto done;
1000     }
1001   }
1002
1003   if (!gst_mpd_client_set_period_index (dashdemux->client, period_idx) ||
1004       !gst_dash_demux_setup_all_streams (dashdemux)) {
1005     ret = FALSE;
1006     goto done;
1007   }
1008
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)) {
1012     GDateTime *gnow;
1013
1014     GST_DEBUG_OBJECT (demux, "Seeking to current time of day for live stream ");
1015
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);
1019   } else {
1020     GST_DEBUG_OBJECT (demux, "Seeking to first segment for on-demand stream ");
1021
1022     /* start playing from the first segment */
1023     gst_mpd_client_seek_to_first_segment (dashdemux->client);
1024   }
1025
1026 done:
1027   if (now != NULL)
1028     gst_date_time_unref (now);
1029   return ret;
1030 }
1031
1032 static gboolean
1033 gst_dash_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
1034 {
1035   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1036   gboolean ret = FALSE;
1037   gchar *manifest;
1038   GstMapInfo mapinfo;
1039
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);
1044
1045   dashdemux->client->mpd_uri = g_strdup (demux->manifest_uri);
1046   dashdemux->client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
1047
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));
1051
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,
1056               NULL)) {
1057         ret = TRUE;
1058       } else {
1059         GST_ELEMENT_ERROR (demux, STREAM, DECODE,
1060             ("Incompatible manifest file."), (NULL));
1061       }
1062     }
1063     gst_buffer_unmap (buf, &mapinfo);
1064   } else {
1065     GST_WARNING_OBJECT (demux, "Failed to map manifest buffer");
1066   }
1067
1068   if (ret)
1069     ret = gst_dash_demux_setup_streams (demux);
1070
1071   return ret;
1072 }
1073
1074 static GstPad *
1075 gst_dash_demux_create_pad (GstDashDemux * demux, GstActiveStream * stream)
1076 {
1077   GstPad *pad;
1078   GstPadTemplate *tmpl;
1079   gchar *name;
1080
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);
1085       break;
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);
1089       break;
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++);
1093         tmpl =
1094             gst_static_pad_template_get (&gst_dash_demux_subtitlesrc_template);
1095       } else {
1096         return NULL;
1097       }
1098       break;
1099     default:
1100       g_assert_not_reached ();
1101       return NULL;
1102   }
1103
1104   /* Create and activate new pads */
1105   pad = gst_pad_new_from_template (tmpl, name);
1106   g_free (name);
1107   gst_object_unref (tmpl);
1108
1109   gst_pad_set_active (pad, TRUE);
1110   GST_INFO_OBJECT (demux, "Creating srcpad %s:%s", GST_DEBUG_PAD_NAME (pad));
1111   return pad;
1112 }
1113
1114 static void
1115 gst_dash_demux_reset (GstAdaptiveDemux * ademux)
1116 {
1117   GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
1118
1119   GST_DEBUG_OBJECT (demux, "Resetting demux");
1120
1121   demux->end_of_period = FALSE;
1122   demux->end_of_manifest = FALSE;
1123
1124   if (demux->client) {
1125     gst_mpd_client_free (demux->client);
1126     demux->client = NULL;
1127   }
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);
1132
1133   demux->n_audio_streams = 0;
1134   demux->n_video_streams = 0;
1135   demux->n_subtitle_streams = 0;
1136
1137   demux->trickmode_no_audio = FALSE;
1138   demux->allow_trickmode_key_units = TRUE;
1139 }
1140
1141 static GstCaps *
1142 gst_dash_demux_get_video_input_caps (GstDashDemux * demux,
1143     GstActiveStream * stream)
1144 {
1145   guint width = 0, height = 0;
1146   gint fps_num = 0, fps_den = 1;
1147   gboolean have_fps = FALSE;
1148   GstCaps *caps = NULL;
1149
1150   if (stream == NULL)
1151     return NULL;
1152
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);
1157     have_fps =
1158         gst_mpd_client_get_video_stream_framerate (stream, &fps_num, &fps_den);
1159   }
1160   caps = gst_mpd_client_get_stream_caps (stream);
1161   if (caps == NULL)
1162     return NULL;
1163
1164   if (width > 0 && height > 0) {
1165     gst_caps_set_simple (caps, "width", G_TYPE_INT, width, "height",
1166         G_TYPE_INT, height, NULL);
1167   }
1168
1169   if (have_fps) {
1170     gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fps_num,
1171         fps_den, NULL);
1172   }
1173
1174   return caps;
1175 }
1176
1177 static GstCaps *
1178 gst_dash_demux_get_audio_input_caps (GstDashDemux * demux,
1179     GstActiveStream * stream)
1180 {
1181   guint rate = 0, channels = 0;
1182   GstCaps *caps = NULL;
1183
1184   if (stream == NULL)
1185     return NULL;
1186
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);
1191   }
1192   caps = gst_mpd_client_get_stream_caps (stream);
1193   if (caps == NULL)
1194     return NULL;
1195
1196   if (rate > 0) {
1197     gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
1198   }
1199   if (channels > 0) {
1200     gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
1201   }
1202
1203   return caps;
1204 }
1205
1206 static GstCaps *
1207 gst_dash_demux_get_application_input_caps (GstDashDemux * demux,
1208     GstActiveStream * stream)
1209 {
1210   GstCaps *caps = NULL;
1211
1212   if (stream == NULL)
1213     return NULL;
1214
1215   caps = gst_mpd_client_get_stream_caps (stream);
1216   if (caps == NULL)
1217     return NULL;
1218
1219   return caps;
1220 }
1221
1222 static GstCaps *
1223 gst_dash_demux_get_input_caps (GstDashDemux * demux, GstActiveStream * stream)
1224 {
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);
1232     default:
1233       return GST_CAPS_NONE;
1234   }
1235 }
1236
1237 static void
1238 gst_dash_demux_stream_update_headers_info (GstAdaptiveDemuxStream * stream)
1239 {
1240   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1241   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1242   gchar *path = NULL;
1243
1244   gst_mpd_client_get_next_header (dashdemux->client,
1245       &path, dashstream->index,
1246       &stream->fragment.header_range_start, &stream->fragment.header_range_end);
1247
1248   if (path != NULL) {
1249     stream->fragment.header_uri =
1250         gst_uri_join_strings (gst_mpd_client_get_baseURL (dashdemux->client,
1251             dashstream->index), path);
1252     g_free (path);
1253     path = NULL;
1254   }
1255
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);
1259
1260   if (path != NULL) {
1261     stream->fragment.index_uri =
1262         gst_uri_join_strings (gst_mpd_client_get_baseURL (dashdemux->client,
1263             dashstream->index), path);
1264     g_free (path);
1265   }
1266 }
1267
1268 static GstFlowReturn
1269 gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream)
1270 {
1271   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1272   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1273   GstClockTime ts;
1274   GstMediaFragmentInfo fragment;
1275   gboolean isombff;
1276
1277   gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
1278
1279   isombff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1280
1281   /* Reset chunk size if any */
1282   stream->fragment.chunk_size = 0;
1283   dashstream->current_fragment_keyframe_distance = GST_CLOCK_TIME_NONE;
1284
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 */
1292       return GST_FLOW_OK;
1293     }
1294   }
1295
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);
1301
1302     gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1303         &fragment);
1304
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 =
1310           entry->duration;
1311     } else {
1312       dashstream->current_fragment_timestamp = fragment.timestamp;
1313       dashstream->current_fragment_duration = fragment.duration;
1314     }
1315
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);
1328
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;
1334
1335     GST_DEBUG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
1336         GST_TIME_ARGS (dashstream->actual_position));
1337
1338     return GST_FLOW_OK;
1339   }
1340
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)) {
1355           GstCaps *caps;
1356           stream->need_header = TRUE;
1357
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);
1363
1364           caps =
1365               gst_dash_demux_get_input_caps (dashdemux,
1366               dashstream->active_stream);
1367           gst_adaptive_demux_stream_set_caps (stream, caps);
1368
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);
1373         }
1374       } else {
1375         g_free (dashstream->last_representation_id);
1376         dashstream->last_representation_id = NULL;
1377       }
1378     }
1379
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);
1383     }
1384
1385     gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1386         &fragment);
1387
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 =
1398           entry->pts;
1399       dashstream->current_fragment_duration = stream->fragment.duration =
1400           entry->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;
1405       } else {
1406         stream->fragment.range_end = fragment.range_end;
1407       }
1408     } else {
1409       dashstream->actual_position = stream->fragment.timestamp =
1410           fragment.timestamp;
1411       dashstream->current_fragment_timestamp = fragment.timestamp;
1412       dashstream->current_fragment_duration = stream->fragment.duration =
1413           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;
1419     }
1420
1421     GST_DEBUG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
1422         GST_TIME_ARGS (dashstream->actual_position));
1423
1424     return GST_FLOW_OK;
1425   }
1426
1427   return GST_FLOW_EOS;
1428 }
1429
1430 static gint
1431 gst_dash_demux_index_entry_search (GstSidxBoxEntry * entry, GstClockTime * ts,
1432     gpointer user_data)
1433 {
1434   GstClockTime entry_ts = entry->pts + entry->duration;
1435   if (entry_ts <= *ts)
1436     return -1;
1437   else if (entry->pts > *ts)
1438     return 1;
1439   else
1440     return 0;
1441 }
1442
1443 static GstFlowReturn
1444 gst_dash_demux_stream_sidx_seek (GstDashDemuxStream * dashstream,
1445     gboolean forward, GstSeekFlags flags, GstClockTime ts,
1446     GstClockTime * final_ts)
1447 {
1448   GstSidxBox *sidx = SIDX (dashstream);
1449   GstSidxBoxEntry *entry;
1450   gint idx = sidx->entries_count;
1451   GstFlowReturn ret = GST_FLOW_OK;
1452
1453   if (sidx->entries_count == 0)
1454     return GST_FLOW_EOS;
1455
1456   entry =
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);
1461
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 */
1467   if (!entry) {
1468     GstSidxBoxEntry *last_entry = &sidx->entries[sidx->entries_count - 1];
1469
1470     GST_WARNING_OBJECT (dashstream->parent.pad, "Couldn't find SIDX entry");
1471
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)
1477       entry = last_entry;
1478   }
1479   if (!entry)
1480     return GST_FLOW_EOS;
1481
1482   idx = entry - sidx->entries;
1483
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) {
1487     idx--;
1488     entry = &sidx->entries[idx];
1489   }
1490
1491   /* Now entry->pts <= ts < entry->pts + entry->duration, need to adjust for
1492    * snapping */
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)
1496       idx += 1;
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)
1500       idx += 1;
1501   }
1502
1503   g_assert (sidx->entry_index < sidx->entries_count);
1504
1505   sidx->entry_index = idx;
1506   dashstream->sidx_position = sidx->entries[idx].pts;
1507
1508   if (final_ts)
1509     *final_ts = dashstream->sidx_position;
1510
1511   return ret;
1512 }
1513
1514 static GstFlowReturn
1515 gst_dash_demux_stream_seek (GstAdaptiveDemuxStream * stream, gboolean forward,
1516     GstSeekFlags flags, GstClockTime ts, GstClockTime * final_ts)
1517 {
1518   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1519   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1520   gint last_index, last_repeat;
1521   gboolean is_isobmff;
1522
1523   last_index = dashstream->active_stream->segment_index;
1524   last_repeat = dashstream->active_stream->segment_repeat_index;
1525
1526   if (dashstream->adapter)
1527     gst_adapter_clear (dashstream->adapter);
1528   dashstream->current_offset = -1;
1529   dashstream->current_index_header_or_data = 0;
1530
1531   dashstream->isobmff_parser.current_fourcc = 0;
1532   dashstream->isobmff_parser.current_start_offset = 0;
1533   dashstream->isobmff_parser.current_size = 0;
1534
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;
1543
1544   is_isobmff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1545
1546   if (!gst_mpd_client_stream_seek (dashdemux->client, dashstream->active_stream,
1547           forward,
1548           is_isobmff ? (flags & (~(GST_SEEK_FLAG_SNAP_BEFORE |
1549                       GST_SEEK_FLAG_SNAP_AFTER))) : flags, ts, final_ts)) {
1550     return GST_FLOW_EOS;
1551   }
1552
1553   if (is_isobmff) {
1554     GstClockTime period_start, offset;
1555
1556     period_start = gst_mpd_client_get_period_start_time (dashdemux->client);
1557     offset =
1558         gst_mpd_client_get_stream_presentation_offset (dashdemux->client,
1559         dashstream->index);
1560
1561     if (G_UNLIKELY (ts < period_start))
1562       ts = offset;
1563     else
1564       ts += offset - period_start;
1565
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;
1573     }
1574
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);
1581       }
1582       dashstream->pending_seek_ts = GST_CLOCK_TIME_NONE;
1583     } else {
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;
1587     }
1588   }
1589
1590   stream->discont = TRUE;
1591
1592   return GST_FLOW_OK;
1593 }
1594
1595 static gboolean
1596 gst_dash_demux_stream_has_next_sync_sample (GstAdaptiveDemuxStream * stream)
1597 {
1598   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1599
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)
1605         return TRUE;
1606     } else {
1607       if (dashstream->current_sync_sample >= 1)
1608         return TRUE;
1609     }
1610   }
1611   return FALSE;
1612 }
1613
1614 static gboolean
1615 gst_dash_demux_stream_has_next_subfragment (GstAdaptiveDemuxStream * stream)
1616 {
1617   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1618   GstSidxBox *sidx = SIDX (dashstream);
1619
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)
1623         return TRUE;
1624     } else {
1625       if (sidx->entry_index >= 1)
1626         return TRUE;
1627     }
1628   }
1629   return FALSE;
1630 }
1631
1632 static gboolean
1633 gst_dash_demux_stream_advance_sync_sample (GstAdaptiveDemuxStream * stream,
1634     GstClockTime target_time)
1635 {
1636   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1637   gboolean fragment_finished = FALSE;
1638   guint idx = -1;
1639
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));
1651
1652     if (stream->demux->segment.rate > 0.0) {
1653       idx =
1654           (target_time -
1655           dashstream->current_fragment_timestamp) /
1656           dashstream->current_fragment_keyframe_distance;
1657
1658       /* Prevent getting stuck in a loop due to rounding errors */
1659       if (idx == dashstream->current_sync_sample)
1660         idx++;
1661     } else {
1662       GstClockTime end_time =
1663           dashstream->current_fragment_timestamp +
1664           dashstream->current_fragment_duration;
1665
1666       if (end_time < target_time) {
1667         idx = dashstream->moof_sync_samples->len;
1668       } else {
1669         idx =
1670             (end_time -
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;
1675           goto beach;
1676         }
1677         idx = dashstream->moof_sync_samples->len - 1 - idx;
1678       }
1679
1680       /* Prevent getting stuck in a loop due to rounding errors */
1681       if (idx == dashstream->current_sync_sample) {
1682         if (idx == 0) {
1683           dashstream->current_sync_sample = -1;
1684           fragment_finished = TRUE;
1685           goto beach;
1686         }
1687
1688         idx--;
1689       }
1690     }
1691   }
1692
1693   GST_DEBUG_OBJECT (stream->pad,
1694       "Advancing sync sample #%d target #%d",
1695       dashstream->current_sync_sample, idx);
1696
1697   if (idx != -1 && idx >= dashstream->moof_sync_samples->len) {
1698     dashstream->current_sync_sample = -1;
1699     fragment_finished = TRUE;
1700     goto beach;
1701   }
1702
1703   if (stream->demux->segment.rate > 0.0) {
1704     /* Try to get the sync sample for the target time */
1705     if (idx != -1) {
1706       dashstream->current_sync_sample = idx;
1707     } else {
1708       dashstream->current_sync_sample++;
1709       if (dashstream->current_sync_sample >= dashstream->moof_sync_samples->len) {
1710         fragment_finished = TRUE;
1711       }
1712     }
1713   } else {
1714     if (idx != -1) {
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;
1721     } else {
1722       dashstream->current_sync_sample--;
1723     }
1724   }
1725
1726 beach:
1727   GST_DEBUG_OBJECT (stream->pad,
1728       "Advancing sync sample #%d fragment_finished:%d",
1729       dashstream->current_sync_sample, fragment_finished);
1730
1731   if (!fragment_finished)
1732     stream->discont = TRUE;
1733
1734   return !fragment_finished;
1735 }
1736
1737 static gboolean
1738 gst_dash_demux_stream_advance_subfragment (GstAdaptiveDemuxStream * stream)
1739 {
1740   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1741
1742   GstSidxBox *sidx = SIDX (dashstream);
1743   gboolean fragment_finished = TRUE;
1744
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;
1750       }
1751
1752       if (idx == sidx->entries_count)
1753         dashstream->sidx_position =
1754             sidx->entries[idx - 1].pts + sidx->entries[idx - 1].duration;
1755       else
1756         dashstream->sidx_position = sidx->entries[idx].pts;
1757     } else {
1758       gint idx = --sidx->entry_index;
1759
1760       if (idx >= 0) {
1761         fragment_finished = FALSE;
1762         dashstream->sidx_position = sidx->entries[idx].pts;
1763       } else {
1764         dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1765       }
1766     }
1767   }
1768
1769   GST_DEBUG_OBJECT (stream->pad, "New sidx index: %d / %d. "
1770       "Finished fragment: %d", sidx->entry_index, sidx->entries_count,
1771       fragment_finished);
1772
1773   return !fragment_finished;
1774 }
1775
1776 static gboolean
1777 gst_dash_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
1778 {
1779   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1780   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1781
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))
1785       return TRUE;
1786   }
1787
1788   if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)) {
1789     if (gst_dash_demux_stream_has_next_subfragment (stream))
1790       return TRUE;
1791   }
1792
1793   return gst_mpd_client_has_next_segment (dashdemux->client,
1794       dashstream->active_stream, stream->demux->segment.rate > 0.0);
1795 }
1796
1797 /* The goal here is to figure out, once we have pushed a keyframe downstream,
1798  * what the next ideal keyframe to download is.
1799  * 
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)
1804  */
1805 static GstClockTime
1806 gst_dash_demux_stream_get_target_time (GstDashDemux * dashdemux,
1807     GstAdaptiveDemuxStream * stream, GstClockTime cur_position,
1808     GstClockTime min_skip)
1809 {
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;
1817
1818   g_assert (min_skip > 0);
1819
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)
1824     min_position = 0;
1825   else
1826     min_position = cur_position - min_skip;
1827
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
1831    * downstream.
1832    *
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
1836    */
1837   upstream_earliest_time =
1838       gst_adaptive_demux_get_qos_earliest_time ((GstAdaptiveDemux *) dashdemux);
1839   if (upstream_earliest_time != GST_CLOCK_TIME_NONE) {
1840     GstClock *clock;
1841
1842     clock = gst_element_get_clock (GST_ELEMENT_CAST (dashdemux));
1843
1844     if (clock) {
1845       GstClockTime base_time;
1846       GstClockTime now_time;
1847
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;
1852       else
1853         now_time = 0;
1854
1855       gst_object_unref (clock);
1856
1857       earliest_time = MAX (now_time, upstream_earliest_time);
1858     } else {
1859       earliest_time = upstream_earliest_time;
1860     }
1861   }
1862
1863   /* our current position in running time */
1864   cur_running =
1865       gst_segment_to_running_time (&stream->segment, GST_FORMAT_TIME,
1866       cur_position);
1867
1868   /* the minimum position we have to skip to in running time */
1869   min_running =
1870       gst_segment_to_running_time (&stream->segment, GST_FORMAT_TIME,
1871       min_position);
1872
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));
1880
1881   /* Take configured maximum video bandwidth and framerate into account */
1882   {
1883     GstClockTime min_run_dist, min_frame_dist, diff = 0;
1884     guint max_fps_n, max_fps_d;
1885
1886     min_run_dist = min_skip / ABS (stream->segment.rate);
1887
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;
1891     } else {
1892       /* more than 10 fps is not very useful if we're skipping anyway */
1893       max_fps_n = 10;
1894       max_fps_d = 1;
1895     }
1896
1897     min_frame_dist = gst_util_uint64_scale_ceil (GST_SECOND,
1898         max_fps_d, max_fps_n);
1899
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);
1907
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);
1912
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);
1917
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);
1924       }
1925     }
1926
1927     if (diff > 0) {
1928       GST_DEBUG_OBJECT (stream->pad,
1929           "Skipping further ahead by %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1930       min_running += diff;
1931     }
1932   }
1933
1934   if (earliest_time == GST_CLOCK_TIME_NONE) {
1935     GstClockTime run_key_dist;
1936
1937     run_key_dist =
1938         dashstream->keyframe_average_distance / ABS (stream->segment.rate);
1939
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)
1945       ret =
1946           gst_segment_position_from_running_time (&stream->segment,
1947           GST_FORMAT_TIME, min_running);
1948     else
1949       ret = gst_segment_position_from_running_time (&stream->segment,
1950           GST_FORMAT_TIME,
1951           min_running - run_key_dist + dashstream->average_download_time);
1952
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));
1956
1957     goto out;
1958   }
1959
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));
1967
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);
1970
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) */
1974     ret =
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));
1988   } else {
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));
1995   }
1996
1997 out:
1998
1999   {
2000     GstClockTime cur_skip =
2001         (cur_position < ret) ? ret - cur_position : cur_position - ret;
2002
2003     if (dashstream->average_skip_size == 0) {
2004       dashstream->average_skip_size = cur_skip;
2005     } else {
2006       dashstream->average_skip_size =
2007           (cur_skip + 3 * dashstream->average_skip_size) / 4;
2008     }
2009
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;
2017       else
2018         ret = 0;
2019     }
2020   }
2021
2022   return ret;
2023 }
2024
2025 static GstFlowReturn
2026 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream)
2027 {
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;
2032   GstFlowReturn ret;
2033
2034   GST_DEBUG_OBJECT (stream->pad, "Advance fragment");
2035
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;
2044     } else {
2045       dashstream->average_download_time = stream->last_download_time;
2046     }
2047
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));
2052   }
2053
2054   previous_position = dashstream->actual_position;
2055
2056   /* Update internal position */
2057   if (GST_CLOCK_TIME_IS_VALID (dashstream->actual_position)) {
2058     GstClockTime dur;
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) {
2064         dur = 0;
2065       } else if (dashstream->current_sync_sample <
2066           dashstream->moof_sync_samples->len) {
2067         dur = dashstream->current_fragment_keyframe_distance;
2068       } else {
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;
2074         } else {
2075           dur =
2076               dashstream->current_fragment_timestamp +
2077               dashstream->current_fragment_duration -
2078               dashstream->actual_position;
2079         }
2080       }
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;
2086     } else {
2087       dur = stream->fragment.duration;
2088     }
2089
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;
2096       else
2097         target_time =
2098             gst_dash_demux_stream_get_target_time (dashdemux, stream,
2099             dashstream->actual_position, dur);
2100       dashstream->actual_position = target_time;
2101     } else {
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;
2107       else
2108         dashstream->actual_position = 0;
2109     }
2110
2111     GST_DEBUG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
2112         GST_TIME_ARGS (dashstream->actual_position));
2113   }
2114   dashstream->target_time = target_time;
2115
2116   GST_DEBUG_OBJECT (stream->pad, "target_time: %" GST_TIME_FORMAT,
2117       GST_TIME_ARGS (target_time));
2118
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))
2123       return GST_FLOW_OK;
2124   }
2125
2126   dashstream->isobmff_parser.current_fourcc = 0;
2127   dashstream->isobmff_parser.current_start_offset = 0;
2128   dashstream->isobmff_parser.current_size = 0;
2129
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;
2137
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;
2145
2146     /* Key-unit trick mode, seek to fragment containing target time
2147      *
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
2152      * flag.
2153      *
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.
2156      */
2157     while (TRUE) {
2158       ret =
2159           gst_dash_demux_stream_seek (stream, (stream->segment.rate > 0), flags,
2160           target_time, &actual_ts);
2161
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));
2165         /* Give up */
2166         if (flags != 0)
2167           break;
2168
2169         /* Retry with skipping ahead */
2170         flags |= GST_SEEK_FLAG_SNAP_AFTER;
2171         continue;
2172       }
2173
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));
2178
2179       if ((stream->segment.rate > 0 && actual_ts <= previous_position) ||
2180           (stream->segment.rate < 0 && actual_ts >= previous_position)) {
2181         /* Give up */
2182         if (flags != 0)
2183           break;
2184
2185         /* Retry with forcing skipping ahead */
2186         flags |= GST_SEEK_FLAG_SNAP_AFTER;
2187
2188         continue;
2189       }
2190
2191       /* All good */
2192       break;
2193     }
2194   } else {
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))
2198         return GST_FLOW_OK;
2199     }
2200
2201     if (dashstream->adapter)
2202       gst_adapter_clear (dashstream->adapter);
2203
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;
2208
2209     ret = gst_mpd_client_advance_segment (dashdemux->client,
2210         dashstream->active_stream, stream->demux->segment.rate > 0.0);
2211   }
2212   return ret;
2213 }
2214
2215 static gboolean
2216 gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream * stream,
2217     guint64 bitrate)
2218 {
2219   GstActiveStream *active_stream = NULL;
2220   GList *rep_list = NULL;
2221   gint new_index;
2222   GstAdaptiveDemux *base_demux = stream->demux;
2223   GstDashDemux *demux = GST_DASH_DEMUX_CAST (stream->demux);
2224   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2225   gboolean ret = FALSE;
2226
2227   active_stream = dashstream->active_stream;
2228   if (active_stream == NULL) {
2229     goto end;
2230   }
2231
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");
2235     goto end;
2236   }
2237
2238   /* retrieve representation list */
2239   if (active_stream->cur_adapt_set)
2240     rep_list = active_stream->cur_adapt_set->Representations;
2241   if (!rep_list) {
2242     goto end;
2243   }
2244
2245   GST_DEBUG_OBJECT (stream->pad,
2246       "Trying to change to bitrate: %" G_GUINT64_FORMAT, bitrate);
2247
2248   if (active_stream->mimeType == GST_STREAM_VIDEO && demux->max_bitrate) {
2249     bitrate = MIN (demux->max_bitrate, bitrate);
2250   }
2251
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) {
2255     new_index =
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);
2259   } else {
2260     new_index =
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);
2265   }
2266
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);
2270
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)) {
2276       GstCaps *caps;
2277
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);
2282       ret = TRUE;
2283
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);
2288     } else {
2289       GST_WARNING_OBJECT (demux, "Can not switch representation, aborting...");
2290     }
2291   }
2292
2293   if (ret) {
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;
2306       else
2307         dashstream->sidx_position = GST_CLOCK_TIME_NONE;
2308     } else {
2309       dashstream->sidx_position = GST_CLOCK_TIME_NONE;
2310     }
2311
2312     gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
2313     dashstream->sidx_base_offset = 0;
2314     dashstream->allow_sidx = TRUE;
2315
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;
2320
2321     dashstream->current_offset = -1;
2322     dashstream->current_index_header_or_data = 0;
2323
2324     if (dashstream->adapter)
2325       gst_adapter_clear (dashstream->adapter);
2326
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;
2335   }
2336
2337 end:
2338   return ret;
2339 }
2340
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))
2344
2345 static gboolean
2346 gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
2347 {
2348   gdouble rate;
2349   GstFormat format;
2350   GstSeekFlags flags;
2351   GstSeekType start_type, stop_type;
2352   gint64 start, stop;
2353   GList *list;
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;
2360
2361   gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
2362       &stop_type, &stop);
2363
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 */
2366     return TRUE;
2367   }
2368
2369   if (demux->segment.rate > 0.0) {
2370     target_pos = (GstClockTime) start;
2371   } else {
2372     target_pos = (GstClockTime) stop;
2373   }
2374
2375   /* select the requested Period in the Media Presentation */
2376   if (!gst_mpd_client_setup_media_presentation (dashdemux->client, target_pos,
2377           -1, NULL))
2378     return FALSE;
2379
2380   current_period = 0;
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) {
2393       break;
2394     }
2395   }
2396   if (list == NULL) {
2397     GST_WARNING_OBJECT (demux, "Could not find seeked Period");
2398     return FALSE;
2399   }
2400
2401   trickmode_no_audio = ! !(flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO);
2402
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);
2406
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;
2410
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))
2414       return FALSE;
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;
2420
2421     /* setup video, audio and subtitle streams, starting from the new Period */
2422     if (!gst_dash_demux_setup_all_streams (dashdemux))
2423       return FALSE;
2424     streams = demux->next_streams;
2425   }
2426
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;
2431
2432     dashstream->average_skip_size = 0;
2433     if (gst_dash_demux_stream_seek (stream, rate >= 0, 0, target_pos,
2434             NULL) != GST_FLOW_OK)
2435       return FALSE;
2436   }
2437
2438   return TRUE;
2439 }
2440
2441 static gint64
2442 gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
2443 {
2444   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2445   return MIN (dashdemux->client->mpd_root_node->minimumUpdatePeriod * 1000,
2446       SLOW_CLOCK_UPDATE_INTERVAL);
2447 }
2448
2449 static GstFlowReturn
2450 gst_dash_demux_update_manifest_data (GstAdaptiveDemux * demux,
2451     GstBuffer * buffer)
2452 {
2453   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2454   GstMPDClient *new_client = NULL;
2455   GstMapInfo mapinfo;
2456
2457   GST_DEBUG_OBJECT (demux, "Updating manifest file from URL");
2458
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);
2465
2466   if (gst_mpd_client_parse (new_client, (gchar *) mapinfo.data, mapinfo.size)) {
2467     const gchar *period_id;
2468     guint period_idx;
2469     GList *iter;
2470     GList *streams_iter;
2471     GList *streams;
2472
2473     /* prepare the new manifest and try to transfer the stream position
2474      * status from the old manifest client  */
2475
2476     GST_DEBUG_OBJECT (demux, "Updating manifest");
2477
2478     period_id = gst_mpd_client_get_period_id (dashdemux->client);
2479     period_idx = gst_mpd_client_get_period_index (dashdemux->client);
2480
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)) {
2484       /* TODO */
2485     }
2486
2487     if (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;
2493       }
2494     } else {
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;
2500       }
2501     }
2502
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;
2508     }
2509
2510     /* If no pads have been exposed yet, need to use those */
2511     streams = NULL;
2512     if (demux->streams == NULL) {
2513       if (demux->prepared_streams) {
2514         streams = demux->prepared_streams;
2515       }
2516     } else {
2517       streams = demux->streams;
2518     }
2519
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;
2527       GstClockTime ts;
2528
2529       if (!new_stream) {
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;
2536       }
2537
2538       if (new_stream->cur_adapt_set
2539           && demux_stream->last_representation_id != NULL) {
2540
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,
2547                   rep_node)) {
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);
2551           } else {
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;
2558           }
2559         } else {
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);
2566         }
2567       }
2568
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)) {
2573
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.
2579          */
2580
2581         /* _get_next_fragment_timestamp() returned relative timestamp to
2582          * corresponding period start, but _client_stream_seek expects absolute
2583          * MPD time. */
2584         ts += gst_mpd_client_get_period_start_time (dashdemux->client);
2585
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);
2593       }
2594
2595       demux_stream->active_stream = new_stream;
2596     }
2597
2598     gst_mpd_client_free (dashdemux->client);
2599     dashdemux->client = new_client;
2600
2601     GST_DEBUG_OBJECT (demux, "Manifest file successfully updated");
2602     if (dashdemux->clock_drift) {
2603       gst_dash_demux_poll_clock_drift (dashdemux);
2604     }
2605   } else {
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
2608      * the manifest */
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;
2613   }
2614
2615   gst_buffer_unmap (buffer, &mapinfo);
2616
2617   return GST_FLOW_OK;
2618 }
2619
2620 static gint64
2621 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
2622     stream)
2623 {
2624   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
2625   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2626   GstDateTime *segmentAvailability;
2627   GstActiveStream *active_stream = dashstream->active_stream;
2628
2629   segmentAvailability =
2630       gst_mpd_client_get_next_segment_availability_start_time
2631       (dashdemux->client, active_stream);
2632
2633   if (segmentAvailability) {
2634     gint64 diff;
2635     GstDateTime *cur_time;
2636
2637     cur_time =
2638         gst_date_time_new_from_g_date_time
2639         (gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST
2640             (dashdemux)));
2641     diff =
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 */
2649     return diff -
2650         gst_dash_demux_get_clock_compensation (dashdemux) * GST_USECOND;
2651   }
2652   return 0;
2653 }
2654
2655 static gboolean
2656 gst_dash_demux_has_next_period (GstAdaptiveDemux * demux)
2657 {
2658   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2659
2660   if (demux->segment.rate >= 0)
2661     return gst_mpd_client_has_next_period (dashdemux->client);
2662   else
2663     return gst_mpd_client_has_previous_period (dashdemux->client);
2664 }
2665
2666 static void
2667 gst_dash_demux_advance_period (GstAdaptiveDemux * demux)
2668 {
2669   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2670
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)) {
2674       /* TODO error */
2675       return;
2676     }
2677   } else {
2678     if (!gst_mpd_client_set_period_index (dashdemux->client,
2679             gst_mpd_client_get_period_index (dashdemux->client) - 1)) {
2680       /* TODO error */
2681       return;
2682     }
2683   }
2684
2685   gst_dash_demux_setup_all_streams (dashdemux);
2686   gst_mpd_client_seek_to_first_segment (dashdemux->client);
2687 }
2688
2689 static GstBuffer *
2690 _gst_buffer_split (GstBuffer * buffer, gint offset, gsize size)
2691 {
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);
2695
2696   gst_buffer_resize (buffer, 0, offset);
2697
2698   return newbuf;
2699 }
2700
2701 static gboolean
2702 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
2703     GstAdaptiveDemuxStream * stream)
2704 {
2705   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2706   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2707
2708   GST_LOG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
2709       GST_TIME_ARGS (dashstream->actual_position));
2710
2711   dashstream->current_index_header_or_data = 0;
2712   dashstream->current_offset = -1;
2713
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
2719    */
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;
2724
2725   return TRUE;
2726 }
2727
2728 static GstFlowReturn
2729 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
2730     GstAdaptiveDemuxStream * stream)
2731 {
2732   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2733   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2734
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
2740    */
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;
2745
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)
2755         return GST_FLOW_OK;
2756     } else if (gst_dash_demux_stream_has_next_subfragment (stream)) {
2757       return GST_FLOW_OK;
2758     }
2759   }
2760
2761   if (G_UNLIKELY (stream->downloading_header || stream->downloading_index))
2762     return GST_FLOW_OK;
2763
2764   return gst_adaptive_demux_stream_advance_fragment (demux, stream,
2765       stream->fragment.duration);
2766 }
2767
2768 static gboolean
2769 gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream * stream)
2770 {
2771   GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
2772   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2773
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
2777    */
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 */
2785
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)) {
2794             first =
2795                 ((dashstream->target_time -
2796                     dashstream->current_fragment_timestamp) /
2797                 dashstream->keyframe_average_distance) == 0 ? TRUE : FALSE;
2798           } else if (stream->segment.rate > 0) {
2799             first = TRUE;
2800           }
2801
2802           if (first)
2803             stream->fragment.chunk_size += dashstream->keyframe_average_size;
2804         }
2805       }
2806
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;
2815
2816         if (dashstream->current_offset == GST_CLOCK_TIME_NONE) {
2817           downloaded_end_offset = sidx_start_offset;
2818         } else {
2819           downloaded_end_offset =
2820               dashstream->current_offset +
2821               gst_adapter_available (dashstream->adapter);
2822         }
2823
2824         downloaded_end_offset = MAX (downloaded_end_offset, sidx_start_offset);
2825
2826         if (stream->fragment.chunk_size +
2827             downloaded_end_offset > sidx_end_offset) {
2828           stream->fragment.chunk_size = sidx_end_offset - downloaded_end_offset;
2829         }
2830       }
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;
2841
2842         downloaded_end_offset =
2843             dashstream->current_offset +
2844             gst_adapter_available (dashstream->adapter);
2845
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;
2852
2853           if (end_offset > sidx_end_offset) {
2854             end_offset = sidx_end_offset;
2855           }
2856         }
2857
2858         if (downloaded_end_offset < end_offset) {
2859           stream->fragment.chunk_size = end_offset - downloaded_end_offset;
2860         } else {
2861           stream->fragment.chunk_size = 0;
2862         }
2863       } else {
2864         stream->fragment.chunk_size = 0;
2865       }
2866     } else {
2867       /* Have moof but can't do key-units mode, just download until the end */
2868       stream->fragment.chunk_size = -1;
2869     }
2870   } else {
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;
2877     } else {
2878       stream->fragment.chunk_size = 0;
2879     }
2880   }
2881
2882   return stream->fragment.chunk_size != 0;
2883 }
2884
2885 static GstFlowReturn
2886 gst_dash_demux_parse_isobmff (GstAdaptiveDemux * demux,
2887     GstDashDemuxStream * dash_stream, gboolean * sidx_seek_needed)
2888 {
2889   GstAdaptiveDemuxStream *stream = (GstAdaptiveDemuxStream *) dash_stream;
2890   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2891   gsize available;
2892   GstBuffer *buffer;
2893   GstMapInfo map;
2894   GstByteReader reader;
2895   guint32 fourcc;
2896   guint header_size;
2897   guint64 size, buffer_offset;
2898
2899   *sidx_seek_needed = FALSE;
2900
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)
2905    */
2906   g_assert (dash_stream->isobmff_parser.current_fourcc !=
2907       GST_ISOFF_FOURCC_MDAT);
2908
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;
2912
2913   /* Always at the start of a box here */
2914   g_assert (dash_stream->isobmff_parser.current_size == 0);
2915
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);
2919
2920   /* While there are more boxes left to parse ... */
2921   dash_stream->isobmff_parser.current_start_offset = buffer_offset;
2922   do {
2923     dash_stream->isobmff_parser.current_fourcc = 0;
2924     dash_stream->isobmff_parser.current_size = 0;
2925
2926     if (!gst_isoff_parse_box_header (&reader, &fourcc, NULL, &header_size,
2927             &size)) {
2928       break;
2929     }
2930
2931     dash_stream->isobmff_parser.current_fourcc = fourcc;
2932     if (size == 0) {
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;
2938       break;
2939     }
2940
2941     dash_stream->isobmff_parser.current_size = size;
2942
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);
2949       break;
2950     }
2951
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);
2956
2957     if (dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MOOF) {
2958       GstByteReader sub_reader;
2959
2960       /* Only allow SIDX before the very first moof */
2961       dash_stream->allow_sidx = FALSE;
2962
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;
2971
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;
2976         else
2977           dash_stream->moof_average_size =
2978               (size + dash_stream->moof_average_size + 3) / 4;
2979       } else {
2980         dash_stream->moof_average_size = size;
2981       }
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;
2988       guint dummy;
2989
2990       dash_stream->sidx_base_offset =
2991           dash_stream->isobmff_parser.current_start_offset + size;
2992       dash_stream->allow_sidx = FALSE;
2993
2994       gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
2995
2996       res =
2997           gst_isoff_sidx_parser_parse (&dash_stream->sidx_parser, &sub_reader,
2998           &dummy);
2999
3000       if (res == GST_ISOFF_PARSER_DONE) {
3001         guint64 first_offset = dash_stream->sidx_parser.sidx.first_offset;
3002         GstSidxBox *sidx = SIDX (dash_stream);
3003         guint i;
3004
3005         if (first_offset) {
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;
3009         }
3010
3011         for (i = 0; i < sidx->entries_count; i++) {
3012           GstSidxBoxEntry *entry = &sidx->entries[i];
3013
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);
3018             break;
3019           }
3020         }
3021
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);
3032             }
3033             dash_stream->pending_seek_ts = GST_CLOCK_TIME_NONE;
3034           } else {
3035
3036             if (dash_stream->sidx_position == GST_CLOCK_TIME_NONE) {
3037               SIDX (dash_stream)->entry_index = 0;
3038             } else {
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);
3046               }
3047             }
3048             dash_stream->sidx_position =
3049                 SIDX (dash_stream)->entries[SIDX (dash_stream)->entry_index].
3050                 pts;
3051           }
3052         }
3053
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;
3059           break;
3060         }
3061       }
3062     } else {
3063       gst_byte_reader_skip (&reader, size - header_size);
3064     }
3065
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);
3070
3071   gst_buffer_unmap (buffer, &map);
3072
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) {
3075     GstBuffer *pending;
3076
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);
3082
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;
3090
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) {
3096     GstBuffer *pending;
3097
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 */
3100
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;
3105
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);
3110   }
3111
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);
3115
3116   return GST_FLOW_OK;
3117 }
3118
3119 static gboolean
3120 gst_dash_demux_find_sync_samples (GstAdaptiveDemux * demux,
3121     GstAdaptiveDemuxStream * stream)
3122 {
3123   GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
3124   GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3125   guint i;
3126   guint32 track_id = 0;
3127   guint64 prev_traf_end;
3128   gboolean trex_sample_flags = FALSE;
3129
3130   if (!dash_stream->moof) {
3131     dashdemux->allow_trickmode_key_units = FALSE;
3132     return FALSE;
3133   }
3134
3135   dash_stream->current_sync_sample = -1;
3136   dash_stream->moof_sync_samples =
3137       g_array_new (FALSE, FALSE, sizeof (GstDashStreamSyncSample));
3138
3139   prev_traf_end = dash_stream->moof_offset;
3140
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;
3145     guint j;
3146
3147     if (i == 0) {
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;
3156       return FALSE;
3157     }
3158
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;
3163     } else {
3164       traf_offset = prev_traf_end;
3165     }
3166
3167     prev_trun_end = traf_offset;
3168
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;
3172       guint k;
3173
3174       if (trun->flags & GST_TRUN_FLAGS_DATA_OFFSET_PRESENT) {
3175         trun_offset = traf_offset + trun->data_offset;
3176       } else {
3177         trun_offset = prev_trun_end;
3178       }
3179
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;
3186 #if 0
3187         guint32 sample_duration;
3188 #endif
3189
3190         sample_offset = prev_sample_end;
3191
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)
3195             && k == 0) {
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;
3200         } else {
3201           trex_sample_flags = TRUE;
3202           continue;
3203         }
3204
3205 #if 0
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;
3211         } else {
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;
3216           return FALSE;
3217         }
3218 #endif
3219
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;
3225         } else {
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;
3231           return FALSE;
3232         }
3233
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);
3241         }
3242       }
3243
3244       prev_trun_end = prev_sample_end;
3245     }
3246
3247     prev_traf_end = prev_trun_end;
3248   }
3249
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");
3254     } else {
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;
3260       return FALSE;
3261     }
3262   }
3263
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;
3269     return FALSE;
3270   }
3271
3272   {
3273     GstDashStreamSyncSample *sync_sample;
3274     guint i;
3275     guint size;
3276     GstClockTime current_keyframe_distance;
3277
3278     for (i = 0; i < dash_stream->moof_sync_samples->len; i++) {
3279       sync_sample =
3280           &g_array_index (dash_stream->moof_sync_samples,
3281           GstDashStreamSyncSample, i);
3282       size = sync_sample->end_offset + 1 - sync_sample->start_offset;
3283
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;
3289         else
3290           dash_stream->keyframe_average_size =
3291               (size + dash_stream->keyframe_average_size * 3) / 4;
3292       } else {
3293         dash_stream->keyframe_average_size = size;
3294       }
3295
3296       if (i == 0) {
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;
3301         } else {
3302           dash_stream->first_sync_sample_after_moof =
3303               (dash_stream->moof_sync_samples->len == 1
3304               || demux->segment.rate > 0.0);
3305         }
3306       }
3307     }
3308
3309     g_assert (stream->fragment.duration != 0);
3310     g_assert (stream->fragment.duration != GST_CLOCK_TIME_NONE);
3311
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;
3318     } else {
3319       current_keyframe_distance =
3320           stream->fragment.duration / dash_stream->moof_sync_samples->len;
3321     }
3322     dash_stream->current_fragment_keyframe_distance = current_keyframe_distance;
3323
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;
3330       else
3331         dash_stream->keyframe_average_distance =
3332             (dash_stream->keyframe_average_distance +
3333             current_keyframe_distance * 3) / 4;
3334     } else {
3335       dash_stream->keyframe_average_distance = current_keyframe_distance;
3336     }
3337
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);
3347   }
3348
3349   return TRUE;
3350 }
3351
3352
3353 static GstFlowReturn
3354 gst_dash_demux_handle_isobmff (GstAdaptiveDemux * demux,
3355     GstAdaptiveDemuxStream * stream)
3356 {
3357   GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3358   GstFlowReturn ret = GST_FLOW_OK;
3359   GstBuffer *buffer;
3360   gboolean sidx_advance = FALSE;
3361
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;
3367
3368     ret = gst_dash_demux_parse_isobmff (demux, dash_stream, &sidx_seek_needed);
3369     if (ret != GST_FLOW_OK)
3370       return ret;
3371
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;
3375
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)
3378       return ret;
3379
3380     /* Here we end up only if we're right at the mdat start */
3381
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)) {
3389       guint idx = -1;
3390
3391       if (GST_CLOCK_TIME_IS_VALID (dash_stream->target_time)) {
3392         idx =
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) {
3397         idx = 0;
3398       }
3399
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 */
3403
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
3408          * the remainder */
3409         dash_stream->current_sync_sample = 0;
3410         GST_DEBUG_OBJECT (stream->pad, "Using first keyframe after header");
3411       }
3412     }
3413
3414     if (gst_adapter_available (dash_stream->adapter) == 0)
3415       return ret;
3416
3417     /* We have some data from the mdat available in the adapter, handle it
3418      * below in the push code */
3419   } else {
3420     /* Somewhere in the middle of the mdat */
3421   }
3422
3423   /* At 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);
3430     gsize available;
3431
3432     /* Need to handle everything in the adapter according to the parsed SIDX
3433      * and advance subsegments accordingly */
3434
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);
3438     } else {
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);
3442       } else {
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;
3449         } else {
3450           buffer =
3451               gst_adapter_take_buffer (dash_stream->adapter,
3452               sidx_end_offset - dash_stream->current_offset);
3453           sidx_advance = TRUE;
3454         }
3455       }
3456     }
3457   } else {
3458     /* Take it all and handle it further below */
3459     buffer =
3460         gst_adapter_take_buffer (dash_stream->adapter,
3461         gst_adapter_available (dash_stream->adapter));
3462
3463     /* Attention: All code paths below need to update dash_stream->current_offset */
3464   }
3465
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);
3475       return GST_FLOW_OK;
3476     } else {
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);
3482
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);
3488         return GST_FLOW_OK;
3489       } else if (end_offset > sync_sample->end_offset + 1) {
3490         guint64 remaining =
3491             sync_sample->end_offset + 1 - dash_stream->current_offset;
3492         GstBuffer *sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0,
3493             remaining);
3494         gst_buffer_unref (buffer);
3495         buffer = sub;
3496       }
3497     }
3498   }
3499
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;
3503
3504   ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
3505   if (ret != GST_FLOW_OK)
3506     return ret;
3507
3508   if (sidx_advance) {
3509     ret =
3510         gst_adaptive_demux_stream_advance_fragment (demux, stream,
3511         SIDX_CURRENT_ENTRY (dash_stream)->duration);
3512     if (ret != GST_FLOW_OK)
3513       return ret;
3514
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);
3518   }
3519
3520   return ret;
3521 }
3522
3523 static GstFlowReturn
3524 gst_dash_demux_data_received (GstAdaptiveDemux * demux,
3525     GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
3526 {
3527   GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3528   GstFlowReturn ret = GST_FLOW_OK;
3529   guint index_header_or_data;
3530
3531   if (stream->downloading_index)
3532     index_header_or_data = 1;
3533   else if (stream->downloading_header)
3534     index_header_or_data = 2;
3535   else
3536     index_header_or_data = 3;
3537
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;
3546   }
3547
3548   if (dash_stream->current_offset == -1)
3549     dash_stream->current_offset =
3550         GST_BUFFER_OFFSET_IS_VALID (buffer) ? GST_BUFFER_OFFSET (buffer) : 0;
3551
3552   gst_adapter_push (dash_stream->adapter, buffer);
3553   buffer = NULL;
3554
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) {
3559     gsize available;
3560
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);
3570
3571       if (dash_stream->current_offset + available < sidx_end_offset) {
3572         buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
3573       } else {
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);
3577         } else {
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;
3584             break;
3585           } else {
3586             buffer =
3587                 gst_adapter_take_buffer (dash_stream->adapter,
3588                 sidx_end_offset - dash_stream->current_offset);
3589             advance = TRUE;
3590           }
3591         }
3592       }
3593
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);
3598
3599       ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
3600
3601       if (advance) {
3602         if (has_next) {
3603           GstFlowReturn new_ret;
3604           new_ret =
3605               gst_adaptive_demux_stream_advance_fragment (demux, stream,
3606               SIDX_CURRENT_ENTRY (dash_stream)->duration);
3607
3608           /* only overwrite if it was OK before */
3609           if (ret == GST_FLOW_OK)
3610             ret = new_ret;
3611         } else {
3612           break;
3613         }
3614       }
3615     }
3616   } else {
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));
3620
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);
3625
3626     ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
3627   }
3628
3629   return ret;
3630 }
3631
3632 static void
3633 gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream)
3634 {
3635   GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3636
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);
3645 }
3646
3647 static GstDashDemuxClockDrift *
3648 gst_dash_demux_clock_drift_new (GstDashDemux * demux)
3649 {
3650   GstDashDemuxClockDrift *clock_drift;
3651
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)));
3657   return clock_drift;
3658 }
3659
3660 static void
3661 gst_dash_demux_clock_drift_free (GstDashDemuxClockDrift * clock_drift)
3662 {
3663   if (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);
3670   }
3671 }
3672
3673 /*
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.
3678  *
3679  * The DASH standard does not specify which version of NTP. This
3680  * function only works with NTPv4 servers.
3681 */
3682 static GstDateTime *
3683 gst_dash_demux_poll_ntp_server (GstDashDemuxClockDrift * clock_drift,
3684     gchar ** urls)
3685 {
3686   GstClockTime ntp_clock_time;
3687   GDateTime *dt, *dt2;
3688
3689   if (!clock_drift->ntp_clock) {
3690     GResolver *resolver;
3691     GList *inet_addrs;
3692     GError *err = NULL;
3693     gchar *ip_addr;
3694
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");
3707       if (inet_addrs)
3708         g_resolver_free_addresses (inet_addrs);
3709       if (err)
3710         g_error_free (err);
3711       return NULL;
3712     }
3713     ip_addr =
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);
3717     g_free (ip_addr);
3718     g_resolver_free_addresses (inet_addrs);
3719     if (!clock_drift->ntp_clock) {
3720       GST_ERROR ("Failed to create NTP clock");
3721       return NULL;
3722     }
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");
3727       return NULL;
3728     }
3729   }
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");
3733     return NULL;
3734   }
3735   ntp_clock_time -= NTP_TO_UNIX_EPOCH * GST_SECOND;
3736   dt = g_date_time_new_from_unix_utc (ntp_clock_time / GST_SECOND);
3737   if (!dt) {
3738     GST_ERROR ("Failed to create GstDateTime");
3739     return NULL;
3740   }
3741   ntp_clock_time =
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);
3746 }
3747
3748 struct Rfc5322TimeZone
3749 {
3750   const gchar *name;
3751   gfloat tzoffset;
3752 };
3753
3754 /*
3755  Parse an RFC5322 (section 3.3) date-time from the Date: field in the
3756  HTTP response. 
3757  See https://tools.ietf.org/html/rfc5322#section-3.3
3758 */
3759 static GstDateTime *
3760 gst_dash_demux_parse_http_head (GstDashDemuxClockDrift * clock_drift,
3761     GstFragment * download)
3762 {
3763   static const gchar *months[] = { NULL, "Jan", "Feb", "Mar", "Apr",
3764     "May", "Jun", "Jul", "Aug",
3765     "Sep", "Oct", "Nov", "Dec", NULL
3766   };
3767   static const struct Rfc5322TimeZone timezones[] = {
3768     {"Z", 0},
3769     {"UT", 0},
3770     {"GMT", 0},
3771     {"BST", 1},
3772     {"EST", -5},
3773     {"EDT", -4},
3774     {"CST", -6},
3775     {"CDT", -5},
3776     {"MST", -7},
3777     {"MDT", -6},
3778     {"PST", -8},
3779     {"PDT", -7},
3780     {NULL, 0}
3781   };
3782   GstDateTime *value = NULL;
3783   const GstStructure *response_headers;
3784   const gchar *http_date;
3785   const GValue *val;
3786   gint ret;
3787   const gchar *pos;
3788   gint year = -1, month = -1, day = -1, hour = -1, minute = -1, second = -1;
3789   gchar zone[6];
3790   gchar monthstr[4];
3791   gfloat tzoffset = 0;
3792   gboolean parsed_tz = FALSE;
3793
3794   g_return_val_if_fail (download != NULL, NULL);
3795   g_return_val_if_fail (download->headers != NULL, NULL);
3796
3797   val = gst_structure_get_value (download->headers, "response-headers");
3798   if (!val) {
3799     return NULL;
3800   }
3801   response_headers = gst_value_get_structure (val);
3802   http_date = gst_structure_get_string (response_headers, "Date");
3803   if (!http_date) {
3804     return NULL;
3805   }
3806
3807   /* skip optional text version of day of the week */
3808   pos = strchr (http_date, ',');
3809   if (pos)
3810     pos++;
3811   else
3812     pos = http_date;
3813   ret =
3814       sscanf (pos, "%02d %3s %04d %02d:%02d:%02d %5s", &day, monthstr, &year,
3815       &hour, &minute, &second, zone);
3816   if (ret == 7) {
3817     gchar *z = zone;
3818     gint i;
3819
3820     for (i = 1; months[i]; ++i) {
3821       if (g_ascii_strncasecmp (months[i], monthstr, strlen (months[i])) == 0) {
3822         month = i;
3823         break;
3824       }
3825     }
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;
3830         parsed_tz = TRUE;
3831       }
3832     }
3833     if (!parsed_tz) {
3834       gint hh, mm;
3835       gboolean neg = FALSE;
3836       /* check if it is in the form +-HHMM */
3837       if (*z == '+' || *z == '-') {
3838         if (*z == '+')
3839           ++z;
3840         else if (*z == '-') {
3841           ++z;
3842           neg = TRUE;
3843         }
3844         ret = sscanf (z, "%02d%02d", &hh, &mm);
3845         if (ret == 2) {
3846           tzoffset = hh;
3847           tzoffset += mm / 60.0;
3848           if (neg)
3849             tzoffset = -tzoffset;
3850           parsed_tz = TRUE;
3851         }
3852       }
3853     }
3854     /* Accept year in both 2 digit or 4 digit format */
3855     if (year < 100)
3856       year += 2000;
3857   }
3858   if (month > 0 && parsed_tz) {
3859     value = gst_date_time_new (tzoffset,
3860         year, month, day, hour, minute, second);
3861   }
3862   return value;
3863 }
3864
3865 /*
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.
3869
3870        0                   1                   2                   3
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       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3873       |                            Seconds                            |
3874       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3875       |                            Fraction                           |
3876       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3877
3878                              NTP Timestamp Format
3879 */
3880 static GstDateTime *
3881 gst_dash_demux_parse_http_ntp (GstDashDemuxClockDrift * clock_drift,
3882     GstBuffer * buffer)
3883 {
3884   gint64 seconds;
3885   guint64 fraction;
3886   GDateTime *dt, *dt2;
3887   GstMapInfo mapinfo;
3888
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);
3894     return NULL;
3895   }
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);
3907 }
3908
3909 /*
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.
3913 */
3914 static GstDateTime *
3915 gst_dash_demux_parse_http_xsdate (GstDashDemuxClockDrift * clock_drift,
3916     GstBuffer * buffer)
3917 {
3918   GstDateTime *value = NULL;
3919   GstMapInfo mapinfo;
3920
3921   /* the string from the server might not be zero terminated */
3922   if (gst_buffer_map (buffer, &mapinfo, GST_MAP_READ)) {
3923     gchar *str;
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);
3927     g_free (str);
3928   }
3929   return value;
3930 }
3931
3932 static gboolean
3933 gst_dash_demux_poll_clock_drift (GstDashDemux * demux)
3934 {
3935   GstDashDemuxClockDrift *clock_drift;
3936   GDateTime *start = NULL, *end;
3937   GstBuffer *buffer = NULL;
3938   GstDateTime *value = NULL;
3939   gboolean ret = FALSE;
3940   gint64 now;
3941   GstMPDUTCTimingType method;
3942   gchar **urls;
3943
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;
3947   now =
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.
3955
3956        Currently the fact that the manifest refresh follows a download failure
3957        does not make it into dashdemux. */
3958     return TRUE;
3959   }
3960   urls = gst_mpd_client_get_utc_timing_sources (demux->client,
3961       SUPPORTED_CLOCK_FORMATS, &method);
3962   if (!urls) {
3963     return FALSE;
3964   }
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);
3969
3970   if (method == GST_MPD_UTCTIMING_TYPE_NTP) {
3971     value = gst_dash_demux_poll_ntp_server (clock_drift, urls);
3972     if (!value) {
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);
3976       goto quit;
3977     }
3978   }
3979   start =
3980       gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3981   if (!value) {
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) {
3987       range_start = -1;
3988     }
3989     download =
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);
3993     if (download) {
3994       if (method == GST_MPD_UTCTIMING_TYPE_HTTP_HEAD && download->headers) {
3995         value = gst_dash_demux_parse_http_head (clock_drift, download);
3996       } else {
3997         buffer = gst_fragment_get_buffer (download);
3998       }
3999       g_object_unref (download);
4000     }
4001   }
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]);
4006     goto quit;
4007   }
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);
4014   }
4015   if (buffer)
4016     gst_buffer_unref (buffer);
4017   if (value) {
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
4023      */
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 */
4029     if (server_now) {
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);
4038       ret = TRUE;
4039     } else {
4040       GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
4041     }
4042     g_date_time_unref (client_now);
4043     gst_date_time_unref (value);
4044   } else {
4045     GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
4046   }
4047   g_date_time_unref (end);
4048 quit:
4049   if (start)
4050     g_date_time_unref (start);
4051   /* if multiple URLs were specified, use a simple round-robin to
4052      poll each server */
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;
4056   } else {
4057     clock_drift->selected_url =
4058         (1 + clock_drift->selected_url) % g_strv_length (urls);
4059     if (ret) {
4060       clock_drift->next_update = now + SLOW_CLOCK_UPDATE_INTERVAL;
4061     } else {
4062       clock_drift->next_update = now + FAST_CLOCK_UPDATE_INTERVAL;
4063     }
4064   }
4065   g_mutex_unlock (&clock_drift->clock_lock);
4066   return ret;
4067 }
4068
4069 static GTimeSpan
4070 gst_dash_demux_get_clock_compensation (GstDashDemux * demux)
4071 {
4072   GTimeSpan rv = 0;
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);
4077   }
4078   GST_LOG_OBJECT (demux, "Clock drift %" GST_STIME_FORMAT, GST_STIME_ARGS (rv));
4079   return rv;
4080 }
4081
4082 static GDateTime *
4083 gst_dash_demux_get_server_now_utc (GstDashDemux * demux)
4084 {
4085   GDateTime *client_now;
4086   GDateTime *server_now;
4087
4088   client_now =
4089       gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
4090   server_now =
4091       g_date_time_add (client_now,
4092       gst_dash_demux_get_clock_compensation (demux));
4093   g_date_time_unref (client_now);
4094   return server_now;
4095 }