#include "avformat.h"
#include "internal.h"
#include "avio_internal.h"
-#include "url.h"
#include "id3v2.h"
#define INITIAL_BUFFER_SIZE 32768
char url[MAX_URL_SIZE];
AVIOContext pb;
uint8_t* read_buffer;
- URLContext *input;
+ AVIOContext *input;
AVFormatContext *parent;
int index;
AVFormatContext *ctx;
typedef struct HLSContext {
AVClass *class;
- AVFormatContext *avfmt;
+ AVFormatContext *ctx;
int n_variants;
struct variant **variants;
int n_playlists;
av_packet_unref(&pls->pkt);
av_freep(&pls->pb.buffer);
if (pls->input)
- ffurl_close(pls->input);
+ ff_format_io_close(c->ctx, &pls->input);
if (pls->ctx) {
pls->ctx->pb = NULL;
avformat_close_input(&pls->ctx);
return 0;
}
-static int url_connect(struct playlist *pls, AVDictionary *opts, AVDictionary *opts2)
-{
- AVDictionary *tmp = NULL;
- int ret;
-
- av_dict_copy(&tmp, opts, 0);
- av_dict_copy(&tmp, opts2, 0);
-
- if (pls->parent->protocol_whitelist) {
- pls->input->protocol_whitelist = av_strdup(pls->parent->protocol_whitelist);
- if (!pls->input->protocol_whitelist) {
- av_dict_free(&tmp);
- return AVERROR(ENOMEM);
- }
- }
-
- if ((ret = ffurl_connect(pls->input, &tmp)) < 0) {
- ffurl_close(pls->input);
- pls->input = NULL;
- }
-
- av_dict_free(&tmp);
- return ret;
-}
-
static void update_options(char **dest, const char *name, void *src)
{
av_freep(dest);
- av_opt_get(src, name, 0, (uint8_t**)dest);
+ av_opt_get(src, name, AV_OPT_SEARCH_CHILDREN, (uint8_t**)dest);
if (*dest && !strlen(*dest))
av_freep(dest);
}
-static int open_url(HLSContext *c, URLContext **uc, const char *url, AVDictionary *opts)
+static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
+ AVDictionary *opts, AVDictionary *opts2)
{
+ HLSContext *c = s->priv_data;
AVDictionary *tmp = NULL;
+ const char *proto_name = NULL;
int ret;
- const char *proto_name = avio_find_protocol_name(url);
+
+ av_dict_copy(&tmp, opts, 0);
+ av_dict_copy(&tmp, opts2, 0);
+
+ if (av_strstart(url, "crypto", NULL)) {
+ if (url[6] == '+' || url[6] == ':')
+ proto_name = avio_find_protocol_name(url + 7);
+ }
+
+ if (!proto_name)
+ proto_name = avio_find_protocol_name(url);
if (!proto_name)
return AVERROR_INVALIDDATA;
return AVERROR_INVALIDDATA;
if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
;
+ else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
+ ;
else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
return AVERROR_INVALIDDATA;
- av_dict_copy(&tmp, c->avio_opts, 0);
- av_dict_copy(&tmp, opts, 0);
-
- ret = ffurl_open_whitelist(uc, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp, c->avfmt->protocol_whitelist);
- if( ret >= 0) {
+ ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
+ if (ret >= 0) {
// update cookies on http response with setcookies.
- URLContext *u = *uc;
- update_options(&c->cookies, "cookies", u->priv_data);
+ void *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb;
+ update_options(&c->cookies, "cookies", u);
av_dict_set(&opts, "cookies", c->cookies, 0);
}
av_dict_set(&opts, "headers", c->headers, 0);
av_dict_set(&opts, "http_proxy", c->http_proxy, 0);
- ret = ffio_open_whitelist(&in, url, AVIO_FLAG_READ,
- c->interrupt_callback, &opts, c->avfmt->protocol_whitelist);
+ ret = c->ctx->io_open(c->ctx, &in, url, AVIO_FLAG_READ, &opts);
av_dict_free(&opts);
if (ret < 0)
return ret;
fail:
av_free(new_url);
if (close_in)
- avio_close(in);
+ ff_format_io_close(c->ctx, &in);
return ret;
}
READ_COMPLETE,
};
-/* read from URLContext, limiting read to current segment */
static int read_from_url(struct playlist *pls, struct segment *seg,
uint8_t *buf, int buf_size,
enum ReadFromURLMode mode)
if (seg->size >= 0)
buf_size = FFMIN(buf_size, seg->size - pls->cur_seg_offset);
- if (mode == READ_COMPLETE)
- ret = ffurl_read_complete(pls->input, buf, buf_size);
- else
- ret = ffurl_read(pls->input, buf, buf_size);
+ if (mode == READ_COMPLETE) {
+ ret = avio_read(pls->input, buf, buf_size);
+ if (ret != buf_size)
+ av_log(NULL, AV_LOG_ERROR, "Could not read complete segment.\n");
+ } else
+ ret = avio_read(pls->input, buf, buf_size);
if (ret > 0)
pls->cur_seg_offset += ret;
ff_id3v2_free_extra_meta(&extra_meta);
}
-/* Intercept and handle ID3 tags between URLContext and AVIOContext */
static void intercept_id3(struct playlist *pls, uint8_t *buf,
int buf_size, int *len)
{
seg->url, seg->url_offset, pls->index);
if (seg->key_type == KEY_NONE) {
- ret = open_url(pls->parent->priv_data, &pls->input, seg->url, opts);
+ ret = open_url(pls->parent, &pls->input, seg->url, c->avio_opts, opts);
} else if (seg->key_type == KEY_AES_128) {
-// HLSContext *c = var->parent->priv_data;
+ AVDictionary *opts2 = NULL;
char iv[33], key[33], url[MAX_URL_SIZE];
if (strcmp(seg->key, pls->key_url)) {
- URLContext *uc;
- if (open_url(pls->parent->priv_data, &uc, seg->key, opts) == 0) {
- if (ffurl_read_complete(uc, pls->key, sizeof(pls->key))
- != sizeof(pls->key)) {
+ AVIOContext *pb;
+ if (open_url(pls->parent, &pb, seg->key, c->avio_opts, opts) == 0) {
+ ret = avio_read(pb, pls->key, sizeof(pls->key));
+ if (ret != sizeof(pls->key)) {
av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",
seg->key);
}
- ffurl_close(uc);
+ ff_format_io_close(pls->parent, &pb);
} else {
av_log(NULL, AV_LOG_ERROR, "Unable to open key file %s\n",
seg->key);
else
snprintf(url, sizeof(url), "crypto:%s", seg->url);
- if ((ret = ffurl_alloc(&pls->input, url, AVIO_FLAG_READ,
- &pls->parent->interrupt_callback)) < 0)
- goto cleanup;
- av_opt_set(pls->input->priv_data, "key", key, 0);
- av_opt_set(pls->input->priv_data, "iv", iv, 0);
+ av_dict_copy(&opts2, c->avio_opts, 0);
+ av_dict_set(&opts2, "key", key, 0);
+ av_dict_set(&opts2, "iv", iv, 0);
+
+ ret = open_url(pls->parent, &pls->input, url, opts2, opts);
+
+ av_dict_free(&opts2);
- if ((ret = url_connect(pls, c->avio_opts, opts)) < 0) {
+ if (ret < 0) {
goto cleanup;
}
ret = 0;
* should already be where want it to, but this allows e.g. local testing
* without a HTTP server. */
if (ret == 0 && seg->key_type == KEY_NONE && seg->url_offset) {
- int seekret = ffurl_seek(pls->input, seg->url_offset, SEEK_SET);
+ int64_t seekret = avio_seek(pls->input, seg->url_offset, SEEK_SET);
if (seekret < 0) {
av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
ret = seekret;
- ffurl_close(pls->input);
- pls->input = NULL;
+ ff_format_io_close(pls->parent, &pls->input);
}
}
if (!seg->init_section)
return 0;
- /* this will clobber playlist URLContext stuff, so this should be
- * called between segments only */
ret = open_input(c, pls, seg->init_section);
if (ret < 0) {
av_log(pls->parent, AV_LOG_WARNING,
if (seg->init_section->size >= 0)
sec_size = seg->init_section->size;
- else if ((urlsize = ffurl_size(pls->input)) >= 0)
+ else if ((urlsize = avio_size(pls->input)) >= 0)
sec_size = urlsize;
else
sec_size = max_init_section_size;
ret = read_from_url(pls, seg->init_section, pls->init_sec_buf,
pls->init_sec_buf_size, READ_COMPLETE);
- ffurl_close(pls->input);
- pls->input = NULL;
+ ff_format_io_close(pls->parent, &pls->input);
if (ret < 0)
return ret;
return ret;
}
- ffurl_close(v->input);
- v->input = NULL;
+ ff_format_io_close(v->parent, &v->input);
v->cur_seq_no++;
c->cur_seq_no = v->cur_seq_no;
return ret;
}
+static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
+ int flags, AVDictionary **opts)
+{
+ av_log(s, AV_LOG_ERROR,
+ "A HLS playlist item '%s' referred to an external file '%s'. "
+ "Opening this file was forbidden for security reasons\n",
+ s->filename, url);
+ return AVERROR(EPERM);
+}
+
static int hls_read_header(AVFormatContext *s)
{
- URLContext *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb->opaque;
+ void *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb;
HLSContext *c = s->priv_data;
int ret = 0, i, j, stream_offset = 0;
- c->avfmt = s;
+ c->ctx = s;
c->interrupt_callback = &s->interrupt_callback;
c->strict_std_compliance = s->strict_std_compliance;
c->first_timestamp = AV_NOPTS_VALUE;
c->cur_timestamp = AV_NOPTS_VALUE;
- // if the URL context is good, read important options we must broker later
- if (u && u->prot->priv_data_class) {
+ if (u) {
// get the previous user agent & set back to null if string size is zero
- update_options(&c->user_agent, "user-agent", u->priv_data);
+ update_options(&c->user_agent, "user-agent", u);
// get the previous cookies & set back to null if string size is zero
- update_options(&c->cookies, "cookies", u->priv_data);
+ update_options(&c->cookies, "cookies", u);
// get the previous headers & set back to null if string size is zero
- update_options(&c->headers, "headers", u->priv_data);
+ update_options(&c->headers, "headers", u);
// get the previous http proxt & set back to null if string size is zero
- update_options(&c->http_proxy, "http_proxy", u->priv_data);
+ update_options(&c->http_proxy, "http_proxy", u);
}
if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
goto fail;
}
pls->ctx->pb = &pls->pb;
+ pls->ctx->io_open = nested_io_open;
pls->stream_offset = stream_offset;
- if ((ret = ff_copy_whitelists(pls->ctx, s)) < 0)
+ if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
goto fail;
ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, NULL);
av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %d\n", i, pls->cur_seq_no);
} else if (first && !pls->cur_needed && pls->needed) {
if (pls->input)
- ffurl_close(pls->input);
- pls->input = NULL;
+ ff_format_io_close(pls->parent, &pls->input);
pls->needed = 0;
changed = 1;
av_log(s, AV_LOG_INFO, "No longer receiving playlist %d\n", i);
for (i = 0; i < c->n_playlists; i++) {
/* Reset reading */
struct playlist *pls = c->playlists[i];
- if (pls->input) {
- ffurl_close(pls->input);
- pls->input = NULL;
- }
+ if (pls->input)
+ ff_format_io_close(pls->parent, &pls->input);
av_packet_unref(&pls->pkt);
reset_packet(&pls->pkt);
pls->pb.eof_reached = 0;