From 1e45fb3facf5cfc8c67828b37b0bbaa994c2ab03 Mon Sep 17 00:00:00 2001 From: Li Kai Date: Thu, 23 Jun 2022 00:55:38 +0800 Subject: Re: [FFmpeg-devel] [PATCH] avformat/hls:use EXT-X-START instead of live_start_index if it's in playlist Signed-off-by: Li Kai --- libavformat/hls.c | 55 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/libavformat/hls.c b/libavformat/hls.c index 8204f55df3..bfb20f6492 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -120,6 +120,8 @@ struct playlist { enum PlaylistType type; int64_t target_duration; int64_t start_seq_no; + int time_offset_flag; + int64_t start_time_offset; int n_segments; struct segment **segments; int needed; @@ -741,6 +743,7 @@ static int parse_playlist(HLSContext *c, const char *url, struct segment **prev_segments = NULL; int prev_n_segments = 0; int64_t prev_start_seq_no = -1; + const char *p; if (is_http && !in && c->http_persistent && c->playlist_pb) { in = c->playlist_pb; @@ -889,6 +892,18 @@ static int parse_playlist(HLSContext *c, const char *url, cur_init_section->key = NULL; } + } else if (av_strstart(line, "#EXT-X-START:", &ptr)) { + ret = ensure_playlist(c, &pls, url); + if (ret < 0) { + goto fail; + } + if (av_strstart(ptr, "TIME-OFFSET=", &p)) { + float offset = strtof(p, NULL); + pls->start_time_offset = offset * AV_TIME_BASE; + pls->time_offset_flag = 1; + } else { + goto fail; + } } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) { if (pls) pls->finished = 1; @@ -1722,9 +1737,45 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) /* If this is a live stream, start live_start_index segments from the * start or end */ if (c->live_start_index < 0) - return pls->start_seq_no + FFMAX(pls->n_segments + c->live_start_index, 0); + seq_no = pls->start_seq_no + FFMAX(pls->n_segments + + c->live_start_index, 0); else - return pls->start_seq_no + FFMIN(c->live_start_index, pls->n_segments - 1); + seq_no = pls->start_seq_no + FFMIN(c->live_start_index, + pls->n_segments - 1); + + /* If #EXT-X-START in playlist, need to recalculate */ + if (pls->time_offset_flag) { + int64_t start_timestamp; + int64_t playlist_duration = 0; + int64_t cur_timestamp = c->cur_timestamp == AV_NOPTS_VALUE ? 0 : + c->cur_timestamp; + + for (int i = 0; i < pls->n_segments; i++) + playlist_duration += pls->segments[i]->duration; + + /* If the absolute value of TIME-OFFSET exceeds + * the duration of the playlist, it indicates either the end of the + * playlist (if positive) or the beginning of the playlist (if + * negative). */ + if (pls->start_time_offset >=0 && + pls->start_time_offset > playlist_duration) + start_timestamp = cur_timestamp + playlist_duration; + else if (pls->start_time_offset >= 0 && + pls->start_time_offset <= playlist_duration) + start_timestamp = cur_timestamp + pls->start_time_offset; + else if (pls->start_time_offset < 0 && + pls->start_time_offset < -playlist_duration) + start_timestamp = cur_timestamp; + else if (pls->start_time_offset < 0 && + pls->start_time_offset > -playlist_duration) + start_timestamp = cur_timestamp + playlist_duration + + pls->start_time_offset; + else + start_timestamp = cur_timestamp; + + find_timestamp_in_playlist(c, pls, start_timestamp, &seq_no, NULL); + } + return seq_no; } /* Otherwise just start on the first segment. */ -- 2.25.1 Thanks a lot!