juliansteenbakker / mobile_scanner

A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
BSD 3-Clause "New" or "Revised" License
756 stars 446 forks source link

MobileScanner: Called stop() while already stopped! #227

Closed Arcttyx closed 1 year ago

Arcttyx commented 1 year ago

I am getting an exception when navigating to another screen after scanning or just switching screens without scanning The camera attempts to complete a state change even when it is already stopped.

I use this code to start scanning:

     cameraController = MobileScannerController(
        facing: CameraFacing.back,
        formats: [BarcodeFormat.qrCode],
        torchEnabled: false,
      );

and I manually stop the camera to prevent it from working on the back when I already move on to the next pages

cameraController.stop();

debug console shows:

D/Camera2CameraImpl( 9667): {Camera@ed74e7b[id=0]} Use cases [androidx.camera.core.Preview-5ba484ec-9a34-4aca-a6a4-872db2421639225955340, androidx.camera.core.ImageAnalysis-752c8076-504c-4c7a-bc6f-29d067be6710101880661] now DETACHED for camera
D/UseCaseAttachState( 9667): All use case: [] for camera: 0
D/Camera2CameraImpl( 9667): {Camera@ed74e7b[id=0]} Resetting Capture Session
D/Camera2CameraImpl( 9667): {Camera@ed74e7b[id=0]} Releasing session in state OPENED
D/UseCaseAttachState( 9667): Active and attached use case: [] for camera: 0
D/UseCaseAttachState( 9667): Active and attached use case: [] for camera: 0
D/DeferrableSurface( 9667): surface closed,  useCount=1 closed=true androidx.camera.core.SurfaceRequest$2@64e4d37
D/DeferrableSurface( 9667): surface closed,  useCount=1 closed=true androidx.camera.core.impl.ImmediateSurface@f023c2
D/Camera2CameraImpl( 9667): {Camera@ed74e7b[id=0]} Closing camera.
D/Camera2CameraImpl( 9667): {Camera@ed74e7b[id=0]} Transitioning camera internal state: OPENED --> CLOSING
D/CameraStateRegistry( 9667): Recalculating open cameras:
D/CameraStateRegistry( 9667): Camera                                       State
D/CameraStateRegistry( 9667): -------------------------------------------------------------------
D/CameraStateRegistry( 9667): Camera@fcacc62[id=1]                         UNKNOWN
D/CameraStateRegistry( 9667): Camera@ed74e7b[id=0]                         CLOSING
D/CameraStateRegistry( 9667): -------------------------------------------------------------------
D/CameraStateRegistry( 9667): Open count: 1 (Max allowed: 1)
D/CameraStateMachine( 9667): New public camera state CameraState{type=CLOSING, error=null} from CLOSING and null
D/CameraStateMachine( 9667): Publishing new public camera state CameraState{type=CLOSING, error=null}
D/Camera2CameraImpl( 9667): {Camera@ed74e7b[id=0]} Resetting Capture Session
D/Camera2CameraImpl( 9667): {Camera@ed74e7b[id=0]} Releasing session in state CLOSING
D/CaptureSession( 9667): onSessionFinished()
W/eteselectorale( 9667): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/eteselectorale( 9667): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
E/BufferQueueProducer( 9667): [SurfaceTexture-0-9667-0] cancelBuffer: BufferQueue has been abandoned
I/chatty  ( 9667): uid=10550(com.mx.test.project) CameraX-core_ca identical 4 lines
E/BufferQueueProducer( 9667): [SurfaceTexture-0-9667-0] cancelBuffer: BufferQueue has been abandoned
W/Binder:9667_3( 9667): type=1400 audit(0.0:2349768): avc: denied { read } for name="u:object_r:persist_camera_prop:s0" dev="tmpfs" ino=11636 scontext=u:r:untrusted_app:s0:c38,c258,c512,c768 tcontext=u:object_r:persist_camera_prop:s0 tclass=file permissive=0
E/libc    ( 9667): Access denied finding property "vendor.camera.aux.packagelist"
W/Binder:9667_3( 9667): type=1400 audit(0.0:2349769): avc: denied { read } for name="u:object_r:persist_camera_prop:s0" dev="tmpfs" ino=11636 scontext=u:r:untrusted_app:s0:c38,c258,c512,c768 tcontext=u:object_r:persist_camera_prop:s0 tclass=file permissive=0
E/libc    ( 9667): Access denied finding property "vendor.camera.aux.packagelist"
D/UseCaseAttachState( 9667): Active and attached use case: [] for camera: 0
D/DeferrableSurface( 9667): use count-1,  useCount=0 closed=true androidx.camera.core.SurfaceRequest$2@64e4d37
D/DeferrableSurface( 9667): Surface no longer in use[total_surfaces=2, used_surfaces=1](androidx.camera.core.SurfaceRequest$2@64e4d37}
D/DeferrableSurface( 9667): Surface terminated[total_surfaces=1, used_surfaces=1](androidx.camera.core.SurfaceRequest$2@64e4d37}
D/DeferrableSurface( 9667): use count-1,  useCount=0 closed=true androidx.camera.core.impl.ImmediateSurface@f023c2
D/DeferrableSurface( 9667): Surface no longer in use[total_surfaces=1, used_surfaces=0](androidx.camera.core.impl.ImmediateSurface@f023c2}
D/DeferrableSurface( 9667): Surface terminated[total_surfaces=0, used_surfaces=0](androidx.camera.core.impl.ImmediateSurface@f023c2}
D/Camera2CameraImpl( 9667): {Camera@ed74e7b[id=0]} CameraDevice.onClosed()
D/Camera2CameraImpl( 9667): {Camera@ed74e7b[id=0]} Transitioning camera internal state: CLOSING --> INITIALIZED
D/CameraStateRegistry( 9667): Recalculating open cameras:
D/CameraStateRegistry( 9667): Camera                                       State
D/CameraStateRegistry( 9667): -------------------------------------------------------------------
D/CameraStateRegistry( 9667): Camera@fcacc62[id=1]                         UNKNOWN
D/CameraStateRegistry( 9667): Camera@ed74e7b[id=0]                         CLOSED
D/CameraStateRegistry( 9667): -------------------------------------------------------------------
D/CameraStateRegistry( 9667): Open count: 0 (Max allowed: 1)
D/CameraStateMachine( 9667): New public camera state CameraState{type=CLOSED, error=null} from CLOSED and null
D/CameraStateMachine( 9667): Publishing new public camera state CameraState{type=CLOSED, error=null}
I/flutter ( 9667): MobileScanner: Called stop() while already stopped!
MrSobirov commented 1 year ago

Any updates?

elmar001 commented 1 year ago

Only workaround for this is immidiately after detecting QR Code put cameraController.stop(); and then if you need to open it again initialize it again: cameraController = MobileScannerController();

After putting it like this MobileScanner: Called stop() while already stopped! never showed up Seems like you almost use same code structure but in my case even first time I initialize it without parameter like this: MobileScannerController cameraController = MobileScannerController();

Arcttyx commented 1 year ago

Thanks @elmar001, I tried without parameters, but same result. I'm calling stop() right after scanning and before switch screen but I got the same exception in the console :/

elmar001 commented 1 year ago

Yes, even tho in my case it never showed 'Called stop() while already stopped!' never showed up again, the camera still comes black randomly. So I replaced the package with the older one - https://pub.dev/packages/qr_code_scanner for now. Seems like the new package doing something wrong inside, keeping it active even after stopping it explicitly.

kamami commented 1 year ago

Same issue...

novas1r1 commented 1 year ago

Is there a possibility to maybe have a status from the controller? So something like controller.isStopped?

iUnknownWs commented 1 year ago

I have the same problem but its occurs even if i block the device screen when the camera is open

elmar001 commented 1 year ago

Seems like there is no direct solution. I repaced this package with older one - https://pub.dev/packages/qr_code_scanner .Then observed same issue in that package as well. Perhaps both of these packages have the same component which causes the issue. Same problem - camera opens black and only solution is to kill the app and open again or using await controller.flipCamera();

elmar001 commented 1 year ago

Is there a possibility to maybe have a status from the controller? So something like controller.isStopped?

Yes, it would be helpful. I assume the problem is that it does not actually stop it so next requests fail and camera view come in black. As a temporary patch it may work: Every time before opening:

cameraController.switchCamera(); cameraController.switchCamera(); Switching camera should solve the issue but it needed to be used twice so rear camera view comes as a result.

akkalbhor commented 1 year ago

Any update??

juliansteenbakker commented 1 year ago

I've released version 3.0.0-beta.1 with many changes. Can you please check if this version fixes the issue?

Arcttyx commented 1 year ago

Thank you @juliansteenbakker for the new changes and this great package I tried using the version 3.0.0-beta.1, but the problem is still present

DevNico commented 1 year ago

Can confirm this issue still persists in 3.0.0-beta.1.

maulikchak commented 1 year ago

same here

I/flutter ( 6913): MobileScanner: Called stop() while already stopped!

[log] E/flutter ( 6913): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: mobile_scanner: Called start() while already starting.

E/flutter ( 6913): #0 MobileScannerController.start (package:mobile_scanner/src/mobile_scanner_controller.dart:129:7) E/flutter ( 6913): #1 ScanQRCodeScreenNewState.checkCameraPermission (package:app/ui/screenname.dart:47:25) E/flutter ( 6913): E/flutter ( 6913):

maulikchak commented 1 year ago

bug solved!! this is happening because we asking permission for camera access for 2 times, I checked my log and find this "Getting W/Activity: Can request only one set of permissions at a time" so I just remove camera permission from my side and it's automatically come when I initialize Mobile Scanner controller and the error goes and camera working perfectly so this issue not from the package side

DevNico commented 1 year ago

If this is confirmed to be the issue It would be necessary to allow disabling the permission request of this package since an app might want to handle that on its own.

EArminjon commented 1 year ago

If we stop the camera and then dispose, we got double call to stop()...

EArminjon commented 1 year ago

A system to check if the camera is running before calling stop() is mandatory... Cannot use this package in production because of that.

Srj-Love commented 1 year ago

Yeah! it's happened to me too now. I wanted to use NFC feature with camera, but in this case while disposing controller getting the stop issue.

juliansteenbakker commented 1 year ago

Please check with latest version v3.0.0-beta.4