1 /*****************************************************************************
2 * httplive.c: HTTP Live Streaming stream filter
3 *****************************************************************************
4 * Copyright (C) 2010-2012 M2X BV
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 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.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 /*****************************************************************************
48 *****************************************************************************/
49 static int Open (vlc_object_t *);
50 static void Close(vlc_object_t *);
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)
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)*/
72 char *psz_key_path; /* url key path */
73 uint8_t aes_key[16]; /* AES-128 */
75 uint8_t psz_AES_IV[AES_BLOCK_SIZE]; /* IV used when decypher the block */
79 block_t *data; /* data */
82 typedef struct hls_stream_s
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) */
93 vlc_array_t *segments; /* list of segments */
94 char *url; /* uri to m3u8 */
96 bool b_cache; /* allow caching */
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;
105 char *m3u8; /* M3U8 url */
106 vlc_thread_t reload; /* HLS m3u8 reload thread */
107 vlc_thread_t thread; /* HLS segment download thread */
112 vlc_array_t *hls_stream; /* bandwidth adaptation */
113 uint64_t bandwidth; /* measured bandwidth (bits per second) */
116 struct hls_download_s
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 */
126 struct hls_playback_s
128 uint64_t offset; /* current offset in media */
129 int stream; /* current hls_stream */
130 int segment; /* current segment for playback */
134 struct hls_playlist_s
136 mtime_t last; /* playlist last loaded */
137 mtime_t wakeup; /* next reload time */
138 int tries; /* times it was not changed */
143 vlc_mutex_t lock_wait; /* used by read condition variable */
144 vlc_cond_t wait; /* some condition to wait on during read */
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 */
160 /****************************************************************************
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);
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);
171 static int hls_Download(stream_t *s, segment_t *segment);
173 static void* hls_Thread(void *);
174 static void* hls_Reload(void *);
176 static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted);
177 static void segment_Free(segment_t *segment);
179 /****************************************************************************
181 ****************************************************************************/
182 static bool isHTTPLiveStreaming(stream_t *s)
186 int size = stream_Peek(s->p_source, &peek, 46);
190 if (memcmp(peek, "#EXTM3U", 7) != 0)
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 */
201 static const char *const ext[] = {
218 if (memcmp(peek, "EXT-X-", 6))
224 for (size_t i = 0; i < ARRAY_SIZE(ext); i++)
226 size_t len = strlen(ext[i]);
227 if (size < 0 || (size_t)size < len)
229 if (!memcmp(peek, ext[i], len))
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)
240 hls_stream_t *hls = (hls_stream_t *)malloc(sizeof(hls_stream_t));
241 if (hls == NULL) return NULL;
245 hls->duration = -1;/* unknown */
246 hls->max_segment_length = -1;/* unknown */
248 hls->sequence = 0; /* default is 0 */
249 hls->version = 1; /* default protocol version */
251 hls->url = strdup(uri);
252 if (hls->url == NULL)
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);
264 static void hls_Free(hls_stream_t *hls)
266 vlc_mutex_destroy(&hls->lock);
270 for (int n = 0; n < vlc_array_count(hls->segments); n++)
272 segment_t *segment = segment_GetSegment(hls, n);
273 if (segment) segment_Free(segment);
275 vlc_array_destroy(hls->segments);
278 free(hls->psz_current_key_path);
282 static hls_stream_t *hls_Copy(hls_stream_t *src, const bool b_cp_segments)
285 assert(!b_cp_segments); /* FIXME: copying segments is not implemented */
287 hls_stream_t *dst = (hls_stream_t *)malloc(sizeof(hls_stream_t));
288 if (dst == NULL) return NULL;
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)
308 dst->segments = vlc_array_new();
309 vlc_mutex_init(&dst->lock);
313 static hls_stream_t *hls_Get(vlc_array_t *hls_stream, const int wanted)
315 int count = vlc_array_count(hls_stream);
318 if ((wanted < 0) || (wanted >= count))
320 return (hls_stream_t *) vlc_array_item_at_index(hls_stream, wanted);
323 static inline hls_stream_t *hls_GetFirst(vlc_array_t *hls_stream)
325 return hls_Get(hls_stream, 0);
328 static hls_stream_t *hls_GetLast(vlc_array_t *hls_stream)
330 int count = vlc_array_count(hls_stream);
334 return hls_Get(hls_stream, count);
337 static hls_stream_t *hls_Find(vlc_array_t *hls_stream, hls_stream_t *hls_new)
339 int count = vlc_array_count(hls_stream);
340 for (int n = 0; n < count; n++)
342 hls_stream_t *hls = hls_Get(hls_stream, n);
346 if ((hls->id == hls_new->id) &&
347 ((hls->bandwidth == hls_new->bandwidth)||(hls_new->bandwidth==0)))
354 static uint64_t hls_GetStreamSize(hls_stream_t *hls)
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. */
362 /* If there is no valid bandwidth yet, then there is no point in
363 * computing stream size. */
364 if (hls->bandwidth == 0)
367 int count = vlc_array_count(hls->segments);
368 for (int n = 0; n < count; n++)
370 segment_t *segment = segment_GetSegment(hls, n);
373 size += (segment->duration * (hls->bandwidth / 8));
380 static segment_t *segment_New(hls_stream_t* hls, const int duration, const char *uri)
382 segment_t *segment = (segment_t *)malloc(sizeof(segment_t));
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)
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);
408 static void segment_Free(segment_t *segment)
410 vlc_mutex_destroy(&segment->lock);
413 free(segment->psz_key_path);
415 block_Release(segment->data);
419 static segment_t *segment_GetSegment(hls_stream_t *hls, const int wanted)
423 int count = vlc_array_count(hls->segments);
426 if ((wanted < 0) || (wanted >= count))
428 return (segment_t *) vlc_array_item_at_index(hls->segments, wanted);
431 static segment_t *segment_Find(hls_stream_t *hls, const int sequence)
435 int count = vlc_array_count(hls->segments);
436 if (count <= 0) return NULL;
437 for (int n = 0; n < count; n++)
439 segment_t *segment = segment_GetSegment(hls, n);
440 if (segment == NULL) break;
441 if (segment->sequence == sequence)
447 static int ChooseSegment(stream_t *s, const int current)
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;
453 /* Choose a segment to start which is no closer than
454 * 3 times the target duration from the end of the playlist.
459 int count = vlc_array_count(hls->segments);
460 int i = p_sys->b_live ? count - 1 : -1;
462 /* We do while loop only with live case, otherwise return 0*/
463 while((i >= 0) && (i < count))
465 segment_t *segment = segment_GetSegment(hls, i);
468 if (segment->duration > hls->duration)
470 msg_Err(s, "EXTINF:%d duration is larger than EXT-X-TARGETDURATION:%d",
471 segment->duration, hls->duration);
474 duration += segment->duration;
475 if (duration >= 3 * hls->duration)
477 /* Start point found */
479 sequence = segment->sequence;
486 msg_Dbg(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
491 static char *parse_Attributes(const char *line, const char *attr)
494 char *begin = (char *) line;
495 char *end = begin + strlen(line);
497 /* Find start of attributes */
498 if ((p = strchr(begin, ':' )) == NULL)
504 if (strncasecmp(begin, attr, strlen(attr)) == 0
505 && begin[strlen(attr)] == '=')
507 /* <attr>="<value>"[,]* */
508 p = strchr(begin, ',');
509 begin += strlen(attr) + 1;
511 /* Check if we have " " marked value*/
512 if( begin[0] == '"' )
514 char *valueend = strchr( begin+1, '"');
516 /* No ending " so bail out */
517 if( unlikely( !valueend ) )
520 p = strchr( valueend, ',');
524 if (p == NULL) /* last attribute */
525 return strndup(begin, end - begin);
527 return strndup(begin, p - begin);
530 } while(begin < end);
535 static int string_to_IV(char *string_hexa, uint8_t iv[AES_BLOCK_SIZE])
537 unsigned long long iv_hi, iv_lo;
539 if (*string_hexa++ != '0')
541 if (*string_hexa != 'x' && *string_hexa != 'X')
546 size_t len = strlen(string_hexa);
549 iv_lo = strtoull(string_hexa, &end, 16);
553 iv_lo = strtoull(&string_hexa[len-16], &end, 16);
556 string_hexa[len-16] = '\0';
557 iv_hi = strtoull(string_hexa, &end, 16);
562 for (int i = 7; i >= 0 ; --i) {
563 iv[ i] = iv_hi & 0xff;
564 iv[8+i] = iv_lo & 0xff;
572 static char *relative_URI(const char *psz_url, const char *psz_path)
576 assert(psz_url != NULL && psz_path != NULL);
579 //If the path is actually an absolute URL, don't do anything.
580 if (strncmp(psz_path, "http", 4) == 0)
583 size_t len = strlen(psz_path);
585 char *new_url = strdup(psz_url);
586 if (unlikely(!new_url))
589 if( psz_path[0] == '/' ) //Relative URL with absolute path
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))
600 while(len >= 3 && !strncmp(psz_path, "../", 3)) {
606 char *slash = strrchr(new_url, '/');
607 if (unlikely(slash == NULL))
614 if (asprintf(&ret, fmt, new_url, psz_path) < 0)
622 static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *duration)
627 /* strip of #EXTINF: */
629 char *token = strtok_r(p_read, ":", &p_next);
634 token = strtok_r(NULL, ",", &p_next);
640 if (hls->version < 3)
643 value = strtol(token, &endptr, 10);
644 if (token == endptr || errno == ERANGE)
654 double d = strtof(token, &endptr);
655 if (token == endptr || errno == ERANGE)
660 if ((d) - ((int)d) >= 0.5)
661 value = ((int)d) + 1;
666 if( *duration > hls->max_segment_length)
667 hls->max_segment_length = *duration;
669 /* Ignore the rest of the line */
673 static int parse_AddSegment(hls_stream_t *hls, const int duration, const char *uri)
678 /* Store segment information */
679 vlc_mutex_lock(&hls->lock);
681 char *psz_uri = relative_URI(hls->url, uri);
683 segment_t *segment = segment_New(hls, duration, psz_uri ? psz_uri : uri);
685 segment->sequence = hls->sequence + vlc_array_count(hls->segments) - 1;
688 vlc_mutex_unlock(&hls->lock);
690 return segment ? VLC_SUCCESS : VLC_ENOMEM;
693 static int parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read)
698 int ret = sscanf(p_read, "#EXT-X-TARGETDURATION:%d", &duration);
701 msg_Err(s, "expected #EXT-X-TARGETDURATION:<s>");
705 hls->duration = duration; /* seconds */
709 static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream,
710 hls_stream_t **hls, char *p_read, const char *uri)
716 assert(*hls == NULL);
718 attr = parse_Attributes(p_read, "PROGRAM-ID");
727 attr = parse_Attributes(p_read, "BANDWIDTH");
730 msg_Err(s, "#EXT-X-STREAM-INF: expected BANDWIDTH=<value>");
738 msg_Err(s, "#EXT-X-STREAM-INF: bandwidth cannot be 0");
742 msg_Dbg(s, "bandwidth adaptation detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
744 char *psz_uri = relative_URI(s->p_sys->m3u8, uri);
746 *hls = hls_New(*hls_stream, id, bw, psz_uri ? psz_uri : uri);
750 return (*hls == NULL) ? VLC_ENOMEM : VLC_SUCCESS;
753 static int parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read)
758 int ret = sscanf(p_read, "#EXT-X-MEDIA-SEQUENCE:%d", &sequence);
761 msg_Err(s, "expected #EXT-X-MEDIA-SEQUENCE:<s>");
765 if (hls->sequence > 0)
767 if (s->p_sys->b_live)
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);
777 msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist (new=%d, old=%d)",
778 sequence, hls->sequence);
780 hls->sequence = sequence;
784 static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
788 /* #EXT-X-KEY:METHOD=<method>[,URI="<URI>"][,IV=<IV>] */
789 int err = VLC_SUCCESS;
790 char *attr = parse_Attributes(p_read, "METHOD");
793 msg_Err(s, "#EXT-X-KEY: expected METHOD=<value>");
797 if (strncasecmp(attr, "NONE", 4) == 0)
799 char *uri = parse_Attributes(p_read, "URI");
802 msg_Err(s, "#EXT-X-KEY: URI not expected");
806 /* IV is only supported in version 2 and above */
807 if (hls->version >= 2)
809 char *iv = parse_Attributes(p_read, "IV");
812 msg_Err(s, "#EXT-X-KEY: IV not expected");
818 else if (strncasecmp(attr, "AES-128", 7) == 0)
820 char *value, *uri, *iv;
821 if (s->p_sys->b_aesmsg == false)
823 msg_Dbg(s, "playback of AES-128 encrypted HTTP Live media detected.");
824 s->p_sys->b_aesmsg = true;
826 value = uri = parse_Attributes(p_read, "URI");
829 msg_Err(s, "#EXT-X-KEY: URI not found for encrypted HTTP Live media in AES-128");
834 /* Url is put between quotes, remove them */
837 /* We need to strip the "" from the attribute value */
839 char* end = strchr(uri, '"');
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 );
849 hls->psz_current_key_path = relative_URI(hls->url, uri);
852 value = iv = parse_Attributes(p_read, "IV");
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.
862 hls->b_current_iv_loaded = false;
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.
873 if (string_to_IV(iv, hls->psz_current_AES_IV) == VLC_EGENERIC)
875 msg_Err(s, "IV invalid");
879 hls->b_current_iv_loaded = true;
885 msg_Warn(s, "playback of encrypted HTTP Live media is not supported.");
892 static int parse_ProgramDateTime(stream_t *s, hls_stream_t *hls, char *p_read)
895 msg_Dbg(s, "tag not supported: #EXT-X-PROGRAM-DATE-TIME %s", p_read);
899 static int parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read)
903 char answer[4] = "\0";
904 int ret = sscanf(p_read, "#EXT-X-ALLOW-CACHE:%3s", answer);
907 msg_Err(s, "#EXT-X-ALLOW-CACHE, ignoring ...");
911 hls->b_cache = (strncmp(answer, "NO", 2) != 0);
915 static int parse_Version(stream_t *s, hls_stream_t *hls, char *p_read)
920 int ret = sscanf(p_read, "#EXT-X-VERSION:%d", &version);
923 msg_Err(s, "#EXT-X-VERSION: no protocol version found, should be version 1.");
928 hls->version = version;
929 if (hls->version <= 0 || hls->version > 3)
931 msg_Err(s, "#EXT-X-VERSION should be version 1, 2 or 3 iso %d", version);
937 static int parse_EndList(stream_t *s, hls_stream_t *hls)
941 s->p_sys->b_live = false;
942 msg_Dbg(s, "video on demand (vod) mode");
946 static int parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read)
950 /* FIXME: Do we need to act on discontinuity ?? */
951 msg_Dbg(s, "#EXT-X-DISCONTINUITY %s", p_read);
955 static int hls_CompareStreams( const void* a, const void* b )
957 hls_stream_t* stream_a = *(hls_stream_t**)a;
958 hls_stream_t* stream_b = *(hls_stream_t**)b;
960 return stream_a->bandwidth - stream_b->bandwidth;
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,
969 static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const ssize_t len)
971 stream_sys_t *p_sys = s->p_sys;
972 uint8_t *p_read, *p_begin, *p_end;
977 msg_Dbg(s, "parse_M3U8\n%s", buffer);
979 p_end = p_begin + len;
981 char *line = ReadLine(p_begin, &p_read, p_end - p_begin);
986 if (strncmp(line, "#EXTM3U", 7) != 0)
988 msg_Err(s, "missing #EXTM3U tag .. aborting");
996 /* What is the version ? */
998 uint8_t *p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-VERSION:");
1001 uint8_t *tmp = NULL;
1002 char *psz_version = ReadLine(p, &tmp, p_end - p);
1003 if (psz_version == NULL)
1005 int ret = sscanf((const char*)psz_version, "#EXT-X-VERSION:%d", &version);
1008 msg_Warn(s, "#EXT-X-VERSION: no protocol version found, assuming version 1.");
1015 /* Is it a live stream ? */
1016 p_sys->b_live = (strstr((const char *)buffer, "#EXT-X-ENDLIST") == NULL) ? true : false;
1018 /* Is it a meta index file ? */
1019 bool b_meta = (strstr((const char *)buffer, "#EXT-X-STREAM-INF") == NULL) ? false : true;
1021 int err = VLC_SUCCESS;
1025 msg_Dbg(s, "Meta playlist");
1027 /* M3U8 Meta Index file */
1030 line = ReadLine(p_begin, &p_read, p_end - p_begin);
1036 if (strncmp(line, "#EXT-X-STREAM-INF", 17) == 0)
1038 p_sys->b_meta = true;
1039 char *uri = ReadLine(p_begin, &p_read, p_end - p_begin);
1046 msg_Warn(s, "Skipping invalid stream-inf: %s", uri);
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;
1061 /* Download playlist file from server */
1062 uint8_t *buf = NULL;
1063 ssize_t len = read_M3U8_from_url(s, hls->url, &buf);
1066 msg_Warn(s, "failed to read %s, continue for other streams", hls->url);
1068 /* remove stream just added */
1069 if (new_stream_added)
1070 vlc_array_remove(streams, vlc_array_count(streams) - 1);
1072 /* ignore download error, so we have chance to try other streams */
1077 /* Parse HLS m3u8 content. */
1078 err = parse_M3U8(s, streams, buf, len);
1082 hls->version = version;
1084 hls->size = hls_GetStreamSize(hls); /* Stream size (approximate) */
1094 if (p_begin >= p_end)
1097 } while (err == VLC_SUCCESS);
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)
1103 msg_Err(s, "No playable streams found in Meta playlist");
1109 msg_Dbg(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version);
1111 hls_stream_t *hls = NULL;
1113 hls = hls_GetLast(streams);
1116 /* No Meta playlist used */
1117 hls = hls_New(streams, 0, 0, p_sys->m3u8);
1120 /* Get TARGET-DURATION first */
1121 p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-TARGETDURATION:");
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);
1134 hls->version = version;
1136 else return VLC_ENOMEM;
1141 bool media_sequence_loaded = false;
1142 int segment_duration = -1;
1146 line = ReadLine(p_begin, &p_read, p_end - p_begin);
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)
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)
1161 err = parse_MediaSequence(s, hls, line);
1162 media_sequence_loaded = true;
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') )
1179 err = parse_AddSegment(hls, segment_duration, line);
1180 segment_duration = -1; /* reset duration */
1186 if (p_begin >= p_end)
1189 } while (err == VLC_SUCCESS);
1198 static int hls_DownloadSegmentKey(stream_t *s, segment_t *seg)
1200 stream_t *p_m3u8 = stream_UrlNew(s, seg->psz_key_path);
1203 msg_Err(s, "Failed to load the AES key for segment sequence %d", seg->sequence);
1204 return VLC_EGENERIC;
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)
1211 msg_Err(s, "The AES key loaded doesn't have the right size (%d)", len);
1212 return VLC_EGENERIC;
1218 static int hls_ManageSegmentKeys(stream_t *s, hls_stream_t *hls)
1220 segment_t *seg = NULL;
1221 segment_t *prev_seg;
1222 int count = vlc_array_count(hls->segments);
1224 for (int i = 0; i < count; i++)
1227 seg = segment_GetSegment(hls, i);
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 */
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)
1239 memcpy(seg->aes_key, prev_seg->aes_key, AES_BLOCK_SIZE);
1240 seg->b_key_loaded = true;
1243 if (hls_DownloadSegmentKey(s, seg) != VLC_SUCCESS)
1244 return VLC_EGENERIC;
1245 seg->b_key_loaded = true;
1250 static int hls_DecodeSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segment)
1252 /* Did the segment need to be decoded ? */
1253 if (segment->psz_key_path == NULL)
1256 /* Do we have loaded the key ? */
1257 if (!segment->b_key_loaded)
1259 /* No ? try to download it now */
1260 if (hls_ManageSegmentKeys(s, hls) != VLC_SUCCESS)
1261 return VLC_EGENERIC;
1264 /* For now, we only decode AES-128 data */
1265 gcry_error_t i_gcrypt_err;
1266 gcry_cipher_hd_t aes_ctx;
1268 i_gcrypt_err = gcry_cipher_open(&aes_ctx, GCRY_CIPHER_AES,
1269 GCRY_CIPHER_MODE_CBC, 0);
1272 msg_Err(s, "gcry_cipher_open failed: %s", gpg_strerror(i_gcrypt_err));
1273 gcry_cipher_close(aes_ctx);
1274 return VLC_EGENERIC;
1278 i_gcrypt_err = gcry_cipher_setkey(aes_ctx, segment->aes_key,
1279 sizeof(segment->aes_key));
1282 msg_Err(s, "gcry_cipher_setkey failed: %s", gpg_strerror(i_gcrypt_err));
1283 gcry_cipher_close(aes_ctx);
1284 return VLC_EGENERIC;
1287 if (segment->b_iv_loaded == false)
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;
1296 i_gcrypt_err = gcry_cipher_setiv(aes_ctx, segment->psz_AES_IV,
1297 sizeof(segment->psz_AES_IV));
1301 msg_Err(s, "gcry_cipher_setiv failed: %s", gpg_strerror(i_gcrypt_err));
1302 gcry_cipher_close(aes_ctx);
1303 return VLC_EGENERIC;
1306 i_gcrypt_err = gcry_cipher_decrypt(aes_ctx,
1307 segment->data->p_buffer, /* out */
1308 segment->data->i_buffer,
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;
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)
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;
1328 if (segment->data->p_buffer[segment->data->i_buffer-1-count] != pad)
1330 msg_Err(s, "Bad ending buffer, perhaps we failed to decrypt the segment with the correct key");
1331 return VLC_EGENERIC;
1335 /* not all the data is readable because of padding */
1336 segment->data->i_buffer -= pad;
1341 static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
1343 stream_sys_t *p_sys = s->p_sys;
1345 int err = VLC_EGENERIC;
1347 /* Duplicate HLS stream META information */
1348 for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
1350 hls_stream_t *src, *dst;
1351 src = hls_Get(p_sys->hls_stream, i);
1353 return VLC_EGENERIC;
1355 dst = hls_Copy(src, false);
1358 vlc_array_append(*streams, dst);
1360 /* Download playlist file from server */
1361 uint8_t *buf = NULL;
1362 ssize_t len = read_M3U8_from_url(s, dst->url, &buf);
1367 /* Parse HLS m3u8 content. */
1368 err = parse_M3U8(s, *streams, buf, len);
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)
1379 int count = vlc_array_count(hls_new->segments);
1381 msg_Dbg(s, "updating hls stream (program-id=%d, bandwidth=%"PRIu64") has %d segments",
1382 hls_new->id, hls_new->bandwidth, count);
1384 vlc_mutex_lock(&hls_old->lock);
1385 hls_old->max_segment_length=-1;
1386 for (int n = 0; n < count; n++)
1388 segment_t *p = segment_GetSegment(hls_new, n);
1392 segment_t *segment = segment_Find(hls_old, p->sequence);
1395 vlc_mutex_lock(&segment->lock);
1398 assert(segment->url);
1400 /* they should be the same */
1401 if ((p->sequence != segment->sequence) ||
1402 (p->duration != segment->duration) ||
1403 (strcmp(p->url, segment->url) != 0))
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);
1411 /* Resetting content */
1412 segment->sequence = p->sequence;
1413 segment->duration = p->duration;
1415 segment->url = strdup(p->url);
1416 if ( segment->url == NULL )
1418 msg_Err(s, "Failed updating segment %d - skipping it", p->sequence);
1420 vlc_mutex_unlock(&segment->lock);
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) &&
1427 block_Release(segment->data);
1428 segment->data = NULL;
1430 free(segment->psz_key_path);
1431 segment->psz_key_path = p->psz_key_path ? strdup(p->psz_key_path) : NULL;
1434 vlc_mutex_unlock(&segment->lock);
1438 int last = vlc_array_count(hls_old->segments) - 1;
1439 segment_t *l = segment_GetSegment(hls_old, last);
1441 vlc_mutex_unlock(&hls_old->lock);
1446 if ((l->sequence + 1) != p->sequence)
1448 msg_Err(s, "gap in sequence numbers found: new=%d expected %d",
1449 p->sequence, l->sequence+1);
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);
1456 // Signal download thread otherwise the segment will not get downloaded
1457 *stream_appended = true;
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);
1467 vlc_array_clear(hls_new->segments);
1471 static int hls_ReloadPlaylist(stream_t *s)
1473 stream_sys_t *p_sys = s->p_sys;
1475 // Flag to indicate if we should signal download thread
1476 bool stream_appended = false;
1478 vlc_array_t *hls_streams = vlc_array_new();
1479 if (hls_streams == NULL)
1482 msg_Dbg(s, "Reloading HLS live meta playlist");
1484 if (get_HTTPLiveMetaPlaylist(s, &hls_streams) != VLC_SUCCESS)
1486 /* Free hls streams */
1487 for (int i = 0; i < vlc_array_count(hls_streams); i++)
1490 hls = hls_Get(hls_streams, i);
1491 if (hls) hls_Free(hls);
1493 vlc_array_destroy(hls_streams);
1495 msg_Err(s, "reloading playlist failed");
1496 return VLC_EGENERIC;
1499 /* merge playlists */
1500 int count = vlc_array_count(hls_streams);
1501 for (int n = 0; n < count; n++)
1503 hls_stream_t *hls_new = hls_Get(hls_streams, n);
1504 if (hls_new == NULL)
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);
1514 // New segment available - signal download thread
1515 stream_appended = true;
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);
1523 vlc_array_destroy(hls_streams);
1525 // Must signal the download thread otherwise new segments will not be downloaded at all!
1526 if (stream_appended == true)
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);
1536 /****************************************************************************
1538 ****************************************************************************/
1539 static int BandwidthAdaptation(stream_t *s, int progid, uint64_t *bandwidth)
1541 stream_sys_t *p_sys = s->p_sys;
1543 uint64_t bw = *bandwidth;
1544 uint64_t bw_candidate = 0;
1546 int count = vlc_array_count(p_sys->hls_stream);
1547 for (int n = 0; n < count; n++)
1549 /* Select best bandwidth match */
1550 hls_stream_t *hls = hls_Get(p_sys->hls_stream, n);
1551 if (hls == NULL) break;
1553 /* only consider streams with the same PROGRAM-ID */
1554 if (hls->id == progid)
1556 if ((bw >= hls->bandwidth) && (bw_candidate < hls->bandwidth))
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 */
1565 *bandwidth = bw_candidate;
1569 static int hls_DownloadSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur_stream)
1571 stream_sys_t *p_sys = s->p_sys;
1576 vlc_mutex_lock(&segment->lock);
1577 if (segment->data != NULL)
1579 /* Segment already downloaded */
1580 vlc_mutex_unlock(&segment->lock);
1584 /* sanity check - can we download this segment on time? */
1585 if ((p_sys->bandwidth > 0) && (hls->bandwidth > 0))
1587 uint64_t size = (segment->duration * hls->bandwidth); /* bits */
1588 int estimated = (int)(size / p_sys->bandwidth);
1589 if (estimated > segment->duration)
1591 msg_Warn(s,"downloading segment %d predicted to take %ds, which exceeds its length (%ds)",
1592 segment->sequence, estimated, segment->duration);
1596 mtime_t start = mdate();
1597 if (hls_Download(s, segment) != VLC_SUCCESS)
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;
1604 mtime_t duration = mdate() - start;
1605 if (hls->bandwidth == 0 && segment->duration > 0)
1607 /* Try to estimate the bandwidth for this stream */
1608 hls->bandwidth = (uint64_t)(((double)segment->size * 8) / ((double)segment->duration));
1611 /* If the segment is encrypted, decode it */
1612 if (hls_DecodeSegmentData(s, hls, segment) != VLC_SUCCESS)
1614 vlc_mutex_unlock(&segment->lock);
1615 return VLC_EGENERIC;
1618 vlc_mutex_unlock(&segment->lock);
1620 msg_Dbg(s, "downloaded segment %d from stream %d",
1621 segment->sequence, *cur_stream);
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))
1627 int newstream = BandwidthAdaptation(s, hls->id, &bw);
1629 /* FIXME: we need an average here */
1630 if ((newstream >= 0) && (newstream != *cur_stream))
1632 msg_Dbg(s, "detected %s bandwidth (%"PRIu64") stream",
1633 (bw >= hls->bandwidth) ? "faster" : "lower", bw);
1634 *cur_stream = newstream;
1640 static void* hls_Thread(void *p_this)
1642 stream_t *s = (stream_t *)p_this;
1643 stream_sys_t *p_sys = s->p_sys;
1645 int canc = vlc_savecancel();
1647 while (vlc_object_alive(s))
1649 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream);
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);
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))
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))
1667 vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
1668 if (p_sys->b_live /*&& (mdate() >= p_sys->playlist.wakeup)*/)
1670 if (!vlc_object_alive(s))
1674 if (p_sys->download.seek >= 0)
1676 p_sys->download.segment = p_sys->download.seek;
1677 p_sys->download.seek = -1;
1679 vlc_mutex_unlock(&p_sys->download.lock_wait);
1682 if (!vlc_object_alive(s)) break;
1684 vlc_mutex_lock(&hls->lock);
1685 segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
1686 vlc_mutex_unlock(&hls->lock);
1688 if ((segment != NULL) &&
1689 (hls_DownloadSegmentData(s, hls, segment, &p_sys->download.stream) != VLC_SUCCESS))
1691 if (!vlc_object_alive(s)) break;
1695 p_sys->b_error = true;
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)
1705 p_sys->download.segment = p_sys->download.seek;
1706 p_sys->download.seek = -1;
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);
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);
1719 vlc_restorecancel(canc);
1723 static void* hls_Reload(void *p_this)
1725 stream_t *s = (stream_t *)p_this;
1726 stream_sys_t *p_sys = s->p_sys;
1728 assert(p_sys->b_live);
1730 int canc = vlc_savecancel();
1733 while (vlc_object_alive(s))
1735 mtime_t now = mdate();
1736 if (now >= p_sys->playlist.wakeup)
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) )
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;
1748 /* Can we afford to backoff? */
1749 if (p_sys->download.segment - p_sys->playback.segment < 3)
1751 p_sys->playlist.tries = 0;
1757 p_sys->playlist.tries = 0;
1761 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream);
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);
1771 p_sys->playlist.wakeup += (mtime_t)((hls->duration * wait) * CLOCK_FREQ);
1774 mwait(p_sys->playlist.wakeup);
1777 vlc_restorecancel(canc);
1781 static int Prefetch(stream_t *s, int *current)
1783 stream_sys_t *p_sys = s->p_sys;
1784 int stream = *current;
1786 hls_stream_t *hls = hls_Get(p_sys->hls_stream, stream);
1788 return VLC_EGENERIC;
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");
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++)
1799 segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
1800 if (segment == NULL )
1801 return VLC_EGENERIC;
1803 /* It is useless to lock the segment here, as Prefetch is called before
1804 download and playlit thread are started. */
1807 p_sys->download.segment++;
1811 if (hls_DownloadSegmentData(s, hls, segment, current) != VLC_SUCCESS)
1812 return VLC_EGENERIC;
1814 p_sys->download.segment++;
1816 /* adapt bandwidth? */
1817 if (*current != stream)
1819 hls_stream_t *hls = hls_Get(p_sys->hls_stream, *current);
1821 return VLC_EGENERIC;
1830 /****************************************************************************
1832 ****************************************************************************/
1833 static int hls_Download(stream_t *s, segment_t *segment)
1835 stream_sys_t *p_sys = s->p_sys;
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);
1843 stream_t *p_ts = stream_UrlNew(s, segment->url);
1845 return VLC_EGENERIC;
1847 segment->size = stream_Size(p_ts);
1849 if (segment->size == 0) {
1850 int chunk_size = 65536;
1851 segment->data = block_Alloc(chunk_size);
1855 if (segment->data->i_buffer - segment->size < chunk_size) {
1857 block_t *p_block = block_Realloc(segment->data, 0, segment->data->i_buffer + chunk_size);
1859 block_Release(segment->data);
1860 segment->data = NULL;
1863 segment->data = p_block;
1866 ssize_t length = stream_Read(p_ts, segment->data->p_buffer + segment->size, chunk_size);
1868 segment->data->i_buffer = segment->size;
1871 segment->size += length;
1872 } while (vlc_object_alive(s));
1874 stream_Delete(p_ts);
1878 segment->data = block_Alloc(segment->size);
1879 if (segment->data == NULL)
1882 assert(segment->data->i_buffer == segment->size);
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.
1891 uint64_t size = stream_Size(p_ts);
1892 if (size > segment->size)
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)
1898 block_Release(segment->data);
1899 segment->data = NULL;
1902 segment->data = p_block;
1903 segment->size = size;
1904 assert(segment->data->i_buffer == segment->size);
1907 ssize_t length = stream_Read(p_ts, segment->data->p_buffer + curlen, segment->size - curlen);
1911 } while (vlc_object_alive(s));
1913 stream_Delete(p_ts);
1917 stream_Delete(p_ts);
1921 /* Read M3U8 file */
1922 static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer)
1924 int64_t total_bytes = 0;
1925 int64_t total_allocated = 0;
1933 bytes = stream_Read(s, buf, sizeof(buf));
1942 if ( (total_bytes + bytes + 1) > total_allocated )
1944 if (total_allocated)
1945 total_allocated *= 2;
1947 total_allocated = __MIN((uint64_t)bytes+1, sizeof(buf));
1949 p = realloc_or_free(p, total_allocated);
1954 memcpy(p+total_bytes, buf, bytes);
1955 total_bytes += bytes;
1958 if (total_allocated == 0)
1959 return VLC_EGENERIC;
1961 p[total_bytes] = '\0';
1967 static ssize_t read_M3U8_from_url(stream_t *s, const char* psz_url, uint8_t **buffer)
1969 assert(*buffer == NULL);
1972 stream_t *p_m3u8 = stream_UrlNew(s, psz_url);
1974 return VLC_EGENERIC;
1976 ssize_t size = read_M3U8_from_stream(p_m3u8, buffer);
1977 stream_Delete(p_m3u8);
1982 static char *ReadLine(uint8_t *buffer, uint8_t **pos, const size_t len)
1987 uint8_t *begin = buffer;
1989 uint8_t *end = p + len;
1993 if ((*p == '\r') || (*p == '\n') || (*p == '\0'))
1998 /* copy line excluding \r \n or \0 */
1999 line = strndup((char *)begin, p - begin);
2001 while ((*p == '\r') || (*p == '\n') || (*p == '\0'))
2010 /* next pass start after \r and \n */
2019 /****************************************************************************
2021 ****************************************************************************/
2022 static int Open(vlc_object_t *p_this)
2024 stream_t *s = (stream_t*)p_this;
2025 stream_sys_t *p_sys;
2027 if (!isHTTPLiveStreaming(s))
2028 return VLC_EGENERIC;
2030 msg_Info(p_this, "HTTP Live Streaming (%s)", s->psz_path);
2032 /* Initialize crypto bit */
2036 s->p_sys = p_sys = calloc(1, sizeof(*p_sys));
2040 char *psz_uri = NULL;
2041 if (asprintf(&psz_uri,"%s://%s", s->psz_access, s->psz_path) < 0)
2046 p_sys->m3u8 = psz_uri;
2049 if (asprintf(&new_path, "%s.ts", s->psz_path) < 0)
2056 s->psz_path = new_path;
2058 p_sys->bandwidth = 0;
2059 p_sys->b_live = true;
2060 p_sys->b_meta = false;
2061 p_sys->b_error = false;
2063 p_sys->hls_stream = vlc_array_new();
2064 if (p_sys->hls_stream == NULL)
2074 s->pf_control = Control;
2076 p_sys->paused = false;
2078 vlc_cond_init(&p_sys->wait);
2079 vlc_mutex_init(&p_sys->lock);
2081 /* Parse HLS m3u8 content. */
2082 uint8_t *buffer = NULL;
2083 ssize_t len = read_M3U8_from_stream(s->p_source, &buffer);
2086 if (parse_M3U8(s, p_sys->hls_stream, buffer, len) != VLC_SUCCESS)
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 );
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);
2101 /* manage encryption key if needed */
2102 hls_ManageSegmentKeys(s, hls_Get(p_sys->hls_stream, current));
2104 if (Prefetch(s, ¤t) != VLC_SUCCESS)
2106 msg_Err(s, "fetching first segment failed.");
2110 p_sys->download.stream = current;
2111 p_sys->playback.stream = current;
2112 p_sys->download.seek = -1;
2114 vlc_mutex_init(&p_sys->download.lock_wait);
2115 vlc_cond_init(&p_sys->download.wait);
2117 vlc_mutex_init(&p_sys->read.lock_wait);
2118 vlc_cond_init(&p_sys->read.wait);
2120 /* Initialize HLS live stream */
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 );
2128 if (vlc_clone(&p_sys->reload, hls_Reload, s, VLC_THREAD_PRIORITY_LOW))
2134 if (vlc_clone(&p_sys->thread, hls_Thread, s, VLC_THREAD_PRIORITY_INPUT))
2137 vlc_join(p_sys->reload, NULL);
2144 vlc_mutex_destroy(&p_sys->download.lock_wait);
2145 vlc_cond_destroy(&p_sys->download.wait);
2147 vlc_mutex_destroy(&p_sys->read.lock_wait);
2148 vlc_cond_destroy(&p_sys->read.wait);
2151 /* Free hls streams */
2152 for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
2154 hls_stream_t *hls = hls_Get(p_sys->hls_stream, i);
2155 if (hls) hls_Free(hls);
2157 vlc_array_destroy(p_sys->hls_stream);
2159 vlc_mutex_destroy(&p_sys->lock);
2160 vlc_cond_destroy(&p_sys->wait);
2165 return VLC_EGENERIC;
2168 /****************************************************************************
2170 ****************************************************************************/
2171 static void Close(vlc_object_t *p_this)
2173 stream_t *s = (stream_t*)p_this;
2174 stream_sys_t *p_sys = s->p_sys;
2176 assert(p_sys->hls_stream);
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);
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);
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);
2198 vlc_mutex_destroy(&p_sys->read.lock_wait);
2199 vlc_cond_destroy(&p_sys->read.wait);
2201 /* Free hls streams */
2202 for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++)
2204 hls_stream_t *hls = hls_Get(p_sys->hls_stream, i);
2205 if (hls) hls_Free(hls);
2207 vlc_array_destroy(p_sys->hls_stream);
2211 vlc_mutex_destroy(&p_sys->lock);
2212 vlc_cond_destroy(&p_sys->wait);
2216 block_Release (p_sys->peeked);
2220 /****************************************************************************
2221 * Stream filters functions
2222 ****************************************************************************/
2223 static segment_t *GetSegment(stream_t *s)
2225 stream_sys_t *p_sys = s->p_sys;
2226 segment_t *segment = NULL;
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);
2232 vlc_mutex_lock(&hls->lock);
2233 segment = segment_GetSegment(hls, p_sys->playback.segment);
2234 if (segment != NULL)
2236 vlc_mutex_lock(&segment->lock);
2237 /* This segment is ready? */
2238 if (segment->data != NULL)
2240 vlc_mutex_unlock(&segment->lock);
2241 p_sys->b_cache = hls->b_cache;
2242 vlc_mutex_unlock(&hls->lock);
2245 vlc_mutex_unlock(&segment->lock);
2247 vlc_mutex_unlock(&hls->lock);
2250 /* Was the HLS stream changed to another bitrate? */
2252 for (int i_stream = 0; i_stream < vlc_array_count(p_sys->hls_stream); i_stream++)
2254 /* Is the next segment ready */
2255 hls_stream_t *hls = hls_Get(p_sys->hls_stream, i_stream);
2259 vlc_mutex_lock(&hls->lock);
2260 segment = segment_GetSegment(hls, p_sys->playback.segment);
2261 if (segment == NULL)
2263 vlc_mutex_unlock(&hls->lock);
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);
2271 vlc_mutex_lock(&segment->lock);
2272 /* This segment is ready? */
2273 if ((segment->data != NULL) &&
2274 (p_sys->playback.segment < i_segment))
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);
2282 vlc_mutex_unlock(&segment->lock);
2283 vlc_mutex_unlock(&hls->lock);
2293 assert(segment->data);
2294 if (segment->data->i_buffer == 0)
2296 vlc_mutex_lock(&hls->lock);
2297 int count = vlc_array_count(hls->segments);
2298 vlc_mutex_unlock(&hls->lock);
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");
2310 static int segment_RestorePos(segment_t *segment)
2314 uint64_t size = segment->size - segment->data->i_buffer;
2317 segment->data->i_buffer += size;
2318 segment->data->p_buffer -= size;
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)
2327 stream_sys_t *p_sys = s->p_sys;
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)
2339 vlc_mutex_lock(&segment->lock);
2340 if (segment->data->i_buffer == 0)
2342 if (!p_sys->b_cache || p_sys->b_live)
2344 block_Release(segment->data);
2345 segment->data = NULL;
2348 segment_RestorePos(segment);
2350 vlc_mutex_unlock(&segment->lock);
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);
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);
2365 if (i_read <= segment->data->i_buffer)
2367 else if (i_read > segment->data->i_buffer)
2368 len = segment->data->i_buffer;
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;
2379 vlc_mutex_unlock(&segment->lock);
2381 } while (i_read > 0);
2386 static int Read(stream_t *s, void *buffer, unsigned int i_read)
2388 stream_sys_t *p_sys = s->p_sys;
2391 assert(p_sys->hls_stream);
2395 // In case an error occurred or the stream was closed return 0
2396 if (p_sys->b_error || !vlc_object_alive(s))
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);
2402 /* NOTE: buffer might be NULL if caller wants to skip data */
2403 length = hls_Read(s, (uint8_t*) buffer, i_read);
2405 // An error has occurred in hls_Read
2408 vlc_mutex_unlock(&p_sys->read.lock_wait);
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
2420 mtime_t start = mdate();
2422 // Wait for 10 seconds
2423 mtime_t timeout_limit = start + (10 * CLOCK_FREQ);
2425 int res = vlc_cond_timedwait(&p_sys->read.wait, &p_sys->read.lock_wait, timeout_limit);
2427 // Error - reached a timeout of 10 seconds without data arriving - kill the stream
2428 if (res == ETIMEDOUT)
2430 msg_Warn(s, "timeout limit reached!");
2432 vlc_mutex_unlock(&p_sys->read.lock_wait);
2436 else if (res == EINVAL)
2437 return 0; // Error - lock is not locked so we can just return
2440 vlc_mutex_unlock(&p_sys->read.lock_wait);
2443 p_sys->playback.offset += length;
2447 static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek)
2449 stream_sys_t *p_sys = s->p_sys;
2451 unsigned int len = i_peek;
2453 segment = GetSegment(s);
2454 if (segment == NULL)
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? */
2461 vlc_mutex_lock(&segment->lock);
2463 size_t i_buff = segment->data->i_buffer;
2464 uint8_t *p_buff = segment->data->p_buffer;
2466 if ( likely(i_peek < i_buff))
2469 vlc_mutex_unlock(&segment->lock);
2473 else /* This will seldom be run */
2475 /* remember segment to read */
2476 int peek_segment = p_sys->playback.segment;
2478 segment_t *nsegment;
2479 p_sys->playback.segment++;
2480 block_t *peeked = p_sys->peeked;
2483 peeked = block_Alloc (i_peek);
2484 else if (peeked->i_buffer < i_peek)
2485 peeked = block_Realloc (peeked, 0, i_peek);
2488 vlc_mutex_unlock(&segment->lock);
2491 p_sys->peeked = peeked;
2493 memcpy(peeked->p_buffer, p_buff, i_buff);
2496 vlc_mutex_unlock(&segment->lock);
2498 i_buff = peeked->i_buffer;
2499 p_buff = peeked->p_buffer;
2502 while (curlen < i_peek)
2504 nsegment = GetSegment(s);
2505 if (nsegment == NULL)
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? */
2514 vlc_mutex_lock(&nsegment->lock);
2516 if (len < nsegment->data->i_buffer)
2518 memcpy(p_buff + curlen, nsegment->data->p_buffer, len);
2523 size_t i_nbuff = nsegment->data->i_buffer;
2524 memcpy(p_buff + curlen, nsegment->data->p_buffer, i_nbuff);
2528 p_sys->playback.segment++;
2531 vlc_mutex_unlock(&nsegment->lock);
2534 /* restore segment to read */
2535 p_sys->playback.segment = peek_segment;
2540 static bool hls_MaySeek(stream_t *s)
2542 stream_sys_t *p_sys = s->p_sys;
2544 if (p_sys->hls_stream == NULL)
2547 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2548 if (hls == NULL) return false;
2552 vlc_mutex_lock(&hls->lock);
2553 int count = vlc_array_count(hls->segments);
2554 vlc_mutex_unlock(&hls->lock);
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);
2564 static uint64_t GetStreamSize(stream_t *s)
2566 stream_sys_t *p_sys = s->p_sys;
2571 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2572 if (hls == NULL) return 0;
2574 vlc_mutex_lock(&hls->lock);
2576 hls->size = hls_GetStreamSize(hls);
2577 uint64_t size = hls->size;
2578 vlc_mutex_unlock(&hls->lock);
2583 static int segment_Seek(stream_t *s, const uint64_t pos)
2585 stream_sys_t *p_sys = s->p_sys;
2587 hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream);
2589 return VLC_EGENERIC;
2591 vlc_mutex_lock(&hls->lock);
2593 bool b_found = false;
2594 uint64_t length = 0;
2595 uint64_t size = hls->size;
2596 int count = vlc_array_count(hls->segments);
2598 segment_t *currentSegment = segment_GetSegment(hls, p_sys->playback.segment);
2599 if (currentSegment == NULL)
2601 vlc_mutex_unlock(&hls->lock);
2602 return VLC_EGENERIC;
2605 for (int n = 0; n < count; n++)
2607 segment_t *segment = segment_GetSegment(hls, n);
2608 if (segment == NULL)
2610 vlc_mutex_unlock(&hls->lock);
2611 return VLC_EGENERIC;
2614 vlc_mutex_lock(&segment->lock);
2615 length += segment->duration * (hls->bandwidth/8);
2616 vlc_mutex_unlock(&segment->lock);
2622 p_sys->playback.segment = n;
2626 /* Do not search in last 3 segments */
2627 vlc_mutex_unlock(&hls->lock);
2628 return VLC_EGENERIC;
2633 if (!b_found && (pos >= size))
2635 p_sys->playback.segment = count - 1;
2643 /* restore current segment to start position */
2644 vlc_mutex_lock(¤tSegment->lock);
2645 segment_RestorePos(currentSegment);
2646 vlc_mutex_unlock(¤tSegment->lock);
2648 /* restore seeked segment to start position */
2649 segment_t *segment = segment_GetSegment(hls, p_sys->playback.segment);
2650 if (segment == NULL)
2652 vlc_mutex_unlock(&hls->lock);
2653 return VLC_EGENERIC;
2656 vlc_mutex_lock(&segment->lock);
2657 segment_RestorePos(segment);
2658 vlc_mutex_unlock(&segment->lock);
2660 /* start download at current playback segment */
2661 vlc_mutex_unlock(&hls->lock);
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);
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)))
2674 vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
2675 if (!vlc_object_alive(s) || s->b_error) break;
2677 vlc_mutex_unlock(&p_sys->download.lock_wait);
2681 vlc_mutex_unlock(&hls->lock);
2683 return b_found ? VLC_SUCCESS : VLC_EGENERIC;
2686 static int Control(stream_t *s, int i_query, va_list args)
2688 stream_sys_t *p_sys = s->p_sys;
2692 case STREAM_CAN_SEEK:
2693 *(va_arg (args, bool *)) = hls_MaySeek(s);
2695 case STREAM_CAN_CONTROL_PACE:
2696 case STREAM_CAN_PAUSE:
2697 *(va_arg (args, bool *)) = true;
2699 case STREAM_CAN_FASTSEEK:
2700 *(va_arg (args, bool *)) = false;
2702 case STREAM_GET_POSITION:
2703 *(va_arg (args, uint64_t *)) = p_sys->playback.offset;
2705 case STREAM_SET_PAUSE_STATE:
2707 bool paused = va_arg (args, unsigned);
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);
2715 case STREAM_SET_POSITION:
2718 uint64_t pos = (uint64_t)va_arg(args, uint64_t);
2719 if (segment_Seek(s, pos) == VLC_SUCCESS)
2721 p_sys->playback.offset = pos;
2725 return VLC_EGENERIC;
2726 case STREAM_GET_SIZE:
2727 *(va_arg (args, uint64_t *)) = GetStreamSize(s);
2729 case STREAM_GET_PTS_DELAY:
2730 *va_arg (args, int64_t *) = INT64_C(1000) *
2731 var_InheritInteger(s, "network-caching");
2734 return VLC_EGENERIC;