sjkummer / janus-gateway-js

Janus-gateway WebRTC client for Node.js and the browser.
MIT License
172 stars 55 forks source link

Data channel support (streaming plugin) #124

Closed S4iR0X closed 3 years ago

S4iR0X commented 3 years ago

Hey Guys, I need some help. I want to send a video stream with data channel over a LAN network to multiple clients.

For this I am using a very basic code template and the video stream is working, but i get no data over the data channel. I added an event listener to the ondatachannel (see code below)

With the janus default demo I get data over the data channel. So my janus instance/config is hopefully not the problem. Inside the streamingtest.js there is a streaming.createAnswer() option of: media: { audioSend: false, videoSend: false, data: true }

Do I miss this data: true maybe in the code below and how can i add this? I added it inside the watch() function but it has no effect.

I am new to Janus / WebRTC and currently somehow lost without concrete examples so I would really appreciate your help. Thanks alot!!

   janus = new Janus.Client("ws://ipAddressOfJanusInstance:8188", {
        keepalive: "true",
      });
janus.createConnection("id").then(function (connection) {
  connection.createSession().then(function (session) {
    session.attachPlugin("janus.plugin.streaming").then(function (plugin) {
      plugin.watch(streamId, { audioSend: false, videoSend: true, data: true }).then(function (response) {
        let pc = plugin.getPeerConnection();

        pc.ondatachannel = function (event) {
          console.log(event); // not getting called
        };
      });

      plugin.on("pc:track:remote", function (event) {
        if (videoRef.current !== null)
          videoRef.current.srcObject = event.streams[0]; // getting called
      });

      plugin.on("message", function (response) {
        console.log(response); // getting called
      });
    });
  });
});
sjkummer commented 3 years ago

Unfortunately, this lib does not support datachannels at the moment.

This means, no createDataChannel is called automatically at the client side (Browser).

However, in your case calling pc.createDataChannel('JanusDataChannel'); might be enough (after getting the PeerConnection from the plugin). But I haven't tested that yet.

S4iR0X commented 3 years ago

Thanks for your answer :)

It worked like you suggested:

    session.attachPlugin("janus.plugin.streaming").then(function (plugin) {
      plugin.watch(streamId).then(function (response) {
        let pc = plugin.getPeerConnection();
        let dataChannel = pc.createDataChannel("JanusDataChannel");

        dataChannel.onmessage = function (event) {
          console.log(event.data);
        };
      });

      plugin.on("pc:track:remote", function (event) {
        if (videoRef.current !== null)
          videoRef.current.srcObject = event.streams[0]; // getting called
      });
    });

However I get the message twice, but thats another problem, probably not related to this tool..

sjkummer commented 3 years ago

That's great to hear. Thanks for letting us know 👍