#include "avformat.h"
#include "avio_internal.h"
+#include "avc.h"
#if CONFIG_HTTP_PROTOCOL
#include "http.h"
#endif
#define KEYSIZE 16
#define LINE_BUFFER_SIZE MAX_URL_SIZE
#define HLS_MICROSECOND_UNIT 1000000
+#define BUFSIZE (16 * 1024)
#define POSTFIX_PATTERN "_%d"
typedef struct HLSSegment {
char iv_string[KEYSIZE*2 + 1];
struct HLSSegment *next;
+ double discont_program_date_time;
} HLSSegment;
typedef enum HLSFlags {
unsigned var_stream_idx;
unsigned number;
int64_t sequence;
- ff_const59 AVOutputFormat *oformat;
- ff_const59 AVOutputFormat *vtt_oformat;
+ const AVOutputFormat *oformat;
+ const AVOutputFormat *vtt_oformat;
AVIOContext *out;
+ AVIOContext *out_single_file;
int packets_written;
int init_range_length;
uint8_t *temp_buffer;
HLSSegment *last_segment;
HLSSegment *old_segments;
+ char *basename_tmp;
char *basename;
char *vtt_basename;
char *vtt_m3u8_name;
char *fmp4_init_filename;
char *base_output_dirname;
+ int encrypt_started;
+
+ char key_file[LINE_BUFFER_SIZE + 1];
+ char key_uri[LINE_BUFFER_SIZE + 1];
+ char key_string[KEYSIZE*2 + 1];
+ char iv_string[KEYSIZE*2 + 1];
+
AVStream **streams;
char codec_attr[128];
CodecAttributeStatus attr_status;
unsigned int nb_streams;
int m3u8_created; /* status of media play-list creation */
int is_default; /* default status of audio group */
- char *language; /* audio lauguage name */
- char *agroup; /* audio group name */
- char *sgroup; /* subtitle group name */
- char *ccgroup; /* closed caption group name */
- char *baseurl;
- char *varname; // variant name
+ const char *language; /* audio lauguage name */
+ const char *agroup; /* audio group name */
+ const char *sgroup; /* subtitle group name */
+ const char *ccgroup; /* closed caption group name */
+ const char *varname; /* variant name */
} VariantStream;
typedef struct ClosedCaptionsStream {
- char *ccgroup; /* closed caption group name */
- char *instreamid; /* closed captions INSTREAM-ID */
- char *language; /* closed captions langauge */
+ const char *ccgroup; /* closed caption group name */
+ const char *instreamid; /* closed captions INSTREAM-ID */
+ const char *language; /* closed captions langauge */
} ClosedCaptionsStream;
typedef struct HLSContext {
int64_t start_sequence;
uint32_t start_sequence_source_type; // enum StartSequenceSourceType
- float time; // Set by a private option.
- float init_time; // Set by a private option.
+ int64_t time; // Set by a private option.
+ int64_t init_time; // Set by a private option.
int max_nb_segments; // Set by a private option.
int hls_delete_threshold; // Set by a private option.
#if FF_API_HLS_WRAP
int has_video_m3u8; /* has video stream m3u8 list */
} HLSContext;
+static int strftime_expand(const char *fmt, char **dest)
+{
+ int r = 1;
+ time_t now0;
+ struct tm *tm, tmpbuf;
+ char *buf;
+
+ buf = av_mallocz(MAX_URL_SIZE);
+ if (!buf)
+ return AVERROR(ENOMEM);
+
+ time(&now0);
+ tm = localtime_r(&now0, &tmpbuf);
+ r = strftime(buf, MAX_URL_SIZE, fmt, tm);
+ if (!r) {
+ av_free(buf);
+ return AVERROR(EINVAL);
+ }
+ *dest = buf;
+
+ return r;
+}
+
static int hlsenc_io_open(AVFormatContext *s, AVIOContext **pb, char *filename,
AVDictionary **options)
{
} else {
goto fail;
}
+ } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
+ uint8_t *data = st->codecpar->extradata;
+ int profile = FF_PROFILE_UNKNOWN;
+ int level = FF_LEVEL_UNKNOWN;
+
+ if (st->codecpar->profile != FF_PROFILE_UNKNOWN)
+ profile = st->codecpar->profile;
+ if (st->codecpar->level != FF_LEVEL_UNKNOWN)
+ level = st->codecpar->level;
+
+ /* check the boundary of data which from current position is small than extradata_size */
+ while (data && (data - st->codecpar->extradata + 19) < st->codecpar->extradata_size) {
+ /* get HEVC SPS NAL and seek to profile_tier_level */
+ if (!(data[0] | data[1] | data[2]) && data[3] == 1 && ((data[4] & 0x7E) == 0x42)) {
+ uint8_t *rbsp_buf;
+ int remain_size = 0;
+ int rbsp_size = 0;
+ /* skip start code + nalu header */
+ data += 6;
+ /* process by reference General NAL unit syntax */
+ remain_size = st->codecpar->extradata_size - (data - st->codecpar->extradata);
+ rbsp_buf = ff_nal_unit_extract_rbsp(data, remain_size, &rbsp_size, 0);
+ if (!rbsp_buf)
+ return;
+ if (rbsp_size < 13) {
+ av_freep(&rbsp_buf);
+ break;
+ }
+ /* skip sps_video_parameter_set_id u(4),
+ * sps_max_sub_layers_minus1 u(3),
+ * and sps_temporal_id_nesting_flag u(1) */
+ profile = rbsp_buf[1] & 0x1f;
+ /* skip 8 + 8 + 32 + 4 + 43 + 1 bit */
+ level = rbsp_buf[12];
+ av_freep(&rbsp_buf);
+ break;
+ }
+ data++;
+ }
+ if (st->codecpar->codec_tag == MKTAG('h','v','c','1') &&
+ profile != FF_PROFILE_UNKNOWN &&
+ level != FF_LEVEL_UNKNOWN) {
+ snprintf(attr, sizeof(attr), "%s.%d.4.L%d.B01", av_fourcc2str(st->codecpar->codec_tag), profile, level);
+ } else
+ goto fail;
} else if (st->codecpar->codec_id == AV_CODEC_ID_MP2) {
snprintf(attr, sizeof(attr), "mp4a.40.33");
} else if (st->codecpar->codec_id == AV_CODEC_ID_MP3) {
static int replace_str_data_in_filename(char **s, const char *filename, char placeholder, const char *datastring)
{
const char *p;
- char *new_filename;
char c;
int addchar_count;
int found_count = 0;
AVBPrint buf;
+ int ret;
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
}
if (!av_bprint_is_complete(&buf)) {
av_bprint_finalize(&buf, NULL);
- return -1;
+ return AVERROR(ENOMEM);
}
- if (av_bprint_finalize(&buf, &new_filename) < 0 || !new_filename)
- return -1;
- *s = new_filename;
+ if ((ret = av_bprint_finalize(&buf, s)) < 0)
+ return ret;
return found_count;
}
static int replace_int_data_in_filename(char **s, const char *filename, char placeholder, int64_t number)
{
const char *p;
- char *new_filename;
char c;
int nd, addchar_count;
int found_count = 0;
AVBPrint buf;
+ int ret;
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
}
if (!av_bprint_is_complete(&buf)) {
av_bprint_finalize(&buf, NULL);
- return -1;
+ return AVERROR(ENOMEM);
}
- if (av_bprint_finalize(&buf, &new_filename) < 0 || !new_filename)
- return -1;
- *s = new_filename;
+ if ((ret = av_bprint_finalize(&buf, s)) < 0)
+ return ret;
return found_count;
}
int ret = 0;
int segment_cnt = 0;
AVBPrint path;
- char *dirname = NULL;
+ const char *dirname = NULL;
char *dirname_r = NULL;
char *dirname_repl = NULL;
- char *vtt_dirname = NULL;
+ const char *vtt_dirname = NULL;
char *vtt_dirname_r = NULL;
const char *proto = NULL;
if (segment && !hls->use_localtime_mkdir) {
dirname_r = hls->segment_filename ? av_strdup(hls->segment_filename): av_strdup(vs->avf->url);
- dirname = (char*)av_dirname(dirname_r);
+ dirname = av_dirname(dirname_r);
}
/* if %v is present in the file's directory
if ((segment->sub_filename[0] != '\0')) {
vtt_dirname_r = av_strdup(vs->vtt_avf->url);
- vtt_dirname = (char*)av_dirname(vtt_dirname_r);
+ vtt_dirname = av_dirname(vtt_dirname_r);
av_bprint_clear(&path);
av_bprintf(&path, "%s%c%s", vtt_dirname, SEPARATOR,
}
-static int hls_encryption_start(AVFormatContext *s)
+static int hls_encryption_start(AVFormatContext *s, VariantStream *vs)
{
HLSContext *hls = s->priv_data;
int ret;
return ret;
}
- ff_get_line(pb, hls->key_uri, sizeof(hls->key_uri));
- hls->key_uri[strcspn(hls->key_uri, "\r\n")] = '\0';
+ ff_get_line(pb, vs->key_uri, sizeof(vs->key_uri));
+ vs->key_uri[strcspn(vs->key_uri, "\r\n")] = '\0';
- ff_get_line(pb, hls->key_file, sizeof(hls->key_file));
- hls->key_file[strcspn(hls->key_file, "\r\n")] = '\0';
+ ff_get_line(pb, vs->key_file, sizeof(vs->key_file));
+ vs->key_file[strcspn(vs->key_file, "\r\n")] = '\0';
- ff_get_line(pb, hls->iv_string, sizeof(hls->iv_string));
- hls->iv_string[strcspn(hls->iv_string, "\r\n")] = '\0';
+ ff_get_line(pb, vs->iv_string, sizeof(vs->iv_string));
+ vs->iv_string[strcspn(vs->iv_string, "\r\n")] = '\0';
ff_format_io_close(s, &pb);
- if (!*hls->key_uri) {
+ if (!*vs->key_uri) {
av_log(hls, AV_LOG_ERROR, "no key URI specified in key info file\n");
return AVERROR(EINVAL);
}
- if (!*hls->key_file) {
+ if (!*vs->key_file) {
av_log(hls, AV_LOG_ERROR, "no key file specified in key info file\n");
return AVERROR(EINVAL);
}
set_http_options(s, &options, hls);
- ret = s->io_open(s, &pb, hls->key_file, AVIO_FLAG_READ, &options);
+ ret = s->io_open(s, &pb, vs->key_file, AVIO_FLAG_READ, &options);
av_dict_free(&options);
if (ret < 0) {
- av_log(hls, AV_LOG_ERROR, "error opening key file %s\n", hls->key_file);
+ av_log(hls, AV_LOG_ERROR, "error opening key file %s\n", vs->key_file);
return ret;
}
ret = avio_read(pb, key, sizeof(key));
ff_format_io_close(s, &pb);
if (ret != sizeof(key)) {
- av_log(hls, AV_LOG_ERROR, "error reading key file %s\n", hls->key_file);
+ av_log(hls, AV_LOG_ERROR, "error reading key file %s\n", vs->key_file);
if (ret >= 0 || ret == AVERROR_EOF)
ret = AVERROR(EINVAL);
return ret;
}
- ff_data_to_hex(hls->key_string, key, sizeof(key), 0);
+ ff_data_to_hex(vs->key_string, key, sizeof(key), 0);
return 0;
}
if (!oc->url)
return AVERROR(ENOMEM);
- oc->oformat = vs->oformat;
oc->interrupt_callback = s->interrupt_callback;
oc->max_delay = s->max_delay;
oc->opaque = s->opaque;
if (ret < 0)
return ret;
vtt_oc = vs->vtt_avf;
- vtt_oc->oformat = vs->vtt_oformat;
av_dict_copy(&vtt_oc->metadata, s->metadata, 0);
}
av_dict_copy(&st->metadata, vs->streams[i]->metadata, 0);
}
- vs->packets_written = 1;
vs->start_pos = 0;
vs->new_start = 1;
}
}
- vs->packets_written = 0;
- vs->init_range_length = 0;
-
if ((ret = avio_open_dyn_buf(&oc->pb)) < 0)
return ret;
en->keyframe_size = vs->video_keyframe_size;
en->next = NULL;
en->discont = 0;
+ en->discont_program_date_time = 0;
if (vs->discontinuity) {
en->discont = 1;
}
if (hls->key_info_file || hls->encrypt) {
- av_strlcpy(en->key_uri, hls->key_uri, sizeof(en->key_uri));
- av_strlcpy(en->iv_string, hls->iv_string, sizeof(en->iv_string));
+ av_strlcpy(en->key_uri, vs->key_uri, sizeof(en->key_uri));
+ av_strlcpy(en->iv_string, vs->iv_string, sizeof(en->iv_string));
}
if (!vs->segments)
if (hls->max_nb_segments && vs->nb_entries >= hls->max_nb_segments) {
en = vs->segments;
- vs->initial_prog_date_time += en->duration;
+ if (!en->next->discont_program_date_time && !en->discont_program_date_time)
+ vs->initial_prog_date_time += en->duration;
vs->segments = en->next;
if (en && hls->flags & HLS_DELETE_SEGMENTS &&
#if FF_API_HLS_WRAP
char line[MAX_URL_SIZE];
const char *ptr;
const char *end;
+ double discont_program_date_time = 0;
if ((ret = ffio_open_whitelist(&in, url, AVIO_FLAG_READ,
&s->interrupt_callback, NULL,
ptr += strlen("URI=\"");
end = av_stristr(ptr, ",");
if (end) {
- av_strlcpy(hls->key_uri, ptr, end - ptr);
+ av_strlcpy(vs->key_uri, ptr, end - ptr);
} else {
- av_strlcpy(hls->key_uri, ptr, sizeof(hls->key_uri));
+ av_strlcpy(vs->key_uri, ptr, sizeof(vs->key_uri));
}
}
ptr += strlen("IV=0x");
end = av_stristr(ptr, ",");
if (end) {
- av_strlcpy(hls->iv_string, ptr, end - ptr);
+ av_strlcpy(vs->iv_string, ptr, end - ptr);
} else {
- av_strlcpy(hls->iv_string, ptr, sizeof(hls->iv_string));
+ av_strlcpy(vs->iv_string, ptr, sizeof(vs->iv_string));
}
}
+ } else if (av_strstart(line, "#EXT-X-PROGRAM-DATE-TIME:", &ptr)) {
+ struct tm program_date_time;
+ int y,M,d,h,m,s;
+ double ms;
+ if (sscanf(ptr, "%d-%d-%dT%d:%d:%d.%lf", &y, &M, &d, &h, &m, &s, &ms) != 7) {
+ ret = AVERROR_INVALIDDATA;
+ goto fail;
+ }
+ program_date_time.tm_year = y - 1900;
+ program_date_time.tm_mon = M - 1;
+ program_date_time.tm_mday = d;
+ program_date_time.tm_hour = h;
+ program_date_time.tm_min = m;
+ program_date_time.tm_sec = s;
+ program_date_time.tm_isdst = -1;
+
+ discont_program_date_time = mktime(&program_date_time);
+ discont_program_date_time += (double)(ms / 1000);
} else if (av_strstart(line, "#", NULL)) {
continue;
} else if (line[0]) {
is_segment = 0;
new_start_pos = avio_tell(vs->avf->pb);
vs->size = new_start_pos - vs->start_pos;
- vs->initial_prog_date_time -= vs->duration; // this is a previously existing segment
ret = hls_append_segment(s, hls, vs, vs->duration, vs->start_pos, vs->size);
+ vs->last_segment->discont_program_date_time = discont_program_date_time;
+ discont_program_date_time += vs->duration;
if (ret < 0)
goto fail;
vs->start_pos = new_start_pos;
int ret, bandwidth;
const char *m3u8_rel_name = NULL;
const char *vtt_m3u8_rel_name = NULL;
- char *ccgroup;
- char *sgroup = NULL;
+ const char *ccgroup;
+ const char *sgroup = NULL;
ClosedCaptionsStream *ccs;
const char *proto = avio_find_protocol_name(hls->master_m3u8_url);
int is_file_proto = proto && !strcmp(proto, "file");
sequence = 0;
}
+ if (hls->flags & HLS_I_FRAMES_ONLY) {
+ hls->version = 4;
+ }
+
if (hls->flags & HLS_INDEPENDENT_SEGMENTS) {
hls->version = 6;
}
ff_hls_write_playlist_header(byterange_mode ? hls->m3u8_out : vs->out, hls->version, hls->allowcache,
target_duration, sequence, hls->pl_type, hls->flags & HLS_I_FRAMES_ONLY);
- if ((hls->flags & HLS_DISCONT_START) && sequence==hls->start_sequence && vs->discontinuity_set==0 ) {
+ if ((hls->flags & HLS_DISCONT_START) && sequence==hls->start_sequence && vs->discontinuity_set==0) {
avio_printf(byterange_mode ? hls->m3u8_out : vs->out, "#EXT-X-DISCONTINUITY\n");
vs->discontinuity_set = 1;
}
ret = ff_hls_write_file_entry(byterange_mode ? hls->m3u8_out : vs->out, en->discont, byterange_mode,
en->duration, hls->flags & HLS_ROUND_DURATIONS,
- en->size, en->pos, vs->baseurl,
- en->filename, prog_date_time_p, en->keyframe_size, en->keyframe_pos, hls->flags & HLS_I_FRAMES_ONLY);
+ en->size, en->pos, hls->baseurl,
+ en->filename,
+ en->discont_program_date_time ? &en->discont_program_date_time : prog_date_time_p,
+ en->keyframe_size, en->keyframe_pos, hls->flags & HLS_I_FRAMES_ONLY);
+ if (en->discont_program_date_time)
+ en->discont_program_date_time -= en->duration;
if (ret < 0) {
av_log(s, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n");
}
for (en = vs->segments; en; en = en->next) {
ret = ff_hls_write_file_entry(hls->sub_m3u8_out, 0, byterange_mode,
en->duration, 0, en->size, en->pos,
- vs->baseurl, en->sub_filename, NULL, 0, 0, 0);
+ hls->baseurl, en->sub_filename, NULL, 0, 0, 0);
if (ret < 0) {
av_log(s, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n");
}
ff_format_set_url(oc, filename);
} else {
if (c->use_localtime) {
- time_t now0;
- struct tm *tm, tmpbuf;
- int bufsize = strlen(vs->basename) + MAX_URL_SIZE;
- char *buf = av_mallocz(bufsize);
- if (!buf)
- return AVERROR(ENOMEM);
- time(&now0);
- tm = localtime_r(&now0, &tmpbuf);
- ff_format_set_url(oc, buf);
- if (!strftime(oc->url, bufsize, vs->basename, tm)) {
+ int r;
+ char *expanded = NULL;
+
+ r = strftime_expand(vs->basename, &expanded);
+ if (r < 0) {
av_log(oc, AV_LOG_ERROR, "Could not get segment filename with strftime\n");
- return AVERROR(EINVAL);
+ return r;
}
+ ff_format_set_url(oc, expanded);
err = sls_flag_use_localtime_filename(oc, c, vs);
if (err < 0) {
if (c->use_localtime_mkdir) {
const char *dir;
char *fn_copy = av_strdup(oc->url);
+ if (!fn_copy)
+ return AVERROR(ENOMEM);
dir = av_dirname(fn_copy);
if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
av_log(oc, AV_LOG_ERROR, "Could not create directory %s with use_localtime_mkdir\n", dir);
}
ff_format_set_url(oc, filename);
}
- if ( vs->vtt_basename) {
+ if (vs->vtt_basename) {
char *filename = NULL;
if (replace_int_data_in_filename(&filename,
#if FF_API_HLS_WRAP
ff_format_set_url(vtt_oc, filename);
}
}
- vs->number++;
proto = avio_find_protocol_name(oc->url);
use_temp_file = proto && !strcmp(proto, "file") && (c->flags & HLS_TEMP_FILE);
" ignoring -hls_enc\n");
}
- if (!c->encrypt_started || (c->flags & HLS_PERIODIC_REKEY)) {
+ if (!vs->encrypt_started || (c->flags & HLS_PERIODIC_REKEY)) {
if (c->key_info_file) {
- if ((err = hls_encryption_start(s)) < 0)
+ if ((err = hls_encryption_start(s, vs)) < 0)
goto fail;
} else {
- if ((err = do_encrypt(s, vs)) < 0)
- goto fail;
+ if (!c->encrypt_started) {
+ if ((err = do_encrypt(s, vs)) < 0)
+ goto fail;
+ c->encrypt_started = 1;
+ }
+ av_strlcpy(vs->key_uri, c->key_uri, sizeof(vs->key_uri));
+ av_strlcpy(vs->key_string, c->key_string, sizeof(vs->key_string));
+ av_strlcpy(vs->iv_string, c->iv_string, sizeof(vs->iv_string));
}
- c->encrypt_started = 1;
+ vs->encrypt_started = 1;
}
- err = av_strlcpy(iv_string, c->iv_string, sizeof(iv_string));
+ err = av_strlcpy(iv_string, vs->iv_string, sizeof(iv_string));
if (!err) {
snprintf(iv_string, sizeof(iv_string), "%032"PRIx64, vs->sequence);
- memset(c->iv_string, 0, sizeof(c->iv_string));
- memcpy(c->iv_string, iv_string, sizeof(iv_string));
+ memset(vs->iv_string, 0, sizeof(vs->iv_string));
+ memcpy(vs->iv_string, iv_string, sizeof(iv_string));
}
}
if (c->segment_type != SEGMENT_TYPE_FMP4) {
av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
}
if (c->flags & HLS_SINGLE_FILE) {
+ if (c->key_info_file || c->encrypt) {
+ av_dict_set(&options, "encryption_key", vs->key_string, 0);
+ av_dict_set(&options, "encryption_iv", vs->iv_string, 0);
+
+ /* Write temp file with cryption content */
+ av_freep(&vs->basename_tmp);
+ vs->basename_tmp = av_asprintf("crypto:%s.tmp", oc->url);
+
+ /* append temp file content into single file */
+ av_freep(&vs->basename);
+ vs->basename = av_asprintf("%s", oc->url);
+ } else {
+ vs->basename_tmp = vs->basename;
+ }
set_http_options(s, &options, c);
- if ((err = hlsenc_io_open(s, &vs->out, oc->url, &options)) < 0) {
+ if (!vs->out_single_file)
+ if ((err = hlsenc_io_open(s, &vs->out_single_file, vs->basename, &options)) < 0) {
+ if (c->ignore_io_errors)
+ err = 0;
+ goto fail;
+ }
+
+ if ((err = hlsenc_io_open(s, &vs->out, vs->basename_tmp, &options)) < 0) {
if (c->ignore_io_errors)
err = 0;
goto fail;
}
+
}
}
if (vs->vtt_basename) {
char *fn_dup = NULL;
int ret = 0;
- if (!fn) {
- ret = AVERROR(EINVAL);
- goto fail;
- }
+ if (!fn)
+ return AVERROR(EINVAL);
fn_dup = av_strdup(fn);
+ if (!fn_dup)
+ return AVERROR(ENOMEM);
filename = av_basename(fn);
subdir_name = av_dirname(fn_dup);
int ret = 0;
orig_buf_dup = av_strdup(buf);
- if (!orig_buf_dup) {
- ret = AVERROR(ENOMEM);
- goto fail;
- }
+ if (!orig_buf_dup)
+ return AVERROR(ENOMEM);
if (!av_stristr(buf, "%v")) {
*s = orig_buf_dup;
- return ret;
+ return 0;
}
if (!varname) {
static int get_nth_codec_stream_index(AVFormatContext *s,
enum AVMediaType codec_type,
- int stream_id)
+ int64_t stream_id)
{
unsigned int stream_index, cnt;
if (stream_id < 0 || stream_id > s->nb_streams - 1)
return AVERROR(EINVAL);
q = varstr;
- while (q < varstr + strlen(varstr)) {
+ while (1) {
if (!av_strncasecmp(q, "a:", 2) || !av_strncasecmp(q, "v:", 2) ||
!av_strncasecmp(q, "s:", 2))
vs->nb_streams++;
+ q = strchr(q, ',');
+ if (!q)
+ break;
q++;
}
vs->streams = av_mallocz(sizeof(AVStream *) * vs->nb_streams);
nb_streams = 0;
while (keyval = av_strtok(varstr, ",", &saveptr2)) {
+ int64_t num;
+ char *end;
varstr = NULL;
if (av_strstart(keyval, "language:", &val)) {
- av_free(vs->language);
- vs->language = av_strdup(val);
- if (!vs->language)
- return AVERROR(ENOMEM);
+ vs->language = val;
continue;
} else if (av_strstart(keyval, "default:", &val)) {
vs->is_default = (!av_strncasecmp(val, "YES", strlen("YES")) ||
hls->has_default_key = 1;
continue;
} else if (av_strstart(keyval, "name:", &val)) {
- av_free(vs->varname);
- vs->varname = av_strdup(val);
- if (!vs->varname)
- return AVERROR(ENOMEM);
+ vs->varname = val;
continue;
} else if (av_strstart(keyval, "agroup:", &val)) {
- av_free(vs->agroup);
- vs->agroup = av_strdup(val);
- if (!vs->agroup)
- return AVERROR(ENOMEM);
+ vs->agroup = val;
continue;
} else if (av_strstart(keyval, "sgroup:", &val)) {
- av_free(vs->sgroup);
- vs->sgroup = av_strdup(val);
- if (!vs->sgroup)
- return AVERROR(ENOMEM);
+ vs->sgroup = val;
continue;
} else if (av_strstart(keyval, "ccgroup:", &val)) {
- av_free(vs->ccgroup);
- vs->ccgroup = av_strdup(val);
- if (!vs->ccgroup)
- return AVERROR(ENOMEM);
+ vs->ccgroup = val;
continue;
} else if (av_strstart(keyval, "v:", &val)) {
codec_type = AVMEDIA_TYPE_VIDEO;
return AVERROR(EINVAL);
}
- stream_index = -1;
- if (av_isdigit(*val))
- stream_index = get_nth_codec_stream_index (s, codec_type,
- atoi(val));
+ num = strtoll(val, &end, 10);
+ if (!av_isdigit(*val) || *end != '\0') {
+ av_log(s, AV_LOG_ERROR, "Invalid stream number: '%s'\n", val);
+ return AVERROR(EINVAL);
+ }
+ stream_index = get_nth_codec_stream_index(s, codec_type, num);
if (stream_index >= 0 && nb_streams < vs->nb_streams) {
for (i = 0; nb_streams > 0 && i < nb_streams; i++) {
ccstr = NULL;
if (av_strstart(keyval, "ccgroup:", &val)) {
- av_free(ccs->ccgroup);
- ccs->ccgroup = av_strdup(val);
- if (!ccs->ccgroup)
- return AVERROR(ENOMEM);
+ ccs->ccgroup = val;
} else if (av_strstart(keyval, "instreamid:", &val)) {
- av_free(ccs->instreamid);
- ccs->instreamid = av_strdup(val);
- if (!ccs->instreamid)
- return AVERROR(ENOMEM);
+ ccs->instreamid = val;
} else if (av_strstart(keyval, "language:", &val)) {
- av_free(ccs->language);
- ccs->language = av_strdup(val);
- if (!ccs->language)
- return AVERROR(ENOMEM);
+ ccs->language = val;
} else {
av_log(s, AV_LOG_ERROR, "Invalid keyval %s\n", keyval);
return AVERROR(EINVAL);
hls->var_streams[0].nb_streams = s->nb_streams;
hls->var_streams[0].streams = av_mallocz(sizeof(AVStream *) *
hls->var_streams[0].nb_streams);
- if (!hls->var_streams[0].streams) {
+ if (!hls->var_streams[0].streams)
return AVERROR(ENOMEM);
- }
//by default, the first available ccgroup is mapped to the variant stream
- if (hls->nb_ccstreams) {
- hls->var_streams[0].ccgroup = av_strdup(hls->cc_streams[0].ccgroup);
- if (!hls->var_streams[0].ccgroup) {
- return AVERROR(ENOMEM);
- }
- }
+ if (hls->nb_ccstreams)
+ hls->var_streams[0].ccgroup = hls->cc_streams[0].ccgroup;
for (i = 0; i < s->nb_streams; i++)
hls->var_streams[0].streams[i] = s->streams[i];
int ret = 0;
fn1 = av_strdup(s->url);
+ if (!fn1)
+ return AVERROR(ENOMEM);
dir = av_dirname(fn1);
/**
*/
if (dir && av_stristr(av_basename(dir), "%v")) {
fn2 = av_strdup(dir);
+ if (!fn2) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
dir = av_dirname(fn2);
}
continue;
}
avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
+ if (outer_st->codecpar->codec_id == AV_CODEC_ID_HEVC &&
+ outer_st->codecpar->codec_tag != MKTAG('h','v','c','1')) {
+ av_log(s, AV_LOG_WARNING, "Stream HEVC is not hvc1, you should use tag:v hvc1 to set it.\n");
+ }
write_codec_attr(outer_st, vs);
}
int ret = 0;
set_http_options(s, &options, hls);
- ret = hlsenc_io_open(s, &vs->out, hls->fmp4_init_filename, &options);
+ ret = hlsenc_io_open(s, &vs->out, vs->base_output_dirname, &options);
av_dict_free(&options);
if (ret < 0)
return ret;
return ret;
}
+static int64_t append_single_file(AVFormatContext *s, VariantStream *vs)
+{
+ int ret = 0;
+ int64_t read_byte = 0;
+ int64_t total_size = 0;
+ char *filename = NULL;
+ char buf[BUFSIZE];
+ AVFormatContext *oc = vs->avf;
+
+ hlsenc_io_close(s, &vs->out, vs->basename_tmp);
+ filename = av_asprintf("%s.tmp", oc->url);
+ ret = s->io_open(s, &vs->out, filename, AVIO_FLAG_READ, NULL);
+ if (ret < 0) {
+ av_free(filename);
+ return ret;
+ }
+
+ do {
+ memset(buf, 0, sizeof(BUFSIZE));
+ read_byte = avio_read(vs->out, buf, BUFSIZE);
+ avio_write(vs->out_single_file, buf, read_byte);
+ if (read_byte > 0) {
+ total_size += read_byte;
+ ret = total_size;
+ }
+ } while (read_byte > 0);
+
+ hlsenc_io_close(s, &vs->out, filename);
+ av_free(filename);
+
+ return ret;
+}
static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
{
HLSContext *hls = s->priv_data;
vs = &hls->var_streams[i];
for (j = 0; j < vs->nb_streams; j++) {
if (vs->streams[j] == st) {
- if ( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ) {
+ if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
oc = vs->vtt_avf;
stream_index = 0;
} else {
if (vs->sequence - vs->nb_entries > hls->start_sequence && hls->init_time > 0) {
/* reset end_pts, hls->recording_time at end of the init hls list */
- int64_t init_list_dur = hls->init_time * vs->nb_entries * AV_TIME_BASE;
- int64_t after_init_list_dur = (vs->sequence - hls->start_sequence - vs->nb_entries ) * (hls->time * AV_TIME_BASE);
- hls->recording_time = hls->time * AV_TIME_BASE;
+ int64_t init_list_dur = hls->init_time * vs->nb_entries;
+ int64_t after_init_list_dur = (vs->sequence - hls->start_sequence - vs->nb_entries) * hls->time;
+ hls->recording_time = hls->time;
end_pts = init_list_dur + after_init_list_dur ;
}
vs->start_pts_from_audio = 0;
}
- if (vs->has_video) {
+ if (vs->has_video) {
can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
((pkt->flags & AV_PKT_FLAG_KEY) || (hls->flags & HLS_SPLIT_BY_TIME));
is_ref_pkt = (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) && (pkt->stream_index == vs->reference_stream_index);
vs->duration = (double)(pkt->pts - vs->end_pts) * st->time_base.num / st->time_base.den;
}
}
-
}
+ can_split = can_split && (pkt->pts - vs->end_pts > 0);
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;
}
}
- if (oc->url[0]) {
- proto = avio_find_protocol_name(oc->url);
- use_temp_file = proto && !strcmp(proto, "file") && (hls->flags & HLS_TEMP_FILE);
- }
-
if (hls->flags & HLS_SINGLE_FILE) {
ret = flush_dynbuf(vs, &range_length);
av_freep(&vs->temp_buffer);
return ret;
}
vs->size = range_length;
+ if (hls->key_info_file || hls->encrypt)
+ vs->size = append_single_file(s, vs);
} else {
- if ((hls->max_seg_size > 0 && (vs->size >= hls->max_seg_size)) || !byterange_mode) {
+ if (oc->url[0]) {
+ proto = avio_find_protocol_name(oc->url);
+ use_temp_file = proto && !strcmp(proto, "file")
+ && (hls->flags & HLS_TEMP_FILE);
+ }
+
+ if ((hls->max_seg_size > 0 && (vs->size + vs->start_pos >= hls->max_seg_size)) || !byterange_mode) {
AVDictionary *options = NULL;
char *filename = NULL;
if (hls->key_info_file || hls->encrypt) {
- av_dict_set(&options, "encryption_key", hls->key_string, 0);
- av_dict_set(&options, "encryption_iv", hls->iv_string, 0);
+ av_dict_set(&options, "encryption_key", vs->key_string, 0);
+ av_dict_set(&options, "encryption_iv", vs->iv_string, 0);
filename = av_asprintf("crypto:%s", oc->url);
} else {
filename = av_asprintf("%s", oc->url);
if (ret < 0) {
av_log(s, hls->ignore_io_errors ? AV_LOG_WARNING : AV_LOG_ERROR,
"Failed to open file '%s'\n", filename);
+ av_freep(&filename);
av_dict_free(&options);
return hls->ignore_io_errors ? 0 : ret;
}
}
ret = flush_dynbuf(vs, &range_length);
if (ret < 0) {
+ av_freep(&filename);
av_dict_free(&options);
return ret;
}
av_freep(&vs->temp_buffer);
av_freep(&filename);
}
- }
- if (use_temp_file && !(hls->flags & HLS_SINGLE_FILE)) {
- hls_rename_temp_file(s, oc);
+ if (use_temp_file)
+ hls_rename_temp_file(s, oc);
}
old_filename = av_strdup(oc->url);
if ((ret = hls_window(s, 0, vs)) < 0) {
av_log(s, AV_LOG_WARNING, "upload playlist failed, will retry with a new http session.\n");
ff_format_io_close(s, &vs->out);
- vs->out = NULL;
if ((ret = hls_window(s, 0, vs)) < 0) {
av_freep(&old_filename);
return ret;
}
if (hls->flags & HLS_SINGLE_FILE) {
- vs->number++;
vs->start_pos += vs->size;
+ if (hls->key_info_file || hls->encrypt)
+ ret = hls_start(s, vs);
} else if (hls->max_seg_size > 0) {
- vs->start_pos = new_start_pos;
- if (vs->size >= hls->max_seg_size) {
+ if (vs->size + vs->start_pos >= hls->max_seg_size) {
vs->sequence++;
sls_flag_file_rename(hls, vs, old_filename);
ret = hls_start(s, vs);
vs->start_pos = 0;
/* When split segment by byte, the duration is short than hls_time,
* so it is not enough one segment duration as hls_time, */
- vs->number--;
+ } else {
+ vs->start_pos = new_start_pos;
}
- vs->number++;
} else {
vs->start_pos = new_start_pos;
sls_flag_file_rename(hls, vs, old_filename);
ret = hls_start(s, vs);
}
+ vs->number++;
av_freep(&old_filename);
if (ret < 0) {
vs->packets_written++;
if (oc->pb) {
+ int64_t keyframe_pre_pos = avio_tell(oc->pb);
ret = ff_write_chained(oc, stream_index, pkt, s, 0);
- vs->video_keyframe_size += pkt->size;
- if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) && (pkt->flags & AV_PKT_FLAG_KEY)) {
- vs->video_keyframe_size = avio_tell(oc->pb);
- } else {
- vs->video_keyframe_pos = avio_tell(vs->out);
+ if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
+ (pkt->flags & AV_PKT_FLAG_KEY) && !keyframe_pre_pos) {
+ av_write_frame(oc, NULL); /* Flush any buffered data */
+ vs->video_keyframe_size = avio_tell(oc->pb) - keyframe_pre_pos;
}
+ vs->video_keyframe_pos = vs->start_pos;
if (hls->ignore_io_errors)
ret = 0;
}
return ret;
}
-static void hls_free_variant_streams(struct HLSContext *hls)
+static void hls_deinit(AVFormatContext *s)
{
+ HLSContext *hls = s->priv_data;
int i = 0;
- AVFormatContext *vtt_oc = NULL;
VariantStream *vs = NULL;
for (i = 0; i < hls->nb_varstreams; i++) {
vs = &hls->var_streams[i];
- vtt_oc = vs->vtt_avf;
av_freep(&vs->basename);
av_freep(&vs->base_output_dirname);
av_freep(&vs->fmp4_init_filename);
- if (vtt_oc) {
- av_freep(&vs->vtt_basename);
- av_freep(&vs->vtt_m3u8_name);
- avformat_free_context(vtt_oc);
- }
+ av_freep(&vs->vtt_basename);
+ av_freep(&vs->vtt_m3u8_name);
+ avformat_free_context(vs->vtt_avf);
avformat_free_context(vs->avf);
if (hls->resend_init_file)
av_freep(&vs->init_buffer);
hls_free_segments(vs->old_segments);
av_freep(&vs->m3u8_name);
av_freep(&vs->streams);
- av_freep(&vs->agroup);
- av_freep(&vs->sgroup);
- av_freep(&vs->language);
- av_freep(&vs->ccgroup);
- av_freep(&vs->baseurl);
- av_freep(&vs->varname);
}
+
+ ff_format_io_close(s, &hls->m3u8_out);
+ ff_format_io_close(s, &hls->sub_m3u8_out);
+ av_freep(&hls->key_basename);
+ av_freep(&hls->var_streams);
+ av_freep(&hls->cc_streams);
+ av_freep(&hls->master_m3u8_url);
}
static int hls_write_trailer(struct AVFormatContext *s)
return AVERROR(ENOMEM);
}
if (hls->key_info_file || hls->encrypt) {
- av_dict_set(&options, "encryption_key", hls->key_string, 0);
- av_dict_set(&options, "encryption_iv", hls->iv_string, 0);
+ av_dict_set(&options, "encryption_key", vs->key_string, 0);
+ av_dict_set(&options, "encryption_iv", vs->iv_string, 0);
filename = av_asprintf("crypto:%s", oc->url);
} else {
filename = av_asprintf("%s", oc->url);
return AVERROR(ENOMEM);
}
- if ( hls->segment_type == SEGMENT_TYPE_FMP4) {
+ if (hls->segment_type == SEGMENT_TYPE_FMP4) {
int range_length = 0;
if (!vs->init_range_length) {
uint8_t *buffer = NULL;
goto failed;
vs->size = range_length;
- hlsenc_io_close(s, &vs->out, filename);
ret = hlsenc_io_close(s, &vs->out, filename);
if (ret < 0) {
av_log(s, AV_LOG_WARNING, "upload segment failed, will retry with a new http session.\n");
if (ret < 0)
av_log(s, AV_LOG_WARNING, "Failed to upload file '%s' at the end.\n", oc->url);
}
- av_freep(&vs->temp_buffer);
-
+ if (hls->flags & HLS_SINGLE_FILE) {
+ if (hls->key_info_file || hls->encrypt) {
+ vs->size = append_single_file(s, vs);
+ }
+ hlsenc_io_close(s, &vs->out_single_file, vs->basename);
+ }
failed:
+ av_freep(&vs->temp_buffer);
+ av_dict_free(&options);
av_freep(&filename);
av_write_trailer(oc);
if (oc->url[0]) {
av_free(old_filename);
}
- hls_free_variant_streams(hls);
-
- for (i = 0; i < hls->nb_ccstreams; i++) {
- ClosedCaptionsStream *ccs = &hls->cc_streams[i];
- av_freep(&ccs->ccgroup);
- av_freep(&ccs->instreamid);
- av_freep(&ccs->language);
- }
-
- ff_format_io_close(s, &hls->m3u8_out);
- ff_format_io_close(s, &hls->sub_m3u8_out);
- av_freep(&hls->key_basename);
- av_freep(&hls->var_streams);
- av_freep(&hls->cc_streams);
- av_freep(&hls->master_m3u8_url);
return 0;
}
int i = 0;
int j = 0;
HLSContext *hls = s->priv_data;
- const char *pattern = "%d.ts";
+ const char *pattern;
VariantStream *vs = NULL;
- int basename_size = 0;
- const char *pattern_localtime_fmt = get_default_pattern_localtime_fmt(s);
- const char *vtt_pattern = "%d.vtt";
+ const char *vtt_pattern = hls->flags & HLS_SINGLE_FILE ? ".vtt" : "%d.vtt";
char *p = NULL;
- int vtt_basename_size = 0;
int http_base_proto = ff_is_http_proto(s->url);
int fmp4_init_filename_len = strlen(hls->fmp4_init_filename) + 1;
+ double initial_program_date_time = av_gettime() / 1000000.0;
+
+ if (hls->use_localtime) {
+ pattern = get_default_pattern_localtime_fmt(s);
+ } else {
+ pattern = hls->segment_type == SEGMENT_TYPE_FMP4 ? "%d.m4s" : "%d.ts";
+ if (hls->flags & HLS_SINGLE_FILE)
+ pattern += 2;
+ }
hls->has_default_key = 0;
hls->has_video_m3u8 = 0;
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Variant stream info update failed with status %x\n",
ret);
- goto fail;
- }
- //TODO: Updates needed to encryption functionality with periodic re-key when more than one variant streams are present
- if (hls->nb_varstreams > 1 && hls->flags & HLS_PERIODIC_REKEY) {
- ret = AVERROR(EINVAL);
- av_log(s, AV_LOG_ERROR, "Periodic re-key not supported when more than one variant streams are present\n");
- goto fail;
+ return ret;
}
if (!hls->method && http_base_proto) {
ret = validate_name(hls->nb_varstreams, s->url);
if (ret < 0)
- goto fail;
+ return ret;
if (hls->segment_filename) {
ret = validate_name(hls->nb_varstreams, hls->segment_filename);
if (ret < 0)
- goto fail;
+ return ret;
}
if (av_strcasecmp(hls->fmp4_init_filename, "init.mp4")) {
ret = validate_name(hls->nb_varstreams, hls->fmp4_init_filename);
if (ret < 0)
- goto fail;
+ return ret;
}
if (hls->subtitle_filename) {
ret = validate_name(hls->nb_varstreams, hls->subtitle_filename);
if (ret < 0)
- goto fail;
+ return ret;
}
if (hls->master_pl_name) {
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Master stream info update failed with status %x\n",
ret);
- goto fail;
+ return ret;
}
}
- if (hls->segment_type == SEGMENT_TYPE_FMP4) {
- pattern = "%d.m4s";
- }
if ((hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH) ||
(hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH) ||
(hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_FORMATTED_DATETIME)) {
av_log(hls, AV_LOG_DEBUG, "start_number evaluated to %"PRId64"\n", hls->start_sequence);
}
- hls->recording_time = (hls->init_time ? hls->init_time : hls->time) * AV_TIME_BASE;
+ hls->recording_time = hls->init_time ? hls->init_time : hls->time;
+
+ if (hls->flags & HLS_SPLIT_BY_TIME && hls->flags & HLS_INDEPENDENT_SEGMENTS) {
+ // Independent segments cannot be guaranteed when splitting by time
+ hls->flags &= ~HLS_INDEPENDENT_SEGMENTS;
+ av_log(s, AV_LOG_WARNING,
+ "'split_by_time' and 'independent_segments' cannot be "
+ "enabled together. Disabling 'independent_segments' flag\n");
+ }
+
for (i = 0; i < hls->nb_varstreams; i++) {
vs = &hls->var_streams[i];
ret = format_name(s->url, &vs->m3u8_name, i, vs->varname);
if (ret < 0)
- goto fail;
+ return ret;
- vs->sequence = hls->start_sequence;
- vs->start_pts = AV_NOPTS_VALUE;
- vs->end_pts = AV_NOPTS_VALUE;
+ vs->sequence = hls->start_sequence;
+ vs->start_pts = AV_NOPTS_VALUE;
+ vs->end_pts = AV_NOPTS_VALUE;
vs->current_segment_final_filename_fmt[0] = '\0';
-
- if (hls->flags & HLS_SPLIT_BY_TIME && hls->flags & HLS_INDEPENDENT_SEGMENTS) {
- // Independent segments cannot be guaranteed when splitting by time
- hls->flags &= ~HLS_INDEPENDENT_SEGMENTS;
- av_log(s, AV_LOG_WARNING,
- "'split_by_time' and 'independent_segments' cannot be enabled together. "
- "Disabling 'independent_segments' flag\n");
- }
-
- if (hls->flags & HLS_PROGRAM_DATE_TIME) {
- time_t now0;
- time(&now0);
- vs->initial_prog_date_time = now0;
- }
+ vs->initial_prog_date_time = initial_program_date_time;
for (j = 0; j < vs->nb_streams; j++) {
vs->has_video += vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;
} else {
vs->oformat = av_guess_format("mpegts", NULL, NULL);
}
+ if (!vs->oformat)
+ return AVERROR_MUXER_NOT_FOUND;
- if (!vs->oformat) {
- ret = AVERROR_MUXER_NOT_FOUND;
- goto fail;
- }
-
- if (vs->has_subtitle) {
- vs->vtt_oformat = av_guess_format("webvtt", NULL, NULL);
- if (!vs->vtt_oformat) {
- ret = AVERROR_MUXER_NOT_FOUND;
- goto fail;
- }
- }
if (hls->segment_filename) {
ret = format_name(hls->segment_filename, &vs->basename, i, vs->varname);
if (ret < 0)
- goto fail;
+ return ret;
} else {
- if (hls->flags & HLS_SINGLE_FILE) {
- if (hls->segment_type == SEGMENT_TYPE_FMP4) {
- pattern = ".m4s";
- } else {
- pattern = ".ts";
- }
- }
-
- if (hls->use_localtime) {
- basename_size = strlen(vs->m3u8_name) + strlen(pattern_localtime_fmt) + 1;
- } else {
- basename_size = strlen(vs->m3u8_name) + strlen(pattern) + 1;
- }
-
- vs->basename = av_malloc(basename_size);
- if (!vs->basename) {
- ret = AVERROR(ENOMEM);
- goto fail;
- }
+ p = strrchr(vs->m3u8_name, '.');
+ if (p)
+ *p = '\0';
- av_strlcpy(vs->basename, vs->m3u8_name, basename_size);
+ vs->basename = av_asprintf("%s%s", vs->m3u8_name, pattern);
+ if (!vs->basename)
+ return AVERROR(ENOMEM);
- p = strrchr(vs->basename, '.');
if (p)
- *p = '\0';
- if (hls->use_localtime) {
- av_strlcat(vs->basename, pattern_localtime_fmt, basename_size);
- } else {
- av_strlcat(vs->basename, pattern, basename_size);
- }
+ *p = '.';
}
if (hls->segment_type == SEGMENT_TYPE_FMP4) {
fmp4_init_filename_len += strlen(POSTFIX_PATTERN);
if (hls->flags & HLS_SINGLE_FILE) {
vs->fmp4_init_filename = av_strdup(vs->basename);
- if (!vs->fmp4_init_filename) {
- ret = AVERROR(ENOMEM);
- goto fail;
- }
+ if (!vs->fmp4_init_filename)
+ return AVERROR(ENOMEM);
} else {
vs->fmp4_init_filename = av_malloc(fmp4_init_filename_len);
- if (!vs->fmp4_init_filename ) {
- ret = AVERROR(ENOMEM);
- goto fail;
- }
+ if (!vs->fmp4_init_filename)
+ return AVERROR(ENOMEM);
av_strlcpy(vs->fmp4_init_filename, hls->fmp4_init_filename,
fmp4_init_filename_len);
if (hls->nb_varstreams > 1) {
if (av_stristr(vs->fmp4_init_filename, "%v")) {
av_freep(&vs->fmp4_init_filename);
- format_name(hls->fmp4_init_filename, &vs->fmp4_init_filename, i, vs->varname);
+ ret = format_name(hls->fmp4_init_filename,
+ &vs->fmp4_init_filename, i, vs->varname);
} else {
ret = append_postfix(vs->fmp4_init_filename, fmp4_init_filename_len, i);
}
if (ret < 0)
- goto fail;
+ return ret;
}
- fmp4_init_filename_len = strlen(vs->m3u8_name) +
- strlen(vs->fmp4_init_filename) + 1;
+ if (hls->use_localtime) {
+ int r;
+ char *expanded = NULL;
- vs->base_output_dirname = av_malloc(fmp4_init_filename_len);
- if (!vs->base_output_dirname) {
- ret = AVERROR(ENOMEM);
- goto fail;
+ r = strftime_expand(vs->fmp4_init_filename, &expanded);
+ if (r < 0) {
+ av_log(s, AV_LOG_ERROR, "Could not get segment filename with strftime\n");
+ return r;
+ }
+ av_free(vs->fmp4_init_filename);
+ vs->fmp4_init_filename = expanded;
}
- av_strlcpy(vs->base_output_dirname, vs->m3u8_name,
- fmp4_init_filename_len);
- p = strrchr(vs->base_output_dirname, '/');
+ p = strrchr(vs->m3u8_name, '/');
if (p) {
- *(p + 1) = '\0';
- av_strlcat(vs->base_output_dirname, vs->fmp4_init_filename,
- fmp4_init_filename_len);
+ char tmp = *(++p);
+ *p = '\0';
+ vs->base_output_dirname = av_asprintf("%s%s", vs->m3u8_name,
+ vs->fmp4_init_filename);
+ *p = tmp;
} else {
- av_strlcpy(vs->base_output_dirname, vs->fmp4_init_filename,
- fmp4_init_filename_len);
+ vs->base_output_dirname = av_strdup(vs->fmp4_init_filename);
}
+ if (!vs->base_output_dirname)
+ return AVERROR(ENOMEM);
}
}
ret = hls->use_localtime ? sls_flag_check_duration_size(hls, vs) : sls_flag_check_duration_size_index(hls);
if (ret < 0)
- goto fail;
+ return ret;
if (vs->has_subtitle) {
+ vs->vtt_oformat = av_guess_format("webvtt", NULL, NULL);
+ if (!vs->vtt_oformat)
+ return AVERROR_MUXER_NOT_FOUND;
- if (hls->flags & HLS_SINGLE_FILE)
- vtt_pattern = ".vtt";
- vtt_basename_size = strlen(vs->m3u8_name) + strlen(vtt_pattern) + 1;
-
- vs->vtt_basename = av_malloc(vtt_basename_size);
- if (!vs->vtt_basename) {
- ret = AVERROR(ENOMEM);
- goto fail;
- }
- av_strlcpy(vs->vtt_basename, vs->m3u8_name, vtt_basename_size);
- p = strrchr(vs->vtt_basename, '.');
+ p = strrchr(vs->m3u8_name, '.');
if (p)
*p = '\0';
- if ( hls->subtitle_filename ) {
+ vs->vtt_basename = av_asprintf("%s%s", vs->m3u8_name, vtt_pattern);
+ if (!vs->vtt_basename)
+ return AVERROR(ENOMEM);
+
+ if (hls->subtitle_filename) {
ret = format_name(hls->subtitle_filename, &vs->vtt_m3u8_name, i, vs->varname);
if (ret < 0)
- goto fail;
+ return ret;
} else {
- vs->vtt_m3u8_name = av_asprintf("%s_vtt.m3u8", vs->vtt_basename);
- if (!vs->vtt_m3u8_name) {
- ret = AVERROR(ENOMEM);
- goto fail;
- }
- }
- av_strlcat(vs->vtt_basename, vtt_pattern, vtt_basename_size);
- }
-
- if (hls->baseurl) {
- vs->baseurl = av_strdup(hls->baseurl);
- if (!vs->baseurl) {
- ret = AVERROR(ENOMEM);
- goto fail;
+ vs->vtt_m3u8_name = av_asprintf("%s_vtt.m3u8", vs->m3u8_name);
+ if (!vs->vtt_m3u8_name)
+ return AVERROR(ENOMEM);
}
+ if (p)
+ *p = '.';
}
if ((ret = hls_mux_init(s, vs)) < 0)
- goto fail;
+ return ret;
if (hls->flags & HLS_APPEND_LIST) {
parse_playlist(s, vs->m3u8_name, vs);
av_log(s, AV_LOG_WARNING, "append_list mode does not support hls_init_time,"
" hls_init_time value will have no effect\n");
hls->init_time = 0;
- hls->recording_time = hls->time * AV_TIME_BASE;
+ hls->recording_time = hls->time;
}
}
if ((ret = hls_start(s, vs)) < 0)
- goto fail;
- }
-
-fail:
- if (ret < 0) {
- hls_free_variant_streams(hls);
- for (i = 0; i < hls->nb_ccstreams; i++) {
- ClosedCaptionsStream *ccs = &hls->cc_streams[i];
- av_freep(&ccs->ccgroup);
- av_freep(&ccs->instreamid);
- av_freep(&ccs->language);
- }
- av_freep(&hls->key_basename);
- av_freep(&hls->var_streams);
- av_freep(&hls->cc_streams);
- av_freep(&hls->master_m3u8_url);
+ return ret;
+ vs->number++;
}
return ret;
#define E AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
{"start_number", "set first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
- {"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
- {"hls_init_time", "set segment length in seconds at init list", OFFSET(init_time), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, FLT_MAX, E},
+ {"hls_time", "set segment length", OFFSET(time), AV_OPT_TYPE_DURATION, {.i64 = 2000000}, 0, INT64_MAX, E},
+ {"hls_init_time", "set segment length at init list", OFFSET(init_time), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, E},
{"hls_list_size", "set maximum number of playlist entries", OFFSET(max_nb_segments), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
{"hls_delete_threshold", "set number of unreferenced segments to keep before deleting", OFFSET(hls_delete_threshold), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, E},
{"hls_ts_options","set hls mpegts list of options for the container format used for hls", OFFSET(format_options), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, E},
{"independent_segments", "add EXT-X-INDEPENDENT-SEGMENTS, whenever applicable", 0, AV_OPT_TYPE_CONST, { .i64 = HLS_INDEPENDENT_SEGMENTS }, 0, UINT_MAX, E, "flags"},
{"iframes_only", "add EXT-X-I-FRAMES-ONLY, whenever applicable", 0, AV_OPT_TYPE_CONST, { .i64 = HLS_I_FRAMES_ONLY }, 0, UINT_MAX, E, "flags"},
#if FF_API_HLS_USE_LOCALTIME
- {"use_localtime", "set filename expansion with strftime at segment creation(will be deprecated )", OFFSET(use_localtime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
+ {"use_localtime", "set filename expansion with strftime at segment creation(will be deprecated)", OFFSET(use_localtime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
#endif
{"strftime", "set filename expansion with strftime at segment creation", OFFSET(use_localtime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
#if FF_API_HLS_USE_LOCALTIME
.write_header = hls_write_header,
.write_packet = hls_write_packet,
.write_trailer = hls_write_trailer,
+ .deinit = hls_deinit,
.priv_class = &hls_class,
};