Android, how to add multiple tracks to peer connection in WebRTC?

695 Views Asked by At

I want to send multiple track to remote peer. For example, videoTrack, audioTrack, shareScreenTrack.

I used UNIFIED_PLAN like below usage.

val rtcConfig = PeerConnection.RTCConfiguration(
    arrayListOf(PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer())
).apply { sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN }

And I add tracks like this

peerConnection?.addTrack(videoTrack)
peerConnection?.addTrack(audioTrack)
peerConnection?.addTrack(captureScreenVideoTrack)

but only the first track goes. When I add onTrack debugging, debugging for videoTrack drops only once. It doesn't fall for audioTrack and captureScreenVideoTrack.

override fun onTrack(transceiver: RtpTransceiver?) {
    super.onTrack(transceiver)
    val track = transceiver?.receiver?.track() ?: return
    when (track.kind()){
            MediaStreamTrack.VIDEO_TRACK_KIND ->{
                //videoTrack or captureScreenVideoTrack
            }
            MediaStreamTrack.AUDIO_TRACK_KIND ->{
                //audioTrack
            }

            else -> {}
        }    
}
1

There are 1 best solutions below

1
cafer yıldız On

I found my problem. my fault. I was calling createOffer from onRenegotiationNeeded. OnRenegotiationNeeded is triggered when the first track is added to the peerconnection. Since the second track is added right after this, onRenegotiationNeeded is triggered again. I removed the createOffer from onRenegotiationNeeded and made it call only on the first call and the problem was resolved.