I'm working on android app and using webrtc with openvidu. I got screen sharing and camera sharing working. But only either of those works in one peerconnection. I'm unable to switch between them. By default when connection is established, the camera will be shared and on click of a button I should be able to share screen without disconnecting existing connection. However that is not happening in my case. It doesn't publish the screen on click of button. It goes blank.
If anyone has worked on switching between camera to sharescreen and vice versa, please help me with this. Thanks in advance.
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;
}
When I click button to share screen, I use below code:
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
}
}
}
}
References that I tried: How to share screen remotely in a video/audio call?
https://github.com/Jeffiano/ScreenShareRTC
WebRTC - change video stream in the middle of communication
Ultimately I'm trying to find a way where I can addTrack/removeTrack in middle of communication. Thank you!