"use strict";functionRecordRTC(mediaStream,config){functionstartRecording(){returnconfig.disableLogs||console.debug("started recording "+config.type+" stream."),mediaRecorder?(mediaRecorder.clearRecordedData(),mediaRecorder.resume(),self.recordingDuration&&handleRecordingDuration(),self):(initRecorder(function(){self.recordingDuration&&handleRecordingDuration()}),self)}functioninitRecorder(initCallback){initCallback&&(config.initCallback=function(){initCallback(),initCallback=config.initCallback=null});varRecorder=newGetRecorderType(mediaStream,config);mediaRecorder=newRecorder(mediaStream,config),mediaRecorder.record(),config.disableLogs||console.debug("Initialized recorderType:",mediaRecorder.constructor.name,"for output-type:",config.type)}functionstopRecording(callback){function_callback(){for(variteminmediaRecorder)self&&(self[item]=mediaRecorder[item]),recordRTC&&(recordRTC[item]=mediaRecorder[item]);varblob=mediaRecorder.blob;if(callback){varurl=URL.createObjectURL(blob);callback(url)}blob&&!config.disableLogs&&console.debug(blob.type,"->",bytesToSize(blob.size)),config.autoWriteToDisk&&getDataURL(function(dataURL){varparameter={};parameter[config.type+"Blob"]=dataURL,DiskStorage.Store(parameter)})}if(!mediaRecorder)returnconsole.warn(WARNING);varrecordRTC=this;config.disableLogs||console.warn("Stopped recording "+config.type+" stream."),"gif"!==config.type?mediaRecorder.stop(_callback):(mediaRecorder.stop(),_callback())}functionpauseRecording(){returnmediaRecorder?(mediaRecorder.pause(),void(config.disableLogs||console.debug("Paused recording."))):console.warn(WARNING)}functionresumeRecording(){returnmediaRecorder?(mediaRecorder.resume(),void(config.disableLogs||console.debug("Resumed recording."))):console.warn(WARNING)}functionreadFile(_blob){postMessage((newFileReaderSync).readAsDataURL(_blob))}functiongetDataURL(callback,_mediaRecorder){functionprocessInWebWorker(_function){varblob=URL.createObjectURL(newBlob([_function.toString(),"this.onmessage = function (e) {"+_function.name+"(e.data);}"],{type:"application/javascript"})),worker=newWorker(blob);returnURL.revokeObjectURL(blob),worker}if(!callback)throw"Pass a callback function over getDataURL.";varblob=_mediaRecorder?_mediaRecorder.blob:(mediaRecorder||{}).blob;if(!blob)returnconfig.disableLogs||console.warn("Blob encoder did not yet finished its job."),voidsetTimeout(function(){getDataURL(callback,_mediaRecorder)},1e3);if("undefined"==typeofWorker||navigator.mozGetUserMedia){varreader=newFileReader;reader.readAsDataURL(blob),reader.onload=function(event){callback(event.target.result)}}else{varwebWorker=processInWebWorker(readFile);webWorker.onmessage=function(event){callback(event.data)},webWorker.postMessage(blob)}}functionhandleRecordingDuration(){setTimeout(function(){stopRecording(self.onRecordingStopped)},self.recordingDuration)}if(!mediaStream)throw"MediaStream is mandatory.";config=newRecordRTCConfiguration(mediaStream,config);varmediaRecorder,self=this,WARNING='It seems that "startRecording" is not invoked for '+config.type+" recorder.",returnObject={startRecording:startRecording,stopRecording:stopRecording,pauseRecording:pauseRecording,resumeRecording:resumeRecording,initRecorder:initRecorder,setRecordingDuration:function(milliseconds,callback){if("undefined"==typeofmilliseconds)throw"milliseconds is required.";if("number"!=typeofmilliseconds)throw"milliseconds must be a number.";returnself.recordingDuration=milliseconds,self.onRecordingStopped=callback||function(){},{onRecordingStopped:function(callback){self.onRecordingStopped=callback}}},clearRecordedData:function(){returnmediaRecorder?(mediaRecorder.clearRecordedData(),void(config.disableLogs||console.debug("Cleared old recorded data."))):console.warn(WARNING)},getBlob:function(){returnmediaRecorder?mediaRecorder.blob:console.warn(WARNING)},getDataURL:getDataURL,toURL:function(){returnmediaRecorder?URL.createObjectURL(mediaRecorder.blob):console.warn(WARNING)},save:function(fileName){returnmediaRecorder?voidinvokeSaveAsDialog(mediaRecorder.blob
},this.clearRecordedData=function(){this.pause(),whammy.frames=[]};varvideo,lastTime,whammy,canvas=document.createElement("canvas"),context=canvas.getContext("2d")}functionGifRecorder(mediaStream,config){if("undefined"==typeofGIFEncoder)throw"Please link: https://cdn.webrtc-experiment.com/gif-recorder.js";config=config||{};varisHTMLObject=mediaStreaminstanceofCanvasRenderingContext2D||mediaStreaminstanceofHTMLCanvasElement;this.record=function(){functiondrawVideoFrame(time){returnisPausedRecording?setTimeout(function(){drawVideoFrame(time)},100):(lastAnimationFrame=requestAnimationFrame(drawVideoFrame),void0===typeoflastFrameTime&&(lastFrameTime=time),void(90>time-lastFrameTime||(!isHTMLObject&&video.paused&&(video.play(),context.drawImage(video,0,0,canvas.width,canvas.height)),config.onGifPreview&&config.onGifPreview(canvas.toDataURL("image/png")),gifEncoder.addFrame(context),lastFrameTime=time)))}isHTMLObject||(config.width||(config.width=video.offsetWidth||320),this.height||(config.height=video.offsetHeight||240),config.video||(config.video={width:config.width,height:config.height}),config.canvas||(config.canvas={width:config.width,height:config.height}),canvas.width=config.canvas.width,canvas.height=config.canvas.height,video.width=config.video.width,video.height=config.video.height),gifEncoder=newGIFEncoder,gifEncoder.setRepeat(0),gifEncoder.setDelay(config.frameRate||200),gifEncoder.setQuality(config.quality||10),gifEncoder.start(),startTime=Date.now();lastAnimationFrame=requestAnimationFrame(drawVideoFrame),config.initCallback&&config.initCallback()},this.stop=function(){lastAnimationFrame&&cancelAnimationFrame(lastAnimationFrame),endTime=Date.now(),this.blob=newBlob([newUint8Array(gifEncoder.stream().bin)],{type:"image/gif"}),gifEncoder.stream().bin=[]};varisPausedRecording=!1;this.pause=function(){isPausedRecording=!0},this.resume=function(){isPausedRecording=!1},this.clearRecordedData=function(){gifEncoder&&(this.pause(),gifEncoder.stream().bin=[])};varcanvas=document.createElement("canvas"),context=canvas.getContext("2d");if(isHTMLObject&&(mediaStreaminstanceofCanvasRenderingContext2D?(context=mediaStream,canvas=context.canvas):mediaStreaminstanceofHTMLCanvasElement&&(context=mediaStream.getContext("2d"),canvas=mediaStream)),!isHTMLObject){varvideo=document.createElement("video");video.muted=!0,video.autoplay=!0,"undefined"!=typeofvideo.srcObject?video.srcObject=mediaStream:video.src=URL.createObjectURL(mediaStream),video.play()}varstartTime,endTime,lastFrameTime,gifEncoder,lastAnimationFrame=null}if(RecordRTC.getFromDisk=function(type,callback){if(!callback)throw"callback is mandatory.";console.log("Getting recorded "+("all"===type?"blobs":type+" blob ")+" from disk!"),DiskStorage.Fetch(function(dataURL,_type){"all"!==type&&_type===type+"Blob"&&callback&&callback(dataURL),"all"===type&&callback&&callback(dataURL,_type.replace("Blob",""))})},RecordRTC.writeToDisk=function(options){console.log("Writing recorded blob(s) to disk!"),options=options||{},options.audio&&options.video&&options.gif?options.audio.getDataURL(function(audioDataURL){options.video.getDataURL(function(videoDataURL){options.gif.getDataURL(function(gifDataURL){DiskStorage.Store({audioBlob:audioDataURL,videoBlob:videoDataURL,gifBlob:gifDataURL})})})}):options.audio&&options.video?options.audio.getDataURL(function(audioDataURL){options.video.getDataURL(function(videoDataURL){DiskStorage.Store({audioBlob:audioDataURL,videoBlob:videoDataURL})})}):options.audio&&options.gif?options.audio.getDataURL(function(audioDataURL){options.gif.getDataURL(function(gifDataURL){DiskStorage.Store({audioBlob:audioDataURL,gifBlob:gifDataURL})})}):options.video&&options.gif?options.video.getDataURL(function(videoDataURL){options.gif.getDataURL(function(gifDataURL){DiskStorage.Store({videoBlob:videoDataURL,gifBlob:gifDataURL})})}):options.audio?options.audio.getDataURL(function(audioDataURL){DiskStorage.Store({audioBlob:audioDataURL})}):options.video?options.video.getDataURL(function(videoDataURL){DiskStorage.Store({videoBlob:videoDataURL})})