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