Skip to content

Commit

Permalink
Merge pull request #55 from bradyisom/master
Browse files Browse the repository at this point in the history
#53 RecordRTC correctly loads images when checking for black frames
  • Loading branch information
muaz-khan committed Oct 27, 2015
2 parents da12b45 + 6aceefd commit 76658e7
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 145 deletions.
173 changes: 101 additions & 72 deletions RecordRTC.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Last time updated at Saturday, October 24th, 2015, 9:38:13 AM
// Last time updated at Monday, October 26th, 2015, 4:28:24 PM

// links:
// Open-Sourced: https://github.com/muaz-khan/RecordRTC
Expand Down Expand Up @@ -2552,6 +2552,22 @@ function WhammyRecorder(mediaStream, config) {
}
}

function asyncLoop(o) {
var i = -1,
length = o.length;

var loop = function() {
i++;
if (i === length) {
o.callback();
return;
}
o.functionToLoop(loop, i);
};
loop(); //init
}


/**
* remove black frames from the beginning to the specified frame
* @param {Array} _frames - array of frames to be checked
Expand All @@ -2561,7 +2577,7 @@ function WhammyRecorder(mediaStream, config) {
* @returns {Array} - array of frames
*/
// pull#293 by @volodalexey
function dropBlackFrames(_frames, _framesToCheck, _pixTolerance, _frameTolerance) {
function dropBlackFrames(_frames, _framesToCheck, _pixTolerance, _frameTolerance, callback) {
var localCanvas = document.createElement('canvas');
localCanvas.width = canvas.width;
localCanvas.height = canvas.height;
Expand All @@ -2585,56 +2601,67 @@ function WhammyRecorder(mediaStream, config) {
var frameTolerance = _frameTolerance && _frameTolerance >= 0 && _frameTolerance <= 1 ? _frameTolerance : 0;
var doNotCheckNext = false;

for (var f = 0; f < endCheckFrame; f++) {
var matchPixCount, endPixCheck, maxPixCount;

if (!doNotCheckNext) {
var image = new Image();
image.src = _frames[f].image;
context2d.drawImage(image, 0, 0, canvas.width, canvas.height);
var imageData = context2d.getImageData(0, 0, canvas.width, canvas.height);
matchPixCount = 0;
endPixCheck = imageData.data.length;
maxPixCount = imageData.data.length / 4;

for (var pix = 0; pix < endPixCheck; pix += 4) {
var currentColor = {
r: imageData.data[pix],
g: imageData.data[pix + 1],
b: imageData.data[pix + 2]
};
var colorDifference = Math.sqrt(
Math.pow(currentColor.r - sampleColor.r, 2) +
Math.pow(currentColor.g - sampleColor.g, 2) +
Math.pow(currentColor.b - sampleColor.b, 2)
);
// difference in color it is difference in color vectors (r1,g1,b1) <=> (r2,g2,b2)
if (colorDifference <= maxColorDifference * pixTolerance) {
matchPixCount++;
asyncLoop({
length: endCheckFrame,
functionToLoop: function(loop, f) {
var matchPixCount, endPixCheck, maxPixCount;

var finishImage = function() {
if (!doNotCheckNext && maxPixCount - matchPixCount <= maxPixCount * frameTolerance) {
// console.log('removed black frame : ' + f + ' ; frame duration ' + _frames[f].duration);
} else {
// console.log('frame is passed : ' + f);
if (checkUntilNotBlack) {
doNotCheckNext = true;
}
resultFrames.push(_frames[f]);
}
}
}
loop();
};

if (!doNotCheckNext && maxPixCount - matchPixCount <= maxPixCount * frameTolerance) {
// console.log('removed black frame : ' + f + ' ; frame duration ' + _frames[f].duration);
} else {
// console.log('frame is passed : ' + f);
if (checkUntilNotBlack) {
doNotCheckNext = true;
if (!doNotCheckNext) {
var image = new Image();
image.onload = function() {
context2d.drawImage(image, 0, 0, canvas.width, canvas.height);
var imageData = context2d.getImageData(0, 0, canvas.width, canvas.height);
matchPixCount = 0;
endPixCheck = imageData.data.length;
maxPixCount = imageData.data.length / 4;

for (var pix = 0; pix < endPixCheck; pix += 4) {
var currentColor = {
r: imageData.data[pix],
g: imageData.data[pix + 1],
b: imageData.data[pix + 2]
};
var colorDifference = Math.sqrt(
Math.pow(currentColor.r - sampleColor.r, 2) +
Math.pow(currentColor.g - sampleColor.g, 2) +
Math.pow(currentColor.b - sampleColor.b, 2)
);
// difference in color it is difference in color vectors (r1,g1,b1) <=> (r2,g2,b2)
if (colorDifference <= maxColorDifference * pixTolerance) {
matchPixCount++;
}
}
finishImage();
};
image.src = _frames[f].image;
} else {
finishImage();
}
resultFrames.push(_frames[f]);
},
callback: function() {
resultFrames = resultFrames.concat(_frames.slice(endCheckFrame));

if (resultFrames.length <= 0) {
// at least one last frame should be available for next manipulation
// if total duration of all frames will be < 1000 than ffmpeg doesn't work well...
resultFrames.push(_frames[_frames.length - 1]);
}
callback(resultFrames);
}
}

resultFrames = resultFrames.concat(_frames.slice(endCheckFrame));

if (resultFrames.length <= 0) {
// at least one last frame should be available for next manipulation
// if total duration of all frames will be < 1000 than ffmpeg doesn't work well...
resultFrames.push(_frames[_frames.length - 1]);
}

return resultFrames;
});
}

var isStopDrawing = false;
Expand All @@ -2658,33 +2685,35 @@ function WhammyRecorder(mediaStream, config) {
// e.g. dropBlackFrames(frames, 10, 1, 1) - will cut all 10 frames
// e.g. dropBlackFrames(frames, 10, 0.5, 0.5) - will analyse 10 frames
// e.g. dropBlackFrames(frames, 10) === dropBlackFrames(frames, 10, 0, 0) - will analyse 10 frames with strict black color
whammy.frames = dropBlackFrames(whammy.frames, -1);

// to display advertisement images!
if (config.advertisement && config.advertisement.length) {
whammy.frames = config.advertisement.concat(whammy.frames);
}

/**
* @property {Blob} blob - Recorded frames in video/webm blob.
* @memberof WhammyRecorder
* @example
* recorder.stop(function() {
* var blob = recorder.blob;
* });
*/
whammy.compile(function(blob) {
_this.blob = blob;
dropBlackFrames(whammy.frames, -1, null, null, function(frames) {
whammy.frames = frames;

if (_this.blob.forEach) {
_this.blob = new Blob([], {
type: 'video/webm'
});
// to display advertisement images!
if (config.advertisement && config.advertisement.length) {
whammy.frames = config.advertisement.concat(whammy.frames);
}

if (callback) {
callback(_this.blob);
}
/**
* @property {Blob} blob - Recorded frames in video/webm blob.
* @memberof WhammyRecorder
* @example
* recorder.stop(function() {
* var blob = recorder.blob;
* });
*/
whammy.compile(function(blob) {
_this.blob = blob;

if (_this.blob.forEach) {
_this.blob = new Blob([], {
type: 'video/webm'
});
}

if (callback) {
callback(_this.blob);
}
});
});
}, 10);
};
Expand Down
Loading

0 comments on commit 76658e7

Please sign in to comment.