]> git.sesse.net Git - vlc/blob - modules/stream_filter/httplive.c
vlc_plugin: allow overriding module meta-infos
[vlc] / modules / stream_filter / httplive.c
1 /*****************************************************************************
2  * httplive.c: HTTP Live Streaming stream filter
3  *****************************************************************************
4  * Copyright (C) 2010-2012 M2X BV
5  * $Id$
6  *
7  * Author: Jean-Paul Saman <jpsaman _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <limits.h>
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <gcrypt.h>
39
40 #include <vlc_threads.h>
41 #include <vlc_arrays.h>
42 #include <vlc_stream.h>
43 #include <vlc_memory.h>
44 #include <vlc_gcrypt.h>
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  Open (vlc_object_t *);
50 static void Close(vlc_object_t *);
51
52 vlc_module_begin()
53     set_category(CAT_INPUT)
54     set_subcategory(SUBCAT_INPUT_STREAM_FILTER)
55     set_description(N_("Http Live Streaming stream filter"))
56     set_capability("stream_filter", 20)
57     set_callbacks(Open, Close)
58 vlc_module_end()
59
60 /*****************************************************************************
61  *
62  *****************************************************************************/
63 #define AES_BLOCK_SIZE 16 /* Only support AES-128 */
64 typedef struct segment_s
65 {
66     int         sequence;   /* unique sequence number */
67     int         duration;   /* segment duration (seconds) */
68     uint64_t    size;       /* segment size in bytes */
69     uint64_t    bandwidth;  /* bandwidth usage of segments (bits per second)*/
70
71     char        *url;
72     char       *psz_key_path;         /* url key path */
73     uint8_t     aes_key[16];      /* AES-128 */
74     bool        b_key_loaded;
75     uint8_t     psz_AES_IV[AES_BLOCK_SIZE];    /* IV used when decypher the block */
76     bool        b_iv_loaded;
77
78     vlc_mutex_t lock;
79     block_t     *data;      /* data */
80 } segment_t;
81
82 typedef struct hls_stream_s
83 {
84     int         id;         /* program id */
85     int         version;    /* protocol version should be 1 */
86     int         sequence;   /* media sequence number */
87     int         duration;   /* maximum duration per segment (s) */
88     int         max_segment_length;   /* maximum duration segments */
89     uint64_t    bandwidth;  /* bandwidth usage of segments (bits per second)*/
90     uint64_t    size;       /* stream length is calculated by taking the sum
91                                foreach segment of (segment->duration * hls->bandwidth/8) */
92
93     vlc_array_t *segments;  /* list of segments */
94     char        *url;        /* uri to m3u8 */
95     vlc_mutex_t lock;
96     bool        b_cache;    /* allow caching */
97
98     char        *psz_current_key_path;          /* URL path of the encrypted key */
99     uint8_t      psz_current_AES_IV[AES_BLOCK_SIZE];    /* IV used when decypher the block */
100     bool         b_current_iv_loaded;
101 } hls_stream_t;
102
103 struct stream_sys_t
104 {
105     char         *m3u8;         /* M3U8 url */
106     vlc_thread_t  reload;       /* HLS m3u8 reload thread */
107     vlc_thread_t  thread;       /* HLS segment download thread */
108
109     block_t      *peeked;
110
111     /* */
112     vlc_array_t  *hls_stream;   /* bandwidth adaptation */
113     uint64_t      bandwidth;    /* measured bandwidth (bits per second) */
114
115     /* Download */
116     struct hls_download_s
117     {
118         int         stream;     /* current hls_stream  */
119         int         segment;    /* current segment for downloading */
120         int         seek;       /* segment requested by seek (default -1) */
121         vlc_mutex_t lock_wait;  /* protect segment download counter */
122         vlc_cond_t  wait;       /* some condition to wait on */
123     } download;
124
125     /* Playback */
126     struct hls_playback_s
127     {
128         uint64_t    offset;     /* current offset in media */
129         int         stream;     /* current hls_stream  */
130         int         segment;    /* current segment for playback */
131     } playback;
132
133     /* Playlist */
134     struct hls_playlist_s
135     {
136         mtime_t     last;       /* playlist last loaded */
137         mtime_t     wakeup;     /* next reload time */
138         int         tries;      /* times it was not changed */
139     } playlist;
140
141     struct hls_read_s
142     {
143         vlc_mutex_t lock_wait;  /* used by read condition variable */
144         vlc_cond_t  wait;       /* some condition to wait on during read */
145     } read;
146
147     /* state */
148     bool        b_cache;    /* can cache files */
149     bool        b_meta;     /* meta playlist */
150     bool        b_live;     /* live stream? or vod? */
151     bool        b_error;    /* parsing error */
152     bool        b_aesmsg;   /* only print one time that the media is encrypted */
153
154     /* Shared data */
155     vlc_cond_t   wait;
156     vlc_mutex_t  lock;
157     bool         paused;
158 };
159
160 /****************************************************************************
161  * Local prototypes
162  ****************************************************************************/
163 static int  Read   (stream_t *, void *p_read, unsigned int i_read);
164 static int  Peek   (stream_t *, const uint8_t **pp_peek, unsigned int i_peek);
165 static int  Control(stream_t *, int i_query, va_list);
166
167 static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer);
168 static ssize_t read_M3U8_from_url(stream_t *s, const char *psz_url, uint8_t **buffer);
169 static char *ReadLine(uint8_t *buffer, uint8_t **pos, size_t len);
170
171 static int hls_Download(stream_t *s, segment_t *segment);
172
173 static void* hls_Thread(void *);
174 static void* hls_Reload(void *);
175
176 static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted);
177 static void segment_Free(segment_t *segment);
178
179 /****************************************************************************
180  *
181  ****************************************************************************/
182 static bool isHTTPLiveStreaming(stream_t *s)
183 {
184     const uint8_t *peek;
185
186     int size = stream_Peek(s->p_source, &peek, 46);
187     if (size < 7)
188         return false;
189
190     if (memcmp(peek, "#EXTM3U", 7) != 0)
191         return false;
192
193     peek += 7;
194     size -= 7;
195
196     /* Parse stream and search for
197      * EXT-X-TARGETDURATION or EXT-X-STREAM-INF tag, see
198      * http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8 */
199     while (size--)
200     {
201         static const char *const ext[] = {
202             "TARGETDURATION",
203             "MEDIA-SEQUENCE",
204             "KEY",
205             "ALLOW-CACHE",
206             "ENDLIST",
207             "STREAM-INF",
208             "DISCONTINUITY",
209             "VERSION"
210         };
211
212         if (*peek++ != '#')
213             continue;
214
215         if (size < 6)
216             continue;
217
218         if (memcmp(peek, "EXT-X-", 6))
219             continue;
220
221         peek += 6;
222         size -= 6;
223
224         for (size_t i = 0; i < ARRAY_SIZE(ext); i++)
225         {
226             size_t len = strlen(ext[i]);
227             if (size < 0 || (size_t)size < len)
228                 continue;
229             if (!memcmp(peek, ext[i], len))
230                 return true;
231         }
232     }
233
234     return false;
235 }
236
237 /* HTTP Live Streaming */
238 static hls_stream_t *hls_New(vlc_array_t *hls_stream, const int id, const uint64_t bw, const char *uri)
239 {
240     hls_stream_t *hls = (hls_stream_t *)malloc(sizeof(hls_stream_t));
241     if (hls == NULL) return NULL;
242
243     hls->id = id;
244     hls->bandwidth = bw;
245     hls->duration = -1;/* unknown */
246     hls->max_segment_length = -1;/* unknown */
247     hls->size = 0;
248     hls->sequence = 0; /* default is 0 */
249     hls->version = 1;  /* default protocol version */
250     hls->b_cache = true;
251     hls->url = strdup(uri);
252     if (hls->url == NULL)
253     {
254         free(hls);
255         return NULL;
256     }
257     hls->psz_current_key_path = NULL;
258     hls->segments = vlc_array_new();
259     vlc_array_append(hls_stream, hls);
260     vlc_mutex_init(&hls->lock);
261     return hls;
262 }
263
264 static void hls_Free(hls_stream_t *hls)
265 {
266     vlc_mutex_destroy(&hls->lock);
267
268     if (hls->segments)
269     {
270         for (int n = 0; n < vlc_array_count(hls->segments); n++)
271         {
272             segment_t *segment = segment_GetSegment(hls, n);
273             if (segment) segment_Free(segment);
274         }
275         vlc_array_destroy(hls->segments);
276     }
277     free(hls->url);
278     free(hls->psz_current_key_path);
279     free(hls);
280 }
281
282 static hls_stream_t *hls_Copy(hls_stream_t *src, const bool b_cp_segments)
283 {
284     assert(src);
285     assert(!b_cp_segments); /* FIXME: copying segments is not implemented */
286
287     hls_stream_t *dst = (hls_stream_t *)malloc(sizeof(hls_stream_t));
288     if (dst == NULL) return NULL;
289
290     dst->id = src->id;
291     dst->bandwidth = src->bandwidth;
292     dst->duration = src->duration;
293     dst->size = src->size;
294     dst->sequence = src->sequence;
295     dst->version = src->version;
296     dst->b_cache = src->b_cache;
297     dst->psz_current_key_path = src->psz_current_key_path ?
298                 strdup( src->psz_current_key_path ) : NULL;
299     memcpy(dst->psz_current_AES_IV, src->psz_current_AES_IV, AES_BLOCK_SIZE);
300     dst->b_current_iv_loaded = src->b_current_iv_loaded;
301     dst->url = strdup(src->url);
302     if (dst->url == NULL)
303     {
304         free(dst);
305         return NULL;
306     }
307     if (!b_cp_segments)
308         dst->segments = vlc_array_new();
309     vlc_mutex_init(&dst->lock);
310     return dst;
311 }
312
313 static hls_stream_t *hls_Get(vlc_array_t *hls_stream, const int wanted)
314 {
315     int count = vlc_array_count(hls_stream);
316     if (count <= 0)
317         return NULL;
318     if ((wanted < 0) || (wanted >= count))
319         return NULL;
320     return (hls_stream_t *) vlc_array_item_at_index(hls_stream, wanted);
321 }
322
323 static inline hls_stream_t *hls_GetFirst(vlc_array_t *hls_stream)
324 {
325     return hls_Get(hls_stream, 0);
326 }
327
328 static hls_stream_t *hls_GetLast(vlc_array_t *hls_stream)
329 {
330     int count = vlc_array_count(hls_stream);
331     if (count <= 0)
332         return NULL;
333     count--;
334     return hls_Get(hls_stream, count);
335 }
336
337 static hls_stream_t *hls_Find(vlc_array_t *hls_stream, hls_stream_t *hls_new)
338 {
339     int count = vlc_array_count(hls_stream);
340     for (int n = 0; n < count; n++)
341     {
342         hls_stream_t *hls = hls_Get(hls_stream, n);
343         if (hls)
344         {
345             /* compare */
346             if ((hls->id == hls_new->id) &&
347                 ((hls->bandwidth == hls_new->bandwidth)||(hls_new->bandwidth==0)))
348                 return hls;
349         }
350     }
351     return NULL;
352 }
353
354 static uint64_t hls_GetStreamSize(hls_stream_t *hls)
355 {
356     /* NOTE: Stream size is calculated based on segment duration and
357      * HLS stream bandwidth from the .m3u8 file. If these are not correct
358      * then the deviation from exact byte size will be big and the seek/
359      * progressbar will not behave entirely as one expects. */
360     uint64_t size = 0UL;
361
362     /* If there is no valid bandwidth yet, then there is no point in
363      * computing stream size. */
364     if (hls->bandwidth == 0)
365         return size;
366
367     int count = vlc_array_count(hls->segments);
368     for (int n = 0; n < count; n++)
369     {
370         segment_t *segment = segment_GetSegment(hls, n);
371         if (segment)
372         {
373             size += (segment->duration * (hls->bandwidth / 8));
374         }
375     }
376     return size;
377 }
378
379 /* Segment */
380 static segment_t *segment_New(hls_stream_t* hls, const int duration, const char *uri)
381 {
382     segment_t *segment = (segment_t *)malloc(sizeof(segment_t));
383     if (segment == NULL)
384         return NULL;
385
386     segment->duration = duration; /* seconds */
387     segment->size = 0; /* bytes */
388     segment->sequence = 0;
389     segment->bandwidth = 0;
390     segment->url = strdup(uri);
391     if (segment->url == NULL)
392     {
393         free(segment);
394         return NULL;
395     }
396     segment->data = NULL;
397     vlc_array_append(hls->segments, segment);
398     vlc_mutex_init(&segment->lock);
399     segment->b_key_loaded = false;
400     segment->psz_key_path = NULL;
401     if (hls->psz_current_key_path)
402         segment->psz_key_path = strdup(hls->psz_current_key_path);
403     segment->b_iv_loaded = hls->b_current_iv_loaded;
404     memcpy(segment->psz_AES_IV, hls->psz_current_AES_IV, AES_BLOCK_SIZE);
405     return segment;
406 }
407
408 static void segment_Free(segment_t *segment)
409 {
410     vlc_mutex_destroy(&segment->lock);
411
412     free(segment->url);
413     free(segment->psz_key_path);
414     if (segment->data)
415         block_Release(segment->data);
416     free(segment);
417 }
418
419 static segment_t *segment_GetSegment(hls_stream_t *hls, const int wanted)
420 {
421     assert(hls);
422
423     int count = vlc_array_count(hls->segments);
424     if (count <= 0)
425         return NULL;
426     if ((wanted < 0) || (wanted >= count))
427         return NULL;
428     return (segment_t *) vlc_array_item_at_index(hls->segments, wanted);
429 }
430
431 static segment_t *segment_Find(hls_stream_t *hls, const int sequence)
432 {
433     assert(hls);
434
435     int count = vlc_array_count(hls->segments);
436     if (count <= 0) return NULL;
437     for (int n = 0; n < count; n++)
438     {
439         segment_t *segment = segment_GetSegment(hls, n);
440         if (segment == NULL) break;
441         if (segment->sequence == sequence)
442             return segment;
443     }
444     return NULL;
445 }
446
447 static int ChooseSegment(stream_t *s, const int current)
448 {
449     stream_sys_t *p_sys = (stream_sys_t *)s->p_sys;
450     hls_stream_t *hls = hls_Get(p_sys->hls_stream, current);
451     if (hls == NULL) return 0;
452
453     /* Choose a segment to start which is no closer than
454      * 3 times the target duration from the end of the playlist.
455      */
456     int wanted = 0;
457     int duration = 0;
458     int sequence = 0;
459     int count = vlc_array_count(hls->segments);
460     int i = p_sys->b_live ? count - 1 : -1;
461
462     /* We do while loop only with live case, otherwise return 0*/
463     while((i >= 0) && (i < count))
464     {
465         segment_t *segment = segment_GetSegment(hls, i);
466         assert(segment);
467
468         if (segment->duration > hls->duration)
469         {
470             msg_Err(s, "EXTINF:%d duration is larger than EXT-X-TARGETDURATION:%d",
471                     segment->duration, hls->duration);
472         }
473
474         duration += segment->duration;
475         if (duration >= 3 * hls->duration)
476         {
477             /* Start point found */
478             wanted = i;
479             sequence = segment->sequence;
480             break;
481         }
482
483         i-- ;
484     }
485
486     msg_Dbg(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
487     return wanted;
488 }
489
490 /* Parsing */
491 static char *parse_Attributes(const char *line, const char *attr)
492 {
493     char *p;
494     char *begin = (char *) line;
495     char *end = begin + strlen(line);
496
497     /* Find start of attributes */
498     if ((p = strchr(begin, ':' )) == NULL)
499         return NULL;
500
501     begin = p;
502     do
503     {
504         if (strncasecmp(begin, attr, strlen(attr)) == 0
505           && begin[strlen(attr)] == '=')
506         {
507             /* <attr>="<value>"[,]* */
508             p = strchr(begin, ',');
509             begin += strlen(attr) + 1;
510
511             /* Check if we have " " marked value*/
512             if( begin[0] == '"' )
513             {
514                 char *valueend = strchr( begin+1, '"');
515
516                 /* No ending " so bail out */
517                 if( unlikely( !valueend ) )
518                     return NULL;
519
520                 p = strchr( valueend, ',');
521             }
522             if (begin >= end)
523                 return NULL;
524             if (p == NULL) /* last attribute */
525                 return strndup(begin, end - begin);
526             /* copy till ',' */
527             return strndup(begin, p - begin);
528         }
529         begin++;
530     } while(begin < end);
531
532     return NULL;
533 }
534
535 static int string_to_IV(char *string_hexa, uint8_t iv[AES_BLOCK_SIZE])
536 {
537     unsigned long long iv_hi, iv_lo;
538     char *end = NULL;
539     if (*string_hexa++ != '0')
540         return VLC_EGENERIC;
541     if (*string_hexa != 'x' && *string_hexa != 'X')
542         return VLC_EGENERIC;
543
544     string_hexa++;
545
546     size_t len = strlen(string_hexa);
547     if (len <= 16) {
548         iv_hi = 0;
549         iv_lo = strtoull(string_hexa, &end, 16);
550         if (*end)
551             return VLC_EGENERIC;
552     } else {
553         iv_lo = strtoull(&string_hexa[len-16], &end, 16);
554         if (*end)
555             return VLC_EGENERIC;
556         string_hexa[len-16] = '\0';
557         iv_hi = strtoull(string_hexa, &end, 16);
558         if (*end)
559             return VLC_EGENERIC;
560     }
561
562     for (int i = 7; i >= 0 ; --i) {
563         iv[  i] = iv_hi & 0xff;
564         iv[8+i] = iv_lo & 0xff;
565         iv_hi >>= 8;
566         iv_lo >>= 8;
567     }
568
569     return VLC_SUCCESS;
570 }
571
572 static char *relative_URI(const char *psz_url, const char *psz_path)
573 {
574     char *ret = NULL;
575     const char *fmt;
576     assert(psz_url != NULL && psz_path != NULL);
577
578
579     //If the path is actually an absolute URL, don't do anything.
580     if (strncmp(psz_path, "http", 4) == 0)
581         return NULL;
582
583     size_t len = strlen(psz_path);
584
585     char *new_url = strdup(psz_url);
586     if (unlikely(!new_url))
587         return NULL;
588
589     if( psz_path[0] == '/' ) //Relative URL with absolute path
590     {
591         //Try to find separator for name and path, try to skip
592         //access and first ://
593         char *slash = strchr(&new_url[8], '/');
594         if (unlikely(slash == NULL))
595             goto end;
596         *slash = '\0';
597         fmt = "%s%s";
598     } else {
599         int levels = 0;
600         while(len >= 3 && !strncmp(psz_path, "../", 3)) {
601             psz_path += 3;
602             len -= 3;
603             levels++;
604         }
605         do {
606             char *slash = strrchr(new_url, '/');
607             if (unlikely(slash == NULL))
608                 goto end;
609             *slash = '\0';
610         } while (levels--);
611         fmt = "%s/%s";
612     }
613
614     if (asprintf(&ret, fmt, new_url, psz_path) < 0)
615         ret = NULL;
616
617 end:
618     free(new_url);
619     return ret;
620 }
621
622 static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *duration)
623 {
624     assert(hls);
625     assert(p_read);
626
627     /* strip of #EXTINF: */
628     char *p_next = NULL;
629     char *token = strtok_r(p_read, ":", &p_next);
630     if (token == NULL)
631         return VLC_EGENERIC;
632
633     /* read duration */
634     token = strtok_r(NULL, ",", &p_next);
635     if (token == NULL)
636         return VLC_EGENERIC;
637
638     int value;
639     char *endptr;
640     if (hls->version < 3)
641     {
642         errno = 0;
643         value = strtol(token, &endptr, 10);
644         if (token == endptr || errno == ERANGE)
645         {
646             *duration = -1;
647             return VLC_EGENERIC;
648         }
649         *duration = value;
650     }
651     else
652     {
653         errno = 0;
654         double d = strtof(token, &endptr);
655         if (token == endptr || errno == ERANGE)
656         {
657             *duration = -1;
658             return VLC_EGENERIC;
659         }
660         if ((d) - ((int)d) >= 0.5)
661             value = ((int)d) + 1;
662         else
663             value = ((int)d);
664         *duration = value;
665     }
666     if( *duration > hls->max_segment_length)
667         hls->max_segment_length = *duration;
668
669     /* Ignore the rest of the line */
670     return VLC_SUCCESS;
671 }
672
673 static int parse_AddSegment(hls_stream_t *hls, const int duration, const char *uri)
674 {
675     assert(hls);
676     assert(uri);
677
678     /* Store segment information */
679     vlc_mutex_lock(&hls->lock);
680
681     char *psz_uri = relative_URI(hls->url, uri);
682
683     segment_t *segment = segment_New(hls, duration, psz_uri ? psz_uri : uri);
684     if (segment)
685         segment->sequence = hls->sequence + vlc_array_count(hls->segments) - 1;
686     free(psz_uri);
687
688     vlc_mutex_unlock(&hls->lock);
689
690     return segment ? VLC_SUCCESS : VLC_ENOMEM;
691 }
692
693 static int parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
694 {
695     assert(hls);
696
697     int duration = -1;
698     int ret = sscanf(p_read, "#EXT-X-TARGETDURATION:%d", &duration);
699     if (ret != 1)
700     {
701         msg_Err(s, "expected #EXT-X-TARGETDURATION:<s>");
702         return VLC_EGENERIC;
703     }
704
705     hls->duration = duration; /* seconds */
706     return VLC_SUCCESS;
707 }
708
709 static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
710                                    hls_stream_t **hls, char *p_read, const char *uri)
711 {
712     int id;
713     uint64_t bw;
714     char *attr;
715
716     assert(*hls == NULL);
717
718     attr = parse_Attributes(p_read, "PROGRAM-ID");
719     if (attr)
720     {
721         id = atol(attr);
722         free(attr);
723     }
724     else
725         id = 0;
726
727     attr = parse_Attributes(p_read, "BANDWIDTH");
728     if (attr == NULL)
729     {
730         msg_Err(s, "#EXT-X-STREAM-INF: expected BANDWIDTH=<value>");
731         return VLC_EGENERIC;
732     }
733     bw = atoll(attr);
734     free(attr);
735
736     if (bw == 0)
737     {
738         msg_Err(s, "#EXT-X-STREAM-INF: bandwidth cannot be 0");
739         return VLC_EGENERIC;
740     }
741
742     msg_Dbg(s, "bandwidth adaptation detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
743
744     char *psz_uri = relative_URI(s->p_sys->m3u8, uri);
745
746     *hls = hls_New(*hls_stream, id, bw, psz_uri ? psz_uri : uri);
747
748     free(psz_uri);
749
750     return (*hls == NULL) ? VLC_ENOMEM : VLC_SUCCESS;
751 }
752
753 static int parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
754 {
755     assert(hls);
756
757     int sequence;
758     int ret = sscanf(p_read, "#EXT-X-MEDIA-SEQUENCE:%d", &sequence);
759     if (ret != 1)
760     {
761         msg_Err(s, "expected #EXT-X-MEDIA-SEQUENCE:<s>");
762         return VLC_EGENERIC;
763     }
764
765     if (hls->sequence > 0)
766     {
767         if (s->p_sys->b_live)
768         {
769             hls_stream_t *last = hls_GetLast(s->p_sys->hls_stream);
770             segment_t *last_segment = segment_GetSegment( last, vlc_array_count( last->segments ) - 1 );
771             if ( ( last_segment->sequence < sequence) &&
772                  ( sequence - last_segment->sequence >= 1 ))
773                 msg_Err(s, "EXT-X-MEDIA-SEQUENCE gap in playlist (new=%d, old=%d)",
774                             sequence, last_segment->sequence);
775         }
776         else
777             msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist (new=%d, old=%d)",
778                         sequence, hls->sequence);
779     }
780     hls->sequence = sequence;
781     return VLC_SUCCESS;
782 }
783
784 static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
785 {
786     assert(hls);
787
788     /* #EXT-X-KEY:METHOD=<method>[,URI="<URI>"][,IV=<IV>] */
789     int err = VLC_SUCCESS;
790     char *attr = parse_Attributes(p_read, "METHOD");
791     if (attr == NULL)
792     {
793         msg_Err(s, "#EXT-X-KEY: expected METHOD=<value>");
794         return err;
795     }
796
797     if (strncasecmp(attr, "NONE", 4) == 0)
798     {
799         char *uri = parse_Attributes(p_read, "URI");
800         if (uri != NULL)
801         {
802             msg_Err(s, "#EXT-X-KEY: URI not expected");
803             err = VLC_EGENERIC;
804         }
805         free(uri);
806         /* IV is only supported in version 2 and above */
807         if (hls->version >= 2)
808         {
809             char *iv = parse_Attributes(p_read, "IV");
810             if (iv != NULL)
811             {
812                 msg_Err(s, "#EXT-X-KEY: IV not expected");
813                 err = VLC_EGENERIC;
814             }
815             free(iv);
816         }
817     }
818     else if (strncasecmp(attr, "AES-128", 7) == 0)
819     {
820         char *value, *uri, *iv;
821         if (s->p_sys->b_aesmsg == false)
822         {
823             msg_Dbg(s, "playback of AES-128 encrypted HTTP Live media detected.");
824             s->p_sys->b_aesmsg = true;
825         }
826         value = uri = parse_Attributes(p_read, "URI");
827         if (value == NULL)
828         {
829             msg_Err(s, "#EXT-X-KEY: URI not found for encrypted HTTP Live media in AES-128");
830             free(attr);
831             return VLC_EGENERIC;
832         }
833
834         /* Url is put between quotes, remove them */
835         if (*value == '"')
836         {
837             /* We need to strip the "" from the attribute value */
838             uri = value + 1;
839             char* end = strchr(uri, '"');
840             if (end != NULL)
841                 *end = 0;
842         }
843         /* For absolute URI, just duplicate it
844          * don't limit to HTTP, maybe some sanity checking
845          * should be done more in here? */
846         if( strstr( uri , "://" ) )
847             hls->psz_current_key_path = strdup( uri );
848         else
849             hls->psz_current_key_path = relative_URI(hls->url, uri);
850         free(value);
851
852         value = iv = parse_Attributes(p_read, "IV");
853         if (iv == NULL)
854         {
855             /*
856             * If the EXT-X-KEY tag does not have the IV attribute, implementations
857             * MUST use the sequence number of the media file as the IV when
858             * encrypting or decrypting that media file.  The big-endian binary
859             * representation of the sequence number SHALL be placed in a 16-octet
860             * buffer and padded (on the left) with zeros.
861             */
862             hls->b_current_iv_loaded = false;
863         }
864         else
865         {
866             /*
867             * If the EXT-X-KEY tag has the IV attribute, implementations MUST use
868             * the attribute value as the IV when encrypting or decrypting with that
869             * key.  The value MUST be interpreted as a 128-bit hexadecimal number
870             * and MUST be prefixed with 0x or 0X.
871             */
872
873             if (string_to_IV(iv, hls->psz_current_AES_IV) == VLC_EGENERIC)
874             {
875                 msg_Err(s, "IV invalid");
876                 err = VLC_EGENERIC;
877             }
878             else
879                 hls->b_current_iv_loaded = true;
880             free(value);
881         }
882     }
883     else
884     {
885         msg_Warn(s, "playback of encrypted HTTP Live media is not supported.");
886         err = VLC_EGENERIC;
887     }
888     free(attr);
889     return err;
890 }
891
892 static int parse_ProgramDateTime(stream_t *s, hls_stream_t *hls, char *p_read)
893 {
894     VLC_UNUSED(hls);
895     msg_Dbg(s, "tag not supported: #EXT-X-PROGRAM-DATE-TIME %s", p_read);
896     return VLC_SUCCESS;
897 }
898
899 static int parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read)
900 {
901     assert(hls);
902
903     char answer[4] = "\0";
904     int ret = sscanf(p_read, "#EXT-X-ALLOW-CACHE:%3s", answer);
905     if (ret != 1)
906     {
907         msg_Err(s, "#EXT-X-ALLOW-CACHE, ignoring ...");
908         return VLC_EGENERIC;
909     }
910
911     hls->b_cache = (strncmp(answer, "NO", 2) != 0);
912     return VLC_SUCCESS;
913 }
914
915 static int parse_Version(stream_t *s, hls_stream_t *hls, char *p_read)
916 {
917     assert(hls);
918
919     int version;
920     int ret = sscanf(p_read, "#EXT-X-VERSION:%d", &version);
921     if (ret != 1)
922     {
923         msg_Err(s, "#EXT-X-VERSION: no protocol version found, should be version 1.");
924         return VLC_EGENERIC;
925     }
926
927     /* Check version */
928     hls->version = version;
929     if (hls->version <= 0 || hls->version > 3)
930     {
931         msg_Err(s, "#EXT-X-VERSION should be version 1, 2 or 3 iso %d", version);
932         return VLC_EGENERIC;
933     }
934     return VLC_SUCCESS;
935 }
936
937 static int parse_EndList(stream_t *s, hls_stream_t *hls)
938 {
939     assert(hls);
940
941     s->p_sys->b_live = false;
942     msg_Dbg(s, "video on demand (vod) mode");
943     return VLC_SUCCESS;
944 }
945
946 static int parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
947 {
948     assert(hls);
949
950     /* FIXME: Do we need to act on discontinuity ?? */
951     msg_Dbg(s, "#EXT-X-DISCONTINUITY %s", p_read);
952     return VLC_SUCCESS;
953 }
954
955 static int hls_CompareStreams( const void* a, const void* b )
956 {
957     hls_stream_t*   stream_a = *(hls_stream_t**)a;
958     hls_stream_t*   stream_b = *(hls_stream_t**)b;
959
960     return stream_a->bandwidth - stream_b->bandwidth;
961 }
962
963 /* The http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8
964  * document defines the following new tags: EXT-X-TARGETDURATION,
965  * EXT-X-MEDIA-SEQUENCE, EXT-X-KEY, EXT-X-PROGRAM-DATE-TIME, EXT-X-
966  * ALLOW-CACHE, EXT-X-STREAM-INF, EXT-X-ENDLIST, EXT-X-DISCONTINUITY,
967  * and EXT-X-VERSION.
968  */
969 static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const ssize_t len)
970 {
971     stream_sys_t *p_sys = s->p_sys;
972     uint8_t *p_read, *p_begin, *p_end;
973
974     assert(streams);
975     assert(buffer);
976
977     msg_Dbg(s, "parse_M3U8\n%s", buffer);
978     p_begin = buffer;
979     p_end = p_begin + len;
980
981     char *line = ReadLine(p_begin, &p_read, p_end - p_begin);
982     if (line == NULL)
983         return VLC_ENOMEM;
984     p_begin = p_read;
985
986     if (strncmp(line, "#EXTM3U", 7) != 0)
987     {
988         msg_Err(s, "missing #EXTM3U tag .. aborting");
989         free(line);
990         return VLC_EGENERIC;
991     }
992
993     free(line);
994     line = NULL;
995
996     /* What is the version ? */
997     int version = 1;
998     uint8_t *p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-VERSION:");
999     if (p != NULL)
1000     {
1001         uint8_t *tmp = NULL;
1002         char *psz_version = ReadLine(p, &tmp, p_end - p);
1003         if (psz_version == NULL)
1004             return VLC_ENOMEM;
1005         int ret = sscanf((const char*)psz_version, "#EXT-X-VERSION:%d", &version);
1006         if (ret != 1)
1007         {
1008             msg_Warn(s, "#EXT-X-VERSION: no protocol version found, assuming version 1.");
1009             version = 1;
1010         }
1011         free(psz_version);
1012         p = NULL;
1013     }
1014
1015     /* Is it a live stream ? */
1016     p_sys->b_live = (strstr((const char *)buffer, "#EXT-X-ENDLIST") == NULL) ? true : false;
1017
1018     /* Is it a meta index file ? */
1019     bool b_meta = (strstr((const char *)buffer, "#EXT-X-STREAM-INF") == NULL) ? false : true;
1020
1021     int err = VLC_SUCCESS;
1022
1023     if (b_meta)
1024     {
1025         msg_Dbg(s, "Meta playlist");
1026
1027         /* M3U8 Meta Index file */
1028         do {
1029             /* Next line */
1030             line = ReadLine(p_begin, &p_read, p_end - p_begin);
1031             if (line == NULL)
1032                 break;
1033             p_begin = p_read;
1034
1035             /* */
1036             if (strncmp(line, "#EXT-X-STREAM-INF", 17) == 0)
1037             {
1038                 p_sys->b_meta = true;
1039                 char *uri = ReadLine(p_begin, &p_read, p_end - p_begin);
1040                 if (uri == NULL)
1041                     err = VLC_ENOMEM;
1042                 else
1043                 {
1044                     if (*uri == '#')
1045                     {
1046                         msg_Warn(s, "Skipping invalid stream-inf: %s", uri);
1047                         free(uri);
1048                     }
1049                     else
1050                     {
1051                         bool new_stream_added = false;
1052                         hls_stream_t *hls = NULL;
1053                         err = parse_StreamInformation(s, &streams, &hls, line, uri);
1054                         if (err == VLC_SUCCESS)
1055                             new_stream_added = true;
1056
1057                         free(uri);
1058
1059                         if (hls)
1060                         {
1061                             /* Download playlist file from server */
1062                             uint8_t *buf = NULL;
1063                             ssize_t len = read_M3U8_from_url(s, hls->url, &buf);
1064                             if (len < 0)
1065                             {
1066                                 msg_Warn(s, "failed to read %s, continue for other streams", hls->url);
1067
1068                                 /* remove stream just added */
1069                                 if (new_stream_added)
1070                                     vlc_array_remove(streams, vlc_array_count(streams) - 1);
1071
1072                                 /* ignore download error, so we have chance to try other streams */
1073                                 err = VLC_SUCCESS;
1074                             }
1075                             else
1076                             {
1077                                 /* Parse HLS m3u8 content. */
1078                                 err = parse_M3U8(s, streams, buf, len);
1079                                 free(buf);
1080                             }
1081
1082                             hls->version = version;
1083                             if (!p_sys->b_live)
1084                                 hls->size = hls_GetStreamSize(hls); /* Stream size (approximate) */
1085                         }
1086                     }
1087                 }
1088                 p_begin = p_read;
1089             }
1090
1091             free(line);
1092             line = NULL;
1093
1094             if (p_begin >= p_end)
1095                 break;
1096
1097         } while (err == VLC_SUCCESS);
1098
1099         size_t stream_count = vlc_array_count(streams);
1100         msg_Dbg(s, "%d streams loaded in Meta playlist", (int)stream_count);
1101         if (stream_count == 0)
1102         {
1103             msg_Err(s, "No playable streams found in Meta playlist");
1104             err = VLC_EGENERIC;
1105         }
1106     }
1107     else
1108     {
1109         msg_Dbg(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
1110
1111         hls_stream_t *hls = NULL;
1112         if (p_sys->b_meta)
1113             hls = hls_GetLast(streams);
1114         else
1115         {
1116             /* No Meta playlist used */
1117             hls = hls_New(streams, 0, 0, p_sys->m3u8);
1118             if (hls)
1119             {
1120                 /* Get TARGET-DURATION first */
1121                 p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-TARGETDURATION:");
1122                 if (p)
1123                 {
1124                     uint8_t *p_rest = NULL;
1125                     char *psz_duration = ReadLine(p, &p_rest,  p_end - p);
1126                     if (psz_duration == NULL)
1127                         return VLC_EGENERIC;
1128                     err = parse_TargetDuration(s, hls, psz_duration);
1129                     free(psz_duration);
1130                     p = NULL;
1131                 }
1132
1133                 /* Store version */
1134                 hls->version = version;
1135             }
1136             else return VLC_ENOMEM;
1137         }
1138         assert(hls);
1139
1140         /* */
1141         bool media_sequence_loaded = false;
1142         int segment_duration = -1;
1143         do
1144         {
1145             /* Next line */
1146             line = ReadLine(p_begin, &p_read, p_end - p_begin);
1147             if (line == NULL)
1148                 break;
1149             p_begin = p_read;
1150
1151             if (strncmp(line, "#EXTINF", 7) == 0)
1152                 err = parse_SegmentInformation(hls, line, &segment_duration);
1153             else if (strncmp(line, "#EXT-X-TARGETDURATION", 21) == 0)
1154                 err = parse_TargetDuration(s, hls, line);
1155             else if (strncmp(line, "#EXT-X-MEDIA-SEQUENCE", 21) == 0)
1156             {
1157                 /* A Playlist file MUST NOT contain more than one EXT-X-MEDIA-SEQUENCE tag. */
1158                 /* We only care about first one */
1159                 if (!media_sequence_loaded)
1160                 {
1161                     err = parse_MediaSequence(s, hls, line);
1162                     media_sequence_loaded = true;
1163                 }
1164             }
1165             else if (strncmp(line, "#EXT-X-KEY", 10) == 0)
1166                 err = parse_Key(s, hls, line);
1167             else if (strncmp(line, "#EXT-X-PROGRAM-DATE-TIME", 24) == 0)
1168                 err = parse_ProgramDateTime(s, hls, line);
1169             else if (strncmp(line, "#EXT-X-ALLOW-CACHE", 18) == 0)
1170                 err = parse_AllowCache(s, hls, line);
1171             else if (strncmp(line, "#EXT-X-DISCONTINUITY", 20) == 0)
1172                 err = parse_Discontinuity(s, hls, line);
1173             else if (strncmp(line, "#EXT-X-VERSION", 14) == 0)
1174                 err = parse_Version(s, hls, line);
1175             else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0)
1176                 err = parse_EndList(s, hls);
1177             else if ((strncmp(line, "#", 1) != 0) && (*line != '\0') )
1178             {
1179                 err = parse_AddSegment(hls, segment_duration, line);
1180                 segment_duration = -1; /* reset duration */
1181             }
1182
1183             free(line);
1184             line = NULL;
1185
1186             if (p_begin >= p_end)
1187                 break;
1188
1189         } while (err == VLC_SUCCESS);
1190
1191         free(line);
1192     }
1193
1194     return err;
1195 }
1196
1197
1198 static int hls_DownloadSegmentKey(stream_t *s, segment_t *seg)
1199 {
1200     stream_t *p_m3u8 = stream_UrlNew(s, seg->psz_key_path);
1201     if (p_m3u8 == NULL)
1202     {
1203         msg_Err(s, "Failed to load the AES key for segment sequence %d", seg->sequence);
1204         return VLC_EGENERIC;
1205     }
1206
1207     int len = stream_Read(p_m3u8, seg->aes_key, sizeof(seg->aes_key));
1208     stream_Delete(p_m3u8);
1209     if (len != AES_BLOCK_SIZE)
1210     {
1211         msg_Err(s, "The AES key loaded doesn't have the right size (%d)", len);
1212         return VLC_EGENERIC;
1213     }
1214
1215     return VLC_SUCCESS;
1216 }
1217
1218 static int hls_ManageSegmentKeys(stream_t *s, hls_stream_t *hls)
1219 {
1220     segment_t   *seg = NULL;
1221     segment_t   *prev_seg;
1222     int         count = vlc_array_count(hls->segments);
1223
1224     for (int i = 0; i < count; i++)
1225     {
1226         prev_seg = seg;
1227         seg = segment_GetSegment(hls, i);
1228         if (seg == NULL )
1229             continue;
1230         if (seg->psz_key_path == NULL)
1231             continue;   /* No key to load ? continue */
1232         if (seg->b_key_loaded)
1233             continue;   /* The key is already loaded */
1234
1235         /* if the key has not changed, and already available from previous segment,
1236          * try to copy it, and don't load the key */
1237         if (prev_seg && prev_seg->b_key_loaded && strcmp(seg->psz_key_path, prev_seg->psz_key_path) == 0)
1238         {
1239             memcpy(seg->aes_key, prev_seg->aes_key, AES_BLOCK_SIZE);
1240             seg->b_key_loaded = true;
1241             continue;
1242         }
1243         if (hls_DownloadSegmentKey(s, seg) != VLC_SUCCESS)
1244             return VLC_EGENERIC;
1245        seg->b_key_loaded = true;
1246     }
1247     return VLC_SUCCESS;
1248 }
1249
1250 static int hls_DecodeSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segment)
1251 {
1252     /* Did the segment need to be decoded ? */
1253     if (segment->psz_key_path == NULL)
1254         return VLC_SUCCESS;
1255
1256     /* Do we have loaded the key ? */
1257     if (!segment->b_key_loaded)
1258     {
1259         /* No ? try to download it now */
1260         if (hls_ManageSegmentKeys(s, hls) != VLC_SUCCESS)
1261             return VLC_EGENERIC;
1262     }
1263
1264     /* For now, we only decode AES-128 data */
1265     gcry_error_t i_gcrypt_err;
1266     gcry_cipher_hd_t aes_ctx;
1267     /* Setup AES */
1268     i_gcrypt_err = gcry_cipher_open(&aes_ctx, GCRY_CIPHER_AES,
1269                                      GCRY_CIPHER_MODE_CBC, 0);
1270     if (i_gcrypt_err)
1271     {
1272         msg_Err(s, "gcry_cipher_open failed: %s", gpg_strerror(i_gcrypt_err));
1273         gcry_cipher_close(aes_ctx);
1274         return VLC_EGENERIC;
1275     }
1276
1277     /* Set key */
1278     i_gcrypt_err = gcry_cipher_setkey(aes_ctx, segment->aes_key,
1279                                        sizeof(segment->aes_key));
1280     if (i_gcrypt_err)
1281     {
1282         msg_Err(s, "gcry_cipher_setkey failed: %s", gpg_strerror(i_gcrypt_err));
1283         gcry_cipher_close(aes_ctx);
1284         return VLC_EGENERIC;
1285     }
1286
1287     if (segment->b_iv_loaded == false)
1288     {
1289         memset(segment->psz_AES_IV, 0, AES_BLOCK_SIZE);
1290         segment->psz_AES_IV[15] = segment->sequence & 0xff;
1291         segment->psz_AES_IV[14] = (segment->sequence >> 8)& 0xff;
1292         segment->psz_AES_IV[13] = (segment->sequence >> 16)& 0xff;
1293         segment->psz_AES_IV[12] = (segment->sequence >> 24)& 0xff;
1294     }
1295
1296     i_gcrypt_err = gcry_cipher_setiv(aes_ctx, segment->psz_AES_IV,
1297                                       sizeof(segment->psz_AES_IV));
1298
1299     if (i_gcrypt_err)
1300     {
1301         msg_Err(s, "gcry_cipher_setiv failed: %s", gpg_strerror(i_gcrypt_err));
1302         gcry_cipher_close(aes_ctx);
1303         return VLC_EGENERIC;
1304     }
1305
1306     i_gcrypt_err = gcry_cipher_decrypt(aes_ctx,
1307                                        segment->data->p_buffer, /* out */
1308                                        segment->data->i_buffer,
1309                                        NULL, /* in */
1310                                        0);
1311     if (i_gcrypt_err)
1312     {
1313         msg_Err(s, "gcry_cipher_decrypt failed:  %s/%s\n", gcry_strsource(i_gcrypt_err), gcry_strerror(i_gcrypt_err));
1314         gcry_cipher_close(aes_ctx);
1315         return VLC_EGENERIC;
1316     }
1317     gcry_cipher_close(aes_ctx);
1318     /* remove the PKCS#7 padding from the buffer */
1319     int pad = segment->data->p_buffer[segment->data->i_buffer-1];
1320     if (pad <= 0 || pad > AES_BLOCK_SIZE)
1321     {
1322         msg_Err(s, "Bad padding character (0x%x), perhaps we failed to decrypt the segment with the correct key", pad);
1323         return VLC_EGENERIC;
1324     }
1325     int count = pad;
1326     while (count--)
1327     {
1328         if (segment->data->p_buffer[segment->data->i_buffer-1-count] != pad)
1329         {
1330                 msg_Err(s, "Bad ending buffer, perhaps we failed to decrypt the segment with the correct key");
1331                 return VLC_EGENERIC;
1332         }
1333     }
1334
1335     /* not all the data is readable because of padding */
1336     segment->data->i_buffer -= pad;
1337
1338     return VLC_SUCCESS;
1339 }
1340
1341 static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
1342 {
1343     stream_sys_t *p_sys = s->p_sys;
1344     assert(*streams);
1345     int err = VLC_EGENERIC;
1346
1347     /* Duplicate HLS stream META information */
1348     for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
1349     {
1350         hls_stream_t *src, *dst;
1351         src = hls_Get(p_sys->hls_stream, i);
1352         if (src == NULL)
1353             return VLC_EGENERIC;
1354
1355         dst = hls_Copy(src, false);
1356         if (dst == NULL)
1357             return VLC_ENOMEM;
1358         vlc_array_append(*streams, dst);
1359
1360         /* Download playlist file from server */
1361         uint8_t *buf = NULL;
1362         ssize_t len = read_M3U8_from_url(s, dst->url, &buf);
1363         if (len < 0)
1364             err = VLC_EGENERIC;
1365         else
1366         {
1367             /* Parse HLS m3u8 content. */
1368             err = parse_M3U8(s, *streams, buf, len);
1369             free(buf);
1370         }
1371     }
1372     return err;
1373 }
1374
1375 /* Update hls_old (an existing member of p_sys->hls_stream) to match hls_new
1376    (which represents a downloaded, perhaps newer version of the same playlist) */
1377 static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *hls_old, bool *stream_appended)
1378 {
1379     int count = vlc_array_count(hls_new->segments);
1380
1381     msg_Dbg(s, "updating hls stream (program-id=%d, bandwidth=%"PRIu64") has %d segments",
1382              hls_new->id, hls_new->bandwidth, count);
1383
1384     vlc_mutex_lock(&hls_old->lock);
1385     hls_old->max_segment_length=-1;
1386     for (int n = 0; n < count; n++)
1387     {
1388         segment_t *p = segment_GetSegment(hls_new, n);
1389         if (p == NULL)
1390             continue;
1391
1392         segment_t *segment = segment_Find(hls_old, p->sequence);
1393         if (segment)
1394         {
1395             vlc_mutex_lock(&segment->lock);
1396
1397             assert(p->url);
1398             assert(segment->url);
1399
1400             /* they should be the same */
1401             if ((p->sequence != segment->sequence) ||
1402                 (p->duration != segment->duration) ||
1403                 (strcmp(p->url, segment->url) != 0))
1404             {
1405                 msg_Warn(s, "existing segment found with different content - resetting");
1406                 msg_Warn(s, "- sequence: new=%d, old=%d", p->sequence, segment->sequence);
1407                 msg_Warn(s, "- duration: new=%d, old=%d", p->duration, segment->duration);
1408                 msg_Warn(s, "- file: new=%s", p->url);
1409                 msg_Warn(s, "        old=%s", segment->url);
1410
1411                 /* Resetting content */
1412                 segment->sequence = p->sequence;
1413                 segment->duration = p->duration;
1414                 free(segment->url);
1415                 segment->url = strdup(p->url);
1416                 if ( segment->url == NULL )
1417                 {
1418                     msg_Err(s, "Failed updating segment %d - skipping it",  p->sequence);
1419                     segment_Free(p);
1420                     vlc_mutex_unlock(&segment->lock);
1421                     continue;
1422                 }
1423                 /* We must free the content, because if the key was not downloaded, content can't be decrypted */
1424                 if ((p->psz_key_path || p->b_key_loaded) &&
1425                     segment->data)
1426                 {
1427                     block_Release(segment->data);
1428                     segment->data = NULL;
1429                 }
1430                 free(segment->psz_key_path);
1431                 segment->psz_key_path = p->psz_key_path ? strdup(p->psz_key_path) : NULL;
1432             }
1433             segment_Free(p);
1434             vlc_mutex_unlock(&segment->lock);
1435         }
1436         else
1437         {
1438             int last = vlc_array_count(hls_old->segments) - 1;
1439             segment_t *l = segment_GetSegment(hls_old, last);
1440             if (l == NULL) {
1441                 vlc_mutex_unlock(&hls_old->lock);
1442                 segment_Free(p);
1443                 continue;
1444             }
1445
1446             if ((l->sequence + 1) != p->sequence)
1447             {
1448                 msg_Err(s, "gap in sequence numbers found: new=%d expected %d",
1449                         p->sequence, l->sequence+1);
1450             }
1451             vlc_array_append(hls_old->segments, p);
1452             msg_Dbg(s, "- segment %d appended", p->sequence);
1453             hls_old->max_segment_length = __MAX(hls_old->max_segment_length, p->duration);
1454             msg_Dbg(s, "  - segments new max duration %d", hls_old->max_segment_length);
1455
1456             // Signal download thread otherwise the segment will not get downloaded
1457             *stream_appended = true;
1458         }
1459     }
1460
1461     /* update meta information */
1462     hls_old->sequence = hls_new->sequence;
1463     hls_old->duration = (hls_new->duration == -1) ? hls_old->duration : hls_new->duration;
1464     hls_old->b_cache = hls_new->b_cache;
1465     vlc_mutex_unlock(&hls_old->lock);
1466
1467     vlc_array_clear(hls_new->segments);
1468     return VLC_SUCCESS;
1469 }
1470
1471 static int hls_ReloadPlaylist(stream_t *s)
1472 {
1473     stream_sys_t *p_sys = s->p_sys;
1474
1475     // Flag to indicate if we should signal download thread
1476     bool stream_appended = false;
1477
1478     vlc_array_t *hls_streams = vlc_array_new();
1479     if (hls_streams == NULL)
1480         return VLC_ENOMEM;
1481
1482     msg_Dbg(s, "Reloading HLS live meta playlist");
1483
1484     if (get_HTTPLiveMetaPlaylist(s, &hls_streams) != VLC_SUCCESS)
1485     {
1486         /* Free hls streams */
1487         for (int i = 0; i < vlc_array_count(hls_streams); i++)
1488         {
1489             hls_stream_t *hls;
1490             hls = hls_Get(hls_streams, i);
1491             if (hls) hls_Free(hls);
1492         }
1493         vlc_array_destroy(hls_streams);
1494
1495         msg_Err(s, "reloading playlist failed");
1496         return VLC_EGENERIC;
1497     }
1498
1499     /* merge playlists */
1500     int count = vlc_array_count(hls_streams);
1501     for (int n = 0; n < count; n++)
1502     {
1503         hls_stream_t *hls_new = hls_Get(hls_streams, n);
1504         if (hls_new == NULL)
1505             continue;
1506
1507         hls_stream_t *hls_old = hls_Find(p_sys->hls_stream, hls_new);
1508         if (hls_old == NULL)
1509         {   /* new hls stream - append */
1510             vlc_array_append(p_sys->hls_stream, hls_new);
1511             msg_Dbg(s, "new HLS stream appended (id=%d, bandwidth=%"PRIu64")",
1512                      hls_new->id, hls_new->bandwidth);
1513
1514             // New segment available -  signal download thread
1515             stream_appended = true;
1516             continue;
1517         }
1518         else if (hls_UpdatePlaylist(s, hls_new, hls_old, &stream_appended) != VLC_SUCCESS)
1519             msg_Warn(s, "failed updating HLS stream (id=%d, bandwidth=%"PRIu64")",
1520                      hls_new->id, hls_new->bandwidth);
1521         hls_Free(hls_new);
1522     }
1523     vlc_array_destroy(hls_streams);
1524
1525     // Must signal the download thread otherwise new segments will not be downloaded at all!
1526     if (stream_appended == true)
1527     {
1528         vlc_mutex_lock(&p_sys->download.lock_wait);
1529         vlc_cond_signal(&p_sys->download.wait);
1530         vlc_mutex_unlock(&p_sys->download.lock_wait);
1531     }
1532
1533     return VLC_SUCCESS;
1534 }
1535
1536 /****************************************************************************
1537  * hls_Thread
1538  ****************************************************************************/
1539 static int BandwidthAdaptation(stream_t *s, int progid, uint64_t *bandwidth)
1540 {
1541     stream_sys_t *p_sys = s->p_sys;
1542     int candidate = -1;
1543     uint64_t bw = *bandwidth;
1544     uint64_t bw_candidate = 0;
1545
1546     int count = vlc_array_count(p_sys->hls_stream);
1547     for (int n = 0; n < count; n++)
1548     {
1549         /* Select best bandwidth match */
1550         hls_stream_t *hls = hls_Get(p_sys->hls_stream, n);
1551         if (hls == NULL) break;
1552
1553         /* only consider streams with the same PROGRAM-ID */
1554         if (hls->id == progid)
1555         {
1556             if ((bw >= hls->bandwidth) && (bw_candidate < hls->bandwidth))
1557             {
1558                 msg_Dbg(s, "candidate %d bandwidth (bits/s) %"PRIu64" >= %"PRIu64,
1559                          n, bw, hls->bandwidth); /* bits / s */
1560                 bw_candidate = hls->bandwidth;
1561                 candidate = n; /* possible candidate */
1562             }
1563         }
1564     }
1565     *bandwidth = bw_candidate;
1566     return candidate;
1567 }
1568
1569 static int hls_DownloadSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur_stream)
1570 {
1571     stream_sys_t *p_sys = s->p_sys;
1572
1573     assert(hls);
1574     assert(segment);
1575
1576     vlc_mutex_lock(&segment->lock);
1577     if (segment->data != NULL)
1578     {
1579         /* Segment already downloaded */
1580         vlc_mutex_unlock(&segment->lock);
1581         return VLC_SUCCESS;
1582     }
1583
1584     /* sanity check - can we download this segment on time? */
1585     if ((p_sys->bandwidth > 0) && (hls->bandwidth > 0))
1586     {
1587         uint64_t size = (segment->duration * hls->bandwidth); /* bits */
1588         int estimated = (int)(size / p_sys->bandwidth);
1589         if (estimated > segment->duration)
1590         {
1591             msg_Warn(s,"downloading segment %d predicted to take %ds, which exceeds its length (%ds)",
1592                         segment->sequence, estimated, segment->duration);
1593         }
1594     }
1595
1596     mtime_t start = mdate();
1597     if (hls_Download(s, segment) != VLC_SUCCESS)
1598     {
1599         msg_Err(s, "downloading segment %d from stream %d failed",
1600                     segment->sequence, *cur_stream);
1601         vlc_mutex_unlock(&segment->lock);
1602         return VLC_EGENERIC;
1603     }
1604     mtime_t duration = mdate() - start;
1605     if (hls->bandwidth == 0 && segment->duration > 0)
1606     {
1607         /* Try to estimate the bandwidth for this stream */
1608         hls->bandwidth = (uint64_t)(((double)segment->size * 8) / ((double)segment->duration));
1609     }
1610
1611     /* If the segment is encrypted, decode it */
1612     if (hls_DecodeSegmentData(s, hls, segment) != VLC_SUCCESS)
1613     {
1614         vlc_mutex_unlock(&segment->lock);
1615         return VLC_EGENERIC;
1616     }
1617
1618     vlc_mutex_unlock(&segment->lock);
1619
1620     msg_Dbg(s, "downloaded segment %d from stream %d",
1621                 segment->sequence, *cur_stream);
1622
1623     uint64_t bw = segment->size * 8 * 1000000 / __MAX(1, duration); /* bits / s */
1624     p_sys->bandwidth = bw;
1625     if (p_sys->b_meta && (hls->bandwidth != bw))
1626     {
1627         int newstream = BandwidthAdaptation(s, hls->id, &bw);
1628
1629         /* FIXME: we need an average here */
1630         if ((newstream >= 0) && (newstream != *cur_stream))
1631         {
1632             msg_Dbg(s, "detected %s bandwidth (%"PRIu64") stream",
1633                      (bw >= hls->bandwidth) ? "faster" : "lower", bw);
1634             *cur_stream = newstream;
1635         }
1636     }
1637     return VLC_SUCCESS;
1638 }
1639
1640 static void* hls_Thread(void *p_this)
1641 {
1642     stream_t *s = (stream_t *)p_this;
1643     stream_sys_t *p_sys = s->p_sys;
1644
1645     int canc = vlc_savecancel();
1646
1647     while (vlc_object_alive(s))
1648     {
1649         hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream);
1650         assert(hls);
1651
1652         /* Sliding window (~60 seconds worth of movie) */
1653         vlc_mutex_lock(&hls->lock);
1654         int count = vlc_array_count(hls->segments);
1655         vlc_mutex_unlock(&hls->lock);
1656
1657         /* Is there a new segment to process? */
1658         if ((!p_sys->b_live && (p_sys->playback.segment < (count - 6))) ||
1659             (p_sys->download.segment >= count))
1660         {
1661             /* wait */
1662             vlc_mutex_lock(&p_sys->download.lock_wait);
1663             while (((p_sys->download.segment - p_sys->playback.segment > 6) ||
1664                     (p_sys->download.segment >= count)) &&
1665                    (p_sys->download.seek == -1))
1666             {
1667                 vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
1668                 if (p_sys->b_live /*&& (mdate() >= p_sys->playlist.wakeup)*/)
1669                     break;
1670                 if (!vlc_object_alive(s))
1671                     break;
1672             }
1673             /* */
1674             if (p_sys->download.seek >= 0)
1675             {
1676                 p_sys->download.segment = p_sys->download.seek;
1677                 p_sys->download.seek = -1;
1678             }
1679             vlc_mutex_unlock(&p_sys->download.lock_wait);
1680         }
1681
1682         if (!vlc_object_alive(s)) break;
1683
1684         vlc_mutex_lock(&hls->lock);
1685         segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
1686         vlc_mutex_unlock(&hls->lock);
1687
1688         if ((segment != NULL) &&
1689             (hls_DownloadSegmentData(s, hls, segment, &p_sys->download.stream) != VLC_SUCCESS))
1690         {
1691             if (!vlc_object_alive(s)) break;
1692
1693             if (!p_sys->b_live)
1694             {
1695                 p_sys->b_error = true;
1696                 break;
1697             }
1698         }
1699
1700         /* download succeeded */
1701         /* determine next segment to download */
1702         vlc_mutex_lock(&p_sys->download.lock_wait);
1703         if (p_sys->download.seek >= 0)
1704         {
1705             p_sys->download.segment = p_sys->download.seek;
1706             p_sys->download.seek = -1;
1707         }
1708         else if (p_sys->download.segment < count)
1709             p_sys->download.segment++;
1710         vlc_cond_signal(&p_sys->download.wait);
1711         vlc_mutex_unlock(&p_sys->download.lock_wait);
1712
1713         // In case of a successful download signal the read thread that data is available
1714         vlc_mutex_lock(&p_sys->read.lock_wait);
1715         vlc_cond_signal(&p_sys->read.wait);
1716         vlc_mutex_unlock(&p_sys->read.lock_wait);
1717     }
1718
1719     vlc_restorecancel(canc);
1720     return NULL;
1721 }
1722
1723 static void* hls_Reload(void *p_this)
1724 {
1725     stream_t *s = (stream_t *)p_this;
1726     stream_sys_t *p_sys = s->p_sys;
1727
1728     assert(p_sys->b_live);
1729
1730     int canc = vlc_savecancel();
1731
1732     double wait = 1.0;
1733     while (vlc_object_alive(s))
1734     {
1735         mtime_t now = mdate();
1736         if (now >= p_sys->playlist.wakeup)
1737         {
1738             /* reload the m3u8 if there are less than 3 segments what aren't downloaded */
1739             if ( ( p_sys->download.segment - p_sys->playback.segment < 3 ) &&
1740                  ( hls_ReloadPlaylist(s) != VLC_SUCCESS) )
1741             {
1742                 /* No change in playlist, then backoff */
1743                 p_sys->playlist.tries++;
1744                 if (p_sys->playlist.tries == 1) wait = 0.5;
1745                 else if (p_sys->playlist.tries == 2) wait = 1;
1746                 else if (p_sys->playlist.tries >= 3) wait = 1.5;
1747
1748                 /* Can we afford to backoff? */
1749                 if (p_sys->download.segment - p_sys->playback.segment < 3)
1750                 {
1751                     p_sys->playlist.tries = 0;
1752                     wait = 0.5;
1753                 }
1754             }
1755             else
1756             {
1757                 p_sys->playlist.tries = 0;
1758                 wait = 1.0;
1759             }
1760
1761             hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream);
1762             assert(hls);
1763
1764             /* determine next time to update playlist */
1765             p_sys->playlist.last = now;
1766             p_sys->playlist.wakeup = now;
1767             /* If there is no new segments,use playlist duration as sleep period base */
1768             if( likely( hls->max_segment_length > 0 ) )
1769                 p_sys->playlist.wakeup += (mtime_t)((hls->max_segment_length * wait) * CLOCK_FREQ);
1770             else
1771                 p_sys->playlist.wakeup += (mtime_t)((hls->duration * wait) * CLOCK_FREQ);
1772         }
1773
1774         mwait(p_sys->playlist.wakeup);
1775     }
1776
1777     vlc_restorecancel(canc);
1778     return NULL;
1779 }
1780
1781 static int Prefetch(stream_t *s, int *current)
1782 {
1783     stream_sys_t *p_sys = s->p_sys;
1784     int stream = *current;
1785
1786     hls_stream_t *hls = hls_Get(p_sys->hls_stream, stream);
1787     if (hls == NULL)
1788         return VLC_EGENERIC;
1789
1790     if (vlc_array_count(hls->segments) == 0)
1791         return VLC_EGENERIC;
1792     else if (vlc_array_count(hls->segments) == 1 && p_sys->b_live)
1793         msg_Warn(s, "Only 1 segment available to prefetch in live stream; may stall");
1794
1795     /* Download ~10s worth of segments of this HLS stream if they exist */
1796     unsigned segment_amount = (0.5f + 10/hls->duration);
1797     for (int i = 0; i < __MIN(vlc_array_count(hls->segments), segment_amount); i++)
1798     {
1799         segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
1800         if (segment == NULL )
1801             return VLC_EGENERIC;
1802
1803         /* It is useless to lock the segment here, as Prefetch is called before
1804            download and playlit thread are started. */
1805         if (segment->data)
1806         {
1807             p_sys->download.segment++;
1808             continue;
1809         }
1810
1811         if (hls_DownloadSegmentData(s, hls, segment, current) != VLC_SUCCESS)
1812             return VLC_EGENERIC;
1813
1814         p_sys->download.segment++;
1815
1816         /* adapt bandwidth? */
1817         if (*current != stream)
1818         {
1819             hls_stream_t *hls = hls_Get(p_sys->hls_stream, *current);
1820             if (hls == NULL)
1821                 return VLC_EGENERIC;
1822
1823              stream = *current;
1824         }
1825     }
1826
1827     return VLC_SUCCESS;
1828 }
1829
1830 /****************************************************************************
1831  *
1832  ****************************************************************************/
1833 static int hls_Download(stream_t *s, segment_t *segment)
1834 {
1835     stream_sys_t *p_sys = s->p_sys;
1836     assert(segment);
1837
1838     vlc_mutex_lock(&p_sys->lock);
1839     while (p_sys->paused)
1840         vlc_cond_wait(&p_sys->wait, &p_sys->lock);
1841     vlc_mutex_unlock(&p_sys->lock);
1842
1843     stream_t *p_ts = stream_UrlNew(s, segment->url);
1844     if (p_ts == NULL)
1845         return VLC_EGENERIC;
1846
1847     segment->size = stream_Size(p_ts);
1848
1849     if (segment->size == 0) {
1850         int chunk_size = 65536;
1851         segment->data = block_Alloc(chunk_size);
1852         if (!segment->data)
1853             goto nomem;
1854         do {
1855             if (segment->data->i_buffer - segment->size < chunk_size) {
1856                 chunk_size *= 2;
1857                 block_t *p_block = block_Realloc(segment->data, 0, segment->data->i_buffer + chunk_size);
1858                 if (!p_block) {
1859                     block_Release(segment->data);
1860                     segment->data = NULL;
1861                     goto nomem;
1862                 }
1863                 segment->data = p_block;
1864             }
1865
1866             ssize_t length = stream_Read(p_ts, segment->data->p_buffer + segment->size, chunk_size);
1867             if (length <= 0) {
1868                 segment->data->i_buffer = segment->size;
1869                 break;
1870             }
1871             segment->size += length;
1872         } while (vlc_object_alive(s));
1873
1874         stream_Delete(p_ts);
1875         return VLC_SUCCESS;
1876     }
1877
1878     segment->data = block_Alloc(segment->size);
1879     if (segment->data == NULL)
1880         goto nomem;
1881
1882     assert(segment->data->i_buffer == segment->size);
1883
1884     ssize_t curlen = 0;
1885     do
1886     {
1887         /* NOTE: Beware the size reported for a segment by the HLS server may not
1888          * be correct, when downloading the segment data. Therefore check the size
1889          * and enlarge the segment data block if necessary.
1890          */
1891         uint64_t size = stream_Size(p_ts);
1892         if (size > segment->size)
1893         {
1894             msg_Dbg(s, "size changed %"PRIu64, segment->size);
1895             block_t *p_block = block_Realloc(segment->data, 0, size);
1896             if (p_block == NULL)
1897             {
1898                 block_Release(segment->data);
1899                 segment->data = NULL;
1900                 goto nomem;
1901             }
1902             segment->data = p_block;
1903             segment->size = size;
1904             assert(segment->data->i_buffer == segment->size);
1905             p_block = NULL;
1906         }
1907         ssize_t length = stream_Read(p_ts, segment->data->p_buffer + curlen, segment->size - curlen);
1908         if (length <= 0)
1909             break;
1910         curlen += length;
1911     } while (vlc_object_alive(s));
1912
1913     stream_Delete(p_ts);
1914     return VLC_SUCCESS;
1915
1916 nomem:
1917     stream_Delete(p_ts);
1918     return VLC_ENOMEM;
1919 }
1920
1921 /* Read M3U8 file */
1922 static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer)
1923 {
1924     int64_t total_bytes = 0;
1925     int64_t total_allocated = 0;
1926     uint8_t *p = NULL;
1927
1928     while (1)
1929     {
1930         char buf[4096];
1931         int64_t bytes;
1932
1933         bytes = stream_Read(s, buf, sizeof(buf));
1934         if (bytes == 0)
1935             break;      /* EOF ? */
1936         else if (bytes < 0)
1937         {
1938             free (p);
1939             return bytes;
1940         }
1941
1942         if ( (total_bytes + bytes + 1) > total_allocated )
1943         {
1944             if (total_allocated)
1945                 total_allocated *= 2;
1946             else
1947                 total_allocated = __MIN((uint64_t)bytes+1, sizeof(buf));
1948
1949             p = realloc_or_free(p, total_allocated);
1950             if (p == NULL)
1951                 return VLC_ENOMEM;
1952         }
1953
1954         memcpy(p+total_bytes, buf, bytes);
1955         total_bytes += bytes;
1956     }
1957
1958     if (total_allocated == 0)
1959         return VLC_EGENERIC;
1960
1961     p[total_bytes] = '\0';
1962     *buffer = p;
1963
1964     return total_bytes;
1965 }
1966
1967 static ssize_t read_M3U8_from_url(stream_t *s, const char* psz_url, uint8_t **buffer)
1968 {
1969     assert(*buffer == NULL);
1970
1971     /* Construct URL */
1972     stream_t *p_m3u8 = stream_UrlNew(s, psz_url);
1973     if (p_m3u8 == NULL)
1974         return VLC_EGENERIC;
1975
1976     ssize_t size = read_M3U8_from_stream(p_m3u8, buffer);
1977     stream_Delete(p_m3u8);
1978
1979     return size;
1980 }
1981
1982 static char *ReadLine(uint8_t *buffer, uint8_t **pos, const size_t len)
1983 {
1984     assert(buffer);
1985
1986     char *line = NULL;
1987     uint8_t *begin = buffer;
1988     uint8_t *p = begin;
1989     uint8_t *end = p + len;
1990
1991     while (p < end)
1992     {
1993         if ((*p == '\r') || (*p == '\n') || (*p == '\0'))
1994             break;
1995         p++;
1996     }
1997
1998     /* copy line excluding \r \n or \0 */
1999     line = strndup((char *)begin, p - begin);
2000
2001     while ((*p == '\r') || (*p == '\n') || (*p == '\0'))
2002     {
2003         if (*p == '\0')
2004         {
2005             *pos = end;
2006             break;
2007         }
2008         else
2009         {
2010             /* next pass start after \r and \n */
2011             p++;
2012             *pos = p;
2013         }
2014     }
2015
2016     return line;
2017 }
2018
2019 /****************************************************************************
2020  * Open
2021  ****************************************************************************/
2022 static int Open(vlc_object_t *p_this)
2023 {
2024     stream_t *s = (stream_t*)p_this;
2025     stream_sys_t *p_sys;
2026
2027     if (!isHTTPLiveStreaming(s))
2028         return VLC_EGENERIC;
2029
2030     msg_Info(p_this, "HTTP Live Streaming (%s)", s->psz_path);
2031
2032     /* Initialize crypto bit */
2033     vlc_gcrypt_init();
2034
2035     /* */
2036     s->p_sys = p_sys = calloc(1, sizeof(*p_sys));
2037     if (p_sys == NULL)
2038         return VLC_ENOMEM;
2039
2040     char *psz_uri = NULL;
2041     if (asprintf(&psz_uri,"%s://%s", s->psz_access, s->psz_path) < 0)
2042     {
2043         free(p_sys);
2044         return VLC_ENOMEM;
2045     }
2046     p_sys->m3u8 = psz_uri;
2047
2048     char *new_path;
2049     if (asprintf(&new_path, "%s.ts", s->psz_path) < 0)
2050     {
2051         free(p_sys->m3u8);
2052         free(p_sys);
2053         return VLC_ENOMEM;
2054     }
2055     free(s->psz_path);
2056     s->psz_path = new_path;
2057
2058     p_sys->bandwidth = 0;
2059     p_sys->b_live = true;
2060     p_sys->b_meta = false;
2061     p_sys->b_error = false;
2062
2063     p_sys->hls_stream = vlc_array_new();
2064     if (p_sys->hls_stream == NULL)
2065     {
2066         free(p_sys->m3u8);
2067         free(p_sys);
2068         return VLC_ENOMEM;
2069     }
2070
2071     /* */
2072     s->pf_read = Read;
2073     s->pf_peek = Peek;
2074     s->pf_control = Control;
2075
2076     p_sys->paused = false;
2077
2078     vlc_cond_init(&p_sys->wait);
2079     vlc_mutex_init(&p_sys->lock);
2080
2081     /* Parse HLS m3u8 content. */
2082     uint8_t *buffer = NULL;
2083     ssize_t len = read_M3U8_from_stream(s->p_source, &buffer);
2084     if (len < 0)
2085         goto fail;
2086     if (parse_M3U8(s, p_sys->hls_stream, buffer, len) != VLC_SUCCESS)
2087     {
2088         free(buffer);
2089         goto fail;
2090     }
2091     free(buffer);
2092     /* HLS standard doesn't provide any guaranty about streams
2093        being sorted by bandwidth, so we sort them */
2094     qsort( p_sys->hls_stream->pp_elems, p_sys->hls_stream->i_count,
2095            sizeof( hls_stream_t* ), &hls_CompareStreams );
2096
2097     /* Choose first HLS stream to start with */
2098     int current = p_sys->playback.stream = p_sys->hls_stream->i_count-1;
2099     p_sys->playback.segment = p_sys->download.segment = ChooseSegment(s, current);
2100
2101     /* manage encryption key if needed */
2102     hls_ManageSegmentKeys(s, hls_Get(p_sys->hls_stream, current));
2103
2104     if (Prefetch(s, &current) != VLC_SUCCESS)
2105     {
2106         msg_Err(s, "fetching first segment failed.");
2107         goto fail;
2108     }
2109
2110     p_sys->download.stream = current;
2111     p_sys->playback.stream = current;
2112     p_sys->download.seek = -1;
2113
2114     vlc_mutex_init(&p_sys->download.lock_wait);
2115     vlc_cond_init(&p_sys->download.wait);
2116
2117     vlc_mutex_init(&p_sys->read.lock_wait);
2118     vlc_cond_init(&p_sys->read.wait);
2119
2120     /* Initialize HLS live stream */
2121     if (p_sys->b_live)
2122     {
2123         hls_stream_t *hls = hls_Get(p_sys->hls_stream, current);
2124         p_sys->playlist.last = mdate();
2125         p_sys->playlist.wakeup = p_sys->playlist.last +
2126                 ((mtime_t)hls->duration * CLOCK_FREQ );
2127
2128         if (vlc_clone(&p_sys->reload, hls_Reload, s, VLC_THREAD_PRIORITY_LOW))
2129         {
2130             goto fail_thread;
2131         }
2132     }
2133
2134     if (vlc_clone(&p_sys->thread, hls_Thread, s, VLC_THREAD_PRIORITY_INPUT))
2135     {
2136         if (p_sys->b_live)
2137             vlc_join(p_sys->reload, NULL);
2138         goto fail_thread;
2139     }
2140
2141     return VLC_SUCCESS;
2142
2143 fail_thread:
2144     vlc_mutex_destroy(&p_sys->download.lock_wait);
2145     vlc_cond_destroy(&p_sys->download.wait);
2146
2147     vlc_mutex_destroy(&p_sys->read.lock_wait);
2148     vlc_cond_destroy(&p_sys->read.wait);
2149
2150 fail:
2151     /* Free hls streams */
2152     for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
2153     {
2154         hls_stream_t *hls = hls_Get(p_sys->hls_stream, i);
2155         if (hls) hls_Free(hls);
2156     }
2157     vlc_array_destroy(p_sys->hls_stream);
2158
2159     vlc_mutex_destroy(&p_sys->lock);
2160     vlc_cond_destroy(&p_sys->wait);
2161
2162     /* */
2163     free(p_sys->m3u8);
2164     free(p_sys);
2165     return VLC_EGENERIC;
2166 }
2167
2168 /****************************************************************************
2169  * Close
2170  ****************************************************************************/
2171 static void Close(vlc_object_t *p_this)
2172 {
2173     stream_t *s = (stream_t*)p_this;
2174     stream_sys_t *p_sys = s->p_sys;
2175
2176     assert(p_sys->hls_stream);
2177
2178     vlc_mutex_lock(&p_sys->lock);
2179     p_sys->paused = false;
2180     vlc_cond_signal(&p_sys->wait);
2181     vlc_mutex_unlock(&p_sys->lock);
2182
2183     /* */
2184     vlc_mutex_lock(&p_sys->download.lock_wait);
2185     /* negate the condition variable's predicate */
2186     p_sys->download.segment = p_sys->playback.segment = 0;
2187     p_sys->download.seek = 0; /* better safe than sorry */
2188     vlc_cond_signal(&p_sys->download.wait);
2189     vlc_mutex_unlock(&p_sys->download.lock_wait);
2190
2191     /* */
2192     if (p_sys->b_live)
2193         vlc_join(p_sys->reload, NULL);
2194     vlc_join(p_sys->thread, NULL);
2195     vlc_mutex_destroy(&p_sys->download.lock_wait);
2196     vlc_cond_destroy(&p_sys->download.wait);
2197
2198     vlc_mutex_destroy(&p_sys->read.lock_wait);
2199     vlc_cond_destroy(&p_sys->read.wait);
2200
2201     /* Free hls streams */
2202     for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
2203     {
2204         hls_stream_t *hls = hls_Get(p_sys->hls_stream, i);
2205         if (hls) hls_Free(hls);
2206     }
2207     vlc_array_destroy(p_sys->hls_stream);
2208
2209     /* */
2210
2211     vlc_mutex_destroy(&p_sys->lock);
2212     vlc_cond_destroy(&p_sys->wait);
2213
2214     free(p_sys->m3u8);
2215     if (p_sys->peeked)
2216         block_Release (p_sys->peeked);
2217     free(p_sys);
2218 }
2219
2220 /****************************************************************************
2221  * Stream filters functions
2222  ****************************************************************************/
2223 static segment_t *GetSegment(stream_t *s)
2224 {
2225     stream_sys_t *p_sys = s->p_sys;
2226     segment_t *segment = NULL;
2227
2228     /* Is this segment of the current HLS stream ready? */
2229     hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2230     if (hls != NULL)
2231     {
2232         vlc_mutex_lock(&hls->lock);
2233         segment = segment_GetSegment(hls, p_sys->playback.segment);
2234         if (segment != NULL)
2235         {
2236             vlc_mutex_lock(&segment->lock);
2237             /* This segment is ready? */
2238             if (segment->data != NULL)
2239             {
2240                 vlc_mutex_unlock(&segment->lock);
2241                 p_sys->b_cache = hls->b_cache;
2242                 vlc_mutex_unlock(&hls->lock);
2243                 goto check;
2244             }
2245             vlc_mutex_unlock(&segment->lock);
2246         }
2247         vlc_mutex_unlock(&hls->lock);
2248     }
2249
2250     /* Was the HLS stream changed to another bitrate? */
2251     segment = NULL;
2252     for (int i_stream = 0; i_stream < vlc_array_count(p_sys->hls_stream); i_stream++)
2253     {
2254         /* Is the next segment ready */
2255         hls_stream_t *hls = hls_Get(p_sys->hls_stream, i_stream);
2256         if (hls == NULL)
2257             return NULL;
2258
2259         vlc_mutex_lock(&hls->lock);
2260         segment = segment_GetSegment(hls, p_sys->playback.segment);
2261         if (segment == NULL)
2262         {
2263             vlc_mutex_unlock(&hls->lock);
2264             break;
2265         }
2266
2267         vlc_mutex_lock(&p_sys->download.lock_wait);
2268         int i_segment = p_sys->download.segment;
2269         vlc_mutex_unlock(&p_sys->download.lock_wait);
2270
2271         vlc_mutex_lock(&segment->lock);
2272         /* This segment is ready? */
2273         if ((segment->data != NULL) &&
2274             (p_sys->playback.segment < i_segment))
2275         {
2276             p_sys->playback.stream = i_stream;
2277             p_sys->b_cache = hls->b_cache;
2278             vlc_mutex_unlock(&segment->lock);
2279             vlc_mutex_unlock(&hls->lock);
2280             goto check;
2281         }
2282         vlc_mutex_unlock(&segment->lock);
2283         vlc_mutex_unlock(&hls->lock);
2284
2285         if (!p_sys->b_meta)
2286             break;
2287     }
2288     /* */
2289     return NULL;
2290
2291 check:
2292     /* sanity check */
2293     assert(segment->data);
2294     if (segment->data->i_buffer == 0)
2295     {
2296         vlc_mutex_lock(&hls->lock);
2297         int count = vlc_array_count(hls->segments);
2298         vlc_mutex_unlock(&hls->lock);
2299
2300         if ((p_sys->download.segment - p_sys->playback.segment == 0) &&
2301             ((count != p_sys->download.segment) || p_sys->b_live))
2302             msg_Err(s, "playback will stall");
2303         else if ((p_sys->download.segment - p_sys->playback.segment < 3) &&
2304                  ((count != p_sys->download.segment) || p_sys->b_live))
2305             msg_Warn(s, "playback in danger of stalling");
2306     }
2307     return segment;
2308 }
2309
2310 static int segment_RestorePos(segment_t *segment)
2311 {
2312     if (segment->data)
2313     {
2314         uint64_t size = segment->size - segment->data->i_buffer;
2315         if (size > 0)
2316         {
2317             segment->data->i_buffer += size;
2318             segment->data->p_buffer -= size;
2319         }
2320     }
2321     return VLC_SUCCESS;
2322 }
2323
2324 /* p_read might be NULL if caller wants to skip data */
2325 static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read)
2326 {
2327     stream_sys_t *p_sys = s->p_sys;
2328     ssize_t used = 0;
2329
2330     do
2331     {
2332         /* Determine next segment to read. If this is a meta playlist and
2333          * bandwidth conditions changed, then the stream might have switched
2334          * to another bandwidth. */
2335         segment_t *segment = GetSegment(s);
2336         if (segment == NULL)
2337             break;
2338
2339         vlc_mutex_lock(&segment->lock);
2340         if (segment->data->i_buffer == 0)
2341         {
2342             if (!p_sys->b_cache || p_sys->b_live)
2343             {
2344                 block_Release(segment->data);
2345                 segment->data = NULL;
2346             }
2347             else
2348                 segment_RestorePos(segment);
2349
2350             vlc_mutex_unlock(&segment->lock);
2351
2352             /* signal download thread */
2353             vlc_mutex_lock(&p_sys->download.lock_wait);
2354             p_sys->playback.segment++;
2355             vlc_cond_signal(&p_sys->download.wait);
2356             vlc_mutex_unlock(&p_sys->download.lock_wait);
2357             continue;
2358         }
2359
2360         if (segment->size == segment->data->i_buffer)
2361             msg_Dbg(s, "playing segment %d from stream %d",
2362                      segment->sequence, p_sys->playback.stream);
2363
2364         ssize_t len = -1;
2365         if (i_read <= segment->data->i_buffer)
2366             len = i_read;
2367         else if (i_read > segment->data->i_buffer)
2368             len = segment->data->i_buffer;
2369
2370         if (len > 0)
2371         {
2372             if (p_read) /* if NULL, then caller skips data */
2373                 memcpy(p_read + used, segment->data->p_buffer, len);
2374             segment->data->i_buffer -= len;
2375             segment->data->p_buffer += len;
2376             used += len;
2377             i_read -= len;
2378         }
2379         vlc_mutex_unlock(&segment->lock);
2380
2381     } while (i_read > 0);
2382
2383     return used;
2384 }
2385
2386 static int Read(stream_t *s, void *buffer, unsigned int i_read)
2387 {
2388     stream_sys_t *p_sys = s->p_sys;
2389     ssize_t length = 0;
2390
2391     assert(p_sys->hls_stream);
2392
2393     while (length == 0)
2394     {
2395         // In case an error occurred or the stream was closed return 0
2396         if (p_sys->b_error || !vlc_object_alive(s))
2397             return 0;
2398
2399         // Lock the mutex before trying to read to avoid a race condition with the download thread
2400         vlc_mutex_lock(&p_sys->read.lock_wait);
2401
2402         /* NOTE: buffer might be NULL if caller wants to skip data */
2403         length = hls_Read(s, (uint8_t*) buffer, i_read);
2404
2405         // An error has occurred in hls_Read
2406         if (length < 0)
2407         {
2408             vlc_mutex_unlock(&p_sys->read.lock_wait);
2409
2410             return 0;
2411         }
2412
2413         // There is no data available yet for the demuxer so we need to wait until reload and
2414         // download operation are over.
2415         // Download thread will signal once download is finished.
2416         // A timed wait is used to avoid deadlock in case data never arrives since the thread
2417         // running this read operation is also responsible for closing the stream
2418         if (length == 0)
2419         {
2420             mtime_t start = mdate();
2421
2422             // Wait for 10 seconds
2423             mtime_t timeout_limit = start + (10 * CLOCK_FREQ);
2424
2425             int res = vlc_cond_timedwait(&p_sys->read.wait, &p_sys->read.lock_wait, timeout_limit);
2426
2427             // Error - reached a timeout of 10 seconds without data arriving - kill the stream
2428             if (res == ETIMEDOUT)
2429             {
2430                 msg_Warn(s, "timeout limit reached!");
2431
2432                 vlc_mutex_unlock(&p_sys->read.lock_wait);
2433
2434                 return 0;
2435             }
2436             else if (res == EINVAL)
2437                 return 0; // Error - lock is not locked so we can just return
2438         }
2439
2440         vlc_mutex_unlock(&p_sys->read.lock_wait);
2441     }
2442
2443     p_sys->playback.offset += length;
2444     return length;
2445 }
2446
2447 static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek)
2448 {
2449     stream_sys_t *p_sys = s->p_sys;
2450     segment_t *segment;
2451     unsigned int len = i_peek;
2452
2453     segment = GetSegment(s);
2454     if (segment == NULL)
2455     {
2456         msg_Err(s, "segment %d should have been available (stream %d)",
2457                 p_sys->playback.segment, p_sys->playback.stream);
2458         return 0; /* eof? */
2459     }
2460
2461     vlc_mutex_lock(&segment->lock);
2462
2463     size_t i_buff = segment->data->i_buffer;
2464     uint8_t *p_buff = segment->data->p_buffer;
2465
2466     if ( likely(i_peek < i_buff))
2467     {
2468         *pp_peek = p_buff;
2469         vlc_mutex_unlock(&segment->lock);
2470         return i_peek;
2471     }
2472
2473     else /* This will seldom be run */
2474     {
2475         /* remember segment to read */
2476         int peek_segment = p_sys->playback.segment;
2477         size_t curlen = 0;
2478         segment_t *nsegment;
2479         p_sys->playback.segment++;
2480         block_t *peeked = p_sys->peeked;
2481
2482         if (peeked == NULL)
2483             peeked = block_Alloc (i_peek);
2484         else if (peeked->i_buffer < i_peek)
2485             peeked = block_Realloc (peeked, 0, i_peek);
2486         if (peeked == NULL)
2487         {
2488             vlc_mutex_unlock(&segment->lock);
2489             return 0;
2490         }
2491         p_sys->peeked = peeked;
2492
2493         memcpy(peeked->p_buffer, p_buff, i_buff);
2494         curlen = i_buff;
2495         len -= i_buff;
2496         vlc_mutex_unlock(&segment->lock);
2497
2498         i_buff = peeked->i_buffer;
2499         p_buff = peeked->p_buffer;
2500         *pp_peek = p_buff;
2501
2502         while (curlen < i_peek)
2503         {
2504             nsegment = GetSegment(s);
2505             if (nsegment == NULL)
2506             {
2507                 msg_Err(s, "segment %d should have been available (stream %d)",
2508                         p_sys->playback.segment, p_sys->playback.stream);
2509                 /* restore segment to read */
2510                 p_sys->playback.segment = peek_segment;
2511                 return curlen; /* eof? */
2512             }
2513
2514             vlc_mutex_lock(&nsegment->lock);
2515
2516             if (len < nsegment->data->i_buffer)
2517             {
2518                 memcpy(p_buff + curlen, nsegment->data->p_buffer, len);
2519                 curlen += len;
2520             }
2521             else
2522             {
2523                 size_t i_nbuff = nsegment->data->i_buffer;
2524                 memcpy(p_buff + curlen, nsegment->data->p_buffer, i_nbuff);
2525                 curlen += i_nbuff;
2526                 len -= i_nbuff;
2527
2528                 p_sys->playback.segment++;
2529             }
2530
2531             vlc_mutex_unlock(&nsegment->lock);
2532         }
2533
2534         /* restore segment to read */
2535         p_sys->playback.segment = peek_segment;
2536         return curlen;
2537     }
2538 }
2539
2540 static bool hls_MaySeek(stream_t *s)
2541 {
2542     stream_sys_t *p_sys = s->p_sys;
2543
2544     if (p_sys->hls_stream == NULL)
2545         return false;
2546
2547     hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2548     if (hls == NULL) return false;
2549
2550     if (p_sys->b_live)
2551     {
2552         vlc_mutex_lock(&hls->lock);
2553         int count = vlc_array_count(hls->segments);
2554         vlc_mutex_unlock(&hls->lock);
2555
2556         vlc_mutex_lock(&p_sys->download.lock_wait);
2557         bool may_seek = (p_sys->download.segment < (count - 2));
2558         vlc_mutex_unlock(&p_sys->download.lock_wait);
2559         return may_seek;
2560     }
2561     return true;
2562 }
2563
2564 static uint64_t GetStreamSize(stream_t *s)
2565 {
2566     stream_sys_t *p_sys = s->p_sys;
2567
2568     if (p_sys->b_live)
2569         return 0;
2570
2571     hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2572     if (hls == NULL) return 0;
2573
2574     vlc_mutex_lock(&hls->lock);
2575     if (hls->size == 0)
2576         hls->size = hls_GetStreamSize(hls);
2577     uint64_t size = hls->size;
2578     vlc_mutex_unlock(&hls->lock);
2579
2580     return size;
2581 }
2582
2583 static int segment_Seek(stream_t *s, const uint64_t pos)
2584 {
2585     stream_sys_t *p_sys = s->p_sys;
2586
2587     hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2588     if (hls == NULL)
2589         return VLC_EGENERIC;
2590
2591     vlc_mutex_lock(&hls->lock);
2592
2593     bool b_found = false;
2594     uint64_t length = 0;
2595     uint64_t size = hls->size;
2596     int count = vlc_array_count(hls->segments);
2597
2598     segment_t *currentSegment = segment_GetSegment(hls, p_sys->playback.segment);
2599     if (currentSegment == NULL)
2600     {
2601         vlc_mutex_unlock(&hls->lock);
2602         return VLC_EGENERIC;
2603     }
2604
2605     for (int n = 0; n < count; n++)
2606     {
2607         segment_t *segment = segment_GetSegment(hls, n);
2608         if (segment == NULL)
2609         {
2610             vlc_mutex_unlock(&hls->lock);
2611             return VLC_EGENERIC;
2612         }
2613
2614         vlc_mutex_lock(&segment->lock);
2615         length += segment->duration * (hls->bandwidth/8);
2616         vlc_mutex_unlock(&segment->lock);
2617
2618         if (pos <= length)
2619         {
2620             if (count - n >= 3)
2621             {
2622                 p_sys->playback.segment = n;
2623                 b_found = true;
2624                 break;
2625             }
2626             /* Do not search in last 3 segments */
2627             vlc_mutex_unlock(&hls->lock);
2628             return VLC_EGENERIC;
2629         }
2630     }
2631
2632     /* */
2633     if (!b_found && (pos >= size))
2634     {
2635         p_sys->playback.segment = count - 1;
2636         b_found = true;
2637     }
2638
2639     /* */
2640     if (b_found)
2641     {
2642
2643         /* restore current segment to start position */
2644         vlc_mutex_lock(&currentSegment->lock);
2645         segment_RestorePos(currentSegment);
2646         vlc_mutex_unlock(&currentSegment->lock);
2647
2648         /* restore seeked segment to start position */
2649         segment_t *segment = segment_GetSegment(hls, p_sys->playback.segment);
2650         if (segment == NULL)
2651         {
2652             vlc_mutex_unlock(&hls->lock);
2653             return VLC_EGENERIC;
2654         }
2655
2656         vlc_mutex_lock(&segment->lock);
2657         segment_RestorePos(segment);
2658         vlc_mutex_unlock(&segment->lock);
2659
2660         /* start download at current playback segment */
2661         vlc_mutex_unlock(&hls->lock);
2662
2663         /* Wake up download thread */
2664         vlc_mutex_lock(&p_sys->download.lock_wait);
2665         p_sys->download.seek = p_sys->playback.segment;
2666         vlc_cond_signal(&p_sys->download.wait);
2667
2668         /* Wait for download to be finished */
2669         msg_Dbg(s, "seek to segment %d", p_sys->playback.segment);
2670         while ((p_sys->download.seek != -1) ||
2671            ((p_sys->download.segment - p_sys->playback.segment < 3) &&
2672                 (p_sys->download.segment < count)))
2673         {
2674             vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
2675             if (!vlc_object_alive(s) || s->b_error) break;
2676         }
2677         vlc_mutex_unlock(&p_sys->download.lock_wait);
2678
2679         return VLC_SUCCESS;
2680     }
2681     vlc_mutex_unlock(&hls->lock);
2682
2683     return b_found ? VLC_SUCCESS : VLC_EGENERIC;
2684 }
2685
2686 static int Control(stream_t *s, int i_query, va_list args)
2687 {
2688     stream_sys_t *p_sys = s->p_sys;
2689
2690     switch (i_query)
2691     {
2692         case STREAM_CAN_SEEK:
2693             *(va_arg (args, bool *)) = hls_MaySeek(s);
2694             break;
2695         case STREAM_CAN_CONTROL_PACE:
2696         case STREAM_CAN_PAUSE:
2697             *(va_arg (args, bool *)) = true;
2698             break;
2699         case STREAM_CAN_FASTSEEK:
2700             *(va_arg (args, bool *)) = false;
2701             break;
2702         case STREAM_GET_POSITION:
2703             *(va_arg (args, uint64_t *)) = p_sys->playback.offset;
2704             break;
2705         case STREAM_SET_PAUSE_STATE:
2706         {
2707             bool paused = va_arg (args, unsigned);
2708
2709             vlc_mutex_lock(&p_sys->lock);
2710             p_sys->paused = paused;
2711             vlc_cond_signal(&p_sys->wait);
2712             vlc_mutex_unlock(&p_sys->lock);
2713             break;
2714         }
2715         case STREAM_SET_POSITION:
2716             if (hls_MaySeek(s))
2717             {
2718                 uint64_t pos = (uint64_t)va_arg(args, uint64_t);
2719                 if (segment_Seek(s, pos) == VLC_SUCCESS)
2720                 {
2721                     p_sys->playback.offset = pos;
2722                     break;
2723                 }
2724             }
2725             return VLC_EGENERIC;
2726         case STREAM_GET_SIZE:
2727             *(va_arg (args, uint64_t *)) = GetStreamSize(s);
2728             break;
2729         case STREAM_GET_PTS_DELAY:
2730             *va_arg (args, int64_t *) = INT64_C(1000) *
2731                 var_InheritInteger(s, "network-caching");
2732              break;
2733         default:
2734             return VLC_EGENERIC;
2735     }
2736     return VLC_SUCCESS;
2737 }