-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix video progress events accuracy (#7654)
Due do rounding errors the progress events were fired way too early (especially the complete event on longer videos). This changes use float comparison to mitigate the issue. Further improvements (video with start time, seek handling) will be added in follow up PRs. Co-authored-by: Álvaro Velad Galván <[email protected]>
- Loading branch information
1 parent
ccae978
commit 576daba
Showing
4 changed files
with
99 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/*! @license | ||
* Shaka Player | ||
* Copyright 2016 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
goog.provide('shaka.util.NumberUtils'); | ||
|
||
|
||
shaka.util.NumberUtils = class { | ||
/** | ||
* Compare two float numbers, taking a configurable tolerance margin into | ||
* account. | ||
* | ||
* @param {number} a | ||
* @param {number} b | ||
* @param {number=} tolerance | ||
* @return {boolean} | ||
*/ | ||
static isFloatEqual(a, b, tolerance = Number.EPSILON) { | ||
if (a === b) { | ||
return true; | ||
} | ||
|
||
const error = Math.abs(a - b); | ||
|
||
if (error <= tolerance) { | ||
return true; | ||
} | ||
|
||
if (tolerance !== Number.EPSILON) { | ||
return Math.abs(error - tolerance) <= Number.EPSILON; | ||
} | ||
|
||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/*! @license | ||
* Shaka Player | ||
* Copyright 2016 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
describe('NumberUtils', () => { | ||
const NumberUtils = shaka.util.NumberUtils; | ||
|
||
it('compares float', () => { | ||
expect(NumberUtils.isFloatEqual(0.1 + 0.2, 0.3)).toBe(true); | ||
expect(NumberUtils.isFloatEqual(0.4 - 0.1, 0.3)).toBe(true); | ||
expect(NumberUtils.isFloatEqual(0.0004, 0.0003)).toBe(false); | ||
}); | ||
|
||
it('respects provided tolerance margin', () => { | ||
expect(NumberUtils.isFloatEqual(1.5, 1.4)).toBe(false); | ||
expect(NumberUtils.isFloatEqual(1.5, 1.4, 0.1)).toBe(true); | ||
}); | ||
}); |