|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import { Input } from '../../src/input.js'; |
| 3 | +import { BufferSource, UrlSource } from '../../src/source.js'; |
| 4 | +import { ALL_FORMATS } from '../../src/input-format.js'; |
| 5 | +import { EncodedPacketSink } from '../../src/media-sink.js'; |
| 6 | +import { EncodedAudioPacketSource } from '../../src/media-source.js'; |
| 7 | +import { Output } from '../../src/output.js'; |
| 8 | +import { StreamTarget, type StreamTargetChunk } from '../../src/target.js'; |
| 9 | +import { AdtsOutputFormat } from '../../src/output-format.js'; |
| 10 | +import { assert } from '../../src/misc.js'; |
| 11 | + |
| 12 | +const createBufferingStreamTarget = () => { |
| 13 | + const written = new Map<number, Uint8Array>(); |
| 14 | + |
| 15 | + const stream = new WritableStream<StreamTargetChunk>({ |
| 16 | + async write(chunk: StreamTargetChunk) { |
| 17 | + written.set(chunk.position, chunk.data.slice()); |
| 18 | + }, |
| 19 | + }); |
| 20 | + |
| 21 | + const toBuffer = () => { |
| 22 | + let maxEnd = 0; |
| 23 | + for (const [offset, data] of written) { |
| 24 | + maxEnd = Math.max(maxEnd, offset + data.byteLength); |
| 25 | + } |
| 26 | + const buffer = new Uint8Array(maxEnd); |
| 27 | + for (const [offset, data] of written) { |
| 28 | + buffer.set(data, offset); |
| 29 | + } |
| 30 | + return buffer; |
| 31 | + }; |
| 32 | + |
| 33 | + return { stream, toBuffer }; |
| 34 | +}; |
| 35 | + |
| 36 | +test('ADTS with metadata over StreamTarget', async () => { |
| 37 | + const target = createBufferingStreamTarget(); |
| 38 | + |
| 39 | + const output = new Output({ |
| 40 | + format: new AdtsOutputFormat(), |
| 41 | + target: new StreamTarget(target.stream), |
| 42 | + }); |
| 43 | + |
| 44 | + output.setMetadataTags({ comment: 'Remotion' }); |
| 45 | + |
| 46 | + const audioSource = new EncodedAudioPacketSource('aac'); |
| 47 | + output.addAudioTrack(audioSource); |
| 48 | + |
| 49 | + await output.start(); |
| 50 | + |
| 51 | + using input = new Input({ |
| 52 | + source: new UrlSource('/sample3.aac'), |
| 53 | + formats: ALL_FORMATS, |
| 54 | + }); |
| 55 | + |
| 56 | + const audioTrack = await input.getPrimaryAudioTrack(); |
| 57 | + assert(audioTrack); |
| 58 | + |
| 59 | + const sink = new EncodedPacketSink(audioTrack); |
| 60 | + |
| 61 | + let isFirst = true; |
| 62 | + for await (const packet of sink.packets()) { |
| 63 | + await audioSource.add(packet, { |
| 64 | + decoderConfig: isFirst ? (await audioTrack.getDecoderConfig())! : undefined, |
| 65 | + }); |
| 66 | + isFirst = false; |
| 67 | + } |
| 68 | + |
| 69 | + await output.finalize(); |
| 70 | + |
| 71 | + const buffer = target.toBuffer(); |
| 72 | + using outputAsInput = new Input({ |
| 73 | + source: new BufferSource(buffer.buffer), |
| 74 | + formats: ALL_FORMATS, |
| 75 | + }); |
| 76 | + |
| 77 | + const readTags = await outputAsInput.getMetadataTags(); |
| 78 | + expect(readTags.comment).toBe('Remotion'); |
| 79 | + |
| 80 | + const outputAudioTrack = await outputAsInput.getPrimaryAudioTrack(); |
| 81 | + assert(outputAudioTrack); |
| 82 | + expect(outputAudioTrack.codec).toBe('aac'); |
| 83 | +}); |
| 84 | + |
| 85 | +// Previously, write handler rejections were silently swallowed and surfaced as |
| 86 | +// "Cannot write to a closing writable stream" instead of the actual error. |
| 87 | +test('StreamTarget write errors surface directly', async () => { |
| 88 | + let writeCount = 0; |
| 89 | + const stream = new WritableStream<StreamTargetChunk>({ |
| 90 | + async write() { |
| 91 | + writeCount++; |
| 92 | + if (writeCount === 2) { |
| 93 | + throw new Error('OPFS write failed'); |
| 94 | + } |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + const output = new Output({ |
| 99 | + format: new AdtsOutputFormat(), |
| 100 | + target: new StreamTarget(stream), |
| 101 | + }); |
| 102 | + |
| 103 | + const audioSource = new EncodedAudioPacketSource('aac'); |
| 104 | + output.addAudioTrack(audioSource); |
| 105 | + |
| 106 | + await output.start(); |
| 107 | + |
| 108 | + using input = new Input({ |
| 109 | + source: new UrlSource('/sample3.aac'), |
| 110 | + formats: ALL_FORMATS, |
| 111 | + }); |
| 112 | + |
| 113 | + const audioTrack = await input.getPrimaryAudioTrack(); |
| 114 | + assert(audioTrack); |
| 115 | + |
| 116 | + const sink = new EncodedPacketSink(audioTrack); |
| 117 | + |
| 118 | + const run = async () => { |
| 119 | + let isFirst = true; |
| 120 | + for await (const packet of sink.packets()) { |
| 121 | + await audioSource.add(packet, { |
| 122 | + decoderConfig: isFirst ? (await audioTrack.getDecoderConfig())! : undefined, |
| 123 | + }); |
| 124 | + isFirst = false; |
| 125 | + } |
| 126 | + await output.finalize(); |
| 127 | + }; |
| 128 | + |
| 129 | + await expect(run()).rejects.toThrow('OPFS write failed'); |
| 130 | +}); |
0 commit comments