I am currently trying to implement the Janus API für a Unity project, for now only the videoroom. I have got it running, including the DataChannel, but I have one small issue:
I get an incomming datachannel for every publisher I subscribe to, but that channel is only created once the first message is sent by that specific publisher and this first message then disappears into the eather. I also don’t get an event for a new subscriber where i could react and directly send a message to open the channel.
Is there a way to force this data channel creation immediately? Maybe some option when starting the Server or some small change I can make myself?
I create a DataChannel on the subscriber PeerConnection after setting the remote offer I get from Janus (if the SDP contains a DataChannel) and then I create the answer SDP. However, this DataChannel never receives any data. It seemingly only serves to inform Janus that the subscriber wants to receive data.
As soon as one of the subscribed publishers sends a message on their outgoing DataChannel, the subscriber PeerConnection gets an OnDataChannel event with a new DataChannel, which has the publisher’s ID for the label and only receives data from that publisher. But the message that seems to trigger the creation of that DataChannel gets lost. And this happens for every publisher the subscriber is listening to.
After that the subscriber can receive messages without issues, but the first one is always lost.
Thanks for the clarification. This may indeed be some sort of race condition between the messages that are sent to open the channel, and the content to send after that. I’ll see if I can replicate it locally, even though I suspect it’s something that may happen more easily in scenarios where the RTT is higher.
As a temporary workaround, I think you can open a channel from the subscriber side yourself anyway, especially considering as a subscriber you are aware of publishers you’re subscribed to (you need to proactively pass the IDs of the publishers you want to subscribe to). As such, you can try opening the datachannel named after that publisher before sending the request to subscribe to that publisher’s datachannel, which would allow the plugin to find it already open when there’s a message to relay. This may be suboptimal in the sense you may end up opening channels that won’t be used, if that publisher never ends up sending anything, but it should address your issue.
That seems to work as a preliminary solution. Thank you for your help. If anyone stumbles across this thread, here is what I am doing:
When I get the “attached” (or “updated”) message I can go through the streams array (I already do that for a lookup from the mid to the publisher id) and for the data stream I can look at the “source_ids” and on the subscriber PeerConnection create a DataChannel for each id in that list (“negotiated” is false). This happens before I set the remote SDP, contrary to what I found here. But now all peers get all messages (of course only the ones sent after they subscribed; I would need to implement a history if I wanted earlier ones).
As a side note: it would be really helpful if the documentation for the VideoRoom plugin was updated to include more about how to setup DataChannels. I was looking through multiple threads here and on the old Google Group to piece together the information needed and it took quite a while to get it working.
That message referred to creating the data channel in general, as in advertising a need to negotiate a data channel line in the SDP for the first time: that definitely needs to happen before, and that only happens once. After that, creating other data channels actually means opening new streams on an existing data channel with different IDs/labels (which is what you’re doing with publisher IDs used as label for a new stream).
That’s fair. The documentation is pretty much a live beast: it started somewhere, and then got updated many times, often any time something changes or gets added. It’s not rare things change or are added/removed and the docs aren’t updated accordingly: unfortunately it’s hard to keep track of everything. We definitely welcome contributions to improve that, so if you have time and willingness to do that, please consider a PR to expand on what you believe may be missing. The VideoRoom documentation is the long text at the beginning of janus_videoroom.c, and is generated automatically using Doxygen.