hlsdemux: Use libgcrypt directly instead of going through gnutls
[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  *
8  * Gsthlsdemux.c:
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25 /**
26  * SECTION:element-hlsdemux
27  *
28  * HTTP Live Streaming demuxer element.
29  *
30  * <refsect2>
31  * <title>Example launch line</title>
32  * |[
33  * gst-launch souphttpsrc location=http://devimages.apple.com/iphone/samples/bipbop/gear4/prog_index.m3u8 ! hlsdemux ! decodebin2 ! videoconvert ! videoscale ! autovideosink
34  * ]|
35  * </refsect2>
36  *
37  * Last reviewed on 2010-10-07
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #  include "config.h"
42 #endif
43
44 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
45  * with newer GLib versions (>= 2.31.0) */
46 #define GLIB_DISABLE_DEPRECATION_WARNINGS
47
48 #include <string.h>
49 #include <gst/glib-compat-private.h>
50 #include <gcrypt.h>
51 #include "gsthlsdemux.h"
52
53 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
54     GST_PAD_SRC,
55     GST_PAD_SOMETIMES,
56     GST_STATIC_CAPS_ANY);
57
58 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("application/x-hls"));
62
63 GST_DEBUG_CATEGORY_STATIC (gst_hls_demux_debug);
64 #define GST_CAT_DEFAULT gst_hls_demux_debug
65
66 enum
67 {
68   PROP_0,
69
70   PROP_FRAGMENTS_CACHE,
71   PROP_BITRATE_LIMIT,
72   PROP_CONNECTION_SPEED,
73   PROP_LAST
74 };
75
76 static const float update_interval_factor[] = { 1, 0.5, 1.5, 3 };
77
78 #define DEFAULT_FRAGMENTS_CACHE 3
79 #define DEFAULT_FAILED_COUNT 3
80 #define DEFAULT_BITRATE_LIMIT 0.8
81 #define DEFAULT_CONNECTION_SPEED    0
82
83 /* GObject */
84 static void gst_hls_demux_set_property (GObject * object, guint prop_id,
85     const GValue * value, GParamSpec * pspec);
86 static void gst_hls_demux_get_property (GObject * object, guint prop_id,
87     GValue * value, GParamSpec * pspec);
88 static void gst_hls_demux_dispose (GObject * obj);
89
90 /* GstElement */
91 static GstStateChangeReturn
92 gst_hls_demux_change_state (GstElement * element, GstStateChange transition);
93
94 /* GstHLSDemux */
95 static GstFlowReturn gst_hls_demux_chain (GstPad * pad, GstObject * parent,
96     GstBuffer * buf);
97 static gboolean gst_hls_demux_sink_event (GstPad * pad, GstObject * parent,
98     GstEvent * event);
99 static gboolean gst_hls_demux_src_event (GstPad * pad, GstObject * parent,
100     GstEvent * event);
101 static gboolean gst_hls_demux_src_query (GstPad * pad, GstObject * parent,
102     GstQuery * query);
103 static void gst_hls_demux_stream_loop (GstHLSDemux * demux);
104 static void gst_hls_demux_updates_loop (GstHLSDemux * demux);
105 static void gst_hls_demux_stop (GstHLSDemux * demux);
106 static void gst_hls_demux_pause_tasks (GstHLSDemux * demux, gboolean caching);
107 static gboolean gst_hls_demux_cache_fragments (GstHLSDemux * demux);
108 static gboolean gst_hls_demux_schedule (GstHLSDemux * demux);
109 static gboolean gst_hls_demux_switch_playlist (GstHLSDemux * demux);
110 static gboolean gst_hls_demux_get_next_fragment (GstHLSDemux * demux,
111     gboolean caching);
112 static gboolean gst_hls_demux_update_playlist (GstHLSDemux * demux,
113     gboolean update);
114 static void gst_hls_demux_reset (GstHLSDemux * demux, gboolean dispose);
115 static gboolean gst_hls_demux_set_location (GstHLSDemux * demux,
116     const gchar * uri);
117 static gchar *gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf);
118
119 #define gst_hls_demux_parent_class parent_class
120 G_DEFINE_TYPE (GstHLSDemux, gst_hls_demux, GST_TYPE_ELEMENT);
121
122 static void
123 gst_hls_demux_dispose (GObject * obj)
124 {
125   GstHLSDemux *demux = GST_HLS_DEMUX (obj);
126
127   if (demux->stream_task) {
128     if (GST_TASK_STATE (demux->stream_task) != GST_TASK_STOPPED) {
129       GST_DEBUG_OBJECT (demux, "Leaving streaming task");
130       gst_task_stop (demux->stream_task);
131       g_rec_mutex_lock (&demux->stream_lock);
132       g_rec_mutex_unlock (&demux->stream_lock);
133       gst_task_join (demux->stream_task);
134     }
135     gst_object_unref (demux->stream_task);
136     g_rec_mutex_clear (&demux->stream_lock);
137     demux->stream_task = NULL;
138   }
139
140   if (demux->updates_task) {
141     if (GST_TASK_STATE (demux->updates_task) != GST_TASK_STOPPED) {
142       GST_DEBUG_OBJECT (demux, "Leaving updates task");
143       demux->cancelled = TRUE;
144       gst_uri_downloader_cancel (demux->downloader);
145       gst_task_stop (demux->updates_task);
146       g_mutex_lock (&demux->updates_timed_lock);
147       GST_TASK_SIGNAL (demux->updates_task);
148       g_rec_mutex_lock (&demux->updates_lock);
149       g_rec_mutex_unlock (&demux->updates_lock);
150       g_mutex_unlock (&demux->updates_timed_lock);
151       gst_task_join (demux->updates_task);
152     }
153     gst_object_unref (demux->updates_task);
154     g_mutex_clear (&demux->updates_timed_lock);
155     g_rec_mutex_clear (&demux->updates_lock);
156     demux->updates_task = NULL;
157   }
158
159   if (demux->downloader != NULL) {
160     g_object_unref (demux->downloader);
161     demux->downloader = NULL;
162   }
163
164   gst_hls_demux_reset (demux, TRUE);
165
166   g_queue_free (demux->queue);
167
168   G_OBJECT_CLASS (parent_class)->dispose (obj);
169 }
170
171 static void
172 gst_hls_demux_class_init (GstHLSDemuxClass * klass)
173 {
174   GObjectClass *gobject_class;
175   GstElementClass *element_class;
176
177   gobject_class = (GObjectClass *) klass;
178   element_class = (GstElementClass *) klass;
179
180   gobject_class->set_property = gst_hls_demux_set_property;
181   gobject_class->get_property = gst_hls_demux_get_property;
182   gobject_class->dispose = gst_hls_demux_dispose;
183
184   g_object_class_install_property (gobject_class, PROP_FRAGMENTS_CACHE,
185       g_param_spec_uint ("fragments-cache", "Fragments cache",
186           "Number of fragments needed to be cached to start playing",
187           2, G_MAXUINT, DEFAULT_FRAGMENTS_CACHE,
188           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189
190   g_object_class_install_property (gobject_class, PROP_BITRATE_LIMIT,
191       g_param_spec_float ("bitrate-limit",
192           "Bitrate limit in %",
193           "Limit of the available bitrate to use when switching to alternates.",
194           0, 1, DEFAULT_BITRATE_LIMIT,
195           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196
197   g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
198       g_param_spec_uint ("connection-speed", "Connection Speed",
199           "Network connection speed in kbps (0 = unknown)",
200           0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
201           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202
203   element_class->change_state = GST_DEBUG_FUNCPTR (gst_hls_demux_change_state);
204
205   gst_element_class_add_pad_template (element_class,
206       gst_static_pad_template_get (&srctemplate));
207
208   gst_element_class_add_pad_template (element_class,
209       gst_static_pad_template_get (&sinktemplate));
210
211   gst_element_class_set_static_metadata (element_class,
212       "HLS Demuxer",
213       "Demuxer/URIList",
214       "HTTP Live Streaming demuxer",
215       "Marc-Andre Lureau <marcandre.lureau@gmail.com>\n"
216       "Andoni Morales Alastruey <ylatuya@gmail.com>");
217
218   GST_DEBUG_CATEGORY_INIT (gst_hls_demux_debug, "hlsdemux", 0,
219       "hlsdemux element");
220 }
221
222 static void
223 gst_hls_demux_init (GstHLSDemux * demux)
224 {
225   /* sink pad */
226   demux->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
227   gst_pad_set_chain_function (demux->sinkpad,
228       GST_DEBUG_FUNCPTR (gst_hls_demux_chain));
229   gst_pad_set_event_function (demux->sinkpad,
230       GST_DEBUG_FUNCPTR (gst_hls_demux_sink_event));
231   gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
232
233   /* Downloader */
234   demux->downloader = gst_uri_downloader_new ();
235
236   demux->do_typefind = TRUE;
237
238   /* Properties */
239   demux->fragments_cache = DEFAULT_FRAGMENTS_CACHE;
240   demux->bitrate_limit = DEFAULT_BITRATE_LIMIT;
241   demux->connection_speed = DEFAULT_CONNECTION_SPEED;
242
243   demux->queue = g_queue_new ();
244
245   /* Updates task */
246   g_rec_mutex_init (&demux->updates_lock);
247   demux->updates_task =
248       gst_task_new ((GstTaskFunction) gst_hls_demux_updates_loop, demux, NULL);
249   gst_task_set_lock (demux->updates_task, &demux->updates_lock);
250   g_mutex_init (&demux->updates_timed_lock);
251
252   /* Streaming task */
253   g_rec_mutex_init (&demux->stream_lock);
254   demux->stream_task =
255       gst_task_new ((GstTaskFunction) gst_hls_demux_stream_loop, demux, NULL);
256   gst_task_set_lock (demux->stream_task, &demux->stream_lock);
257
258   demux->have_group_id = FALSE;
259   demux->group_id = G_MAXUINT;
260 }
261
262 static void
263 gst_hls_demux_set_property (GObject * object, guint prop_id,
264     const GValue * value, GParamSpec * pspec)
265 {
266   GstHLSDemux *demux = GST_HLS_DEMUX (object);
267
268   switch (prop_id) {
269     case PROP_FRAGMENTS_CACHE:
270       demux->fragments_cache = g_value_get_uint (value);
271       break;
272     case PROP_BITRATE_LIMIT:
273       demux->bitrate_limit = g_value_get_float (value);
274       break;
275     case PROP_CONNECTION_SPEED:
276       demux->connection_speed = g_value_get_uint (value) * 1000;
277       break;
278     default:
279       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280       break;
281   }
282 }
283
284 static void
285 gst_hls_demux_get_property (GObject * object, guint prop_id, GValue * value,
286     GParamSpec * pspec)
287 {
288   GstHLSDemux *demux = GST_HLS_DEMUX (object);
289
290   switch (prop_id) {
291     case PROP_FRAGMENTS_CACHE:
292       g_value_set_uint (value, demux->fragments_cache);
293       break;
294     case PROP_BITRATE_LIMIT:
295       g_value_set_float (value, demux->bitrate_limit);
296       break;
297     case PROP_CONNECTION_SPEED:
298       g_value_set_uint (value, demux->connection_speed / 1000);
299       break;
300     default:
301       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
302       break;
303   }
304 }
305
306 static GstStateChangeReturn
307 gst_hls_demux_change_state (GstElement * element, GstStateChange transition)
308 {
309   GstStateChangeReturn ret;
310   GstHLSDemux *demux = GST_HLS_DEMUX (element);
311
312   switch (transition) {
313     case GST_STATE_CHANGE_READY_TO_PAUSED:
314       gst_hls_demux_reset (demux, FALSE);
315       gst_uri_downloader_reset (demux->downloader);
316       break;
317     default:
318       break;
319   }
320
321   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
322
323   switch (transition) {
324     case GST_STATE_CHANGE_PAUSED_TO_READY:
325       demux->cancelled = TRUE;
326       gst_hls_demux_stop (demux);
327       gst_task_join (demux->stream_task);
328       gst_hls_demux_reset (demux, FALSE);
329       break;
330     default:
331       break;
332   }
333   return ret;
334 }
335
336 static gboolean
337 gst_hls_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
338 {
339   GstHLSDemux *demux;
340
341   demux = GST_HLS_DEMUX (parent);
342
343   switch (event->type) {
344     case GST_EVENT_SEEK:
345     {
346       gdouble rate;
347       GstFormat format;
348       GstSeekFlags flags;
349       GstSeekType start_type, stop_type;
350       gint64 start, stop;
351       GList *walk;
352       GstClockTime position, current_pos, target_pos;
353       gint current_sequence;
354       GstM3U8MediaFile *file;
355
356       GST_INFO_OBJECT (demux, "Received GST_EVENT_SEEK");
357
358       if (gst_m3u8_client_is_live (demux->client)) {
359         GST_WARNING_OBJECT (demux, "Received seek event for live stream");
360         return FALSE;
361       }
362
363       gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
364           &stop_type, &stop);
365
366       if (format != GST_FORMAT_TIME)
367         return FALSE;
368
369       GST_DEBUG_OBJECT (demux, "seek event, rate: %f start: %" GST_TIME_FORMAT
370           " stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
371           GST_TIME_ARGS (stop));
372
373       GST_M3U8_CLIENT_LOCK (demux->client);
374       file = GST_M3U8_MEDIA_FILE (demux->client->current->files->data);
375       current_sequence = file->sequence;
376       current_pos = 0;
377       target_pos = (GstClockTime) start;
378       for (walk = demux->client->current->files; walk; walk = walk->next) {
379         file = walk->data;
380
381         current_sequence = file->sequence;
382         if (current_pos <= target_pos
383             && target_pos < current_pos + file->duration) {
384           break;
385         }
386         current_pos += file->duration;
387       }
388       GST_M3U8_CLIENT_UNLOCK (demux->client);
389
390       if (walk == NULL) {
391         GST_WARNING_OBJECT (demux, "Could not find seeked fragment");
392         return FALSE;
393       }
394
395       if (flags & GST_SEEK_FLAG_FLUSH) {
396         GST_DEBUG_OBJECT (demux, "sending flush start");
397         gst_pad_push_event (demux->srcpad, gst_event_new_flush_start ());
398       }
399
400       demux->cancelled = TRUE;
401       gst_task_pause (demux->stream_task);
402       gst_uri_downloader_cancel (demux->downloader);
403       gst_task_stop (demux->updates_task);
404       g_mutex_lock (&demux->updates_timed_lock);
405       GST_TASK_SIGNAL (demux->updates_task);
406       g_mutex_unlock (&demux->updates_timed_lock);
407       g_rec_mutex_lock (&demux->updates_lock);
408       g_rec_mutex_unlock (&demux->updates_lock);
409       gst_task_pause (demux->stream_task);
410
411       /* wait for streaming to finish */
412       g_rec_mutex_lock (&demux->stream_lock);
413
414       demux->need_cache = TRUE;
415       while (!g_queue_is_empty (demux->queue)) {
416         GstFragment *fragment = g_queue_pop_head (demux->queue);
417         g_object_unref (fragment);
418       }
419       g_queue_clear (demux->queue);
420
421       GST_M3U8_CLIENT_LOCK (demux->client);
422       GST_DEBUG_OBJECT (demux, "seeking to sequence %d", current_sequence);
423       demux->client->sequence = current_sequence;
424       gst_m3u8_client_get_current_position (demux->client, &position);
425       demux->position_shift = start - position;
426       demux->need_segment = TRUE;
427       GST_M3U8_CLIENT_UNLOCK (demux->client);
428
429
430       if (flags & GST_SEEK_FLAG_FLUSH) {
431         GST_DEBUG_OBJECT (demux, "sending flush stop");
432         gst_pad_push_event (demux->srcpad, gst_event_new_flush_stop (TRUE));
433       }
434
435       demux->cancelled = FALSE;
436       gst_uri_downloader_reset (demux->downloader);
437       gst_task_start (demux->stream_task);
438       g_rec_mutex_unlock (&demux->stream_lock);
439
440       return TRUE;
441     }
442     default:
443       break;
444   }
445
446   return gst_pad_event_default (pad, parent, event);
447 }
448
449 static gboolean
450 gst_hls_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
451 {
452   GstHLSDemux *demux;
453   GstQuery *query;
454   gboolean ret;
455   gchar *uri;
456
457   demux = GST_HLS_DEMUX (parent);
458
459   switch (event->type) {
460     case GST_EVENT_EOS:{
461       gchar *playlist = NULL;
462
463       if (demux->playlist == NULL) {
464         GST_WARNING_OBJECT (demux, "Received EOS without a playlist.");
465         break;
466       }
467
468       GST_DEBUG_OBJECT (demux,
469           "Got EOS on the sink pad: main playlist fetched");
470
471       query = gst_query_new_uri ();
472       ret = gst_pad_peer_query (demux->sinkpad, query);
473       if (ret) {
474         gst_query_parse_uri_redirection (query, &uri);
475         if (uri == NULL)
476           gst_query_parse_uri (query, &uri);
477         gst_hls_demux_set_location (demux, uri);
478         g_free (uri);
479       }
480       gst_query_unref (query);
481
482       playlist = gst_hls_src_buf_to_utf8_playlist (demux->playlist);
483       demux->playlist = NULL;
484       if (playlist == NULL) {
485         GST_WARNING_OBJECT (demux, "Error validating first playlist.");
486       } else if (!gst_m3u8_client_update (demux->client, playlist)) {
487         /* In most cases, this will happen if we set a wrong url in the
488          * source element and we have received the 404 HTML response instead of
489          * the playlist */
490         GST_ELEMENT_ERROR (demux, STREAM, DECODE, ("Invalid playlist."),
491             (NULL));
492         return FALSE;
493       }
494
495       if (!ret && gst_m3u8_client_is_live (demux->client)) {
496         GST_ELEMENT_ERROR (demux, RESOURCE, NOT_FOUND,
497             ("Failed querying the playlist uri, "
498                 "required for live sources."), (NULL));
499         return FALSE;
500       }
501
502       gst_task_start (demux->stream_task);
503       gst_event_unref (event);
504       return TRUE;
505     }
506     case GST_EVENT_SEGMENT:
507       /* Swallow newsegments, we'll push our own */
508       gst_event_unref (event);
509       return TRUE;
510     default:
511       break;
512   }
513
514   return gst_pad_event_default (pad, parent, event);
515 }
516
517 static gboolean
518 gst_hls_demux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
519 {
520   GstHLSDemux *hlsdemux;
521   gboolean ret = FALSE;
522
523   if (query == NULL)
524     return FALSE;
525
526   hlsdemux = GST_HLS_DEMUX (parent);
527
528   switch (query->type) {
529     case GST_QUERY_DURATION:{
530       GstClockTime duration = -1;
531       GstFormat fmt;
532
533       gst_query_parse_duration (query, &fmt, NULL);
534       if (fmt == GST_FORMAT_TIME) {
535         duration = gst_m3u8_client_get_duration (hlsdemux->client);
536         if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0) {
537           gst_query_set_duration (query, GST_FORMAT_TIME, duration);
538           ret = TRUE;
539         }
540       }
541       GST_INFO_OBJECT (hlsdemux, "GST_QUERY_DURATION returns %s with duration %"
542           GST_TIME_FORMAT, ret ? "TRUE" : "FALSE", GST_TIME_ARGS (duration));
543       break;
544     }
545     case GST_QUERY_URI:
546       if (hlsdemux->client) {
547         /* FIXME: Do we answer with the variant playlist, with the current
548          * playlist or the the uri of the least downlowaded fragment? */
549         gst_query_set_uri (query, gst_m3u8_client_get_uri (hlsdemux->client));
550         ret = TRUE;
551       }
552       break;
553     case GST_QUERY_SEEKING:{
554       GstFormat fmt;
555       gint64 stop = -1;
556
557       gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
558       GST_INFO_OBJECT (hlsdemux, "Received GST_QUERY_SEEKING with format %d",
559           fmt);
560       if (fmt == GST_FORMAT_TIME) {
561         GstClockTime duration;
562
563         duration = gst_m3u8_client_get_duration (hlsdemux->client);
564         if (GST_CLOCK_TIME_IS_VALID (duration) && duration > 0)
565           stop = duration;
566
567         gst_query_set_seeking (query, fmt,
568             !gst_m3u8_client_is_live (hlsdemux->client), 0, stop);
569         ret = TRUE;
570         GST_INFO_OBJECT (hlsdemux, "GST_QUERY_SEEKING returning with stop : %"
571             GST_TIME_FORMAT, GST_TIME_ARGS (stop));
572       }
573       break;
574     }
575     default:
576       /* Don't fordward queries upstream because of the special nature of this
577        * "demuxer", which relies on the upstream element only to be fed with the
578        * first playlist */
579       break;
580   }
581
582   return ret;
583 }
584
585 static GstFlowReturn
586 gst_hls_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
587 {
588   GstHLSDemux *demux = GST_HLS_DEMUX (parent);
589
590   if (demux->playlist == NULL)
591     demux->playlist = buf;
592   else
593     demux->playlist = gst_buffer_append (demux->playlist, buf);
594
595   return GST_FLOW_OK;
596 }
597
598 static void
599 gst_hls_demux_pause_tasks (GstHLSDemux * demux, gboolean caching)
600 {
601   if (GST_TASK_STATE (demux->updates_task) != GST_TASK_STOPPED) {
602     demux->cancelled = TRUE;
603     gst_uri_downloader_cancel (demux->downloader);
604     gst_task_pause (demux->updates_task);
605     if (!caching)
606       g_mutex_lock (&demux->updates_timed_lock);
607     GST_TASK_SIGNAL (demux->updates_task);
608     if (!caching)
609       g_mutex_unlock (&demux->updates_timed_lock);
610   }
611
612   if (GST_TASK_STATE (demux->stream_task) != GST_TASK_STOPPED) {
613     demux->stop_stream_task = TRUE;
614     gst_task_pause (demux->stream_task);
615   }
616 }
617
618 static void
619 gst_hls_demux_stop (GstHLSDemux * demux)
620 {
621   gst_uri_downloader_cancel (demux->downloader);
622
623   if (GST_TASK_STATE (demux->updates_task) != GST_TASK_STOPPED) {
624     demux->cancelled = TRUE;
625     gst_uri_downloader_cancel (demux->downloader);
626     gst_task_stop (demux->updates_task);
627     g_mutex_lock (&demux->updates_timed_lock);
628     GST_TASK_SIGNAL (demux->updates_task);
629     g_mutex_unlock (&demux->updates_timed_lock);
630     g_rec_mutex_lock (&demux->updates_lock);
631     g_rec_mutex_unlock (&demux->updates_lock);
632   }
633
634   if (GST_TASK_STATE (demux->stream_task) != GST_TASK_STOPPED) {
635     demux->stop_stream_task = TRUE;
636     gst_task_stop (demux->stream_task);
637     g_rec_mutex_lock (&demux->stream_lock);
638     g_rec_mutex_unlock (&demux->stream_lock);
639   }
640 }
641
642 static void
643 switch_pads (GstHLSDemux * demux, GstCaps * newcaps)
644 {
645   GstPad *oldpad = demux->srcpad;
646   GstEvent *event;
647   gchar *stream_id;
648
649   GST_DEBUG ("Switching pads (oldpad:%p) with caps: %" GST_PTR_FORMAT, oldpad,
650       newcaps);
651
652   /* First create and activate new pad */
653   demux->srcpad = gst_pad_new_from_static_template (&srctemplate, NULL);
654   gst_pad_set_event_function (demux->srcpad,
655       GST_DEBUG_FUNCPTR (gst_hls_demux_src_event));
656   gst_pad_set_query_function (demux->srcpad,
657       GST_DEBUG_FUNCPTR (gst_hls_demux_src_query));
658   gst_pad_set_element_private (demux->srcpad, demux);
659   gst_pad_set_active (demux->srcpad, TRUE);
660
661   stream_id =
662       gst_pad_create_stream_id (demux->srcpad, GST_ELEMENT_CAST (demux), NULL);
663
664   event = gst_pad_get_sticky_event (demux->sinkpad, GST_EVENT_STREAM_START, 0);
665   if (event) {
666     if (gst_event_parse_group_id (event, &demux->group_id))
667       demux->have_group_id = TRUE;
668     else
669       demux->have_group_id = FALSE;
670     gst_event_unref (event);
671   } else if (!demux->have_group_id) {
672     demux->have_group_id = TRUE;
673     demux->group_id = gst_util_group_id_next ();
674   }
675   event = gst_event_new_stream_start (stream_id);
676   if (demux->have_group_id)
677     gst_event_set_group_id (event, demux->group_id);
678
679   gst_pad_push_event (demux->srcpad, event);
680   g_free (stream_id);
681
682   gst_pad_set_caps (demux->srcpad, newcaps);
683
684   gst_element_add_pad (GST_ELEMENT (demux), demux->srcpad);
685
686   gst_element_no_more_pads (GST_ELEMENT (demux));
687
688   if (oldpad) {
689     /* Push out EOS */
690     gst_pad_push_event (oldpad, gst_event_new_eos ());
691     gst_pad_set_active (oldpad, FALSE);
692     gst_element_remove_pad (GST_ELEMENT (demux), oldpad);
693   }
694 }
695
696 static void
697 gst_hls_demux_stream_loop (GstHLSDemux * demux)
698 {
699   GstFragment *fragment;
700   GstBuffer *buf;
701   GstFlowReturn ret;
702   GstCaps *bufcaps, *srccaps = NULL;
703
704   /* Loop for the source pad task. The task is started when we have
705    * received the main playlist from the source element. It tries first to
706    * cache the first fragments and then it waits until it has more data in the
707    * queue. This task is woken up when we push a new fragment to the queue or
708    * when we reached the end of the playlist  */
709   GST_DEBUG_OBJECT (demux, "Enter task");
710
711   if (G_UNLIKELY (demux->need_cache)) {
712     if (!gst_hls_demux_cache_fragments (demux))
713       goto cache_error;
714
715     /* we can start now the updates thread (only if on playing) */
716     gst_task_start (demux->updates_task);
717     GST_INFO_OBJECT (demux, "First fragments cached successfully");
718   }
719
720   if (g_queue_is_empty (demux->queue)) {
721     if (demux->end_of_playlist)
722       goto end_of_playlist;
723
724     goto pause_task;
725   }
726
727   fragment = g_queue_pop_head (demux->queue);
728   buf = gst_fragment_get_buffer (fragment);
729
730   /* Figure out if we need to create/switch pads */
731   if (G_LIKELY (demux->srcpad))
732     srccaps = gst_pad_get_current_caps (demux->srcpad);
733   bufcaps = gst_fragment_get_caps (fragment);
734   if (G_UNLIKELY (!srccaps || !gst_caps_is_equal_fixed (bufcaps, srccaps)
735           || demux->need_segment)) {
736     switch_pads (demux, bufcaps);
737     demux->need_segment = TRUE;
738   }
739   gst_caps_unref (bufcaps);
740   if (G_LIKELY (srccaps))
741     gst_caps_unref (srccaps);
742   g_object_unref (fragment);
743
744   if (demux->need_segment) {
745     GstSegment segment;
746     GstClockTime start = GST_BUFFER_PTS (buf);
747
748     start += demux->position_shift;
749     /* And send a newsegment */
750     GST_DEBUG_OBJECT (demux, "Sending new-segment. segment start:%"
751         GST_TIME_FORMAT, GST_TIME_ARGS (start));
752     gst_segment_init (&segment, GST_FORMAT_TIME);
753     segment.start = start;
754     segment.time = start;
755     gst_pad_push_event (demux->srcpad, gst_event_new_segment (&segment));
756     demux->need_segment = FALSE;
757     demux->position_shift = 0;
758   }
759
760   GST_DEBUG_OBJECT (demux, "Pushing buffer %p", buf);
761
762   ret = gst_pad_push (demux->srcpad, buf);
763   if (ret != GST_FLOW_OK)
764     goto error_pushing;
765
766   GST_DEBUG_OBJECT (demux, "Pushed buffer");
767
768   return;
769
770 end_of_playlist:
771   {
772     GST_DEBUG_OBJECT (demux, "Reached end of playlist, sending EOS");
773     gst_pad_push_event (demux->srcpad, gst_event_new_eos ());
774     gst_hls_demux_pause_tasks (demux, FALSE);
775     return;
776   }
777
778 cache_error:
779   {
780     gst_task_pause (demux->stream_task);
781     if (!demux->cancelled) {
782       GST_ELEMENT_ERROR (demux, RESOURCE, NOT_FOUND,
783           ("Could not cache the first fragments"), (NULL));
784       gst_hls_demux_pause_tasks (demux, FALSE);
785     }
786     return;
787   }
788
789 error_pushing:
790   {
791     if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
792       GST_ELEMENT_ERROR (demux, STREAM, FAILED, (NULL),
793           ("stream stopped, reason %s", gst_flow_get_name (ret)));
794       gst_pad_push_event (demux->srcpad, gst_event_new_eos ());
795     } else {
796       GST_DEBUG_OBJECT (demux, "stream stopped, reason %s",
797           gst_flow_get_name (ret));
798     }
799     gst_hls_demux_pause_tasks (demux, FALSE);
800     return;
801   }
802
803 pause_task:
804   {
805     GST_DEBUG_OBJECT (demux, "Pause task");
806     gst_task_pause (demux->stream_task);
807     return;
808   }
809 }
810
811 static void
812 gst_hls_demux_reset (GstHLSDemux * demux, gboolean dispose)
813 {
814   demux->need_cache = TRUE;
815   demux->end_of_playlist = FALSE;
816   demux->cancelled = FALSE;
817   demux->do_typefind = TRUE;
818
819   if (demux->input_caps) {
820     gst_caps_unref (demux->input_caps);
821     demux->input_caps = NULL;
822   }
823
824   if (demux->playlist) {
825     gst_buffer_unref (demux->playlist);
826     demux->playlist = NULL;
827   }
828
829   if (demux->client) {
830     gst_m3u8_client_free (demux->client);
831     demux->client = NULL;
832   }
833
834   if (!dispose) {
835     demux->client = gst_m3u8_client_new ("");
836   }
837
838   while (!g_queue_is_empty (demux->queue)) {
839     GstFragment *fragment = g_queue_pop_head (demux->queue);
840     g_object_unref (fragment);
841   }
842   g_queue_clear (demux->queue);
843
844   demux->position_shift = 0;
845   demux->need_segment = TRUE;
846
847   demux->have_group_id = FALSE;
848   demux->group_id = G_MAXUINT;
849 }
850
851 static gboolean
852 gst_hls_demux_set_location (GstHLSDemux * demux, const gchar * uri)
853 {
854   if (demux->client)
855     gst_m3u8_client_free (demux->client);
856   demux->client = gst_m3u8_client_new (uri);
857   GST_INFO_OBJECT (demux, "Changed location: %s", uri);
858   return TRUE;
859 }
860
861 void
862 gst_hls_demux_updates_loop (GstHLSDemux * demux)
863 {
864   /* Loop for the updates. It's started when the first fragments are cached and
865    * schedules the next update of the playlist (for lives sources) and the next
866    * update of fragments. When a new fragment is downloaded, it compares the
867    * download time with the next scheduled update to check if we can or should
868    * switch to a different bitrate */
869
870   /* block until the next scheduled update or the signal to quit this thread */
871   g_mutex_lock (&demux->updates_timed_lock);
872   GST_DEBUG_OBJECT (demux, "Started updates task");
873   while (TRUE) {
874     if (demux->cancelled)
875       goto quit;
876
877     /* schedule the next update */
878     gst_hls_demux_schedule (demux);
879
880     /*  block until the next scheduled update or the signal to quit this thread */
881     GST_DEBUG_OBJECT (demux, "Waiting");
882     if (g_cond_timed_wait (GST_TASK_GET_COND (demux->updates_task),
883             &demux->updates_timed_lock, &demux->next_update)) {
884       GST_DEBUG_OBJECT (demux, "Unlocked");
885       goto quit;
886     }
887     GST_DEBUG_OBJECT (demux, "Continue");
888
889     if (demux->cancelled)
890       goto quit;
891
892     /* update the playlist for live sources */
893     if (gst_m3u8_client_is_live (demux->client)) {
894       if (!gst_hls_demux_update_playlist (demux, TRUE)) {
895         if (demux->cancelled)
896           goto quit;
897         demux->client->update_failed_count++;
898         if (demux->client->update_failed_count < DEFAULT_FAILED_COUNT) {
899           GST_WARNING_OBJECT (demux, "Could not update the playlist");
900           continue;
901         } else {
902           GST_ELEMENT_ERROR (demux, RESOURCE, NOT_FOUND,
903               ("Could not update the playlist"), (NULL));
904           goto error;
905         }
906       }
907     }
908
909     /* if it's a live source and the playlist couldn't be updated, there aren't
910      * more fragments in the playlist, so we just wait for the next schedulled
911      * update */
912     if (gst_m3u8_client_is_live (demux->client) &&
913         demux->client->update_failed_count > 0) {
914       GST_WARNING_OBJECT (demux,
915           "The playlist hasn't been updated, failed count is %d",
916           demux->client->update_failed_count);
917       continue;
918     }
919
920     if (demux->cancelled)
921       goto quit;
922
923     /* fetch the next fragment */
924     if (g_queue_is_empty (demux->queue)) {
925       GST_DEBUG_OBJECT (demux, "queue empty, get next fragment");
926       if (!gst_hls_demux_get_next_fragment (demux, FALSE)) {
927         if (demux->cancelled) {
928           goto quit;
929         } else if (!demux->end_of_playlist) {
930           demux->client->update_failed_count++;
931           if (demux->client->update_failed_count < DEFAULT_FAILED_COUNT) {
932             GST_WARNING_OBJECT (demux, "Could not fetch the next fragment");
933             continue;
934           } else {
935             GST_ELEMENT_ERROR (demux, RESOURCE, NOT_FOUND,
936                 ("Could not fetch the next fragment"), (NULL));
937             goto error;
938           }
939         }
940       } else {
941         demux->client->update_failed_count = 0;
942
943         if (demux->cancelled)
944           goto quit;
945
946         /* try to switch to another bitrate if needed */
947         gst_hls_demux_switch_playlist (demux);
948       }
949     }
950   }
951
952 quit:
953   {
954     GST_DEBUG_OBJECT (demux, "Stopped updates task");
955     g_mutex_unlock (&demux->updates_timed_lock);
956     return;
957   }
958
959 error:
960   {
961     GST_DEBUG_OBJECT (demux, "Stopped updates task because of error");
962     gst_hls_demux_pause_tasks (demux, TRUE);
963     g_mutex_unlock (&demux->updates_timed_lock);
964   }
965 }
966
967 static gboolean
968 gst_hls_demux_cache_fragments (GstHLSDemux * demux)
969 {
970   gint i;
971
972   /* If this playlist is a variant playlist, select the first one
973    * and update it */
974   if (gst_m3u8_client_has_variant_playlist (demux->client)) {
975     GstM3U8 *child = NULL;
976
977     if (demux->connection_speed == 0) {
978
979       GST_M3U8_CLIENT_LOCK (demux->client);
980       child = demux->client->main->current_variant->data;
981       GST_M3U8_CLIENT_UNLOCK (demux->client);
982     } else {
983       GList *tmp = gst_m3u8_client_get_playlist_for_bitrate (demux->client,
984           demux->connection_speed);
985
986       child = GST_M3U8 (tmp->data);
987     }
988
989     gst_m3u8_client_set_current (demux->client, child);
990     if (!gst_hls_demux_update_playlist (demux, FALSE)) {
991       GST_ERROR_OBJECT (demux, "Could not fetch the child playlist %s",
992           child->uri);
993       return FALSE;
994     }
995   }
996
997   if (!gst_m3u8_client_is_live (demux->client)) {
998     GstClockTime duration = gst_m3u8_client_get_duration (demux->client);
999
1000     GST_DEBUG_OBJECT (demux, "Sending duration message : %" GST_TIME_FORMAT,
1001         GST_TIME_ARGS (duration));
1002     if (duration != GST_CLOCK_TIME_NONE)
1003       gst_element_post_message (GST_ELEMENT (demux),
1004           gst_message_new_duration_changed (GST_OBJECT (demux)));
1005   }
1006
1007   /* Cache the first fragments */
1008   for (i = 0; i < demux->fragments_cache; i++) {
1009     gst_element_post_message (GST_ELEMENT (demux),
1010         gst_message_new_buffering (GST_OBJECT (demux),
1011             100 * i / demux->fragments_cache));
1012     g_get_current_time (&demux->next_update);
1013     if (!gst_hls_demux_get_next_fragment (demux, TRUE)) {
1014       if (demux->end_of_playlist)
1015         break;
1016       if (!demux->cancelled)
1017         GST_ERROR_OBJECT (demux, "Error caching the first fragments");
1018       return FALSE;
1019     }
1020     /* make sure we stop caching fragments if something cancelled it */
1021     if (demux->cancelled)
1022       return FALSE;
1023     gst_hls_demux_switch_playlist (demux);
1024   }
1025   gst_element_post_message (GST_ELEMENT (demux),
1026       gst_message_new_buffering (GST_OBJECT (demux), 100));
1027
1028   g_get_current_time (&demux->next_update);
1029
1030   demux->need_cache = FALSE;
1031   return TRUE;
1032
1033 }
1034
1035 static gchar *
1036 gst_hls_src_buf_to_utf8_playlist (GstBuffer * buf)
1037 {
1038   GstMapInfo info;
1039   gchar *playlist;
1040
1041   if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1042     goto map_error;
1043
1044   if (!g_utf8_validate ((gchar *) info.data, info.size, NULL))
1045     goto validate_error;
1046
1047   /* alloc size + 1 to end with a null character */
1048   playlist = g_malloc0 (info.size + 1);
1049   memcpy (playlist, info.data, info.size);
1050
1051   gst_buffer_unmap (buf, &info);
1052   gst_buffer_unref (buf);
1053   return playlist;
1054
1055 validate_error:
1056   gst_buffer_unmap (buf, &info);
1057 map_error:
1058   gst_buffer_unref (buf);
1059   return NULL;
1060 }
1061
1062 static gboolean
1063 gst_hls_demux_update_playlist (GstHLSDemux * demux, gboolean update)
1064 {
1065   GstFragment *download;
1066   GstBuffer *buf;
1067   gchar *playlist;
1068   gboolean updated = FALSE;
1069
1070   const gchar *uri = gst_m3u8_client_get_current_uri (demux->client);
1071
1072   download = gst_uri_downloader_fetch_uri (demux->downloader, uri);
1073
1074   if (download == NULL)
1075     return FALSE;
1076
1077   buf = gst_fragment_get_buffer (download);
1078   playlist = gst_hls_src_buf_to_utf8_playlist (buf);
1079   g_object_unref (download);
1080
1081   if (playlist == NULL) {
1082     GST_WARNING_OBJECT (demux, "Couldn't not validate playlist encoding");
1083     return FALSE;
1084   }
1085
1086   updated = gst_m3u8_client_update (demux->client, playlist);
1087
1088   /*  If it's a live source, do not let the sequence number go beyond
1089    * three fragments before the end of the list */
1090   if (updated && update == FALSE && demux->client->current &&
1091       gst_m3u8_client_is_live (demux->client)) {
1092     guint last_sequence;
1093
1094     GST_M3U8_CLIENT_LOCK (demux->client);
1095     last_sequence =
1096         GST_M3U8_MEDIA_FILE (g_list_last (demux->client->current->
1097             files)->data)->sequence;
1098
1099     if (demux->client->sequence >= last_sequence - 3) {
1100       GST_DEBUG_OBJECT (demux, "Sequence is beyond playlist. Moving back to %d",
1101           last_sequence - 3);
1102       demux->need_segment = TRUE;
1103       demux->client->sequence = last_sequence - 3;
1104     }
1105     GST_M3U8_CLIENT_UNLOCK (demux->client);
1106   }
1107
1108   return updated;
1109 }
1110
1111 static gboolean
1112 gst_hls_demux_change_playlist (GstHLSDemux * demux, guint max_bitrate)
1113 {
1114   GList *previous_variant, *current_variant;
1115   gint old_bandwidth, new_bandwidth;
1116
1117   /* If user specifies a connection speed never use a playlist with a bandwidth
1118    * superior than it */
1119   if (demux->connection_speed != 0 && max_bitrate > demux->connection_speed)
1120     max_bitrate = demux->connection_speed;
1121
1122   previous_variant = demux->client->main->current_variant;
1123   current_variant = gst_m3u8_client_get_playlist_for_bitrate (demux->client,
1124       max_bitrate);
1125
1126 retry_failover_protection:
1127   old_bandwidth = GST_M3U8 (previous_variant->data)->bandwidth;
1128   new_bandwidth = GST_M3U8 (current_variant->data)->bandwidth;
1129
1130   /* Don't do anything else if the playlist is the same */
1131   if (new_bandwidth == old_bandwidth) {
1132     return TRUE;
1133   }
1134
1135   demux->client->main->current_variant = current_variant;
1136   GST_M3U8_CLIENT_UNLOCK (demux->client);
1137
1138   gst_m3u8_client_set_current (demux->client, current_variant->data);
1139
1140   GST_INFO_OBJECT (demux, "Client was on %dbps, max allowed is %dbps, switching"
1141       " to bitrate %dbps", old_bandwidth, max_bitrate, new_bandwidth);
1142
1143   if (gst_hls_demux_update_playlist (demux, FALSE)) {
1144     GstStructure *s;
1145
1146     s = gst_structure_new ("playlist",
1147         "uri", G_TYPE_STRING, gst_m3u8_client_get_current_uri (demux->client),
1148         "bitrate", G_TYPE_INT, new_bandwidth, NULL);
1149     gst_element_post_message (GST_ELEMENT_CAST (demux),
1150         gst_message_new_element (GST_OBJECT_CAST (demux), s));
1151   } else {
1152     GList *failover = NULL;
1153
1154     GST_INFO_OBJECT (demux, "Unable to update playlist. Switching back");
1155     GST_M3U8_CLIENT_LOCK (demux->client);
1156
1157     failover = g_list_previous (current_variant);
1158     if (failover && new_bandwidth == GST_M3U8 (failover->data)->bandwidth) {
1159       current_variant = failover;
1160       goto retry_failover_protection;
1161     }
1162
1163     demux->client->main->current_variant = previous_variant;
1164     GST_M3U8_CLIENT_UNLOCK (demux->client);
1165     gst_m3u8_client_set_current (demux->client, previous_variant->data);
1166     /*  Try a lower bitrate (or stop if we just tried the lowest) */
1167     if (new_bandwidth ==
1168         GST_M3U8 (g_list_first (demux->client->main->lists)->data)->bandwidth)
1169       return FALSE;
1170     else
1171       return gst_hls_demux_change_playlist (demux, new_bandwidth - 1);
1172   }
1173
1174   /* Force typefinding since we might have changed media type */
1175   demux->do_typefind = TRUE;
1176
1177   return TRUE;
1178 }
1179
1180 static gboolean
1181 gst_hls_demux_schedule (GstHLSDemux * demux)
1182 {
1183   gfloat update_factor;
1184   gint count;
1185
1186   /* As defined in §6.3.4. Reloading the Playlist file:
1187    * "If the client reloads a Playlist file and finds that it has not
1188    * changed then it MUST wait for a period of time before retrying.  The
1189    * minimum delay is a multiple of the target duration.  This multiple is
1190    * 0.5 for the first attempt, 1.5 for the second, and 3.0 thereafter."
1191    */
1192   count = demux->client->update_failed_count;
1193   if (count < 3)
1194     update_factor = update_interval_factor[count];
1195   else
1196     update_factor = update_interval_factor[3];
1197
1198   /* schedule the next update using the target duration field of the
1199    * playlist */
1200   g_time_val_add (&demux->next_update,
1201       gst_m3u8_client_get_target_duration (demux->client)
1202       / GST_SECOND * G_USEC_PER_SEC * update_factor);
1203   GST_DEBUG_OBJECT (demux, "Next update scheduled at %s",
1204       g_time_val_to_iso8601 (&demux->next_update));
1205
1206   return TRUE;
1207 }
1208
1209 static gboolean
1210 gst_hls_demux_switch_playlist (GstHLSDemux * demux)
1211 {
1212   GTimeVal now;
1213   GstClockTime diff;
1214   gsize size;
1215   gint bitrate;
1216   GstFragment *fragment;
1217   GstBuffer *buffer;
1218
1219   GST_M3U8_CLIENT_LOCK (demux->client);
1220   fragment = g_queue_peek_tail (demux->queue);
1221   if (!demux->client->main->lists || !fragment) {
1222     GST_M3U8_CLIENT_UNLOCK (demux->client);
1223     return TRUE;
1224   }
1225   GST_M3U8_CLIENT_UNLOCK (demux->client);
1226
1227   /* compare the time when the fragment was downloaded with the time when it was
1228    * scheduled */
1229   g_get_current_time (&now);
1230   diff = (GST_TIMEVAL_TO_TIME (now) - GST_TIMEVAL_TO_TIME (demux->next_update));
1231   buffer = gst_fragment_get_buffer (fragment);
1232   size = gst_buffer_get_size (buffer);
1233   bitrate = (size * 8) / ((double) diff / GST_SECOND);
1234
1235   GST_DEBUG ("Downloaded %d bytes in %" GST_TIME_FORMAT ". Bitrate is : %d",
1236       (guint) size, GST_TIME_ARGS (diff), bitrate);
1237
1238   gst_buffer_unref (buffer);
1239   return gst_hls_demux_change_playlist (demux, bitrate * demux->bitrate_limit);
1240 }
1241
1242 static GstFragment *
1243 gst_hls_demux_decrypt_fragment (GstHLSDemux * demux,
1244     GstFragment * encrypted_fragment, const gchar * key, const guint8 * iv)
1245 {
1246   GstFragment *key_fragment, *ret = NULL;
1247   GstBuffer *key_buffer, *encrypted_buffer, *decrypted_buffer;
1248   GstMapInfo key_info, encrypted_info, decrypted_info;
1249   gcry_cipher_hd_t aes_ctx = NULL;
1250   gcry_error_t err = 0;
1251   gsize unpadded_size;
1252
1253   GST_INFO_OBJECT (demux, "Fetching key %s", key);
1254   key_fragment = gst_uri_downloader_fetch_uri (demux->downloader, key);
1255   if (key_fragment == NULL)
1256     goto key_failed;
1257
1258   key_buffer = gst_fragment_get_buffer (key_fragment);
1259   encrypted_buffer = gst_fragment_get_buffer (encrypted_fragment);
1260   decrypted_buffer =
1261       gst_buffer_new_allocate (NULL, gst_buffer_get_size (encrypted_buffer),
1262       NULL);
1263
1264   gst_buffer_map (key_buffer, &key_info, GST_MAP_READ);
1265   gst_buffer_map (encrypted_buffer, &encrypted_info, GST_MAP_READ);
1266   gst_buffer_map (decrypted_buffer, &decrypted_info, GST_MAP_WRITE);
1267
1268   err =
1269       gcry_cipher_open (&aes_ctx, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
1270   if (err)
1271     goto gcry_error;
1272   err = gcry_cipher_setkey (aes_ctx, key_info.data, 16);
1273   if (err)
1274     goto gcry_error;
1275   err = gcry_cipher_setiv (aes_ctx, iv, 16);
1276   if (err)
1277     goto gcry_error;
1278   err = gcry_cipher_decrypt (aes_ctx, decrypted_info.data, decrypted_info.size,
1279       encrypted_info.data, encrypted_info.size);
1280   if (err)
1281     goto gcry_error;
1282   gcry_cipher_close (aes_ctx);
1283
1284   /* Handle pkcs7 unpadding here */
1285   unpadded_size =
1286       decrypted_info.size - decrypted_info.data[decrypted_info.size - 1];
1287
1288   gst_buffer_unmap (decrypted_buffer, &decrypted_info);
1289   gst_buffer_unmap (encrypted_buffer, &encrypted_info);
1290   gst_buffer_unmap (key_buffer, &key_info);
1291
1292   gst_buffer_resize (decrypted_buffer, 0, unpadded_size);
1293
1294   gst_buffer_unref (key_buffer);
1295   gst_buffer_unref (encrypted_buffer);
1296   g_object_unref (key_fragment);
1297
1298   ret = gst_fragment_new ();
1299   gst_fragment_add_buffer (ret, decrypted_buffer);
1300   ret->completed = TRUE;
1301 key_failed:
1302   g_object_unref (encrypted_fragment);
1303   return ret;
1304
1305 gcry_error:
1306   GST_ERROR_OBJECT (demux, "Failed to decrypt fragment: %s",
1307       gpg_strerror (err));
1308
1309   if (aes_ctx)
1310     gcry_cipher_close (aes_ctx);
1311
1312   gst_buffer_unref (key_buffer);
1313   gst_buffer_unref (encrypted_buffer);
1314   gst_buffer_unref (decrypted_buffer);
1315
1316   gst_buffer_unmap (decrypted_buffer, &decrypted_info);
1317   gst_buffer_unmap (encrypted_buffer, &encrypted_info);
1318   gst_buffer_unmap (key_buffer, &key_info);
1319
1320   g_object_unref (encrypted_fragment);
1321   return ret;
1322 }
1323
1324 static gboolean
1325 gst_hls_demux_get_next_fragment (GstHLSDemux * demux, gboolean caching)
1326 {
1327   GstFragment *download;
1328   const gchar *next_fragment_uri;
1329   GstClockTime duration;
1330   GstClockTime timestamp;
1331   GstBuffer *buf;
1332   gboolean discont;
1333   const gchar *key = NULL;
1334   const guint8 *iv = NULL;
1335
1336   if (!gst_m3u8_client_get_next_fragment (demux->client, &discont,
1337           &next_fragment_uri, &duration, &timestamp, &key, &iv)) {
1338     GST_INFO_OBJECT (demux, "This playlist doesn't contain more fragments");
1339     demux->end_of_playlist = TRUE;
1340     gst_task_start (demux->stream_task);
1341     return FALSE;
1342   }
1343
1344   GST_INFO_OBJECT (demux, "Fetching next fragment %s", next_fragment_uri);
1345
1346   download = gst_uri_downloader_fetch_uri (demux->downloader,
1347       next_fragment_uri);
1348
1349   if (download && key)
1350     download = gst_hls_demux_decrypt_fragment (demux, download, key, iv);
1351
1352   if (download == NULL)
1353     goto error;
1354
1355   buf = gst_fragment_get_buffer (download);
1356
1357   GST_BUFFER_DURATION (buf) = duration;
1358   GST_BUFFER_PTS (buf) = timestamp;
1359
1360   /* We actually need to do this every time we switch bitrate */
1361   if (G_UNLIKELY (demux->do_typefind)) {
1362     GstCaps *caps = gst_fragment_get_caps (download);
1363
1364     if (!demux->input_caps || !gst_caps_is_equal (caps, demux->input_caps)) {
1365       gst_caps_replace (&demux->input_caps, caps);
1366       /* gst_pad_set_caps (demux->srcpad, demux->input_caps); */
1367       GST_INFO_OBJECT (demux, "Input source caps: %" GST_PTR_FORMAT,
1368           demux->input_caps);
1369       demux->do_typefind = FALSE;
1370     }
1371     gst_caps_unref (caps);
1372   } else {
1373     gst_fragment_set_caps (download, demux->input_caps);
1374   }
1375
1376   if (discont) {
1377     GST_DEBUG_OBJECT (demux, "Marking fragment as discontinuous");
1378     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1379   }
1380
1381   /* The buffer ref is still kept inside the fragment download */
1382   gst_buffer_unref (buf);
1383
1384   GST_DEBUG_OBJECT (demux, "Pushing fragment in queue");
1385   g_queue_push_tail (demux->queue, download);
1386   if (!caching) {
1387     GST_TASK_SIGNAL (demux->updates_task);
1388     gst_task_start (demux->stream_task);
1389   }
1390   return TRUE;
1391
1392 error:
1393   {
1394     return FALSE;
1395   }
1396 }