Commit 1723efcc authored by Jean-Paul Saman's avatar Jean-Paul Saman

stream_filter/httplive.c: HTTP Live Streaming

Support HTTP Live Streaming as described in the IETF draft standard:
 * http://tools.ietf.org/html/draft-pantos-http-live-streaming-04#page-8
and Apple its description:
 * http://developer.apple.com/library/ios/#documentation/networkinginternet/conceptual/streamingmediaguide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008332-CH1-DontLinkElementID_29

Not implemented (yet):
- Encrypted streams (EXT-X-KEY)
- playlist reloading for live streams
parent 52b89d15
SOURCES_decomp = decomp.c
SOURCES_stream_filter_record = record.c
SOURCES_stream_filter_httplive = httplive.c
libvlc_LTLIBRARIES += \
libstream_filter_record_plugin.la \
libstream_filter_httplive_plugin.la \
$(NULL)
if !HAVE_WIN32
if !HAVE_WINCE
......
/*****************************************************************************
* httplive.c: HTTP Live Streaming stream filter
*****************************************************************************
* Copyright (C) 2010 M2X BV
* $Id$
*
* Author: Jean-Paul Saman <jpsaman _AT_ videolan _DOT_ org>
*
* 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
* (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.
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <assert.h>
#include <vlc_threads.h>
#include <vlc_arrays.h>
#include <vlc_stream.h>
#include <vlc_input.h>
#include <vlc_fs.h>
#include <vlc_network.h>
#include <vlc_url.h>
#include <vlc_modules.h>
#include <vlc_access.h>
/*****************************************************************************
* Module descriptor
*****************************************************************************/
static int Open (vlc_object_t *);
static void Close(vlc_object_t *);
vlc_module_begin()
set_category(CAT_INPUT)
set_subcategory(SUBCAT_INPUT_STREAM_FILTER)
set_description(N_("Http Live Streaming stream filter"))
set_capability("stream_filter", 20)
set_callbacks(Open, Close)
vlc_module_end()
/*****************************************************************************
*
*****************************************************************************/
typedef struct segment_s
{
int sequence; /* unique sequence number */
int length; /* segment duration (ms) */
uint64_t size; /* segment size in bytes */
vlc_url_t url;
vlc_mutex_t lock;
block_t *data; /* data */
} segment_t;
typedef struct hls_stream_s
{
int id; /* program id */
uint64_t bandwidth; /* bandwidth usage of segments (kbps)*/
int version; /* protocol version should be 1 */
int sequence; /* media sequence number */
int duration; /* maximum duration per segment (ms) */
int segment; /* current segment downloading */
vlc_array_t *segments; /* list of segments */
vlc_mutex_t lock;
vlc_url_t url; /* uri to m3u8 */
bool b_cache; /* allow caching */
} hls_stream_t;
typedef struct
{
VLC_COMMON_MEMBERS
/* */
vlc_array_t *hls_stream;/* bandwidth adaptation */
int current; /* current hls_stream */
stream_t *s;
} hls_thread_t;
struct stream_sys_t
{
access_t *p_access; /* HTTP access input */
/* */
hls_thread_t *thread;
vlc_array_t *hls_stream;/* bandwidth adaptation */
/* Playback */
uint64_t offset; /* current offset in media */
int current; /* current hls_stream */
int segment; /* current segment for playback */
/* Playlist */
mtime_t last; /* playlist last loaded */
mtime_t wakeup; /* next reload time */
int tries; /* times it was not changed */
/* state */
bool b_cache; /* can cache files */
bool b_meta; /* meta playlist */
bool b_live; /* live stream? or vod? */
bool b_error; /* parsing error */
};
/****************************************************************************
* Local prototypes
****************************************************************************/
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 void* hls_Thread(vlc_object_t *);
static int get_HTTPLivePlaylist(stream_t *s, hls_stream_t *hls);
static void segment_Free(segment_t *segment);
/****************************************************************************
*
****************************************************************************/
static bool isHTTPLiveStreaming(stream_t *s)
{
const uint8_t *peek, *peek_end;
int64_t i_size = stream_Peek(s->p_source, &peek, 46);
if (i_size < 1)
return false;
if (strncasecmp((const char*)peek, "#EXTM3U", 7) != 0)
return false;
/* 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 == '#')
{
if (strncasecmp((const char*)peek, "#EXT-X-TARGETDURATION", 21) == 0)
return true;
else if (strncasecmp((const char*)peek, "#EXT-X-STREAM-INF", 17) == 0)
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)
{
hls_stream_t *hls = (hls_stream_t *)malloc(sizeof(hls_stream_t));
if (hls == NULL) return NULL;
hls->id = id;
hls->bandwidth = bw;
hls->sequence = 0; /* default is 0 */
hls->version = 1; /* default protocol version */
hls->segment = 0;
hls->b_cache = true;
vlc_UrlParse(&hls->url, uri, 0);
hls->segments = vlc_array_new();
vlc_array_append(hls_stream, hls);
vlc_mutex_init(&hls->lock);
return hls;
}
static void hls_Free(hls_stream_t *hls)
{
vlc_mutex_destroy(&hls->lock);
if (hls->segments)
{
for (int n = 0; n < vlc_array_count(hls->segments); n++)
{
segment_t *segment = (segment_t *)vlc_array_item_at_index(hls->segments, n);
if (segment) segment_Free(segment);
}
vlc_array_destroy(hls->segments);
}
vlc_UrlClean(&hls->url);
free(hls);
hls = NULL;
}
static hls_stream_t *hls_Get(vlc_array_t *hls_stream, int wanted)
{
int count = vlc_array_count(hls_stream);
if (count <= 0)
return NULL;
if ((wanted < 0) || (wanted >= count))
return NULL;
return (hls_stream_t *) vlc_array_item_at_index(hls_stream, wanted);
}
static inline hls_stream_t *hls_GetFirst(vlc_array_t *hls_stream)
{
return (hls_stream_t*) hls_Get(hls_stream, 0);
}
static hls_stream_t *hls_GetLast(vlc_array_t *hls_stream)
{
int count = vlc_array_count(hls_stream);
if (count <= 0)
return NULL;
count--;
return (hls_stream_t *) hls_Get(hls_stream, count);
}
/* Segment */
static segment_t *segment_New(hls_stream_t* hls, int duration, char *uri)
{
segment_t *segment = (segment_t *)malloc(sizeof(segment_t));
if (segment == NULL)
return NULL;
segment->length = duration; /* seconds */
segment->size = 0; /* bytes */
segment->sequence = 0;
vlc_UrlParse(&segment->url, uri, 0);
segment->data = NULL;
vlc_array_append(hls->segments, segment);
vlc_mutex_init(&segment->lock);
return segment;
}
static void segment_Free(segment_t *segment)
{
vlc_mutex_destroy(&segment->lock);
vlc_UrlClean(&segment->url);
if (segment->data)
block_Release(segment->data);
free(segment);
segment = NULL;
}
static segment_t *segment_GetSegment(hls_stream_t *hls, int wanted)
{
assert(hls);
int count = vlc_array_count(hls->segments);
if (count <= 0)
return NULL;
if ((wanted < 0) || (wanted >= count))
return NULL;
return (segment_t *) vlc_array_item_at_index(hls->segments, wanted);
}
/* Parsing */
static char *parse_Attributes(const char *line, const char *attr)
{
char *p;
char *begin = (char *) line;
char *end = begin + strlen(line);
/* Find start of attributes */
if ((p = strchr(begin, ':' )) == NULL)
return NULL;
begin = p;
do
{
if (strncasecmp(begin, attr, strlen(attr)) == 0)
{
/* <attr>=<value>[,]* */
p = strchr(begin, ',');
begin += strlen(attr) + 1;
if (begin >= end)
return NULL;
if (p == NULL) /* last attribute */
return strndup(begin, end - begin);
/* copy till ',' */
return strndup(begin, p - begin);
}
begin++;
} while(begin < end);
return NULL;
}
static void parse_SegmentInformation(stream_t *s, hls_stream_t *hls, char *p_read, char *uri)
{
stream_sys_t *p_sys = s->p_sys;
assert(hls);
int duration;
int ret = sscanf(p_read, "#EXTINF:%d,", &duration);
if (ret != 1)
{
msg_Err(s, "expected #EXTINF:<s>,");
p_sys->b_error = true;
return;
}
vlc_mutex_lock(&hls->lock);
segment_t *segment = segment_New(hls, duration, uri);
if (segment)
segment->sequence = hls->sequence + vlc_array_count(hls->segments);
if (duration > hls->duration)
{
msg_Err(s, "EXTINF:%d duration is larger then EXT-X-TARGETDURATION:%d",
duration, hls->duration);
}
vlc_mutex_unlock(&hls->lock);
}
static void parse_TargetDuration(stream_t *s, char *p_read)
{
stream_sys_t *p_sys = s->p_sys;
int duration;
int ret = sscanf(p_read, "#EXT-X-TARGETDURATION:%d", &duration);
if (ret != 1)
{
msg_Err(s, "expected #EXT-X-TARGETDURATION:<s>");
p_sys->b_error = true;
return;
}
hls_stream_t *hls = hls_GetLast(p_sys->hls_stream);
assert(hls);
hls->duration = duration; /* seconds */
}
static void parse_StreamInformation(stream_t *s, char *p_read, char *uri)
{
stream_sys_t *p_sys = s->p_sys;
int id, bw;
char *attr;
attr = parse_Attributes(p_read, "PROGRAM-ID");
if (attr == NULL)
{
msg_Err(s, "#EXT-X-STREAM-INF: expected PROGRAM-ID=<value>");
p_sys->b_error = true;
return;
}
id = atol(attr);
free(attr);
attr = parse_Attributes(p_read, "BANDWIDTH");
if (attr == NULL)
{
msg_Err(s, "#EXT-X-STREAM-INF: expected BANDWIDTH=<value>");
p_sys->b_error = true;
return;
}
bw = atoll(attr);
free(attr);
if (bw == 0)
{
msg_Err(s, "#EXT-X-STREAM-INF: bandwidth cannot be 0");
p_sys->b_error = true;
return;
}
msg_Info(s, "bandwidth adaption detected (program-id=%d, bandwidth=%d).", id, bw);
hls_stream_t *hls = hls_New(p_sys->hls_stream, id, bw, uri);
if (hls == NULL)
p_sys->b_error = true;
}
static void parse_MediaSequence(stream_t *s, char *p_read)
{
stream_sys_t *p_sys = s->p_sys;
int sequence;
int ret = sscanf(p_read, "#EXT-X-MEDIA-SEQUENCE:%d", &sequence);
if (ret != 1)
{
msg_Err(s, "expected #EXT-X-MEDIA-SEQUENCE:<s>");
p_sys->b_error = true;
return;
}
hls_stream_t *hls = hls_GetLast(p_sys->hls_stream);
assert(hls);
if (hls->sequence > 0)
msg_Err(s, "EXT-X-MEDIA-SEQUENCE already present in playlist");
hls->sequence = sequence;
}
static void parse_Key(stream_t *s, char *p_read)
{
stream_sys_t *p_sys = s->p_sys;
/* #EXT-X-KEY:METHOD=<method>[,URI="<URI>"][,IV=<IV>] */
char *attr;
attr = parse_Attributes(p_read, "METHOD");
if (attr == NULL)
{
msg_Err(s, "#EXT-X-KEY: expected METHOD=<value>");
p_sys->b_error = true;
}
else if (strncasecmp(attr, "NONE", 4) == 0)
{
char *uri = parse_Attributes(p_read, "URI");
char *iv = parse_Attributes(p_read, "IV");
if ((iv != NULL) || (uri != NULL))
{
msg_Err(s, "#EXT-X-KEY: URI and IV not expected");
p_sys->b_error = true;
free(iv);
free(uri);
}
}
else
{
msg_Warn(s, "playback of encrypted HTTP Live media is not supported.");
p_sys->b_error = true;
}
free(attr);
}
static void parse_ProgramDateTime(stream_t *s, char *p_read)
{
msg_Dbg(s, "tag not supported: #EXT-X-PROGRAM-DATE-TIME %s", p_read);
}
static void parse_AllowCache(stream_t *s, char *p_read)
{
stream_sys_t *p_sys = s->p_sys;
char answer[4] = "\0";
int ret = sscanf(p_read, "#EXT-X-ALLOW-CACHE:%3s", answer);
if (ret != 1)
{
msg_Err(s, "#EXT-X-ALLOW-CACHE, ignoring ...");
p_sys->b_error = true;
return;
}
hls_stream_t *hls = hls_GetLast(p_sys->hls_stream);
assert(hls);
hls->b_cache = (strncmp(answer, "NO", 2) != 0);
}
static void parse_Version(stream_t *s, char *p_read)
{
stream_sys_t *p_sys = s->p_sys;
int version;
int ret = sscanf(p_read, "#EXT-X-VERSION:%d", &version);
if (ret != 1)
{
msg_Err(s, "#EXT-X-VERSION: no protocol version found, should be version 1.");
p_sys->b_error = true;
return;
}
hls_stream_t *hls = hls_GetLast(p_sys->hls_stream);
assert(hls);
/* Check version */
hls->version = version;
if (hls->version != 1)
{
msg_Err(s, "#EXT-X-VERSION should be version 1 iso %d", version);
p_sys->b_error = true;
}
}
static void parse_EndList(stream_t *s)
{
stream_sys_t *p_sys = s->p_sys;
p_sys->b_live = false;
msg_Info(s, "video on demand (vod) mode");
}
static void parse_Discontinuity(stream_t *s, char *p_read)
{
/* FIXME: Do we need to act on discontinuity ?? */
msg_Dbg(s, "#EXT-X-DISCONTINUITY %s", p_read);
}
static void parse_M3U8ExtLine(stream_t *s, char *line)
{
if (*line == '#')
{
if (strncmp(line, "#EXT-X-TARGETDURATION", 21) == 0)
parse_TargetDuration(s, line);
else if (strncmp(line, "#EXT-X-MEDIA-SEQUENCE", 22) == 0)
parse_MediaSequence(s, line);
else if (strncmp(line, "#EXT-X-KEY", 11) == 0)
parse_Key(s, line);
else if (strncmp(line, "#EXT-X-PROGRAM-DATE-TIME", 25) == 0)
parse_ProgramDateTime(s, line);
else if (strncmp(line, "#EXT-X-ALLOW-CACHE", 17) == 0)
parse_AllowCache(s, line);
else if (strncmp(line, "#EXT-X-DISCONTINUITY", 20) == 0)
parse_Discontinuity(s, line);
else if (strncmp(line, "#EXT-X-VERSION", 14) == 0)
parse_Version(s, line);
else if (strncmp(line, "#EXT-X-ENDLIST", 14) == 0)
parse_EndList(s);
}
}
#define HTTPLIVE_MAX_LINE 4096
static int get_HTTPLivePlaylist(stream_t *s, hls_stream_t *hls)
{
stream_sys_t *p_sys = s->p_sys;
/* Download new playlist file from server */
if (AccessOpen(s, &hls->url) != VLC_SUCCESS)
goto error;
/* Parse the rest of the reply */
uint8_t *tmp = calloc(1, HTTPLIVE_MAX_LINE);
if (tmp == NULL)
{
AccessClose(s);
return VLC_ENOMEM;
}
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;
}
free(line);
line = NULL;
for( ; ; )
{
line = AccessReadLine(p_sys->p_access, tmp, HTTPLIVE_MAX_LINE);
if (line == NULL)
{
msg_Dbg(s, "end of data");
break;
}
if (!vlc_object_alive(s))
goto error;
/* 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
{
parse_SegmentInformation(s, hls, line, uri);
free(uri);
}
}
else
{
parse_M3U8ExtLine(s, line);
}
/* Error during m3u8 parsing abort */
if (p_sys->b_error)
goto error;
free(line);
}
free(line);
free(tmp);
AccessClose(s);
return VLC_SUCCESS;
error:
free(line);
free(tmp);
AccessClose(s);
return VLC_EGENERIC;
}
#undef HTTPLIVE_MAX_LINE
/* 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;
assert(p_sys->hls_stream);
p_begin = p_read = stream_ReadLine(s->p_source);
if (!p_begin)
return VLC_ENOMEM;
/* */
int i_len = strlen(p_begin);
p_end = p_read + i_len;