Skip to content

Commit

Permalink
Test preload worker
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rudmin committed Dec 18, 2017
1 parent 2cdfffe commit 90acb54
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dist/recorder.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 23 additions & 16 deletions src/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var Recorder = function( config ){
wavSampleRate: this.audioContext.sampleRate
}, config );

this.initWorker();
this.setMonitorGain( this.config.monitorGain );
this.scriptProcessorNode = this.audioContext.createScriptProcessor( this.config.bufferLength, this.config.numberOfChannels, this.config.numberOfChannels );
this.scriptProcessorNode.onaudioprocess = function( e ){
Expand Down Expand Up @@ -108,6 +109,26 @@ Recorder.prototype.initStream = function(){
return getUserMedia(constraints).then( onStreamInit, onStreamError );
};

Recorder.prototype.initWorker = function(){
this.encoder = new global.Worker( this.config.encoderPath );

if (this.config.streamPages){
this.encoder.addEventListener( "message", function( e ) {
that.streamPage( e.data );
});
}

else {
this.recordedPages = [];
this.totalLength = 0;
this.encoder.addEventListener( "message", function( e ) {
that.storePage( e.data );
});
}

this.encoder.postMessage( this.config );
}

Recorder.prototype.pause = function(){
if ( this.state === "recording" ){
this.state = "paused";
Expand All @@ -133,21 +154,6 @@ Recorder.prototype.setMonitorGain = function( gain ){
Recorder.prototype.start = function(){
if ( this.state === "inactive" && this.stream ) {
var that = this;
this.encoder = new global.Worker( this.config.encoderPath );

if (this.config.streamPages){
this.encoder.addEventListener( "message", function( e ) {
that.streamPage( e.data );
});
}

else {
this.recordedPages = [];
this.totalLength = 0;
this.encoder.addEventListener( "message", function( e ) {
that.storePage( e.data );
});
}

// First buffer can contain old data. Don't encode it.
this.encodeBuffers = function(){
Expand All @@ -158,7 +164,6 @@ Recorder.prototype.start = function(){
this.monitorNode.connect( this.audioContext.destination );
this.scriptProcessorNode.connect( this.audioContext.destination );
this.eventTarget.dispatchEvent( new global.Event( 'start' ) );
this.encoder.postMessage( this.config );
}
};

Expand Down Expand Up @@ -192,6 +197,7 @@ Recorder.prototype.storePage = function( page ) {

this.recordedPages = [];
this.eventTarget.dispatchEvent( new global.Event( 'stop' ) );
this.initWorker();
}

else {
Expand All @@ -203,6 +209,7 @@ Recorder.prototype.storePage = function( page ) {
Recorder.prototype.streamPage = function( page ) {
if ( page === null ) {
this.eventTarget.dispatchEvent( new global.Event( 'stop' ) );
this.initWorker();
}

else {
Expand Down
26 changes: 22 additions & 4 deletions test/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,9 @@ describe('Recorder', function(){
var rec = new Recorder();
return rec.initStream().then(function(){
rec.start();
expect(global.Worker).to.have.been.calledWithNew;
expect(rec.encoder.addEventListener).to.have.been.calledOnce;
expect(rec.encoder.addEventListener).to.have.been.calledWith('message');
expect(rec.state).to.equal('recording');
expect(rec.scriptProcessorNode.connect).to.have.been.calledWith( rec.audioContext.destination );
expect(global.Event).to.have.been.calledWith('start');
expect(rec.encoder.postMessage).to.have.been.calledWith( rec.config );
});
});

Expand All @@ -299,4 +295,26 @@ describe('Recorder', function(){
expect(rec.monitorNode.gain.setTargetAtTime).to.have.been.calledOnce;
});
});

it('should init worker', function () {
var rec = new Recorder();
expect(global.Worker).to.have.been.calledWithNew;
expect(rec.encoder.addEventListener).to.have.been.calledOnce;
expect(rec.encoder.addEventListener).to.have.been.calledWith('message');
expect(rec.encoder.postMessage).to.have.been.calledWith( rec.config );
});

it('should re-init worker when storePage completes', function () {
var rec = new Recorder();
expect(global.Worker).to.have.been.calledOnce;
rec.storePage(null);
expect(global.Worker).to.have.been.calledTwice;
});

it('should re-init worker when streamPage completes', function () {
var rec = new Recorder();
expect(global.Worker).to.have.been.calledOnce;
rec.streamPage(null);
expect(global.Worker).to.have.been.calledTwice;
});
});

0 comments on commit 90acb54

Please sign in to comment.