Skip to content

Commit d62a985

Browse files
committed
feat: stop ui updates in player when ui not visible (250x lower cpu usage)
1 parent 0887365 commit d62a985

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

resources/js/components/video/VideoPlayer.vue

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ const cachedVolume = ref(0.5);
133133
const currentSpeed = ref(1);
134134
135135
// Player State
136+
const shouldUpdateUI = computed(() => (isShowingControls.value || isShowingStats.value) && !isScrubbing.value && !isLoading.value);
137+
136138
const latestPlayRequestId = ref<number>(0);
137139
const controlsHideTimeout = ref<number>();
138140
const volumeChangeTimeout = ref<number>();
@@ -695,13 +697,25 @@ const handleFullScreenChange = (e: Event) => {
695697
isFullScreen.value = document.fullscreenElement !== null;
696698
};
697699
700+
/** Main UI Stack
701+
*
702+
* Triggers when not scrubbing and not loading and showing controls
703+
*
704+
* Calls getBufferHealth()
705+
* -> Calls getPlayerInfo()
706+
* -> Updates bufferTime and bufferPercentage based on time elapsed
707+
* -> Triggers update to buffer bar
708+
* -> Updates framehealth
709+
* Updates timeElapsed
710+
* -> Triggers a whole load of stuff
711+
*/
698712
const handlePlayerTimeUpdate = (event: any) => {
699-
// update time if showing controls, or paused, or not seeking
700-
if (isScrubbing.value || isLoading.value || !isShowingControls.value) return;
713+
// update time if showing controls or showing stats, or paused, or not seeking
714+
if (!shouldUpdateUI.value) return;
701715
getBufferHealth();
702716
703717
// if playing or have not started playing yet, force seek (I do not remember what this is for)
704-
if (!isPaused.value || (currentId.value === -1 && timeElapsed.value)) {
718+
if (isShowingControls.value && (!isPaused.value || (currentId.value === -1 && timeElapsed.value))) {
705719
timeElapsed.value = (event.target.currentTime / timeDuration.value) * 100;
706720
}
707721
};
@@ -771,7 +785,7 @@ function handleControlsTimeout() {
771785
772786
const debouncedEndTime = debounce(getEndTime, 100);
773787
774-
function playerMouseActivity() {
788+
function playerMouseActivity(event: any) {
775789
if (!isPaused.value) {
776790
resetControlsTimeout();
777791
return;
@@ -797,6 +811,7 @@ function getEndTime() {
797811
});
798812
}
799813
814+
// Buffers getting player information for the stats panel and the buffer bar
800815
function getBufferHealth() {
801816
playerHealthCounter.value += 1;
802817
@@ -806,12 +821,13 @@ function getBufferHealth() {
806821
}
807822
}
808823
824+
// Gets playback information for the stats panel and updates the buffer bar
809825
function getPlayerInfo() {
810826
if (!player.value) return;
811-
const playbackQuality = player.value.getVideoPlaybackQuality();
812827
813-
const buffered = player.value.buffered;
828+
const playbackQuality = player.value.getVideoPlaybackQuality();
814829
const currentTime = player.value.currentTime;
830+
const buffered = player.value.buffered;
815831
816832
let bufferedSeconds = 0;
817833
@@ -983,6 +999,12 @@ watch(isPictureInPicture, async (value) => {
983999
9841000
watch(stateVideo, initVideoPlayer);
9851001
1002+
watch(isShowingControls, async (visible) => {
1003+
if (!visible || !shouldUpdateUI.value || !player.value) return;
1004+
handlePlayerTimeUpdate({ target: player.value });
1005+
await nextTick();
1006+
});
1007+
9861008
onMounted(() => {
9871009
if (document.pictureInPictureElement) document.exitPictureInPicture();
9881010
handleLoadSavedVolume();
@@ -1022,9 +1044,6 @@ defineExpose({
10221044
@mouseleave="handleControlsTimeout"
10231045
@contextmenu="
10241046
(e: any) => {
1025-
// if (isFullScreen) return;
1026-
// This does not work in fullscreen because the video container requests fullscreen but the context menu is higher in the document tree.
1027-
// TODO: change the video page to be widescreen like on youtube when fullscreen
10281047
setContextMenu(e, { items: playerContextMenuItems, style: 'w-32', itemStyle: 'text-xs' });
10291048
playerContextMenu?.contextMenuToggle(e, true);
10301049
}

0 commit comments

Comments
 (0)