Skip to content

Commit

Permalink
feat(Stats): Add manifestPeriodCount to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
tykus160 committed Jun 11, 2024
1 parent 2f3a1eb commit 8012042
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 7 deletions.
8 changes: 7 additions & 1 deletion externs/shaka/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* ignoreManifestTimestampsInSegmentsMode: boolean,
* type: string,
* serviceDescription: ?shaka.extern.ServiceDescription,
* nextUrl: ?string
* nextUrl: ?string,
* periodCount: number
* }}
*
* @description
Expand Down Expand Up @@ -96,6 +97,11 @@
* decrease latency.
* @property {?string} nextUrl
* The next url to play.
* @property {number} periodCount
* Number of periods found in a manifest. For DASH, it represents number of
* Period elements in a manifest. If streaming protocol does not implement
* period-like structure, it should be set to 1.
* <i>Defaults to <code>1</code>.</i>
*
* @exportDoc
*/
Expand Down
5 changes: 5 additions & 0 deletions externs/shaka/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ shaka.extern.StateChange;
* bytesDownloaded: number,
*
* nonFatalErrorCount: number,
* manifestPeriodCount: number,
*
* switchHistory: !Array.<shaka.extern.TrackChoice>,
* stateHistory: !Array.<shaka.extern.StateChange>
Expand Down Expand Up @@ -175,6 +176,10 @@ shaka.extern.StateChange;
*
* @property {number} nonFatalErrorCount
* The amount of non fatal errors that occurred. If nothing is loaded, NaN.
* @property {number} manifestPeriodCount
* The amount of periods occurred in the manifest. For DASH it represents
* number of Period elements in a manifest. For HLS & MSS it is always 1.
* In src= mode or if nothing is loaded, NaN.
*
* @property {!Array.<shaka.extern.TrackChoice>} switchHistory
* A history of the stream changes.
Expand Down
7 changes: 5 additions & 2 deletions lib/dash/dash_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ shaka.dash.DashParser = class {
type: shaka.media.ManifestParser.DASH,
serviceDescription: this.parseServiceDescription_(mpd),
nextUrl: this.parseMpdChaining_(mpd),
periodCount: periods.length,
};

// We only need to do clock sync when we're using presentation start
Expand All @@ -779,8 +780,8 @@ shaka.dash.DashParser = class {
// maintain consistency from here on.
presentationTimeline.lockStartTime();
} else {
await this.postPeriodProcessing_(
periodsAndDuration.periods, /* isPatchUpdate= */ false);
this.manifest_.periodCount = periods.length;
await this.postPeriodProcessing_(periods, /* isPatchUpdate= */ false);
}

// Add text streams to correspond to closed captions. This happens right
Expand Down Expand Up @@ -901,6 +902,7 @@ shaka.dash.DashParser = class {
}

if (newPeriods.length) {
this.manifest_.periodCount += newPeriods.length;
await this.postPeriodProcessing_(newPeriods, /* isPatchUpdate= */ true);
}
if (this.manifestPatchContext_.type == 'static') {
Expand Down Expand Up @@ -981,6 +983,7 @@ shaka.dash.DashParser = class {
*/
removePatchPeriod_(periodId) {
const SegmentTemplate = shaka.dash.SegmentTemplate;
this.manifest_.periodCount--;
for (const contextId of this.contextCache_.keys()) {
if (contextId.startsWith(periodId)) {
const context = this.contextCache_.get(contextId);
Expand Down
1 change: 1 addition & 0 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ shaka.hls.HlsParser = class {
type: shaka.media.ManifestParser.HLS,
serviceDescription: null,
nextUrl: null,
periodCount: 1,
};

// If there is no 'CODECS' attribute in the manifest and codec guessing is
Expand Down
1 change: 1 addition & 0 deletions lib/mss/mss_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ shaka.mss.MssParser = class {
type: shaka.media.ManifestParser.MSS,
serviceDescription: null,
nextUrl: null,
periodCount: 1,
};

// This is the first point where we have a meaningful presentation start
Expand Down
1 change: 1 addition & 0 deletions lib/offline/manifest_converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ shaka.offline.ManifestConverter = class {
type: manifestDB.type || shaka.media.ManifestParser.UNKNOWN,
serviceDescription: null,
nextUrl: null,
periodCount: 1,
};
}

Expand Down
11 changes: 7 additions & 4 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -5337,10 +5337,13 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.stats_.setLiveLatency(latency);
}

if (this.manifest_ && this.manifest_.presentationTimeline) {
const maxSegmentDuration =
this.manifest_.presentationTimeline.getMaxSegmentDuration();
this.stats_.setMaxSegmentDuration(maxSegmentDuration);
if (this.manifest_) {
this.stats_.setManifestPeriodCount(this.manifest_.periodCount);
if (this.manifest_.presentationTimeline) {
const maxSegmentDuration =
this.manifest_.presentationTimeline.getMaxSegmentDuration();
this.stats_.setMaxSegmentDuration(maxSegmentDuration);
}
}

const estimate = this.abrManager_.getBandwidthEstimate();
Expand Down
11 changes: 11 additions & 0 deletions lib/util/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ shaka.util.Stats = class {

/** @private {number} */
this.nonFatalErrorCount_ = 0;
/** @private {number} */
this.manifestPeriodCount_ = NaN;

/** @private {!shaka.util.StateHistory} */
this.stateHistory_ = new shaka.util.StateHistory();
Expand Down Expand Up @@ -234,6 +236,13 @@ shaka.util.Stats = class {
this.nonFatalErrorCount_++;
}

/**
* @param {number} count
*/
setManifestPeriodCount(count) {
this.manifestPeriodCount_ = count;
}

/**
* @return {!shaka.util.StateHistory}
*/
Expand Down Expand Up @@ -278,6 +287,7 @@ shaka.util.Stats = class {
manifestSizeBytes: this.manifestSizeBytes_,
bytesDownloaded: this.bytesDownloaded_,
nonFatalErrorCount: this.nonFatalErrorCount_,
manifestPeriodCount: this.manifestPeriodCount_,
stateHistory: this.stateHistory_.getCopy(),
switchHistory: this.switchHistory_.getCopy(),
};
Expand Down Expand Up @@ -313,6 +323,7 @@ shaka.util.Stats = class {
manifestSizeBytes: NaN,
bytesDownloaded: NaN,
nonFatalErrorCount: NaN,
manifestPeriodCount: NaN,
switchHistory: [],
stateHistory: [],
};
Expand Down
6 changes: 6 additions & 0 deletions test/dash/dash_parser_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ describe('DashParser Live', () => {
Date.now = () => 5;

const manifest = await parser.start('dummy://foo', playerInterface);
expect(manifest.periodCount).toBe(1);
const variant = manifest.variants[0];
const stream = variant.video;
await stream.createSegmentIndex();
Expand All @@ -428,6 +429,8 @@ describe('DashParser Live', () => {

await updateManifest();

expect(manifest.periodCount).toBe(2);

// The update should have affected the same variant object we captured
// before. Now the entire first period should exist (0-10s), plus the next
// two segments (10-14s).
Expand Down Expand Up @@ -507,6 +510,7 @@ describe('DashParser Live', () => {
Date.now = () => 5;

const manifest = await parser.start('dummy://foo', playerInterface);
expect(manifest.periodCount).toBe(1);
const variant = manifest.variants[0];
const stream = variant.video;
await stream.createSegmentIndex();
Expand All @@ -520,6 +524,8 @@ describe('DashParser Live', () => {

await updateManifest();

expect(manifest.periodCount).toBe(2);

// The update should have affected the same variant object we captured
// before. Now the entire first period should exist (0-40s), plus the next
// two segments of the second period(40-60s).
Expand Down
4 changes: 4 additions & 0 deletions test/dash/dash_parser_patch_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ describe('DashParser Patch', () => {
describe('Period', () => {
it('adds new period as an MPD child', async () => {
const manifest = await parser.start('dummy://foo', playerInterface);
expect(manifest.periodCount).toBe(1);
const stream = manifest.variants[0].video;
const patchText = [
`<Patch mpdId="${mpdId}"`,
Expand All @@ -326,13 +327,15 @@ describe('DashParser Patch', () => {
expect(stream.matchedStreams.length).toBe(1);

await updateManifest();
expect(manifest.periodCount).toBe(2);
await stream.createSegmentIndex();

expect(stream.matchedStreams.length).toBe(2);
});

it('adds new period as a Period successor', async () => {
const manifest = await parser.start('dummy://foo', playerInterface);
expect(manifest.periodCount).toBe(1);
const stream = manifest.variants[0].video;
const patchText = [
`<Patch mpdId="${mpdId}"`,
Expand All @@ -355,6 +358,7 @@ describe('DashParser Patch', () => {
expect(stream.matchedStreams.length).toBe(1);

await updateManifest();
expect(manifest.periodCount).toBe(2);
await stream.createSegmentIndex();

expect(stream.matchedStreams.length).toBe(2);
Expand Down
1 change: 1 addition & 0 deletions test/media/playhead_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ describe('Playhead', () => {
type: 'UNKNOWN',
serviceDescription: null,
nextUrl: null,
periodCount: 1,
};

config = shaka.util.PlayerConfiguration.createDefault().streaming;
Expand Down
1 change: 1 addition & 0 deletions test/media/streaming_engine_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ describe('StreamingEngine', () => {
type: 'UNKNOWN',
serviceDescription: null,
nextUrl: null,
periodCount: 1,
variants: [{
id: 1,
video: {
Expand Down
1 change: 1 addition & 0 deletions test/player_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ describe('Player', () => {
bytesDownloaded: jasmine.any(Number),

nonFatalErrorCount: jasmine.any(Number),
manifestPeriodCount: jasmine.any(Number),

// We should have loaded the first Period by now, so we should have a
// history.
Expand Down
2 changes: 2 additions & 0 deletions test/test/util/manifest_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ shaka.test.ManifestGenerator.Manifest = class {
this.serviceDescription = null;
/** @type {?string} */
this.nextUrl = null;
/** @type {number} */
this.periodCount = 1;


/** @type {shaka.extern.Manifest} */
Expand Down
1 change: 1 addition & 0 deletions test/test/util/streaming_engine_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ shaka.test.StreamingEngineUtil = class {
type: 'UNKNOWN',
serviceDescription: null,
nextUrl: null,
periodCount: 1,
};

/** @type {shaka.extern.Variant} */
Expand Down
5 changes: 5 additions & 0 deletions ui/statistics_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ shaka.ui.StatisticsButton = class extends shaka.ui.Element {
return this.currentStats_[name] + ' (errors)';
};

const parsePeriods = (name) => {
return this.currentStats_[name] + ' (periods)';
};

const parseBytes = (name) => {
const bytes = parseInt(this.currentStats_[name], 10);
if (bytes > 1e6) {
Expand Down Expand Up @@ -156,6 +160,7 @@ shaka.ui.StatisticsButton = class extends shaka.ui.Element {
'manifestSizeBytes': parseBytes,
'bytesDownloaded': parseBytes,
'nonFatalErrorCount': parseErrors,
'manifestPeriodCount': parsePeriods,
};

/** @private {shaka.util.Timer} */
Expand Down

0 comments on commit 8012042

Please sign in to comment.