Skip to content

🎥 Make videos programmatically with React

almost there

Jonny Burger59000b5ea437
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
new file mode 100644
index 0000000000..bff9526c69
--- /dev/null
+++ b/packages/media-parser/src/containers/m3u/first-sample-in-m3u-chunk.ts
@@ -0,0 +1,41 @@
+import type {MediaParserController} from '../../controller/media-parser-controller';
+import type {M3uState} from '../../state/m3u-state';
+import type {AudioOrVideoSample} from '../../webcodec-sample-types';
+
+export const considerSeekBasedOnChunk = ({
+ sample,
+ controller,
+ callback,
+ m3uState,
+ playlistUrl,
+}: {
+ sample: AudioOrVideoSample;
+ callback: (sample: AudioOrVideoSample) => void | Promise<void>;
+ controller: MediaParserController;
+ playlistUrl: string;
+ m3uState: M3uState;
+}) => {
+ const pendingSeek = m3uState.getSeekToSecondsToProcess(playlistUrl);
+ // If there is not even a seek to consider, just call the callback
+ if (pendingSeek === null) {
+ callback(sample);
+ return;
+ }
+
+ const timestamp = Math.min(
+ sample.dts / sample.timescale,
+ sample.cts / sample.timescale,
+ );
+
+ // Already too far, now we should go to the previous chunk
+ if (timestamp > pendingSeek) {
+ throw new Error('too far');
+ }
+
+ // We are good, we have not gone too far! Don't emit sample and seek and clear pending seek
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: pendingSeek,
+ });
+ m3uState.setSeekToSecondsToProcess(playlistUrl, null);
+};
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 5192e4b2db..9a2a4d3e30 100644
--- a/packages/media-parser/src/containers/m3u/process-m3u-chunk.ts
+++ b/packages/media-parser/src/containers/m3u/process-m3u-chunk.ts
@@ -7,11 +7,16 @@ import {type M3uRun} from '../../state/m3u-state';
import type {ParserState} from '../../state/parser-state';
import type {OnAudioSample, OnVideoSample} from '../../webcodec-sample-types';
import {withResolvers} from '../../with-resolvers';
+import {considerSeekBasedOnChunk} from './first-sample-in-m3u-chunk';
import {getChunks} from './get-chunks';
import {getPlaylist} from './get-playlist';
import {getChunkToSeekTo} from './seek/get-chunk-to-seek-to';
import type {M3uStructure} from './types';
+export type PendingSeek = {
+ pending: number | null;
+};
+
export const processM3uChunk = ({
playlistUrl,
state,
@@ -27,7 +32,6 @@ export const processM3uChunk = ({
}) => {
const {promise, reject, resolve} = withResolvers<void>();
- console.log({playlistUrl, audioDone});
const onGlobalAudioTrack = audioDone
? null
: async (track: AudioTrack): Promise<OnAudioSample | null> => {
@@ -107,15 +111,15 @@ export const processM3uChunk = ({
const pausableIterator = async () => {
const playlist = getPlaylist(structure, playlistUrl);
const chunks = getChunks(playlist);
- let seekToSecondsToProcess =
+ const seekToSecondsToProcess =
state.m3u.getSeekToSecondsToProcess(playlistUrl);
let chunkIndex = null;
+
if (seekToSecondsToProcess !== null) {
chunkIndex = getChunkToSeekTo({
chunks,
seekToSecondsToProcess,
});
- state.m3u.setSeekToSecondsToProcess(playlistUrl, null);
}
const currentPromise = {
@@ -146,13 +150,6 @@ export const processM3uChunk = ({
currentPromise.rejector = reject;
const childController = mediaParserController();
- if (seekToSecondsToProcess !== null && !chunk.isHeader) {
- childController._experimentalSeek({
- type: 'keyframe-before-time',
- timeInSeconds: seekToSecondsToProcess,
- });
- seekToSecondsToProcess = null;
- }
const forwarded = forwardMediaParserControllerPauseResume({
childController,
@@ -236,11 +233,29 @@ export const processM3uChunk = ({
state.m3u.setHasEmittedAudioTrack(playlistUrl, callback);
return (sample) => {
- return callback(sample);
+ considerSeekBasedOnChunk({
+ sample,
+ callback,
+ controller: childController,
+ m3uState: state.m3u,
+ playlistUrl,
+ });
};
}
- return callbackOrFalse;
+ if (callbackOrFalse === null) {
+ return null;
+ }
+
+ return (sample) => {
+ considerSeekBasedOnChunk({
+ sample,
+ m3uState: state.m3u,
+ playlistUrl,
+ callback: callbackOrFalse,
+ controller: childController,
+ });
+ };
},
onVideoTrack:
onGlobalVideoTrack === null
@@ -262,11 +277,29 @@ export const processM3uChunk = ({
state.m3u.setHasEmittedVideoTrack(playlistUrl, callback);
return (sample) => {
- return callback(sample);
+ considerSeekBasedOnChunk({
+ sample,
+ m3uState: state.m3u,
+ playlistUrl,
+ callback,
+ controller: childController,
+ });
};
}
- return callbackOrFalse;
+ if (callbackOrFalse === null) {
+ return null;
+ }
+
+ return (sample) => {
+ considerSeekBasedOnChunk({
+ sample,
+ m3uState: state.m3u,
+ playlistUrl,
+ callback: callbackOrFalse,
+ controller: childController,
+ });
+ };
},
reader: state.readerInterface,
mp4HeaderSegment,
diff --git a/packages/media-parser/src/test/seeking/seek-m3u.test.ts b/packages/media-parser/src/test/seeking/seek-m3u.test.ts
index b4371203c8..ab8db4c729 100644
--- a/packages/media-parser/src/test/seeking/seek-m3u.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-m3u.test.ts
@@ -18,7 +18,6 @@ test.only('seek m3u, only video', async () => {
acknowledgeRemotionLicense: true,
controller,
reader: nodeReader,
- logLevel: 'trace',
onVideoTrack: () => {
return (sample) => {
console.log(sample.dts);
@@ -31,6 +30,8 @@ test.only('seek m3u, only video', async () => {
}
if (samples === 1) {
+ expect(sample.dts / sample.timescale).toBe(1.99);
+
controller.abort();
}