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
31 * HTTP Live Streaming demuxer element.
33 * ## Example launch line
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 (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 /* FIXME: the return value is never used? */
77 static gboolean gst_hls_demux_change_playlist (GstHLSDemux * demux,
78 guint max_bitrate, gboolean * changed);
79 static GstBuffer *gst_hls_demux_decrypt_fragment (GstHLSDemux * demux,
80 GstHLSDemuxStream * stream, GstBuffer * encrypted_buffer, GError ** err);
82 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
83 const guint8 * key_data, const guint8 * iv_data);
84 static void gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream);
86 static gboolean gst_hls_demux_is_live (GstAdaptiveDemux * demux);
87 static GstClockTime gst_hls_demux_get_duration (GstAdaptiveDemux * demux);
88 static gint64 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux *
90 static gboolean gst_hls_demux_process_manifest (GstAdaptiveDemux * demux,
92 static GstFlowReturn gst_hls_demux_update_manifest (GstAdaptiveDemux * demux);
93 static gboolean gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
94 static GstFlowReturn gst_hls_demux_stream_seek (GstAdaptiveDemuxStream *
95 stream, gboolean forward, GstSeekFlags flags, GstClockTime ts,
96 GstClockTime * final_ts);
98 gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
99 GstAdaptiveDemuxStream * stream);
100 static GstFlowReturn gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
101 GstAdaptiveDemuxStream * stream);
102 static GstFlowReturn gst_hls_demux_data_received (GstAdaptiveDemux * demux,
103 GstAdaptiveDemuxStream * stream, GstBuffer * buffer);
104 static void gst_hls_demux_stream_free (GstAdaptiveDemuxStream * stream);
105 static gboolean gst_hls_demux_stream_has_next_fragment (GstAdaptiveDemuxStream *
107 static GstFlowReturn gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream *
109 static GstFlowReturn gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream
111 static gboolean gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream,
113 static void gst_hls_demux_reset (GstAdaptiveDemux * demux);
114 static gboolean gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux,
115 gint64 * start, gint64 * stop);
116 static GstM3U8 *gst_hls_demux_stream_get_m3u8 (GstHLSDemuxStream * hls_stream);
117 static void gst_hls_demux_set_current_variant (GstHLSDemux * hlsdemux,
118 GstHLSVariantStream * variant);
120 #define gst_hls_demux_parent_class parent_class
121 G_DEFINE_TYPE (GstHLSDemux, gst_hls_demux, GST_TYPE_ADAPTIVE_DEMUX);
124 gst_hls_demux_finalize (GObject * obj)
126 GstHLSDemux *demux = GST_HLS_DEMUX (obj);
128 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
129 g_mutex_clear (&demux->keys_lock);
131 g_hash_table_unref (demux->keys);
135 G_OBJECT_CLASS (parent_class)->finalize (obj);
139 gst_hls_demux_class_init (GstHLSDemuxClass * klass)
141 GObjectClass *gobject_class;
142 GstElementClass *element_class;
143 GstAdaptiveDemuxClass *adaptivedemux_class;
145 gobject_class = (GObjectClass *) klass;
146 element_class = (GstElementClass *) klass;
147 adaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
149 gobject_class->finalize = gst_hls_demux_finalize;
151 element_class->change_state = GST_DEBUG_FUNCPTR (gst_hls_demux_change_state);
153 gst_element_class_add_static_pad_template (element_class, &srctemplate);
154 gst_element_class_add_static_pad_template (element_class, &sinktemplate);
156 gst_element_class_set_static_metadata (element_class,
158 "Codec/Demuxer/Adaptive",
159 "HTTP Live Streaming demuxer",
160 "Marc-Andre Lureau <marcandre.lureau@gmail.com>\n"
161 "Andoni Morales Alastruey <ylatuya@gmail.com>");
163 adaptivedemux_class->is_live = gst_hls_demux_is_live;
164 adaptivedemux_class->get_live_seek_range = gst_hls_demux_get_live_seek_range;
165 adaptivedemux_class->get_duration = gst_hls_demux_get_duration;
166 adaptivedemux_class->get_manifest_update_interval =
167 gst_hls_demux_get_manifest_update_interval;
168 adaptivedemux_class->process_manifest = gst_hls_demux_process_manifest;
169 adaptivedemux_class->update_manifest = gst_hls_demux_update_manifest;
170 adaptivedemux_class->reset = gst_hls_demux_reset;
171 adaptivedemux_class->seek = gst_hls_demux_seek;
172 adaptivedemux_class->stream_seek = gst_hls_demux_stream_seek;
173 adaptivedemux_class->stream_has_next_fragment =
174 gst_hls_demux_stream_has_next_fragment;
175 adaptivedemux_class->stream_advance_fragment = gst_hls_demux_advance_fragment;
176 adaptivedemux_class->stream_update_fragment_info =
177 gst_hls_demux_update_fragment_info;
178 adaptivedemux_class->stream_select_bitrate = gst_hls_demux_select_bitrate;
179 adaptivedemux_class->stream_free = gst_hls_demux_stream_free;
181 adaptivedemux_class->start_fragment = gst_hls_demux_start_fragment;
182 adaptivedemux_class->finish_fragment = gst_hls_demux_finish_fragment;
183 adaptivedemux_class->data_received = gst_hls_demux_data_received;
185 GST_DEBUG_CATEGORY_INIT (gst_hls_demux_debug, "hlsdemux", 0,
190 gst_hls_demux_init (GstHLSDemux * demux)
192 gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
193 sizeof (GstHLSDemuxStream));
195 demux->keys = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
196 g_mutex_init (&demux->keys_lock);
199 static GstStateChangeReturn
200 gst_hls_demux_change_state (GstElement * element, GstStateChange transition)
202 GstStateChangeReturn ret;
203 GstHLSDemux *demux = GST_HLS_DEMUX (element);
205 switch (transition) {
206 case GST_STATE_CHANGE_READY_TO_PAUSED:
207 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
213 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
215 switch (transition) {
216 case GST_STATE_CHANGE_PAUSED_TO_READY:
217 gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
218 g_hash_table_remove_all (demux->keys);
227 gst_hls_demux_create_pad (GstHLSDemux * hlsdemux)
232 name = g_strdup_printf ("src_%u", hlsdemux->srcpad_counter++);
233 pad = gst_pad_new_from_static_template (&srctemplate, name);
240 gst_hls_demux_get_bitrate (GstHLSDemux * hlsdemux)
242 GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (hlsdemux);
246 * No, there isn't a single output :D */
248 /* Valid because hlsdemux only has a single output */
249 if (demux->streams) {
250 GstAdaptiveDemuxStream *stream = demux->streams->data;
251 return stream->current_download_rate;
258 gst_hls_demux_stream_clear_pending_data (GstHLSDemuxStream * hls_stream)
260 if (hls_stream->pending_encrypted_data)
261 gst_adapter_clear (hls_stream->pending_encrypted_data);
262 gst_buffer_replace (&hls_stream->pending_decrypted_buffer, NULL);
263 gst_buffer_replace (&hls_stream->pending_typefind_buffer, NULL);
264 gst_buffer_replace (&hls_stream->pending_pcr_buffer, NULL);
265 hls_stream->current_offset = -1;
266 gst_hls_demux_stream_decrypt_end (hls_stream);
270 gst_hls_demux_clear_all_pending_data (GstHLSDemux * hlsdemux)
272 GstAdaptiveDemux *demux = (GstAdaptiveDemux *) hlsdemux;
275 for (walk = demux->streams; walk != NULL; walk = walk->next) {
276 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (walk->data);
277 gst_hls_demux_stream_clear_pending_data (hls_stream);
283 gst_hls_demux_set_current (GstHLSDemux * self, GstM3U8 * m3u8)
285 GST_M3U8_CLIENT_LOCK (self);
286 if (m3u8 != self->current) {
287 self->current = m3u8;
288 self->current->duration = GST_CLOCK_TIME_NONE;
289 self->current->current_file = NULL;
292 // FIXME: this makes no sense after we just set self->current=m3u8 above (tpm)
293 // also, these values don't necessarily align between different lists
294 m3u8->current_file_duration = self->current->current_file_duration;
295 m3u8->sequence = self->current->sequence;
296 m3u8->sequence_position = self->current->sequence_position;
297 m3u8->highest_sequence_number = self->current->highest_sequence_number;
298 m3u8->first_file_start = self->current->first_file_start;
299 m3u8->last_file_end = self->current->last_file_end;
302 GST_M3U8_CLIENT_UNLOCK (self);
306 #define SEEK_UPDATES_PLAY_POSITION(r, start_type, stop_type) \
307 ((r >= 0 && start_type != GST_SEEK_TYPE_NONE) || \
308 (r < 0 && stop_type != GST_SEEK_TYPE_NONE))
310 #define IS_SNAP_SEEK(f) (f & (GST_SEEK_FLAG_SNAP_BEFORE | \
311 GST_SEEK_FLAG_SNAP_AFTER | \
312 GST_SEEK_FLAG_SNAP_NEAREST | \
313 GST_SEEK_FLAG_TRICKMODE_KEY_UNITS | \
314 GST_SEEK_FLAG_KEY_UNIT))
317 gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
319 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
322 GstSeekType start_type, stop_type;
324 gdouble rate, old_rate;
326 GstClockTime current_pos, target_pos, final_pos;
329 gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
332 if (!SEEK_UPDATES_PLAY_POSITION (rate, start_type, stop_type)) {
333 /* nothing to do if we don't have to update the current position */
337 old_rate = demux->segment.rate;
339 bitrate = gst_hls_demux_get_bitrate (hlsdemux);
341 /* Use I-frame variants for trick modes */
342 if (hlsdemux->master->iframe_variants != NULL
343 && rate < -1.0 && old_rate >= -1.0 && old_rate <= 1.0) {
346 /* Switch to I-frame variant */
347 gst_hls_demux_set_current_variant (hlsdemux,
348 hlsdemux->master->iframe_variants->data);
349 gst_uri_downloader_reset (demux->downloader);
350 if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
351 GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
354 //hlsdemux->discont = TRUE;
356 gst_hls_demux_change_playlist (hlsdemux, bitrate / ABS (rate), NULL);
357 } else if (rate > -1.0 && rate <= 1.0 && (old_rate < -1.0 || old_rate > 1.0)) {
359 /* Switch to normal variant */
360 gst_hls_demux_set_current_variant (hlsdemux,
361 hlsdemux->master->variants->data);
362 gst_uri_downloader_reset (demux->downloader);
363 if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
364 GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
367 //hlsdemux->discont = TRUE;
368 /* TODO why not continue using the same? that was being used up to now? */
369 gst_hls_demux_change_playlist (hlsdemux, bitrate, NULL);
372 target_pos = rate < 0 ? stop : start;
373 final_pos = target_pos;
375 /* properly cleanup pending decryption status */
376 if (flags & GST_SEEK_FLAG_FLUSH) {
377 gst_hls_demux_clear_all_pending_data (hlsdemux);
380 for (walk = demux->streams; walk; walk = g_list_next (walk)) {
381 GstAdaptiveDemuxStream *stream =
382 GST_ADAPTIVE_DEMUX_STREAM_CAST (walk->data);
384 gst_hls_demux_stream_seek (stream, rate >= 0, flags, target_pos,
387 /* FIXME: use minimum position always ? */
388 if (final_pos > current_pos)
389 final_pos = current_pos;
392 if (IS_SNAP_SEEK (flags)) {
394 gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
395 final_pos, stop_type, stop, NULL);
397 gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
398 start, stop_type, final_pos, NULL);
405 gst_hls_demux_stream_seek (GstAdaptiveDemuxStream * stream, gboolean forward,
406 GstSeekFlags flags, GstClockTime ts, GstClockTime * final_ts)
408 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
410 GstClockTime current_pos;
411 gint64 current_sequence;
412 gboolean snap_after, snap_nearest;
413 GstM3U8MediaFile *file = NULL;
415 current_sequence = 0;
416 current_pos = gst_m3u8_is_live (hls_stream->playlist) ?
417 hls_stream->playlist->first_file_start : 0;
419 /* Snap to segment boundary. Improves seek performance on slow machines. */
421 (flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST;
422 snap_after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
424 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
425 /* FIXME: Here we need proper discont handling */
426 for (walk = hls_stream->playlist->files; walk; walk = walk->next) {
429 current_sequence = file->sequence;
430 if ((forward && snap_after) || snap_nearest) {
431 if (current_pos >= ts)
433 if (snap_nearest && ts - current_pos < file->duration / 2)
435 } else if (!forward && snap_after) {
436 /* check if the next fragment is our target, in this case we want to
437 * start from the previous fragment */
438 GstClockTime next_pos = current_pos + file->duration;
440 if (next_pos <= ts && ts < next_pos + file->duration) {
443 } else if (current_pos <= ts && ts < current_pos + file->duration) {
446 current_pos += file->duration;
450 GST_DEBUG_OBJECT (stream->pad, "seeking further than track duration");
454 GST_DEBUG_OBJECT (stream->pad, "seeking to sequence %u",
455 (guint) current_sequence);
456 hls_stream->reset_pts = TRUE;
457 hls_stream->playlist->sequence = current_sequence;
458 hls_stream->playlist->current_file = walk;
459 hls_stream->playlist->sequence_position = current_pos;
460 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
462 /* Play from the end of the current selected segment */
464 if (!forward && IS_SNAP_SEEK (flags))
465 current_pos += file->duration;
468 /* update stream's segment position */
469 stream->segment.position = current_pos;
472 *final_ts = current_pos;
478 gst_hls_demux_update_manifest (GstAdaptiveDemux * demux)
480 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
481 if (!gst_hls_demux_update_playlist (hlsdemux, TRUE, NULL))
482 return GST_FLOW_ERROR;
488 create_stream_for_playlist (GstAdaptiveDemux * demux, GstM3U8 * playlist,
489 gboolean is_primary_playlist, gboolean selected)
491 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
492 GstHLSDemuxStream *hlsdemux_stream;
493 GstAdaptiveDemuxStream *stream;
496 /* FIXME: Later, create the stream but mark not-selected */
497 GST_LOG_OBJECT (demux, "Ignoring not-selected stream");
501 GST_DEBUG_OBJECT (demux,
502 "is_primary_playlist:%d selected:%d playlist name '%s'",
503 is_primary_playlist, selected, playlist->name);
505 stream = gst_adaptive_demux_stream_new (demux,
506 gst_hls_demux_create_pad (hlsdemux));
508 hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
510 hlsdemux_stream->stream_type = GST_HLS_TSREADER_NONE;
512 hlsdemux_stream->playlist = gst_m3u8_ref (playlist);
513 hlsdemux_stream->is_primary_playlist = is_primary_playlist;
515 hlsdemux_stream->do_typefind = TRUE;
516 hlsdemux_stream->reset_pts = TRUE;
519 static GstHLSDemuxStream *
520 find_adaptive_stream_for_playlist (GstAdaptiveDemux * demux, GstM3U8 * playlist)
524 GST_DEBUG_OBJECT (demux, "Looking for existing stream for '%s' %s",
525 playlist->name, playlist->uri);
527 for (tmp = demux->streams; tmp; tmp = tmp->next) {
528 GstHLSDemuxStream *hlsstream = (GstHLSDemuxStream *) tmp->data;
529 if (hlsstream->playlist == playlist)
536 /* Returns TRUE if the previous and current (to switch to) variant are compatible.
539 * * They have the same number of streams
540 * * The streams are of the same type
543 new_variant_is_compatible (GstAdaptiveDemux * demux)
545 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
546 GstHLSVariantStream *previous = hlsdemux->previous_variant;
547 GstHLSVariantStream *current = hlsdemux->current_variant;
550 GST_DEBUG_OBJECT (demux,
551 "Checking whether new variant is compatible with previous");
553 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
554 GList *mlist = current->media[i];
555 if (g_list_length (previous->media[i]) != g_list_length (current->media[i])) {
556 GST_LOG_OBJECT (demux, "Number of medias for type %s don't match",
557 gst_hls_media_type_get_name (i));
561 /* Check if all new media were present in previous (if not there are new ones) */
562 while (mlist != NULL) {
563 GstHLSMedia *media = mlist->data;
564 if (!gst_hls_variant_find_matching_media (previous, media)) {
565 GST_LOG_OBJECT (demux,
566 "New stream of type %s present. Variant not compatible",
567 gst_hls_media_type_get_name (i));
573 /* Check if all old media are present in current (if not some have gone) */
574 mlist = previous->media[i];
575 while (mlist != NULL) {
576 GstHLSMedia *media = mlist->data;
577 if (!gst_hls_variant_find_matching_media (current, media)) {
578 GST_LOG_OBJECT (demux,
579 "Old stream of type %s gone. Variant not compatible",
580 gst_hls_media_type_get_name (i));
587 GST_DEBUG_OBJECT (demux, "Variants are compatible");
593 gst_hls_demux_setup_streams (GstAdaptiveDemux * demux)
595 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
596 GstHLSVariantStream *playlist = hlsdemux->current_variant;
599 if (playlist == NULL) {
600 GST_WARNING_OBJECT (demux, "Can't configure streams - no variant selected");
604 GST_DEBUG_OBJECT (demux, "Setting up streams");
605 if (hlsdemux->streams_aware && hlsdemux->previous_variant &&
606 new_variant_is_compatible (demux)) {
607 GstHLSDemuxStream *hlsstream;
608 GST_DEBUG_OBJECT (demux, "Have a previous variant, Re-using streams");
610 /* Carry over the main playlist */
612 find_adaptive_stream_for_playlist (demux,
613 hlsdemux->previous_variant->m3u8);
614 if (G_UNLIKELY (hlsstream == NULL))
617 gst_m3u8_unref (hlsstream->playlist);
618 hlsstream->playlist = gst_m3u8_ref (playlist->m3u8);
620 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
621 GList *mlist = playlist->media[i];
622 while (mlist != NULL) {
623 GstHLSMedia *media = mlist->data;
624 GstHLSMedia *old_media =
625 gst_hls_variant_find_matching_media (hlsdemux->previous_variant,
628 if (G_UNLIKELY (old_media == NULL)) {
629 GST_FIXME_OBJECT (demux, "Handle new stream !");
632 if (!g_strcmp0 (media->uri, old_media->uri))
633 GST_DEBUG_OBJECT (demux, "Identical stream !");
634 if (media->mtype == GST_HLS_MEDIA_TYPE_AUDIO ||
635 media->mtype == GST_HLS_MEDIA_TYPE_VIDEO) {
637 find_adaptive_stream_for_playlist (demux, old_media->playlist);
641 GST_DEBUG_OBJECT (demux, "Found matching stream");
642 gst_m3u8_unref (hlsstream->playlist);
643 hlsstream->playlist = gst_m3u8_ref (media->playlist);
645 GST_DEBUG_OBJECT (demux, "Skipping stream of type %s",
646 gst_hls_media_type_get_name (media->mtype));
656 /* FIXME : This seems wrong and assumes there's only one stream :( */
657 gst_hls_demux_clear_all_pending_data (hlsdemux);
659 /* 1 output for the main playlist */
660 create_stream_for_playlist (demux, playlist->m3u8, TRUE, TRUE);
662 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
663 GList *mlist = playlist->media[i];
664 while (mlist != NULL) {
665 GstHLSMedia *media = mlist->data;
667 if (media->uri == NULL /* || media->mtype != GST_HLS_MEDIA_TYPE_AUDIO */ ) {
668 /* No uri means this is a placeholder for a stream
669 * contained in another mux */
670 GST_LOG_OBJECT (demux, "Skipping stream %s type %s with no URI",
671 media->name, gst_hls_media_type_get_name (media->mtype));
675 GST_LOG_OBJECT (demux, "media of type %s - %s, uri: %s",
676 gst_hls_media_type_get_name (i), media->name, media->uri);
677 create_stream_for_playlist (demux, media->playlist, FALSE,
678 (media->mtype == GST_HLS_MEDIA_TYPE_VIDEO
679 || media->mtype == GST_HLS_MEDIA_TYPE_AUDIO));
689 /* POST ERROR MESSAGE */
690 GST_ERROR_OBJECT (demux, "Should not happen ! Could not find old stream");
696 gst_adaptive_demux_get_manifest_ref_uri (GstAdaptiveDemux * d)
698 return d->manifest_base_uri ? d->manifest_base_uri : d->manifest_uri;
702 gst_hls_demux_set_current_variant (GstHLSDemux * hlsdemux,
703 GstHLSVariantStream * variant)
705 if (hlsdemux->current_variant == variant || variant == NULL)
708 if (hlsdemux->current_variant != NULL) {
711 //#warning FIXME: Syncing fragments across variants
712 // should be done based on media timestamps, and
713 // discont-sequence-numbers not sequence numbers.
714 variant->m3u8->sequence_position =
715 hlsdemux->current_variant->m3u8->sequence_position;
716 variant->m3u8->sequence = hlsdemux->current_variant->m3u8->sequence;
718 GST_DEBUG_OBJECT (hlsdemux,
719 "Switching Variant. Copying over sequence %" G_GINT64_FORMAT
720 " and sequence_pos %" GST_TIME_FORMAT, variant->m3u8->sequence,
721 GST_TIME_ARGS (variant->m3u8->sequence_position));
723 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
724 GList *mlist = hlsdemux->current_variant->media[i];
726 while (mlist != NULL) {
727 GstHLSMedia *old_media = mlist->data;
728 GstHLSMedia *new_media =
729 gst_hls_variant_find_matching_media (variant, old_media);
732 GST_LOG_OBJECT (hlsdemux, "Found matching GstHLSMedia");
733 GST_LOG_OBJECT (hlsdemux, "old_media '%s' '%s'", old_media->name,
735 GST_LOG_OBJECT (hlsdemux, "new_media '%s' '%s'", new_media->name,
737 new_media->playlist->sequence = old_media->playlist->sequence;
738 new_media->playlist->sequence_position =
739 old_media->playlist->sequence_position;
741 GST_LOG_OBJECT (hlsdemux,
742 "Didn't find a matching variant for '%s' '%s'", old_media->name,
749 if (hlsdemux->previous_variant)
750 gst_hls_variant_stream_unref (hlsdemux->previous_variant);
751 /* Steal the reference */
752 hlsdemux->previous_variant = hlsdemux->current_variant;
755 hlsdemux->current_variant = gst_hls_variant_stream_ref (variant);
760 gst_hls_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
762 GstHLSVariantStream *variant;
763 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
764 gchar *playlist = NULL;
766 GST_INFO_OBJECT (demux, "Initial playlist location: %s (base uri: %s)",
767 demux->manifest_uri, demux->manifest_base_uri);
769 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
770 if (playlist == NULL) {
771 GST_WARNING_OBJECT (demux, "Error validating initial playlist");
775 GST_M3U8_CLIENT_LOCK (self);
776 hlsdemux->master = gst_hls_master_playlist_new_from_data (playlist,
777 gst_adaptive_demux_get_manifest_ref_uri (demux));
779 if (hlsdemux->master == NULL || hlsdemux->master->variants == NULL) {
780 /* In most cases, this will happen if we set a wrong url in the
781 * source element and we have received the 404 HTML response instead of
783 GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Invalid playlist."),
784 ("Could not parse playlist. Check if the URL is correct."));
785 GST_M3U8_CLIENT_UNLOCK (self);
789 /* select the initial variant stream */
790 if (demux->connection_speed == 0) {
791 variant = hlsdemux->master->default_variant;
794 gst_hls_master_playlist_get_variant_for_bitrate (hlsdemux->master,
795 NULL, demux->connection_speed);
799 GST_INFO_OBJECT (hlsdemux, "selected %s", variant->name);
800 gst_hls_demux_set_current_variant (hlsdemux, variant); // FIXME: inline?
803 /* get the selected media playlist (unless the initial list was one already) */
804 if (!hlsdemux->master->is_simple) {
807 if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
808 GST_ELEMENT_ERROR_FROM_ERROR (demux, "Could not fetch media playlist",
810 GST_M3U8_CLIENT_UNLOCK (self);
814 GST_M3U8_CLIENT_UNLOCK (self);
816 return gst_hls_demux_setup_streams (demux);
820 gst_hls_demux_get_duration (GstAdaptiveDemux * demux)
822 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
823 GstClockTime duration = GST_CLOCK_TIME_NONE;
825 if (hlsdemux->current_variant != NULL)
826 duration = gst_m3u8_get_duration (hlsdemux->current_variant->m3u8);
832 gst_hls_demux_is_live (GstAdaptiveDemux * demux)
834 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
835 gboolean is_live = FALSE;
837 if (hlsdemux->current_variant)
838 is_live = gst_hls_variant_stream_is_live (hlsdemux->current_variant);
843 static const GstHLSKey *
844 gst_hls_demux_get_key (GstHLSDemux * demux, const gchar * key_url,
845 const gchar * referer, gboolean allow_cache)
847 GstFragment *key_fragment;
848 GstBuffer *key_buffer;
852 GST_LOG_OBJECT (demux, "Looking up key for key url %s", key_url);
854 g_mutex_lock (&demux->keys_lock);
856 key = g_hash_table_lookup (demux->keys, key_url);
859 GST_LOG_OBJECT (demux, "Found key for key url %s in key cache", key_url);
863 GST_INFO_OBJECT (demux, "Fetching key %s", key_url);
866 gst_uri_downloader_fetch_uri (GST_ADAPTIVE_DEMUX (demux)->downloader,
867 key_url, referer, FALSE, FALSE, allow_cache, &err);
869 if (key_fragment == NULL) {
870 GST_WARNING_OBJECT (demux, "Failed to download key to decrypt data: %s",
871 err ? err->message : "error");
872 g_clear_error (&err);
876 key_buffer = gst_fragment_get_buffer (key_fragment);
878 key = g_new0 (GstHLSKey, 1);
879 if (gst_buffer_extract (key_buffer, 0, key->data, 16) < 16)
880 GST_WARNING_OBJECT (demux, "Download decryption key is too short!");
882 g_hash_table_insert (demux->keys, g_strdup (key_url), key);
884 gst_buffer_unref (key_buffer);
885 g_object_unref (key_fragment);
889 g_mutex_unlock (&demux->keys_lock);
892 GST_MEMDUMP_OBJECT (demux, "Key", key->data, 16);
898 gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
899 GstAdaptiveDemuxStream * stream)
901 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
902 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
903 const GstHLSKey *key;
906 gst_hls_demux_stream_clear_pending_data (hls_stream);
908 /* Init the timestamp reader for this fragment */
909 gst_hlsdemux_tsreader_init (&hls_stream->tsreader);
910 /* Reset the stream type if we already know it */
911 gst_hlsdemux_tsreader_set_type (&hls_stream->tsreader,
912 hls_stream->stream_type);
914 /* If no decryption is needed, there's nothing to be done here */
915 if (hls_stream->current_key == NULL)
918 m3u8 = gst_hls_demux_stream_get_m3u8 (hls_stream);
920 key = gst_hls_demux_get_key (hlsdemux, hls_stream->current_key,
921 m3u8->uri, m3u8->allowcache);
926 if (!gst_hls_demux_stream_decrypt_start (hls_stream, key->data,
927 hls_stream->current_iv))
928 goto decrypt_start_failed;
934 GST_ELEMENT_ERROR (demux, STREAM, DECRYPT_NOKEY,
935 ("Couldn't retrieve key for decryption"), (NULL));
936 GST_WARNING_OBJECT (demux, "Failed to decrypt data");
939 decrypt_start_failed:
941 GST_ELEMENT_ERROR (demux, STREAM, DECRYPT, ("Failed to start decrypt"),
942 ("Couldn't set key and IV or plugin was built without crypto library"));
947 static GstHLSTSReaderType
948 caps_to_reader (const GstCaps * caps)
950 const GstStructure *s = gst_caps_get_structure (caps, 0);
952 if (gst_structure_has_name (s, "video/mpegts"))
953 return GST_HLS_TSREADER_MPEGTS;
954 if (gst_structure_has_name (s, "application/x-id3"))
955 return GST_HLS_TSREADER_ID3;
957 return GST_HLS_TSREADER_NONE;
961 gst_hls_demux_handle_buffer (GstAdaptiveDemux * demux,
962 GstAdaptiveDemuxStream * stream, GstBuffer * buffer, gboolean at_eos)
964 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream); // FIXME: pass HlsStream into function
965 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
966 GstClockTime first_pcr, last_pcr;
972 if (G_UNLIKELY (hls_stream->do_typefind)) {
973 GstCaps *caps = NULL;
975 GstTypeFindProbability prob = GST_TYPE_FIND_NONE;
978 if (hls_stream->pending_typefind_buffer)
979 buffer = gst_buffer_append (hls_stream->pending_typefind_buffer, buffer);
980 hls_stream->pending_typefind_buffer = NULL;
982 gst_buffer_map (buffer, &info, GST_MAP_READ);
983 buffer_size = info.size;
985 /* Typefind could miss if buffer is too small. In this case we
986 * will retry later */
987 if (buffer_size >= (2 * 1024) || at_eos) {
989 gst_type_find_helper_for_data (GST_OBJECT_CAST (hlsdemux), info.data,
993 if (G_UNLIKELY (!caps)) {
994 /* Won't need this mapping any more all paths return inside this if() */
995 gst_buffer_unmap (buffer, &info);
997 /* Only fail typefinding if we already a good amount of data
998 * and we still don't know the type */
999 if (buffer_size > (2 * 1024 * 1024) || at_eos) {
1000 GST_ELEMENT_ERROR (hlsdemux, STREAM, TYPE_NOT_FOUND,
1001 ("Could not determine type of stream"), (NULL));
1002 gst_buffer_unref (buffer);
1003 return GST_FLOW_NOT_NEGOTIATED;
1006 hls_stream->pending_typefind_buffer = buffer;
1011 GST_DEBUG_OBJECT (stream->pad,
1012 "Typefind result: %" GST_PTR_FORMAT " prob:%d", caps, prob);
1014 hls_stream->stream_type = caps_to_reader (caps);
1015 gst_hlsdemux_tsreader_set_type (&hls_stream->tsreader,
1016 hls_stream->stream_type);
1018 gst_adaptive_demux_stream_set_caps (stream, caps);
1020 hls_stream->do_typefind = FALSE;
1022 gst_buffer_unmap (buffer, &info);
1024 g_assert (hls_stream->pending_typefind_buffer == NULL);
1026 // Accumulate this buffer
1027 if (hls_stream->pending_pcr_buffer) {
1028 buffer = gst_buffer_append (hls_stream->pending_pcr_buffer, buffer);
1029 hls_stream->pending_pcr_buffer = NULL;
1032 if (!gst_hlsdemux_tsreader_find_pcrs (&hls_stream->tsreader, &buffer,
1033 &first_pcr, &last_pcr, &tags)
1035 // Store this buffer for later
1036 hls_stream->pending_pcr_buffer = buffer;
1041 gst_adaptive_demux_stream_set_tags (stream, tags);
1042 /* run typefind again on the trimmed buffer */
1043 hls_stream->do_typefind = TRUE;
1044 return gst_hls_demux_handle_buffer (demux, stream, buffer, at_eos);
1048 buffer = gst_buffer_make_writable (buffer);
1049 GST_BUFFER_OFFSET (buffer) = hls_stream->current_offset;
1050 hls_stream->current_offset += gst_buffer_get_size (buffer);
1051 GST_BUFFER_OFFSET_END (buffer) = hls_stream->current_offset;
1052 return gst_adaptive_demux_stream_push_buffer (stream, buffer);
1057 static GstFlowReturn
1058 gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
1059 GstAdaptiveDemuxStream * stream)
1061 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream); // FIXME: pass HlsStream into function
1062 GstFlowReturn ret = GST_FLOW_OK;
1064 if (hls_stream->current_key)
1065 gst_hls_demux_stream_decrypt_end (hls_stream);
1067 if (stream->last_ret == GST_FLOW_OK) {
1068 if (hls_stream->pending_decrypted_buffer) {
1069 if (hls_stream->current_key) {
1071 gssize unpadded_size;
1073 /* Handle pkcs7 unpadding here */
1074 gst_buffer_map (hls_stream->pending_decrypted_buffer, &info,
1076 unpadded_size = info.size - info.data[info.size - 1];
1077 gst_buffer_unmap (hls_stream->pending_decrypted_buffer, &info);
1079 gst_buffer_resize (hls_stream->pending_decrypted_buffer, 0,
1084 gst_hls_demux_handle_buffer (demux, stream,
1085 hls_stream->pending_decrypted_buffer, TRUE);
1086 hls_stream->pending_decrypted_buffer = NULL;
1089 if (ret == GST_FLOW_OK || ret == GST_FLOW_NOT_LINKED) {
1090 if (G_UNLIKELY (hls_stream->pending_typefind_buffer)) {
1091 GstBuffer *buf = hls_stream->pending_typefind_buffer;
1092 hls_stream->pending_typefind_buffer = NULL;
1094 gst_hls_demux_handle_buffer (demux, stream, buf, TRUE);
1097 if (hls_stream->pending_pcr_buffer) {
1098 GstBuffer *buf = hls_stream->pending_pcr_buffer;
1099 hls_stream->pending_pcr_buffer = NULL;
1101 ret = gst_hls_demux_handle_buffer (demux, stream, buf, TRUE);
1104 GST_LOG_OBJECT (stream,
1105 "Fragment PCRs were %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
1106 GST_TIME_ARGS (hls_stream->tsreader.first_pcr),
1107 GST_TIME_ARGS (hls_stream->tsreader.last_pcr));
1111 if (G_UNLIKELY (stream->downloading_header || stream->downloading_index))
1114 gst_hls_demux_stream_clear_pending_data (hls_stream);
1116 if (ret == GST_FLOW_OK || ret == GST_FLOW_NOT_LINKED)
1117 return gst_adaptive_demux_stream_advance_fragment (demux, stream,
1118 stream->fragment.duration);
1122 static GstFlowReturn
1123 gst_hls_demux_data_received (GstAdaptiveDemux * demux,
1124 GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
1126 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
1127 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1129 if (hls_stream->current_offset == -1)
1130 hls_stream->current_offset = 0;
1132 /* Is it encrypted? */
1133 if (hls_stream->current_key) {
1136 GstBuffer *tmp_buffer;
1138 if (hls_stream->pending_encrypted_data == NULL)
1139 hls_stream->pending_encrypted_data = gst_adapter_new ();
1141 gst_adapter_push (hls_stream->pending_encrypted_data, buffer);
1142 size = gst_adapter_available (hls_stream->pending_encrypted_data);
1144 /* must be a multiple of 16 */
1151 buffer = gst_adapter_take_buffer (hls_stream->pending_encrypted_data, size);
1153 gst_hls_demux_decrypt_fragment (hlsdemux, hls_stream, buffer, &err);
1154 if (buffer == NULL) {
1155 GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Failed to decrypt buffer"),
1156 ("decryption failed %s", err->message));
1158 return GST_FLOW_ERROR;
1161 tmp_buffer = hls_stream->pending_decrypted_buffer;
1162 hls_stream->pending_decrypted_buffer = buffer;
1163 buffer = tmp_buffer;
1166 return gst_hls_demux_handle_buffer (demux, stream, buffer, FALSE);
1170 gst_hls_demux_stream_free (GstAdaptiveDemuxStream * stream)
1172 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
1174 if (hls_stream->playlist) {
1175 gst_m3u8_unref (hls_stream->playlist);
1176 hls_stream->playlist = NULL;
1179 if (hls_stream->pending_encrypted_data)
1180 g_object_unref (hls_stream->pending_encrypted_data);
1182 gst_buffer_replace (&hls_stream->pending_decrypted_buffer, NULL);
1183 gst_buffer_replace (&hls_stream->pending_typefind_buffer, NULL);
1184 gst_buffer_replace (&hls_stream->pending_pcr_buffer, NULL);
1186 if (hls_stream->current_key) {
1187 g_free (hls_stream->current_key);
1188 hls_stream->current_key = NULL;
1190 if (hls_stream->current_iv) {
1191 g_free (hls_stream->current_iv);
1192 hls_stream->current_iv = NULL;
1194 gst_hls_demux_stream_decrypt_end (hls_stream);
1198 gst_hls_demux_stream_get_m3u8 (GstHLSDemuxStream * hlsdemux_stream)
1202 m3u8 = hlsdemux_stream->playlist;
1208 gst_hls_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
1213 m3u8 = gst_hls_demux_stream_get_m3u8 (GST_HLS_DEMUX_STREAM_CAST (stream));
1215 has_next = gst_m3u8_has_next_fragment (m3u8, stream->demux->segment.rate > 0);
1220 static GstFlowReturn
1221 gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream * stream)
1223 GstHLSDemuxStream *hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
1226 m3u8 = gst_hls_demux_stream_get_m3u8 (hlsdemux_stream);
1228 gst_m3u8_advance_fragment (m3u8, stream->demux->segment.rate > 0);
1229 hlsdemux_stream->reset_pts = FALSE;
1234 static GstFlowReturn
1235 gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream * stream)
1237 GstHLSDemuxStream *hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
1238 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
1239 GstM3U8MediaFile *file;
1240 GstClockTime sequence_pos;
1241 gboolean discont, forward;
1244 m3u8 = gst_hls_demux_stream_get_m3u8 (hlsdemux_stream);
1246 forward = (stream->demux->segment.rate > 0);
1247 file = gst_m3u8_get_next_fragment (m3u8, forward, &sequence_pos, &discont);
1250 GST_INFO_OBJECT (hlsdemux, "This playlist doesn't contain more fragments");
1251 return GST_FLOW_EOS;
1254 if (GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER (stream) && file->init_file) {
1255 GstM3U8InitFile *header_file = file->init_file;
1256 stream->fragment.header_uri = g_strdup (header_file->uri);
1257 stream->fragment.header_range_start = header_file->offset;
1258 if (header_file->size != -1) {
1259 stream->fragment.header_range_end =
1260 header_file->offset + header_file->size - 1;
1262 stream->fragment.header_range_end = -1;
1266 if (stream->discont)
1269 /* set up our source for download */
1270 if (hlsdemux_stream->reset_pts || discont
1271 || stream->demux->segment.rate < 0.0) {
1272 stream->fragment.timestamp = sequence_pos;
1274 stream->fragment.timestamp = GST_CLOCK_TIME_NONE;
1277 g_free (hlsdemux_stream->current_key);
1278 hlsdemux_stream->current_key = g_strdup (file->key);
1279 g_free (hlsdemux_stream->current_iv);
1280 hlsdemux_stream->current_iv = g_memdup (file->iv, sizeof (file->iv));
1282 g_free (stream->fragment.uri);
1283 stream->fragment.uri = g_strdup (file->uri);
1285 GST_DEBUG_OBJECT (hlsdemux, "Stream %p URI now %s", stream, file->uri);
1287 stream->fragment.range_start = file->offset;
1288 if (file->size != -1)
1289 stream->fragment.range_end = file->offset + file->size - 1;
1291 stream->fragment.range_end = -1;
1293 stream->fragment.duration = file->duration;
1296 stream->discont = TRUE;
1298 gst_m3u8_media_file_unref (file);
1304 gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream, guint64 bitrate)
1306 GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (stream->demux);
1307 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
1308 GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
1310 gboolean changed = FALSE;
1312 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
1313 if (hlsdemux->master == NULL || hlsdemux->master->is_simple) {
1314 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
1317 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
1319 if (hls_stream->is_primary_playlist == FALSE) {
1320 GST_LOG_OBJECT (hlsdemux,
1321 "Stream %p Not choosing new bitrate - not the primary stream", stream);
1325 gst_hls_demux_change_playlist (hlsdemux, bitrate / MAX (1.0,
1326 ABS (demux->segment.rate)), &changed);
1328 gst_hls_demux_setup_streams (GST_ADAPTIVE_DEMUX_CAST (hlsdemux));
1333 gst_hls_demux_reset (GstAdaptiveDemux * ademux)
1335 GstHLSDemux *demux = GST_HLS_DEMUX_CAST (ademux);
1337 GST_DEBUG_OBJECT (demux, "resetting");
1339 GST_M3U8_CLIENT_LOCK (hlsdemux->client);
1340 if (demux->master) {
1341 gst_hls_master_playlist_unref (demux->master);
1342 demux->master = NULL;
1344 if (demux->current_variant != NULL) {
1345 gst_hls_variant_stream_unref (demux->current_variant);
1346 demux->current_variant = NULL;
1348 if (demux->previous_variant != NULL) {
1349 gst_hls_variant_stream_unref (demux->previous_variant);
1350 demux->previous_variant = NULL;
1352 demux->srcpad_counter = 0;
1353 demux->streams_aware = GST_OBJECT_PARENT (demux)
1354 && GST_OBJECT_FLAG_IS_SET (GST_OBJECT_PARENT (demux),
1355 GST_BIN_FLAG_STREAMS_AWARE);
1356 GST_DEBUG_OBJECT (demux, "Streams aware : %d", demux->streams_aware);
1358 gst_hls_demux_clear_all_pending_data (demux);
1359 GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
1363 gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf)
1368 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1371 if (!g_utf8_validate ((gchar *) info.data, info.size, NULL))
1372 goto validate_error;
1374 /* alloc size + 1 to end with a null character */
1375 playlist = g_malloc0 (info.size + 1);
1376 memcpy (playlist, info.data, info.size);
1378 gst_buffer_unmap (buf, &info);
1382 gst_buffer_unmap (buf, &info);
1388 gst_hls_demux_find_variant_match (const GstHLSVariantStream * a,
1389 const GstHLSVariantStream * b)
1391 if (g_strcmp0 (a->name, b->name) == 0 &&
1392 a->bandwidth == b->bandwidth &&
1393 a->program_id == b->program_id &&
1394 g_strcmp0 (a->codecs, b->codecs) == 0 &&
1395 a->width == b->width &&
1396 a->height == b->height && a->iframe == b->iframe) {
1403 /* Update the master playlist, which contains the list of available
1406 gst_hls_demux_update_variant_playlist (GstHLSDemux * hlsdemux, gchar * data,
1407 const gchar * uri, const gchar * base_uri)
1409 GstHLSMasterPlaylist *new_master, *old;
1410 gboolean ret = FALSE;
1411 GList *l, *unmatched_lists;
1412 GstHLSVariantStream *new_variant;
1414 new_master = gst_hls_master_playlist_new_from_data (data, base_uri ? base_uri : uri); // FIXME: check which uri to use here
1416 if (new_master == NULL)
1419 if (new_master->is_simple) {
1420 // FIXME: we should be able to support this though, in the unlikely
1421 // case that it changed?
1423 ("Cannot update variant playlist: New playlist is not a variant playlist");
1424 gst_hls_master_playlist_unref (new_master);
1428 GST_M3U8_CLIENT_LOCK (self);
1430 if (hlsdemux->master->is_simple) {
1432 ("Cannot update variant playlist: Current playlist is not a variant playlist");
1433 gst_hls_master_playlist_unref (new_master);
1437 /* Now see if the variant playlist still has the same lists */
1438 unmatched_lists = g_list_copy (hlsdemux->master->variants);
1439 for (l = new_master->variants; l != NULL; l = l->next) {
1440 GList *match = g_list_find_custom (unmatched_lists, l->data,
1441 (GCompareFunc) gst_hls_demux_find_variant_match);
1444 GstHLSVariantStream *variant = l->data;
1445 GstHLSVariantStream *old = match->data;
1447 unmatched_lists = g_list_delete_link (unmatched_lists, match);
1448 /* FIXME: Deal with losing position due to missing an update */
1449 variant->m3u8->sequence_position = old->m3u8->sequence_position;
1450 variant->m3u8->sequence = old->m3u8->sequence;
1454 if (unmatched_lists != NULL) {
1455 GST_WARNING ("Unable to match all playlists");
1457 for (l = unmatched_lists; l != NULL; l = l->next) {
1458 if (l->data == hlsdemux->current_variant) {
1459 GST_WARNING ("Unable to match current playlist");
1463 g_list_free (unmatched_lists);
1466 /* Switch out the variant playlist */
1467 old = hlsdemux->master;
1469 // FIXME: check all this and also switch of variants, if anything needs updating
1470 hlsdemux->master = new_master;
1472 if (hlsdemux->current_variant == NULL) {
1473 new_variant = new_master->default_variant;
1475 /* Find the same variant in the new playlist */
1477 gst_hls_master_playlist_get_matching_variant (new_master,
1478 hlsdemux->current_variant);
1481 /* Use the function to set the current variant, as it copies over data */
1482 if (new_variant != NULL)
1483 gst_hls_demux_set_current_variant (hlsdemux, new_variant);
1485 gst_hls_master_playlist_unref (old);
1487 ret = (hlsdemux->current_variant != NULL);
1489 GST_M3U8_CLIENT_UNLOCK (self);
1495 gst_hls_demux_update_rendition_manifest (GstHLSDemux * demux,
1496 GstHLSMedia * media, GError ** err)
1498 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
1499 GstFragment *download;
1502 const gchar *main_uri;
1504 gchar *uri = media->uri;
1506 main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1508 gst_uri_downloader_fetch_uri (adaptive_demux->downloader, uri, main_uri,
1509 TRUE, TRUE, TRUE, err);
1511 if (download == NULL)
1514 m3u8 = media->playlist;
1516 /* Set the base URI of the playlist to the redirect target if any */
1517 if (download->redirect_permanent && download->redirect_uri) {
1518 gst_m3u8_set_uri (m3u8, download->redirect_uri, NULL, media->name);
1520 gst_m3u8_set_uri (m3u8, download->uri, download->redirect_uri, media->name);
1523 buf = gst_fragment_get_buffer (download);
1524 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1525 gst_buffer_unref (buf);
1526 g_object_unref (download);
1528 if (playlist == NULL) {
1529 GST_WARNING_OBJECT (demux, "Couldn't validate playlist encoding");
1530 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1531 "Couldn't validate playlist encoding");
1535 if (!gst_m3u8_update (m3u8, playlist)) {
1536 GST_WARNING_OBJECT (demux, "Couldn't update playlist");
1537 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
1538 "Couldn't update playlist");
1546 gst_hls_demux_update_playlist (GstHLSDemux * demux, gboolean update,
1549 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
1550 GstFragment *download;
1553 gboolean main_checked = FALSE;
1554 const gchar *main_uri;
1560 uri = gst_m3u8_get_uri (demux->current_variant->m3u8);
1561 main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1563 gst_uri_downloader_fetch_uri (adaptive_demux->downloader, uri, main_uri,
1564 TRUE, TRUE, TRUE, err);
1565 if (download == NULL) {
1568 if (!update || main_checked || demux->master->is_simple
1569 || !gst_adaptive_demux_is_running (GST_ADAPTIVE_DEMUX_CAST (demux))) {
1573 g_clear_error (err);
1574 GST_INFO_OBJECT (demux,
1575 "Updating playlist %s failed, attempt to refresh variant playlist %s",
1578 gst_uri_downloader_fetch_uri (adaptive_demux->downloader,
1579 main_uri, NULL, TRUE, TRUE, TRUE, err);
1580 if (download == NULL) {
1585 buf = gst_fragment_get_buffer (download);
1586 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1587 gst_buffer_unref (buf);
1589 if (playlist == NULL) {
1590 GST_WARNING_OBJECT (demux,
1591 "Failed to validate variant playlist encoding");
1593 g_object_unref (download);
1594 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1595 "Couldn't validate playlist encoding");
1600 if (download->redirect_permanent && download->redirect_uri) {
1601 uri = download->redirect_uri;
1604 uri = download->uri;
1605 base_uri = download->redirect_uri;
1608 if (!gst_hls_demux_update_variant_playlist (demux, playlist, uri, base_uri)) {
1609 GST_WARNING_OBJECT (demux, "Failed to update the variant playlist");
1610 g_object_unref (download);
1611 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
1612 "Couldn't update playlist");
1616 g_object_unref (download);
1618 main_checked = TRUE;
1623 m3u8 = demux->current_variant->m3u8;
1625 /* Set the base URI of the playlist to the redirect target if any */
1626 if (download->redirect_permanent && download->redirect_uri) {
1627 gst_m3u8_set_uri (m3u8, download->redirect_uri, NULL,
1628 demux->current_variant->name);
1630 gst_m3u8_set_uri (m3u8, download->uri, download->redirect_uri,
1631 demux->current_variant->name);
1634 buf = gst_fragment_get_buffer (download);
1635 playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1636 gst_buffer_unref (buf);
1637 g_object_unref (download);
1639 if (playlist == NULL) {
1640 GST_WARNING_OBJECT (demux, "Couldn't validate playlist encoding");
1641 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1642 "Couldn't validate playlist encoding");
1646 if (!gst_m3u8_update (m3u8, playlist)) {
1647 GST_WARNING_OBJECT (demux, "Couldn't update playlist");
1648 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
1649 "Couldn't update playlist");
1653 for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
1654 GList *mlist = demux->current_variant->media[i];
1656 while (mlist != NULL) {
1657 GstHLSMedia *media = mlist->data;
1659 if (media->uri == NULL) {
1660 /* No uri means this is a placeholder for a stream
1661 * contained in another mux */
1662 mlist = mlist->next;
1665 GST_LOG_OBJECT (demux,
1666 "Updating playlist for media of type %d - %s, uri: %s", i,
1667 media->name, media->uri);
1669 if (!gst_hls_demux_update_rendition_manifest (demux, media, err))
1672 mlist = mlist->next;
1676 /* If it's a live source, do not let the sequence number go beyond
1677 * three fragments before the end of the list */
1678 if (update == FALSE && gst_m3u8_is_live (m3u8)) {
1679 gint64 last_sequence, first_sequence;
1681 GST_M3U8_CLIENT_LOCK (demux->client);
1683 GST_M3U8_MEDIA_FILE (g_list_last (m3u8->files)->data)->sequence;
1685 GST_M3U8_MEDIA_FILE (g_list_first (m3u8->files)->data)->sequence;
1687 GST_DEBUG_OBJECT (demux,
1688 "sequence:%" G_GINT64_FORMAT " , first_sequence:%" G_GINT64_FORMAT
1689 " , last_sequence:%" G_GINT64_FORMAT, m3u8->sequence,
1690 first_sequence, last_sequence);
1691 if (m3u8->sequence > last_sequence - 3) {
1692 //demux->need_segment = TRUE;
1693 /* Make sure we never go below the minimum sequence number */
1694 m3u8->sequence = MAX (first_sequence, last_sequence - 3);
1695 GST_DEBUG_OBJECT (demux,
1696 "Sequence is beyond playlist. Moving back to %" G_GINT64_FORMAT,
1699 GST_M3U8_CLIENT_UNLOCK (demux->client);
1700 } else if (!gst_m3u8_is_live (m3u8)) {
1701 GstClockTime current_pos, target_pos;
1705 /* Sequence numbers are not guaranteed to be the same in different
1706 * playlists, so get the correct fragment here based on the current
1709 GST_M3U8_CLIENT_LOCK (demux->client);
1711 /* Valid because hlsdemux only has a single output */
1712 if (GST_ADAPTIVE_DEMUX_CAST (demux)->streams) {
1713 GstAdaptiveDemuxStream *stream =
1714 GST_ADAPTIVE_DEMUX_CAST (demux)->streams->data;
1715 target_pos = stream->segment.position;
1719 if (GST_CLOCK_TIME_IS_VALID (m3u8->sequence_position)) {
1720 target_pos = MAX (target_pos, m3u8->sequence_position);
1723 GST_LOG_OBJECT (demux, "Looking for sequence position %"
1724 GST_TIME_FORMAT " in updated playlist", GST_TIME_ARGS (target_pos));
1727 for (walk = m3u8->files; walk; walk = walk->next) {
1728 GstM3U8MediaFile *file = walk->data;
1730 sequence = file->sequence;
1731 if (current_pos <= target_pos
1732 && target_pos < current_pos + file->duration) {
1735 current_pos += file->duration;
1737 /* End of playlist */
1740 m3u8->sequence = sequence;
1741 m3u8->sequence_position = current_pos;
1742 GST_M3U8_CLIENT_UNLOCK (demux->client);
1749 gst_hls_demux_change_playlist (GstHLSDemux * demux, guint max_bitrate,
1752 GstHLSVariantStream *lowest_variant, *lowest_ivariant;
1753 GstHLSVariantStream *previous_variant, *new_variant;
1754 gint old_bandwidth, new_bandwidth;
1755 GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX_CAST (demux);
1756 GstAdaptiveDemuxStream *stream;
1758 g_return_val_if_fail (adaptive_demux->streams != NULL, FALSE);
1760 stream = adaptive_demux->streams->data;
1762 /* Make sure we keep a reference in case we need to switch back */
1763 previous_variant = gst_hls_variant_stream_ref (demux->current_variant);
1765 gst_hls_master_playlist_get_variant_for_bitrate (demux->master,
1766 demux->current_variant, max_bitrate);
1768 GST_M3U8_CLIENT_LOCK (demux->client);
1770 retry_failover_protection:
1771 old_bandwidth = previous_variant->bandwidth;
1772 new_bandwidth = new_variant->bandwidth;
1774 /* Don't do anything else if the playlist is the same */
1775 if (new_bandwidth == old_bandwidth) {
1776 GST_M3U8_CLIENT_UNLOCK (demux->client);
1777 gst_hls_variant_stream_unref (previous_variant);
1781 GST_M3U8_CLIENT_UNLOCK (demux->client);
1783 gst_hls_demux_set_current_variant (demux, new_variant);
1785 GST_INFO_OBJECT (demux, "Client was on %dbps, max allowed is %dbps, switching"
1786 " to bitrate %dbps", old_bandwidth, max_bitrate, new_bandwidth);
1788 if (gst_hls_demux_update_playlist (demux, TRUE, NULL)) {
1789 const gchar *main_uri;
1792 uri = gst_m3u8_get_uri (new_variant->m3u8);
1793 main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1794 gst_element_post_message (GST_ELEMENT_CAST (demux),
1795 gst_message_new_element (GST_OBJECT_CAST (demux),
1796 gst_structure_new (GST_ADAPTIVE_DEMUX_STATISTICS_MESSAGE_NAME,
1797 "manifest-uri", G_TYPE_STRING,
1798 main_uri, "uri", G_TYPE_STRING,
1799 uri, "bitrate", G_TYPE_INT, new_bandwidth, NULL)));
1803 stream->discont = TRUE;
1804 } else if (gst_adaptive_demux_is_running (GST_ADAPTIVE_DEMUX_CAST (demux))) {
1805 GstHLSVariantStream *failover_variant = NULL;
1808 GST_INFO_OBJECT (demux, "Unable to update playlist. Switching back");
1809 GST_M3U8_CLIENT_LOCK (demux->client);
1811 /* we find variants by bitrate by going from highest to lowest, so it's
1812 * possible that there's another variant with the same bitrate before the
1813 * one selected which we can use as failover */
1814 failover = g_list_find (demux->master->variants, new_variant);
1815 if (failover != NULL)
1816 failover = failover->prev;
1817 if (failover != NULL)
1818 failover_variant = failover->data;
1819 if (failover_variant && new_bandwidth == failover_variant->bandwidth) {
1820 new_variant = failover_variant;
1821 goto retry_failover_protection;
1824 GST_M3U8_CLIENT_UNLOCK (demux->client);
1825 gst_hls_demux_set_current_variant (demux, previous_variant);
1826 /* Try a lower bitrate (or stop if we just tried the lowest) */
1827 if (previous_variant->iframe) {
1828 lowest_ivariant = demux->master->iframe_variants->data;
1829 if (new_bandwidth == lowest_ivariant->bandwidth)
1832 lowest_variant = demux->master->variants->data;
1833 if (new_bandwidth == lowest_variant->bandwidth)
1836 return gst_hls_demux_change_playlist (demux, new_bandwidth - 1, changed);
1839 gst_hls_variant_stream_unref (previous_variant);
1843 #if defined(HAVE_OPENSSL)
1845 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1846 const guint8 * key_data, const guint8 * iv_data)
1848 EVP_CIPHER_CTX *ctx;
1849 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1850 EVP_CIPHER_CTX_init (&stream->aes_ctx);
1851 ctx = &stream->aes_ctx;
1853 stream->aes_ctx = EVP_CIPHER_CTX_new ();
1854 ctx = stream->aes_ctx;
1856 if (!EVP_DecryptInit_ex (ctx, EVP_aes_128_cbc (), NULL, key_data, iv_data))
1858 EVP_CIPHER_CTX_set_padding (ctx, 0);
1863 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1864 const guint8 * encrypted_data, guint8 * decrypted_data)
1867 EVP_CIPHER_CTX *ctx;
1869 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1870 ctx = &stream->aes_ctx;
1872 ctx = stream->aes_ctx;
1875 if (G_UNLIKELY (length > G_MAXINT || length % 16 != 0))
1879 if (!EVP_DecryptUpdate (ctx, decrypted_data, &len, encrypted_data, len))
1881 EVP_DecryptFinal_ex (ctx, decrypted_data + len, &flen);
1882 g_return_val_if_fail (len + flen == length, FALSE);
1887 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1889 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1890 EVP_CIPHER_CTX_cleanup (&stream->aes_ctx);
1892 EVP_CIPHER_CTX_free (stream->aes_ctx);
1893 stream->aes_ctx = NULL;
1897 #elif defined(HAVE_NETTLE)
1899 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1900 const guint8 * key_data, const guint8 * iv_data)
1902 aes128_set_decrypt_key (&stream->aes_ctx.ctx, key_data);
1903 CBC_SET_IV (&stream->aes_ctx, iv_data);
1909 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1910 const guint8 * encrypted_data, guint8 * decrypted_data)
1912 if (length % 16 != 0)
1915 CBC_DECRYPT (&stream->aes_ctx, aes128_decrypt, length, decrypted_data,
1922 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1927 #elif defined(HAVE_LIBGCRYPT)
1929 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1930 const guint8 * key_data, const guint8 * iv_data)
1932 gcry_error_t err = 0;
1933 gboolean ret = FALSE;
1936 gcry_cipher_open (&stream->aes_ctx, GCRY_CIPHER_AES128,
1937 GCRY_CIPHER_MODE_CBC, 0);
1940 err = gcry_cipher_setkey (stream->aes_ctx, key_data, 16);
1943 err = gcry_cipher_setiv (stream->aes_ctx, iv_data, 16);
1949 if (stream->aes_ctx)
1950 gcry_cipher_close (stream->aes_ctx);
1956 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1957 const guint8 * encrypted_data, guint8 * decrypted_data)
1959 gcry_error_t err = 0;
1961 err = gcry_cipher_decrypt (stream->aes_ctx, decrypted_data, length,
1962 encrypted_data, length);
1968 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1970 if (stream->aes_ctx) {
1971 gcry_cipher_close (stream->aes_ctx);
1972 stream->aes_ctx = NULL;
1977 /* NO crypto available */
1979 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1980 const guint8 * key_data, const guint8 * iv_data)
1982 GST_ERROR ("No crypto available");
1987 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1988 const guint8 * encrypted_data, guint8 * decrypted_data)
1990 GST_ERROR ("Cannot decrypt fragment, no crypto available");
1995 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
2002 gst_hls_demux_decrypt_fragment (GstHLSDemux * demux, GstHLSDemuxStream * stream,
2003 GstBuffer * encrypted_buffer, GError ** err)
2005 GstBuffer *decrypted_buffer = NULL;
2006 GstMapInfo encrypted_info, decrypted_info;
2009 gst_buffer_new_allocate (NULL, gst_buffer_get_size (encrypted_buffer),
2012 gst_buffer_map (encrypted_buffer, &encrypted_info, GST_MAP_READ);
2013 gst_buffer_map (decrypted_buffer, &decrypted_info, GST_MAP_WRITE);
2015 if (!decrypt_fragment (stream, encrypted_info.size,
2016 encrypted_info.data, decrypted_info.data))
2020 gst_buffer_unmap (decrypted_buffer, &decrypted_info);
2021 gst_buffer_unmap (encrypted_buffer, &encrypted_info);
2023 gst_buffer_unref (encrypted_buffer);
2025 return decrypted_buffer;
2028 GST_ERROR_OBJECT (demux, "Failed to decrypt fragment");
2029 g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_DECRYPT,
2030 "Failed to decrypt fragment");
2032 gst_buffer_unmap (decrypted_buffer, &decrypted_info);
2033 gst_buffer_unmap (encrypted_buffer, &encrypted_info);
2035 gst_buffer_unref (encrypted_buffer);
2036 gst_buffer_unref (decrypted_buffer);
2042 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
2044 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
2045 GstClockTime target_duration;
2047 if (hlsdemux->current_variant) {
2049 gst_m3u8_get_target_duration (hlsdemux->current_variant->m3u8);
2051 target_duration = 5 * GST_SECOND;
2054 return gst_util_uint64_scale (target_duration, G_USEC_PER_SEC, GST_SECOND);
2058 gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
2061 GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
2062 gboolean ret = FALSE;
2064 if (hlsdemux->current_variant) {
2066 gst_m3u8_get_seek_range (hlsdemux->current_variant->m3u8, start, stop);