Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: wolfleekay@gmail.com
To: ffmpeg-devel@ffmpeg.org
Cc: Li Kai <wolfleekay@gmail.com>
Subject: [FFmpeg-devel] [PATCH] avformat/hls:use EXT-X-START instead of live_start_index if it's in playlist
Date: Thu, 23 Jun 2022 23:00:40 +0800
Message-ID: <20220623150040.1981341-1-wolfleekay@gmail.com> (raw)

From: Li Kai <wolfleekay@gmail.com>

Refer to https://datatracker.ietf.org/doc/html/rfc8216#section-4.3.5.2

Signed-off-by: Li Kai <wolfleekay@gmail.com>
---
 libavformat/hls.c | 68 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 62 insertions(+), 6 deletions(-)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index 8204f55df3..96923c3eac 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;
@@ -1719,12 +1734,53 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls)
              * require us to download a segment to inspect its timestamps. */
             return c->cur_seq_no;
 
-        /* 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);
-        else
-            return pls->start_seq_no + FFMIN(c->live_start_index, pls->n_segments - 1);
+        if (!pls->time_offset_flag) {
+            /* 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);
+            else
+                return pls->start_seq_no + FFMIN(c->live_start_index,
+                                                pls->n_segments - 1);
+        } else {
+            /* If playlist indicate a TIME-OFFSET, need to recalculate seq_no */
+            int i;
+            int64_t start_timestamp;
+            int64_t playlist_duration = 0;
+            int64_t cur_timestamp = c->cur_timestamp == AV_NOPTS_VALUE ? 0 :
+                                    c->cur_timestamp;
+
+            for (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);
+            av_log(c, AV_LOG_DEBUG, "start_timestamp: %" PRId64
+                                    "cur_timestamp:%" PRId64
+                                    "cur_seq_no:%" PRId64 "\n",
+                                    start_timestamp, cur_timestamp, seq_no);
+            return seq_no;
+        }
     }
 
     /* Otherwise just start on the first segment. */
-- 
2.25.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

             reply	other threads:[~2022-06-23 15:01 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-23 15:00 wolfleekay [this message]
2022-06-24  1:50 ` Steven Liu
2022-06-24  3:58   ` 少宇李
2022-06-24  6:21     ` Steven Liu
2022-06-25  4:32       ` 少宇李
2022-06-25  4:51         ` Steven Liu
2022-06-25  5:23           ` Li Kai
2022-06-25  5:24           ` Li Kai
2022-06-25 13:26             ` Steven Liu
2022-06-25 14:15               ` Li Kai
2022-06-27  2:23                 ` Steven Liu
2022-06-27  2:58                   ` Li Kai
2022-06-27  3:05                     ` Steven Liu
2022-06-29  4:17                       ` Steven Liu
2022-06-24 16:44 Li Kai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220623150040.1981341-1-wolfleekay@gmail.com \
    --to=wolfleekay@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git