adaptive: extract variant info
[platform/upstream/gstreamer.git] / 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
144 #ifdef HAVE_CONFIG_H
145 #  include "config.h"
146 #endif
147
148 #include <string.h>
149 #include <stdio.h>
150 #include <stdlib.h>
151 #include <inttypes.h>
152 #include <gio/gio.h>
153 #include <gst/base/gsttypefindhelper.h>
154 #include <gst/tag/tag.h>
155 #include <gst/net/gstnet.h>
156 #include "gst/gst-i18n-plugin.h"
157 #include "gstdashdemux.h"
158 #include "gstdash_debug.h"
159
160 static GstStaticPadTemplate gst_dash_demux_videosrc_template =
161 GST_STATIC_PAD_TEMPLATE ("video_%02u",
162     GST_PAD_SRC,
163     GST_PAD_SOMETIMES,
164     GST_STATIC_CAPS_ANY);
165
166 static GstStaticPadTemplate gst_dash_demux_audiosrc_template =
167 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
168     GST_PAD_SRC,
169     GST_PAD_SOMETIMES,
170     GST_STATIC_CAPS_ANY);
171
172 static GstStaticPadTemplate gst_dash_demux_subtitlesrc_template =
173 GST_STATIC_PAD_TEMPLATE ("subtitle_%02u",
174     GST_PAD_SRC,
175     GST_PAD_SOMETIMES,
176     GST_STATIC_CAPS_ANY);
177
178 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
179     GST_PAD_SINK,
180     GST_PAD_ALWAYS,
181     GST_STATIC_CAPS ("application/dash+xml"));
182
183 GST_DEBUG_CATEGORY (gst_dash_demux_debug);
184 #define GST_CAT_DEFAULT gst_dash_demux_debug
185
186 enum
187 {
188   PROP_0,
189
190   PROP_MAX_BUFFERING_TIME,
191   PROP_BANDWIDTH_USAGE,
192   PROP_MAX_BITRATE,
193   PROP_MAX_VIDEO_WIDTH,
194   PROP_MAX_VIDEO_HEIGHT,
195   PROP_MAX_VIDEO_FRAMERATE,
196   PROP_PRESENTATION_DELAY,
197   PROP_LAST
198 };
199
200 /* Default values for properties */
201 #define DEFAULT_MAX_BUFFERING_TIME       30     /* in seconds */
202 #define DEFAULT_BANDWIDTH_USAGE         0.8f    /* 0 to 1     */
203 #define DEFAULT_MAX_BITRATE               0     /* in bit/s  */
204 #define DEFAULT_MAX_VIDEO_WIDTH           0
205 #define DEFAULT_MAX_VIDEO_HEIGHT          0
206 #define DEFAULT_MAX_VIDEO_FRAMERATE_N     0
207 #define DEFAULT_MAX_VIDEO_FRAMERATE_D     1
208 #define DEFAULT_PRESENTATION_DELAY     "10s"    /* 10s */
209
210 /* Clock drift compensation for live streams */
211 #define SLOW_CLOCK_UPDATE_INTERVAL  (1000000 * 30 * 60) /* 30 minutes */
212 #define FAST_CLOCK_UPDATE_INTERVAL  (1000000 * 30)      /* 30 seconds */
213 #define SUPPORTED_CLOCK_FORMATS (GST_MPD_UTCTIMING_TYPE_NTP | GST_MPD_UTCTIMING_TYPE_HTTP_HEAD | GST_MPD_UTCTIMING_TYPE_HTTP_XSDATE | GST_MPD_UTCTIMING_TYPE_HTTP_ISO | GST_MPD_UTCTIMING_TYPE_HTTP_NTP)
214 #define NTP_TO_UNIX_EPOCH G_GUINT64_CONSTANT(2208988800)        /* difference (in seconds) between NTP epoch and Unix epoch */
215
216 struct _GstDashDemuxClockDrift
217 {
218   GMutex clock_lock;            /* used to protect access to struct */
219   guint selected_url;
220   gint64 next_update;
221   /* @clock_compensation: amount (in usecs) to add to client's idea of
222      now to map it to the server's idea of now */
223   GTimeSpan clock_compensation;
224   GstClock *ntp_clock;
225 };
226
227 typedef struct
228 {
229   guint64 start_offset, end_offset;
230   /* TODO: Timestamp and duration */
231 } GstDashStreamSyncSample;
232
233 /* GObject */
234 static void gst_dash_demux_set_property (GObject * object, guint prop_id,
235     const GValue * value, GParamSpec * pspec);
236 static void gst_dash_demux_get_property (GObject * object, guint prop_id,
237     GValue * value, GParamSpec * pspec);
238 static void gst_dash_demux_dispose (GObject * obj);
239
240 /* GstAdaptiveDemux */
241 static GstClockTime gst_dash_demux_get_duration (GstAdaptiveDemux * ademux);
242 static gboolean gst_dash_demux_is_live (GstAdaptiveDemux * ademux);
243 static void gst_dash_demux_reset (GstAdaptiveDemux * ademux);
244 static gboolean gst_dash_demux_process_manifest (GstAdaptiveDemux * ademux,
245     GstBuffer * buf);
246 static gboolean gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
247 static GstFlowReturn
248 gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream);
249 static GstFlowReturn gst_dash_demux_stream_seek (GstAdaptiveDemuxStream *
250     stream, gboolean forward, GstSeekFlags flags, GstClockTime ts,
251     GstClockTime * final_ts);
252 static gboolean gst_dash_demux_stream_has_next_fragment (GstAdaptiveDemuxStream
253     * stream);
254 static GstFlowReturn
255 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream);
256 static gboolean
257 gst_dash_demux_stream_advance_subfragment (GstAdaptiveDemuxStream * stream);
258 static gboolean gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream *
259     stream, guint64 bitrate);
260 static gint64 gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux *
261     demux);
262 static GstFlowReturn gst_dash_demux_update_manifest_data (GstAdaptiveDemux *
263     demux, GstBuffer * buf);
264 static gint64
265 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
266     stream);
267 static void gst_dash_demux_advance_period (GstAdaptiveDemux * demux);
268 static gboolean gst_dash_demux_has_next_period (GstAdaptiveDemux * demux);
269 static GstFlowReturn gst_dash_demux_data_received (GstAdaptiveDemux * demux,
270     GstAdaptiveDemuxStream * stream, GstBuffer * buffer);
271 static gboolean
272 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
273     GstAdaptiveDemuxStream * stream);
274 static GstFlowReturn
275 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
276     GstAdaptiveDemuxStream * stream);
277 static gboolean gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream *
278     stream);
279
280 /* GstDashDemux */
281 static gboolean gst_dash_demux_setup_all_streams (GstDashDemux * demux);
282 static void gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream);
283
284 static GstCaps *gst_dash_demux_get_input_caps (GstDashDemux * demux,
285     GstActiveStream * stream);
286 static GstPad *gst_dash_demux_create_pad (GstDashDemux * demux,
287     GstActiveStream * stream);
288 static GstDashDemuxClockDrift *gst_dash_demux_clock_drift_new (GstDashDemux *
289     demux);
290 static void gst_dash_demux_clock_drift_free (GstDashDemuxClockDrift *);
291 static gboolean gst_dash_demux_poll_clock_drift (GstDashDemux * demux);
292 static GTimeSpan gst_dash_demux_get_clock_compensation (GstDashDemux * demux);
293 static GDateTime *gst_dash_demux_get_server_now_utc (GstDashDemux * demux);
294
295 #define SIDX(s) (&(s)->sidx_parser.sidx)
296
297 static inline GstSidxBoxEntry *
298 SIDX_ENTRY (GstDashDemuxStream * s, gint i)
299 {
300   g_assert (i < SIDX (s)->entries_count);
301   return &(SIDX (s)->entries[(i)]);
302 }
303
304 #define SIDX_CURRENT_ENTRY(s) SIDX_ENTRY(s, SIDX(s)->entry_index)
305
306 static void gst_dash_demux_send_content_protection_event (gpointer cp_data,
307     gpointer stream);
308
309 #define gst_dash_demux_parent_class parent_class
310 G_DEFINE_TYPE_WITH_CODE (GstDashDemux, gst_dash_demux, GST_TYPE_ADAPTIVE_DEMUX,
311     GST_DEBUG_CATEGORY_INIT (gst_dash_demux_debug, "dashdemux", 0,
312         "dashdemux element")
313     );
314
315 static void
316 gst_dash_demux_dispose (GObject * obj)
317 {
318   GstDashDemux *demux = GST_DASH_DEMUX (obj);
319
320   gst_dash_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
321
322   if (demux->client) {
323     gst_mpd_client_free (demux->client);
324     demux->client = NULL;
325   }
326
327   g_mutex_clear (&demux->client_lock);
328
329   gst_dash_demux_clock_drift_free (demux->clock_drift);
330   demux->clock_drift = NULL;
331   g_free (demux->default_presentation_delay);
332   G_OBJECT_CLASS (parent_class)->dispose (obj);
333 }
334
335 static gboolean
336 gst_dash_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
337     gint64 * stop)
338 {
339   GstDashDemux *self = GST_DASH_DEMUX (demux);
340   GDateTime *now;
341   GDateTime *mstart;
342   GTimeSpan stream_now;
343   GstClockTime seg_duration;
344
345   if (self->client->mpd_node->availabilityStartTime == NULL)
346     return FALSE;
347
348   seg_duration = gst_mpd_client_get_maximum_segment_duration (self->client);
349   now = gst_dash_demux_get_server_now_utc (self);
350   mstart =
351       gst_date_time_to_g_date_time (self->client->
352       mpd_node->availabilityStartTime);
353   stream_now = g_date_time_difference (now, mstart);
354   g_date_time_unref (now);
355   g_date_time_unref (mstart);
356
357   if (stream_now <= 0)
358     return FALSE;
359
360   *stop = stream_now * GST_USECOND;
361   if (self->client->mpd_node->timeShiftBufferDepth == GST_MPD_DURATION_NONE) {
362     *start = 0;
363   } else {
364     *start =
365         *stop - (self->client->mpd_node->timeShiftBufferDepth * GST_MSECOND);
366     if (*start < 0)
367       *start = 0;
368   }
369
370   /* As defined in 5.3.9.5.3 of the DASH specification, a segment does
371      not become available until the sum of:
372      * the value of the MPD@availabilityStartTime,
373      * the PeriodStart time of the containing Period
374      * the MPD start time of the Media Segment, and
375      * the MPD duration of the Media Segment.
376      Therefore we need to subtract the media segment duration from the stop
377      time.
378    */
379   *stop -= seg_duration;
380   return TRUE;
381 }
382
383 static GstClockTime
384 gst_dash_demux_get_presentation_offset (GstAdaptiveDemux * demux,
385     GstAdaptiveDemuxStream * stream)
386 {
387   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
388   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
389
390   return gst_mpd_parser_get_stream_presentation_offset (dashdemux->client,
391       dashstream->index);
392 }
393
394 static GstClockTime
395 gst_dash_demux_get_period_start_time (GstAdaptiveDemux * demux)
396 {
397   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
398
399   return gst_mpd_parser_get_period_start_time (dashdemux->client);
400 }
401
402 static void
403 gst_dash_demux_class_init (GstDashDemuxClass * klass)
404 {
405   GObjectClass *gobject_class;
406   GstElementClass *gstelement_class;
407   GstAdaptiveDemuxClass *gstadaptivedemux_class;
408
409   gobject_class = (GObjectClass *) klass;
410   gstelement_class = (GstElementClass *) klass;
411   gstadaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
412
413   gobject_class->set_property = gst_dash_demux_set_property;
414   gobject_class->get_property = gst_dash_demux_get_property;
415   gobject_class->dispose = gst_dash_demux_dispose;
416
417 #ifndef GST_REMOVE_DEPRECATED
418   g_object_class_install_property (gobject_class, PROP_MAX_BUFFERING_TIME,
419       g_param_spec_uint ("max-buffering-time", "Maximum buffering time",
420           "Maximum number of seconds of buffer accumulated during playback"
421           "(deprecated)",
422           2, G_MAXUINT, DEFAULT_MAX_BUFFERING_TIME,
423           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
424
425   g_object_class_install_property (gobject_class, PROP_BANDWIDTH_USAGE,
426       g_param_spec_float ("bandwidth-usage",
427           "Bandwidth usage [0..1]",
428           "Percentage of the available bandwidth to use when "
429           "selecting representations (deprecated)",
430           0, 1, DEFAULT_BANDWIDTH_USAGE,
431           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
432 #endif
433
434   g_object_class_install_property (gobject_class, PROP_MAX_BITRATE,
435       g_param_spec_uint ("max-bitrate", "Max bitrate",
436           "Max of bitrate supported by target video decoder (0 = no maximum)",
437           0, G_MAXUINT, DEFAULT_MAX_BITRATE,
438           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
439
440   g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_WIDTH,
441       g_param_spec_uint ("max-video-width", "Max video width",
442           "Max video width to select (0 = no maximum)",
443           0, G_MAXUINT, DEFAULT_MAX_VIDEO_WIDTH,
444           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445
446   g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_HEIGHT,
447       g_param_spec_uint ("max-video-height", "Max video height",
448           "Max video height to select (0 = no maximum)",
449           0, G_MAXUINT, DEFAULT_MAX_VIDEO_HEIGHT,
450           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451
452   g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_FRAMERATE,
453       gst_param_spec_fraction ("max-video-framerate", "Max video framerate",
454           "Max video framerate to select (0/1 = no maximum)",
455           0, 1, G_MAXINT, 1, DEFAULT_MAX_VIDEO_FRAMERATE_N,
456           DEFAULT_MAX_VIDEO_FRAMERATE_D,
457           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
458
459   g_object_class_install_property (gobject_class, PROP_PRESENTATION_DELAY,
460       g_param_spec_string ("presentation-delay", "Presentation delay",
461           "Default presentation delay (in seconds, milliseconds or fragments) (e.g. 12s, 2500ms, 3f)",
462           DEFAULT_PRESENTATION_DELAY,
463           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
464
465   gst_element_class_add_static_pad_template (gstelement_class,
466       &gst_dash_demux_audiosrc_template);
467   gst_element_class_add_static_pad_template (gstelement_class,
468       &gst_dash_demux_videosrc_template);
469   gst_element_class_add_static_pad_template (gstelement_class,
470       &gst_dash_demux_subtitlesrc_template);
471
472   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
473
474   gst_element_class_set_static_metadata (gstelement_class,
475       "DASH Demuxer",
476       "Codec/Demuxer/Adaptive",
477       "Dynamic Adaptive Streaming over HTTP demuxer",
478       "David Corvoysier <david.corvoysier@orange.com>\n\
479                 Hamid Zakari <hamid.zakari@gmail.com>\n\
480                 Gianluca Gennari <gennarone@gmail.com>");
481
482
483   gstadaptivedemux_class->get_duration = gst_dash_demux_get_duration;
484   gstadaptivedemux_class->is_live = gst_dash_demux_is_live;
485   gstadaptivedemux_class->reset = gst_dash_demux_reset;
486   gstadaptivedemux_class->seek = gst_dash_demux_seek;
487
488   gstadaptivedemux_class->process_manifest = gst_dash_demux_process_manifest;
489   gstadaptivedemux_class->update_manifest_data =
490       gst_dash_demux_update_manifest_data;
491   gstadaptivedemux_class->get_manifest_update_interval =
492       gst_dash_demux_get_manifest_update_interval;
493
494   gstadaptivedemux_class->has_next_period = gst_dash_demux_has_next_period;
495   gstadaptivedemux_class->advance_period = gst_dash_demux_advance_period;
496   gstadaptivedemux_class->stream_has_next_fragment =
497       gst_dash_demux_stream_has_next_fragment;
498   gstadaptivedemux_class->stream_advance_fragment =
499       gst_dash_demux_stream_advance_fragment;
500   gstadaptivedemux_class->stream_get_fragment_waiting_time =
501       gst_dash_demux_stream_get_fragment_waiting_time;
502   gstadaptivedemux_class->stream_seek = gst_dash_demux_stream_seek;
503   gstadaptivedemux_class->stream_select_bitrate =
504       gst_dash_demux_stream_select_bitrate;
505   gstadaptivedemux_class->stream_update_fragment_info =
506       gst_dash_demux_stream_update_fragment_info;
507   gstadaptivedemux_class->stream_free = gst_dash_demux_stream_free;
508   gstadaptivedemux_class->get_live_seek_range =
509       gst_dash_demux_get_live_seek_range;
510   gstadaptivedemux_class->get_presentation_offset =
511       gst_dash_demux_get_presentation_offset;
512   gstadaptivedemux_class->get_period_start_time =
513       gst_dash_demux_get_period_start_time;
514
515   gstadaptivedemux_class->start_fragment = gst_dash_demux_stream_fragment_start;
516   gstadaptivedemux_class->finish_fragment =
517       gst_dash_demux_stream_fragment_finished;
518   gstadaptivedemux_class->data_received = gst_dash_demux_data_received;
519   gstadaptivedemux_class->need_another_chunk =
520       gst_dash_demux_need_another_chunk;
521 }
522
523 static void
524 gst_dash_demux_init (GstDashDemux * demux)
525 {
526   /* Properties */
527   demux->max_buffering_time = DEFAULT_MAX_BUFFERING_TIME * GST_SECOND;
528   demux->max_bitrate = DEFAULT_MAX_BITRATE;
529   demux->max_video_width = DEFAULT_MAX_VIDEO_WIDTH;
530   demux->max_video_height = DEFAULT_MAX_VIDEO_HEIGHT;
531   demux->max_video_framerate_n = DEFAULT_MAX_VIDEO_FRAMERATE_N;
532   demux->max_video_framerate_d = DEFAULT_MAX_VIDEO_FRAMERATE_D;
533   demux->default_presentation_delay = g_strdup (DEFAULT_PRESENTATION_DELAY);
534
535   g_mutex_init (&demux->client_lock);
536
537   gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
538       sizeof (GstDashDemuxStream));
539 }
540
541 static void
542 gst_dash_demux_set_property (GObject * object, guint prop_id,
543     const GValue * value, GParamSpec * pspec)
544 {
545   GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
546   GstDashDemux *demux = GST_DASH_DEMUX (object);
547
548   switch (prop_id) {
549     case PROP_MAX_BUFFERING_TIME:
550       demux->max_buffering_time = g_value_get_uint (value) * GST_SECOND;
551       break;
552     case PROP_BANDWIDTH_USAGE:
553       adaptivedemux->bitrate_limit = g_value_get_float (value);
554       break;
555     case PROP_MAX_BITRATE:
556       demux->max_bitrate = g_value_get_uint (value);
557       break;
558     case PROP_MAX_VIDEO_WIDTH:
559       demux->max_video_width = g_value_get_uint (value);
560       break;
561     case PROP_MAX_VIDEO_HEIGHT:
562       demux->max_video_height = g_value_get_uint (value);
563       break;
564     case PROP_MAX_VIDEO_FRAMERATE:
565       demux->max_video_framerate_n = gst_value_get_fraction_numerator (value);
566       demux->max_video_framerate_d = gst_value_get_fraction_denominator (value);
567       break;
568     case PROP_PRESENTATION_DELAY:
569       g_free (demux->default_presentation_delay);
570       demux->default_presentation_delay = g_value_dup_string (value);
571       break;
572     default:
573       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
574       break;
575   }
576 }
577
578 static void
579 gst_dash_demux_get_property (GObject * object, guint prop_id, GValue * value,
580     GParamSpec * pspec)
581 {
582   GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
583   GstDashDemux *demux = GST_DASH_DEMUX (object);
584
585   switch (prop_id) {
586     case PROP_MAX_BUFFERING_TIME:
587       g_value_set_uint (value, demux->max_buffering_time / GST_SECOND);
588       break;
589     case PROP_BANDWIDTH_USAGE:
590       g_value_set_float (value, adaptivedemux->bitrate_limit);
591       break;
592     case PROP_MAX_BITRATE:
593       g_value_set_uint (value, demux->max_bitrate);
594       break;
595     case PROP_MAX_VIDEO_WIDTH:
596       g_value_set_uint (value, demux->max_video_width);
597       break;
598     case PROP_MAX_VIDEO_HEIGHT:
599       g_value_set_uint (value, demux->max_video_height);
600       break;
601     case PROP_MAX_VIDEO_FRAMERATE:
602       gst_value_set_fraction (value, demux->max_video_framerate_n,
603           demux->max_video_framerate_d);
604       break;
605     case PROP_PRESENTATION_DELAY:
606       if (demux->default_presentation_delay == NULL)
607         g_value_set_static_string (value, "");
608       else
609         g_value_set_string (value, demux->default_presentation_delay);
610       break;
611     default:
612       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
613       break;
614   }
615 }
616
617 static gboolean
618 gst_dash_demux_setup_mpdparser_streams (GstDashDemux * demux,
619     GstMpdClient * client)
620 {
621   gboolean has_streams = FALSE;
622   GList *adapt_sets, *iter;
623
624   adapt_sets = gst_mpd_client_get_adaptation_sets (client);
625   for (iter = adapt_sets; iter; iter = g_list_next (iter)) {
626     GstAdaptationSetNode *adapt_set_node = iter->data;
627
628     gst_mpd_client_setup_streaming (client, adapt_set_node);
629     has_streams = TRUE;
630   }
631
632   if (!has_streams) {
633     GST_ELEMENT_ERROR (demux, STREAM, DEMUX, ("Manifest has no playable "
634             "streams"), ("No streams could be activated from the manifest"));
635   }
636   return has_streams;
637 }
638
639 static gboolean
640 gst_dash_demux_setup_all_streams (GstDashDemux * demux)
641 {
642   guint i;
643
644   GST_DEBUG_OBJECT (demux, "Setting up streams for period %d",
645       gst_mpd_client_get_period_index (demux->client));
646
647   /* clean old active stream list, if any */
648   gst_active_streams_free (demux->client);
649
650   if (!gst_dash_demux_setup_mpdparser_streams (demux, demux->client)) {
651     return FALSE;
652   }
653
654   GST_DEBUG_OBJECT (demux, "Creating stream objects");
655   for (i = 0; i < gst_mpdparser_get_nb_active_stream (demux->client); i++) {
656     GstDashDemuxStream *stream;
657     GstActiveStream *active_stream;
658     GstCaps *caps;
659     GstStructure *s;
660     GstPad *srcpad;
661     gchar *lang = NULL;
662     GstTagList *tags = NULL;
663
664     active_stream = gst_mpdparser_get_active_stream_by_index (demux->client, i);
665     if (active_stream == NULL)
666       continue;
667
668     if (demux->trickmode_no_audio
669         && active_stream->mimeType == GST_STREAM_AUDIO) {
670       GST_DEBUG_OBJECT (demux,
671           "Skipping audio stream %d because of TRICKMODE_NO_AUDIO flag", i);
672       continue;
673     }
674
675     srcpad = gst_dash_demux_create_pad (demux, active_stream);
676     if (srcpad == NULL)
677       continue;
678
679     caps = gst_dash_demux_get_input_caps (demux, active_stream);
680     GST_LOG_OBJECT (demux, "Creating stream %d %" GST_PTR_FORMAT, i, caps);
681 #ifdef TIZEN_FEATURE_ADAPTIVE_MODIFICATION
682     caps = gst_caps_make_writable (caps);
683     gst_caps_set_simple (caps, "adaptive_stream", G_TYPE_BOOLEAN, TRUE,
684         "dash_stream", G_TYPE_BOOLEAN, TRUE, NULL);
685     GST_LOG_OBJECT (demux,
686         "*** modified caps for dash streaming = %" GST_PTR_FORMAT, caps);
687 #endif
688
689     if (active_stream->cur_adapt_set) {
690       GstAdaptationSetNode *adp_set = active_stream->cur_adapt_set;
691       lang = adp_set->lang;
692
693       /* Fallback to the language in ContentComponent node */
694       if (lang == NULL) {
695         GList *it;
696
697         for (it = adp_set->ContentComponents; it; it = it->next) {
698           GstContentComponentNode *cc_node = it->data;
699           if (cc_node->lang) {
700             lang = cc_node->lang;
701             break;
702           }
703         }
704       }
705
706 #ifdef TIZEN_FEATURE_ADAPTIVE_MODIFICATION
707       if (active_stream->mimeType == GST_STREAM_VIDEO
708           && adp_set->VariantInfo != NULL) {
709         GST_LOG_OBJECT (demux,
710             "post msg about video active stream variant info");
711
712         gst_element_post_message (GST_ELEMENT_CAST (demux),
713             gst_message_new_element (GST_OBJECT_CAST (demux),
714                 gst_structure_new (GST_ADAPTIVE_DEMUX_VARIANT_MESSAGE_NAME,
715                     "video-variant-info", G_TYPE_POINTER, adp_set->VariantInfo,
716                     NULL)));
717       }
718 #endif
719     }
720
721     if (lang) {
722       if (gst_tag_check_language_code (lang))
723         tags = gst_tag_list_new (GST_TAG_LANGUAGE_CODE, lang, NULL);
724       else
725         tags = gst_tag_list_new (GST_TAG_LANGUAGE_NAME, lang, NULL);
726     }
727
728     stream = (GstDashDemuxStream *)
729         gst_adaptive_demux_stream_new (GST_ADAPTIVE_DEMUX_CAST (demux), srcpad);
730     stream->active_stream = active_stream;
731     s = gst_caps_get_structure (caps, 0);
732     stream->allow_sidx =
733         gst_mpd_client_has_isoff_ondemand_profile (demux->client);
734     stream->is_isobmff = gst_structure_has_name (s, "video/quicktime")
735         || gst_structure_has_name (s, "audio/x-m4a");
736     stream->first_sync_sample_always_after_moof = TRUE;
737     if (stream->is_isobmff
738         || gst_mpd_client_has_isoff_ondemand_profile (demux->client))
739       stream->adapter = gst_adapter_new ();
740     gst_adaptive_demux_stream_set_caps (GST_ADAPTIVE_DEMUX_STREAM_CAST (stream),
741         caps);
742     if (tags)
743       gst_adaptive_demux_stream_set_tags (GST_ADAPTIVE_DEMUX_STREAM_CAST
744           (stream), tags);
745     stream->index = i;
746     stream->pending_seek_ts = GST_CLOCK_TIME_NONE;
747     stream->sidx_position = GST_CLOCK_TIME_NONE;
748     if (active_stream->cur_adapt_set &&
749         active_stream->cur_adapt_set->RepresentationBase &&
750         active_stream->cur_adapt_set->RepresentationBase->ContentProtection) {
751       GST_DEBUG_OBJECT (demux, "Adding ContentProtection events to source pad");
752       g_list_foreach (active_stream->cur_adapt_set->RepresentationBase->
753           ContentProtection, gst_dash_demux_send_content_protection_event,
754           stream);
755     }
756
757     gst_isoff_sidx_parser_init (&stream->sidx_parser);
758   }
759
760   return TRUE;
761 }
762
763 static void
764 gst_dash_demux_send_content_protection_event (gpointer data, gpointer userdata)
765 {
766   GstDescriptorType *cp = (GstDescriptorType *) data;
767   GstDashDemuxStream *stream = (GstDashDemuxStream *) userdata;
768   GstEvent *event;
769   GstBuffer *pssi;
770   glong pssi_len;
771   gchar *schemeIdUri;
772
773   if (cp->schemeIdUri == NULL)
774     return;
775
776   GST_TRACE_OBJECT (stream, "check schemeIdUri %s", cp->schemeIdUri);
777   /* RFC 2141 states: The leading "urn:" sequence is case-insensitive */
778   schemeIdUri = g_ascii_strdown (cp->schemeIdUri, -1);
779   if (g_str_has_prefix (schemeIdUri, "urn:uuid:")) {
780     pssi_len = strlen (cp->value);
781     pssi = gst_buffer_new_wrapped (g_memdup (cp->value, pssi_len), pssi_len);
782     GST_LOG_OBJECT (stream, "Queuing Protection event on source pad");
783     /* RFC 4122 states that the hex part of a UUID is in lower case,
784      * but some streams seem to ignore this and use upper case for the
785      * protection system ID */
786     event = gst_event_new_protection (cp->schemeIdUri + 9, pssi, "dash/mpd");
787     gst_adaptive_demux_stream_queue_event ((GstAdaptiveDemuxStream *) stream,
788         event);
789     gst_buffer_unref (pssi);
790   }
791   g_free (schemeIdUri);
792 }
793
794 static GstClockTime
795 gst_dash_demux_get_duration (GstAdaptiveDemux * ademux)
796 {
797   GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
798
799   g_return_val_if_fail (demux->client != NULL, GST_CLOCK_TIME_NONE);
800
801   return gst_mpd_client_get_media_presentation_duration (demux->client);
802 }
803
804 static gboolean
805 gst_dash_demux_is_live (GstAdaptiveDemux * ademux)
806 {
807   GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
808
809   g_return_val_if_fail (demux->client != NULL, FALSE);
810
811   return gst_mpd_client_is_live (demux->client);
812 }
813
814 static gboolean
815 gst_dash_demux_setup_streams (GstAdaptiveDemux * demux)
816 {
817   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
818   gboolean ret = TRUE;
819   GstDateTime *now = NULL;
820   guint period_idx;
821
822   /* setup video, audio and subtitle streams, starting from first Period if
823    * non-live */
824   period_idx = 0;
825   if (gst_mpd_client_is_live (dashdemux->client)) {
826     GDateTime *g_now;
827     if (dashdemux->client->mpd_node->availabilityStartTime == NULL) {
828       ret = FALSE;
829       GST_ERROR_OBJECT (demux, "MPD does not have availabilityStartTime");
830       goto done;
831     }
832     if (dashdemux->clock_drift == NULL) {
833       gchar **urls;
834       urls =
835           gst_mpd_client_get_utc_timing_sources (dashdemux->client,
836           SUPPORTED_CLOCK_FORMATS, NULL);
837       if (urls) {
838         GST_DEBUG_OBJECT (dashdemux, "Found a supported UTCTiming element");
839         dashdemux->clock_drift = gst_dash_demux_clock_drift_new (dashdemux);
840         gst_dash_demux_poll_clock_drift (dashdemux);
841       }
842     }
843     /* get period index for period encompassing the current time */
844     g_now = gst_dash_demux_get_server_now_utc (dashdemux);
845     now = gst_date_time_new_from_g_date_time (g_now);
846     if (dashdemux->client->mpd_node->suggestedPresentationDelay != -1) {
847       GstDateTime *target = gst_mpd_client_add_time_difference (now,
848           dashdemux->client->mpd_node->suggestedPresentationDelay * -1000);
849       gst_date_time_unref (now);
850       now = target;
851     } else if (dashdemux->default_presentation_delay) {
852       gint64 dfp =
853           gst_mpd_client_parse_default_presentation_delay (dashdemux->client,
854           dashdemux->default_presentation_delay);
855       GstDateTime *target = gst_mpd_client_add_time_difference (now,
856           dfp * -1000);
857       gst_date_time_unref (now);
858       now = target;
859     }
860     period_idx =
861         gst_mpd_client_get_period_index_at_time (dashdemux->client, now);
862     if (period_idx == G_MAXUINT) {
863 #ifndef GST_DISABLE_GST_DEBUG
864       gchar *date_str = gst_date_time_to_iso8601_string (now);
865       GST_DEBUG_OBJECT (demux, "Unable to find live period active at %s",
866           date_str);
867       g_free (date_str);
868 #endif
869       ret = FALSE;
870       goto done;
871     }
872   }
873
874   if (!gst_mpd_client_set_period_index (dashdemux->client, period_idx) ||
875       !gst_dash_demux_setup_all_streams (dashdemux)) {
876     ret = FALSE;
877     goto done;
878   }
879
880   /* If stream is live, try to find the segment that
881    * is closest to current time */
882   if (gst_mpd_client_is_live (dashdemux->client)) {
883     GDateTime *gnow;
884
885     GST_DEBUG_OBJECT (demux, "Seeking to current time of day for live stream ");
886
887     gnow = gst_date_time_to_g_date_time (now);
888     gst_mpd_client_seek_to_time (dashdemux->client, gnow);
889     g_date_time_unref (gnow);
890   } else {
891     GST_DEBUG_OBJECT (demux, "Seeking to first segment for on-demand stream ");
892
893     /* start playing from the first segment */
894     gst_mpd_client_seek_to_first_segment (dashdemux->client);
895   }
896
897 done:
898   if (now != NULL)
899     gst_date_time_unref (now);
900   return ret;
901 }
902
903 static gboolean
904 gst_dash_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
905 {
906   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
907   gboolean ret = FALSE;
908   gchar *manifest;
909   GstMapInfo mapinfo;
910
911   if (dashdemux->client)
912     gst_mpd_client_free (dashdemux->client);
913   dashdemux->client = gst_mpd_client_new ();
914   gst_mpd_client_set_uri_downloader (dashdemux->client, demux->downloader);
915
916   dashdemux->client->mpd_uri = g_strdup (demux->manifest_uri);
917   dashdemux->client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
918
919   GST_DEBUG_OBJECT (demux, "Fetched MPD file at URI: %s (base: %s)",
920       dashdemux->client->mpd_uri,
921       GST_STR_NULL (dashdemux->client->mpd_base_uri));
922
923   if (gst_buffer_map (buf, &mapinfo, GST_MAP_READ)) {
924     manifest = (gchar *) mapinfo.data;
925     if (gst_mpd_parse (dashdemux->client, manifest, mapinfo.size)) {
926       if (gst_mpd_client_setup_media_presentation (dashdemux->client, 0, 0,
927               NULL)) {
928         ret = TRUE;
929       } else {
930         GST_ELEMENT_ERROR (demux, STREAM, DECODE,
931             ("Incompatible manifest file."), (NULL));
932       }
933     }
934     gst_buffer_unmap (buf, &mapinfo);
935   } else {
936     GST_WARNING_OBJECT (demux, "Failed to map manifest buffer");
937   }
938
939   if (ret)
940     ret = gst_dash_demux_setup_streams (demux);
941
942   return ret;
943 }
944
945 static GstPad *
946 gst_dash_demux_create_pad (GstDashDemux * demux, GstActiveStream * stream)
947 {
948   GstPad *pad;
949   GstPadTemplate *tmpl;
950   gchar *name;
951
952   switch (stream->mimeType) {
953     case GST_STREAM_AUDIO:
954       name = g_strdup_printf ("audio_%02u", demux->n_audio_streams++);
955       tmpl = gst_static_pad_template_get (&gst_dash_demux_audiosrc_template);
956       break;
957     case GST_STREAM_VIDEO:
958       name = g_strdup_printf ("video_%02u", demux->n_video_streams++);
959       tmpl = gst_static_pad_template_get (&gst_dash_demux_videosrc_template);
960       break;
961     case GST_STREAM_APPLICATION:
962       if (gst_mpd_client_active_stream_contains_subtitles (stream)) {
963         name = g_strdup_printf ("subtitle_%02u", demux->n_subtitle_streams++);
964         tmpl =
965             gst_static_pad_template_get (&gst_dash_demux_subtitlesrc_template);
966       } else {
967         return NULL;
968       }
969       break;
970     default:
971       g_assert_not_reached ();
972       return NULL;
973   }
974
975   /* Create and activate new pads */
976   pad = gst_pad_new_from_template (tmpl, name);
977   g_free (name);
978   gst_object_unref (tmpl);
979
980   gst_pad_set_active (pad, TRUE);
981   GST_INFO_OBJECT (demux, "Creating srcpad %s:%s", GST_DEBUG_PAD_NAME (pad));
982   return pad;
983 }
984
985 static void
986 gst_dash_demux_reset (GstAdaptiveDemux * ademux)
987 {
988   GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
989
990   GST_DEBUG_OBJECT (demux, "Resetting demux");
991
992   demux->end_of_period = FALSE;
993   demux->end_of_manifest = FALSE;
994
995   if (demux->client) {
996     gst_mpd_client_free (demux->client);
997     demux->client = NULL;
998   }
999   gst_dash_demux_clock_drift_free (demux->clock_drift);
1000   demux->clock_drift = NULL;
1001   demux->client = gst_mpd_client_new ();
1002   gst_mpd_client_set_uri_downloader (demux->client, ademux->downloader);
1003
1004   demux->n_audio_streams = 0;
1005   demux->n_video_streams = 0;
1006   demux->n_subtitle_streams = 0;
1007
1008   demux->trickmode_no_audio = FALSE;
1009   demux->allow_trickmode_key_units = TRUE;
1010 }
1011
1012 static GstCaps *
1013 gst_dash_demux_get_video_input_caps (GstDashDemux * demux,
1014     GstActiveStream * stream)
1015 {
1016   guint width = 0, height = 0;
1017   gint fps_num = 0, fps_den = 1;
1018   gboolean have_fps = FALSE;
1019   GstCaps *caps = NULL;
1020
1021   if (stream == NULL)
1022     return NULL;
1023
1024   /* if bitstreamSwitching is true we dont need to swich pads on resolution change */
1025   if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
1026     width = gst_mpd_client_get_video_stream_width (stream);
1027     height = gst_mpd_client_get_video_stream_height (stream);
1028     have_fps =
1029         gst_mpd_client_get_video_stream_framerate (stream, &fps_num, &fps_den);
1030   }
1031   caps = gst_mpd_client_get_stream_caps (stream);
1032   if (caps == NULL)
1033     return NULL;
1034
1035   if (width > 0 && height > 0) {
1036     gst_caps_set_simple (caps, "width", G_TYPE_INT, width, "height",
1037         G_TYPE_INT, height, NULL);
1038   }
1039
1040   if (have_fps) {
1041     gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fps_num,
1042         fps_den, NULL);
1043   }
1044
1045   return caps;
1046 }
1047
1048 static GstCaps *
1049 gst_dash_demux_get_audio_input_caps (GstDashDemux * demux,
1050     GstActiveStream * stream)
1051 {
1052   guint rate = 0, channels = 0;
1053   GstCaps *caps = NULL;
1054
1055   if (stream == NULL)
1056     return NULL;
1057
1058   /* if bitstreamSwitching is true we dont need to swich pads on rate/channels change */
1059   if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
1060     channels = gst_mpd_client_get_audio_stream_num_channels (stream);
1061     rate = gst_mpd_client_get_audio_stream_rate (stream);
1062   }
1063   caps = gst_mpd_client_get_stream_caps (stream);
1064   if (caps == NULL)
1065     return NULL;
1066
1067   if (rate > 0) {
1068     gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
1069   }
1070   if (channels > 0) {
1071     gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
1072   }
1073
1074   return caps;
1075 }
1076
1077 static GstCaps *
1078 gst_dash_demux_get_application_input_caps (GstDashDemux * demux,
1079     GstActiveStream * stream)
1080 {
1081   GstCaps *caps = NULL;
1082
1083   if (stream == NULL)
1084     return NULL;
1085
1086   caps = gst_mpd_client_get_stream_caps (stream);
1087   if (caps == NULL)
1088     return NULL;
1089
1090   return caps;
1091 }
1092
1093 static GstCaps *
1094 gst_dash_demux_get_input_caps (GstDashDemux * demux, GstActiveStream * stream)
1095 {
1096   switch (stream->mimeType) {
1097     case GST_STREAM_VIDEO:
1098       return gst_dash_demux_get_video_input_caps (demux, stream);
1099     case GST_STREAM_AUDIO:
1100       return gst_dash_demux_get_audio_input_caps (demux, stream);
1101     case GST_STREAM_APPLICATION:
1102       return gst_dash_demux_get_application_input_caps (demux, stream);
1103     default:
1104       return GST_CAPS_NONE;
1105   }
1106 }
1107
1108 static void
1109 gst_dash_demux_stream_update_headers_info (GstAdaptiveDemuxStream * stream)
1110 {
1111   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1112   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1113   gchar *path = NULL;
1114
1115   gst_mpd_client_get_next_header (dashdemux->client,
1116       &path, dashstream->index,
1117       &stream->fragment.header_range_start, &stream->fragment.header_range_end);
1118
1119   if (path != NULL) {
1120     stream->fragment.header_uri =
1121         gst_uri_join_strings (gst_mpdparser_get_baseURL (dashdemux->client,
1122             dashstream->index), path);
1123     g_free (path);
1124     path = NULL;
1125   }
1126
1127   gst_mpd_client_get_next_header_index (dashdemux->client,
1128       &path, dashstream->index,
1129       &stream->fragment.index_range_start, &stream->fragment.index_range_end);
1130
1131   if (path != NULL) {
1132     stream->fragment.index_uri =
1133         gst_uri_join_strings (gst_mpdparser_get_baseURL (dashdemux->client,
1134             dashstream->index), path);
1135     g_free (path);
1136   }
1137 }
1138
1139 static GstFlowReturn
1140 gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream)
1141 {
1142   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1143   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1144   GstClockTime ts;
1145   GstMediaFragmentInfo fragment;
1146   gboolean isombff;
1147
1148   gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
1149
1150   isombff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1151
1152   /* Reset chunk size if any */
1153   stream->fragment.chunk_size = 0;
1154
1155   if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream) && isombff) {
1156     gst_dash_demux_stream_update_headers_info (stream);
1157     /* sidx entries may not be available in here */
1158     if (stream->fragment.index_uri
1159         && dashstream->sidx_position != GST_CLOCK_TIME_NONE) {
1160       /* request only the index to be downloaded as we need to reposition the
1161        * stream to a subsegment */
1162       return GST_FLOW_OK;
1163     }
1164   }
1165
1166   if (dashstream->moof_sync_samples
1167       && GST_ADAPTIVE_DEMUX (dashdemux)->
1168       segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1169     GstDashStreamSyncSample *sync_sample =
1170         &g_array_index (dashstream->moof_sync_samples, GstDashStreamSyncSample,
1171         dashstream->current_sync_sample);
1172
1173     gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1174         &fragment);
1175
1176     stream->fragment.uri = fragment.uri;
1177     stream->fragment.timestamp = GST_CLOCK_TIME_NONE;
1178     stream->fragment.duration = GST_CLOCK_TIME_NONE;
1179     stream->fragment.range_start = sync_sample->start_offset;
1180     stream->fragment.range_end = sync_sample->end_offset;
1181
1182     return GST_FLOW_OK;
1183   }
1184
1185   if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
1186           dashstream->index, &ts)) {
1187     if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream)) {
1188       gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
1189       gst_dash_demux_stream_update_headers_info (stream);
1190     }
1191
1192     gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1193         &fragment);
1194
1195     stream->fragment.uri = fragment.uri;
1196     /* If mpd does not specify indexRange (i.e., null index_uri),
1197      * sidx entries may not be available until download it */
1198     if (isombff && dashstream->sidx_position != GST_CLOCK_TIME_NONE
1199         && SIDX (dashstream)->entries) {
1200       GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
1201       stream->fragment.range_start =
1202           dashstream->sidx_base_offset + entry->offset;
1203       stream->fragment.timestamp = entry->pts;
1204       stream->fragment.duration = entry->duration;
1205       if (stream->demux->segment.rate < 0.0) {
1206         stream->fragment.range_end =
1207             stream->fragment.range_start + entry->size - 1;
1208       } else {
1209         stream->fragment.range_end = fragment.range_end;
1210       }
1211     } else {
1212       stream->fragment.timestamp = fragment.timestamp;
1213       stream->fragment.duration = fragment.duration;
1214       stream->fragment.range_start =
1215           MAX (fragment.range_start, dashstream->sidx_base_offset);
1216       stream->fragment.range_end = fragment.range_end;
1217     }
1218
1219     return GST_FLOW_OK;
1220   }
1221
1222   return GST_FLOW_EOS;
1223 }
1224
1225 static gint
1226 gst_dash_demux_index_entry_search (GstSidxBoxEntry * entry, GstClockTime * ts,
1227     gpointer user_data)
1228 {
1229   GstClockTime entry_ts = entry->pts + entry->duration;
1230   if (entry_ts <= *ts)
1231     return -1;
1232   else if (entry->pts > *ts)
1233     return 1;
1234   else
1235     return 0;
1236 }
1237
1238 static GstFlowReturn
1239 gst_dash_demux_stream_sidx_seek (GstDashDemuxStream * dashstream,
1240     gboolean forward, GstSeekFlags flags, GstClockTime ts,
1241     GstClockTime * final_ts)
1242 {
1243   GstSidxBox *sidx = SIDX (dashstream);
1244   GstSidxBoxEntry *entry;
1245   gint idx = sidx->entries_count;
1246   GstFlowReturn ret = GST_FLOW_OK;
1247
1248   if (sidx->entries_count == 0)
1249     return GST_FLOW_EOS;
1250
1251   entry =
1252       gst_util_array_binary_search (sidx->entries, sidx->entries_count,
1253       sizeof (GstSidxBoxEntry),
1254       (GCompareDataFunc) gst_dash_demux_index_entry_search,
1255       GST_SEARCH_MODE_EXACT, &ts, NULL);
1256
1257   /* No exact match found, nothing in our index
1258    * This is usually a bug or broken stream, as the seeking code already
1259    * makes sure that we're in the correct period and segment, and only need
1260    * to find the correct place inside the segment. Allow for some rounding
1261    * errors and inaccuracies here though */
1262   if (!entry) {
1263     GstSidxBoxEntry *last_entry = &sidx->entries[sidx->entries_count - 1];
1264
1265     GST_WARNING_OBJECT (dashstream->parent.pad, "Couldn't find SIDX entry");
1266
1267     if (ts < sidx->entries[0].pts
1268         && ts + 250 * GST_MSECOND >= sidx->entries[0].pts)
1269       entry = &sidx->entries[0];
1270     else if (ts >= last_entry->pts + last_entry->duration &&
1271         ts < last_entry->pts + last_entry->duration + 250 * GST_MSECOND)
1272       entry = last_entry;
1273   }
1274   if (!entry)
1275     return GST_FLOW_EOS;
1276
1277   idx = entry - sidx->entries;
1278
1279   /* FIXME in reverse mode, if we are exactly at a fragment start it makes more
1280    * sense to start from the end of the previous fragment */
1281   if (!forward && idx > 0 && entry->pts == ts) {
1282     idx--;
1283     entry = &sidx->entries[idx];
1284   }
1285
1286   /* Now entry->pts <= ts < entry->pts + entry->duration, need to adjust for
1287    * snapping */
1288   if ((flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST) {
1289     if (idx + 1 < sidx->entries_count
1290         && sidx->entries[idx + 1].pts - ts < ts - sidx->entries[idx].pts)
1291       idx += 1;
1292   } else if ((forward && (flags & GST_SEEK_FLAG_SNAP_AFTER)) || (!forward
1293           && (flags & GST_SEEK_FLAG_SNAP_BEFORE))) {
1294     if (idx + 1 < sidx->entries_count && entry->pts < ts)
1295       idx += 1;
1296   }
1297
1298   g_assert (sidx->entry_index < sidx->entries_count);
1299
1300   sidx->entry_index = idx;
1301   dashstream->sidx_position = sidx->entries[idx].pts;
1302
1303   if (final_ts)
1304     *final_ts = dashstream->sidx_position;
1305
1306   return ret;
1307 }
1308
1309 static GstFlowReturn
1310 gst_dash_demux_stream_seek (GstAdaptiveDemuxStream * stream, gboolean forward,
1311     GstSeekFlags flags, GstClockTime ts, GstClockTime * final_ts)
1312 {
1313   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1314   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1315   gint last_index, last_repeat;
1316   gboolean is_isobmff;
1317
1318   last_index = dashstream->active_stream->segment_index;
1319   last_repeat = dashstream->active_stream->segment_repeat_index;
1320
1321   if (dashstream->adapter)
1322     gst_adapter_clear (dashstream->adapter);
1323   dashstream->current_offset = -1;
1324   dashstream->current_index_header_or_data = 0;
1325
1326   dashstream->isobmff_parser.current_fourcc = 0;
1327   dashstream->isobmff_parser.current_start_offset = 0;
1328   dashstream->isobmff_parser.current_size = 0;
1329
1330   if (dashstream->moof)
1331     gst_isoff_moof_box_free (dashstream->moof);
1332   dashstream->moof = NULL;
1333   if (dashstream->moof_sync_samples)
1334     g_array_free (dashstream->moof_sync_samples, TRUE);
1335   dashstream->moof_sync_samples = NULL;
1336   dashstream->current_sync_sample = -1;
1337
1338   is_isobmff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1339
1340   if (!gst_mpd_client_stream_seek (dashdemux->client, dashstream->active_stream,
1341           forward,
1342           is_isobmff ? (flags & (~(GST_SEEK_FLAG_SNAP_BEFORE |
1343                       GST_SEEK_FLAG_SNAP_AFTER))) : flags, ts, final_ts)) {
1344     return GST_FLOW_EOS;
1345   }
1346
1347   if (is_isobmff) {
1348     GstClockTime period_start, offset;
1349
1350     period_start = gst_mpd_parser_get_period_start_time (dashdemux->client);
1351     offset =
1352         gst_mpd_parser_get_stream_presentation_offset (dashdemux->client,
1353         dashstream->index);
1354
1355     if (G_UNLIKELY (ts < period_start))
1356       ts = offset;
1357     else
1358       ts += offset - period_start;
1359
1360     if (last_index != dashstream->active_stream->segment_index ||
1361         last_repeat != dashstream->active_stream->segment_repeat_index) {
1362       GST_LOG_OBJECT (stream->pad,
1363           "Segment index was changed, reset sidx parser");
1364       gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1365       dashstream->sidx_base_offset = 0;
1366       dashstream->allow_sidx = TRUE;
1367     }
1368
1369     if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1370       if (gst_dash_demux_stream_sidx_seek (dashstream, forward, flags, ts,
1371               final_ts) != GST_FLOW_OK) {
1372         GST_ERROR_OBJECT (stream->pad, "Couldn't find position in sidx");
1373         dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1374         gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1375       }
1376       dashstream->pending_seek_ts = GST_CLOCK_TIME_NONE;
1377     } else {
1378       /* no index yet, seek when we have it */
1379       /* FIXME - the final_ts won't be correct here */
1380       dashstream->pending_seek_ts = ts;
1381     }
1382   }
1383
1384   return GST_FLOW_OK;
1385 }
1386
1387 static gboolean
1388 gst_dash_demux_stream_has_next_sync_sample (GstAdaptiveDemuxStream * stream)
1389 {
1390   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1391
1392   if (dashstream->moof_sync_samples
1393       && GST_ADAPTIVE_DEMUX (stream->demux)->
1394       segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1395     if (stream->demux->segment.rate > 0.0) {
1396       if (dashstream->current_sync_sample + 1 <
1397           dashstream->moof_sync_samples->len)
1398         return TRUE;
1399     } else {
1400       if (dashstream->current_sync_sample >= 1)
1401         return TRUE;
1402     }
1403   }
1404   return FALSE;
1405 }
1406
1407 static gboolean
1408 gst_dash_demux_stream_has_next_subfragment (GstAdaptiveDemuxStream * stream)
1409 {
1410   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1411   GstSidxBox *sidx = SIDX (dashstream);
1412
1413   if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1414     if (stream->demux->segment.rate > 0.0) {
1415       if (sidx->entry_index + 1 < sidx->entries_count)
1416         return TRUE;
1417     } else {
1418       if (sidx->entry_index >= 1)
1419         return TRUE;
1420     }
1421   }
1422   return FALSE;
1423 }
1424
1425 static gboolean
1426 gst_dash_demux_stream_advance_sync_sample (GstAdaptiveDemuxStream * stream)
1427 {
1428   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1429   gboolean fragment_finished = FALSE;
1430
1431   if (dashstream->moof_sync_samples
1432       && GST_ADAPTIVE_DEMUX (stream->demux)->
1433       segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1434     if (stream->demux->segment.rate > 0.0) {
1435       dashstream->current_sync_sample++;
1436       if (dashstream->current_sync_sample >= dashstream->moof_sync_samples->len) {
1437         fragment_finished = TRUE;
1438       }
1439     } else {
1440       if (dashstream->current_sync_sample == -1) {
1441         dashstream->current_sync_sample =
1442             dashstream->moof_sync_samples->len - 1;
1443       } else if (dashstream->current_sync_sample == 0) {
1444         dashstream->current_sync_sample = -1;
1445         fragment_finished = TRUE;
1446       } else {
1447         dashstream->current_sync_sample--;
1448       }
1449     }
1450   }
1451
1452   GST_DEBUG_OBJECT (stream->pad,
1453       "Advancing sync sample #%d fragment_finished:%d",
1454       dashstream->current_sync_sample, fragment_finished);
1455
1456   if (!fragment_finished)
1457     stream->discont = TRUE;
1458
1459   return !fragment_finished;
1460 }
1461
1462 static gboolean
1463 gst_dash_demux_stream_advance_subfragment (GstAdaptiveDemuxStream * stream)
1464 {
1465   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1466
1467   GstSidxBox *sidx = SIDX (dashstream);
1468   gboolean fragment_finished = TRUE;
1469
1470   if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1471     if (stream->demux->segment.rate > 0.0) {
1472       gint idx = ++sidx->entry_index;
1473       if (idx < sidx->entries_count) {
1474         fragment_finished = FALSE;
1475       }
1476
1477       if (idx == sidx->entries_count)
1478         dashstream->sidx_position =
1479             sidx->entries[idx - 1].pts + sidx->entries[idx - 1].duration;
1480       else
1481         dashstream->sidx_position = sidx->entries[idx].pts;
1482     } else {
1483       gint idx = --sidx->entry_index;
1484
1485       if (idx >= 0) {
1486         fragment_finished = FALSE;
1487         dashstream->sidx_position = sidx->entries[idx].pts;
1488       } else {
1489         dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1490       }
1491     }
1492   }
1493
1494   GST_DEBUG_OBJECT (stream->pad, "New sidx index: %d / %d. "
1495       "Finished fragment: %d", sidx->entry_index, sidx->entries_count,
1496       fragment_finished);
1497
1498   return !fragment_finished;
1499 }
1500
1501 static gboolean
1502 gst_dash_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
1503 {
1504   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1505   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1506
1507   if (dashstream->moof_sync_samples
1508       && GST_ADAPTIVE_DEMUX (dashdemux)->
1509       segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1510     if (gst_dash_demux_stream_has_next_sync_sample (stream))
1511       return TRUE;
1512   }
1513
1514   if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)) {
1515     if (gst_dash_demux_stream_has_next_subfragment (stream))
1516       return TRUE;
1517   }
1518
1519   return gst_mpd_client_has_next_segment (dashdemux->client,
1520       dashstream->active_stream, stream->demux->segment.rate > 0.0);
1521 }
1522
1523 static GstFlowReturn
1524 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream)
1525 {
1526   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1527   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1528
1529   GST_DEBUG_OBJECT (stream->pad, "Advance fragment");
1530
1531   /* If downloading only keyframes, switch to the next one or fall through */
1532   if (dashstream->moof_sync_samples
1533       && GST_ADAPTIVE_DEMUX (dashdemux)->
1534       segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1535     if (gst_dash_demux_stream_advance_sync_sample (stream))
1536       return GST_FLOW_OK;
1537   }
1538
1539   dashstream->isobmff_parser.current_fourcc = 0;
1540   dashstream->isobmff_parser.current_start_offset = 0;
1541   dashstream->isobmff_parser.current_size = 0;
1542
1543   if (dashstream->moof)
1544     gst_isoff_moof_box_free (dashstream->moof);
1545   dashstream->moof = NULL;
1546   if (dashstream->moof_sync_samples)
1547     g_array_free (dashstream->moof_sync_samples, TRUE);
1548   dashstream->moof_sync_samples = NULL;
1549   dashstream->current_sync_sample = -1;
1550
1551   if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)) {
1552     if (gst_dash_demux_stream_advance_subfragment (stream))
1553       return GST_FLOW_OK;
1554   }
1555
1556   gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1557   gst_isoff_sidx_parser_init (&dashstream->sidx_parser);
1558   dashstream->sidx_base_offset = 0;
1559   dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1560   dashstream->allow_sidx = TRUE;
1561   if (dashstream->adapter)
1562     gst_adapter_clear (dashstream->adapter);
1563
1564   return gst_mpd_client_advance_segment (dashdemux->client,
1565       dashstream->active_stream, stream->demux->segment.rate > 0.0);
1566 }
1567
1568 static gboolean
1569 gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream * stream,
1570     guint64 bitrate)
1571 {
1572   GstActiveStream *active_stream = NULL;
1573   GList *rep_list = NULL;
1574   gint new_index;
1575   GstAdaptiveDemux *base_demux = stream->demux;
1576   GstDashDemux *demux = GST_DASH_DEMUX_CAST (stream->demux);
1577   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1578   gboolean ret = FALSE;
1579
1580   active_stream = dashstream->active_stream;
1581   if (active_stream == NULL) {
1582     goto end;
1583   }
1584
1585   /* retrieve representation list */
1586   if (active_stream->cur_adapt_set)
1587     rep_list = active_stream->cur_adapt_set->Representations;
1588   if (!rep_list) {
1589     goto end;
1590   }
1591
1592   GST_DEBUG_OBJECT (stream->pad,
1593       "Trying to change to bitrate: %" G_GUINT64_FORMAT, bitrate);
1594
1595   if (active_stream->mimeType == GST_STREAM_VIDEO && demux->max_bitrate) {
1596     bitrate = MIN (demux->max_bitrate, bitrate);
1597   }
1598
1599 #ifdef TIZEN_FEATURE_ADAPTIVE_MODIFICATION
1600   bitrate = (base_demux->max_bandwidth > DEFAULT_ADAPTIVE_VARIANT)?
1601                           (guint64)(base_demux->max_bandwidth):(bitrate);
1602   GST_DEBUG_OBJECT (stream->pad,
1603       "Trying to change to bitrate under : %" G_GUINT64_FORMAT, bitrate);
1604
1605   /* get representation index with current max_bandwidth */
1606   if ((base_demux->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) ||
1607       ABS (base_demux->segment.rate) <= 1.0) {
1608     new_index =
1609         gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list, (gint)bitrate,
1610         demux->max_video_width, demux->max_video_height);
1611   } else {
1612     new_index =
1613         gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list,
1614         (gint)(bitrate / ABS (base_demux->segment.rate)), demux->max_video_width,
1615         demux->max_video_height);
1616   }
1617 #else
1618   /* get representation index with current max_bandwidth */
1619   if ((base_demux->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) ||
1620       ABS (base_demux->segment.rate) <= 1.0) {
1621     new_index =
1622         gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list, bitrate,
1623         demux->max_video_width, demux->max_video_height,
1624         demux->max_video_framerate_n, demux->max_video_framerate_d);
1625   } else {
1626     new_index =
1627         gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list,
1628         bitrate / ABS (base_demux->segment.rate), demux->max_video_width,
1629         demux->max_video_height, demux->max_video_framerate_n,
1630         demux->max_video_framerate_d);
1631   }
1632 #endif
1633
1634   /* if no representation has the required bandwidth, take the lowest one */
1635   if (new_index == -1)
1636     new_index = gst_mpdparser_get_rep_idx_with_min_bandwidth (rep_list);
1637
1638   if (new_index != active_stream->representation_idx) {
1639     GstRepresentationNode *rep = g_list_nth_data (rep_list, new_index);
1640     GST_INFO_OBJECT (demux, "Changing representation idx: %d %d %u",
1641         dashstream->index, new_index, rep->bandwidth);
1642     if (gst_mpd_client_setup_representation (demux->client, active_stream, rep)) {
1643       GstCaps *caps;
1644
1645       GST_INFO_OBJECT (demux, "Switching bitrate to %d",
1646           active_stream->cur_representation->bandwidth);
1647       caps = gst_dash_demux_get_input_caps (demux, active_stream);
1648       gst_adaptive_demux_stream_set_caps (stream, caps);
1649       ret = TRUE;
1650
1651     } else {
1652       GST_WARNING_OBJECT (demux, "Can not switch representation, aborting...");
1653     }
1654   }
1655
1656   if (ret) {
1657     if (gst_mpd_client_has_isoff_ondemand_profile (demux->client)
1658         && SIDX (dashstream)->entries) {
1659       /* store our current position to change to the same one in a different
1660        * representation if needed */
1661       if (SIDX (dashstream)->entry_index < SIDX (dashstream)->entries_count)
1662         dashstream->sidx_position = SIDX_CURRENT_ENTRY (dashstream)->pts;
1663       else if (SIDX (dashstream)->entry_index >=
1664           SIDX (dashstream)->entries_count)
1665         dashstream->sidx_position =
1666             SIDX_ENTRY (dashstream,
1667             SIDX (dashstream)->entries_count - 1)->pts + SIDX_ENTRY (dashstream,
1668             SIDX (dashstream)->entries_count - 1)->duration;
1669       else
1670         dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1671     } else {
1672       dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1673     }
1674
1675     gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1676     dashstream->sidx_base_offset = 0;
1677     dashstream->allow_sidx = TRUE;
1678
1679     /* Reset ISOBMFF box parsing state */
1680     dashstream->isobmff_parser.current_fourcc = 0;
1681     dashstream->isobmff_parser.current_start_offset = 0;
1682     dashstream->isobmff_parser.current_size = 0;
1683
1684     dashstream->current_offset = -1;
1685     dashstream->current_index_header_or_data = 0;
1686
1687     if (dashstream->adapter)
1688       gst_adapter_clear (dashstream->adapter);
1689
1690     if (dashstream->moof)
1691       gst_isoff_moof_box_free (dashstream->moof);
1692     dashstream->moof = NULL;
1693     if (dashstream->moof_sync_samples)
1694       g_array_free (dashstream->moof_sync_samples, TRUE);
1695     dashstream->moof_sync_samples = NULL;
1696     dashstream->current_sync_sample = -1;
1697   }
1698
1699 end:
1700   return ret;
1701 }
1702
1703 #define SEEK_UPDATES_PLAY_POSITION(r, start_type, stop_type) \
1704   ((r >= 0 && start_type != GST_SEEK_TYPE_NONE) || \
1705    (r < 0 && stop_type != GST_SEEK_TYPE_NONE))
1706
1707 static gboolean
1708 gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
1709 {
1710   gdouble rate;
1711   GstFormat format;
1712   GstSeekFlags flags;
1713   GstSeekType start_type, stop_type;
1714   gint64 start, stop;
1715   GList *list;
1716   GstClockTime current_pos, target_pos;
1717   guint current_period;
1718   GstStreamPeriod *period;
1719   GList *iter, *streams = NULL;
1720   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1721   gboolean trickmode_no_audio;
1722
1723   gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
1724       &stop_type, &stop);
1725
1726   if (!SEEK_UPDATES_PLAY_POSITION (rate, start_type, stop_type)) {
1727     /* nothing to do if we don't have to update the current position */
1728     return TRUE;
1729   }
1730
1731   if (demux->segment.rate > 0.0) {
1732     target_pos = (GstClockTime) start;
1733   } else {
1734     target_pos = (GstClockTime) stop;
1735   }
1736
1737   /* select the requested Period in the Media Presentation */
1738   if (!gst_mpd_client_setup_media_presentation (dashdemux->client, target_pos,
1739           -1, NULL))
1740     return FALSE;
1741
1742   current_period = 0;
1743   for (list = g_list_first (dashdemux->client->periods); list;
1744       list = g_list_next (list)) {
1745     period = list->data;
1746     current_pos = period->start;
1747     current_period = period->number;
1748     GST_DEBUG_OBJECT (demux, "Looking at period %u) start:%"
1749         GST_TIME_FORMAT " - duration:%"
1750         GST_TIME_FORMAT ") for position %" GST_TIME_FORMAT,
1751         current_period, GST_TIME_ARGS (current_pos),
1752         GST_TIME_ARGS (period->duration), GST_TIME_ARGS (target_pos));
1753     if (current_pos <= target_pos
1754         && target_pos <= current_pos + period->duration) {
1755       break;
1756     }
1757   }
1758   if (list == NULL) {
1759     GST_WARNING_OBJECT (demux, "Could not find seeked Period");
1760     return FALSE;
1761   }
1762
1763   trickmode_no_audio = ! !(flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO);
1764
1765   streams = demux->streams;
1766   if (current_period != gst_mpd_client_get_period_index (dashdemux->client)) {
1767     GST_DEBUG_OBJECT (demux, "Seeking to Period %d", current_period);
1768
1769     /* clean old active stream list, if any */
1770     gst_active_streams_free (dashdemux->client);
1771     dashdemux->trickmode_no_audio = trickmode_no_audio;
1772
1773     /* setup video, audio and subtitle streams, starting from the new Period */
1774     if (!gst_mpd_client_set_period_index (dashdemux->client, current_period)
1775         || !gst_dash_demux_setup_all_streams (dashdemux))
1776       return FALSE;
1777     streams = demux->next_streams;
1778   } else if (dashdemux->trickmode_no_audio != trickmode_no_audio) {
1779     /* clean old active stream list, if any */
1780     gst_active_streams_free (dashdemux->client);
1781     dashdemux->trickmode_no_audio = trickmode_no_audio;
1782
1783     /* setup video, audio and subtitle streams, starting from the new Period */
1784     if (!gst_dash_demux_setup_all_streams (dashdemux))
1785       return FALSE;
1786     streams = demux->next_streams;
1787   }
1788
1789   /* Update the current sequence on all streams */
1790   for (iter = streams; iter; iter = g_list_next (iter)) {
1791     GstAdaptiveDemuxStream *stream = iter->data;
1792
1793     if (gst_dash_demux_stream_seek (stream, rate >= 0, 0, target_pos,
1794             NULL) != GST_FLOW_OK)
1795       return FALSE;
1796   }
1797
1798   return TRUE;
1799 }
1800
1801 static gint64
1802 gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
1803 {
1804   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1805   return MIN (dashdemux->client->mpd_node->minimumUpdatePeriod * 1000,
1806       SLOW_CLOCK_UPDATE_INTERVAL);
1807 }
1808
1809 static GstFlowReturn
1810 gst_dash_demux_update_manifest_data (GstAdaptiveDemux * demux,
1811     GstBuffer * buffer)
1812 {
1813   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1814   GstMpdClient *new_client = NULL;
1815   GstMapInfo mapinfo;
1816
1817   GST_DEBUG_OBJECT (demux, "Updating manifest file from URL");
1818
1819   /* parse the manifest file */
1820   new_client = gst_mpd_client_new ();
1821   gst_mpd_client_set_uri_downloader (new_client, demux->downloader);
1822   new_client->mpd_uri = g_strdup (demux->manifest_uri);
1823   new_client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
1824   gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
1825
1826   if (gst_mpd_parse (new_client, (gchar *) mapinfo.data, mapinfo.size)) {
1827     const gchar *period_id;
1828     guint period_idx;
1829     GList *iter;
1830     GList *streams_iter;
1831     GList *streams;
1832
1833     /* prepare the new manifest and try to transfer the stream position
1834      * status from the old manifest client  */
1835
1836     GST_DEBUG_OBJECT (demux, "Updating manifest");
1837
1838     period_id = gst_mpd_client_get_period_id (dashdemux->client);
1839     period_idx = gst_mpd_client_get_period_index (dashdemux->client);
1840
1841     /* setup video, audio and subtitle streams, starting from current Period */
1842     if (!gst_mpd_client_setup_media_presentation (new_client, -1,
1843             (period_id ? -1 : period_idx), period_id)) {
1844       /* TODO */
1845     }
1846
1847     if (period_id) {
1848       if (!gst_mpd_client_set_period_id (new_client, period_id)) {
1849         GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
1850         gst_mpd_client_free (new_client);
1851         gst_buffer_unmap (buffer, &mapinfo);
1852         return GST_FLOW_EOS;
1853       }
1854     } else {
1855       if (!gst_mpd_client_set_period_index (new_client, period_idx)) {
1856         GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
1857         gst_mpd_client_free (new_client);
1858         gst_buffer_unmap (buffer, &mapinfo);
1859         return GST_FLOW_EOS;
1860       }
1861     }
1862
1863     if (!gst_dash_demux_setup_mpdparser_streams (dashdemux, new_client)) {
1864       GST_ERROR_OBJECT (demux, "Failed to setup streams on manifest " "update");
1865       gst_mpd_client_free (new_client);
1866       gst_buffer_unmap (buffer, &mapinfo);
1867       return GST_FLOW_ERROR;
1868     }
1869
1870     /* If no pads have been exposed yet, need to use those */
1871     streams = NULL;
1872     if (demux->streams == NULL) {
1873       if (demux->prepared_streams) {
1874         streams = demux->prepared_streams;
1875       }
1876     } else {
1877       streams = demux->streams;
1878     }
1879
1880     /* update the streams to play from the next segment */
1881     for (iter = streams, streams_iter = new_client->active_streams;
1882         iter && streams_iter;
1883         iter = g_list_next (iter), streams_iter = g_list_next (streams_iter)) {
1884       GstDashDemuxStream *demux_stream = iter->data;
1885       GstActiveStream *new_stream = streams_iter->data;
1886       GstClockTime ts;
1887
1888       if (!new_stream) {
1889         GST_DEBUG_OBJECT (demux,
1890             "Stream of index %d is missing from manifest update",
1891             demux_stream->index);
1892         gst_mpd_client_free (new_client);
1893         gst_buffer_unmap (buffer, &mapinfo);
1894         return GST_FLOW_EOS;
1895       }
1896
1897       if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
1898               demux_stream->index, &ts)
1899           || gst_mpd_client_get_last_fragment_timestamp_end (dashdemux->client,
1900               demux_stream->index, &ts)) {
1901
1902         /* Due to rounding when doing the timescale conversions it might happen
1903          * that the ts falls back to a previous segment, leading the same data
1904          * to be downloaded twice. We try to work around this by always adding
1905          * 10 microseconds to get back to the correct segment. The errors are
1906          * usually on the order of nanoseconds so it should be enough.
1907          */
1908         GST_DEBUG_OBJECT (GST_ADAPTIVE_DEMUX_STREAM_PAD (demux_stream),
1909             "Current position: %" GST_TIME_FORMAT ", updating to %"
1910             GST_TIME_FORMAT, GST_TIME_ARGS (ts),
1911             GST_TIME_ARGS (ts + (10 * GST_USECOND)));
1912         ts += 10 * GST_USECOND;
1913         gst_mpd_client_stream_seek (new_client, new_stream,
1914             demux->segment.rate >= 0, 0, ts, NULL);
1915       }
1916
1917       demux_stream->active_stream = new_stream;
1918     }
1919
1920     gst_mpd_client_free (dashdemux->client);
1921     dashdemux->client = new_client;
1922
1923     GST_DEBUG_OBJECT (demux, "Manifest file successfully updated");
1924     if (dashdemux->clock_drift) {
1925       gst_dash_demux_poll_clock_drift (dashdemux);
1926     }
1927   } else {
1928     /* In most cases, this will happen if we set a wrong url in the
1929      * source element and we have received the 404 HTML response instead of
1930      * the manifest */
1931     GST_WARNING_OBJECT (demux, "Error parsing the manifest.");
1932     gst_mpd_client_free (new_client);
1933     gst_buffer_unmap (buffer, &mapinfo);
1934     return GST_FLOW_ERROR;
1935   }
1936
1937   gst_buffer_unmap (buffer, &mapinfo);
1938
1939   return GST_FLOW_OK;
1940 }
1941
1942 static gint64
1943 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
1944     stream)
1945 {
1946   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1947   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1948   GstDateTime *segmentAvailability;
1949   GstActiveStream *active_stream = dashstream->active_stream;
1950
1951   segmentAvailability =
1952       gst_mpd_client_get_next_segment_availability_start_time
1953       (dashdemux->client, active_stream);
1954
1955   if (segmentAvailability) {
1956     gint64 diff;
1957     GstDateTime *cur_time;
1958
1959     cur_time =
1960         gst_date_time_new_from_g_date_time
1961         (gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST
1962             (dashdemux)));
1963     diff =
1964         gst_mpd_client_calculate_time_difference (cur_time,
1965         segmentAvailability);
1966     gst_date_time_unref (segmentAvailability);
1967     gst_date_time_unref (cur_time);
1968     /* subtract the server's clock drift, so that if the server's
1969        time is behind our idea of UTC, we need to sleep for longer
1970        before requesting a fragment */
1971     return diff -
1972         gst_dash_demux_get_clock_compensation (dashdemux) * GST_USECOND;
1973   }
1974   return 0;
1975 }
1976
1977 static gboolean
1978 gst_dash_demux_has_next_period (GstAdaptiveDemux * demux)
1979 {
1980   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1981
1982   if (demux->segment.rate >= 0)
1983     return gst_mpd_client_has_next_period (dashdemux->client);
1984   else
1985     return gst_mpd_client_has_previous_period (dashdemux->client);
1986 }
1987
1988 static void
1989 gst_dash_demux_advance_period (GstAdaptiveDemux * demux)
1990 {
1991   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1992
1993   if (demux->segment.rate >= 0) {
1994     if (!gst_mpd_client_set_period_index (dashdemux->client,
1995             gst_mpd_client_get_period_index (dashdemux->client) + 1)) {
1996       /* TODO error */
1997       return;
1998     }
1999   } else {
2000     if (!gst_mpd_client_set_period_index (dashdemux->client,
2001             gst_mpd_client_get_period_index (dashdemux->client) - 1)) {
2002       /* TODO error */
2003       return;
2004     }
2005   }
2006
2007   gst_dash_demux_setup_all_streams (dashdemux);
2008   gst_mpd_client_seek_to_first_segment (dashdemux->client);
2009 }
2010
2011 static GstBuffer *
2012 _gst_buffer_split (GstBuffer * buffer, gint offset, gsize size)
2013 {
2014   GstBuffer *newbuf = gst_buffer_copy_region (buffer,
2015       GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_META
2016       | GST_BUFFER_COPY_MEMORY, offset, size == -1 ? size : size - offset);
2017
2018   gst_buffer_resize (buffer, 0, offset);
2019
2020   return newbuf;
2021 }
2022
2023 static gboolean
2024 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
2025     GstAdaptiveDemuxStream * stream)
2026 {
2027   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2028   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2029
2030   dashstream->current_index_header_or_data = 0;
2031   dashstream->current_offset = -1;
2032
2033   /* We need to mark every first buffer of a key unit as discont,
2034    * and also every first buffer of a moov and moof. This ensures
2035    * that qtdemux takes note of our buffer offsets for each of those
2036    * buffers instead of keeping track of them itself from the first
2037    * buffer. We need offsets to be consistent between moof and mdat
2038    */
2039   if (dashstream->is_isobmff && dashdemux->allow_trickmode_key_units
2040       && (demux->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS)
2041       && dashstream->active_stream->mimeType == GST_STREAM_VIDEO)
2042     stream->discont = TRUE;
2043
2044   return TRUE;
2045 }
2046
2047 static GstFlowReturn
2048 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
2049     GstAdaptiveDemuxStream * stream)
2050 {
2051   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2052   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2053
2054   /* We need to mark every first buffer of a key unit as discont,
2055    * and also every first buffer of a moov and moof. This ensures
2056    * that qtdemux takes note of our buffer offsets for each of those
2057    * buffers instead of keeping track of them itself from the first
2058    * buffer. We need offsets to be consistent between moof and mdat
2059    */
2060   if (dashstream->is_isobmff && dashdemux->allow_trickmode_key_units
2061       && (demux->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS)
2062       && dashstream->active_stream->mimeType == GST_STREAM_VIDEO)
2063     stream->discont = TRUE;
2064
2065   /* Only handle fragment advancing specifically for SIDX if we're not
2066    * in key unit mode */
2067   if (!(dashstream->moof_sync_samples
2068           && GST_ADAPTIVE_DEMUX (dashdemux)->
2069           segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS)
2070       && gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)
2071       && dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
2072     /* fragment is advanced on data_received when byte limits are reached */
2073     if (dashstream->pending_seek_ts != GST_CLOCK_TIME_NONE) {
2074       if (SIDX (dashstream)->entry_index < SIDX (dashstream)->entries_count)
2075         return GST_FLOW_OK;
2076     } else if (gst_dash_demux_stream_has_next_subfragment (stream)) {
2077       return GST_FLOW_OK;
2078     }
2079   }
2080
2081   if (G_UNLIKELY (stream->downloading_header || stream->downloading_index))
2082     return GST_FLOW_OK;
2083
2084   return gst_adaptive_demux_stream_advance_fragment (demux, stream,
2085       stream->fragment.duration);
2086 }
2087
2088 static gboolean
2089 gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream * stream)
2090 {
2091   GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
2092   GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2093
2094   /* We're chunked downloading for ISOBMFF in KEY_UNITS mode for the actual
2095    * fragment until we parsed the moof and arrived at the mdat. 8192 is a
2096    * random guess for the moof size
2097    */
2098   if (dashstream->is_isobmff
2099       && (GST_ADAPTIVE_DEMUX (stream->demux)->
2100           segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS)
2101       && dashstream->active_stream->mimeType == GST_STREAM_VIDEO
2102       && !stream->downloading_header && !stream->downloading_index
2103       && dashdemux->allow_trickmode_key_units) {
2104     if (dashstream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT) {
2105       /* Need to download the moof first to know anything */
2106
2107       stream->fragment.chunk_size = 8192;
2108       /* Do we have the first fourcc already or are we in the middle */
2109       if (dashstream->isobmff_parser.current_fourcc == 0) {
2110         stream->fragment.chunk_size += dashstream->moof_average_size;
2111         if (dashstream->first_sync_sample_always_after_moof)
2112           stream->fragment.chunk_size +=
2113               dashstream->first_sync_sample_average_size;
2114       }
2115
2116       if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2117           dashstream->sidx_parser.sidx.entries) {
2118         guint64 sidx_end_offset =
2119             dashstream->sidx_base_offset +
2120             SIDX_CURRENT_ENTRY (dashstream)->offset +
2121             SIDX_CURRENT_ENTRY (dashstream)->size;
2122         guint64 downloaded_end_offset =
2123             dashstream->current_offset +
2124             gst_adapter_available (dashstream->adapter);
2125
2126         if (stream->fragment.chunk_size +
2127             downloaded_end_offset > sidx_end_offset) {
2128           stream->fragment.chunk_size = sidx_end_offset - downloaded_end_offset;
2129         }
2130       }
2131     } else if (dashstream->moof && dashstream->moof_sync_samples) {
2132       /* Have the moof, either we're done now or we want to download the
2133        * directly following sync sample */
2134       if (dashstream->first_sync_sample_after_moof
2135           && dashstream->current_sync_sample == 0) {
2136         GstDashStreamSyncSample *sync_sample =
2137             &g_array_index (dashstream->moof_sync_samples,
2138             GstDashStreamSyncSample, 0);
2139         guint64 end_offset = sync_sample->end_offset + 1;
2140         guint64 downloaded_end_offset =
2141             dashstream->current_offset +
2142             gst_adapter_available (dashstream->adapter);
2143
2144         if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2145             dashstream->sidx_parser.sidx.entries) {
2146           guint64 sidx_end_offset =
2147               dashstream->sidx_base_offset +
2148               SIDX_CURRENT_ENTRY (dashstream)->offset +
2149               SIDX_CURRENT_ENTRY (dashstream)->size;
2150
2151           if (end_offset > sidx_end_offset) {
2152             end_offset = sidx_end_offset;
2153           }
2154         }
2155
2156         if (downloaded_end_offset < end_offset) {
2157           stream->fragment.chunk_size = end_offset - downloaded_end_offset;
2158         } else {
2159           stream->fragment.chunk_size = 0;
2160         }
2161       } else {
2162         stream->fragment.chunk_size = 0;
2163       }
2164     } else {
2165       /* Have moof but can't do key-units mode, just download until the end */
2166       stream->fragment.chunk_size = -1;
2167     }
2168   } else {
2169     /* We might've decided that we can't allow key-unit only
2170      * trickmodes while doing chunked downloading. In that case
2171      * just download from here to the end now */
2172     if (dashstream->moof
2173         && (GST_ADAPTIVE_DEMUX (stream->demux)->
2174             segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS))
2175       stream->fragment.chunk_size = -1;
2176     else
2177       stream->fragment.chunk_size = 0;
2178   }
2179
2180   return stream->fragment.chunk_size != 0;
2181 }
2182
2183 static GstFlowReturn
2184 gst_dash_demux_parse_isobmff (GstAdaptiveDemux * demux,
2185     GstDashDemuxStream * dash_stream, gboolean * sidx_seek_needed)
2186 {
2187   GstAdaptiveDemuxStream *stream = (GstAdaptiveDemuxStream *) dash_stream;
2188   GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2189   gsize available;
2190   GstBuffer *buffer;
2191   GstMapInfo map;
2192   GstByteReader reader;
2193   guint32 fourcc;
2194   guint header_size;
2195   guint64 size, buffer_offset;
2196
2197   *sidx_seek_needed = FALSE;
2198
2199   /* This must not be called when we're in the mdat. We only look at the mdat
2200    * header and then stop parsing the boxes as we're only interested in the
2201    * metadata! Handling mdat is the job of the surrounding code, as well as
2202    * stopping or starting the next fragment when mdat is over (=> sidx)
2203    */
2204   g_assert (dash_stream->isobmff_parser.current_fourcc !=
2205       GST_ISOFF_FOURCC_MDAT);
2206
2207   available = gst_adapter_available (dash_stream->adapter);
2208   buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2209   buffer_offset = dash_stream->current_offset;
2210
2211   /* Always at the start of a box here */
2212   g_assert (dash_stream->isobmff_parser.current_size == 0);
2213
2214   /* At the start of a box => Parse it */
2215   gst_buffer_map (buffer, &map, GST_MAP_READ);
2216   gst_byte_reader_init (&reader, map.data, map.size);
2217
2218   /* While there are more boxes left to parse ... */
2219   dash_stream->isobmff_parser.current_start_offset = buffer_offset;
2220   do {
2221     dash_stream->isobmff_parser.current_fourcc = 0;
2222     dash_stream->isobmff_parser.current_size = 0;
2223
2224     if (!gst_isoff_parse_box_header (&reader, &fourcc, NULL, &header_size,
2225             &size)) {
2226       break;
2227     }
2228
2229     dash_stream->isobmff_parser.current_fourcc = fourcc;
2230     if (size == 0) {
2231       /* We assume this is mdat, anything else with "size until end"
2232        * does not seem to make sense */
2233       g_assert (dash_stream->isobmff_parser.current_fourcc ==
2234           GST_ISOFF_FOURCC_MDAT);
2235       dash_stream->isobmff_parser.current_size = -1;
2236       break;
2237     }
2238
2239     dash_stream->isobmff_parser.current_size = size;
2240
2241     /* Do we have the complete box or are at MDAT */
2242     if (gst_byte_reader_get_remaining (&reader) < size - header_size ||
2243         dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MDAT) {
2244       /* Reset byte reader to the beginning of the box */
2245       gst_byte_reader_set_pos (&reader,
2246           gst_byte_reader_get_pos (&reader) - header_size);
2247       break;
2248     }
2249
2250     GST_LOG_OBJECT (stream->pad,
2251         "box %" GST_FOURCC_FORMAT " at offset %" G_GUINT64_FORMAT " size %"
2252         G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc),
2253         dash_stream->isobmff_parser.current_start_offset, size);
2254
2255     if (dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MOOF) {
2256       GstByteReader sub_reader;
2257
2258       /* Only allow SIDX before the very first moof */
2259       dash_stream->allow_sidx = FALSE;
2260
2261       g_assert (dash_stream->moof == NULL);
2262       g_assert (dash_stream->moof_sync_samples == NULL);
2263       gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
2264       dash_stream->moof = gst_isoff_moof_box_parse (&sub_reader);
2265       dash_stream->moof_offset =
2266           dash_stream->isobmff_parser.current_start_offset;
2267       dash_stream->moof_size = size;
2268       dash_stream->current_sync_sample = -1;
2269
2270       if (dash_stream->moof_average_size) {
2271         if (dash_stream->moof_average_size < size)
2272           dash_stream->moof_average_size =
2273               (size * 3 + dash_stream->moof_average_size) / 4;
2274         else
2275           dash_stream->moof_average_size =
2276               (size + dash_stream->moof_average_size + 3) / 4;
2277       } else {
2278         dash_stream->moof_average_size = size;
2279       }
2280     } else if (dash_stream->isobmff_parser.current_fourcc ==
2281         GST_ISOFF_FOURCC_SIDX &&
2282         gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2283         dash_stream->allow_sidx) {
2284       GstByteReader sub_reader;
2285       GstIsoffParserResult res;
2286       guint dummy;
2287
2288       dash_stream->sidx_base_offset =
2289           dash_stream->isobmff_parser.current_start_offset + size;
2290       dash_stream->allow_sidx = FALSE;
2291
2292       gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
2293
2294       res =
2295           gst_isoff_sidx_parser_parse (&dash_stream->sidx_parser, &sub_reader,
2296           &dummy);
2297
2298       if (res == GST_ISOFF_PARSER_DONE) {
2299         guint64 first_offset = dash_stream->sidx_parser.sidx.first_offset;
2300         GstSidxBox *sidx = SIDX (dash_stream);
2301         guint i;
2302
2303         if (first_offset) {
2304           GST_LOG_OBJECT (stream->pad,
2305               "non-zero sidx first offset %" G_GUINT64_FORMAT, first_offset);
2306           dash_stream->sidx_base_offset += first_offset;
2307         }
2308
2309         for (i = 0; i < sidx->entries_count; i++) {
2310           GstSidxBoxEntry *entry = &sidx->entries[i];
2311
2312           if (entry->ref_type != 0) {
2313             GST_FIXME_OBJECT (stream->pad, "SIDX ref_type 1 not supported yet");
2314             dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
2315             gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2316             break;
2317           }
2318         }
2319
2320         /* We might've cleared the index above */
2321         if (sidx->entries_count > 0) {
2322           if (GST_CLOCK_TIME_IS_VALID (dash_stream->pending_seek_ts)) {
2323             /* FIXME, preserve seek flags */
2324             if (gst_dash_demux_stream_sidx_seek (dash_stream,
2325                     demux->segment.rate >= 0, 0, dash_stream->pending_seek_ts,
2326                     NULL) != GST_FLOW_OK) {
2327               GST_ERROR_OBJECT (stream->pad, "Couldn't find position in sidx");
2328               dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
2329               gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2330             }
2331             dash_stream->pending_seek_ts = GST_CLOCK_TIME_NONE;
2332           } else {
2333
2334             if (dash_stream->sidx_position == GST_CLOCK_TIME_NONE) {
2335               SIDX (dash_stream)->entry_index = 0;
2336             } else {
2337               if (gst_dash_demux_stream_sidx_seek (dash_stream,
2338                       demux->segment.rate >= 0, GST_SEEK_FLAG_SNAP_BEFORE,
2339                       dash_stream->sidx_position, NULL) != GST_FLOW_OK) {
2340                 GST_ERROR_OBJECT (stream->pad,
2341                     "Couldn't find position in sidx");
2342                 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
2343                 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2344               }
2345             }
2346             dash_stream->sidx_position =
2347                 SIDX (dash_stream)->entries[SIDX (dash_stream)->
2348                 entry_index].pts;
2349           }
2350         }
2351
2352         if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED &&
2353             SIDX (dash_stream)->entry_index != 0) {
2354           /* Need to jump to the requested SIDX entry. Push everything up to
2355            * the SIDX box below and let the caller handle everything else */
2356           *sidx_seek_needed = TRUE;
2357           break;
2358         }
2359       }
2360     } else {
2361       gst_byte_reader_skip (&reader, size - header_size);
2362     }
2363
2364     dash_stream->isobmff_parser.current_fourcc = 0;
2365     dash_stream->isobmff_parser.current_start_offset += size;
2366     dash_stream->isobmff_parser.current_size = 0;
2367   } while (gst_byte_reader_get_remaining (&reader) > 0);
2368
2369   gst_buffer_unmap (buffer, &map);
2370
2371   /* mdat? Push all we have and wait for it to be over */
2372   if (dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MDAT) {
2373     GstBuffer *pending;
2374
2375     GST_LOG_OBJECT (stream->pad,
2376         "box %" GST_FOURCC_FORMAT " at offset %" G_GUINT64_FORMAT " size %"
2377         G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc),
2378         dash_stream->isobmff_parser.current_start_offset,
2379         dash_stream->isobmff_parser.current_size);
2380
2381     /* At mdat. Move the start of the mdat to the adapter and have everything
2382      * else be pushed. We parsed all header boxes at this point and are not
2383      * supposed to be called again until the next moof */
2384     pending = _gst_buffer_split (buffer, gst_byte_reader_get_pos (&reader), -1);
2385     gst_adapter_push (dash_stream->adapter, pending);
2386     dash_stream->current_offset += gst_byte_reader_get_pos (&reader);
2387     dash_stream->isobmff_parser.current_size = 0;
2388
2389     GST_BUFFER_OFFSET (buffer) = buffer_offset;
2390     GST_BUFFER_OFFSET_END (buffer) =
2391         buffer_offset + gst_buffer_get_size (buffer);
2392     return gst_adaptive_demux_stream_push_buffer (stream, buffer);
2393   } else if (gst_byte_reader_get_pos (&reader) != 0) {
2394     GstBuffer *pending;
2395
2396     /* Multiple complete boxes and no mdat? Push them and keep the remainder,
2397      * which is the start of the next box if any remainder */
2398
2399     pending = _gst_buffer_split (buffer, gst_byte_reader_get_pos (&reader), -1);
2400     gst_adapter_push (dash_stream->adapter, pending);
2401     dash_stream->current_offset += gst_byte_reader_get_pos (&reader);
2402     dash_stream->isobmff_parser.current_size = 0;
2403
2404     GST_BUFFER_OFFSET (buffer) = buffer_offset;
2405     GST_BUFFER_OFFSET_END (buffer) =
2406         buffer_offset + gst_buffer_get_size (buffer);
2407     return gst_adaptive_demux_stream_push_buffer (stream, buffer);
2408   }
2409
2410   /* Not even a single complete, non-mdat box, wait */
2411   dash_stream->isobmff_parser.current_size = 0;
2412   gst_adapter_push (dash_stream->adapter, buffer);
2413
2414   return GST_FLOW_OK;
2415 }
2416
2417 static gboolean
2418 gst_dash_demux_find_sync_samples (GstAdaptiveDemux * demux,
2419     GstAdaptiveDemuxStream * stream)
2420 {
2421   GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
2422   GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
2423   guint i;
2424   guint32 track_id = 0;
2425   guint64 prev_traf_end;
2426   gboolean trex_sample_flags = FALSE;
2427
2428   if (!dash_stream->moof) {
2429     dashdemux->allow_trickmode_key_units = FALSE;
2430     return FALSE;
2431   }
2432
2433   dash_stream->current_sync_sample = -1;
2434   dash_stream->moof_sync_samples =
2435       g_array_new (FALSE, FALSE, sizeof (GstDashStreamSyncSample));
2436
2437   prev_traf_end = dash_stream->moof_offset;
2438
2439   /* generate table of keyframes and offsets */
2440   for (i = 0; i < dash_stream->moof->traf->len; i++) {
2441     GstTrafBox *traf = &g_array_index (dash_stream->moof->traf, GstTrafBox, i);
2442     guint64 traf_offset = 0, prev_trun_end;
2443     guint j;
2444
2445     if (i == 0) {
2446       track_id = traf->tfhd.track_id;
2447     } else if (track_id != traf->tfhd.track_id) {
2448       GST_ERROR_OBJECT (stream->pad,
2449           "moof with trafs of different track ids (%u != %u)", track_id,
2450           traf->tfhd.track_id);
2451       g_array_free (dash_stream->moof_sync_samples, TRUE);
2452       dash_stream->moof_sync_samples = NULL;
2453       dashdemux->allow_trickmode_key_units = FALSE;
2454       return FALSE;
2455     }
2456
2457     if (traf->tfhd.flags & GST_TFHD_FLAGS_BASE_DATA_OFFSET_PRESENT) {
2458       traf_offset = traf->tfhd.base_data_offset;
2459     } else if (traf->tfhd.flags & GST_TFHD_FLAGS_DEFAULT_BASE_IS_MOOF) {
2460       traf_offset = dash_stream->moof_offset;
2461     } else {
2462       traf_offset = prev_traf_end;
2463     }
2464
2465     prev_trun_end = traf_offset;
2466
2467     for (j = 0; j < traf->trun->len; j++) {
2468       GstTrunBox *trun = &g_array_index (traf->trun, GstTrunBox, j);
2469       guint64 trun_offset, prev_sample_end;
2470       guint k;
2471
2472       if (trun->flags & GST_TRUN_FLAGS_DATA_OFFSET_PRESENT) {
2473         trun_offset = traf_offset + trun->data_offset;
2474       } else {
2475         trun_offset = prev_trun_end;
2476       }
2477
2478       prev_sample_end = trun_offset;
2479       for (k = 0; k < trun->samples->len; k++) {
2480         GstTrunSample *sample =
2481             &g_array_index (trun->samples, GstTrunSample, k);
2482         guint64 sample_offset;
2483         guint32 sample_flags;
2484 #if 0
2485         guint32 sample_duration;
2486 #endif
2487
2488         sample_offset = prev_sample_end;
2489
2490         if (trun->flags & GST_TRUN_FLAGS_SAMPLE_FLAGS_PRESENT) {
2491           sample_flags = sample->sample_flags;
2492         } else if ((trun->flags & GST_TRUN_FLAGS_FIRST_SAMPLE_FLAGS_PRESENT)
2493             && k == 0) {
2494           sample_flags = trun->first_sample_flags;
2495         } else if (traf->
2496             tfhd.flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_FLAGS_PRESENT) {
2497           sample_flags = traf->tfhd.default_sample_flags;
2498         } else {
2499           trex_sample_flags = TRUE;
2500           continue;
2501         }
2502
2503 #if 0
2504         if (trun->flags & GST_TRUN_FLAGS_SAMPLE_DURATION_PRESENT) {
2505           sample_duration = sample->sample_duration;
2506         } else if (traf->
2507             tfhd.flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_DURATION_PRESENT) {
2508           sample_duration = traf->tfhd.default_sample_duration;
2509         } else {
2510           GST_FIXME_OBJECT (stream->pad,
2511               "Sample duration given by trex - can't download only keyframes");
2512           g_array_free (dash_stream->moof_sync_samples, TRUE);
2513           dash_stream->moof_sync_samples = NULL;
2514           return FALSE;
2515         }
2516 #endif
2517
2518         if (trun->flags & GST_TRUN_FLAGS_SAMPLE_SIZE_PRESENT) {
2519           prev_sample_end += sample->sample_size;
2520         } else if (traf->
2521             tfhd.flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_SIZE_PRESENT) {
2522           prev_sample_end += traf->tfhd.default_sample_size;
2523         } else {
2524           GST_FIXME_OBJECT (stream->pad,
2525               "Sample size given by trex - can't download only keyframes");
2526           g_array_free (dash_stream->moof_sync_samples, TRUE);
2527           dash_stream->moof_sync_samples = NULL;
2528           dashdemux->allow_trickmode_key_units = FALSE;
2529           return FALSE;
2530         }
2531
2532         /* Non-non-sync sample aka sync sample */
2533         if (!GST_ISOFF_SAMPLE_FLAGS_SAMPLE_IS_NON_SYNC_SAMPLE (sample_flags) ||
2534             GST_ISOFF_SAMPLE_FLAGS_SAMPLE_DEPENDS_ON (sample_flags) == 2) {
2535           GstDashStreamSyncSample sync_sample =
2536               { sample_offset, prev_sample_end - 1 };
2537           /* TODO: need timestamps so we can decide to download or not */
2538           g_array_append_val (dash_stream->moof_sync_samples, sync_sample);
2539         }
2540       }
2541
2542       prev_trun_end = prev_sample_end;
2543     }
2544
2545     prev_traf_end = prev_trun_end;
2546   }
2547
2548   if (trex_sample_flags) {
2549     if (dash_stream->moof_sync_samples->len > 0) {
2550       GST_LOG_OBJECT (stream->pad,
2551           "Some sample flags given by trex but still found sync samples");
2552     } else {
2553       GST_FIXME_OBJECT (stream->pad,
2554           "Sample flags given by trex - can't download only keyframes");
2555       g_array_free (dash_stream->moof_sync_samples, TRUE);
2556       dash_stream->moof_sync_samples = NULL;
2557       dashdemux->allow_trickmode_key_units = FALSE;
2558       return FALSE;
2559     }
2560   }
2561
2562   if (dash_stream->moof_sync_samples->len == 0) {
2563     GST_LOG_OBJECT (stream->pad, "No sync samples found in fragment");
2564     g_array_free (dash_stream->moof_sync_samples, TRUE);
2565     dash_stream->moof_sync_samples = NULL;
2566     dashdemux->allow_trickmode_key_units = FALSE;
2567     return FALSE;
2568   }
2569
2570   {
2571     GstDashStreamSyncSample *sync_sample =
2572         &g_array_index (dash_stream->moof_sync_samples, GstDashStreamSyncSample,
2573         0);
2574     guint size = sync_sample->end_offset + 1 - sync_sample->start_offset;
2575
2576     if (dash_stream->first_sync_sample_average_size) {
2577       if (dash_stream->first_sync_sample_average_size < size)
2578         dash_stream->first_sync_sample_average_size =
2579             (size * 3 + dash_stream->first_sync_sample_average_size) / 4;
2580       else
2581         dash_stream->first_sync_sample_average_size =
2582             (size + dash_stream->first_sync_sample_average_size * 3) / 4;
2583     } else {
2584       dash_stream->first_sync_sample_average_size = size;
2585     }
2586
2587     if (dash_stream->moof_offset + dash_stream->moof_size + 8 <
2588         sync_sample->start_offset) {
2589       dash_stream->first_sync_sample_after_moof = FALSE;
2590       dash_stream->first_sync_sample_always_after_moof = FALSE;
2591     } else {
2592       dash_stream->first_sync_sample_after_moof =
2593           (dash_stream->moof_sync_samples->len == 1
2594           || demux->segment.rate > 0.0);
2595     }
2596   }
2597
2598   return TRUE;
2599 }
2600
2601
2602 static GstFlowReturn
2603 gst_dash_demux_handle_isobmff (GstAdaptiveDemux * demux,
2604     GstAdaptiveDemuxStream * stream)
2605 {
2606   GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
2607   GstFlowReturn ret = GST_FLOW_OK;
2608   GstBuffer *buffer;
2609   gboolean sidx_advance = FALSE;
2610
2611   /* We parse all ISOBMFF boxes of a (sub)fragment until the mdat. This covers
2612    * at least moov, moof and sidx boxes. Once mdat is received we just output
2613    * everything until the next (sub)fragment */
2614   if (dash_stream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT) {
2615     gboolean sidx_seek_needed = FALSE;
2616
2617     ret = gst_dash_demux_parse_isobmff (demux, dash_stream, &sidx_seek_needed);
2618     if (ret != GST_FLOW_OK)
2619       return ret;
2620
2621     /* Go to selected segment if needed here */
2622     if (sidx_seek_needed && !stream->downloading_index)
2623       return GST_ADAPTIVE_DEMUX_FLOW_END_OF_FRAGMENT;
2624
2625     /* No mdat yet, let's get called again with the next boxes */
2626     if (dash_stream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT)
2627       return ret;
2628
2629     /* Here we end up only if we're right at the mdat start */
2630
2631     /* Jump to the next sync sample. As we're doing chunked downloading
2632      * here, just drop data until our chunk is over so we can reuse the
2633      * HTTP connection instead of having to create a new one or
2634      * reuse the data if the sync sample follows the moof */
2635     if (dash_stream->active_stream->mimeType == GST_STREAM_VIDEO
2636         && gst_dash_demux_find_sync_samples (demux, stream) &&
2637         GST_ADAPTIVE_DEMUX (stream->demux)->
2638         segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
2639
2640       if (dash_stream->first_sync_sample_after_moof) {
2641         /* If we're here, don't throw away data but collect sync
2642          * sample while we're at it below. We're doing chunked
2643          * downloading so might need to adjust the next chunk size for
2644          * the remainder */
2645         dash_stream->current_sync_sample = 0;
2646       }
2647     }
2648
2649     if (gst_adapter_available (dash_stream->adapter) == 0)
2650       return ret;
2651
2652     /* We have some data from the mdat available in the adapter, handle it
2653      * below in the push code */
2654   } else {
2655     /* Somewhere in the middle of the mdat */
2656   }
2657
2658   /* At mdat */
2659   if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
2660     guint64 sidx_end_offset =
2661         dash_stream->sidx_base_offset +
2662         SIDX_CURRENT_ENTRY (dash_stream)->offset +
2663         SIDX_CURRENT_ENTRY (dash_stream)->size;
2664     gboolean has_next = gst_dash_demux_stream_has_next_subfragment (stream);
2665     gsize available;
2666
2667     /* Need to handle everything in the adapter according to the parsed SIDX
2668      * and advance subsegments accordingly */
2669
2670     available = gst_adapter_available (dash_stream->adapter);
2671     if (dash_stream->current_offset + available < sidx_end_offset) {
2672       buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2673     } else {
2674       if (!has_next && sidx_end_offset <= dash_stream->current_offset) {
2675         /* Drain all bytes, since there might be trailing bytes at the end of subfragment */
2676         buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2677       } else {
2678         if (sidx_end_offset <= dash_stream->current_offset) {
2679           /* This means a corrupted stream or a bug: ignoring bugs, it
2680            * should only happen if the SIDX index is corrupt */
2681           GST_ERROR_OBJECT (stream->pad, "Invalid SIDX state");
2682           gst_adapter_clear (dash_stream->adapter);
2683           return GST_FLOW_ERROR;
2684         } else {
2685           buffer =
2686               gst_adapter_take_buffer (dash_stream->adapter,
2687               sidx_end_offset - dash_stream->current_offset);
2688           sidx_advance = TRUE;
2689         }
2690       }
2691     }
2692   } else {
2693     /* Take it all and handle it further below */
2694     buffer =
2695         gst_adapter_take_buffer (dash_stream->adapter,
2696         gst_adapter_available (dash_stream->adapter));
2697
2698     /* Attention: All code paths below need to update dash_stream->current_offset */
2699   }
2700
2701   /* We're actually running in key-units trick mode */
2702   if (dash_stream->active_stream->mimeType == GST_STREAM_VIDEO
2703       && dash_stream->moof_sync_samples
2704       && GST_ADAPTIVE_DEMUX (stream->demux)->
2705       segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
2706
2707     if (dash_stream->current_sync_sample == -1) {
2708       /* We're doing chunked downloading and wait for finishing the current
2709        * chunk so we can jump to the first keyframe */
2710       dash_stream->current_offset += gst_buffer_get_size (buffer);
2711       gst_buffer_unref (buffer);
2712       return GST_FLOW_OK;
2713     } else {
2714       GstDashStreamSyncSample *sync_sample =
2715           &g_array_index (dash_stream->moof_sync_samples,
2716           GstDashStreamSyncSample, dash_stream->current_sync_sample);
2717       guint64 end_offset =
2718           dash_stream->current_offset + gst_buffer_get_size (buffer);
2719
2720       /* Make sure to not download too much, this should only happen for
2721        * the very first keyframe if it follows the moof */
2722       if (dash_stream->current_offset >= sync_sample->end_offset + 1) {
2723         dash_stream->current_offset += gst_buffer_get_size (buffer);
2724         gst_buffer_unref (buffer);
2725         return GST_FLOW_OK;
2726       } else if (end_offset > sync_sample->end_offset + 1) {
2727         guint64 remaining =
2728             sync_sample->end_offset + 1 - dash_stream->current_offset;
2729         GstBuffer *sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0,
2730             remaining);
2731         gst_buffer_unref (buffer);
2732         buffer = sub;
2733       }
2734     }
2735   }
2736
2737   GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
2738   dash_stream->current_offset += gst_buffer_get_size (buffer);
2739   GST_BUFFER_OFFSET_END (buffer) = dash_stream->current_offset;
2740
2741   ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
2742   if (ret != GST_FLOW_OK)
2743     return ret;
2744
2745   if (sidx_advance) {
2746     ret =
2747         gst_adaptive_demux_stream_advance_fragment (demux, stream,
2748         SIDX_CURRENT_ENTRY (dash_stream)->duration);
2749     if (ret != GST_FLOW_OK)
2750       return ret;
2751
2752     /* If we still have data available, recurse and use it up if possible */
2753     if (gst_adapter_available (dash_stream->adapter) > 0)
2754       return gst_dash_demux_handle_isobmff (demux, stream);
2755   }
2756
2757   return ret;
2758 }
2759
2760 static GstFlowReturn
2761 gst_dash_demux_data_received (GstAdaptiveDemux * demux,
2762     GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
2763 {
2764   GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
2765   GstFlowReturn ret = GST_FLOW_OK;
2766   guint index_header_or_data;
2767
2768   if (stream->downloading_index)
2769     index_header_or_data = 1;
2770   else if (stream->downloading_header)
2771     index_header_or_data = 2;
2772   else
2773     index_header_or_data = 3;
2774
2775   if (dash_stream->current_index_header_or_data != index_header_or_data) {
2776     /* Clear pending data */
2777     if (gst_adapter_available (dash_stream->adapter) != 0)
2778       GST_ERROR_OBJECT (stream->pad,
2779           "Had pending SIDX data after switch between index/header/data");
2780     gst_adapter_clear (dash_stream->adapter);
2781     dash_stream->current_index_header_or_data = index_header_or_data;
2782     dash_stream->current_offset = -1;
2783   }
2784
2785   if (dash_stream->current_offset == -1)
2786     dash_stream->current_offset =
2787         GST_BUFFER_OFFSET_IS_VALID (buffer) ? GST_BUFFER_OFFSET (buffer) : 0;
2788
2789   gst_adapter_push (dash_stream->adapter, buffer);
2790   buffer = NULL;
2791
2792   if (dash_stream->is_isobmff || stream->downloading_index) {
2793     /* SIDX index is also ISOBMMF */
2794     ret = gst_dash_demux_handle_isobmff (demux, stream);
2795   } else if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
2796     gsize available;
2797
2798     /* Not ISOBMFF but had a SIDX index. Does this even exist or work? */
2799     while (ret == GST_FLOW_OK
2800         && ((available = gst_adapter_available (dash_stream->adapter)) > 0)) {
2801       gboolean advance = FALSE;
2802       guint64 sidx_end_offset =
2803           dash_stream->sidx_base_offset +
2804           SIDX_CURRENT_ENTRY (dash_stream)->offset +
2805           SIDX_CURRENT_ENTRY (dash_stream)->size;
2806       gboolean has_next = gst_dash_demux_stream_has_next_subfragment (stream);
2807
2808       if (dash_stream->current_offset + available < sidx_end_offset) {
2809         buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2810       } else {
2811         if (!has_next && sidx_end_offset <= dash_stream->current_offset) {
2812           /* Drain all bytes, since there might be trailing bytes at the end of subfragment */
2813           buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2814         } else {
2815           if (sidx_end_offset <= dash_stream->current_offset) {
2816             /* This means a corrupted stream or a bug: ignoring bugs, it
2817              * should only happen if the SIDX index is corrupt */
2818             GST_ERROR_OBJECT (stream->pad, "Invalid SIDX state");
2819             gst_adapter_clear (dash_stream->adapter);
2820             ret = GST_FLOW_ERROR;
2821             break;
2822           } else {
2823             buffer =
2824                 gst_adapter_take_buffer (dash_stream->adapter,
2825                 sidx_end_offset - dash_stream->current_offset);
2826             advance = TRUE;
2827           }
2828         }
2829       }
2830
2831       GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
2832       GST_BUFFER_OFFSET_END (buffer) =
2833           GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer);
2834       dash_stream->current_offset = GST_BUFFER_OFFSET_END (buffer);
2835
2836       ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
2837
2838       if (advance) {
2839         if (has_next) {
2840           GstFlowReturn new_ret;
2841           new_ret =
2842               gst_adaptive_demux_stream_advance_fragment (demux, stream,
2843               SIDX_CURRENT_ENTRY (dash_stream)->duration);
2844
2845           /* only overwrite if it was OK before */
2846           if (ret == GST_FLOW_OK)
2847             ret = new_ret;
2848         } else {
2849           break;
2850         }
2851       }
2852     }
2853   } else {
2854     /* this should be the main header, just push it all */
2855     buffer = gst_adapter_take_buffer (dash_stream->adapter,
2856         gst_adapter_available (dash_stream->adapter));
2857
2858     GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
2859     GST_BUFFER_OFFSET_END (buffer) =
2860         GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer);
2861     dash_stream->current_offset = GST_BUFFER_OFFSET_END (buffer);
2862
2863     ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
2864   }
2865
2866   return ret;
2867 }
2868
2869 static void
2870 gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream)
2871 {
2872   GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
2873
2874   gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2875   if (dash_stream->adapter)
2876     g_object_unref (dash_stream->adapter);
2877   if (dash_stream->moof)
2878     gst_isoff_moof_box_free (dash_stream->moof);
2879   if (dash_stream->moof_sync_samples)
2880     g_array_free (dash_stream->moof_sync_samples, TRUE);
2881 }
2882
2883 static GstDashDemuxClockDrift *
2884 gst_dash_demux_clock_drift_new (GstDashDemux * demux)
2885 {
2886   GstDashDemuxClockDrift *clock_drift;
2887
2888   clock_drift = g_slice_new0 (GstDashDemuxClockDrift);
2889   g_mutex_init (&clock_drift->clock_lock);
2890   clock_drift->next_update =
2891       GST_TIME_AS_USECONDS (gst_adaptive_demux_get_monotonic_time
2892       (GST_ADAPTIVE_DEMUX_CAST (demux)));
2893   return clock_drift;
2894 }
2895
2896 static void
2897 gst_dash_demux_clock_drift_free (GstDashDemuxClockDrift * clock_drift)
2898 {
2899   if (clock_drift) {
2900     g_mutex_lock (&clock_drift->clock_lock);
2901     if (clock_drift->ntp_clock)
2902       g_object_unref (clock_drift->ntp_clock);
2903     g_mutex_unlock (&clock_drift->clock_lock);
2904     g_mutex_clear (&clock_drift->clock_lock);
2905     g_slice_free (GstDashDemuxClockDrift, clock_drift);
2906   }
2907 }
2908
2909 /*
2910  * The value attribute of the UTCTiming element contains a white-space
2911  * separated list of servers that are recommended to be used in
2912  * combination with the NTP protocol as defined in IETF RFC 5905 for
2913  * getting the appropriate time.
2914  *
2915  * The DASH standard does not specify which version of NTP. This
2916  * function only works with NTPv4 servers.
2917 */
2918 static GstDateTime *
2919 gst_dash_demux_poll_ntp_server (GstDashDemuxClockDrift * clock_drift,
2920     gchar ** urls)
2921 {
2922   GstClockTime ntp_clock_time;
2923   GDateTime *dt, *dt2;
2924
2925   if (!clock_drift->ntp_clock) {
2926     GResolver *resolver;
2927     GList *inet_addrs;
2928     GError *err;
2929     gchar *ip_addr;
2930
2931     resolver = g_resolver_get_default ();
2932     /* We don't round-robin NTP servers. If the manifest specifies multiple
2933        NTP time servers, select one at random */
2934     clock_drift->selected_url = g_random_int_range (0, g_strv_length (urls));
2935     GST_DEBUG ("Connecting to NTP time server %s",
2936         urls[clock_drift->selected_url]);
2937     inet_addrs = g_resolver_lookup_by_name (resolver,
2938         urls[clock_drift->selected_url], NULL, &err);
2939     g_object_unref (resolver);
2940     if (!inet_addrs || g_list_length (inet_addrs) == 0) {
2941       GST_ERROR ("Failed to resolve hostname of NTP server: %s",
2942           err ? (err->message) : "unknown error");
2943       if (inet_addrs)
2944         g_resolver_free_addresses (inet_addrs);
2945       if (err)
2946         g_error_free (err);
2947       return NULL;
2948     }
2949     ip_addr =
2950         g_inet_address_to_string ((GInetAddress
2951             *) (g_list_first (inet_addrs)->data));
2952     clock_drift->ntp_clock = gst_ntp_clock_new ("dashntp", ip_addr, 123, 0);
2953     g_free (ip_addr);
2954     g_resolver_free_addresses (inet_addrs);
2955     if (!clock_drift->ntp_clock) {
2956       GST_ERROR ("Failed to create NTP clock");
2957       return NULL;
2958     }
2959     if (!gst_clock_wait_for_sync (clock_drift->ntp_clock, 5 * GST_SECOND)) {
2960       g_object_unref (clock_drift->ntp_clock);
2961       clock_drift->ntp_clock = NULL;
2962       GST_ERROR ("Failed to lock to NTP clock");
2963       return NULL;
2964     }
2965   }
2966   ntp_clock_time = gst_clock_get_time (clock_drift->ntp_clock);
2967   if (ntp_clock_time == GST_CLOCK_TIME_NONE) {
2968     GST_ERROR ("Failed to get time from NTP clock");
2969     return NULL;
2970   }
2971   ntp_clock_time -= NTP_TO_UNIX_EPOCH * GST_SECOND;
2972   dt = g_date_time_new_from_unix_utc (ntp_clock_time / GST_SECOND);
2973   if (!dt) {
2974     GST_ERROR ("Failed to create GstDateTime");
2975     return NULL;
2976   }
2977   ntp_clock_time =
2978       gst_util_uint64_scale (ntp_clock_time % GST_SECOND, 1000000, GST_SECOND);
2979   dt2 = g_date_time_add (dt, ntp_clock_time);
2980   g_date_time_unref (dt);
2981   return gst_date_time_new_from_g_date_time (dt2);
2982 }
2983
2984 struct Rfc5322TimeZone
2985 {
2986   const gchar *name;
2987   gfloat tzoffset;
2988 };
2989
2990 /*
2991  Parse an RFC5322 (section 3.3) date-time from the Date: field in the
2992  HTTP response.
2993  See https://tools.ietf.org/html/rfc5322#section-3.3
2994 */
2995 static GstDateTime *
2996 gst_dash_demux_parse_http_head (GstDashDemuxClockDrift * clock_drift,
2997     GstFragment * download)
2998 {
2999   static const gchar *months[] = { NULL, "Jan", "Feb", "Mar", "Apr",
3000     "May", "Jun", "Jul", "Aug",
3001     "Sep", "Oct", "Nov", "Dec", NULL
3002   };
3003   static const struct Rfc5322TimeZone timezones[] = {
3004     {"Z", 0},
3005     {"UT", 0},
3006     {"GMT", 0},
3007     {"BST", 1},
3008     {"EST", -5},
3009     {"EDT", -4},
3010     {"CST", -6},
3011     {"CDT", -5},
3012     {"MST", -7},
3013     {"MDT", -6},
3014     {"PST", -8},
3015     {"PDT", -7},
3016     {NULL, 0}
3017   };
3018   GstDateTime *value = NULL;
3019   const GstStructure *response_headers;
3020   const gchar *http_date;
3021   const GValue *val;
3022   gint ret;
3023   const gchar *pos;
3024   gint year = -1, month = -1, day = -1, hour = -1, minute = -1, second = -1;
3025   gchar zone[6];
3026   gchar monthstr[4];
3027   gfloat tzoffset = 0;
3028   gboolean parsed_tz = FALSE;
3029
3030   g_return_val_if_fail (download != NULL, NULL);
3031   g_return_val_if_fail (download->headers != NULL, NULL);
3032
3033   val = gst_structure_get_value (download->headers, "response-headers");
3034   if (!val) {
3035     return NULL;
3036   }
3037   response_headers = gst_value_get_structure (val);
3038   http_date = gst_structure_get_string (response_headers, "Date");
3039   if (!http_date) {
3040     return NULL;
3041   }
3042
3043   /* skip optional text version of day of the week */
3044   pos = strchr (http_date, ',');
3045   if (pos)
3046     pos++;
3047   else
3048     pos = http_date;
3049   ret =
3050       sscanf (pos, "%02d %3s %04d %02d:%02d:%02d %5s", &day, monthstr, &year,
3051       &hour, &minute, &second, zone);
3052   if (ret == 7) {
3053     gchar *z = zone;
3054     gint i;
3055
3056     for (i = 1; months[i]; ++i) {
3057       if (g_ascii_strncasecmp (months[i], monthstr, strlen (months[i])) == 0) {
3058         month = i;
3059         break;
3060       }
3061     }
3062     for (i = 0; timezones[i].name && !parsed_tz; ++i) {
3063       if (g_ascii_strncasecmp (timezones[i].name, z,
3064               strlen (timezones[i].name)) == 0) {
3065         tzoffset = timezones[i].tzoffset;
3066         parsed_tz = TRUE;
3067       }
3068     }
3069     if (!parsed_tz) {
3070       gint hh, mm;
3071       gboolean neg = FALSE;
3072       /* check if it is in the form +-HHMM */
3073       if (*z == '+' || *z == '-') {
3074         if (*z == '+')
3075           ++z;
3076         else if (*z == '-') {
3077           ++z;
3078           neg = TRUE;
3079         }
3080         ret = sscanf (z, "%02d%02d", &hh, &mm);
3081         if (ret == 2) {
3082           tzoffset = hh;
3083           tzoffset += mm / 60.0;
3084           if (neg)
3085             tzoffset = -tzoffset;
3086           parsed_tz = TRUE;
3087         }
3088       }
3089     }
3090     /* Accept year in both 2 digit or 4 digit format */
3091     if (year < 100)
3092       year += 2000;
3093   }
3094   if (month > 0 && parsed_tz) {
3095     value = gst_date_time_new (tzoffset,
3096         year, month, day, hour, minute, second);
3097   }
3098   return value;
3099 }
3100
3101 /*
3102    The timing information is contained in the message body of the HTTP
3103    response and contains a time value formatted according to NTP timestamp
3104    format in IETF RFC 5905.
3105
3106        0                   1                   2                   3
3107        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3108       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3109       |                            Seconds                            |
3110       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3111       |                            Fraction                           |
3112       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3113
3114                              NTP Timestamp Format
3115 */
3116 static GstDateTime *
3117 gst_dash_demux_parse_http_ntp (GstDashDemuxClockDrift * clock_drift,
3118     GstBuffer * buffer)
3119 {
3120   gint64 seconds;
3121   guint64 fraction;
3122   GDateTime *dt, *dt2;
3123   GstMapInfo mapinfo;
3124
3125   /* See https://tools.ietf.org/html/rfc5905#page-12 for details of
3126      the NTP Timestamp Format */
3127   gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
3128   if (mapinfo.size != 8) {
3129     gst_buffer_unmap (buffer, &mapinfo);
3130     return NULL;
3131   }
3132   seconds = GST_READ_UINT32_BE (mapinfo.data);
3133   fraction = GST_READ_UINT32_BE (mapinfo.data + 4);
3134   gst_buffer_unmap (buffer, &mapinfo);
3135   fraction = gst_util_uint64_scale (fraction, 1000000,
3136       G_GUINT64_CONSTANT (1) << 32);
3137   /* subtract constant to convert from 1900 based time to 1970 based time */
3138   seconds -= NTP_TO_UNIX_EPOCH;
3139   dt = g_date_time_new_from_unix_utc (seconds);
3140   dt2 = g_date_time_add (dt, fraction);
3141   g_date_time_unref (dt);
3142   return gst_date_time_new_from_g_date_time (dt2);
3143 }
3144
3145 /*
3146   The timing information is contained in the message body of the
3147   HTTP response and contains a time value formatted according to
3148   xs:dateTime as defined in W3C XML Schema Part 2: Datatypes specification.
3149 */
3150 static GstDateTime *
3151 gst_dash_demux_parse_http_xsdate (GstDashDemuxClockDrift * clock_drift,
3152     GstBuffer * buffer)
3153 {
3154   GstDateTime *value = NULL;
3155   GstMapInfo mapinfo;
3156
3157   /* the string from the server might not be zero terminated */
3158   if (gst_buffer_map (buffer, &mapinfo, GST_MAP_READ)) {
3159     gchar *str;
3160     str = g_strndup ((const gchar *) mapinfo.data, mapinfo.size);
3161     gst_buffer_unmap (buffer, &mapinfo);
3162     value = gst_date_time_new_from_iso8601_string (str);
3163     g_free (str);
3164   }
3165   return value;
3166 }
3167
3168 static gboolean
3169 gst_dash_demux_poll_clock_drift (GstDashDemux * demux)
3170 {
3171   GstDashDemuxClockDrift *clock_drift;
3172   GDateTime *start = NULL, *end;
3173   GstBuffer *buffer = NULL;
3174   GstDateTime *value = NULL;
3175   gboolean ret = FALSE;
3176   gint64 now;
3177   GstMPDUTCTimingType method;
3178   gchar **urls;
3179
3180   g_return_val_if_fail (demux != NULL, FALSE);
3181   g_return_val_if_fail (demux->clock_drift != NULL, FALSE);
3182   clock_drift = demux->clock_drift;
3183   now =
3184       GST_TIME_AS_USECONDS (gst_adaptive_demux_get_monotonic_time
3185       (GST_ADAPTIVE_DEMUX_CAST (demux)));
3186   if (now < clock_drift->next_update) {
3187     /*TODO: If a fragment fails to download in adaptivedemux, it waits
3188        for a manifest reload before another attempt to fetch a fragment.
3189        Section 10.8.6 of the DVB-DASH standard states that the DASH client
3190        shall refresh the manifest and resynchronise to one of the time sources.
3191
3192        Currently the fact that the manifest refresh follows a download failure
3193        does not make it into dashdemux. */
3194     return TRUE;
3195   }
3196   urls = gst_mpd_client_get_utc_timing_sources (demux->client,
3197       SUPPORTED_CLOCK_FORMATS, &method);
3198   if (!urls) {
3199     return FALSE;
3200   }
3201   /* Update selected_url just in case the number of URLs in the UTCTiming
3202      element has shrunk since the last poll */
3203   clock_drift->selected_url = clock_drift->selected_url % g_strv_length (urls);
3204   g_mutex_lock (&clock_drift->clock_lock);
3205
3206   if (method == GST_MPD_UTCTIMING_TYPE_NTP) {
3207     value = gst_dash_demux_poll_ntp_server (clock_drift, urls);
3208     if (!value) {
3209       GST_ERROR_OBJECT (demux, "Failed to fetch time from NTP server %s",
3210           urls[clock_drift->selected_url]);
3211       g_mutex_unlock (&clock_drift->clock_lock);
3212       goto quit;
3213     }
3214   }
3215   start =
3216       gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3217   if (!value) {
3218     GstFragment *download;
3219 #ifdef TIZEN_FEATURE_ADAPTIVE_MODIFICATION
3220   GST_DEBUG_OBJECT (demux, "Fetching current time from %s",
3221       urls[clock_drift->selected_url]);
3222   download = gst_uri_downloader_fetch_uri (GST_ADAPTIVE_DEMUX_CAST
3223       (demux)->downloader, urls[clock_drift->selected_url], NULL, NULL, NULL,
3224       DEFAULT_ADAPTIVE_RETRY, DEFAULT_ADAPTIVE_TIMEOUT, TRUE, FALSE, TRUE,
3225       NULL);
3226 #else
3227     gint64 range_start = 0, range_end = -1;
3228     GST_DEBUG_OBJECT (demux, "Fetching current time from %s",
3229         urls[clock_drift->selected_url]);
3230     if (method == GST_MPD_UTCTIMING_TYPE_HTTP_HEAD) {
3231       range_start = -1;
3232     }
3233     download =
3234         gst_uri_downloader_fetch_uri_with_range (GST_ADAPTIVE_DEMUX_CAST
3235         (demux)->downloader, urls[clock_drift->selected_url], NULL, TRUE, TRUE,
3236         TRUE, range_start, range_end, NULL);
3237 #endif
3238     if (download) {
3239       if (method == GST_MPD_UTCTIMING_TYPE_HTTP_HEAD && download->headers) {
3240         value = gst_dash_demux_parse_http_head (clock_drift, download);
3241       } else {
3242         buffer = gst_fragment_get_buffer (download);
3243       }
3244       g_object_unref (download);
3245     }
3246   }
3247   g_mutex_unlock (&clock_drift->clock_lock);
3248   if (!value && !buffer) {
3249     GST_ERROR_OBJECT (demux, "Failed to fetch time from %s",
3250         urls[clock_drift->selected_url]);
3251     goto quit;
3252   }
3253   end = gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3254   if (!value && method == GST_MPD_UTCTIMING_TYPE_HTTP_NTP) {
3255     value = gst_dash_demux_parse_http_ntp (clock_drift, buffer);
3256   } else if (!value) {
3257     /* GST_MPD_UTCTIMING_TYPE_HTTP_XSDATE or GST_MPD_UTCTIMING_TYPE_HTTP_ISO */
3258     value = gst_dash_demux_parse_http_xsdate (clock_drift, buffer);
3259   }
3260   if (buffer)
3261     gst_buffer_unref (buffer);
3262   if (value) {
3263     GTimeSpan download_duration = g_date_time_difference (end, start);
3264     GDateTime *client_now, *server_now;
3265     /* We don't know when the server sampled its clock, but we know
3266        it must have been before "end" and probably after "start".
3267        A reasonable estimate is to use (start+end)/2
3268      */
3269     client_now = g_date_time_add (start, download_duration / 2);
3270     server_now = gst_date_time_to_g_date_time (value);
3271     /* If gst_date_time_new_from_iso8601_string is given an unsupported
3272        ISO 8601 format, it can return a GstDateTime that is not valid,
3273        which causes gst_date_time_to_g_date_time to return NULL */
3274     if (server_now) {
3275       g_mutex_lock (&clock_drift->clock_lock);
3276       clock_drift->clock_compensation =
3277           g_date_time_difference (server_now, client_now);
3278       g_mutex_unlock (&clock_drift->clock_lock);
3279       GST_DEBUG_OBJECT (demux,
3280           "Difference between client and server clocks is %lfs",
3281           ((double) clock_drift->clock_compensation) / 1000000.0);
3282       g_date_time_unref (server_now);
3283       ret = TRUE;
3284     } else {
3285       GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
3286     }
3287     g_date_time_unref (client_now);
3288     gst_date_time_unref (value);
3289   } else {
3290     GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
3291   }
3292   g_date_time_unref (end);
3293 quit:
3294   if (start)
3295     g_date_time_unref (start);
3296   /* if multiple URLs were specified, use a simple round-robin to
3297      poll each server */
3298   g_mutex_lock (&clock_drift->clock_lock);
3299   if (method == GST_MPD_UTCTIMING_TYPE_NTP) {
3300     clock_drift->next_update = now + FAST_CLOCK_UPDATE_INTERVAL;
3301   } else {
3302     clock_drift->selected_url =
3303         (1 + clock_drift->selected_url) % g_strv_length (urls);
3304     if (ret) {
3305       clock_drift->next_update = now + SLOW_CLOCK_UPDATE_INTERVAL;
3306     } else {
3307       clock_drift->next_update = now + FAST_CLOCK_UPDATE_INTERVAL;
3308     }
3309   }
3310   g_mutex_unlock (&clock_drift->clock_lock);
3311   return ret;
3312 }
3313
3314 static GTimeSpan
3315 gst_dash_demux_get_clock_compensation (GstDashDemux * demux)
3316 {
3317   GTimeSpan rv = 0;
3318   if (demux->clock_drift) {
3319     g_mutex_lock (&demux->clock_drift->clock_lock);
3320     rv = demux->clock_drift->clock_compensation;
3321     g_mutex_unlock (&demux->clock_drift->clock_lock);
3322   }
3323   GST_LOG_OBJECT (demux, "Clock drift %" GST_STIME_FORMAT, GST_STIME_ARGS (rv));
3324   return rv;
3325 }
3326
3327 static GDateTime *
3328 gst_dash_demux_get_server_now_utc (GstDashDemux * demux)
3329 {
3330   GDateTime *client_now;
3331   GDateTime *server_now;
3332
3333   client_now =
3334       gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3335   server_now =
3336       g_date_time_add (client_now,
3337       gst_dash_demux_get_clock_compensation (demux));
3338   g_date_time_unref (client_now);
3339   return server_now;
3340 }