Android无法在通信过程中从摄像头切换到屏幕共享webrtc



我正在开发android应用程序,并将webrtc与openvidu一起使用。我的屏幕共享和相机共享工作。但这两部作品中只有一部是同类作品。我无法在它们之间切换。默认情况下,当建立连接时,相机将被共享,点击按钮,我应该能够在不断开现有连接的情况下共享屏幕。然而,这并没有发生在我的情况下。它不会在点击按钮时发布屏幕。它变成了空白。

如果有人在相机和共享屏幕之间切换,请帮助我。提前谢谢。

public VideoTrack MakeCameraSettingsReady() {
final EglBase.Context eglBaseContext = EglBase.create().getEglBaseContext();
PeerConnectionFactory peerConnectionFactory = this.sessionManager.getPeerConnectionFactory();

// create AudioSource
AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
this.audioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);

surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext);
// create VideoCapturer
videoCapturer = createVideoCapturer();

//MediaConstraints constraints = new MediaConstraints();

VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturer.isScreencast());
localVideoTrack = peerConnectionFactory.createVideoTrack("100", videoSource);
videoCapturer.initialize(surfaceTextureHelper, context, videoSource.getCapturerObserver());
videoCapturer.startCapture(720, 1280, 30);

return localVideoTrack;
}

public VideoTrack MakeScreenCaptureReady() {
final EglBase.Context eglBaseContext = EglBase.create().getEglBaseContext();
PeerConnectionFactory peerConnectionFactory = this.sessionManager.getPeerConnectionFactory();

// create AudioSource
AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
this.audioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);

surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext);
// create VideoCapturer
screenCapturerAndroid = (ScreenCapturerAndroid) createScreenCapturer();

//MediaConstraints constraints = new MediaConstraints();

VideoSource videoSource = peerConnectionFactory.createVideoSource(screenCapturerAndroid.isScreencast());
localVideoTrack = peerConnectionFactory.createVideoTrack("100", videoSource);
screenCapturerAndroid.initialize(surfaceTextureHelper, context, videoSource.getCapturerObserver());
screenCapturerAndroid.startCapture(720, 1280, 30);
return localVideoTrack;
}

当我点击按钮共享屏幕时,我使用以下代码:

private fun stopCameraShare(){
videoCapturerAndroid?.stopCapture()
localRenderer.dispose()
localVideoView.release()
localVideoView.clearImage()
stream?.removeTrack(localVideoTrack)
localVideoTrack.dispose()
}
private fun shareScreen(){
stopCameraShare()
val mediaProjectionManager = activity!!.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), 29)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SCREEN_RECORD_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//Start screen recording
mResultCode = resultCode
mResultData = data
runOnUiThread {
screenCaptureVideoTrack = mLiveSessionViewModel!!.localParticipant?.MakeScreenCaptureReady()
mLiveSessionViewModel!!.localParticipant?.screenCaptureVideoTrack = screenCaptureVideoTrack
}
}
}
}

我尝试过的参考资料:如何在视频/音频通话中远程共享屏幕?

https://github.com/Jeffiano/ScreenShareRTC

https://chromium.googlesource.com/external/webrtc/+/b75f2541c942e2f35c3b7d7003ed17504176ced1/webrtc/api/android/java/src.org/webrtc/ScreenCapturerAndroid.java

WebRTC-在通信中间更改视频流

最终,我试图找到一种方法,在通信过程中添加Track/removeTrack。非常感谢。

我认为您正在使用两个视频音轨。你为什么不试着只播放一首视频曲目呢。

与相机共享类似,将视频轨道分配为

mLiveSessionViewModel!!.localParticipant?.mediaStream.videotraks[0] = cameraVideoTrack

要切换到屏幕共享,请删除现有的视频轨道并添加相应的录像带

mLiveSessionViewModel!!.localParticipant?.mediaStream.videotrakcs[0].removeTrack(cameraVideoTrack)
mLiveSessionViewModel!!.localParticipant?.mediaStream.videotracks[0].addTrack(screenShareVideoTrack)

相关内容

  • 没有找到相关文章

最新更新