Various small fixes and adjustments

This commit is contained in:
Vanilagy
2026-07-09 14:35:25 +02:00
parent 9ced2853fa
commit f0a2bbae37
7 changed files with 58 additions and 34 deletions
+11 -13
View File
@@ -2,7 +2,6 @@ import { expect, test } from 'vitest';
import { Input } from '../../src/input.js';
import { ALL_FORMATS } from '../../src/input-format.js';
import { AudioSampleSource } from '../../src/media-source.js';
import { AudioSampleCursor } from '../../src/cursors.js';
import { assert } from '../../src/misc.js';
import { Output } from '../../src/output.js';
import { FlacOutputFormat } from '../../src/output-format.js';
@@ -10,6 +9,7 @@ import { AudioSample } from '../../src/sample.js';
import { BufferSource } from '../../src/source.js';
import { BufferTarget } from '../../src/target.js';
import { registerFlacEncoder } from '@mediabunny/flac-encoder';
import { EncodedPacket, PacketReader } from '../../src/packet.js';
test('FLAC encoder, 24-bit', async () => {
registerFlacEncoder();
@@ -19,15 +19,14 @@ test('FLAC encoder, 24-bit', async () => {
const durationSeconds = 2;
const data = createF32SineWave(sampleRate, channels, durationSeconds);
using sample = await encodeAndDecodeFirstSample(new AudioSample({
const packet = await encodeSample(new AudioSample({
data,
format: 'f32',
numberOfChannels: channels,
sampleRate,
timestamp: 0,
}));
expect(sample.format).toBe('s32');
expect(getBitDepthFromFlacPacket(packet!)).toBe(0b110); // 0b110 = 24 bit
});
test('FLAC encoder, 16-bit', async () => {
@@ -38,15 +37,14 @@ test('FLAC encoder, 16-bit', async () => {
const durationSeconds = 2;
const data = createS16SineWave(sampleRate, channels, durationSeconds);
using sample = await encodeAndDecodeFirstSample(new AudioSample({
const packet = await encodeSample(new AudioSample({
data,
format: 's16',
numberOfChannels: channels,
sampleRate,
timestamp: 0,
}));
expect(sample.format).toBe('s16');
expect(getBitDepthFromFlacPacket(packet!)).toBe(0b100); // 0b100 = 16 bit
});
const createF32SineWave = (sampleRate: number, channels: number, durationSeconds: number) => {
@@ -77,7 +75,7 @@ const createS16SineWave = (sampleRate: number, channels: number, durationSeconds
return data;
};
const encodeAndDecodeFirstSample = async (audioSample: AudioSample) => {
const encodeSample = async (audioSample: AudioSample) => {
const output = new Output({
format: new FlacOutputFormat(),
target: new BufferTarget(),
@@ -100,10 +98,10 @@ const encodeAndDecodeFirstSample = async (audioSample: AudioSample) => {
const track = await input.getPrimaryAudioTrack();
assert(track);
const cursor = new AudioSampleCursor(track, { autoClose: false });
const sample = await cursor.seekToFirst();
await cursor.close();
assert(sample);
const reader = new PacketReader(track);
return reader.getFirst();
};
return sample;
const getBitDepthFromFlacPacket = (packet: EncodedPacket) => {
return (packet.data[3]! & 0b1110) >> 1;
};
+9 -5
View File
@@ -373,7 +373,7 @@ test('Sample cursor sample reuse', async () => {
expect(sample2!.closed).toBe(true);
const cursor2 = new VideoSampleCursor(videoTrack, {
autoClose: false,
closeSamples: false,
});
const sample3 = await cursor2.seekToFirst();
@@ -398,7 +398,7 @@ test('Sample cursor sample reuse', async () => {
count = 0;
const cursor4 = new VideoSampleCursor(videoTrack, {
autoClose: false,
closeSamples: false,
transform: sample => (sample.close(), count++),
});
@@ -519,7 +519,7 @@ test('Decoder pump error handling & reset', async () => {
});
const videoTrack = (await input.getPrimaryVideoTrack())!;
const cursor = new VideoSampleCursor(videoTrack);
await using cursor = new VideoSampleCursor(videoTrack);
cursor._debug.enabled = true;
cursor._debug.throwInPump = true;
@@ -859,7 +859,7 @@ test('Command queuing', async () => {
await promiseAllEnsureOrder(commands7);
const cursor8 = new VideoSampleCursor(videoTrack, { autoClose: false });
const cursor8 = new VideoSampleCursor(videoTrack, { closeSamples: false });
const firstSample = await cursor8.seekToFirst();
firstSample!.close();
@@ -878,7 +878,7 @@ test('Command queuing', async () => {
await cursor8.close();
const cursor9 = new VideoSampleCursor(videoTrack, { autoClose: false });
const cursor9 = new VideoSampleCursor(videoTrack, { closeSamples: false });
const commands9 = [
cursor9.seekToFirst(),
@@ -929,6 +929,10 @@ test('Automatic cursor disposal', async () => {
input.dispose();
expect(cursor.closed).toBe(true);
// Make sure the close is actually complete
using lock = cursor._mutex.lock();
if (lock.pending) await lock.ready;
});
test('Video with stubborn first sample emit', async () => {