Skip to content

Commit

Permalink
fix(CMCD): Only report ltc if playing; round to int (#8011)
Browse files Browse the repository at this point in the history
Fixes #8010
  • Loading branch information
costela authored Feb 4, 2025
1 parent ecfcf49 commit 6ce3c73
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Jürgen Kartnaller <[email protected]>
Justin Swaney <[email protected]>
Konstantin Grushetsky <[email protected]>
Leandro Ribeiro Moreira <[email protected]>
Leo Antunes <[email protected]>
Loïc Raux <[email protected]>
Lucas Gabriel Sánchez <[email protected]>
Martin Stark <[email protected]>
Expand Down
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
17 changes: 15 additions & 2 deletions test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2925,21 +2925,34 @@ 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', () => {
video.currentTime = 0;
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(990600);
});
});

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

0 comments on commit 6ce3c73

Please sign in to comment.