/*****************************************************************************
* httplive.c: HTTP Live Streaming stream filter
*****************************************************************************
- * Copyright (C) 2010-2011 M2X BV
+ * Copyright (C) 2010-2012 M2X BV
* $Id$
*
* Author: Jean-Paul Saman <jpsaman _AT_ videolan _DOT_ org>
#endif
#include <limits.h>
-#include <errno.h>
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <assert.h>
+#include <errno.h>
#include <gcrypt.h>
#include <vlc_threads.h>
#include <vlc_arrays.h>
#include <vlc_stream.h>
-#include <vlc_url.h>
#include <vlc_memory.h>
#include <vlc_gcrypt.h>
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 psz_AES_key[16]; /* AES-128 */
+ uint8_t aes_key[16]; /* AES-128 */
bool b_key_loaded;
vlc_mutex_t lock;
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 */
struct stream_sys_t
{
- vlc_url_t m3u8; /* M3U8 url */
+ char *m3u8; /* M3U8 url */
vlc_thread_t reload; /* HLS m3u8 reload thread */
vlc_thread_t thread; /* HLS segment download thread */
static int Control(stream_t *, int i_query, va_list);
static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer);
-static ssize_t read_M3U8_from_url(stream_t *s, vlc_url_t *url, 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 segment_t *segment_GetSegment(hls_stream_t *hls, int wanted);
static void segment_Free(segment_t *segment);
-static char *ConstructUrl(vlc_url_t *url);
-
/****************************************************************************
*
****************************************************************************/
-static const char *const ext[] = {
- "#EXT-X-TARGETDURATION",
- "#EXT-X-MEDIA-SEQUENCE",
- "#EXT-X-KEY",
- "#EXT-X-ALLOW-CACHE",
- "#EXT-X-ENDLIST",
- "#EXT-X-STREAM-INF",
- "#EXT-X-DISCONTINUITY",
- "#EXT-X-VERSION"
-};
-
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)
- {
- if (*peek == '#')
+ while (size--)
+ {
+ 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++)
{
- for (unsigned int i = 0; i < ARRAY_SIZE(ext); i++)
- {
- char *p = strstr((const char*)peek, ext[i]);
- if (p != NULL)
- return true;
- }
+ size_t len = strlen(ext[i]);
+ if (size < len)
+ continue;
+ if (!memcmp(peek, ext[i], len))
+ return true;
}
- peek++;
- };
+ }
return false;
}
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_array_destroy(hls->segments);
}
- vlc_UrlClean(&hls->url);
+ free(hls->url);
free(hls->psz_current_key_path);
free(hls);
}
dst->b_cache = src->b_cache;
dst->psz_current_key_path = src->psz_current_key_path ?
strdup( src->psz_current_key_path ) : NULL;
- char *uri = ConstructUrl(&src->url);
- if (uri == NULL)
+ dst->url = strdup(src->url);
+ if (dst->url == NULL)
{
free(dst);
return NULL;
}
- vlc_UrlParse(&dst->url, uri, 0);
if (!b_cp_segments)
dst->segments = vlc_array_new();
vlc_mutex_init(&dst->lock);
{
/* compare */
if ((hls->id == hls_new->id) &&
- (hls->bandwidth == hls_new->bandwidth))
+ ((hls->bandwidth == hls_new->bandwidth)||(hls_new->bandwidth==0)))
return hls;
}
}
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);
{
vlc_mutex_destroy(&segment->lock);
- vlc_UrlClean(&segment->url);
+ free(segment->url);
free(segment->psz_key_path);
if (segment->data)
block_Release(segment->data);
int count = vlc_array_count(hls->segments);
int i = p_sys->b_live ? count - 1 : 0;
- while((i >= 0) && (i < count) && vlc_object_alive(s))
+ while((i >= 0) && (i < count))
{
segment_t *segment = segment_GetSegment(hls, i);
assert(segment);
if (p_sys->b_live)
i-- ;
else
- i++;
+ i++;
}
msg_Info(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
begin = p;
do
{
- if (strncasecmp(begin, attr, strlen(attr)) == 0)
+ if (strncasecmp(begin, attr, strlen(attr)) == 0
+ && begin[strlen(attr)] == '=')
{
/* <attr>=<value>[,]* */
p = strchr(begin, ',');
return NULL;
}
-static int hex2int(char c)
+static int string_to_IV(char *string_hexa, uint8_t iv[AES_BLOCK_SIZE])
{
- if (c >= '0' && c <= '9')
- return c - '0';
- if (c >= 'A' && c <= 'F')
- return c - 'A' + 10;
- if (c >= 'a' && c <= 'f')
- return c - 'a' + 10;
- return -1;
-}
-
-static int string_to_IV(const char *string_hexa, uint8_t iv[AES_BLOCK_SIZE])
-{
- const char *p = string_hexa;
- uint8_t *d = iv;
- unsigned int c;
-
- if (*p++ != '0')
+ unsigned long long iv_hi, iv_lo;
+ char *end = NULL;
+ if (*string_hexa++ != '0')
return VLC_EGENERIC;
- if (*p++ != 'x')
+ if (*string_hexa != 'x' && *string_hexa != 'X')
return VLC_EGENERIC;
- while (*p && *(p+1))
- {
- c = hex2int(*p++) << 4;
- c |= hex2int(*p++);
- *d++ = c;
- }
+ string_hexa++;
- return VLC_SUCCESS;
-}
-
-static char *relative_URI(stream_t *s, const char *uri, const vlc_url_t *url)
-{
- stream_sys_t *p_sys = s->p_sys;
- char *psz_password = NULL;
- char *psz_username = NULL;
- char *psz_protocol = NULL;
- char *psz_path = NULL;
- char *psz_host = NULL;
- char *psz_uri = NULL;
- int i_port = -1;
-
- char *p = strchr(uri, ':');
- if (p != NULL)
- return NULL;
-
- /* Determine protocol to use */
- if (url && url->psz_protocol)
- {
- psz_protocol = strdup(url->psz_protocol);
- i_port = url->i_port;
- }
- else if (p_sys->m3u8.psz_protocol)
- {
- psz_protocol = strdup(p_sys->m3u8.psz_protocol);
- i_port = p_sys->m3u8.i_port;
+ 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], NULL, 16);
+ if (end)
+ return VLC_EGENERIC;
+ string_hexa[len-16] = '\0';
+ iv_hi = strtoull(string_hexa, NULL, 16);
+ if (end)
+ return VLC_EGENERIC;
}
- /* Determine host to use */
- if (url && url->psz_host)
- psz_host = strdup(url->psz_host);
- else if (p_sys->m3u8.psz_host)
- psz_host = strdup(p_sys->m3u8.psz_host);
-
- /* Determine path to use */
- if (url && url->psz_path != NULL)
- psz_path = strdup(url->psz_path);
- else if (p_sys->m3u8.psz_path != NULL)
- psz_path = strdup(p_sys->m3u8.psz_path);
-
- if ((psz_protocol == NULL) ||
- (psz_path == NULL) ||
- (psz_host == NULL))
- goto fail;
-
- p = strrchr(psz_path, '/');
- if (p) *p = '\0';
-
- /* Determine credentials to use */
- if (url && url->psz_username)
- psz_username = strdup(url->psz_username);
- else if (p_sys->m3u8.psz_username)
- psz_username = strdup(p_sys->m3u8.psz_username);
-
- if (url && url->psz_password)
- psz_password = strdup(url->psz_password);
- else if (p_sys->m3u8.psz_password)
- psz_password = strdup(p_sys->m3u8.psz_password);
-
- /* */
- if (psz_password || psz_username)
- {
- if (asprintf(&psz_uri, "%s://%s:%s@%s:%d%s/%s",
- psz_protocol,
- psz_username ? psz_username : "",
- psz_password ? psz_password : "",
- psz_host, i_port,
- psz_path, uri) < 0)
- goto fail;
- }
- else
- {
- if (asprintf(&psz_uri, "%s://%s:%d%s/%s",
- psz_protocol, psz_host, i_port,
- psz_path, uri) < 0)
- goto fail;
+ for (int i = 8; i ; --i) {
+ iv[ i] = iv_hi & 0xff;
+ iv[8+i] = iv_lo & 0xff;
+ iv_hi >>= 8;
+ iv_lo >>= 8;
}
-fail:
- free(psz_password);
- free(psz_username);
- free(psz_protocol);
- free(psz_path);
- free(psz_host);
- return psz_uri;
+ return VLC_SUCCESS;
}
-static char *ConstructUrl(vlc_url_t *url)
+static char *relative_URI(const char *psz_url, const char *psz_path)
{
- if ((url->psz_protocol == NULL) ||
- (url->psz_path == NULL))
+ 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;
- if (url->i_port <= 0)
- {
- if (strncmp(url->psz_protocol, "https", 5) == 0)
- url->i_port = 443;
- else
- url->i_port = 80;
- }
-
- char *psz_url = NULL;
- if (url->psz_password || url->psz_username)
- {
- if (asprintf(&psz_url, "%s://%s:%s@%s:%d%s",
- url->psz_protocol,
- url->psz_username, url->psz_password,
- url->psz_host, url->i_port, url->psz_path) < 0)
- return NULL;
- }
- else
- {
- if (asprintf(&psz_url, "%s://%s:%d%s",
- url->psz_protocol,
- url->psz_host, url->i_port, url->psz_path) < 0)
- return NULL;
- }
-
- return psz_url;
+ char *path_end = strrchr(psz_url, '/');
+ if (path_end == NULL)
+ return NULL;
+ unsigned int url_length = path_end - psz_url + 1;
+ char *psz_res = malloc(url_length + strlen(psz_path) + 1);
+ strncpy(psz_res, psz_url, url_length);
+ psz_res[url_length] = 0;
+ strcat(psz_res, psz_path);
+ return psz_res;
}
static int parse_SegmentInformation(hls_stream_t *hls, char *p_read, int *duration)
return VLC_EGENERIC;
int value;
+ char *endptr;
if (hls->version < 3)
{
- value = strtol(token, NULL, 10);
- if (errno == ERANGE)
- {
- *duration = -1;
- return VLC_EGENERIC;
- }
- *duration = value;
+ errno = 0;
+ value = strtol(token, &endptr, 10);
+ if (token == endptr || errno == ERANGE)
+ {
+ *duration = -1;
+ return VLC_EGENERIC;
+ }
+ *duration = value;
}
else
{
- double d = strtod(token, (char **) NULL);
- if (errno == ERANGE)
+ errno = 0;
+ double d = strtof(token, &endptr);
+ if (token == endptr || errno == ERANGE)
{
*duration = -1;
return VLC_EGENERIC;
value = ((int)d) + 1;
else
value = ((int)d);
+ *duration = value;
}
/* Ignore the rest of the line */
-
return VLC_SUCCESS;
}
-static int parse_AddSegment(stream_t *s, hls_stream_t *hls, const int duration, const char *uri)
+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(s, uri, &hls->url);
+ char *psz_uri = relative_URI(hls->url, uri);
segment_t *segment = segment_New(hls, duration, psz_uri ? psz_uri : uri);
if (segment)
msg_Info(s, "bandwidth adaptation detected (program-id=%d, bandwidth=%"PRIu64").", id, bw);
- char *psz_uri = relative_URI(s, uri, NULL);
+ char *psz_uri = relative_URI(s->p_sys->m3u8, uri);
*hls = hls_New(*hls_stream, id, bw, psz_uri ? psz_uri : uri);
*/
if (string_to_IV(iv, hls->psz_AES_IV) == VLC_EGENERIC)
+ {
+ msg_Err(s, "IV invalid");
err = VLC_EGENERIC;
+ }
else
hls->b_iv_loaded = true;
free(value);
/* Download playlist file from server */
uint8_t *buf = NULL;
- ssize_t len = read_M3U8_from_url(s, &hls->url, &buf);
+ ssize_t len = read_M3U8_from_url(s, hls->url, &buf);
if (len < 0)
err = VLC_EGENERIC;
else
if (p_begin >= p_end)
break;
- } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
+ } while (err == VLC_SUCCESS);
}
else
else
{
/* No Meta playlist used */
- char* uri = ConstructUrl( &s->p_sys->m3u8 );
- if ( uri == NULL )
- return VLC_EGENERIC;
- hls = hls_New(streams, 0, 0, uri);
- free( uri );
+ hls = hls_New(streams, 0, 0, p_sys->m3u8);
if (hls)
{
/* Get TARGET-DURATION first */
err = parse_EndList(s, hls);
else if ((strncmp(line, "#", 1) != 0) && (*line != '\0') )
{
- err = parse_AddSegment(s, hls, segment_duration, line);
+ err = parse_AddSegment(hls, segment_duration, line);
segment_duration = -1; /* reset duration */
}
if (p_begin >= p_end)
break;
- } while ((err == VLC_SUCCESS) && vlc_object_alive(s));
+ } while (err == VLC_SUCCESS);
free(line);
}
static int hls_DownloadSegmentKey(stream_t *s, segment_t *seg)
{
- uint8_t aeskey[32]; /* AES-512 can use up to 32 bytes */
- ssize_t len;
-
stream_t *p_m3u8 = stream_UrlNew(s, seg->psz_key_path);
if (p_m3u8 == NULL)
{
return VLC_EGENERIC;
}
- len = stream_Read(p_m3u8, aeskey, sizeof(aeskey));
+ 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 (%zd)", len);
- stream_Delete(p_m3u8);
+ msg_Err(s, "The AES key loaded doesn't have the right size (%d)", len);
return VLC_EGENERIC;
}
- memcpy(seg->psz_AES_key, aeskey, AES_BLOCK_SIZE);
-
- stream_Delete(p_m3u8);
-
return VLC_SUCCESS;
}
* 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)
{
- memcpy(seg->psz_AES_key, prev_seg->psz_AES_key, AES_BLOCK_SIZE);
+ memcpy(seg->aes_key, prev_seg->aes_key, AES_BLOCK_SIZE);
seg->b_key_loaded = true;
continue;
}
}
/* Set key */
- i_gcrypt_err = gcry_cipher_setkey(aes_ctx, segment->psz_AES_key,
- sizeof(segment->psz_AES_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));
/* Download playlist file from server */
uint8_t *buf = NULL;
- ssize_t len = read_M3U8_from_url(s, &dst->url, &buf);
+ ssize_t len = read_M3U8_from_url(s, dst->url, &buf);
if (len < 0)
err = VLC_EGENERIC;
else
{
vlc_mutex_lock(&segment->lock);
- assert(p->url.psz_path);
- assert(segment->url.psz_path);
+ assert(p->url);
+ assert(segment->url);
/* they should be the same */
if ((p->sequence != segment->sequence) ||
(p->duration != segment->duration) ||
- (strcmp(p->url.psz_path, segment->url.psz_path) != 0))
+ (strcmp(p->url, segment->url) != 0))
{
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.psz_path);
- msg_Warn(s, " old=%s", segment->url.psz_path);
+ msg_Warn(s, "- file: new=%s", p->url);
+ msg_Warn(s, " old=%s", segment->url);
/* Resetting content */
- char *psz_url = ConstructUrl(&p->url);
- if (psz_url == NULL)
+ 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;
}
- segment->sequence = p->sequence;
- segment->duration = p->duration;
- vlc_UrlClean(&segment->url);
- vlc_UrlParse(&segment->url, psz_url, 0);
/* We must free the content, because if the key was not downloaded, content can't be decrypted */
if (segment->data)
{
free(segment->psz_key_path);
segment->psz_key_path = p->psz_key_path ? strdup(p->psz_key_path) : NULL;
segment_Free(p);
- free(psz_url);
}
vlc_mutex_unlock(&segment->lock);
}
return VLC_EGENERIC;
}
mtime_t duration = mdate() - start;
- if (hls->bandwidth == 0)
+ if (hls->bandwidth == 0 && segment->duration > 0)
{
/* Try to estimate the bandwidth for this stream */
- hls->bandwidth = (uint64_t)((double)segment->size / ((double)duration / 1000000.0));
+ hls->bandwidth = (uint64_t)(((double)segment->size * 8) / ((double)segment->duration));
}
/* If the segment is encrypted, decode it */
{
assert(segment);
- /* Construct URL */
- char *psz_url = ConstructUrl(&segment->url);
- if (psz_url == NULL)
- return VLC_ENOMEM;
-
- stream_t *p_ts = stream_UrlNew(s, psz_url);
- free(psz_url);
+ stream_t *p_ts = stream_UrlNew(s, segment->url);
if (p_ts == NULL)
return VLC_EGENERIC;
uint64_t size;
do
{
+ /* 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.
+ */
size = stream_Size(p_ts);
if (size > segment->size)
{
return total_bytes;
}
-static ssize_t read_M3U8_from_url(stream_t *s, vlc_url_t *url, uint8_t **buffer)
+static ssize_t read_M3U8_from_url(stream_t *s, const char* psz_url, uint8_t **buffer)
{
assert(*buffer == NULL);
/* Construct URL */
- char *psz_url = ConstructUrl(url);
- if (psz_url == NULL)
- return VLC_ENOMEM;
-
stream_t *p_m3u8 = stream_UrlNew(s, psz_url);
- free(psz_url);
if (p_m3u8 == NULL)
return VLC_EGENERIC;
while (p < end)
{
- if ((*p == '\n') || (*p == '\0'))
+ if ((*p == '\r') || (*p == '\n') || (*p == '\0'))
break;
p++;
}
- /* copy line excluding \n or \0 */
+ /* copy line excluding \r \n or \0 */
line = strndup((char *)begin, p - begin);
- if (*p == '\0')
- *pos = end;
- else
+ while ((*p == '\r') || (*p == '\n') || (*p == '\0'))
{
- /* next pass start after \n */
- p++;
- *pos = p;
+ if (*p == '\0')
+ {
+ *pos = end;
+ break;
+ }
+ else
+ {
+ /* next pass start after \r and \n */
+ p++;
+ *pos = p;
+ }
}
return line;
free(p_sys);
return VLC_ENOMEM;
}
- vlc_UrlParse(&p_sys->m3u8, psz_uri, 0);
- free(psz_uri);
+ 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->hls_stream = vlc_array_new();
if (p_sys->hls_stream == NULL)
{
- vlc_UrlClean(&p_sys->m3u8);
+ free(p_sys->m3u8);
free(p_sys);
return VLC_ENOMEM;
}
goto fail;
}
-
p_sys->download.stream = current;
p_sys->playback.stream = current;
p_sys->download.seek = -1;
vlc_array_destroy(p_sys->hls_stream);
/* */
- vlc_UrlClean(&p_sys->m3u8);
+ free(p_sys->m3u8);
free(p_sys);
return VLC_EGENERIC;
}
vlc_array_destroy(p_sys->hls_stream);
/* */
- vlc_UrlClean(&p_sys->m3u8);
+ free(p_sys->m3u8);
if (p_sys->peeked)
block_Release (p_sys->peeked);
free(p_sys);
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);
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);
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->playback.segment < i_segment))
{
p_sys->playback.stream = i_stream;
p_sys->b_cache = hls->b_cache;
+ vlc_mutex_unlock(&segment->lock);
vlc_mutex_unlock(&hls->lock);
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);
return segment;
}
-static int segment_RestorePos( segment_t *segment )
+static int segment_RestorePos(segment_t *segment)
{
- if( segment->data )
+ if (segment->data)
{
- uint64_t size = segment->size -segment->data->i_buffer;
- if( size > 0 )
+ 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
{
segment->data = NULL;
}
else
- segment_RestorePos( segment );
+ segment_RestorePos(segment);
p_sys->playback.segment++;
vlc_mutex_unlock(&segment->lock);
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)
if (p_sys->b_error)
return 0;
- if (buffer == NULL)
- {
- /* 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*/
- }
-
+ /* NOTE: buffer might be NULL if caller wants to skip data */
length = hls_Read(s, (uint8_t*) buffer, i_read);
if (length < 0)
return 0;
peeked = block_Realloc (peeked, 0, i_peek);
if (peeked == NULL)
return 0;
+ p_sys->peeked = peeked;
memcpy(peeked->p_buffer, p_buff, i_buff);
curlen = i_buff;
p_buff = peeked->p_buffer;
*pp_peek = p_buff;
- while ((curlen < i_peek) && vlc_object_alive(s))
+ while (curlen < i_peek)
{
nsegment = GetSegment(s);
if (nsegment == NULL)
return size;
}
-
static int segment_Seek(stream_t *s, const uint64_t pos)
{
stream_sys_t *p_sys = s->p_sys;
return VLC_EGENERIC;
}
vlc_mutex_lock(&segment->lock);
- segment_RestorePos( segment );
+ segment_RestorePos(segment);
vlc_mutex_unlock(&segment->lock);
for (int n = 0; n < count; n++)
length += segment->duration * (hls->bandwidth/8);
vlc_mutex_unlock(&segment->lock);
- if (!b_found && (pos <= length))
+ if (pos <= length)
{
if (count - n >= 3)
{
vlc_mutex_unlock(&hls->lock);
return VLC_EGENERIC;
}
+
vlc_mutex_lock(&segment->lock);
- segment_RestorePos( segment );
+ segment_RestorePos(segment);
vlc_mutex_unlock(&segment->lock);
/* start download at current playback segment */
vlc_mutex_lock(&p_sys->download.lock_wait);
p_sys->download.seek = p_sys->playback.segment;
vlc_cond_signal(&p_sys->download.wait);
- vlc_mutex_unlock(&p_sys->download.lock_wait);
/* Wait for download to be finished */
- vlc_mutex_lock(&p_sys->download.lock_wait);
msg_Info(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 - 6)))
+ 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;