Skip to content

Commit

Permalink
fix(cmcd): only report ltc if playing; round to int
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Feb 4, 2025
1 parent ecfcf49 commit 2ea26e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -5863,12 +5863,13 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
* is playing.
*/
getLiveLatency() {
if (!this.video_) {
if (!this.video_ || !this.video_.currentTime) {
return null;
}
const now = this.getPresentationStartTimeAsDate().getTime() +
this.video_.currentTime * 1000;
return Date.now() - now;

return Math.floor(Date.now() - now);
}

/**
Expand Down
16 changes: 14 additions & 2 deletions test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2925,21 +2925,33 @@ describe('Player', () => {

// Load the player with the live manifest.
await player.load(fakeManifestUri, null, fakeMimeType);

video.currentTime = 10; // Simulate that we're 10 seconds into playback.
});

it('returns null if video element does not exist', async () => {
video.currentTime = 10; // Simulate that we're 10 seconds into playback.
await player.detach();
const latency = player.getLiveLatency();
expect(latency).toBeNull();
});

it('returns null if no position defined yet', async () => {
const latency = player.getLiveLatency();
expect(latency).toBeNull();
});

it('returns correct latency when video is playing', () => {
video.currentTime = 10; // Simulate that we're 10 seconds into playback.
Date.now = () => 2000 * 1000;
const latency = player.getLiveLatency();
expect(latency).toBe(990000);
});

it('returns integer latency also when Date.now returns float', () => {
video.currentTime = 10; // Simulate that we're 10 seconds into playback.
Date.now = () => 2000 * 1000.3;
const latency = player.getLiveLatency();
expect(latency).toBe(990000);
});
});

describe('getStats', () => {
Expand Down

0 comments on commit 2ea26e5

Please sign in to comment.