2b103428a5fe5398cad905714fdf28d722a6fedc
[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  * @title: hlsdemux
30  *
31  * HTTP Live Streaming demuxer element.
32  *
33  * ## Example launch line
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  *
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 (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 /* 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);
81 static gboolean
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);
85
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 *
89     demux);
90 static gboolean gst_hls_demux_process_manifest (GstAdaptiveDemux * demux,
91     GstBuffer * buf);
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);
97 static gboolean
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 *
106     stream);
107 static GstFlowReturn gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream *
108     stream);
109 static GstFlowReturn gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream
110     * stream);
111 static gboolean gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream,
112     guint64 bitrate);
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);
119
120 #define gst_hls_demux_parent_class parent_class
121 G_DEFINE_TYPE (GstHLSDemux, gst_hls_demux, GST_TYPE_ADAPTIVE_DEMUX);
122
123 static void
124 gst_hls_demux_finalize (GObject * obj)
125 {
126   GstHLSDemux *demux = GST_HLS_DEMUX (obj);
127
128   gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
129   g_mutex_clear (&demux->keys_lock);
130   if (demux->keys) {
131     g_hash_table_unref (demux->keys);
132     demux->keys = NULL;
133   }
134
135   G_OBJECT_CLASS (parent_class)->finalize (obj);
136 }
137
138 static void
139 gst_hls_demux_class_init (GstHLSDemuxClass * klass)
140 {
141   GObjectClass *gobject_class;
142   GstElementClass *element_class;
143   GstAdaptiveDemuxClass *adaptivedemux_class;
144
145   gobject_class = (GObjectClass *) klass;
146   element_class = (GstElementClass *) klass;
147   adaptivedemux_class = (GstAdaptiveDemuxClass *) klass;
148
149   gobject_class->finalize = gst_hls_demux_finalize;
150
151   element_class->change_state = GST_DEBUG_FUNCPTR (gst_hls_demux_change_state);
152
153   gst_element_class_add_static_pad_template (element_class, &srctemplate);
154   gst_element_class_add_static_pad_template (element_class, &sinktemplate);
155
156   gst_element_class_set_static_metadata (element_class,
157       "HLS Demuxer",
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>");
162
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;
180
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;
184
185   GST_DEBUG_CATEGORY_INIT (gst_hls_demux_debug, "hlsdemux", 0,
186       "hlsdemux element");
187 }
188
189 static void
190 gst_hls_demux_init (GstHLSDemux * demux)
191 {
192   gst_adaptive_demux_set_stream_struct_size (GST_ADAPTIVE_DEMUX_CAST (demux),
193       sizeof (GstHLSDemuxStream));
194
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);
197 }
198
199 static GstStateChangeReturn
200 gst_hls_demux_change_state (GstElement * element, GstStateChange transition)
201 {
202   GstStateChangeReturn ret;
203   GstHLSDemux *demux = GST_HLS_DEMUX (element);
204
205   switch (transition) {
206     case GST_STATE_CHANGE_READY_TO_PAUSED:
207       gst_hls_demux_reset (GST_ADAPTIVE_DEMUX_CAST (demux));
208       break;
209     default:
210       break;
211   }
212
213   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
214
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);
219       break;
220     default:
221       break;
222   }
223   return ret;
224 }
225
226 static GstPad *
227 gst_hls_demux_create_pad (GstHLSDemux * hlsdemux)
228 {
229   gchar *name;
230   GstPad *pad;
231
232   name = g_strdup_printf ("src_%u", hlsdemux->srcpad_counter++);
233   pad = gst_pad_new_from_static_template (&srctemplate, name);
234   g_free (name);
235
236   return pad;
237 }
238
239 static guint64
240 gst_hls_demux_get_bitrate (GstHLSDemux * hlsdemux)
241 {
242   GstAdaptiveDemux *demux = GST_ADAPTIVE_DEMUX_CAST (hlsdemux);
243
244   /* FIXME !!!
245    *
246    * No, there isn't a single output :D */
247
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;
252   }
253
254   return 0;
255 }
256
257 static void
258 gst_hls_demux_stream_clear_pending_data (GstHLSDemuxStream * hls_stream)
259 {
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);
267 }
268
269 static void
270 gst_hls_demux_clear_all_pending_data (GstHLSDemux * hlsdemux)
271 {
272   GstAdaptiveDemux *demux = (GstAdaptiveDemux *) hlsdemux;
273   GList *walk;
274
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);
278   }
279 }
280
281 #if 0
282 static void
283 gst_hls_demux_set_current (GstHLSDemux * self, GstM3U8 * m3u8)
284 {
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;
290
291 #if 0
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;
300 #endif
301   }
302   GST_M3U8_CLIENT_UNLOCK (self);
303 }
304 #endif
305
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))
309
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))
315
316 static gboolean
317 gst_hls_demux_seek (GstAdaptiveDemux * demux, GstEvent * seek)
318 {
319   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
320   GstFormat format;
321   GstSeekFlags flags;
322   GstSeekType start_type, stop_type;
323   gint64 start, stop;
324   gdouble rate, old_rate;
325   GList *walk;
326   GstClockTime current_pos, target_pos, final_pos;
327   guint64 bitrate;
328
329   gst_event_parse_seek (seek, &rate, &format, &flags, &start_type, &start,
330       &stop_type, &stop);
331
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 */
334     return TRUE;
335   }
336
337   old_rate = demux->segment.rate;
338
339   bitrate = gst_hls_demux_get_bitrate (hlsdemux);
340
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) {
344     GError *err = NULL;
345
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);
352       return FALSE;
353     }
354     //hlsdemux->discont = TRUE;
355
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)) {
358     GError *err = NULL;
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);
365       return FALSE;
366     }
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);
370   }
371
372   target_pos = rate < 0 ? stop : start;
373   final_pos = target_pos;
374
375   /* properly cleanup pending decryption status */
376   if (flags & GST_SEEK_FLAG_FLUSH) {
377     gst_hls_demux_clear_all_pending_data (hlsdemux);
378   }
379
380   for (walk = demux->streams; walk; walk = g_list_next (walk)) {
381     GstAdaptiveDemuxStream *stream =
382         GST_ADAPTIVE_DEMUX_STREAM_CAST (walk->data);
383
384     gst_hls_demux_stream_seek (stream, rate >= 0, flags, target_pos,
385         &current_pos);
386
387     /* FIXME: use minimum position always ? */
388     if (final_pos > current_pos)
389       final_pos = current_pos;
390   }
391
392   if (IS_SNAP_SEEK (flags)) {
393     if (rate >= 0)
394       gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
395           final_pos, stop_type, stop, NULL);
396     else
397       gst_segment_do_seek (&demux->segment, rate, format, flags, start_type,
398           start, stop_type, final_pos, NULL);
399   }
400
401   return TRUE;
402 }
403
404 static GstFlowReturn
405 gst_hls_demux_stream_seek (GstAdaptiveDemuxStream * stream, gboolean forward,
406     GstSeekFlags flags, GstClockTime ts, GstClockTime * final_ts)
407 {
408   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
409   GList *walk;
410   GstClockTime current_pos;
411   gint64 current_sequence;
412   gboolean snap_after, snap_nearest;
413   GstM3U8MediaFile *file = NULL;
414
415   current_sequence = 0;
416   current_pos = gst_m3u8_is_live (hls_stream->playlist) ?
417       hls_stream->playlist->first_file_start : 0;
418
419   /* Snap to segment boundary. Improves seek performance on slow machines. */
420   snap_nearest =
421       (flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST;
422   snap_after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
423
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) {
427     file = walk->data;
428
429     current_sequence = file->sequence;
430     if ((forward && snap_after) || snap_nearest) {
431       if (current_pos >= ts)
432         break;
433       if (snap_nearest && ts - current_pos < file->duration / 2)
434         break;
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;
439
440       if (next_pos <= ts && ts < next_pos + file->duration) {
441         break;
442       }
443     } else if (current_pos <= ts && ts < current_pos + file->duration) {
444       break;
445     }
446     current_pos += file->duration;
447   }
448
449   if (walk == NULL) {
450     GST_DEBUG_OBJECT (stream->pad, "seeking further than track duration");
451     current_sequence++;
452   }
453
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);
461
462   /* Play from the end of the current selected segment */
463   if (file) {
464     if (!forward && IS_SNAP_SEEK (flags))
465       current_pos += file->duration;
466   }
467
468   /* update stream's segment position */
469   stream->segment.position = current_pos;
470
471   if (final_ts)
472     *final_ts = current_pos;
473
474   return GST_FLOW_OK;
475 }
476
477 static GstFlowReturn
478 gst_hls_demux_update_manifest (GstAdaptiveDemux * demux)
479 {
480   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
481   if (!gst_hls_demux_update_playlist (hlsdemux, TRUE, NULL))
482     return GST_FLOW_ERROR;
483
484   return GST_FLOW_OK;
485 }
486
487 static void
488 create_stream_for_playlist (GstAdaptiveDemux * demux, GstM3U8 * playlist,
489     gboolean is_primary_playlist, gboolean selected)
490 {
491   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
492   GstHLSDemuxStream *hlsdemux_stream;
493   GstAdaptiveDemuxStream *stream;
494
495   if (!selected) {
496     /* FIXME: Later, create the stream but mark not-selected */
497     GST_LOG_OBJECT (demux, "Ignoring not-selected stream");
498     return;
499   }
500
501   GST_DEBUG_OBJECT (demux,
502       "is_primary_playlist:%d selected:%d playlist name '%s'",
503       is_primary_playlist, selected, playlist->name);
504
505   stream = gst_adaptive_demux_stream_new (demux,
506       gst_hls_demux_create_pad (hlsdemux));
507
508   hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
509
510   hlsdemux_stream->stream_type = GST_HLS_TSREADER_NONE;
511
512   hlsdemux_stream->playlist = gst_m3u8_ref (playlist);
513   hlsdemux_stream->is_primary_playlist = is_primary_playlist;
514
515   hlsdemux_stream->do_typefind = TRUE;
516   hlsdemux_stream->reset_pts = TRUE;
517 }
518
519 static GstHLSDemuxStream *
520 find_adaptive_stream_for_playlist (GstAdaptiveDemux * demux, GstM3U8 * playlist)
521 {
522   GList *tmp;
523
524   GST_DEBUG_OBJECT (demux, "Looking for existing stream for '%s' %s",
525       playlist->name, playlist->uri);
526
527   for (tmp = demux->streams; tmp; tmp = tmp->next) {
528     GstHLSDemuxStream *hlsstream = (GstHLSDemuxStream *) tmp->data;
529     if (hlsstream->playlist == playlist)
530       return hlsstream;
531   }
532
533   return NULL;
534 }
535
536 /* Returns TRUE if the previous and current (to switch to) variant are compatible.
537  *
538  * That is:
539  * * They have the same number of streams
540  * * The streams are of the same type
541  */
542 static gboolean
543 new_variant_is_compatible (GstAdaptiveDemux * demux)
544 {
545   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
546   GstHLSVariantStream *previous = hlsdemux->previous_variant;
547   GstHLSVariantStream *current = hlsdemux->current_variant;
548   gint i;
549
550   GST_DEBUG_OBJECT (demux,
551       "Checking whether new variant is compatible with previous");
552
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));
558       return FALSE;
559     }
560
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));
568         return FALSE;
569       }
570       mlist = mlist->next;
571     }
572
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));
581         return FALSE;
582       }
583       mlist = mlist->next;
584     }
585   }
586
587   GST_DEBUG_OBJECT (demux, "Variants are compatible");
588
589   return TRUE;
590 }
591
592 static gboolean
593 gst_hls_demux_setup_streams (GstAdaptiveDemux * demux)
594 {
595   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
596   GstHLSVariantStream *playlist = hlsdemux->current_variant;
597   gint i;
598
599   if (playlist == NULL) {
600     GST_WARNING_OBJECT (demux, "Can't configure streams - no variant selected");
601     return FALSE;
602   }
603
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");
609
610     /* Carry over the main playlist */
611     hlsstream =
612         find_adaptive_stream_for_playlist (demux,
613         hlsdemux->previous_variant->m3u8);
614     if (G_UNLIKELY (hlsstream == NULL))
615       goto no_match_error;
616
617     gst_m3u8_unref (hlsstream->playlist);
618     hlsstream->playlist = gst_m3u8_ref (playlist->m3u8);
619
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,
626             media);
627
628         if (G_UNLIKELY (old_media == NULL)) {
629           GST_FIXME_OBJECT (demux, "Handle new stream !");
630           goto no_match_error;
631         }
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) {
636           hlsstream =
637               find_adaptive_stream_for_playlist (demux, old_media->playlist);
638           if (!hlsstream)
639             goto no_match_error;
640
641           GST_DEBUG_OBJECT (demux, "Found matching stream");
642           gst_m3u8_unref (hlsstream->playlist);
643           hlsstream->playlist = gst_m3u8_ref (media->playlist);
644         } else {
645           GST_DEBUG_OBJECT (demux, "Skipping stream of type %s",
646               gst_hls_media_type_get_name (media->mtype));
647         }
648
649         mlist = mlist->next;
650       }
651     }
652
653     return TRUE;
654   }
655
656   /* FIXME : This seems wrong and assumes there's only one stream :( */
657   gst_hls_demux_clear_all_pending_data (hlsdemux);
658
659   /* 1 output for the main playlist */
660   create_stream_for_playlist (demux, playlist->m3u8, TRUE, TRUE);
661
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;
666
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));
672         mlist = mlist->next;
673         continue;
674       }
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));
680
681       mlist = mlist->next;
682     }
683   }
684
685   return TRUE;
686
687 no_match_error:
688   {
689     /* POST ERROR MESSAGE */
690     GST_ERROR_OBJECT (demux, "Should not happen ! Could not find old stream");
691     return FALSE;
692   }
693 }
694
695 static const gchar *
696 gst_adaptive_demux_get_manifest_ref_uri (GstAdaptiveDemux * d)
697 {
698   return d->manifest_base_uri ? d->manifest_base_uri : d->manifest_uri;
699 }
700
701 static void
702 gst_hls_demux_set_current_variant (GstHLSDemux * hlsdemux,
703     GstHLSVariantStream * variant)
704 {
705   if (hlsdemux->current_variant == variant || variant == NULL)
706     return;
707
708   if (hlsdemux->current_variant != NULL) {
709     gint i;
710
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;
717
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));
722
723     for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
724       GList *mlist = hlsdemux->current_variant->media[i];
725
726       while (mlist != NULL) {
727         GstHLSMedia *old_media = mlist->data;
728         GstHLSMedia *new_media =
729             gst_hls_variant_find_matching_media (variant, old_media);
730
731         if (new_media) {
732           GST_LOG_OBJECT (hlsdemux, "Found matching GstHLSMedia");
733           GST_LOG_OBJECT (hlsdemux, "old_media '%s' '%s'", old_media->name,
734               old_media->uri);
735           GST_LOG_OBJECT (hlsdemux, "new_media '%s' '%s'", new_media->name,
736               new_media->uri);
737           new_media->playlist->sequence = old_media->playlist->sequence;
738           new_media->playlist->sequence_position =
739               old_media->playlist->sequence_position;
740         } else {
741           GST_LOG_OBJECT (hlsdemux,
742               "Didn't find a matching variant for '%s' '%s'", old_media->name,
743               old_media->uri);
744         }
745         mlist = mlist->next;
746       }
747     }
748
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;
753   }
754
755   hlsdemux->current_variant = gst_hls_variant_stream_ref (variant);
756
757 }
758
759 static gboolean
760 gst_hls_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
761 {
762   GstHLSVariantStream *variant;
763   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
764   gchar *playlist = NULL;
765
766   GST_INFO_OBJECT (demux, "Initial playlist location: %s (base uri: %s)",
767       demux->manifest_uri, demux->manifest_base_uri);
768
769   playlist = gst_hls_src_buf_to_utf8_playlist (buf);
770   if (playlist == NULL) {
771     GST_WARNING_OBJECT (demux, "Error validating initial playlist");
772     return FALSE;
773   }
774
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));
778
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
782      * the playlist */
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);
786     return FALSE;
787   }
788
789   /* select the initial variant stream */
790   if (demux->connection_speed == 0) {
791     variant = hlsdemux->master->default_variant;
792   } else {
793     variant =
794         gst_hls_master_playlist_get_variant_for_bitrate (hlsdemux->master,
795         NULL, demux->connection_speed);
796   }
797
798   if (variant) {
799     GST_INFO_OBJECT (hlsdemux, "selected %s", variant->name);
800     gst_hls_demux_set_current_variant (hlsdemux, variant);      // FIXME: inline?
801   }
802
803   /* get the selected media playlist (unless the initial list was one already) */
804   if (!hlsdemux->master->is_simple) {
805     GError *err = NULL;
806
807     if (!gst_hls_demux_update_playlist (hlsdemux, FALSE, &err)) {
808       GST_ELEMENT_ERROR_FROM_ERROR (demux, "Could not fetch media playlist",
809           err);
810       GST_M3U8_CLIENT_UNLOCK (self);
811       return FALSE;
812     }
813   }
814   GST_M3U8_CLIENT_UNLOCK (self);
815
816   return gst_hls_demux_setup_streams (demux);
817 }
818
819 static GstClockTime
820 gst_hls_demux_get_duration (GstAdaptiveDemux * demux)
821 {
822   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
823   GstClockTime duration = GST_CLOCK_TIME_NONE;
824
825   if (hlsdemux->current_variant != NULL)
826     duration = gst_m3u8_get_duration (hlsdemux->current_variant->m3u8);
827
828   return duration;
829 }
830
831 static gboolean
832 gst_hls_demux_is_live (GstAdaptiveDemux * demux)
833 {
834   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
835   gboolean is_live = FALSE;
836
837   if (hlsdemux->current_variant)
838     is_live = gst_hls_variant_stream_is_live (hlsdemux->current_variant);
839
840   return is_live;
841 }
842
843 static const GstHLSKey *
844 gst_hls_demux_get_key (GstHLSDemux * demux, const gchar * key_url,
845     const gchar * referer, gboolean allow_cache)
846 {
847   GstFragment *key_fragment;
848   GstBuffer *key_buffer;
849   GstHLSKey *key;
850   GError *err = NULL;
851
852   GST_LOG_OBJECT (demux, "Looking up key for key url %s", key_url);
853
854   g_mutex_lock (&demux->keys_lock);
855
856   key = g_hash_table_lookup (demux->keys, key_url);
857
858   if (key != NULL) {
859     GST_LOG_OBJECT (demux, "Found key for key url %s in key cache", key_url);
860     goto out;
861   }
862
863   GST_INFO_OBJECT (demux, "Fetching key %s", key_url);
864
865   key_fragment =
866       gst_uri_downloader_fetch_uri (GST_ADAPTIVE_DEMUX (demux)->downloader,
867       key_url, referer, FALSE, FALSE, allow_cache, &err);
868
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);
873     goto out;
874   }
875
876   key_buffer = gst_fragment_get_buffer (key_fragment);
877
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!");
881
882   g_hash_table_insert (demux->keys, g_strdup (key_url), key);
883
884   gst_buffer_unref (key_buffer);
885   g_object_unref (key_fragment);
886
887 out:
888
889   g_mutex_unlock (&demux->keys_lock);
890
891   if (key != NULL)
892     GST_MEMDUMP_OBJECT (demux, "Key", key->data, 16);
893
894   return key;
895 }
896
897 static gboolean
898 gst_hls_demux_start_fragment (GstAdaptiveDemux * demux,
899     GstAdaptiveDemuxStream * stream)
900 {
901   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
902   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
903   const GstHLSKey *key;
904   GstM3U8 *m3u8;
905
906   gst_hls_demux_stream_clear_pending_data (hls_stream);
907
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);
913
914   /* If no decryption is needed, there's nothing to be done here */
915   if (hls_stream->current_key == NULL)
916     return TRUE;
917
918   m3u8 = gst_hls_demux_stream_get_m3u8 (hls_stream);
919
920   key = gst_hls_demux_get_key (hlsdemux, hls_stream->current_key,
921       m3u8->uri, m3u8->allowcache);
922
923   if (key == NULL)
924     goto key_failed;
925
926   if (!gst_hls_demux_stream_decrypt_start (hls_stream, key->data,
927           hls_stream->current_iv))
928     goto decrypt_start_failed;
929
930   return TRUE;
931
932 key_failed:
933   {
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");
937     return FALSE;
938   }
939 decrypt_start_failed:
940   {
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"));
943     return FALSE;
944   }
945 }
946
947 static GstHLSTSReaderType
948 caps_to_reader (const GstCaps * caps)
949 {
950   const GstStructure *s = gst_caps_get_structure (caps, 0);
951
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;
956
957   return GST_HLS_TSREADER_NONE;
958 }
959
960 static GstFlowReturn
961 gst_hls_demux_handle_buffer (GstAdaptiveDemux * demux,
962     GstAdaptiveDemuxStream * stream, GstBuffer * buffer, gboolean at_eos)
963 {
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;
967   GstTagList *tags;
968
969   if (buffer == NULL)
970     return GST_FLOW_OK;
971
972   if (G_UNLIKELY (hls_stream->do_typefind)) {
973     GstCaps *caps = NULL;
974     guint buffer_size;
975     GstTypeFindProbability prob = GST_TYPE_FIND_NONE;
976     GstMapInfo info;
977
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;
981
982     gst_buffer_map (buffer, &info, GST_MAP_READ);
983     buffer_size = info.size;
984
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) {
988       caps =
989           gst_type_find_helper_for_data (GST_OBJECT_CAST (hlsdemux), info.data,
990           info.size, &prob);
991     }
992
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);
996
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;
1004       }
1005
1006       hls_stream->pending_typefind_buffer = buffer;
1007
1008       return GST_FLOW_OK;
1009     }
1010
1011     GST_DEBUG_OBJECT (stream->pad,
1012         "Typefind result: %" GST_PTR_FORMAT " prob:%d", caps, prob);
1013
1014     hls_stream->stream_type = caps_to_reader (caps);
1015     gst_hlsdemux_tsreader_set_type (&hls_stream->tsreader,
1016         hls_stream->stream_type);
1017
1018     gst_adaptive_demux_stream_set_caps (stream, caps);
1019
1020     hls_stream->do_typefind = FALSE;
1021
1022     gst_buffer_unmap (buffer, &info);
1023   }
1024   g_assert (hls_stream->pending_typefind_buffer == NULL);
1025
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;
1030   }
1031
1032   if (!gst_hlsdemux_tsreader_find_pcrs (&hls_stream->tsreader, &buffer,
1033           &first_pcr, &last_pcr, &tags)
1034       && !at_eos) {
1035     // Store this buffer for later
1036     hls_stream->pending_pcr_buffer = buffer;
1037     return GST_FLOW_OK;
1038   }
1039
1040   if (tags) {
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);
1045   }
1046
1047   if (buffer) {
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);
1053   }
1054   return GST_FLOW_OK;
1055 }
1056
1057 static GstFlowReturn
1058 gst_hls_demux_finish_fragment (GstAdaptiveDemux * demux,
1059     GstAdaptiveDemuxStream * stream)
1060 {
1061   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);   // FIXME: pass HlsStream into function
1062   GstFlowReturn ret = GST_FLOW_OK;
1063
1064   if (hls_stream->current_key)
1065     gst_hls_demux_stream_decrypt_end (hls_stream);
1066
1067   if (stream->last_ret == GST_FLOW_OK) {
1068     if (hls_stream->pending_decrypted_buffer) {
1069       if (hls_stream->current_key) {
1070         GstMapInfo info;
1071         gssize unpadded_size;
1072
1073         /* Handle pkcs7 unpadding here */
1074         gst_buffer_map (hls_stream->pending_decrypted_buffer, &info,
1075             GST_MAP_READ);
1076         unpadded_size = info.size - info.data[info.size - 1];
1077         gst_buffer_unmap (hls_stream->pending_decrypted_buffer, &info);
1078
1079         gst_buffer_resize (hls_stream->pending_decrypted_buffer, 0,
1080             unpadded_size);
1081       }
1082
1083       ret =
1084           gst_hls_demux_handle_buffer (demux, stream,
1085           hls_stream->pending_decrypted_buffer, TRUE);
1086       hls_stream->pending_decrypted_buffer = NULL;
1087     }
1088
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;
1093
1094         gst_hls_demux_handle_buffer (demux, stream, buf, TRUE);
1095       }
1096
1097       if (hls_stream->pending_pcr_buffer) {
1098         GstBuffer *buf = hls_stream->pending_pcr_buffer;
1099         hls_stream->pending_pcr_buffer = NULL;
1100
1101         ret = gst_hls_demux_handle_buffer (demux, stream, buf, TRUE);
1102       }
1103
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));
1108     }
1109   }
1110
1111   if (G_UNLIKELY (stream->downloading_header || stream->downloading_index))
1112     return GST_FLOW_OK;
1113
1114   gst_hls_demux_stream_clear_pending_data (hls_stream);
1115
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);
1119   return ret;
1120 }
1121
1122 static GstFlowReturn
1123 gst_hls_demux_data_received (GstAdaptiveDemux * demux,
1124     GstAdaptiveDemuxStream * stream, GstBuffer * buffer)
1125 {
1126   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
1127   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
1128
1129   if (hls_stream->current_offset == -1)
1130     hls_stream->current_offset = 0;
1131
1132   /* Is it encrypted? */
1133   if (hls_stream->current_key) {
1134     GError *err = NULL;
1135     gsize size;
1136     GstBuffer *tmp_buffer;
1137
1138     if (hls_stream->pending_encrypted_data == NULL)
1139       hls_stream->pending_encrypted_data = gst_adapter_new ();
1140
1141     gst_adapter_push (hls_stream->pending_encrypted_data, buffer);
1142     size = gst_adapter_available (hls_stream->pending_encrypted_data);
1143
1144     /* must be a multiple of 16 */
1145     size &= (~0xF);
1146
1147     if (size == 0) {
1148       return GST_FLOW_OK;
1149     }
1150
1151     buffer = gst_adapter_take_buffer (hls_stream->pending_encrypted_data, size);
1152     buffer =
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));
1157       g_error_free (err);
1158       return GST_FLOW_ERROR;
1159     }
1160
1161     tmp_buffer = hls_stream->pending_decrypted_buffer;
1162     hls_stream->pending_decrypted_buffer = buffer;
1163     buffer = tmp_buffer;
1164   }
1165
1166   return gst_hls_demux_handle_buffer (demux, stream, buffer, FALSE);
1167 }
1168
1169 static void
1170 gst_hls_demux_stream_free (GstAdaptiveDemuxStream * stream)
1171 {
1172   GstHLSDemuxStream *hls_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
1173
1174   if (hls_stream->playlist) {
1175     gst_m3u8_unref (hls_stream->playlist);
1176     hls_stream->playlist = NULL;
1177   }
1178
1179   if (hls_stream->pending_encrypted_data)
1180     g_object_unref (hls_stream->pending_encrypted_data);
1181
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);
1185
1186   if (hls_stream->current_key) {
1187     g_free (hls_stream->current_key);
1188     hls_stream->current_key = NULL;
1189   }
1190   if (hls_stream->current_iv) {
1191     g_free (hls_stream->current_iv);
1192     hls_stream->current_iv = NULL;
1193   }
1194   gst_hls_demux_stream_decrypt_end (hls_stream);
1195 }
1196
1197 static GstM3U8 *
1198 gst_hls_demux_stream_get_m3u8 (GstHLSDemuxStream * hlsdemux_stream)
1199 {
1200   GstM3U8 *m3u8;
1201
1202   m3u8 = hlsdemux_stream->playlist;
1203
1204   return m3u8;
1205 }
1206
1207 static gboolean
1208 gst_hls_demux_stream_has_next_fragment (GstAdaptiveDemuxStream * stream)
1209 {
1210   gboolean has_next;
1211   GstM3U8 *m3u8;
1212
1213   m3u8 = gst_hls_demux_stream_get_m3u8 (GST_HLS_DEMUX_STREAM_CAST (stream));
1214
1215   has_next = gst_m3u8_has_next_fragment (m3u8, stream->demux->segment.rate > 0);
1216
1217   return has_next;
1218 }
1219
1220 static GstFlowReturn
1221 gst_hls_demux_advance_fragment (GstAdaptiveDemuxStream * stream)
1222 {
1223   GstHLSDemuxStream *hlsdemux_stream = GST_HLS_DEMUX_STREAM_CAST (stream);
1224   GstM3U8 *m3u8;
1225
1226   m3u8 = gst_hls_demux_stream_get_m3u8 (hlsdemux_stream);
1227
1228   gst_m3u8_advance_fragment (m3u8, stream->demux->segment.rate > 0);
1229   hlsdemux_stream->reset_pts = FALSE;
1230
1231   return GST_FLOW_OK;
1232 }
1233
1234 static GstFlowReturn
1235 gst_hls_demux_update_fragment_info (GstAdaptiveDemuxStream * stream)
1236 {
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;
1242   GstM3U8 *m3u8;
1243
1244   m3u8 = gst_hls_demux_stream_get_m3u8 (hlsdemux_stream);
1245
1246   forward = (stream->demux->segment.rate > 0);
1247   file = gst_m3u8_get_next_fragment (m3u8, forward, &sequence_pos, &discont);
1248
1249   if (file == NULL) {
1250     GST_INFO_OBJECT (hlsdemux, "This playlist doesn't contain more fragments");
1251     return GST_FLOW_EOS;
1252   }
1253
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;
1261     } else {
1262       stream->fragment.header_range_end = -1;
1263     }
1264   }
1265
1266   if (stream->discont)
1267     discont = TRUE;
1268
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;
1273   } else {
1274     stream->fragment.timestamp = GST_CLOCK_TIME_NONE;
1275   }
1276
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));
1281
1282   g_free (stream->fragment.uri);
1283   stream->fragment.uri = g_strdup (file->uri);
1284
1285   GST_DEBUG_OBJECT (hlsdemux, "Stream %p URI now %s", stream, file->uri);
1286
1287   stream->fragment.range_start = file->offset;
1288   if (file->size != -1)
1289     stream->fragment.range_end = file->offset + file->size - 1;
1290   else
1291     stream->fragment.range_end = -1;
1292
1293   stream->fragment.duration = file->duration;
1294
1295   if (discont)
1296     stream->discont = TRUE;
1297
1298   gst_m3u8_media_file_unref (file);
1299
1300   return GST_FLOW_OK;
1301 }
1302
1303 static gboolean
1304 gst_hls_demux_select_bitrate (GstAdaptiveDemuxStream * stream, guint64 bitrate)
1305 {
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);
1309
1310   gboolean changed = FALSE;
1311
1312   GST_M3U8_CLIENT_LOCK (hlsdemux->client);
1313   if (hlsdemux->master == NULL || hlsdemux->master->is_simple) {
1314     GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
1315     return FALSE;
1316   }
1317   GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
1318
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);
1322     return FALSE;
1323   }
1324
1325   gst_hls_demux_change_playlist (hlsdemux, bitrate / MAX (1.0,
1326           ABS (demux->segment.rate)), &changed);
1327   if (changed)
1328     gst_hls_demux_setup_streams (GST_ADAPTIVE_DEMUX_CAST (hlsdemux));
1329   return changed;
1330 }
1331
1332 static void
1333 gst_hls_demux_reset (GstAdaptiveDemux * ademux)
1334 {
1335   GstHLSDemux *demux = GST_HLS_DEMUX_CAST (ademux);
1336
1337   GST_DEBUG_OBJECT (demux, "resetting");
1338
1339   GST_M3U8_CLIENT_LOCK (hlsdemux->client);
1340   if (demux->master) {
1341     gst_hls_master_playlist_unref (demux->master);
1342     demux->master = NULL;
1343   }
1344   if (demux->current_variant != NULL) {
1345     gst_hls_variant_stream_unref (demux->current_variant);
1346     demux->current_variant = NULL;
1347   }
1348   if (demux->previous_variant != NULL) {
1349     gst_hls_variant_stream_unref (demux->previous_variant);
1350     demux->previous_variant = NULL;
1351   }
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);
1357
1358   gst_hls_demux_clear_all_pending_data (demux);
1359   GST_M3U8_CLIENT_UNLOCK (hlsdemux->client);
1360 }
1361
1362 static gchar *
1363 gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf)
1364 {
1365   GstMapInfo info;
1366   gchar *playlist;
1367
1368   if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1369     goto map_error;
1370
1371   if (!g_utf8_validate ((gchar *) info.data, info.size, NULL))
1372     goto validate_error;
1373
1374   /* alloc size + 1 to end with a null character */
1375   playlist = g_malloc0 (info.size + 1);
1376   memcpy (playlist, info.data, info.size);
1377
1378   gst_buffer_unmap (buf, &info);
1379   return playlist;
1380
1381 validate_error:
1382   gst_buffer_unmap (buf, &info);
1383 map_error:
1384   return NULL;
1385 }
1386
1387 static gint
1388 gst_hls_demux_find_variant_match (const GstHLSVariantStream * a,
1389     const GstHLSVariantStream * b)
1390 {
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) {
1397     return 0;
1398   }
1399
1400   return 1;
1401 }
1402
1403 /* Update the master playlist, which contains the list of available
1404  * variants */
1405 static gboolean
1406 gst_hls_demux_update_variant_playlist (GstHLSDemux * hlsdemux, gchar * data,
1407     const gchar * uri, const gchar * base_uri)
1408 {
1409   GstHLSMasterPlaylist *new_master, *old;
1410   gboolean ret = FALSE;
1411   GList *l, *unmatched_lists;
1412   GstHLSVariantStream *new_variant;
1413
1414   new_master = gst_hls_master_playlist_new_from_data (data, base_uri ? base_uri : uri); // FIXME: check which uri to use here
1415
1416   if (new_master == NULL)
1417     return ret;
1418
1419   if (new_master->is_simple) {
1420     // FIXME: we should be able to support this though, in the unlikely
1421     // case that it changed?
1422     GST_ERROR
1423         ("Cannot update variant playlist: New playlist is not a variant playlist");
1424     gst_hls_master_playlist_unref (new_master);
1425     return FALSE;
1426   }
1427
1428   GST_M3U8_CLIENT_LOCK (self);
1429
1430   if (hlsdemux->master->is_simple) {
1431     GST_ERROR
1432         ("Cannot update variant playlist: Current playlist is not a variant playlist");
1433     gst_hls_master_playlist_unref (new_master);
1434     goto out;
1435   }
1436
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);
1442
1443     if (match) {
1444       GstHLSVariantStream *variant = l->data;
1445       GstHLSVariantStream *old = match->data;
1446
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;
1451     }
1452   }
1453
1454   if (unmatched_lists != NULL) {
1455     GST_WARNING ("Unable to match all playlists");
1456
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");
1460       }
1461     }
1462
1463     g_list_free (unmatched_lists);
1464   }
1465
1466   /* Switch out the variant playlist */
1467   old = hlsdemux->master;
1468
1469   // FIXME: check all this and also switch of variants, if anything needs updating
1470   hlsdemux->master = new_master;
1471
1472   if (hlsdemux->current_variant == NULL) {
1473     new_variant = new_master->default_variant;
1474   } else {
1475     /* Find the same variant in the new playlist */
1476     new_variant =
1477         gst_hls_master_playlist_get_matching_variant (new_master,
1478         hlsdemux->current_variant);
1479   }
1480
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);
1484
1485   gst_hls_master_playlist_unref (old);
1486
1487   ret = (hlsdemux->current_variant != NULL);
1488 out:
1489   GST_M3U8_CLIENT_UNLOCK (self);
1490
1491   return ret;
1492 }
1493
1494 static gboolean
1495 gst_hls_demux_update_rendition_manifest (GstHLSDemux * demux,
1496     GstHLSMedia * media, GError ** err)
1497 {
1498   GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
1499   GstFragment *download;
1500   GstBuffer *buf;
1501   gchar *playlist;
1502   const gchar *main_uri;
1503   GstM3U8 *m3u8;
1504   gchar *uri = media->uri;
1505
1506   main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1507   download =
1508       gst_uri_downloader_fetch_uri (adaptive_demux->downloader, uri, main_uri,
1509       TRUE, TRUE, TRUE, err);
1510
1511   if (download == NULL)
1512     return FALSE;
1513
1514   m3u8 = media->playlist;
1515
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);
1519   } else {
1520     gst_m3u8_set_uri (m3u8, download->uri, download->redirect_uri, media->name);
1521   }
1522
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);
1527
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");
1532     return FALSE;
1533   }
1534
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");
1539     return FALSE;
1540   }
1541
1542   return TRUE;
1543 }
1544
1545 static gboolean
1546 gst_hls_demux_update_playlist (GstHLSDemux * demux, gboolean update,
1547     GError ** err)
1548 {
1549   GstAdaptiveDemux *adaptive_demux = GST_ADAPTIVE_DEMUX (demux);
1550   GstFragment *download;
1551   GstBuffer *buf;
1552   gchar *playlist;
1553   gboolean main_checked = FALSE;
1554   const gchar *main_uri;
1555   GstM3U8 *m3u8;
1556   gchar *uri;
1557   gint i;
1558
1559 retry:
1560   uri = gst_m3u8_get_uri (demux->current_variant->m3u8);
1561   main_uri = gst_adaptive_demux_get_manifest_ref_uri (adaptive_demux);
1562   download =
1563       gst_uri_downloader_fetch_uri (adaptive_demux->downloader, uri, main_uri,
1564       TRUE, TRUE, TRUE, err);
1565   if (download == NULL) {
1566     gchar *base_uri;
1567
1568     if (!update || main_checked || demux->master->is_simple
1569         || !gst_adaptive_demux_is_running (GST_ADAPTIVE_DEMUX_CAST (demux))) {
1570       g_free (uri);
1571       return FALSE;
1572     }
1573     g_clear_error (err);
1574     GST_INFO_OBJECT (demux,
1575         "Updating playlist %s failed, attempt to refresh variant playlist %s",
1576         uri, main_uri);
1577     download =
1578         gst_uri_downloader_fetch_uri (adaptive_demux->downloader,
1579         main_uri, NULL, TRUE, TRUE, TRUE, err);
1580     if (download == NULL) {
1581       g_free (uri);
1582       return FALSE;
1583     }
1584
1585     buf = gst_fragment_get_buffer (download);
1586     playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1587     gst_buffer_unref (buf);
1588
1589     if (playlist == NULL) {
1590       GST_WARNING_OBJECT (demux,
1591           "Failed to validate variant playlist encoding");
1592       g_free (uri);
1593       g_object_unref (download);
1594       g_set_error (err, GST_STREAM_ERROR, GST_STREAM_ERROR_WRONG_TYPE,
1595           "Couldn't validate playlist encoding");
1596       return FALSE;
1597     }
1598
1599     g_free (uri);
1600     if (download->redirect_permanent && download->redirect_uri) {
1601       uri = download->redirect_uri;
1602       base_uri = NULL;
1603     } else {
1604       uri = download->uri;
1605       base_uri = download->redirect_uri;
1606     }
1607
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");
1613       return FALSE;
1614     }
1615
1616     g_object_unref (download);
1617
1618     main_checked = TRUE;
1619     goto retry;
1620   }
1621   g_free (uri);
1622
1623   m3u8 = demux->current_variant->m3u8;
1624
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);
1629   } else {
1630     gst_m3u8_set_uri (m3u8, download->uri, download->redirect_uri,
1631         demux->current_variant->name);
1632   }
1633
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);
1638
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");
1643     return FALSE;
1644   }
1645
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");
1650     return FALSE;
1651   }
1652
1653   for (i = 0; i < GST_HLS_N_MEDIA_TYPES; ++i) {
1654     GList *mlist = demux->current_variant->media[i];
1655
1656     while (mlist != NULL) {
1657       GstHLSMedia *media = mlist->data;
1658
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;
1663         continue;
1664       }
1665       GST_LOG_OBJECT (demux,
1666           "Updating playlist for media of type %d - %s, uri: %s", i,
1667           media->name, media->uri);
1668
1669       if (!gst_hls_demux_update_rendition_manifest (demux, media, err))
1670         return FALSE;
1671
1672       mlist = mlist->next;
1673     }
1674   }
1675
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;
1680
1681     GST_M3U8_CLIENT_LOCK (demux->client);
1682     last_sequence =
1683         GST_M3U8_MEDIA_FILE (g_list_last (m3u8->files)->data)->sequence;
1684     first_sequence =
1685         GST_M3U8_MEDIA_FILE (g_list_first (m3u8->files)->data)->sequence;
1686
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,
1697           m3u8->sequence);
1698     }
1699     GST_M3U8_CLIENT_UNLOCK (demux->client);
1700   } else if (!gst_m3u8_is_live (m3u8)) {
1701     GstClockTime current_pos, target_pos;
1702     guint sequence = 0;
1703     GList *walk;
1704
1705     /* Sequence numbers are not guaranteed to be the same in different
1706      * playlists, so get the correct fragment here based on the current
1707      * position
1708      */
1709     GST_M3U8_CLIENT_LOCK (demux->client);
1710
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;
1716     } else {
1717       target_pos = 0;
1718     }
1719     if (GST_CLOCK_TIME_IS_VALID (m3u8->sequence_position)) {
1720       target_pos = MAX (target_pos, m3u8->sequence_position);
1721     }
1722
1723     GST_LOG_OBJECT (demux, "Looking for sequence position %"
1724         GST_TIME_FORMAT " in updated playlist", GST_TIME_ARGS (target_pos));
1725
1726     current_pos = 0;
1727     for (walk = m3u8->files; walk; walk = walk->next) {
1728       GstM3U8MediaFile *file = walk->data;
1729
1730       sequence = file->sequence;
1731       if (current_pos <= target_pos
1732           && target_pos < current_pos + file->duration) {
1733         break;
1734       }
1735       current_pos += file->duration;
1736     }
1737     /* End of playlist */
1738     if (!walk)
1739       sequence++;
1740     m3u8->sequence = sequence;
1741     m3u8->sequence_position = current_pos;
1742     GST_M3U8_CLIENT_UNLOCK (demux->client);
1743   }
1744
1745   return TRUE;
1746 }
1747
1748 static gboolean
1749 gst_hls_demux_change_playlist (GstHLSDemux * demux, guint max_bitrate,
1750     gboolean * changed)
1751 {
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;
1757
1758   g_return_val_if_fail (adaptive_demux->streams != NULL, FALSE);
1759
1760   stream = adaptive_demux->streams->data;
1761
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);
1764   new_variant =
1765       gst_hls_master_playlist_get_variant_for_bitrate (demux->master,
1766       demux->current_variant, max_bitrate);
1767
1768   GST_M3U8_CLIENT_LOCK (demux->client);
1769
1770 retry_failover_protection:
1771   old_bandwidth = previous_variant->bandwidth;
1772   new_bandwidth = new_variant->bandwidth;
1773
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);
1778     return TRUE;
1779   }
1780
1781   GST_M3U8_CLIENT_UNLOCK (demux->client);
1782
1783   gst_hls_demux_set_current_variant (demux, new_variant);
1784
1785   GST_INFO_OBJECT (demux, "Client was on %dbps, max allowed is %dbps, switching"
1786       " to bitrate %dbps", old_bandwidth, max_bitrate, new_bandwidth);
1787
1788   if (gst_hls_demux_update_playlist (demux, TRUE, NULL)) {
1789     const gchar *main_uri;
1790     gchar *uri;
1791
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)));
1800     g_free (uri);
1801     if (changed)
1802       *changed = TRUE;
1803     stream->discont = TRUE;
1804   } else if (gst_adaptive_demux_is_running (GST_ADAPTIVE_DEMUX_CAST (demux))) {
1805     GstHLSVariantStream *failover_variant = NULL;
1806     GList *failover;
1807
1808     GST_INFO_OBJECT (demux, "Unable to update playlist. Switching back");
1809     GST_M3U8_CLIENT_LOCK (demux->client);
1810
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;
1822     }
1823
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)
1830         return FALSE;
1831     } else {
1832       lowest_variant = demux->master->variants->data;
1833       if (new_bandwidth == lowest_variant->bandwidth)
1834         return FALSE;
1835     }
1836     return gst_hls_demux_change_playlist (demux, new_bandwidth - 1, changed);
1837   }
1838
1839   gst_hls_variant_stream_unref (previous_variant);
1840   return TRUE;
1841 }
1842
1843 #if defined(HAVE_OPENSSL)
1844 static gboolean
1845 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1846     const guint8 * key_data, const guint8 * iv_data)
1847 {
1848   EVP_CIPHER_CTX *ctx;
1849 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1850   EVP_CIPHER_CTX_init (&stream->aes_ctx);
1851   ctx = &stream->aes_ctx;
1852 #else
1853   stream->aes_ctx = EVP_CIPHER_CTX_new ();
1854   ctx = stream->aes_ctx;
1855 #endif
1856   if (!EVP_DecryptInit_ex (ctx, EVP_aes_128_cbc (), NULL, key_data, iv_data))
1857     return FALSE;
1858   EVP_CIPHER_CTX_set_padding (ctx, 0);
1859   return TRUE;
1860 }
1861
1862 static gboolean
1863 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1864     const guint8 * encrypted_data, guint8 * decrypted_data)
1865 {
1866   int len, flen = 0;
1867   EVP_CIPHER_CTX *ctx;
1868
1869 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1870   ctx = &stream->aes_ctx;
1871 #else
1872   ctx = stream->aes_ctx;
1873 #endif
1874
1875   if (G_UNLIKELY (length > G_MAXINT || length % 16 != 0))
1876     return FALSE;
1877
1878   len = (int) length;
1879   if (!EVP_DecryptUpdate (ctx, decrypted_data, &len, encrypted_data, len))
1880     return FALSE;
1881   EVP_DecryptFinal_ex (ctx, decrypted_data + len, &flen);
1882   g_return_val_if_fail (len + flen == length, FALSE);
1883   return TRUE;
1884 }
1885
1886 static void
1887 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1888 {
1889 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1890   EVP_CIPHER_CTX_cleanup (&stream->aes_ctx);
1891 #else
1892   EVP_CIPHER_CTX_free (stream->aes_ctx);
1893   stream->aes_ctx = NULL;
1894 #endif
1895 }
1896
1897 #elif defined(HAVE_NETTLE)
1898 static gboolean
1899 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1900     const guint8 * key_data, const guint8 * iv_data)
1901 {
1902   aes128_set_decrypt_key (&stream->aes_ctx.ctx, key_data);
1903   CBC_SET_IV (&stream->aes_ctx, iv_data);
1904
1905   return TRUE;
1906 }
1907
1908 static gboolean
1909 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1910     const guint8 * encrypted_data, guint8 * decrypted_data)
1911 {
1912   if (length % 16 != 0)
1913     return FALSE;
1914
1915   CBC_DECRYPT (&stream->aes_ctx, aes128_decrypt, length, decrypted_data,
1916       encrypted_data);
1917
1918   return TRUE;
1919 }
1920
1921 static void
1922 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1923 {
1924   /* NOP */
1925 }
1926
1927 #elif defined(HAVE_LIBGCRYPT)
1928 static gboolean
1929 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1930     const guint8 * key_data, const guint8 * iv_data)
1931 {
1932   gcry_error_t err = 0;
1933   gboolean ret = FALSE;
1934
1935   err =
1936       gcry_cipher_open (&stream->aes_ctx, GCRY_CIPHER_AES128,
1937       GCRY_CIPHER_MODE_CBC, 0);
1938   if (err)
1939     goto out;
1940   err = gcry_cipher_setkey (stream->aes_ctx, key_data, 16);
1941   if (err)
1942     goto out;
1943   err = gcry_cipher_setiv (stream->aes_ctx, iv_data, 16);
1944   if (!err)
1945     ret = TRUE;
1946
1947 out:
1948   if (!ret)
1949     if (stream->aes_ctx)
1950       gcry_cipher_close (stream->aes_ctx);
1951
1952   return ret;
1953 }
1954
1955 static gboolean
1956 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1957     const guint8 * encrypted_data, guint8 * decrypted_data)
1958 {
1959   gcry_error_t err = 0;
1960
1961   err = gcry_cipher_decrypt (stream->aes_ctx, decrypted_data, length,
1962       encrypted_data, length);
1963
1964   return err == 0;
1965 }
1966
1967 static void
1968 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1969 {
1970   if (stream->aes_ctx) {
1971     gcry_cipher_close (stream->aes_ctx);
1972     stream->aes_ctx = NULL;
1973   }
1974 }
1975
1976 #else
1977 /* NO crypto available */
1978 static gboolean
1979 gst_hls_demux_stream_decrypt_start (GstHLSDemuxStream * stream,
1980     const guint8 * key_data, const guint8 * iv_data)
1981 {
1982   GST_ERROR ("No crypto available");
1983   return FALSE;
1984 }
1985
1986 static gboolean
1987 decrypt_fragment (GstHLSDemuxStream * stream, gsize length,
1988     const guint8 * encrypted_data, guint8 * decrypted_data)
1989 {
1990   GST_ERROR ("Cannot decrypt fragment, no crypto available");
1991   return FALSE;
1992 }
1993
1994 static void
1995 gst_hls_demux_stream_decrypt_end (GstHLSDemuxStream * stream)
1996 {
1997   return;
1998 }
1999 #endif
2000
2001 static GstBuffer *
2002 gst_hls_demux_decrypt_fragment (GstHLSDemux * demux, GstHLSDemuxStream * stream,
2003     GstBuffer * encrypted_buffer, GError ** err)
2004 {
2005   GstBuffer *decrypted_buffer = NULL;
2006   GstMapInfo encrypted_info, decrypted_info;
2007
2008   decrypted_buffer =
2009       gst_buffer_new_allocate (NULL, gst_buffer_get_size (encrypted_buffer),
2010       NULL);
2011
2012   gst_buffer_map (encrypted_buffer, &encrypted_info, GST_MAP_READ);
2013   gst_buffer_map (decrypted_buffer, &decrypted_info, GST_MAP_WRITE);
2014
2015   if (!decrypt_fragment (stream, encrypted_info.size,
2016           encrypted_info.data, decrypted_info.data))
2017     goto decrypt_error;
2018
2019
2020   gst_buffer_unmap (decrypted_buffer, &decrypted_info);
2021   gst_buffer_unmap (encrypted_buffer, &encrypted_info);
2022
2023   gst_buffer_unref (encrypted_buffer);
2024
2025   return decrypted_buffer;
2026
2027 decrypt_error:
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");
2031
2032   gst_buffer_unmap (decrypted_buffer, &decrypted_info);
2033   gst_buffer_unmap (encrypted_buffer, &encrypted_info);
2034
2035   gst_buffer_unref (encrypted_buffer);
2036   gst_buffer_unref (decrypted_buffer);
2037
2038   return NULL;
2039 }
2040
2041 static gint64
2042 gst_hls_demux_get_manifest_update_interval (GstAdaptiveDemux * demux)
2043 {
2044   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
2045   GstClockTime target_duration;
2046
2047   if (hlsdemux->current_variant) {
2048     target_duration =
2049         gst_m3u8_get_target_duration (hlsdemux->current_variant->m3u8);
2050   } else {
2051     target_duration = 5 * GST_SECOND;
2052   }
2053
2054   return gst_util_uint64_scale (target_duration, G_USEC_PER_SEC, GST_SECOND);
2055 }
2056
2057 static gboolean
2058 gst_hls_demux_get_live_seek_range (GstAdaptiveDemux * demux, gint64 * start,
2059     gint64 * stop)
2060 {
2061   GstHLSDemux *hlsdemux = GST_HLS_DEMUX_CAST (demux);
2062   gboolean ret = FALSE;
2063
2064   if (hlsdemux->current_variant) {
2065     ret =
2066         gst_m3u8_get_seek_range (hlsdemux->current_variant->m3u8, start, stop);
2067   }
2068
2069   return ret;
2070 }