Skip to content

Commit

Permalink
Merge pull request #329 from ericblade/fix266
Browse files Browse the repository at this point in the history
testing bug fix for android 11 freezing and other chromes not releasing camera
  • Loading branch information
ericblade authored Feb 27, 2021
2 parents bcefbc1 + 8552cce commit 7cf932c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ericblade/quagga2",
"version": "1.2.1",
"version": "1.2.3",
"description": "An advanced barcode-scanner written in JavaScript",
"main": "lib/quagga.js",
"types": "type-definitions/quagga.d.ts",
Expand Down
24 changes: 17 additions & 7 deletions src/input/camera_access.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pick from 'lodash/pick';
import { getUserMedia, enumerateDevices } from '../common/mediaDevices';
import { MediaTrackConstraintsWithDeprecated } from '../../type-definitions/quagga.d';
import { MediaTrackConstraintsWithDeprecated, QuaggaJSCameraAccess as CameraAccessType } from '../../type-definitions/quagga.d';

let streamRef: MediaStream | null;

Expand Down Expand Up @@ -92,18 +92,28 @@ function getActiveTrack(): MediaStreamTrack | null {
/**
* Used for accessing information about the active stream track and available video devices.
*/
const QuaggaJSCameraAccess = {
const QuaggaJSCameraAccess: CameraAccessType = {
requestedVideoElement: null,
async request(video: HTMLVideoElement, videoConstraints?: MediaTrackConstraintsWithDeprecated): Promise<any> {
QuaggaJSCameraAccess.requestedVideoElement = video;
const newConstraints = await pickConstraints(videoConstraints);
return initCamera(video, newConstraints);
},
release(): void {
// TODO: i wonder if telling the Video element to pause() before calling MediaStreamTrack.stop() would alleviate some of the issues with the camera appearing to stay open on Android even after stopping.
release(): Promise<void> {
const tracks = streamRef && streamRef.getVideoTracks();
if (tracks && tracks.length) {
tracks[0].stop();
if (QuaggaJSCameraAccess.requestedVideoElement !== null) {
QuaggaJSCameraAccess.requestedVideoElement.pause();
}
streamRef = null;
return new Promise<void>((resolve) => {
setTimeout(() => {
if (tracks && tracks.length) {
tracks[0].stop();
}
streamRef = null;
QuaggaJSCameraAccess.requestedVideoElement = null;
resolve();
});
});
},
enumerateVideoDevices,
getActiveStreamLabel(): string {
Expand Down
2 changes: 1 addition & 1 deletion src/input/test/browser/camera_access.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('CameraAccess (browser)', () => {
it('works', async () => {
const video = document.createElement('video');
await Quagga.CameraAccess.request(video, {});
Quagga.CameraAccess.release();
await Quagga.CameraAccess.release();
expect(((video?.srcObject) as any)?.active).to.equal(false);
});
});
Expand Down
6 changes: 3 additions & 3 deletions src/quagga.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import merge from 'lodash/merge';
import TypeDefs from './common/typedefs'; // eslint-disable-line no-unused-vars
import ImageWrapper from './common/image_wrapper';
import BarcodeDecoder from './decoder/barcode_decoder';
Expand All @@ -7,7 +8,6 @@ import CameraAccess from './input/camera_access';
import ImageDebug from './common/image_debug';
import ResultCollector from './analytics/result_collector';
import Config from './config/config';
import merge from 'lodash/merge';

import Quagga from './quagga/quagga';

Expand Down Expand Up @@ -39,10 +39,10 @@ const QuaggaJSStaticInterface = {
return promise;
},
start: function () {
instance.start();
return instance.start();
},
stop: function () {
instance.stop();
return instance.stop();
},
pause: function () {
_context.stopped = true;
Expand Down
4 changes: 2 additions & 2 deletions src/quagga/quagga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ export default class Quagga {
}
}

stop(): void {
async stop(): Promise<void> {
this.context.stopped = true;
QWorkers.adjustWorkerPool(0);
if (this.context.config?.inputStream && this.context.config.inputStream.type === 'LiveStream') {
CameraAccess.release();
await CameraAccess.release();
this.context.inputStream.clearEventHandlers();
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/test-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('testing node import', () => {
const { CameraAccess: CA } = Q;
expect(CA).to.be.an('object').with.keys([
'request', 'release', 'enumerateVideoDevices',
'getActiveStreamLabel', 'getActiveTrack',
'getActiveStreamLabel', 'getActiveTrack', 'requestedVideoElement',
]);
});
});
Expand Down
11 changes: 6 additions & 5 deletions type-definitions/quagga.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ export interface QuaggaJSStatic {
init(
config: QuaggaJSConfigObject,
callback?: (err: any) => void
): void;
): Promise<void>;

init(
config: QuaggaJSConfigObject,
callback: (err: any) => void,
imageWrapper: ImageWrapper,
): void;
): Promise<void>;

/**
* When the library is initialized, the start()
Expand All @@ -139,7 +139,7 @@ export interface QuaggaJSStatic {
* Additionally, if a camera-stream was requested upon initialization,
* this operation also disconnects the camera.
*/
stop(): void;
stop(): Promise<void>;

/**
* Pauses processing, but does not release any handlers
Expand Down Expand Up @@ -220,9 +220,10 @@ export interface QuaggaJSStatic {
* Used for accessing information about the active stream track and available video devices.
*/
export interface QuaggaJSCameraAccess {
request(video: HTMLVideoElement, videoConstraints: MediaTrackConstraintsWithDeprecated): Promise<void> | never;
requestedVideoElement: HTMLVideoElement | null,
request(video: HTMLVideoElement, videoConstraints?: MediaTrackConstraintsWithDeprecated): Promise<void> | never;

release(): void;
release(): Promise<void>;

enumerateVideoDevices(): Promise<MediaDeviceInfo[]> | never;

Expand Down

0 comments on commit 7cf932c

Please sign in to comment.