mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-29 11:53:52 +02:00
Migrate fragmented MP4 reading to new packet system, add tests for it, add packet internal data, and various other fixes prob
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import { Input } from '../../src/input.js';
|
||||
import { BufferSource, FilePathSource } from '../../src/source.js';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import { ALL_FORMATS, MP3 } from '../../src/input-format.js';
|
||||
import { PacketReader } from '../../src/cursors.js';
|
||||
import { InputAudioTrack } from '../../src/input-track.js';
|
||||
import { assert } from '../../src/misc.js';
|
||||
import { EncodedPacket } from '../../src/packet.js';
|
||||
|
||||
const __dirname = new URL('.', import.meta.url).pathname;
|
||||
|
||||
test('MP3 demuxing', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(path.join(__dirname, '../public/AudacityTest1.mp3')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getFormat()).toBe(MP3);
|
||||
expect(await input.getMimeType()).toBe('audio/mpeg');
|
||||
|
||||
const tracks = await input.getTracks();
|
||||
expect(tracks).toHaveLength(1);
|
||||
const audioTrack = tracks[0] as InputAudioTrack;
|
||||
expect(audioTrack).toBeInstanceOf(InputAudioTrack);
|
||||
|
||||
expect(audioTrack.numberOfChannels).toBe(1);
|
||||
expect(audioTrack.sampleRate).toBe(44100);
|
||||
|
||||
const decoderConfig = await audioTrack.getDecoderConfig();
|
||||
expect(decoderConfig!.codec).toBe('mp3');
|
||||
expect(decoderConfig!.numberOfChannels).toBe(audioTrack.numberOfChannels);
|
||||
expect(decoderConfig!.sampleRate).toBe(audioTrack.sampleRate);
|
||||
expect(decoderConfig!.description).toBeUndefined();
|
||||
|
||||
const reader = new PacketReader(audioTrack);
|
||||
const first = await reader.readFirst();
|
||||
expect(first).not.toBe(null);
|
||||
expect(first!.timestamp).toBe(0);
|
||||
expect(first!.data[0]).toBe(255);
|
||||
|
||||
const next = await reader.readNext(first!);
|
||||
expect(next).not.toBe(null);
|
||||
expect(next!.timestamp).toBeCloseTo(first!.timestamp + first!.duration);
|
||||
expect(next!.sequenceNumber).toBeGreaterThan(first!.sequenceNumber);
|
||||
|
||||
const nextKey = await reader.readNextKey(first!);
|
||||
expect(nextKey).not.toBe(null);
|
||||
expect(nextKey!.timestamp).toBe(next!.timestamp);
|
||||
|
||||
const seeked = await reader.readAt(1);
|
||||
expect(seeked).not.toBe(null);
|
||||
expect(seeked!.timestamp).toBeGreaterThan(0.9);
|
||||
expect(seeked!.timestamp).toBeLessThanOrEqual(1);
|
||||
expect(seeked!.sequenceNumber).toBeGreaterThan(next!.sequenceNumber);
|
||||
|
||||
const seekedKey = await reader.readKeyAt(1);
|
||||
expect(seekedKey).not.toBe(null);
|
||||
expect(seeked!.sequenceNumber).toBe(seeked!.sequenceNumber);
|
||||
|
||||
const last = await reader.readAt(Infinity);
|
||||
expect(last).not.toBe(null);
|
||||
expect(last!.sequenceNumber).toBeGreaterThan(seeked!.sequenceNumber);
|
||||
|
||||
const afterLast = await reader.readNext(last!);
|
||||
expect(afterLast).toBe(null);
|
||||
|
||||
const firstTimestamp = await audioTrack.getFirstTimestamp();
|
||||
expect(firstTimestamp).toBe(0);
|
||||
|
||||
const duration = await audioTrack.computeDuration();
|
||||
expect(duration).toBe(last!.timestamp + last!.duration);
|
||||
});
|
||||
|
||||
test('Sync reading', async () => {
|
||||
using input = new Input({
|
||||
source: new BufferSource(fs.readFileSync(path.join(__dirname, '../public/AudacityTest1.mp3'))),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
const audioTrack = await input.getPrimaryAudioTrack();
|
||||
assert(audioTrack);
|
||||
|
||||
const reader = new PacketReader(audioTrack);
|
||||
|
||||
let current = reader.readFirst() as EncodedPacket | null;
|
||||
let count = 0;
|
||||
while (current) {
|
||||
current = reader.readNext(current) as EncodedPacket | null;
|
||||
count++;
|
||||
}
|
||||
|
||||
expect(count).toBe(475);
|
||||
});
|
||||
@@ -0,0 +1,177 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import { Input } from '../../src/input.js';
|
||||
import { BufferSource, FilePathSource } from '../../src/source.js';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import { ALL_FORMATS, MP3, MP4, QTFF } from '../../src/input-format.js';
|
||||
import { PacketReader } from '../../src/cursors.js';
|
||||
import { InputAudioTrack, InputTrack } from '../../src/input-track.js';
|
||||
import { assert } from '../../src/misc.js';
|
||||
import { EncodedPacket } from '../../src/packet.js';
|
||||
|
||||
const __dirname = new URL('.', import.meta.url).pathname;
|
||||
|
||||
const testBasicPacketReading = async (track: InputTrack) => {
|
||||
const reader = new PacketReader(track);
|
||||
const first = await reader.readFirst();
|
||||
expect(first).not.toBe(null);
|
||||
expect(first!.timestamp).toBe(0);
|
||||
|
||||
const next = await reader.readNext(first!);
|
||||
expect(next).not.toBe(null);
|
||||
expect(next!.timestamp).toBeCloseTo(first!.timestamp + first!.duration);
|
||||
expect(next!.sequenceNumber).toBeGreaterThan(first!.sequenceNumber);
|
||||
|
||||
const nextKey = await reader.readNextKey(first!);
|
||||
expect(nextKey).not.toBe(null);
|
||||
expect(nextKey!.type).toBe('key');
|
||||
expect(nextKey!.timestamp).toBeGreaterThanOrEqual(next!.timestamp);
|
||||
|
||||
const seeked = await reader.readAt(1);
|
||||
expect(seeked).not.toBe(null);
|
||||
expect(seeked!.timestamp).toBeGreaterThan(0.9);
|
||||
expect(seeked!.timestamp).toBeLessThanOrEqual(1);
|
||||
expect(seeked!.sequenceNumber).toBeGreaterThan(next!.sequenceNumber);
|
||||
|
||||
const seekedKey = await reader.readKeyAt(1);
|
||||
expect(seekedKey).not.toBe(null);
|
||||
expect(seekedKey!.type).toBe('key');
|
||||
expect(seekedKey!.sequenceNumber).toBeGreaterThanOrEqual(first!.sequenceNumber);
|
||||
expect(seekedKey!.sequenceNumber).toBeLessThanOrEqual(seeked!.sequenceNumber);
|
||||
|
||||
const last = await reader.readAt(Infinity);
|
||||
expect(last).not.toBe(null);
|
||||
expect(last!.sequenceNumber).toBeGreaterThan(seeked!.sequenceNumber);
|
||||
|
||||
const afterLast = await reader.readNext(last!);
|
||||
expect(afterLast).toBe(null);
|
||||
};
|
||||
|
||||
test('Regular ISOBMFF demuxing', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(path.join(__dirname, '../public/trim-buck-bunny.mov')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getFormat()).toBe(QTFF);
|
||||
expect(await input.getMimeType()).toBe('video/quicktime; codecs="avc1.4d4029, mp4a.40.2"');
|
||||
|
||||
const tracks = await input.getTracks();
|
||||
expect(tracks).toHaveLength(2);
|
||||
|
||||
const videoTrack = await input.getPrimaryAudioTrack();
|
||||
assert(videoTrack);
|
||||
|
||||
await testBasicPacketReading(videoTrack);
|
||||
|
||||
expect(await input.computeDuration()).toBeCloseTo(5.041666666666667);
|
||||
});
|
||||
|
||||
test('Fragmented ISOBMFF demuxing', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(path.join(__dirname, '../public/frag-buck-bunny.mp4')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getFormat()).toBe(MP4);
|
||||
expect(await input.getMimeType()).toBe('video/mp4; codecs="avc1.640014, mp4a.40.2"');
|
||||
|
||||
const tracks = await input.getTracks();
|
||||
expect(tracks).toHaveLength(2);
|
||||
|
||||
const videoTrack = await input.getPrimaryAudioTrack();
|
||||
assert(videoTrack);
|
||||
|
||||
await testBasicPacketReading(videoTrack);
|
||||
|
||||
expect(await input.computeDuration()).toBeCloseTo(5);
|
||||
});
|
||||
|
||||
test('Regular ISOBMFF sync reading', async () => {
|
||||
using input = new Input({
|
||||
source: new BufferSource(fs.readFileSync(path.join(__dirname, '../public/trim-buck-bunny.mov'))),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
const videoTrack = await input.getPrimaryVideoTrack();
|
||||
assert(videoTrack);
|
||||
|
||||
const count = testSyncPacketReading(videoTrack);
|
||||
expect(count).toBe(121);
|
||||
});
|
||||
|
||||
test('Fragmented ISOBMFF sync reading', async () => {
|
||||
using input = new Input({
|
||||
source: new BufferSource(fs.readFileSync(path.join(__dirname, '../public/frag-buck-bunny.mp4'))),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
const videoTrack = await input.getPrimaryVideoTrack();
|
||||
assert(videoTrack);
|
||||
|
||||
const count = testSyncPacketReading(videoTrack);
|
||||
expect(count).toBe(120);
|
||||
});
|
||||
|
||||
test('MP3 demuxing', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(path.join(__dirname, '../public/AudacityTest1.mp3')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getFormat()).toBe(MP3);
|
||||
expect(await input.getMimeType()).toBe('audio/mpeg');
|
||||
|
||||
const tracks = await input.getTracks();
|
||||
expect(tracks).toHaveLength(1);
|
||||
const audioTrack = tracks[0] as InputAudioTrack;
|
||||
expect(audioTrack).toBeInstanceOf(InputAudioTrack);
|
||||
|
||||
expect(audioTrack.numberOfChannels).toBe(1);
|
||||
expect(audioTrack.sampleRate).toBe(44100);
|
||||
|
||||
const decoderConfig = await audioTrack.getDecoderConfig();
|
||||
expect(decoderConfig!.codec).toBe('mp3');
|
||||
expect(decoderConfig!.numberOfChannels).toBe(audioTrack.numberOfChannels);
|
||||
expect(decoderConfig!.sampleRate).toBe(audioTrack.sampleRate);
|
||||
expect(decoderConfig!.description).toBeUndefined();
|
||||
|
||||
await testBasicPacketReading(audioTrack);
|
||||
|
||||
const firstTimestamp = await audioTrack.getFirstTimestamp();
|
||||
expect(firstTimestamp).toBe(0);
|
||||
|
||||
const duration = await audioTrack.computeDuration();
|
||||
expect(duration).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
const testSyncPacketReading = (track: InputTrack) => {
|
||||
const reader = new PacketReader(track);
|
||||
|
||||
let current = reader.readFirst() as EncodedPacket | null;
|
||||
expect(current).toBeInstanceOf(EncodedPacket);
|
||||
|
||||
let count = 0;
|
||||
while (current) {
|
||||
current = reader.readNext(current) as EncodedPacket | null;
|
||||
expect(current instanceof EncodedPacket || current === null).toBe(true);
|
||||
count++;
|
||||
}
|
||||
|
||||
expect(count).toBeGreaterThan(0);
|
||||
|
||||
return count;
|
||||
};
|
||||
|
||||
test('MP3 sync reading', async () => {
|
||||
using input = new Input({
|
||||
source: new BufferSource(fs.readFileSync(path.join(__dirname, '../public/AudacityTest1.mp3'))),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
const audioTrack = await input.getPrimaryAudioTrack();
|
||||
assert(audioTrack);
|
||||
|
||||
const count = testSyncPacketReading(audioTrack);
|
||||
expect(count).toBe(475);
|
||||
});
|
||||
@@ -64,8 +64,7 @@ test('Packet cursor seeking', async () => {
|
||||
});
|
||||
|
||||
const videoTrack = (await input.getPrimaryVideoTrack())!;
|
||||
const reader = new PacketReader(videoTrack);
|
||||
const cursor = new PacketCursor(reader);
|
||||
const cursor = new PacketCursor(videoTrack);
|
||||
|
||||
expect(cursor.current).toBe(null);
|
||||
|
||||
@@ -102,8 +101,7 @@ test('Packet cursor iteration', async () => {
|
||||
});
|
||||
|
||||
const videoTrack = (await input.getPrimaryVideoTrack())!;
|
||||
const reader = new PacketReader(videoTrack);
|
||||
const cursor = new PacketCursor(reader);
|
||||
const cursor = new PacketCursor(videoTrack);
|
||||
|
||||
const packet0 = (await cursor.seekToFirst())!;
|
||||
expect(cursor.current!.timestamp).toBe(0);
|
||||
@@ -211,7 +209,7 @@ test('Packet cursor iteration', async () => {
|
||||
|
||||
expect(total).toBe(121);
|
||||
|
||||
const cursor2 = new PacketCursor(reader);
|
||||
const cursor2 = new PacketCursor(videoTrack);
|
||||
const packet9 = (await cursor2.next())!; // Without any prior seeks
|
||||
expect(packet9.sequenceNumber).toBe(packet0.sequenceNumber);
|
||||
});
|
||||
@@ -224,7 +222,7 @@ test('Synchronous packet reading', async () => {
|
||||
|
||||
const videoTrack = (await input.getPrimaryVideoTrack())!;
|
||||
const reader = new PacketReader(videoTrack);
|
||||
const cursor = new PacketCursor(reader);
|
||||
const cursor = new PacketCursor(videoTrack);
|
||||
|
||||
expect(reader.readFirst()).not.toBeInstanceOf(Promise);
|
||||
|
||||
@@ -249,8 +247,7 @@ test('Command queuing', async () => {
|
||||
});
|
||||
|
||||
const videoTrack = (await input.getPrimaryVideoTrack())!;
|
||||
const reader = new PacketReader(videoTrack);
|
||||
const cursor = new PacketCursor(reader);
|
||||
const cursor = new PacketCursor(videoTrack);
|
||||
|
||||
expect(cursor.waitUntilIdle()).toBe(null);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user