-
Notifications
You must be signed in to change notification settings - Fork 19
ECHO-588 Better mechanism to keep a device awake (during recording) #389
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
Changes from all commits
Commits
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
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,126 @@ | ||
| import { useEffect, useRef } from "react"; | ||
|
|
||
| const MINIMAL_VIDEO_BASE64 = | ||
| "data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAAAu1tZGF0AAACrQYF//+r3EXpvebZSLeWLNgg2SPu73gyNjQgLSBjb3JlIDE2NCAtIEguMjY0L01QRUctNCBBVkMgY29kZWMgLSBDb3B5bGVmdCAyMDAzLTIwMjMgLSBodHRwOi8vd3d3LnZpZGVvbGFuLm9yZy94MjY0Lmh0bWwgLSBvcHRpb25zOiBjYWJhYz0xIHJlZj0zIGRlYmxvY2s9MTowOjAgYW5hbHlzZT0weDM6MHgxMTMgbWU9aGV4IHN1Ym1lPTcgcHN5PTEgcHN5X3JkPTEuMDA6MC4wMCBtaXhlZF9yZWY9MSBtZV9yYW5nZT0xNiBjaHJvbWFfbWU9MSB0cmVsbGlzPTEgOHg4ZGN0PTEgY3FtPTAgZGVhZHpvbmU9MjEsMTEgZmFzdF9wc2tpcD0xIGNocm9tYV9xcF9vZmZzZXQ9LTIgdGhyZWFkcz0xIGxv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d3N0YmwAAACXc3RzZAAAAAAAAAABAAAAh2F2YzEAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAACAAgASAAAAEgAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABj//wAAADFhdmNDAWQAFf/hABhnZAAVrNlAmBkwhAAAAwAQAAADAzg8WLZYAQAGaOvjyyLAAAAAHHV1aWRraEDyXyRPxbo5pRvPAyPzAAAAAAAAABhzdHRzAAAAAAAAAAEAAAABAAAEAAAAABRzdHNzAAAAAAAAAAEAAAABAAAADGN0dHMAAAAAAAAAAAAAACBzdHNjAAAAAAAAAAEAAAABAAAAAQAAAAEAAAAcc3RzegAAAAAAAAARAAAAAQAAAAxzdGNvAAAAAAAAAAEAAAAsAAAAYXVkdGEAAABZbWV0YQAAAAAAAAAhaGRscgAAAAAAAAAAbWRpcmFwcGwAAAAAAAAAAAAAAAAsaWxzdAAAACSpdG9vAAAAHGRhdGEAAAABAAAAAExhdmY2MC4xNi4xMDA="; | ||
|
|
||
| export const useVideoWakeLockFallback = ({ | ||
| isRecording, | ||
| isWakeLockActive, | ||
| }: { | ||
| isRecording: boolean; | ||
| isWakeLockActive: boolean; | ||
| }) => { | ||
| const videoRef = useRef<HTMLVideoElement | null>(null); | ||
| const checkIntervalRef = useRef<NodeJS.Timeout | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| // Only activate fallback if recording AND wakelock is not active | ||
| const shouldActivateFallback = isRecording && !isWakeLockActive; | ||
|
|
||
| if (!shouldActivateFallback) { | ||
| // Clean up video element if it exists | ||
| if (videoRef.current) { | ||
| videoRef.current.pause(); | ||
| videoRef.current.src = ""; | ||
| videoRef.current.remove(); | ||
| videoRef.current = null; | ||
| console.log( | ||
| "[VideoWakeLockFallback] Cleaned up video - wakelock is working", | ||
| ); | ||
| } | ||
| if (checkIntervalRef.current) { | ||
| clearInterval(checkIntervalRef.current); | ||
| checkIntervalRef.current = null; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // If video already exists, don't create a new one | ||
| // The cleanup function at the end of this effect will handle cleanup when needed | ||
| if (videoRef.current) { | ||
| console.log( | ||
| "[VideoWakeLockFallback] Video already active, skipping creation", | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| // Create a minimal 1x1 pixel video element | ||
| try { | ||
| console.log( | ||
| "[VideoWakeLockFallback] Activating video fallback - wakelock not active", | ||
| ); | ||
|
|
||
| // Create video element | ||
| const video = document.createElement("video"); | ||
| video.style.position = "fixed"; | ||
| video.style.top = "-9999px"; | ||
| video.style.left = "-9999px"; | ||
| video.style.width = "1px"; | ||
| video.style.height = "1px"; | ||
| video.style.opacity = "0"; | ||
| video.style.pointerEvents = "none"; | ||
| video.style.zIndex = "-9999"; | ||
| video.setAttribute("playsinline", "true"); | ||
| video.setAttribute("webkit-playsinline", "true"); | ||
| video.muted = true; | ||
| video.loop = true; | ||
| video.src = MINIMAL_VIDEO_BASE64; | ||
| videoRef.current = video; | ||
|
|
||
| // Append to DOM (required for iOS) | ||
| document.body.appendChild(video); | ||
|
|
||
| // Play the video | ||
| const playVideo = async () => { | ||
| try { | ||
| await video.play(); | ||
| console.log( | ||
| "[VideoWakeLockFallback] Silent 1-pixel video playing as fallback for wakelock", | ||
| ); | ||
| } catch (error) { | ||
| console.warn( | ||
| "[VideoWakeLockFallback] Failed to play fallback video:", | ||
| error, | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| playVideo(); | ||
|
|
||
| // Periodically check if video is still playing and restart if needed | ||
| // (iOS can sometimes pause background videos) | ||
| checkIntervalRef.current = setInterval(() => { | ||
| if (videoRef.current?.paused) { | ||
| console.log( | ||
| "[VideoWakeLockFallback] Video was paused, restarting...", | ||
| ); | ||
| playVideo(); | ||
| } | ||
| }, 5000); // Check every 5 seconds | ||
| } catch (error) { | ||
| console.error( | ||
| "[VideoWakeLockFallback] Failed to create fallback video:", | ||
| error, | ||
| ); | ||
| } | ||
|
|
||
| // Cleanup on unmount or when conditions change | ||
| return () => { | ||
| if (videoRef.current) { | ||
| videoRef.current.pause(); | ||
| videoRef.current.src = ""; | ||
| videoRef.current.remove(); | ||
| videoRef.current = null; | ||
| } | ||
| if (checkIntervalRef.current) { | ||
| clearInterval(checkIntervalRef.current); | ||
| checkIntervalRef.current = null; | ||
| } | ||
| }; | ||
| }, [isRecording, isWakeLockActive]); | ||
|
|
||
| return { | ||
| isActive: isRecording && !isWakeLockActive, | ||
| videoElement: videoRef.current, | ||
| }; | ||
| }; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Consider extracting cleanup logic to avoid duplication.
The cleanup code at lines 22-34 and lines 109-118 is nearly identical. Not a blocker, but could DRY this up with a helper:
Then call
cleanupVideo(videoRef, checkIntervalRef)in both places. Optional refactor.Also applies to: 107-119
🤖 Prompt for AI Agents