Search code examples
androidkotlinwebrtc

Android webrtc still running after app exited with back button


I'm new to Webrtc, I'm using the AWS Webrtc demo with Android Nav Component. When I exited the app with the back button, I can see that Webrtc is still running or I can see the following log:

EglRenderer: cameraSurfaceViewDropping frame - No surface

In my fragment at the onStop method my code is as follow:

        Thread.setDefaultUncaughtExceptionHandler(null)

        if (rootEglBase != null) {
            rootEglBase!!.release()
            rootEglBase = null
        }

        if (remoteView != null) {
            remoteView!!.release()
            remoteView = null
        }

        if (localPeer != null) {
            localPeer!!.dispose()
            localPeer = null
        }

        if (videoSource != null) {
            videoSource!!.dispose()
            videoSource = null
        }

        if (videoCapturer != null) {
            try {
                videoCapturer?.stopCapture()
                videoCapturer?.dispose()
            } catch (e: InterruptedException) {
                Timber.e("Failed to stop webrtc video capture. $e ")
            }
            videoCapturer = null
        }

        if (client != null) {
            this.client!!.disconnect()
            this.client = null
        }
        peerConnectionFoundMap.clear()
        pendingIceCandidatesMap.clear()

However, I can see the problem only when I exited the app through the back button, if I killed the app, I don't get the log. Has anyone experienced this?

Thank you.


Solution

  • This is the way you should destroy your WebRTC session on onDestroy() or onStop().

        if (videoCaptureAndroid != null) {
            videoCaptureAndroid?.stopCapture()
            videoCaptureAndroid = null
        }
        if (localPeer != null) {
            localPeer?.close()
            localPeer = null
        }
        if (videoSource != null) {
            videoSource?.dispose()
            videoSource = null
        }
        if (audioSource != null) {
            audioSource?.dispose()
            audioSource = null
        }
        if (localAudioTrack != null) {
            localAudioTrack?.dispose()
            localAudioTrack = null
        }
        if (currentRemoteMediaStream != null) {
            currentRemoteMediaStream?.dispose()
            currentRemoteMediaStream = null
        }
        if (localVideoView != null) {
            localVideoView?.release()
            localVideoView = null
        }
        if (remoteVideoView != null) {
            remoteVideoView?.release()
            remoteVideoView = null
        }
        rootEglBase.release();