Can't reuse PeerConnectionClient after activity recreated

284 views Asked by At

I'm trying to correctly handle screen rotation during WebRTC call on Android. But after first rotation local video translation stopping. After creating (or recreating) activty I am creating SurfaceViewRenderers for local & remote views:

ourView.init(eglBase.eglBaseContext, null)
ourView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT)
ourView.setZOrderMediaOverlay(true)
ourView.setEnableHardwareScaler(true)
ourView.setMirror(true)


theirView.init(eglBase.eglBaseContext, null)
theirView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT)
theirView.setEnableHardwareScaler(false)
theirView.setMirror(true) 

localVideoSyncer.setTarget(ourView)
remoteVideoSyncer.setTarget(theirView)

After that, if this is a first time when connection must be created, I am initializing peer connection:

iceConnected = false
 val dataChannelParameters = CallUtils.createChannelParameters()
 peerConnectionParameters = CallUtils.createConnectionParameters(videoWidth, videoHeight, dataChannelParameters)
 if(appRTCClient == null) {
 appRTCClient = CallUtils.createAppRTCClient(roomId, this, info?.ice ?: emptyList())
 }
 roomConnectionParameters = CallUtils.createRoomConnectionParameters(info?.address, roomId)
 if(peerConnectionClient == null) {
 peerConnectionClient = PeerConnectionClient(
 applicationContext, eglBase, peerConnectionParameters, this)
 }
 val options = PeerConnectionFactory.Options()
 peerConnectionClient!!.createPeerConnectionFactory(options)
 if (appRTCClient == null) {
 NetworkLogger.log(TAG,"AppRTC client is not allocated for a call.")
 return
 }
 if(callStartedTimeMs == 0L)
 callStartedTimeMs = System.currentTimeMillis()

 appRTCClient!!.connectToRoom(roomConnectionParameters!!)

 if(audioManager == null)
 audioManager = AppRTCAudioManager(applicationContext)

If activity is recreated, PeerConnectionClient is no longer can send video to remote target. I tried to reassign localRender & videoCapturer, but that has no effect. Is it possible to reuse existing connection after activity recreated?

1

There are 1 answers

2
Alexey Osminin On

I think the best solution here is to place appRTCClient and related objects outside of activity and make them independent from activity's lifecycle. You use applicationContext thats why you can store it in some singletone which relies on application's lifecycle. Another way is to use fragment inside your activity for displaying chat windows and manually handle rotatation inside onConfigurationChanged method, so you will have to replace portrait fragment with landscape one.