Native WebRTC, properly calling datachannel methods

382 views
Skip to first unread message

mike ads

unread,
Apr 8, 2016, 12:22:56 AM4/8/16
to discuss-webrtc
I'm successfully able to create a data channel. The OnDataChannel() callback of the PeerConnectionObserver fires, i get the pointer to the data channel. I use this pointer to register a data channel observer, and i can successfully use that observer to receive messages.

But when i try to use that saved data channel pointer to call it's methods like state() or send(), i just get access violations. I can call state() immediately after the OnDataChannel() callback is fired, but it doesn't seem usable at any time later on. I can still receive data though the OnMessage callback, but i can't send any data.

I can't seem to figure out whats wrong. I thought maybe it was due to my not calling the methods on the worker or signaling threads, so i tried invoking a lambda from which i called the methods, still errors. I tried calling the state() method from the OnStateChanged callback, still errors. The channel isn't being closed on the other end, and even then it shouldn't matter because i should be able to call state() regardless to check if it's closed.

Am I doing something wrong here?

Taylor Brandstetter

unread,
Apr 8, 2016, 1:16:46 PM4/8/16
to discuss...@googlegroups.com
When OnDataChannel fires, it actually gives you a "proxy" object. When a method on the proxy object is called, it will invoke it on the correct thread, so it provides added safety/convenience to the application.

Just like the actual DataChannel underneath, the proxy object is reference counted. So unless you obtain a reference to it, it will go out of scope and be deleted after the OnDataChannel callback.

So, even though the actual DataChannel is alive and providing you callbacks, the pointer to the proxy object you have is invalid. To avoid this you need to either use a scoped_refptr or call AddRef/Release.

I realize that if OnDataChannel fired with a scoped_refptr instead of a raw pointer, this confusion probably could have been avoided. I'll make a note to myself to look into changing the API in that way.

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/c876ab92-9633-4304-89bd-58da0a828c9d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mike ads

unread,
Apr 8, 2016, 1:23:36 PM4/8/16
to discuss-webrtc
Hey Taylor thank you very much for the response. I did figure this morning that this was the issue.

I've noticed scoped_refptr being used in some areas and i thought i understood what they were. So I initially suspected the issue might be because the pointer is going out of scope, but I did not see any traces of a special pointer anywhere, so dropped the idea. It's only when i ran out of options and saw that the DataChannelInterface implemented the RefCountInterface, did I again believe this to be the issue. Calling AddRef() before going out of scope on the callback did indeed solve the problem.

Thanks again!

shyam pradeep

unread,
May 30, 2017, 9:53:00 AM5/30/17
to discuss-webrtc
Hi Mike,

Can you share the code to implement the data channel in native code? Or Can you point me to any references to code for data channel? 

Thanks,
Shyam S.
Reply all
Reply to author
Forward
0 new messages