Skip to content

Commit

Permalink
added offset compensation for presentationTimeOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
jobispo committed Nov 24, 2023
1 parent 8104f8e commit 031dc5f
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ shaka.media.StreamingEngine = class {
mediaState.performingUpdate = true;

try {
await this.initSourceBuffer_(mediaState, reference);
const timescale = await this.initSourceBuffer_(mediaState, reference);
this.destroyer_.ensureNotDestroyed();
if (this.fatalError_) {
return;
Expand Down Expand Up @@ -1261,6 +1261,39 @@ shaka.media.StreamingEngine = class {
this.scheduleUpdate_(mediaState, 0);
return;
}
if (timescale) {
new shaka.util.Mp4Parser()
.box('moof', shaka.util.Mp4Parser.children)
.box('traf', shaka.util.Mp4Parser.children)
.fullBox('tfdt', async (box) => {
goog.asserts.assert(
box.version != null,
'TFDT is a full box and should have a valid version.');

const parsedTFDT = shaka.util.Mp4BoxParsers.parseTFDT(
box.reader, box.version);

const baseMediaDecodeTime = parsedTFDT.baseMediaDecodeTime;
const scaledMediaDecodeTime = baseMediaDecodeTime / timescale;

if (Math.abs(mediaState.lastTimestampOffset) >
scaledMediaDecodeTime) {
const lastAppendWindowEnd = 0 +
mediaState.lastAppendWindowEnd;
const lastAppendWindowStart = 0 +
mediaState.lastAppendWindowStart;
shaka.log.v1(logPrefix,
'setting timestamp offset to ' + -scaledMediaDecodeTime);
await this.playerInterface_.mediaSourceEngine
.setStreamProperties(
mediaState.type, -scaledMediaDecodeTime,
lastAppendWindowStart,
lastAppendWindowEnd);
}
})
.parse(result, /* partialOkay= */ false,
/* isChunkedData= */ true);
}
await this.append_(
mediaState, presentationTime, stream, reference, result);
}
Expand Down Expand Up @@ -1509,18 +1542,39 @@ shaka.media.StreamingEngine = class {
operations.push(setProperties());
}

let timescale;

if (!shaka.media.InitSegmentReference.equal(
reference.initSegmentReference, mediaState.lastInitSegmentReference)) {
mediaState.lastInitSegmentReference = reference.initSegmentReference;

if (reference.initSegmentReference) {
shaka.log.v1(logPrefix, 'fetching init segment');

const setTimeScale = (data) => {
new shaka.util.Mp4Parser()
.box('moov', shaka.util.Mp4Parser.children)
.box('mvhd', (box) => {
const version = box.reader.readUint8();
if (version === 1) {
// skip according the standard
box.reader.skip(19);
} else {
box.reader.skip(11);
}
timescale = box.reader.readUint32();
shaka.log.v2('mvhd', 'timescale', timescale);
})
.parse(data, /* partialOkay= */ false,
/* isChunkedData= */ true);
};

const fetchInit =
this.fetch_(mediaState, reference.initSegmentReference);
const append = async () => {
try {
const initSegment = await fetchInit;
mediaState.type === 'video' && setTimeScale(initSegment);
this.destroyer_.ensureNotDestroyed();
shaka.log.v1(logPrefix, 'appending init segment');
const hasClosedCaptions = mediaState.stream.closedCaptions &&
Expand All @@ -1538,6 +1592,7 @@ shaka.media.StreamingEngine = class {
}

await Promise.all(operations);
return timescale;
}


Expand Down

0 comments on commit 031dc5f

Please sign in to comment.