2 * Copyright (C) 2010 Marc-Andre Lureau <marcandre.lureau@gmail.com>
3 * Copyright (C) 2010 Andoni Morales Alastruey <ylatuya@gmail.com>
4 * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
5 * Author: Youness Alaoui <youness.alaoui@collabora.co.uk>, Collabora Ltd.
6 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
7 * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
8 * Copyright (C) 2015 Tim-Philipp Müller <tim@centricular.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
28 * SECTION:element-hlsdemux
30 * HTTP Live Streaming demuxer element.
33 * <title>Example launch line</title>
35 * gst-launch-1.0 souphttpsrc location=http://devimages.apple.com/iphone/samples/bipbop/gear4/prog_index.m3u8 ! hlsdemux ! decodebin ! videoconvert ! videoscale ! autovideosink
45 #include <gst/base/gsttypefindhelper.h>
46 #include "gsthlsdemux.h"
48 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
53 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_STATIC_CAPS ("application/x-hls"));
58 GST_DEBUG_CATEGORY_STATIC (gst_hls_demux_debug);
59 #define GST_CAT_DEFAULT gst_hls_demux_debug
61 #define GST_M3U8_CLIENT_LOCK(l) /* FIXME */
62 #define GST_M3U8_CLIENT_UNLOCK(l) /* FIXME */
65 static void gst_hls_demux_finalize (GObject * obj);
68 static GstStateChangeReturn
69 gst_hls_demux_change_state (GstElement * element, GstStateChange transition);
72 static gboolean gst_hls_demux_update_playlist (GstHLSDemux * demux,
73 gboolean update, GError ** err);
74 static gchar *gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf);
76 static gboolean gst_hls_demux_change_playlist (GstHLSDemux * demux,
77 guint max_bitrate, gboolean * changed);
78 static GstBuffer *gst_hls_demux_decrypt_fragment (GstHLSDemux * demux,
79 GstHLSDemuxStream * stream, GstBuffer * encrypted_buffer, GError ** err);
81 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
82 const guint8 * key_data, const guint8 * iv_data);
83 static void gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream);
85 static gboolean gst_hls_demux_is_live (GstAdaptiveDemux * demux);
86 static GstClockTime gst_hls_demux_get_duration (GstAdaptiveDemux * demux);
87 static gint64 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux *
89 static gboolean gst_hls_demux_process_manifest (GstAdaptiveDemux * demux,
91 static GstFlowReturn gst_hls_demux_update_manifest (GstAdaptiveDemux * demux);
92 static gboolean gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
94 gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
95 GstAdaptiveDemuxStream * stream);
96 static GstFlowReturn gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
97 GstAdaptiveDemuxStream * stream);
98 static GstFlowReturn gst_hls_demux_data_received (GstAdaptiveDemux * demux,
99 GstAdaptiveDemuxStream * stream, GstBuffer * buffer);
100 static void gst_hls_demux_stream_free (GstAdaptiveDemuxStream * stream);
101 static gboolean gst_hls_demux_stream_has_next_fragment (GstAdaptiveDemuxStream *
103 static GstFlowReturn gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream *
105 static GstFlowReturn gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream
107 static gboolean gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream,
109 static void gst_hls_demux_reset (GstAdaptiveDemux * demux);
110 static gboolean gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux,
111 gint64 * start, gint64 * stop);
112 static GstM3U8 *gst_hls_demux_stream_get_m3u8 (GstHLSDemuxStream * hls_stream);
113 static void gst_hls_demux_set_current_variant (GstHLSDemux * hlsdemux,
114 GstHLSVariantStream * variant);
116 #define gst_hls_demux_parent_class parent_class
117 G_DEFINE_TYPE (GstHLSDemux, gst_hls_demux, GST_TYPE_ADAPTIVE_DEMUX);
120 gst_hls_demux_finalize (GObject * obj)
122 GstHLSDemux *demux = GST_HLS_DEMUX (obj);
124 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
125 g_mutex_clear (&demux->keys_lock);
127 G_OBJECT_CLASS (parent_class)->finalize (obj);
131 gst_hls_demux_class_init (GstHLSDemuxClass * klass)
133 GObjectClass *gobject_class;
134 GstElementClass *element_class;
135 GstAdaptiveDemuxClass *adaptivedemux_class;
137 gobject_class = (GObjectClass *) klass;
138 element_class = (GstElementClass *) klass;
139 adaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
141 gobject_class->finalize = gst_hls_demux_finalize;
143 element_class->change_state = GST_DEBUG_FUNCPTR (gst_hls_demux_change_state);
145 gst_element_class_add_static_pad_template (element_class, &srctemplate);
146 gst_element_class_add_static_pad_template (element_class, &sinktemplate);
148 gst_element_class_set_static_metadata (element_class,
150 "Codec/Demuxer/Adaptive",
151 "HTTP Live Streaming demuxer",
152 "Marc-Andre Lureau <marcandre.lureau@gmail.com>\n"
153 "Andoni Morales Alastruey <ylatuya@gmail.com>");
155 adaptivedemux_class->is_live = gst_hls_demux_is_live;
156 adaptivedemux_class->get_live_seek_range = gst_hls_demux_get_live_seek_range;
157 adaptivedemux_class->get_duration = gst_hls_demux_get_duration;
158 adaptivedemux_class->get_manifest_update_interval =
159 gst_hls_demux_get_manifest_update_interval;
160 adaptivedemux_class->process_manifest = gst_hls_demux_process_manifest;
161 adaptivedemux_class->update_manifest = gst_hls_demux_update_manifest;
162 adaptivedemux_class->reset = gst_hls_demux_reset;
163 adaptivedemux_class->seek = gst_hls_demux_seek;
164 adaptivedemux_class->stream_has_next_fragment =
165 gst_hls_demux_stream_has_next_fragment;
166 adaptivedemux_class->stream_advance_fragment = gst_hls_demux_advance_fragment;
167 adaptivedemux_class->stream_update_fragment_info =
168 gst_hls_demux_update_fragment_info;
169 adaptivedemux_class->stream_select_bitrate = gst_hls_demux_select_bitrate;
170 adaptivedemux_class->stream_free = gst_hls_demux_stream_free;
172 adaptivedemux_class->start_fragment = gst_hls_demux_start_fragment;
173 adaptivedemux_class->finish_fragment = gst_hls_demux_finish_fragment;
174 adaptivedemux_class->data_received = gst_hls_demux_data_received;
176 GST_DEBUG_CATEGORY_INIT (gst_hls_demux_debug, "hlsdemux", 0,
181 gst_hls_demux_init (GstHLSDemux * demux)
183 gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
184 sizeof (GstHLSDemuxStream));
186 demux->keys = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
187 g_mutex_init (&demux->keys_lock);
190 static GstStateChangeReturn
191 gst_hls_demux_change_state (GstElement * element, GstStateChange transition)
193 GstStateChangeReturn ret;
194 GstHLSDemux *demux = GST_HLS_DEMUX (element);
196 switch (transition) {
197 case GST_STATE_CHANGE_READY_TO_PAUSED:
198 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
204 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
206 switch (transition) {
207 case GST_STATE_CHANGE_PAUSED_TO_READY:
208 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
209 g_hash_table_remove_all (demux->keys);
218 gst_hls_demux_create_pad (GstHLSDemux * hlsdemux)
223 name = g_strdup_printf ("src_%u", hlsdemux->srcpad_counter++);
224 pad = gst_pad_new_from_static_template (&srctemplate, name);
231 gst_hls_demux_get_bitrate (GstHLSDemux * hlsdemux)
233 GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (hlsdemux);
235 /* Valid because hlsdemux only has a single output */
236 if (demux->streams) {
237 GstAdaptiveDemuxStream *stream = demux->streams->data;
238 return stream->current_download_rate;
245 gst_hls_demux_stream_clear_pending_data (GstHLSDemuxStream * hls_stream)
247 if (hls_stream->pending_encrypted_data)
248 gst_adapter_clear (hls_stream->pending_encrypted_data);
249 gst_buffer_replace (&hls_stream->pending_decrypted_buffer, NULL);
250 gst_buffer_replace (&hls_stream->pending_typefind_buffer, NULL);
251 hls_stream->current_offset = -1;
252 gst_hls_demux_stream_decrypt_end (hls_stream);
256 gst_hls_demux_clear_all_pending_data (GstHLSDemux * hlsdemux)
258 GstAdaptiveDemux *demux = (GstAdaptiveDemux *) hlsdemux;
261 for (walk = demux->streams; walk != NULL; walk = walk->next) {
262 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (walk->data);
263 gst_hls_demux_stream_clear_pending_data (hls_stream);
269 gst_hls_demux_set_current (GstHLSDemux * self, GstM3U8 * m3u8)
271 GST_M3U8_CLIENT_LOCK (self);
272 if (m3u8 != self->current) {
273 self->current = m3u8;
274 self->current->duration = GST_CLOCK_TIME_NONE;
275 self->current->current_file = NULL;
278 // FIXME: this makes no sense after we just set self->current=m3u8 above (tpm)
279 // also, these values don't necessarily align between different lists
280 m3u8->current_file_duration = self->current->current_file_duration;
281 m3u8->sequence = self->current->sequence;
282 m3u8->sequence_position = self->current->sequence_position;
283 m3u8->highest_sequence_number = self->current->highest_sequence_number;
284 m3u8->first_file_start = self->current->first_file_start;
285 m3u8->last_file_end = self->current->last_file_end;
288 GST_M3U8_CLIENT_UNLOCK (self);
293 gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
295 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
298 GstSeekType start_type, stop_type;
300 gdouble rate, old_rate;
301 GList *walk, *current_file = NULL;
302 GstClockTime current_pos, target_pos;
303 gint64 current_sequence;
304 GstM3U8MediaFile *file;
306 gboolean snap_before, snap_after, snap_nearest, keyunit;
309 old_rate = demux->segment.rate;
311 gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
314 bitrate = gst_hls_demux_get_bitrate (hlsdemux);
316 /* properly cleanup pending decryption status */
317 if (flags & GST_SEEK_FLAG_FLUSH) {
318 gst_hls_demux_clear_all_pending_data (hlsdemux);
321 /* Use I-frame variants for trick modes */
322 if (hlsdemux->master->iframe_variants != NULL
323 && rate < -1.0 && old_rate >= -1.0 && old_rate <= 1.0) {
326 /* Switch to I-frame variant */
327 gst_hls_demux_set_current_variant (hlsdemux,
328 hlsdemux->master->iframe_variants->data);
329 gst_uri_downloader_reset (demux->downloader);
330 if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
331 GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
334 //hlsdemux->discont = TRUE;
336 gst_hls_demux_change_playlist (hlsdemux, bitrate / ABS (rate), NULL);
337 } else if (rate > -1.0 && rate <= 1.0 && (old_rate < -1.0 || old_rate > 1.0)) {
339 /* Switch to normal variant */
340 gst_hls_demux_set_current_variant (hlsdemux,
341 hlsdemux->master->variants->data);
342 gst_uri_downloader_reset (demux->downloader);
343 if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
344 GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
347 //hlsdemux->discont = TRUE;
348 /* TODO why not continue using the same? that was being used up to now? */
349 gst_hls_demux_change_playlist (hlsdemux, bitrate, NULL);
351 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
352 file = GST_M3U8_MEDIA_FILE (hlsdemux->current_variant->m3u8->files->data);
353 current_sequence = file->sequence;
356 target_pos = reverse ? stop : start;
358 /* Snap to segment boundary. Improves seek performance on slow machines. */
359 keyunit = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
361 (flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST;
362 snap_before = ! !(flags & GST_SEEK_FLAG_SNAP_BEFORE);
363 snap_after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
365 /* FIXME: Here we need proper discont handling */
366 for (walk = hlsdemux->current_variant->m3u8->files; walk; walk = walk->next) {
369 current_sequence = file->sequence;
371 if ((!reverse && snap_after) || snap_nearest) {
372 if (current_pos >= target_pos)
374 if (snap_nearest && target_pos - current_pos < file->duration / 2)
376 } else if (reverse && snap_after) {
377 /* check if the next fragment is our target, in this case we want to
378 * start from the previous fragment */
379 GstClockTime next_pos = current_pos + file->duration;
381 if (next_pos <= target_pos && target_pos < next_pos + file->duration) {
384 } else if (current_pos <= target_pos
385 && target_pos < current_pos + file->duration) {
388 current_pos += file->duration;
392 GST_DEBUG_OBJECT (demux, "seeking further than track duration");
396 GST_DEBUG_OBJECT (demux, "seeking to sequence %u", (guint) current_sequence);
397 for (walk = demux->streams; walk != NULL; walk = walk->next)
398 GST_HLS_DEMUX_STREAM_CAST (walk->data)->reset_pts = TRUE;
399 hlsdemux->current_variant->m3u8->sequence = current_sequence;
400 hlsdemux->current_variant->m3u8->current_file =
401 current_file ? current_file : hlsdemux->current_variant->m3u8->files;
402 hlsdemux->current_variant->m3u8->sequence_position = current_pos;
403 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
405 /* Play from the end of the current selected segment */
406 if (reverse && (snap_before || snap_after || snap_nearest))
407 current_pos += file->duration;
409 if (keyunit || snap_before || snap_after || snap_nearest) {
411 gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
412 current_pos, stop_type, stop, NULL);
414 gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
415 start, stop_type, current_pos, NULL);
422 gst_hls_demux_update_manifest (GstAdaptiveDemux * demux)
424 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
425 if (!gst_hls_demux_update_playlist (hlsdemux, TRUE, NULL))
426 return GST_FLOW_ERROR;
432 create_stream_for_playlist (GstAdaptiveDemux * demux, GstM3U8 * playlist)
434 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
435 GstHLSDemuxStream *hlsdemux_stream;
436 GstAdaptiveDemuxStream *stream;
438 stream = gst_adaptive_demux_stream_new (demux,
439 gst_hls_demux_create_pad (hlsdemux));
441 hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
442 hlsdemux_stream->playlist = gst_m3u8_ref (playlist);
444 hlsdemux_stream->do_typefind = TRUE;
445 hlsdemux_stream->reset_pts = TRUE;
449 gst_hls_demux_setup_streams (GstAdaptiveDemux * demux)
451 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
452 GstHLSVariantStream *playlist = hlsdemux->current_variant;
455 if (playlist == NULL) {
456 GST_WARNING_OBJECT (demux, "Can't configure streams - no variant selected");
460 gst_hls_demux_clear_all_pending_data (hlsdemux);
462 /* 1 output for the main playlist */
463 create_stream_for_playlist (demux, playlist->m3u8);
465 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
466 GList *mlist = playlist->media[i];
467 while (mlist != NULL) {
468 GstHLSMedia *media = mlist->data;
470 if (media->uri == NULL /* || media->mtype != GST_HLS_MEDIA_TYPE_AUDIO */ ) {
471 /* No uri means this is a placeholder for a stream
472 * contained in another mux */
473 GST_LOG_OBJECT (demux, "Skipping stream %s type %d with no URI",
474 media->name, media->mtype);
478 GST_LOG_OBJECT (demux, "media of type %d - %s, uri: %s", i,
479 media->name, media->uri);
480 create_stream_for_playlist (demux, media->playlist);
490 gst_adaptive_demux_get_manifest_ref_uri (GstAdaptiveDemux * d)
492 return d->manifest_base_uri ? d->manifest_base_uri : d->manifest_uri;
496 gst_hls_demux_set_current_variant (GstHLSDemux * hlsdemux,
497 GstHLSVariantStream * variant)
499 if (hlsdemux->current_variant == variant || variant == NULL)
502 if (hlsdemux->current_variant != NULL) {
505 //#warning FIXME: Synching fragments across variants
506 // should be done based on media timestamps, and
507 // discont-sequence-numbers not sequence numbers.
508 variant->m3u8->sequence_position =
509 hlsdemux->current_variant->m3u8->sequence_position;
510 variant->m3u8->sequence = hlsdemux->current_variant->m3u8->sequence;
512 GST_DEBUG_OBJECT (hlsdemux,
513 "Switching Variant. Copying over sequence %" G_GINT64_FORMAT
514 " and sequence_pos %" GST_TIME_FORMAT, variant->m3u8->sequence,
515 GST_TIME_ARGS (variant->m3u8->sequence_position));
517 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
518 GList *mlist = hlsdemux->current_variant->media[i];
520 while (mlist != NULL) {
521 GstHLSMedia *old_media = mlist->data;
522 GstHLSMedia *new_media =
523 gst_hls_variant_find_matching_media (variant, old_media);
526 new_media->playlist->sequence = old_media->playlist->sequence;
527 new_media->playlist->sequence_position =
528 old_media->playlist->sequence_position;
534 gst_hls_variant_stream_unref (hlsdemux->current_variant);
537 hlsdemux->current_variant = gst_hls_variant_stream_ref (variant);
542 gst_hls_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
544 GstHLSVariantStream *variant;
545 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
546 gchar *playlist = NULL;
548 GST_INFO_OBJECT (demux, "Initial playlist location: %s (base uri: %s)",
549 demux->manifest_uri, demux->manifest_base_uri);
551 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
552 if (playlist == NULL) {
553 GST_WARNING_OBJECT (demux, "Error validating initial playlist");
557 hlsdemux->master = gst_hls_master_playlist_new_from_data (playlist,
558 gst_adaptive_demux_get_manifest_ref_uri (demux));
560 if (hlsdemux->master == NULL || hlsdemux->master->variants == NULL) {
561 /* In most cases, this will happen if we set a wrong url in the
562 * source element and we have received the 404 HTML response instead of
564 GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Invalid playlist."),
565 ("Could not parse playlist. Check if the URL is correct."));
569 /* select the initial variant stream */
570 if (demux->connection_speed == 0) {
571 variant = hlsdemux->master->default_variant;
574 gst_hls_master_playlist_get_variant_for_bitrate (hlsdemux->master,
575 NULL, demux->connection_speed);
578 GST_INFO_OBJECT (hlsdemux, "selected %s", variant->name);
579 gst_hls_demux_set_current_variant (hlsdemux, variant); // FIXME: inline?
581 /* get the selected media playlist (unless the inital list was one already) */
582 if (!hlsdemux->master->is_simple) {
585 if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
586 GST_ELEMENT_ERROR_FROM_ERROR (demux, "Could not fetch media playlist",
592 return gst_hls_demux_setup_streams (demux);
596 gst_hls_demux_get_duration (GstAdaptiveDemux * demux)
598 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
599 GstClockTime duration = GST_CLOCK_TIME_NONE;
601 if (hlsdemux->current_variant != NULL)
602 duration = gst_m3u8_get_duration (hlsdemux->current_variant->m3u8);
608 gst_hls_demux_is_live (GstAdaptiveDemux * demux)
610 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
611 gboolean is_live = FALSE;
613 if (hlsdemux->current_variant)
614 is_live = gst_hls_variant_stream_is_live (hlsdemux->current_variant);
619 static const GstHLSKey *
620 gst_hls_demux_get_key (GstHLSDemux * demux, const gchar * key_url,
621 const gchar * referer, gboolean allow_cache)
623 GstFragment *key_fragment;
624 GstBuffer *key_buffer;
628 GST_LOG_OBJECT (demux, "Looking up key for key url %s", key_url);
630 g_mutex_lock (&demux->keys_lock);
632 key = g_hash_table_lookup (demux->keys, key_url);
635 GST_LOG_OBJECT (demux, "Found key for key url %s in key cache", key_url);
639 GST_INFO_OBJECT (demux, "Fetching key %s", key_url);
642 gst_uri_downloader_fetch_uri (GST_ADAPTIVE_DEMUX (demux)->downloader,
643 key_url, referer, FALSE, FALSE, allow_cache, &err);
645 if (key_fragment == NULL) {
646 GST_WARNING_OBJECT (demux, "Failed to download key to decrypt data: %s",
647 err ? err->message : "error");
648 g_clear_error (&err);
652 key_buffer = gst_fragment_get_buffer (key_fragment);
654 key = g_new0 (GstHLSKey, 1);
655 if (gst_buffer_extract (key_buffer, 0, key->data, 16) < 16)
656 GST_WARNING_OBJECT (demux, "Download decryption key is too short!");
658 g_hash_table_insert (demux->keys, g_strdup (key_url), key);
660 gst_buffer_unref (key_buffer);
661 g_object_unref (key_fragment);
665 g_mutex_unlock (&demux->keys_lock);
668 GST_MEMDUMP_OBJECT (demux, "Key", key->data, 16);
674 gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
675 GstAdaptiveDemuxStream * stream)
677 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
678 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
679 const GstHLSKey *key;
682 gst_hls_demux_stream_clear_pending_data (hls_stream);
684 /* If no decryption is needed, there's nothing to be done here */
685 if (hls_stream->current_key == NULL)
688 m3u8 = gst_hls_demux_stream_get_m3u8 (hls_stream);
690 key = gst_hls_demux_get_key (hlsdemux, hls_stream->current_key,
691 m3u8->uri, m3u8->allowcache);
696 gst_hls_demux_stream_decrypt_start (hls_stream, key->data,
697 hls_stream->current_iv);
703 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
704 ("Couldn't retrieve key for decryption"), (NULL));
705 GST_WARNING_OBJECT (demux, "Failed to decrypt data");
711 gst_hls_demux_handle_buffer (GstAdaptiveDemux * demux,
712 GstAdaptiveDemuxStream * stream, GstBuffer * buffer, gboolean at_eos)
714 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream); // FIXME: pass HlsStream into function
715 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
720 if (G_UNLIKELY (hls_stream->do_typefind)) {
721 GstCaps *caps = NULL;
724 GstTypeFindProbability prob = GST_TYPE_FIND_NONE;
726 if (hls_stream->pending_typefind_buffer)
727 buffer = gst_buffer_append (hls_stream->pending_typefind_buffer, buffer);
728 hls_stream->pending_typefind_buffer = NULL;
730 gst_buffer_map (buffer, &info, GST_MAP_READ);
731 buffer_size = info.size;
733 /* Typefind could miss if buffer is too small. In this case we
734 * will retry later */
735 if (buffer_size >= (2 * 1024) || at_eos) {
737 gst_type_find_helper_for_data (GST_OBJECT_CAST (hlsdemux), info.data,
740 gst_buffer_unmap (buffer, &info);
742 if (G_UNLIKELY (!caps)) {
743 /* Only fail typefinding if we already a good amount of data
744 * and we still don't know the type */
745 if (buffer_size > (2 * 1024 * 1024) || at_eos) {
746 GST_ELEMENT_ERROR (hlsdemux, STREAM, TYPE_NOT_FOUND,
747 ("Could not determine type of stream"), (NULL));
748 gst_buffer_unref (buffer);
749 return GST_FLOW_NOT_NEGOTIATED;
752 hls_stream->pending_typefind_buffer = buffer;
757 GST_DEBUG_OBJECT (hlsdemux, "Typefind result: %" GST_PTR_FORMAT " prob:%d",
760 gst_adaptive_demux_stream_set_caps (stream, caps);
761 hls_stream->do_typefind = FALSE;
763 g_assert (hls_stream->pending_typefind_buffer == NULL);
766 buffer = gst_buffer_make_writable (buffer);
767 GST_BUFFER_OFFSET (buffer) = hls_stream->current_offset;
768 hls_stream->current_offset += gst_buffer_get_size (buffer);
769 GST_BUFFER_OFFSET_END (buffer) = hls_stream->current_offset;
770 return gst_adaptive_demux_stream_push_buffer (stream, buffer);
776 gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
777 GstAdaptiveDemuxStream * stream)
779 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream); // FIXME: pass HlsStream into function
780 GstFlowReturn ret = GST_FLOW_OK;
782 if (hls_stream->current_key)
783 gst_hls_demux_stream_decrypt_end (hls_stream);
785 if (stream->last_ret == GST_FLOW_OK) {
786 if (hls_stream->pending_decrypted_buffer) {
787 if (hls_stream->current_key) {
789 gssize unpadded_size;
791 /* Handle pkcs7 unpadding here */
792 gst_buffer_map (hls_stream->pending_decrypted_buffer, &info,
794 unpadded_size = info.size - info.data[info.size - 1];
795 gst_buffer_unmap (hls_stream->pending_decrypted_buffer, &info);
797 gst_buffer_resize (hls_stream->pending_decrypted_buffer, 0,
802 gst_hls_demux_handle_buffer (demux, stream,
803 hls_stream->pending_decrypted_buffer, TRUE);
804 hls_stream->pending_decrypted_buffer = NULL;
807 gst_hls_demux_stream_clear_pending_data (hls_stream);
809 if (ret == GST_FLOW_OK || ret == GST_FLOW_NOT_LINKED)
810 return gst_adaptive_demux_stream_advance_fragment (demux, stream,
811 stream->fragment.duration);
816 gst_hls_demux_data_received (GstAdaptiveDemux * demux,
817 GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
819 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
820 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
822 if (hls_stream->current_offset == -1)
823 hls_stream->current_offset = 0;
825 /* Is it encrypted? */
826 if (hls_stream->current_key) {
829 GstBuffer *tmp_buffer;
831 if (hls_stream->pending_encrypted_data == NULL)
832 hls_stream->pending_encrypted_data = gst_adapter_new ();
834 gst_adapter_push (hls_stream->pending_encrypted_data, buffer);
835 size = gst_adapter_available (hls_stream->pending_encrypted_data);
837 /* must be a multiple of 16 */
844 buffer = gst_adapter_take_buffer (hls_stream->pending_encrypted_data, size);
846 gst_hls_demux_decrypt_fragment (hlsdemux, hls_stream, buffer, &err);
847 if (buffer == NULL) {
848 GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Failed to decrypt buffer"),
849 ("decryption failed %s", err->message));
851 return GST_FLOW_ERROR;
854 tmp_buffer = hls_stream->pending_decrypted_buffer;
855 hls_stream->pending_decrypted_buffer = buffer;
859 return gst_hls_demux_handle_buffer (demux, stream, buffer, FALSE);
863 gst_hls_demux_stream_free (GstAdaptiveDemuxStream * stream)
865 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
867 if (hls_stream->playlist) {
868 gst_m3u8_unref (hls_stream->playlist);
869 hls_stream->playlist = NULL;
872 if (hls_stream->pending_encrypted_data)
873 g_object_unref (hls_stream->pending_encrypted_data);
875 gst_buffer_replace (&hls_stream->pending_decrypted_buffer, NULL);
876 gst_buffer_replace (&hls_stream->pending_typefind_buffer, NULL);
878 if (hls_stream->current_key) {
879 g_free (hls_stream->current_key);
880 hls_stream->current_key = NULL;
882 if (hls_stream->current_iv) {
883 g_free (hls_stream->current_iv);
884 hls_stream->current_iv = NULL;
886 gst_hls_demux_stream_decrypt_end (hls_stream);
890 gst_hls_demux_stream_get_m3u8 (GstHLSDemuxStream * hlsdemux_stream)
894 m3u8 = hlsdemux_stream->playlist;
900 gst_hls_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
905 m3u8 = gst_hls_demux_stream_get_m3u8 (GST_HLS_DEMUX_STREAM_CAST (stream));
907 has_next = gst_m3u8_has_next_fragment (m3u8, stream->demux->segment.rate > 0);
913 gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream * stream)
915 GstHLSDemuxStream *hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
918 m3u8 = gst_hls_demux_stream_get_m3u8 (hlsdemux_stream);
920 gst_m3u8_advance_fragment (m3u8, stream->demux->segment.rate > 0);
921 hlsdemux_stream->reset_pts = FALSE;
927 gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream * stream)
929 GstHLSDemuxStream *hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
930 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
931 GstM3U8MediaFile *file;
932 GstClockTime sequence_pos;
933 gboolean discont, forward;
936 m3u8 = gst_hls_demux_stream_get_m3u8 (hlsdemux_stream);
938 forward = (stream->demux->segment.rate > 0);
939 file = gst_m3u8_get_next_fragment (m3u8, forward, &sequence_pos, &discont);
942 GST_INFO_OBJECT (hlsdemux, "This playlist doesn't contain more fragments");
949 /* set up our source for download */
950 if (hlsdemux_stream->reset_pts || discont
951 || stream->demux->segment.rate < 0.0) {
952 stream->fragment.timestamp = sequence_pos;
954 stream->fragment.timestamp = GST_CLOCK_TIME_NONE;
957 g_free (hlsdemux_stream->current_key);
958 hlsdemux_stream->current_key = g_strdup (file->key);
959 g_free (hlsdemux_stream->current_iv);
960 hlsdemux_stream->current_iv = g_memdup (file->iv, sizeof (file->iv));
962 g_free (stream->fragment.uri);
963 stream->fragment.uri = g_strdup (file->uri);
965 GST_DEBUG_OBJECT (stream, "URI now %s", file->uri);
967 stream->fragment.range_start = file->offset;
968 if (file->size != -1)
969 stream->fragment.range_end = file->offset + file->size - 1;
971 stream->fragment.range_end = -1;
973 stream->fragment.duration = file->duration;
976 stream->discont = TRUE;
982 gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream, guint64 bitrate)
984 GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (stream->demux);
985 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
986 gboolean changed = FALSE;
988 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
989 if (hlsdemux->master->is_simple) {
990 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
993 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
995 /* Bitrate adaptation during trick modes does not work well */
996 if (demux->segment.rate != 1.0)
999 gst_hls_demux_change_playlist (hlsdemux, bitrate, &changed);
1001 gst_hls_demux_setup_streams (GST_ADAPTIVE_DEMUX_CAST (hlsdemux));
1006 gst_hls_demux_reset (GstAdaptiveDemux * ademux)
1008 GstHLSDemux *demux = GST_HLS_DEMUX_CAST (ademux);
1010 if (demux->master) {
1011 gst_hls_master_playlist_unref (demux->master);
1012 demux->master = NULL;
1014 if (demux->current_variant != NULL) {
1015 gst_hls_variant_stream_unref (demux->current_variant);
1016 demux->current_variant = NULL;
1018 demux->srcpad_counter = 0;
1020 gst_hls_demux_clear_all_pending_data (demux);
1024 gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf)
1029 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1032 if (!g_utf8_validate ((gchar *) info.data, info.size, NULL))
1033 goto validate_error;
1035 /* alloc size + 1 to end with a null character */
1036 playlist = g_malloc0 (info.size + 1);
1037 memcpy (playlist, info.data, info.size);
1039 gst_buffer_unmap (buf, &info);
1043 gst_buffer_unmap (buf, &info);
1049 gst_hls_demux_find_variant_match (const GstHLSVariantStream * a,
1050 const GstHLSVariantStream * b)
1052 if (g_strcmp0 (a->name, b->name) == 0 &&
1053 a->bandwidth == b->bandwidth &&
1054 a->program_id == b->program_id &&
1055 g_strcmp0 (a->codecs, b->codecs) == 0 &&
1056 a->width == b->width &&
1057 a->height == b->height && a->iframe == b->iframe) {
1064 /* Update the master playlist, which contains the list of available
1067 gst_hls_demux_update_variant_playlist (GstHLSDemux * hlsdemux, gchar * data,
1068 const gchar * uri, const gchar * base_uri)
1070 GstHLSMasterPlaylist *new_master, *old;
1071 gboolean ret = FALSE;
1072 GList *l, *unmatched_lists;
1073 GstHLSVariantStream *new_variant;
1075 new_master = gst_hls_master_playlist_new_from_data (data, base_uri ? base_uri : uri); // FIXME: check which uri to use here
1077 if (new_master == NULL)
1080 if (new_master->is_simple) {
1081 // FIXME: we should be able to support this though, in the unlikely
1082 // case that it changed?
1084 ("Cannot update variant playlist: New playlist is not a variant playlist");
1085 gst_hls_master_playlist_unref (new_master);
1089 GST_M3U8_CLIENT_LOCK (self);
1091 if (hlsdemux->master->is_simple) {
1093 ("Cannot update variant playlist: Current playlist is not a variant playlist");
1097 /* Now see if the variant playlist still has the same lists */
1098 unmatched_lists = g_list_copy (hlsdemux->master->variants);
1099 for (l = new_master->variants; l != NULL; l = l->next) {
1100 GList *match = g_list_find_custom (unmatched_lists, l->data,
1101 (GCompareFunc) gst_hls_demux_find_variant_match);
1104 GstHLSVariantStream *variant = l->data;
1105 GstHLSVariantStream *old = match->data;
1107 unmatched_lists = g_list_delete_link (unmatched_lists, match);
1108 /* FIXME: Deal with losing position due to missing an update */
1109 variant->m3u8->sequence_position = old->m3u8->sequence_position;
1110 variant->m3u8->sequence = old->m3u8->sequence;
1114 if (unmatched_lists != NULL) {
1115 GST_WARNING ("Unable to match all playlists");
1117 for (l = unmatched_lists; l != NULL; l = l->next) {
1118 if (l->data == hlsdemux->current_variant) {
1119 GST_WARNING ("Unable to match current playlist");
1123 g_list_free (unmatched_lists);
1126 /* Switch out the variant playlist */
1127 old = hlsdemux->master;
1129 // FIXME: check all this and also switch of variants, if anything needs updating
1130 hlsdemux->master = new_master;
1132 if (hlsdemux->current_variant == NULL) {
1133 new_variant = new_master->default_variant;
1135 /* Find the same variant in the new playlist */
1137 gst_hls_master_playlist_get_matching_variant (new_master,
1138 hlsdemux->current_variant);
1141 /* Use the function to set the current variant, as it copies over data */
1142 if (new_variant != NULL)
1143 gst_hls_demux_set_current_variant (hlsdemux, new_variant);
1145 gst_hls_master_playlist_unref (old);
1147 ret = (hlsdemux->current_variant != NULL);
1149 GST_M3U8_CLIENT_UNLOCK (self);
1155 gst_hls_demux_update_rendition_manifest (GstHLSDemux * demux,
1156 GstHLSMedia * media, GError ** err)
1158 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
1159 GstFragment *download;
1162 const gchar *main_uri;
1164 gchar *uri = media->uri;
1166 main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1168 gst_uri_downloader_fetch_uri (adaptive_demux->downloader, uri, main_uri,
1169 TRUE, TRUE, TRUE, err);
1171 if (download == NULL)
1174 m3u8 = media->playlist;
1176 /* Set the base URI of the playlist to the redirect target if any */
1177 if (download->redirect_permanent && download->redirect_uri) {
1178 gst_m3u8_set_uri (m3u8, download->redirect_uri, NULL, media->name);
1180 gst_m3u8_set_uri (m3u8, download->uri, download->redirect_uri, media->name);
1183 buf = gst_fragment_get_buffer (download);
1184 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1185 gst_buffer_unref (buf);
1186 g_object_unref (download);
1188 if (playlist == NULL) {
1189 GST_WARNING_OBJECT (demux, "Couldn't validate playlist encoding");
1190 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1191 "Couldn't validate playlist encoding");
1195 if (!gst_m3u8_update (m3u8, playlist)) {
1196 GST_WARNING_OBJECT (demux, "Couldn't update playlist");
1197 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
1198 "Couldn't update playlist");
1206 gst_hls_demux_update_playlist (GstHLSDemux * demux, gboolean update,
1209 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
1210 GstFragment *download;
1213 gboolean main_checked = FALSE;
1214 const gchar *main_uri;
1220 uri = gst_m3u8_get_uri (demux->current_variant->m3u8);
1221 main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1223 gst_uri_downloader_fetch_uri (adaptive_demux->downloader, uri, main_uri,
1224 TRUE, TRUE, TRUE, err);
1225 if (download == NULL) {
1228 if (!update || main_checked || demux->master->is_simple) {
1232 g_clear_error (err);
1233 GST_INFO_OBJECT (demux,
1234 "Updating playlist %s failed, attempt to refresh variant playlist %s",
1237 gst_uri_downloader_fetch_uri (adaptive_demux->downloader,
1238 main_uri, NULL, TRUE, TRUE, TRUE, err);
1239 if (download == NULL) {
1244 buf = gst_fragment_get_buffer (download);
1245 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1246 gst_buffer_unref (buf);
1248 if (playlist == NULL) {
1249 GST_WARNING_OBJECT (demux,
1250 "Failed to validate variant playlist encoding");
1252 g_object_unref (download);
1253 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1254 "Couldn't validate playlist encoding");
1259 if (download->redirect_permanent && download->redirect_uri) {
1260 uri = download->redirect_uri;
1263 uri = download->uri;
1264 base_uri = download->redirect_uri;
1267 if (!gst_hls_demux_update_variant_playlist (demux, playlist, uri, base_uri)) {
1268 GST_WARNING_OBJECT (demux, "Failed to update the variant playlist");
1269 g_object_unref (download);
1270 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
1271 "Couldn't update playlist");
1275 g_object_unref (download);
1277 main_checked = TRUE;
1282 m3u8 = demux->current_variant->m3u8;
1284 /* Set the base URI of the playlist to the redirect target if any */
1285 if (download->redirect_permanent && download->redirect_uri) {
1286 gst_m3u8_set_uri (m3u8, download->redirect_uri, NULL,
1287 demux->current_variant->name);
1289 gst_m3u8_set_uri (m3u8, download->uri, download->redirect_uri,
1290 demux->current_variant->name);
1293 buf = gst_fragment_get_buffer (download);
1294 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1295 gst_buffer_unref (buf);
1296 g_object_unref (download);
1298 if (playlist == NULL) {
1299 GST_WARNING_OBJECT (demux, "Couldn't validate playlist encoding");
1300 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1301 "Couldn't validate playlist encoding");
1305 if (!gst_m3u8_update (m3u8, playlist)) {
1306 GST_WARNING_OBJECT (demux, "Couldn't update playlist");
1307 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
1308 "Couldn't update playlist");
1312 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
1313 GList *mlist = demux->current_variant->media[i];
1315 while (mlist != NULL) {
1316 GstHLSMedia *media = mlist->data;
1318 if (media->uri == NULL) {
1319 /* No uri means this is a placeholder for a stream
1320 * contained in another mux */
1321 mlist = mlist->next;
1324 GST_LOG_OBJECT (demux,
1325 "Updating playlist for media of type %d - %s, uri: %s", i,
1326 media->name, media->uri);
1328 if (!gst_hls_demux_update_rendition_manifest (demux, media, err))
1331 mlist = mlist->next;
1335 /* If it's a live source, do not let the sequence number go beyond
1336 * three fragments before the end of the list */
1337 if (update == FALSE && gst_m3u8_is_live (m3u8)) {
1338 gint64 last_sequence, first_sequence;
1340 GST_M3U8_CLIENT_LOCK (demux->client);
1342 GST_M3U8_MEDIA_FILE (g_list_last (m3u8->files)->data)->sequence;
1344 GST_M3U8_MEDIA_FILE (g_list_first (m3u8->files)->data)->sequence;
1346 GST_DEBUG_OBJECT (demux,
1347 "sequence:%" G_GINT64_FORMAT " , first_sequence:%" G_GINT64_FORMAT
1348 " , last_sequence:%" G_GINT64_FORMAT, m3u8->sequence,
1349 first_sequence, last_sequence);
1350 if (m3u8->sequence >= last_sequence - 3) {
1351 //demux->need_segment = TRUE;
1352 /* Make sure we never go below the minimum sequence number */
1353 m3u8->sequence = MAX (first_sequence, last_sequence - 3);
1354 GST_DEBUG_OBJECT (demux,
1355 "Sequence is beyond playlist. Moving back to %" G_GINT64_FORMAT,
1358 GST_M3U8_CLIENT_UNLOCK (demux->client);
1359 } else if (!gst_m3u8_is_live (m3u8)) {
1360 GstClockTime current_pos, target_pos;
1364 /* Sequence numbers are not guaranteed to be the same in different
1365 * playlists, so get the correct fragment here based on the current
1368 GST_M3U8_CLIENT_LOCK (demux->client);
1370 /* Valid because hlsdemux only has a single output */
1371 if (GST_ADAPTIVE_DEMUX_CAST (demux)->streams) {
1372 GstAdaptiveDemuxStream *stream =
1373 GST_ADAPTIVE_DEMUX_CAST (demux)->streams->data;
1374 target_pos = stream->segment.position;
1378 if (GST_CLOCK_TIME_IS_VALID (m3u8->sequence_position)) {
1379 target_pos = MAX (target_pos, m3u8->sequence_position);
1382 GST_LOG_OBJECT (demux, "Looking for sequence position %"
1383 GST_TIME_FORMAT " in updated playlist", GST_TIME_ARGS (target_pos));
1386 for (walk = m3u8->files; walk; walk = walk->next) {
1387 GstM3U8MediaFile *file = walk->data;
1389 sequence = file->sequence;
1390 if (current_pos <= target_pos
1391 && target_pos < current_pos + file->duration) {
1394 current_pos += file->duration;
1396 /* End of playlist */
1399 m3u8->sequence = sequence;
1400 m3u8->sequence_position = current_pos;
1401 GST_M3U8_CLIENT_UNLOCK (demux->client);
1408 gst_hls_demux_change_playlist (GstHLSDemux * demux, guint max_bitrate,
1411 GstHLSVariantStream *lowest_variant, *lowest_ivariant;
1412 GstHLSVariantStream *previous_variant, *new_variant;
1413 gint old_bandwidth, new_bandwidth;
1414 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX_CAST (demux);
1415 GstAdaptiveDemuxStream *stream;
1417 g_return_val_if_fail (adaptive_demux->streams != NULL, FALSE);
1419 stream = adaptive_demux->streams->data;
1421 previous_variant = demux->current_variant;
1423 gst_hls_master_playlist_get_variant_for_bitrate (demux->master,
1424 demux->current_variant, max_bitrate);
1426 GST_M3U8_CLIENT_LOCK (demux->client);
1428 retry_failover_protection:
1429 old_bandwidth = previous_variant->bandwidth;
1430 new_bandwidth = new_variant->bandwidth;
1432 /* Don't do anything else if the playlist is the same */
1433 if (new_bandwidth == old_bandwidth) {
1434 GST_M3U8_CLIENT_UNLOCK (demux->client);
1438 GST_M3U8_CLIENT_UNLOCK (demux->client);
1440 gst_hls_demux_set_current_variant (demux, new_variant);
1442 GST_INFO_OBJECT (demux, "Client was on %dbps, max allowed is %dbps, switching"
1443 " to bitrate %dbps", old_bandwidth, max_bitrate, new_bandwidth);
1445 if (gst_hls_demux_update_playlist (demux, TRUE, NULL)) {
1446 const gchar *main_uri;
1449 uri = gst_m3u8_get_uri (new_variant->m3u8);
1450 main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1451 gst_element_post_message (GST_ELEMENT_CAST (demux),
1452 gst_message_new_element (GST_OBJECT_CAST (demux),
1453 gst_structure_new (GST_ADAPTIVE_DEMUX_STATISTICS_MESSAGE_NAME,
1454 "manifest-uri", G_TYPE_STRING,
1455 main_uri, "uri", G_TYPE_STRING,
1456 uri, "bitrate", G_TYPE_INT, new_bandwidth, NULL)));
1460 stream->discont = TRUE;
1462 GstHLSVariantStream *failover_variant = NULL;
1465 GST_INFO_OBJECT (demux, "Unable to update playlist. Switching back");
1466 GST_M3U8_CLIENT_LOCK (demux->client);
1468 /* we find variants by bitrate by going from highest to lowest, so it's
1469 * possible that there's another variant with the same bitrate before the
1470 * one selected which we can use as failover */
1471 failover = g_list_find (demux->master->variants, new_variant);
1472 if (failover != NULL)
1473 failover = failover->prev;
1474 if (failover != NULL)
1475 failover_variant = failover->data;
1476 if (failover_variant && new_bandwidth == failover_variant->bandwidth) {
1477 new_variant = failover_variant;
1478 goto retry_failover_protection;
1481 GST_M3U8_CLIENT_UNLOCK (demux->client);
1482 gst_hls_demux_set_current_variant (demux, previous_variant);
1483 /* Try a lower bitrate (or stop if we just tried the lowest) */
1484 lowest_variant = demux->master->variants->data;
1485 lowest_ivariant = demux->master->iframe_variants->data;
1486 if (previous_variant->iframe && new_bandwidth == lowest_ivariant->bandwidth)
1488 if (!previous_variant->iframe && new_bandwidth == lowest_variant->bandwidth)
1491 return gst_hls_demux_change_playlist (demux, new_bandwidth - 1, changed);
1497 #if defined(HAVE_OPENSSL)
1499 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1500 const guint8 * key_data, const guint8 * iv_data)
1502 EVP_CIPHER_CTX_init (&stream->aes_ctx);
1503 if (!EVP_DecryptInit_ex (&stream->aes_ctx, EVP_aes_128_cbc (), NULL, key_data,
1506 EVP_CIPHER_CTX_set_padding (&stream->aes_ctx, 0);
1511 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1512 const guint8 * encrypted_data, guint8 * decrypted_data)
1516 if (G_UNLIKELY (length > G_MAXINT || length % 16 != 0))
1520 if (!EVP_DecryptUpdate (&stream->aes_ctx, decrypted_data, &len,
1521 encrypted_data, len))
1523 EVP_DecryptFinal_ex (&stream->aes_ctx, decrypted_data + len, &flen);
1524 g_return_val_if_fail (len + flen == length, FALSE);
1529 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1531 EVP_CIPHER_CTX_cleanup (&stream->aes_ctx);
1534 #elif defined(HAVE_NETTLE)
1536 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1537 const guint8 * key_data, const guint8 * iv_data)
1539 aes_set_decrypt_key (&stream->aes_ctx.ctx, 16, key_data);
1540 CBC_SET_IV (&stream->aes_ctx, iv_data);
1546 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1547 const guint8 * encrypted_data, guint8 * decrypted_data)
1549 if (length % 16 != 0)
1552 CBC_DECRYPT (&stream->aes_ctx, aes_decrypt, length, decrypted_data,
1559 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1566 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1567 const guint8 * key_data, const guint8 * iv_data)
1569 gcry_error_t err = 0;
1570 gboolean ret = FALSE;
1573 gcry_cipher_open (&stream->aes_ctx, GCRY_CIPHER_AES128,
1574 GCRY_CIPHER_MODE_CBC, 0);
1577 err = gcry_cipher_setkey (stream->aes_ctx, key_data, 16);
1580 err = gcry_cipher_setiv (stream->aes_ctx, iv_data, 16);
1586 if (stream->aes_ctx)
1587 gcry_cipher_close (stream->aes_ctx);
1593 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1594 const guint8 * encrypted_data, guint8 * decrypted_data)
1596 gcry_error_t err = 0;
1598 err = gcry_cipher_decrypt (stream->aes_ctx, decrypted_data, length,
1599 encrypted_data, length);
1605 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1607 if (stream->aes_ctx) {
1608 gcry_cipher_close (stream->aes_ctx);
1609 stream->aes_ctx = NULL;
1615 gst_hls_demux_decrypt_fragment (GstHLSDemux * demux, GstHLSDemuxStream * stream,
1616 GstBuffer * encrypted_buffer, GError ** err)
1618 GstBuffer *decrypted_buffer = NULL;
1619 GstMapInfo encrypted_info, decrypted_info;
1622 gst_buffer_new_allocate (NULL, gst_buffer_get_size (encrypted_buffer),
1625 gst_buffer_map (encrypted_buffer, &encrypted_info, GST_MAP_READ);
1626 gst_buffer_map (decrypted_buffer, &decrypted_info, GST_MAP_WRITE);
1628 if (!decrypt_fragment (stream, encrypted_info.size,
1629 encrypted_info.data, decrypted_info.data))
1633 gst_buffer_unmap (decrypted_buffer, &decrypted_info);
1634 gst_buffer_unmap (encrypted_buffer, &encrypted_info);
1636 gst_buffer_unref (encrypted_buffer);
1638 return decrypted_buffer;
1641 GST_ERROR_OBJECT (demux, "Failed to decrypt fragment");
1642 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_DECRYPT,
1643 "Failed to decrypt fragment");
1645 gst_buffer_unmap (decrypted_buffer, &decrypted_info);
1646 gst_buffer_unmap (encrypted_buffer, &encrypted_info);
1648 gst_buffer_unref (encrypted_buffer);
1649 gst_buffer_unref (decrypted_buffer);
1655 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
1657 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1658 GstClockTime target_duration;
1660 if (hlsdemux->current_variant) {
1662 gst_m3u8_get_target_duration (hlsdemux->current_variant->m3u8);
1664 target_duration = 5 * GST_SECOND;
1667 return gst_util_uint64_scale (target_duration, G_USEC_PER_SEC, GST_SECOND);
1671 gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
1674 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1675 gboolean ret = FALSE;
1677 if (hlsdemux->current_variant) {
1679 gst_m3u8_get_seek_range (hlsdemux->current_variant->m3u8, start, stop);