Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Memory management fix | Adding max number of active threads #2

Open
wants to merge 2 commits into
base: solution
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions app/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
(function() {
'use strict';

var imageDecoderWorker = new Worker('scripts/jsqrcode/qrworker.js');

var QRCodeCamera = function(element) {
// Controls the Camera and the QRCode Module

Expand All @@ -28,53 +30,51 @@

cameraManager.onframe = function() {
// There is a frame in the camera, what should we do with it?

var imageData = cameraManager.getImageData();
var detectedQRCode = qrCodeManager.detectQRCode(imageData, function(url) {
if(url !== undefined) {
qrCodeManager.showDialog(url);
}
});
if (qrCodeManager.numActiveThreads < qrCodeManager.maxThreads) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if numActiveThreads is over maxThreads? does the frame get dropped?

Copy link
Author

@melloc01 melloc01 Dec 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The consequence is: The worker won't be called.

On my i5 it takes about 10ms for the worker to respond back, at the worst case scenario we'll have a "10ms lag", ** 2.5ms** on the best case, not noticeable.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool cool. thanks

qrCodeManager.detectQRCode(cameraManager.getImageData());
qrCodeManager.numActiveThreads++;
}
};

// Worker listener
imageDecoderWorker.onmessage = function (e) {
var url = e.data;
// freeing 1 thread slot
qrCodeManager.numActiveThreads--;

if (url) {
// found a QRCode
qrCodeManager.showDialog(url);
qrCodeManager.currentUrl = url;
}
};

imageDecoderWorker.onerror = function(error) {
function WorkerException(message) {
this.name = "WorkerException";
this.message = message;
};
throw new WorkerException('Decoder error');
};
};

var QRCodeManager = function(element) {
this.numActiveThreads = 0;
this.maxThreads = window.navigator.hardwareConcurrency || 4;

var root = document.getElementById(element);
var canvas = document.getElementById("qr-canvas");
var qrcodeData = root.querySelector(".QRCodeSuccessDialog-data");
var qrcodeNavigate = root.querySelector(".QRCodeSuccessDialog-navigate");
var qrcodeIgnore = root.querySelector(".QRCodeSuccessDialog-ignore");

var client = new QRClient();

var imageDecoderWorker = new Worker('scripts/jsqrcode/qrworker.js');

var self = this;

this.currentUrl = undefined;

this.detectQRCode = function(imageData, callback) {
callback = callback || function() {};

this.detectQRCode = function(imageData) {
imageDecoderWorker.postMessage(imageData);

imageDecoderWorker.onmessage = function(result) {
var url = result.data;
if(url !== undefined) {
self.currentUrl = url;
}
callback(url);
};

imageDecoderWorker.onerror = function(error) {
function WorkerException(message) {
this.name = "WorkerException";
this.message = message;
};
throw new WorkerException('Decoder error');
callback(undefined);
};
};

this.showDialog = function(url) {
Expand Down Expand Up @@ -226,7 +226,7 @@
else {
params = { video: { optional: [{sourceId: videoSource.id}] } };
}

gUM.call(navigator, params, function(theStream) {
localStream = theStream;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"gulp-load-plugins": "^0.8.0",
"gulp-minify-html": "^0.1.8",
"gulp-replace": "^0.5.0",
"gulp-sass": "^1.2.2",
"gulp-sass": "^2.1.1",
"gulp-size": "^1.0.0",
"gulp-uglify": "^1.0.1",
"gulp-uncss": "^0.5.2",
Expand Down