mobile_scanner icon indicating copy to clipboard operation
mobile_scanner copied to clipboard

MobileScanner: Called stop() while already stopped!

Open Arcttyx opened this issue 2 years ago • 13 comments

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!

Arcttyx avatar Jul 12 '22 19:07 Arcttyx

Any updates?

MrSobirov avatar Jul 16 '22 05:07 MrSobirov

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();

elmar001 avatar Jul 25 '22 12:07 elmar001

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 :/

Arcttyx avatar Aug 09 '22 21:08 Arcttyx

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.

elmar001 avatar Aug 09 '22 22:08 elmar001

Same issue...

kamami avatar Aug 15 '22 13:08 kamami

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

novas1r1 avatar Aug 27 '22 15:08 novas1r1

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

iUnknownWs avatar Aug 28 '22 06:08 iUnknownWs

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 avatar Aug 30 '22 12:08 elmar001

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.

elmar001 avatar Aug 30 '22 12:08 elmar001

Any update??

akkalbhor avatar Sep 26 '22 12:09 akkalbhor

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

juliansteenbakker avatar Sep 28 '22 19:09 juliansteenbakker

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

Arcttyx avatar Oct 05 '22 17:10 Arcttyx

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

DevNico avatar Oct 13 '22 09:10 DevNico

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 avatar Nov 24 '22 05:11 maulikchak

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

maulikchak avatar Nov 28 '22 03:11 maulikchak

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.

DevNico avatar Nov 28 '22 10:11 DevNico

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

EArminjon avatar Nov 29 '22 16:11 EArminjon

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

EArminjon avatar Nov 29 '22 16:11 EArminjon

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.

Srj-Love avatar Dec 01 '22 08:12 Srj-Love

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

juliansteenbakker avatar Dec 13 '22 15:12 juliansteenbakker