Skip to content

Commit

Permalink
bug fix: #4 Socket.io connection getting aborted in Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
Mido22 committed Jul 29, 2015
1 parent d526d2b commit 83e531c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
41 changes: 27 additions & 14 deletions client/scripts/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

function OggShimRecorder(stream, cfg){

console.log('using OggShimRecorder for recording...' );

var config = cfg || {}
, self = this
, bufferLen = 16384
Expand All @@ -24,20 +24,24 @@
, sampleRate = ctx.sampleRate
, node = ctx.createScriptProcessor(bufferLen, 2, 2)
, callback = config.callback
, type = config.type || 'ogg'
, vInterval
, recorderType = 'OggShim'
;
source.connect(node);
node.connect(ctx.destination);

//using the below variables to keep track of chucks sent to worker for conversion, we are gonna convert chunks sequencially as when done in parallel, sometimes the (last) smaller chuck finishes first sometimes and previous few chucks are missed.
var encodingInProgess = false,
waitQueue = []; // for holding wav to ogg conversion worker call data( used for ensuring sequencing)


node.onaudioprocess = function(e){
if (!recording) return;
worker.postMessage({
command: 'record',
buffer: [e.inputBuffer.getChannelData(0), e.inputBuffer.getChannelData(1)]
});
}
};

worker.postMessage({
command: 'init',
Expand All @@ -49,28 +53,30 @@
worker.onmessage = function(e){
var data = e.data;
delete data.command;
if(type === 'wav'){
callback(data);
}else{
blobToArrayBuffer(data.blob, function(buffer){
blobToArrayBuffer(data.blob, function(buffer){
oggWorker.postMessage({
command: 'encode',
args: ['in', 'out'],
outData: {out: {MIME: 'audio/ogg'}},
fileData: {in: new Uint8Array(buffer)}
});
oggWorker.onmessage = function(e) {
var next;
if(e.data && e.data.reply){
if(e.data.reply === 'done'){
data.blob = e.data.values.out.blob;
callback(data);
if(waitQueue.length){
next = waitQueue.shift();
worker.postMessage(next);
}else{
encodingInProgess = false;
}
}
}
};
});
}
}

});
};

this.start = function(){
recording = true;
Expand All @@ -85,14 +91,21 @@
};

function getBlob(last){
worker.postMessage({

var msg = {
command: 'export',
autoUpload: autoUpload,
stop: last,
type: type,
recorderType: recorderType
});
};
if(encodingInProgess){
waitQueue.push(msg);
}else{
encodingInProgess = true;
worker.postMessage(msg);
}
}

};

function FoxRecorder(stream, cfg){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "recordOpus",
"version": "1.0.0",
"version": "1.0.1",
"description": "audio recorder using opus encoding",
"scripts": {
"start": "node server/app.js",
Expand Down
3 changes: 2 additions & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function returnLink(data, filepath, callback){
callback(data);
}

// for concating
// for concating all audio chunks into single file.
function concat(inFile, outFile, callback){
try{
ffmpeg().input(inFile)
Expand All @@ -109,6 +109,7 @@ function concat(inFile, outFile, callback){
}
}

// for creating a directory at the given path if not present already.
function mkDir(dirPath, cb){
fs.exists(dirPath, function(exists){
if(!exists){
Expand Down

0 comments on commit 83e531c

Please sign in to comment.