-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show debug logs for delay, current time and buffered end.
- Loading branch information
1 parent
07f9471
commit 5daea7e
Showing
5 changed files
with
94 additions
and
0 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
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,45 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { Debugger } from 'debug' | ||
|
||
/** | ||
* Show debug logs with information received from | ||
* 'progress' & 'timeupdate' events including the current | ||
* up time, delay and end time of last buffer. | ||
* bufferedEnd: the last buffered time | ||
* currentTime: current playback time | ||
* delay: the last buffered time - current playback time | ||
*/ | ||
export const useVideoDebug = ( | ||
videoEl: HTMLVideoElement | null, | ||
debugLog: Debugger | ||
) => { | ||
useEffect(() => { | ||
if (videoEl === null) { | ||
return | ||
} | ||
|
||
const onUpdate = () => { | ||
try { | ||
const currentTime = videoEl.currentTime | ||
const bufferedEnd = videoEl.buffered.end(videoEl.buffered.length - 1) | ||
|
||
debugLog('%o', { | ||
delay: bufferedEnd - currentTime, | ||
currentTime, | ||
bufferedEnd, | ||
}) | ||
} catch (err) { | ||
debugLog('%o', err) | ||
} | ||
} | ||
|
||
videoEl.addEventListener('timeupdate', onUpdate) | ||
videoEl.addEventListener('progress', onUpdate) | ||
|
||
return () => { | ||
videoEl.removeEventListener('timeupdate', onUpdate) | ||
videoEl.removeEventListener('progress', onUpdate) | ||
} | ||
}, [debugLog, videoEl]) | ||
} |