Add NEWS for FFT windowing
[vlc.git] / modules / stream_filter / httplive.c
blobf9ce835d6b476ae9c2a17b348ef02dec43e98b9f
1 /*****************************************************************************
2 * httplive.c: HTTP Live Streaming stream filter
3 *****************************************************************************
4 * Copyright (C) 2010-2012 M2X BV
5 * $Id$
7 * Author: Jean-Paul Saman <jpsaman _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <limits.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <assert.h>
37 #include <errno.h>
38 #include <gcrypt.h>
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>
46 /*****************************************************************************
47 * Module descriptor
48 *****************************************************************************/
49 static int Open (vlc_object_t *);
50 static void Close(vlc_object_t *);
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()
60 /*****************************************************************************
62 *****************************************************************************/
63 #define AES_BLOCK_SIZE 16 /* Only support AES-128 */
64 typedef struct segment_s
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)*/
71 char *url;
72 char *psz_key_path; /* url key path */
73 uint8_t aes_key[16]; /* AES-128 */
74 bool b_key_loaded;
76 vlc_mutex_t lock;
77 block_t *data; /* data */
78 } segment_t;
80 typedef struct hls_stream_s
82 int id; /* program id */
83 int version; /* protocol version should be 1 */
84 int sequence; /* media sequence number */
85 int duration; /* maximum duration per segment (s) */
86 int max_segment_length; /* maximum duration segments */
87 uint64_t bandwidth; /* bandwidth usage of segments (bits per second)*/
88 uint64_t size; /* stream length is calculated by taking the sum
89 foreach segment of (segment->duration * hls->bandwidth/8) */
91 vlc_array_t *segments; /* list of segments */
92 char *url; /* uri to m3u8 */
93 vlc_mutex_t lock;
94 bool b_cache; /* allow caching */
96 char *psz_current_key_path; /* URL path of the encrypted key */
97 uint8_t psz_AES_IV[AES_BLOCK_SIZE]; /* IV used when decypher the block */
98 bool b_iv_loaded;
99 } hls_stream_t;
101 struct stream_sys_t
103 char *m3u8; /* M3U8 url */
104 vlc_thread_t reload; /* HLS m3u8 reload thread */
105 vlc_thread_t thread; /* HLS segment download thread */
107 block_t *peeked;
109 /* */
110 vlc_array_t *hls_stream; /* bandwidth adaptation */
111 uint64_t bandwidth; /* measured bandwidth (bits per second) */
113 /* Download */
114 struct hls_download_s
116 int stream; /* current hls_stream */
117 int segment; /* current segment for downloading */
118 int seek; /* segment requested by seek (default -1) */
119 vlc_mutex_t lock_wait; /* protect segment download counter */
120 vlc_cond_t wait; /* some condition to wait on */
121 } download;
123 /* Playback */
124 struct hls_playback_s
126 uint64_t offset; /* current offset in media */
127 int stream; /* current hls_stream */
128 int segment; /* current segment for playback */
129 } playback;
131 /* Playlist */
132 struct hls_playlist_s
134 mtime_t last; /* playlist last loaded */
135 mtime_t wakeup; /* next reload time */
136 int tries; /* times it was not changed */
137 } playlist;
139 struct hls_read_s
141 vlc_mutex_t lock_wait; /* used by read condition variable */
142 vlc_cond_t wait; /* some condition to wait on during read */
143 } read;
145 /* state */
146 bool b_cache; /* can cache files */
147 bool b_meta; /* meta playlist */
148 bool b_live; /* live stream? or vod? */
149 bool b_error; /* parsing error */
150 bool b_aesmsg; /* only print one time that the media is encrypted */
152 /* Shared data */
153 vlc_cond_t wait;
154 vlc_mutex_t lock;
155 bool paused;
158 /****************************************************************************
159 * Local prototypes
160 ****************************************************************************/
161 static int Read (stream_t *, void *p_read, unsigned int i_read);
162 static int Peek (stream_t *, const uint8_t **pp_peek, unsigned int i_peek);
163 static int Control(stream_t *, int i_query, va_list);
165 static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer);
166 static ssize_t read_M3U8_from_url(stream_t *s, const char *psz_url, uint8_t **buffer);
167 static char *ReadLine(uint8_t *buffer, uint8_t **pos, size_t len);
169 static int hls_Download(stream_t *s, segment_t *segment);
171 static void* hls_Thread(void *);
172 static void* hls_Reload(void *);
174 static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted);
175 static void segment_Free(segment_t *segment);
177 /****************************************************************************
179 ****************************************************************************/
180 static bool isHTTPLiveStreaming(stream_t *s)
182 const uint8_t *peek;
184 int size = stream_Peek(s->p_source, &peek, 46);
185 if (size < 7)
186 return false;
188 if (memcmp(peek, "#EXTM3U", 7) != 0)
189 return false;
191 peek += 7;
192 size -= 7;
194 /* Parse stream and search for
195 * EXT-X-TARGETDURATION or EXT-X-STREAM-INF tag, see
196 * http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8 */
197 while (size--)
199 static const char *const ext[] = {
200 "TARGETDURATION",
201 "MEDIA-SEQUENCE",
202 "KEY",
203 "ALLOW-CACHE",
204 "ENDLIST",
205 "STREAM-INF",
206 "DISCONTINUITY",
207 "VERSION"
210 if (*peek++ != '#')
211 continue;
213 if (size < 6)
214 continue;
216 if (memcmp(peek, "EXT-X-", 6))
217 continue;
219 peek += 6;
220 size -= 6;
222 for (size_t i = 0; i < ARRAY_SIZE(ext); i++)
224 size_t len = strlen(ext[i]);
225 if (size < 0 || (size_t)size < len)
226 continue;
227 if (!memcmp(peek, ext[i], len))
228 return true;
232 return false;
235 /* HTTP Live Streaming */
236 static hls_stream_t *hls_New(vlc_array_t *hls_stream, const int id, const uint64_t bw, const char *uri)
238 hls_stream_t *hls = (hls_stream_t *)malloc(sizeof(hls_stream_t));
239 if (hls == NULL) return NULL;
241 hls->id = id;
242 hls->bandwidth = bw;
243 hls->duration = -1;/* unknown */
244 hls->max_segment_length = -1;/* unknown */
245 hls->size = 0;
246 hls->sequence = 0; /* default is 0 */
247 hls->version = 1; /* default protocol version */
248 hls->b_cache = true;
249 hls->url = strdup(uri);
250 if (hls->url == NULL)
252 free(hls);
253 return NULL;
255 hls->psz_current_key_path = NULL;
256 hls->segments = vlc_array_new();
257 vlc_array_append(hls_stream, hls);
258 vlc_mutex_init(&hls->lock);
259 return hls;
262 static void hls_Free(hls_stream_t *hls)
264 vlc_mutex_destroy(&hls->lock);
266 if (hls->segments)
268 for (int n = 0; n < vlc_array_count(hls->segments); n++)
270 segment_t *segment = segment_GetSegment(hls, n);
271 if (segment) segment_Free(segment);
273 vlc_array_destroy(hls->segments);
275 free(hls->url);
276 free(hls->psz_current_key_path);
277 free(hls);
280 static hls_stream_t *hls_Copy(hls_stream_t *src, const bool b_cp_segments)
282 assert(src);
283 assert(!b_cp_segments); /* FIXME: copying segments is not implemented */
285 hls_stream_t *dst = (hls_stream_t *)malloc(sizeof(hls_stream_t));
286 if (dst == NULL) return NULL;
288 dst->id = src->id;
289 dst->bandwidth = src->bandwidth;
290 dst->duration = src->duration;
291 dst->size = src->size;
292 dst->sequence = src->sequence;
293 dst->version = src->version;
294 dst->b_cache = src->b_cache;
295 dst->psz_current_key_path = src->psz_current_key_path ?
296 strdup( src->psz_current_key_path ) : NULL;
297 dst->url = strdup(src->url);
298 if (dst->url == NULL)
300 free(dst);
301 return NULL;
303 if (!b_cp_segments)
304 dst->segments = vlc_array_new();
305 vlc_mutex_init(&dst->lock);
306 return dst;
309 static hls_stream_t *hls_Get(vlc_array_t *hls_stream, const int wanted)
311 int count = vlc_array_count(hls_stream);
312 if (count <= 0)
313 return NULL;
314 if ((wanted < 0) || (wanted >= count))
315 return NULL;
316 return (hls_stream_t *) vlc_array_item_at_index(hls_stream, wanted);
319 static inline hls_stream_t *hls_GetFirst(vlc_array_t *hls_stream)
321 return hls_Get(hls_stream, 0);
324 static hls_stream_t *hls_GetLast(vlc_array_t *hls_stream)
326 int count = vlc_array_count(hls_stream);
327 if (count <= 0)
328 return NULL;
329 count--;
330 return hls_Get(hls_stream, count);
333 static hls_stream_t *hls_Find(vlc_array_t *hls_stream, hls_stream_t *hls_new)
335 int count = vlc_array_count(hls_stream);
336 for (int n = 0; n < count; n++)
338 hls_stream_t *hls = hls_Get(hls_stream, n);
339 if (hls)
341 /* compare */
342 if ((hls->id == hls_new->id) &&
343 ((hls->bandwidth == hls_new->bandwidth)||(hls_new->bandwidth==0)))
344 return hls;
347 return NULL;
350 static uint64_t hls_GetStreamSize(hls_stream_t *hls)
352 /* NOTE: Stream size is calculated based on segment duration and
353 * HLS stream bandwidth from the .m3u8 file. If these are not correct
354 * then the deviation from exact byte size will be big and the seek/
355 * progressbar will not behave entirely as one expects. */
356 uint64_t size = 0UL;
358 /* If there is no valid bandwidth yet, then there is no point in
359 * computing stream size. */
360 if (hls->bandwidth == 0)
361 return size;
363 int count = vlc_array_count(hls->segments);
364 for (int n = 0; n < count; n++)
366 segment_t *segment = segment_GetSegment(hls, n);
367 if (segment)
369 size += (segment->duration * (hls->bandwidth / 8));
372 return size;
375 /* Segment */
376 static segment_t *segment_New(hls_stream_t* hls, const int duration, const char *uri)
378 segment_t *segment = (segment_t *)malloc(sizeof(segment_t));
379 if (segment == NULL)
380 return NULL;
382 segment->duration = duration; /* seconds */
383 segment->size = 0; /* bytes */
384 segment->sequence = 0;
385 segment->bandwidth = 0;
386 segment->url = strdup(uri);
387 if (segment->url == NULL)
389 free(segment);
390 return NULL;
392 segment->data = NULL;
393 vlc_array_append(hls->segments, segment);
394 vlc_mutex_init(&segment->lock);
395 segment->b_key_loaded = false;
396 segment->psz_key_path = NULL;
397 if (hls->psz_current_key_path)
398 segment->psz_key_path = strdup(hls->psz_current_key_path);
399 return segment;
402 static void segment_Free(segment_t *segment)
404 vlc_mutex_destroy(&segment->lock);
406 free(segment->url);
407 free(segment->psz_key_path);
408 if (segment->data)
409 block_Release(segment->data);
410 free(segment);
413 static segment_t *segment_GetSegment(hls_stream_t *hls, const int wanted)
415 assert(hls);
417 int count = vlc_array_count(hls->segments);
418 if (count <= 0)
419 return NULL;
420 if ((wanted < 0) || (wanted >= count))
421 return NULL;
422 return (segment_t *) vlc_array_item_at_index(hls->segments, wanted);
425 static segment_t *segment_Find(hls_stream_t *hls, const int sequence)
427 assert(hls);
429 int count = vlc_array_count(hls->segments);
430 if (count <= 0) return NULL;
431 for (int n = 0; n < count; n++)
433 segment_t *segment = segment_GetSegment(hls, n);
434 if (segment == NULL) break;
435 if (segment->sequence == sequence)
436 return segment;
438 return NULL;
441 static int ChooseSegment(stream_t *s, const int current)
443 stream_sys_t *p_sys = (stream_sys_t *)s->p_sys;
444 hls_stream_t *hls = hls_Get(p_sys->hls_stream, current);
445 if (hls == NULL) return 0;
447 /* Choose a segment to start which is no closer than
448 * 3 times the target duration from the end of the playlist.
450 int wanted = 0;
451 int duration = 0;
452 int sequence = 0;
453 int count = vlc_array_count(hls->segments);
454 int i = p_sys->b_live ? count - 1 : -1;
456 /* We do while loop only with live case, otherwise return 0*/
457 while((i >= 0) && (i < count))
459 segment_t *segment = segment_GetSegment(hls, i);
460 assert(segment);
462 if (segment->duration > hls->duration)
464 msg_Err(s, "EXTINF:%d duration is larger than EXT-X-TARGETDURATION:%d",
465 segment->duration, hls->duration);
468 duration += segment->duration;
469 if (duration >= 3 * hls->duration)
471 /* Start point found */
472 wanted = i;
473 sequence = segment->sequence;
474 break;
477 i-- ;
480 msg_Dbg(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
481 return wanted;
484 /* Parsing */
485 static char *parse_Attributes(const char *line, const char *attr)
487 char *p;
488 char *begin = (char *) line;
489 char *end = begin + strlen(line);
491 /* Find start of attributes */
492 if ((p = strchr(begin, ':' )) == NULL)
493 return NULL;
495 begin = p;
498 if (strncasecmp(begin, attr, strlen(attr)) == 0
499 && begin[strlen(attr)] == '=')
501 /* <attr>="<value>"[,]* */
502 p = strchr(begin, ',');
503 begin += strlen(attr) + 1;
505 /* Check if we have " " marked value*/
506 if( begin[0] == '"' )
508 char *valueend = strchr( begin+1, '"');
510 /* No ending " so bail out */
511 if( unlikely( !valueend ) )
512 return NULL;
514 p = strchr( valueend, ',');
516 if (begin >= end)
517 return NULL;
518 if (p == NULL) /* last attribute */
519 return strndup(begin, end - begin);
520 /* copy till ',' */
521 return strndup(begin, p - begin);
523 begin++;
524 } while(begin < end);
526 return NULL;
529 static int string_to_IV(char *string_hexa, uint8_t iv[AES_BLOCK_SIZE])
531 unsigned long long iv_hi, iv_lo;
532 char *end = NULL;
533 if (*string_hexa++ != '0')
534 return VLC_EGENERIC;
535 if (*string_hexa != 'x' && *string_hexa != 'X')
536 return VLC_EGENERIC;
538 string_hexa++;
540 size_t len = strlen(string_hexa);
541 if (len <= 16) {
542 iv_hi = 0;
543 iv_lo = strtoull(string_hexa, &end, 16);
544 if (end)
545 return VLC_EGENERIC;
546 } else {
547 iv_lo = strtoull(&string_hexa[len-16], NULL, 16);
548 if (end)
549 return VLC_EGENERIC;
550 string_hexa[len-16] = '\0';
551 iv_hi = strtoull(string_hexa, NULL, 16);
552 if (end)
553 return VLC_EGENERIC;
556 for (int i = 7; i >= 0 ; --i) {
557 iv[ i] = iv_hi & 0xff;
558 iv[8+i] = iv_lo & 0xff;
559 iv_hi >>= 8;
560 iv_lo >>= 8;
563 return VLC_SUCCESS;
566 static char *relative_URI(const char *psz_url, const char *psz_path)
568 char *ret = NULL;
569 assert(psz_url != NULL && psz_path != NULL);
572 //If the path is actually an absolute URL, don't do anything.
573 if (strncmp(psz_path, "http", 4) == 0)
574 return NULL;
576 size_t len = strlen(psz_path);
578 char *new_url = strdup(psz_url);
579 if (unlikely(!new_url))
580 return NULL;
582 if( psz_path[0] == '/' ) //Relative URL with absolute path
584 //Try to find separator for name and path, try to skip
585 //access and first ://
586 char *slash = strchr(&new_url[8], '/');
587 if (unlikely(slash == NULL))
588 goto end;
589 *slash = '\0';
590 } else {
591 int levels = 0;
592 while(len >= 3 && !strncmp(psz_path, "../", 3)) {
593 psz_path += 3;
594 len -= 3;
595 levels++;
597 do {
598 char *slash = strrchr(new_url, '/');
599 if (unlikely(slash == NULL))
600 goto end;
601 *slash = '\0';
602 } while (levels--);
605 if (asprintf(&ret, "%s/%s", new_url, psz_path) < 0)
606 ret = NULL;
608 end:
609 free(new_url);
610 return ret;
613 static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *duration)
615 assert(hls);
616 assert(p_read);
618 /* strip of #EXTINF: */
619 char *p_next = NULL;
620 char *token = strtok_r(p_read, ":", &p_next);
621 if (token == NULL)
622 return VLC_EGENERIC;
624 /* read duration */
625 token = strtok_r(NULL, ",", &p_next);
626 if (token == NULL)
627 return VLC_EGENERIC;
629 int value;
630 char *endptr;
631 if (hls->version < 3)
633 errno = 0;
634 value = strtol(token, &endptr, 10);
635 if (token == endptr || errno == ERANGE)
637 *duration = -1;
638 return VLC_EGENERIC;
640 *duration = value;
642 else
644 errno = 0;
645 double d = strtof(token, &endptr);
646 if (token == endptr || errno == ERANGE)
648 *duration = -1;
649 return VLC_EGENERIC;
651 if ((d) - ((int)d) >= 0.5)
652 value = ((int)d) + 1;
653 else
654 value = ((int)d);
655 *duration = value;
657 if( *duration > hls->max_segment_length)
658 hls->max_segment_length = *duration;
660 /* Ignore the rest of the line */
661 return VLC_SUCCESS;
664 static int parse_AddSegment(hls_stream_t *hls, const int duration, const char *uri)
666 assert(hls);
667 assert(uri);
669 /* Store segment information */
670 vlc_mutex_lock(&hls->lock);
672 char *psz_uri = relative_URI(hls->url, uri);
674 segment_t *segment = segment_New(hls, duration, psz_uri ? psz_uri : uri);
675 if (segment)
676 segment->sequence = hls->sequence + vlc_array_count(hls->segments) - 1;
677 free(psz_uri);
679 vlc_mutex_unlock(&hls->lock);
681 return segment ? VLC_SUCCESS : VLC_ENOMEM;
684 static int parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
686 assert(hls);
688 int duration = -1;
689 int ret = sscanf(p_read, "#EXT-X-TARGETDURATION:%d", &duration);
690 if (ret != 1)
692 msg_Err(s, "expected #EXT-X-TARGETDURATION:<s>");
693 return VLC_EGENERIC;
696 hls->duration = duration; /* seconds */
697 return VLC_SUCCESS;
700 static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
701 hls_stream_t **hls, char *p_read, const char *uri)
703 int id;
704 uint64_t bw;
705 char *attr;
707 assert(*hls == NULL);
709 attr = parse_Attributes(p_read, "PROGRAM-ID");
710 if (attr)
712 id = atol(attr);
713 free(attr);
715 else
716 id = 0;
718 attr = parse_Attributes(p_read, "BANDWIDTH");
719 if (attr == NULL)
721 msg_Err(s, "#EXT-X-STREAM-INF: expected BANDWIDTH=<value>");
722 return VLC_EGENERIC;
724 bw = atoll(attr);
725 free(attr);
727 if (bw == 0)
729 msg_Err(s, "#EXT-X-STREAM-INF: bandwidth cannot be 0");
730 return VLC_EGENERIC;
733 msg_Dbg(s, "bandwidth adaptation detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
735 char *psz_uri = relative_URI(s->p_sys->m3u8, uri);
737 *hls = hls_New(*hls_stream, id, bw, psz_uri ? psz_uri : uri);
739 free(psz_uri);
741 return (*hls == NULL) ? VLC_ENOMEM : VLC_SUCCESS;
744 static int parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
746 assert(hls);
748 int sequence;
749 int ret = sscanf(p_read, "#EXT-X-MEDIA-SEQUENCE:%d", &sequence);
750 if (ret != 1)
752 msg_Err(s, "expected #EXT-X-MEDIA-SEQUENCE:<s>");
753 return VLC_EGENERIC;
756 if (hls->sequence > 0)
758 if (s->p_sys->b_live)
760 hls_stream_t *last = hls_GetLast(s->p_sys->hls_stream);
761 segment_t *last_segment = segment_GetSegment( last, vlc_array_count( last->segments ) - 1 );
762 if ( ( last_segment->sequence < sequence) &&
763 ( sequence - last_segment->sequence >= 1 ))
764 msg_Err(s, "EXT-X-MEDIA-SEQUENCE gap in playlist (new=%d, old=%d)",
765 sequence, last_segment->sequence);
767 else
768 msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist (new=%d, old=%d)",
769 sequence, hls->sequence);
771 hls->sequence = sequence;
772 return VLC_SUCCESS;
775 static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
777 assert(hls);
779 /* #EXT-X-KEY:METHOD=<method>[,URI="<URI>"][,IV=<IV>] */
780 int err = VLC_SUCCESS;
781 char *attr = parse_Attributes(p_read, "METHOD");
782 if (attr == NULL)
784 msg_Err(s, "#EXT-X-KEY: expected METHOD=<value>");
785 return err;
788 if (strncasecmp(attr, "NONE", 4) == 0)
790 char *uri = parse_Attributes(p_read, "URI");
791 if (uri != NULL)
793 msg_Err(s, "#EXT-X-KEY: URI not expected");
794 err = VLC_EGENERIC;
796 free(uri);
797 /* IV is only supported in version 2 and above */
798 if (hls->version >= 2)
800 char *iv = parse_Attributes(p_read, "IV");
801 if (iv != NULL)
803 msg_Err(s, "#EXT-X-KEY: IV not expected");
804 err = VLC_EGENERIC;
806 free(iv);
809 else if (strncasecmp(attr, "AES-128", 7) == 0)
811 char *value, *uri, *iv;
812 if (s->p_sys->b_aesmsg == false)
814 msg_Dbg(s, "playback of AES-128 encrypted HTTP Live media detected.");
815 s->p_sys->b_aesmsg = true;
817 value = uri = parse_Attributes(p_read, "URI");
818 if (value == NULL)
820 msg_Err(s, "#EXT-X-KEY: URI not found for encrypted HTTP Live media in AES-128");
821 free(attr);
822 return VLC_EGENERIC;
825 /* Url is put between quotes, remove them */
826 if (*value == '"')
828 /* We need to strip the "" from the attribute value */
829 uri = value + 1;
830 char* end = strchr(uri, '"');
831 if (end != NULL)
832 *end = 0;
834 /* For absolute URI, just duplicate it
835 * don't limit to HTTP, maybe some sanity checking
836 * should be done more in here? */
837 if( strstr( uri , "://" ) )
838 hls->psz_current_key_path = strdup( uri );
839 else
840 hls->psz_current_key_path = relative_URI(hls->url, uri);
841 free(value);
843 value = iv = parse_Attributes(p_read, "IV");
844 if (iv == NULL)
847 * If the EXT-X-KEY tag does not have the IV attribute, implementations
848 * MUST use the sequence number of the media file as the IV when
849 * encrypting or decrypting that media file. The big-endian binary
850 * representation of the sequence number SHALL be placed in a 16-octet
851 * buffer and padded (on the left) with zeros.
853 hls->b_iv_loaded = false;
855 else
858 * If the EXT-X-KEY tag has the IV attribute, implementations MUST use
859 * the attribute value as the IV when encrypting or decrypting with that
860 * key. The value MUST be interpreted as a 128-bit hexadecimal number
861 * and MUST be prefixed with 0x or 0X.
864 if (string_to_IV(iv, hls->psz_AES_IV) == VLC_EGENERIC)
866 msg_Err(s, "IV invalid");
867 err = VLC_EGENERIC;
869 else
870 hls->b_iv_loaded = true;
871 free(value);
874 else
876 msg_Warn(s, "playback of encrypted HTTP Live media is not supported.");
877 err = VLC_EGENERIC;
879 free(attr);
880 return err;
883 static int parse_ProgramDateTime(stream_t *s, hls_stream_t *hls, char *p_read)
885 VLC_UNUSED(hls);
886 msg_Dbg(s, "tag not supported: #EXT-X-PROGRAM-DATE-TIME %s", p_read);
887 return VLC_SUCCESS;
890 static int parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read)
892 assert(hls);
894 char answer[4] = "\0";
895 int ret = sscanf(p_read, "#EXT-X-ALLOW-CACHE:%3s", answer);
896 if (ret != 1)
898 msg_Err(s, "#EXT-X-ALLOW-CACHE, ignoring ...");
899 return VLC_EGENERIC;
902 hls->b_cache = (strncmp(answer, "NO", 2) != 0);
903 return VLC_SUCCESS;
906 static int parse_Version(stream_t *s, hls_stream_t *hls, char *p_read)
908 assert(hls);
910 int version;
911 int ret = sscanf(p_read, "#EXT-X-VERSION:%d", &version);
912 if (ret != 1)
914 msg_Err(s, "#EXT-X-VERSION: no protocol version found, should be version 1.");
915 return VLC_EGENERIC;
918 /* Check version */
919 hls->version = version;
920 if (hls->version <= 0 || hls->version > 3)
922 msg_Err(s, "#EXT-X-VERSION should be version 1, 2 or 3 iso %d", version);
923 return VLC_EGENERIC;
925 return VLC_SUCCESS;
928 static int parse_EndList(stream_t *s, hls_stream_t *hls)
930 assert(hls);
932 s->p_sys->b_live = false;
933 msg_Dbg(s, "video on demand (vod) mode");
934 return VLC_SUCCESS;
937 static int parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
939 assert(hls);
941 /* FIXME: Do we need to act on discontinuity ?? */
942 msg_Dbg(s, "#EXT-X-DISCONTINUITY %s", p_read);
943 return VLC_SUCCESS;
946 static int hls_CompareStreams( const void* a, const void* b )
948 hls_stream_t* stream_a = *(hls_stream_t**)a;
949 hls_stream_t* stream_b = *(hls_stream_t**)b;
951 return stream_a->bandwidth - stream_b->bandwidth;
954 /* The http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8
955 * document defines the following new tags: EXT-X-TARGETDURATION,
956 * EXT-X-MEDIA-SEQUENCE, EXT-X-KEY, EXT-X-PROGRAM-DATE-TIME, EXT-X-
957 * ALLOW-CACHE, EXT-X-STREAM-INF, EXT-X-ENDLIST, EXT-X-DISCONTINUITY,
958 * and EXT-X-VERSION.
960 static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const ssize_t len)
962 stream_sys_t *p_sys = s->p_sys;
963 uint8_t *p_read, *p_begin, *p_end;
965 assert(streams);
966 assert(buffer);
968 msg_Dbg(s, "parse_M3U8\n%s", buffer);
969 p_begin = buffer;
970 p_end = p_begin + len;
972 char *line = ReadLine(p_begin, &p_read, p_end - p_begin);
973 if (line == NULL)
974 return VLC_ENOMEM;
975 p_begin = p_read;
977 if (strncmp(line, "#EXTM3U", 7) != 0)
979 msg_Err(s, "missing #EXTM3U tag .. aborting");
980 free(line);
981 return VLC_EGENERIC;
984 free(line);
985 line = NULL;
987 /* What is the version ? */
988 int version = 1;
989 uint8_t *p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-VERSION:");
990 if (p != NULL)
992 uint8_t *tmp = NULL;
993 char *psz_version = ReadLine(p, &tmp, p_end - p);
994 if (psz_version == NULL)
995 return VLC_ENOMEM;
996 int ret = sscanf((const char*)psz_version, "#EXT-X-VERSION:%d", &version);
997 if (ret != 1)
999 msg_Warn(s, "#EXT-X-VERSION: no protocol version found, assuming version 1.");
1000 version = 1;
1002 free(psz_version);
1003 p = NULL;
1006 /* Is it a live stream ? */
1007 p_sys->b_live = (strstr((const char *)buffer, "#EXT-X-ENDLIST") == NULL) ? true : false;
1009 /* Is it a meta index file ? */
1010 bool b_meta = (strstr((const char *)buffer, "#EXT-X-STREAM-INF") == NULL) ? false : true;
1012 int err = VLC_SUCCESS;
1014 if (b_meta)
1016 msg_Dbg(s, "Meta playlist");
1018 /* M3U8 Meta Index file */
1019 do {
1020 /* Next line */
1021 line = ReadLine(p_begin, &p_read, p_end - p_begin);
1022 if (line == NULL)
1023 break;
1024 p_begin = p_read;
1026 /* */
1027 if (strncmp(line, "#EXT-X-STREAM-INF", 17) == 0)
1029 p_sys->b_meta = true;
1030 char *uri = ReadLine(p_begin, &p_read, p_end - p_begin);
1031 if (uri == NULL)
1032 err = VLC_ENOMEM;
1033 else
1035 if (*uri == '#')
1037 msg_Warn(s, "Skipping invalid stream-inf: %s", uri);
1038 free(uri);
1040 else
1042 bool new_stream_added = false;
1043 hls_stream_t *hls = NULL;
1044 err = parse_StreamInformation(s, &streams, &hls, line, uri);
1045 if (err == VLC_SUCCESS)
1046 new_stream_added = true;
1048 free(uri);
1050 if (hls)
1052 /* Download playlist file from server */
1053 uint8_t *buf = NULL;
1054 ssize_t len = read_M3U8_from_url(s, hls->url, &buf);
1055 if (len < 0)
1057 msg_Warn(s, "failed to read %s, continue for other streams", hls->url);
1059 /* remove stream just added */
1060 if (new_stream_added)
1061 vlc_array_remove(streams, vlc_array_count(streams) - 1);
1063 /* ignore download error, so we have chance to try other streams */
1064 err = VLC_SUCCESS;
1066 else
1068 /* Parse HLS m3u8 content. */
1069 err = parse_M3U8(s, streams, buf, len);
1070 free(buf);
1073 hls->version = version;
1074 if (!p_sys->b_live)
1075 hls->size = hls_GetStreamSize(hls); /* Stream size (approximate) */
1079 p_begin = p_read;
1082 free(line);
1083 line = NULL;
1085 if (p_begin >= p_end)
1086 break;
1088 } while (err == VLC_SUCCESS);
1090 size_t stream_count = vlc_array_count(streams);
1091 msg_Dbg(s, "%d streams loaded in Meta playlist", (int)stream_count);
1092 if (stream_count == 0)
1094 msg_Err(s, "No playable streams found in Meta playlist");
1095 err = VLC_EGENERIC;
1098 else
1100 msg_Dbg(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
1102 hls_stream_t *hls = NULL;
1103 if (p_sys->b_meta)
1104 hls = hls_GetLast(streams);
1105 else
1107 /* No Meta playlist used */
1108 hls = hls_New(streams, 0, 0, p_sys->m3u8);
1109 if (hls)
1111 /* Get TARGET-DURATION first */
1112 p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-TARGETDURATION:");
1113 if (p)
1115 uint8_t *p_rest = NULL;
1116 char *psz_duration = ReadLine(p, &p_rest, p_end - p);
1117 if (psz_duration == NULL)
1118 return VLC_EGENERIC;
1119 err = parse_TargetDuration(s, hls, psz_duration);
1120 free(psz_duration);
1121 p = NULL;
1124 /* Store version */
1125 hls->version = version;
1127 else return VLC_ENOMEM;
1129 assert(hls);
1131 /* */
1132 bool media_sequence_loaded = false;
1133 int segment_duration = -1;
1136 /* Next line */
1137 line = ReadLine(p_begin, &p_read, p_end - p_begin);
1138 if (line == NULL)
1139 break;
1140 p_begin = p_read;
1142 if (strncmp(line, "#EXTINF", 7) == 0)
1143 err = parse_SegmentInformation(hls, line, &segment_duration);
1144 else if (strncmp(line, "#EXT-X-TARGETDURATION", 21) == 0)
1145 err = parse_TargetDuration(s, hls, line);
1146 else if (strncmp(line, "#EXT-X-MEDIA-SEQUENCE", 21) == 0)
1148 /* A Playlist file MUST NOT contain more than one EXT-X-MEDIA-SEQUENCE tag. */
1149 /* We only care about first one */
1150 if (!media_sequence_loaded)
1152 err = parse_MediaSequence(s, hls, line);
1153 media_sequence_loaded = true;
1156 else if (strncmp(line, "#EXT-X-KEY", 10) == 0)
1157 err = parse_Key(s, hls, line);
1158 else if (strncmp(line, "#EXT-X-PROGRAM-DATE-TIME", 24) == 0)
1159 err = parse_ProgramDateTime(s, hls, line);
1160 else if (strncmp(line, "#EXT-X-ALLOW-CACHE", 18) == 0)
1161 err = parse_AllowCache(s, hls, line);
1162 else if (strncmp(line, "#EXT-X-DISCONTINUITY", 20) == 0)
1163 err = parse_Discontinuity(s, hls, line);
1164 else if (strncmp(line, "#EXT-X-VERSION", 14) == 0)
1165 err = parse_Version(s, hls, line);
1166 else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0)
1167 err = parse_EndList(s, hls);
1168 else if ((strncmp(line, "#", 1) != 0) && (*line != '\0') )
1170 err = parse_AddSegment(hls, segment_duration, line);
1171 segment_duration = -1; /* reset duration */
1174 free(line);
1175 line = NULL;
1177 if (p_begin >= p_end)
1178 break;
1180 } while (err == VLC_SUCCESS);
1182 free(line);
1185 return err;
1189 static int hls_DownloadSegmentKey(stream_t *s, segment_t *seg)
1191 stream_t *p_m3u8 = stream_UrlNew(s, seg->psz_key_path);
1192 if (p_m3u8 == NULL)
1194 msg_Err(s, "Failed to load the AES key for segment sequence %d", seg->sequence);
1195 return VLC_EGENERIC;
1198 int len = stream_Read(p_m3u8, seg->aes_key, sizeof(seg->aes_key));
1199 stream_Delete(p_m3u8);
1200 if (len != AES_BLOCK_SIZE)
1202 msg_Err(s, "The AES key loaded doesn't have the right size (%d)", len);
1203 return VLC_EGENERIC;
1206 return VLC_SUCCESS;
1209 static int hls_ManageSegmentKeys(stream_t *s, hls_stream_t *hls)
1211 segment_t *seg = NULL;
1212 segment_t *prev_seg;
1213 int count = vlc_array_count(hls->segments);
1215 for (int i = 0; i < count; i++)
1217 prev_seg = seg;
1218 seg = segment_GetSegment(hls, i);
1219 if (seg == NULL )
1220 continue;
1221 if (seg->psz_key_path == NULL)
1222 continue; /* No key to load ? continue */
1223 if (seg->b_key_loaded)
1224 continue; /* The key is already loaded */
1226 /* if the key has not changed, and already available from previous segment,
1227 * try to copy it, and don't load the key */
1228 if (prev_seg && prev_seg->b_key_loaded && strcmp(seg->psz_key_path, prev_seg->psz_key_path) == 0)
1230 memcpy(seg->aes_key, prev_seg->aes_key, AES_BLOCK_SIZE);
1231 seg->b_key_loaded = true;
1232 continue;
1234 if (hls_DownloadSegmentKey(s, seg) != VLC_SUCCESS)
1235 return VLC_EGENERIC;
1236 seg->b_key_loaded = true;
1238 return VLC_SUCCESS;
1241 static int hls_DecodeSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segment)
1243 /* Did the segment need to be decoded ? */
1244 if (segment->psz_key_path == NULL)
1245 return VLC_SUCCESS;
1247 /* Do we have loaded the key ? */
1248 if (!segment->b_key_loaded)
1250 /* No ? try to download it now */
1251 if (hls_ManageSegmentKeys(s, hls) != VLC_SUCCESS)
1252 return VLC_EGENERIC;
1255 /* For now, we only decode AES-128 data */
1256 gcry_error_t i_gcrypt_err;
1257 gcry_cipher_hd_t aes_ctx;
1258 /* Setup AES */
1259 i_gcrypt_err = gcry_cipher_open(&aes_ctx, GCRY_CIPHER_AES,
1260 GCRY_CIPHER_MODE_CBC, 0);
1261 if (i_gcrypt_err)
1263 msg_Err(s, "gcry_cipher_open failed: %s", gpg_strerror(i_gcrypt_err));
1264 gcry_cipher_close(aes_ctx);
1265 return VLC_EGENERIC;
1268 /* Set key */
1269 i_gcrypt_err = gcry_cipher_setkey(aes_ctx, segment->aes_key,
1270 sizeof(segment->aes_key));
1271 if (i_gcrypt_err)
1273 msg_Err(s, "gcry_cipher_setkey failed: %s", gpg_strerror(i_gcrypt_err));
1274 gcry_cipher_close(aes_ctx);
1275 return VLC_EGENERIC;
1278 if (hls->b_iv_loaded == false)
1280 memset(hls->psz_AES_IV, 0, AES_BLOCK_SIZE);
1281 hls->psz_AES_IV[15] = segment->sequence & 0xff;
1282 hls->psz_AES_IV[14] = (segment->sequence >> 8)& 0xff;
1283 hls->psz_AES_IV[13] = (segment->sequence >> 16)& 0xff;
1284 hls->psz_AES_IV[12] = (segment->sequence >> 24)& 0xff;
1287 i_gcrypt_err = gcry_cipher_setiv(aes_ctx, hls->psz_AES_IV,
1288 sizeof(hls->psz_AES_IV));
1290 if (i_gcrypt_err)
1292 msg_Err(s, "gcry_cipher_setiv failed: %s", gpg_strerror(i_gcrypt_err));
1293 gcry_cipher_close(aes_ctx);
1294 return VLC_EGENERIC;
1297 i_gcrypt_err = gcry_cipher_decrypt(aes_ctx,
1298 segment->data->p_buffer, /* out */
1299 segment->data->i_buffer,
1300 NULL, /* in */
1302 if (i_gcrypt_err)
1304 msg_Err(s, "gcry_cipher_decrypt failed: %s/%s\n", gcry_strsource(i_gcrypt_err), gcry_strerror(i_gcrypt_err));
1305 gcry_cipher_close(aes_ctx);
1306 return VLC_EGENERIC;
1308 gcry_cipher_close(aes_ctx);
1309 /* remove the PKCS#7 padding from the buffer */
1310 int pad = segment->data->p_buffer[segment->data->i_buffer-1];
1311 if (pad <= 0 || pad > AES_BLOCK_SIZE)
1313 msg_Err(s, "Bad padding character (0x%x), perhaps we failed to decrypt the segment with the correct key", pad);
1314 return VLC_EGENERIC;
1316 int count = pad;
1317 while (count--)
1319 if (segment->data->p_buffer[segment->data->i_buffer-1-count] != pad)
1321 msg_Err(s, "Bad ending buffer, perhaps we failed to decrypt the segment with the correct key");
1322 return VLC_EGENERIC;
1326 /* not all the data is readable because of padding */
1327 segment->data->i_buffer -= pad;
1329 return VLC_SUCCESS;
1332 static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
1334 stream_sys_t *p_sys = s->p_sys;
1335 assert(*streams);
1336 int err = VLC_EGENERIC;
1338 /* Duplicate HLS stream META information */
1339 for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
1341 hls_stream_t *src, *dst;
1342 src = hls_Get(p_sys->hls_stream, i);
1343 if (src == NULL)
1344 return VLC_EGENERIC;
1346 dst = hls_Copy(src, false);
1347 if (dst == NULL)
1348 return VLC_ENOMEM;
1349 vlc_array_append(*streams, dst);
1351 /* Download playlist file from server */
1352 uint8_t *buf = NULL;
1353 ssize_t len = read_M3U8_from_url(s, dst->url, &buf);
1354 if (len < 0)
1355 err = VLC_EGENERIC;
1356 else
1358 /* Parse HLS m3u8 content. */
1359 err = parse_M3U8(s, *streams, buf, len);
1360 free(buf);
1363 return err;
1366 /* Update hls_old (an existing member of p_sys->hls_stream) to match hls_new
1367 (which represents a downloaded, perhaps newer version of the same playlist) */
1368 static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *hls_old, bool *stream_appended)
1370 int count = vlc_array_count(hls_new->segments);
1372 msg_Dbg(s, "updating hls stream (program-id=%d, bandwidth=%"PRIu64") has %d segments",
1373 hls_new->id, hls_new->bandwidth, count);
1375 vlc_mutex_lock(&hls_old->lock);
1376 hls_old->max_segment_length=-1;
1377 for (int n = 0; n < count; n++)
1379 segment_t *p = segment_GetSegment(hls_new, n);
1380 if (p == NULL)
1382 vlc_mutex_unlock(&hls_old->lock);
1383 return VLC_EGENERIC;
1386 segment_t *segment = segment_Find(hls_old, p->sequence);
1387 if (segment)
1389 vlc_mutex_lock(&segment->lock);
1391 assert(p->url);
1392 assert(segment->url);
1394 /* they should be the same */
1395 if ((p->sequence != segment->sequence) ||
1396 (p->duration != segment->duration) ||
1397 (strcmp(p->url, segment->url) != 0))
1399 msg_Warn(s, "existing segment found with different content - resetting");
1400 msg_Warn(s, "- sequence: new=%d, old=%d", p->sequence, segment->sequence);
1401 msg_Warn(s, "- duration: new=%d, old=%d", p->duration, segment->duration);
1402 msg_Warn(s, "- file: new=%s", p->url);
1403 msg_Warn(s, " old=%s", segment->url);
1405 /* Resetting content */
1406 segment->sequence = p->sequence;
1407 segment->duration = p->duration;
1408 free(segment->url);
1409 segment->url = strdup(p->url);
1410 if ( segment->url == NULL )
1412 msg_Err(s, "Failed updating segment %d - skipping it", p->sequence);
1413 segment_Free(p);
1414 vlc_mutex_unlock(&segment->lock);
1415 continue;
1417 /* We must free the content, because if the key was not downloaded, content can't be decrypted */
1418 if ((p->psz_key_path || p->b_key_loaded) &&
1419 segment->data)
1421 block_Release(segment->data);
1422 segment->data = NULL;
1424 free(segment->psz_key_path);
1425 segment->psz_key_path = p->psz_key_path ? strdup(p->psz_key_path) : NULL;
1426 segment_Free(p);
1428 vlc_mutex_unlock(&segment->lock);
1430 else
1432 int last = vlc_array_count(hls_old->segments) - 1;
1433 segment_t *l = segment_GetSegment(hls_old, last);
1434 if (l == NULL) {
1435 vlc_mutex_unlock(&hls_old->lock);
1436 return VLC_EGENERIC;
1439 if ((l->sequence + 1) != p->sequence)
1441 msg_Err(s, "gap in sequence numbers found: new=%d expected %d",
1442 p->sequence, l->sequence+1);
1444 vlc_array_append(hls_old->segments, p);
1445 msg_Dbg(s, "- segment %d appended", p->sequence);
1446 hls_old->max_segment_length = __MAX(hls_old->max_segment_length, p->duration);
1447 msg_Dbg(s, " - segments new max duration %d", hls_old->max_segment_length);
1449 // Signal download thread otherwise the segment will not get downloaded
1450 *stream_appended = true;
1454 /* update meta information */
1455 hls_old->sequence = hls_new->sequence;
1456 hls_old->duration = (hls_new->duration == -1) ? hls_old->duration : hls_new->duration;
1457 hls_old->b_cache = hls_new->b_cache;
1458 vlc_mutex_unlock(&hls_old->lock);
1459 return VLC_SUCCESS;
1463 static int hls_ReloadPlaylist(stream_t *s)
1465 stream_sys_t *p_sys = s->p_sys;
1467 // Flag to indicate if we should signal download thread
1468 bool stream_appended = false;
1470 vlc_array_t *hls_streams = vlc_array_new();
1471 if (hls_streams == NULL)
1472 return VLC_ENOMEM;
1474 msg_Dbg(s, "Reloading HLS live meta playlist");
1476 if (get_HTTPLiveMetaPlaylist(s, &hls_streams) != VLC_SUCCESS)
1478 /* Free hls streams */
1479 for (int i = 0; i < vlc_array_count(hls_streams); i++)
1481 hls_stream_t *hls;
1482 hls = hls_Get(hls_streams, i);
1483 if (hls) hls_Free(hls);
1485 vlc_array_destroy(hls_streams);
1487 msg_Err(s, "reloading playlist failed");
1488 return VLC_EGENERIC;
1491 /* merge playlists */
1492 int count = vlc_array_count(hls_streams);
1493 for (int n = 0; n < count; n++)
1495 hls_stream_t *hls_new = hls_Get(hls_streams, n);
1496 if (hls_new == NULL)
1497 continue;
1499 hls_stream_t *hls_old = hls_Find(p_sys->hls_stream, hls_new);
1500 if (hls_old == NULL)
1501 { /* new hls stream - append */
1502 vlc_array_append(p_sys->hls_stream, hls_new);
1503 msg_Dbg(s, "new HLS stream appended (id=%d, bandwidth=%"PRIu64")",
1504 hls_new->id, hls_new->bandwidth);
1506 // New segment available - signal download thread
1507 stream_appended = true;
1509 else if (hls_UpdatePlaylist(s, hls_new, hls_old, &stream_appended) != VLC_SUCCESS)
1510 msg_Warn(s, "failed updating HLS stream (id=%d, bandwidth=%"PRIu64")",
1511 hls_new->id, hls_new->bandwidth);
1513 vlc_array_destroy(hls_streams);
1515 // Must signal the download thread otherwise new segments will not be downloaded at all!
1516 if (stream_appended == true)
1518 vlc_mutex_lock(&p_sys->download.lock_wait);
1519 vlc_cond_signal(&p_sys->download.wait);
1520 vlc_mutex_unlock(&p_sys->download.lock_wait);
1523 return VLC_SUCCESS;
1526 /****************************************************************************
1527 * hls_Thread
1528 ****************************************************************************/
1529 static int BandwidthAdaptation(stream_t *s, int progid, uint64_t *bandwidth)
1531 stream_sys_t *p_sys = s->p_sys;
1532 int candidate = -1;
1533 uint64_t bw = *bandwidth;
1534 uint64_t bw_candidate = 0;
1536 int count = vlc_array_count(p_sys->hls_stream);
1537 for (int n = 0; n < count; n++)
1539 /* Select best bandwidth match */
1540 hls_stream_t *hls = hls_Get(p_sys->hls_stream, n);
1541 if (hls == NULL) break;
1543 /* only consider streams with the same PROGRAM-ID */
1544 if (hls->id == progid)
1546 if ((bw >= hls->bandwidth) && (bw_candidate < hls->bandwidth))
1548 msg_Dbg(s, "candidate %d bandwidth (bits/s) %"PRIu64" >= %"PRIu64,
1549 n, bw, hls->bandwidth); /* bits / s */
1550 bw_candidate = hls->bandwidth;
1551 candidate = n; /* possible candidate */
1555 *bandwidth = bw_candidate;
1556 return candidate;
1559 static int hls_DownloadSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur_stream)
1561 stream_sys_t *p_sys = s->p_sys;
1563 assert(hls);
1564 assert(segment);
1566 vlc_mutex_lock(&segment->lock);
1567 if (segment->data != NULL)
1569 /* Segment already downloaded */
1570 vlc_mutex_unlock(&segment->lock);
1571 return VLC_SUCCESS;
1574 /* sanity check - can we download this segment on time? */
1575 if ((p_sys->bandwidth > 0) && (hls->bandwidth > 0))
1577 uint64_t size = (segment->duration * hls->bandwidth); /* bits */
1578 int estimated = (int)(size / p_sys->bandwidth);
1579 if (estimated > segment->duration)
1581 msg_Warn(s,"downloading segment %d predicted to take %ds, which exceeds its length (%ds)",
1582 segment->sequence, estimated, segment->duration);
1586 mtime_t start = mdate();
1587 if (hls_Download(s, segment) != VLC_SUCCESS)
1589 msg_Err(s, "downloading segment %d from stream %d failed",
1590 segment->sequence, *cur_stream);
1591 vlc_mutex_unlock(&segment->lock);
1592 return VLC_EGENERIC;
1594 mtime_t duration = mdate() - start;
1595 if (hls->bandwidth == 0 && segment->duration > 0)
1597 /* Try to estimate the bandwidth for this stream */
1598 hls->bandwidth = (uint64_t)(((double)segment->size * 8) / ((double)segment->duration));
1601 /* If the segment is encrypted, decode it */
1602 if (hls_DecodeSegmentData(s, hls, segment) != VLC_SUCCESS)
1604 vlc_mutex_unlock(&segment->lock);
1605 return VLC_EGENERIC;
1608 vlc_mutex_unlock(&segment->lock);
1610 msg_Dbg(s, "downloaded segment %d from stream %d",
1611 segment->sequence, *cur_stream);
1613 uint64_t bw = segment->size * 8 * 1000000 / __MAX(1, duration); /* bits / s */
1614 p_sys->bandwidth = bw;
1615 if (p_sys->b_meta && (hls->bandwidth != bw))
1617 int newstream = BandwidthAdaptation(s, hls->id, &bw);
1619 /* FIXME: we need an average here */
1620 if ((newstream >= 0) && (newstream != *cur_stream))
1622 msg_Dbg(s, "detected %s bandwidth (%"PRIu64") stream",
1623 (bw >= hls->bandwidth) ? "faster" : "lower", bw);
1624 *cur_stream = newstream;
1627 return VLC_SUCCESS;
1630 static void* hls_Thread(void *p_this)
1632 stream_t *s = (stream_t *)p_this;
1633 stream_sys_t *p_sys = s->p_sys;
1635 int canc = vlc_savecancel();
1637 while (vlc_object_alive(s))
1639 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream);
1640 assert(hls);
1642 /* Sliding window (~60 seconds worth of movie) */
1643 vlc_mutex_lock(&hls->lock);
1644 int count = vlc_array_count(hls->segments);
1645 vlc_mutex_unlock(&hls->lock);
1647 /* Is there a new segment to process? */
1648 if ((!p_sys->b_live && (p_sys->playback.segment < (count - 6))) ||
1649 (p_sys->download.segment >= count))
1651 /* wait */
1652 vlc_mutex_lock(&p_sys->download.lock_wait);
1653 while (((p_sys->download.segment - p_sys->playback.segment > 6) ||
1654 (p_sys->download.segment >= count)) &&
1655 (p_sys->download.seek == -1))
1657 vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
1658 if (p_sys->b_live /*&& (mdate() >= p_sys->playlist.wakeup)*/)
1659 break;
1660 if (!vlc_object_alive(s))
1661 break;
1663 /* */
1664 if (p_sys->download.seek >= 0)
1666 p_sys->download.segment = p_sys->download.seek;
1667 p_sys->download.seek = -1;
1669 vlc_mutex_unlock(&p_sys->download.lock_wait);
1672 if (!vlc_object_alive(s)) break;
1674 vlc_mutex_lock(&hls->lock);
1675 segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
1676 vlc_mutex_unlock(&hls->lock);
1678 if ((segment != NULL) &&
1679 (hls_DownloadSegmentData(s, hls, segment, &p_sys->download.stream) != VLC_SUCCESS))
1681 if (!vlc_object_alive(s)) break;
1683 if (!p_sys->b_live)
1685 p_sys->b_error = true;
1686 break;
1690 /* download succeeded */
1691 /* determine next segment to download */
1692 vlc_mutex_lock(&p_sys->download.lock_wait);
1693 if (p_sys->download.seek >= 0)
1695 p_sys->download.segment = p_sys->download.seek;
1696 p_sys->download.seek = -1;
1698 else if (p_sys->download.segment < count)
1699 p_sys->download.segment++;
1700 vlc_cond_signal(&p_sys->download.wait);
1701 vlc_mutex_unlock(&p_sys->download.lock_wait);
1703 // In case of a successful download signal the read thread that data is available
1704 vlc_mutex_lock(&p_sys->read.lock_wait);
1705 vlc_cond_signal(&p_sys->read.wait);
1706 vlc_mutex_unlock(&p_sys->read.lock_wait);
1709 vlc_restorecancel(canc);
1710 return NULL;
1713 static void* hls_Reload(void *p_this)
1715 stream_t *s = (stream_t *)p_this;
1716 stream_sys_t *p_sys = s->p_sys;
1718 assert(p_sys->b_live);
1720 int canc = vlc_savecancel();
1722 double wait = 1.0;
1723 while (vlc_object_alive(s))
1725 mtime_t now = mdate();
1726 if (now >= p_sys->playlist.wakeup)
1728 /* reload the m3u8 if there are less than 3 segments what aren't downloaded */
1729 if ( ( p_sys->download.segment - p_sys->playback.segment < 3 ) &&
1730 ( hls_ReloadPlaylist(s) != VLC_SUCCESS) )
1732 /* No change in playlist, then backoff */
1733 p_sys->playlist.tries++;
1734 if (p_sys->playlist.tries == 1) wait = 0.5;
1735 else if (p_sys->playlist.tries == 2) wait = 1;
1736 else if (p_sys->playlist.tries >= 3) wait = 1.5;
1738 /* Can we afford to backoff? */
1739 if (p_sys->download.segment - p_sys->playback.segment < 3)
1741 p_sys->playlist.tries = 0;
1742 wait = 0.5;
1745 else
1747 p_sys->playlist.tries = 0;
1748 wait = 1.0;
1751 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream);
1752 assert(hls);
1754 /* determine next time to update playlist */
1755 p_sys->playlist.last = now;
1756 p_sys->playlist.wakeup = now + ((mtime_t)(hls->max_segment_length * wait)
1757 * (mtime_t)1000000);
1760 mwait(p_sys->playlist.wakeup);
1763 vlc_restorecancel(canc);
1764 return NULL;
1767 static int Prefetch(stream_t *s, int *current)
1769 stream_sys_t *p_sys = s->p_sys;
1770 int stream = *current;
1772 hls_stream_t *hls = hls_Get(p_sys->hls_stream, stream);
1773 if (hls == NULL)
1774 return VLC_EGENERIC;
1776 if (vlc_array_count(hls->segments) == 0)
1777 return VLC_EGENERIC;
1778 else if (vlc_array_count(hls->segments) == 1 && p_sys->b_live)
1779 msg_Warn(s, "Only 1 segment available to prefetch in live stream; may stall");
1781 /* Download ~10s worth of segments of this HLS stream if they exist */
1782 unsigned segment_amount = (0.5f + 10/hls->duration);
1783 for (int i = 0; i < __MIN(vlc_array_count(hls->segments), segment_amount); i++)
1785 segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
1786 if (segment == NULL )
1787 return VLC_EGENERIC;
1789 /* It is useless to lock the segment here, as Prefetch is called before
1790 download and playlit thread are started. */
1791 if (segment->data)
1793 p_sys->download.segment++;
1794 continue;
1797 if (hls_DownloadSegmentData(s, hls, segment, current) != VLC_SUCCESS)
1798 return VLC_EGENERIC;
1800 p_sys->download.segment++;
1802 /* adapt bandwidth? */
1803 if (*current != stream)
1805 hls_stream_t *hls = hls_Get(p_sys->hls_stream, *current);
1806 if (hls == NULL)
1807 return VLC_EGENERIC;
1809 stream = *current;
1813 return VLC_SUCCESS;
1816 /****************************************************************************
1818 ****************************************************************************/
1819 static int hls_Download(stream_t *s, segment_t *segment)
1821 stream_sys_t *p_sys = s->p_sys;
1822 assert(segment);
1824 vlc_mutex_lock(&p_sys->lock);
1825 while (p_sys->paused)
1826 vlc_cond_wait(&p_sys->wait, &p_sys->lock);
1827 vlc_mutex_unlock(&p_sys->lock);
1829 stream_t *p_ts = stream_UrlNew(s, segment->url);
1830 if (p_ts == NULL)
1831 return VLC_EGENERIC;
1833 segment->size = stream_Size(p_ts);
1834 assert(segment->size > 0);
1836 segment->data = block_Alloc(segment->size);
1837 if (segment->data == NULL)
1839 stream_Delete(p_ts);
1840 return VLC_ENOMEM;
1843 assert(segment->data->i_buffer == segment->size);
1845 ssize_t length = 0, curlen = 0;
1846 uint64_t size;
1849 /* NOTE: Beware the size reported for a segment by the HLS server may not
1850 * be correct, when downloading the segment data. Therefore check the size
1851 * and enlarge the segment data block if necessary.
1853 size = stream_Size(p_ts);
1854 if (size > segment->size)
1856 msg_Dbg(s, "size changed %"PRIu64, segment->size);
1857 block_t *p_block = block_Realloc(segment->data, 0, size);
1858 if (p_block == NULL)
1860 stream_Delete(p_ts);
1861 block_Release(segment->data);
1862 segment->data = NULL;
1863 return VLC_ENOMEM;
1865 segment->data = p_block;
1866 segment->size = size;
1867 assert(segment->data->i_buffer == segment->size);
1868 p_block = NULL;
1870 length = stream_Read(p_ts, segment->data->p_buffer + curlen, segment->size - curlen);
1871 if (length <= 0)
1872 break;
1873 curlen += length;
1874 } while (vlc_object_alive(s));
1876 stream_Delete(p_ts);
1877 return VLC_SUCCESS;
1880 /* Read M3U8 file */
1881 static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer)
1883 int64_t total_bytes = 0;
1884 int64_t total_allocated = 0;
1885 uint8_t *p = NULL;
1887 while (1)
1889 char buf[4096];
1890 int64_t bytes;
1892 bytes = stream_Read(s, buf, sizeof(buf));
1893 if (bytes == 0)
1894 break; /* EOF ? */
1895 else if (bytes < 0)
1897 free (p);
1898 return bytes;
1901 if ( (total_bytes + bytes + 1) > total_allocated )
1903 if (total_allocated)
1904 total_allocated *= 2;
1905 else
1906 total_allocated = __MIN((uint64_t)bytes+1, sizeof(buf));
1908 p = realloc_or_free(p, total_allocated);
1909 if (p == NULL)
1910 return VLC_ENOMEM;
1913 memcpy(p+total_bytes, buf, bytes);
1914 total_bytes += bytes;
1917 if (total_allocated == 0)
1918 return VLC_EGENERIC;
1920 p[total_bytes] = '\0';
1921 *buffer = p;
1923 return total_bytes;
1926 static ssize_t read_M3U8_from_url(stream_t *s, const char* psz_url, uint8_t **buffer)
1928 assert(*buffer == NULL);
1930 /* Construct URL */
1931 stream_t *p_m3u8 = stream_UrlNew(s, psz_url);
1932 if (p_m3u8 == NULL)
1933 return VLC_EGENERIC;
1935 ssize_t size = read_M3U8_from_stream(p_m3u8, buffer);
1936 stream_Delete(p_m3u8);
1938 return size;
1941 static char *ReadLine(uint8_t *buffer, uint8_t **pos, const size_t len)
1943 assert(buffer);
1945 char *line = NULL;
1946 uint8_t *begin = buffer;
1947 uint8_t *p = begin;
1948 uint8_t *end = p + len;
1950 while (p < end)
1952 if ((*p == '\r') || (*p == '\n') || (*p == '\0'))
1953 break;
1954 p++;
1957 /* copy line excluding \r \n or \0 */
1958 line = strndup((char *)begin, p - begin);
1960 while ((*p == '\r') || (*p == '\n') || (*p == '\0'))
1962 if (*p == '\0')
1964 *pos = end;
1965 break;
1967 else
1969 /* next pass start after \r and \n */
1970 p++;
1971 *pos = p;
1975 return line;
1978 /****************************************************************************
1979 * Open
1980 ****************************************************************************/
1981 static int Open(vlc_object_t *p_this)
1983 stream_t *s = (stream_t*)p_this;
1984 stream_sys_t *p_sys;
1986 if (!isHTTPLiveStreaming(s))
1987 return VLC_EGENERIC;
1989 msg_Info(p_this, "HTTP Live Streaming (%s)", s->psz_path);
1991 /* Initialize crypto bit */
1992 vlc_gcrypt_init();
1994 /* */
1995 s->p_sys = p_sys = calloc(1, sizeof(*p_sys));
1996 if (p_sys == NULL)
1997 return VLC_ENOMEM;
1999 char *psz_uri = NULL;
2000 if (asprintf(&psz_uri,"%s://%s", s->psz_access, s->psz_path) < 0)
2002 free(p_sys);
2003 return VLC_ENOMEM;
2005 p_sys->m3u8 = psz_uri;
2007 char *new_path;
2008 if (asprintf(&new_path, "%s.ts", s->psz_path) < 0)
2010 free(p_sys->m3u8);
2011 free(p_sys);
2012 return VLC_ENOMEM;
2014 free(s->psz_path);
2015 s->psz_path = new_path;
2017 p_sys->bandwidth = 0;
2018 p_sys->b_live = true;
2019 p_sys->b_meta = false;
2020 p_sys->b_error = false;
2022 p_sys->hls_stream = vlc_array_new();
2023 if (p_sys->hls_stream == NULL)
2025 free(p_sys->m3u8);
2026 free(p_sys);
2027 return VLC_ENOMEM;
2030 /* */
2031 s->pf_read = Read;
2032 s->pf_peek = Peek;
2033 s->pf_control = Control;
2035 p_sys->paused = false;
2037 vlc_cond_init(&p_sys->wait);
2038 vlc_mutex_init(&p_sys->lock);
2040 /* Parse HLS m3u8 content. */
2041 uint8_t *buffer = NULL;
2042 ssize_t len = read_M3U8_from_stream(s->p_source, &buffer);
2043 if (len < 0)
2044 goto fail;
2045 if (parse_M3U8(s, p_sys->hls_stream, buffer, len) != VLC_SUCCESS)
2047 free(buffer);
2048 goto fail;
2050 free(buffer);
2051 /* HLS standard doesn't provide any guaranty about streams
2052 being sorted by bandwidth, so we sort them */
2053 qsort( p_sys->hls_stream->pp_elems, p_sys->hls_stream->i_count,
2054 sizeof( hls_stream_t* ), &hls_CompareStreams );
2056 /* Choose first HLS stream to start with */
2057 int current = p_sys->playback.stream = p_sys->hls_stream->i_count-1;
2058 p_sys->playback.segment = p_sys->download.segment = ChooseSegment(s, current);
2060 /* manage encryption key if needed */
2061 hls_ManageSegmentKeys(s, hls_Get(p_sys->hls_stream, current));
2063 if (Prefetch(s, &current) != VLC_SUCCESS)
2065 msg_Err(s, "fetching first segment failed.");
2066 goto fail;
2069 p_sys->download.stream = current;
2070 p_sys->playback.stream = current;
2071 p_sys->download.seek = -1;
2073 vlc_mutex_init(&p_sys->download.lock_wait);
2074 vlc_cond_init(&p_sys->download.wait);
2076 vlc_mutex_init(&p_sys->read.lock_wait);
2077 vlc_cond_init(&p_sys->read.wait);
2079 /* Initialize HLS live stream */
2080 if (p_sys->b_live)
2082 hls_stream_t *hls = hls_Get(p_sys->hls_stream, current);
2083 p_sys->playlist.last = mdate();
2084 p_sys->playlist.wakeup = p_sys->playlist.last +
2085 ((mtime_t)hls->duration * UINT64_C(1000000));
2087 if (vlc_clone(&p_sys->reload, hls_Reload, s, VLC_THREAD_PRIORITY_LOW))
2089 goto fail_thread;
2093 if (vlc_clone(&p_sys->thread, hls_Thread, s, VLC_THREAD_PRIORITY_INPUT))
2095 if (p_sys->b_live)
2096 vlc_join(p_sys->reload, NULL);
2097 goto fail_thread;
2100 return VLC_SUCCESS;
2102 fail_thread:
2103 vlc_mutex_destroy(&p_sys->download.lock_wait);
2104 vlc_cond_destroy(&p_sys->download.wait);
2106 vlc_mutex_destroy(&p_sys->read.lock_wait);
2107 vlc_cond_destroy(&p_sys->read.wait);
2109 fail:
2110 /* Free hls streams */
2111 for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
2113 hls_stream_t *hls = hls_Get(p_sys->hls_stream, i);
2114 if (hls) hls_Free(hls);
2116 vlc_array_destroy(p_sys->hls_stream);
2118 vlc_mutex_destroy(&p_sys->lock);
2119 vlc_cond_destroy(&p_sys->wait);
2121 /* */
2122 free(p_sys->m3u8);
2123 free(p_sys);
2124 return VLC_EGENERIC;
2127 /****************************************************************************
2128 * Close
2129 ****************************************************************************/
2130 static void Close(vlc_object_t *p_this)
2132 stream_t *s = (stream_t*)p_this;
2133 stream_sys_t *p_sys = s->p_sys;
2135 assert(p_sys->hls_stream);
2137 vlc_mutex_lock(&p_sys->lock);
2138 p_sys->paused = false;
2139 vlc_cond_signal(&p_sys->wait);
2140 vlc_mutex_unlock(&p_sys->lock);
2142 /* */
2143 vlc_mutex_lock(&p_sys->download.lock_wait);
2144 /* negate the condition variable's predicate */
2145 p_sys->download.segment = p_sys->playback.segment = 0;
2146 p_sys->download.seek = 0; /* better safe than sorry */
2147 vlc_cond_signal(&p_sys->download.wait);
2148 vlc_mutex_unlock(&p_sys->download.lock_wait);
2150 /* */
2151 if (p_sys->b_live)
2152 vlc_join(p_sys->reload, NULL);
2153 vlc_join(p_sys->thread, NULL);
2154 vlc_mutex_destroy(&p_sys->download.lock_wait);
2155 vlc_cond_destroy(&p_sys->download.wait);
2157 vlc_mutex_destroy(&p_sys->read.lock_wait);
2158 vlc_cond_destroy(&p_sys->read.wait);
2160 /* Free hls streams */
2161 for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
2163 hls_stream_t *hls = hls_Get(p_sys->hls_stream, i);
2164 if (hls) hls_Free(hls);
2166 vlc_array_destroy(p_sys->hls_stream);
2168 /* */
2170 vlc_mutex_destroy(&p_sys->lock);
2171 vlc_cond_destroy(&p_sys->wait);
2173 free(p_sys->m3u8);
2174 if (p_sys->peeked)
2175 block_Release (p_sys->peeked);
2176 free(p_sys);
2179 /****************************************************************************
2180 * Stream filters functions
2181 ****************************************************************************/
2182 static segment_t *GetSegment(stream_t *s)
2184 stream_sys_t *p_sys = s->p_sys;
2185 segment_t *segment = NULL;
2187 /* Is this segment of the current HLS stream ready? */
2188 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2189 if (hls != NULL)
2191 vlc_mutex_lock(&hls->lock);
2192 segment = segment_GetSegment(hls, p_sys->playback.segment);
2193 if (segment != NULL)
2195 vlc_mutex_lock(&segment->lock);
2196 /* This segment is ready? */
2197 if (segment->data != NULL)
2199 vlc_mutex_unlock(&segment->lock);
2200 p_sys->b_cache = hls->b_cache;
2201 vlc_mutex_unlock(&hls->lock);
2202 goto check;
2204 vlc_mutex_unlock(&segment->lock);
2206 vlc_mutex_unlock(&hls->lock);
2209 /* Was the HLS stream changed to another bitrate? */
2210 segment = NULL;
2211 for (int i_stream = 0; i_stream < vlc_array_count(p_sys->hls_stream); i_stream++)
2213 /* Is the next segment ready */
2214 hls_stream_t *hls = hls_Get(p_sys->hls_stream, i_stream);
2215 if (hls == NULL)
2216 return NULL;
2218 vlc_mutex_lock(&hls->lock);
2219 segment = segment_GetSegment(hls, p_sys->playback.segment);
2220 if (segment == NULL)
2222 vlc_mutex_unlock(&hls->lock);
2223 break;
2226 vlc_mutex_lock(&p_sys->download.lock_wait);
2227 int i_segment = p_sys->download.segment;
2228 vlc_mutex_unlock(&p_sys->download.lock_wait);
2230 vlc_mutex_lock(&segment->lock);
2231 /* This segment is ready? */
2232 if ((segment->data != NULL) &&
2233 (p_sys->playback.segment < i_segment))
2235 p_sys->playback.stream = i_stream;
2236 p_sys->b_cache = hls->b_cache;
2237 vlc_mutex_unlock(&segment->lock);
2238 vlc_mutex_unlock(&hls->lock);
2239 goto check;
2241 vlc_mutex_unlock(&segment->lock);
2242 vlc_mutex_unlock(&hls->lock);
2244 if (!p_sys->b_meta)
2245 break;
2247 /* */
2248 return NULL;
2250 check:
2251 /* sanity check */
2252 assert(segment->data);
2253 if (segment->data->i_buffer == 0)
2255 vlc_mutex_lock(&hls->lock);
2256 int count = vlc_array_count(hls->segments);
2257 vlc_mutex_unlock(&hls->lock);
2259 if ((p_sys->download.segment - p_sys->playback.segment == 0) &&
2260 ((count != p_sys->download.segment) || p_sys->b_live))
2261 msg_Err(s, "playback will stall");
2262 else if ((p_sys->download.segment - p_sys->playback.segment < 3) &&
2263 ((count != p_sys->download.segment) || p_sys->b_live))
2264 msg_Warn(s, "playback in danger of stalling");
2266 return segment;
2269 static int segment_RestorePos(segment_t *segment)
2271 if (segment->data)
2273 uint64_t size = segment->size - segment->data->i_buffer;
2274 if (size > 0)
2276 segment->data->i_buffer += size;
2277 segment->data->p_buffer -= size;
2280 return VLC_SUCCESS;
2283 /* p_read might be NULL if caller wants to skip data */
2284 static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read)
2286 stream_sys_t *p_sys = s->p_sys;
2287 ssize_t used = 0;
2291 /* Determine next segment to read. If this is a meta playlist and
2292 * bandwidth conditions changed, then the stream might have switched
2293 * to another bandwidth. */
2294 segment_t *segment = GetSegment(s);
2295 if (segment == NULL)
2296 break;
2298 vlc_mutex_lock(&segment->lock);
2299 if (segment->data->i_buffer == 0)
2301 if (!p_sys->b_cache || p_sys->b_live)
2303 block_Release(segment->data);
2304 segment->data = NULL;
2306 else
2307 segment_RestorePos(segment);
2309 vlc_mutex_unlock(&segment->lock);
2311 /* signal download thread */
2312 vlc_mutex_lock(&p_sys->download.lock_wait);
2313 p_sys->playback.segment++;
2314 vlc_cond_signal(&p_sys->download.wait);
2315 vlc_mutex_unlock(&p_sys->download.lock_wait);
2316 continue;
2319 if (segment->size == segment->data->i_buffer)
2320 msg_Dbg(s, "playing segment %d from stream %d",
2321 segment->sequence, p_sys->playback.stream);
2323 ssize_t len = -1;
2324 if (i_read <= segment->data->i_buffer)
2325 len = i_read;
2326 else if (i_read > segment->data->i_buffer)
2327 len = segment->data->i_buffer;
2329 if (len > 0)
2331 if (p_read) /* if NULL, then caller skips data */
2332 memcpy(p_read + used, segment->data->p_buffer, len);
2333 segment->data->i_buffer -= len;
2334 segment->data->p_buffer += len;
2335 used += len;
2336 i_read -= len;
2338 vlc_mutex_unlock(&segment->lock);
2340 } while (i_read > 0);
2342 return used;
2345 static int Read(stream_t *s, void *buffer, unsigned int i_read)
2347 stream_sys_t *p_sys = s->p_sys;
2348 ssize_t length = 0;
2350 assert(p_sys->hls_stream);
2352 while (length == 0)
2354 // In case an error occurred or the stream was closed return 0
2355 if (p_sys->b_error || !vlc_object_alive(s))
2356 return 0;
2358 // Lock the mutex before trying to read to avoid a race condition with the download thread
2359 vlc_mutex_lock(&p_sys->read.lock_wait);
2361 /* NOTE: buffer might be NULL if caller wants to skip data */
2362 length = hls_Read(s, (uint8_t*) buffer, i_read);
2364 // An error has occurred in hls_Read
2365 if (length < 0)
2367 vlc_mutex_unlock(&p_sys->read.lock_wait);
2369 return 0;
2372 // There is no data available yet for the demuxer so we need to wait until reload and
2373 // download operation are over.
2374 // Download thread will signal once download is finished.
2375 // A timed wait is used to avoid deadlock in case data never arrives since the thread
2376 // running this read operation is also responsible for closing the stream
2377 if (length == 0)
2379 mtime_t start = mdate();
2381 // Wait for 10 seconds
2382 mtime_t timeout_limit = start + (10 * UINT64_C(1000000));
2384 int res = vlc_cond_timedwait(&p_sys->read.wait, &p_sys->read.lock_wait, timeout_limit);
2386 // Error - reached a timeout of 10 seconds without data arriving - kill the stream
2387 if (res == ETIMEDOUT)
2389 msg_Warn(s, "timeout limit reached!");
2391 vlc_mutex_unlock(&p_sys->read.lock_wait);
2393 return 0;
2395 else if (res == EINVAL)
2396 return 0; // Error - lock is not locked so we can just return
2399 vlc_mutex_unlock(&p_sys->read.lock_wait);
2402 p_sys->playback.offset += length;
2403 return length;
2406 static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek)
2408 stream_sys_t *p_sys = s->p_sys;
2409 segment_t *segment;
2410 unsigned int len = i_peek;
2412 segment = GetSegment(s);
2413 if (segment == NULL)
2415 msg_Err(s, "segment %d should have been available (stream %d)",
2416 p_sys->playback.segment, p_sys->playback.stream);
2417 return 0; /* eof? */
2420 vlc_mutex_lock(&segment->lock);
2422 size_t i_buff = segment->data->i_buffer;
2423 uint8_t *p_buff = segment->data->p_buffer;
2425 if ( likely(i_peek < i_buff))
2427 *pp_peek = p_buff;
2428 vlc_mutex_unlock(&segment->lock);
2429 return i_peek;
2432 else /* This will seldom be run */
2434 /* remember segment to read */
2435 int peek_segment = p_sys->playback.segment;
2436 size_t curlen = 0;
2437 segment_t *nsegment;
2438 p_sys->playback.segment++;
2439 block_t *peeked = p_sys->peeked;
2441 if (peeked == NULL)
2442 peeked = block_Alloc (i_peek);
2443 else if (peeked->i_buffer < i_peek)
2444 peeked = block_Realloc (peeked, 0, i_peek);
2445 if (peeked == NULL)
2447 vlc_mutex_unlock(&segment->lock);
2448 return 0;
2450 p_sys->peeked = peeked;
2452 memcpy(peeked->p_buffer, p_buff, i_buff);
2453 curlen = i_buff;
2454 len -= i_buff;
2455 vlc_mutex_unlock(&segment->lock);
2457 i_buff = peeked->i_buffer;
2458 p_buff = peeked->p_buffer;
2459 *pp_peek = p_buff;
2461 while (curlen < i_peek)
2463 nsegment = GetSegment(s);
2464 if (nsegment == NULL)
2466 msg_Err(s, "segment %d should have been available (stream %d)",
2467 p_sys->playback.segment, p_sys->playback.stream);
2468 /* restore segment to read */
2469 p_sys->playback.segment = peek_segment;
2470 return curlen; /* eof? */
2473 vlc_mutex_lock(&nsegment->lock);
2475 if (len < nsegment->data->i_buffer)
2477 memcpy(p_buff + curlen, nsegment->data->p_buffer, len);
2478 curlen += len;
2480 else
2482 size_t i_nbuff = nsegment->data->i_buffer;
2483 memcpy(p_buff + curlen, nsegment->data->p_buffer, i_nbuff);
2484 curlen += i_nbuff;
2485 len -= i_nbuff;
2487 p_sys->playback.segment++;
2490 vlc_mutex_unlock(&nsegment->lock);
2493 /* restore segment to read */
2494 p_sys->playback.segment = peek_segment;
2495 return curlen;
2499 static bool hls_MaySeek(stream_t *s)
2501 stream_sys_t *p_sys = s->p_sys;
2503 if (p_sys->hls_stream == NULL)
2504 return false;
2506 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2507 if (hls == NULL) return false;
2509 if (p_sys->b_live)
2511 vlc_mutex_lock(&hls->lock);
2512 int count = vlc_array_count(hls->segments);
2513 vlc_mutex_unlock(&hls->lock);
2515 vlc_mutex_lock(&p_sys->download.lock_wait);
2516 bool may_seek = (p_sys->download.segment < (count - 2));
2517 vlc_mutex_unlock(&p_sys->download.lock_wait);
2518 return may_seek;
2520 return true;
2523 static uint64_t GetStreamSize(stream_t *s)
2525 stream_sys_t *p_sys = s->p_sys;
2527 if (p_sys->b_live)
2528 return 0;
2530 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2531 if (hls == NULL) return 0;
2533 vlc_mutex_lock(&hls->lock);
2534 if (hls->size == 0)
2535 hls->size = hls_GetStreamSize(hls);
2536 uint64_t size = hls->size;
2537 vlc_mutex_unlock(&hls->lock);
2539 return size;
2542 static int segment_Seek(stream_t *s, const uint64_t pos)
2544 stream_sys_t *p_sys = s->p_sys;
2546 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2547 if (hls == NULL)
2548 return VLC_EGENERIC;
2550 vlc_mutex_lock(&hls->lock);
2552 bool b_found = false;
2553 uint64_t length = 0;
2554 uint64_t size = hls->size;
2555 int count = vlc_array_count(hls->segments);
2557 segment_t *currentSegment = segment_GetSegment(hls, p_sys->playback.segment);
2558 if (currentSegment == NULL)
2560 vlc_mutex_unlock(&hls->lock);
2561 return VLC_EGENERIC;
2564 for (int n = 0; n < count; n++)
2566 segment_t *segment = segment_GetSegment(hls, n);
2567 if (segment == NULL)
2569 vlc_mutex_unlock(&hls->lock);
2570 return VLC_EGENERIC;
2573 vlc_mutex_lock(&segment->lock);
2574 length += segment->duration * (hls->bandwidth/8);
2575 vlc_mutex_unlock(&segment->lock);
2577 if (pos <= length)
2579 if (count - n >= 3)
2581 p_sys->playback.segment = n;
2582 b_found = true;
2583 break;
2585 /* Do not search in last 3 segments */
2586 vlc_mutex_unlock(&hls->lock);
2587 return VLC_EGENERIC;
2591 /* */
2592 if (!b_found && (pos >= size))
2594 p_sys->playback.segment = count - 1;
2595 b_found = true;
2598 /* */
2599 if (b_found)
2602 /* restore current segment to start position */
2603 vlc_mutex_lock(&currentSegment->lock);
2604 segment_RestorePos(currentSegment);
2605 vlc_mutex_unlock(&currentSegment->lock);
2607 /* restore seeked segment to start position */
2608 segment_t *segment = segment_GetSegment(hls, p_sys->playback.segment);
2609 if (segment == NULL)
2611 vlc_mutex_unlock(&hls->lock);
2612 return VLC_EGENERIC;
2615 vlc_mutex_lock(&segment->lock);
2616 segment_RestorePos(segment);
2617 vlc_mutex_unlock(&segment->lock);
2619 /* start download at current playback segment */
2620 vlc_mutex_unlock(&hls->lock);
2622 /* Wake up download thread */
2623 vlc_mutex_lock(&p_sys->download.lock_wait);
2624 p_sys->download.seek = p_sys->playback.segment;
2625 vlc_cond_signal(&p_sys->download.wait);
2627 /* Wait for download to be finished */
2628 msg_Dbg(s, "seek to segment %d", p_sys->playback.segment);
2629 while ((p_sys->download.seek != -1) ||
2630 ((p_sys->download.segment - p_sys->playback.segment < 3) &&
2631 (p_sys->download.segment < count)))
2633 vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
2634 if (!vlc_object_alive(s) || s->b_error) break;
2636 vlc_mutex_unlock(&p_sys->download.lock_wait);
2638 return VLC_SUCCESS;
2640 vlc_mutex_unlock(&hls->lock);
2642 return b_found ? VLC_SUCCESS : VLC_EGENERIC;
2645 static int Control(stream_t *s, int i_query, va_list args)
2647 stream_sys_t *p_sys = s->p_sys;
2649 switch (i_query)
2651 case STREAM_CAN_SEEK:
2652 *(va_arg (args, bool *)) = hls_MaySeek(s);
2653 break;
2654 case STREAM_CAN_CONTROL_PACE:
2655 case STREAM_CAN_PAUSE:
2656 *(va_arg (args, bool *)) = true;
2657 break;
2658 case STREAM_CAN_FASTSEEK:
2659 *(va_arg (args, bool *)) = false;
2660 break;
2661 case STREAM_GET_POSITION:
2662 *(va_arg (args, uint64_t *)) = p_sys->playback.offset;
2663 break;
2664 case STREAM_SET_PAUSE_STATE:
2666 bool paused = va_arg (args, unsigned);
2668 vlc_mutex_lock(&p_sys->lock);
2669 p_sys->paused = paused;
2670 vlc_cond_signal(&p_sys->wait);
2671 vlc_mutex_unlock(&p_sys->lock);
2672 break;
2674 case STREAM_SET_POSITION:
2675 if (hls_MaySeek(s))
2677 uint64_t pos = (uint64_t)va_arg(args, uint64_t);
2678 if (segment_Seek(s, pos) == VLC_SUCCESS)
2680 p_sys->playback.offset = pos;
2681 break;
2684 return VLC_EGENERIC;
2685 case STREAM_GET_SIZE:
2686 *(va_arg (args, uint64_t *)) = GetStreamSize(s);
2687 break;
2688 case STREAM_GET_PTS_DELAY:
2689 *va_arg (args, int64_t *) =
2690 var_InheritInteger(s, "network-caching");
2691 break;
2692 default:
2693 return VLC_EGENERIC;
2695 return VLC_SUCCESS;