-
Notifications
You must be signed in to change notification settings - Fork 13.8k
fix: media playback failing due to expired urls #36622
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
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad7cd5a
fix: media playback failing due to expired urls
abhinavkrin bdb1c1e
minor changes
abhinavkrin cac40c2
added changeset
abhinavkrin 0eab50e
added changeset
abhinavkrin f274799
changeset
abhinavkrin 8650a38
changeset
abhinavkrin 74af4fa
minor changes
abhinavkrin 98a80f0
minor change
abhinavkrin 49e62eb
requested changes
abhinavkrin 18e1ca8
Merge branch 'develop' into fix/media-playback-fails-with-expiring-urls
kodiakhq[bot] 5950773
Merge branch 'develop' into fix/media-playback-fails-with-expiring-urls
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fixes an issue where audio and video messages would stop playing if left idle past their link expiration. Now the player automatically refreshes expired links so users can continue listening or watching without reloading the chat. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
144 changes: 144 additions & 0 deletions
144
apps/meteor/client/components/message/content/attachments/file/hooks/useReloadOnError.tsx
This file contains hidden or 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,144 @@ | ||
| import { useEffectEvent } from '@rocket.chat/fuselage-hooks'; | ||
| import { useSafeRefCallback } from '@rocket.chat/ui-client'; | ||
| import { useCallback, useRef, useState } from 'react'; | ||
|
|
||
| const events = ['error', 'stalled', 'play']; | ||
| export const useReloadOnError = (url: string, type: 'video' | 'audio') => { | ||
| const [expiresAt, setExpiresAt] = useState<number | null>(null); | ||
| const isRecovering = useRef(false); | ||
| const firstRecoveryAttempted = useRef(false); | ||
|
|
||
| const getRedirectURLInfo = useCallback(async (url: string): Promise<{ redirectUrl: string | false; expires: number | null }> => { | ||
| const [path, query] = url.split('?'); | ||
| const params = new URLSearchParams(query); | ||
| params.set('replyWithRedirectUrl', 'true'); | ||
| const response = await fetch(`${path}?${params.toString()}`, { | ||
| credentials: 'same-origin', | ||
| }); | ||
|
abhinavkrin marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch URL info: ${response.statusText}`); | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| return { | ||
| redirectUrl: data.redirectUrl, | ||
| expires: data.expires ? new Date(data.expires * 1000).getTime() : null, | ||
| }; | ||
| }, []); | ||
|
|
||
| const renderBufferingUIFallback = useCallback((vidEl: HTMLVideoElement) => { | ||
| const computed = getComputedStyle(vidEl); | ||
|
|
||
| const videoTempStyles = { | ||
| width: vidEl.style.width, | ||
| height: vidEl.style.height, | ||
| }; | ||
| Object.assign(vidEl.style, { | ||
| width: computed.width, | ||
| height: computed.height, | ||
| }); | ||
|
|
||
| return () => { | ||
| Object.assign(vidEl.style, videoTempStyles); | ||
| }; | ||
| }, []); | ||
|
abhinavkrin marked this conversation as resolved.
Outdated
|
||
|
|
||
| const handleMediaURLRecovery = useEffectEvent(async (event: Event) => { | ||
| if (isRecovering.current) { | ||
| console.debug(`Media URL recovery already in progress, skipping ${event.type} event`); | ||
| return; | ||
| } | ||
| isRecovering.current = true; | ||
|
|
||
| const node = event.target as HTMLMediaElement | null; | ||
| if (!node) { | ||
| isRecovering.current = false; | ||
| return; | ||
| } | ||
|
|
||
| if (firstRecoveryAttempted.current && !expiresAt) { | ||
| console.debug('No expiration time set, skipping recovery'); | ||
| isRecovering.current = false; | ||
| return; | ||
| } | ||
| firstRecoveryAttempted.current = true; | ||
|
|
||
| if (expiresAt && Date.now() < expiresAt) { | ||
| console.debug('Media URL is still valid, skipping recovery'); | ||
| isRecovering.current = false; | ||
| return; | ||
| } | ||
|
|
||
| console.debug('Handling media URL recovery for event:', event.type); | ||
|
|
||
| let cleanup: (() => void) | undefined; | ||
| if (type === 'video') { | ||
| cleanup = renderBufferingUIFallback(node as HTMLVideoElement); | ||
| } | ||
|
|
||
| const wasPlaying = !node.paused; | ||
| const { currentTime } = node; | ||
|
|
||
| try { | ||
| const { redirectUrl: newUrl, expires: newExpiresAt } = await getRedirectURLInfo(url); | ||
| setExpiresAt(newExpiresAt); | ||
| node.src = newUrl || url; | ||
|
|
||
| const onCanPlay = async () => { | ||
| node.removeEventListener('canplay', onCanPlay); | ||
|
|
||
| node.currentTime = currentTime; | ||
| if (wasPlaying) { | ||
| try { | ||
| await node.play(); | ||
| } catch (playError) { | ||
| console.warn('Failed to resume playback after URL recovery:', playError); | ||
| } finally { | ||
| isRecovering.current = false; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const onMetaDataLoaded = () => { | ||
| node.removeEventListener('loadedmetadata', onMetaDataLoaded); | ||
| isRecovering.current = false; | ||
| cleanup?.(); | ||
| }; | ||
|
|
||
| node.addEventListener('canplay', onCanPlay, { once: true }); | ||
| node.addEventListener('loadedmetadata', onMetaDataLoaded, { once: true }); | ||
| node.load(); | ||
| } catch (err) { | ||
| console.error('Error during URL recovery:', err); | ||
| isRecovering.current = false; | ||
| cleanup?.(); | ||
| } | ||
| }); | ||
|
|
||
| const mediaRefCallback = useSafeRefCallback( | ||
| useCallback( | ||
| (node: HTMLAudioElement | null) => { | ||
| if (!node) { | ||
| return; | ||
| } | ||
|
|
||
| events.forEach((event) => { | ||
| node.addEventListener(event, handleMediaURLRecovery); | ||
| }); | ||
| return () => { | ||
| if (!node) { | ||
| return; | ||
| } | ||
|
abhinavkrin marked this conversation as resolved.
Outdated
|
||
| events.forEach((event) => { | ||
| node.removeEventListener(event, handleMediaURLRecovery); | ||
| }); | ||
| }; | ||
| }, | ||
| [handleMediaURLRecovery], | ||
| ), | ||
| ); | ||
|
|
||
| return { mediaRef: mediaRefCallback }; | ||
| }; | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.