Adjust timestamp logic for media sinks for more accurate audio timing

This commit is contained in:
Vanilagy
2025-01-12 16:41:42 +01:00
parent 764a6dfd52
commit aea808ca0c
6 changed files with 91 additions and 37 deletions
+7 -1
View File
@@ -1,4 +1,5 @@
import {
AacCodecInfo,
AudioCodec,
extractAudioCodecString,
extractAv1CodecInfoFromFrame,
@@ -122,6 +123,7 @@ type InternalTrack = {
sampleRate: number;
bitDepth: number;
codec: AudioCodec | null;
aacCodecInfo: AacCodecInfo | null;
};
};
type InternalVideoTrack = InternalTrack & { info: { type: 'video' } };
@@ -625,6 +627,9 @@ export class MatroskaDemuxer extends Demuxer {
) {
if (codecIdWithoutSuffix === CODEC_STRING_MAP.aac) {
this.currentTrack.info.codec = 'aac';
this.currentTrack.info.aacCodecInfo = {
isMpeg2: this.currentTrack.codecId.includes('MPEG2'),
};
} else if (this.currentTrack.codecId === CODEC_STRING_MAP.mp3) {
this.currentTrack.info.codec = 'mp3';
} else if (codecIdWithoutSuffix === CODEC_STRING_MAP.opus) {
@@ -695,6 +700,7 @@ export class MatroskaDemuxer extends Demuxer {
sampleRate: -1,
bitDepth: -1,
codec: null,
aacCodecInfo: null,
};
}
}; break;
@@ -1476,7 +1482,7 @@ class MatroskaAudioTrackBacking extends MatroskaTrackBacking<EncodedAudioSample>
codec: extractAudioCodecString({
codec: this.internalTrack.info.codec,
codecDescription: this.internalTrack.codecPrivate,
aacCodecInfo: null,
aacCodecInfo: this.internalTrack.info.aacCodecInfo,
}),
numberOfChannels: this.internalTrack.info.numberOfChannels,
sampleRate: this.internalTrack.info.sampleRate,
+3 -3
View File
@@ -487,7 +487,7 @@ export class MatroskaMuxer extends Muxer {
const timestamp = this.validateAndNormalizeTimestamp(trackData.track, cue.timestamp, true);
let bodyText = cue.text;
const timestampMs = Math.floor(timestamp * 1000);
const timestampMs = Math.round(timestamp * 1000);
// Replace in-body timestamps so that they're relative to the cue start time
inlineTimestampRegex.lastIndex = 0;
@@ -622,7 +622,7 @@ export class MatroskaMuxer extends Muxer {
this.createSegment();
}
const msTimestamp = Math.floor(1000 * chunk.timestamp);
const msTimestamp = Math.round(1000 * chunk.timestamp);
// We can only finalize this cluster (and begin a new one) if we know that each track will be able to
// start the new one with a key frame.
const keyFrameQueuedEverywhere = this.trackDatas.every((otherTrackData) => {
@@ -673,7 +673,7 @@ export class MatroskaMuxer extends Muxer {
view.setUint8(0, 0x80 | trackData.track.id);
view.setInt16(1, relativeTimestamp, false);
const msDuration = Math.floor(1000 * chunk.duration);
const msDuration = Math.round(1000 * chunk.duration);
if (msDuration === 0 && !chunk.additions) {
// No duration or additions, we can write out a SimpleBlock