X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fhlsenc.c;h=6e7d6d554b1bccc2ccff6fd9ea8b7a01e3dde229;hb=2086d635c36c2865d1a0145c56e448b30af59ba0;hp=9bce37460554c0c53dd2ec7a5070b3678572507b;hpb=45a673ebee2e603a87c0ecb259c0027c14321018;p=ffmpeg diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index 9bce3746055..6e7d6d554b1 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -119,8 +119,8 @@ typedef struct VariantStream { 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; @@ -196,13 +196,10 @@ 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 wrap; // Set by a private option. -#endif uint32_t flags; // enum HLSFlags uint32_t pl_type; // enum PlaylistType char *segment_filename; @@ -260,6 +257,29 @@ typedef struct HLSContext { 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) { @@ -1040,11 +1060,7 @@ static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c, V if (c->flags & HLS_SECOND_LEVEL_SEGMENT_INDEX) { char *filename = NULL; if (replace_int_data_in_filename(&filename, -#if FF_API_HLS_WRAP - oc->url, 'd', c->wrap ? vs->sequence % c->wrap : vs->sequence) < 1) { -#else 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", oc->url); @@ -1154,11 +1170,7 @@ static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, vs->initial_prog_date_time += en->duration; vs->segments = en->next; if (en && hls->flags & HLS_DELETE_SEGMENTS && -#if FF_API_HLS_WRAP - !(hls->flags & HLS_SINGLE_FILE || hls->wrap)) { -#else !(hls->flags & HLS_SINGLE_FILE)) { -#endif en->next = vs->old_segments; vs->old_segments = en; if ((ret = hls_delete_old_segments(s, hls, vs)) < 0) @@ -1541,6 +1553,10 @@ static int hls_window(AVFormatContext *s, int last, VariantStream *vs) sequence = 0; } + if (hls->flags & HLS_I_FRAMES_ONLY) { + hls->version = 4; + } + if (hls->flags & HLS_INDEPENDENT_SEGMENTS) { hls->version = 6; } @@ -1675,11 +1691,7 @@ static int hls_start(AVFormatContext *s, VariantStream *vs) } else if (c->max_seg_size > 0) { char *filename = NULL; if (replace_int_data_in_filename(&filename, -#if FF_API_HLS_WRAP - vs->basename, 'd', c->wrap ? vs->sequence % c->wrap : vs->sequence) < 1) { -#else vs->basename, 'd', vs->sequence) < 1) { -#endif av_freep(&filename); av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s', you can try to use -strftime 1 with it\n", vs->basename); return AVERROR(EINVAL); @@ -1687,19 +1699,15 @@ static int hls_start(AVFormatContext *s, VariantStream *vs) 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) { @@ -1722,11 +1730,7 @@ static int hls_start(AVFormatContext *s, VariantStream *vs) } else { char *filename = NULL; if (replace_int_data_in_filename(&filename, -#if FF_API_HLS_WRAP - vs->basename, 'd', c->wrap ? vs->sequence % c->wrap : vs->sequence) < 1) { -#else vs->basename, 'd', vs->sequence) < 1) { -#endif av_freep(&filename); av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s' you can try to use -strftime 1 with it\n", vs->basename); return AVERROR(EINVAL); @@ -1736,11 +1740,7 @@ static int hls_start(AVFormatContext *s, VariantStream *vs) if (vs->vtt_basename) { char *filename = NULL; if (replace_int_data_in_filename(&filename, -#if FF_API_HLS_WRAP - vs->vtt_basename, 'd', c->wrap ? vs->sequence % c->wrap : vs->sequence) < 1) { -#else vs->vtt_basename, 'd', vs->sequence) < 1) { -#endif av_freep(&filename); av_log(vtt_oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", vs->vtt_basename); return AVERROR(EINVAL); @@ -2435,9 +2435,9 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) 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 ; } @@ -2922,7 +2922,7 @@ static int hls_init(AVFormatContext *s) 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 @@ -3007,6 +3007,19 @@ static int hls_init(AVFormatContext *s) return ret; } + if (hls->use_localtime) { + int r; + char *expanded = NULL; + + 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; + } + p = strrchr(vs->m3u8_name, '/'); if (p) { char tmp = *(++p); @@ -3062,7 +3075,7 @@ static int hls_init(AVFormatContext *s) 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; } } @@ -3078,15 +3091,12 @@ static int hls_init(AVFormatContext *s) #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}, {"hls_vtt_options","set hls vtt list of options for the container format used for hls", OFFSET(vtt_format_options_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E}, -#if FF_API_HLS_WRAP - {"hls_wrap", "set number after which the index wraps (will be deprecated)", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E}, -#endif {"hls_allow_cache", "explicitly set whether the client MAY (1) or MUST NOT (0) cache media segments", OFFSET(allowcache), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, E}, {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E}, {"hls_segment_filename", "filename template for segment files", OFFSET(segment_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E}, @@ -3118,13 +3128,7 @@ static const AVOption options[] = { {"periodic_rekey", "reload keyinfo file periodically for re-keying", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PERIODIC_REKEY }, 0, UINT_MAX, E, "flags"}, {"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 }, -#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 - {"use_localtime_mkdir", "create last directory component in strftime-generated filename(will be deprecated)", OFFSET(use_localtime_mkdir), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E }, -#endif {"strftime_mkdir", "create last directory component in strftime-generated filename", OFFSET(use_localtime_mkdir), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E }, {"hls_playlist_type", "set the HLS playlist type", OFFSET(pl_type), AV_OPT_TYPE_INT, {.i64 = PLAYLIST_TYPE_NONE }, 0, PLAYLIST_TYPE_NB-1, E, "pl_type" }, {"event", "EVENT playlist", 0, AV_OPT_TYPE_CONST, {.i64 = PLAYLIST_TYPE_EVENT }, INT_MIN, INT_MAX, E, "pl_type" },