mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-02 13:23:51 +02:00
Add TrackDescriptor concept
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { TrackType } from '../output';
|
||||
import {
|
||||
extractAv1CodecInfoFromPacket,
|
||||
extractAvcDecoderConfigurationRecord,
|
||||
@@ -24,12 +25,8 @@ import {
|
||||
import { Demuxer } from '../demuxer';
|
||||
import { Input } from '../input';
|
||||
import {
|
||||
InputAudioTrack,
|
||||
InputAudioTrackBacking,
|
||||
InputTrack,
|
||||
InputTrackBacking,
|
||||
|
||||
InputVideoTrack,
|
||||
InputVideoTrackBacking,
|
||||
} from '../input-track';
|
||||
import { AttachedFile, DEFAULT_TRACK_DISPOSITION, MetadataTags, TrackDisposition } from '../metadata';
|
||||
@@ -190,7 +187,7 @@ type InternalTrack = {
|
||||
cuePoints: CuePoint[];
|
||||
|
||||
disposition: TrackDisposition;
|
||||
inputTrack: InputTrack | null;
|
||||
trackBacking: InputTrackBacking | null;
|
||||
codecId: string | null;
|
||||
codecPrivate: Uint8Array | null;
|
||||
defaultDuration: number | null;
|
||||
@@ -273,16 +270,18 @@ export class MatroskaDemuxer extends Demuxer {
|
||||
this.reader = input._reader;
|
||||
}
|
||||
|
||||
async getTracks() {
|
||||
async getTrackBackings() {
|
||||
await this.readMetadata();
|
||||
return this.segments.flatMap(segment => segment.tracks.map(track => track.inputTrack!));
|
||||
return this.segments.flatMap(segment => segment.tracks.map(track => track.trackBacking!));
|
||||
}
|
||||
|
||||
override async getMimeType() {
|
||||
await this.readMetadata();
|
||||
|
||||
const tracks = await this.getTracks();
|
||||
const codecStrings = await Promise.all(tracks.map(x => x.getCodecParameterString()));
|
||||
const backings = await this.getTrackBackings();
|
||||
const codecStrings = await Promise.all(backings.map(
|
||||
x => x.getDecoderConfig().then(c => c?.codec ?? null),
|
||||
));
|
||||
|
||||
return buildMatroskaMimeType({
|
||||
isWebM: this.isWebM,
|
||||
@@ -328,7 +327,10 @@ export class MatroskaDemuxer extends Demuxer {
|
||||
|
||||
if (segment.tracks.length > 0) {
|
||||
const minFirstTimestamp = Math.min(
|
||||
...await Promise.all(segment.tracks.map(x => x.inputTrack!.getFirstTimestamp())),
|
||||
...await Promise.all(segment.tracks.map(x =>
|
||||
x.trackBacking!.getFirstPacket({ metadataOnly: true })
|
||||
.then(p => p?.timestamp ?? 0),
|
||||
)),
|
||||
);
|
||||
|
||||
endTimestamp += minFirstTimestamp;
|
||||
@@ -1020,7 +1022,7 @@ export class MatroskaDemuxer extends Demuxer {
|
||||
...DEFAULT_TRACK_DISPOSITION,
|
||||
primary: false,
|
||||
},
|
||||
inputTrack: null,
|
||||
trackBacking: null,
|
||||
codecId: null,
|
||||
codecPrivate: null,
|
||||
defaultDuration: null,
|
||||
@@ -1101,8 +1103,7 @@ export class MatroskaDemuxer extends Demuxer {
|
||||
}
|
||||
|
||||
const videoTrack = this.currentTrack as InternalVideoTrack;
|
||||
const inputTrack = new InputVideoTrack(this.input, new MatroskaVideoTrackBacking(videoTrack));
|
||||
this.currentTrack.inputTrack = inputTrack;
|
||||
this.currentTrack.trackBacking = new MatroskaVideoTrackBacking(videoTrack);
|
||||
this.currentSegment.tracks.push(this.currentTrack);
|
||||
} else if (this.currentTrack.info.type === 'audio') {
|
||||
if (codecIdWithoutSuffix === CODEC_STRING_MAP.aac) {
|
||||
@@ -1159,8 +1160,7 @@ export class MatroskaDemuxer extends Demuxer {
|
||||
}
|
||||
|
||||
const audioTrack = this.currentTrack as InternalAudioTrack;
|
||||
const inputTrack = new InputAudioTrack(this.input, new MatroskaAudioTrackBacking(audioTrack));
|
||||
this.currentTrack.inputTrack = inputTrack;
|
||||
this.currentTrack.trackBacking = new MatroskaAudioTrackBacking(audioTrack);
|
||||
this.currentSegment.tracks.push(this.currentTrack);
|
||||
}
|
||||
}
|
||||
@@ -1926,19 +1926,21 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
|
||||
constructor(public internalTrack: InternalTrack) {}
|
||||
|
||||
abstract getType(): TrackType;
|
||||
abstract getDecoderConfig(): Promise<VideoDecoderConfig | AudioDecoderConfig | null>;
|
||||
|
||||
getId() {
|
||||
return this.internalTrack.id;
|
||||
}
|
||||
|
||||
getNumber() {
|
||||
const demuxer = this.internalTrack.demuxer;
|
||||
const inputTrack = this.internalTrack.inputTrack!;
|
||||
const trackType = inputTrack.type;
|
||||
const trackType = this.internalTrack.trackBacking!.getType();
|
||||
|
||||
let number = 0;
|
||||
for (const segment of demuxer.segments) {
|
||||
for (const track of segment.tracks) {
|
||||
if (track.inputTrack!.type === trackType) {
|
||||
if (track.trackBacking!.getType() === trackType) {
|
||||
number++;
|
||||
}
|
||||
|
||||
@@ -2409,6 +2411,10 @@ class MatroskaVideoTrackBacking extends MatroskaTrackBacking implements InputVid
|
||||
this.internalTrack = internalTrack;
|
||||
}
|
||||
|
||||
getType() {
|
||||
return 'video' as const;
|
||||
}
|
||||
|
||||
override getCodec(): VideoCodec | null {
|
||||
return this.internalTrack.info.codec;
|
||||
}
|
||||
@@ -2514,6 +2520,10 @@ class MatroskaAudioTrackBacking extends MatroskaTrackBacking implements InputAud
|
||||
this.internalTrack = internalTrack;
|
||||
}
|
||||
|
||||
getType() {
|
||||
return 'audio' as const;
|
||||
}
|
||||
|
||||
override getCodec(): AudioCodec | null {
|
||||
return this.internalTrack.info.codec;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user