how to convert getUsermedia audio stream into a bl

2019-04-02 02:34发布

问题:

I am getting audio stream from getUserMeda and then convert it into a blob or buffer and send it to server as audio is comming I am using socket.io to emit it to server how can i convert audio mediastream into buffer?

Following is the code that i have written yet

navigator.getUserMedia({audio: true, video: false}, function(stream) {
webcamstream = stream;
var media = stream.getAudioTracks();
socket.emit("sendaudio", media);
},
function(e){
   console.log(e);
  }
});

How to convert stream into buffer and emit it to node.js server as stream comes from getusermedia function?

回答1:

Per @MuazKhan's comment, use MediaRecorder (in Firefox, eventually will be in Chrome) or RecordRTC/etc to capture the data into blobs. Then you can export it via one of several methods to the server for distribution: WebSockets, WebRTC DataChannels, etc. Note that these are NOT guaranteed to transfer the data in realtime, and also MediaRecorder does not yet have bitrate controls. If transmission is delayed, data may build up locally.

If realtime (re)transmission is important, strongly consider using instead a PeerConnection to a server (per @Robert's comment) and then transform it there into a stream. (How that is done will depend on the server, but you should have encoded Opus data to either repackage or decode and re-encode.) While re-encoding is generally not good, in this case you would do best to decode through NetEq (webrtc.org stack's jitter-buffer and PacketLossConcealment code) and get a clean realtime audio stream to re-encode for streaming, with loss and jitter dealt with.