Skip to content

🎥 Make videos programmatically with React

seek in m3u also works

Jonny Burger4998c95cb287
diff --git a/packages/media-parser/src/containers/m3u/first-sample-in-m3u-chunk.ts b/packages/media-parser/src/containers/m3u/first-sample-in-m3u-chunk.ts
index 42322aa3cb..1f8415f223 100644
--- a/packages/media-parser/src/containers/m3u/first-sample-in-m3u-chunk.ts
+++ b/packages/media-parser/src/containers/m3u/first-sample-in-m3u-chunk.ts
@@ -10,6 +10,7 @@ export const considerSeekBasedOnChunk = async ({
m3uState,
playlistUrl,
subtractChunks,
+ chunkIndex,
}: {
sample: AudioOrVideoSample;
callback: (sample: AudioOrVideoSample) => void | Promise<void>;
@@ -18,6 +19,7 @@ export const considerSeekBasedOnChunk = async ({
playlistUrl: string;
m3uState: M3uState;
subtractChunks: number;
+ chunkIndex: number | null;
}) => {
const pendingSeek = m3uState.getSeekToSecondsToProcess(playlistUrl);
// If there is not even a seek to consider, just call the callback
@@ -32,7 +34,11 @@ export const considerSeekBasedOnChunk = async ({
);
// Already too far, now we should go to the previous chunk
- if (timestamp > pendingSeek.targetTime) {
+ if (
+ timestamp > pendingSeek.targetTime &&
+ chunkIndex !== null &&
+ chunkIndex > 0
+ ) {
m3uState.setNextSeekShouldSubtractChunks(playlistUrl, subtractChunks + 1);
parentController._experimentalSeek({
type: 'keyframe-before-time',
diff --git a/packages/media-parser/src/containers/m3u/process-m3u-chunk.ts b/packages/media-parser/src/containers/m3u/process-m3u-chunk.ts
index 01f82c012b..56058c37ae 100644
--- a/packages/media-parser/src/containers/m3u/process-m3u-chunk.ts
+++ b/packages/media-parser/src/containers/m3u/process-m3u-chunk.ts
@@ -118,11 +118,13 @@ export const processM3uChunk = ({
let chunkIndex = null;
if (seekToSecondsToProcess !== null) {
- chunkIndex =
+ chunkIndex = Math.max(
+ 0,
getChunkToSeekTo({
chunks,
seekToSecondsToProcess: seekToSecondsToProcess.targetTime,
- }) - chunksToSubtract;
+ }) - chunksToSubtract,
+ );
}
const currentPromise = {
@@ -235,8 +237,8 @@ export const processM3uChunk = ({
}
state.m3u.setHasEmittedAudioTrack(playlistUrl, callback);
- return (sample) => {
- considerSeekBasedOnChunk({
+ return async (sample) => {
+ await considerSeekBasedOnChunk({
sample,
callback,
parentController: state.controller,
@@ -244,6 +246,7 @@ export const processM3uChunk = ({
m3uState: state.m3u,
playlistUrl,
subtractChunks: chunksToSubtract,
+ chunkIndex,
});
};
}
@@ -252,8 +255,8 @@ export const processM3uChunk = ({
return null;
}
- return (sample) => {
- considerSeekBasedOnChunk({
+ return async (sample) => {
+ await considerSeekBasedOnChunk({
sample,
m3uState: state.m3u,
playlistUrl,
@@ -261,6 +264,7 @@ export const processM3uChunk = ({
parentController: state.controller,
childController,
subtractChunks: chunksToSubtract,
+ chunkIndex,
});
};
},
@@ -283,8 +287,8 @@ export const processM3uChunk = ({
}
state.m3u.setHasEmittedVideoTrack(playlistUrl, callback);
- return (sample) => {
- considerSeekBasedOnChunk({
+ return async (sample) => {
+ await considerSeekBasedOnChunk({
sample,
m3uState: state.m3u,
playlistUrl,
@@ -292,6 +296,7 @@ export const processM3uChunk = ({
parentController: state.controller,
childController,
subtractChunks: chunksToSubtract,
+ chunkIndex,
});
};
}
@@ -300,8 +305,8 @@ export const processM3uChunk = ({
return null;
}
- return (sample) => {
- considerSeekBasedOnChunk({
+ return async (sample) => {
+ await considerSeekBasedOnChunk({
sample,
m3uState: state.m3u,
playlistUrl,
@@ -309,6 +314,7 @@ export const processM3uChunk = ({
parentController: state.controller,
childController,
subtractChunks: chunksToSubtract,
+ chunkIndex,
});
};
},
diff --git a/packages/media-parser/src/containers/m3u/seek/get-chunk-to-seek-to.ts b/packages/media-parser/src/containers/m3u/seek/get-chunk-to-seek-to.ts
index f57424c990..a949168635 100644
--- a/packages/media-parser/src/containers/m3u/seek/get-chunk-to-seek-to.ts
+++ b/packages/media-parser/src/containers/m3u/seek/get-chunk-to-seek-to.ts
@@ -16,5 +16,5 @@ export const getChunkToSeekTo = ({
duration += chunks[i].duration;
}
- return chunks.length - 1;
+ return Math.max(0, chunks.length - 1);
};
diff --git a/packages/media-parser/src/test/seeking/seek-m3u-ts.test.ts b/packages/media-parser/src/test/seeking/seek-m3u-ts.test.ts
new file mode 100644
index 0000000000..ca8120bd3b
--- /dev/null
+++ b/packages/media-parser/src/test/seeking/seek-m3u-ts.test.ts
@@ -0,0 +1,77 @@
+import {expect, test} from 'bun:test';
+import {mediaParserController} from '../../controller/media-parser-controller';
+import {hasBeenAborted} from '../../errors';
+import {parseMedia} from '../../parse-media';
+
+test('seek m3u, ts segments', async () => {
+ const controller = mediaParserController();
+
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: 10.2,
+ });
+
+ let samples = 0;
+
+ // TODO: Annoying special case, the first sample has timestamp of 10 seconds
+ // should we zero out the first sample? would require a new
+
+ // TODO: Document this
+
+ try {
+ await parseMedia({
+ src: 'https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8',
+ acknowledgeRemotionLicense: true,
+ controller,
+ onVideoTrack: () => {
+ return (sample) => {
+ if (samples === 0) {
+ expect(sample.dts / sample.timescale).toBe(10);
+ expect(sample.type).toBe('key');
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: 39,
+ });
+ }
+
+ if (samples === 1) {
+ expect(sample.dts / sample.timescale).toBe(30);
+ expect(sample.type).toBe('key');
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: 5,
+ });
+ }
+
+ if (samples === 2) {
+ expect(sample.dts / sample.timescale).toBe(10);
+ expect(sample.type).toBe('key');
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: 25,
+ });
+ }
+
+ if (samples === 3) {
+ expect(sample.dts / sample.timescale).toBe(20);
+ expect(sample.type).toBe('key');
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: 10000,
+ });
+ }
+
+ if (samples === 4) {
+ expect(sample.dts / sample.timescale).toBe(640);
+ expect(sample.type).toBe('key');
+ controller.abort();
+ }
+
+ samples++;
+ };
+ },
+ });
+ } catch (err) {
+ expect(hasBeenAborted(err)).toBe(true);
+ }
+});