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?
Try calling when you open the app again:
This will start video(video capturer) source again