From 6ce3c73b790402cca252f5129bcb6026e76a2f3c Mon Sep 17 00:00:00 2001 From: Leo Antunes Date: Tue, 4 Feb 2025 18:24:44 +0100 Subject: [PATCH] fix(CMCD): Only report ltc if playing; round to int (#8011) Fixes #8010 --- CONTRIBUTORS | 1 + lib/player.js | 5 +++-- test/player_unit.js | 17 +++++++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index ca37820029..f3c11cd35b 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -91,6 +91,7 @@ Jürgen Kartnaller Justin Swaney Konstantin Grushetsky Leandro Ribeiro Moreira +Leo Antunes Loïc Raux Lucas Gabriel Sánchez Martin Stark diff --git a/lib/player.js b/lib/player.js index 8607d09eff..64a9ca80d6 100644 --- a/lib/player.js +++ b/lib/player.js @@ -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); } /** diff --git a/test/player_unit.js b/test/player_unit.js index 074a3f3f03..088a206cc1 100644 --- a/test/player_unit.js +++ b/test/player_unit.js @@ -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', () => {