8000 [Android] Screen share orientation fix #495. (#562) · Appleprabh/flutter-webrtc@d0f3713 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0f3713

Browse files
[Android] Screen share orientation fix flutter-webrtc#495. (flutter-webrtc#562)
Co-authored-by: Ayman-Barghout <ayman.a.barghout@gmail.com>
1 parent 364753d commit d0f3713

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

android/src/main/java/com/cloudwebrtc/webrtc/GetUserMediaImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import org.webrtc.MediaStream;
6161
import org.webrtc.MediaStreamTrack;
6262
import org.webrtc.PeerConnectionFactory;
63-
import org.webrtc.ScreenCapturerAndroid;
6463
import org.webrtc.SurfaceTextureHelper;
6564
import org.webrtc.VideoCapturer;
6665
import org.webrtc.VideoSource;
@@ -466,7 +465,7 @@ protected void onReceiveResult(int requestCode, Bundle resultData) {
466465
MediaStreamTrack[] tracks = new MediaStreamTrack[1];
467466
VideoCapturer videoCapturer = null;
468467
videoCapturer =
469-
new ScreenCapturerAndroid(
468+
new OrientationAwareScreenCapturer(
470469
mediaProjectionData,
471470
new MediaProjection.Callback() {
472471
@Override
@@ -502,7 +501,7 @@ public void onStop() {
502501
info.capturer = videoCapturer;
503502

504503
videoCapturer.startCapture(info.width, info.height, info.fps);
505-
Log.d(TAG, "ScreenCapturerAndroid.startCapture: " + info.width + "x" + info.height + "@" + info.fps);
504+
Log.d(TAG, "OrientationAwareScreenCapturer.startCapture: " + info.width + "x" + info.height + "@" + info.fps);
506505

507506
String trackId = stateProvider.getNextTrackUUID();
508507
mVideoCapturers.put(trackId, info);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.cloudwebrtc.webrtc;
2+
3+
import org.webrtc.ScreenCapturerAndroid;
4+
import org.webrtc.SurfaceTextureHelper;
5+
import org.webrtc.CapturerObserver;
6+
import org.webrtc.VideoFrame;
7+
8+
import android.annotation.TargetApi;
9+
import android.content.Context;
10+
import android.content.Intent;
11+
import android.media.projection.MediaProjection;
12+
import android.util.Log;
10000 13+
import android.view.Surface;
14+
import android.view.WindowManager;
15+
16+
17+
/**
18+
* An implementation of ScreenCapturerAndroid to capture the screen content while being aware of device orientation
19+
*/
20+
@TargetApi(21)
21+
public class OrientationAwareScreenCapturer
22+
extends ScreenCapturerAndroid {
23+
private Context applicationContext;
24+
private WindowManager windowManager;
25+
private int width;
26+
private int height;
27+
private boolean isPortrait;
28+
29+
/**
30+
* Constructs a new Screen Capturer.
31+
*
32+
* @param mediaProjectionPermissionResultData the result data of MediaProjection permission
33+
* activity; the calling app must validate that result code is Activity.RESULT_OK before
34+
* calling this method.
35+
* @param mediaProjectionCallback MediaProjection callback to implement application specific
36+
* logic in events such as when the user revokes a previously granted capture permission.
37+
**/
38+
public OrientationAwareScreenCapturer(Intent mediaProjectionPermissionResultData,
39+
MediaProjection.Callback mediaProjectionCallback) {
40+
super(mediaProjectionPermissionResultData, mediaProjectionCallback);
41+
}
42+
43+
@Override
44+
public synchronized void initialize(SurfaceTextureHelper surfaceTextureHelper, Context applicationContext, CapturerObserver capturerObserver) {
45+
super.initialize(surfaceTextureHelper, applicationContext, capturerObserver);
46+
this.applicationContext = applicationContext;
47+
Log.d("OrientationAwareSC", "OrientationAwareScreenCapturer: initialized and orientation isPortrait? " + this.isPortrait);
48+
}
49+
50+
@Override
51+
public synchronized void startCapture(int width, int height, int ignoredFramerate) {
52+
this.windowManager = (WindowManager) applicationContext.getSystemService(
53+
Context.WINDOW_SERVICE);
54+
this.isPortrait = isDeviceOrientationPortrait();
55+
if (this.isPortrait) {
56+
this.width = width;
57+
this.height = height;
58+
} else {
59+
this.height = width;
60+
this.width = height;
61+
}
62+
super.startCapture(width, height, ignoredFramerate);
63+
}
64+
65+
@Override
66+
public void onFrame(VideoFrame frame) {
67+
final boolean isOrientationPortrait = isDeviceOrientationPortrait();
68+
if (isOrientationPortrait != this.isPortrait) {
69+
this.isPortrait = isOrientationPortrait;
70+
71+
if (this.isPortrait) {
72+
super.changeCaptureFormat(this.width, this.height, 15);
73+
} else {
74+
super.changeCaptureFormat(this.height, this.width, 15);
75+
}
76+
}
77+
super.onFrame(frame);
78+
}
79+
80+
private boolean isDeviceOrientationPortrait() {
81+
final int surfaceRotation = windowManager.getDefaultDisplay().getRotation();
82+
83+
return surfaceRotation != Surface.ROTATION_90 && surfaceRotation != Surface.ROTATION_270;
84+
}
85+
86+
}

0 commit comments

Comments
 (0)
0