Debian Codegraph @

ext/adaptivedemux2/hls/gsthlsdemux-playlist-loader.c

raw
1 /* GStreamer
2 *
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4 * Author: Thiago Santos <[email protected]>
5 *
6 * Copyright (C) 2021-2022 Centricular Ltd
7 * Author: Edward Hervey <[email protected]>
8 * Author: Jan Schmidt <[email protected]>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "gsthlsdemux.h"
31 #include "gsthlsdemux-playlist-loader.h"
32 #include "m3u8.h"
33
34 GST_DEBUG_CATEGORY_EXTERN (gst_hls_demux2_debug);
35 #define GST_CAT_DEFAULT gst_hls_demux2_debug
36
37 #define MAX_DOWNLOAD_ERROR_COUNT 3
38
39 typedef enum _PlaylistLoaderState PlaylistLoaderState;
40
41 enum _PlaylistLoaderState
42 {
43 PLAYLIST_LOADER_STATE_STOPPED = 0,
44 PLAYLIST_LOADER_STATE_STARTING,
45 PLAYLIST_LOADER_STATE_LOADING,
46 PLAYLIST_LOADER_STATE_WAITING,
47 };
48
49 struct _GstHLSDemuxPlaylistLoaderPrivate
50 {
51 GstAdaptiveDemux *demux;
52
53 GstHLSDemuxPlaylistLoaderSuccessCallback success_cb;
54 GstHLSDemuxPlaylistLoaderErrorCallback error_cb;
55 gpointer userdata;
56
57 GstAdaptiveDemuxLoop *scheduler_task;
58 DownloadHelper *download_helper;
59 DownloadRequest *download_request;
60
61 PlaylistLoaderState state;
62 guint pending_cb_id;
63
64 gchar *base_uri;
65 gchar *target_playlist_uri;
66
67 gchar *loading_playlist_uri;
68
69 gboolean delta_merge_failed;
70 gchar *current_playlist_uri;
71 GstHLSMediaPlaylist *current_playlist;
72
73 gchar *current_playlist_redirect_uri;
74
75 guint download_error_count;
76 };
77
78 #define gst_hls_demux_playlist_loader_parent_class parent_class
79 G_DEFINE_TYPE_WITH_PRIVATE (GstHLSDemuxPlaylistLoader,
80 gst_hls_demux_playlist_loader, GST_TYPE_OBJECT);
81
82 static void gst_hls_demux_playlist_loader_finalize (GObject * object);
83 static gboolean gst_hls_demux_playlist_loader_update (GstHLSDemuxPlaylistLoader
84 * pl);
85 static void start_playlist_download (GstHLSDemuxPlaylistLoader * pl,
86 GstHLSDemuxPlaylistLoaderPrivate * priv);
87
88 /* Takes ownership of the loop ref */
89 GstHLSDemuxPlaylistLoader *
90 gst_hls_demux_playlist_loader_new (GstAdaptiveDemux * demux,
91 DownloadHelper * download_helper)
92 {
93 GstHLSDemuxPlaylistLoader *pl =
94 g_object_new (GST_TYPE_HLS_DEMUX_PLAYLIST_LOADER, NULL);
95 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
96
97 priv->demux = demux;
98 priv->scheduler_task = gst_adaptive_demux_get_loop (demux);
99 priv->download_helper = download_helper;
100
101 return pl;
102 }
103
104 void
105 gst_hls_demux_playlist_loader_set_callbacks (GstHLSDemuxPlaylistLoader * pl,
106 GstHLSDemuxPlaylistLoaderSuccessCallback success_cb,
107 GstHLSDemuxPlaylistLoaderErrorCallback error_cb, gpointer userdata)
108 {
109 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
110
111 priv->success_cb = success_cb;
112 priv->error_cb = error_cb;
113 priv->userdata = userdata;
114 }
115
116 static void
117 gst_hls_demux_playlist_loader_class_init (GstHLSDemuxPlaylistLoaderClass *
118 klass)
119 {
120 GObjectClass *gobject_class = (GObjectClass *) klass;
121
122 gobject_class->finalize = gst_hls_demux_playlist_loader_finalize;
123 }
124
125 static void
126 gst_hls_demux_playlist_loader_init (GstHLSDemuxPlaylistLoader * pl)
127 {
128 pl->priv = gst_hls_demux_playlist_loader_get_instance_private (pl);
129 }
130
131 static void
132 gst_hls_demux_playlist_loader_finalize (GObject * object)
133 {
134 GstHLSDemuxPlaylistLoader *pl = GST_HLS_DEMUX_PLAYLIST_LOADER (object);
135 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
136
137 if (priv->pending_cb_id != 0) {
138 gst_adaptive_demux_loop_cancel_call (priv->scheduler_task,
139 priv->pending_cb_id);
140 priv->pending_cb_id = 0;
141 }
142
143 if (priv->download_request) {
144 downloadhelper_cancel_request (priv->download_helper,
145 priv->download_request);
146 download_request_unref (priv->download_request);
147 priv->download_request = NULL;
148 }
149
150 if (priv->scheduler_task)
151 gst_adaptive_demux_loop_unref (priv->scheduler_task);
152
153 g_free (priv->base_uri);
154 g_free (priv->target_playlist_uri);
155 g_free (priv->loading_playlist_uri);
156
157 if (priv->current_playlist)
158 gst_hls_media_playlist_unref (priv->current_playlist);
159 g_free (priv->current_playlist_uri);
160 g_free (priv->current_playlist_redirect_uri);
161
162 G_OBJECT_CLASS (parent_class)->finalize (object);
163 }
164
165 static void
166 schedule_state_update (GstHLSDemuxPlaylistLoader * pl,
167 GstHLSDemuxPlaylistLoaderPrivate * priv)
168 {
169 g_assert (priv->pending_cb_id == 0);
170 priv->pending_cb_id =
171 gst_adaptive_demux_loop_call (priv->scheduler_task,
172 (GSourceFunc) gst_hls_demux_playlist_loader_update,
173 gst_object_ref (pl), (GDestroyNotify) gst_object_unref);
174 }
175
176 static void
177 schedule_next_playlist_load (GstHLSDemuxPlaylistLoader * pl,
178 GstHLSDemuxPlaylistLoaderPrivate * priv, GstClockTime next_load_interval)
179 {
180 /* If we have a valid request time, compute a more accurate download time for
181 * the playlist.
182 *
183 * This better takes into account the time it took to actually get and process
184 * the current playlist.
185 */
186 if (priv->current_playlist
187 && GST_CLOCK_TIME_IS_VALID (priv->current_playlist->request_time)) {
188 GstClockTime now = gst_adaptive_demux2_get_monotonic_time (priv->demux);
189 GstClockTimeDiff load_diff = GST_CLOCK_DIFF (now,
190 priv->current_playlist->request_time + next_load_interval);
191 GST_LOG_OBJECT (pl,
192 "now %" GST_TIME_FORMAT " request_time %" GST_TIME_FORMAT
193 " next_load_interval %" GST_TIME_FORMAT, GST_TIME_ARGS (now),
194 GST_TIME_ARGS (priv->current_playlist->request_time),
195 GST_TIME_ARGS (next_load_interval));
196 if (load_diff < 0) {
197 GST_LOG_OBJECT (pl, "Playlist update already late by %" GST_STIME_FORMAT,
198 GST_STIME_ARGS (load_diff));
199 };
200 next_load_interval = MAX (0, load_diff);
201 }
202
203 GST_LOG_OBJECT (pl, "Scheduling next playlist reload in %" GST_TIME_FORMAT,
204 GST_TIME_ARGS (next_load_interval));
205 g_assert (priv->pending_cb_id == 0);
206 priv->state = PLAYLIST_LOADER_STATE_WAITING;
207 priv->pending_cb_id =
208 gst_adaptive_demux_loop_call_delayed (priv->scheduler_task,
209 next_load_interval,
210 (GSourceFunc) gst_hls_demux_playlist_loader_update,
211 gst_object_ref (pl), (GDestroyNotify) gst_object_unref);
212 }
213
214 void
215 gst_hls_demux_playlist_loader_start (GstHLSDemuxPlaylistLoader * pl)
216 {
217 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
218
219 if (priv->state != PLAYLIST_LOADER_STATE_STOPPED) {
220 GST_LOG_OBJECT (pl, "Already started - state %d", priv->state);
221 return; /* Already active */
222 }
223
224 GST_DEBUG_OBJECT (pl, "Starting playlist loading");
225 priv->state = PLAYLIST_LOADER_STATE_STARTING;
226 schedule_state_update (pl, priv);
227 }
228
229 void
230 gst_hls_demux_playlist_loader_stop (GstHLSDemuxPlaylistLoader * pl)
231 {
232 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
233
234 if (priv->state == PLAYLIST_LOADER_STATE_STOPPED)
235 return; /* Not runnning */
236
237 GST_DEBUG_OBJECT (pl, "Stopping playlist loading");
238
239 if (priv->pending_cb_id != 0) {
240 gst_adaptive_demux_loop_cancel_call (priv->scheduler_task,
241 priv->pending_cb_id);
242 priv->pending_cb_id = 0;
243 }
244
245 if (priv->download_request) {
246 downloadhelper_cancel_request (priv->download_helper,
247 priv->download_request);
248 download_request_unref (priv->download_request);
249 priv->download_request = NULL;
250 }
251
252 priv->state = PLAYLIST_LOADER_STATE_STOPPED;
253 }
254
255 void
256 gst_hls_demux_playlist_loader_set_playlist_uri (GstHLSDemuxPlaylistLoader * pl,
257 const gchar * base_uri, const gchar * new_playlist_uri)
258 {
259 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
260
261 gboolean playlist_uri_change = (priv->target_playlist_uri == NULL
262 || g_strcmp0 (new_playlist_uri, priv->target_playlist_uri) != 0);
263
264 if (!playlist_uri_change)
265 return;
266
267 GST_DEBUG_OBJECT (pl, "Setting target playlist URI to %s", new_playlist_uri);
268
269 g_free (priv->base_uri);
270 g_free (priv->target_playlist_uri);
271
272 priv->base_uri = g_strdup (base_uri);
273 priv->target_playlist_uri = g_strdup (new_playlist_uri);
274 priv->delta_merge_failed = FALSE;
275
276 switch (priv->state) {
277 case PLAYLIST_LOADER_STATE_STOPPED:
278 return; /* Not runnning */
279 case PLAYLIST_LOADER_STATE_STARTING:
280 case PLAYLIST_LOADER_STATE_LOADING:
281 /* If there's no pending state check, trigger one */
282 if (priv->pending_cb_id == 0) {
283 GST_LOG_OBJECT (pl, "Scheduling state update from state %d",
284 priv->state);
285 schedule_state_update (pl, priv);
286 }
287 break;
288 case PLAYLIST_LOADER_STATE_WAITING:
289 /* Waiting for the next time to load a live playlist, but the playlist has changed so
290 * cancel that and trigger a new one */
291 g_assert (priv->pending_cb_id != 0);
292 gst_adaptive_demux_loop_cancel_call (priv->scheduler_task,
293 priv->pending_cb_id);
294 priv->pending_cb_id = 0;
295 schedule_state_update (pl, priv);
296 break;
297 }
298 }
299
300 /* Check that the current playlist matches the target URI, and return
301 * TRUE if so */
302 gboolean
303 gst_hls_demux_playlist_loader_has_current_uri (GstHLSDemuxPlaylistLoader * pl,
304 const gchar * target_playlist_uri)
305 {
306 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
307
308 if (target_playlist_uri == NULL)
309 target_playlist_uri = priv->target_playlist_uri;
310
311 if (priv->current_playlist == NULL
312 || g_strcmp0 (target_playlist_uri, priv->current_playlist_uri))
313 return FALSE;
314
315 return TRUE;
316 }
317
318 enum PlaylistDownloadParamFlags
319 {
320 PLAYLIST_DOWNLOAD_FLAG_SKIP_V1 = (1 << 0),
321 PLAYLIST_DOWNLOAD_FLAG_SKIP_V2 = (1 << 1), /* V2 also skips date-ranges */
322 PLAYLIST_DOWNLOAD_FLAG_BLOCKING_REQUEST = (1 << 2),
323 };
324
325 struct PlaylistDownloadParams
326 {
327 enum PlaylistDownloadParamFlags flags;
328 gint64 next_msn, next_part;
329 };
330
331 #define HLS_SKIP_QUERY_KEY "_HLS_skip"
332 #define HLS_MSN_QUERY_KEY "_HLS_msn"
333 #define HLS_PART_QUERY_KEY "_HLS_part"
334
335 static gchar *
336 remove_HLS_directives_from_uri (const gchar * playlist_uri)
337 {
338
339 /* Catch the simple case and keep NULL as NULL */
340 if (playlist_uri == NULL)
341 return NULL;
342
343 GstUri *uri = gst_uri_from_string (playlist_uri);
344 gst_uri_remove_query_key (uri, HLS_SKIP_QUERY_KEY);
345 gst_uri_remove_query_key (uri, HLS_MSN_QUERY_KEY);
346 gst_uri_remove_query_key (uri, HLS_PART_QUERY_KEY);
347
348 GList *keys = gst_uri_get_query_keys (uri);
349 if (keys)
350 keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
351 gchar *out_uri = gst_uri_to_string_with_keys (uri, keys);
352 gst_uri_unref (uri);
353
354 return out_uri;
355 }
356
357 static gchar *
358 apply_directives_to_uri (GstHLSDemuxPlaylistLoader * pl,
359 const gchar * playlist_uri, struct PlaylistDownloadParams *dl_params)
360 {
361 /* Short-circuit URI parsing if nothing will change */
362 if (dl_params->flags == 0)
363 return g_strdup (playlist_uri);
364
365 GstUri *uri = gst_uri_from_string (playlist_uri);
366
367 if (dl_params->flags & PLAYLIST_DOWNLOAD_FLAG_SKIP_V1) {
368 GST_LOG_OBJECT (pl, "Doing HLS skip (v1) request");
369 gst_uri_set_query_value (uri, HLS_SKIP_QUERY_KEY, "YES");
370 } else if (dl_params->flags & PLAYLIST_DOWNLOAD_FLAG_SKIP_V2) {
371 GST_LOG_OBJECT (pl, "Doing HLS skip (v2) request");
372 gst_uri_set_query_value (uri, HLS_SKIP_QUERY_KEY, "v2");
373 } else {
374 gst_uri_remove_query_key (uri, HLS_SKIP_QUERY_KEY);
375 }
376
377 if (dl_params->flags & PLAYLIST_DOWNLOAD_FLAG_BLOCKING_REQUEST
378 && dl_params->next_msn != -1) {
379 GST_LOG_OBJECT (pl,
380 "Doing HLS blocking request for URI %s with MSN %" G_GINT64_FORMAT
381 " part %" G_GINT64_FORMAT, playlist_uri, dl_params->next_msn,
382 dl_params->next_part);
383
384 gchar *next_msn_str =
385 g_strdup_printf ("%" G_GINT64_FORMAT, dl_params->next_msn);
386 gst_uri_set_query_value (uri, HLS_MSN_QUERY_KEY, next_msn_str);
387 g_free (next_msn_str);
388
389 if (dl_params->next_part != -1) {
390 gchar *next_part_str =
391 g_strdup_printf ("%" G_GINT64_FORMAT, dl_params->next_part);
392 gst_uri_set_query_value (uri, HLS_PART_QUERY_KEY, next_part_str);
393 g_free (next_part_str);
394 } else {
395 gst_uri_remove_query_key (uri, HLS_PART_QUERY_KEY);
396 }
397 } else {
398 gst_uri_remove_query_key (uri, HLS_MSN_QUERY_KEY);
399 gst_uri_remove_query_key (uri, HLS_PART_QUERY_KEY);
400 }
401
402 /* Produce the resulting URI with query arguments in UTF-8 order
403 * as required by the HLS spec:
404 * `Clients using Delivery Directives (Section 6.2.5) MUST ensure that
405 * all query parameters appear in UTF-8 order within the URI.`
406 */
407 GList *keys = gst_uri_get_query_keys (uri);
408 if (keys)
409 keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
410 gchar *out_uri = gst_uri_to_string_with_keys (uri, keys);
411 gst_uri_unref (uri);
412
413 return out_uri;
414 }
415
416 static GstClockTime
417 get_playlist_reload_interval (GstHLSDemuxPlaylistLoader * pl,
418 GstHLSDemuxPlaylistLoaderPrivate * priv, GstHLSMediaPlaylist * playlist)
419 {
420 if (playlist == NULL)
421 return GST_CLOCK_TIME_NONE; /* No playlist yet */
422
423 /* Use the most recent segment (or part segment) duration, as per
424 * https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis-11#section-6.3.4
425 */
426 GstClockTime target_duration = GST_CLOCK_TIME_NONE;
427 GstClockTime min_reload_interval = playlist->targetduration / 2;
428
429 if (playlist->segments->len) {
430 GstM3U8MediaSegment *last_seg =
431 g_ptr_array_index (playlist->segments, playlist->segments->len - 1);
432
433 if (last_seg->partial_segments) {
434 GstM3U8PartialSegment *last_part =
435 g_ptr_array_index (last_seg->partial_segments,
436 last_seg->partial_segments->len - 1);
437
438 target_duration = last_part->duration;
439 if (GST_CLOCK_TIME_IS_VALID (playlist->partial_targetduration)) {
440 min_reload_interval = playlist->partial_targetduration / 2;
441 } else {
442 min_reload_interval = target_duration / 2;
443 }
444 } else {
445 target_duration = last_seg->duration;
446 min_reload_interval = target_duration / 2;
447 }
448 } else if (GST_CLOCK_TIME_IS_VALID (playlist->partial_targetduration)) {
449 target_duration = playlist->partial_targetduration;
450 min_reload_interval = target_duration / 2;
451 } else if (playlist->version > 5) {
452 target_duration = playlist->targetduration;
453 }
454
455 if (playlist->reloaded && target_duration > min_reload_interval) {
456 GST_DEBUG_OBJECT (pl,
457 "Playlist didn't change previously, returning lower update interval (%"
458 GST_TIME_FORMAT " -> %" GST_TIME_FORMAT ")",
459 GST_TIME_ARGS (target_duration), GST_TIME_ARGS (min_reload_interval));
460 target_duration = min_reload_interval;
461 }
462
463 GST_DEBUG_OBJECT (pl, "Returning target duration %" GST_TIME_FORMAT,
464 GST_TIME_ARGS (target_duration));
465
466 return target_duration;
467 }
468
469 static void
470 handle_download_error (GstHLSDemuxPlaylistLoader * pl,
471 GstHLSDemuxPlaylistLoaderPrivate * priv)
472 {
473 gint max_retries = gst_adaptive_demux_max_retries (priv->demux);
474 if (max_retries >= 0 && ++priv->download_error_count > max_retries) {
475 GST_DEBUG_OBJECT (pl,
476 "Reached %d download failures on URI %s. Reporting the failure",
477 priv->download_error_count, priv->loading_playlist_uri);
478 if (priv->error_cb)
479 priv->error_cb (pl, priv->loading_playlist_uri, priv->userdata);
480 }
481
482 /* The error callback may have provided a new playlist to load, which
483 * will have scheduled a state update immediately. In that case,
484 * don't trigger our own delayed retry */
485 if (priv->pending_cb_id == 0) {
486 GstClockTime delay =
487 gst_adaptive_demux_retry_delay (priv->demux, priv->download_error_count,
488 100 * GST_MSECOND);
489 GST_DEBUG_OBJECT (pl,
490 "Scheduling delayed next playlist download in %" GST_TIMEP_FORMAT,
491 &delay);
492 schedule_next_playlist_load (pl, priv, delay);
493 }
494 }
495
496 static void
497 on_download_complete (DownloadRequest * download, DownloadRequestState state,
498 GstHLSDemuxPlaylistLoader * pl)
499 {
500 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
501
502 if (priv->state != PLAYLIST_LOADER_STATE_LOADING) {
503 GST_DEBUG_OBJECT (pl, "Loader state changed to %d. Aborting", priv->state);
504 return;
505 }
506
507 if (g_strcmp0 (priv->target_playlist_uri, priv->loading_playlist_uri)) {
508 /* This callback happened just as the playlist URI was updated. There should be
509 * a pending state update scheduled, but we can just kick off the new download
510 * immediately */
511 GST_DEBUG_OBJECT (pl,
512 "Target playlist URI changed from %s to %s. Discarding download",
513 priv->loading_playlist_uri, priv->target_playlist_uri);
514 start_playlist_download (pl, priv);
515 return;
516 }
517
518 GST_DEBUG_OBJECT (pl, "Handling completed playlist download for URI %s",
519 download->uri);
520
521 /* If we got a permanent redirect, use that as the new
522 * playlist URI, otherwise set the base URI of the playlist
523 * to the redirect target if any (NULL if there was no redirect) */
524 GstHLSMediaPlaylist *playlist = NULL;
525 gchar *base_uri, *uri;
526
527 if (download->redirect_uri) {
528 /* Strip HLS request params from the playlist and redirect URI */
529 uri = remove_HLS_directives_from_uri (download->redirect_uri);
530 base_uri = NULL;
531
532 if (download->redirect_permanent) {
533 /* Store this redirect as the future request URI for this playlist */
534 g_free (priv->current_playlist_redirect_uri);
535 priv->current_playlist_redirect_uri = g_strdup (uri);
536 }
537 } else {
538 /* Strip HLS request params from the playlist and redirect URI */
539 uri = remove_HLS_directives_from_uri (download->uri);
540 base_uri = remove_HLS_directives_from_uri (download->redirect_uri);
541 }
542
543 /* Calculate the newest time we know this playlist was valid to store on the HLS Media Playlist */
544 GstClockTime playlist_ts =
545 MAX (0, GST_CLOCK_DIFF (download_request_get_age (download),
546 download->download_start_time));
547
548 GstBuffer *buf = download_request_take_buffer (download);
549
550 /* there should be a buf if there wasn't an error (handled above) */
551 g_assert (buf);
552
553 gchar *playlist_data = gst_hls_buf_to_utf8_text (buf);
554 gst_buffer_unref (buf);
555
556 if (playlist_data == NULL) {
557 GST_WARNING_OBJECT (pl, "Couldn't validate playlist encoding");
558 goto error_retry_out;
559 }
560
561 GstHLSMediaPlaylist *current_playlist = priv->current_playlist;
562 gboolean playlist_uri_change = (current_playlist == NULL
563 || g_strcmp0 (priv->loading_playlist_uri,
564 priv->current_playlist_uri) != 0);
565
566 gboolean always_reload = FALSE;
567 #if 0
568 /* Test code the reports a playlist load error if we load
569 the same playlist 2 times in a row and the URI contains "video.m3u8"
570 https://playertest.longtailvideo.com/adaptive/elephants_dream_v4/redundant.m3u8
571 works as a test URL
572 */
573 static gint playlist_load_counter = 0;
574 if (playlist_uri_change)
575 playlist_load_counter = 0;
576 else if (strstr (priv->loading_playlist_uri, "video.m3u8") != NULL) {
577 playlist_load_counter++;
578 if (playlist_load_counter > 1) {
579 g_print ("Triggering playlist failure for %s\n",
580 priv->loading_playlist_uri);
581 goto error_retry_out;
582 }
583 }
584 always_reload = TRUE;
585 #endif
586
587 if (!playlist_uri_change && current_playlist
588 && gst_hls_media_playlist_has_same_data (current_playlist,
589 playlist_data)) {
590 GST_DEBUG_OBJECT (pl, "playlist data was unchanged");
591 playlist = gst_hls_media_playlist_ref (current_playlist);
592 playlist->reloaded = TRUE;
593 playlist->request_time = GST_CLOCK_TIME_NONE;
594 g_free (playlist_data);
595 } else {
596 playlist =
597 gst_hls_media_playlist_parse (playlist_data, playlist_ts, uri,
598 base_uri);
599 if (!playlist) {
600 GST_WARNING_OBJECT (pl, "Couldn't parse playlist");
601 goto error_retry_out;
602 }
603 playlist->request_time = download->download_request_time;
604 }
605
606 /* Transfer over any skipped segments from the current playlist if
607 * we did a delta playlist update */
608 if (!playlist_uri_change && current_playlist && playlist
609 && playlist->skipped_segments > 0) {
610 if (!gst_hls_media_playlist_sync_skipped_segments (playlist,
611 current_playlist)) {
612 GST_DEBUG_OBJECT (pl,
613 "Could not merge delta update to playlist. Retrying with full request");
614
615 gst_hls_media_playlist_unref (playlist);
616
617 /* Delta playlist update failed. Load a full playlist */
618 priv->delta_merge_failed = TRUE;
619 start_playlist_download (pl, priv);
620 goto out;
621 }
622 }
623
624 g_free (priv->current_playlist_uri);
625 if (priv->current_playlist)
626 gst_hls_media_playlist_unref (priv->current_playlist);
627
628 priv->current_playlist_uri = g_strdup (priv->loading_playlist_uri);
629 priv->current_playlist = playlist;
630
631 /* Successfully loaded the playlist. Forget any prior failures */
632 priv->download_error_count = 0;
633
634 if (priv->success_cb)
635 priv->success_cb (pl, priv->current_playlist_uri, priv->current_playlist,
636 priv->userdata);
637
638 g_free (priv->loading_playlist_uri);
639 priv->loading_playlist_uri = NULL;
640
641 if (gst_hls_media_playlist_is_live (playlist) || always_reload) {
642 /* Schedule the next playlist load. If we can do a blocking load,
643 * do it immediately, otherwise delayed */
644 if (playlist->can_block_reload) {
645 start_playlist_download (pl, priv);
646 } else {
647 GstClockTime delay = get_playlist_reload_interval (pl, priv, playlist);
648 schedule_next_playlist_load (pl, priv, delay);
649 }
650 } else {
651 GST_LOG_OBJECT (pl, "Playlist is not live. Not scheduling a reload");
652 /* Go back to the starting state until/if the playlist uri is updated */
653 priv->state = PLAYLIST_LOADER_STATE_STARTING;
654 }
655
656 out:
657 g_free (uri);
658 g_free (base_uri);
659 return;
660
661 error_retry_out:
662 /* Got invalid playlist data, retry soon or error out */
663 handle_download_error (pl, priv);
664 goto out;
665 }
666
667 static void
668 on_download_error (DownloadRequest * download, DownloadRequestState state,
669 GstHLSDemuxPlaylistLoader * pl)
670 {
671 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
672
673 if (priv->state != PLAYLIST_LOADER_STATE_LOADING) {
674 GST_DEBUG_OBJECT (pl, "Loader state changed to %d. Aborting", priv->state);
675 return;
676 }
677
678 GST_WARNING_OBJECT (pl,
679 "Couldn't retrieve playlist, got HTTP status code %d",
680 download->status_code);
681
682 handle_download_error (pl, priv);
683 }
684
685 static void
686 start_playlist_download (GstHLSDemuxPlaylistLoader * pl,
687 GstHLSDemuxPlaylistLoaderPrivate * priv)
688 {
689 gboolean allow_skip = !priv->delta_merge_failed;
690 const gchar *orig_uri = priv->target_playlist_uri;
691
692 /* Can't download yet */
693 if (orig_uri == NULL)
694 return;
695
696 struct PlaylistDownloadParams dl_params;
697 memset (&dl_params, 0, sizeof (struct PlaylistDownloadParams));
698
699 GstHLSMediaPlaylist *current_playlist = priv->current_playlist;
700
701 /* If there's no previous playlist, or the URI changed this
702 * is not a refresh/update but a switch to a new playlist */
703 gboolean playlist_uri_change = (current_playlist == NULL
704 || g_strcmp0 (orig_uri, priv->current_playlist_uri) != 0);
705
706 if (!playlist_uri_change) {
707 GST_LOG_OBJECT (pl, "Updating the playlist");
708
709 /* If we have a redirect stored for this playlist URI, use that instead */
710 if (priv->current_playlist_redirect_uri) {
711 orig_uri = priv->current_playlist_redirect_uri;
712 GST_LOG_OBJECT (pl, "Using redirected playlist URI %s", orig_uri);
713 }
714
715 /* See if we can do a delta playlist update (if the playlist age is less than
716 * one half of the Skip Boundary */
717 if (GST_CLOCK_TIME_IS_VALID (current_playlist->skip_boundary) && allow_skip) {
718 GstClockTime now = gst_adaptive_demux2_get_monotonic_time (priv->demux);
719 GstClockTimeDiff playlist_age =
720 GST_CLOCK_DIFF (current_playlist->playlist_ts, now);
721
722 if (GST_CLOCK_TIME_IS_VALID (current_playlist->playlist_ts) &&
723 playlist_age <= current_playlist->skip_boundary / 2) {
724 if (current_playlist->can_skip_dateranges) {
725 dl_params.flags |= PLAYLIST_DOWNLOAD_FLAG_SKIP_V2;
726 } else {
727 dl_params.flags |= PLAYLIST_DOWNLOAD_FLAG_SKIP_V1;
728 }
729 }
730 } else if (GST_CLOCK_TIME_IS_VALID (current_playlist->skip_boundary)) {
731 GST_DEBUG_OBJECT (pl,
732 "Doing full playlist update after failed delta request");
733 }
734 } else {
735 /* This is the first time loading this playlist URI, clear the error counter
736 * and redirect URI */
737 if (!priv->loading_playlist_uri
738 || g_strcmp0 (orig_uri, priv->loading_playlist_uri))
739 priv->download_error_count = 0;
740 g_free (priv->current_playlist_redirect_uri);
741 priv->current_playlist_redirect_uri = NULL;
742 }
743
744 /* Blocking playlist reload check */
745 if (current_playlist != NULL && current_playlist->can_block_reload) {
746 if (playlist_uri_change) {
747 /* FIXME: We're changing playlist, but if there's a EXT-X-RENDITION-REPORT
748 * for the new playlist we might be able to use it to do a blocking request */
749 } else {
750 /* Get the next MSN (and/or possibly part number) for the request params */
751 gst_hls_media_playlist_get_next_msn_and_part (current_playlist,
752 &dl_params.next_msn, &dl_params.next_part);
753 dl_params.flags |= PLAYLIST_DOWNLOAD_FLAG_BLOCKING_REQUEST;
754 }
755 }
756
757 gchar *target_uri = apply_directives_to_uri (pl, orig_uri, &dl_params);
758
759 if (priv->download_request == NULL) {
760 priv->download_request = download_request_new_uri (target_uri);
761
762 download_request_set_callbacks (priv->download_request,
763 (DownloadRequestEventCallback) on_download_complete,
764 (DownloadRequestEventCallback) on_download_error,
765 (DownloadRequestEventCallback) NULL,
766 (DownloadRequestEventCallback) NULL, pl);
767 } else {
768 download_request_set_uri (priv->download_request, target_uri, 0, -1);
769 }
770
771 GST_DEBUG_OBJECT (pl, "Submitting playlist download request for URI %s",
772 target_uri);
773 g_free (target_uri);
774
775 g_free (priv->loading_playlist_uri);
776 priv->loading_playlist_uri = g_strdup (orig_uri);
777 priv->state = PLAYLIST_LOADER_STATE_LOADING;
778
779 if (!downloadhelper_submit_request (priv->download_helper,
780 NULL, DOWNLOAD_FLAG_COMPRESS | DOWNLOAD_FLAG_FORCE_REFRESH,
781 priv->download_request, NULL)) {
782 /* Failed to submit the download - could be invalid URI, but
783 * could just mean the download helper was stopped */
784 priv->state = PLAYLIST_LOADER_STATE_STOPPED;
785 }
786 }
787
788 static gboolean
789 gst_hls_demux_playlist_loader_update (GstHLSDemuxPlaylistLoader * pl)
790 {
791 GstHLSDemuxPlaylistLoaderPrivate *priv = pl->priv;
792
793 GST_LOG_OBJECT (pl, "Updating at state %d", priv->state);
794 priv->pending_cb_id = 0;
795
796 switch (priv->state) {
797 case PLAYLIST_LOADER_STATE_STOPPED:
798 break;
799 case PLAYLIST_LOADER_STATE_STARTING:
800 if (priv->target_playlist_uri)
801 start_playlist_download (pl, priv);
802 break;
803 case PLAYLIST_LOADER_STATE_LOADING:
804 /* A download is in progress, but if we reach here it's
805 * because the target playlist URI got updated, so check
806 * for cancelling the current download. */
807 if (!g_strcmp0 (priv->target_playlist_uri, priv->current_playlist_uri))
808 break;
809
810 /* A download is in progress. Cancel it and trigger a new one */
811 if (priv->download_request) {
812 GST_DEBUG_OBJECT (pl,
813 "Playlist URI changed from %s to %s. Cancelling current download",
814 priv->target_playlist_uri, priv->current_playlist_uri);
815 downloadhelper_cancel_request (priv->download_helper,
816 priv->download_request);
817 download_request_unref (priv->download_request);
818 priv->download_request = NULL;
819 }
820 start_playlist_download (pl, priv);
821 break;
822 case PLAYLIST_LOADER_STATE_WAITING:
823 /* We were waiting until time to load a playlist. Load it now */
824 start_playlist_download (pl, priv);
825 break;
826 }
827
828 return G_SOURCE_REMOVE;
829 }