🎥 Make videos programmatically with React
getTargetTime
Jonny Burger13b7b8f9938f
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.tsindex ff587b4bef..7090445aa7 100644--- a/packages/media/src/media-player.ts+++ b/packages/media/src/media-player.ts@@ -321,6 +321,7 @@ export class MediaPlayer {getIsPlaying: () => this.playing,scheduleAudioNode: this.scheduleAudioNode,debugAudioScheduling: this.debugAudioScheduling,+ getTargetTime: this.getTargetTime,}): Promise.resolve(),this.videoIteratorManager@@ -407,6 +408,7 @@ export class MediaPlayer {getIsPlaying: () => this.playing,scheduleAudioNode: this.scheduleAudioNode,debugAudioScheduling: this.debugAudioScheduling,+ getTargetTime: this.getTargetTime,}): null,]);@@ -643,10 +645,7 @@ export class MediaPlayer {this.input.dispose();}- private scheduleAudioNode = (- node: AudioBufferSourceNode,- mediaTimestamp: number,- ): ScheduleAudioNodeResult => {+ private getTargetTime = (mediaTimestamp: number): number | null => {if (!this.sharedAudioContext) {throw new Error('Shared audio context not found');}@@ -657,16 +656,37 @@ export class MediaPlayer {const globalTime =(currentTime - this.sharedAudioContext.audioSyncAnchor.value) *this.globalPlaybackRate;+const timeInSeconds = globalTime - this.sequenceOffset;+const localTime = this.getTrimmedTime(timeInSeconds);if (localTime === null) {- return {type: 'not-started'};+ return null;}const targetTime =(mediaTimestamp - localTime) /(this.playbackRate * this.globalPlaybackRate);+ return targetTime;+ };++ private scheduleAudioNode = (+ node: AudioBufferSourceNode,+ mediaTimestamp: number,+ ): ScheduleAudioNodeResult => {+ if (!this.sharedAudioContext) {+ throw new Error('Shared audio context not found');+ }++ const {audioContext} = this.sharedAudioContext;+ const {currentTime} = audioContext;++ const targetTime = this.getTargetTime(mediaTimestamp);+ if (targetTime === null) {+ return {type: 'not-started'};+ }+return this.sharedAudioContext.scheduleAudioNode({node,mediaTimestamp,diff --git a/packages/media/src/test/audio-iterator.test.ts b/packages/media/src/test/audio-iterator.test.tsindex a2557bcc3a..81944280e7 100644--- a/packages/media/src/test/audio-iterator.test.ts+++ b/packages/media/src/test/audio-iterator.test.ts@@ -75,6 +75,7 @@ test('media player should work', async () => {nonce: makeNonceManager().createAsyncOperation(),playbackRate,debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});const chunksAt996 = manager@@ -91,6 +92,7 @@ test('media player should work', async () => {nonce: makeNonceManager().createAsyncOperation(),playbackRate,debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});await manager.seek({@@ -100,6 +102,7 @@ test('media player should work', async () => {nonce: makeNonceManager().createAsyncOperation(),playbackRate,debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});const chunksAtEnd = manager@@ -138,6 +141,7 @@ test('should not create too many iterators when the audio ends', async () => {nonce: makeNonceManager().createAsyncOperation(),playbackRate,debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});await manager.seek({newTime: 9.98,@@ -146,14 +150,7 @@ test('should not create too many iterators when the audio ends', async () => {nonce: makeNonceManager().createAsyncOperation(),playbackRate,debugAudioScheduling: false,- });- await manager.seek({- newTime: 9.99,- scheduleAudioNode,- getIsPlaying,- nonce: makeNonceManager().createAsyncOperation(),- playbackRate,- debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});const created = manager.getAudioIteratorsCreated();@@ -190,6 +187,7 @@ test('should create more iterators when seeking ', async () => {nonce: makeNonceManager().createAsyncOperation(),playbackRate,debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});const chunksAt0 = manager.getAudioBufferIterator()@@ -205,6 +203,7 @@ test('should create more iterators when seeking ', async () => {nonce: makeNonceManager().createAsyncOperation(),playbackRate,debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});const created = manager.getAudioIteratorsCreated();@@ -288,6 +287,7 @@ test('should not schedule duplicate chunks with playbackRate=0.5', async () => {nonce: makeNonceManager().createAsyncOperation(),playbackRate,debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});}@@ -361,6 +361,7 @@ test('should not decode + schedule audio chunks beyond the end time', async () =nonce: makeNonceManager().createAsyncOperation(),playbackRate: 1,debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});}diff --git a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts b/packages/media/src/test/video-with-no-frames-in-beginning.test.tsindex 9c401d0e79..e2ad26b514 100644--- a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts+++ b/packages/media/src/test/video-with-no-frames-in-beginning.test.ts@@ -118,6 +118,7 @@ test('same goes for audio', async () => {scheduledTime: 0,}),debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});await manager.seek({@@ -130,6 +131,7 @@ test('same goes for audio', async () => {scheduledTime: 0,}),debugAudioScheduling: false,+ getTargetTime: (mediaTimestamp: number) => mediaTimestamp,});const iterators = manager.getAudioIteratorsCreated();