avformat/hls: migrate to AVFormatContext->url

Signed-off-by: Marton Balint <cus@passwd.hu>
pull/277/head
Marton Balint 7 years ago
parent 4bb0409820
commit 45ec2e44be
  1. 4
      libavformat/hls.c
  2. 166
      libavformat/hlsenc.c

@ -1682,7 +1682,7 @@ static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
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);
s->url, url);
return AVERROR(EPERM);
}
@ -1820,7 +1820,7 @@ static int hls_read_header(AVFormatContext *s)
update_options(&c->http_proxy, "http_proxy", u);
}
if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
if ((ret = parse_playlist(c, s->url, NULL, s->pb)) < 0)
goto fail;
if ((ret = save_avio_options(s)) < 0)

@ -292,7 +292,7 @@ static void hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename
static void set_http_options(AVFormatContext *s, AVDictionary **options, HLSContext *c)
{
int http_base_proto = ff_is_http_proto(s->filename);
int http_base_proto = ff_is_http_proto(s->url);
if (c->method) {
av_dict_set(options, "method", c->method, 0);
@ -467,7 +467,7 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
if (hls->segment_filename) {
dirname = av_strdup(hls->segment_filename);
} else {
dirname = av_strdup(vs->avf->filename);
dirname = av_strdup(vs->avf->url);
}
if (!dirname) {
ret = AVERROR(ENOMEM);
@ -494,7 +494,7 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
av_strlcat(path, segment->filename, path_size);
}
proto = avio_find_protocol_name(s->filename);
proto = avio_find_protocol_name(s->url);
if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
av_dict_set(&options, "method", "DELETE", 0);
if ((ret = vs->avf->io_open(vs->avf, &out, path, AVIO_FLAG_WRITE, &options)) < 0)
@ -564,12 +564,12 @@ static int do_encrypt(AVFormatContext *s, VariantStream *vs)
AVIOContext *pb;
uint8_t key[KEYSIZE];
len = strlen(s->filename) + 4 + 1;
len = strlen(s->url) + 4 + 1;
hls->key_basename = av_mallocz(len);
if (!hls->key_basename)
return AVERROR(ENOMEM);
av_strlcpy(hls->key_basename, s->filename, len);
av_strlcpy(hls->key_basename, s->url, len);
av_strlcat(hls->key_basename, ".key", len);
if (hls->key_url) {
@ -699,7 +699,10 @@ static int hls_mux_init(AVFormatContext *s, VariantStream *vs)
return ret;
oc = vs->avf;
oc->filename[0] = '\0';
oc->url = av_strdup("");
if (!oc->url)
return AVERROR(ENOMEM);
oc->oformat = vs->oformat;
oc->interrupt_callback = s->interrupt_callback;
oc->max_delay = s->max_delay;
@ -808,35 +811,38 @@ static int sls_flags_filename_process(struct AVFormatContext *s, HLSContext *hls
{
if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
strlen(vs->current_segment_final_filename_fmt)) {
av_strlcpy(vs->avf->filename, vs->current_segment_final_filename_fmt, sizeof(vs->avf->filename));
char * new_url = av_strdup(vs->current_segment_final_filename_fmt);
if (!new_url) {
av_free(en);
return AVERROR(ENOMEM);
}
ff_format_set_url(vs->avf, new_url);
if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
char *filename = NULL;
if (replace_int_data_in_filename(&filename, vs->avf->filename, 's', pos + size) < 1) {
if (replace_int_data_in_filename(&filename, vs->avf->url, 's', pos + size) < 1) {
av_log(hls, AV_LOG_ERROR,
"Invalid second level segment filename template '%s', "
"you can try to remove second_level_segment_size flag\n",
filename);
vs->avf->url);
av_free(filename);
av_free(en);
return AVERROR(EINVAL);
}
av_strlcpy(vs->avf->filename, filename, sizeof(vs->avf->filename));
av_free(filename);
ff_format_set_url(vs->avf, filename);
}
if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
char *filename = NULL;
if (replace_int_data_in_filename(&filename, vs->avf->filename,
if (replace_int_data_in_filename(&filename, vs->avf->url,
't', (int64_t)round(duration * HLS_MICROSECOND_UNIT)) < 1) {
av_log(hls, AV_LOG_ERROR,
"Invalid second level segment filename template '%s', "
"you can try to remove second_level_segment_time flag\n",
filename);
vs->avf->url);
av_free(filename);
av_free(en);
return AVERROR(EINVAL);
}
av_strlcpy(vs->avf->filename, filename, sizeof(vs->avf->filename));
av_free(filename);
ff_format_set_url(vs->avf, filename);
}
}
return 0;
@ -888,7 +894,7 @@ static int sls_flag_check_duration_size(HLSContext *hls, VariantStream *vs)
static void sls_flag_file_rename(HLSContext *hls, VariantStream *vs, char *old_filename) {
if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
strlen(vs->current_segment_final_filename_fmt)) {
ff_rename(old_filename, vs->avf->filename, hls);
ff_rename(old_filename, vs->avf->url, hls);
}
}
@ -898,45 +904,42 @@ static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c, V
char *filename = NULL;
if (replace_int_data_in_filename(&filename,
#if FF_API_HLS_WRAP
oc->filename, 'd', c->wrap ? vs->sequence % c->wrap : vs->sequence) < 1) {
oc->url, 'd', c->wrap ? vs->sequence % c->wrap : vs->sequence) < 1) {
#else
oc->filename, 'd', vs->sequence) < 1) {
oc->url, 'd', vs->sequence) < 1) {
#endif
av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
"you can try to remove second_level_segment_index flag\n",
filename);
oc->url);
av_free(filename);
return AVERROR(EINVAL);
}
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
av_free(filename);
ff_format_set_url(oc, filename);
}
if (c->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) {
av_strlcpy(vs->current_segment_final_filename_fmt, oc->filename,
av_strlcpy(vs->current_segment_final_filename_fmt, oc->url,
sizeof(vs->current_segment_final_filename_fmt));
if (c->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
char *filename = NULL;
if (replace_int_data_in_filename(&filename, oc->filename, 's', 0) < 1) {
if (replace_int_data_in_filename(&filename, oc->url, 's', 0) < 1) {
av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
"you can try to remove second_level_segment_size flag\n",
filename);
oc->url);
av_free(filename);
return AVERROR(EINVAL);
}
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
av_free(filename);
ff_format_set_url(oc, filename);
}
if (c->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
char *filename = NULL;
if (replace_int_data_in_filename(&filename, oc->filename, 't', 0) < 1) {
if (replace_int_data_in_filename(&filename, oc->url, 't', 0) < 1) {
av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
"you can try to remove second_level_segment_time flag\n",
filename);
oc->url);
av_free(filename);
return AVERROR(EINVAL);
}
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
av_free(filename);
ff_format_set_url(oc, filename);
}
}
return 0;
@ -960,10 +963,10 @@ static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls,
return ret;
}
filename = av_basename(vs->avf->filename);
filename = av_basename(vs->avf->url);
if (hls->use_localtime_mkdir) {
filename = vs->avf->filename;
filename = vs->avf->url;
}
if ((find_segment_by_filename(vs->segments, filename) || find_segment_by_filename(vs->old_segments, filename))
&& !byterange_mode) {
@ -972,7 +975,7 @@ static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls,
av_strlcpy(en->filename, filename, sizeof(en->filename));
if(vs->has_subtitle)
av_strlcpy(en->sub_filename, av_basename(vs->vtt_avf->filename), sizeof(en->sub_filename));
av_strlcpy(en->sub_filename, av_basename(vs->vtt_avf->url), sizeof(en->sub_filename));
else
en->sub_filename[0] = '\0';
@ -1098,10 +1101,15 @@ static int parse_playlist(AVFormatContext *s, const char *url, VariantStream *vs
continue;
} else if (line[0]) {
if (is_segment) {
char *new_file = av_strdup(line);
if (!new_file) {
ret = AVERROR(ENOMEM);
goto fail;
}
ff_format_set_url(vs->avf, new_file);
is_segment = 0;
new_start_pos = avio_tell(vs->avf->pb);
vs->size = new_start_pos - vs->start_pos;
av_strlcpy(vs->avf->filename, line, sizeof(line));
ret = hls_append_segment(s, hls, vs, vs->duration, vs->start_pos, vs->size);
if (ret < 0)
goto fail;
@ -1126,15 +1134,19 @@ static void hls_free_segments(HLSSegment *p)
}
}
static void hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
static int hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
{
size_t len = strlen(oc->filename);
char final_filename[sizeof(oc->filename)];
size_t len = strlen(oc->url);
char *final_filename = av_strdup(oc->url);
int ret;
av_strlcpy(final_filename, oc->filename, len);
if (!final_filename)
return AVERROR(ENOMEM);
final_filename[len-4] = '\0';
ff_rename(oc->filename, final_filename, s);
oc->filename[len-4] = '\0';
ret = ff_rename(oc->url, final_filename, s);
oc->url[len-4] = '\0';
av_freep(&final_filename);
return ret;
}
static int get_relative_url(const char *master_url, const char *media_url,
@ -1322,7 +1334,7 @@ static int hls_window(AVFormatContext *s, int last, VariantStream *vs)
int ret = 0;
char temp_filename[1024];
int64_t sequence = FFMAX(hls->start_sequence, vs->sequence - vs->nb_entries);
const char *proto = avio_find_protocol_name(s->filename);
const char *proto = avio_find_protocol_name(s->url);
int use_rename = proto && !strcmp(proto, "file");
static unsigned warned_non_file;
char *key_uri = NULL;
@ -1441,11 +1453,16 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
int err = 0;
if (c->flags & HLS_SINGLE_FILE) {
av_strlcpy(oc->filename, vs->basename,
sizeof(oc->filename));
if (vs->vtt_basename)
av_strlcpy(vtt_oc->filename, vs->vtt_basename,
sizeof(vtt_oc->filename));
char *new_name = av_strdup(vs->basename);
if (!new_name)
return AVERROR(ENOMEM);
ff_format_set_url(oc, new_name);
if (vs->vtt_basename) {
new_name = av_strdup(vs->vtt_basename);
if (!new_name)
return AVERROR(ENOMEM);
ff_format_set_url(vtt_oc, new_name);
}
} else if (c->max_seg_size > 0) {
char *filename = NULL;
if (replace_int_data_in_filename(&filename,
@ -1458,15 +1475,19 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s', you can try to use -use_localtime 1 with it\n", vs->basename);
return AVERROR(EINVAL);
}
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
av_free(filename);
ff_format_set_url(oc, filename);
} else {
if (c->use_localtime) {
time_t now0;
struct tm *tm, tmpbuf;
int bufsize = strlen(vs->basename) + 1024;
char *buf = av_mallocz(bufsize);
if (!buf)
return AVERROR(ENOMEM);
time(&now0);
tm = localtime_r(&now0, &tmpbuf);
if (!strftime(oc->filename, sizeof(oc->filename), vs->basename, tm)) {
ff_format_set_url(oc, buf);
if (!strftime(oc->url, bufsize, vs->basename, tm)) {
av_log(oc, AV_LOG_ERROR, "Could not get segment filename with use_localtime\n");
return AVERROR(EINVAL);
}
@ -1478,7 +1499,7 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
if (c->use_localtime_mkdir) {
const char *dir;
char *fn_copy = av_strdup(oc->filename);
char *fn_copy = av_strdup(oc->url);
if (!fn_copy) {
return AVERROR(ENOMEM);
}
@ -1502,8 +1523,7 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s' you can try to use -use_localtime 1 with it\n", vs->basename);
return AVERROR(EINVAL);
}
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
av_free(filename);
ff_format_set_url(oc, filename);
}
if( vs->vtt_basename) {
char *filename = NULL;
@ -1517,8 +1537,7 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
av_log(vtt_oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", vs->vtt_basename);
return AVERROR(EINVAL);
}
av_strlcpy(vtt_oc->filename, filename, sizeof(vtt_oc->filename));
av_free(filename);
ff_format_set_url(vtt_oc, filename);
}
}
vs->number++;
@ -1526,7 +1545,10 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
set_http_options(s, &options, c);
if (c->flags & HLS_TEMP_FILE) {
av_strlcat(oc->filename, ".tmp", sizeof(oc->filename));
char *new_name = av_asprintf("%s.tmp", oc->url);
if (!new_name)
return AVERROR(ENOMEM);
ff_format_set_url(oc, new_name);
}
if (c->key_info_file || c->encrypt) {
@ -1554,7 +1576,7 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
if ((err = av_dict_set(&options, "encryption_iv", iv_string, 0)) < 0)
goto fail;
filename = av_asprintf("crypto:%s", oc->filename);
filename = av_asprintf("crypto:%s", oc->url);
if (!filename) {
err = AVERROR(ENOMEM);
goto fail;
@ -1565,12 +1587,12 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
if (err < 0)
return err;
} else if (c->segment_type != SEGMENT_TYPE_FMP4) {
if ((err = hlsenc_io_open(s, &oc->pb, oc->filename, &options)) < 0)
if ((err = hlsenc_io_open(s, &oc->pb, oc->url, &options)) < 0)
goto fail;
}
if (vs->vtt_basename) {
set_http_options(s, &options, c);
if ((err = hlsenc_io_open(s, &vtt_oc->pb, vtt_oc->filename, &options)) < 0)
if ((err = hlsenc_io_open(s, &vtt_oc->pb, vtt_oc->url, &options)) < 0)
goto fail;
}
av_dict_free(&options);
@ -1964,7 +1986,7 @@ static int update_master_pl_info(AVFormatContext *s) {
char *fn1= NULL, *fn2 = NULL;
int ret = 0;
fn1 = av_strdup(s->filename);
fn1 = av_strdup(s->url);
if (!fn1) {
ret = AVERROR(ENOMEM);
goto fail;
@ -2146,7 +2168,7 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
if (vs->packets_written && can_split && av_compare_ts(pkt->pts - vs->start_pts, st->time_base,
end_pts, AV_TIME_BASE_Q) >= 0) {
int64_t new_start_pos;
char *old_filename = av_strdup(vs->avf->filename);
char *old_filename = av_strdup(vs->avf->url);
int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
if (!old_filename) {
@ -2171,13 +2193,13 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
hlsenc_io_close(s, &vs->out, vs->base_output_dirname);
}
} else {
hlsenc_io_close(s, &oc->pb, oc->filename);
hlsenc_io_close(s, &oc->pb, oc->url);
}
if (vs->vtt_avf) {
hlsenc_io_close(s, &vs->vtt_avf->pb, vs->vtt_avf->filename);
hlsenc_io_close(s, &vs->vtt_avf->pb, vs->vtt_avf->url);
}
}
if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {
if ((hls->flags & HLS_TEMP_FILE) && oc->url[0]) {
if (!(hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size <= 0))
if ((vs->avf->oformat->priv_class && vs->avf->priv_data) && hls->segment_type != SEGMENT_TYPE_FMP4)
av_opt_set(vs->avf->priv_data, "mpegts_flags", "resend_headers", 0);
@ -2189,10 +2211,10 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
}
if (hls->segment_type == SEGMENT_TYPE_FMP4) {
ret = hlsenc_io_open(s, &vs->out, vs->avf->filename, NULL);
ret = hlsenc_io_open(s, &vs->out, vs->avf->url, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to open file '%s'\n",
vs->avf->filename);
vs->avf->url);
av_free(old_filename);
return ret;
}
@ -2265,16 +2287,16 @@ static int hls_write_trailer(struct AVFormatContext *s)
oc = vs->avf;
vtt_oc = vs->vtt_avf;
old_filename = av_strdup(vs->avf->filename);
old_filename = av_strdup(vs->avf->url);
if (!old_filename) {
return AVERROR(ENOMEM);
}
if ( hls->segment_type == SEGMENT_TYPE_FMP4) {
int range_length = 0;
ret = hlsenc_io_open(s, &vs->out, vs->avf->filename, NULL);
ret = hlsenc_io_open(s, &vs->out, vs->avf->url, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to open file '%s'\n", vs->avf->filename);
av_log(NULL, AV_LOG_ERROR, "Failed to open file '%s'\n", vs->avf->url);
goto failed;
}
write_styp(vs->out);
@ -2292,7 +2314,7 @@ failed:
if (hls->segment_type != SEGMENT_TYPE_FMP4)
ff_format_io_close(s, &oc->pb);
if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {
if ((hls->flags & HLS_TEMP_FILE) && oc->url[0]) {
hls_rename_temp_file(s, oc);
}
@ -2377,7 +2399,7 @@ static int hls_init(AVFormatContext *s)
goto fail;
}
ret = validate_name(hls->nb_varstreams, s->filename);
ret = validate_name(hls->nb_varstreams, s->url);
if (ret < 0)
goto fail;
@ -2432,12 +2454,12 @@ static int hls_init(AVFormatContext *s)
for (i = 0; i < hls->nb_varstreams; i++) {
vs = &hls->var_streams[i];
vs->m3u8_name = av_strdup(s->filename);
vs->m3u8_name = av_strdup(s->url);
if (!vs->m3u8_name ) {
ret = AVERROR(ENOMEM);
goto fail;
}
ret = format_name(vs->m3u8_name, strlen(s->filename) + 1, i);
ret = format_name(vs->m3u8_name, strlen(s->url) + 1, i);
if (ret < 0)
goto fail;

Loading…
Cancel
Save