Reopen local surfaceViewRenderer after pictureInPicture mode android

684 Views Asked by At

I have implemented a webRtc android application with kotlin. I have created a class LocalParticipant and inside there i have a function that starts the camera and audio streams

private var eglBaseContext: EglBase.Context = EglBase.create().eglBaseContext
private var peerConnectionFactory: PeerConnectionFactory? = null
private var surfaceTextureHelper: SurfaceTextureHelper? = null

private var audioSource: AudioSource? = null
private var videoSource: VideoSource? = null
private var videoCapturer: VideoCapturer? = null

fun initAudioSource(): AudioSource? {
        if (audioSource == null) audioSource = peerConnectionFactory!!.createAudioSource(MediaConstraints())
        return audioSource
    }

    fun initAudioTrack(): AudioTrack? {
        return peerConnectionFactory?.let {
            audioTrack = it.createAudioTrack("101", it.createAudioSource(MediaConstraints()))
            audioTrack
        }
    }

    fun initVideoSource(): VideoSource? {
        if (videoSource == null) {
            if (videoCapturer == null)
                videoCapturer = createCameraCapturer()
            videoSource = peerConnectionFactory?.createVideoSource(videoCapturer!!.isScreencast)
        }
        return videoSource
    }

    fun initVideoTrack(videoHeader: VideoHeader): VideoTrack? {
        if (videoTrack == null) {
            videoCapturer?.initialize(surfaceTextureHelper, context, videoSource?.capturerObserver)
            videoCapturer?.startCapture(videoHeader.width, videoHeader.height, videoHeader.fps)
            videoTrack = peerConnectionFactory?.createVideoTrack("100", videoSource)
        }
        return videoTrack
    }

    fun addLocalSink() {
        videoTrack?.removeSink(localVideoView)
        videoTrack?.addSink(localVideoView)
    }

    fun setUpVideoSource(videoHeader: VideoHeader) {
        peerConnectionFactory?.let {
            surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext)
            initVideoSource()
            initVideoTrack(videoHeader)
            addLocalSink()
        }
    }

    fun startLocalParticipantTracks(videoHeader: VideoHeader) {
        peerConnectionFactory = session.peerConnectionFactory ?: return
        initAudioSource()
        initAudioTrack()
        if (videoHeader.hasVideo == 1) setUpVideoSource(videoHeader)
    }

In order to start the whole proccess so that the localParticipant can see himself i call the startLocalParticipantTracks from the ViewModel. Everything works fine.
So now, i want to implement the PictureInPictureMode. When i press the home button the localParticipantContainer view gets hidden. But i open back again the application window the preview does not load so i see nothing, although that the remoteParticipants keep on see me live on camera.

On Exit from PipMode i call only the localParticipant.addLocalSink What else should i do?

2

There are 2 best solutions below

0
On

Try calling when you open the app again:

videoCapturer?.startCapture(videoHeader.width, videoHeader.height, videoHeader.fps)
 

This will start video(video capturer) source again

0
On

Problem solved. The issue was that I was not passing the new surfaceViewRenderer because the activity is recreated. So i had to pass the new view