Skip to content

Fix crash when expanding video player #37772

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 1 addition & 12 deletions src/components/Attachments/AttachmentView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function AttachmentView({
isHovered,
optionalVideoDuration,
}) {
const {updateCurrentlyPlayingURL, currentVideoPlayerRef} = usePlaybackContext();
const {updateCurrentlyPlayingURL} = usePlaybackContext();
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
Expand All @@ -115,17 +115,6 @@ function AttachmentView({
updateCurrentlyPlayingURL(isVideo ? source : null);
}, [isFocused, isVideo, source, updateCurrentlyPlayingURL, file, isUsedInAttachmentModal]);

// This should ensure we clean up any video references when closing the attachment modal as these only existed here in memory during attachment preview.
useEffect(
() => () => {
if (!isVideo) {
return;
}
currentVideoPlayerRef.current = null;
},
[isVideo, currentVideoPlayerRef],
);
Comment on lines -118 to -127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure that this removal doesn't cause regression of #37142.


const [imageError, setImageError] = useState(false);

useNetwork({onReconnect: () => setImageError(false)});
Expand Down
19 changes: 18 additions & 1 deletion src/components/VideoPlayer/BaseVideoPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function BaseVideoPlayer({
const canUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
const isCurrentlyURLSet = currentlyPlayingURL === url;
const isUploading = _.some(CONST.ATTACHMENT_LOCAL_URL_PREFIX, (prefix) => url.startsWith(prefix));
const shouldUseSharedVideoElementRef = useRef(shouldUseSharedVideoElement);

const togglePlayCurrentVideo = useCallback(() => {
videoResumeTryNumber.current = 0;
Expand Down Expand Up @@ -145,8 +146,24 @@ function BaseVideoPlayer({
}, [currentVideoPlayerRef, handleFullscreenUpdate, handlePlaybackStatusUpdate]);

useEffect(() => {
if (!isUploading) {
return;
}
// If we are uploading a new video, we want to immediately set the video player ref.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use isUploading condition here because in attachment carousel with multiple video player on small screen, the video player ref is set to the wrong video player.

Copy link
Contributor Author

@bernhardoj bernhardoj Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, if we have video A and B in carousel, and we open video B, it could be possible the video A video player is set as the ref when both component mounted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If we are uploading a new video, we want to immediately set the video player ref.
// If we are uploading a new video, we want to immediately set the video player ref.

currentVideoPlayerRef.current = videoPlayerRef.current;
}, [url, currentVideoPlayerRef]);
}, [url, currentVideoPlayerRef, isUploading]);

useEffect(() => {
shouldUseSharedVideoElementRef.current = shouldUseSharedVideoElement;
}, [shouldUseSharedVideoElement]);

useEffect(() => () => {
if (shouldUseSharedVideoElementRef.current) {
return;
}
// If it's not a shared video player, clear the video player ref.
currentVideoPlayerRef.current = null;
}, [currentVideoPlayerRef]);

// update shared video elements
useEffect(() => {
Expand Down