From e6df3e3ecfd327548aaa8b3018fb6361e93b4077 Mon Sep 17 00:00:00 2001 From: nikvaessen Date: Mon, 11 Jul 2016 18:52:02 +0200 Subject: [PATCH] only records audio now --- modules/audioRecording/StorageManager.js | 3 + modules/audioRecording/audioRecorder.js | 75 ++++++++++++++++++++---- 2 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 modules/audioRecording/StorageManager.js diff --git a/modules/audioRecording/StorageManager.js b/modules/audioRecording/StorageManager.js new file mode 100644 index 000000000..471254643 --- /dev/null +++ b/modules/audioRecording/StorageManager.js @@ -0,0 +1,3 @@ +/** + * Created by workingnik on 10/07/16. + */ diff --git a/modules/audioRecording/audioRecorder.js b/modules/audioRecording/audioRecorder.js index 2e2915b64..0b30bb33d 100644 --- a/modules/audioRecording/audioRecorder.js +++ b/modules/audioRecording/audioRecorder.js @@ -3,7 +3,10 @@ /** * options passed along when the MediaRecorder gets created */ -const MEDIA_RECORDING_OPTIONS = {mimeType: "audio/webm"}; +const AUDIO_WEBM = "audio/webm"; +const AUDIO_OGG = "audio/ogg"; +const MEDIA_RECORDING_OPTIONS_CHROME = {mimeType: AUDIO_WEBM}; //for chrome +const MEDIA_RECORDING_OPTIONS_FIREFOX = {mimeType: AUDIO_OGG}; //for ff /** * File type to convert data into when it's downloaded @@ -22,6 +25,24 @@ var TrackRecorder = function(track){ this.data = null; }; +/** + * Determines which kind of audio recording the browser supports + * chrome supports "audio/webm" and firefox supports "audio/ogg" + */ +function determineCorrectFileType() +{ + if(MediaRecorder.isTypeSupported(AUDIO_WEBM)) { + return AUDIO_WEBM; + } + else if(MediaRecorder.isTypeSupported(AUDIO_OGG)) { + return AUDIO_OGG; + } + else { + throw new Error("unable to create a MediaRecorder with the" + + "right mimetype!") + } +} + /** * main exported object of the file, holding all * relevant functions and variables for the outside world @@ -29,7 +50,10 @@ var TrackRecorder = function(track){ var audioRecorder= { // array of TrackRecorders, where each trackRecorder // holds the JitsiTrack, MediaRecorder and recorder data - recorders: [] + recorders: [], + + //get which file type is supported by the current browser + fileType: determineCorrectFileType() }; /** @@ -51,17 +75,22 @@ audioRecorder.startAudioRecording = function () { //loop through all stored tracks and create a mediaRecorder for each stream audioRecorder.recorders.forEach(function(trackRecorder){ //creates the recorder if not already created - if(!trackRecorder.recorder) { - trackRecorder.recorder = - new MediaRecorder(trackRecorder.track.getOriginalStream(), - MEDIA_RECORDING_OPTIONS); - //function handling a dataEvent, e.g the stream gets new data + if(trackRecorder.recorder == null) { + // Create a new stream which only holds the audio track + var originalStream = trackRecorder.track.getOriginalStream(); + var stream = createEmptyStream(); + originalStream.getAudioTracks().forEach(function(track){ + stream.addTrack(track); + }); + // Create the recorder + trackRecorder.recorder = new MediaRecorder(stream, + {mimeType: audioRecorder.fileType}); + // function handling a dataEvent, e.g the stream gets new data trackRecorder.recorder.ondataavailable = function (dataEvent) { if(dataEvent.data.size > 0) { - trackRecorder.data.push(event.data); + trackRecorder.data.push(dataEvent.data); } }; - } //array for holding the recorder data. Resets it when //audio already has been recorder once @@ -92,18 +121,42 @@ audioRecorder.stopAudioRecording = function() { */ audioRecorder.download = function () { audioRecorder.recorders.forEach(function (trackRecorder) { - var blob = new Blob(trackRecorder.data, FILE_TYPE); + var blob = new Blob(trackRecorder.data, {type: audioRecorder.fileType}); var url = URL.createObjectURL(blob); var a = document.createElement('a'); document.body.appendChild(a); a.style = "display: none"; a.href = url; - a.download = 'test.webm'; + a.download = 'test.' + audioRecorder.fileType.split("/")[1]; a.click(); window.URL.revokeObjectURL(url); }); }; +/** + * Creates a empty MediaStream object which can be used + * to add MediaStreamTracks to + * @returns MediaStream + */ +function createEmptyStream() { + // Firefox supports creation of a clean empty MediaStream + // so test if it's firefox by just returning + // a new instance + try { + return new MediaStream(); + } + // if it's chrome we need to make it the dirty way... + catch(e){ + if(e instanceof ReferenceError) + { + return new webkitMediaStream(); + } + else { + throw new Error("cannot create a clean MediaStream"); + } + } +} + /** * export the main object audioRecorder */