【发布时间】:2019-02-14 12:37:58
【问题描述】:
我正在尝试构建一个具有屏幕共享功能的视频通话应用程序。用户可以在通话期间共享他们的屏幕。我正在使用WebRTC SDK 来满足我的目的,但是他们有一个在通话开始时屏幕共享的解决方案,但在通话进行时没有屏幕共享的解决方案。可以勾选屏幕共享选项,可以开始通话,但通话过程中不能开始屏幕共享。
我在 CallActivity 屏幕上添加了一个按钮,单击该按钮会调用 Android 的 MediaProjection 类来投射屏幕,但投射的屏幕并未远程显示。
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.");
}
});
}
此代码在本地设备上开始投射,但不在远程设备上流式传输任何内容。
更新
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 异常。
【问题讨论】:
-
您提到如果您不先启动视频,则共享工作正常,因此除了在添加新视频轨道之前删除视频轨道外,无需进行任何更改。检查我的更新
-
Reference 仍然无法成功。你能帮帮我吗,实际上是从过去 5 天开始尝试的。
-
您可以查看我的代码示例并满足您的需求
-
@113408 我陷入了与上述相同的情况。如果我能获得您的代码链接以满足我的需要,那将非常有帮助。
标签: android webrtc screensharing android-mediaprojection