ngx-scanner: v3.2.0: Camera does not get freed after route change

I am using Angular v12. ngx-scanner v3.2.0. Chrome browser under Android 9

Initialy the scanner works fine, but after a route change the camera does not get freed and therefore when entering the scanner page again it can not get a video stream resulting in an error.

When I switch back to v3.0.0 by using npm install @zxing/ngx-scanner@3.0.0 --force everything works fine again.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 15 (5 by maintainers)

Commits related to this issue

Most upvoted comments

@Daskii I’m testing the new code now… Hopefully a new release will be published in the upcoming days.

Sorry guys I don’t have time to maintain the component but if anybody can make a PR I’ll be happy to merge it and create a new release

I had a better look in the problem. It is indeed creating multiple streams but it is not problem in browser library. Functions from browser library returnis scannerControls and zxing-component is responsible for stopping the stream. In zxing-component functions ngOnInit() and enable(boolean) are called one after another realy fast which is causing multiple streams to be opened and only last one will be used and closed while first one will be untrackable. Probably because async function calls and Promise.

Removing init() call from ngOnInit() on zxing-component solves this problem.

But if you change enable state (to true, false, true, false, true) multiple times in realy short period, it would also create multiple streams that are not trackable and only last one will be used and closed. This issue can be fixed in Browser library by introducing monitoring over created streams, and calling a function that will release all of them when ngOnClose() is called.

Here is the patch for browser library: [#425]_-_v3_2_0__Camera_does_not_get_freed_after_route_change.patch.txt

And here it is for zxing-scanner component: [#425]_-_v3_2_0__Camera_does_not_get_freed_after_route_change_zxing.patch.txt

I think that i found the issue but maybe i am wrong. I am using Chrome and camera does not get freed after route change.

I have noticed that navigator.media.Devices.getUserMedia({video: true}) is used in multiple places (library, browser and ngx-scanner).

I did a simple experiment like this:

var streams = []; streams.push(navigator.mediaDevices.getUserMedia({ video: true })); streams.push(navigator.mediaDevices.getUserMedia({ video: true })); streams.push(navigator.mediaDevices.getUserMedia({ video: true }));

after this if i execute next line: streams[0].then(str => str.getTracks()[0].stop())

stream still stays open (i can see camera flash still in use and camera is still in use) after I execute same line for all items in streams:

streams[1].then(str => str.getTracks()[0].stop()) streams[2].then(str => str.getTracks()[0].stop())

camera is released.

I have noticed this behaviour on my machine, i wonder if someone else can notice it too. If this is the case, there should be only one place from where this camera source is provided (singleton use) and closed there before opening another stream.

I faced this issue with version 3.2.0. I have to use “@zxing/library”: “^0.18.6” , see https://github.com/zxing-js/ngx-scanner/issues/428#issuecomment-937800191

The camera is not freed when I use enabled attribute, example:

<zxing-scanner
      [enable]="enabled"
      previewFitMode="cover"
    >
</zxing-scanner>

If I completely remove this attribute, then everything works fine. During the tests I used desktop Chrome browser. It indicates with a red dot if camera is in use: image

Using this indicator it’s easy to tell when camera is freed or not.