1

I have a React App that uses the webcam to detect specific poses. As the title suggests, when I deploy the app locally, it works as expected.

However, when deploying the app to firebase hosting, the camera feed is black. I have provided the necessary permissions in Chrome and the website is being served securely over https. Curiously the camera light on my Macbook lights up green for a while but nothing shows up.

This is the relevant code in the camera component:

const PoseCamera = forwardRef(({ onCapture, poseType }, ref) => {
const videoRef = useRef(null);
const canvasRef = useRef(null);
const containerRef = useRef(null);
const [detector, setDetector] = useState(null);
const [isStreaming, setIsStreaming] = useState(false);
const [isInitializing, setIsInitializing] = useState(true);
const requestAnimationRef = useRef(null);
const localStreamRef = useRef(null);
const FRAMES_TO_HOLD = 30;
const poseBufferRef = useRef([]);
const currentPoseType = POSE_TYPES[poseType];
const SMOOTHING_WINDOW = 10; // Number of frames to average
const poseHistoryRef = useRef([]); // Store recent poses
const isReadyForDetection = useCallback(() => {
 if (!detector) {
 console.error("Pose detector is not initialized.");
 return false;
 }
 if (!videoRef.current) {
 console.error("Video element is not available.");
 return false;
 }
 if (!canvasRef.current) {
 console.error("Canvas element is not available.");
 return false;
 }
 if (!isStreaming) {
 console.error("Camera is not streaming.");
 return false;
 }
 return true;
}, [detector, isStreaming]);

... Which Throws: Video element is not available.


 const startCamera = useCallback(async () => {
 setIsInitializing(true);
 try {
 if (!window.isSecureContext) {
 console.error('Application not running in secure context - camera may not work');
 }
 const stream = await navigator.mediaDevices.getUserMedia({
 video: true,
 audio: false
 });
 console.log('Camera stream obtained:', stream);
 if (videoRef.current) {
 videoRef.current.srcObject = stream;
 console.log('Video element assigned stream:', videoRef.current.srcObject);
 localStreamRef.current = stream;
 videoRef.current.onplay = () => {
 console.log('Video is playing');
 };
 videoRef.current.onloadedmetadata = () => {
 const { videoWidth, videoHeight } = videoRef.current;
 if (canvasRef.current) {
 canvasRef.current.width = videoWidth;
 canvasRef.current.height = videoHeight;
 if (containerRef.current) {
 containerRef.current.style.aspectRatio = `${videoWidth}/${videoHeight}`;
 console.log('Canvas dimensions set:', videoWidth, videoHeight);
 const ctx = canvasRef.current.getContext('2d');
 ctx.drawImage(videoRef.current, 0, 0, canvasRef.current.width, canvasRef.current.height);
 }
 setIsStreaming(true);
 }
 };
 // Add canplay event listener to ensure video is ready
 videoRef.current.oncanplay = () => {
 console.log('Video is ready to play');
 videoRef.current.play();
 };
 } else {
 console.error('Video element is not available.');
 }
 } catch (error) {
 console.error('Error accessing camera:', error);
 } finally {
 setIsInitializing(false);
 }
}, []);

Screenshot of React App in Local Local Screenshot

Screenshot in Hosted App Hosted Screenshot

Are there additional permissions that I need to include for the firebase application?

Doug Stevenson
321k37 gold badges458 silver badges474 bronze badges
asked Jan 7, 2025 at 17:54

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.