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