react-native-vision-camera icon indicating copy to clipboard operation
react-native-vision-camera copied to clipboard

🐛 V3 - Camera not initialized in Android when video prop is set to true.

Open dsipahioglu opened this issue 3 years ago • 18 comments

What's happening?

I'm trying to record videos, but the camera output is black in Android even when the permissions are given. I'm giving the permissions from app settings to create this issue faster. I have no issues in taking photos, and onInitialized is fired as expected, but when the video prop is set to true, onInitialized is never fired.

Project information:

"react-native": "0.72.3",
"react-native-reanimated": "^3.3.0",
"react-native-vision-camera": "^3.0.0",

Node version: v16.20.1

Reproduceable Code

import { useIsFocused } from '@react-navigation/native';
import React, {
  useCallback,
  useEffect, useRef
} from 'react';
import { Button, View } from 'react-native';
import { Camera, useCameraDevices } from 'react-native-vision-camera';

function TryNewCameraScreen() {
  const cameraRef = useRef(null);
  const isFocused = useIsFocused();

  const devices = useCameraDevices('wide-angle-camera');
  const device = devices.back;

  const onCameraInitialized = useCallback(() => console.log('camera initialized'), []);

  useEffect(() => {
    if (device) {
      console.log('device is', device);
    }
  }, [device]);

  useEffect(() => {
    if (cameraRef) {
      console.log('cameraRef is', cameraRef);
    }
  }, [cameraRef]);

  const takePicture = async () => {
    if (cameraRef.current !== null) {
      const image = await cameraRef.current.takePhoto();
      console.log('image', image);
    }
  };

  const recordVideo = async () => {
    try {
      cameraRef.current.startRecording({
        flash: 'on',
        onRecordingFinished: (video2) => console.log('success', video2),
        onRecordingError: (error) => console.log('error', error),
      });
    } catch (e) {
      console.log('err', e);
    }
  };

  const stopRecording = async () => {
    try {
      await cameraRef.current.stopRecording();
    } catch (e) {
      console.log('err', e);
    }
  };

  return (
    <View>
      {device == null ? <></> : (
        <>
          <Camera
            ref={cameraRef}
            style={{ height: '80%' }}
            isActive={isFocused}
            video
            audio={false}
            photo
            device={device}
            onInitialized={onCameraInitialized}
          />
          <View style={{ flexDirection: 'row', justifyContent: 'space-evenly' }}>
            <Button
              color="#841584"
              accessibilityLabel="TAKE PICTURE"
              title="TAKE PICTURE"
              onPress={takePicture}
            />
            <Button
              color="#841584"
              accessibilityLabel="CAPTURE VIDEO"
              title="CAPTURE VIDEO"
              onPress={recordVideo}
            />
            <Button
              color="#841584"
              accessibilityLabel="CAPTURE VIDEO"
              title="STOP VIDEO"
              onPress={stopRecording}
            />
          </View>
        </>
      )}
    </View>
  );
}

export default TryNewCameraScreen;

Relevant log output

error {
    "userInfo": null,
    "message": "[session/camera-not-ready] The Camera is not ready yet! Wait for the onInitialized() callback!",
    "cause": {
        "stacktrace": "com.mrousavy.camera.CameraNotReadyError: [session/camera-not-ready] The Camera is not ready yet! Wait for the onInitialized() callback!\n\tat com.mrousavy.camera.core.CameraSession.startRecording(CameraSession.kt:265)\n\tat com.mrousavy.camera.CameraView_RecordVideoKt.startRecording(CameraView+RecordVideo.kt:47)\n\tat com.mrousavy.camera.CameraViewModule$startRecording$1.invokeSuspend(CameraViewModule.kt:85)\n\tat kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)\n\tat kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)\n\tat kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)\n\tat kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)\n\tat kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)\n\tat kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)\n",
        "message": "[session/camera-not-ready] The Camera is not ready yet! Wait for the onInitialized() callback!"
    },
    "code": "session/camera-not-ready"
}

Camera Device

{
  "formats": [Size too large],

  "hardwareLevel": "level-3",
  "pixelFormats": [
    "RAW_SENSOR",
    "JPEG",
    "PRIVATE",
    "YUV_420_888",
    "RAW_PRIVATE",
    "RAW10"
  ],
  "maxZoom": 8,
  "minZoom": 1,
  "supportsLowLightBoost": false,
  "sensorOrientation": "landscape-right",
  "supportsDepthCapture": false,
  "neutralZoom": 1,
  "supportsFocus": true,
  "supportsRawCapture": true,
  "isMultiCam": false,
  "hasTorch": true,
  "devices": [
    "wide-angle-camera"
  ],
  "hasFlash": true,
  "name": "BACK (0)",
  "position": "back",
  "id": "0"
}

Device

Xiaomi Redmi Note 8 - Android 11 - MIUI Global 12.5.2

VisionCamera Version

3.0.0

Can you reproduce this issue in the VisionCamera Example app?

  • [ ] I can reproduce the issue in the VisionCamera Example app.

Additional information

dsipahioglu avatar Sep 05 '23 19:09 dsipahioglu

Same issue on Redmi Note 5G

jide avatar Sep 05 '23 20:09 jide

Same issue on Asus Zenfone 8

bjornhutter avatar Sep 06 '23 07:09 bjornhutter

enabling frameProcessor and installing react-native-worklets-core solved the issue.

Maybe required C++ part of VideoPipeline is being built if only frameProcessors is enabled.

Reproduced same issue with example project by removing react-native-worklets-core and disabling frameProcessor

kocaagasefa avatar Sep 06 '23 12:09 kocaagasefa

I observed the same behavior, video only works when frameProcessors are enabled!

iernstig avatar Sep 08 '23 14:09 iernstig

image

fedu44 avatar Sep 12 '23 15:09 fedu44

Please share your adb logcat logs.

mrousavy avatar Sep 14 '23 19:09 mrousavy

Please share your adb logcat logs.

No implementation found for com.facebook.jni.HybridData com.mrousavy.camera.core.VideoPipeline.initHybrid(int, int) (tried Java_com_mrousavy_camera_core_VideoPipeline_initHybrid and Java_com_mrousavy_camera_core_VideoPipeline_initHybrid__II)
E/CameraView: Failed to configure session: No implementation found for com.facebook.jni.HybridData com.mrousavy.camera.core.VideoPipeline.initHybrid(int, int) (tried Java_com_mrousavy_camera_core_VideoPipeline_initHybrid and Java_com_mrousavy_camera_core_VideoPipeline_initHybrid__II)
    java.lang.UnsatisfiedLinkError: No implementation found for com.facebook.jni.HybridData com.mrousavy.camera.core.VideoPipeline.initHybrid(int, int) (tried Java_com_mrousavy_camera_core_VideoPipeline_initHybrid and Java_com_mrousavy_camera_core_VideoPipeline_initHybrid__II)
        at com.mrousavy.camera.core.VideoPipeline.initHybrid(Native Method)
        at com.mrousavy.camera.core.VideoPipeline.<init>(VideoPipeline.kt:51)
        at com.mrousavy.camera.core.outputs.CameraOutputs.<init>(CameraOutputs.kt:122)
        at com.mrousavy.camera.core.CameraSession.configureSession(CameraSession.kt:137)
        at com.mrousavy.camera.CameraView.configureSession(CameraView.kt:205)
...

I searched for this issue, and that you have said https://github.com/mrousavy/react-native-vision-camera/pull/1789 might help. I'll try this asap.

dsipahioglu avatar Sep 14 '23 20:09 dsipahioglu

Hey - this should be fixed with the latest update in VisionCamera V3 (🥳) - if not, please create a new issue.

If your issue has been fixed, consider sponsoring me on GitHub to say thanks 💖

mrousavy avatar Sep 30 '23 10:09 mrousavy

bug on example camera no reload when app is in background and back to foreground fixed adding {device != null && **isActive &&** (...

R4tch3t avatar Oct 17 '23 05:10 R4tch3t

not fixed

ombogdanJoinToIt avatar Oct 17 '23 05:10 ombogdanJoinToIt

not fixed

any sugestion ?... ONLY for worklets try, background to foreground:

const [reRender, setReRender] = useState(false)
useEffect(()=>{
    if(!isActive){
      setReRender(true)
    }else{
      setTimeout(()=>{
         setReRender(false)
       },333)
    }
  },[isActive])

if(reRender){
    return <></>
}

other feature example change FPS:

onPress={() => {
      setReRender(true)              
      setTimeout(()=>{
        setTargetFps((t) => (t === 30 ? 60 : 30))
        setReRender(false)
      },50)
}}

this updates not are a good experience... :/

R4tch3t avatar Oct 18 '23 02:10 R4tch3t

Has anyone fixed this? why this issue is closed if not fixed?

sumitthakur11 avatar Oct 29 '23 09:10 sumitthakur11

Has anyone fixed this? why this issue is closed if not fixed?

is fixed update all yours deps RNReanimated and Vision camera to latest, now if you're using worklets yes exist a problem when pixelFormat="yuv" but it's not the topic of this thread

R4tch3t avatar Oct 29 '23 15:10 R4tch3t

@mrousavy

This issue still persist for me. Any solution?

amanrathore18 avatar Nov 16 '23 16:11 amanrathore18

I still have this problem, this is what I receive in Sentry only when I generate the apk for production, in debug mode it works fine. how stressful.

unknown/unknown: [unknown/unknown] no "Lcom/facebook/jni/HybridData;" field "mHybridData" in class "Lcom/mrousavy/camera/core/VideoPipeline;" or its superclasses

DavichoStar avatar Nov 16 '23 19:11 DavichoStar

Update on my specific issue: The production app now works!!! I did several things to it but in the end it seems that it was deactivating proguard, I don't like it so much that I have to deactivate it but I like that the video works better.

def enableProguardInReleaseBuilds = false

I also added implementation 'com.facebook.fbjni:fbjni:0.5.1' to the dependencies of my build.gradle, but I don't think it was that because before proguard I was trying with previous versions, anyway I will leave it there so as not to move it much anymore

DavichoStar avatar Nov 16 '23 23:11 DavichoStar

@DavichoStar

Your solution is working fine for APK created by command line or android studio but when i've uploaded .aab bundle on PlayStore and downloaded the same from PlayStore in my device then i'm facing same issue.

amanrathore18 avatar Nov 17 '23 12:11 amanrathore18

@DavichoStar @amanrathore18 thanks, just fixed this in https://github.com/mrousavy/react-native-vision-camera/pull/2169 :) If you appreciate my work and dedication to the library please consider 💖 sponsoring me on GitHub 💖 to keep the project running!

mrousavy avatar Nov 17 '23 13:11 mrousavy