X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fstream_filter%2Fhttplive.c;h=fa4201b4968a97fc29b8832ca4ecd679f4b36b38;hb=fc81c4e2b55c1367bbf67cb7a2b98bb63d4ca6d1;hp=bb62be463080d311abf3769c40565a1dc65ef214;hpb=cc8181c8095d5344b5013bb3418f21781f6a9831;p=vlc diff --git a/modules/stream_filter/httplive.c b/modules/stream_filter/httplive.c index bb62be4630..fa4201b496 100644 --- a/modules/stream_filter/httplive.c +++ b/modules/stream_filter/httplive.c @@ -1,24 +1,24 @@ /***************************************************************************** * httplive.c: HTTP Live Streaming stream filter ***************************************************************************** - * Copyright (C) 2010 M2X BV + * Copyright (C) 2010-2012 M2X BV * $Id$ * * Author: Jean-Paul Saman * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** @@ -28,18 +28,20 @@ # include "config.h" #endif +#include + #include #include #include +#include +#include #include #include #include -#include - -#include -#include +#include +#include /***************************************************************************** * Module descriptor @@ -58,14 +60,21 @@ vlc_module_end() /***************************************************************************** * *****************************************************************************/ +#define AES_BLOCK_SIZE 16 /* Only support AES-128 */ typedef struct segment_s { int sequence; /* unique sequence number */ - int duration; /* segment duration (seconds) */ + int duration; /* segment duration (seconds) */ uint64_t size; /* segment size in bytes */ uint64_t bandwidth; /* bandwidth usage of segments (bits per second)*/ - vlc_url_t url; + char *url; + char *psz_key_path; /* url key path */ + uint8_t aes_key[16]; /* AES-128 */ + bool b_key_loaded; + uint8_t psz_AES_IV[AES_BLOCK_SIZE]; /* IV used when decypher the block */ + bool b_iv_loaded; + vlc_mutex_t lock; block_t *data; /* data */ } segment_t; @@ -75,54 +84,77 @@ typedef struct hls_stream_s int id; /* program id */ int version; /* protocol version should be 1 */ int sequence; /* media sequence number */ - int duration; /* maximum duration per segment (ms) */ + int duration; /* maximum duration per segment (s) */ + int max_segment_length; /* maximum duration segments */ uint64_t bandwidth; /* bandwidth usage of segments (bits per second)*/ - uint64_t size; /* stream length (segment->duration * hls->bandwidth/8) */ + uint64_t size; /* stream length is calculated by taking the sum + foreach segment of (segment->duration * hls->bandwidth/8) */ vlc_array_t *segments; /* list of segments */ - vlc_url_t url; /* uri to m3u8 */ + char *url; /* uri to m3u8 */ vlc_mutex_t lock; bool b_cache; /* allow caching */ -} hls_stream_t; - -typedef struct -{ - VLC_COMMON_MEMBERS - - /* */ - int current; /* current hls_stream */ - int segment; /* current segment for downloading */ - int seek; /* segment requested by seek (default -1) */ - vlc_mutex_t lock_wait; /* protect segment download counter */ - vlc_cond_t wait; /* some condition to wait on */ - vlc_array_t *hls_stream;/* bandwidth adaptation */ - stream_t *s; -} hls_thread_t; + char *psz_current_key_path; /* URL path of the encrypted key */ + uint8_t psz_current_AES_IV[AES_BLOCK_SIZE]; /* IV used when decypher the block */ + bool b_current_iv_loaded; +} hls_stream_t; struct stream_sys_t { - access_t *p_access; /* HTTP access input */ + char *m3u8; /* M3U8 url */ + vlc_thread_t reload; /* HLS m3u8 reload thread */ + vlc_thread_t thread; /* HLS segment download thread */ + + block_t *peeked; /* */ - hls_thread_t *thread; - vlc_array_t *hls_stream;/* bandwidth adaptation */ + vlc_array_t *hls_stream; /* bandwidth adaptation */ + uint64_t bandwidth; /* measured bandwidth (bits per second) */ + + /* Download */ + struct hls_download_s + { + int stream; /* current hls_stream */ + int segment; /* current segment for downloading */ + int seek; /* segment requested by seek (default -1) */ + vlc_mutex_t lock_wait; /* protect segment download counter */ + vlc_cond_t wait; /* some condition to wait on */ + } download; /* Playback */ - uint64_t offset; /* current offset in media */ - int current; /* current hls_stream */ - int segment; /* current segment for playback */ + struct hls_playback_s + { + uint64_t offset; /* current offset in media */ + int stream; /* current hls_stream */ + int segment; /* current segment for playback */ + } playback; /* Playlist */ - mtime_t last; /* playlist last loaded */ - mtime_t wakeup; /* next reload time */ - int tries; /* times it was not changed */ + struct hls_playlist_s + { + mtime_t last; /* playlist last loaded */ + mtime_t wakeup; /* next reload time */ + int tries; /* times it was not changed */ + } playlist; + + struct hls_read_s + { + vlc_mutex_t lock_wait; /* used by read condition variable */ + vlc_cond_t wait; /* some condition to wait on during read */ + } read; /* state */ bool b_cache; /* can cache files */ bool b_meta; /* meta playlist */ bool b_live; /* live stream? or vod? */ bool b_error; /* parsing error */ + bool b_aesmsg; /* only print one time that the media is encrypted */ + + /* Shared data */ + vlc_cond_t wait; + vlc_mutex_t lock; + bool paused; }; /**************************************************************************** @@ -132,13 +164,14 @@ static int Read (stream_t *, void *p_read, unsigned int i_read); static int Peek (stream_t *, const uint8_t **pp_peek, unsigned int i_peek); static int Control(stream_t *, int i_query, va_list); -static int AccessOpen(stream_t *s, vlc_url_t *url); -static void AccessClose(stream_t *s); -static char *AccessReadLine(access_t *p_access, uint8_t *psz_tmp, size_t i_len); -static int AccessDownload(stream_t *s, segment_t *segment); +static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer); +static ssize_t read_M3U8_from_url(stream_t *s, const char *psz_url, uint8_t **buffer); +static char *ReadLine(uint8_t *buffer, uint8_t **pos, size_t len); + +static int hls_Download(stream_t *s, segment_t *segment); -static void* hls_Thread(vlc_object_t *); -static int get_HTTPLivePlaylist(stream_t *s, hls_stream_t *hls); +static void* hls_Thread(void *); +static void* hls_Reload(void *); static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted); static void segment_Free(segment_t *segment); @@ -148,36 +181,61 @@ static void segment_Free(segment_t *segment); ****************************************************************************/ static bool isHTTPLiveStreaming(stream_t *s) { - const uint8_t *peek, *peek_end; + const uint8_t *peek; - int64_t i_size = stream_Peek(s->p_source, &peek, 46); - if (i_size < 1) + int size = stream_Peek(s->p_source, &peek, 46); + if (size < 7) return false; - if (strncasecmp((const char*)peek, "#EXTM3U", 7) != 0) + if (memcmp(peek, "#EXTM3U", 7) != 0) return false; + peek += 7; + size -= 7; + /* Parse stream and search for * EXT-X-TARGETDURATION or EXT-X-STREAM-INF tag, see * http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8 */ - peek_end = peek + i_size; - while(peek <= peek_end) + while (size--) { - if (*peek == '#') + static const char *const ext[] = { + "TARGETDURATION", + "MEDIA-SEQUENCE", + "KEY", + "ALLOW-CACHE", + "ENDLIST", + "STREAM-INF", + "DISCONTINUITY", + "VERSION" + }; + + if (*peek++ != '#') + continue; + + if (size < 6) + continue; + + if (memcmp(peek, "EXT-X-", 6)) + continue; + + peek += 6; + size -= 6; + + for (size_t i = 0; i < ARRAY_SIZE(ext); i++) { - if (strncasecmp((const char*)peek, "#EXT-X-TARGETDURATION", 21) == 0) - return true; - else if (strncasecmp((const char*)peek, "#EXT-X-STREAM-INF", 17) == 0) + size_t len = strlen(ext[i]); + if (size < 0 || (size_t)size < len) + continue; + if (!memcmp(peek, ext[i], len)) return true; } - peek++; - }; + } return false; } /* HTTP Live Streaming */ -static hls_stream_t *hls_New(vlc_array_t *hls_stream, int id, uint64_t bw, char *uri) +static hls_stream_t *hls_New(vlc_array_t *hls_stream, const int id, const uint64_t bw, const char *uri) { hls_stream_t *hls = (hls_stream_t *)malloc(sizeof(hls_stream_t)); if (hls == NULL) return NULL; @@ -185,11 +243,18 @@ static hls_stream_t *hls_New(vlc_array_t *hls_stream, int id, uint64_t bw, char hls->id = id; hls->bandwidth = bw; hls->duration = -1;/* unknown */ + hls->max_segment_length = -1;/* unknown */ hls->size = 0; hls->sequence = 0; /* default is 0 */ hls->version = 1; /* default protocol version */ hls->b_cache = true; - vlc_UrlParse(&hls->url, uri, 0); + hls->url = strdup(uri); + if (hls->url == NULL) + { + free(hls); + return NULL; + } + hls->psz_current_key_path = NULL; hls->segments = vlc_array_new(); vlc_array_append(hls_stream, hls); vlc_mutex_init(&hls->lock); @@ -204,18 +269,48 @@ static void hls_Free(hls_stream_t *hls) { for (int n = 0; n < vlc_array_count(hls->segments); n++) { - segment_t *segment = (segment_t *)vlc_array_item_at_index(hls->segments, n); + segment_t *segment = segment_GetSegment(hls, n); if (segment) segment_Free(segment); } vlc_array_destroy(hls->segments); } - - vlc_UrlClean(&hls->url); + free(hls->url); + free(hls->psz_current_key_path); free(hls); - hls = NULL; } -static hls_stream_t *hls_Get(vlc_array_t *hls_stream, int wanted) +static hls_stream_t *hls_Copy(hls_stream_t *src, const bool b_cp_segments) +{ + assert(src); + assert(!b_cp_segments); /* FIXME: copying segments is not implemented */ + + hls_stream_t *dst = (hls_stream_t *)malloc(sizeof(hls_stream_t)); + if (dst == NULL) return NULL; + + dst->id = src->id; + dst->bandwidth = src->bandwidth; + dst->duration = src->duration; + dst->size = src->size; + dst->sequence = src->sequence; + dst->version = src->version; + dst->b_cache = src->b_cache; + dst->psz_current_key_path = src->psz_current_key_path ? + strdup( src->psz_current_key_path ) : NULL; + memcpy(dst->psz_current_AES_IV, src->psz_current_AES_IV, AES_BLOCK_SIZE); + dst->b_current_iv_loaded = src->b_current_iv_loaded; + dst->url = strdup(src->url); + if (dst->url == NULL) + { + free(dst); + return NULL; + } + if (!b_cp_segments) + dst->segments = vlc_array_new(); + vlc_mutex_init(&dst->lock); + return dst; +} + +static hls_stream_t *hls_Get(vlc_array_t *hls_stream, const int wanted) { int count = vlc_array_count(hls_stream); if (count <= 0) @@ -227,7 +322,7 @@ static hls_stream_t *hls_Get(vlc_array_t *hls_stream, int wanted) static inline hls_stream_t *hls_GetFirst(vlc_array_t *hls_stream) { - return (hls_stream_t*) hls_Get(hls_stream, 0); + return hls_Get(hls_stream, 0); } static hls_stream_t *hls_GetLast(vlc_array_t *hls_stream) @@ -236,7 +331,24 @@ static hls_stream_t *hls_GetLast(vlc_array_t *hls_stream) if (count <= 0) return NULL; count--; - return (hls_stream_t *) hls_Get(hls_stream, count); + return hls_Get(hls_stream, count); +} + +static hls_stream_t *hls_Find(vlc_array_t *hls_stream, hls_stream_t *hls_new) +{ + int count = vlc_array_count(hls_stream); + for (int n = 0; n < count; n++) + { + hls_stream_t *hls = hls_Get(hls_stream, n); + if (hls) + { + /* compare */ + if ((hls->id == hls_new->id) && + ((hls->bandwidth == hls_new->bandwidth)||(hls_new->bandwidth==0))) + return hls; + } + } + return NULL; } static uint64_t hls_GetStreamSize(hls_stream_t *hls) @@ -246,6 +358,12 @@ static uint64_t hls_GetStreamSize(hls_stream_t *hls) * then the deviation from exact byte size will be big and the seek/ * progressbar will not behave entirely as one expects. */ uint64_t size = 0UL; + + /* If there is no valid bandwidth yet, then there is no point in + * computing stream size. */ + if (hls->bandwidth == 0) + return size; + int count = vlc_array_count(hls->segments); for (int n = 0; n < count; n++) { @@ -259,7 +377,7 @@ static uint64_t hls_GetStreamSize(hls_stream_t *hls) } /* Segment */ -static segment_t *segment_New(hls_stream_t* hls, int duration, char *uri) +static segment_t *segment_New(hls_stream_t* hls, const int duration, const char *uri) { segment_t *segment = (segment_t *)malloc(sizeof(segment_t)); if (segment == NULL) @@ -269,10 +387,21 @@ static segment_t *segment_New(hls_stream_t* hls, int duration, char *uri) segment->size = 0; /* bytes */ segment->sequence = 0; segment->bandwidth = 0; - vlc_UrlParse(&segment->url, uri, 0); + segment->url = strdup(uri); + if (segment->url == NULL) + { + free(segment); + return NULL; + } segment->data = NULL; vlc_array_append(hls->segments, segment); vlc_mutex_init(&segment->lock); + segment->b_key_loaded = false; + segment->psz_key_path = NULL; + if (hls->psz_current_key_path) + segment->psz_key_path = strdup(hls->psz_current_key_path); + segment->b_iv_loaded = hls->b_current_iv_loaded; + memcpy(segment->psz_AES_IV, hls->psz_current_AES_IV, AES_BLOCK_SIZE); return segment; } @@ -280,14 +409,14 @@ static void segment_Free(segment_t *segment) { vlc_mutex_destroy(&segment->lock); - vlc_UrlClean(&segment->url); + free(segment->url); + free(segment->psz_key_path); if (segment->data) block_Release(segment->data); free(segment); - segment = NULL; } -static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted) +static segment_t *segment_GetSegment(hls_stream_t *hls, const int wanted) { assert(hls); @@ -299,6 +428,65 @@ static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted) return (segment_t *) vlc_array_item_at_index(hls->segments, wanted); } +static segment_t *segment_Find(hls_stream_t *hls, const int sequence) +{ + assert(hls); + + int count = vlc_array_count(hls->segments); + if (count <= 0) return NULL; + for (int n = 0; n < count; n++) + { + segment_t *segment = segment_GetSegment(hls, n); + if (segment == NULL) break; + if (segment->sequence == sequence) + return segment; + } + return NULL; +} + +static int ChooseSegment(stream_t *s, const int current) +{ + stream_sys_t *p_sys = (stream_sys_t *)s->p_sys; + hls_stream_t *hls = hls_Get(p_sys->hls_stream, current); + if (hls == NULL) return 0; + + /* Choose a segment to start which is no closer than + * 3 times the target duration from the end of the playlist. + */ + int wanted = 0; + int duration = 0; + int sequence = 0; + int count = vlc_array_count(hls->segments); + int i = p_sys->b_live ? count - 1 : -1; + + /* We do while loop only with live case, otherwise return 0*/ + while((i >= 0) && (i < count)) + { + segment_t *segment = segment_GetSegment(hls, i); + assert(segment); + + if (segment->duration > hls->duration) + { + msg_Err(s, "EXTINF:%d duration is larger than EXT-X-TARGETDURATION:%d", + segment->duration, hls->duration); + } + + duration += segment->duration; + if (duration >= 3 * hls->duration) + { + /* Start point found */ + wanted = i; + sequence = segment->sequence; + break; + } + + i-- ; + } + + msg_Dbg(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence); + return wanted; +} + /* Parsing */ static char *parse_Attributes(const char *line, const char *attr) { @@ -313,11 +501,24 @@ static char *parse_Attributes(const char *line, const char *attr) begin = p; do { - if (strncasecmp(begin, attr, strlen(attr)) == 0) + if (strncasecmp(begin, attr, strlen(attr)) == 0 + && begin[strlen(attr)] == '=') { - /* =[,]* */ + /* =""[,]* */ p = strchr(begin, ','); begin += strlen(attr) + 1; + + /* Check if we have " " marked value*/ + if( begin[0] == '"' ) + { + char *valueend = strchr( begin+1, '"'); + + /* No ending " so bail out */ + if( unlikely( !valueend ) ) + return NULL; + + p = strchr( valueend, ','); + } if (begin >= end) return NULL; if (p == NULL) /* last attribute */ @@ -331,73 +532,166 @@ static char *parse_Attributes(const char *line, const char *attr) return NULL; } -static char *relative_URI(stream_t *s, const char *uri, char *psz_uri) +static int string_to_IV(char *string_hexa, uint8_t iv[AES_BLOCK_SIZE]) { - char *p = strchr(uri, ':'); - if (p != NULL) - return NULL; + unsigned long long iv_hi, iv_lo; + char *end = NULL; + if (*string_hexa++ != '0') + return VLC_EGENERIC; + if (*string_hexa != 'x' && *string_hexa != 'X') + return VLC_EGENERIC; - char *tmp; - if (asprintf(&tmp,"%s://%s", s->psz_access, s->psz_path) < 0) - { - s->p_sys->b_error = true; - return NULL; + string_hexa++; + + size_t len = strlen(string_hexa); + if (len <= 16) { + iv_hi = 0; + iv_lo = strtoull(string_hexa, &end, 16); + if (*end) + return VLC_EGENERIC; + } else { + iv_lo = strtoull(&string_hexa[len-16], &end, 16); + if (*end) + return VLC_EGENERIC; + string_hexa[len-16] = '\0'; + iv_hi = strtoull(string_hexa, &end, 16); + if (*end) + return VLC_EGENERIC; + } + + for (int i = 7; i >= 0 ; --i) { + iv[ i] = iv_hi & 0xff; + iv[8+i] = iv_lo & 0xff; + iv_hi >>= 8; + iv_lo >>= 8; } - char *psz_path = strrchr(tmp, '/'); - if (psz_path) *psz_path = '\0'; + return VLC_SUCCESS; +} - vlc_url_t url; - vlc_UrlParse(&url, tmp, 0); - if (asprintf(&psz_uri, "%s://%s%s/%s", - url.psz_protocol, url.psz_host, url.psz_path, uri) < 0) - { - free(tmp); - vlc_UrlClean(&url); - s->p_sys->b_error = true; +static char *relative_URI(const char *psz_url, const char *psz_path) +{ + char *ret = NULL; + const char *fmt; + assert(psz_url != NULL && psz_path != NULL); + + + //If the path is actually an absolute URL, don't do anything. + if (strncmp(psz_path, "http", 4) == 0) + return NULL; + + size_t len = strlen(psz_path); + + char *new_url = strdup(psz_url); + if (unlikely(!new_url)) return NULL; + + if( psz_path[0] == '/' ) //Relative URL with absolute path + { + //Try to find separator for name and path, try to skip + //access and first :// + char *slash = strchr(&new_url[8], '/'); + if (unlikely(slash == NULL)) + goto end; + *slash = '\0'; + fmt = "%s%s"; + } else { + int levels = 0; + while(len >= 3 && !strncmp(psz_path, "../", 3)) { + psz_path += 3; + len -= 3; + levels++; + } + do { + char *slash = strrchr(new_url, '/'); + if (unlikely(slash == NULL)) + goto end; + *slash = '\0'; + } while (levels--); + fmt = "%s/%s"; } - vlc_UrlClean(&url); - free(tmp); - return psz_uri; + + if (asprintf(&ret, fmt, new_url, psz_path) < 0) + ret = NULL; + +end: + free(new_url); + return ret; } -static void parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_read, char *uri) +static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *duration) { - stream_sys_t *p_sys = s->p_sys; - assert(hls); + assert(p_read); - int duration; - int ret = sscanf(p_read, "#EXTINF:%d,", &duration); - if (ret != 1) + /* strip of #EXTINF: */ + char *p_next = NULL; + char *token = strtok_r(p_read, ":", &p_next); + if (token == NULL) + return VLC_EGENERIC; + + /* read duration */ + token = strtok_r(NULL, ",", &p_next); + if (token == NULL) + return VLC_EGENERIC; + + int value; + char *endptr; + if (hls->version < 3) + { + errno = 0; + value = strtol(token, &endptr, 10); + if (token == endptr || errno == ERANGE) + { + *duration = -1; + return VLC_EGENERIC; + } + *duration = value; + } + else { - msg_Err(s, "expected #EXTINF:,"); - p_sys->b_error = true; - return; + errno = 0; + double d = strtof(token, &endptr); + if (token == endptr || errno == ERANGE) + { + *duration = -1; + return VLC_EGENERIC; + } + if ((d) - ((int)d) >= 0.5) + value = ((int)d) + 1; + else + value = ((int)d); + *duration = value; } + if( *duration > hls->max_segment_length) + hls->max_segment_length = *duration; - char *psz_uri = NULL; - psz_uri = relative_URI(s, uri, psz_uri); + /* Ignore the rest of the line */ + return VLC_SUCCESS; +} + +static int parse_AddSegment(hls_stream_t *hls, const int duration, const char *uri) +{ + assert(hls); + assert(uri); + /* Store segment information */ vlc_mutex_lock(&hls->lock); + + char *psz_uri = relative_URI(hls->url, uri); + segment_t *segment = segment_New(hls, duration, psz_uri ? psz_uri : uri); if (segment) segment->sequence = hls->sequence + vlc_array_count(hls->segments) - 1; - if (duration > hls->duration) - { - msg_Err(s, "EXTINF:%d duration is larger then EXT-X-TARGETDURATION:%d", - duration, hls->duration); - } + free(psz_uri); + vlc_mutex_unlock(&hls->lock); - free(psz_uri); + return segment ? VLC_SUCCESS : VLC_ENOMEM; } -static void parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read) +static int parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read) { - stream_sys_t *p_sys = s->p_sys; - assert(hls); int duration = -1; @@ -405,37 +699,36 @@ static void parse_TargetDuration(stream_t *s, hls_stream_t *hls, char *p_read) if (ret != 1) { msg_Err(s, "expected #EXT-X-TARGETDURATION:"); - p_sys->b_error = true; - return; + return VLC_EGENERIC; } hls->duration = duration; /* seconds */ + return VLC_SUCCESS; } -static void parse_StreamInformation(stream_t *s, char *p_read, char *uri) +static int parse_StreamInformation(stream_t *s, vlc_array_t **hls_stream, + hls_stream_t **hls, char *p_read, const char *uri) { - stream_sys_t *p_sys = s->p_sys; - int id; uint64_t bw; char *attr; + assert(*hls == NULL); + attr = parse_Attributes(p_read, "PROGRAM-ID"); - if (attr == NULL) + if (attr) { - msg_Err(s, "#EXT-X-STREAM-INF: expected PROGRAM-ID="); - p_sys->b_error = true; - return; + id = atol(attr); + free(attr); } - id = atol(attr); - free(attr); + else + id = 0; attr = parse_Attributes(p_read, "BANDWIDTH"); if (attr == NULL) { msg_Err(s, "#EXT-X-STREAM-INF: expected BANDWIDTH="); - p_sys->b_error = true; - return; + return VLC_EGENERIC; } bw = atoll(attr); free(attr); @@ -443,26 +736,22 @@ static void parse_StreamInformation(stream_t *s, char *p_read, char *uri) if (bw == 0) { msg_Err(s, "#EXT-X-STREAM-INF: bandwidth cannot be 0"); - p_sys->b_error = true; - return; + return VLC_EGENERIC; } - msg_Info(s, "bandwidth adaption detected (program-id=%d, bandwidth=%"PRIu64").", id, bw); + msg_Dbg(s, "bandwidth adaptation detected (program-id=%d, bandwidth=%"PRIu64").", id, bw); - char *psz_uri = NULL; - psz_uri = relative_URI(s, uri, psz_uri); + char *psz_uri = relative_URI(s->p_sys->m3u8, uri); - hls_stream_t *hls = hls_New(p_sys->hls_stream, id, bw, psz_uri ? psz_uri : uri); - if (hls == NULL) - p_sys->b_error = true; + *hls = hls_New(*hls_stream, id, bw, psz_uri ? psz_uri : uri); free(psz_uri); + + return (*hls == NULL) ? VLC_ENOMEM : VLC_SUCCESS; } -static void parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read) +static int parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read) { - stream_sys_t *p_sys = s->p_sys; - assert(hls); int sequence; @@ -470,37 +759,48 @@ static void parse_MediaSequence(stream_t *s, hls_stream_t *hls, char *p_read) if (ret != 1) { msg_Err(s, "expected #EXT-X-MEDIA-SEQUENCE:"); - p_sys->b_error = true; - return; + return VLC_EGENERIC; } if (hls->sequence > 0) - msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist"); - + { + if (s->p_sys->b_live) + { + hls_stream_t *last = hls_GetLast(s->p_sys->hls_stream); + segment_t *last_segment = segment_GetSegment( last, vlc_array_count( last->segments ) - 1 ); + if ( ( last_segment->sequence < sequence) && + ( sequence - last_segment->sequence >= 1 )) + msg_Err(s, "EXT-X-MEDIA-SEQUENCE gap in playlist (new=%d, old=%d)", + sequence, last_segment->sequence); + } + else + msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist (new=%d, old=%d)", + sequence, hls->sequence); + } hls->sequence = sequence; + return VLC_SUCCESS; } -static void parse_Key(stream_t *s, hls_stream_t *hls, char *p_read) +static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read) { - stream_sys_t *p_sys = s->p_sys; - assert(hls); /* #EXT-X-KEY:METHOD=[,URI=""][,IV=] */ - char *attr; - attr = parse_Attributes(p_read, "METHOD"); + int err = VLC_SUCCESS; + char *attr = parse_Attributes(p_read, "METHOD"); if (attr == NULL) { msg_Err(s, "#EXT-X-KEY: expected METHOD="); - p_sys->b_error = true; + return err; } - else if (strncasecmp(attr, "NONE", 4) == 0) + + if (strncasecmp(attr, "NONE", 4) == 0) { char *uri = parse_Attributes(p_read, "URI"); if (uri != NULL) { msg_Err(s, "#EXT-X-KEY: URI not expected"); - p_sys->b_error = true; + err = VLC_EGENERIC; } free(uri); /* IV is only supported in version 2 and above */ @@ -510,29 +810,94 @@ static void parse_Key(stream_t *s, hls_stream_t *hls, char *p_read) if (iv != NULL) { msg_Err(s, "#EXT-X-KEY: IV not expected"); - p_sys->b_error = true; + err = VLC_EGENERIC; } free(iv); } } + else if (strncasecmp(attr, "AES-128", 7) == 0) + { + char *value, *uri, *iv; + if (s->p_sys->b_aesmsg == false) + { + msg_Dbg(s, "playback of AES-128 encrypted HTTP Live media detected."); + s->p_sys->b_aesmsg = true; + } + value = uri = parse_Attributes(p_read, "URI"); + if (value == NULL) + { + msg_Err(s, "#EXT-X-KEY: URI not found for encrypted HTTP Live media in AES-128"); + free(attr); + return VLC_EGENERIC; + } + + /* Url is put between quotes, remove them */ + if (*value == '"') + { + /* We need to strip the "" from the attribute value */ + uri = value + 1; + char* end = strchr(uri, '"'); + if (end != NULL) + *end = 0; + } + /* For absolute URI, just duplicate it + * don't limit to HTTP, maybe some sanity checking + * should be done more in here? */ + if( strstr( uri , "://" ) ) + hls->psz_current_key_path = strdup( uri ); + else + hls->psz_current_key_path = relative_URI(hls->url, uri); + free(value); + + value = iv = parse_Attributes(p_read, "IV"); + if (iv == NULL) + { + /* + * If the EXT-X-KEY tag does not have the IV attribute, implementations + * MUST use the sequence number of the media file as the IV when + * encrypting or decrypting that media file. The big-endian binary + * representation of the sequence number SHALL be placed in a 16-octet + * buffer and padded (on the left) with zeros. + */ + hls->b_current_iv_loaded = false; + } + else + { + /* + * If the EXT-X-KEY tag has the IV attribute, implementations MUST use + * the attribute value as the IV when encrypting or decrypting with that + * key. The value MUST be interpreted as a 128-bit hexadecimal number + * and MUST be prefixed with 0x or 0X. + */ + + if (string_to_IV(iv, hls->psz_current_AES_IV) == VLC_EGENERIC) + { + msg_Err(s, "IV invalid"); + err = VLC_EGENERIC; + } + else + hls->b_current_iv_loaded = true; + free(value); + } + } else { msg_Warn(s, "playback of encrypted HTTP Live media is not supported."); - p_sys->b_error = true; + err = VLC_EGENERIC; } free(attr); + return err; } -static void parse_ProgramDateTime(stream_t *s, hls_stream_t *hls, char *p_read) +static int parse_ProgramDateTime(stream_t *s, hls_stream_t *hls, char *p_read) { VLC_UNUSED(hls); msg_Dbg(s, "tag not supported: #EXT-X-PROGRAM-DATE-TIME %s", p_read); + return VLC_SUCCESS; } -static void parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read) +static int parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read) { - stream_sys_t *p_sys = s->p_sys; - assert(hls); char answer[4] = "\0"; @@ -540,17 +905,15 @@ static void parse_AllowCache(stream_t *s, hls_stream_t *hls, char *p_read) if (ret != 1) { msg_Err(s, "#EXT-X-ALLOW-CACHE, ignoring ..."); - p_sys->b_error = true; - return; + return VLC_EGENERIC; } hls->b_cache = (strncmp(answer, "NO", 2) != 0); + return VLC_SUCCESS; } -static void parse_Version(stream_t *s, hls_stream_t *hls, char *p_read) +static int parse_Version(stream_t *s, hls_stream_t *hls, char *p_read) { - stream_sys_t *p_sys = s->p_sys; - assert(hls); int version; @@ -558,273 +921,612 @@ static void parse_Version(stream_t *s, hls_stream_t *hls, char *p_read) if (ret != 1) { msg_Err(s, "#EXT-X-VERSION: no protocol version found, should be version 1."); - p_sys->b_error = true; - return; + return VLC_EGENERIC; } /* Check version */ hls->version = version; - if (hls->version != 1) + if (hls->version <= 0 || hls->version > 3) { - msg_Err(s, "#EXT-X-VERSION should be version 1 iso %d", version); - p_sys->b_error = true; + msg_Err(s, "#EXT-X-VERSION should be version 1, 2 or 3 iso %d", version); + return VLC_EGENERIC; } + return VLC_SUCCESS; } -static void parse_EndList(stream_t *s, hls_stream_t *hls) +static int parse_EndList(stream_t *s, hls_stream_t *hls) { - stream_sys_t *p_sys = s->p_sys; - assert(hls); - p_sys->b_live = false; - msg_Info(s, "video on demand (vod) mode"); + s->p_sys->b_live = false; + msg_Dbg(s, "video on demand (vod) mode"); + return VLC_SUCCESS; } -static void parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read) +static int parse_Discontinuity(stream_t *s, hls_stream_t *hls, char *p_read) { assert(hls); /* FIXME: Do we need to act on discontinuity ?? */ msg_Dbg(s, "#EXT-X-DISCONTINUITY %s", p_read); + return VLC_SUCCESS; } -static void parse_M3U8ExtLine(stream_t *s, hls_stream_t *hls, char *line) +static int hls_CompareStreams( const void* a, const void* b ) { - if (*line == '#') - { - if (strncmp(line, "#EXT-X-TARGETDURATION", 21) == 0) - parse_TargetDuration(s, hls, line); - else if (strncmp(line, "#EXT-X-MEDIA-SEQUENCE", 22) == 0) - parse_MediaSequence(s, hls, line); - else if (strncmp(line, "#EXT-X-KEY", 11) == 0) - parse_Key(s, hls, line); - else if (strncmp(line, "#EXT-X-PROGRAM-DATE-TIME", 25) == 0) - parse_ProgramDateTime(s, hls, line); - else if (strncmp(line, "#EXT-X-ALLOW-CACHE", 17) == 0) - parse_AllowCache(s, hls, line); - else if (strncmp(line, "#EXT-X-DISCONTINUITY", 20) == 0) - parse_Discontinuity(s, hls, line); - else if (strncmp(line, "#EXT-X-VERSION", 14) == 0) - parse_Version(s, hls, line); - else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0) - parse_EndList(s, hls); - } + hls_stream_t* stream_a = *(hls_stream_t**)a; + hls_stream_t* stream_b = *(hls_stream_t**)b; + + return stream_a->bandwidth - stream_b->bandwidth; } -#define HTTPLIVE_MAX_LINE 4096 -static int get_HTTPLivePlaylist(stream_t *s, hls_stream_t *hls) +/* The http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8 + * document defines the following new tags: EXT-X-TARGETDURATION, + * EXT-X-MEDIA-SEQUENCE, EXT-X-KEY, EXT-X-PROGRAM-DATE-TIME, EXT-X- + * ALLOW-CACHE, EXT-X-STREAM-INF, EXT-X-ENDLIST, EXT-X-DISCONTINUITY, + * and EXT-X-VERSION. + */ +static int parse_M3U8(stream_t *s, vlc_array_t *streams, uint8_t *buffer, const ssize_t len) { stream_sys_t *p_sys = s->p_sys; + uint8_t *p_read, *p_begin, *p_end; - /* Download new playlist file from server */ - if (AccessOpen(s, &hls->url) != VLC_SUCCESS) - return VLC_EGENERIC; + assert(streams); + assert(buffer); - /* Parse the rest of the reply */ - uint8_t *tmp = calloc(1, HTTPLIVE_MAX_LINE); - if (tmp == NULL) - { - AccessClose(s); + msg_Dbg(s, "parse_M3U8\n%s", buffer); + p_begin = buffer; + p_end = p_begin + len; + + char *line = ReadLine(p_begin, &p_read, p_end - p_begin); + if (line == NULL) return VLC_ENOMEM; - } + p_begin = p_read; - char *line = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE); if (strncmp(line, "#EXTM3U", 7) != 0) { - msg_Err(s, "missing #EXTM3U tag"); - goto error; + msg_Err(s, "missing #EXTM3U tag .. aborting"); + free(line); + return VLC_EGENERIC; } + free(line); line = NULL; - for( ; ; ) + /* What is the version ? */ + int version = 1; + uint8_t *p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-VERSION:"); + if (p != NULL) { - line = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE); - if (line == NULL) + uint8_t *tmp = NULL; + char *psz_version = ReadLine(p, &tmp, p_end - p); + if (psz_version == NULL) + return VLC_ENOMEM; + int ret = sscanf((const char*)psz_version, "#EXT-X-VERSION:%d", &version); + if (ret != 1) { - msg_Dbg(s, "end of data"); - break; + msg_Warn(s, "#EXT-X-VERSION: no protocol version found, assuming version 1."); + version = 1; } + free(psz_version); + p = NULL; + } - if (!vlc_object_alive(s)) - goto error; + /* Is it a live stream ? */ + p_sys->b_live = (strstr((const char *)buffer, "#EXT-X-ENDLIST") == NULL) ? true : false; - /* some more checks for actual data */ - if (strncmp(line, "#EXTINF", 7) == 0) - { - char *uri = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE); - if (uri == NULL) - p_sys->b_error = true; - else + /* Is it a meta index file ? */ + bool b_meta = (strstr((const char *)buffer, "#EXT-X-STREAM-INF") == NULL) ? false : true; + + int err = VLC_SUCCESS; + + if (b_meta) + { + msg_Dbg(s, "Meta playlist"); + + /* M3U8 Meta Index file */ + do { + /* Next line */ + line = ReadLine(p_begin, &p_read, p_end - p_begin); + if (line == NULL) + break; + p_begin = p_read; + + /* */ + if (strncmp(line, "#EXT-X-STREAM-INF", 17) == 0) { - parse_SegmentInformation(s, hls, line, uri); - free(uri); + p_sys->b_meta = true; + char *uri = ReadLine(p_begin, &p_read, p_end - p_begin); + if (uri == NULL) + err = VLC_ENOMEM; + else + { + if (*uri == '#') + { + msg_Warn(s, "Skipping invalid stream-inf: %s", uri); + free(uri); + } + else + { + bool new_stream_added = false; + hls_stream_t *hls = NULL; + err = parse_StreamInformation(s, &streams, &hls, line, uri); + if (err == VLC_SUCCESS) + new_stream_added = true; + + free(uri); + + if (hls) + { + /* Download playlist file from server */ + uint8_t *buf = NULL; + ssize_t len = read_M3U8_from_url(s, hls->url, &buf); + if (len < 0) + { + msg_Warn(s, "failed to read %s, continue for other streams", hls->url); + + /* remove stream just added */ + if (new_stream_added) + vlc_array_remove(streams, vlc_array_count(streams) - 1); + + /* ignore download error, so we have chance to try other streams */ + err = VLC_SUCCESS; + } + else + { + /* Parse HLS m3u8 content. */ + err = parse_M3U8(s, streams, buf, len); + free(buf); + } + + hls->version = version; + if (!p_sys->b_live) + hls->size = hls_GetStreamSize(hls); /* Stream size (approximate) */ + } + } + } + p_begin = p_read; } - } - else - { - parse_M3U8ExtLine(s, hls, line); - } - /* Error during m3u8 parsing abort */ - if (p_sys->b_error) - goto error; + free(line); + line = NULL; - free(line); - } + if (p_begin >= p_end) + break; - free(line); - free(tmp); - AccessClose(s); - return VLC_SUCCESS; + } while (err == VLC_SUCCESS); -error: - free(line); - free(tmp); - AccessClose(s); - return VLC_EGENERIC; -} -#undef HTTPLIVE_MAX_LINE + size_t stream_count = vlc_array_count(streams); + msg_Dbg(s, "%d streams loaded in Meta playlist", (int)stream_count); + if (stream_count == 0) + { + msg_Err(s, "No playable streams found in Meta playlist"); + err = VLC_EGENERIC; + } + } + else + { + msg_Dbg(s, "%s Playlist HLS protocol version: %d", p_sys->b_live ? "Live": "VOD", version); -/* The http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8 - * document defines the following new tags: EXT-X-TARGETDURATION, - * EXT-X-MEDIA-SEQUENCE, EXT-X-KEY, EXT-X-PROGRAM-DATE-TIME, EXT-X- - * ALLOW-CACHE, EXT-X-STREAM-INF, EXT-X-ENDLIST, EXT-X-DISCONTINUITY, - * and EXT-X-VERSION. - */ -static int parse_HTTPLiveStreaming(stream_t *s) -{ - stream_sys_t *p_sys = s->p_sys; - char *p_read, *p_begin, *p_end; + hls_stream_t *hls = NULL; + if (p_sys->b_meta) + hls = hls_GetLast(streams); + else + { + /* No Meta playlist used */ + hls = hls_New(streams, 0, 0, p_sys->m3u8); + if (hls) + { + /* Get TARGET-DURATION first */ + p = (uint8_t *)strstr((const char *)buffer, "#EXT-X-TARGETDURATION:"); + if (p) + { + uint8_t *p_rest = NULL; + char *psz_duration = ReadLine(p, &p_rest, p_end - p); + if (psz_duration == NULL) + return VLC_EGENERIC; + err = parse_TargetDuration(s, hls, psz_duration); + free(psz_duration); + p = NULL; + } - assert(p_sys->hls_stream); + /* Store version */ + hls->version = version; + } + else return VLC_ENOMEM; + } + assert(hls); - p_begin = p_read = stream_ReadLine(s->p_source); - if (!p_begin) - return VLC_ENOMEM; + /* */ + bool media_sequence_loaded = false; + int segment_duration = -1; + do + { + /* Next line */ + line = ReadLine(p_begin, &p_read, p_end - p_begin); + if (line == NULL) + break; + p_begin = p_read; - /* */ - int i_len = strlen(p_begin); - p_end = p_read + i_len; + if (strncmp(line, "#EXTINF", 7) == 0) + err = parse_SegmentInformation(hls, line, &segment_duration); + else if (strncmp(line, "#EXT-X-TARGETDURATION", 21) == 0) + err = parse_TargetDuration(s, hls, line); + else if (strncmp(line, "#EXT-X-MEDIA-SEQUENCE", 21) == 0) + { + /* A Playlist file MUST NOT contain more than one EXT-X-MEDIA-SEQUENCE tag. */ + /* We only care about first one */ + if (!media_sequence_loaded) + { + err = parse_MediaSequence(s, hls, line); + media_sequence_loaded = true; + } + } + else if (strncmp(line, "#EXT-X-KEY", 10) == 0) + err = parse_Key(s, hls, line); + else if (strncmp(line, "#EXT-X-PROGRAM-DATE-TIME", 24) == 0) + err = parse_ProgramDateTime(s, hls, line); + else if (strncmp(line, "#EXT-X-ALLOW-CACHE", 18) == 0) + err = parse_AllowCache(s, hls, line); + else if (strncmp(line, "#EXT-X-DISCONTINUITY", 20) == 0) + err = parse_Discontinuity(s, hls, line); + else if (strncmp(line, "#EXT-X-VERSION", 14) == 0) + err = parse_Version(s, hls, line); + else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0) + err = parse_EndList(s, hls); + else if ((strncmp(line, "#", 1) != 0) && (*line != '\0') ) + { + err = parse_AddSegment(hls, segment_duration, line); + segment_duration = -1; /* reset duration */ + } + + free(line); + line = NULL; + + if (p_begin >= p_end) + break; + + } while (err == VLC_SUCCESS); + + free(line); + } + + return err; +} - if (strncmp(p_read, "#EXTM3U", 7) != 0) + +static int hls_DownloadSegmentKey(stream_t *s, segment_t *seg) +{ + stream_t *p_m3u8 = stream_UrlNew(s, seg->psz_key_path); + if (p_m3u8 == NULL) { - msg_Err(s, "missing #EXTM3U tag .. aborting"); - free(p_begin); + msg_Err(s, "Failed to load the AES key for segment sequence %d", seg->sequence); return VLC_EGENERIC; } - do { - free(p_begin); - - if (p_sys->b_error) - return VLC_EGENERIC; + int len = stream_Read(p_m3u8, seg->aes_key, sizeof(seg->aes_key)); + stream_Delete(p_m3u8); + if (len != AES_BLOCK_SIZE) + { + msg_Err(s, "The AES key loaded doesn't have the right size (%d)", len); + return VLC_EGENERIC; + } - /* Next line */ - p_begin = stream_ReadLine(s->p_source); - if (p_begin == NULL) - break; + return VLC_SUCCESS; +} - i_len = strlen(p_begin); - p_read = p_begin; - p_end = p_read + i_len; +static int hls_ManageSegmentKeys(stream_t *s, hls_stream_t *hls) +{ + segment_t *seg = NULL; + segment_t *prev_seg; + int count = vlc_array_count(hls->segments); - if (strncmp(p_read, "#EXT-X-STREAM-INF", 17) == 0) + for (int i = 0; i < count; i++) + { + prev_seg = seg; + seg = segment_GetSegment(hls, i); + if (seg == NULL ) + continue; + if (seg->psz_key_path == NULL) + continue; /* No key to load ? continue */ + if (seg->b_key_loaded) + continue; /* The key is already loaded */ + + /* if the key has not changed, and already available from previous segment, + * try to copy it, and don't load the key */ + if (prev_seg && prev_seg->b_key_loaded && strcmp(seg->psz_key_path, prev_seg->psz_key_path) == 0) { - p_sys->b_meta = true; - char *uri = stream_ReadLine(s->p_source); - if (uri == NULL) - p_sys->b_error = true; - else - { - parse_StreamInformation(s, p_read, uri); - free(uri); - } + memcpy(seg->aes_key, prev_seg->aes_key, AES_BLOCK_SIZE); + seg->b_key_loaded = true; + continue; } - else if (strncmp(p_read, "#EXTINF", 7) == 0) + if (hls_DownloadSegmentKey(s, seg) != VLC_SUCCESS) + return VLC_EGENERIC; + seg->b_key_loaded = true; + } + return VLC_SUCCESS; +} + +static int hls_DecodeSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segment) +{ + /* Did the segment need to be decoded ? */ + if (segment->psz_key_path == NULL) + return VLC_SUCCESS; + + /* Do we have loaded the key ? */ + if (!segment->b_key_loaded) + { + /* No ? try to download it now */ + if (hls_ManageSegmentKeys(s, hls) != VLC_SUCCESS) + return VLC_EGENERIC; + } + + /* For now, we only decode AES-128 data */ + gcry_error_t i_gcrypt_err; + gcry_cipher_hd_t aes_ctx; + /* Setup AES */ + i_gcrypt_err = gcry_cipher_open(&aes_ctx, GCRY_CIPHER_AES, + GCRY_CIPHER_MODE_CBC, 0); + if (i_gcrypt_err) + { + msg_Err(s, "gcry_cipher_open failed: %s", gpg_strerror(i_gcrypt_err)); + gcry_cipher_close(aes_ctx); + return VLC_EGENERIC; + } + + /* Set key */ + i_gcrypt_err = gcry_cipher_setkey(aes_ctx, segment->aes_key, + sizeof(segment->aes_key)); + if (i_gcrypt_err) + { + msg_Err(s, "gcry_cipher_setkey failed: %s", gpg_strerror(i_gcrypt_err)); + gcry_cipher_close(aes_ctx); + return VLC_EGENERIC; + } + + if (segment->b_iv_loaded == false) + { + memset(segment->psz_AES_IV, 0, AES_BLOCK_SIZE); + segment->psz_AES_IV[15] = segment->sequence & 0xff; + segment->psz_AES_IV[14] = (segment->sequence >> 8)& 0xff; + segment->psz_AES_IV[13] = (segment->sequence >> 16)& 0xff; + segment->psz_AES_IV[12] = (segment->sequence >> 24)& 0xff; + } + + i_gcrypt_err = gcry_cipher_setiv(aes_ctx, segment->psz_AES_IV, + sizeof(segment->psz_AES_IV)); + + if (i_gcrypt_err) + { + msg_Err(s, "gcry_cipher_setiv failed: %s", gpg_strerror(i_gcrypt_err)); + gcry_cipher_close(aes_ctx); + return VLC_EGENERIC; + } + + i_gcrypt_err = gcry_cipher_decrypt(aes_ctx, + segment->data->p_buffer, /* out */ + segment->data->i_buffer, + NULL, /* in */ + 0); + if (i_gcrypt_err) + { + msg_Err(s, "gcry_cipher_decrypt failed: %s/%s\n", gcry_strsource(i_gcrypt_err), gcry_strerror(i_gcrypt_err)); + gcry_cipher_close(aes_ctx); + return VLC_EGENERIC; + } + gcry_cipher_close(aes_ctx); + /* remove the PKCS#7 padding from the buffer */ + int pad = segment->data->p_buffer[segment->data->i_buffer-1]; + if (pad <= 0 || pad > AES_BLOCK_SIZE) + { + msg_Err(s, "Bad padding character (0x%x), perhaps we failed to decrypt the segment with the correct key", pad); + return VLC_EGENERIC; + } + int count = pad; + while (count--) + { + if (segment->data->p_buffer[segment->data->i_buffer-1-count] != pad) { - char *uri = stream_ReadLine(s->p_source); - if (uri == NULL) - p_sys->b_error = true; - else - { - hls_stream_t *hls = hls_GetLast(p_sys->hls_stream); - if (hls) - parse_SegmentInformation(s, hls, p_read, uri); - else - p_sys->b_error = true; - free(uri); - } + msg_Err(s, "Bad ending buffer, perhaps we failed to decrypt the segment with the correct key"); + return VLC_EGENERIC; } + } + + /* not all the data is readable because of padding */ + segment->data->i_buffer -= pad; + + return VLC_SUCCESS; +} + +static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams) +{ + stream_sys_t *p_sys = s->p_sys; + assert(*streams); + int err = VLC_EGENERIC; + + /* Duplicate HLS stream META information */ + for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++) + { + hls_stream_t *src, *dst; + src = hls_Get(p_sys->hls_stream, i); + if (src == NULL) + return VLC_EGENERIC; + + dst = hls_Copy(src, false); + if (dst == NULL) + return VLC_ENOMEM; + vlc_array_append(*streams, dst); + + /* Download playlist file from server */ + uint8_t *buf = NULL; + ssize_t len = read_M3U8_from_url(s, dst->url, &buf); + if (len < 0) + err = VLC_EGENERIC; else { - hls_stream_t *hls = hls_GetLast(p_sys->hls_stream); - if (hls == NULL) - { - if (!p_sys->b_meta) - { - hls = hls_New(p_sys->hls_stream, -1, -1, NULL); - if (hls == NULL) - { - p_sys->b_error = true; - return VLC_ENOMEM; - } - } - } - /* Parse M3U8 Ext Line */ - parse_M3U8ExtLine(s, hls, p_read); + /* Parse HLS m3u8 content. */ + err = parse_M3U8(s, *streams, buf, len); + free(buf); } - } while(p_read < p_end); + } + return err; +} - free(p_begin); +/* Update hls_old (an existing member of p_sys->hls_stream) to match hls_new + (which represents a downloaded, perhaps newer version of the same playlist) */ +static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *hls_old, bool *stream_appended) +{ + int count = vlc_array_count(hls_new->segments); - /* */ - int count = vlc_array_count(p_sys->hls_stream); + msg_Dbg(s, "updating hls stream (program-id=%d, bandwidth=%"PRIu64") has %d segments", + hls_new->id, hls_new->bandwidth, count); + + vlc_mutex_lock(&hls_old->lock); + hls_old->max_segment_length=-1; for (int n = 0; n < count; n++) { - hls_stream_t *hls = hls_Get(p_sys->hls_stream, n); - if (hls == NULL) break; - - /* Is it a meta playlist? */ - if (p_sys->b_meta) + segment_t *p = segment_GetSegment(hls_new, n); + if (p == NULL) { - msg_Dbg(s, "parsing %s", hls->url.psz_path); - if (get_HTTPLivePlaylist(s, hls) != VLC_SUCCESS) - { - msg_Err(s, "could not parse playlist file from meta index." ); - return VLC_EGENERIC; - } + vlc_mutex_unlock(&hls_old->lock); + return VLC_EGENERIC; } - vlc_mutex_lock(&hls->lock); - if (p_sys->b_live) + segment_t *segment = segment_Find(hls_old, p->sequence); + if (segment) { + vlc_mutex_lock(&segment->lock); + + assert(p->url); + assert(segment->url); - /* There should at least be 3 segments of hls->duration */ - int ok = 0; - int num = vlc_array_count(hls->segments); - for (int i = 0; i < num; i++) + /* they should be the same */ + if ((p->sequence != segment->sequence) || + (p->duration != segment->duration) || + (strcmp(p->url, segment->url) != 0)) { - segment_t *segment = segment_GetSegment(hls, i); - if (segment && segment->duration >= hls->duration) - ok++; + msg_Warn(s, "existing segment found with different content - resetting"); + msg_Warn(s, "- sequence: new=%d, old=%d", p->sequence, segment->sequence); + msg_Warn(s, "- duration: new=%d, old=%d", p->duration, segment->duration); + msg_Warn(s, "- file: new=%s", p->url); + msg_Warn(s, " old=%s", segment->url); + + /* Resetting content */ + segment->sequence = p->sequence; + segment->duration = p->duration; + free(segment->url); + segment->url = strdup(p->url); + if ( segment->url == NULL ) + { + msg_Err(s, "Failed updating segment %d - skipping it", p->sequence); + segment_Free(p); + vlc_mutex_unlock(&segment->lock); + continue; + } + /* We must free the content, because if the key was not downloaded, content can't be decrypted */ + if ((p->psz_key_path || p->b_key_loaded) && + segment->data) + { + block_Release(segment->data); + segment->data = NULL; + } + free(segment->psz_key_path); + segment->psz_key_path = p->psz_key_path ? strdup(p->psz_key_path) : NULL; + segment_Free(p); } - if (ok < 3) - { - msg_Err(s, "cannot start live playback at this time, try again later."); - vlc_mutex_unlock(&hls->lock); + vlc_mutex_unlock(&segment->lock); + } + else + { + int last = vlc_array_count(hls_old->segments) - 1; + segment_t *l = segment_GetSegment(hls_old, last); + if (l == NULL) { + vlc_mutex_unlock(&hls_old->lock); return VLC_EGENERIC; } + + if ((l->sequence + 1) != p->sequence) + { + msg_Err(s, "gap in sequence numbers found: new=%d expected %d", + p->sequence, l->sequence+1); + } + vlc_array_append(hls_old->segments, p); + msg_Dbg(s, "- segment %d appended", p->sequence); + hls_old->max_segment_length = __MAX(hls_old->max_segment_length, p->duration); + msg_Dbg(s, " - segments new max duration %d", hls_old->max_segment_length); + + // Signal download thread otherwise the segment will not get downloaded + *stream_appended = true; } + } - /* Stream size (approximate) */ - hls->size = hls_GetStreamSize(hls); + /* update meta information */ + hls_old->sequence = hls_new->sequence; + hls_old->duration = (hls_new->duration == -1) ? hls_old->duration : hls_new->duration; + hls_old->b_cache = hls_new->b_cache; + vlc_mutex_unlock(&hls_old->lock); + return VLC_SUCCESS; - /* Can we cache files after playback */ - p_sys->b_cache = hls->b_cache; +} - vlc_mutex_unlock(&hls->lock); +static int hls_ReloadPlaylist(stream_t *s) +{ + stream_sys_t *p_sys = s->p_sys; + + // Flag to indicate if we should signal download thread + bool stream_appended = false; + + vlc_array_t *hls_streams = vlc_array_new(); + if (hls_streams == NULL) + return VLC_ENOMEM; + + msg_Dbg(s, "Reloading HLS live meta playlist"); + + if (get_HTTPLiveMetaPlaylist(s, &hls_streams) != VLC_SUCCESS) + { + /* Free hls streams */ + for (int i = 0; i < vlc_array_count(hls_streams); i++) + { + hls_stream_t *hls; + hls = hls_Get(hls_streams, i); + if (hls) hls_Free(hls); + } + vlc_array_destroy(hls_streams); + + msg_Err(s, "reloading playlist failed"); + return VLC_EGENERIC; + } + + /* merge playlists */ + int count = vlc_array_count(hls_streams); + for (int n = 0; n < count; n++) + { + hls_stream_t *hls_new = hls_Get(hls_streams, n); + if (hls_new == NULL) + continue; + + hls_stream_t *hls_old = hls_Find(p_sys->hls_stream, hls_new); + if (hls_old == NULL) + { /* new hls stream - append */ + vlc_array_append(p_sys->hls_stream, hls_new); + msg_Dbg(s, "new HLS stream appended (id=%d, bandwidth=%"PRIu64")", + hls_new->id, hls_new->bandwidth); + + // New segment available - signal download thread + stream_appended = true; + } + else if (hls_UpdatePlaylist(s, hls_new, hls_old, &stream_appended) != VLC_SUCCESS) + msg_Warn(s, "failed updating HLS stream (id=%d, bandwidth=%"PRIu64")", + hls_new->id, hls_new->bandwidth); + } + vlc_array_destroy(hls_streams); + + // Must signal the download thread otherwise new segments will not be downloaded at all! + if (stream_appended == true) + { + vlc_mutex_lock(&p_sys->download.lock_wait); + vlc_cond_signal(&p_sys->download.wait); + vlc_mutex_unlock(&p_sys->download.lock_wait); } return VLC_SUCCESS; @@ -863,8 +1565,10 @@ static int BandwidthAdaptation(stream_t *s, int progid, uint64_t *bandwidth) return candidate; } -static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur_stream) +static int hls_DownloadSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur_stream) { + stream_sys_t *p_sys = s->p_sys; + assert(hls); assert(segment); @@ -876,34 +1580,55 @@ static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur return VLC_SUCCESS; } + /* sanity check - can we download this segment on time? */ + if ((p_sys->bandwidth > 0) && (hls->bandwidth > 0)) + { + uint64_t size = (segment->duration * hls->bandwidth); /* bits */ + int estimated = (int)(size / p_sys->bandwidth); + if (estimated > segment->duration) + { + msg_Warn(s,"downloading segment %d predicted to take %ds, which exceeds its length (%ds)", + segment->sequence, estimated, segment->duration); + } + } + mtime_t start = mdate(); - if (AccessDownload(s, segment) != VLC_SUCCESS) + if (hls_Download(s, segment) != VLC_SUCCESS) { + msg_Err(s, "downloading segment %d from stream %d failed", + segment->sequence, *cur_stream); vlc_mutex_unlock(&segment->lock); return VLC_EGENERIC; } mtime_t duration = mdate() - start; + if (hls->bandwidth == 0 && segment->duration > 0) + { + /* Try to estimate the bandwidth for this stream */ + hls->bandwidth = (uint64_t)(((double)segment->size * 8) / ((double)segment->duration)); + } + + /* If the segment is encrypted, decode it */ + if (hls_DecodeSegmentData(s, hls, segment) != VLC_SUCCESS) + { + vlc_mutex_unlock(&segment->lock); + return VLC_EGENERIC; + } vlc_mutex_unlock(&segment->lock); - msg_Info(s, "downloaded segment %d from stream %d", + msg_Dbg(s, "downloaded segment %d from stream %d", segment->sequence, *cur_stream); - /* check for division by zero */ - double ms = (double)duration / 1000.0; /* ms */ - if (ms <= 0.0) - return VLC_SUCCESS; - - uint64_t bw = ((double)(segment->size * 8) / ms) * 1000; /* bits / s */ - segment->bandwidth = bw; - if (hls->bandwidth != bw) + uint64_t bw = segment->size * 8 * 1000000 / __MAX(1, duration); /* bits / s */ + p_sys->bandwidth = bw; + if (p_sys->b_meta && (hls->bandwidth != bw)) { int newstream = BandwidthAdaptation(s, hls->id, &bw); /* FIXME: we need an average here */ if ((newstream >= 0) && (newstream != *cur_stream)) { - msg_Info(s, "detected %s bandwidth (%"PRIu64") stream", + msg_Dbg(s, "detected %s bandwidth (%"PRIu64") stream", (bw >= hls->bandwidth) ? "faster" : "lower", bw); *cur_stream = newstream; } @@ -911,76 +1636,141 @@ static int Download(stream_t *s, hls_stream_t *hls, segment_t *segment, int *cur return VLC_SUCCESS; } -static void* hls_Thread(vlc_object_t *p_this) +static void* hls_Thread(void *p_this) { - hls_thread_t *client = (hls_thread_t *) p_this; - stream_t *s = client->s; + stream_t *s = (stream_t *)p_this; stream_sys_t *p_sys = s->p_sys; int canc = vlc_savecancel(); - while (vlc_object_alive(p_this)) + while (vlc_object_alive(s)) { - hls_stream_t *hls = hls_Get(client->hls_stream, client->current); + hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream); assert(hls); + /* Sliding window (~60 seconds worth of movie) */ vlc_mutex_lock(&hls->lock); - segment_t *segment = segment_GetSegment(hls, client->segment); + int count = vlc_array_count(hls->segments); vlc_mutex_unlock(&hls->lock); /* Is there a new segment to process? */ - if (segment == NULL) + if ((!p_sys->b_live && (p_sys->playback.segment < (count - 6))) || + (p_sys->download.segment >= count)) { - if (!p_sys->b_live) + /* wait */ + vlc_mutex_lock(&p_sys->download.lock_wait); + while (((p_sys->download.segment - p_sys->playback.segment > 6) || + (p_sys->download.segment >= count)) && + (p_sys->download.seek == -1)) { - p_sys->last = mdate(); - p_sys->wakeup = p_sys->last + (2 * (mtime_t)1000000); + vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait); + if (p_sys->b_live /*&& (mdate() >= p_sys->playlist.wakeup)*/) + break; + if (!vlc_object_alive(s)) + break; } - mwait(p_sys->wakeup); - - /* reset download segment to current playback segment */ - client->segment = p_sys->segment + 1; + /* */ + if (p_sys->download.seek >= 0) + { + p_sys->download.segment = p_sys->download.seek; + p_sys->download.seek = -1; + } + vlc_mutex_unlock(&p_sys->download.lock_wait); } - else if (Download(client->s, hls, segment, &client->current) != VLC_SUCCESS) + + if (!vlc_object_alive(s)) break; + + vlc_mutex_lock(&hls->lock); + segment_t *segment = segment_GetSegment(hls, p_sys->download.segment); + vlc_mutex_unlock(&hls->lock); + + if ((segment != NULL) && + (hls_DownloadSegmentData(s, hls, segment, &p_sys->download.stream) != VLC_SUCCESS)) { - if (!p_sys->b_live) break; + if (!vlc_object_alive(s)) break; + + if (!p_sys->b_live) + { + p_sys->b_error = true; + break; + } } /* download succeeded */ /* determine next segment to download */ - vlc_mutex_lock(&client->lock_wait); - if (client->seek >= 0) + vlc_mutex_lock(&p_sys->download.lock_wait); + if (p_sys->download.seek >= 0) { - client->segment = client->seek; - client->seek = -1; + p_sys->download.segment = p_sys->download.seek; + p_sys->download.seek = -1; } - else client->segment++; - vlc_cond_signal(&client->wait); - vlc_mutex_unlock(&client->lock_wait); + else if (p_sys->download.segment < count) + p_sys->download.segment++; + vlc_cond_signal(&p_sys->download.wait); + vlc_mutex_unlock(&p_sys->download.lock_wait); + + // In case of a successful download signal the read thread that data is available + vlc_mutex_lock(&p_sys->read.lock_wait); + vlc_cond_signal(&p_sys->read.wait); + vlc_mutex_unlock(&p_sys->read.lock_wait); + } - /* FIXME: Reread the m3u8 index file */ - if (p_sys->b_live) + vlc_restorecancel(canc); + return NULL; +} + +static void* hls_Reload(void *p_this) +{ + stream_t *s = (stream_t *)p_this; + stream_sys_t *p_sys = s->p_sys; + + assert(p_sys->b_live); + + int canc = vlc_savecancel(); + + double wait = 1.0; + while (vlc_object_alive(s)) + { + mtime_t now = mdate(); + if (now >= p_sys->playlist.wakeup) { - double wait = 1; - mtime_t now = mdate(); - if (now >= p_sys->wakeup) + /* reload the m3u8 if there are less than 3 segments what aren't downloaded */ + if ( ( p_sys->download.segment - p_sys->playback.segment < 3 ) && + ( hls_ReloadPlaylist(s) != VLC_SUCCESS) ) { -#if 0 - /** FIXME: Implement m3u8 playlist reloading */ - if (!hls_ReloadPlaylist(client->s)) + /* No change in playlist, then backoff */ + p_sys->playlist.tries++; + if (p_sys->playlist.tries == 1) wait = 0.5; + else if (p_sys->playlist.tries == 2) wait = 1; + else if (p_sys->playlist.tries >= 3) wait = 1.5; + + /* Can we afford to backoff? */ + if (p_sys->download.segment - p_sys->playback.segment < 3) { - /* No change in playlist, then backoff */ - p_sys->tries++; - if (p_sys->tries == 1) wait = 0.5; - else if (p_sys->tries == 2) wait = 1; - else if (p_sys->tries >= 3) wait = 3; + p_sys->playlist.tries = 0; + wait = 0.5; } -#endif - /* determine next time to update playlist */ - p_sys->last = now; - p_sys->wakeup = now + ((mtime_t)(hls->duration * wait) * (mtime_t)1000000); } + else + { + p_sys->playlist.tries = 0; + wait = 1.0; + } + + hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->download.stream); + assert(hls); + + /* determine next time to update playlist */ + p_sys->playlist.last = now; + p_sys->playlist.wakeup = now; + /* If there is no new segments,use playlist duration as sleep period base */ + if( likely( hls->max_segment_length > 0 ) ) + p_sys->playlist.wakeup += (mtime_t)((hls->max_segment_length * wait) * CLOCK_FREQ); + else + p_sys->playlist.wakeup += (mtime_t)((hls->duration * wait) * CLOCK_FREQ); } + + mwait(p_sys->playlist.wakeup); } vlc_restorecancel(canc); @@ -990,203 +1780,239 @@ static void* hls_Thread(vlc_object_t *p_this) static int Prefetch(stream_t *s, int *current) { stream_sys_t *p_sys = s->p_sys; - int stream; + int stream = *current; - /* Try to pick best matching stream */ -again: - stream = *current; - - hls_stream_t *hls = hls_Get(p_sys->hls_stream, *current); + hls_stream_t *hls = hls_Get(p_sys->hls_stream, stream); if (hls == NULL) return VLC_EGENERIC; - segment_t *segment = segment_GetSegment(hls, p_sys->segment); - if (segment == NULL ) - return VLC_EGENERIC; - - if (Download(s, hls, segment, current) != VLC_SUCCESS) + if (vlc_array_count(hls->segments) == 0) return VLC_EGENERIC; + else if (vlc_array_count(hls->segments) == 1 && p_sys->b_live) + msg_Warn(s, "Only 1 segment available to prefetch in live stream; may stall"); - /* Found better bandwidth match, try again */ - if (*current != stream) - goto again; - - /* Download first 2 segments of this HLS stream */ - for (int i = 0; i < 2; i++) + /* Download ~10s worth of segments of this HLS stream if they exist */ + unsigned segment_amount = (0.5f + 10/hls->duration); + for (int i = 0; i < __MIN(vlc_array_count(hls->segments), segment_amount); i++) { - segment_t *segment = segment_GetSegment(hls, p_sys->segment); + segment_t *segment = segment_GetSegment(hls, p_sys->download.segment); if (segment == NULL ) return VLC_EGENERIC; + /* It is useless to lock the segment here, as Prefetch is called before + download and playlit thread are started. */ if (segment->data) { - p_sys->segment++; + p_sys->download.segment++; continue; } - if (Download(s, hls, segment, current) != VLC_SUCCESS) + if (hls_DownloadSegmentData(s, hls, segment, current) != VLC_SUCCESS) return VLC_EGENERIC; - p_sys->segment++; + p_sys->download.segment++; + + /* adapt bandwidth? */ + if (*current != stream) + { + hls_stream_t *hls = hls_Get(p_sys->hls_stream, *current); + if (hls == NULL) + return VLC_EGENERIC; + + stream = *current; + } } return VLC_SUCCESS; } /**************************************************************************** - * Access + * ****************************************************************************/ -static int AccessOpen(stream_t *s, vlc_url_t *url) +static int hls_Download(stream_t *s, segment_t *segment) { - stream_sys_t *p_sys = (stream_sys_t *) s->p_sys; + stream_sys_t *p_sys = s->p_sys; + assert(segment); - if ((url->psz_protocol == NULL) || - (url->psz_path == NULL)) + vlc_mutex_lock(&p_sys->lock); + while (p_sys->paused) + vlc_cond_wait(&p_sys->wait, &p_sys->lock); + vlc_mutex_unlock(&p_sys->lock); + + stream_t *p_ts = stream_UrlNew(s, segment->url); + if (p_ts == NULL) return VLC_EGENERIC; - p_sys->p_access = vlc_object_create(s, sizeof(access_t)); - if (p_sys->p_access == NULL) - return VLC_ENOMEM; + segment->size = stream_Size(p_ts); + + if (segment->size == 0) { + int chunk_size = 65536; + segment->data = block_Alloc(chunk_size); + if (!segment->data) + goto nomem; + do { + if (segment->data->i_buffer - segment->size < chunk_size) { + chunk_size *= 2; + block_t *p_block = block_Realloc(segment->data, 0, segment->data->i_buffer + chunk_size); + if (!p_block) { + block_Release(segment->data); + segment->data = NULL; + goto nomem; + } + segment->data = p_block; + } - p_sys->p_access->psz_access = strdup(url->psz_protocol); - p_sys->p_access->psz_filepath = strdup(url->psz_path); - if (url->psz_password || url->psz_username) - { - if (asprintf(&p_sys->p_access->psz_location, "%s:%s@%s%s", - url->psz_username, url->psz_password, - url->psz_host, url->psz_path) < 0) - { - msg_Err(s, "creating http access module"); - goto fail; - } + ssize_t length = stream_Read(p_ts, segment->data->p_buffer + segment->size, chunk_size); + if (length <= 0) { + segment->data->i_buffer = segment->size; + break; + } + segment->size += length; + } while (vlc_object_alive(s)); + + stream_Delete(p_ts); + return VLC_SUCCESS; } - else + + segment->data = block_Alloc(segment->size); + if (segment->data == NULL) + goto nomem; + + assert(segment->data->i_buffer == segment->size); + + ssize_t curlen = 0; + do { - if (asprintf(&p_sys->p_access->psz_location, "%s%s", - url->psz_host, url->psz_path) < 0) + /* NOTE: Beware the size reported for a segment by the HLS server may not + * be correct, when downloading the segment data. Therefore check the size + * and enlarge the segment data block if necessary. + */ + uint64_t size = stream_Size(p_ts); + if (size > segment->size) { - msg_Err(s, "creating http access module"); - goto fail; + msg_Dbg(s, "size changed %"PRIu64, segment->size); + block_t *p_block = block_Realloc(segment->data, 0, size); + if (p_block == NULL) + { + block_Release(segment->data); + segment->data = NULL; + goto nomem; + } + segment->data = p_block; + segment->size = size; + assert(segment->data->i_buffer == segment->size); + p_block = NULL; } - } - vlc_object_attach(p_sys->p_access, s); - p_sys->p_access->p_module = - module_need(p_sys->p_access, "access", "http", true); - if (p_sys->p_access->p_module == NULL) - { - msg_Err(s, "could not load http access module"); - goto fail; - } + ssize_t length = stream_Read(p_ts, segment->data->p_buffer + curlen, segment->size - curlen); + if (length <= 0) + break; + curlen += length; + } while (vlc_object_alive(s)); + stream_Delete(p_ts); return VLC_SUCCESS; -fail: - vlc_object_release(p_sys->p_access); - p_sys->p_access = NULL; - return VLC_EGENERIC; +nomem: + stream_Delete(p_ts); + return VLC_ENOMEM; } -static void AccessClose(stream_t *s) +/* Read M3U8 file */ +static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer) { - stream_sys_t *p_sys = (stream_sys_t *) s->p_sys; + int64_t total_bytes = 0; + int64_t total_allocated = 0; + uint8_t *p = NULL; - if (p_sys->p_access) + while (1) { - vlc_object_kill(p_sys->p_access); - free(p_sys->p_access->psz_access); - if (p_sys->p_access->p_module) - module_unneed(p_sys->p_access, - p_sys->p_access->p_module); - - vlc_object_release(p_sys->p_access); - p_sys->p_access = NULL; - } -} - -static char *AccessReadLine(access_t *p_access, uint8_t *psz_tmp, size_t i_len) -{ - char *line = NULL; - char *begin = (char *)psz_tmp; - - assert(psz_tmp); + char buf[4096]; + int64_t bytes; - int skip = strlen(begin); - ssize_t len = p_access->pf_read(p_access, psz_tmp + skip, i_len - skip); - if (len < 0) return NULL; - if ((len == 0) && (skip == 0)) - return NULL; + bytes = stream_Read(s, buf, sizeof(buf)); + if (bytes == 0) + break; /* EOF ? */ + else if (bytes < 0) + { + free (p); + return bytes; + } - char *p = begin; - char *end = p + len + skip; + if ( (total_bytes + bytes + 1) > total_allocated ) + { + if (total_allocated) + total_allocated *= 2; + else + total_allocated = __MIN((uint64_t)bytes+1, sizeof(buf)); - while (p < end) - { - if (*p == '\n') - break; + p = realloc_or_free(p, total_allocated); + if (p == NULL) + return VLC_ENOMEM; + } - p++; + memcpy(p+total_bytes, buf, bytes); + total_bytes += bytes; } - /* copy line excluding \n */ - line = strndup(begin, p - begin); + if (total_allocated == 0) + return VLC_EGENERIC; - p++; - if (p < end) - { - psz_tmp = memmove(begin, p, end - p); - psz_tmp[end - p] = '\0'; - } - else memset(psz_tmp, 0, i_len); + p[total_bytes] = '\0'; + *buffer = p; - return line; + return total_bytes; } -static int AccessDownload(stream_t *s, segment_t *segment) +static ssize_t read_M3U8_from_url(stream_t *s, const char* psz_url, uint8_t **buffer) { - stream_sys_t *p_sys = (stream_sys_t *) s->p_sys; + assert(*buffer == NULL); - assert(segment); - - /* Download new playlist file from server */ - if (AccessOpen(s, &segment->url) != VLC_SUCCESS) + /* Construct URL */ + stream_t *p_m3u8 = stream_UrlNew(s, psz_url); + if (p_m3u8 == NULL) return VLC_EGENERIC; - segment->size = p_sys->p_access->info.i_size; - assert(segment->size > 0); + ssize_t size = read_M3U8_from_stream(p_m3u8, buffer); + stream_Delete(p_m3u8); - segment->data = block_Alloc(segment->size); - if (segment->data == NULL) + return size; +} + +static char *ReadLine(uint8_t *buffer, uint8_t **pos, const size_t len) +{ + assert(buffer); + + char *line = NULL; + uint8_t *begin = buffer; + uint8_t *p = begin; + uint8_t *end = p + len; + + while (p < end) { - AccessClose(s); - return VLC_ENOMEM; + if ((*p == '\r') || (*p == '\n') || (*p == '\0')) + break; + p++; } - assert(segment->data->i_buffer == segment->size); + /* copy line excluding \r \n or \0 */ + line = strndup((char *)begin, p - begin); - ssize_t length = 0, curlen = 0; - do + while ((*p == '\r') || (*p == '\n') || (*p == '\0')) { - if (p_sys->p_access->info.i_size > segment->size) + if (*p == '\0') { - msg_Dbg(s, "size changed %"PRIu64, segment->size); - segment->data = block_Realloc(segment->data, 0, p_sys->p_access->info.i_size); - if (segment->data == NULL) - { - AccessClose(s); - return VLC_ENOMEM; - } - segment->size = p_sys->p_access->info.i_size; - assert(segment->data->i_buffer == segment->size); - } - length = p_sys->p_access->pf_read(p_sys->p_access, - segment->data->p_buffer + curlen, segment->size - curlen); - if ((length <= 0) || ((uint64_t)length >= segment->size)) + *pos = end; break; - curlen += length; - } while (vlc_object_alive(s)); + } + else + { + /* next pass start after \r and \n */ + p++; + *pos = p; + } + } - AccessClose(s); - return VLC_SUCCESS; + return line; } /**************************************************************************** @@ -1202,17 +2028,41 @@ static int Open(vlc_object_t *p_this) msg_Info(p_this, "HTTP Live Streaming (%s)", s->psz_path); + /* Initialize crypto bit */ + vlc_gcrypt_init(); + /* */ s->p_sys = p_sys = calloc(1, sizeof(*p_sys)); if (p_sys == NULL) return VLC_ENOMEM; + char *psz_uri = NULL; + if (asprintf(&psz_uri,"%s://%s", s->psz_access, s->psz_path) < 0) + { + free(p_sys); + return VLC_ENOMEM; + } + p_sys->m3u8 = psz_uri; + + char *new_path; + if (asprintf(&new_path, "%s.ts", s->psz_path) < 0) + { + free(p_sys->m3u8); + free(p_sys); + return VLC_ENOMEM; + } + free(s->psz_path); + s->psz_path = new_path; + + p_sys->bandwidth = 0; p_sys->b_live = true; p_sys->b_meta = false; + p_sys->b_error = false; p_sys->hls_stream = vlc_array_new(); if (p_sys->hls_stream == NULL) { + free(p_sys->m3u8); free(p_sys); return VLC_ENOMEM; } @@ -1222,53 +2072,95 @@ static int Open(vlc_object_t *p_this) s->pf_peek = Peek; s->pf_control = Control; - /* Select first segment to play */ - p_sys->last = mdate(); - if (parse_HTTPLiveStreaming(s) != VLC_SUCCESS) + p_sys->paused = false; + + vlc_cond_init(&p_sys->wait); + vlc_mutex_init(&p_sys->lock); + + /* Parse HLS m3u8 content. */ + uint8_t *buffer = NULL; + ssize_t len = read_M3U8_from_stream(s->p_source, &buffer); + if (len < 0) + goto fail; + if (parse_M3U8(s, p_sys->hls_stream, buffer, len) != VLC_SUCCESS) { + free(buffer); goto fail; } + free(buffer); + /* HLS standard doesn't provide any guaranty about streams + being sorted by bandwidth, so we sort them */ + qsort( p_sys->hls_stream->pp_elems, p_sys->hls_stream->i_count, + sizeof( hls_stream_t* ), &hls_CompareStreams ); /* Choose first HLS stream to start with */ - int current = p_sys->current = 0; - p_sys->segment = 0; + int current = p_sys->playback.stream = p_sys->hls_stream->i_count-1; + p_sys->playback.segment = p_sys->download.segment = ChooseSegment(s, current); + + /* manage encryption key if needed */ + hls_ManageSegmentKeys(s, hls_Get(p_sys->hls_stream, current)); if (Prefetch(s, ¤t) != VLC_SUCCESS) { - msg_Err(s, "fetching first segment."); + msg_Err(s, "fetching first segment failed."); goto fail; } - p_sys->thread = vlc_object_create(s, sizeof(hls_thread_t)); - if( p_sys->thread == NULL ) - { - msg_Err(s, "creating HTTP Live Streaming client thread"); - goto fail; - } + p_sys->download.stream = current; + p_sys->playback.stream = current; + p_sys->download.seek = -1; - p_sys->thread->hls_stream = p_sys->hls_stream; - p_sys->thread->current = current; - p_sys->current = current; - p_sys->thread->segment = p_sys->segment; - p_sys->thread->seek = -1; - p_sys->segment = 0; /* reset to first segment */ - p_sys->thread->s = s; + vlc_mutex_init(&p_sys->download.lock_wait); + vlc_cond_init(&p_sys->download.wait); - vlc_mutex_init(&p_sys->thread->lock_wait); - vlc_cond_init(&p_sys->thread->wait); + vlc_mutex_init(&p_sys->read.lock_wait); + vlc_cond_init(&p_sys->read.wait); - if (vlc_thread_create(p_sys->thread, "HTTP Live Streaming client", - hls_Thread, VLC_THREAD_PRIORITY_INPUT)) + /* Initialize HLS live stream */ + if (p_sys->b_live) { - goto fail; + hls_stream_t *hls = hls_Get(p_sys->hls_stream, current); + p_sys->playlist.last = mdate(); + p_sys->playlist.wakeup = p_sys->playlist.last + + ((mtime_t)hls->duration * CLOCK_FREQ ); + + if (vlc_clone(&p_sys->reload, hls_Reload, s, VLC_THREAD_PRIORITY_LOW)) + { + goto fail_thread; + } } - vlc_object_attach(p_sys->thread, s); + if (vlc_clone(&p_sys->thread, hls_Thread, s, VLC_THREAD_PRIORITY_INPUT)) + { + if (p_sys->b_live) + vlc_join(p_sys->reload, NULL); + goto fail_thread; + } return VLC_SUCCESS; +fail_thread: + vlc_mutex_destroy(&p_sys->download.lock_wait); + vlc_cond_destroy(&p_sys->download.wait); + + vlc_mutex_destroy(&p_sys->read.lock_wait); + vlc_cond_destroy(&p_sys->read.wait); + fail: - Close(p_this); + /* Free hls streams */ + for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++) + { + hls_stream_t *hls = hls_Get(p_sys->hls_stream, i); + if (hls) hls_Free(hls); + } + vlc_array_destroy(p_sys->hls_stream); + + vlc_mutex_destroy(&p_sys->lock); + vlc_cond_destroy(&p_sys->wait); + + /* */ + free(p_sys->m3u8); + free(p_sys); return VLC_EGENERIC; } @@ -1282,31 +2174,45 @@ static void Close(vlc_object_t *p_this) assert(p_sys->hls_stream); + vlc_mutex_lock(&p_sys->lock); + p_sys->paused = false; + vlc_cond_signal(&p_sys->wait); + vlc_mutex_unlock(&p_sys->lock); + /* */ - if (p_sys->thread) - { - vlc_mutex_lock(&p_sys->thread->lock_wait); - vlc_object_kill(p_sys->thread); - vlc_cond_signal(&p_sys->thread->wait); - vlc_mutex_unlock(&p_sys->thread->lock_wait); + vlc_mutex_lock(&p_sys->download.lock_wait); + /* negate the condition variable's predicate */ + p_sys->download.segment = p_sys->playback.segment = 0; + p_sys->download.seek = 0; /* better safe than sorry */ + vlc_cond_signal(&p_sys->download.wait); + vlc_mutex_unlock(&p_sys->download.lock_wait); - /* */ - vlc_thread_join(p_sys->thread); - vlc_mutex_destroy(&p_sys->thread->lock_wait); - vlc_cond_destroy(&p_sys->thread->wait); - vlc_object_release(p_sys->thread); - } + /* */ + if (p_sys->b_live) + vlc_join(p_sys->reload, NULL); + vlc_join(p_sys->thread, NULL); + vlc_mutex_destroy(&p_sys->download.lock_wait); + vlc_cond_destroy(&p_sys->download.wait); + + vlc_mutex_destroy(&p_sys->read.lock_wait); + vlc_cond_destroy(&p_sys->read.wait); /* Free hls streams */ for (int i = 0; i < vlc_array_count(p_sys->hls_stream); i++) { - hls_stream_t *hls; - hls = (hls_stream_t *)vlc_array_item_at_index(p_sys->hls_stream, i); + hls_stream_t *hls = hls_Get(p_sys->hls_stream, i); if (hls) hls_Free(hls); } vlc_array_destroy(p_sys->hls_stream); /* */ + + vlc_mutex_destroy(&p_sys->lock); + vlc_cond_destroy(&p_sys->wait); + + free(p_sys->m3u8); + if (p_sys->peeked) + block_Release (p_sys->peeked); free(p_sys); } @@ -1319,27 +2225,30 @@ static segment_t *GetSegment(stream_t *s) segment_t *segment = NULL; /* Is this segment of the current HLS stream ready? */ - hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current); + hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream); if (hls != NULL) { vlc_mutex_lock(&hls->lock); - segment = segment_GetSegment(hls, p_sys->segment); + segment = segment_GetSegment(hls, p_sys->playback.segment); if (segment != NULL) { + vlc_mutex_lock(&segment->lock); /* This segment is ready? */ if (segment->data != NULL) { + vlc_mutex_unlock(&segment->lock); + p_sys->b_cache = hls->b_cache; vlc_mutex_unlock(&hls->lock); - return segment; + goto check; } + vlc_mutex_unlock(&segment->lock); } vlc_mutex_unlock(&hls->lock); } /* Was the HLS stream changed to another bitrate? */ - int i_stream = 0; segment = NULL; - while(vlc_object_alive(s)) + for (int i_stream = 0; i_stream < vlc_array_count(p_sys->hls_stream); i_stream++) { /* Is the next segment ready */ hls_stream_t *hls = hls_Get(p_sys->hls_stream, i_stream); @@ -1347,43 +2256,75 @@ static segment_t *GetSegment(stream_t *s) return NULL; vlc_mutex_lock(&hls->lock); - segment = segment_GetSegment(hls, p_sys->segment); + segment = segment_GetSegment(hls, p_sys->playback.segment); if (segment == NULL) { vlc_mutex_unlock(&hls->lock); break; } - vlc_mutex_lock(&p_sys->thread->lock_wait); - int i_segment = p_sys->thread->segment; - vlc_mutex_unlock(&p_sys->thread->lock_wait); + vlc_mutex_lock(&p_sys->download.lock_wait); + int i_segment = p_sys->download.segment; + vlc_mutex_unlock(&p_sys->download.lock_wait); + vlc_mutex_lock(&segment->lock); /* This segment is ready? */ if ((segment->data != NULL) && - (p_sys->segment < i_segment)) + (p_sys->playback.segment < i_segment)) { - p_sys->current = i_stream; + p_sys->playback.stream = i_stream; + p_sys->b_cache = hls->b_cache; + vlc_mutex_unlock(&segment->lock); vlc_mutex_unlock(&hls->lock); - return segment; + goto check; } + vlc_mutex_unlock(&segment->lock); vlc_mutex_unlock(&hls->lock); if (!p_sys->b_meta) break; - - /* Was the stream changed to another bitrate? */ - i_stream++; - if (i_stream >= vlc_array_count(p_sys->hls_stream)) - break; } /* */ return NULL; + +check: + /* sanity check */ + assert(segment->data); + if (segment->data->i_buffer == 0) + { + vlc_mutex_lock(&hls->lock); + int count = vlc_array_count(hls->segments); + vlc_mutex_unlock(&hls->lock); + + if ((p_sys->download.segment - p_sys->playback.segment == 0) && + ((count != p_sys->download.segment) || p_sys->b_live)) + msg_Err(s, "playback will stall"); + else if ((p_sys->download.segment - p_sys->playback.segment < 3) && + ((count != p_sys->download.segment) || p_sys->b_live)) + msg_Warn(s, "playback in danger of stalling"); + } + return segment; +} + +static int segment_RestorePos(segment_t *segment) +{ + if (segment->data) + { + uint64_t size = segment->size - segment->data->i_buffer; + if (size > 0) + { + segment->data->i_buffer += size; + segment->data->p_buffer -= size; + } + } + return VLC_SUCCESS; } +/* p_read might be NULL if caller wants to skip data */ static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read) { stream_sys_t *p_sys = s->p_sys; - ssize_t copied = 0; + ssize_t used = 0; do { @@ -1397,28 +2338,27 @@ static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read) vlc_mutex_lock(&segment->lock); if (segment->data->i_buffer == 0) { - if (!p_sys->b_cache) + if (!p_sys->b_cache || p_sys->b_live) { block_Release(segment->data); segment->data = NULL; } else - { /* reset playback pointer to start of buffer */ - uint64_t size = segment->size - segment->data->i_buffer; - if (size > 0) - { - segment->data->i_buffer += size; - segment->data->p_buffer -= size; - } - } - p_sys->segment++; + segment_RestorePos(segment); + vlc_mutex_unlock(&segment->lock); + + /* signal download thread */ + vlc_mutex_lock(&p_sys->download.lock_wait); + p_sys->playback.segment++; + vlc_cond_signal(&p_sys->download.wait); + vlc_mutex_unlock(&p_sys->download.lock_wait); continue; } if (segment->size == segment->data->i_buffer) - msg_Info(s, "playing segment %d from stream %d", - p_sys->segment, p_sys->current); + msg_Dbg(s, "playing segment %d from stream %d", + segment->sequence, p_sys->playback.stream); ssize_t len = -1; if (i_read <= segment->data->i_buffer) @@ -1428,17 +2368,18 @@ static ssize_t hls_Read(stream_t *s, uint8_t *p_read, unsigned int i_read) if (len > 0) { - memcpy(p_read + copied, segment->data->p_buffer, len); + if (p_read) /* if NULL, then caller skips data */ + memcpy(p_read + used, segment->data->p_buffer, len); segment->data->i_buffer -= len; segment->data->p_buffer += len; - copied += len; + used += len; i_read -= len; } vlc_mutex_unlock(&segment->lock); - } while ((i_read > 0) && vlc_object_alive(s)); + } while (i_read > 0); - return copied; + return used; } static int Read(stream_t *s, void *buffer, unsigned int i_read) @@ -1448,73 +2389,161 @@ static int Read(stream_t *s, void *buffer, unsigned int i_read) assert(p_sys->hls_stream); - if (buffer == NULL) + while (length == 0) { - /* caller skips data, get big enough buffer */ - msg_Warn(s, "buffer is NULL (allocate %d)", i_read); - buffer = calloc(1, i_read); - if (buffer == NULL) - return 0; /* NO MEMORY left*/ - } + // In case an error occurred or the stream was closed return 0 + if (p_sys->b_error || !vlc_object_alive(s)) + return 0; - length = hls_Read(s, (uint8_t*) buffer, i_read); - if (length < 0) - return 0; + // Lock the mutex before trying to read to avoid a race condition with the download thread + vlc_mutex_lock(&p_sys->read.lock_wait); + + /* NOTE: buffer might be NULL if caller wants to skip data */ + length = hls_Read(s, (uint8_t*) buffer, i_read); + + // An error has occurred in hls_Read + if (length < 0) + { + vlc_mutex_unlock(&p_sys->read.lock_wait); + + return 0; + } - p_sys->offset += length; + // There is no data available yet for the demuxer so we need to wait until reload and + // download operation are over. + // Download thread will signal once download is finished. + // A timed wait is used to avoid deadlock in case data never arrives since the thread + // running this read operation is also responsible for closing the stream + if (length == 0) + { + mtime_t start = mdate(); + + // Wait for 10 seconds + mtime_t timeout_limit = start + (10 * CLOCK_FREQ); + + int res = vlc_cond_timedwait(&p_sys->read.wait, &p_sys->read.lock_wait, timeout_limit); + + // Error - reached a timeout of 10 seconds without data arriving - kill the stream + if (res == ETIMEDOUT) + { + msg_Warn(s, "timeout limit reached!"); + + vlc_mutex_unlock(&p_sys->read.lock_wait); + + return 0; + } + else if (res == EINVAL) + return 0; // Error - lock is not locked so we can just return + } + + vlc_mutex_unlock(&p_sys->read.lock_wait); + } + + p_sys->playback.offset += length; return length; } static int Peek(stream_t *s, const uint8_t **pp_peek, unsigned int i_peek) { stream_sys_t *p_sys = s->p_sys; - size_t curlen = 0; segment_t *segment; + unsigned int len = i_peek; -again: segment = GetSegment(s); if (segment == NULL) { - msg_Err(s, "segment should have been available"); + msg_Err(s, "segment %d should have been available (stream %d)", + p_sys->playback.segment, p_sys->playback.stream); return 0; /* eof? */ } vlc_mutex_lock(&segment->lock); - /* remember segment to peek */ - int peek_segment = p_sys->segment; - do + size_t i_buff = segment->data->i_buffer; + uint8_t *p_buff = segment->data->p_buffer; + + if ( likely(i_peek < i_buff)) { - if (i_peek < segment->data->i_buffer) - { - *pp_peek = segment->data->p_buffer; - curlen += i_peek; - } - else + *pp_peek = p_buff; + vlc_mutex_unlock(&segment->lock); + return i_peek; + } + + else /* This will seldom be run */ + { + /* remember segment to read */ + int peek_segment = p_sys->playback.segment; + size_t curlen = 0; + segment_t *nsegment; + p_sys->playback.segment++; + block_t *peeked = p_sys->peeked; + + if (peeked == NULL) + peeked = block_Alloc (i_peek); + else if (peeked->i_buffer < i_peek) + peeked = block_Realloc (peeked, 0, i_peek); + if (peeked == NULL) { - p_sys->segment++; vlc_mutex_unlock(&segment->lock); - goto again; + return 0; } - } while ((curlen < i_peek) && vlc_object_alive(s)); + p_sys->peeked = peeked; - /* restore segment to read */ - p_sys->segment = peek_segment; + memcpy(peeked->p_buffer, p_buff, i_buff); + curlen = i_buff; + len -= i_buff; + vlc_mutex_unlock(&segment->lock); - vlc_mutex_unlock(&segment->lock); + i_buff = peeked->i_buffer; + p_buff = peeked->p_buffer; + *pp_peek = p_buff; - return curlen; + while (curlen < i_peek) + { + nsegment = GetSegment(s); + if (nsegment == NULL) + { + msg_Err(s, "segment %d should have been available (stream %d)", + p_sys->playback.segment, p_sys->playback.stream); + /* restore segment to read */ + p_sys->playback.segment = peek_segment; + return curlen; /* eof? */ + } + + vlc_mutex_lock(&nsegment->lock); + + if (len < nsegment->data->i_buffer) + { + memcpy(p_buff + curlen, nsegment->data->p_buffer, len); + curlen += len; + } + else + { + size_t i_nbuff = nsegment->data->i_buffer; + memcpy(p_buff + curlen, nsegment->data->p_buffer, i_nbuff); + curlen += i_nbuff; + len -= i_nbuff; + + p_sys->playback.segment++; + } + + vlc_mutex_unlock(&nsegment->lock); + } + + /* restore segment to read */ + p_sys->playback.segment = peek_segment; + return curlen; + } } static bool hls_MaySeek(stream_t *s) { stream_sys_t *p_sys = s->p_sys; - if ((p_sys->hls_stream == NULL) || - (p_sys->thread == NULL)) + if (p_sys->hls_stream == NULL) return false; - hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current); + hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream); if (hls == NULL) return false; if (p_sys->b_live) @@ -1523,9 +2552,9 @@ static bool hls_MaySeek(stream_t *s) int count = vlc_array_count(hls->segments); vlc_mutex_unlock(&hls->lock); - vlc_mutex_lock(&p_sys->thread->lock_wait); - bool may_seek = (p_sys->thread->segment < (count - 2)); - vlc_mutex_unlock(&p_sys->thread->lock_wait); + vlc_mutex_lock(&p_sys->download.lock_wait); + bool may_seek = (p_sys->download.segment < (count - 2)); + vlc_mutex_unlock(&p_sys->download.lock_wait); return may_seek; } return true; @@ -1538,21 +2567,23 @@ static uint64_t GetStreamSize(stream_t *s) if (p_sys->b_live) return 0; - hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current); + hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream); if (hls == NULL) return 0; vlc_mutex_lock(&hls->lock); + if (hls->size == 0) + hls->size = hls_GetStreamSize(hls); uint64_t size = hls->size; vlc_mutex_unlock(&hls->lock); return size; } -static int segment_Seek(stream_t *s, uint64_t pos) +static int segment_Seek(stream_t *s, const uint64_t pos) { stream_sys_t *p_sys = s->p_sys; - hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->current); + hls_stream_t *hls = hls_Get(p_sys->hls_stream, p_sys->playback.stream); if (hls == NULL) return VLC_EGENERIC; @@ -1563,9 +2594,16 @@ static int segment_Seek(stream_t *s, uint64_t pos) uint64_t size = hls->size; int count = vlc_array_count(hls->segments); + segment_t *currentSegment = segment_GetSegment(hls, p_sys->playback.segment); + if (currentSegment == NULL) + { + vlc_mutex_unlock(&hls->lock); + return VLC_EGENERIC; + } + for (int n = 0; n < count; n++) { - segment_t *segment = vlc_array_item_at_index(hls->segments, n); + segment_t *segment = segment_GetSegment(hls, n); if (segment == NULL) { vlc_mutex_unlock(&hls->lock); @@ -1576,11 +2614,11 @@ static int segment_Seek(stream_t *s, uint64_t pos) length += segment->duration * (hls->bandwidth/8); vlc_mutex_unlock(&segment->lock); - if (!b_found && (pos <= length)) + if (pos <= length) { if (count - n >= 3) { - p_sys->segment = n; + p_sys->playback.segment = n; b_found = true; break; } @@ -1593,15 +2631,21 @@ static int segment_Seek(stream_t *s, uint64_t pos) /* */ if (!b_found && (pos >= size)) { - p_sys->segment = count - 1; + p_sys->playback.segment = count - 1; b_found = true; } /* */ if (b_found) { - /* restore segment to start position */ - segment_t *segment = segment_GetSegment(hls, p_sys->segment); + + /* restore current segment to start position */ + vlc_mutex_lock(¤tSegment->lock); + segment_RestorePos(currentSegment); + vlc_mutex_unlock(¤tSegment->lock); + + /* restore seeked segment to start position */ + segment_t *segment = segment_GetSegment(hls, p_sys->playback.segment); if (segment == NULL) { vlc_mutex_unlock(&hls->lock); @@ -1609,40 +2653,29 @@ static int segment_Seek(stream_t *s, uint64_t pos) } vlc_mutex_lock(&segment->lock); - if (segment->data) - { - uint64_t size = segment->size -segment->data->i_buffer; - if (size > 0) - { - segment->data->i_buffer += size; - segment->data->p_buffer -= size; - } - } + segment_RestorePos(segment); vlc_mutex_unlock(&segment->lock); /* start download at current playback segment */ - if (p_sys->thread) - { - vlc_mutex_unlock(&hls->lock); + vlc_mutex_unlock(&hls->lock); - /* Wait for download to be finished */ - vlc_mutex_lock(&p_sys->thread->lock_wait); - p_sys->thread->seek = p_sys->segment; - msg_Info(s, "seek to segment %d", p_sys->segment); - while ((p_sys->thread->seek != -1) || - (p_sys->thread->segment - p_sys->segment < 3)) - { - /* FIXME: This never finishes when the download thread is - * at its end and the user searches to a segment within the - * last three segments. In that case it should just count the - * segment as being available. */ - vlc_cond_wait(&p_sys->thread->wait, &p_sys->thread->lock_wait); - if (!vlc_object_alive(s) || s->b_error) break; - } - vlc_mutex_unlock(&p_sys->thread->lock_wait); + /* Wake up download thread */ + vlc_mutex_lock(&p_sys->download.lock_wait); + p_sys->download.seek = p_sys->playback.segment; + vlc_cond_signal(&p_sys->download.wait); - return VLC_SUCCESS; + /* Wait for download to be finished */ + msg_Dbg(s, "seek to segment %d", p_sys->playback.segment); + while ((p_sys->download.seek != -1) || + ((p_sys->download.segment - p_sys->playback.segment < 3) && + (p_sys->download.segment < count))) + { + vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait); + if (!vlc_object_alive(s) || s->b_error) break; } + vlc_mutex_unlock(&p_sys->download.lock_wait); + + return VLC_SUCCESS; } vlc_mutex_unlock(&hls->lock); @@ -1656,19 +2689,35 @@ static int Control(stream_t *s, int i_query, va_list args) switch (i_query) { case STREAM_CAN_SEEK: - case STREAM_CAN_FASTSEEK: *(va_arg (args, bool *)) = hls_MaySeek(s); break; + case STREAM_CAN_CONTROL_PACE: + case STREAM_CAN_PAUSE: + *(va_arg (args, bool *)) = true; + break; + case STREAM_CAN_FASTSEEK: + *(va_arg (args, bool *)) = false; + break; case STREAM_GET_POSITION: - *(va_arg (args, uint64_t *)) = p_sys->offset; + *(va_arg (args, uint64_t *)) = p_sys->playback.offset; break; + case STREAM_SET_PAUSE_STATE: + { + bool paused = va_arg (args, unsigned); + + vlc_mutex_lock(&p_sys->lock); + p_sys->paused = paused; + vlc_cond_signal(&p_sys->wait); + vlc_mutex_unlock(&p_sys->lock); + break; + } case STREAM_SET_POSITION: if (hls_MaySeek(s)) { uint64_t pos = (uint64_t)va_arg(args, uint64_t); if (segment_Seek(s, pos) == VLC_SUCCESS) { - p_sys->offset = pos; + p_sys->playback.offset = pos; break; } } @@ -1676,6 +2725,10 @@ static int Control(stream_t *s, int i_query, va_list args) case STREAM_GET_SIZE: *(va_arg (args, uint64_t *)) = GetStreamSize(s); break; + case STREAM_GET_PTS_DELAY: + *va_arg (args, int64_t *) = INT64_C(1000) * + var_InheritInteger(s, "network-caching"); + break; default: return VLC_EGENERIC; }