Skip to content

🎥 Make videos programmatically with React

seeking in fragmented mp4

Jonny Burger77411ae4dc69
diff --git a/packages/media-parser/src/containers/iso-base-media/get-sample-position-bounds.ts b/packages/media-parser/src/containers/iso-base-media/get-sample-position-bounds.ts
index 328ee2dffe..90ada8826d 100644
--- a/packages/media-parser/src/containers/iso-base-media/get-sample-position-bounds.ts
+++ b/packages/media-parser/src/containers/iso-base-media/get-sample-position-bounds.ts
@@ -8,12 +8,15 @@ export const getSamplePositionBounds = (
let max = -Infinity;
for (const samplePosition of samplePositions) {
- if (samplePosition.cts < min) {
- min = samplePosition.cts;
+ const timestampMin = Math.min(samplePosition.cts, samplePosition.dts);
+ const timestampMax = Math.max(samplePosition.cts, samplePosition.dts);
+
+ if (timestampMin < min) {
+ min = timestampMin;
}
- if (samplePosition.cts > max) {
- max = samplePosition.cts;
+ if (timestampMax > max) {
+ max = timestampMax;
}
}
diff --git a/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts b/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts
index 39f6c2947e..dece881300 100644
--- a/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts
+++ b/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts
@@ -88,28 +88,28 @@ export const getSeekingByteFromIsoBaseMedia = ({
isByteInVideoSection({
position: currentPosition,
videoSections: info.videoSections,
- }) === 'in-section'
+ }) !== 'in-section'
) {
- Log.trace(
- logLevel,
- 'Fragmented MP4 - Inside the wrong video section, skipping to the end of the section',
- );
- const videoSection = getCurrentVideoSection({
- offset: currentPosition,
- videoSections: info.videoSections,
- });
- if (!videoSection) {
- throw new Error('No video section defined');
- }
-
return {
- type: 'intermediary-seek',
- byte: videoSection.start + videoSection.size,
+ type: 'valid-but-must-wait',
};
}
+ Log.trace(
+ logLevel,
+ 'Fragmented MP4 - Inside the wrong video section, skipping to the end of the section',
+ );
+ const videoSection = getCurrentVideoSection({
+ offset: currentPosition,
+ videoSections: info.videoSections,
+ });
+ if (!videoSection) {
+ throw new Error('No video section defined');
+ }
+
return {
- type: 'valid-but-must-wait',
+ type: 'intermediary-seek',
+ byte: videoSection.start + videoSection.size,
};
}
diff --git a/packages/media-parser/src/internal-parse-media.ts b/packages/media-parser/src/internal-parse-media.ts
index 23071c6bdd..c79b8d6a32 100644
--- a/packages/media-parser/src/internal-parse-media.ts
+++ b/packages/media-parser/src/internal-parse-media.ts
@@ -132,5 +132,9 @@ export const internalParseMedia: InternalParseMedia = async function <
throw state.errored;
}
+ if (state.controller._internals.seekSignal.getSeek()) {
+ throw new Error('Should not finish while a seek is pending');
+ }
+
return state.returnValue as ParseMediaResult<F>;
};
diff --git a/packages/media-parser/src/state/sample-callbacks.ts b/packages/media-parser/src/state/sample-callbacks.ts
index 093bd3bbf4..7c0c9f660a 100644
--- a/packages/media-parser/src/state/sample-callbacks.ts
+++ b/packages/media-parser/src/state/sample-callbacks.ts
@@ -92,7 +92,7 @@ export const sampleCallback = ({
// If we emit samples with data length 0, Chrome will fail
if (callback) {
if (seekSignal.getSeek()) {
- Log.verbose(
+ Log.trace(
logLevel,
'Not emitting sample because seek is processing',
);
@@ -125,7 +125,7 @@ export const sampleCallback = ({
// If we emit samples with data 0, Chrome will fail
if (callback) {
if (seekSignal.getSeek()) {
- Log.verbose(
+ Log.trace(
logLevel,
'Not emitting sample because seek is processing',
);
diff --git a/packages/media-parser/src/test/seeking/seek-moof.test.ts b/packages/media-parser/src/test/seeking/seek-moof.test.ts
index dfb3648d48..39df320737 100644
--- a/packages/media-parser/src/test/seeking/seek-moof.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-moof.test.ts
@@ -14,6 +14,7 @@ test('seek moof', async () => {
type: 'keyframe-before-time-in-seconds',
time: 20,
});
+ let samples = 0;
try {
await parseMedia({
@@ -22,18 +23,35 @@ test('seek moof', async () => {
acknowledgeRemotionLicense: true,
controller,
fields: {
- slowKeyframes: true,
internalStats: true,
},
onVideoTrack: () => {
return (sample) => {
- if (sample.cts < 10_400_000) {
- throw new Error('did not seek correctly');
+ samples++;
+
+ if (samples > 3) {
+ throw new Error('should not reach here');
+ }
+
+ if (sample.cts === 19533333.333333332) {
+ expect(sample.type).toBe('key');
+
+ controller._experimentalSeek({
+ type: 'keyframe-before-time-in-seconds',
+ time: 0,
+ });
+ }
+
+ if (sample.dts === 0) {
+ controller._experimentalSeek({
+ type: 'keyframe-before-time-in-seconds',
+ time: 10,
+ });
}
- expect(sample.cts).toBe(19533333.333333332);
- expect(sample.type).toBe('key');
- controller.abort();
+ if (sample.dts === 9700000) {
+ controller.abort();
+ }
};
},
});
@@ -153,6 +171,16 @@ test('seek moof', async () => {
to: 1214780,
type: 'internal',
},
+ {
+ from: 1261511,
+ to: 2052,
+ type: 'user-initiated',
+ },
+ {
+ from: 9349,
+ to: 802836,
+ type: 'user-initiated',
+ },
]);
}
});