Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Parses a TFDT Box, with a loss of precision beyond 53 bits #5329

Merged
merged 4 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lib/media/media_source_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,11 +696,28 @@ shaka.media.MediaSourceEngine = class {
goog.asserts.assert(
box.version == 0 || box.version == 1,
'TFDT version can only be 0 or 1');

const parsedTFDTBox = shaka.util.Mp4BoxParsers.parseTFDT(
box.reader, box.version);
const baseTime = parsedTFDTBox.baseMediaDecodeTime;
startTime = baseTime / timescale;
const Mp4BoxParsers = shaka.util.Mp4BoxParsers;
try {
const parsedTFDTBox = Mp4BoxParsers.parseTFDT(
box.reader, box.version);
const baseTime = parsedTFDTBox.baseMediaDecodeTime;
startTime = baseTime / timescale;
} catch (error) {
if (error.code != shaka.util.Error.Code.JS_INTEGER_OVERFLOW) {
throw error;
}
if (!window.BigInt) {
throw error;
}
const parsedTFDTBox = Mp4BoxParsers.parseTFDTAsBigInt(
box.reader);
const baseTime = parsedTFDTBox.baseMediaDecodeTime;
const startTimeBigInt = baseTime / BigInt(timescale);
if (startTimeBigInt > Number.MAX_SAFE_INTEGER) {
throw error;
}
startTime = Number(startTimeBigInt);
avelad marked this conversation as resolved.
Show resolved Hide resolved
}
parsedMedia = true;
box.parser.stop();
}).parse(data, /* partialOkay= */ true);
Expand Down
36 changes: 36 additions & 0 deletions lib/util/data_view_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,42 @@ shaka.util.DataViewReader = class {
}


/**
* Reads a 64 bit integer, and advances the reader.
* @return {bigint} The integer.
* @export
*/
readBigInt64() {
/** @type {number} */
let low;
/** @type {number} */
let high;

try {
if (this.littleEndian_) {
low = this.dataView_.getUint32(this.position_, true);
high = this.dataView_.getUint32(this.position_ + 4, true);
} else {
high = this.dataView_.getUint32(this.position_, false);
low = this.dataView_.getUint32(this.position_ + 4, false);
}
} catch (exception) {
throw this.outOfBounds_();
}

if (!window.BigInt) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MEDIA,
shaka.util.Error.Code.JS_INTEGER_OVERFLOW);
}

this.position_ += 8;

return BigInt((high * Math.pow(2, 32)) + low);
avelad marked this conversation as resolved.
Show resolved Hide resolved
}


/**
* Reads the specified number of raw bytes.
* @param {number} bytes The number of bytes to read.
Expand Down
25 changes: 25 additions & 0 deletions lib/util/mp4_box_parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ shaka.util.Mp4BoxParsers = class {
};
}

/**
* Parses a TFDT Box.
* @param {!shaka.util.DataViewReader} reader
* @return {!shaka.util.ParsedTFDTBoxAsBigInt}
*/
static parseTFDTAsBigInt(reader) {
return {
baseMediaDecodeTime: reader.readBigInt64(),
};
}

/**
* Parses a MDHD Box.
* @param {!shaka.util.DataViewReader} reader
Expand Down Expand Up @@ -291,6 +302,20 @@ shaka.util.ParsedTFHDBox;
*/
shaka.util.ParsedTFDTBox;


/**
* @typedef {{
* baseMediaDecodeTime: bigint
* }}
*
* @property {bigint} baseMediaDecodeTime
* As per the spec: the absolute decode time, measured on the media
* timeline, of the first sample in decode order in the track fragment
*
* @exportDoc
*/
shaka.util.ParsedTFDTBoxAsBigInt;

/**
* @typedef {{
* timescale: number,
Expand Down