1 /*
2 * DASH demux plugin for GStreamer
3 *
4 * gstdashdemux.c
5 *
6 * Copyright (C) 2012 Orange
7 *
8 * Authors:
9 * David Corvoysier <david.corvoysier@orange.com>
10 * Hamid Zakari <hamid.zakari@gmail.com>
11 *
12 * Copyright (C) 2013 Smart TV Alliance
13 * Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>, Collabora Ltd.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Library General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Library General Public License for more details.
24 *
25 * You should have received a copy of the GNU Library General Public
26 * License along with this library (COPYING); if not, write to the
27 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
29 */
30 /**
31 * SECTION:element-dashdemux
32 * @title: dashdemux
33 *
34 * DASH demuxer element.
35 * ## Example launch line
36 * |[
37 * gst-launch-1.0 playbin uri="http://www-itec.uni-klu.ac.at/ftp/datasets/mmsys12/RedBullPlayStreets/redbull_4s/RedBullPlayStreets_4s_isoffmain_DIS_23009_1_v_2_1c2_2011_08_30.mpd"
38 * ]|
39 */
40
41 /* Implementation notes:
42 *
43 * The following section describes how dashdemux works internally.
44 *
45 * Introduction:
46 *
47 * dashdemux is a "fake" demux, as unlike traditional demux elements, it
48 * doesn't split data streams contained in an envelope to expose them to
49 * downstream decoding elements.
50 *
51 * Instead, it parses an XML file called a manifest to identify a set of
52 * individual stream fragments it needs to fetch and expose to the actual
53 * demux elements that will handle them (this behavior is sometimes
54 * referred as the "demux after a demux" scenario).
55 *
56 * For a given section of content, several representations corresponding
57 * to different bitrates may be available: dashdemux will select the most
58 * appropriate representation based on local conditions (typically the
59 * available bandwidth and the amount of buffering available, capped by
60 * a maximum allowed bitrate).
61 *
62 * The representation selection algorithm can be configured using
63 * specific properties: max bitrate, min/max buffering, bandwidth ratio.
64 *
65 *
66 * General Design:
67 *
68 * dashdemux has a single sink pad that accepts the data corresponding
69 * to the manifest, typically fetched from an HTTP or file source.
70 *
71 * dashdemux exposes the streams it recreates based on the fragments it
72 * fetches through dedicated src pads corresponding to the caps of the
73 * fragments container (ISOBMFF/MP4 or MPEG2TS).
74 *
75 * During playback, new representations will typically be exposed as a
76 * new set of pads (see 'Switching between representations' below).
77 *
78 * Fragments downloading is performed using a dedicated task that fills
79 * an internal queue. Another task is in charge of popping fragments
80 * from the queue and pushing them downstream.
81 *
82 * Switching between representations:
83 *
84 * Decodebin supports scenarios allowing to seamlessly switch from one
85 * stream to another inside the same "decoding chain".
86 *
87 * To achieve that, it combines the elements it autoplugged in chains
88 * and groups, allowing only one decoding group to be active at a given
89 * time for a given chain.
90 *
91 * A chain can signal decodebin that it is complete by sending a
92 * no-more-pads event, but even after that new pads can be added to
93 * create new subgroups, providing that a new no-more-pads event is sent.
94 *
95 * We take advantage of that to dynamically create a new decoding group
96 * in order to select a different representation during playback.
97 *
98 * Typically, assuming that each fragment contains both audio and video,
99 * the following tree would be created:
100 *
101 * chain "DASH Demux"
102 * |_ group "Representation set 1"
103 * | |_ chain "Qt Demux 0"
104 * | |_ group "Stream 0"
105 * | |_ chain "H264"
106 * | |_ chain "AAC"
107 * |_ group "Representation set 2"
108 * |_ chain "Qt Demux 1"
109 * |_ group "Stream 1"
110 * |_ chain "H264"
111 * |_ chain "AAC"
112 *
113 * Or, if audio and video are contained in separate fragments:
114 *
115 * chain "DASH Demux"
116 * |_ group "Representation set 1"
117 * | |_ chain "Qt Demux 0"
118 * | | |_ group "Stream 0"
119 * | | |_ chain "H264"
120 * | |_ chain "Qt Demux 1"
121 * | |_ group "Stream 1"
122 * | |_ chain "AAC"
123 * |_ group "Representation set 2"
124 * |_ chain "Qt Demux 3"
125 * | |_ group "Stream 2"
126 * | |_ chain "H264"
127 * |_ chain "Qt Demux 4"
128 * |_ group "Stream 3"
129 * |_ chain "AAC"
130 *
131 * In both cases, when switching from Set 1 to Set 2 an EOS is sent on
132 * each end pad corresponding to Rep 0, triggering the "drain" state to
133 * propagate upstream.
134 * Once both EOS have been processed, the "Set 1" group is completely
135 * drained, and decodebin2 will switch to the "Set 2" group.
136 *
137 * Note: nothing can be pushed to the new decoding group before the
138 * old one has been drained, which means that in order to be able to
139 * adapt quickly to bandwidth changes, we will not be able to rely
140 * on downstream buffering, and will instead manage an internal queue.
141 *
142 *
143 * Keyframe trick-mode implementation:
144 *
145 * When requested (with GST_SEEK_FLAG_TRICKMODE_KEY_UNIT) and if the format
146 * is supported (ISOBMFF profiles), dashdemux can download only keyframes
147 * in order to provide fast forward/reverse playback without exceeding the
148 * available bandwith/cpu/memory usage.
149 *
150 * This is done in two parts:
151 * 1) Parsing ISOBMFF atoms to detect the location of keyframes and only
152 * download/push those.
153 * 2) Deciding what the ideal next keyframe to download is in order to
154 * provide as many keyframes as possible without rebuffering.
155 *
156 * * Keyframe-only downloads:
157 *
158 * For each beginning of fragment, the fragment header will be parsed in
159 * gst_dash_demux_parse_isobmff() and then the information (offset, pts...)
160 * of each keyframe will be stored in moof_sync_samples.
161 *
162 * gst_dash_demux_stream_update_fragment_info() will specify the range
163 * start and end of the current keyframe, which will cause GstAdaptiveDemux
164 * to do a new upstream range request.
165 *
166 * When advancing, if there are still some keyframes in the current
167 * fragment, gst_dash_demux_stream_advance_fragment() will call
168 * gst_dash_demux_stream_advance_sync_sample() which decides what the next
169 * keyframe to get will be (it can be in reverse order for example, or
170 * might not be the *next* keyframe but one further as explained below).
171 *
172 * If no more keyframes are available in the current fragment, dash will
173 * advance to the next fragment (just like in the normal case) or to a
174 * fragment much further away (as explained below).
175 *
176 *
177 * * Deciding the optimal "next" keyframe/fragment to download:
178 *
179 * The main reason for doing keyframe-only downloads is for trick-modes
180 * (i.e. being able to do fast reverse/forward playback with limited
181 * bandwith/cpu/memory).
182 *
183 * Downloading all keyframes might not be the optimal solution, especially
184 * at high playback rates, since the time taken to download the keyframe
185 * might exceed the available running time between two displayed frames
186 * (i.e. all frames would end up arriving late). This would cause severe
187 * rebuffering.
188 *
189 * Note: The values specified below can be in either the segment running
190 * time or in absolute values. Where position values need to be converted
191 * to segment running time the "running_time(val)" notation is used, and
192 * where running time need ot be converted to segment poisition the
193 * "position(val)" notation is used.
194 *
195 * The goal instead is to be able to download/display as many frames as
196 * possible for a given playback rate. For that the implementation will
197 * take into account:
198 * * The requested playback rate and segment
199 * * The average time to request and download a keyframe (in running time)
200 * * The current position of dashdemux in the stream
201 * * The current downstream (i.e. sink) position (in running time)
202 *
203 * To reach this goal we consider that there is some amount of buffering
204 * (in time) between dashdemux and the display sink. While we do not know
205 * the exact amount of buffering available, a safe and reasonable assertion
206 * is that there is at least a second (in running time).
207 *
208 * The average time to request and fully download a keyframe (with or
209 * without fragment header) is obtained by averaging the
210 * GstAdaptiveDemuxStream->last_download_time and is stored in
211 * GstDashDemuxStream->average_download_time. Those values include the
212 * network latency and full download time, which are more interesting and
213 * correct than just bitrates (with small download sizes, the impact of the
214 * network latency is much higher).
215 *
216 * The current position is calculated based on the fragment timestamp and
217 * the current keyframe index within that fragment. It is stored in
218 * GstDashDemuxStream->actual_position.
219 *
220 * The downstream position of the pipeline is obtained via QoS events and
221 * is stored in GstAdaptiveDemuxStream->qos_earliest_time (note: it's a
222 * running time value).
223 *
224 * The estimated buffering level between dashdemux and downstream is
225 * therefore:
226 * buffering_level = running_time(actual_position) - qos_earliest_time
227 *
228 * In order to avoid rebuffering, we want to ensure that the next keyframe
229 * (including potential fragment header) we request will be download, demuxed
230 * and decoded in time so that it is not late. That next keyframe time is
231 * called the "target_time" and is calculated whenever we have finished
232 * pushing a keyframe downstream.
233 *
234 * One simple observation at this point is that we *need* to make sure that
235 * the target time is chosen such that:
236 * running_time(target_time) > qos_earliest_time + average_download_time
237 *
238 * i.e. we chose a target time which will be greater than the time at which
239 * downstream will be once we request and download the keyframe (otherwise
240 * we're guaranteed to be late).
241 *
242 * This would provide the highest number of displayed frames per
243 * second, but it is just a *minimal* value and is not enough as-is,
244 * since it doesn't take into account the following items which could
245 * cause frames to arrive late (and therefore rebuffering):
246 * * Network jitter (i.e. by how much the download time can fluctuate)
247 * * Network stalling
248 * * Different keyframe sizes (and therefore download time)
249 * * Decoding speed
250 *
251 * Instead, we adjust the target time calculation based on the
252 * buffering_level.
253 *
254 * The smaller the buffering level is (i.e. the closer we are between
255 * current and downstream), the more aggresively we skip forward (and
256 * guarantee the keyframe will be downloaded, decoded and displayed in
257 * time). And the higher the buffering level, the least aggresivelly
258 * we need to skip forward (and therefore display more frames per
259 * second).
260 *
261 * Right now the threshold for agressive switching is set to 3
262 * average_download_time. Below that buffering level we set the target time
263 * to at least 3 average_download_time distance beyond the
264 * qos_earliest_time.
265 *
266 * If we are above that buffering level we set the target time to:
267 * position(running_time(position) + average_download_time)
268 *
269 * The logic is therefore:
270 * WHILE(!EOS)
271 * Calculate target_time
272 * Advance to keyframe/fragment for that target_time
273 * Adaptivedemux downloads that keyframe/fragment
274 *
275 */
276
277 #ifdef HAVE_CONFIG_H
278 # include "config.h"
279 #endif
280
281 #include <string.h>
282 #include <stdio.h>
283 #include <stdlib.h>
284 #include <inttypes.h>
285 #include <gio/gio.h>
286 #include <gst/base/gsttypefindhelper.h>
287 #include <gst/tag/tag.h>
288 #include <gst/net/gstnet.h>
289 #include "gst/gst-i18n-plugin.h"
290 #include "gstdashdemux.h"
291 #include "gstdash_debug.h"
292
293 static GstStaticPadTemplate gst_dash_demux_videosrc_template =
294 GST_STATIC_PAD_TEMPLATE ("video_%02u",
295 GST_PAD_SRC,
296 GST_PAD_SOMETIMES,
297 GST_STATIC_CAPS_ANY);
298
299 static GstStaticPadTemplate gst_dash_demux_audiosrc_template =
300 GST_STATIC_PAD_TEMPLATE ("audio_%02u",
301 GST_PAD_SRC,
302 GST_PAD_SOMETIMES,
303 GST_STATIC_CAPS_ANY);
304
305 static GstStaticPadTemplate gst_dash_demux_subtitlesrc_template =
306 GST_STATIC_PAD_TEMPLATE ("subtitle_%02u",
307 GST_PAD_SRC,
308 GST_PAD_SOMETIMES,
309 GST_STATIC_CAPS_ANY);
310
311 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
312 GST_PAD_SINK,
313 GST_PAD_ALWAYS,
314 GST_STATIC_CAPS ("application/dash+xml"));
315
316 GST_DEBUG_CATEGORY (gst_dash_demux_debug);
317 #define GST_CAT_DEFAULT gst_dash_demux_debug
318
319 enum
320 {
321 PROP_0,
322
323 PROP_MAX_BUFFERING_TIME,
324 PROP_BANDWIDTH_USAGE,
325 PROP_MAX_BITRATE,
326 PROP_MAX_VIDEO_WIDTH,
327 PROP_MAX_VIDEO_HEIGHT,
328 PROP_MAX_VIDEO_FRAMERATE,
329 PROP_PRESENTATION_DELAY,
330 PROP_LAST
331 };
332
333 /* Default values for properties */
334 #define DEFAULT_MAX_BUFFERING_TIME 30 /* in seconds */
335 #define DEFAULT_BANDWIDTH_USAGE 0.8f /* 0 to 1 */
336 #define DEFAULT_MAX_BITRATE 0 /* in bit/s */
337 #define DEFAULT_MAX_VIDEO_WIDTH 0
338 #define DEFAULT_MAX_VIDEO_HEIGHT 0
339 #define DEFAULT_MAX_VIDEO_FRAMERATE_N 0
340 #define DEFAULT_MAX_VIDEO_FRAMERATE_D 1
341 #define DEFAULT_PRESENTATION_DELAY "10s" /* 10s */
342
343 /* Clock drift compensation for live streams */
344 #define SLOW_CLOCK_UPDATE_INTERVAL (1000000 * 30 * 60) /* 30 minutes */
345 #define FAST_CLOCK_UPDATE_INTERVAL (1000000 * 30) /* 30 seconds */
346 #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)
347 #define NTP_TO_UNIX_EPOCH G_GUINT64_CONSTANT(2208988800) /* difference (in seconds) between NTP epoch and Unix epoch */
348
349 struct _GstDashDemuxClockDrift
350 {
351 GMutex clock_lock; /* used to protect access to struct */
352 guint selected_url;
353 gint64 next_update;
354 /* @clock_compensation: amount (in usecs) to add to client's idea of
355 now to map it to the server's idea of now */
356 GTimeSpan clock_compensation;
357 GstClock *ntp_clock;
358 };
359
360 typedef struct
361 {
362 guint64 start_offset, end_offset;
363 /* TODO: Timestamp and duration */
364 } GstDashStreamSyncSample;
365
366 /* GObject */
367 static void gst_dash_demux_set_property (GObject * object, guint prop_id,
368 const GValue * value, GParamSpec * pspec);
369 static void gst_dash_demux_get_property (GObject * object, guint prop_id,
370 GValue * value, GParamSpec * pspec);
371 static void gst_dash_demux_dispose (GObject * obj);
372
373 /* GstAdaptiveDemux */
374 static GstClockTime gst_dash_demux_get_duration (GstAdaptiveDemux * ademux);
375 static gboolean gst_dash_demux_is_live (GstAdaptiveDemux * ademux);
376 static void gst_dash_demux_reset (GstAdaptiveDemux * ademux);
377 static gboolean gst_dash_demux_process_manifest (GstAdaptiveDemux * ademux,
378 GstBuffer * buf);
379 static gboolean gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
380 static GstFlowReturn
381 gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream);
382 static GstFlowReturn gst_dash_demux_stream_seek (GstAdaptiveDemuxStream *
383 stream, gboolean forward, GstSeekFlags flags, GstClockTime ts,
384 GstClockTime * final_ts);
385 static gboolean gst_dash_demux_stream_has_next_fragment (GstAdaptiveDemuxStream
386 * stream);
387 static GstFlowReturn
388 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream);
389 static gboolean
390 gst_dash_demux_stream_advance_subfragment (GstAdaptiveDemuxStream * stream);
391 static gboolean gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream *
392 stream, guint64 bitrate);
393 static gint64 gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux *
394 demux);
395 static GstFlowReturn gst_dash_demux_update_manifest_data (GstAdaptiveDemux *
396 demux, GstBuffer * buf);
397 static gint64
398 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
399 stream);
400 static void gst_dash_demux_advance_period (GstAdaptiveDemux * demux);
401 static gboolean gst_dash_demux_has_next_period (GstAdaptiveDemux * demux);
402 static GstFlowReturn gst_dash_demux_data_received (GstAdaptiveDemux * demux,
403 GstAdaptiveDemuxStream * stream, GstBuffer * buffer);
404 static gboolean
405 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
406 GstAdaptiveDemuxStream * stream);
407 static GstFlowReturn
408 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
409 GstAdaptiveDemuxStream * stream);
410 static gboolean gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream *
411 stream);
412
413 /* GstDashDemux */
414 static gboolean gst_dash_demux_setup_all_streams (GstDashDemux * demux);
415 static void gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream);
416
417 static GstCaps *gst_dash_demux_get_input_caps (GstDashDemux * demux,
418 GstActiveStream * stream);
419 static GstPad *gst_dash_demux_create_pad (GstDashDemux * demux,
420 GstActiveStream * stream);
421 static GstDashDemuxClockDrift *gst_dash_demux_clock_drift_new (GstDashDemux *
422 demux);
423 static void gst_dash_demux_clock_drift_free (GstDashDemuxClockDrift *);
424 static gboolean gst_dash_demux_poll_clock_drift (GstDashDemux * demux);
425 static GTimeSpan gst_dash_demux_get_clock_compensation (GstDashDemux * demux);
426 static GDateTime *gst_dash_demux_get_server_now_utc (GstDashDemux * demux);
427
428 #define SIDX(s) (&(s)->sidx_parser.sidx)
429
430 static inline GstSidxBoxEntry *
SIDX_ENTRY(GstDashDemuxStream * s,gint i)431 SIDX_ENTRY (GstDashDemuxStream * s, gint i)
432 {
433 g_assert (i < SIDX (s)->entries_count);
434 return &(SIDX (s)->entries[(i)]);
435 }
436
437 #define SIDX_CURRENT_ENTRY(s) SIDX_ENTRY(s, SIDX(s)->entry_index)
438
439 static void gst_dash_demux_send_content_protection_event (gpointer cp_data,
440 gpointer stream);
441
442 #define gst_dash_demux_parent_class parent_class
443 G_DEFINE_TYPE_WITH_CODE (GstDashDemux, gst_dash_demux, GST_TYPE_ADAPTIVE_DEMUX,
444 GST_DEBUG_CATEGORY_INIT (gst_dash_demux_debug, "dashdemux", 0,
445 "dashdemux element")
446 );
447
448 static void
gst_dash_demux_dispose(GObject * obj)449 gst_dash_demux_dispose (GObject * obj)
450 {
451 GstDashDemux *demux = GST_DASH_DEMUX (obj);
452
453 gst_dash_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
454
455 if (demux->client) {
456 gst_mpd_client_free (demux->client);
457 demux->client = NULL;
458 }
459
460 g_mutex_clear (&demux->client_lock);
461
462 gst_dash_demux_clock_drift_free (demux->clock_drift);
463 demux->clock_drift = NULL;
464 g_free (demux->default_presentation_delay);
465 G_OBJECT_CLASS (parent_class)->dispose (obj);
466 }
467
468 static gboolean
gst_dash_demux_get_live_seek_range(GstAdaptiveDemux * demux,gint64 * start,gint64 * stop)469 gst_dash_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
470 gint64 * stop)
471 {
472 GstDashDemux *self = GST_DASH_DEMUX (demux);
473 GDateTime *now;
474 GDateTime *mstart;
475 GTimeSpan stream_now;
476 GstClockTime seg_duration;
477
478 if (self->client->mpd_node->availabilityStartTime == NULL)
479 return FALSE;
480
481 seg_duration = gst_mpd_client_get_maximum_segment_duration (self->client);
482 now = gst_dash_demux_get_server_now_utc (self);
483 mstart =
484 gst_date_time_to_g_date_time (self->client->
485 mpd_node->availabilityStartTime);
486 stream_now = g_date_time_difference (now, mstart);
487 g_date_time_unref (now);
488 g_date_time_unref (mstart);
489
490 if (stream_now <= 0)
491 return FALSE;
492
493 *stop = stream_now * GST_USECOND;
494 if (self->client->mpd_node->timeShiftBufferDepth == GST_MPD_DURATION_NONE) {
495 *start = 0;
496 } else {
497 *start =
498 *stop - (self->client->mpd_node->timeShiftBufferDepth * GST_MSECOND);
499 if (*start < 0)
500 *start = 0;
501 }
502
503 /* As defined in 5.3.9.5.3 of the DASH specification, a segment does
504 not become available until the sum of:
505 * the value of the MPD@availabilityStartTime,
506 * the PeriodStart time of the containing Period
507 * the MPD start time of the Media Segment, and
508 * the MPD duration of the Media Segment.
509 Therefore we need to subtract the media segment duration from the stop
510 time.
511 */
512 *stop -= seg_duration;
513 return TRUE;
514 }
515
516 static GstClockTime
gst_dash_demux_get_presentation_offset(GstAdaptiveDemux * demux,GstAdaptiveDemuxStream * stream)517 gst_dash_demux_get_presentation_offset (GstAdaptiveDemux * demux,
518 GstAdaptiveDemuxStream * stream)
519 {
520 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
521 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
522
523 return gst_mpd_parser_get_stream_presentation_offset (dashdemux->client,
524 dashstream->index);
525 }
526
527 static GstClockTime
gst_dash_demux_get_period_start_time(GstAdaptiveDemux * demux)528 gst_dash_demux_get_period_start_time (GstAdaptiveDemux * demux)
529 {
530 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
531
532 return gst_mpd_parser_get_period_start_time (dashdemux->client);
533 }
534
535 static void
gst_dash_demux_class_init(GstDashDemuxClass * klass)536 gst_dash_demux_class_init (GstDashDemuxClass * klass)
537 {
538 GObjectClass *gobject_class;
539 GstElementClass *gstelement_class;
540 GstAdaptiveDemuxClass *gstadaptivedemux_class;
541
542 gobject_class = (GObjectClass *) klass;
543 gstelement_class = (GstElementClass *) klass;
544 gstadaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
545
546 gobject_class->set_property = gst_dash_demux_set_property;
547 gobject_class->get_property = gst_dash_demux_get_property;
548 gobject_class->dispose = gst_dash_demux_dispose;
549
550 #ifndef GST_REMOVE_DEPRECATED
551 g_object_class_install_property (gobject_class, PROP_MAX_BUFFERING_TIME,
552 g_param_spec_uint ("max-buffering-time", "Maximum buffering time",
553 "Maximum number of seconds of buffer accumulated during playback"
554 "(deprecated)",
555 2, G_MAXUINT, DEFAULT_MAX_BUFFERING_TIME,
556 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
557
558 g_object_class_install_property (gobject_class, PROP_BANDWIDTH_USAGE,
559 g_param_spec_float ("bandwidth-usage",
560 "Bandwidth usage [0..1]",
561 "Percentage of the available bandwidth to use when "
562 "selecting representations (deprecated)",
563 0, 1, DEFAULT_BANDWIDTH_USAGE,
564 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
565 #endif
566
567 g_object_class_install_property (gobject_class, PROP_MAX_BITRATE,
568 g_param_spec_uint ("max-bitrate", "Max bitrate",
569 "Max of bitrate supported by target video decoder (0 = no maximum)",
570 0, G_MAXUINT, DEFAULT_MAX_BITRATE,
571 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
572
573 g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_WIDTH,
574 g_param_spec_uint ("max-video-width", "Max video width",
575 "Max video width to select (0 = no maximum)",
576 0, G_MAXUINT, DEFAULT_MAX_VIDEO_WIDTH,
577 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578
579 g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_HEIGHT,
580 g_param_spec_uint ("max-video-height", "Max video height",
581 "Max video height to select (0 = no maximum)",
582 0, G_MAXUINT, DEFAULT_MAX_VIDEO_HEIGHT,
583 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584
585 g_object_class_install_property (gobject_class, PROP_MAX_VIDEO_FRAMERATE,
586 gst_param_spec_fraction ("max-video-framerate", "Max video framerate",
587 "Max video framerate to select (0/1 = no maximum)",
588 0, 1, G_MAXINT, 1, DEFAULT_MAX_VIDEO_FRAMERATE_N,
589 DEFAULT_MAX_VIDEO_FRAMERATE_D,
590 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
591
592 g_object_class_install_property (gobject_class, PROP_PRESENTATION_DELAY,
593 g_param_spec_string ("presentation-delay", "Presentation delay",
594 "Default presentation delay (in seconds, milliseconds or fragments) (e.g. 12s, 2500ms, 3f)",
595 DEFAULT_PRESENTATION_DELAY,
596 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
597
598 gst_element_class_add_static_pad_template (gstelement_class,
599 &gst_dash_demux_audiosrc_template);
600 gst_element_class_add_static_pad_template (gstelement_class,
601 &gst_dash_demux_videosrc_template);
602 gst_element_class_add_static_pad_template (gstelement_class,
603 &gst_dash_demux_subtitlesrc_template);
604
605 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
606
607 gst_element_class_set_static_metadata (gstelement_class,
608 "DASH Demuxer",
609 "Codec/Demuxer/Adaptive",
610 "Dynamic Adaptive Streaming over HTTP demuxer",
611 "David Corvoysier <david.corvoysier@orange.com>\n\
612 Hamid Zakari <hamid.zakari@gmail.com>\n\
613 Gianluca Gennari <gennarone@gmail.com>");
614
615
616 gstadaptivedemux_class->get_duration = gst_dash_demux_get_duration;
617 gstadaptivedemux_class->is_live = gst_dash_demux_is_live;
618 gstadaptivedemux_class->reset = gst_dash_demux_reset;
619 gstadaptivedemux_class->seek = gst_dash_demux_seek;
620
621 gstadaptivedemux_class->process_manifest = gst_dash_demux_process_manifest;
622 gstadaptivedemux_class->update_manifest_data =
623 gst_dash_demux_update_manifest_data;
624 gstadaptivedemux_class->get_manifest_update_interval =
625 gst_dash_demux_get_manifest_update_interval;
626
627 gstadaptivedemux_class->has_next_period = gst_dash_demux_has_next_period;
628 gstadaptivedemux_class->advance_period = gst_dash_demux_advance_period;
629 gstadaptivedemux_class->stream_has_next_fragment =
630 gst_dash_demux_stream_has_next_fragment;
631 gstadaptivedemux_class->stream_advance_fragment =
632 gst_dash_demux_stream_advance_fragment;
633 gstadaptivedemux_class->stream_get_fragment_waiting_time =
634 gst_dash_demux_stream_get_fragment_waiting_time;
635 gstadaptivedemux_class->stream_seek = gst_dash_demux_stream_seek;
636 gstadaptivedemux_class->stream_select_bitrate =
637 gst_dash_demux_stream_select_bitrate;
638 gstadaptivedemux_class->stream_update_fragment_info =
639 gst_dash_demux_stream_update_fragment_info;
640 gstadaptivedemux_class->stream_free = gst_dash_demux_stream_free;
641 gstadaptivedemux_class->get_live_seek_range =
642 gst_dash_demux_get_live_seek_range;
643 gstadaptivedemux_class->get_presentation_offset =
644 gst_dash_demux_get_presentation_offset;
645 gstadaptivedemux_class->get_period_start_time =
646 gst_dash_demux_get_period_start_time;
647
648 gstadaptivedemux_class->start_fragment = gst_dash_demux_stream_fragment_start;
649 gstadaptivedemux_class->finish_fragment =
650 gst_dash_demux_stream_fragment_finished;
651 gstadaptivedemux_class->data_received = gst_dash_demux_data_received;
652 gstadaptivedemux_class->need_another_chunk =
653 gst_dash_demux_need_another_chunk;
654 }
655
656 static void
gst_dash_demux_init(GstDashDemux * demux)657 gst_dash_demux_init (GstDashDemux * demux)
658 {
659 /* Properties */
660 demux->max_buffering_time = DEFAULT_MAX_BUFFERING_TIME * GST_SECOND;
661 demux->max_bitrate = DEFAULT_MAX_BITRATE;
662 demux->max_video_width = DEFAULT_MAX_VIDEO_WIDTH;
663 demux->max_video_height = DEFAULT_MAX_VIDEO_HEIGHT;
664 demux->max_video_framerate_n = DEFAULT_MAX_VIDEO_FRAMERATE_N;
665 demux->max_video_framerate_d = DEFAULT_MAX_VIDEO_FRAMERATE_D;
666 demux->default_presentation_delay = g_strdup (DEFAULT_PRESENTATION_DELAY);
667
668 g_mutex_init (&demux->client_lock);
669
670 gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
671 sizeof (GstDashDemuxStream));
672 }
673
674 static void
gst_dash_demux_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)675 gst_dash_demux_set_property (GObject * object, guint prop_id,
676 const GValue * value, GParamSpec * pspec)
677 {
678 GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
679 GstDashDemux *demux = GST_DASH_DEMUX (object);
680
681 switch (prop_id) {
682 case PROP_MAX_BUFFERING_TIME:
683 demux->max_buffering_time = g_value_get_uint (value) * GST_SECOND;
684 break;
685 case PROP_BANDWIDTH_USAGE:
686 adaptivedemux->bitrate_limit = g_value_get_float (value);
687 break;
688 case PROP_MAX_BITRATE:
689 demux->max_bitrate = g_value_get_uint (value);
690 break;
691 case PROP_MAX_VIDEO_WIDTH:
692 demux->max_video_width = g_value_get_uint (value);
693 break;
694 case PROP_MAX_VIDEO_HEIGHT:
695 demux->max_video_height = g_value_get_uint (value);
696 break;
697 case PROP_MAX_VIDEO_FRAMERATE:
698 demux->max_video_framerate_n = gst_value_get_fraction_numerator (value);
699 demux->max_video_framerate_d = gst_value_get_fraction_denominator (value);
700 break;
701 case PROP_PRESENTATION_DELAY:
702 g_free (demux->default_presentation_delay);
703 demux->default_presentation_delay = g_value_dup_string (value);
704 break;
705 default:
706 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
707 break;
708 }
709 }
710
711 static void
gst_dash_demux_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)712 gst_dash_demux_get_property (GObject * object, guint prop_id, GValue * value,
713 GParamSpec * pspec)
714 {
715 GstAdaptiveDemux *adaptivedemux = GST_ADAPTIVE_DEMUX_CAST (object);
716 GstDashDemux *demux = GST_DASH_DEMUX (object);
717
718 switch (prop_id) {
719 case PROP_MAX_BUFFERING_TIME:
720 g_value_set_uint (value, demux->max_buffering_time / GST_SECOND);
721 break;
722 case PROP_BANDWIDTH_USAGE:
723 g_value_set_float (value, adaptivedemux->bitrate_limit);
724 break;
725 case PROP_MAX_BITRATE:
726 g_value_set_uint (value, demux->max_bitrate);
727 break;
728 case PROP_MAX_VIDEO_WIDTH:
729 g_value_set_uint (value, demux->max_video_width);
730 break;
731 case PROP_MAX_VIDEO_HEIGHT:
732 g_value_set_uint (value, demux->max_video_height);
733 break;
734 case PROP_MAX_VIDEO_FRAMERATE:
735 gst_value_set_fraction (value, demux->max_video_framerate_n,
736 demux->max_video_framerate_d);
737 break;
738 case PROP_PRESENTATION_DELAY:
739 if (demux->default_presentation_delay == NULL)
740 g_value_set_static_string (value, "");
741 else
742 g_value_set_string (value, demux->default_presentation_delay);
743 break;
744 default:
745 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
746 break;
747 }
748 }
749
750 static gboolean
gst_dash_demux_setup_mpdparser_streams(GstDashDemux * demux,GstMpdClient * client)751 gst_dash_demux_setup_mpdparser_streams (GstDashDemux * demux,
752 GstMpdClient * client)
753 {
754 gboolean has_streams = FALSE;
755 GList *adapt_sets, *iter;
756
757 adapt_sets = gst_mpd_client_get_adaptation_sets (client);
758 for (iter = adapt_sets; iter; iter = g_list_next (iter)) {
759 GstAdaptationSetNode *adapt_set_node = iter->data;
760
761 gst_mpd_client_setup_streaming (client, adapt_set_node);
762 has_streams = TRUE;
763 }
764
765 if (!has_streams) {
766 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, ("Manifest has no playable "
767 "streams"), ("No streams could be activated from the manifest"));
768 }
769 return has_streams;
770 }
771
772 static gboolean
gst_dash_demux_setup_all_streams(GstDashDemux * demux)773 gst_dash_demux_setup_all_streams (GstDashDemux * demux)
774 {
775 guint i;
776
777 GST_DEBUG_OBJECT (demux, "Setting up streams for period %d",
778 gst_mpd_client_get_period_index (demux->client));
779
780 /* clean old active stream list, if any */
781 gst_active_streams_free (demux->client);
782
783 if (!gst_dash_demux_setup_mpdparser_streams (demux, demux->client)) {
784 return FALSE;
785 }
786
787 GST_DEBUG_OBJECT (demux, "Creating stream objects");
788 for (i = 0; i < gst_mpdparser_get_nb_active_stream (demux->client); i++) {
789 GstDashDemuxStream *stream;
790 GstActiveStream *active_stream;
791 GstCaps *caps;
792 GstStructure *s;
793 GstPad *srcpad;
794 gchar *lang = NULL;
795 GstTagList *tags = NULL;
796
797 active_stream = gst_mpdparser_get_active_stream_by_index (demux->client, i);
798 if (active_stream == NULL)
799 continue;
800
801 if (demux->trickmode_no_audio
802 && active_stream->mimeType == GST_STREAM_AUDIO) {
803 GST_DEBUG_OBJECT (demux,
804 "Skipping audio stream %d because of TRICKMODE_NO_AUDIO flag", i);
805 continue;
806 }
807
808 srcpad = gst_dash_demux_create_pad (demux, active_stream);
809 if (srcpad == NULL)
810 continue;
811
812 caps = gst_dash_demux_get_input_caps (demux, active_stream);
813 GST_LOG_OBJECT (demux, "Creating stream %d %" GST_PTR_FORMAT, i, caps);
814
815 if (active_stream->cur_adapt_set) {
816 GstAdaptationSetNode *adp_set = active_stream->cur_adapt_set;
817 lang = adp_set->lang;
818
819 /* Fallback to the language in ContentComponent node */
820 if (lang == NULL) {
821 GList *it;
822
823 for (it = adp_set->ContentComponents; it; it = it->next) {
824 GstContentComponentNode *cc_node = it->data;
825 if (cc_node->lang) {
826 lang = cc_node->lang;
827 break;
828 }
829 }
830 }
831 }
832
833 if (lang) {
834 if (gst_tag_check_language_code (lang))
835 tags = gst_tag_list_new (GST_TAG_LANGUAGE_CODE, lang, NULL);
836 else
837 tags = gst_tag_list_new (GST_TAG_LANGUAGE_NAME, lang, NULL);
838 }
839
840 stream = (GstDashDemuxStream *)
841 gst_adaptive_demux_stream_new (GST_ADAPTIVE_DEMUX_CAST (demux), srcpad);
842 stream->active_stream = active_stream;
843 s = gst_caps_get_structure (caps, 0);
844 stream->allow_sidx =
845 gst_mpd_client_has_isoff_ondemand_profile (demux->client);
846 stream->is_isobmff = gst_structure_has_name (s, "video/quicktime")
847 || gst_structure_has_name (s, "audio/x-m4a");
848 stream->first_sync_sample_always_after_moof = TRUE;
849 stream->adapter = gst_adapter_new ();
850 gst_adaptive_demux_stream_set_caps (GST_ADAPTIVE_DEMUX_STREAM_CAST (stream),
851 caps);
852 if (tags)
853 gst_adaptive_demux_stream_set_tags (GST_ADAPTIVE_DEMUX_STREAM_CAST
854 (stream), tags);
855 stream->index = i;
856 stream->pending_seek_ts = GST_CLOCK_TIME_NONE;
857 stream->sidx_position = GST_CLOCK_TIME_NONE;
858 stream->actual_position = GST_CLOCK_TIME_NONE;
859 stream->target_time = GST_CLOCK_TIME_NONE;
860 /* Set a default average keyframe download time of a quarter of a second */
861 stream->average_download_time = 250 * GST_MSECOND;
862 if (active_stream->cur_adapt_set &&
863 active_stream->cur_adapt_set->RepresentationBase &&
864 active_stream->cur_adapt_set->RepresentationBase->ContentProtection) {
865 GST_DEBUG_OBJECT (demux, "Adding ContentProtection events to source pad");
866 g_list_foreach (active_stream->cur_adapt_set->RepresentationBase->
867 ContentProtection, gst_dash_demux_send_content_protection_event,
868 stream);
869 }
870
871 gst_isoff_sidx_parser_init (&stream->sidx_parser);
872 }
873
874 return TRUE;
875 }
876
877 static void
gst_dash_demux_send_content_protection_event(gpointer data,gpointer userdata)878 gst_dash_demux_send_content_protection_event (gpointer data, gpointer userdata)
879 {
880 GstDescriptorType *cp = (GstDescriptorType *) data;
881 GstDashDemuxStream *stream = (GstDashDemuxStream *) userdata;
882 GstEvent *event;
883 GstBuffer *pssi;
884 glong pssi_len;
885 gchar *schemeIdUri;
886
887 if (cp->schemeIdUri == NULL)
888 return;
889
890 GST_TRACE_OBJECT (stream, "check schemeIdUri %s", cp->schemeIdUri);
891 /* RFC 2141 states: The leading "urn:" sequence is case-insensitive */
892 schemeIdUri = g_ascii_strdown (cp->schemeIdUri, -1);
893 if (g_str_has_prefix (schemeIdUri, "urn:uuid:")) {
894 pssi_len = strlen (cp->value);
895 pssi = gst_buffer_new_wrapped (g_memdup (cp->value, pssi_len), pssi_len);
896 GST_LOG_OBJECT (stream, "Queuing Protection event on source pad");
897 /* RFC 4122 states that the hex part of a UUID is in lower case,
898 * but some streams seem to ignore this and use upper case for the
899 * protection system ID */
900 event = gst_event_new_protection (cp->schemeIdUri + 9, pssi, "dash/mpd");
901 gst_adaptive_demux_stream_queue_event ((GstAdaptiveDemuxStream *) stream,
902 event);
903 gst_buffer_unref (pssi);
904 }
905 g_free (schemeIdUri);
906 }
907
908 static GstClockTime
gst_dash_demux_get_duration(GstAdaptiveDemux * ademux)909 gst_dash_demux_get_duration (GstAdaptiveDemux * ademux)
910 {
911 GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
912
913 g_return_val_if_fail (demux->client != NULL, GST_CLOCK_TIME_NONE);
914
915 return gst_mpd_client_get_media_presentation_duration (demux->client);
916 }
917
918 static gboolean
gst_dash_demux_is_live(GstAdaptiveDemux * ademux)919 gst_dash_demux_is_live (GstAdaptiveDemux * ademux)
920 {
921 GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
922
923 g_return_val_if_fail (demux->client != NULL, FALSE);
924
925 return gst_mpd_client_is_live (demux->client);
926 }
927
928 static gboolean
gst_dash_demux_setup_streams(GstAdaptiveDemux * demux)929 gst_dash_demux_setup_streams (GstAdaptiveDemux * demux)
930 {
931 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
932 gboolean ret = TRUE;
933 GstDateTime *now = NULL;
934 guint period_idx;
935
936 /* setup video, audio and subtitle streams, starting from first Period if
937 * non-live */
938 period_idx = 0;
939 if (gst_mpd_client_is_live (dashdemux->client)) {
940 GDateTime *g_now;
941 if (dashdemux->client->mpd_node->availabilityStartTime == NULL) {
942 ret = FALSE;
943 GST_ERROR_OBJECT (demux, "MPD does not have availabilityStartTime");
944 goto done;
945 }
946 if (dashdemux->clock_drift == NULL) {
947 gchar **urls;
948 urls =
949 gst_mpd_client_get_utc_timing_sources (dashdemux->client,
950 SUPPORTED_CLOCK_FORMATS, NULL);
951 if (urls) {
952 GST_DEBUG_OBJECT (dashdemux, "Found a supported UTCTiming element");
953 dashdemux->clock_drift = gst_dash_demux_clock_drift_new (dashdemux);
954 gst_dash_demux_poll_clock_drift (dashdemux);
955 }
956 }
957 /* get period index for period encompassing the current time */
958 g_now = gst_dash_demux_get_server_now_utc (dashdemux);
959 now = gst_date_time_new_from_g_date_time (g_now);
960 if (dashdemux->client->mpd_node->suggestedPresentationDelay != -1) {
961 GstDateTime *target = gst_mpd_client_add_time_difference (now,
962 dashdemux->client->mpd_node->suggestedPresentationDelay * -1000);
963 gst_date_time_unref (now);
964 now = target;
965 } else if (dashdemux->default_presentation_delay) {
966 gint64 dfp =
967 gst_mpd_client_parse_default_presentation_delay (dashdemux->client,
968 dashdemux->default_presentation_delay);
969 GstDateTime *target = gst_mpd_client_add_time_difference (now,
970 dfp * -1000);
971 gst_date_time_unref (now);
972 now = target;
973 }
974 period_idx =
975 gst_mpd_client_get_period_index_at_time (dashdemux->client, now);
976 if (period_idx == G_MAXUINT) {
977 #ifndef GST_DISABLE_GST_DEBUG
978 gchar *date_str = gst_date_time_to_iso8601_string (now);
979 GST_DEBUG_OBJECT (demux, "Unable to find live period active at %s",
980 date_str);
981 g_free (date_str);
982 #endif
983 ret = FALSE;
984 goto done;
985 }
986 }
987
988 if (!gst_mpd_client_set_period_index (dashdemux->client, period_idx) ||
989 !gst_dash_demux_setup_all_streams (dashdemux)) {
990 ret = FALSE;
991 goto done;
992 }
993
994 /* If stream is live, try to find the segment that
995 * is closest to current time */
996 if (gst_mpd_client_is_live (dashdemux->client)) {
997 GDateTime *gnow;
998
999 GST_DEBUG_OBJECT (demux, "Seeking to current time of day for live stream ");
1000
1001 gnow = gst_date_time_to_g_date_time (now);
1002 gst_mpd_client_seek_to_time (dashdemux->client, gnow);
1003 g_date_time_unref (gnow);
1004 } else {
1005 GST_DEBUG_OBJECT (demux, "Seeking to first segment for on-demand stream ");
1006
1007 /* start playing from the first segment */
1008 gst_mpd_client_seek_to_first_segment (dashdemux->client);
1009 }
1010
1011 done:
1012 if (now != NULL)
1013 gst_date_time_unref (now);
1014 return ret;
1015 }
1016
1017 static gboolean
gst_dash_demux_process_manifest(GstAdaptiveDemux * demux,GstBuffer * buf)1018 gst_dash_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
1019 {
1020 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
1021 gboolean ret = FALSE;
1022 gchar *manifest;
1023 GstMapInfo mapinfo;
1024
1025 if (dashdemux->client)
1026 gst_mpd_client_free (dashdemux->client);
1027 dashdemux->client = gst_mpd_client_new ();
1028 gst_mpd_client_set_uri_downloader (dashdemux->client, demux->downloader);
1029
1030 dashdemux->client->mpd_uri = g_strdup (demux->manifest_uri);
1031 dashdemux->client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
1032
1033 GST_DEBUG_OBJECT (demux, "Fetched MPD file at URI: %s (base: %s)",
1034 dashdemux->client->mpd_uri,
1035 GST_STR_NULL (dashdemux->client->mpd_base_uri));
1036
1037 if (gst_buffer_map (buf, &mapinfo, GST_MAP_READ)) {
1038 manifest = (gchar *) mapinfo.data;
1039 if (gst_mpd_parse (dashdemux->client, manifest, mapinfo.size)) {
1040 if (gst_mpd_client_setup_media_presentation (dashdemux->client, 0, 0,
1041 NULL)) {
1042 ret = TRUE;
1043 } else {
1044 GST_ELEMENT_ERROR (demux, STREAM, DECODE,
1045 ("Incompatible manifest file."), (NULL));
1046 }
1047 }
1048 gst_buffer_unmap (buf, &mapinfo);
1049 } else {
1050 GST_WARNING_OBJECT (demux, "Failed to map manifest buffer");
1051 }
1052
1053 if (ret)
1054 ret = gst_dash_demux_setup_streams (demux);
1055
1056 return ret;
1057 }
1058
1059 static GstPad *
gst_dash_demux_create_pad(GstDashDemux * demux,GstActiveStream * stream)1060 gst_dash_demux_create_pad (GstDashDemux * demux, GstActiveStream * stream)
1061 {
1062 GstPad *pad;
1063 GstPadTemplate *tmpl;
1064 gchar *name;
1065
1066 switch (stream->mimeType) {
1067 case GST_STREAM_AUDIO:
1068 name = g_strdup_printf ("audio_%02u", demux->n_audio_streams++);
1069 tmpl = gst_static_pad_template_get (&gst_dash_demux_audiosrc_template);
1070 break;
1071 case GST_STREAM_VIDEO:
1072 name = g_strdup_printf ("video_%02u", demux->n_video_streams++);
1073 tmpl = gst_static_pad_template_get (&gst_dash_demux_videosrc_template);
1074 break;
1075 case GST_STREAM_APPLICATION:
1076 if (gst_mpd_client_active_stream_contains_subtitles (stream)) {
1077 name = g_strdup_printf ("subtitle_%02u", demux->n_subtitle_streams++);
1078 tmpl =
1079 gst_static_pad_template_get (&gst_dash_demux_subtitlesrc_template);
1080 } else {
1081 return NULL;
1082 }
1083 break;
1084 default:
1085 g_assert_not_reached ();
1086 return NULL;
1087 }
1088
1089 /* Create and activate new pads */
1090 pad = gst_pad_new_from_template (tmpl, name);
1091 g_free (name);
1092 gst_object_unref (tmpl);
1093
1094 gst_pad_set_active (pad, TRUE);
1095 GST_INFO_OBJECT (demux, "Creating srcpad %s:%s", GST_DEBUG_PAD_NAME (pad));
1096 return pad;
1097 }
1098
1099 static void
gst_dash_demux_reset(GstAdaptiveDemux * ademux)1100 gst_dash_demux_reset (GstAdaptiveDemux * ademux)
1101 {
1102 GstDashDemux *demux = GST_DASH_DEMUX_CAST (ademux);
1103
1104 GST_DEBUG_OBJECT (demux, "Resetting demux");
1105
1106 demux->end_of_period = FALSE;
1107 demux->end_of_manifest = FALSE;
1108
1109 if (demux->client) {
1110 gst_mpd_client_free (demux->client);
1111 demux->client = NULL;
1112 }
1113 gst_dash_demux_clock_drift_free (demux->clock_drift);
1114 demux->clock_drift = NULL;
1115 demux->client = gst_mpd_client_new ();
1116 gst_mpd_client_set_uri_downloader (demux->client, ademux->downloader);
1117
1118 demux->n_audio_streams = 0;
1119 demux->n_video_streams = 0;
1120 demux->n_subtitle_streams = 0;
1121
1122 demux->trickmode_no_audio = FALSE;
1123 demux->allow_trickmode_key_units = TRUE;
1124 }
1125
1126 static GstCaps *
gst_dash_demux_get_video_input_caps(GstDashDemux * demux,GstActiveStream * stream)1127 gst_dash_demux_get_video_input_caps (GstDashDemux * demux,
1128 GstActiveStream * stream)
1129 {
1130 guint width = 0, height = 0;
1131 gint fps_num = 0, fps_den = 1;
1132 gboolean have_fps = FALSE;
1133 GstCaps *caps = NULL;
1134
1135 if (stream == NULL)
1136 return NULL;
1137
1138 /* if bitstreamSwitching is true we dont need to swich pads on resolution change */
1139 if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
1140 width = gst_mpd_client_get_video_stream_width (stream);
1141 height = gst_mpd_client_get_video_stream_height (stream);
1142 have_fps =
1143 gst_mpd_client_get_video_stream_framerate (stream, &fps_num, &fps_den);
1144 }
1145 caps = gst_mpd_client_get_stream_caps (stream);
1146 if (caps == NULL)
1147 return NULL;
1148
1149 if (width > 0 && height > 0) {
1150 gst_caps_set_simple (caps, "width", G_TYPE_INT, width, "height",
1151 G_TYPE_INT, height, NULL);
1152 }
1153
1154 if (have_fps) {
1155 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fps_num,
1156 fps_den, NULL);
1157 }
1158
1159 return caps;
1160 }
1161
1162 static GstCaps *
gst_dash_demux_get_audio_input_caps(GstDashDemux * demux,GstActiveStream * stream)1163 gst_dash_demux_get_audio_input_caps (GstDashDemux * demux,
1164 GstActiveStream * stream)
1165 {
1166 guint rate = 0, channels = 0;
1167 GstCaps *caps = NULL;
1168
1169 if (stream == NULL)
1170 return NULL;
1171
1172 /* if bitstreamSwitching is true we dont need to swich pads on rate/channels change */
1173 if (!gst_mpd_client_get_bitstream_switching_flag (stream)) {
1174 channels = gst_mpd_client_get_audio_stream_num_channels (stream);
1175 rate = gst_mpd_client_get_audio_stream_rate (stream);
1176 }
1177 caps = gst_mpd_client_get_stream_caps (stream);
1178 if (caps == NULL)
1179 return NULL;
1180
1181 if (rate > 0) {
1182 gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
1183 }
1184 if (channels > 0) {
1185 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
1186 }
1187
1188 return caps;
1189 }
1190
1191 static GstCaps *
gst_dash_demux_get_application_input_caps(GstDashDemux * demux,GstActiveStream * stream)1192 gst_dash_demux_get_application_input_caps (GstDashDemux * demux,
1193 GstActiveStream * stream)
1194 {
1195 GstCaps *caps = NULL;
1196
1197 if (stream == NULL)
1198 return NULL;
1199
1200 caps = gst_mpd_client_get_stream_caps (stream);
1201 if (caps == NULL)
1202 return NULL;
1203
1204 return caps;
1205 }
1206
1207 static GstCaps *
gst_dash_demux_get_input_caps(GstDashDemux * demux,GstActiveStream * stream)1208 gst_dash_demux_get_input_caps (GstDashDemux * demux, GstActiveStream * stream)
1209 {
1210 switch (stream->mimeType) {
1211 case GST_STREAM_VIDEO:
1212 return gst_dash_demux_get_video_input_caps (demux, stream);
1213 case GST_STREAM_AUDIO:
1214 return gst_dash_demux_get_audio_input_caps (demux, stream);
1215 case GST_STREAM_APPLICATION:
1216 return gst_dash_demux_get_application_input_caps (demux, stream);
1217 default:
1218 return GST_CAPS_NONE;
1219 }
1220 }
1221
1222 static void
gst_dash_demux_stream_update_headers_info(GstAdaptiveDemuxStream * stream)1223 gst_dash_demux_stream_update_headers_info (GstAdaptiveDemuxStream * stream)
1224 {
1225 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1226 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1227 gchar *path = NULL;
1228
1229 gst_mpd_client_get_next_header (dashdemux->client,
1230 &path, dashstream->index,
1231 &stream->fragment.header_range_start, &stream->fragment.header_range_end);
1232
1233 if (path != NULL) {
1234 stream->fragment.header_uri =
1235 gst_uri_join_strings (gst_mpdparser_get_baseURL (dashdemux->client,
1236 dashstream->index), path);
1237 g_free (path);
1238 path = NULL;
1239 }
1240
1241 gst_mpd_client_get_next_header_index (dashdemux->client,
1242 &path, dashstream->index,
1243 &stream->fragment.index_range_start, &stream->fragment.index_range_end);
1244
1245 if (path != NULL) {
1246 stream->fragment.index_uri =
1247 gst_uri_join_strings (gst_mpdparser_get_baseURL (dashdemux->client,
1248 dashstream->index), path);
1249 g_free (path);
1250 }
1251 }
1252
1253 static GstFlowReturn
gst_dash_demux_stream_update_fragment_info(GstAdaptiveDemuxStream * stream)1254 gst_dash_demux_stream_update_fragment_info (GstAdaptiveDemuxStream * stream)
1255 {
1256 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1257 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1258 GstClockTime ts;
1259 GstMediaFragmentInfo fragment;
1260 gboolean isombff;
1261
1262 gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
1263
1264 isombff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1265
1266 /* Reset chunk size if any */
1267 stream->fragment.chunk_size = 0;
1268 dashstream->current_fragment_keyframe_distance = GST_CLOCK_TIME_NONE;
1269
1270 if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream) && isombff) {
1271 gst_dash_demux_stream_update_headers_info (stream);
1272 /* sidx entries may not be available in here */
1273 if (stream->fragment.index_uri
1274 && dashstream->sidx_position != GST_CLOCK_TIME_NONE) {
1275 /* request only the index to be downloaded as we need to reposition the
1276 * stream to a subsegment */
1277 return GST_FLOW_OK;
1278 }
1279 }
1280
1281 if (dashstream->moof_sync_samples
1282 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
1283 GstDashStreamSyncSample *sync_sample =
1284 &g_array_index (dashstream->moof_sync_samples, GstDashStreamSyncSample,
1285 dashstream->current_sync_sample);
1286
1287 gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1288 &fragment);
1289
1290 if (isombff && dashstream->sidx_position != GST_CLOCK_TIME_NONE
1291 && SIDX (dashstream)->entries) {
1292 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
1293 dashstream->current_fragment_timestamp = fragment.timestamp = entry->pts;
1294 dashstream->current_fragment_duration = fragment.duration =
1295 entry->duration;
1296 } else {
1297 dashstream->current_fragment_timestamp = fragment.timestamp;
1298 dashstream->current_fragment_duration = fragment.duration;
1299 }
1300
1301 dashstream->current_fragment_keyframe_distance =
1302 fragment.duration / dashstream->moof_sync_samples->len;
1303 dashstream->actual_position =
1304 fragment.timestamp +
1305 dashstream->current_sync_sample *
1306 dashstream->current_fragment_keyframe_distance;
1307 if (stream->segment.rate < 0.0)
1308 dashstream->actual_position +=
1309 dashstream->current_fragment_keyframe_distance;
1310 dashstream->actual_position =
1311 MIN (dashstream->actual_position,
1312 fragment.timestamp + fragment.duration);
1313
1314 stream->fragment.uri = fragment.uri;
1315 stream->fragment.timestamp = GST_CLOCK_TIME_NONE;
1316 stream->fragment.duration = GST_CLOCK_TIME_NONE;
1317 stream->fragment.range_start = sync_sample->start_offset;
1318 stream->fragment.range_end = sync_sample->end_offset;
1319
1320 GST_DEBUG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
1321 GST_TIME_ARGS (dashstream->actual_position));
1322
1323 return GST_FLOW_OK;
1324 }
1325
1326 if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
1327 dashstream->index, &ts)) {
1328 if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream)) {
1329 gst_adaptive_demux_stream_fragment_clear (&stream->fragment);
1330 gst_dash_demux_stream_update_headers_info (stream);
1331 }
1332
1333 gst_mpd_client_get_next_fragment (dashdemux->client, dashstream->index,
1334 &fragment);
1335
1336 stream->fragment.uri = fragment.uri;
1337 /* If mpd does not specify indexRange (i.e., null index_uri),
1338 * sidx entries may not be available until download it */
1339 if (isombff && dashstream->sidx_position != GST_CLOCK_TIME_NONE
1340 && SIDX (dashstream)->entries) {
1341 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
1342 stream->fragment.range_start =
1343 dashstream->sidx_base_offset + entry->offset;
1344 dashstream->actual_position = stream->fragment.timestamp = entry->pts;
1345 dashstream->current_fragment_timestamp = stream->fragment.timestamp =
1346 entry->pts;
1347 dashstream->current_fragment_duration = stream->fragment.duration =
1348 entry->duration;
1349 if (stream->demux->segment.rate < 0.0) {
1350 stream->fragment.range_end =
1351 stream->fragment.range_start + entry->size - 1;
1352 dashstream->actual_position += entry->duration;
1353 } else {
1354 stream->fragment.range_end = fragment.range_end;
1355 }
1356 } else {
1357 dashstream->actual_position = stream->fragment.timestamp =
1358 fragment.timestamp;
1359 dashstream->current_fragment_timestamp = fragment.timestamp;
1360 dashstream->current_fragment_duration = stream->fragment.duration =
1361 fragment.duration;
1362 if (stream->demux->segment.rate < 0.0)
1363 dashstream->actual_position += fragment.duration;
1364 stream->fragment.range_start =
1365 MAX (fragment.range_start, dashstream->sidx_base_offset);
1366 stream->fragment.range_end = fragment.range_end;
1367 }
1368
1369 GST_DEBUG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
1370 GST_TIME_ARGS (dashstream->actual_position));
1371
1372 return GST_FLOW_OK;
1373 }
1374
1375 return GST_FLOW_EOS;
1376 }
1377
1378 static gint
gst_dash_demux_index_entry_search(GstSidxBoxEntry * entry,GstClockTime * ts,gpointer user_data)1379 gst_dash_demux_index_entry_search (GstSidxBoxEntry * entry, GstClockTime * ts,
1380 gpointer user_data)
1381 {
1382 GstClockTime entry_ts = entry->pts + entry->duration;
1383 if (entry_ts <= *ts)
1384 return -1;
1385 else if (entry->pts > *ts)
1386 return 1;
1387 else
1388 return 0;
1389 }
1390
1391 static GstFlowReturn
gst_dash_demux_stream_sidx_seek(GstDashDemuxStream * dashstream,gboolean forward,GstSeekFlags flags,GstClockTime ts,GstClockTime * final_ts)1392 gst_dash_demux_stream_sidx_seek (GstDashDemuxStream * dashstream,
1393 gboolean forward, GstSeekFlags flags, GstClockTime ts,
1394 GstClockTime * final_ts)
1395 {
1396 GstSidxBox *sidx = SIDX (dashstream);
1397 GstSidxBoxEntry *entry;
1398 gint idx = sidx->entries_count;
1399 GstFlowReturn ret = GST_FLOW_OK;
1400
1401 if (sidx->entries_count == 0)
1402 return GST_FLOW_EOS;
1403
1404 entry =
1405 gst_util_array_binary_search (sidx->entries, sidx->entries_count,
1406 sizeof (GstSidxBoxEntry),
1407 (GCompareDataFunc) gst_dash_demux_index_entry_search,
1408 GST_SEARCH_MODE_EXACT, &ts, NULL);
1409
1410 /* No exact match found, nothing in our index
1411 * This is usually a bug or broken stream, as the seeking code already
1412 * makes sure that we're in the correct period and segment, and only need
1413 * to find the correct place inside the segment. Allow for some rounding
1414 * errors and inaccuracies here though */
1415 if (!entry) {
1416 GstSidxBoxEntry *last_entry = &sidx->entries[sidx->entries_count - 1];
1417
1418 GST_WARNING_OBJECT (dashstream->parent.pad, "Couldn't find SIDX entry");
1419
1420 if (ts < sidx->entries[0].pts
1421 && ts + 250 * GST_MSECOND >= sidx->entries[0].pts)
1422 entry = &sidx->entries[0];
1423 else if (ts >= last_entry->pts + last_entry->duration &&
1424 ts < last_entry->pts + last_entry->duration + 250 * GST_MSECOND)
1425 entry = last_entry;
1426 }
1427 if (!entry)
1428 return GST_FLOW_EOS;
1429
1430 idx = entry - sidx->entries;
1431
1432 /* FIXME in reverse mode, if we are exactly at a fragment start it makes more
1433 * sense to start from the end of the previous fragment */
1434 if (!forward && idx > 0 && entry->pts == ts) {
1435 idx--;
1436 entry = &sidx->entries[idx];
1437 }
1438
1439 /* Now entry->pts <= ts < entry->pts + entry->duration, need to adjust for
1440 * snapping */
1441 if ((flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST) {
1442 if (idx + 1 < sidx->entries_count
1443 && sidx->entries[idx + 1].pts - ts < ts - sidx->entries[idx].pts)
1444 idx += 1;
1445 } else if ((forward && (flags & GST_SEEK_FLAG_SNAP_AFTER)) || (!forward
1446 && (flags & GST_SEEK_FLAG_SNAP_BEFORE))) {
1447 if (idx + 1 < sidx->entries_count && entry->pts < ts)
1448 idx += 1;
1449 }
1450
1451 g_assert (sidx->entry_index < sidx->entries_count);
1452
1453 sidx->entry_index = idx;
1454 dashstream->sidx_position = sidx->entries[idx].pts;
1455
1456 if (final_ts)
1457 *final_ts = dashstream->sidx_position;
1458
1459 return ret;
1460 }
1461
1462 static GstFlowReturn
gst_dash_demux_stream_seek(GstAdaptiveDemuxStream * stream,gboolean forward,GstSeekFlags flags,GstClockTime ts,GstClockTime * final_ts)1463 gst_dash_demux_stream_seek (GstAdaptiveDemuxStream * stream, gboolean forward,
1464 GstSeekFlags flags, GstClockTime ts, GstClockTime * final_ts)
1465 {
1466 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1467 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1468 gint last_index, last_repeat;
1469 gboolean is_isobmff;
1470
1471 last_index = dashstream->active_stream->segment_index;
1472 last_repeat = dashstream->active_stream->segment_repeat_index;
1473
1474 if (dashstream->adapter)
1475 gst_adapter_clear (dashstream->adapter);
1476 dashstream->current_offset = -1;
1477 dashstream->current_index_header_or_data = 0;
1478
1479 dashstream->isobmff_parser.current_fourcc = 0;
1480 dashstream->isobmff_parser.current_start_offset = 0;
1481 dashstream->isobmff_parser.current_size = 0;
1482
1483 if (dashstream->moof)
1484 gst_isoff_moof_box_free (dashstream->moof);
1485 dashstream->moof = NULL;
1486 if (dashstream->moof_sync_samples)
1487 g_array_free (dashstream->moof_sync_samples, TRUE);
1488 dashstream->moof_sync_samples = NULL;
1489 dashstream->current_sync_sample = -1;
1490 dashstream->target_time = GST_CLOCK_TIME_NONE;
1491
1492 is_isobmff = gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client);
1493
1494 if (!gst_mpd_client_stream_seek (dashdemux->client, dashstream->active_stream,
1495 forward,
1496 is_isobmff ? (flags & (~(GST_SEEK_FLAG_SNAP_BEFORE |
1497 GST_SEEK_FLAG_SNAP_AFTER))) : flags, ts, final_ts)) {
1498 return GST_FLOW_EOS;
1499 }
1500
1501 if (is_isobmff) {
1502 GstClockTime period_start, offset;
1503
1504 period_start = gst_mpd_parser_get_period_start_time (dashdemux->client);
1505 offset =
1506 gst_mpd_parser_get_stream_presentation_offset (dashdemux->client,
1507 dashstream->index);
1508
1509 if (G_UNLIKELY (ts < period_start))
1510 ts = offset;
1511 else
1512 ts += offset - period_start;
1513
1514 if (last_index != dashstream->active_stream->segment_index ||
1515 last_repeat != dashstream->active_stream->segment_repeat_index) {
1516 GST_LOG_OBJECT (stream->pad,
1517 "Segment index was changed, reset sidx parser");
1518 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1519 dashstream->sidx_base_offset = 0;
1520 dashstream->allow_sidx = TRUE;
1521 }
1522
1523 if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1524 if (gst_dash_demux_stream_sidx_seek (dashstream, forward, flags, ts,
1525 final_ts) != GST_FLOW_OK) {
1526 GST_ERROR_OBJECT (stream->pad, "Couldn't find position in sidx");
1527 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1528 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
1529 }
1530 dashstream->pending_seek_ts = GST_CLOCK_TIME_NONE;
1531 } else {
1532 /* no index yet, seek when we have it */
1533 /* FIXME - the final_ts won't be correct here */
1534 dashstream->pending_seek_ts = ts;
1535 }
1536 }
1537
1538 stream->discont = TRUE;
1539
1540 return GST_FLOW_OK;
1541 }
1542
1543 static gboolean
gst_dash_demux_stream_has_next_sync_sample(GstAdaptiveDemuxStream * stream)1544 gst_dash_demux_stream_has_next_sync_sample (GstAdaptiveDemuxStream * stream)
1545 {
1546 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1547
1548 if (dashstream->moof_sync_samples &&
1549 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)) {
1550 if (stream->demux->segment.rate > 0.0) {
1551 if (dashstream->current_sync_sample + 1 <
1552 dashstream->moof_sync_samples->len)
1553 return TRUE;
1554 } else {
1555 if (dashstream->current_sync_sample >= 1)
1556 return TRUE;
1557 }
1558 }
1559 return FALSE;
1560 }
1561
1562 static gboolean
gst_dash_demux_stream_has_next_subfragment(GstAdaptiveDemuxStream * stream)1563 gst_dash_demux_stream_has_next_subfragment (GstAdaptiveDemuxStream * stream)
1564 {
1565 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1566 GstSidxBox *sidx = SIDX (dashstream);
1567
1568 if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1569 if (stream->demux->segment.rate > 0.0) {
1570 if (sidx->entry_index + 1 < sidx->entries_count)
1571 return TRUE;
1572 } else {
1573 if (sidx->entry_index >= 1)
1574 return TRUE;
1575 }
1576 }
1577 return FALSE;
1578 }
1579
1580 static gboolean
gst_dash_demux_stream_advance_sync_sample(GstAdaptiveDemuxStream * stream,GstClockTime target_time)1581 gst_dash_demux_stream_advance_sync_sample (GstAdaptiveDemuxStream * stream,
1582 GstClockTime target_time)
1583 {
1584 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1585 gboolean fragment_finished = FALSE;
1586 guint idx = -1;
1587
1588 if (GST_CLOCK_TIME_IS_VALID (target_time)) {
1589 GST_LOG_OBJECT (stream->pad,
1590 "target_time:%" GST_TIME_FORMAT " fragment ts %" GST_TIME_FORMAT
1591 " average keyframe dist: %" GST_TIME_FORMAT
1592 " current keyframe dist: %" GST_TIME_FORMAT
1593 " fragment duration:%" GST_TIME_FORMAT,
1594 GST_TIME_ARGS (target_time),
1595 GST_TIME_ARGS (dashstream->current_fragment_timestamp),
1596 GST_TIME_ARGS (dashstream->keyframe_average_distance),
1597 GST_TIME_ARGS (dashstream->current_fragment_keyframe_distance),
1598 GST_TIME_ARGS (stream->fragment.duration));
1599
1600 if (stream->demux->segment.rate > 0.0) {
1601 idx =
1602 (target_time -
1603 dashstream->current_fragment_timestamp) /
1604 dashstream->current_fragment_keyframe_distance;
1605
1606 /* Prevent getting stuck in a loop due to rounding errors */
1607 if (idx == dashstream->current_sync_sample)
1608 idx++;
1609 } else {
1610 GstClockTime end_time =
1611 dashstream->current_fragment_timestamp +
1612 dashstream->current_fragment_duration;
1613
1614 if (end_time < target_time) {
1615 idx = dashstream->moof_sync_samples->len;
1616 } else {
1617 idx =
1618 (end_time -
1619 target_time) / dashstream->current_fragment_keyframe_distance;
1620 if (idx == dashstream->moof_sync_samples->len) {
1621 dashstream->current_sync_sample = -1;
1622 fragment_finished = TRUE;
1623 goto beach;
1624 }
1625 idx = dashstream->moof_sync_samples->len - 1 - idx;
1626 }
1627
1628 /* Prevent getting stuck in a loop due to rounding errors */
1629 if (idx == dashstream->current_sync_sample) {
1630 if (idx == 0) {
1631 dashstream->current_sync_sample = -1;
1632 fragment_finished = TRUE;
1633 goto beach;
1634 }
1635
1636 idx--;
1637 }
1638 }
1639 }
1640
1641 GST_DEBUG_OBJECT (stream->pad,
1642 "Advancing sync sample #%d target #%d",
1643 dashstream->current_sync_sample, idx);
1644
1645 if (idx != -1 && idx >= dashstream->moof_sync_samples->len) {
1646 dashstream->current_sync_sample = -1;
1647 fragment_finished = TRUE;
1648 goto beach;
1649 }
1650
1651 if (stream->demux->segment.rate > 0.0) {
1652 /* Try to get the sync sample for the target time */
1653 if (idx != -1) {
1654 dashstream->current_sync_sample = idx;
1655 } else {
1656 dashstream->current_sync_sample++;
1657 if (dashstream->current_sync_sample >= dashstream->moof_sync_samples->len) {
1658 fragment_finished = TRUE;
1659 }
1660 }
1661 } else {
1662 if (idx != -1) {
1663 dashstream->current_sync_sample = idx;
1664 } else if (dashstream->current_sync_sample == -1) {
1665 dashstream->current_sync_sample = dashstream->moof_sync_samples->len - 1;
1666 } else if (dashstream->current_sync_sample == 0) {
1667 dashstream->current_sync_sample = -1;
1668 fragment_finished = TRUE;
1669 } else {
1670 dashstream->current_sync_sample--;
1671 }
1672 }
1673
1674 beach:
1675 GST_DEBUG_OBJECT (stream->pad,
1676 "Advancing sync sample #%d fragment_finished:%d",
1677 dashstream->current_sync_sample, fragment_finished);
1678
1679 if (!fragment_finished)
1680 stream->discont = TRUE;
1681
1682 return !fragment_finished;
1683 }
1684
1685 static gboolean
gst_dash_demux_stream_advance_subfragment(GstAdaptiveDemuxStream * stream)1686 gst_dash_demux_stream_advance_subfragment (GstAdaptiveDemuxStream * stream)
1687 {
1688 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1689
1690 GstSidxBox *sidx = SIDX (dashstream);
1691 gboolean fragment_finished = TRUE;
1692
1693 if (dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
1694 if (stream->demux->segment.rate > 0.0) {
1695 gint idx = ++sidx->entry_index;
1696 if (idx < sidx->entries_count) {
1697 fragment_finished = FALSE;
1698 }
1699
1700 if (idx == sidx->entries_count)
1701 dashstream->sidx_position =
1702 sidx->entries[idx - 1].pts + sidx->entries[idx - 1].duration;
1703 else
1704 dashstream->sidx_position = sidx->entries[idx].pts;
1705 } else {
1706 gint idx = --sidx->entry_index;
1707
1708 if (idx >= 0) {
1709 fragment_finished = FALSE;
1710 dashstream->sidx_position = sidx->entries[idx].pts;
1711 } else {
1712 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
1713 }
1714 }
1715 }
1716
1717 GST_DEBUG_OBJECT (stream->pad, "New sidx index: %d / %d. "
1718 "Finished fragment: %d", sidx->entry_index, sidx->entries_count,
1719 fragment_finished);
1720
1721 return !fragment_finished;
1722 }
1723
1724 static gboolean
gst_dash_demux_stream_has_next_fragment(GstAdaptiveDemuxStream * stream)1725 gst_dash_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
1726 {
1727 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1728 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1729
1730 if (dashstream->moof_sync_samples &&
1731 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
1732 if (gst_dash_demux_stream_has_next_sync_sample (stream))
1733 return TRUE;
1734 }
1735
1736 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)) {
1737 if (gst_dash_demux_stream_has_next_subfragment (stream))
1738 return TRUE;
1739 }
1740
1741 return gst_mpd_client_has_next_segment (dashdemux->client,
1742 dashstream->active_stream, stream->demux->segment.rate > 0.0);
1743 }
1744
1745 /* The goal here is to figure out, once we have pushed a keyframe downstream,
1746 * what the next ideal keyframe to download is.
1747 *
1748 * This is done based on:
1749 * * the current internal position (i.e. actual_position)
1750 * * the reported downstream position (QoS feedback)
1751 * * the average keyframe download time (average_download_time)
1752 */
1753 static GstClockTime
gst_dash_demux_stream_get_target_time(GstDashDemux * dashdemux,GstAdaptiveDemuxStream * stream,GstClockTime cur_position,GstClockTime min_skip)1754 gst_dash_demux_stream_get_target_time (GstDashDemux * dashdemux,
1755 GstAdaptiveDemuxStream * stream, GstClockTime cur_position,
1756 GstClockTime min_skip)
1757 {
1758 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1759 GstClockTime cur_running, min_running, min_position;
1760 GstClockTimeDiff diff;
1761 GstClockTime ret = cur_position;
1762 GstClockTime deadline;
1763 GstClockTime earliest_time = GST_CLOCK_TIME_NONE;
1764
1765 g_assert (min_skip > 0);
1766
1767 /* minimum stream position we have to skip to */
1768 if (stream->segment.rate > 0)
1769 min_position = cur_position + min_skip;
1770 else if (cur_position < min_skip)
1771 min_position = 0;
1772 else
1773 min_position = cur_position - min_skip;
1774
1775 /* Use current clock time or the QoS earliest time, whichever is further in
1776 * the future. The QoS time is only updated on every QoS event and
1777 * especially not if e.g. a videodecoder or converter drops a frame further
1778 * downstream.
1779 *
1780 * We only use the times if we ever received a QoS event since the last
1781 * flush, as otherwise base_time and clock might not be correct because of a
1782 * still pre-rolling sink
1783 */
1784 if (stream->qos_earliest_time != GST_CLOCK_TIME_NONE) {
1785 GstClock *clock;
1786
1787 clock = gst_element_get_clock (GST_ELEMENT_CAST (dashdemux));
1788
1789 if (clock) {
1790 GstClockTime base_time;
1791 GstClockTime now_time;
1792
1793 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (dashdemux));
1794 now_time = gst_clock_get_time (clock);
1795 if (now_time > base_time)
1796 now_time -= base_time;
1797 else
1798 now_time = 0;
1799
1800 gst_object_unref (clock);
1801
1802 earliest_time = MAX (now_time, stream->qos_earliest_time);
1803 } else {
1804 earliest_time = stream->qos_earliest_time;
1805 }
1806 }
1807
1808 /* our current position in running time */
1809 cur_running =
1810 gst_segment_to_running_time (&stream->segment, GST_FORMAT_TIME,
1811 cur_position);
1812
1813 /* the minimum position we have to skip to in running time */
1814 min_running =
1815 gst_segment_to_running_time (&stream->segment, GST_FORMAT_TIME,
1816 min_position);
1817
1818 GST_DEBUG_OBJECT (stream->pad,
1819 "position: current %" GST_TIME_FORMAT " min next %" GST_TIME_FORMAT,
1820 GST_TIME_ARGS (cur_position), GST_TIME_ARGS (min_position));
1821 GST_DEBUG_OBJECT (stream->pad,
1822 "running time: current %" GST_TIME_FORMAT " min next %" GST_TIME_FORMAT
1823 " earliest %" GST_TIME_FORMAT, GST_TIME_ARGS (cur_running),
1824 GST_TIME_ARGS (min_running), GST_TIME_ARGS (earliest_time));
1825
1826 /* Take configured maximum video bandwidth and framerate into account */
1827 {
1828 GstClockTime min_run_dist, min_frame_dist, diff = 0;
1829 guint max_fps_n, max_fps_d;
1830
1831 min_run_dist = min_skip / ABS (stream->segment.rate);
1832
1833 if (dashdemux->max_video_framerate_n != 0) {
1834 max_fps_n = dashdemux->max_video_framerate_n;
1835 max_fps_d = dashdemux->max_video_framerate_d;
1836 } else {
1837 /* more than 10 fps is not very useful if we're skipping anyway */
1838 max_fps_n = 10;
1839 max_fps_d = 1;
1840 }
1841
1842 min_frame_dist = gst_util_uint64_scale_ceil (GST_SECOND,
1843 max_fps_d, max_fps_n);
1844
1845 GST_DEBUG_OBJECT (stream->pad,
1846 "Have max framerate %d/%d - Min dist %" GST_TIME_FORMAT
1847 ", min requested dist %" GST_TIME_FORMAT,
1848 max_fps_n, max_fps_d,
1849 GST_TIME_ARGS (min_run_dist), GST_TIME_ARGS (min_frame_dist));
1850 if (min_frame_dist > min_run_dist)
1851 diff = MAX (diff, min_frame_dist - min_run_dist);
1852
1853 if (dashdemux->max_bitrate != 0) {
1854 guint64 max_bitrate = gst_util_uint64_scale_ceil (GST_SECOND,
1855 8 * dashstream->keyframe_average_size,
1856 dashstream->keyframe_average_distance) * ABS (stream->segment.rate);
1857
1858 if (max_bitrate > dashdemux->max_bitrate) {
1859 min_frame_dist = gst_util_uint64_scale_ceil (GST_SECOND,
1860 8 * dashstream->keyframe_average_size,
1861 dashdemux->max_bitrate) * ABS (stream->segment.rate);
1862
1863 GST_DEBUG_OBJECT (stream->pad,
1864 "Have max bitrate %u - Min dist %" GST_TIME_FORMAT
1865 ", min requested dist %" GST_TIME_FORMAT, dashdemux->max_bitrate,
1866 GST_TIME_ARGS (min_run_dist), GST_TIME_ARGS (min_frame_dist));
1867 if (min_frame_dist > min_run_dist)
1868 diff = MAX (diff, min_frame_dist - min_run_dist);
1869 }
1870 }
1871
1872 if (diff > 0) {
1873 GST_DEBUG_OBJECT (stream->pad,
1874 "Skipping further ahead by %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1875 min_running += diff;
1876 }
1877 }
1878
1879 if (earliest_time == GST_CLOCK_TIME_NONE) {
1880 GstClockTime run_key_dist;
1881
1882 run_key_dist =
1883 dashstream->keyframe_average_distance / ABS (stream->segment.rate);
1884
1885 /* If we don't have downstream information (such as at startup or
1886 * without live sinks), just get the next time by taking the minimum
1887 * amount we have to skip ahead
1888 * Except if it takes us longer to download */
1889 if (run_key_dist > dashstream->average_download_time)
1890 ret =
1891 gst_segment_position_from_running_time (&stream->segment,
1892 GST_FORMAT_TIME, min_running);
1893 else
1894 ret = gst_segment_position_from_running_time (&stream->segment,
1895 GST_FORMAT_TIME,
1896 min_running - run_key_dist + dashstream->average_download_time);
1897
1898 GST_DEBUG_OBJECT (stream->pad,
1899 "Advancing to %" GST_TIME_FORMAT " (was %" GST_TIME_FORMAT ")",
1900 GST_TIME_ARGS (ret), GST_TIME_ARGS (min_position));
1901
1902 goto out;
1903 }
1904
1905 /* Figure out the difference, in running time, between where we are and
1906 * where downstream is */
1907 diff = min_running - earliest_time;
1908 GST_LOG_OBJECT (stream->pad,
1909 "min_running %" GST_TIME_FORMAT " diff %" GST_STIME_FORMAT
1910 " average_download %" GST_TIME_FORMAT, GST_TIME_ARGS (min_running),
1911 GST_STIME_ARGS (diff), GST_TIME_ARGS (dashstream->average_download_time));
1912
1913 /* Have at least 500ms or 3 keyframes safety between current position and downstream */
1914 deadline = MAX (500 * GST_MSECOND, 3 * dashstream->average_download_time);
1915
1916 /* The furthest away we are from the current position, the least we need to advance */
1917 if (diff < 0 || diff < deadline) {
1918 /* Force skipping (but not more than 1s ahead) */
1919 ret =
1920 gst_segment_position_from_running_time (&stream->segment,
1921 GST_FORMAT_TIME, earliest_time + MIN (deadline, GST_SECOND));
1922 GST_DEBUG_OBJECT (stream->pad,
1923 "MUST SKIP to at least %" GST_TIME_FORMAT " (was %" GST_TIME_FORMAT ")",
1924 GST_TIME_ARGS (ret), GST_TIME_ARGS (min_position));
1925 } else if (diff < 4 * dashstream->average_download_time) {
1926 /* Go forward a bit less aggresively (and at most 1s forward) */
1927 ret = gst_segment_position_from_running_time (&stream->segment,
1928 GST_FORMAT_TIME, min_running + MIN (GST_SECOND,
1929 2 * dashstream->average_download_time));
1930 GST_DEBUG_OBJECT (stream->pad,
1931 "MUST SKIP to at least %" GST_TIME_FORMAT " (was %" GST_TIME_FORMAT ")",
1932 GST_TIME_ARGS (ret), GST_TIME_ARGS (min_position));
1933 } else {
1934 /* Get the next position satisfying the download time */
1935 ret = gst_segment_position_from_running_time (&stream->segment,
1936 GST_FORMAT_TIME, min_running);
1937 GST_DEBUG_OBJECT (stream->pad,
1938 "Advance to %" GST_TIME_FORMAT " (was %" GST_TIME_FORMAT ")",
1939 GST_TIME_ARGS (ret), GST_TIME_ARGS (min_position));
1940 }
1941
1942 out:
1943
1944 {
1945 GstClockTime cur_skip =
1946 (cur_position < ret) ? ret - cur_position : cur_position - ret;
1947
1948 if (dashstream->average_skip_size == 0) {
1949 dashstream->average_skip_size = cur_skip;
1950 } else {
1951 dashstream->average_skip_size =
1952 (cur_skip + 3 * dashstream->average_skip_size) / 4;
1953 }
1954
1955 if (dashstream->average_skip_size >
1956 cur_skip + dashstream->keyframe_average_distance
1957 && dashstream->average_skip_size > min_skip) {
1958 if (stream->segment.rate > 0)
1959 ret = cur_position + dashstream->average_skip_size;
1960 else if (cur_position > dashstream->average_skip_size)
1961 ret = cur_position - dashstream->average_skip_size;
1962 else
1963 ret = 0;
1964 }
1965 }
1966
1967 return ret;
1968 }
1969
1970 static GstFlowReturn
gst_dash_demux_stream_advance_fragment(GstAdaptiveDemuxStream * stream)1971 gst_dash_demux_stream_advance_fragment (GstAdaptiveDemuxStream * stream)
1972 {
1973 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
1974 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
1975 GstClockTime target_time = GST_CLOCK_TIME_NONE;
1976 GstClockTime previous_position;
1977 GstFlowReturn ret;
1978
1979 GST_DEBUG_OBJECT (stream->pad, "Advance fragment");
1980
1981 /* Update download statistics */
1982 if (dashstream->moof_sync_samples &&
1983 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux) &&
1984 GST_CLOCK_TIME_IS_VALID (stream->last_download_time)) {
1985 if (GST_CLOCK_TIME_IS_VALID (dashstream->average_download_time)) {
1986 dashstream->average_download_time =
1987 (3 * dashstream->average_download_time +
1988 stream->last_download_time) / 4;
1989 } else {
1990 dashstream->average_download_time = stream->last_download_time;
1991 }
1992
1993 GST_DEBUG_OBJECT (stream->pad,
1994 "Download time last: %" GST_TIME_FORMAT " average: %" GST_TIME_FORMAT,
1995 GST_TIME_ARGS (stream->last_download_time),
1996 GST_TIME_ARGS (dashstream->average_download_time));
1997 }
1998
1999 previous_position = dashstream->actual_position;
2000
2001 /* Update internal position */
2002 if (GST_CLOCK_TIME_IS_VALID (dashstream->actual_position)) {
2003 GstClockTime dur;
2004 if (dashstream->moof_sync_samples
2005 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
2006 GST_LOG_OBJECT (stream->pad, "current sync sample #%d",
2007 dashstream->current_sync_sample);
2008 if (dashstream->current_sync_sample == -1) {
2009 dur = 0;
2010 } else if (dashstream->current_sync_sample <
2011 dashstream->moof_sync_samples->len) {
2012 dur = dashstream->current_fragment_keyframe_distance;
2013 } else {
2014 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2015 dashstream->sidx_position != GST_CLOCK_TIME_NONE
2016 && SIDX (dashstream)->entries) {
2017 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
2018 dur = entry->duration;
2019 } else {
2020 dur =
2021 dashstream->current_fragment_timestamp +
2022 dashstream->current_fragment_duration -
2023 dashstream->actual_position;
2024 }
2025 }
2026 } else if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2027 dashstream->sidx_position != GST_CLOCK_TIME_NONE
2028 && SIDX (dashstream)->entries) {
2029 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dashstream);
2030 dur = entry->duration;
2031 } else {
2032 dur = stream->fragment.duration;
2033 }
2034
2035 if (dashstream->moof_sync_samples
2036 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
2037 /* We just downloaded the header, we actually use the previous
2038 * target_time now as it was not used up yet */
2039 if (dashstream->current_sync_sample == -1)
2040 target_time = dashstream->target_time;
2041 else
2042 target_time =
2043 gst_dash_demux_stream_get_target_time (dashdemux, stream,
2044 dashstream->actual_position, dur);
2045 dashstream->actual_position = target_time;
2046 } else {
2047 /* Adjust based on direction */
2048 if (stream->demux->segment.rate > 0.0)
2049 dashstream->actual_position += dur;
2050 else if (dashstream->actual_position >= dur)
2051 dashstream->actual_position -= dur;
2052 else
2053 dashstream->actual_position = 0;
2054 }
2055
2056 GST_DEBUG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
2057 GST_TIME_ARGS (dashstream->actual_position));
2058 }
2059 dashstream->target_time = target_time;
2060
2061 GST_DEBUG_OBJECT (stream->pad, "target_time: %" GST_TIME_FORMAT,
2062 GST_TIME_ARGS (target_time));
2063
2064 /* If downloading only keyframes, switch to the next one or fall through */
2065 if (dashstream->moof_sync_samples &&
2066 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux)) {
2067 if (gst_dash_demux_stream_advance_sync_sample (stream, target_time))
2068 return GST_FLOW_OK;
2069 }
2070
2071 dashstream->isobmff_parser.current_fourcc = 0;
2072 dashstream->isobmff_parser.current_start_offset = 0;
2073 dashstream->isobmff_parser.current_size = 0;
2074
2075 if (dashstream->moof)
2076 gst_isoff_moof_box_free (dashstream->moof);
2077 dashstream->moof = NULL;
2078 if (dashstream->moof_sync_samples)
2079 g_array_free (dashstream->moof_sync_samples, TRUE);
2080 dashstream->moof_sync_samples = NULL;
2081 dashstream->current_sync_sample = -1;
2082
2083 /* Check if we just need to 'advance' to the next fragment, or if we
2084 * need to skip by more. */
2085 if (GST_CLOCK_TIME_IS_VALID (target_time)
2086 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux) &&
2087 dashstream->active_stream->mimeType == GST_STREAM_VIDEO) {
2088 GstClockTime actual_ts;
2089 GstSeekFlags flags = 0;
2090
2091 /* Key-unit trick mode, seek to fragment containing target time
2092 *
2093 * We first try seeking without snapping. As above code to skip keyframes
2094 * in the current fragment was not successful, we should go at least one
2095 * fragment ahead. Due to rounding errors we could end up at the same
2096 * fragment again here, in which case we retry seeking with the SNAP_AFTER
2097 * flag.
2098 *
2099 * We don't always set that flag as we would then end up one further
2100 * fragment in the future in all good cases.
2101 */
2102 while (TRUE) {
2103 ret =
2104 gst_dash_demux_stream_seek (stream, (stream->segment.rate > 0), flags,
2105 target_time, &actual_ts);
2106
2107 if (ret != GST_FLOW_OK) {
2108 GST_WARNING_OBJECT (stream->pad, "Failed to seek to %" GST_TIME_FORMAT,
2109 GST_TIME_ARGS (target_time));
2110 /* Give up */
2111 if (flags != 0)
2112 break;
2113
2114 /* Retry with skipping ahead */
2115 flags |= GST_SEEK_FLAG_SNAP_AFTER;
2116 continue;
2117 }
2118
2119 GST_DEBUG_OBJECT (stream->pad,
2120 "Skipped to %" GST_TIME_FORMAT " (wanted %" GST_TIME_FORMAT ", was %"
2121 GST_TIME_FORMAT ")", GST_TIME_ARGS (actual_ts),
2122 GST_TIME_ARGS (target_time), GST_TIME_ARGS (previous_position));
2123
2124 if ((stream->segment.rate > 0 && actual_ts <= previous_position) ||
2125 (stream->segment.rate < 0 && actual_ts >= previous_position)) {
2126 /* Give up */
2127 if (flags != 0)
2128 break;
2129
2130 /* Retry with forcing skipping ahead */
2131 flags |= GST_SEEK_FLAG_SNAP_AFTER;
2132
2133 continue;
2134 }
2135
2136 /* All good */
2137 break;
2138 }
2139 } else {
2140 /* Normal mode, advance to the next fragment */
2141 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)) {
2142 if (gst_dash_demux_stream_advance_subfragment (stream))
2143 return GST_FLOW_OK;
2144 }
2145
2146 if (dashstream->adapter)
2147 gst_adapter_clear (dashstream->adapter);
2148
2149 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
2150 dashstream->sidx_base_offset = 0;
2151 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
2152 dashstream->allow_sidx = TRUE;
2153
2154 ret = gst_mpd_client_advance_segment (dashdemux->client,
2155 dashstream->active_stream, stream->demux->segment.rate > 0.0);
2156 }
2157 return ret;
2158 }
2159
2160 static gboolean
gst_dash_demux_stream_select_bitrate(GstAdaptiveDemuxStream * stream,guint64 bitrate)2161 gst_dash_demux_stream_select_bitrate (GstAdaptiveDemuxStream * stream,
2162 guint64 bitrate)
2163 {
2164 GstActiveStream *active_stream = NULL;
2165 GList *rep_list = NULL;
2166 gint new_index;
2167 GstAdaptiveDemux *base_demux = stream->demux;
2168 GstDashDemux *demux = GST_DASH_DEMUX_CAST (stream->demux);
2169 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2170 gboolean ret = FALSE;
2171
2172 active_stream = dashstream->active_stream;
2173 if (active_stream == NULL) {
2174 goto end;
2175 }
2176
2177 /* In key-frame trick mode don't change bitrates */
2178 if (GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (demux)) {
2179 GST_DEBUG_OBJECT (demux, "In key-frame trick mode, not changing bitrates");
2180 goto end;
2181 }
2182
2183 /* retrieve representation list */
2184 if (active_stream->cur_adapt_set)
2185 rep_list = active_stream->cur_adapt_set->Representations;
2186 if (!rep_list) {
2187 goto end;
2188 }
2189
2190 GST_DEBUG_OBJECT (stream->pad,
2191 "Trying to change to bitrate: %" G_GUINT64_FORMAT, bitrate);
2192
2193 if (active_stream->mimeType == GST_STREAM_VIDEO && demux->max_bitrate) {
2194 bitrate = MIN (demux->max_bitrate, bitrate);
2195 }
2196
2197 /* get representation index with current max_bandwidth */
2198 if (GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (base_demux) ||
2199 ABS (base_demux->segment.rate) <= 1.0) {
2200 new_index =
2201 gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list, bitrate,
2202 demux->max_video_width, demux->max_video_height,
2203 demux->max_video_framerate_n, demux->max_video_framerate_d);
2204 } else {
2205 new_index =
2206 gst_mpdparser_get_rep_idx_with_max_bandwidth (rep_list,
2207 bitrate / ABS (base_demux->segment.rate), demux->max_video_width,
2208 demux->max_video_height, demux->max_video_framerate_n,
2209 demux->max_video_framerate_d);
2210 }
2211
2212 /* if no representation has the required bandwidth, take the lowest one */
2213 if (new_index == -1)
2214 new_index = gst_mpdparser_get_rep_idx_with_min_bandwidth (rep_list);
2215
2216 if (new_index != active_stream->representation_idx) {
2217 GstRepresentationNode *rep = g_list_nth_data (rep_list, new_index);
2218 GST_INFO_OBJECT (demux, "Changing representation idx: %d %d %u",
2219 dashstream->index, new_index, rep->bandwidth);
2220 if (gst_mpd_client_setup_representation (demux->client, active_stream, rep)) {
2221 GstCaps *caps;
2222
2223 GST_INFO_OBJECT (demux, "Switching bitrate to %d",
2224 active_stream->cur_representation->bandwidth);
2225 caps = gst_dash_demux_get_input_caps (demux, active_stream);
2226 gst_adaptive_demux_stream_set_caps (stream, caps);
2227 ret = TRUE;
2228
2229 } else {
2230 GST_WARNING_OBJECT (demux, "Can not switch representation, aborting...");
2231 }
2232 }
2233
2234 if (ret) {
2235 if (gst_mpd_client_has_isoff_ondemand_profile (demux->client)
2236 && SIDX (dashstream)->entries) {
2237 /* store our current position to change to the same one in a different
2238 * representation if needed */
2239 if (SIDX (dashstream)->entry_index < SIDX (dashstream)->entries_count)
2240 dashstream->sidx_position = SIDX_CURRENT_ENTRY (dashstream)->pts;
2241 else if (SIDX (dashstream)->entry_index >=
2242 SIDX (dashstream)->entries_count)
2243 dashstream->sidx_position =
2244 SIDX_ENTRY (dashstream,
2245 SIDX (dashstream)->entries_count - 1)->pts + SIDX_ENTRY (dashstream,
2246 SIDX (dashstream)->entries_count - 1)->duration;
2247 else
2248 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
2249 } else {
2250 dashstream->sidx_position = GST_CLOCK_TIME_NONE;
2251 }
2252
2253 gst_isoff_sidx_parser_clear (&dashstream->sidx_parser);
2254 dashstream->sidx_base_offset = 0;
2255 dashstream->allow_sidx = TRUE;
2256
2257 /* Reset ISOBMFF box parsing state */
2258 dashstream->isobmff_parser.current_fourcc = 0;
2259 dashstream->isobmff_parser.current_start_offset = 0;
2260 dashstream->isobmff_parser.current_size = 0;
2261
2262 dashstream->current_offset = -1;
2263 dashstream->current_index_header_or_data = 0;
2264
2265 if (dashstream->adapter)
2266 gst_adapter_clear (dashstream->adapter);
2267
2268 if (dashstream->moof)
2269 gst_isoff_moof_box_free (dashstream->moof);
2270 dashstream->moof = NULL;
2271 if (dashstream->moof_sync_samples)
2272 g_array_free (dashstream->moof_sync_samples, TRUE);
2273 dashstream->moof_sync_samples = NULL;
2274 dashstream->current_sync_sample = -1;
2275 dashstream->target_time = GST_CLOCK_TIME_NONE;
2276 }
2277
2278 end:
2279 return ret;
2280 }
2281
2282 #define SEEK_UPDATES_PLAY_POSITION(r, start_type, stop_type) \
2283 ((r >= 0 && start_type != GST_SEEK_TYPE_NONE) || \
2284 (r < 0 && stop_type != GST_SEEK_TYPE_NONE))
2285
2286 static gboolean
gst_dash_demux_seek(GstAdaptiveDemux * demux,GstEvent * seek)2287 gst_dash_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
2288 {
2289 gdouble rate;
2290 GstFormat format;
2291 GstSeekFlags flags;
2292 GstSeekType start_type, stop_type;
2293 gint64 start, stop;
2294 GList *list;
2295 GstClockTime current_pos, target_pos;
2296 guint current_period;
2297 GstStreamPeriod *period;
2298 GList *iter, *streams = NULL;
2299 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2300 gboolean trickmode_no_audio;
2301
2302 gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
2303 &stop_type, &stop);
2304
2305 if (!SEEK_UPDATES_PLAY_POSITION (rate, start_type, stop_type)) {
2306 /* nothing to do if we don't have to update the current position */
2307 return TRUE;
2308 }
2309
2310 if (demux->segment.rate > 0.0) {
2311 target_pos = (GstClockTime) start;
2312 } else {
2313 target_pos = (GstClockTime) stop;
2314 }
2315
2316 /* select the requested Period in the Media Presentation */
2317 if (!gst_mpd_client_setup_media_presentation (dashdemux->client, target_pos,
2318 -1, NULL))
2319 return FALSE;
2320
2321 current_period = 0;
2322 for (list = g_list_first (dashdemux->client->periods); list;
2323 list = g_list_next (list)) {
2324 period = list->data;
2325 current_pos = period->start;
2326 current_period = period->number;
2327 GST_DEBUG_OBJECT (demux, "Looking at period %u) start:%"
2328 GST_TIME_FORMAT " - duration:%"
2329 GST_TIME_FORMAT ") for position %" GST_TIME_FORMAT,
2330 current_period, GST_TIME_ARGS (current_pos),
2331 GST_TIME_ARGS (period->duration), GST_TIME_ARGS (target_pos));
2332 if (current_pos <= target_pos
2333 && target_pos <= current_pos + period->duration) {
2334 break;
2335 }
2336 }
2337 if (list == NULL) {
2338 GST_WARNING_OBJECT (demux, "Could not find seeked Period");
2339 return FALSE;
2340 }
2341
2342 trickmode_no_audio = ! !(flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO);
2343
2344 streams = demux->streams;
2345 if (current_period != gst_mpd_client_get_period_index (dashdemux->client)) {
2346 GST_DEBUG_OBJECT (demux, "Seeking to Period %d", current_period);
2347
2348 /* clean old active stream list, if any */
2349 gst_active_streams_free (dashdemux->client);
2350 dashdemux->trickmode_no_audio = trickmode_no_audio;
2351
2352 /* setup video, audio and subtitle streams, starting from the new Period */
2353 if (!gst_mpd_client_set_period_index (dashdemux->client, current_period)
2354 || !gst_dash_demux_setup_all_streams (dashdemux))
2355 return FALSE;
2356 streams = demux->next_streams;
2357 } else if (dashdemux->trickmode_no_audio != trickmode_no_audio) {
2358 /* clean old active stream list, if any */
2359 gst_active_streams_free (dashdemux->client);
2360 dashdemux->trickmode_no_audio = trickmode_no_audio;
2361
2362 /* setup video, audio and subtitle streams, starting from the new Period */
2363 if (!gst_dash_demux_setup_all_streams (dashdemux))
2364 return FALSE;
2365 streams = demux->next_streams;
2366 }
2367
2368 /* Update the current sequence on all streams */
2369 for (iter = streams; iter; iter = g_list_next (iter)) {
2370 GstAdaptiveDemuxStream *stream = iter->data;
2371 GstDashDemuxStream *dashstream = iter->data;
2372
2373 dashstream->average_skip_size = 0;
2374 if (gst_dash_demux_stream_seek (stream, rate >= 0, 0, target_pos,
2375 NULL) != GST_FLOW_OK)
2376 return FALSE;
2377 }
2378
2379 return TRUE;
2380 }
2381
2382 static gint64
gst_dash_demux_get_manifest_update_interval(GstAdaptiveDemux * demux)2383 gst_dash_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
2384 {
2385 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2386 return MIN (dashdemux->client->mpd_node->minimumUpdatePeriod * 1000,
2387 SLOW_CLOCK_UPDATE_INTERVAL);
2388 }
2389
2390 static GstFlowReturn
gst_dash_demux_update_manifest_data(GstAdaptiveDemux * demux,GstBuffer * buffer)2391 gst_dash_demux_update_manifest_data (GstAdaptiveDemux * demux,
2392 GstBuffer * buffer)
2393 {
2394 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2395 GstMpdClient *new_client = NULL;
2396 GstMapInfo mapinfo;
2397
2398 GST_DEBUG_OBJECT (demux, "Updating manifest file from URL");
2399
2400 /* parse the manifest file */
2401 new_client = gst_mpd_client_new ();
2402 gst_mpd_client_set_uri_downloader (new_client, demux->downloader);
2403 new_client->mpd_uri = g_strdup (demux->manifest_uri);
2404 new_client->mpd_base_uri = g_strdup (demux->manifest_base_uri);
2405 gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
2406
2407 if (gst_mpd_parse (new_client, (gchar *) mapinfo.data, mapinfo.size)) {
2408 const gchar *period_id;
2409 guint period_idx;
2410 GList *iter;
2411 GList *streams_iter;
2412 GList *streams;
2413
2414 /* prepare the new manifest and try to transfer the stream position
2415 * status from the old manifest client */
2416
2417 GST_DEBUG_OBJECT (demux, "Updating manifest");
2418
2419 period_id = gst_mpd_client_get_period_id (dashdemux->client);
2420 period_idx = gst_mpd_client_get_period_index (dashdemux->client);
2421
2422 /* setup video, audio and subtitle streams, starting from current Period */
2423 if (!gst_mpd_client_setup_media_presentation (new_client, -1,
2424 (period_id ? -1 : period_idx), period_id)) {
2425 /* TODO */
2426 }
2427
2428 if (period_id) {
2429 if (!gst_mpd_client_set_period_id (new_client, period_id)) {
2430 GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
2431 gst_mpd_client_free (new_client);
2432 gst_buffer_unmap (buffer, &mapinfo);
2433 return GST_FLOW_EOS;
2434 }
2435 } else {
2436 if (!gst_mpd_client_set_period_index (new_client, period_idx)) {
2437 GST_DEBUG_OBJECT (demux, "Error setting up the updated manifest file");
2438 gst_mpd_client_free (new_client);
2439 gst_buffer_unmap (buffer, &mapinfo);
2440 return GST_FLOW_EOS;
2441 }
2442 }
2443
2444 if (!gst_dash_demux_setup_mpdparser_streams (dashdemux, new_client)) {
2445 GST_ERROR_OBJECT (demux, "Failed to setup streams on manifest " "update");
2446 gst_mpd_client_free (new_client);
2447 gst_buffer_unmap (buffer, &mapinfo);
2448 return GST_FLOW_ERROR;
2449 }
2450
2451 /* If no pads have been exposed yet, need to use those */
2452 streams = NULL;
2453 if (demux->streams == NULL) {
2454 if (demux->prepared_streams) {
2455 streams = demux->prepared_streams;
2456 }
2457 } else {
2458 streams = demux->streams;
2459 }
2460
2461 /* update the streams to play from the next segment */
2462 for (iter = streams, streams_iter = new_client->active_streams;
2463 iter && streams_iter;
2464 iter = g_list_next (iter), streams_iter = g_list_next (streams_iter)) {
2465 GstDashDemuxStream *demux_stream = iter->data;
2466 GstActiveStream *new_stream = streams_iter->data;
2467 GstClockTime ts;
2468
2469 if (!new_stream) {
2470 GST_DEBUG_OBJECT (demux,
2471 "Stream of index %d is missing from manifest update",
2472 demux_stream->index);
2473 gst_mpd_client_free (new_client);
2474 gst_buffer_unmap (buffer, &mapinfo);
2475 return GST_FLOW_EOS;
2476 }
2477
2478 if (gst_mpd_client_get_next_fragment_timestamp (dashdemux->client,
2479 demux_stream->index, &ts)
2480 || gst_mpd_client_get_last_fragment_timestamp_end (dashdemux->client,
2481 demux_stream->index, &ts)) {
2482
2483 /* Due to rounding when doing the timescale conversions it might happen
2484 * that the ts falls back to a previous segment, leading the same data
2485 * to be downloaded twice. We try to work around this by always adding
2486 * 10 microseconds to get back to the correct segment. The errors are
2487 * usually on the order of nanoseconds so it should be enough.
2488 */
2489
2490 /* _get_next_fragment_timestamp() returned relative timestamp to
2491 * corresponding period start, but _client_stream_seek expects absolute
2492 * MPD time. */
2493 ts += gst_mpd_parser_get_period_start_time (dashdemux->client);
2494
2495 GST_DEBUG_OBJECT (GST_ADAPTIVE_DEMUX_STREAM_PAD (demux_stream),
2496 "Current position: %" GST_TIME_FORMAT ", updating to %"
2497 GST_TIME_FORMAT, GST_TIME_ARGS (ts),
2498 GST_TIME_ARGS (ts + (10 * GST_USECOND)));
2499 ts += 10 * GST_USECOND;
2500 gst_mpd_client_stream_seek (new_client, new_stream,
2501 demux->segment.rate >= 0, 0, ts, NULL);
2502 }
2503
2504 demux_stream->active_stream = new_stream;
2505 }
2506
2507 gst_mpd_client_free (dashdemux->client);
2508 dashdemux->client = new_client;
2509
2510 GST_DEBUG_OBJECT (demux, "Manifest file successfully updated");
2511 if (dashdemux->clock_drift) {
2512 gst_dash_demux_poll_clock_drift (dashdemux);
2513 }
2514 } else {
2515 /* In most cases, this will happen if we set a wrong url in the
2516 * source element and we have received the 404 HTML response instead of
2517 * the manifest */
2518 GST_WARNING_OBJECT (demux, "Error parsing the manifest.");
2519 gst_mpd_client_free (new_client);
2520 gst_buffer_unmap (buffer, &mapinfo);
2521 return GST_FLOW_ERROR;
2522 }
2523
2524 gst_buffer_unmap (buffer, &mapinfo);
2525
2526 return GST_FLOW_OK;
2527 }
2528
2529 static gint64
gst_dash_demux_stream_get_fragment_waiting_time(GstAdaptiveDemuxStream * stream)2530 gst_dash_demux_stream_get_fragment_waiting_time (GstAdaptiveDemuxStream *
2531 stream)
2532 {
2533 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (stream->demux);
2534 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2535 GstDateTime *segmentAvailability;
2536 GstActiveStream *active_stream = dashstream->active_stream;
2537
2538 segmentAvailability =
2539 gst_mpd_client_get_next_segment_availability_start_time
2540 (dashdemux->client, active_stream);
2541
2542 if (segmentAvailability) {
2543 gint64 diff;
2544 GstDateTime *cur_time;
2545
2546 cur_time =
2547 gst_date_time_new_from_g_date_time
2548 (gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST
2549 (dashdemux)));
2550 diff =
2551 gst_mpd_client_calculate_time_difference (cur_time,
2552 segmentAvailability);
2553 gst_date_time_unref (segmentAvailability);
2554 gst_date_time_unref (cur_time);
2555 /* subtract the server's clock drift, so that if the server's
2556 time is behind our idea of UTC, we need to sleep for longer
2557 before requesting a fragment */
2558 return diff -
2559 gst_dash_demux_get_clock_compensation (dashdemux) * GST_USECOND;
2560 }
2561 return 0;
2562 }
2563
2564 static gboolean
gst_dash_demux_has_next_period(GstAdaptiveDemux * demux)2565 gst_dash_demux_has_next_period (GstAdaptiveDemux * demux)
2566 {
2567 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2568
2569 if (demux->segment.rate >= 0)
2570 return gst_mpd_client_has_next_period (dashdemux->client);
2571 else
2572 return gst_mpd_client_has_previous_period (dashdemux->client);
2573 }
2574
2575 static void
gst_dash_demux_advance_period(GstAdaptiveDemux * demux)2576 gst_dash_demux_advance_period (GstAdaptiveDemux * demux)
2577 {
2578 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2579
2580 if (demux->segment.rate >= 0) {
2581 if (!gst_mpd_client_set_period_index (dashdemux->client,
2582 gst_mpd_client_get_period_index (dashdemux->client) + 1)) {
2583 /* TODO error */
2584 return;
2585 }
2586 } else {
2587 if (!gst_mpd_client_set_period_index (dashdemux->client,
2588 gst_mpd_client_get_period_index (dashdemux->client) - 1)) {
2589 /* TODO error */
2590 return;
2591 }
2592 }
2593
2594 gst_dash_demux_setup_all_streams (dashdemux);
2595 gst_mpd_client_seek_to_first_segment (dashdemux->client);
2596 }
2597
2598 static GstBuffer *
_gst_buffer_split(GstBuffer * buffer,gint offset,gsize size)2599 _gst_buffer_split (GstBuffer * buffer, gint offset, gsize size)
2600 {
2601 GstBuffer *newbuf = gst_buffer_copy_region (buffer,
2602 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_META
2603 | GST_BUFFER_COPY_MEMORY, offset, size == -1 ? size : size - offset);
2604
2605 gst_buffer_resize (buffer, 0, offset);
2606
2607 return newbuf;
2608 }
2609
2610 static gboolean
gst_dash_demux_stream_fragment_start(GstAdaptiveDemux * demux,GstAdaptiveDemuxStream * stream)2611 gst_dash_demux_stream_fragment_start (GstAdaptiveDemux * demux,
2612 GstAdaptiveDemuxStream * stream)
2613 {
2614 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2615 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2616
2617 GST_LOG_OBJECT (stream->pad, "Actual position %" GST_TIME_FORMAT,
2618 GST_TIME_ARGS (dashstream->actual_position));
2619
2620 dashstream->current_index_header_or_data = 0;
2621 dashstream->current_offset = -1;
2622
2623 /* We need to mark every first buffer of a key unit as discont,
2624 * and also every first buffer of a moov and moof. This ensures
2625 * that qtdemux takes note of our buffer offsets for each of those
2626 * buffers instead of keeping track of them itself from the first
2627 * buffer. We need offsets to be consistent between moof and mdat
2628 */
2629 if (dashstream->is_isobmff && dashdemux->allow_trickmode_key_units
2630 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (demux)
2631 && dashstream->active_stream->mimeType == GST_STREAM_VIDEO)
2632 stream->discont = TRUE;
2633
2634 return TRUE;
2635 }
2636
2637 static GstFlowReturn
gst_dash_demux_stream_fragment_finished(GstAdaptiveDemux * demux,GstAdaptiveDemuxStream * stream)2638 gst_dash_demux_stream_fragment_finished (GstAdaptiveDemux * demux,
2639 GstAdaptiveDemuxStream * stream)
2640 {
2641 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2642 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2643
2644 /* We need to mark every first buffer of a key unit as discont,
2645 * and also every first buffer of a moov and moof. This ensures
2646 * that qtdemux takes note of our buffer offsets for each of those
2647 * buffers instead of keeping track of them itself from the first
2648 * buffer. We need offsets to be consistent between moof and mdat
2649 */
2650 if (dashstream->is_isobmff && dashdemux->allow_trickmode_key_units
2651 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (demux)
2652 && dashstream->active_stream->mimeType == GST_STREAM_VIDEO)
2653 stream->discont = TRUE;
2654
2655 /* Only handle fragment advancing specifically for SIDX if we're not
2656 * in key unit mode */
2657 if (!(dashstream->moof_sync_samples
2658 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (dashdemux))
2659 && gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)
2660 && dashstream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
2661 /* fragment is advanced on data_received when byte limits are reached */
2662 if (dashstream->pending_seek_ts != GST_CLOCK_TIME_NONE) {
2663 if (SIDX (dashstream)->entry_index < SIDX (dashstream)->entries_count)
2664 return GST_FLOW_OK;
2665 } else if (gst_dash_demux_stream_has_next_subfragment (stream)) {
2666 return GST_FLOW_OK;
2667 }
2668 }
2669
2670 if (G_UNLIKELY (stream->downloading_header || stream->downloading_index))
2671 return GST_FLOW_OK;
2672
2673 return gst_adaptive_demux_stream_advance_fragment (demux, stream,
2674 stream->fragment.duration);
2675 }
2676
2677 static gboolean
gst_dash_demux_need_another_chunk(GstAdaptiveDemuxStream * stream)2678 gst_dash_demux_need_another_chunk (GstAdaptiveDemuxStream * stream)
2679 {
2680 GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
2681 GstDashDemuxStream *dashstream = (GstDashDemuxStream *) stream;
2682
2683 /* We're chunked downloading for ISOBMFF in KEY_UNITS mode for the actual
2684 * fragment until we parsed the moof and arrived at the mdat. 8192 is a
2685 * random guess for the moof size
2686 */
2687 if (dashstream->is_isobmff
2688 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)
2689 && dashstream->active_stream->mimeType == GST_STREAM_VIDEO
2690 && !stream->downloading_header && !stream->downloading_index
2691 && dashdemux->allow_trickmode_key_units) {
2692 if (dashstream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT) {
2693 /* Need to download the moof first to know anything */
2694
2695 stream->fragment.chunk_size = 8192;
2696 /* Do we have the first fourcc already or are we in the middle */
2697 if (dashstream->isobmff_parser.current_fourcc == 0) {
2698 stream->fragment.chunk_size += dashstream->moof_average_size;
2699 if (dashstream->first_sync_sample_always_after_moof) {
2700 gboolean first = FALSE;
2701 /* Check if we'll really need that first sample */
2702 if (GST_CLOCK_TIME_IS_VALID (dashstream->target_time)) {
2703 first =
2704 ((dashstream->target_time -
2705 dashstream->current_fragment_timestamp) /
2706 dashstream->keyframe_average_distance) == 0 ? TRUE : FALSE;
2707 } else if (stream->segment.rate > 0) {
2708 first = TRUE;
2709 }
2710
2711 if (first)
2712 stream->fragment.chunk_size += dashstream->keyframe_average_size;
2713 }
2714 }
2715
2716 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2717 dashstream->sidx_parser.sidx.entries) {
2718 guint64 sidx_start_offset =
2719 dashstream->sidx_base_offset +
2720 SIDX_CURRENT_ENTRY (dashstream)->offset;
2721 guint64 sidx_end_offset =
2722 sidx_start_offset + SIDX_CURRENT_ENTRY (dashstream)->size;
2723 guint64 downloaded_end_offset;
2724
2725 if (dashstream->current_offset == GST_CLOCK_TIME_NONE) {
2726 downloaded_end_offset = sidx_start_offset;
2727 } else {
2728 downloaded_end_offset =
2729 dashstream->current_offset +
2730 gst_adapter_available (dashstream->adapter);
2731 }
2732
2733 downloaded_end_offset = MAX (downloaded_end_offset, sidx_start_offset);
2734
2735 if (stream->fragment.chunk_size +
2736 downloaded_end_offset > sidx_end_offset) {
2737 stream->fragment.chunk_size = sidx_end_offset - downloaded_end_offset;
2738 }
2739 }
2740 } else if (dashstream->moof && dashstream->moof_sync_samples) {
2741 /* Have the moof, either we're done now or we want to download the
2742 * directly following sync sample */
2743 if (dashstream->first_sync_sample_after_moof
2744 && dashstream->current_sync_sample == 0) {
2745 GstDashStreamSyncSample *sync_sample =
2746 &g_array_index (dashstream->moof_sync_samples,
2747 GstDashStreamSyncSample, 0);
2748 guint64 end_offset = sync_sample->end_offset + 1;
2749 guint64 downloaded_end_offset;
2750
2751 downloaded_end_offset =
2752 dashstream->current_offset +
2753 gst_adapter_available (dashstream->adapter);
2754
2755 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2756 dashstream->sidx_parser.sidx.entries) {
2757 guint64 sidx_end_offset =
2758 dashstream->sidx_base_offset +
2759 SIDX_CURRENT_ENTRY (dashstream)->offset +
2760 SIDX_CURRENT_ENTRY (dashstream)->size;
2761
2762 if (end_offset > sidx_end_offset) {
2763 end_offset = sidx_end_offset;
2764 }
2765 }
2766
2767 if (downloaded_end_offset < end_offset) {
2768 stream->fragment.chunk_size = end_offset - downloaded_end_offset;
2769 } else {
2770 stream->fragment.chunk_size = 0;
2771 }
2772 } else {
2773 stream->fragment.chunk_size = 0;
2774 }
2775 } else {
2776 /* Have moof but can't do key-units mode, just download until the end */
2777 stream->fragment.chunk_size = -1;
2778 }
2779 } else {
2780 /* We might've decided that we can't allow key-unit only
2781 * trickmodes while doing chunked downloading. In that case
2782 * just download from here to the end now */
2783 if (dashstream->moof
2784 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)) {
2785 stream->fragment.chunk_size = -1;
2786 } else {
2787 stream->fragment.chunk_size = 0;
2788 }
2789 }
2790
2791 return stream->fragment.chunk_size != 0;
2792 }
2793
2794 static GstFlowReturn
gst_dash_demux_parse_isobmff(GstAdaptiveDemux * demux,GstDashDemuxStream * dash_stream,gboolean * sidx_seek_needed)2795 gst_dash_demux_parse_isobmff (GstAdaptiveDemux * demux,
2796 GstDashDemuxStream * dash_stream, gboolean * sidx_seek_needed)
2797 {
2798 GstAdaptiveDemuxStream *stream = (GstAdaptiveDemuxStream *) dash_stream;
2799 GstDashDemux *dashdemux = GST_DASH_DEMUX_CAST (demux);
2800 gsize available;
2801 GstBuffer *buffer;
2802 GstMapInfo map;
2803 GstByteReader reader;
2804 guint32 fourcc;
2805 guint header_size;
2806 guint64 size, buffer_offset;
2807
2808 *sidx_seek_needed = FALSE;
2809
2810 /* This must not be called when we're in the mdat. We only look at the mdat
2811 * header and then stop parsing the boxes as we're only interested in the
2812 * metadata! Handling mdat is the job of the surrounding code, as well as
2813 * stopping or starting the next fragment when mdat is over (=> sidx)
2814 */
2815 g_assert (dash_stream->isobmff_parser.current_fourcc !=
2816 GST_ISOFF_FOURCC_MDAT);
2817
2818 available = gst_adapter_available (dash_stream->adapter);
2819 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
2820 buffer_offset = dash_stream->current_offset;
2821
2822 /* Always at the start of a box here */
2823 g_assert (dash_stream->isobmff_parser.current_size == 0);
2824
2825 /* At the start of a box => Parse it */
2826 gst_buffer_map (buffer, &map, GST_MAP_READ);
2827 gst_byte_reader_init (&reader, map.data, map.size);
2828
2829 /* While there are more boxes left to parse ... */
2830 dash_stream->isobmff_parser.current_start_offset = buffer_offset;
2831 do {
2832 dash_stream->isobmff_parser.current_fourcc = 0;
2833 dash_stream->isobmff_parser.current_size = 0;
2834
2835 if (!gst_isoff_parse_box_header (&reader, &fourcc, NULL, &header_size,
2836 &size)) {
2837 break;
2838 }
2839
2840 dash_stream->isobmff_parser.current_fourcc = fourcc;
2841 if (size == 0) {
2842 /* We assume this is mdat, anything else with "size until end"
2843 * does not seem to make sense */
2844 g_assert (dash_stream->isobmff_parser.current_fourcc ==
2845 GST_ISOFF_FOURCC_MDAT);
2846 dash_stream->isobmff_parser.current_size = -1;
2847 break;
2848 }
2849
2850 dash_stream->isobmff_parser.current_size = size;
2851
2852 /* Do we have the complete box or are at MDAT */
2853 if (gst_byte_reader_get_remaining (&reader) < size - header_size ||
2854 dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MDAT) {
2855 /* Reset byte reader to the beginning of the box */
2856 gst_byte_reader_set_pos (&reader,
2857 gst_byte_reader_get_pos (&reader) - header_size);
2858 break;
2859 }
2860
2861 GST_LOG_OBJECT (stream->pad,
2862 "box %" GST_FOURCC_FORMAT " at offset %" G_GUINT64_FORMAT " size %"
2863 G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc),
2864 dash_stream->isobmff_parser.current_start_offset, size);
2865
2866 if (dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MOOF) {
2867 GstByteReader sub_reader;
2868
2869 /* Only allow SIDX before the very first moof */
2870 dash_stream->allow_sidx = FALSE;
2871
2872 g_assert (dash_stream->moof == NULL);
2873 g_assert (dash_stream->moof_sync_samples == NULL);
2874 gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
2875 dash_stream->moof = gst_isoff_moof_box_parse (&sub_reader);
2876 dash_stream->moof_offset =
2877 dash_stream->isobmff_parser.current_start_offset;
2878 dash_stream->moof_size = size;
2879 dash_stream->current_sync_sample = -1;
2880
2881 if (dash_stream->moof_average_size) {
2882 if (dash_stream->moof_average_size < size)
2883 dash_stream->moof_average_size =
2884 (size * 3 + dash_stream->moof_average_size) / 4;
2885 else
2886 dash_stream->moof_average_size =
2887 (size + dash_stream->moof_average_size + 3) / 4;
2888 } else {
2889 dash_stream->moof_average_size = size;
2890 }
2891 } else if (dash_stream->isobmff_parser.current_fourcc ==
2892 GST_ISOFF_FOURCC_SIDX &&
2893 gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client) &&
2894 dash_stream->allow_sidx) {
2895 GstByteReader sub_reader;
2896 GstIsoffParserResult res;
2897 guint dummy;
2898
2899 dash_stream->sidx_base_offset =
2900 dash_stream->isobmff_parser.current_start_offset + size;
2901 dash_stream->allow_sidx = FALSE;
2902
2903 gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
2904
2905 res =
2906 gst_isoff_sidx_parser_parse (&dash_stream->sidx_parser, &sub_reader,
2907 &dummy);
2908
2909 if (res == GST_ISOFF_PARSER_DONE) {
2910 guint64 first_offset = dash_stream->sidx_parser.sidx.first_offset;
2911 GstSidxBox *sidx = SIDX (dash_stream);
2912 guint i;
2913
2914 if (first_offset) {
2915 GST_LOG_OBJECT (stream->pad,
2916 "non-zero sidx first offset %" G_GUINT64_FORMAT, first_offset);
2917 dash_stream->sidx_base_offset += first_offset;
2918 }
2919
2920 for (i = 0; i < sidx->entries_count; i++) {
2921 GstSidxBoxEntry *entry = &sidx->entries[i];
2922
2923 if (entry->ref_type != 0) {
2924 GST_FIXME_OBJECT (stream->pad, "SIDX ref_type 1 not supported yet");
2925 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
2926 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2927 break;
2928 }
2929 }
2930
2931 /* We might've cleared the index above */
2932 if (sidx->entries_count > 0) {
2933 if (GST_CLOCK_TIME_IS_VALID (dash_stream->pending_seek_ts)) {
2934 /* FIXME, preserve seek flags */
2935 if (gst_dash_demux_stream_sidx_seek (dash_stream,
2936 demux->segment.rate >= 0, 0, dash_stream->pending_seek_ts,
2937 NULL) != GST_FLOW_OK) {
2938 GST_ERROR_OBJECT (stream->pad, "Couldn't find position in sidx");
2939 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
2940 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2941 }
2942 dash_stream->pending_seek_ts = GST_CLOCK_TIME_NONE;
2943 } else {
2944
2945 if (dash_stream->sidx_position == GST_CLOCK_TIME_NONE) {
2946 SIDX (dash_stream)->entry_index = 0;
2947 } else {
2948 if (gst_dash_demux_stream_sidx_seek (dash_stream,
2949 demux->segment.rate >= 0, GST_SEEK_FLAG_SNAP_BEFORE,
2950 dash_stream->sidx_position, NULL) != GST_FLOW_OK) {
2951 GST_ERROR_OBJECT (stream->pad,
2952 "Couldn't find position in sidx");
2953 dash_stream->sidx_position = GST_CLOCK_TIME_NONE;
2954 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
2955 }
2956 }
2957 dash_stream->sidx_position =
2958 SIDX (dash_stream)->entries[SIDX (dash_stream)->
2959 entry_index].pts;
2960 }
2961 }
2962
2963 if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED &&
2964 SIDX (dash_stream)->entry_index != 0) {
2965 /* Need to jump to the requested SIDX entry. Push everything up to
2966 * the SIDX box below and let the caller handle everything else */
2967 *sidx_seek_needed = TRUE;
2968 break;
2969 }
2970 }
2971 } else {
2972 gst_byte_reader_skip (&reader, size - header_size);
2973 }
2974
2975 dash_stream->isobmff_parser.current_fourcc = 0;
2976 dash_stream->isobmff_parser.current_start_offset += size;
2977 dash_stream->isobmff_parser.current_size = 0;
2978 } while (gst_byte_reader_get_remaining (&reader) > 0);
2979
2980 gst_buffer_unmap (buffer, &map);
2981
2982 /* mdat? Push all we have and wait for it to be over */
2983 if (dash_stream->isobmff_parser.current_fourcc == GST_ISOFF_FOURCC_MDAT) {
2984 GstBuffer *pending;
2985
2986 GST_LOG_OBJECT (stream->pad,
2987 "box %" GST_FOURCC_FORMAT " at offset %" G_GUINT64_FORMAT " size %"
2988 G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc),
2989 dash_stream->isobmff_parser.current_start_offset,
2990 dash_stream->isobmff_parser.current_size);
2991
2992 /* At mdat. Move the start of the mdat to the adapter and have everything
2993 * else be pushed. We parsed all header boxes at this point and are not
2994 * supposed to be called again until the next moof */
2995 pending = _gst_buffer_split (buffer, gst_byte_reader_get_pos (&reader), -1);
2996 gst_adapter_push (dash_stream->adapter, pending);
2997 dash_stream->current_offset += gst_byte_reader_get_pos (&reader);
2998 dash_stream->isobmff_parser.current_size = 0;
2999
3000 GST_BUFFER_OFFSET (buffer) = buffer_offset;
3001 GST_BUFFER_OFFSET_END (buffer) =
3002 buffer_offset + gst_buffer_get_size (buffer);
3003 return gst_adaptive_demux_stream_push_buffer (stream, buffer);
3004 } else if (gst_byte_reader_get_pos (&reader) != 0) {
3005 GstBuffer *pending;
3006
3007 /* Multiple complete boxes and no mdat? Push them and keep the remainder,
3008 * which is the start of the next box if any remainder */
3009
3010 pending = _gst_buffer_split (buffer, gst_byte_reader_get_pos (&reader), -1);
3011 gst_adapter_push (dash_stream->adapter, pending);
3012 dash_stream->current_offset += gst_byte_reader_get_pos (&reader);
3013 dash_stream->isobmff_parser.current_size = 0;
3014
3015 GST_BUFFER_OFFSET (buffer) = buffer_offset;
3016 GST_BUFFER_OFFSET_END (buffer) =
3017 buffer_offset + gst_buffer_get_size (buffer);
3018 return gst_adaptive_demux_stream_push_buffer (stream, buffer);
3019 }
3020
3021 /* Not even a single complete, non-mdat box, wait */
3022 dash_stream->isobmff_parser.current_size = 0;
3023 gst_adapter_push (dash_stream->adapter, buffer);
3024
3025 return GST_FLOW_OK;
3026 }
3027
3028 static gboolean
gst_dash_demux_find_sync_samples(GstAdaptiveDemux * demux,GstAdaptiveDemuxStream * stream)3029 gst_dash_demux_find_sync_samples (GstAdaptiveDemux * demux,
3030 GstAdaptiveDemuxStream * stream)
3031 {
3032 GstDashDemux *dashdemux = (GstDashDemux *) stream->demux;
3033 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3034 guint i;
3035 guint32 track_id = 0;
3036 guint64 prev_traf_end;
3037 gboolean trex_sample_flags = FALSE;
3038
3039 if (!dash_stream->moof) {
3040 dashdemux->allow_trickmode_key_units = FALSE;
3041 return FALSE;
3042 }
3043
3044 dash_stream->current_sync_sample = -1;
3045 dash_stream->moof_sync_samples =
3046 g_array_new (FALSE, FALSE, sizeof (GstDashStreamSyncSample));
3047
3048 prev_traf_end = dash_stream->moof_offset;
3049
3050 /* generate table of keyframes and offsets */
3051 for (i = 0; i < dash_stream->moof->traf->len; i++) {
3052 GstTrafBox *traf = &g_array_index (dash_stream->moof->traf, GstTrafBox, i);
3053 guint64 traf_offset = 0, prev_trun_end;
3054 guint j;
3055
3056 if (i == 0) {
3057 track_id = traf->tfhd.track_id;
3058 } else if (track_id != traf->tfhd.track_id) {
3059 GST_ERROR_OBJECT (stream->pad,
3060 "moof with trafs of different track ids (%u != %u)", track_id,
3061 traf->tfhd.track_id);
3062 g_array_free (dash_stream->moof_sync_samples, TRUE);
3063 dash_stream->moof_sync_samples = NULL;
3064 dashdemux->allow_trickmode_key_units = FALSE;
3065 return FALSE;
3066 }
3067
3068 if (traf->tfhd.flags & GST_TFHD_FLAGS_BASE_DATA_OFFSET_PRESENT) {
3069 traf_offset = traf->tfhd.base_data_offset;
3070 } else if (traf->tfhd.flags & GST_TFHD_FLAGS_DEFAULT_BASE_IS_MOOF) {
3071 traf_offset = dash_stream->moof_offset;
3072 } else {
3073 traf_offset = prev_traf_end;
3074 }
3075
3076 prev_trun_end = traf_offset;
3077
3078 for (j = 0; j < traf->trun->len; j++) {
3079 GstTrunBox *trun = &g_array_index (traf->trun, GstTrunBox, j);
3080 guint64 trun_offset, prev_sample_end;
3081 guint k;
3082
3083 if (trun->flags & GST_TRUN_FLAGS_DATA_OFFSET_PRESENT) {
3084 trun_offset = traf_offset + trun->data_offset;
3085 } else {
3086 trun_offset = prev_trun_end;
3087 }
3088
3089 prev_sample_end = trun_offset;
3090 for (k = 0; k < trun->samples->len; k++) {
3091 GstTrunSample *sample =
3092 &g_array_index (trun->samples, GstTrunSample, k);
3093 guint64 sample_offset;
3094 guint32 sample_flags;
3095 #if 0
3096 guint32 sample_duration;
3097 #endif
3098
3099 sample_offset = prev_sample_end;
3100
3101 if (trun->flags & GST_TRUN_FLAGS_SAMPLE_FLAGS_PRESENT) {
3102 sample_flags = sample->sample_flags;
3103 } else if ((trun->flags & GST_TRUN_FLAGS_FIRST_SAMPLE_FLAGS_PRESENT)
3104 && k == 0) {
3105 sample_flags = trun->first_sample_flags;
3106 } else if (traf->
3107 tfhd.flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_FLAGS_PRESENT) {
3108 sample_flags = traf->tfhd.default_sample_flags;
3109 } else {
3110 trex_sample_flags = TRUE;
3111 continue;
3112 }
3113
3114 #if 0
3115 if (trun->flags & GST_TRUN_FLAGS_SAMPLE_DURATION_PRESENT) {
3116 sample_duration = sample->sample_duration;
3117 } else if (traf->
3118 tfhd.flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_DURATION_PRESENT) {
3119 sample_duration = traf->tfhd.default_sample_duration;
3120 } else {
3121 GST_FIXME_OBJECT (stream->pad,
3122 "Sample duration given by trex - can't download only keyframes");
3123 g_array_free (dash_stream->moof_sync_samples, TRUE);
3124 dash_stream->moof_sync_samples = NULL;
3125 return FALSE;
3126 }
3127 #endif
3128
3129 if (trun->flags & GST_TRUN_FLAGS_SAMPLE_SIZE_PRESENT) {
3130 prev_sample_end += sample->sample_size;
3131 } else if (traf->
3132 tfhd.flags & GST_TFHD_FLAGS_DEFAULT_SAMPLE_SIZE_PRESENT) {
3133 prev_sample_end += traf->tfhd.default_sample_size;
3134 } else {
3135 GST_FIXME_OBJECT (stream->pad,
3136 "Sample size given by trex - can't download only keyframes");
3137 g_array_free (dash_stream->moof_sync_samples, TRUE);
3138 dash_stream->moof_sync_samples = NULL;
3139 dashdemux->allow_trickmode_key_units = FALSE;
3140 return FALSE;
3141 }
3142
3143 /* Non-non-sync sample aka sync sample */
3144 if (!GST_ISOFF_SAMPLE_FLAGS_SAMPLE_IS_NON_SYNC_SAMPLE (sample_flags) ||
3145 GST_ISOFF_SAMPLE_FLAGS_SAMPLE_DEPENDS_ON (sample_flags) == 2) {
3146 GstDashStreamSyncSample sync_sample =
3147 { sample_offset, prev_sample_end - 1 };
3148 /* TODO: need timestamps so we can decide to download or not */
3149 g_array_append_val (dash_stream->moof_sync_samples, sync_sample);
3150 }
3151 }
3152
3153 prev_trun_end = prev_sample_end;
3154 }
3155
3156 prev_traf_end = prev_trun_end;
3157 }
3158
3159 if (trex_sample_flags) {
3160 if (dash_stream->moof_sync_samples->len > 0) {
3161 GST_LOG_OBJECT (stream->pad,
3162 "Some sample flags given by trex but still found sync samples");
3163 } else {
3164 GST_FIXME_OBJECT (stream->pad,
3165 "Sample flags given by trex - can't download only keyframes");
3166 g_array_free (dash_stream->moof_sync_samples, TRUE);
3167 dash_stream->moof_sync_samples = NULL;
3168 dashdemux->allow_trickmode_key_units = FALSE;
3169 return FALSE;
3170 }
3171 }
3172
3173 if (dash_stream->moof_sync_samples->len == 0) {
3174 GST_LOG_OBJECT (stream->pad, "No sync samples found in fragment");
3175 g_array_free (dash_stream->moof_sync_samples, TRUE);
3176 dash_stream->moof_sync_samples = NULL;
3177 dashdemux->allow_trickmode_key_units = FALSE;
3178 return FALSE;
3179 }
3180
3181 {
3182 GstDashStreamSyncSample *sync_sample;
3183 guint i;
3184 guint size;
3185 GstClockTime current_keyframe_distance;
3186
3187 for (i = 0; i < dash_stream->moof_sync_samples->len; i++) {
3188 sync_sample =
3189 &g_array_index (dash_stream->moof_sync_samples,
3190 GstDashStreamSyncSample, i);
3191 size = sync_sample->end_offset + 1 - sync_sample->start_offset;
3192
3193 if (dash_stream->keyframe_average_size) {
3194 /* Over-estimate the keyframe size */
3195 if (dash_stream->keyframe_average_size < size)
3196 dash_stream->keyframe_average_size =
3197 (size * 3 + dash_stream->keyframe_average_size) / 4;
3198 else
3199 dash_stream->keyframe_average_size =
3200 (size + dash_stream->keyframe_average_size * 3) / 4;
3201 } else {
3202 dash_stream->keyframe_average_size = size;
3203 }
3204
3205 if (i == 0) {
3206 if (dash_stream->moof_offset + dash_stream->moof_size + 8 <
3207 sync_sample->start_offset) {
3208 dash_stream->first_sync_sample_after_moof = FALSE;
3209 dash_stream->first_sync_sample_always_after_moof = FALSE;
3210 } else {
3211 dash_stream->first_sync_sample_after_moof =
3212 (dash_stream->moof_sync_samples->len == 1
3213 || demux->segment.rate > 0.0);
3214 }
3215 }
3216 }
3217
3218 g_assert (stream->fragment.duration != 0);
3219 g_assert (stream->fragment.duration != GST_CLOCK_TIME_NONE);
3220
3221 if (gst_mpd_client_has_isoff_ondemand_profile (dashdemux->client)
3222 && dash_stream->sidx_position != GST_CLOCK_TIME_NONE
3223 && SIDX (dash_stream)->entries) {
3224 GstSidxBoxEntry *entry = SIDX_CURRENT_ENTRY (dash_stream);
3225 current_keyframe_distance =
3226 entry->duration / dash_stream->moof_sync_samples->len;
3227 } else {
3228 current_keyframe_distance =
3229 stream->fragment.duration / dash_stream->moof_sync_samples->len;
3230 }
3231 dash_stream->current_fragment_keyframe_distance = current_keyframe_distance;
3232
3233 if (dash_stream->keyframe_average_distance) {
3234 /* Under-estimate the keyframe distance */
3235 if (dash_stream->keyframe_average_distance > current_keyframe_distance)
3236 dash_stream->keyframe_average_distance =
3237 (dash_stream->keyframe_average_distance * 3 +
3238 current_keyframe_distance) / 4;
3239 else
3240 dash_stream->keyframe_average_distance =
3241 (dash_stream->keyframe_average_distance +
3242 current_keyframe_distance * 3) / 4;
3243 } else {
3244 dash_stream->keyframe_average_distance = current_keyframe_distance;
3245 }
3246
3247 GST_DEBUG_OBJECT (stream->pad,
3248 "average keyframe sample size: %" G_GUINT64_FORMAT,
3249 dash_stream->keyframe_average_size);
3250 GST_DEBUG_OBJECT (stream->pad,
3251 "average keyframe distance: %" GST_TIME_FORMAT " (%" GST_TIME_FORMAT
3252 ")", GST_TIME_ARGS (dash_stream->keyframe_average_distance),
3253 GST_TIME_ARGS (current_keyframe_distance));
3254 GST_DEBUG_OBJECT (stream->pad, "first sync sample after moof: %d",
3255 dash_stream->first_sync_sample_after_moof);
3256 }
3257
3258 return TRUE;
3259 }
3260
3261
3262 static GstFlowReturn
gst_dash_demux_handle_isobmff(GstAdaptiveDemux * demux,GstAdaptiveDemuxStream * stream)3263 gst_dash_demux_handle_isobmff (GstAdaptiveDemux * demux,
3264 GstAdaptiveDemuxStream * stream)
3265 {
3266 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3267 GstFlowReturn ret = GST_FLOW_OK;
3268 GstBuffer *buffer;
3269 gboolean sidx_advance = FALSE;
3270
3271 /* We parse all ISOBMFF boxes of a (sub)fragment until the mdat. This covers
3272 * at least moov, moof and sidx boxes. Once mdat is received we just output
3273 * everything until the next (sub)fragment */
3274 if (dash_stream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT) {
3275 gboolean sidx_seek_needed = FALSE;
3276
3277 ret = gst_dash_demux_parse_isobmff (demux, dash_stream, &sidx_seek_needed);
3278 if (ret != GST_FLOW_OK)
3279 return ret;
3280
3281 /* Go to selected segment if needed here */
3282 if (sidx_seek_needed && !stream->downloading_index)
3283 return GST_ADAPTIVE_DEMUX_FLOW_END_OF_FRAGMENT;
3284
3285 /* No mdat yet, let's get called again with the next boxes */
3286 if (dash_stream->isobmff_parser.current_fourcc != GST_ISOFF_FOURCC_MDAT)
3287 return ret;
3288
3289 /* Here we end up only if we're right at the mdat start */
3290
3291 /* Jump to the next sync sample. As we're doing chunked downloading
3292 * here, just drop data until our chunk is over so we can reuse the
3293 * HTTP connection instead of having to create a new one or
3294 * reuse the data if the sync sample follows the moof */
3295 if (dash_stream->active_stream->mimeType == GST_STREAM_VIDEO
3296 && gst_dash_demux_find_sync_samples (demux, stream) &&
3297 GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)) {
3298 guint idx = -1;
3299
3300 if (GST_CLOCK_TIME_IS_VALID (dash_stream->target_time)) {
3301 idx =
3302 (dash_stream->target_time -
3303 dash_stream->current_fragment_timestamp) /
3304 dash_stream->current_fragment_keyframe_distance;
3305 } else if (stream->segment.rate > 0) {
3306 idx = 0;
3307 }
3308
3309 GST_DEBUG_OBJECT (stream->pad, "target %" GST_TIME_FORMAT " idx %d",
3310 GST_TIME_ARGS (dash_stream->target_time), idx);
3311 /* Figure out target time */
3312
3313 if (dash_stream->first_sync_sample_after_moof && idx == 0) {
3314 /* If we're here, don't throw away data but collect sync
3315 * sample while we're at it below. We're doing chunked
3316 * downloading so might need to adjust the next chunk size for
3317 * the remainder */
3318 dash_stream->current_sync_sample = 0;
3319 GST_DEBUG_OBJECT (stream->pad, "Using first keyframe after header");
3320 }
3321 }
3322
3323 if (gst_adapter_available (dash_stream->adapter) == 0)
3324 return ret;
3325
3326 /* We have some data from the mdat available in the adapter, handle it
3327 * below in the push code */
3328 } else {
3329 /* Somewhere in the middle of the mdat */
3330 }
3331
3332 /* At mdat */
3333 if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
3334 guint64 sidx_end_offset =
3335 dash_stream->sidx_base_offset +
3336 SIDX_CURRENT_ENTRY (dash_stream)->offset +
3337 SIDX_CURRENT_ENTRY (dash_stream)->size;
3338 gboolean has_next = gst_dash_demux_stream_has_next_subfragment (stream);
3339 gsize available;
3340
3341 /* Need to handle everything in the adapter according to the parsed SIDX
3342 * and advance subsegments accordingly */
3343
3344 available = gst_adapter_available (dash_stream->adapter);
3345 if (dash_stream->current_offset + available < sidx_end_offset) {
3346 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
3347 } else {
3348 if (!has_next && sidx_end_offset <= dash_stream->current_offset) {
3349 /* Drain all bytes, since there might be trailing bytes at the end of subfragment */
3350 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
3351 } else {
3352 if (sidx_end_offset <= dash_stream->current_offset) {
3353 /* This means a corrupted stream or a bug: ignoring bugs, it
3354 * should only happen if the SIDX index is corrupt */
3355 GST_ERROR_OBJECT (stream->pad, "Invalid SIDX state");
3356 gst_adapter_clear (dash_stream->adapter);
3357 return GST_FLOW_ERROR;
3358 } else {
3359 buffer =
3360 gst_adapter_take_buffer (dash_stream->adapter,
3361 sidx_end_offset - dash_stream->current_offset);
3362 sidx_advance = TRUE;
3363 }
3364 }
3365 }
3366 } else {
3367 /* Take it all and handle it further below */
3368 buffer =
3369 gst_adapter_take_buffer (dash_stream->adapter,
3370 gst_adapter_available (dash_stream->adapter));
3371
3372 /* Attention: All code paths below need to update dash_stream->current_offset */
3373 }
3374
3375 /* We're actually running in key-units trick mode */
3376 if (dash_stream->active_stream->mimeType == GST_STREAM_VIDEO
3377 && dash_stream->moof_sync_samples
3378 && GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS (stream->demux)) {
3379 if (dash_stream->current_sync_sample == -1) {
3380 /* We're doing chunked downloading and wait for finishing the current
3381 * chunk so we can jump to the first keyframe */
3382 dash_stream->current_offset += gst_buffer_get_size (buffer);
3383 gst_buffer_unref (buffer);
3384 return GST_FLOW_OK;
3385 } else {
3386 GstDashStreamSyncSample *sync_sample =
3387 &g_array_index (dash_stream->moof_sync_samples,
3388 GstDashStreamSyncSample, dash_stream->current_sync_sample);
3389 guint64 end_offset =
3390 dash_stream->current_offset + gst_buffer_get_size (buffer);
3391
3392 /* Make sure to not download too much, this should only happen for
3393 * the very first keyframe if it follows the moof */
3394 if (dash_stream->current_offset >= sync_sample->end_offset + 1) {
3395 dash_stream->current_offset += gst_buffer_get_size (buffer);
3396 gst_buffer_unref (buffer);
3397 return GST_FLOW_OK;
3398 } else if (end_offset > sync_sample->end_offset + 1) {
3399 guint64 remaining =
3400 sync_sample->end_offset + 1 - dash_stream->current_offset;
3401 GstBuffer *sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0,
3402 remaining);
3403 gst_buffer_unref (buffer);
3404 buffer = sub;
3405 }
3406 }
3407 }
3408
3409 GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
3410 dash_stream->current_offset += gst_buffer_get_size (buffer);
3411 GST_BUFFER_OFFSET_END (buffer) = dash_stream->current_offset;
3412
3413 ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
3414 if (ret != GST_FLOW_OK)
3415 return ret;
3416
3417 if (sidx_advance) {
3418 ret =
3419 gst_adaptive_demux_stream_advance_fragment (demux, stream,
3420 SIDX_CURRENT_ENTRY (dash_stream)->duration);
3421 if (ret != GST_FLOW_OK)
3422 return ret;
3423
3424 /* If we still have data available, recurse and use it up if possible */
3425 if (gst_adapter_available (dash_stream->adapter) > 0)
3426 return gst_dash_demux_handle_isobmff (demux, stream);
3427 }
3428
3429 return ret;
3430 }
3431
3432 static GstFlowReturn
gst_dash_demux_data_received(GstAdaptiveDemux * demux,GstAdaptiveDemuxStream * stream,GstBuffer * buffer)3433 gst_dash_demux_data_received (GstAdaptiveDemux * demux,
3434 GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
3435 {
3436 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3437 GstFlowReturn ret = GST_FLOW_OK;
3438 guint index_header_or_data;
3439
3440 if (stream->downloading_index)
3441 index_header_or_data = 1;
3442 else if (stream->downloading_header)
3443 index_header_or_data = 2;
3444 else
3445 index_header_or_data = 3;
3446
3447 if (dash_stream->current_index_header_or_data != index_header_or_data) {
3448 /* Clear pending data */
3449 if (gst_adapter_available (dash_stream->adapter) != 0)
3450 GST_ERROR_OBJECT (stream->pad,
3451 "Had pending SIDX data after switch between index/header/data");
3452 gst_adapter_clear (dash_stream->adapter);
3453 dash_stream->current_index_header_or_data = index_header_or_data;
3454 dash_stream->current_offset = -1;
3455 }
3456
3457 if (dash_stream->current_offset == -1)
3458 dash_stream->current_offset =
3459 GST_BUFFER_OFFSET_IS_VALID (buffer) ? GST_BUFFER_OFFSET (buffer) : 0;
3460
3461 gst_adapter_push (dash_stream->adapter, buffer);
3462 buffer = NULL;
3463
3464 if (dash_stream->is_isobmff || stream->downloading_index) {
3465 /* SIDX index is also ISOBMMF */
3466 ret = gst_dash_demux_handle_isobmff (demux, stream);
3467 } else if (dash_stream->sidx_parser.status == GST_ISOFF_SIDX_PARSER_FINISHED) {
3468 gsize available;
3469
3470 /* Not ISOBMFF but had a SIDX index. Does this even exist or work? */
3471 while (ret == GST_FLOW_OK
3472 && ((available = gst_adapter_available (dash_stream->adapter)) > 0)) {
3473 gboolean advance = FALSE;
3474 guint64 sidx_end_offset =
3475 dash_stream->sidx_base_offset +
3476 SIDX_CURRENT_ENTRY (dash_stream)->offset +
3477 SIDX_CURRENT_ENTRY (dash_stream)->size;
3478 gboolean has_next = gst_dash_demux_stream_has_next_subfragment (stream);
3479
3480 if (dash_stream->current_offset + available < sidx_end_offset) {
3481 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
3482 } else {
3483 if (!has_next && sidx_end_offset <= dash_stream->current_offset) {
3484 /* Drain all bytes, since there might be trailing bytes at the end of subfragment */
3485 buffer = gst_adapter_take_buffer (dash_stream->adapter, available);
3486 } else {
3487 if (sidx_end_offset <= dash_stream->current_offset) {
3488 /* This means a corrupted stream or a bug: ignoring bugs, it
3489 * should only happen if the SIDX index is corrupt */
3490 GST_ERROR_OBJECT (stream->pad, "Invalid SIDX state");
3491 gst_adapter_clear (dash_stream->adapter);
3492 ret = GST_FLOW_ERROR;
3493 break;
3494 } else {
3495 buffer =
3496 gst_adapter_take_buffer (dash_stream->adapter,
3497 sidx_end_offset - dash_stream->current_offset);
3498 advance = TRUE;
3499 }
3500 }
3501 }
3502
3503 GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
3504 GST_BUFFER_OFFSET_END (buffer) =
3505 GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer);
3506 dash_stream->current_offset = GST_BUFFER_OFFSET_END (buffer);
3507
3508 ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
3509
3510 if (advance) {
3511 if (has_next) {
3512 GstFlowReturn new_ret;
3513 new_ret =
3514 gst_adaptive_demux_stream_advance_fragment (demux, stream,
3515 SIDX_CURRENT_ENTRY (dash_stream)->duration);
3516
3517 /* only overwrite if it was OK before */
3518 if (ret == GST_FLOW_OK)
3519 ret = new_ret;
3520 } else {
3521 break;
3522 }
3523 }
3524 }
3525 } else {
3526 /* this should be the main header, just push it all */
3527 buffer = gst_adapter_take_buffer (dash_stream->adapter,
3528 gst_adapter_available (dash_stream->adapter));
3529
3530 GST_BUFFER_OFFSET (buffer) = dash_stream->current_offset;
3531 GST_BUFFER_OFFSET_END (buffer) =
3532 GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer);
3533 dash_stream->current_offset = GST_BUFFER_OFFSET_END (buffer);
3534
3535 ret = gst_adaptive_demux_stream_push_buffer (stream, buffer);
3536 }
3537
3538 return ret;
3539 }
3540
3541 static void
gst_dash_demux_stream_free(GstAdaptiveDemuxStream * stream)3542 gst_dash_demux_stream_free (GstAdaptiveDemuxStream * stream)
3543 {
3544 GstDashDemuxStream *dash_stream = (GstDashDemuxStream *) stream;
3545
3546 gst_isoff_sidx_parser_clear (&dash_stream->sidx_parser);
3547 if (dash_stream->adapter)
3548 g_object_unref (dash_stream->adapter);
3549 if (dash_stream->moof)
3550 gst_isoff_moof_box_free (dash_stream->moof);
3551 if (dash_stream->moof_sync_samples)
3552 g_array_free (dash_stream->moof_sync_samples, TRUE);
3553 }
3554
3555 static GstDashDemuxClockDrift *
gst_dash_demux_clock_drift_new(GstDashDemux * demux)3556 gst_dash_demux_clock_drift_new (GstDashDemux * demux)
3557 {
3558 GstDashDemuxClockDrift *clock_drift;
3559
3560 clock_drift = g_slice_new0 (GstDashDemuxClockDrift);
3561 g_mutex_init (&clock_drift->clock_lock);
3562 clock_drift->next_update =
3563 GST_TIME_AS_USECONDS (gst_adaptive_demux_get_monotonic_time
3564 (GST_ADAPTIVE_DEMUX_CAST (demux)));
3565 return clock_drift;
3566 }
3567
3568 static void
gst_dash_demux_clock_drift_free(GstDashDemuxClockDrift * clock_drift)3569 gst_dash_demux_clock_drift_free (GstDashDemuxClockDrift * clock_drift)
3570 {
3571 if (clock_drift) {
3572 g_mutex_lock (&clock_drift->clock_lock);
3573 if (clock_drift->ntp_clock)
3574 g_object_unref (clock_drift->ntp_clock);
3575 g_mutex_unlock (&clock_drift->clock_lock);
3576 g_mutex_clear (&clock_drift->clock_lock);
3577 g_slice_free (GstDashDemuxClockDrift, clock_drift);
3578 }
3579 }
3580
3581 /*
3582 * The value attribute of the UTCTiming element contains a white-space
3583 * separated list of servers that are recommended to be used in
3584 * combination with the NTP protocol as defined in IETF RFC 5905 for
3585 * getting the appropriate time.
3586 *
3587 * The DASH standard does not specify which version of NTP. This
3588 * function only works with NTPv4 servers.
3589 */
3590 static GstDateTime *
gst_dash_demux_poll_ntp_server(GstDashDemuxClockDrift * clock_drift,gchar ** urls)3591 gst_dash_demux_poll_ntp_server (GstDashDemuxClockDrift * clock_drift,
3592 gchar ** urls)
3593 {
3594 GstClockTime ntp_clock_time;
3595 GDateTime *dt, *dt2;
3596
3597 if (!clock_drift->ntp_clock) {
3598 GResolver *resolver;
3599 GList *inet_addrs;
3600 GError *err;
3601 gchar *ip_addr;
3602
3603 resolver = g_resolver_get_default ();
3604 /* We don't round-robin NTP servers. If the manifest specifies multiple
3605 NTP time servers, select one at random */
3606 clock_drift->selected_url = g_random_int_range (0, g_strv_length (urls));
3607 GST_DEBUG ("Connecting to NTP time server %s",
3608 urls[clock_drift->selected_url]);
3609 inet_addrs = g_resolver_lookup_by_name (resolver,
3610 urls[clock_drift->selected_url], NULL, &err);
3611 g_object_unref (resolver);
3612 if (!inet_addrs || g_list_length (inet_addrs) == 0) {
3613 GST_ERROR ("Failed to resolve hostname of NTP server: %s",
3614 err ? (err->message) : "unknown error");
3615 if (inet_addrs)
3616 g_resolver_free_addresses (inet_addrs);
3617 if (err)
3618 g_error_free (err);
3619 return NULL;
3620 }
3621 ip_addr =
3622 g_inet_address_to_string ((GInetAddress
3623 *) (g_list_first (inet_addrs)->data));
3624 clock_drift->ntp_clock = gst_ntp_clock_new ("dashntp", ip_addr, 123, 0);
3625 g_free (ip_addr);
3626 g_resolver_free_addresses (inet_addrs);
3627 if (!clock_drift->ntp_clock) {
3628 GST_ERROR ("Failed to create NTP clock");
3629 return NULL;
3630 }
3631 if (!gst_clock_wait_for_sync (clock_drift->ntp_clock, 5 * GST_SECOND)) {
3632 g_object_unref (clock_drift->ntp_clock);
3633 clock_drift->ntp_clock = NULL;
3634 GST_ERROR ("Failed to lock to NTP clock");
3635 return NULL;
3636 }
3637 }
3638 ntp_clock_time = gst_clock_get_time (clock_drift->ntp_clock);
3639 if (ntp_clock_time == GST_CLOCK_TIME_NONE) {
3640 GST_ERROR ("Failed to get time from NTP clock");
3641 return NULL;
3642 }
3643 ntp_clock_time -= NTP_TO_UNIX_EPOCH * GST_SECOND;
3644 dt = g_date_time_new_from_unix_utc (ntp_clock_time / GST_SECOND);
3645 if (!dt) {
3646 GST_ERROR ("Failed to create GstDateTime");
3647 return NULL;
3648 }
3649 ntp_clock_time =
3650 gst_util_uint64_scale (ntp_clock_time % GST_SECOND, 1000000, GST_SECOND);
3651 dt2 = g_date_time_add (dt, ntp_clock_time);
3652 g_date_time_unref (dt);
3653 return gst_date_time_new_from_g_date_time (dt2);
3654 }
3655
3656 struct Rfc5322TimeZone
3657 {
3658 const gchar *name;
3659 gfloat tzoffset;
3660 };
3661
3662 /*
3663 Parse an RFC5322 (section 3.3) date-time from the Date: field in the
3664 HTTP response.
3665 See https://tools.ietf.org/html/rfc5322#section-3.3
3666 */
3667 static GstDateTime *
gst_dash_demux_parse_http_head(GstDashDemuxClockDrift * clock_drift,GstFragment * download)3668 gst_dash_demux_parse_http_head (GstDashDemuxClockDrift * clock_drift,
3669 GstFragment * download)
3670 {
3671 static const gchar *months[] = { NULL, "Jan", "Feb", "Mar", "Apr",
3672 "May", "Jun", "Jul", "Aug",
3673 "Sep", "Oct", "Nov", "Dec", NULL
3674 };
3675 static const struct Rfc5322TimeZone timezones[] = {
3676 {"Z", 0},
3677 {"UT", 0},
3678 {"GMT", 0},
3679 {"BST", 1},
3680 {"EST", -5},
3681 {"EDT", -4},
3682 {"CST", -6},
3683 {"CDT", -5},
3684 {"MST", -7},
3685 {"MDT", -6},
3686 {"PST", -8},
3687 {"PDT", -7},
3688 {NULL, 0}
3689 };
3690 GstDateTime *value = NULL;
3691 const GstStructure *response_headers;
3692 const gchar *http_date;
3693 const GValue *val;
3694 gint ret;
3695 const gchar *pos;
3696 gint year = -1, month = -1, day = -1, hour = -1, minute = -1, second = -1;
3697 gchar zone[6];
3698 gchar monthstr[4];
3699 gfloat tzoffset = 0;
3700 gboolean parsed_tz = FALSE;
3701
3702 g_return_val_if_fail (download != NULL, NULL);
3703 g_return_val_if_fail (download->headers != NULL, NULL);
3704
3705 val = gst_structure_get_value (download->headers, "response-headers");
3706 if (!val) {
3707 return NULL;
3708 }
3709 response_headers = gst_value_get_structure (val);
3710 http_date = gst_structure_get_string (response_headers, "Date");
3711 if (!http_date) {
3712 return NULL;
3713 }
3714
3715 /* skip optional text version of day of the week */
3716 pos = strchr (http_date, ',');
3717 if (pos)
3718 pos++;
3719 else
3720 pos = http_date;
3721 ret =
3722 sscanf (pos, "%02d %3s %04d %02d:%02d:%02d %5s", &day, monthstr, &year,
3723 &hour, &minute, &second, zone);
3724 if (ret == 7) {
3725 gchar *z = zone;
3726 gint i;
3727
3728 for (i = 1; months[i]; ++i) {
3729 if (g_ascii_strncasecmp (months[i], monthstr, strlen (months[i])) == 0) {
3730 month = i;
3731 break;
3732 }
3733 }
3734 for (i = 0; timezones[i].name && !parsed_tz; ++i) {
3735 if (g_ascii_strncasecmp (timezones[i].name, z,
3736 strlen (timezones[i].name)) == 0) {
3737 tzoffset = timezones[i].tzoffset;
3738 parsed_tz = TRUE;
3739 }
3740 }
3741 if (!parsed_tz) {
3742 gint hh, mm;
3743 gboolean neg = FALSE;
3744 /* check if it is in the form +-HHMM */
3745 if (*z == '+' || *z == '-') {
3746 if (*z == '+')
3747 ++z;
3748 else if (*z == '-') {
3749 ++z;
3750 neg = TRUE;
3751 }
3752 ret = sscanf (z, "%02d%02d", &hh, &mm);
3753 if (ret == 2) {
3754 tzoffset = hh;
3755 tzoffset += mm / 60.0;
3756 if (neg)
3757 tzoffset = -tzoffset;
3758 parsed_tz = TRUE;
3759 }
3760 }
3761 }
3762 /* Accept year in both 2 digit or 4 digit format */
3763 if (year < 100)
3764 year += 2000;
3765 }
3766 if (month > 0 && parsed_tz) {
3767 value = gst_date_time_new (tzoffset,
3768 year, month, day, hour, minute, second);
3769 }
3770 return value;
3771 }
3772
3773 /*
3774 The timing information is contained in the message body of the HTTP
3775 response and contains a time value formatted according to NTP timestamp
3776 format in IETF RFC 5905.
3777
3778 0 1 2 3
3779 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
3780 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3781 | Seconds |
3782 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3783 | Fraction |
3784 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3785
3786 NTP Timestamp Format
3787 */
3788 static GstDateTime *
gst_dash_demux_parse_http_ntp(GstDashDemuxClockDrift * clock_drift,GstBuffer * buffer)3789 gst_dash_demux_parse_http_ntp (GstDashDemuxClockDrift * clock_drift,
3790 GstBuffer * buffer)
3791 {
3792 gint64 seconds;
3793 guint64 fraction;
3794 GDateTime *dt, *dt2;
3795 GstMapInfo mapinfo;
3796
3797 /* See https://tools.ietf.org/html/rfc5905#page-12 for details of
3798 the NTP Timestamp Format */
3799 gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
3800 if (mapinfo.size != 8) {
3801 gst_buffer_unmap (buffer, &mapinfo);
3802 return NULL;
3803 }
3804 seconds = GST_READ_UINT32_BE (mapinfo.data);
3805 fraction = GST_READ_UINT32_BE (mapinfo.data + 4);
3806 gst_buffer_unmap (buffer, &mapinfo);
3807 fraction = gst_util_uint64_scale (fraction, 1000000,
3808 G_GUINT64_CONSTANT (1) << 32);
3809 /* subtract constant to convert from 1900 based time to 1970 based time */
3810 seconds -= NTP_TO_UNIX_EPOCH;
3811 dt = g_date_time_new_from_unix_utc (seconds);
3812 dt2 = g_date_time_add (dt, fraction);
3813 g_date_time_unref (dt);
3814 return gst_date_time_new_from_g_date_time (dt2);
3815 }
3816
3817 /*
3818 The timing information is contained in the message body of the
3819 HTTP response and contains a time value formatted according to
3820 xs:dateTime as defined in W3C XML Schema Part 2: Datatypes specification.
3821 */
3822 static GstDateTime *
gst_dash_demux_parse_http_xsdate(GstDashDemuxClockDrift * clock_drift,GstBuffer * buffer)3823 gst_dash_demux_parse_http_xsdate (GstDashDemuxClockDrift * clock_drift,
3824 GstBuffer * buffer)
3825 {
3826 GstDateTime *value = NULL;
3827 GstMapInfo mapinfo;
3828
3829 /* the string from the server might not be zero terminated */
3830 if (gst_buffer_map (buffer, &mapinfo, GST_MAP_READ)) {
3831 gchar *str;
3832 str = g_strndup ((const gchar *) mapinfo.data, mapinfo.size);
3833 gst_buffer_unmap (buffer, &mapinfo);
3834 value = gst_date_time_new_from_iso8601_string (str);
3835 g_free (str);
3836 }
3837 return value;
3838 }
3839
3840 static gboolean
gst_dash_demux_poll_clock_drift(GstDashDemux * demux)3841 gst_dash_demux_poll_clock_drift (GstDashDemux * demux)
3842 {
3843 GstDashDemuxClockDrift *clock_drift;
3844 GDateTime *start = NULL, *end;
3845 GstBuffer *buffer = NULL;
3846 GstDateTime *value = NULL;
3847 gboolean ret = FALSE;
3848 gint64 now;
3849 GstMPDUTCTimingType method;
3850 gchar **urls;
3851
3852 g_return_val_if_fail (demux != NULL, FALSE);
3853 g_return_val_if_fail (demux->clock_drift != NULL, FALSE);
3854 clock_drift = demux->clock_drift;
3855 now =
3856 GST_TIME_AS_USECONDS (gst_adaptive_demux_get_monotonic_time
3857 (GST_ADAPTIVE_DEMUX_CAST (demux)));
3858 if (now < clock_drift->next_update) {
3859 /*TODO: If a fragment fails to download in adaptivedemux, it waits
3860 for a manifest reload before another attempt to fetch a fragment.
3861 Section 10.8.6 of the DVB-DASH standard states that the DASH client
3862 shall refresh the manifest and resynchronise to one of the time sources.
3863
3864 Currently the fact that the manifest refresh follows a download failure
3865 does not make it into dashdemux. */
3866 return TRUE;
3867 }
3868 urls = gst_mpd_client_get_utc_timing_sources (demux->client,
3869 SUPPORTED_CLOCK_FORMATS, &method);
3870 if (!urls) {
3871 return FALSE;
3872 }
3873 /* Update selected_url just in case the number of URLs in the UTCTiming
3874 element has shrunk since the last poll */
3875 clock_drift->selected_url = clock_drift->selected_url % g_strv_length (urls);
3876 g_mutex_lock (&clock_drift->clock_lock);
3877
3878 if (method == GST_MPD_UTCTIMING_TYPE_NTP) {
3879 value = gst_dash_demux_poll_ntp_server (clock_drift, urls);
3880 if (!value) {
3881 GST_ERROR_OBJECT (demux, "Failed to fetch time from NTP server %s",
3882 urls[clock_drift->selected_url]);
3883 g_mutex_unlock (&clock_drift->clock_lock);
3884 goto quit;
3885 }
3886 }
3887 start =
3888 gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3889 if (!value) {
3890 GstFragment *download;
3891 gint64 range_start = 0, range_end = -1;
3892 GST_DEBUG_OBJECT (demux, "Fetching current time from %s",
3893 urls[clock_drift->selected_url]);
3894 if (method == GST_MPD_UTCTIMING_TYPE_HTTP_HEAD) {
3895 range_start = -1;
3896 }
3897 download =
3898 gst_uri_downloader_fetch_uri_with_range (GST_ADAPTIVE_DEMUX_CAST
3899 (demux)->downloader, urls[clock_drift->selected_url], NULL, TRUE, TRUE,
3900 TRUE, range_start, range_end, NULL);
3901 if (download) {
3902 if (method == GST_MPD_UTCTIMING_TYPE_HTTP_HEAD && download->headers) {
3903 value = gst_dash_demux_parse_http_head (clock_drift, download);
3904 } else {
3905 buffer = gst_fragment_get_buffer (download);
3906 }
3907 g_object_unref (download);
3908 }
3909 }
3910 g_mutex_unlock (&clock_drift->clock_lock);
3911 if (!value && !buffer) {
3912 GST_ERROR_OBJECT (demux, "Failed to fetch time from %s",
3913 urls[clock_drift->selected_url]);
3914 goto quit;
3915 }
3916 end = gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3917 if (!value && method == GST_MPD_UTCTIMING_TYPE_HTTP_NTP) {
3918 value = gst_dash_demux_parse_http_ntp (clock_drift, buffer);
3919 } else if (!value) {
3920 /* GST_MPD_UTCTIMING_TYPE_HTTP_XSDATE or GST_MPD_UTCTIMING_TYPE_HTTP_ISO */
3921 value = gst_dash_demux_parse_http_xsdate (clock_drift, buffer);
3922 }
3923 if (buffer)
3924 gst_buffer_unref (buffer);
3925 if (value) {
3926 GTimeSpan download_duration = g_date_time_difference (end, start);
3927 GDateTime *client_now, *server_now;
3928 /* We don't know when the server sampled its clock, but we know
3929 it must have been before "end" and probably after "start".
3930 A reasonable estimate is to use (start+end)/2
3931 */
3932 client_now = g_date_time_add (start, download_duration / 2);
3933 server_now = gst_date_time_to_g_date_time (value);
3934 /* If gst_date_time_new_from_iso8601_string is given an unsupported
3935 ISO 8601 format, it can return a GstDateTime that is not valid,
3936 which causes gst_date_time_to_g_date_time to return NULL */
3937 if (server_now) {
3938 g_mutex_lock (&clock_drift->clock_lock);
3939 clock_drift->clock_compensation =
3940 g_date_time_difference (server_now, client_now);
3941 g_mutex_unlock (&clock_drift->clock_lock);
3942 GST_DEBUG_OBJECT (demux,
3943 "Difference between client and server clocks is %lfs",
3944 ((double) clock_drift->clock_compensation) / 1000000.0);
3945 g_date_time_unref (server_now);
3946 ret = TRUE;
3947 } else {
3948 GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
3949 }
3950 g_date_time_unref (client_now);
3951 gst_date_time_unref (value);
3952 } else {
3953 GST_ERROR_OBJECT (demux, "Failed to parse DateTime from server");
3954 }
3955 g_date_time_unref (end);
3956 quit:
3957 if (start)
3958 g_date_time_unref (start);
3959 /* if multiple URLs were specified, use a simple round-robin to
3960 poll each server */
3961 g_mutex_lock (&clock_drift->clock_lock);
3962 if (method == GST_MPD_UTCTIMING_TYPE_NTP) {
3963 clock_drift->next_update = now + FAST_CLOCK_UPDATE_INTERVAL;
3964 } else {
3965 clock_drift->selected_url =
3966 (1 + clock_drift->selected_url) % g_strv_length (urls);
3967 if (ret) {
3968 clock_drift->next_update = now + SLOW_CLOCK_UPDATE_INTERVAL;
3969 } else {
3970 clock_drift->next_update = now + FAST_CLOCK_UPDATE_INTERVAL;
3971 }
3972 }
3973 g_mutex_unlock (&clock_drift->clock_lock);
3974 return ret;
3975 }
3976
3977 static GTimeSpan
gst_dash_demux_get_clock_compensation(GstDashDemux * demux)3978 gst_dash_demux_get_clock_compensation (GstDashDemux * demux)
3979 {
3980 GTimeSpan rv = 0;
3981 if (demux->clock_drift) {
3982 g_mutex_lock (&demux->clock_drift->clock_lock);
3983 rv = demux->clock_drift->clock_compensation;
3984 g_mutex_unlock (&demux->clock_drift->clock_lock);
3985 }
3986 GST_LOG_OBJECT (demux, "Clock drift %" GST_STIME_FORMAT, GST_STIME_ARGS (rv));
3987 return rv;
3988 }
3989
3990 static GDateTime *
gst_dash_demux_get_server_now_utc(GstDashDemux * demux)3991 gst_dash_demux_get_server_now_utc (GstDashDemux * demux)
3992 {
3993 GDateTime *client_now;
3994 GDateTime *server_now;
3995
3996 client_now =
3997 gst_adaptive_demux_get_client_now_utc (GST_ADAPTIVE_DEMUX_CAST (demux));
3998 server_now =
3999 g_date_time_add (client_now,
4000 gst_dash_demux_get_clock_compensation (demux));
4001 g_date_time_unref (client_now);
4002 return server_now;
4003 }
4004