Skip to content

Commit

Permalink
Merge pull request zxing-js#73 from lazoapostolovski/master
Browse files Browse the repository at this point in the history
  • Loading branch information
werthdavid committed Nov 7, 2021
2 parents eaefab4 + 46f5bd4 commit 4a9a020
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/readers/BrowserCodeReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Reader,
Result,
} from '@zxing/library';
import { ErrorCorrectionLevelValues } from '@zxing/library/esm/core/qrcode/decoder/ErrorCorrectionLevel';
import { DecodeContinuouslyCallback } from '../common/DecodeContinuouslyCallback';
import { HTMLCanvasElementLuminanceSource } from '../common/HTMLCanvasElementLuminanceSource';
import { HTMLVisualMediaElement } from '../common/HTMLVisualMediaElement';
Expand Down Expand Up @@ -96,7 +95,9 @@ export class BrowserCodeReader {
} catch (err) {
// some browsers may not be compatible with ImageCapture
// so we are ignoring this for now.
// tslint:disable-next-line:no-console
console.error(err);
// tslint:disable-next-line:no-console
console.warn('Your browser may be not fully compatible with WebRTC and/or ImageCapture specs. Torch will not be available.');
return false;
}
Expand Down Expand Up @@ -414,6 +415,19 @@ export class BrowserCodeReader {
}
}

/**
* Stops all media streams that are created.
*/
public static releaseAllStreams() {
if (BrowserCodeReader.streamTracker.length !== 0) {
// tslint:disable-next-line:no-console
BrowserCodeReader.streamTracker.forEach((mediaStream) => {
mediaStream.getTracks().forEach((track) => track.stop());
});
}
BrowserCodeReader.streamTracker = [];
}

/**
* Waits for a video to load and then hits play on it.
* To accomplish that, it binds listeners and callbacks to the video element.
Expand Down Expand Up @@ -480,6 +494,12 @@ export class BrowserCodeReader {
return videoElement;
}

/**
* Keeps track to created media streams.
* @private there is no need this array to be accessible from outside.
*/
private static streamTracker: MediaStream[] = [];

/**
* Returns a Promise that resolves when the given image element loads.
*/
Expand Down Expand Up @@ -525,7 +545,7 @@ export class BrowserCodeReader {
* Standard method to dispose a media stream object.
*/
private static disposeMediaStream(stream: MediaStream) {
stream.getVideoTracks().forEach(x => x.stop());
stream.getVideoTracks().forEach((x) => x.stop());
stream = undefined;
}

Expand Down Expand Up @@ -633,8 +653,7 @@ export class BrowserCodeReader {

BrowserCodeReader.checkCallbackFnOrThrow(callbackFn);

const stream = await navigator.mediaDevices.getUserMedia(constraints);

const stream = await this.getUserMedia(constraints);
try {
return await this.decodeFromStream(stream, previewElem, callbackFn);
} catch (error) {
Expand Down Expand Up @@ -768,7 +787,6 @@ export class BrowserCodeReader {
}

const constraints: MediaStreamConstraints = { video: videoConstraints };

return await this.decodeFromConstraints(constraints, previewElem, callbackFn);
}

Expand Down Expand Up @@ -849,7 +867,7 @@ export class BrowserCodeReader {
videoSource?: string | HTMLVideoElement,
): Promise<Result> {

const stream = await navigator.mediaDevices.getUserMedia(constraints);
const stream = await this.getUserMedia(constraints);

return await this.decodeOnceFromStream(stream, videoSource);
}
Expand Down Expand Up @@ -1092,4 +1110,15 @@ export class BrowserCodeReader {

return this.decode(element);
}

/**
* Get MediaStream from browser to be used.
* @param constraints constraints the media stream constraints to get s valid media stream to decode from.
* @private For private use.
*/
private async getUserMedia(constraints: MediaStreamConstraints): Promise<MediaStream> {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
BrowserCodeReader.streamTracker.push(stream);
return stream;
}
}

0 comments on commit 4a9a020

Please sign in to comment.