Skip to content

🎥 Make videos programmatically with React

assertion that we cannot seek into the negative section

Jonny Burger950dedd70a8e
diff --git a/packages/media-parser/src/containers/webm/seek/get-seeking-byte.ts b/packages/media-parser/src/containers/webm/seek/get-seeking-byte.ts
index 74903a9b2b..4adec5152e 100644
--- a/packages/media-parser/src/containers/webm/seek/get-seeking-byte.ts
+++ b/packages/media-parser/src/containers/webm/seek/get-seeking-byte.ts
@@ -3,6 +3,7 @@ import {Log} from '../../../log';
import type {MediaParserKeyframe} from '../../../options';
import type {WebmSeekingInfo} from '../../../seeking-info';
import type {KeyframesState} from '../../../state/keyframes';
+import type {LazyCuesLoaded} from '../../../state/matroska/lazy-cues-fetch';
import type {WebmState} from '../../../state/matroska/webm';
import type {MediaSectionState} from '../../../state/video-section';
import type {SeekResolution} from '../../../work-on-seek-request';
@@ -42,22 +43,15 @@ const findBiggestCueBeforeTime = ({
const getStartOfClusterToSeekTo = ({
biggestCueBeforeTime,
- webmState,
segmentOffset,
}: {
biggestCueBeforeTime: MatroskaCue | undefined;
- webmState: WebmState;
segmentOffset: number;
}): number | null => {
if (biggestCueBeforeTime) {
return biggestCueBeforeTime.clusterPositionInSegment + segmentOffset;
}
- const hasMediaSectionWithIndex = webmState.getFirstCluster();
- if (hasMediaSectionWithIndex) {
- return hasMediaSectionWithIndex.start;
- }
-
return null;
};
@@ -80,7 +74,41 @@ const findKeyframeBeforeTime = ({
}
}
- return keyframeBeforeTime;
+ return keyframeBeforeTime?.positionInBytes ?? null;
+};
+
+const getByteFromCues = ({
+ cuesResponse,
+ time,
+ info,
+ logLevel,
+}: {
+ cuesResponse: LazyCuesLoaded;
+ time: number;
+ info: WebmSeekingInfo;
+ logLevel: LogLevel;
+}) => {
+ if (!cuesResponse) {
+ Log.trace(logLevel, 'Has no Matroska cues at the moment, cannot use them');
+ return null;
+ }
+
+ const {cues, segmentOffset} = cuesResponse;
+
+ Log.trace(logLevel, 'Has Matroska cues. Will use them to perform a seek.');
+
+ const biggestCueBeforeTime = findBiggestCueBeforeTime({
+ cues,
+ time,
+ track: info.track!,
+ });
+
+ const startOfClusterToSeekTo = getStartOfClusterToSeekTo({
+ biggestCueBeforeTime,
+ segmentOffset,
+ });
+
+ return startOfClusterToSeekTo;
};
export const getSeekingByteFromMatroska = async ({
@@ -105,45 +133,31 @@ export const getSeekingByteFromMatroska = async ({
};
}
- const cuesResponse = await webmState.cues.getLoadedCues();
- if (!cuesResponse) {
- Log.trace(logLevel, 'Has no Matroska Cues at the moment, continuing...');
- return {
- type: 'valid-but-must-wait',
- };
- }
-
- const {cues, segmentOffset} = cuesResponse;
-
- Log.trace(logLevel, 'Has Matroska cues. Will use them to perform a seek.');
-
- const biggestCueBeforeTime = findBiggestCueBeforeTime({
- cues,
+ const cuesResponse = (await webmState.cues.getLoadedCues()) as LazyCuesLoaded;
+ const byteFromObservedKeyframe = findKeyframeBeforeTime({
+ keyframes: keyframes.getKeyframes(),
time,
- track: info.track!,
});
- const keyframeBeforeTime = findKeyframeBeforeTime({
- keyframes: keyframes.getKeyframes(),
+ const byteFromCues = getByteFromCues({
+ cuesResponse,
time,
+ info,
+ logLevel,
});
- const startOfClusterToSeekTo = getStartOfClusterToSeekTo({
- biggestCueBeforeTime,
- webmState,
- segmentOffset,
- });
+ const byteFromFirstMediaSection = webmState.getFirstCluster()?.start ?? null;
+
+ const seekPossibilities = [
+ byteFromCues,
+ byteFromObservedKeyframe,
+ byteFromFirstMediaSection,
+ ].filter((n) => n !== null);
const byteToSeekTo =
- keyframeBeforeTime && startOfClusterToSeekTo
- ? Math.max(startOfClusterToSeekTo, keyframeBeforeTime.positionInBytes)
- : startOfClusterToSeekTo
- ? startOfClusterToSeekTo
- : keyframeBeforeTime
- ? keyframeBeforeTime.positionInBytes
- : null;
-
- if (!byteToSeekTo) {
+ seekPossibilities.length === 0 ? null : Math.max(...seekPossibilities);
+
+ if (byteToSeekTo === null) {
// dont know what to do
return {
type: 'invalid',
diff --git a/packages/media-parser/src/state/matroska/lazy-cues-fetch.ts b/packages/media-parser/src/state/matroska/lazy-cues-fetch.ts
index b58bf69c28..c3e2550e00 100644
--- a/packages/media-parser/src/state/matroska/lazy-cues-fetch.ts
+++ b/packages/media-parser/src/state/matroska/lazy-cues-fetch.ts
@@ -72,3 +72,8 @@ export const lazyCuesFetch = ({
getLoadedCues,
};
};
+
+export type LazyCuesFetch = ReturnType<typeof lazyCuesFetch>;
+export type LazyCuesLoaded = Awaited<
+ ReturnType<LazyCuesFetch['getLoadedCues']>
+>;
diff --git a/packages/media-parser/src/test/seeking/basic-mp4.test.ts b/packages/media-parser/src/test/seeking/basic-mp4.test.ts
index 5c03a9b717..91993540ce 100644
--- a/packages/media-parser/src/test/seeking/basic-mp4.test.ts
+++ b/packages/media-parser/src/test/seeking/basic-mp4.test.ts
@@ -36,3 +36,29 @@ test('should process a basic seek request', async () => {
expect(timeInSeconds).toBe(10.5);
}
});
+
+test('should not be able to seek into a negative time', async () => {
+ const controller = mediaParserController();
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: -1,
+ });
+
+ try {
+ await parseMedia({
+ src: exampleVideos.bigBuckBunny,
+ controller,
+ reader: nodeReader,
+ acknowledgeRemotionLicense: true,
+ fields: {
+ durationInSeconds: true,
+ },
+ });
+
+ throw new Error('should not complete');
+ } catch (err) {
+ expect((err as Error).message).toBe(
+ 'Cannot seek to a negative time: {"type":"keyframe-before-time","timeInSeconds":-1}',
+ );
+ }
+});
diff --git a/packages/media-parser/src/test/seeking/webm-cues.test.ts b/packages/media-parser/src/test/seeking/webm-cues.test.ts
index 961c9abee6..a2e7edad40 100644
--- a/packages/media-parser/src/test/seeking/webm-cues.test.ts
+++ b/packages/media-parser/src/test/seeking/webm-cues.test.ts
@@ -123,4 +123,71 @@ test('should use them for seeking', async () => {
]);
});
-test.todo('should not be able to seek into a negative time');
+test('should work if there are no cues', async () => {
+ const controller = mediaParserController();
+
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: 10,
+ });
+
+ let samples = 0;
+
+ try {
+ await parseMedia({
+ src: exampleVideos.unevendim,
+ acknowledgeRemotionLicense: true,
+ reader: nodeReader,
+ controller,
+ onVideoTrack: () => {
+ return (s) => {
+ if (samples === 213) {
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: 5,
+ });
+ expect(s.timestamp / s.timescale).toBe(7.227);
+ }
+
+ if (samples === 214) {
+ expect(s.timestamp / s.timescale).toBe(3.408);
+ controller._experimentalSeek({
+ type: 'keyframe-before-time',
+ timeInSeconds: 0,
+ });
+ }
+
+ if (samples === 215) {
+ expect(s.timestamp / s.timescale).toBe(0);
+ controller.abort();
+ }
+
+ samples++;
+ };
+ },
+ });
+ } catch (error) {
+ if (!hasBeenAborted(error)) {
+ throw error;
+ }
+ }
+
+ const seeks = controller._internals.performedSeeksSignal.getPerformedSeeks();
+ expect(seeks).toEqual([
+ {
+ from: 157,
+ to: 145,
+ type: 'user-initiated',
+ },
+ {
+ from: 658442,
+ to: 311967,
+ type: 'user-initiated',
+ },
+ {
+ from: 400111,
+ to: 145,
+ type: 'user-initiated',
+ },
+ ]);
+});
diff --git a/packages/media-parser/src/work-on-seek-request.ts b/packages/media-parser/src/work-on-seek-request.ts
index 9afc3088c6..ccd397697b 100644
--- a/packages/media-parser/src/work-on-seek-request.ts
+++ b/packages/media-parser/src/work-on-seek-request.ts
@@ -55,6 +55,12 @@ const turnSeekIntoByte = async ({
}
if (seek.type === 'keyframe-before-time') {
+ if (seek.timeInSeconds < 0) {
+ throw new Error(
+ `Cannot seek to a negative time: ${JSON.stringify(seek)}`,
+ );
+ }
+
const seekingInfo = getSeekingInfo({
structureState,
mp4HeaderSegment,
@@ -63,6 +69,7 @@ const turnSeekIntoByte = async ({
transportStream,
tracksState,
});
+
if (!seekingInfo) {
Log.trace(logLevel, 'No seeking info, cannot seek yet');
return {