Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve scrubbing in Safari with throttle and debounce on update events #597

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import videojs from 'video.js';
import { timeToHHmmss } from '@Services/utility-helpers';
import { IS_MOBILE, IS_SAFARI } from '@Services/browser';

const vjsComponent = videojs.getComponent('Component');

Expand Down Expand Up @@ -87,6 +88,16 @@ function CurrentTimeDisplay({ player, options }) {
setInitTime(0);
};

/*
In Safari, when player is paused selecting and clicking on a
timepoint on the progress-bar doesn't update the currentTime immediately.
This event handler fixes this issue.
*/
player.on('seeked', () => {
if (IS_SAFARI && !IS_MOBILE) {
setCurrTime(player.currentTime());
}
});

// Update our timer after the user leaves full screen
player.on("fullscreenchange", (e) => {
Expand Down
42 changes: 28 additions & 14 deletions src/components/MediaPlayer/VideoJS/components/js/VideoJSProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ReactDOM from 'react-dom';
import videojs from 'video.js';
import '../styles/VideoJSProgress.scss';
import { IS_MOBILE, IS_IPAD, IS_SAFARI, IS_IPHONE } from '@Services/browser';
import debounce from 'lodash/debounce';
import throttle from 'lodash/throttle';

const vjsComponent = videojs.getComponent('Component');

Expand Down Expand Up @@ -336,7 +338,6 @@ function ProgressBar({
const abortableTimeupdateHandler = () => {
player.on('waiting', () => {
// Set the player's current time to scrubbed time
Dananji marked this conversation as resolved.
Show resolved Hide resolved
player.currentTime(progressRef.current);
cancelInterval();
});

Expand All @@ -354,7 +355,6 @@ function ProgressBar({
// Update progress bar with timeupdate in the player
const timeUpdateHandler = () => {
if (player.isDisposed() || player.ended() || player == null) { return; }
const iOS = player.hasClass("vjs-ios-native-fs");
let curTime;
// Initially update progress from the prop passed from Ramp,
// this accounts for structured navigation when switching canvases
Expand All @@ -364,14 +364,27 @@ function ProgressBar({
} else {
curTime = player.currentTime();
}
// Use debounced updates since, Safari desktop browsers need the extra
// update on 'seeked' event to timely update the progress bar.
if (IS_SAFARI && !IS_MOBILE) {
debounce(
() => { onTimeUpdate(curTime); }
);
} else {
onTimeUpdate(curTime);
}
setInitTime(0);
};

const onTimeUpdate = (curTime) => {
// This state update caused weird lagging behaviors when using the iOS native
// video player. iOS player handles its own progress bar, so we can skip the
// update here only for video.
if (!(iOS && !player.audioOnlyMode_)) {
const iOS = player.hasClass("vjs-ios-native-fs");
if (!(iOS && !player.audioOnlyMode_) && !player.paused()) {
setProgress(curTime);
};
handleTimeUpdate(curTime);
setInitTime(0);
};

/*
Expand All @@ -380,7 +393,7 @@ function ProgressBar({
handler fixes this issue.
*/
player.on('seeked', () => {
if (IS_SAFARI) {
if (IS_SAFARI && !IS_MOBILE) {
handleTimeUpdate(progressRef.current);
}
});
Expand Down Expand Up @@ -417,17 +430,18 @@ function ProgressBar({
* (progress bar) to seek to a particular time point
* @param {Object} e onChange event for input range
*/
const updateProgress = (e) => {
let time = currentTime;
const updateProgress = throttle(
() => {
let time = currentTime;

if (activeSrcIndex > 0) time -= targets[activeSrcIndex].altStart;
if (activeSrcIndex > 0) time -= targets[activeSrcIndex].altStart;

const { start, end } = canvasTimes;
if (time >= start && time <= end) {
player.currentTime(time);
setProgress(time);
}
};
const { start, end } = canvasTimes;
if (time >= start && time <= end) {
player.currentTime(time);
setProgress(time);
}
}, 10);

/**
* Handle onMouseMove event for the progress bar, using the event
Expand Down