Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move rotation logic from onFrame to OrientationEventListener #28

Merged
merged 18 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ coverage:
patch:
default:
target: 75.0
ignore:
- "webrtc-android-sample-app"

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ScreenCapturerAndroid implements VideoCapturer, VideoSink {
@Nullable private MediaProjection mediaProjection;
private boolean isDisposed;
private WindowManager windowManager;
private int deviceRotation = 0;
public int deviceRotation = 0;
private static final String TAG = ScreenCapturerAndroid.class.getSimpleName();

@Nullable private MediaProjectionManager mediaProjectionManager;
Expand Down Expand Up @@ -213,25 +213,28 @@ VIRTUAL_DISPLAY_DPI, DISPLAY_FLAGS, new Surface(surfaceTextureHelper.getSurfaceT
null /* callback */, null /* callback handler */);
}

// This is called on the internal looper thread of {@Code SurfaceTextureHelper}.
@Override
public void onFrame(VideoFrame frame) {
numCapturedFrames++;
Log.v(TAG, "Frame received " + numCapturedFrames);
int rotation = windowManager.getDefaultDisplay().getRotation();
public void rotateScreen(int rotation) {
if (deviceRotation != rotation) {
Log.w("Rotation", "onFrame: " + rotation);
deviceRotation = rotation;

if (deviceRotation*90 % 180 != 0) {
virtualDisplay.resize(height, width, VIRTUAL_DISPLAY_DPI);
surfaceTextureHelper.setTextureSize(height, width);
}
else {
if (deviceRotation == 0) {
virtualDisplay.resize(width, height, VIRTUAL_DISPLAY_DPI);
surfaceTextureHelper.setTextureSize(width, height);
} else if (deviceRotation == 180) {
// 180 degree is not supported by MediaProjection
} else {
virtualDisplay.resize(height, width, VIRTUAL_DISPLAY_DPI);
surfaceTextureHelper.setTextureSize(height, width);
}
}
}

// This is called on the internal looper thread of {@Code SurfaceTextureHelper}.
@Override
public void onFrame(VideoFrame frame) {
numCapturedFrames++;
Log.v(TAG, "Frame received " + numCapturedFrames);
capturerObserver.onFrameCaptured(frame);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class ScreenCapturerAndroidTest {

@Test
public void testOnFrameRotation() {
public void testRotateScreen() {

ScreenCapturerAndroid screenCapturerAndroid = spy(new ScreenCapturerAndroid(null, null));
WindowManager windowManager = Mockito.spy(WindowManager.class);
Expand All @@ -43,30 +43,34 @@ public void testOnFrameRotation() {
screenCapturerAndroid.setVirtualDisplay(virtualDisplay);
screenCapturerAndroid.setSurfaceTextureHelper(surfaceTextureHelper);

screenCapturerAndroid.setCapturerObserver(mock(CapturerObserver.class));
CapturerObserver capturerObserver = mock(CapturerObserver.class);
screenCapturerAndroid.setCapturerObserver(capturerObserver);

int width = 540;
int height = 960;
screenCapturerAndroid.setWidth(width);
screenCapturerAndroid.setHeight(height);

VideoFrame frame = new VideoFrame(mock(VideoFrame.Buffer.class), 0, 0);
screenCapturerAndroid.onFrame(frame);
screenCapturerAndroid.deviceRotation = 0;

screenCapturerAndroid.rotateScreen(90);

Mockito.when(display.getRotation()).thenReturn(1);
screenCapturerAndroid.onFrame(frame);
screenCapturerAndroid.rotateScreen(0);

Mockito.verify(virtualDisplay).resize(height, width, VIRTUAL_DISPLAY_DPI);
Mockito.verify(surfaceTextureHelper).setTextureSize(height, width);

Mockito.when(display.getRotation()).thenReturn(2);
screenCapturerAndroid.onFrame(frame);
screenCapturerAndroid.rotateScreen(90);

Mockito.verify(virtualDisplay).resize(width, height, VIRTUAL_DISPLAY_DPI);
Mockito.verify(surfaceTextureHelper).setTextureSize(width,height);

VideoFrame frame = mock(VideoFrame.class);
screenCapturerAndroid.onFrame(frame);


Mockito.verify(capturerObserver).onFrameCaptured(frame);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -905,7 +906,7 @@ public void testCreatePeerConnection() {
doNothing().when(webRTCClient).reportError(anyString(), anyString());
doThrow(new NullPointerException()).when(webRTCClient).createMediaConstraintsInternal();
webRTCClient.createPeerConnection(streamId);
verify(webRTCClient, timeout(1000)).reportError(eq(streamId), anyString());
verify(webRTCClient, timeout(10000)).reportError(eq(streamId), anyString());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public void perform(MultitrackConferenceActivity activity) {
onView(withId(R.id.join_conference_button)).perform(click());

onView(withId(R.id.join_conference_button)).check(matches(withText("Leave")));

//TODO remove sleep
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void perform(ScreenCaptureActivity activity) {
UiDevice device = UiDevice.getInstance(getInstrumentation());

onView(withId(R.id.rbScreen)).perform(click());
UiObject2 button = device.wait(Until.findObject(By.text("Start now")), 10000);
UiObject2 button = device.wait(Until.findObject(By.text("Start now")), 100000);
assertNotNull(button);
button.click();

Expand All @@ -123,7 +123,7 @@ public void perform(ScreenCaptureActivity activity) {

//FIXME: without this sleep, it's failing because onFinish event received but resources are not closed yet
try {
Thread.sleep(3000);
Thread.sleep(30000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.hardware.SensorManager;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
Expand All @@ -20,6 +24,7 @@
import android.widget.Toast;

import org.webrtc.RendererCommon;
import org.webrtc.ScreenCapturerAndroid;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoTrack;

Expand Down Expand Up @@ -47,6 +52,8 @@ public class ScreenCaptureActivity extends AbstractSampleSDKActivity {
private static final String TAG = ScreenCaptureActivity.class.getSimpleName();
private View broadcastingView;

private OrientationEventListener orientationEventListener;


/*
ATTENTION: Android refresh rate changes according to the screen changes.
Expand Down Expand Up @@ -130,6 +137,23 @@ else if(checkedId == R.id.rbRear) {
PreferenceManager.getDefaultSharedPreferences(this /* Activity context */);
serverUrl = sharedPreferences.getString(getString(R.string.serverAddress), SettingsActivity.DEFAULT_WEBSOCKET_URL);
webRTCClient.init(serverUrl, streamIdEditText.getText().toString(), IWebRTCClient.MODE_PUBLISH, tokenId, this.getIntent());

orientationEventListener
= new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){

@Override
public void onOrientationChanged(int rotationDegree) {
if (webRTCClient.getVideoCapturer() instanceof ScreenCapturerAndroid) {
((ScreenCapturerAndroid) webRTCClient.getVideoCapturer()).rotateScreen(rotationDegree);
}
}};

if (orientationEventListener.canDetectOrientation()){
orientationEventListener.enable();
}
else{
orientationEventListener.disable();
}
}

@Override
Expand Down