Skip to content

Commit 286bb41

Browse files
committed
fix(: resolve hls seek due to ffmpeg patch
1 parent 04ffb9b commit 286bb41

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
From 0c14ac8d5f19f22f31ae9505e3db5eb417e8d6e0 Mon Sep 17 00:00:00 2001
2+
From: llyyr <llyyr.public@gmail.com>
3+
Date: Sun, 27 Oct 2024 06:52:42 +0530
4+
Subject: [PATCH 1/2] avformat/hls: always return keyframe if not
5+
AVSEEK_FLAG_ANY
6+
7+
Co-Authored-by: vectronic <hello.vectronic@gmail.com>
8+
---
9+
libavformat/hls.c | 8 +++++---
10+
1 file changed, 5 insertions(+), 3 deletions(-)
11+
12+
diff --git a/libavformat/hls.c b/libavformat/hls.c
13+
index 62473a15ddb5..4d02faa9e49a 100644
14+
--- a/libavformat/hls.c
15+
+++ b/libavformat/hls.c
16+
@@ -2350,8 +2350,10 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
17+
ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE,
18+
tb.den, AV_ROUND_DOWN) -
19+
pls->seek_timestamp;
20+
- if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY ||
21+
- pls->pkt->flags & AV_PKT_FLAG_KEY)) {
22+
+ /* If AVSEEK_FLAG_ANY, keep reading until ts_diff >= 0,
23+
+ * otherwise return the first keyframe encountered */
24+
+ if ((ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY)) ||
25+
+ (!(pls->seek_flags & AVSEEK_FLAG_ANY) && (pls->pkt->flags & AV_PKT_FLAG_KEY))) {
26+
pls->seek_timestamp = AV_NOPTS_VALUE;
27+
break;
28+
}
29+
@@ -2502,7 +2504,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
30+
pb->eof_reached = 0;
31+
/* Clear any buffered data */
32+
pb->buf_end = pb->buf_ptr = pb->buffer;
33+
- /* Reset the pos, to let the mpegts demuxer know we've seeked. */
34+
+ /* Reset the pos, to let the mpegts/mov demuxer know we've seeked. */
35+
pb->pos = 0;
36+
/* Flush the packet queue of the subdemuxer. */
37+
ff_read_frame_flush(pls->ctx);
38+
--
39+
2.47.0
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
From 07045b8245cfc5822005c716db8a84b710579787 Mon Sep 17 00:00:00 2001
2+
From: llyyr <llyyr.public@gmail.com>
3+
Date: Sun, 27 Oct 2024 06:44:51 +0530
4+
Subject: [PATCH 2/2] avformat/mov: handle stream position resets
5+
6+
If the stream position has been reset, clear fragment index, the index
7+
entries and the current sample and search for the next root.
8+
9+
Co-Authored-by: vectronic <hello.vectronic@gmail.com>
10+
---
11+
libavformat/mov.c | 37 +++++++++++++++++++++++++++++++++----
12+
1 file changed, 33 insertions(+), 4 deletions(-)
13+
14+
diff --git a/libavformat/mov.c b/libavformat/mov.c
15+
index 8c3329b81596..78540bec1d11 100644
16+
--- a/libavformat/mov.c
17+
+++ b/libavformat/mov.c
18+
@@ -10612,15 +10612,15 @@ static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
19+
20+
if (index >= 0 && index < mov->frag_index.nb_items)
21+
target = mov->frag_index.item[index].moof_offset;
22+
- if (avio_seek(s->pb, target, SEEK_SET) != target) {
23+
+ if (target >= 0 && avio_seek(s->pb, target, SEEK_SET) != target) {
24+
av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
25+
return AVERROR_INVALIDDATA;
26+
}
27+
28+
mov->next_root_atom = 0;
29+
- if (index < 0 || index >= mov->frag_index.nb_items)
30+
+ if ((index < 0 && target >= 0) || index >= mov->frag_index.nb_items)
31+
index = search_frag_moof_offset(&mov->frag_index, target);
32+
- if (index < mov->frag_index.nb_items &&
33+
+ if (index >= 0 && index < mov->frag_index.nb_items &&
34+
mov->frag_index.item[index].moof_offset == target) {
35+
if (index + 1 < mov->frag_index.nb_items)
36+
mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
37+
@@ -10751,9 +10751,38 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
38+
AVIndexEntry *sample;
39+
AVStream *st = NULL;
40+
int64_t current_index;
41+
- int ret;
42+
+ int ret, i;
43+
mov->fc = s;
44+
retry:
45+
+ if (s->pb->pos == 0) {
46+
+ // Discard current fragment index
47+
+ if (mov->frag_index.allocated_size > 0) {
48+
+ av_freep(&mov->frag_index.item);
49+
+ mov->frag_index.nb_items = 0;
50+
+ mov->frag_index.allocated_size = 0;
51+
+ mov->frag_index.current = -1;
52+
+ mov->frag_index.complete = 0;
53+
+ }
54+
+
55+
+ for (i = 0; i < s->nb_streams; i++) {
56+
+ AVStream *avst = s->streams[i];
57+
+ FFStream *sti = ffstream(avst);
58+
+ MOVStreamContext *msc = avst->priv_data;
59+
+
60+
+ // Clear current sample
61+
+ mov_current_sample_set(msc, 0);
62+
+
63+
+ // Discard current index entries
64+
+ if (sti->index_entries_allocated_size > 0) {
65+
+ av_freep(&sti->index_entries);
66+
+ sti->index_entries_allocated_size = 0;
67+
+ sti->nb_index_entries = 0;
68+
+ }
69+
+ }
70+
+
71+
+ if ((ret = mov_switch_root(s, -1, -1)) < 0)
72+
+ return ret;
73+
+ }
74+
sample = mov_find_next_sample(s, &st);
75+
if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
76+
if (!mov->next_root_atom)
77+
--
78+
2.47.0

0 commit comments

Comments
 (0)