Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 2 additions & 4 deletions src/components/VideoPlayer/BaseVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import useNetwork from '@hooks/useNetwork';
import useReportOrReportDraft from '@hooks/useReportOrReportDraft';
import useThemeStyles from '@hooks/useThemeStyles';

import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
import {isSafari} from '@libs/Browser';
import {canUseTouchScreen as canUseTouchScreenLib} from '@libs/DeviceCapabilities';

Expand All @@ -33,7 +32,7 @@ import {scheduleOnRN} from 'react-native-worklets';
import type VideoPlayerProps from './types';

import useHandleNativeVideoControls from './useHandleNativeVideoControls';
import * as VideoUtils from './utils';
import {buildVideoSourceURL} from './utils';
import VideoErrorIndicator from './VideoErrorIndicator';
import VideoPlayerControls from './VideoPlayerControls';

Expand Down Expand Up @@ -78,8 +77,7 @@ function BaseVideoPlayer(props: BaseVideoPlayerProps) {
const [duration, setDuration] = useState(videoDuration);
const [isEnded, setIsEnded] = useState(false);
const [isFirstLoad, setIsFirstLoad] = useState(true);
// we add "#t=0.001" at the end of the URL to skip first millisecond of the video and always be able to show proper video preview when video is paused at the beginning
const [sourceURL] = useState(() => VideoUtils.addSkipTimeTagToURL(url.includes('blob:') || url.includes('file:///') ? url : addEncryptedAuthTokenToURL(url, encryptedAuthToken), 0.001));
const [sourceURL] = useState(() => buildVideoSourceURL(url, encryptedAuthToken));
const isPopoverVisible = useIsPopoverVisible();
const [controlStatusState, setControlStatusState] = useState(controlsStatus);
const controlsOpacity = useSharedValue(1);
Expand Down
22 changes: 21 additions & 1 deletion src/components/VideoPlayer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
import {isMobileWebKit} from '@libs/Browser';

/**
* Skip the first millisecond so a paused video can still show a proper preview frame.
* Applied via a `#t=` URL fragment.
*/
const VIDEO_SKIP_TIME_SECONDS = 0.001;

/**
* Converts seconds to '[hours:]minutes:seconds' format
*/
Expand All @@ -24,4 +31,17 @@ function addSkipTimeTagToURL(url: string, seconds: number) {
return `${url}#t=${seconds}`;
}

export {convertSecondsToTime, addSkipTimeTagToURL};
/**
* Builds the playable video source URL. Remote attachments get an auth token.
* All URLs get the skip-time tag when supported.
*/
function buildVideoSourceURL(url: string, encryptedAuthToken: string) {
if (!url) {
return url;
}

const sourceWithAuth = url.includes('blob:') || url.includes('file:///') ? url : addEncryptedAuthTokenToURL(url, encryptedAuthToken);
return addSkipTimeTagToURL(sourceWithAuth, VIDEO_SKIP_TIME_SECONDS);
}

export {convertSecondsToTime, buildVideoSourceURL};
11 changes: 8 additions & 3 deletions src/components/VideoPlayerPreview/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {useSession} from '@components/OnyxListItemProvider';
import {useIsOnSearch} from '@components/Search/SearchScopeProvider';
import VideoPlayer from '@components/VideoPlayer';
import IconButton from '@components/VideoPlayer/IconButton';
import {buildVideoSourceURL} from '@components/VideoPlayer/utils';
import {usePlaybackActionsContext, usePlaybackStateContext} from '@components/VideoPlayerContexts/PlaybackContext';

import useCheckIfRouteHasRemainedUnchanged from '@hooks/useCheckIfRouteHasRemainedUnchanged';
Expand Down Expand Up @@ -76,12 +78,15 @@ function VideoPlayerPreview({videoUrl, thumbnailUrl, reportID, fileName, videoDi
const isOnSearch = useIsOnSearch();
const navigation = useNavigation();
const {isOffline} = useNetwork();
const session = useSession();
const encryptedAuthToken = session?.encryptedAuthToken ?? '';
const sourceURL = buildVideoSourceURL(videoUrl, encryptedAuthToken);

// While offline, render BaseVideoPlayer instead of the thumbnail so the existing player-level offline state is shown consistently.
const shouldRenderVideoPlayer = !isDeleted && (isOffline || (!isSmallScreenWidth && !isThumbnail));

useEffect(() => {
if (!videoUrl || getPlatform() !== CONST.PLATFORM.WEB) {
if (!sourceURL || getPlatform() !== CONST.PLATFORM.WEB) {
return;
}
const video = document.createElement('video');
Expand All @@ -94,12 +99,12 @@ function VideoPlayerPreview({videoUrl, thumbnailUrl, reportID, fileName, videoDi
height: video.videoHeight,
});
};
video.src = videoUrl;
video.src = sourceURL;
video.load();
return () => {
video.src = '';
};
}, [videoUrl, videoDimensions.width, videoDimensions.height]);
}, [sourceURL, videoDimensions.width, videoDimensions.height]);

// We want to play the video only when the user is on the page where it was initially rendered
const doesUserRemainOnFirstRenderRoute = useCheckIfRouteHasRemainedUnchanged(videoUrl);
Expand Down
Loading