Commit 987026ad authored by Steven Liu's avatar Steven Liu

avformat/hlsenc: add option hls_delete_threshold

When using hls_list_size with hls_flags delete_segments, currently
hls_list_size * 2 +- segments remain on disk. With this new option,
the amount of disk space used can be controlled by the user.

fix ticket: #7131
Signed-off-by: 's avatarSteven Liu <lq@chinaffmpeg.org>
Signed-off-by: 's avatarAman Gupta <aman@tmm1.net>
parent 9e857aa0
...@@ -513,6 +513,12 @@ Segment will be cut on the next key frame after this time has passed. ...@@ -513,6 +513,12 @@ Segment will be cut on the next key frame after this time has passed.
Set the maximum number of playlist entries. If set to 0 the list file Set the maximum number of playlist entries. If set to 0 the list file
will contain all the segments. Default value is 5. will contain all the segments. Default value is 5.
@item hls_delete_threshold @var{size}
Set the number of unreferenced segments to keep on disk before @code{hls_flags delete_segments}
deletes them. Increase this to allow continue clients to download segments which
were recently referenced in the playlist. Default value is 1, meaning segments older than
@code{hls_list_size+1} will be deleted.
@item hls_ts_options @var{options_list} @item hls_ts_options @var{options_list}
Set output format options using a :-separated list of key=value Set output format options using a :-separated list of key=value
parameters. Values containing @code{:} special characters must be parameters. Values containing @code{:} special characters must be
......
...@@ -173,6 +173,7 @@ typedef struct HLSContext { ...@@ -173,6 +173,7 @@ typedef struct HLSContext {
float time; // Set by a private option. float time; // Set by a private option.
float init_time; // Set by a private option. float init_time; // Set by a private option.
int max_nb_segments; // 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 #if FF_API_HLS_WRAP
int wrap; // Set by a private option. int wrap; // Set by a private option.
#endif #endif
...@@ -445,6 +446,7 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls, ...@@ -445,6 +446,7 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
HLSSegment *segment, *previous_segment = NULL; HLSSegment *segment, *previous_segment = NULL;
float playlist_duration = 0.0f; float playlist_duration = 0.0f;
int ret = 0, path_size, sub_path_size; int ret = 0, path_size, sub_path_size;
int segment_cnt = 0;
char *dirname = NULL, *p, *sub_path; char *dirname = NULL, *p, *sub_path;
char *path = NULL; char *path = NULL;
AVDictionary *options = NULL; AVDictionary *options = NULL;
...@@ -458,14 +460,20 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls, ...@@ -458,14 +460,20 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
} }
segment = vs->old_segments; segment = vs->old_segments;
segment_cnt = 0;
while (segment) { while (segment) {
playlist_duration -= segment->duration; playlist_duration -= segment->duration;
previous_segment = segment; previous_segment = segment;
segment = previous_segment->next; segment = previous_segment->next;
segment_cnt++;
if (playlist_duration <= -previous_segment->duration) { if (playlist_duration <= -previous_segment->duration) {
previous_segment->next = NULL; previous_segment->next = NULL;
break; break;
} }
if (segment_cnt >= hls->hls_delete_threshold) {
previous_segment->next = NULL;
break;
}
} }
if (segment && !hls->use_localtime_mkdir) { if (segment && !hls->use_localtime_mkdir) {
...@@ -2791,6 +2799,7 @@ static const AVOption options[] = { ...@@ -2791,6 +2799,7 @@ static const AVOption options[] = {
{"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_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_init_time", "set segment length in seconds at init list", OFFSET(init_time), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, FLT_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_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_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E}, {"hls_ts_options","set hls mpegts list of options for the container format used for hls", OFFSET(format_options_str), AV_OPT_TYPE_STRING, {.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}, {"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 #if FF_API_HLS_WRAP
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment