如何在视频/音频通话中远程共享屏幕?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在视频/音频通话中远程共享屏幕?相关的知识,希望对你有一定的参考价值。
我正在尝试构建一个具有屏幕共享功能的视频通话应用程序。用户可以在通话期间共享屏幕。我正在使用WebRTC
SDK来满足我的目的,但他们在呼叫开始时有屏幕共享的解决方案,但在呼叫正在进行时没有屏幕共享。可以勾选屏幕共享选项并可以启动呼叫,但无法在通话期间启动屏幕共享。
我在CallActivity屏幕上添加了一个按钮,在点击时调用MediaProjection Class of android来投射屏幕,但是没有远程显示投射屏幕。
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.");
}
});
}
此代码在本地设备上启动转换,但不在远程设备上传输任何内容。
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);
}
我做了更改,并使代码看起来类似于switchCamera()代码,但我得到一个Not On Camera Thread Exception。
我不确定您是否可以同时从相机和屏幕流式传输。但是你可以做的是:
- 用户单击屏幕共享按钮
- 您可以使用
PeerConnection
从PeerConnection.removeTrack(RtpSender sender)
中移除相机视频轨道 - 您可以使用
ScreenCapturerAndroid
创建屏幕视频轨道(如您所做) - 您将曲目添加到
PeerConnection
如果你说屏幕共享没有通话就行了,那么第4步和第5步应该已经完成了。
当您删除其轨道时,不要忘记处置/释放与Camera
相关的所有资源。
同样要停止屏幕共享并返回相机,只需执行屏幕轨道的上述步骤。
更新:
以下是WebRTC客户端的一部分,它允许我实现您的要求(您可以根据当前的代码库进行调整):
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?) {
if (requestCode != 29)
return
initVideos()
videoCapturerAndroid = ScreenCapturerAndroid(
data, object : MediaProjection.Callback() {
override fun onStop() {
Timber.e("User revoked permission to capture the screen.")
}
})
peerConnectionFactory.setVideoHwAccelerationOptions(rootEglBase.eglBaseContext, rootEglBase.eglBaseContext)
videoSource = peerConnectionFactory.createVideoSource(videoCapturerAndroid)
localVideoTrack = peerConnectionFactory.createVideoTrack("100", videoSource)
videoCapturerAndroid?.startCapture(300, 300, 30)
stream?.addTrack(localVideoTrack)
}
PS:这对peerConnectionFactory.setVideoHwAccelerationOptions(rootEglBase.eglBaseContext, rootEglBase.eglBaseContext)
非常重要
希望对你有帮助 !
以上是关于如何在视频/音频通话中远程共享屏幕?的主要内容,如果未能解决你的问题,请参考以下文章