Skip to content

Commit

Permalink
feat: Parse TS frameRate (#6998)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Jul 8, 2024
1 parent 7fcc72c commit f4f9b05
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2473,6 +2473,7 @@ shaka.hls.HlsParser = class {
stream.height = stream.height || realStream.height;
stream.hdr = stream.hdr || realStream.hdr;
stream.colorGamut = stream.colorGamut || realStream.colorGamut;
stream.frameRate = stream.frameRate || realStream.frameRate;
if (stream.language == 'und' && realStream.language != 'und') {
stream.language = realStream.language;
}
Expand Down Expand Up @@ -2790,6 +2791,7 @@ shaka.hls.HlsParser = class {
let height = null;
let videoRange = null;
let colorGamut = null;
let frameRate = null;
if (segments.length > 0 && requestBasicInfo) {
const basicInfo = await this.getBasicInfoFromSegments_(segments);

Expand All @@ -2803,6 +2805,7 @@ shaka.hls.HlsParser = class {
width = basicInfo.width;
videoRange = basicInfo.videoRange;
colorGamut = basicInfo.colorGamut;
frameRate = basicInfo.frameRate;

if (allowOverrideMimeType) {
mimeType = basicInfo.mimeType;
Expand Down Expand Up @@ -2839,8 +2842,7 @@ shaka.hls.HlsParser = class {
if (type == shaka.util.ManifestParserUtils.ContentType.VIDEO &&
(width || height || videoRange || colorGamut)) {
this.addVideoAttributes_(stream, width, height,
/* frameRate= */ null, videoRange, /* videoLayout= */ null,
colorGamut);
frameRate, videoRange, /* videoLayout= */ null, colorGamut);
}

// This new calculation is necessary for Low Latency streams.
Expand Down
9 changes: 8 additions & 1 deletion lib/media/segment_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ shaka.media.SegmentUtils = class {
closedCaptions: new Map(),
videoRange: null,
colorGamut: null,
frameRate: null,
};
}

Expand Down Expand Up @@ -127,6 +128,7 @@ shaka.media.SegmentUtils = class {
closedCaptions: closedCaptions,
videoRange: null,
colorGamut: null,
frameRate: videoInfo.frameRate,
};
}

Expand Down Expand Up @@ -204,6 +206,8 @@ shaka.media.SegmentUtils = class {
let realVideoRange = null;
/** @type {?string} */
let realColorGamut = null;
/** @type {?string} */
const realFrameRate = null;

/** @type {?string} */
let baseBox;
Expand Down Expand Up @@ -432,6 +436,7 @@ shaka.media.SegmentUtils = class {
closedCaptions: closedCaptions,
videoRange: realVideoRange,
colorGamut: realColorGamut,
frameRate: realFrameRate,
};
}

Expand Down Expand Up @@ -549,7 +554,8 @@ shaka.media.SegmentUtils = class {
* sampleRate: ?number,
* closedCaptions: Map.<string, string>,
* videoRange: ?string,
* colorGamut: ?string
* colorGamut: ?string,
* frameRate: ?string
* }}
*
* @property {string} type
Expand All @@ -563,5 +569,6 @@ shaka.media.SegmentUtils = class {
* @property {Map.<string, string>} closedCaptions
* @property {?string} videoRange
* @property {?string} colorGamut
* @property {?string} frameRate
*/
shaka.media.SegmentUtils.BasicInfo;
35 changes: 32 additions & 3 deletions lib/util/ts_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

goog.provide('shaka.util.TsParser');

goog.require('goog.asserts');
goog.require('shaka.Deprecate');
goog.require('shaka.log');
goog.require('shaka.util.ExpGolomb');
Expand Down Expand Up @@ -862,7 +863,8 @@ shaka.util.TsParser = class {
/**
* Return the video information
*
* @return {{height: ?string, width: ?string, codec: ?string}}
* @return {{height: ?string, width: ?string, codec: ?string,
* frameRate: ?string}}
* @export
*/
getVideoInfo() {
Expand All @@ -872,10 +874,32 @@ shaka.util.TsParser = class {
return this.getAvcInfo_();
}

/**
* @return {?string}
* @private
*/
getFrameRate_() {
const timescale = shaka.util.TsParser.Timescale;
const videoData = this.getVideoData();
if (videoData.length > 1) {
const firstPts = videoData[0].pts;
goog.asserts.assert(typeof(firstPts) == 'number',
'Should be an number!');
const secondPts = videoData[1].pts;
goog.asserts.assert(typeof(secondPts) == 'number',
'Should be an number!');
if (!isNaN(secondPts - firstPts)) {
return String(1 / (secondPts - firstPts) * timescale);
}
}
return null;
}

/**
* Return the video information for AVC
*
* @return {{height: ?string, width: ?string, codec: ?string}}
* @return {{height: ?string, width: ?string, codec: ?string,
* frameRate: ?string}}
* @private
*/
getAvcInfo_() {
Expand All @@ -884,6 +908,7 @@ shaka.util.TsParser = class {
height: null,
width: null,
codec: null,
frameRate: null,
};
const videoNalus = this.getVideoNalus();
if (!videoNalus.length) {
Expand Down Expand Up @@ -995,14 +1020,16 @@ shaka.util.TsParser = class {
frameCropLeftOffset * 2 - frameCropRightOffset * 2);
videoInfo.codec = 'avc1.' + this.byteToHex_(profileIdc) +
this.byteToHex_(profileCompatibility) + this.byteToHex_(levelIdc);
videoInfo.frameRate = this.getFrameRate_();

return videoInfo;
}

/**
* Return the video information for HVC
*
* @return {{height: ?string, width: ?string, codec: ?string}}
* @return {{height: ?string, width: ?string, codec: ?string,
* frameRate: ?string}}
* @private
*/
getHvcInfo_() {
Expand All @@ -1011,6 +1038,7 @@ shaka.util.TsParser = class {
height: null,
width: null,
codec: null,
frameRate: null,
};
const videoNalus = this.getVideoNalus();
if (!videoNalus.length) {
Expand Down Expand Up @@ -1135,6 +1163,7 @@ shaka.util.TsParser = class {
generalConstraintIndicatorFlags1.toString(16).toUpperCase();
}
videoInfo.codec = codec;
videoInfo.frameRate = this.getFrameRate_();

return videoInfo;
}
Expand Down

0 comments on commit f4f9b05

Please sign in to comment.