WebRTC Android - Unable to get PeerConnection.Observer callbacks

142 views
Skip to first unread message

dev tilak

unread,
Jul 24, 2024, 12:29:29 PM7/24/24
to discuss-webrtc
I am trying to implement WebRTC sample and trying to get the ICECandidates from ICE Server in Android using WebRTC. I am not able to get the PeerConnection.Observer callback like onIceCandidate(). I am only able to get the onSignalingChange() callback when I invoke the peerconnection.Close method.
Snippets:

private fun createPeerConnection() {
    PeerConnectionFactory.initialize(
        InitializationOptions.builder(this@MainActivity)
            .setEnableInternalTracer(true)
            .createInitializationOptions()
    )

    val factory = PeerConnectionFactory.builder()
        .setAudioDeviceModule(
            JavaAudioDeviceModule.builder(this@MainActivity)
                .setUseHardwareAcousticEchoCanceler(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
                .setUseHardwareNoiseSuppressor(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
                .createAudioDeviceModule().also {
                    it.setMicrophoneMute(false)
                    it.setSpeakerMute(false)
                }
        )
        .createPeerConnectionFactory()

    val iceServers = getICEServerList()
    val rtcConfig = PeerConnection.RTCConfiguration(iceServers).apply {
        sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN
    }
    Log.w(TAG, "CREATING PEER CONNECTION")
    val peerConnection = factory.createPeerConnection(rtcConfig, peerConnObserver)
    Log.d(TAG, "peerConnection: $peerConnection")
    if(peerConnection!=null) {
        Log.d(TAG, "peerConnection: ${peerConnection.connectionState()}")

    }

}

private fun getICEServerList(): List<PeerConnection.IceServer> {
    val iceServers = arrayListOf<PeerConnection.IceServer>()
   
    iceServers.add(
        PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer()
    )

    return iceServers
}

private val peerConnObserver = object : PeerConnection.Observer {
    override fun onSignalingChange(p0: PeerConnection.SignalingState?) {
        Log.d(TAG, "onSignalingChange: ")
    }

    override fun onIceConnectionChange(p0: PeerConnection.IceConnectionState?) {
        Log.d(TAG, "onIceConnectionChange: ")
    }

    override fun onIceConnectionReceivingChange(p0: Boolean) {
        Log.d(TAG, "onIceConnectionReceivingChange: ")
    }

    override fun onIceGatheringChange(p0: PeerConnection.IceGatheringState?) {
        Log.d(TAG, "onIceGatheringChange: ")
    }

    override fun onIceCandidate(p0: IceCandidate?) {
        Log.d(TAG, "onIceCandidate: ")
    }

    override fun onIceCandidatesRemoved(p0: Array<out IceCandidate>?) {
        Log.d(TAG, "onIceCandidatesRemoved: ")
    }

    override fun onAddStream(p0: MediaStream?) {
        Log.d(TAG, "onAddStream: ")
    }

    override fun onRemoveStream(p0: MediaStream?) {
        Log.d(TAG, "onRemoveStream: ")
    }

    override fun onDataChannel(p0: DataChannel?) {
        Log.d(TAG, "onDataChannel: ")
    }

    override fun onRenegotiationNeeded() {
        Log.d(TAG, "onRenegotiationNeeded: ")
    }

    override fun onIceCandidateError(event: IceCandidateErrorEvent?) {
        Log.d(TAG, "onRenegotiationNeeded: ")
    }
}

I am using io.getstream:stream-webrtc-android:1.1.3 webRTC dependency

how to fix it?

Mikko Koivisto

unread,
Jul 26, 2024, 6:13:41 AM7/26/24
to discuss-webrtc
You need to have other peer and send him offer or then wait for his offer and reply him.
I think ice candidate gathering only starts when you set local description. You won't get ice candidates just by creating peer connection. 
Reply all
Reply to author
Forward
0 new messages