GetStream / webrtc-android

🛰️ A versatile WebRTC pre-compiled Android library that reflects the recent WebRTC updates to facilitate real-time video chat for Android and Compose.
https://getstream.github.io/webrtc-android/
Apache License 2.0
568 stars 62 forks source link

DataChannel example #44

Open mdrlzy opened 1 year ago

mdrlzy commented 1 year ago

Looks like PeerConnection.createDataChannel does not trigger PeerConnection.Observer.onDataChannel Maybe I'm doing something wrong So could you provide small example?

ApplY3D commented 10 months ago

It will be triggered after the two peers establish a connection

ApplY3D commented 10 months ago

also you could set negotiated=true and id=0 to all your peers and you will not need onDataChannel anymore

val dataChannelConfig = DataChannel.Init()
dataChannelConfig.negotiated = true
dataChannelConfig.id = 0
dataChannel = localPeer?.createDataChannel("ch0", dataChannelConfig)
dataChannel?.registerObserver(object : DataChannel.Observer {
    override fun onBufferedAmountChange(p0: Long) {}
    override fun onMessage(buffer: DataChannel.Buffer?) {
        if (buffer == null) return
        val msg = StandardCharsets.UTF_8.decode(buffer.data)
    }

    override fun onStateChange() {
        if (dataChannel?.state() == DataChannel.State.OPEN) {
            // peers connected, channel open
        }
    }
})