2

I'm working on project that requires capturing user audio and its manipulation on server. To speed up preview process and skip lengthily uploads at the end of recording we are testing streaming trough Socket.io. While we did get it to work, something is missing, file plays but reports "Internal data stream error" at the end, both in ogg and wav format. Client side code:

var mediaRecorder,mystream;
var last = false;
var socket = io('http://localhost:3000');
var mediaConstraints = {
audio: true
};

function onMediaSuccess(stream) {
mystream=stream;
mediaRecorder = new MediaStreamRecorder(stream);
mediaRecorder.mimeType = 'audio/ogg';
mediaRecorder.audioChannels = 1;
mediaRecorder.ondataavailable = function (blob) {
    socket.emit('stream', {'user':1,'last':last,'data':blob});
};

mediaRecorder.onstop = function() {
    last= true;
 };

mediaRecorder.start(3000);
}

function onMediaError(e) {
   console.error('media error', e);
}

socket.on('finished', function(msg){
   console.log(msg);
   mystream.stop();
});
$(function() {

$('#start').click(function(e){
 e.preventDefault();
   navigator.getUserMedia(mediaConstraints, onMediaSuccess,     onMediaError);
});

$('#stop').click(function(e){
    e.preventDefault();
    mediaRecorder.stop();
});
});

Server side code (Node.js):

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');

io.on('connection', function(socket){
   console.log('a user connected');
   var tmp_path = 'test.ogg';
   var wstream = fs.createWriteStream(tmp_path);
   //CONNECTION TERMINATED
  socket.on('disconnect', function(){
    console.log('user disconnected');
    if(wstream){
      wstream.end();
      fs.unlink(tmp_path);
    }
  });

 //RECIEVE DATA
 socket.on('stream', function(msg){
    if(msg.last === true){
       console.log('recording done');
       wstream.write(msg.data);
       wstream.end();
       var responseObject = {
       temp_path: tmp_path
       }
       socket.emit('finished',responseObject);
    }
    else{
      wstream.write(msg.data);
      console.log('message: ' + msg);
   }

   });
 });

 http.listen(3000, function(){
   console.log('listening on *:3000');
 });

I'm kind of new with Node and my guess I'm just not seeing obvious.

Alex
  • 21
  • 1
  • 2

1 Answers1

1

one obvious error I am seeing is:

mediaRecorder.ondataavailable = function (blob) {
    socket.emit('stream', {'user':1,'last':last,'data':blob});
};

the parameter of method ondataavailable is event object( not blob as you specified), to retrieve blob, you could do event.data.

other than that I am not sure if your implementation is scalable when multiple users are recording simultaneously.

I made a small git project for this, you can check it out: fox-ogg-recorder

Note: I hope you already know that, right now, only Firefox supports this kind of recording.

mido
  • 24,198
  • 15
  • 92
  • 117
  • Thank you for answer. I will check your git project. So far I may go for single upload solution though as long it's in ogg format. But yes as you stated, problem is browser support. Do you know any solution that would be cross browser? I'm still fond of streaming but just seems like technology is still not there. p.s. to be honest I didn't bother with scalability until I get proof of concept. – Alex Jun 03 '15 at 10:08
  • you can take a look at this answer: http://stackoverflow.com/questions/11979528/record-audio-stream-from-getusermedia/29247802#29247802 – mido Jun 03 '15 at 10:32
  • recordOpus looks promising, will test it – Alex Jun 03 '15 at 10:52