hlsdemux: Publish all media for alternate renditions.
[platform/upstream/gstreamer.git] / ext / hls / gsthlsdemux.c
1 /* GStreamer
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>
9  *
10  * Gsthlsdemux.c:
11  *
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.
16  *
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.
21  *
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.
26  */
27 /**
28  * SECTION:element-hlsdemux
29  *
30  * HTTP Live Streaming demuxer element.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch-1.0 souphttpsrc location=http://devimages.apple.com/iphone/samples/bipbop/gear4/prog_index.m3u8 ! hlsdemux ! decodebin ! videoconvert ! videoscale ! autovideosink
36  * ]|
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #  include "config.h"
42 #endif
43
44 #include <string.h>
45 #include <gst/base/gsttypefindhelper.h>
46 #include "gsthlsdemux.h"
47
48 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
49     GST_PAD_SRC,
50     GST_PAD_SOMETIMES,
51     GST_STATIC_CAPS_ANY);
52
53 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("application/x-hls"));
57
58 GST_DEBUG_CATEGORY_STATIC (gst_hls_demux_debug);
59 #define GST_CAT_DEFAULT gst_hls_demux_debug
60
61 #define GST_M3U8_CLIENT_LOCK(l) /* FIXME */
62 #define GST_M3U8_CLIENT_UNLOCK(l)       /* FIXME */
63
64 /* GObject */
65 static void gst_hls_demux_finalize (GObject * obj);
66
67 /* GstElement */
68 static GstStateChangeReturn
69 gst_hls_demux_change_state (GstElement * element, GstStateChange transition);
70
71 /* GstHLSDemux */
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);
75
76 static gboolean gst_hls_demux_change_playlist (GstHLSDemux * demux,
77     guint max_bitrate, gboolean * changed);
78 static GstBuffer *gst_hls_demux_decrypt_fragment (GstHLSDemux * demux,
79     GstHLSDemuxStream * stream, GstBuffer * encrypted_buffer, GError ** err);
80 static gboolean
81 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
82     const guint8 * key_data, const guint8 * iv_data);
83 static void gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream);
84
85 static gboolean gst_hls_demux_is_live (GstAdaptiveDemux * demux);
86 static GstClockTime gst_hls_demux_get_duration (GstAdaptiveDemux * demux);
87 static gint64 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux *
88     demux);
89 static gboolean gst_hls_demux_process_manifest (GstAdaptiveDemux * demux,
90     GstBuffer * buf);
91 static GstFlowReturn gst_hls_demux_update_manifest (GstAdaptiveDemux * demux);
92 static gboolean gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek);
93 static gboolean
94 gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
95     GstAdaptiveDemuxStream * stream);
96 static GstFlowReturn gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
97     GstAdaptiveDemuxStream * stream);
98 static GstFlowReturn gst_hls_demux_data_received (GstAdaptiveDemux * demux,
99     GstAdaptiveDemuxStream * stream, GstBuffer * buffer);
100 static void gst_hls_demux_stream_free (GstAdaptiveDemuxStream * stream);
101 static gboolean gst_hls_demux_stream_has_next_fragment (GstAdaptiveDemuxStream *
102     stream);
103 static GstFlowReturn gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream *
104     stream);
105 static GstFlowReturn gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream
106     * stream);
107 static gboolean gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream,
108     guint64 bitrate);
109 static void gst_hls_demux_reset (GstAdaptiveDemux * demux);
110 static gboolean gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux,
111     gint64 * start, gint64 * stop);
112 static GstM3U8 *gst_hls_demux_stream_get_m3u8 (GstHLSDemuxStream * hls_stream);
113 static void gst_hls_demux_set_current_variant (GstHLSDemux * hlsdemux,
114     GstHLSVariantStream * variant);
115
116 #define gst_hls_demux_parent_class parent_class
117 G_DEFINE_TYPE (GstHLSDemux, gst_hls_demux, GST_TYPE_ADAPTIVE_DEMUX);
118
119 static void
120 gst_hls_demux_finalize (GObject * obj)
121 {
122   GstHLSDemux *demux = GST_HLS_DEMUX (obj);
123
124   gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
125   g_mutex_clear (&demux->keys_lock);
126
127   G_OBJECT_CLASS (parent_class)->finalize (obj);
128 }
129
130 static void
131 gst_hls_demux_class_init (GstHLSDemuxClass * klass)
132 {
133   GObjectClass *gobject_class;
134   GstElementClass *element_class;
135   GstAdaptiveDemuxClass *adaptivedemux_class;
136
137   gobject_class = (GObjectClass *) klass;
138   element_class = (GstElementClass *) klass;
139   adaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
140
141   gobject_class->finalize = gst_hls_demux_finalize;
142
143   element_class->change_state = GST_DEBUG_FUNCPTR (gst_hls_demux_change_state);
144
145   gst_element_class_add_static_pad_template (element_class, &srctemplate);
146   gst_element_class_add_static_pad_template (element_class, &sinktemplate);
147
148   gst_element_class_set_static_metadata (element_class,
149       "HLS Demuxer",
150       "Codec/Demuxer/Adaptive",
151       "HTTP Live Streaming demuxer",
152       "Marc-Andre Lureau <marcandre.lureau@gmail.com>\n"
153       "Andoni Morales Alastruey <ylatuya@gmail.com>");
154
155   adaptivedemux_class->is_live = gst_hls_demux_is_live;
156   adaptivedemux_class->get_live_seek_range = gst_hls_demux_get_live_seek_range;
157   adaptivedemux_class->get_duration = gst_hls_demux_get_duration;
158   adaptivedemux_class->get_manifest_update_interval =
159       gst_hls_demux_get_manifest_update_interval;
160   adaptivedemux_class->process_manifest = gst_hls_demux_process_manifest;
161   adaptivedemux_class->update_manifest = gst_hls_demux_update_manifest;
162   adaptivedemux_class->reset = gst_hls_demux_reset;
163   adaptivedemux_class->seek = gst_hls_demux_seek;
164   adaptivedemux_class->stream_has_next_fragment =
165       gst_hls_demux_stream_has_next_fragment;
166   adaptivedemux_class->stream_advance_fragment = gst_hls_demux_advance_fragment;
167   adaptivedemux_class->stream_update_fragment_info =
168       gst_hls_demux_update_fragment_info;
169   adaptivedemux_class->stream_select_bitrate = gst_hls_demux_select_bitrate;
170   adaptivedemux_class->stream_free = gst_hls_demux_stream_free;
171
172   adaptivedemux_class->start_fragment = gst_hls_demux_start_fragment;
173   adaptivedemux_class->finish_fragment = gst_hls_demux_finish_fragment;
174   adaptivedemux_class->data_received = gst_hls_demux_data_received;
175
176   GST_DEBUG_CATEGORY_INIT (gst_hls_demux_debug, "hlsdemux", 0,
177       "hlsdemux element");
178 }
179
180 static void
181 gst_hls_demux_init (GstHLSDemux * demux)
182 {
183   gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
184       sizeof (GstHLSDemuxStream));
185
186   demux->keys = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
187   g_mutex_init (&demux->keys_lock);
188 }
189
190 static GstStateChangeReturn
191 gst_hls_demux_change_state (GstElement * element, GstStateChange transition)
192 {
193   GstStateChangeReturn ret;
194   GstHLSDemux *demux = GST_HLS_DEMUX (element);
195
196   switch (transition) {
197     case GST_STATE_CHANGE_READY_TO_PAUSED:
198       gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
199       break;
200     default:
201       break;
202   }
203
204   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
205
206   switch (transition) {
207     case GST_STATE_CHANGE_PAUSED_TO_READY:
208       gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
209       g_hash_table_remove_all (demux->keys);
210       break;
211     default:
212       break;
213   }
214   return ret;
215 }
216
217 static GstPad *
218 gst_hls_demux_create_pad (GstHLSDemux * hlsdemux)
219 {
220   gchar *name;
221   GstPad *pad;
222
223   name = g_strdup_printf ("src_%u", hlsdemux->srcpad_counter++);
224   pad = gst_pad_new_from_static_template (&srctemplate, name);
225   g_free (name);
226
227   return pad;
228 }
229
230 static guint64
231 gst_hls_demux_get_bitrate (GstHLSDemux * hlsdemux)
232 {
233   GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (hlsdemux);
234
235   /* Valid because hlsdemux only has a single output */
236   if (demux->streams) {
237     GstAdaptiveDemuxStream *stream = demux->streams->data;
238     return stream->current_download_rate;
239   }
240
241   return 0;
242 }
243
244 static void
245 gst_hls_demux_stream_clear_pending_data (GstHLSDemuxStream * hls_stream)
246 {
247   if (hls_stream->pending_encrypted_data)
248     gst_adapter_clear (hls_stream->pending_encrypted_data);
249   gst_buffer_replace (&hls_stream->pending_decrypted_buffer, NULL);
250   gst_buffer_replace (&hls_stream->pending_typefind_buffer, NULL);
251   hls_stream->current_offset = -1;
252   gst_hls_demux_stream_decrypt_end (hls_stream);
253 }
254
255 static void
256 gst_hls_demux_clear_all_pending_data (GstHLSDemux * hlsdemux)
257 {
258   GstAdaptiveDemux *demux = (GstAdaptiveDemux *) hlsdemux;
259   GList *walk;
260
261   for (walk = demux->streams; walk != NULL; walk = walk->next) {
262     GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (walk->data);
263     gst_hls_demux_stream_clear_pending_data (hls_stream);
264   }
265 }
266
267 #if 0
268 static void
269 gst_hls_demux_set_current (GstHLSDemux * self, GstM3U8 * m3u8)
270 {
271   GST_M3U8_CLIENT_LOCK (self);
272   if (m3u8 != self->current) {
273     self->current = m3u8;
274     self->current->duration = GST_CLOCK_TIME_NONE;
275     self->current->current_file = NULL;
276
277 #if 0
278     // FIXME: this makes no sense after we just set self->current=m3u8 above (tpm)
279     // also, these values don't necessarily align between different lists
280     m3u8->current_file_duration = self->current->current_file_duration;
281     m3u8->sequence = self->current->sequence;
282     m3u8->sequence_position = self->current->sequence_position;
283     m3u8->highest_sequence_number = self->current->highest_sequence_number;
284     m3u8->first_file_start = self->current->first_file_start;
285     m3u8->last_file_end = self->current->last_file_end;
286 #endif
287   }
288   GST_M3U8_CLIENT_UNLOCK (self);
289 }
290 #endif
291
292 static gboolean
293 gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
294 {
295   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
296   GstFormat format;
297   GstSeekFlags flags;
298   GstSeekType start_type, stop_type;
299   gint64 start, stop;
300   gdouble rate, old_rate;
301   GList *walk, *current_file = NULL;
302   GstClockTime current_pos, target_pos;
303   gint64 current_sequence;
304   GstM3U8MediaFile *file;
305   guint64 bitrate;
306   gboolean snap_before, snap_after, snap_nearest, keyunit;
307   gboolean reverse;
308
309   old_rate = demux->segment.rate;
310
311   gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
312       &stop_type, &stop);
313
314   bitrate = gst_hls_demux_get_bitrate (hlsdemux);
315
316   /* properly cleanup pending decryption status */
317   if (flags & GST_SEEK_FLAG_FLUSH) {
318     gst_hls_demux_clear_all_pending_data (hlsdemux);
319   }
320
321   /* Use I-frame variants for trick modes */
322   if (hlsdemux->master->iframe_variants != NULL
323       && rate < -1.0 && old_rate >= -1.0 && old_rate <= 1.0) {
324     GError *err = NULL;
325
326     /* Switch to I-frame variant */
327     gst_hls_demux_set_current_variant (hlsdemux,
328         hlsdemux->master->iframe_variants->data);
329     gst_uri_downloader_reset (demux->downloader);
330     if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
331       GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
332       return FALSE;
333     }
334     //hlsdemux->discont = TRUE;
335
336     gst_hls_demux_change_playlist (hlsdemux, bitrate / ABS (rate), NULL);
337   } else if (rate > -1.0 && rate <= 1.0 && (old_rate < -1.0 || old_rate > 1.0)) {
338     GError *err = NULL;
339     /* Switch to normal variant */
340     gst_hls_demux_set_current_variant (hlsdemux,
341         hlsdemux->master->variants->data);
342     gst_uri_downloader_reset (demux->downloader);
343     if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
344       GST_ELEMENT_ERROR_FROM_ERROR (hlsdemux, "Could not switch playlist", err);
345       return FALSE;
346     }
347     //hlsdemux->discont = TRUE;
348     /* TODO why not continue using the same? that was being used up to now? */
349     gst_hls_demux_change_playlist (hlsdemux, bitrate, NULL);
350   }
351   GST_M3U8_CLIENT_LOCK (hlsdemux->client);
352   file = GST_M3U8_MEDIA_FILE (hlsdemux->current_variant->m3u8->files->data);
353   current_sequence = file->sequence;
354   current_pos = 0;
355   reverse = rate < 0;
356   target_pos = reverse ? stop : start;
357
358   /* Snap to segment boundary. Improves seek performance on slow machines. */
359   keyunit = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
360   snap_nearest =
361       (flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST;
362   snap_before = ! !(flags & GST_SEEK_FLAG_SNAP_BEFORE);
363   snap_after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
364
365   /* FIXME: Here we need proper discont handling */
366   for (walk = hlsdemux->current_variant->m3u8->files; walk; walk = walk->next) {
367     file = walk->data;
368
369     current_sequence = file->sequence;
370     current_file = walk;
371     if ((!reverse && snap_after) || snap_nearest) {
372       if (current_pos >= target_pos)
373         break;
374       if (snap_nearest && target_pos - current_pos < file->duration / 2)
375         break;
376     } else if (reverse && snap_after) {
377       /* check if the next fragment is our target, in this case we want to
378        * start from the previous fragment */
379       GstClockTime next_pos = current_pos + file->duration;
380
381       if (next_pos <= target_pos && target_pos < next_pos + file->duration) {
382         break;
383       }
384     } else if (current_pos <= target_pos
385         && target_pos < current_pos + file->duration) {
386       break;
387     }
388     current_pos += file->duration;
389   }
390
391   if (walk == NULL) {
392     GST_DEBUG_OBJECT (demux, "seeking further than track duration");
393     current_sequence++;
394   }
395
396   GST_DEBUG_OBJECT (demux, "seeking to sequence %u", (guint) current_sequence);
397   for (walk = demux->streams; walk != NULL; walk = walk->next)
398     GST_HLS_DEMUX_STREAM_CAST (walk->data)->reset_pts = TRUE;
399   hlsdemux->current_variant->m3u8->sequence = current_sequence;
400   hlsdemux->current_variant->m3u8->current_file =
401       current_file ? current_file : hlsdemux->current_variant->m3u8->files;
402   hlsdemux->current_variant->m3u8->sequence_position = current_pos;
403   GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
404
405   /* Play from the end of the current selected segment */
406   if (reverse && (snap_before || snap_after || snap_nearest))
407     current_pos += file->duration;
408
409   if (keyunit || snap_before || snap_after || snap_nearest) {
410     if (!reverse)
411       gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
412           current_pos, stop_type, stop, NULL);
413     else
414       gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
415           start, stop_type, current_pos, NULL);
416   }
417
418   return TRUE;
419 }
420
421 static GstFlowReturn
422 gst_hls_demux_update_manifest (GstAdaptiveDemux * demux)
423 {
424   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
425   if (!gst_hls_demux_update_playlist (hlsdemux, TRUE, NULL))
426     return GST_FLOW_ERROR;
427
428   return GST_FLOW_OK;
429 }
430
431 static void
432 create_stream_for_playlist (GstAdaptiveDemux * demux, GstM3U8 * playlist)
433 {
434   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
435   GstHLSDemuxStream *hlsdemux_stream;
436   GstAdaptiveDemuxStream *stream;
437
438   stream = gst_adaptive_demux_stream_new (demux,
439       gst_hls_demux_create_pad (hlsdemux));
440
441   hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
442   hlsdemux_stream->playlist = gst_m3u8_ref (playlist);
443
444   hlsdemux_stream->do_typefind = TRUE;
445   hlsdemux_stream->reset_pts = TRUE;
446 }
447
448 static gboolean
449 gst_hls_demux_setup_streams (GstAdaptiveDemux * demux)
450 {
451   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
452   GstHLSVariantStream *playlist = hlsdemux->current_variant;
453   gint i;
454
455   if (playlist == NULL) {
456     GST_WARNING_OBJECT (demux, "Can't configure streams - no variant selected");
457     return FALSE;
458   }
459
460   gst_hls_demux_clear_all_pending_data (hlsdemux);
461
462   /* 1 output for the main playlist */
463   create_stream_for_playlist (demux, playlist->m3u8);
464
465   for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
466     GList *mlist = playlist->media[i];
467     while (mlist != NULL) {
468       GstHLSMedia *media = mlist->data;
469
470       if (media->uri == NULL /* || media->mtype != GST_HLS_MEDIA_TYPE_AUDIO */ ) {
471         /* No uri means this is a placeholder for a stream
472          * contained in another mux */
473         GST_LOG_OBJECT (demux, "Skipping stream %s type %d with no URI",
474             media->name, media->mtype);
475         mlist = mlist->next;
476         continue;
477       }
478       GST_LOG_OBJECT (demux, "media of type %d - %s, uri: %s", i,
479           media->name, media->uri);
480       create_stream_for_playlist (demux, media->playlist);
481
482       mlist = mlist->next;
483     }
484   }
485
486   return TRUE;
487 }
488
489 static const gchar *
490 gst_adaptive_demux_get_manifest_ref_uri (GstAdaptiveDemux * d)
491 {
492   return d->manifest_base_uri ? d->manifest_base_uri : d->manifest_uri;
493 }
494
495 static void
496 gst_hls_demux_set_current_variant (GstHLSDemux * hlsdemux,
497     GstHLSVariantStream * variant)
498 {
499   if (hlsdemux->current_variant == variant || variant == NULL)
500     return;
501
502   if (hlsdemux->current_variant != NULL) {
503     gint i;
504
505     //#warning FIXME: Synching fragments across variants
506     //  should be done based on media timestamps, and
507     //  discont-sequence-numbers not sequence numbers.
508     variant->m3u8->sequence_position =
509         hlsdemux->current_variant->m3u8->sequence_position;
510     variant->m3u8->sequence = hlsdemux->current_variant->m3u8->sequence;
511
512     GST_DEBUG_OBJECT (hlsdemux,
513         "Switching Variant. Copying over sequence %" G_GINT64_FORMAT
514         " and sequence_pos %" GST_TIME_FORMAT, variant->m3u8->sequence,
515         GST_TIME_ARGS (variant->m3u8->sequence_position));
516
517     for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
518       GList *mlist = hlsdemux->current_variant->media[i];
519
520       while (mlist != NULL) {
521         GstHLSMedia *old_media = mlist->data;
522         GstHLSMedia *new_media =
523             gst_hls_variant_find_matching_media (variant, old_media);
524
525         if (new_media) {
526           new_media->playlist->sequence = old_media->playlist->sequence;
527           new_media->playlist->sequence_position =
528               old_media->playlist->sequence_position;
529         }
530         mlist = mlist->next;
531       }
532     }
533
534     gst_hls_variant_stream_unref (hlsdemux->current_variant);
535   }
536
537   hlsdemux->current_variant = gst_hls_variant_stream_ref (variant);
538
539 }
540
541 static gboolean
542 gst_hls_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
543 {
544   GstHLSVariantStream *variant;
545   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
546   gchar *playlist = NULL;
547
548   GST_INFO_OBJECT (demux, "Initial playlist location: %s (base uri: %s)",
549       demux->manifest_uri, demux->manifest_base_uri);
550
551   playlist = gst_hls_src_buf_to_utf8_playlist (buf);
552   if (playlist == NULL) {
553     GST_WARNING_OBJECT (demux, "Error validating initial playlist");
554     return FALSE;
555   }
556
557   hlsdemux->master = gst_hls_master_playlist_new_from_data (playlist,
558       gst_adaptive_demux_get_manifest_ref_uri (demux));
559
560   if (hlsdemux->master == NULL || hlsdemux->master->variants == NULL) {
561     /* In most cases, this will happen if we set a wrong url in the
562      * source element and we have received the 404 HTML response instead of
563      * the playlist */
564     GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Invalid playlist."),
565         ("Could not parse playlist. Check if the URL is correct."));
566     return FALSE;
567   }
568
569   /* select the initial variant stream */
570   if (demux->connection_speed == 0) {
571     variant = hlsdemux->master->default_variant;
572   } else {
573     variant =
574         gst_hls_master_playlist_get_variant_for_bitrate (hlsdemux->master,
575         NULL, demux->connection_speed);
576   }
577
578   GST_INFO_OBJECT (hlsdemux, "selected %s", variant->name);
579   gst_hls_demux_set_current_variant (hlsdemux, variant);        // FIXME: inline?
580
581   /* get the selected media playlist (unless the inital list was one already) */
582   if (!hlsdemux->master->is_simple) {
583     GError *err = NULL;
584
585     if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
586       GST_ELEMENT_ERROR_FROM_ERROR (demux, "Could not fetch media playlist",
587           err);
588       return FALSE;
589     }
590   }
591
592   return gst_hls_demux_setup_streams (demux);
593 }
594
595 static GstClockTime
596 gst_hls_demux_get_duration (GstAdaptiveDemux * demux)
597 {
598   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
599   GstClockTime duration = GST_CLOCK_TIME_NONE;
600
601   if (hlsdemux->current_variant != NULL)
602     duration = gst_m3u8_get_duration (hlsdemux->current_variant->m3u8);
603
604   return duration;
605 }
606
607 static gboolean
608 gst_hls_demux_is_live (GstAdaptiveDemux * demux)
609 {
610   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
611   gboolean is_live = FALSE;
612
613   if (hlsdemux->current_variant)
614     is_live = gst_hls_variant_stream_is_live (hlsdemux->current_variant);
615
616   return is_live;
617 }
618
619 static const GstHLSKey *
620 gst_hls_demux_get_key (GstHLSDemux * demux, const gchar * key_url,
621     const gchar * referer, gboolean allow_cache)
622 {
623   GstFragment *key_fragment;
624   GstBuffer *key_buffer;
625   GstHLSKey *key;
626   GError *err = NULL;
627
628   GST_LOG_OBJECT (demux, "Looking up key for key url %s", key_url);
629
630   g_mutex_lock (&demux->keys_lock);
631
632   key = g_hash_table_lookup (demux->keys, key_url);
633
634   if (key != NULL) {
635     GST_LOG_OBJECT (demux, "Found key for key url %s in key cache", key_url);
636     goto out;
637   }
638
639   GST_INFO_OBJECT (demux, "Fetching key %s", key_url);
640
641   key_fragment =
642       gst_uri_downloader_fetch_uri (GST_ADAPTIVE_DEMUX (demux)->downloader,
643       key_url, referer, FALSE, FALSE, allow_cache, &err);
644
645   if (key_fragment == NULL) {
646     GST_WARNING_OBJECT (demux, "Failed to download key to decrypt data: %s",
647         err ? err->message : "error");
648     g_clear_error (&err);
649     goto out;
650   }
651
652   key_buffer = gst_fragment_get_buffer (key_fragment);
653
654   key = g_new0 (GstHLSKey, 1);
655   if (gst_buffer_extract (key_buffer, 0, key->data, 16) < 16)
656     GST_WARNING_OBJECT (demux, "Download decryption key is too short!");
657
658   g_hash_table_insert (demux->keys, g_strdup (key_url), key);
659
660   gst_buffer_unref (key_buffer);
661   g_object_unref (key_fragment);
662
663 out:
664
665   g_mutex_unlock (&demux->keys_lock);
666
667   if (key != NULL)
668     GST_MEMDUMP_OBJECT (demux, "Key", key->data, 16);
669
670   return key;
671 }
672
673 static gboolean
674 gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
675     GstAdaptiveDemuxStream * stream)
676 {
677   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
678   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
679   const GstHLSKey *key;
680   GstM3U8 *m3u8;
681
682   gst_hls_demux_stream_clear_pending_data (hls_stream);
683
684   /* If no decryption is needed, there's nothing to be done here */
685   if (hls_stream->current_key == NULL)
686     return TRUE;
687
688   m3u8 = gst_hls_demux_stream_get_m3u8 (hls_stream);
689
690   key = gst_hls_demux_get_key (hlsdemux, hls_stream->current_key,
691       m3u8->uri, m3u8->allowcache);
692
693   if (key == NULL)
694     goto key_failed;
695
696   gst_hls_demux_stream_decrypt_start (hls_stream, key->data,
697       hls_stream->current_iv);
698
699   return TRUE;
700
701 key_failed:
702   {
703     GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
704         ("Couldn't retrieve key for decryption"), (NULL));
705     GST_WARNING_OBJECT (demux, "Failed to decrypt data");
706     return FALSE;
707   }
708 }
709
710 static GstFlowReturn
711 gst_hls_demux_handle_buffer (GstAdaptiveDemux * demux,
712     GstAdaptiveDemuxStream * stream, GstBuffer * buffer, gboolean at_eos)
713 {
714   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);   // FIXME: pass HlsStream into function
715   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
716
717   if (buffer == NULL)
718     return GST_FLOW_OK;
719
720   if (G_UNLIKELY (hls_stream->do_typefind)) {
721     GstCaps *caps = NULL;
722     GstMapInfo info;
723     guint buffer_size;
724     GstTypeFindProbability prob = GST_TYPE_FIND_NONE;
725
726     if (hls_stream->pending_typefind_buffer)
727       buffer = gst_buffer_append (hls_stream->pending_typefind_buffer, buffer);
728     hls_stream->pending_typefind_buffer = NULL;
729
730     gst_buffer_map (buffer, &info, GST_MAP_READ);
731     buffer_size = info.size;
732
733     /* Typefind could miss if buffer is too small. In this case we
734      * will retry later */
735     if (buffer_size >= (2 * 1024) || at_eos) {
736       caps =
737           gst_type_find_helper_for_data (GST_OBJECT_CAST (hlsdemux), info.data,
738           info.size, &prob);
739     }
740     gst_buffer_unmap (buffer, &info);
741
742     if (G_UNLIKELY (!caps)) {
743       /* Only fail typefinding if we already a good amount of data
744        * and we still don't know the type */
745       if (buffer_size > (2 * 1024 * 1024) || at_eos) {
746         GST_ELEMENT_ERROR (hlsdemux, STREAM, TYPE_NOT_FOUND,
747             ("Could not determine type of stream"), (NULL));
748         gst_buffer_unref (buffer);
749         return GST_FLOW_NOT_NEGOTIATED;
750       }
751
752       hls_stream->pending_typefind_buffer = buffer;
753
754       return GST_FLOW_OK;
755     }
756
757     GST_DEBUG_OBJECT (hlsdemux, "Typefind result: %" GST_PTR_FORMAT " prob:%d",
758         caps, prob);
759
760     gst_adaptive_demux_stream_set_caps (stream, caps);
761     hls_stream->do_typefind = FALSE;
762   }
763   g_assert (hls_stream->pending_typefind_buffer == NULL);
764
765   if (buffer) {
766     buffer = gst_buffer_make_writable (buffer);
767     GST_BUFFER_OFFSET (buffer) = hls_stream->current_offset;
768     hls_stream->current_offset += gst_buffer_get_size (buffer);
769     GST_BUFFER_OFFSET_END (buffer) = hls_stream->current_offset;
770     return gst_adaptive_demux_stream_push_buffer (stream, buffer);
771   }
772   return GST_FLOW_OK;
773 }
774
775 static GstFlowReturn
776 gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
777     GstAdaptiveDemuxStream * stream)
778 {
779   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);   // FIXME: pass HlsStream into function
780   GstFlowReturn ret = GST_FLOW_OK;
781
782   if (hls_stream->current_key)
783     gst_hls_demux_stream_decrypt_end (hls_stream);
784
785   if (stream->last_ret == GST_FLOW_OK) {
786     if (hls_stream->pending_decrypted_buffer) {
787       if (hls_stream->current_key) {
788         GstMapInfo info;
789         gssize unpadded_size;
790
791         /* Handle pkcs7 unpadding here */
792         gst_buffer_map (hls_stream->pending_decrypted_buffer, &info,
793             GST_MAP_READ);
794         unpadded_size = info.size - info.data[info.size - 1];
795         gst_buffer_unmap (hls_stream->pending_decrypted_buffer, &info);
796
797         gst_buffer_resize (hls_stream->pending_decrypted_buffer, 0,
798             unpadded_size);
799       }
800
801       ret =
802           gst_hls_demux_handle_buffer (demux, stream,
803           hls_stream->pending_decrypted_buffer, TRUE);
804       hls_stream->pending_decrypted_buffer = NULL;
805     }
806   }
807   gst_hls_demux_stream_clear_pending_data (hls_stream);
808
809   if (ret == GST_FLOW_OK || ret == GST_FLOW_NOT_LINKED)
810     return gst_adaptive_demux_stream_advance_fragment (demux, stream,
811         stream->fragment.duration);
812   return ret;
813 }
814
815 static GstFlowReturn
816 gst_hls_demux_data_received (GstAdaptiveDemux * demux,
817     GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
818 {
819   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
820   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
821
822   if (hls_stream->current_offset == -1)
823     hls_stream->current_offset = 0;
824
825   /* Is it encrypted? */
826   if (hls_stream->current_key) {
827     GError *err = NULL;
828     gsize size;
829     GstBuffer *tmp_buffer;
830
831     if (hls_stream->pending_encrypted_data == NULL)
832       hls_stream->pending_encrypted_data = gst_adapter_new ();
833
834     gst_adapter_push (hls_stream->pending_encrypted_data, buffer);
835     size = gst_adapter_available (hls_stream->pending_encrypted_data);
836
837     /* must be a multiple of 16 */
838     size &= (~0xF);
839
840     if (size == 0) {
841       return GST_FLOW_OK;
842     }
843
844     buffer = gst_adapter_take_buffer (hls_stream->pending_encrypted_data, size);
845     buffer =
846         gst_hls_demux_decrypt_fragment (hlsdemux, hls_stream, buffer, &err);
847     if (buffer == NULL) {
848       GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Failed to decrypt buffer"),
849           ("decryption failed %s", err->message));
850       g_error_free (err);
851       return GST_FLOW_ERROR;
852     }
853
854     tmp_buffer = hls_stream->pending_decrypted_buffer;
855     hls_stream->pending_decrypted_buffer = buffer;
856     buffer = tmp_buffer;
857   }
858
859   return gst_hls_demux_handle_buffer (demux, stream, buffer, FALSE);
860 }
861
862 static void
863 gst_hls_demux_stream_free (GstAdaptiveDemuxStream * stream)
864 {
865   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
866
867   if (hls_stream->playlist) {
868     gst_m3u8_unref (hls_stream->playlist);
869     hls_stream->playlist = NULL;
870   }
871
872   if (hls_stream->pending_encrypted_data)
873     g_object_unref (hls_stream->pending_encrypted_data);
874
875   gst_buffer_replace (&hls_stream->pending_decrypted_buffer, NULL);
876   gst_buffer_replace (&hls_stream->pending_typefind_buffer, NULL);
877
878   if (hls_stream->current_key) {
879     g_free (hls_stream->current_key);
880     hls_stream->current_key = NULL;
881   }
882   if (hls_stream->current_iv) {
883     g_free (hls_stream->current_iv);
884     hls_stream->current_iv = NULL;
885   }
886   gst_hls_demux_stream_decrypt_end (hls_stream);
887 }
888
889 static GstM3U8 *
890 gst_hls_demux_stream_get_m3u8 (GstHLSDemuxStream * hlsdemux_stream)
891 {
892   GstM3U8 *m3u8;
893
894   m3u8 = hlsdemux_stream->playlist;
895
896   return m3u8;
897 }
898
899 static gboolean
900 gst_hls_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
901 {
902   gboolean has_next;
903   GstM3U8 *m3u8;
904
905   m3u8 = gst_hls_demux_stream_get_m3u8 (GST_HLS_DEMUX_STREAM_CAST (stream));
906
907   has_next = gst_m3u8_has_next_fragment (m3u8, stream->demux->segment.rate > 0);
908
909   return has_next;
910 }
911
912 static GstFlowReturn
913 gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream * stream)
914 {
915   GstHLSDemuxStream *hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
916   GstM3U8 *m3u8;
917
918   m3u8 = gst_hls_demux_stream_get_m3u8 (hlsdemux_stream);
919
920   gst_m3u8_advance_fragment (m3u8, stream->demux->segment.rate > 0);
921   hlsdemux_stream->reset_pts = FALSE;
922
923   return GST_FLOW_OK;
924 }
925
926 static GstFlowReturn
927 gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream * stream)
928 {
929   GstHLSDemuxStream *hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
930   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
931   GstM3U8MediaFile *file;
932   GstClockTime sequence_pos;
933   gboolean discont, forward;
934   GstM3U8 *m3u8;
935
936   m3u8 = gst_hls_demux_stream_get_m3u8 (hlsdemux_stream);
937
938   forward = (stream->demux->segment.rate > 0);
939   file = gst_m3u8_get_next_fragment (m3u8, forward, &sequence_pos, &discont);
940
941   if (file == NULL) {
942     GST_INFO_OBJECT (hlsdemux, "This playlist doesn't contain more fragments");
943     return GST_FLOW_EOS;
944   }
945
946   if (stream->discont)
947     discont = TRUE;
948
949   /* set up our source for download */
950   if (hlsdemux_stream->reset_pts || discont
951       || stream->demux->segment.rate < 0.0) {
952     stream->fragment.timestamp = sequence_pos;
953   } else {
954     stream->fragment.timestamp = GST_CLOCK_TIME_NONE;
955   }
956
957   g_free (hlsdemux_stream->current_key);
958   hlsdemux_stream->current_key = g_strdup (file->key);
959   g_free (hlsdemux_stream->current_iv);
960   hlsdemux_stream->current_iv = g_memdup (file->iv, sizeof (file->iv));
961
962   g_free (stream->fragment.uri);
963   stream->fragment.uri = g_strdup (file->uri);
964
965   GST_DEBUG_OBJECT (stream, "URI now %s", file->uri);
966
967   stream->fragment.range_start = file->offset;
968   if (file->size != -1)
969     stream->fragment.range_end = file->offset + file->size - 1;
970   else
971     stream->fragment.range_end = -1;
972
973   stream->fragment.duration = file->duration;
974
975   if (discont)
976     stream->discont = TRUE;
977
978   return GST_FLOW_OK;
979 }
980
981 static gboolean
982 gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream, guint64 bitrate)
983 {
984   GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (stream->demux);
985   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (stream->demux);
986   gboolean changed = FALSE;
987
988   GST_M3U8_CLIENT_LOCK (hlsdemux->client);
989   if (hlsdemux->master->is_simple) {
990     GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
991     return FALSE;
992   }
993   GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
994
995   /* Bitrate adaptation during trick modes does not work well */
996   if (demux->segment.rate != 1.0)
997     return FALSE;
998
999   gst_hls_demux_change_playlist (hlsdemux, bitrate, &changed);
1000   if (changed)
1001     gst_hls_demux_setup_streams (GST_ADAPTIVE_DEMUX_CAST (hlsdemux));
1002   return changed;
1003 }
1004
1005 static void
1006 gst_hls_demux_reset (GstAdaptiveDemux * ademux)
1007 {
1008   GstHLSDemux *demux = GST_HLS_DEMUX_CAST (ademux);
1009
1010   if (demux->master) {
1011     gst_hls_master_playlist_unref (demux->master);
1012     demux->master = NULL;
1013   }
1014   if (demux->current_variant != NULL) {
1015     gst_hls_variant_stream_unref (demux->current_variant);
1016     demux->current_variant = NULL;
1017   }
1018   demux->srcpad_counter = 0;
1019
1020   gst_hls_demux_clear_all_pending_data (demux);
1021 }
1022
1023 static gchar *
1024 gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf)
1025 {
1026   GstMapInfo info;
1027   gchar *playlist;
1028
1029   if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1030     goto map_error;
1031
1032   if (!g_utf8_validate ((gchar *) info.data, info.size, NULL))
1033     goto validate_error;
1034
1035   /* alloc size + 1 to end with a null character */
1036   playlist = g_malloc0 (info.size + 1);
1037   memcpy (playlist, info.data, info.size);
1038
1039   gst_buffer_unmap (buf, &info);
1040   return playlist;
1041
1042 validate_error:
1043   gst_buffer_unmap (buf, &info);
1044 map_error:
1045   return NULL;
1046 }
1047
1048 static gint
1049 gst_hls_demux_find_variant_match (const GstHLSVariantStream * a,
1050     const GstHLSVariantStream * b)
1051 {
1052   if (g_strcmp0 (a->name, b->name) == 0 &&
1053       a->bandwidth == b->bandwidth &&
1054       a->program_id == b->program_id &&
1055       g_strcmp0 (a->codecs, b->codecs) == 0 &&
1056       a->width == b->width &&
1057       a->height == b->height && a->iframe == b->iframe) {
1058     return 0;
1059   }
1060
1061   return 1;
1062 }
1063
1064 /* Update the master playlist, which contains the list of available
1065  * variants */
1066 static gboolean
1067 gst_hls_demux_update_variant_playlist (GstHLSDemux * hlsdemux, gchar * data,
1068     const gchar * uri, const gchar * base_uri)
1069 {
1070   GstHLSMasterPlaylist *new_master, *old;
1071   gboolean ret = FALSE;
1072   GList *l, *unmatched_lists;
1073   GstHLSVariantStream *new_variant;
1074
1075   new_master = gst_hls_master_playlist_new_from_data (data, base_uri ? base_uri : uri); // FIXME: check which uri to use here
1076
1077   if (new_master == NULL)
1078     return ret;
1079
1080   if (new_master->is_simple) {
1081     // FIXME: we should be able to support this though, in the unlikely
1082     // case that it changed?
1083     GST_ERROR
1084         ("Cannot update variant playlist: New playlist is not a variant playlist");
1085     gst_hls_master_playlist_unref (new_master);
1086     return FALSE;
1087   }
1088
1089   GST_M3U8_CLIENT_LOCK (self);
1090
1091   if (hlsdemux->master->is_simple) {
1092     GST_ERROR
1093         ("Cannot update variant playlist: Current playlist is not a variant playlist");
1094     goto out;
1095   }
1096
1097   /* Now see if the variant playlist still has the same lists */
1098   unmatched_lists = g_list_copy (hlsdemux->master->variants);
1099   for (l = new_master->variants; l != NULL; l = l->next) {
1100     GList *match = g_list_find_custom (unmatched_lists, l->data,
1101         (GCompareFunc) gst_hls_demux_find_variant_match);
1102
1103     if (match) {
1104       GstHLSVariantStream *variant = l->data;
1105       GstHLSVariantStream *old = match->data;
1106
1107       unmatched_lists = g_list_delete_link (unmatched_lists, match);
1108       /* FIXME: Deal with losing position due to missing an update */
1109       variant->m3u8->sequence_position = old->m3u8->sequence_position;
1110       variant->m3u8->sequence = old->m3u8->sequence;
1111     }
1112   }
1113
1114   if (unmatched_lists != NULL) {
1115     GST_WARNING ("Unable to match all playlists");
1116
1117     for (l = unmatched_lists; l != NULL; l = l->next) {
1118       if (l->data == hlsdemux->current_variant) {
1119         GST_WARNING ("Unable to match current playlist");
1120       }
1121     }
1122
1123     g_list_free (unmatched_lists);
1124   }
1125
1126   /* Switch out the variant playlist */
1127   old = hlsdemux->master;
1128
1129   // FIXME: check all this and also switch of variants, if anything needs updating
1130   hlsdemux->master = new_master;
1131
1132   if (hlsdemux->current_variant == NULL) {
1133     new_variant = new_master->default_variant;
1134   } else {
1135     /* Find the same variant in the new playlist */
1136     new_variant =
1137         gst_hls_master_playlist_get_matching_variant (new_master,
1138         hlsdemux->current_variant);
1139   }
1140
1141   /* Use the function to set the current variant, as it copies over data */
1142   if (new_variant != NULL)
1143     gst_hls_demux_set_current_variant (hlsdemux, new_variant);
1144
1145   gst_hls_master_playlist_unref (old);
1146
1147   ret = (hlsdemux->current_variant != NULL);
1148 out:
1149   GST_M3U8_CLIENT_UNLOCK (self);
1150
1151   return ret;
1152 }
1153
1154 static gboolean
1155 gst_hls_demux_update_rendition_manifest (GstHLSDemux * demux,
1156     GstHLSMedia * media, GError ** err)
1157 {
1158   GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
1159   GstFragment *download;
1160   GstBuffer *buf;
1161   gchar *playlist;
1162   const gchar *main_uri;
1163   GstM3U8 *m3u8;
1164   gchar *uri = media->uri;
1165
1166   main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1167   download =
1168       gst_uri_downloader_fetch_uri (adaptive_demux->downloader, uri, main_uri,
1169       TRUE, TRUE, TRUE, err);
1170
1171   if (download == NULL)
1172     return FALSE;
1173
1174   m3u8 = media->playlist;
1175
1176   /* Set the base URI of the playlist to the redirect target if any */
1177   if (download->redirect_permanent && download->redirect_uri) {
1178     gst_m3u8_set_uri (m3u8, download->redirect_uri, NULL, media->name);
1179   } else {
1180     gst_m3u8_set_uri (m3u8, download->uri, download->redirect_uri, media->name);
1181   }
1182
1183   buf = gst_fragment_get_buffer (download);
1184   playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1185   gst_buffer_unref (buf);
1186   g_object_unref (download);
1187
1188   if (playlist == NULL) {
1189     GST_WARNING_OBJECT (demux, "Couldn't validate playlist encoding");
1190     g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1191         "Couldn't validate playlist encoding");
1192     return FALSE;
1193   }
1194
1195   if (!gst_m3u8_update (m3u8, playlist)) {
1196     GST_WARNING_OBJECT (demux, "Couldn't update playlist");
1197     g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
1198         "Couldn't update playlist");
1199     return FALSE;
1200   }
1201
1202   return TRUE;
1203 }
1204
1205 static gboolean
1206 gst_hls_demux_update_playlist (GstHLSDemux * demux, gboolean update,
1207     GError ** err)
1208 {
1209   GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
1210   GstFragment *download;
1211   GstBuffer *buf;
1212   gchar *playlist;
1213   gboolean main_checked = FALSE;
1214   const gchar *main_uri;
1215   GstM3U8 *m3u8;
1216   gchar *uri;
1217   gint i;
1218
1219 retry:
1220   uri = gst_m3u8_get_uri (demux->current_variant->m3u8);
1221   main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1222   download =
1223       gst_uri_downloader_fetch_uri (adaptive_demux->downloader, uri, main_uri,
1224       TRUE, TRUE, TRUE, err);
1225   if (download == NULL) {
1226     gchar *base_uri;
1227
1228     if (!update || main_checked || demux->master->is_simple) {
1229       g_free (uri);
1230       return FALSE;
1231     }
1232     g_clear_error (err);
1233     GST_INFO_OBJECT (demux,
1234         "Updating playlist %s failed, attempt to refresh variant playlist %s",
1235         uri, main_uri);
1236     download =
1237         gst_uri_downloader_fetch_uri (adaptive_demux->downloader,
1238         main_uri, NULL, TRUE, TRUE, TRUE, err);
1239     if (download == NULL) {
1240       g_free (uri);
1241       return FALSE;
1242     }
1243
1244     buf = gst_fragment_get_buffer (download);
1245     playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1246     gst_buffer_unref (buf);
1247
1248     if (playlist == NULL) {
1249       GST_WARNING_OBJECT (demux,
1250           "Failed to validate variant playlist encoding");
1251       g_free (uri);
1252       g_object_unref (download);
1253       g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1254           "Couldn't validate playlist encoding");
1255       return FALSE;
1256     }
1257
1258     g_free (uri);
1259     if (download->redirect_permanent && download->redirect_uri) {
1260       uri = download->redirect_uri;
1261       base_uri = NULL;
1262     } else {
1263       uri = download->uri;
1264       base_uri = download->redirect_uri;
1265     }
1266
1267     if (!gst_hls_demux_update_variant_playlist (demux, playlist, uri, base_uri)) {
1268       GST_WARNING_OBJECT (demux, "Failed to update the variant playlist");
1269       g_object_unref (download);
1270       g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
1271           "Couldn't update playlist");
1272       return FALSE;
1273     }
1274
1275     g_object_unref (download);
1276
1277     main_checked = TRUE;
1278     goto retry;
1279   }
1280   g_free (uri);
1281
1282   m3u8 = demux->current_variant->m3u8;
1283
1284   /* Set the base URI of the playlist to the redirect target if any */
1285   if (download->redirect_permanent && download->redirect_uri) {
1286     gst_m3u8_set_uri (m3u8, download->redirect_uri, NULL,
1287         demux->current_variant->name);
1288   } else {
1289     gst_m3u8_set_uri (m3u8, download->uri, download->redirect_uri,
1290         demux->current_variant->name);
1291   }
1292
1293   buf = gst_fragment_get_buffer (download);
1294   playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1295   gst_buffer_unref (buf);
1296   g_object_unref (download);
1297
1298   if (playlist == NULL) {
1299     GST_WARNING_OBJECT (demux, "Couldn't validate playlist encoding");
1300     g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1301         "Couldn't validate playlist encoding");
1302     return FALSE;
1303   }
1304
1305   if (!gst_m3u8_update (m3u8, playlist)) {
1306     GST_WARNING_OBJECT (demux, "Couldn't update playlist");
1307     g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
1308         "Couldn't update playlist");
1309     return FALSE;
1310   }
1311
1312   for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
1313     GList *mlist = demux->current_variant->media[i];
1314
1315     while (mlist != NULL) {
1316       GstHLSMedia *media = mlist->data;
1317
1318       if (media->uri == NULL) {
1319         /* No uri means this is a placeholder for a stream
1320          * contained in another mux */
1321         mlist = mlist->next;
1322         continue;
1323       }
1324       GST_LOG_OBJECT (demux,
1325           "Updating playlist for media of type %d - %s, uri: %s", i,
1326           media->name, media->uri);
1327
1328       if (!gst_hls_demux_update_rendition_manifest (demux, media, err))
1329         return FALSE;
1330
1331       mlist = mlist->next;
1332     }
1333   }
1334
1335   /* If it's a live source, do not let the sequence number go beyond
1336    * three fragments before the end of the list */
1337   if (update == FALSE && gst_m3u8_is_live (m3u8)) {
1338     gint64 last_sequence, first_sequence;
1339
1340     GST_M3U8_CLIENT_LOCK (demux->client);
1341     last_sequence =
1342         GST_M3U8_MEDIA_FILE (g_list_last (m3u8->files)->data)->sequence;
1343     first_sequence =
1344         GST_M3U8_MEDIA_FILE (g_list_first (m3u8->files)->data)->sequence;
1345
1346     GST_DEBUG_OBJECT (demux,
1347         "sequence:%" G_GINT64_FORMAT " , first_sequence:%" G_GINT64_FORMAT
1348         " , last_sequence:%" G_GINT64_FORMAT, m3u8->sequence,
1349         first_sequence, last_sequence);
1350     if (m3u8->sequence >= last_sequence - 3) {
1351       //demux->need_segment = TRUE;
1352       /* Make sure we never go below the minimum sequence number */
1353       m3u8->sequence = MAX (first_sequence, last_sequence - 3);
1354       GST_DEBUG_OBJECT (demux,
1355           "Sequence is beyond playlist. Moving back to %" G_GINT64_FORMAT,
1356           m3u8->sequence);
1357     }
1358     GST_M3U8_CLIENT_UNLOCK (demux->client);
1359   } else if (!gst_m3u8_is_live (m3u8)) {
1360     GstClockTime current_pos, target_pos;
1361     guint sequence = 0;
1362     GList *walk;
1363
1364     /* Sequence numbers are not guaranteed to be the same in different
1365      * playlists, so get the correct fragment here based on the current
1366      * position
1367      */
1368     GST_M3U8_CLIENT_LOCK (demux->client);
1369
1370     /* Valid because hlsdemux only has a single output */
1371     if (GST_ADAPTIVE_DEMUX_CAST (demux)->streams) {
1372       GstAdaptiveDemuxStream *stream =
1373           GST_ADAPTIVE_DEMUX_CAST (demux)->streams->data;
1374       target_pos = stream->segment.position;
1375     } else {
1376       target_pos = 0;
1377     }
1378     if (GST_CLOCK_TIME_IS_VALID (m3u8->sequence_position)) {
1379       target_pos = MAX (target_pos, m3u8->sequence_position);
1380     }
1381
1382     GST_LOG_OBJECT (demux, "Looking for sequence position %"
1383         GST_TIME_FORMAT " in updated playlist", GST_TIME_ARGS (target_pos));
1384
1385     current_pos = 0;
1386     for (walk = m3u8->files; walk; walk = walk->next) {
1387       GstM3U8MediaFile *file = walk->data;
1388
1389       sequence = file->sequence;
1390       if (current_pos <= target_pos
1391           && target_pos < current_pos + file->duration) {
1392         break;
1393       }
1394       current_pos += file->duration;
1395     }
1396     /* End of playlist */
1397     if (!walk)
1398       sequence++;
1399     m3u8->sequence = sequence;
1400     m3u8->sequence_position = current_pos;
1401     GST_M3U8_CLIENT_UNLOCK (demux->client);
1402   }
1403
1404   return TRUE;
1405 }
1406
1407 static gboolean
1408 gst_hls_demux_change_playlist (GstHLSDemux * demux, guint max_bitrate,
1409     gboolean * changed)
1410 {
1411   GstHLSVariantStream *lowest_variant, *lowest_ivariant;
1412   GstHLSVariantStream *previous_variant, *new_variant;
1413   gint old_bandwidth, new_bandwidth;
1414   GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX_CAST (demux);
1415   GstAdaptiveDemuxStream *stream;
1416
1417   g_return_val_if_fail (adaptive_demux->streams != NULL, FALSE);
1418
1419   stream = adaptive_demux->streams->data;
1420
1421   previous_variant = demux->current_variant;
1422   new_variant =
1423       gst_hls_master_playlist_get_variant_for_bitrate (demux->master,
1424       demux->current_variant, max_bitrate);
1425
1426   GST_M3U8_CLIENT_LOCK (demux->client);
1427
1428 retry_failover_protection:
1429   old_bandwidth = previous_variant->bandwidth;
1430   new_bandwidth = new_variant->bandwidth;
1431
1432   /* Don't do anything else if the playlist is the same */
1433   if (new_bandwidth == old_bandwidth) {
1434     GST_M3U8_CLIENT_UNLOCK (demux->client);
1435     return TRUE;
1436   }
1437
1438   GST_M3U8_CLIENT_UNLOCK (demux->client);
1439
1440   gst_hls_demux_set_current_variant (demux, new_variant);
1441
1442   GST_INFO_OBJECT (demux, "Client was on %dbps, max allowed is %dbps, switching"
1443       " to bitrate %dbps", old_bandwidth, max_bitrate, new_bandwidth);
1444
1445   if (gst_hls_demux_update_playlist (demux, TRUE, NULL)) {
1446     const gchar *main_uri;
1447     gchar *uri;
1448
1449     uri = gst_m3u8_get_uri (new_variant->m3u8);
1450     main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1451     gst_element_post_message (GST_ELEMENT_CAST (demux),
1452         gst_message_new_element (GST_OBJECT_CAST (demux),
1453             gst_structure_new (GST_ADAPTIVE_DEMUX_STATISTICS_MESSAGE_NAME,
1454                 "manifest-uri", G_TYPE_STRING,
1455                 main_uri, "uri", G_TYPE_STRING,
1456                 uri, "bitrate", G_TYPE_INT, new_bandwidth, NULL)));
1457     g_free (uri);
1458     if (changed)
1459       *changed = TRUE;
1460     stream->discont = TRUE;
1461   } else {
1462     GstHLSVariantStream *failover_variant = NULL;
1463     GList *failover;
1464
1465     GST_INFO_OBJECT (demux, "Unable to update playlist. Switching back");
1466     GST_M3U8_CLIENT_LOCK (demux->client);
1467
1468     /* we find variants by bitrate by going from highest to lowest, so it's
1469      * possible that there's another variant with the same bitrate before the
1470      * one selected which we can use as failover */
1471     failover = g_list_find (demux->master->variants, new_variant);
1472     if (failover != NULL)
1473       failover = failover->prev;
1474     if (failover != NULL)
1475       failover_variant = failover->data;
1476     if (failover_variant && new_bandwidth == failover_variant->bandwidth) {
1477       new_variant = failover_variant;
1478       goto retry_failover_protection;
1479     }
1480
1481     GST_M3U8_CLIENT_UNLOCK (demux->client);
1482     gst_hls_demux_set_current_variant (demux, previous_variant);
1483     /*  Try a lower bitrate (or stop if we just tried the lowest) */
1484     lowest_variant = demux->master->variants->data;
1485     lowest_ivariant = demux->master->iframe_variants->data;
1486     if (previous_variant->iframe && new_bandwidth == lowest_ivariant->bandwidth)
1487       return FALSE;
1488     if (!previous_variant->iframe && new_bandwidth == lowest_variant->bandwidth)
1489       return FALSE;
1490     else
1491       return gst_hls_demux_change_playlist (demux, new_bandwidth - 1, changed);
1492   }
1493
1494   return TRUE;
1495 }
1496
1497 #if defined(HAVE_OPENSSL)
1498 static gboolean
1499 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1500     const guint8 * key_data, const guint8 * iv_data)
1501 {
1502   EVP_CIPHER_CTX_init (&stream->aes_ctx);
1503   if (!EVP_DecryptInit_ex (&stream->aes_ctx, EVP_aes_128_cbc (), NULL, key_data,
1504           iv_data))
1505     return FALSE;
1506   EVP_CIPHER_CTX_set_padding (&stream->aes_ctx, 0);
1507   return TRUE;
1508 }
1509
1510 static gboolean
1511 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1512     const guint8 * encrypted_data, guint8 * decrypted_data)
1513 {
1514   int len, flen = 0;
1515
1516   if (G_UNLIKELY (length > G_MAXINT || length % 16 != 0))
1517     return FALSE;
1518
1519   len = (int) length;
1520   if (!EVP_DecryptUpdate (&stream->aes_ctx, decrypted_data, &len,
1521           encrypted_data, len))
1522     return FALSE;
1523   EVP_DecryptFinal_ex (&stream->aes_ctx, decrypted_data + len, &flen);
1524   g_return_val_if_fail (len + flen == length, FALSE);
1525   return TRUE;
1526 }
1527
1528 static void
1529 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1530 {
1531   EVP_CIPHER_CTX_cleanup (&stream->aes_ctx);
1532 }
1533
1534 #elif defined(HAVE_NETTLE)
1535 static gboolean
1536 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1537     const guint8 * key_data, const guint8 * iv_data)
1538 {
1539   aes_set_decrypt_key (&stream->aes_ctx.ctx, 16, key_data);
1540   CBC_SET_IV (&stream->aes_ctx, iv_data);
1541
1542   return TRUE;
1543 }
1544
1545 static gboolean
1546 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1547     const guint8 * encrypted_data, guint8 * decrypted_data)
1548 {
1549   if (length % 16 != 0)
1550     return FALSE;
1551
1552   CBC_DECRYPT (&stream->aes_ctx, aes_decrypt, length, decrypted_data,
1553       encrypted_data);
1554
1555   return TRUE;
1556 }
1557
1558 static void
1559 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1560 {
1561   /* NOP */
1562 }
1563
1564 #else
1565 static gboolean
1566 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1567     const guint8 * key_data, const guint8 * iv_data)
1568 {
1569   gcry_error_t err = 0;
1570   gboolean ret = FALSE;
1571
1572   err =
1573       gcry_cipher_open (&stream->aes_ctx, GCRY_CIPHER_AES128,
1574       GCRY_CIPHER_MODE_CBC, 0);
1575   if (err)
1576     goto out;
1577   err = gcry_cipher_setkey (stream->aes_ctx, key_data, 16);
1578   if (err)
1579     goto out;
1580   err = gcry_cipher_setiv (stream->aes_ctx, iv_data, 16);
1581   if (!err)
1582     ret = TRUE;
1583
1584 out:
1585   if (!ret)
1586     if (stream->aes_ctx)
1587       gcry_cipher_close (stream->aes_ctx);
1588
1589   return ret;
1590 }
1591
1592 static gboolean
1593 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1594     const guint8 * encrypted_data, guint8 * decrypted_data)
1595 {
1596   gcry_error_t err = 0;
1597
1598   err = gcry_cipher_decrypt (stream->aes_ctx, decrypted_data, length,
1599       encrypted_data, length);
1600
1601   return err == 0;
1602 }
1603
1604 static void
1605 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1606 {
1607   if (stream->aes_ctx) {
1608     gcry_cipher_close (stream->aes_ctx);
1609     stream->aes_ctx = NULL;
1610   }
1611 }
1612 #endif
1613
1614 static GstBuffer *
1615 gst_hls_demux_decrypt_fragment (GstHLSDemux * demux, GstHLSDemuxStream * stream,
1616     GstBuffer * encrypted_buffer, GError ** err)
1617 {
1618   GstBuffer *decrypted_buffer = NULL;
1619   GstMapInfo encrypted_info, decrypted_info;
1620
1621   decrypted_buffer =
1622       gst_buffer_new_allocate (NULL, gst_buffer_get_size (encrypted_buffer),
1623       NULL);
1624
1625   gst_buffer_map (encrypted_buffer, &encrypted_info, GST_MAP_READ);
1626   gst_buffer_map (decrypted_buffer, &decrypted_info, GST_MAP_WRITE);
1627
1628   if (!decrypt_fragment (stream, encrypted_info.size,
1629           encrypted_info.data, decrypted_info.data))
1630     goto decrypt_error;
1631
1632
1633   gst_buffer_unmap (decrypted_buffer, &decrypted_info);
1634   gst_buffer_unmap (encrypted_buffer, &encrypted_info);
1635
1636   gst_buffer_unref (encrypted_buffer);
1637
1638   return decrypted_buffer;
1639
1640 decrypt_error:
1641   GST_ERROR_OBJECT (demux, "Failed to decrypt fragment");
1642   g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_DECRYPT,
1643       "Failed to decrypt fragment");
1644
1645   gst_buffer_unmap (decrypted_buffer, &decrypted_info);
1646   gst_buffer_unmap (encrypted_buffer, &encrypted_info);
1647
1648   gst_buffer_unref (encrypted_buffer);
1649   gst_buffer_unref (decrypted_buffer);
1650
1651   return NULL;
1652 }
1653
1654 static gint64
1655 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
1656 {
1657   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1658   GstClockTime target_duration;
1659
1660   if (hlsdemux->current_variant) {
1661     target_duration =
1662         gst_m3u8_get_target_duration (hlsdemux->current_variant->m3u8);
1663   } else {
1664     target_duration = 5 * GST_SECOND;
1665   }
1666
1667   return gst_util_uint64_scale (target_duration, G_USEC_PER_SEC, GST_SECOND);
1668 }
1669
1670 static gboolean
1671 gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
1672     gint64 * stop)
1673 {
1674   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1675   gboolean ret = FALSE;
1676
1677   if (hlsdemux->current_variant) {
1678     ret =
1679         gst_m3u8_get_seek_range (hlsdemux->current_variant->m3u8, start, stop);
1680   }
1681
1682   return ret;
1683 }