Skip to content

Instantly share code, notes, and snippets.

@dave-nicholas
Created August 2, 2022 09:47
Show Gist options
  • Save dave-nicholas/6eccac115899e2479e7528d96248858b to your computer and use it in GitHub Desktop.
Save dave-nicholas/6eccac115899e2479e7528d96248858b to your computer and use it in GitHub Desktop.
shaka prft mp4 box surfacing proposal
// file: streaming_engine.js
// The _append method will call this method
/**
* Parse PRFT box.
* @param {!shaka.media.SegmentReference} reference
* @param {!shaka.extern.ParsedBox} box
* @private
*/
parsePrft_(reference, box) {
if (this.parsedPrftEventRaised) {
return;
}
box.reader.readUint32(); // Ignore referenceTrackId
const ntpTimestampSec = box.reader.readUint32();
const ntpTimestampFrac = box.reader.readUint32();
const ntpTimestamp = ntpTimestampSec * 1000 + ntpTimestampFrac / 2**32;
let mediaTime;
if ( box.version === 0) {
mediaTime = box.reader.readUint32();
} else {
mediaTime = box.reader.readUint64();
}
const wallClockTime = this.convertNtp(ntpTimestamp);
const eventName = shaka.Player.EventName.Prft;
const event = new shaka.util.FakeEvent(
eventName, {
wallClockTime,
mediaTime,
timescale: reference.timescale, //timescale added to segment reference
});
this.playerInterface_.onEvent(event);
this.parsedPrftEventRaised = true;
}
/**
* Convert Ntp ntpTimeStamp to UTC Time
*
* @param {number} ntpTimeStamp
* @return {number} utcTime
*/
convertNtp(ntpTimeStamp) {
const start = new Date(Date.UTC(1900, 0, 1, 0, 0, 0));
return new Date(start.getTime() + ntpTimeStamp).getTime();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment