artoolkitx / arunityx

artoolkitX for Unity, providing high-performance video acquisition, marker and texture tracking and full Unity Editor support
https://www.artoolkitx.org/
Other
41 stars 22 forks source link

Cannot turn on camera on Android device #9

Closed chuen1118 closed 5 years ago

chuen1118 commented 5 years ago

Encountered some errors when running on Android device.

I only have this one device so could not test if this is a device specific issue, but no error when running on emulator.

ARproject I/CameraAccessHandlerImpl: CameraAccessHandlerImpl(): ctor called
ARproject I/AndroidUtils: onResume(): Cam2CaptureSurface constructed
ARproject I/CameraSurfaceImpl: surfaceCreated(): called
ARproject I/CameraSurfaceImpl: surfaceCreated(): will attempt to open camera "0", set orientation, set preview surface
ARproject I/CameraSurfaceImpl: surfaceChanged(): called
    openCamera2(): called
ARproject I/CameraManagerGlobal: Connecting to camera service
ARproject E/Unity: Unable to find __Internal

ARproject E/Unity: Unable to find __Internal This one seemed not affecting much

ARproject I/OpenCV: calling android_getCpuFeatures() ...
    calling android_getCpuFeatures() ... Done (1f7ff)
ARproject I/libAR: [info] artoolkitX v1.0.2 initalised.
ARproject I/Unity: ARController: ARToolKit version 1.0.2 initialised.

ARproject I/Unity: ARController: Starting AR.

ARproject I/Unity: ARController: Render device: OpenGL ES 3.2 NVIDIA 361.00.

ARproject I/Unity: ARController: Starting ARToolKit video with vconf ' -cachedir="/storage/emulated/0/Android/data/xxx/cache"'.

ARproject I/libAR: [info] Starting...
    [info] Setting video configuration ' -cachedir="/storage/emulated/0/Android/data/xxx/cache"'.
    [info] Opening artoolkitX video using configuration ' -cachedir="/storage/emulated/0/Android/data/xxx/cache"'.
    [info] Using supplied video config " -cachedir="/storage/emulated/0/Android/data/xxx/cache"".
ARproject I/Unity: ARController: UpdateAR: Waiting for ARToolKit video.

ARproject I/libAR: [info] Waiting for video source.
ARproject I/libAR: [info] Waiting for video.
ARproject I/Unity: Finished loading scene General_models in 1.713779 seconds

ARproject D/CameraEventListenerUnityImpl: cameraStreamStarted
ARproject I/ARX_jni: Attempting to load library ARX.
ARproject I/ARController: Loaded native library.
    ARController(): ARController constructor
ARproject I/libAR: [info] Opened artoolkitX video 640x480@1Bpp (AR_PIXEL_FORMAT_NV21).
    [info] Fetching external camera parameters.
ARproject I/libAR: [info] cparamSearch beginning search for google/Pixel C/dragon, camera 0, aspect ratio 4:3.
ARproject E/libAR: [error] ARController::androidVideoPush2: ARVideoSource is not running.
ARproject E/libAR: [error] ARController::androidVideoPush2: ARVideoSource is not running.

After 5 minutes of not running

ARproject E/libAR: [error] ARController::androidVideoPush2: ARVideoSource is not running.
ARproject E/libAR: [error] ARController::androidVideoPush2: ARVideoSource is not running.
ARproject I/libAR: [info] Fetched 5 camera calibration records from online database.
ARproject I/libAR: [info] Matched fetched camera calibration record (1440x1080, 0.00).
ARproject E/libAR: [error] ARController::androidVideoPush2: ARVideoSource is not running.
ARproject I/libAR: [info] Matched fetched camera calibration record (640x480, 0.00).
ARproject E/libAR: [error] ARController::androidVideoPush2: ARVideoSource is not running.
ARproject E/libAR: [error] ARController::androidVideoPush2: ARVideoSource is not running.
ARproject E/libAR: [error] ar2VideoPushAndroid2: Error: unexpected buffer size (368512) for format YUV_420_888.
ARproject I/libAR: [info] Video source is running. (Waited 14611 calls.)
ARproject I/Unity: LoadingText update

ARproject I/Unity: ARController: UpdateAR: ARToolKit video is running. Configuring Unity scene for video.

ARproject I/Unity: ARController: Video 640x480@1Bpp (AR_PIXEL_FORMAT_NV21)

ARproject I/Unity: ARController: Projection matrix: [
    18.40825    0.00000 -0.00107    0.00000
    0.00000 24.52116    0.01076 0.00000
    0.00000 0.00000 -1.00401    -0.02004
    0.00000 0.00000 -1.00000    0.00000]

ARproject I/Unity: ARController: Video size 640x480 will use texture size 640x480.

ARproject I/Unity: ARController: Scene configured for video.

ARproject E/libAR: [error] ar2VideoPushAndroid2: Error: unexpected buffer size (368512) for format YUV_420_888.
ARproject E/libAR: [error] ar2VideoPushAndroid2: Error: unexpected buffer size (368512) for format YUV_420_888.

Error: unexpected buffer size (368512) for format YUV_420_888. What does this mean?

EDIT:

The device is running on Nougat (7.0.0) Camera is set to 640x480, thus the buffer size should be 406800, I don't understand where 368512 comes from. Here is what the planes from reader.acquireLatestImage() look like

Plane[0]

Plane[1]

Plane[2]

chuen1118 commented 5 years ago

Checked this post, android would add padding to image buffer, thus the Y-plane buffer size returned from ImageReader should be size = rowStride * height - (rowStride - width) in my case 768*480-(768-640) = 368512

In videoAndroid.c, https://github.com/artoolkitx/artoolkitx/blob/381ec933048cbb4dfffb4d8397c88f463d587f67/Source/ARX/ARVideo/Android/videoAndroid.c#L763-L766

Changed if ((vid->widthIn * vid->heightIn) != buf0Size) { to if ((buf0RowStride * vid->heightIn - (buf0RowStride - vid->widthIn)) != buf0Size) { problem solved.