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>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
27 * SECTION:element-hlsdemux
29 * HTTP Live Streaming demuxer element.
32 * <title>Example launch line</title>
34 * gst-launch-1.0 souphttpsrc location=http://devimages.apple.com/iphone/samples/bipbop/gear4/prog_index.m3u8 ! hlsdemux ! decodebin ! videoconvert ! videoscale ! autovideosink
44 #include <gst/base/gsttypefindhelper.h>
45 #include "gsthlsdemux.h"
47 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
52 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS ("application/x-hls"));
57 GST_DEBUG_CATEGORY_STATIC (gst_hls_demux_debug);
58 #define GST_CAT_DEFAULT gst_hls_demux_debug
61 static void gst_hls_demux_finalize (GObject * obj);
64 static GstStateChangeReturn
65 gst_hls_demux_change_state (GstElement * element, GstStateChange transition);
68 static gboolean gst_hls_demux_update_playlist (GstHLSDemux * demux,
69 gboolean update, GError ** err);
70 static gchar *gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf);
72 static gboolean gst_hls_demux_change_playlist (GstHLSDemux * demux,
73 guint max_bitrate, gboolean * changed);
74 static GstBuffer *gst_hls_demux_decrypt_fragment (GstHLSDemux * demux,
75 GstBuffer * encrypted_buffer, GError ** err);
77 gst_hls_demux_decrypt_start (GstHLSDemux * demux, const guint8 * key_data,
78 const guint8 * iv_data);
79 static void gst_hls_demux_decrypt_end (GstHLSDemux * demux);
81 static gboolean gst_hls_demux_is_live (GstAdaptiveDemux * demux);
82 static GstClockTime gst_hls_demux_get_duration (GstAdaptiveDemux * demux);
83 static gint64 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux *
85 static gboolean gst_hls_demux_process_manifest (GstAdaptiveDemux * demux,
87 static GstFlowReturn gst_hls_demux_update_manifest (GstAdaptiveDemux * demux);
88 static gboolean gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
90 gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
91 GstAdaptiveDemuxStream * stream);
92 static GstFlowReturn gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
93 GstAdaptiveDemuxStream * stream);
94 static GstFlowReturn gst_hls_demux_data_received (GstAdaptiveDemux * demux,
95 GstAdaptiveDemuxStream * stream, GstBuffer * buffer);
96 static gboolean gst_hls_demux_stream_has_next_fragment (GstAdaptiveDemuxStream *
98 static GstFlowReturn gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream *
100 static GstFlowReturn gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream
102 static gboolean gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream,
104 static void gst_hls_demux_reset (GstAdaptiveDemux * demux);
105 static gboolean gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux,
106 gint64 * start, gint64 * stop);
108 #define gst_hls_demux_parent_class parent_class
109 G_DEFINE_TYPE (GstHLSDemux, gst_hls_demux, GST_TYPE_ADAPTIVE_DEMUX);
112 gst_hls_demux_finalize (GObject * obj)
114 GstHLSDemux *demux = GST_HLS_DEMUX (obj);
116 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
117 g_object_unref (demux->pending_encrypted_data);
118 gst_m3u8_client_free (demux->client);
120 G_OBJECT_CLASS (parent_class)->finalize (obj);
124 gst_hls_demux_class_init (GstHLSDemuxClass * klass)
126 GObjectClass *gobject_class;
127 GstElementClass *element_class;
128 GstAdaptiveDemuxClass *adaptivedemux_class;
130 gobject_class = (GObjectClass *) klass;
131 element_class = (GstElementClass *) klass;
132 adaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
134 gobject_class->finalize = gst_hls_demux_finalize;
136 element_class->change_state = GST_DEBUG_FUNCPTR (gst_hls_demux_change_state);
138 gst_element_class_add_static_pad_template (element_class, &srctemplate);
139 gst_element_class_add_static_pad_template (element_class, &sinktemplate);
141 gst_element_class_set_static_metadata (element_class,
143 "Codec/Demuxer/Adaptive",
144 "HTTP Live Streaming demuxer",
145 "Marc-Andre Lureau <marcandre.lureau@gmail.com>\n"
146 "Andoni Morales Alastruey <ylatuya@gmail.com>");
148 adaptivedemux_class->is_live = gst_hls_demux_is_live;
149 adaptivedemux_class->get_live_seek_range = gst_hls_demux_get_live_seek_range;
150 adaptivedemux_class->get_duration = gst_hls_demux_get_duration;
151 adaptivedemux_class->get_manifest_update_interval =
152 gst_hls_demux_get_manifest_update_interval;
153 adaptivedemux_class->process_manifest = gst_hls_demux_process_manifest;
154 adaptivedemux_class->update_manifest = gst_hls_demux_update_manifest;
155 adaptivedemux_class->reset = gst_hls_demux_reset;
156 adaptivedemux_class->seek = gst_hls_demux_seek;
157 adaptivedemux_class->stream_has_next_fragment =
158 gst_hls_demux_stream_has_next_fragment;
159 adaptivedemux_class->stream_advance_fragment = gst_hls_demux_advance_fragment;
160 adaptivedemux_class->stream_update_fragment_info =
161 gst_hls_demux_update_fragment_info;
162 adaptivedemux_class->stream_select_bitrate = gst_hls_demux_select_bitrate;
164 adaptivedemux_class->start_fragment = gst_hls_demux_start_fragment;
165 adaptivedemux_class->finish_fragment = gst_hls_demux_finish_fragment;
166 adaptivedemux_class->data_received = gst_hls_demux_data_received;
168 GST_DEBUG_CATEGORY_INIT (gst_hls_demux_debug, "hlsdemux", 0,
173 gst_hls_demux_init (GstHLSDemux * demux)
175 demux->do_typefind = TRUE;
176 demux->pending_encrypted_data = gst_adapter_new ();
179 static GstStateChangeReturn
180 gst_hls_demux_change_state (GstElement * element, GstStateChange transition)
182 GstStateChangeReturn ret;
183 GstHLSDemux *demux = GST_HLS_DEMUX (element);
185 switch (transition) {
186 case GST_STATE_CHANGE_READY_TO_PAUSED:
187 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
193 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
195 switch (transition) {
196 case GST_STATE_CHANGE_PAUSED_TO_READY:
197 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
206 gst_hls_demux_create_pad (GstHLSDemux * hlsdemux)
211 name = g_strdup_printf ("src_%u", hlsdemux->srcpad_counter++);
212 pad = gst_pad_new_from_static_template (&srctemplate, name);
219 gst_hls_demux_get_bitrate (GstHLSDemux * hlsdemux)
221 GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (hlsdemux);
223 /* Valid because hlsdemux only has a single output */
224 if (demux->streams) {
225 GstAdaptiveDemuxStream *stream = demux->streams->data;
226 return stream->current_download_rate;
233 gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
235 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
238 GstSeekType start_type, stop_type;
241 GList *walk, *current_file = NULL;
242 GstClockTime current_pos, target_pos;
243 gint64 current_sequence;
244 GstM3U8MediaFile *file;
246 gboolean snap_before, snap_after, snap_nearest, keyunit;
249 gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
252 bitrate = gst_hls_demux_get_bitrate (hlsdemux);
254 /* properly cleanup pending decryption status */
255 if (flags & GST_SEEK_FLAG_FLUSH) {
256 gst_hls_demux_decrypt_end (hlsdemux);
259 /* Use I-frame variants for trick modes */
260 if (hlsdemux->client->main->iframe_lists && rate < -1.0
261 && demux->segment.rate >= -1.0 && demux->segment.rate <= 1.0) {
264 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
265 /* Switch to I-frame variant */
266 hlsdemux->client->main->current_variant =
267 hlsdemux->client->main->iframe_lists;
268 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
269 gst_m3u8_client_set_current (hlsdemux->client,
270 hlsdemux->client->main->iframe_lists->data);
271 gst_uri_downloader_reset (demux->downloader);
272 if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
273 GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
276 //hlsdemux->discont = TRUE;
277 hlsdemux->do_typefind = TRUE;
279 gst_hls_demux_change_playlist (hlsdemux, bitrate / ABS (rate), NULL);
280 } else if (rate > -1.0 && rate <= 1.0 && (demux->segment.rate < -1.0
281 || demux->segment.rate > 1.0)) {
283 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
284 /* Switch to normal variant */
285 hlsdemux->client->main->current_variant = hlsdemux->client->main->lists;
286 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
287 gst_m3u8_client_set_current (hlsdemux->client,
288 hlsdemux->client->main->lists->data);
289 gst_uri_downloader_reset (demux->downloader);
290 if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
291 GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
294 //hlsdemux->discont = TRUE;
295 hlsdemux->do_typefind = TRUE;
296 /* TODO why not continue using the same? that was being used up to now? */
297 gst_hls_demux_change_playlist (hlsdemux, bitrate, NULL);
300 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
301 file = GST_M3U8_MEDIA_FILE (hlsdemux->client->current->files->data);
302 current_sequence = file->sequence;
305 target_pos = reverse ? stop : start;
307 /* Snap to segment boundary. Improves seek performance on slow machines. */
308 keyunit = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
310 (flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST;
311 snap_before = ! !(flags & GST_SEEK_FLAG_SNAP_BEFORE);
312 snap_after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
314 /* FIXME: Here we need proper discont handling */
315 for (walk = hlsdemux->client->current->files; walk; walk = walk->next) {
318 current_sequence = file->sequence;
320 if ((!reverse && snap_after) || snap_nearest) {
321 if (current_pos >= target_pos)
323 if (snap_nearest && target_pos - current_pos < file->duration / 2)
325 } else if (reverse && snap_after) {
326 /* check if the next fragment is our target, in this case we want to
327 * start from the previous fragment */
328 GstClockTime next_pos = current_pos + file->duration;
330 if (next_pos <= target_pos && target_pos < next_pos + file->duration) {
333 } else if (current_pos <= target_pos
334 && target_pos < current_pos + file->duration) {
337 current_pos += file->duration;
341 GST_DEBUG_OBJECT (demux, "seeking further than track duration");
345 GST_DEBUG_OBJECT (demux, "seeking to sequence %u", (guint) current_sequence);
346 hlsdemux->reset_pts = TRUE;
347 hlsdemux->client->sequence = current_sequence;
348 hlsdemux->client->current_file =
349 current_file ? current_file : hlsdemux->client->current->files;
350 hlsdemux->client->sequence_position = current_pos;
351 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
353 /* Play from the end of the current selected segment */
354 if (reverse && (snap_before || snap_after || snap_nearest))
355 current_pos += file->duration;
357 if (keyunit || snap_before || snap_after || snap_nearest) {
359 gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
360 current_pos, stop_type, stop, NULL);
362 gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
363 start, stop_type, current_pos, NULL);
370 gst_hls_demux_update_manifest (GstAdaptiveDemux * demux)
372 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
373 if (!gst_hls_demux_update_playlist (hlsdemux, TRUE, NULL))
374 return GST_FLOW_ERROR;
380 gst_hls_demux_setup_streams (GstAdaptiveDemux * demux)
382 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
384 /* only 1 output supported */
385 gst_adaptive_demux_stream_new (demux, gst_hls_demux_create_pad (hlsdemux));
387 hlsdemux->reset_pts = TRUE;
394 gst_hls_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
396 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
397 gchar *playlist = NULL;
399 if (hlsdemux->client)
400 gst_m3u8_client_free (hlsdemux->client);
403 gst_m3u8_client_new (demux->manifest_uri, demux->manifest_base_uri);
405 GST_INFO_OBJECT (demux, "Changed location: %s (base uri: %s)",
406 demux->manifest_uri, GST_STR_NULL (demux->manifest_base_uri));
408 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
409 if (playlist == NULL) {
410 GST_WARNING_OBJECT (demux, "Error validating first playlist.");
412 } else if (!gst_m3u8_client_update (hlsdemux->client, playlist)) {
413 /* In most cases, this will happen if we set a wrong url in the
414 * source element and we have received the 404 HTML response instead of
416 GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Invalid playlist."), (NULL));
420 /* If this playlist is a variant playlist, select the first one
422 if (gst_m3u8_client_has_variant_playlist (hlsdemux->client)) {
423 GstM3U8 *child = NULL;
426 if (demux->connection_speed == 0) {
427 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
428 child = hlsdemux->client->main->current_variant->data;
429 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
431 GList *tmp = gst_m3u8_client_get_playlist_for_bitrate (hlsdemux->client,
432 demux->connection_speed);
433 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
434 hlsdemux->client->main->current_variant = tmp;
435 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
437 child = GST_M3U8 (tmp->data);
440 gst_m3u8_client_set_current (hlsdemux->client, child);
441 if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
442 GST_ELEMENT_ERROR_FROM_ERROR (demux, "Could not fetch the child playlist",
448 return gst_hls_demux_setup_streams (demux);
452 gst_hls_demux_get_duration (GstAdaptiveDemux * demux)
454 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
456 return gst_m3u8_client_get_duration (hlsdemux->client);
460 gst_hls_demux_is_live (GstAdaptiveDemux * demux)
462 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
464 return gst_m3u8_client_is_live (hlsdemux->client);
468 gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
469 GstAdaptiveDemuxStream * stream)
471 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
473 if (hlsdemux->current_key) {
475 GstFragment *key_fragment;
476 GstBuffer *key_buffer;
480 if (hlsdemux->key_url
481 && strcmp (hlsdemux->key_url, hlsdemux->current_key) == 0) {
482 key_fragment = g_object_ref (hlsdemux->key_fragment);
484 g_free (hlsdemux->key_url);
485 hlsdemux->key_url = NULL;
487 if (hlsdemux->key_fragment)
488 g_object_unref (hlsdemux->key_fragment);
489 hlsdemux->key_fragment = NULL;
491 GST_INFO_OBJECT (demux, "Fetching key %s", hlsdemux->current_key);
493 gst_uri_downloader_fetch_uri (demux->downloader,
494 hlsdemux->current_key, hlsdemux->client->main ?
495 hlsdemux->client->main->uri : NULL, FALSE, FALSE,
496 hlsdemux->client->current ? hlsdemux->client->current->
497 allowcache : TRUE, &err);
498 if (key_fragment == NULL)
500 hlsdemux->key_url = g_strdup (hlsdemux->current_key);
501 hlsdemux->key_fragment = g_object_ref (key_fragment);
504 key_buffer = gst_fragment_get_buffer (key_fragment);
505 gst_buffer_map (key_buffer, &key_info, GST_MAP_READ);
507 gst_hls_demux_decrypt_start (hlsdemux, key_info.data, hlsdemux->current_iv);
509 gst_buffer_unmap (key_buffer, &key_info);
510 gst_buffer_unref (key_buffer);
511 g_object_unref (key_fragment);
518 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
519 ("Couldn't retrieve key for decryption"), (NULL));
520 GST_WARNING_OBJECT (demux, "Failed to decrypt data");
525 /* Handles decrypted buffers only */
527 gst_hls_demux_handle_buffer (GstAdaptiveDemux * demux,
528 GstAdaptiveDemuxStream * stream, GstBuffer * buffer, gboolean force)
530 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
532 if (G_UNLIKELY (hlsdemux->do_typefind && buffer != NULL)) {
533 GstCaps *caps = NULL;
536 GstTypeFindProbability prob = GST_TYPE_FIND_NONE;
538 if (hlsdemux->pending_typefind_buffer)
539 buffer = gst_buffer_append (hlsdemux->pending_typefind_buffer, buffer);
540 hlsdemux->pending_typefind_buffer = NULL;
542 gst_buffer_map (buffer, &info, GST_MAP_READ);
543 buffer_size = info.size;
545 /* Typefind could miss if buffer is too small. In this case we
546 * will retry later */
547 if (buffer_size >= (2 * 1024)) {
549 gst_type_find_helper_for_data (GST_OBJECT_CAST (hlsdemux), info.data,
552 gst_buffer_unmap (buffer, &info);
554 if (G_UNLIKELY (!caps)) {
555 /* Only fail typefinding if we already a good amount of data
556 * and we still don't know the type */
557 if (buffer_size > (2 * 1024 * 1024) || force) {
558 GST_ELEMENT_ERROR (hlsdemux, STREAM, TYPE_NOT_FOUND,
559 ("Could not determine type of stream"), (NULL));
560 gst_buffer_unref (buffer);
561 return GST_FLOW_NOT_NEGOTIATED;
563 hlsdemux->pending_typefind_buffer = buffer;
568 GST_DEBUG_OBJECT (hlsdemux, "Typefind result: %" GST_PTR_FORMAT " prob:%d",
571 gst_adaptive_demux_stream_set_caps (stream, caps);
572 hlsdemux->do_typefind = FALSE;
575 g_assert (hlsdemux->pending_typefind_buffer == NULL);
578 return gst_adaptive_demux_stream_push_buffer (stream, buffer);
583 gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
584 GstAdaptiveDemuxStream * stream)
586 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
587 GstFlowReturn ret = GST_FLOW_OK;
589 if (hlsdemux->current_key)
590 gst_hls_demux_decrypt_end (hlsdemux);
592 if (stream->last_ret == GST_FLOW_OK) {
593 if (hlsdemux->pending_decrypted_buffer) {
594 if (hlsdemux->current_key) {
596 gssize unpadded_size;
598 /* Handle pkcs7 unpadding here */
599 gst_buffer_map (hlsdemux->pending_decrypted_buffer, &info,
601 unpadded_size = info.size - info.data[info.size - 1];
602 gst_buffer_unmap (hlsdemux->pending_decrypted_buffer, &info);
604 gst_buffer_resize (hlsdemux->pending_decrypted_buffer, 0,
609 gst_hls_demux_handle_buffer (demux, stream,
610 hlsdemux->pending_decrypted_buffer, TRUE);
611 hlsdemux->pending_decrypted_buffer = NULL;
614 gst_buffer_replace (&hlsdemux->pending_decrypted_buffer, NULL);
615 gst_adapter_clear (hlsdemux->pending_encrypted_data);
618 if (ret == GST_FLOW_OK || ret == GST_FLOW_NOT_LINKED)
619 return gst_adaptive_demux_stream_advance_fragment (demux, stream,
620 stream->fragment.duration);
625 gst_hls_demux_data_received (GstAdaptiveDemux * demux,
626 GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
628 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
630 /* Is it encrypted? */
631 if (hlsdemux->current_key) {
634 GstBuffer *tmp_buffer;
636 gst_adapter_push (hlsdemux->pending_encrypted_data, buffer);
637 size = gst_adapter_available (hlsdemux->pending_encrypted_data);
639 /* must be a multiple of 16 */
640 size = size & (~0xF);
646 buffer = gst_adapter_take_buffer (hlsdemux->pending_encrypted_data, size);
647 buffer = gst_hls_demux_decrypt_fragment (hlsdemux, buffer, &err);
648 if (buffer == NULL) {
649 GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Failed to decrypt buffer"),
650 ("decryption failed %s", err->message));
652 return GST_FLOW_ERROR;
655 tmp_buffer = hlsdemux->pending_decrypted_buffer;
656 hlsdemux->pending_decrypted_buffer = buffer;
660 return gst_hls_demux_handle_buffer (demux, stream, buffer, FALSE);
664 gst_hls_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
666 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
668 return gst_m3u8_client_has_next_fragment (hlsdemux->client,
669 stream->demux->segment.rate > 0);
673 gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream * stream)
675 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
677 gst_m3u8_client_advance_fragment (hlsdemux->client,
678 stream->demux->segment.rate > 0);
679 hlsdemux->reset_pts = FALSE;
684 gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream * stream)
686 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
687 gchar *next_fragment_uri;
688 GstClockTime duration;
689 GstClockTime timestamp;
691 gint64 range_start, range_end;
695 if (!gst_m3u8_client_get_next_fragment (hlsdemux->client, &discont,
696 &next_fragment_uri, &duration, ×tamp, &range_start, &range_end,
697 &key, &iv, stream->demux->segment.rate > 0)) {
698 GST_INFO_OBJECT (hlsdemux, "This playlist doesn't contain more fragments");
705 /* set up our source for download */
706 if (hlsdemux->reset_pts || discont || stream->demux->segment.rate < 0.0) {
707 stream->fragment.timestamp = timestamp;
709 stream->fragment.timestamp = GST_CLOCK_TIME_NONE;
712 g_free (hlsdemux->current_key);
713 hlsdemux->current_key = key;
714 g_free (hlsdemux->current_iv);
715 hlsdemux->current_iv = iv;
716 g_free (stream->fragment.uri);
717 stream->fragment.uri = next_fragment_uri;
718 stream->fragment.range_start = range_start;
719 stream->fragment.range_end = range_end;
720 stream->fragment.duration = duration;
722 stream->discont = discont;
728 gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream, guint64 bitrate)
730 GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (stream->demux);
731 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
732 gboolean changed = FALSE;
734 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
735 if (!hlsdemux->client->main->lists) {
736 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
739 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
741 /* FIXME: Currently several issues have be found when letting bitrate adaptation
742 * happen using trick modes (such as 'All streams finished without buffers') and
743 * the adaptive algorithm does not properly behave. */
744 if (demux->segment.rate != 1.0)
747 gst_hls_demux_change_playlist (hlsdemux, bitrate, &changed);
749 gst_hls_demux_setup_streams (GST_ADAPTIVE_DEMUX_CAST (hlsdemux));
754 gst_hls_demux_reset (GstAdaptiveDemux * ademux)
756 GstHLSDemux *demux = GST_HLS_DEMUX_CAST (ademux);
758 demux->do_typefind = TRUE;
759 demux->reset_pts = TRUE;
761 g_free (demux->key_url);
762 demux->key_url = NULL;
764 if (demux->key_fragment)
765 g_object_unref (demux->key_fragment);
766 demux->key_fragment = NULL;
769 gst_m3u8_client_free (demux->client);
770 demux->client = NULL;
772 /* TODO recreated on hls only if reset was not for disposing */
773 demux->client = gst_m3u8_client_new ("", NULL);
775 demux->srcpad_counter = 0;
776 gst_adapter_clear (demux->pending_encrypted_data);
777 gst_buffer_replace (&demux->pending_decrypted_buffer, NULL);
778 gst_buffer_replace (&demux->pending_typefind_buffer, NULL);
779 if (demux->current_key) {
780 g_free (demux->current_key);
781 demux->current_key = NULL;
783 if (demux->current_iv) {
784 g_free (demux->current_iv);
785 demux->current_iv = NULL;
788 gst_hls_demux_decrypt_end (demux);
792 gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf)
797 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
800 if (!g_utf8_validate ((gchar *) info.data, info.size, NULL))
803 /* alloc size + 1 to end with a null character */
804 playlist = g_malloc0 (info.size + 1);
805 memcpy (playlist, info.data, info.size);
807 gst_buffer_unmap (buf, &info);
811 gst_buffer_unmap (buf, &info);
817 gst_hls_demux_update_playlist (GstHLSDemux * demux, gboolean update,
820 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
821 GstFragment *download;
824 gboolean main_checked = FALSE, updated = FALSE;
825 gchar *uri, *main_uri;
828 uri = gst_m3u8_client_get_current_uri (demux->client);
829 main_uri = gst_m3u8_client_get_uri (demux->client);
831 gst_uri_downloader_fetch_uri (adaptive_demux->downloader, uri, main_uri,
832 TRUE, TRUE, TRUE, err);
834 if (download == NULL) {
837 if (!update || main_checked
838 || !gst_m3u8_client_has_variant_playlist (demux->client)) {
844 main_uri = gst_m3u8_client_get_uri (demux->client);
845 GST_INFO_OBJECT (demux,
846 "Updating playlist %s failed, attempt to refresh variant playlist %s",
849 gst_uri_downloader_fetch_uri (adaptive_demux->downloader,
850 main_uri, NULL, TRUE, TRUE, TRUE, err);
852 if (download == NULL) {
857 buf = gst_fragment_get_buffer (download);
858 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
859 gst_buffer_unref (buf);
861 if (playlist == NULL) {
862 GST_WARNING_OBJECT (demux,
863 "Failed to validate variant playlist encoding");
865 g_object_unref (download);
866 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
867 "Couldn't validate playlist encoding");
872 if (download->redirect_permanent && download->redirect_uri) {
873 uri = download->redirect_uri;
877 base_uri = download->redirect_uri;
880 if (!gst_m3u8_client_update_variant_playlist (demux->client, playlist,
882 GST_WARNING_OBJECT (demux, "Failed to update the variant playlist");
883 g_object_unref (download);
884 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
885 "Couldn't update playlist");
889 g_object_unref (download);
896 /* Set the base URI of the playlist to the redirect target if any */
897 GST_M3U8_CLIENT_LOCK (demux->client);
898 g_free (demux->client->current->uri);
899 g_free (demux->client->current->base_uri);
900 if (download->redirect_permanent && download->redirect_uri) {
901 demux->client->current->uri = g_strdup (download->redirect_uri);
902 demux->client->current->base_uri = NULL;
904 demux->client->current->uri = g_strdup (download->uri);
905 demux->client->current->base_uri = g_strdup (download->redirect_uri);
907 GST_M3U8_CLIENT_UNLOCK (demux->client);
909 buf = gst_fragment_get_buffer (download);
910 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
911 gst_buffer_unref (buf);
912 g_object_unref (download);
914 if (playlist == NULL) {
915 GST_WARNING_OBJECT (demux, "Couldn't validate playlist encoding");
916 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
917 "Couldn't validate playlist encoding");
921 updated = gst_m3u8_client_update (demux->client, playlist);
923 GST_WARNING_OBJECT (demux, "Couldn't update playlist");
924 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
925 "Couldn't update playlist");
929 /* If it's a live source, do not let the sequence number go beyond
930 * three fragments before the end of the list */
931 if (update == FALSE && demux->client->current &&
932 gst_m3u8_client_is_live (demux->client)) {
933 gint64 last_sequence, first_sequence;
935 GST_M3U8_CLIENT_LOCK (demux->client);
937 GST_M3U8_MEDIA_FILE (g_list_last (demux->client->current->
938 files)->data)->sequence;
940 GST_M3U8_MEDIA_FILE (demux->client->current->files->data)->sequence;
942 GST_DEBUG_OBJECT (demux,
943 "sequence:%" G_GINT64_FORMAT " , first_sequence:%" G_GINT64_FORMAT
944 " , last_sequence:%" G_GINT64_FORMAT, demux->client->sequence,
945 first_sequence, last_sequence);
946 if (demux->client->sequence >= last_sequence - 3) {
947 //demux->need_segment = TRUE;
948 /* Make sure we never go below the minimum sequence number */
949 demux->client->sequence = MAX (first_sequence, last_sequence - 3);
950 GST_DEBUG_OBJECT (demux,
951 "Sequence is beyond playlist. Moving back to %" G_GINT64_FORMAT,
952 demux->client->sequence);
954 GST_M3U8_CLIENT_UNLOCK (demux->client);
955 } else if (demux->client->current && !gst_m3u8_client_is_live (demux->client)) {
956 GstClockTime current_pos, target_pos;
960 /* Sequence numbers are not guaranteed to be the same in different
961 * playlists, so get the correct fragment here based on the current
964 GST_M3U8_CLIENT_LOCK (demux->client);
966 /* Valid because hlsdemux only has a single output */
967 if (GST_ADAPTIVE_DEMUX_CAST (demux)->streams) {
968 GstAdaptiveDemuxStream *stream =
969 GST_ADAPTIVE_DEMUX_CAST (demux)->streams->data;
970 target_pos = stream->segment.position;
974 if (GST_CLOCK_TIME_IS_VALID (demux->client->sequence_position)) {
975 target_pos = MAX (target_pos, demux->client->sequence_position);
978 GST_LOG_OBJECT (demux, "Looking for sequence position %"
979 GST_TIME_FORMAT " in updated playlist", GST_TIME_ARGS (target_pos));
982 for (walk = demux->client->current->files; walk; walk = walk->next) {
983 GstM3U8MediaFile *file = walk->data;
985 sequence = file->sequence;
986 if (current_pos <= target_pos
987 && target_pos < current_pos + file->duration) {
990 current_pos += file->duration;
992 /* End of playlist */
995 demux->client->sequence = sequence;
996 demux->client->sequence_position = current_pos;
997 GST_M3U8_CLIENT_UNLOCK (demux->client);
1004 gst_hls_demux_change_playlist (GstHLSDemux * demux, guint max_bitrate,
1007 GList *previous_variant, *current_variant;
1008 gint old_bandwidth, new_bandwidth;
1009 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX_CAST (demux);
1010 GstAdaptiveDemuxStream *stream;
1012 g_return_val_if_fail (adaptive_demux->streams != NULL, FALSE);
1014 stream = adaptive_demux->streams->data;
1016 previous_variant = demux->client->main->current_variant;
1017 current_variant = gst_m3u8_client_get_playlist_for_bitrate (demux->client,
1020 GST_M3U8_CLIENT_LOCK (demux->client);
1022 retry_failover_protection:
1023 old_bandwidth = GST_M3U8 (previous_variant->data)->bandwidth;
1024 new_bandwidth = GST_M3U8 (current_variant->data)->bandwidth;
1026 /* Don't do anything else if the playlist is the same */
1027 if (new_bandwidth == old_bandwidth) {
1028 GST_M3U8_CLIENT_UNLOCK (demux->client);
1032 demux->client->main->current_variant = current_variant;
1033 GST_M3U8_CLIENT_UNLOCK (demux->client);
1035 gst_m3u8_client_set_current (demux->client, current_variant->data);
1037 GST_INFO_OBJECT (demux, "Client was on %dbps, max allowed is %dbps, switching"
1038 " to bitrate %dbps", old_bandwidth, max_bitrate, new_bandwidth);
1040 if (gst_hls_demux_update_playlist (demux, TRUE, NULL)) {
1043 uri = gst_m3u8_client_get_current_uri (demux->client);
1044 main_uri = gst_m3u8_client_get_uri (demux->client);
1045 gst_element_post_message (GST_ELEMENT_CAST (demux),
1046 gst_message_new_element (GST_OBJECT_CAST (demux),
1047 gst_structure_new (GST_ADAPTIVE_DEMUX_STATISTICS_MESSAGE_NAME,
1048 "manifest-uri", G_TYPE_STRING,
1049 main_uri, "uri", G_TYPE_STRING,
1050 uri, "bitrate", G_TYPE_INT, new_bandwidth, NULL)));
1055 stream->discont = TRUE;
1057 GList *failover = NULL;
1059 GST_INFO_OBJECT (demux, "Unable to update playlist. Switching back");
1060 GST_M3U8_CLIENT_LOCK (demux->client);
1062 failover = g_list_previous (current_variant);
1063 if (failover && new_bandwidth == GST_M3U8 (failover->data)->bandwidth) {
1064 current_variant = failover;
1065 goto retry_failover_protection;
1068 demux->client->main->current_variant = previous_variant;
1069 GST_M3U8_CLIENT_UNLOCK (demux->client);
1070 gst_m3u8_client_set_current (demux->client, previous_variant->data);
1071 /* Try a lower bitrate (or stop if we just tried the lowest) */
1072 if (GST_M3U8 (previous_variant->data)->iframe && new_bandwidth ==
1073 GST_M3U8 (g_list_first (demux->client->main->iframe_lists)->data)->
1076 else if (!GST_M3U8 (previous_variant->data)->iframe && new_bandwidth ==
1077 GST_M3U8 (g_list_first (demux->client->main->lists)->data)->bandwidth)
1080 return gst_hls_demux_change_playlist (demux, new_bandwidth - 1, changed);
1083 /* Force typefinding since we might have changed media type */
1084 demux->do_typefind = TRUE;
1089 #if defined(HAVE_OPENSSL)
1091 gst_hls_demux_decrypt_start (GstHLSDemux * demux, const guint8 * key_data,
1092 const guint8 * iv_data)
1094 EVP_CIPHER_CTX_init (&demux->aes_ctx);
1095 if (!EVP_DecryptInit_ex (&demux->aes_ctx, EVP_aes_128_cbc (), NULL, key_data,
1098 EVP_CIPHER_CTX_set_padding (&demux->aes_ctx, 0);
1103 decrypt_fragment (GstHLSDemux * demux, gsize length,
1104 const guint8 * encrypted_data, guint8 * decrypted_data)
1108 if (G_UNLIKELY (length > G_MAXINT || length % 16 != 0))
1112 if (!EVP_DecryptUpdate (&demux->aes_ctx, decrypted_data, &len, encrypted_data,
1115 EVP_DecryptFinal_ex (&demux->aes_ctx, decrypted_data + len, &flen);
1116 g_return_val_if_fail (len + flen == length, FALSE);
1121 gst_hls_demux_decrypt_end (GstHLSDemux * demux)
1123 EVP_CIPHER_CTX_cleanup (&demux->aes_ctx);
1126 #elif defined(HAVE_NETTLE)
1128 gst_hls_demux_decrypt_start (GstHLSDemux * demux, const guint8 * key_data,
1129 const guint8 * iv_data)
1131 aes_set_decrypt_key (&demux->aes_ctx.ctx, 16, key_data);
1132 CBC_SET_IV (&demux->aes_ctx, iv_data);
1138 decrypt_fragment (GstHLSDemux * demux, gsize length,
1139 const guint8 * encrypted_data, guint8 * decrypted_data)
1141 if (length % 16 != 0)
1144 CBC_DECRYPT (&demux->aes_ctx, aes_decrypt, length, decrypted_data,
1151 gst_hls_demux_decrypt_end (GstHLSDemux * demux)
1158 gst_hls_demux_decrypt_start (GstHLSDemux * demux, const guint8 * key_data,
1159 const guint8 * iv_data)
1161 gcry_error_t err = 0;
1162 gboolean ret = FALSE;
1165 gcry_cipher_open (&demux->aes_ctx, GCRY_CIPHER_AES128,
1166 GCRY_CIPHER_MODE_CBC, 0);
1169 err = gcry_cipher_setkey (demux->aes_ctx, key_data, 16);
1172 err = gcry_cipher_setiv (demux->aes_ctx, iv_data, 16);
1179 gcry_cipher_close (demux->aes_ctx);
1185 decrypt_fragment (GstHLSDemux * demux, gsize length,
1186 const guint8 * encrypted_data, guint8 * decrypted_data)
1188 gcry_error_t err = 0;
1190 err = gcry_cipher_decrypt (demux->aes_ctx, decrypted_data, length,
1191 encrypted_data, length);
1197 gst_hls_demux_decrypt_end (GstHLSDemux * demux)
1199 if (demux->aes_ctx) {
1200 gcry_cipher_close (demux->aes_ctx);
1201 demux->aes_ctx = NULL;
1207 gst_hls_demux_decrypt_fragment (GstHLSDemux * demux,
1208 GstBuffer * encrypted_buffer, GError ** err)
1210 GstBuffer *decrypted_buffer = NULL;
1211 GstMapInfo encrypted_info, decrypted_info;
1214 gst_buffer_new_allocate (NULL, gst_buffer_get_size (encrypted_buffer),
1217 gst_buffer_map (encrypted_buffer, &encrypted_info, GST_MAP_READ);
1218 gst_buffer_map (decrypted_buffer, &decrypted_info, GST_MAP_WRITE);
1220 if (!decrypt_fragment (demux, encrypted_info.size,
1221 encrypted_info.data, decrypted_info.data))
1225 gst_buffer_unmap (decrypted_buffer, &decrypted_info);
1226 gst_buffer_unmap (encrypted_buffer, &encrypted_info);
1228 gst_buffer_unref (encrypted_buffer);
1230 return decrypted_buffer;
1233 GST_ERROR_OBJECT (demux, "Failed to decrypt fragment");
1234 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_DECRYPT,
1235 "Failed to decrypt fragment");
1237 gst_buffer_unmap (decrypted_buffer, &decrypted_info);
1238 gst_buffer_unmap (encrypted_buffer, &encrypted_info);
1240 gst_buffer_unref (encrypted_buffer);
1241 gst_buffer_unref (decrypted_buffer);
1247 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
1249 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1251 return gst_util_uint64_scale (gst_m3u8_client_get_target_duration
1252 (hlsdemux->client), G_USEC_PER_SEC, GST_SECOND);
1256 gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
1259 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1261 return gst_m3u8_client_get_seek_range (hlsdemux->client, start, stop);