Hello, i'm trying tu use SimplePeer On a client/server based application sending Video/Audio Stream from the client browser to a NodeJs Server to perform some transformation or just record it !
Anyway , the library is a way instinctive and i want to use it instead of writing all the webrtc code client and server side !
The issue is that when the signaling process is done , i'm not receiving the Stream server side ! The code is the following :
///////////////////////////////////////////////////////////////////// Client //////////////////////////////////////////////////////////////////////////
let connection ;
let stream ;
function getWebSocket(){
var websocketEndpoint = 'ws://localhost:7000';
connection = new WebSocket(websocketEndpoint) ;
connection.onmessage = function (message){
p.signal(JSON.parse(message.data));
} ;
}
getWebSocket();
let p = null ;
function bindEvents(p){
p.on('signal' , function(data){
connection.send(JSON.stringify(data));
}) ;
p.on('error' , function(err){
console.log('error' , err);
}) ;
p.on('connect' , function(){
console.log("client connected");
})
}
document.querySelector('#start').addEventListener('click' , function(e){
navigator.getUserMedia({
video : true ,
audio : true
} , function(stream){
p = new SimplePeer({
initiator: true,
stream: stream ,
trickle : false
})
bindEvents(p);
} , function(err){
console.log(err);
})
})
///////////////////////////////////////////////////////////////////// Server Side //////////////////////////////////////////////////////////////////
'use strict';
var fs = require('fs');
var Peer = require('simple-peer')
var wrtc = require('wrtc') ;
var exec = require('child_process').exec;
var uuid = require('node-uuid');
function bindEvents(p , ws){
p.on('signal' , function(data){
ws.send(JSON.stringify(data));
}) ;
p.on('stream' , function(stream){
console.log("here we are buddy");
}) ;
p.on('error' , function(error){
console.log('erreur' , error) ;
}) ;
p.on('connect' , function(){
console.log("serveur connected");
})
}
module.exports = function (app) {
app.ws('/', function (ws, req) {
console.log('new connection established');
ws.on('message', function(data) {
var peer2 = new Peer({ trickle : false , wrtc: wrtc });
peer2.signal(JSON.parse(data));
bindEvents(peer2, ws) ;
});
});
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Assuming that if a stream was received my message "here we are buddy" must be logged ! What im i missing ? Is it related to some server configuration ? Thank you
Media streaming is not currently supported on Node.js server unfortunately, you can only use RTCDataChannel. For example, see readme here: https://github.com/js-platform/node-webrtc
node-webrtc provides Node.js bindings to WebRTC M60. You can write Node.js applications that use RTCDataChannels with it. MediaStream APIs are not supported at this time.
There is nothing simple-peer can do about that, but as soon as there will be an implementation available in node-webrtc or other project, it should be straightforward to get it supported in simple-peer.
@nazar-pc @feross Thank you for your prompt answer !
The MediaStream API will be used client Side , on the server i only wanna receive and handle the Stream ( To say that i'm not using any MediaStream Api on the server , only consuming its result ) :
1)Browser executes Javascript wich gets a handle to the camera , Creates an RTCPeerConnection , Calls "createOffer" and "setLocalDescription" on the RTCPeer Connection and send a request to the server containing the offer.
2)the server processes the offer SDP and generates its own answer SDP wich it returns to the browser in its response
3)The JS calls "setRemoteDescription" on the RTCPeerConnection to start the media flowing
4) The server starts receiving DTLS/SRTP packets from the browser :D
I'm not doubting your answer , just trying to figure out why there are limitations on the server side
In that case stream is not handled on server, server just acts as mediator for signal messages, it doesn't matter what kind of WebRTC connection is used. Just make sure to add listener for signal event and send all of the data in it to the other side.
Then if the server is only here for signalling , why can we instanciate a new peer Server Side (NodeJs) ?
Well in my case : WebSocket is handling the signalling process ( mediator for signal messages as you said ) And my NodeJS Server is a Peer receiving Stream from my Browser
Sometimes you might want to connect browser to server via WebRTC, in this case you'll instantiate a new peer in Node.js (it will only support RTCDataChannel). If you want to connect browser to browser, then you only need server for signaling. These are different scenarios.
OK ! You are saying that it is impossible to establish a P2P between client and server and sending Stream from one to another ! At least not with Simple peer ... Do you guys have idea how can i make this possible ? Do i need a media Server such as Kureno to make it possible ?
That is right, you'll need special media server for such task. There are a few existing options, but I didn't use any of them personally, so can't suggest anything specific.
For anyone stumbling over this thread: node-webrtc supports sending and receiving MediaStream objects Server-Side through its VideoSink and AudioSink interfaces: Just take a look at the video-compositing example.
Most helpful comment
For anyone stumbling over this thread:
node-webrtcsupports sending and receiving MediaStream objects Server-Side through its VideoSink and AudioSink interfaces: Just take a look at thevideo-compositingexample.