Debian Codegraph @

ext/adaptivedemux2/hls/gsthlsdemux.c

raw
1 /* GStreamer
2 * Copyright (C) 2010 Marc-Andre Lureau <[email protected]>
3 * Copyright (C) 2010 Andoni Morales Alastruey <[email protected]>
4 * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
5 * Author: Youness Alaoui <[email protected]>, Collabora Ltd.
6 * Author: Sebastian Dröge <[email protected]>, Collabora Ltd.
7 * Copyright (C) 2014 Sebastian Dröge <[email protected]>
8 * Copyright (C) 2015 Tim-Philipp Müller <[email protected]>
9 *
10 * Copyright (C) 2021-2022 Centricular Ltd
11 * Author: Edward Hervey <[email protected]>
12 * Author: Jan Schmidt <[email protected]>
13 *
14 * Gsthlsdemux.c:
15 *
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Library General Public
18 * License as published by the Free Software Foundation; either
19 * version 2 of the License, or (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Library General Public License for more details.
25 *
26 * You should have received a copy of the GNU Library General Public
27 * License along with this library; if not, write to the
28 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
29 * Boston, MA 02110-1301, USA.
30 */
31 /**
32 * SECTION:element-hlsdemux2
33 * @title: hlsdemux2
34 *
35 * HTTP Live Streaming demuxer element.
36 *
37 * ## Example launch line
38 * |[
39 * gst-launch-1.0 playbin3 uri=http://devimages.apple.com/iphone/samples/bipbop/gear4/prog_index.m3u8
40 * ]|
41 *
42 * Since: 1.22
43 */
44
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
48
49 #include <string.h>
50 #include <gst/base/gsttypefindhelper.h>
51 #include <gst/tag/tag.h>
52 #include <glib/gi18n-lib.h>
53
54 /* FIXME: Only needed for scheduler-unlock/lock hack */
55 #include <gstadaptivedemux-private.h>
56
57 #include "gsthlselements.h"
58 #include "gstadaptivedemuxelements.h"
59 #include "gsthlsdemux.h"
60 #include "gsthlsdemux-stream.h"
61
62 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_PAD_SINK,
64 GST_PAD_ALWAYS,
65 GST_STATIC_CAPS ("application/x-hls"));
66
67 GST_DEBUG_CATEGORY (gst_hls_demux2_debug);
68 #define GST_CAT_DEFAULT gst_hls_demux2_debug
69
70 enum
71 {
72 PROP_0,
73
74 PROP_START_BITRATE,
75 };
76
77 #define DEFAULT_START_BITRATE 0
78
79 /* GObject */
80 static void gst_hls_demux_finalize (GObject * obj);
81
82 /* GstElement */
83 static GstStateChangeReturn
84 gst_hls_demux_change_state (GstElement * element, GstStateChange transition);
85
86 /* GstHLSDemux */
87 static GstFlowReturn
88 gst_hls_demux_check_variant_playlist_loaded (GstHLSDemux * demux);
89
90 static gboolean gst_hls_demux_is_live (GstAdaptiveDemux * demux);
91 static GstClockTime gst_hls_demux_get_duration (GstAdaptiveDemux * demux);
92 static gboolean gst_hls_demux_process_initial_manifest (GstAdaptiveDemux *
93 demux, GstBuffer * buf);
94 static GstFlowReturn gst_hls_demux_update_manifest (GstAdaptiveDemux * demux);
95
96 static void gst_hls_prune_time_mappings (GstHLSDemux * demux);
97
98 static gboolean gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
99
100 static gboolean hlsdemux2_element_init (GstPlugin * plugin);
101
102 GST_ELEMENT_REGISTER_DEFINE_CUSTOM (hlsdemux2, hlsdemux2_element_init);
103
104 typedef struct _GstHLSDemux2 GstHLSDemux2;
105 typedef struct _GstHLSDemux2Class GstHLSDemux2Class;
106
107 #define gst_hls_demux2_parent_class parent_class
108 G_DEFINE_TYPE_WITH_CODE (GstHLSDemux2, gst_hls_demux2, GST_TYPE_ADAPTIVE_DEMUX,
109 hls2_element_init ());
110
111 static void gst_hls_demux_reset (GstAdaptiveDemux * demux);
112 static gboolean gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux,
113 gint64 * start, gint64 * stop);
114 static void gst_hls_demux_set_current_variant (GstHLSDemux * hlsdemux,
115 GstHLSVariantStream * variant);
116
117 static void
118 gst_hls_demux_finalize (GObject * obj)
119 {
120 GstHLSDemux *demux = GST_HLS_DEMUX (obj);
121
122 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
123 g_mutex_clear (&demux->keys_lock);
124 if (demux->keys) {
125 g_hash_table_unref (demux->keys);
126 demux->keys = NULL;
127 }
128
129 G_OBJECT_CLASS (parent_class)->finalize (obj);
130 }
131
132 static void
133 gst_hls_demux_set_property (GObject * object, guint prop_id,
134 const GValue * value, GParamSpec * pspec)
135 {
136 GstHLSDemux *demux = GST_HLS_DEMUX (object);
137
138 switch (prop_id) {
139 case PROP_START_BITRATE:
140 demux->start_bitrate = g_value_get_uint (value);
141 break;
142 default:
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 break;
145 }
146 }
147
148 static void
149 gst_hls_demux_get_property (GObject * object, guint prop_id,
150 GValue * value, GParamSpec * pspec)
151 {
152 GstHLSDemux *demux = GST_HLS_DEMUX (object);
153
154 switch (prop_id) {
155 case PROP_START_BITRATE:
156 g_value_set_uint (value, demux->start_bitrate);
157 break;
158 default:
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 break;
161 }
162 }
163
164 static gboolean
165 hlsdemux_requires_periodical_playlist_update_default (GstAdaptiveDemux *
166 demux G_GNUC_UNUSED)
167 {
168 /* We don't need the base class to update our manifest periodically, the
169 * playlist loader for the main stream will do that and trigger
170 * an update manual */
171 return FALSE;
172 }
173
174
175 static void
176 gst_hls_demux2_class_init (GstHLSDemux2Class * klass)
177 {
178 GObjectClass *gobject_class;
179 GstElementClass *element_class;
180 GstAdaptiveDemuxClass *adaptivedemux_class;
181
182 gobject_class = (GObjectClass *) klass;
183 element_class = (GstElementClass *) klass;
184 adaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
185
186 gobject_class->set_property = gst_hls_demux_set_property;
187 gobject_class->get_property = gst_hls_demux_get_property;
188 gobject_class->finalize = gst_hls_demux_finalize;
189
190 g_object_class_install_property (gobject_class, PROP_START_BITRATE,
191 g_param_spec_uint ("start-bitrate", "Starting Bitrate",
192 "Initial bitrate to use to choose first alternate (0 = automatic) (bits/s)",
193 0, G_MAXUINT, DEFAULT_START_BITRATE,
194 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195
196 element_class->change_state = GST_DEBUG_FUNCPTR (gst_hls_demux_change_state);
197
198 gst_element_class_add_static_pad_template (element_class, &sinktemplate);
199
200 gst_element_class_set_static_metadata (element_class,
201 "HLS Demuxer",
202 "Codec/Demuxer/Adaptive",
203 "HTTP Live Streaming demuxer",
204 "Edward Hervey <[email protected]>, "
205 "Jan Schmidt <[email protected]>");
206
207 adaptivedemux_class->is_live = gst_hls_demux_is_live;
208 adaptivedemux_class->get_live_seek_range = gst_hls_demux_get_live_seek_range;
209 adaptivedemux_class->get_duration = gst_hls_demux_get_duration;
210 adaptivedemux_class->requires_periodical_playlist_update =
211 hlsdemux_requires_periodical_playlist_update_default;
212 adaptivedemux_class->process_manifest =
213 gst_hls_demux_process_initial_manifest;
214 adaptivedemux_class->reset = gst_hls_demux_reset;
215 adaptivedemux_class->update_manifest = gst_hls_demux_update_manifest;
216 adaptivedemux_class->seek = gst_hls_demux_seek;
217 }
218
219 static void
220 gst_hls_demux2_init (GstHLSDemux * demux)
221 {
222 demux->keys = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
223 demux->start_bitrate = DEFAULT_START_BITRATE;
224 g_mutex_init (&demux->keys_lock);
225 }
226
227 static GstStateChangeReturn
228 gst_hls_demux_change_state (GstElement * element, GstStateChange transition)
229 {
230 GstStateChangeReturn ret;
231 GstHLSDemux *demux = GST_HLS_DEMUX (element);
232
233 switch (transition) {
234 case GST_STATE_CHANGE_READY_TO_PAUSED:
235 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
236 break;
237 default:
238 break;
239 }
240
241 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
242
243 switch (transition) {
244 case GST_STATE_CHANGE_PAUSED_TO_READY:
245 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
246 g_hash_table_remove_all (demux->keys);
247 break;
248 default:
249 break;
250 }
251 return ret;
252 }
253
254 static guint64
255 gst_hls_demux_get_bitrate (GstHLSDemux * hlsdemux)
256 {
257 GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (hlsdemux);
258
259 /* FIXME !!!
260 *
261 * No, there isn't a single output :D.
262 * Until the download helper can do estimates,
263 * use the main variant, or a video stream if the
264 * main variant stream is not loading */
265
266 /* Valid because hlsdemux only has a single output */
267 if (demux->input_period->streams) {
268 GstAdaptiveDemux2Stream *stream = demux->input_period->streams->data;
269 return stream->current_download_rate;
270 }
271
272 return 0;
273 }
274
275 static void
276 gst_hls_demux_clear_all_pending_data (GstHLSDemux * hlsdemux)
277 {
278 GstAdaptiveDemux *demux = (GstAdaptiveDemux *) hlsdemux;
279 GList *walk;
280
281 if (!demux->input_period)
282 return;
283
284 for (walk = demux->input_period->streams; walk != NULL; walk = walk->next) {
285 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (walk->data);
286 gst_hls_demux_stream_clear_pending_data (hls_stream, TRUE);
287 }
288 }
289
290 /* Wait until the current variant playlist finishes loading, only
291 * for use when called from an external thread - seeking or initial
292 * manifest. From the scheduler task it will just hang */
293 static GstFlowReturn
294 gst_hls_demux_wait_for_variant_playlist (GstHLSDemux * hlsdemux)
295 {
296 GstFlowReturn flow_ret;
297
298 while ((flow_ret = gst_hls_demux_check_variant_playlist_loaded (hlsdemux)
299 == GST_ADAPTIVE_DEMUX_FLOW_BUSY)) {
300 GstAdaptiveDemux2Stream *stream =
301 GST_ADAPTIVE_DEMUX2_STREAM (hlsdemux->main_stream);
302
303 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED) {
304 GST_DEBUG_OBJECT (hlsdemux,
305 "Interrupted waiting for stream to be prepared");
306 return GST_FLOW_FLUSHING;
307 }
308
309 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_PREPARE;
310
311 if (!gst_adaptive_demux2_stream_wait_prepared (stream)) {
312 GST_DEBUG_OBJECT (hlsdemux,
313 "Interrupted waiting for stream to be prepared");
314 return GST_FLOW_FLUSHING;
315 }
316 }
317
318 return flow_ret;
319 }
320
321 #define SEEK_UPDATES_PLAY_POSITION(r, start_type, stop_type) \
322 ((r >= 0 && start_type != GST_SEEK_TYPE_NONE) || \
323 (r < 0 && stop_type != GST_SEEK_TYPE_NONE))
324
325 #define IS_SNAP_SEEK(f) (f & (GST_SEEK_FLAG_SNAP_BEFORE | \
326 GST_SEEK_FLAG_SNAP_AFTER | \
327 GST_SEEK_FLAG_SNAP_NEAREST | \
328 GST_SEEK_FLAG_TRICKMODE_KEY_UNITS | \
329 GST_SEEK_FLAG_KEY_UNIT))
330
331 static gboolean
332 gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
333 {
334 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
335 GstFormat format;
336 GstSeekFlags flags;
337 GstSeekType start_type, stop_type;
338 gint64 start, stop;
339 gdouble rate, old_rate;
340 GList *walk;
341 gint64 current_pos, target_pos, final_pos;
342 guint64 bitrate;
343
344 gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
345 &stop_type, &stop);
346
347 if (!SEEK_UPDATES_PLAY_POSITION (rate, start_type, stop_type)) {
348 /* nothing to do if we don't have to update the current position */
349 return TRUE;
350 }
351
352 old_rate = demux->segment.rate;
353
354 bitrate = gst_hls_demux_get_bitrate (hlsdemux);
355
356 /* Use I-frame variants for trick modes */
357 if (hlsdemux->master->iframe_variants != NULL
358 && rate < -1.0 && old_rate >= -1.0 && old_rate <= 1.0) {
359
360 /* Switch to I-frame variant */
361 if (!gst_hls_demux_change_variant_playlist (hlsdemux, TRUE,
362 bitrate / ABS (rate), NULL))
363 return FALSE;
364
365 } else if (rate > -1.0 && rate <= 1.0 && (old_rate < -1.0 || old_rate > 1.0)) {
366 /* Switch to normal variant */
367 if (!gst_hls_demux_change_variant_playlist (hlsdemux, FALSE, bitrate, NULL))
368 return FALSE;
369 }
370
371 /* Of course the playlist isn't loaded as soon as we ask - we need to wait */
372 GstFlowReturn flow_ret = gst_hls_demux_wait_for_variant_playlist (hlsdemux);
373 if (flow_ret == GST_FLOW_FLUSHING)
374 return FALSE;
375 if (flow_ret != GST_FLOW_OK) {
376 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
377 (_("Internal data stream error.")), ("Could not switch playlist"));
378 return FALSE;
379 }
380
381 target_pos = rate < 0 ? stop : start;
382 final_pos = target_pos;
383
384 /* properly cleanup pending decryption status */
385 if (flags & GST_SEEK_FLAG_FLUSH) {
386 gst_hls_demux_clear_all_pending_data (hlsdemux);
387 gst_hls_prune_time_mappings (hlsdemux);
388 }
389
390 for (walk = demux->input_period->streams; walk; walk = g_list_next (walk)) {
391 GstAdaptiveDemux2Stream *stream =
392 GST_ADAPTIVE_DEMUX2_STREAM_CAST (walk->data);
393
394 /* Only seek on selected streams */
395 if (!gst_adaptive_demux2_stream_is_selected (stream))
396 continue;
397
398 GstFlowReturn flow_ret;
399
400 while ((flow_ret =
401 gst_hls_demux_stream_seek (stream, rate >= 0, flags, target_pos,
402 &current_pos) == GST_ADAPTIVE_DEMUX_FLOW_BUSY)) {
403 if (!gst_adaptive_demux2_stream_wait_prepared (GST_ADAPTIVE_DEMUX2_STREAM
404 (stream))) {
405 GST_DEBUG_OBJECT (hlsdemux,
406 "Interrupted waiting for stream to be prepared for seek");
407 return FALSE;
408 }
409 }
410
411 if (flow_ret != GST_FLOW_OK) {
412 GST_ERROR_OBJECT (stream, "Failed to seek on stream");
413 return FALSE;
414 }
415
416 /* FIXME: use minimum position always ? */
417 if (final_pos > current_pos)
418 final_pos = current_pos;
419 }
420
421 if (IS_SNAP_SEEK (flags)) {
422 if (rate >= 0)
423 gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
424 final_pos, stop_type, stop, NULL);
425 else
426 gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
427 start, stop_type, final_pos, NULL);
428 }
429
430 return TRUE;
431 }
432
433 static GstAdaptiveDemux2Stream *
434 create_common_hls_stream (GstHLSDemux * demux, const gchar * name)
435 {
436 GstAdaptiveDemux2Stream *stream;
437
438 stream = g_object_new (GST_TYPE_HLS_DEMUX_STREAM, "name", name, NULL);
439
440 gst_adaptive_demux2_add_stream ((GstAdaptiveDemux *) demux, stream);
441
442 return stream;
443 }
444
445 static void
446 create_main_variant_stream (GstHLSDemux * demux)
447 {
448 GstAdaptiveDemux2Stream *stream;
449 GstHLSDemuxStream *hlsdemux_stream;
450
451 GST_DEBUG_OBJECT (demux, "Creating main variant stream");
452
453 stream = create_common_hls_stream (demux, "hlsstream-variant");
454 demux->main_stream = hlsdemux_stream = (GstHLSDemuxStream *) stream;
455
456 hlsdemux_stream->is_variant = TRUE;
457
458 /* Due to HLS manifest information being so unreliable/inconsistent, we will
459 * create the actual tracks once we have information about the streams present
460 * in the variant data stream */
461 stream->pending_tracks = TRUE;
462
463 gst_hls_demux_stream_set_playlist_uri (hlsdemux_stream,
464 demux->current_variant->uri);
465
466 /* Start this stream, which will trigger a playlist load */
467 gst_adaptive_demux2_stream_start (stream);
468 }
469
470 GstAdaptiveDemuxTrack *
471 gst_hls_demux_new_track_for_rendition (GstHLSDemux * demux,
472 GstHLSRenditionStream * rendition,
473 GstCaps * caps, GstStreamFlags flags, GstTagList * tags)
474 {
475 GstAdaptiveDemuxTrack *track;
476 gchar *stream_id;
477 GstStreamType stream_type = gst_stream_type_from_hls_type (rendition->mtype);
478
479 if (rendition->name)
480 stream_id =
481 g_strdup_printf ("%s-%s", gst_stream_type_get_name (stream_type),
482 rendition->name);
483 else if (rendition->lang)
484 stream_id =
485 g_strdup_printf ("%s-%s", gst_stream_type_get_name (stream_type),
486 rendition->lang);
487 else
488 stream_id = g_strdup (gst_stream_type_get_name (stream_type));
489
490 if (rendition->lang) {
491 if (tags == NULL)
492 tags = gst_tag_list_new_empty ();
493 if (gst_tag_check_language_code (rendition->lang))
494 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_LANGUAGE_CODE,
495 rendition->lang, NULL);
496 else
497 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_LANGUAGE_NAME,
498 rendition->lang, NULL);
499 }
500
501 if (stream_type == GST_STREAM_TYPE_TEXT)
502 flags |= GST_STREAM_FLAG_SPARSE;
503
504 if (rendition->is_default)
505 flags |= GST_STREAM_FLAG_SELECT;
506
507 track =
508 gst_adaptive_demux_track_new ((GstAdaptiveDemux *) demux, stream_type,
509 flags, stream_id, caps, tags);
510 g_free (stream_id);
511
512 return track;
513 }
514
515 static GstHLSDemuxStream *
516 create_rendition_stream (GstHLSDemux * demux, GstHLSRenditionStream * media)
517 {
518 GstAdaptiveDemux2Stream *stream;
519 GstAdaptiveDemuxTrack *track;
520 GstHLSDemuxStream *hlsdemux_stream;
521 gchar *stream_name;
522
523 GST_DEBUG_OBJECT (demux,
524 "Creating stream for media %s lang:%s (%" GST_PTR_FORMAT ")", media->name,
525 media->lang, media->caps);
526
527 /* We can't reliably provide caps for HLS target tracks since they might
528 * change at any point in time */
529 track = gst_hls_demux_new_track_for_rendition (demux, media, NULL, 0, NULL);
530
531 stream_name = g_strdup_printf ("hlsstream-%s", track->stream_id);
532 stream = create_common_hls_stream (demux, stream_name);
533 g_free (stream_name);
534 hlsdemux_stream = (GstHLSDemuxStream *) stream;
535
536 hlsdemux_stream->is_variant = FALSE;
537 hlsdemux_stream->playlist_fetched = FALSE;
538 stream->stream_type = hlsdemux_stream->rendition_type =
539 gst_stream_type_from_hls_type (media->mtype);
540 if (media->lang)
541 hlsdemux_stream->lang = g_strdup (media->lang);
542 if (media->name)
543 hlsdemux_stream->name = g_strdup (media->name);
544
545 gst_adaptive_demux2_stream_add_track (stream, track);
546 gst_adaptive_demux_track_unref (track);
547
548 return hlsdemux_stream;
549 }
550
551 static GstHLSDemuxStream *
552 existing_rendition_stream (GList * streams, GstHLSRenditionStream * media)
553 {
554 GList *tmp;
555 GstStreamType stream_type = gst_stream_type_from_hls_type (media->mtype);
556
557 for (tmp = streams; tmp; tmp = tmp->next) {
558 GstHLSDemuxStream *demux_stream = tmp->data;
559
560 if (demux_stream->is_variant)
561 continue;
562
563 if (demux_stream->rendition_type == stream_type) {
564 if (!g_strcmp0 (demux_stream->name, media->name))
565 return demux_stream;
566 if (media->name == NULL && media->lang
567 && !g_strcmp0 (demux_stream->lang, media->lang))
568 return demux_stream;
569 }
570 }
571
572 return NULL;
573 }
574
575 static gboolean
576 gst_hls_demux_setup_streams (GstAdaptiveDemux * demux)
577 {
578 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
579 GstHLSVariantStream *playlist = hlsdemux->current_variant;
580 GList *tmp;
581 GList *streams = NULL;
582
583 if (playlist == NULL) {
584 GST_WARNING_OBJECT (demux, "Can't configure streams - no variant selected");
585 return FALSE;
586 }
587
588 GST_DEBUG_OBJECT (demux, "Setting up streams");
589
590 /* If there are alternate renditions, we will produce a GstAdaptiveDemux2Stream
591 * and GstAdaptiveDemuxTrack for each combination of GstStreamType and other
592 * unique identifier (for now just language)
593 *
594 * Which actual GstHLSMedia to use for each stream will be determined based on
595 * the `group-id` (if present and more than one) selected on the main variant
596 * stream */
597 for (tmp = hlsdemux->master->renditions; tmp; tmp = tmp->next) {
598 GstHLSRenditionStream *media = tmp->data;
599 GstHLSDemuxStream *media_stream, *previous_media_stream;
600
601 GST_LOG_OBJECT (demux, "Rendition %s name:'%s' lang:'%s' uri:%s",
602 gst_stream_type_get_name (gst_stream_type_from_hls_type (media->mtype)),
603 media->name, media->lang, media->uri);
604
605 if (media->uri == NULL) {
606 GST_DEBUG_OBJECT (demux,
607 "Skipping media '%s' , it's provided by the variant stream",
608 media->name);
609 continue;
610 }
611
612 media_stream = previous_media_stream =
613 existing_rendition_stream (streams, media);
614
615 if (!media_stream) {
616 media_stream = create_rendition_stream (hlsdemux, tmp->data);
617 } else
618 GST_DEBUG_OBJECT (demux, "Re-using existing GstHLSDemuxStream %s %s",
619 media_stream->name, media_stream->lang);
620
621 /* Is this rendition active in the current variant ? */
622 if (!g_strcmp0 (playlist->media_groups[media->mtype], media->group_id)) {
623 GST_DEBUG_OBJECT (demux, "Enabling rendition");
624 if (media_stream->current_rendition)
625 gst_hls_rendition_stream_unref (media_stream->current_rendition);
626 media_stream->current_rendition = gst_hls_rendition_stream_ref (media);
627 gst_hls_demux_stream_set_playlist_uri (media_stream, media->uri);
628 }
629
630 if (!previous_media_stream)
631 streams = g_list_append (streams, media_stream);
632 }
633
634 /* Free the list (but not the contents, which are stored
635 * elsewhere */
636 if (streams)
637 g_list_free (streams);
638
639 create_main_variant_stream (hlsdemux);
640
641 return TRUE;
642 }
643
644 static void
645 gst_hls_demux_set_current_variant (GstHLSDemux * hlsdemux,
646 GstHLSVariantStream * variant)
647 {
648 if (hlsdemux->current_variant == variant || variant == NULL)
649 return;
650
651 if (hlsdemux->current_variant != NULL) {
652 GST_DEBUG_OBJECT (hlsdemux, "Will switch from variant '%s' to '%s'",
653 hlsdemux->current_variant->name, variant->name);
654 if (hlsdemux->pending_variant) {
655 if (hlsdemux->pending_variant != variant) {
656 GST_DEBUG_OBJECT (hlsdemux, "Already waiting for pending variant '%s'",
657 hlsdemux->pending_variant->name);
658 }
659 gst_hls_variant_stream_unref (hlsdemux->pending_variant);
660 }
661 hlsdemux->pending_variant = gst_hls_variant_stream_ref (variant);
662 } else {
663 GST_DEBUG_OBJECT (hlsdemux, "Setting variant '%s'", variant->name);
664 hlsdemux->current_variant = gst_hls_variant_stream_ref (variant);
665 }
666
667 if (hlsdemux->main_stream) {
668 /* The variant stream exists, update the playlist we're loading */
669 gst_hls_demux_stream_set_playlist_uri (hlsdemux->main_stream, variant->uri);
670 }
671 }
672
673 /* Called to process the initial multi-variant (or simple playlist)
674 * received on the element's sinkpad */
675 static gboolean
676 gst_hls_demux_process_initial_manifest (GstAdaptiveDemux * demux,
677 GstBuffer * buf)
678 {
679 GstHLSVariantStream *variant;
680 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
681 gchar *playlist = NULL;
682 guint start_bitrate = hlsdemux->start_bitrate;
683 gboolean ret;
684 GstHLSMediaPlaylist *simple_media_playlist = NULL;
685
686 GST_INFO_OBJECT (demux, "Initial playlist location: %s (base uri: %s)",
687 demux->manifest_uri, demux->manifest_base_uri);
688
689 playlist = gst_hls_buf_to_utf8_text (buf);
690 if (playlist == NULL) {
691 GST_WARNING_OBJECT (demux, "Error validating initial playlist");
692 return FALSE;
693 }
694
695 if (hlsdemux->master) {
696 gst_hls_master_playlist_unref (hlsdemux->master);
697 hlsdemux->master = NULL;
698 }
699 hlsdemux->master = gst_hls_master_playlist_new_from_data (playlist,
700 gst_adaptive_demux_get_manifest_ref_uri (demux));
701
702 if (hlsdemux->master == NULL) {
703 /* In most cases, this will happen if we set a wrong url in the
704 * source element and we have received the 404 HTML response instead of
705 * the playlist */
706 GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Invalid playlist."),
707 ("Could not parse playlist. Check if the URL is correct."));
708 return FALSE;
709 }
710
711 if (hlsdemux->master->is_simple) {
712 simple_media_playlist =
713 gst_hls_media_playlist_parse (playlist, GST_CLOCK_TIME_NONE,
714 gst_adaptive_demux_get_manifest_ref_uri (demux), NULL);
715 }
716
717 if (start_bitrate == 0)
718 start_bitrate = demux->connection_speed;
719
720 /* select the initial variant stream */
721 if (start_bitrate > 0) {
722 variant =
723 gst_hls_master_playlist_get_variant_for_bitrate (hlsdemux->master,
724 FALSE, start_bitrate, demux->min_bitrate, hlsdemux->failed_variants);
725 } else {
726 variant = hlsdemux->master->default_variant;
727 }
728
729 if (variant == NULL) {
730 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
731 (_("Internal data stream error.")),
732 ("Could not find an initial variant to play"));
733 }
734
735 GST_INFO_OBJECT (hlsdemux,
736 "Manifest processed, initial variant selected : `%s`", variant->name);
737 gst_hls_demux_set_current_variant (hlsdemux, variant);
738
739 GST_DEBUG_OBJECT (hlsdemux, "Manifest handled, now setting up streams");
740
741 ret = gst_hls_demux_setup_streams (demux);
742 if (!ret)
743 return FALSE;
744
745 if (simple_media_playlist) {
746 GstM3U8SeekResult seek_result;
747 GstM3U8MediaSegment *segment;
748
749 hlsdemux->main_stream->playlist = simple_media_playlist;
750 /* This is the initial variant playlist. We will use it to base all our timing
751 * from. */
752 segment = g_ptr_array_index (simple_media_playlist->segments, 0);
753 if (segment) {
754 segment->stream_time = 0;
755 gst_hls_media_playlist_recalculate_stream_time (simple_media_playlist,
756 segment);
757 }
758
759 if (!gst_hls_media_playlist_get_starting_segment (simple_media_playlist,
760 &seek_result)) {
761 GST_DEBUG_OBJECT (hlsdemux->main_stream,
762 "Failed to find a segment to start at");
763 return FALSE;
764 }
765 hlsdemux->main_stream->current_segment = seek_result.segment;
766 hlsdemux->main_stream->in_partial_segments =
767 seek_result.found_partial_segment;
768 hlsdemux->main_stream->part_idx = seek_result.part_idx;
769
770 gst_hls_demux_handle_variant_playlist_update (hlsdemux,
771 simple_media_playlist->uri, simple_media_playlist);
772 }
773
774 /* If this is a multi-variant playlist, wait for the initial variant playlist to load */
775 if (!hlsdemux->master->is_simple) {
776 GstFlowReturn flow_ret = gst_hls_demux_wait_for_variant_playlist (hlsdemux);
777 if (flow_ret == GST_FLOW_FLUSHING)
778 return FALSE;
779 if (flow_ret != GST_FLOW_OK) {
780 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
781 (_("Internal data stream error.")),
782 ("Could not fetch media playlist"));
783 return FALSE;
784 }
785 }
786
787 /* Make sure the external manifest copy of the main playlist
788 * is available to the baseclass at the start */
789 gst_hls_demux_update_manifest (demux);
790
791 return TRUE;
792 }
793
794 static GstClockTime
795 gst_hls_demux_get_duration (GstAdaptiveDemux * demux)
796 {
797 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
798 GstClockTime duration = GST_CLOCK_TIME_NONE;
799
800 if (hlsdemux->main_playlist)
801 duration = gst_hls_media_playlist_get_duration (hlsdemux->main_playlist);
802
803 return duration;
804 }
805
806 /* Called from base class with the MANIFEST_LOCK held */
807 static gboolean
808 gst_hls_demux_is_live (GstAdaptiveDemux * demux)
809 {
810 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
811 gboolean is_live = FALSE;
812
813 if (hlsdemux->main_playlist)
814 is_live = gst_hls_media_playlist_is_live (hlsdemux->main_playlist);
815
816 return is_live;
817 }
818
819 const GstHLSKey *
820 gst_hls_demux_get_key (GstHLSDemux * demux, const gchar * key_url,
821 const gchar * referer, gboolean allow_cache)
822 {
823 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
824 DownloadRequest *key_request;
825 DownloadFlags dl_flags = DOWNLOAD_FLAG_NONE;
826 GstBuffer *key_buffer;
827 GstHLSKey *key;
828 GError *err = NULL;
829
830 GST_LOG_OBJECT (demux, "Looking up key for key url %s", key_url);
831
832 g_mutex_lock (&demux->keys_lock);
833
834 key = g_hash_table_lookup (demux->keys, key_url);
835
836 if (key != NULL) {
837 GST_LOG_OBJECT (demux, "Found key for key url %s in key cache", key_url);
838 goto out;
839 }
840
841 GST_INFO_OBJECT (demux, "Fetching key %s", key_url);
842
843 if (!allow_cache)
844 dl_flags |= DOWNLOAD_FLAG_FORCE_REFRESH;
845
846 key_request =
847 downloadhelper_fetch_uri (adaptive_demux->download_helper,
848 key_url, referer, dl_flags, &err);
849 if (key_request == NULL) {
850 GST_WARNING_OBJECT (demux, "Failed to download key to decrypt data: %s",
851 err ? err->message : "error");
852 g_clear_error (&err);
853 goto out;
854 }
855
856 key_buffer = download_request_take_buffer (key_request);
857 download_request_unref (key_request);
858
859 key = g_new0 (GstHLSKey, 1);
860 if (gst_buffer_extract (key_buffer, 0, key->data, 16) < 16)
861 GST_WARNING_OBJECT (demux, "Download decryption key is too short!");
862
863 g_hash_table_insert (demux->keys, g_strdup (key_url), key);
864
865 gst_buffer_unref (key_buffer);
866
867 out:
868
869 g_mutex_unlock (&demux->keys_lock);
870
871 if (key != NULL)
872 GST_MEMDUMP_OBJECT (demux, "Key", key->data, 16);
873
874 return key;
875 }
876
877 void
878 gst_hls_demux_start_rendition_streams (GstHLSDemux * hlsdemux)
879 {
880 GstAdaptiveDemux *demux = (GstAdaptiveDemux *) hlsdemux;
881 GList *tmp;
882
883 for (tmp = demux->input_period->streams; tmp; tmp = tmp->next) {
884 GstAdaptiveDemux2Stream *stream = (GstAdaptiveDemux2Stream *) tmp->data;
885 GstHLSDemuxStream *hls_stream = (GstHLSDemuxStream *) stream;
886
887 if (!hls_stream->is_variant
888 && gst_adaptive_demux2_stream_is_selected (stream))
889 gst_adaptive_demux2_stream_start (stream);
890 }
891 }
892
893 static GstHLSTimeMap *
894 time_map_in_list (GList * list, gint64 dsn)
895 {
896 GList *iter;
897
898 for (iter = list; iter; iter = iter->next) {
899 GstHLSTimeMap *map = iter->data;
900
901 if (map->dsn == dsn)
902 return map;
903 }
904
905 return NULL;
906 }
907
908 GstHLSTimeMap *
909 gst_hls_demux_find_time_map (GstHLSDemux * demux, gint64 dsn)
910 {
911 return time_map_in_list (demux->mappings, dsn);
912 }
913
914 static GstHLSTimeMap *
915 gst_hls_time_map_new (void)
916 {
917 GstHLSTimeMap *map = g_new0 (GstHLSTimeMap, 1);
918
919 map->stream_time = GST_CLOCK_TIME_NONE;
920 map->internal_time = GST_CLOCK_TIME_NONE;
921
922 return map;
923 }
924
925 static void
926 gst_hls_time_map_free (GstHLSTimeMap * map)
927 {
928 if (map->pdt)
929 g_date_time_unref (map->pdt);
930 g_free (map);
931 }
932
933 void
934 gst_time_map_set_values (GstHLSTimeMap * map, GstClockTimeDiff stream_time,
935 GstClockTime internal_time, GDateTime * pdt)
936 {
937 GstClockTime offset = 0;
938
939 if (stream_time < 0) {
940 offset = -stream_time;
941 stream_time = 0;
942 /* Handle negative stream times. This can happen for example when the server
943 * returns an older playlist.
944 *
945 * Shift the values accordingly to end up with non-negative reference stream
946 * time */
947 GST_DEBUG ("Shifting values before storage (offset : %" GST_TIME_FORMAT ")",
948 GST_TIME_ARGS (offset));
949 }
950
951 map->stream_time = stream_time;
952 map->internal_time = internal_time;
953 if (pdt) {
954 if (offset)
955 map->pdt = g_date_time_add (pdt, offset / GST_USECOND);
956 else
957 map->pdt = g_date_time_ref (pdt);
958 }
959 }
960
961 void
962 gst_hls_demux_add_time_mapping (GstHLSDemux * demux, gint64 dsn,
963 GstClockTimeDiff stream_time, GDateTime * pdt)
964 {
965 #ifndef GST_DISABLE_GST_DEBUG
966 gchar *datestring = NULL;
967 #endif
968 GstHLSTimeMap *map;
969 GList *tmp;
970
971 /* Check if we don't already have a mapping for the given dsn */
972 for (tmp = demux->mappings; tmp; tmp = tmp->next) {
973 GstHLSTimeMap *map = tmp->data;
974
975 if (map->dsn == dsn) {
976 #ifndef GST_DISABLE_GST_DEBUG
977 if (map->pdt)
978 datestring = g_date_time_format_iso8601 (map->pdt);
979 GST_DEBUG_OBJECT (demux,
980 "Already have mapping, dsn:%" G_GINT64_FORMAT " stream_time:%"
981 GST_TIME_FORMAT " internal_time:%" GST_TIME_FORMAT " pdt:%s",
982 map->dsn, GST_TIME_ARGS (map->stream_time),
983 GST_TIME_ARGS (map->internal_time), datestring);
984 g_free (datestring);
985 #endif
986 return;
987 }
988 }
989
990 #ifndef GST_DISABLE_GST_DEBUG
991 if (pdt)
992 datestring = g_date_time_format_iso8601 (pdt);
993 GST_DEBUG_OBJECT (demux,
994 "New mapping, dsn:%" G_GINT64_FORMAT " stream_time:%" GST_TIME_FORMAT
995 " pdt:%s", dsn, GST_TIME_ARGS (stream_time), datestring);
996 g_free (datestring);
997 #endif
998
999 map = gst_hls_time_map_new ();
1000 map->dsn = dsn;
1001 gst_time_map_set_values (map, stream_time, GST_CLOCK_TIME_NONE, pdt);
1002
1003 demux->mappings = g_list_append (demux->mappings, map);
1004 }
1005
1006 /* Remove any time mapping which isn't currently used by any stream playlist */
1007 static void
1008 gst_hls_prune_time_mappings (GstHLSDemux * hlsdemux)
1009 {
1010 GstAdaptiveDemux *demux = (GstAdaptiveDemux *) hlsdemux;
1011 GList *active = NULL;
1012 GList *iterstream;
1013
1014 for (iterstream = demux->input_period->streams; iterstream;
1015 iterstream = iterstream->next) {
1016 GstAdaptiveDemux2Stream *stream = iterstream->data;
1017 GstHLSDemuxStream *hls_stream = (GstHLSDemuxStream *) stream;
1018 gint64 dsn = G_MAXINT64;
1019 guint idx, len;
1020
1021 if (!hls_stream->playlist)
1022 continue;
1023 len = hls_stream->playlist->segments->len;
1024 for (idx = 0; idx < len; idx++) {
1025 GstM3U8MediaSegment *segment =
1026 g_ptr_array_index (hls_stream->playlist->segments, idx);
1027
1028 if (dsn == G_MAXINT64 || segment->discont_sequence != dsn) {
1029 dsn = segment->discont_sequence;
1030 if (!time_map_in_list (active, dsn)) {
1031 GstHLSTimeMap *map = gst_hls_demux_find_time_map (hlsdemux, dsn);
1032 if (map) {
1033 GST_DEBUG_OBJECT (demux,
1034 "Keeping active time map dsn:%" G_GINT64_FORMAT, map->dsn);
1035 /* Move active dsn to active list */
1036 hlsdemux->mappings = g_list_remove (hlsdemux->mappings, map);
1037 active = g_list_append (active, map);
1038 }
1039 }
1040 }
1041 }
1042 }
1043
1044 g_list_free_full (hlsdemux->mappings, (GDestroyNotify) gst_hls_time_map_free);
1045 hlsdemux->mappings = active;
1046 }
1047
1048 /* Go over the DSN from the playlist and add any missing time mapping */
1049 void
1050 gst_hls_update_time_mappings (GstHLSDemux * demux,
1051 GstHLSMediaPlaylist * playlist)
1052 {
1053 guint idx, len = playlist->segments->len;
1054 gint64 dsn = G_MAXINT64;
1055
1056 for (idx = 0; idx < len; idx++) {
1057 GstM3U8MediaSegment *segment = g_ptr_array_index (playlist->segments, idx);
1058
1059 if (dsn == G_MAXINT64 || segment->discont_sequence != dsn) {
1060 dsn = segment->discont_sequence;
1061 if (!gst_hls_demux_find_time_map (demux, segment->discont_sequence))
1062 gst_hls_demux_add_time_mapping (demux, segment->discont_sequence,
1063 segment->stream_time, segment->datetime);
1064 }
1065 }
1066 }
1067
1068 /* Called by the base class with the manifest lock held */
1069 static GstFlowReturn
1070 gst_hls_demux_update_manifest (GstAdaptiveDemux * demux)
1071 {
1072 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1073
1074 /* Take a copy of the main variant playlist for base class
1075 * calls that need access from outside the scheduler task,
1076 * holding the MANIFEST_LOCK */
1077 if (hlsdemux->main_stream && hlsdemux->main_stream->playlist) {
1078 if (hlsdemux->main_playlist)
1079 gst_hls_media_playlist_unref (hlsdemux->main_playlist);
1080 hlsdemux->main_playlist =
1081 gst_hls_media_playlist_ref (hlsdemux->main_stream->playlist);
1082 return GST_FLOW_OK;
1083 }
1084
1085 return GST_ADAPTIVE_DEMUX_FLOW_BUSY;
1086 }
1087
1088 void
1089 gst_hls_demux_handle_variant_playlist_update (GstHLSDemux * demux,
1090 const gchar * playlist_uri, GstHLSMediaPlaylist * playlist)
1091 {
1092 if (demux->main_stream == NULL || !demux->main_stream->playlist_fetched) {
1093 GstM3U8MediaSegment *segment;
1094
1095 GST_DEBUG_OBJECT (demux,
1096 "Setting up initial variant segment and time mapping");
1097
1098 /* This is the initial variant playlist. We will use it to base all our timing
1099 * from. */
1100 segment = g_ptr_array_index (playlist->segments, 0);
1101 if (segment) {
1102 segment->stream_time = 0;
1103 gst_hls_media_playlist_recalculate_stream_time (playlist, segment);
1104 }
1105 }
1106
1107 if (demux->pending_variant) {
1108 /* The pending variant must always match the one that just got updated:
1109 * The loader should only do a callback for the most recently set URI */
1110 g_assert (!g_strcmp0 (demux->pending_variant->uri, playlist_uri));
1111
1112 gboolean changed = (demux->pending_variant != demux->current_variant);
1113
1114 gst_hls_variant_stream_unref (demux->current_variant);
1115 /* Stealing ref */
1116 demux->current_variant = demux->pending_variant;
1117 demux->pending_variant = NULL;
1118
1119 if (changed) {
1120 GstAdaptiveDemux *basedemux = GST_ADAPTIVE_DEMUX (demux);
1121 const gchar *main_uri =
1122 gst_adaptive_demux_get_manifest_ref_uri (basedemux);
1123 gchar *uri = demux->current_variant->uri;
1124 gint new_bandwidth = demux->current_variant->bandwidth;
1125
1126 gst_element_post_message (GST_ELEMENT_CAST (demux),
1127 gst_message_new_element (GST_OBJECT_CAST (demux),
1128 gst_structure_new (GST_ADAPTIVE_DEMUX_STATISTICS_MESSAGE_NAME,
1129 "manifest-uri", G_TYPE_STRING,
1130 main_uri, "uri", G_TYPE_STRING,
1131 uri, "bitrate", G_TYPE_INT, new_bandwidth, NULL)));
1132
1133 GST_DEBUG_OBJECT (demux, "Changed variant");
1134 }
1135 }
1136
1137 /* Update time mappings. We only use the variant stream for collecting
1138 * mappings since it is the reference on which rendition stream timing will
1139 * be based. */
1140 gst_hls_update_time_mappings (demux, playlist);
1141 gst_hls_media_playlist_dump (playlist);
1142
1143 /* Get the base class to call the update_manifest() vfunc with the MANIFEST_LOCK()
1144 * held */
1145 gst_adaptive_demux2_manual_manifest_update (GST_ADAPTIVE_DEMUX (demux));
1146 }
1147
1148 void
1149 gst_hls_demux_handle_variant_playlist_update_error (GstHLSDemux * demux,
1150 const gchar * playlist_uri)
1151 {
1152 GST_DEBUG_OBJECT (demux, "Playlist update failure for variant URI %s",
1153 playlist_uri);
1154
1155 /* Check if this is a new load of the pending variant, or a reload
1156 * of the current variant */
1157 GstHLSVariantStream *variant = demux->pending_variant;
1158 if (variant == NULL)
1159 variant = demux->current_variant;
1160
1161 /* If the variant has an fallback URIs available, we can try one of those */
1162 if (variant->fallback != NULL) {
1163 gchar *fallback_uri = (gchar *) (variant->fallback->data);
1164
1165 GST_DEBUG_OBJECT (demux,
1166 "Variant playlist update failed. Switching to fallback URI %s",
1167 fallback_uri);
1168
1169 variant->fallback = g_list_remove (variant->fallback, fallback_uri);
1170 g_free (variant->uri);
1171 variant->uri = fallback_uri;
1172
1173 if (demux->main_stream) {
1174 /* The variant stream exists, update the playlist we're loading */
1175 gst_hls_demux_stream_set_playlist_uri (demux->main_stream, variant->uri);
1176 }
1177 return;
1178 }
1179
1180 GST_DEBUG_OBJECT (demux, "Variant playlist update failed. "
1181 "Marking variant URL %s as failed and switching over to another variant",
1182 playlist_uri);
1183
1184 /* The variant must always match the one that just got updated:
1185 * The loader should only do a callback for the most recently set URI */
1186 g_assert (!g_strcmp0 (variant->uri, playlist_uri));
1187
1188 /* If we didn't already add this playlist to the failed variants list
1189 * do so now. It's possible we get an update error again if we failed
1190 * to choose a new variant and posted error but didn't get shut down
1191 * yet */
1192 if (g_list_find (demux->failed_variants, variant) == NULL) {
1193 demux->failed_variants =
1194 g_list_prepend (demux->failed_variants,
1195 gst_hls_variant_stream_ref (variant));
1196 }
1197
1198 /* Now try to find another variant to play */
1199 gdouble play_rate = gst_adaptive_demux_play_rate (GST_ADAPTIVE_DEMUX (demux));
1200 guint64 bitrate = gst_hls_demux_get_bitrate (demux);
1201
1202 GST_DEBUG_OBJECT (demux, "Trying to find failover variant playlist");
1203
1204 if (!gst_hls_demux_change_variant_playlist (demux,
1205 variant->iframe, bitrate / MAX (1.0, ABS (play_rate)), NULL)) {
1206 GST_ERROR_OBJECT (demux, "Failed to choose a new variant to play");
1207 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
1208 (_("Internal data stream error.")),
1209 ("Could not update any variant playlist"));
1210 }
1211 }
1212
1213 /* Reset hlsdemux in case of live synchronization loss (i.e. when a media
1214 * playlist update doesn't match at all with the previous one) */
1215 gboolean
1216 gst_hls_demux_reset_for_lost_sync (GstHLSDemux * hlsdemux)
1217 {
1218 GstAdaptiveDemux *demux = (GstAdaptiveDemux *) hlsdemux;
1219 GList *iter;
1220
1221 GST_DEBUG_OBJECT (hlsdemux, "Resetting for lost sync");
1222
1223 for (iter = demux->input_period->streams; iter; iter = iter->next) {
1224 GstHLSDemuxStream *hls_stream = iter->data;
1225 GstAdaptiveDemux2Stream *stream = (GstAdaptiveDemux2Stream *) hls_stream;
1226
1227 if (hls_stream->current_segment)
1228 gst_m3u8_media_segment_unref (hls_stream->current_segment);
1229 hls_stream->current_segment = NULL;
1230
1231 if (hls_stream->is_variant) {
1232 GstHLSTimeMap *map;
1233 GstM3U8SeekResult seek_result;
1234
1235 /* Resynchronize the variant stream */
1236 if (stream->current_position == GST_CLOCK_TIME_NONE) {
1237 GST_ERROR_OBJECT (stream,
1238 "Variant doesn't have a current position to recalculate sync");
1239 return FALSE;
1240 }
1241
1242 if (gst_hls_media_playlist_get_starting_segment (hls_stream->playlist,
1243 &seek_result)) {
1244 hls_stream->current_segment = seek_result.segment;
1245 hls_stream->in_partial_segments = seek_result.found_partial_segment;
1246 hls_stream->part_idx = seek_result.part_idx;
1247
1248 hls_stream->current_segment->stream_time = stream->current_position;
1249 gst_hls_media_playlist_recalculate_stream_time (hls_stream->playlist,
1250 hls_stream->current_segment);
1251 GST_DEBUG_OBJECT (stream,
1252 "Resynced variant playlist to %" GST_TIME_FORMAT,
1253 GST_TIME_ARGS (stream->current_position));
1254 map =
1255 gst_hls_demux_find_time_map (hlsdemux,
1256 hls_stream->current_segment->discont_sequence);
1257 if (map)
1258 map->internal_time = GST_CLOCK_TIME_NONE;
1259 gst_hls_update_time_mappings (hlsdemux, hls_stream->playlist);
1260 gst_hls_media_playlist_dump (hls_stream->playlist);
1261 } else {
1262 GST_ERROR_OBJECT (stream, "Failed to locate a segment to restart at!");
1263 }
1264 } else {
1265 /* Force playlist update for the rendition streams, it will resync to the
1266 * variant stream on the next round */
1267 if (hls_stream->playlist)
1268 gst_hls_media_playlist_unref (hls_stream->playlist);
1269 hls_stream->playlist = NULL;
1270 hls_stream->playlist_fetched = FALSE;
1271 }
1272 }
1273
1274 return TRUE;
1275 }
1276
1277 static void
1278 gst_hls_demux_reset (GstAdaptiveDemux * ademux)
1279 {
1280 GstHLSDemux *demux = GST_HLS_DEMUX_CAST (ademux);
1281
1282 GST_DEBUG_OBJECT (demux, "resetting");
1283
1284 if (ademux->input_period) {
1285 GList *walk;
1286 for (walk = ademux->input_period->streams; walk != NULL; walk = walk->next) {
1287 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (walk->data);
1288 hls_stream->pdt_tag_sent = FALSE;
1289 }
1290 }
1291
1292 if (demux->master) {
1293 gst_hls_master_playlist_unref (demux->master);
1294 demux->master = NULL;
1295 }
1296 if (demux->main_playlist) {
1297 gst_hls_media_playlist_unref (demux->main_playlist);
1298 demux->main_playlist = NULL;
1299 }
1300 if (demux->current_variant != NULL) {
1301 gst_hls_variant_stream_unref (demux->current_variant);
1302 demux->current_variant = NULL;
1303 }
1304 if (demux->pending_variant != NULL) {
1305 gst_hls_variant_stream_unref (demux->pending_variant);
1306 demux->pending_variant = NULL;
1307 }
1308 if (demux->failed_variants != NULL) {
1309 g_list_free_full (demux->failed_variants,
1310 (GDestroyNotify) gst_hls_variant_stream_unref);
1311 demux->failed_variants = NULL;
1312 }
1313
1314 g_list_free_full (demux->mappings, (GDestroyNotify) gst_hls_time_map_free);
1315 demux->mappings = NULL;
1316
1317 gst_hls_demux_clear_all_pending_data (demux);
1318 }
1319
1320 static GstFlowReturn
1321 gst_hls_demux_check_variant_playlist_loaded (GstHLSDemux * demux)
1322 {
1323 GstHLSVariantStream *target_variant =
1324 demux->pending_variant ? demux->pending_variant : demux->current_variant;
1325 GstHLSDemuxStream *stream = demux->main_stream;
1326
1327 /* The demuxer has been resetted in the meantime */
1328 if (target_variant == NULL)
1329 return GST_FLOW_FLUSHING;
1330
1331 return gst_hls_demux_stream_check_current_playlist_uri (stream,
1332 target_variant->uri);
1333 }
1334
1335 gboolean
1336 gst_hls_demux_change_variant_playlist (GstHLSDemux * demux,
1337 gboolean iframe_variant, guint max_bitrate, gboolean * changed)
1338 {
1339 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX_CAST (demux);
1340
1341 if (changed)
1342 *changed = FALSE;
1343
1344 /* Make sure we keep a reference for the debug output below */
1345 GstHLSVariantStream *new_variant =
1346 gst_hls_master_playlist_get_variant_for_bitrate (demux->master,
1347 iframe_variant, max_bitrate, adaptive_demux->min_bitrate,
1348 demux->failed_variants);
1349
1350 /* We're out of available variants to use */
1351 if (new_variant == NULL) {
1352 return FALSE;
1353 }
1354
1355 GstHLSVariantStream *previous_variant =
1356 gst_hls_variant_stream_ref (demux->current_variant);
1357
1358 /* Don't do anything else if the playlist is the same */
1359 if (new_variant == previous_variant) {
1360 GST_TRACE_OBJECT (demux, "Variant didn't change from bandwidth %dbps",
1361 new_variant->bandwidth);
1362 gst_hls_variant_stream_unref (previous_variant);
1363 return TRUE;
1364 }
1365
1366 gst_hls_demux_set_current_variant (demux, new_variant);
1367
1368 gint new_bandwidth = new_variant->bandwidth;
1369
1370 GST_INFO_OBJECT (demux, "Client was on %dbps, max allowed is %dbps, switching"
1371 " to bitrate %dbps", previous_variant->bandwidth, max_bitrate,
1372 new_bandwidth);
1373
1374 gst_hls_variant_stream_unref (previous_variant);
1375 if (changed)
1376 *changed = TRUE;
1377 return TRUE;
1378 }
1379
1380 static gboolean
1381 gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
1382 gint64 * stop)
1383 {
1384 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1385 gboolean ret = FALSE;
1386
1387 if (hlsdemux->main_playlist) {
1388 ret =
1389 gst_hls_media_playlist_get_seek_range (hlsdemux->main_playlist, start,
1390 stop);
1391 }
1392
1393 return ret;
1394 }
1395
1396 static gboolean
1397 hlsdemux2_element_init (GstPlugin * plugin)
1398 {
1399 gboolean ret = TRUE;
1400
1401 GST_DEBUG_CATEGORY_INIT (gst_hls_demux2_debug, "hlsdemux2", 0,
1402 "hlsdemux2 element");
1403
1404 if (!adaptivedemux2_base_element_init (plugin))
1405 return TRUE;
1406
1407 ret = gst_element_register (plugin, "hlsdemux2",
1408 GST_RANK_PRIMARY + 1, GST_TYPE_HLS_DEMUX2);
1409
1410 return ret;
1411 }