I am trying to build a video call app which has a feature of screen sharing. The users can share their screen during the call. I'm using WebRTC
SDK to meet my purpose, but they have a solution for screen share when the call starts but not for screen share while the call is ongoing. One can tick the screen sharing option and can start the call but cannot start screen sharing during the call.
I added a button on the CallActivity screen which on click calls MediaProjection Class of Android to cast the screen but the casted screen is not being shown remotly.
public void onScreenShare(boolean isScreenShared) {
screencaptureEnabled = isScreenShared;
if (screencaptureEnabled && videoWidth == 0 && videoHeight == 0) {
DisplayMetrics displayMetrics = getDisplayMetrics();
videoWidth = displayMetrics.widthPixels;
videoHeight = displayMetrics.heightPixels;
}
if (isPemitted()) {
startScreenCapture();
} else {
Log.i(TAG, "onScreenShare: not permitted");
}
/*if (peerConnectionClient != null) {
peerConnectionClient.stopVideoSource();
}*/
}
private void startScreenCapture() {
MediaProjectionManager mediaProjectionManager =
(MediaProjectionManager) getApplication().getSystemService(
Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(
mediaProjectionManager.createScreenCaptureIntent(),
CAPTURE_PERMISSION_REQUEST_CODE);
Log.d("tagged", ">>>>Method called :- ");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("tagged", ">>>>Method called :- " + requestCode);
if (requestCode != CAPTURE_PERMISSION_REQUEST_CODE)
return;
else {
mediaProjectionPermissionResultCode = resultCode;
mediaProjectionPermissionResultData = data;
if (peerConnectionParameters.videoCallEnabled) {
videoCapturer = createVideoCapturer();
}
peerConnectionClient.createPeerConnection(
localProxyVideoSink, remoteSinks, videoCapturer,
signalingParameters);
}
}
private @Nullable
VideoCapturer createScreenCapturer() {
Log.d("CheckMedia", ">>>Checking " +
mediaProjectionPermissionResultData);
if (mediaProjectionPermissionResultCode != Activity.RESULT_OK) {
reportError("User didn't give permission to capture the screen.");
return null;
}
return new ScreenCapturerAndroid(
mediaProjectionPermissionResultData, new
MediaProjection.Callback() {
@Override
public void onStop() {
reportError("User revoked permission to capture the screen.");
}
});
}
This code starts the casting on local device but is not streaming anything on remote device.
UPDATE
private void switchCameraInternal() {
if (videoCapturer instanceof CameraVideoCapturer) {
if (!isVideoCallEnabled() || isError) {
Log.e(TAG,
"Failed to switch camera. Video: " +
isVideoCallEnabled() + ". Error : " + isError);
return; // No video is sent or only one camera is available or
error happened.
}
Log.d(TAG, "Switch camera");
CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer)
videoCapturer;
cameraVideoCapturer.switchCamera(null);
} else {
Log.d(TAG, "Will not switch camera, video caputurer is not a
camera");
}
}
public void switchCamera() {
executor.execute(this::switchCameraInternal);
}
private void startScreenSharing() {
if (videoCapturer instanceof ScreenCapturerAndroid) {
if (!isVideoCallEnabled() || isError) {
Log.e(TAG,
"Failed to share screen. Video: " + isVideoCallEnabled()
+ ". Error : " + isError);
return; // No video is sent or only one camera is available or
error happened.
}
ScreenCapturerAndroid screenCapturerAndroid =
(ScreenCapturerAndroid) videoCapturer;
screenCapturerAndroid.startCapture(500, 500, 30);
}
}
public void screenSharing() {
executor.execute(this::startScreenSharing);
}
I did the changes, and made the code look similar to switchCamera() code but I am getting an Not On Camera Thread Exception.