murat-dogan / node-datachannel

WebRTC For Node.js and Electron. libdatachannel node bindings.
Mozilla Public License 2.0
294 stars 57 forks source link

Question : How to source a video stream on the server #205

Closed kirianguiller closed 7 months ago

kirianguiller commented 10 months ago

Hi, I originally sent this message on the issue #122 , but it might never be seen as the issue is closed since long time.

I would like to send a video stream (from a go2rtc server) from a server (but more specifically a node-datachannel client) to another node-datachannel client. However, I don't understand how to send this stream.

In this issue #122, we can see that @Canees set the Video track on the server, but I don't understand how this track is reading from another source.

Here is the relevant code snippet from @Canees :

  this.socket.on('msg', data =>{
    // not showing the unrelevant part of the code
    if (data.type === 'startRTC') {
      // test video
      const video = new nodeDataChannel.Video('video', nodeDataChannel.Direction.SendOnly);
      video.addH264Codec(96);
      video.addSSRC(42, 'video-send');
      this.Peer.addTrack(video)
      this.Peer.setLocalDescription()
    }
  })

Is the video.addSSRC method responsible for this ? How does it works ? I don't see this method being tested in the tests files, neither do I see it in one of the 5 examples. Maybe this project could benefit by having an example showing how to use this library to send a video stream with datachannel. I would gladly help adding such an example once I will have figured how to make this works.

Thanks a lot for your help !

paullouisageneau commented 10 months ago

You need to send RTP packets into the track, similarly to what the media example does but in the other direction. The SSRC of the RTP stream must match the SSRC in the video description. libdatachannel has such an example in C++.

kirianguiller commented 10 months ago

Thanks. I've indeed studied the C++ example but didn't quite manage to make it works as I didn't succeed to bind an udp socket (with the node dgram library). I still don't have a working example to share to other people.

paullouisageneau commented 10 months ago

Listening on a UDP socket with node.js is quite straightforward, you can do it like this:

const server = dgram.createSocket('udp4');

server.on('error', (err) => {
  console.error(err);
  server.close();
});

server.on('message', (msg) => {
    if (track.isOpen()) {
        // Foward in track
        track.sendMessageBinary(msg);
    }
});

server.bind(6000);
kirianguiller commented 10 months ago

Thank you. I will try something simple and suggest it in the examples of the project !

Canees commented 9 months ago

Sorry for the late arrival, I thought this would help you, this is the example I tested earlier, and so far it has worked

https://github.com/Canees/node-datachannel/blob/master/examples/media-send/peer-send/index.js

murat-dogan commented 7 months ago

I am closing the issue. Feel free to open a new issue when you need to. Thanks.