我已经用 kotlin 实现了一个 webRtc android 应用程序。我创建了一个类 LocalParticipant,在里面我有一个启动相机和音频流的功能
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)
}
为了启动整个过程,以便 localParticipant 可以看到自己,我
startLocalParticipantTracks
从 ViewModel 调用。一切正常。
所以现在,我想实现 PictureInPictureMode。当我按下主页按钮时,localParticipantContainer 视图被隐藏。但是我再次打开应用程序窗口,预览没有加载,所以我什么也看不到,尽管远程参与者继续看到我在相机上直播。
从 PipMode 退出时,我只调用localParticipant.addLocalSink
我还应该做什么?