Skip to content

Commit

Permalink
Reset video capture on folding event
Browse files Browse the repository at this point in the history
Handle folding event the same way as rotation events.

Fixes #3960 <#3960>
PR #3979 <#3979>

Signed-off-by: Romain Vimont <[email protected]>
  • Loading branch information
AdoPi authored and rom1v committed May 29, 2023
1 parent e7d70f3 commit 6e1aebc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
26 changes: 26 additions & 0 deletions server/src/main/aidl/android/view/IDisplayFoldListener.aidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.view;

/**
* {@hide}
*/
oneway interface IDisplayFoldListener
{
/** Called when the foldedness of a display changes */
void onDisplayFoldChanged(int displayId, boolean folded);
}
30 changes: 30 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.os.IBinder;
import android.os.SystemClock;
import android.view.IRotationWatcher;
import android.view.IDisplayFoldListener;
import android.view.InputDevice;
import android.view.InputEvent;
import android.view.KeyCharacterMap;
Expand All @@ -35,6 +36,10 @@ public interface RotationListener {
void onRotationChanged(int rotation);
}

public interface FoldListener {
void onFoldChanged(int displayId, boolean folded);
}

public interface ClipboardListener {
void onClipboardTextChanged(String text);
}
Expand All @@ -46,6 +51,7 @@ public interface ClipboardListener {

private ScreenInfo screenInfo;
private RotationListener rotationListener;
private FoldListener foldListener;
private ClipboardListener clipboardListener;
private final AtomicBoolean isSettingClipboard = new AtomicBoolean();

Expand Down Expand Up @@ -93,6 +99,26 @@ public void onRotationChanged(int rotation) {
}
}, displayId);

ServiceManager.getWindowManager().registerDisplayFoldListener(new IDisplayFoldListener.Stub() {
@Override
public void onDisplayFoldChanged(int displayId, boolean folded) {
synchronized (Device.this) {
DisplayInfo displayInfo = ServiceManager.getDisplayManager().getDisplayInfo(displayId);
if (displayInfo == null) {
Ln.e("Display " + displayId + " not found\n" + LogUtils.buildDisplayListMessage());
return;
}

screenInfo = ScreenInfo.computeScreenInfo(displayInfo.getRotation(), displayInfo.getSize(), options.getCrop(),
options.getMaxSize(), options.getLockVideoOrientation());
// notify
if (foldListener != null) {
foldListener.onFoldChanged(displayId, folded);
}
}
}
});

if (options.getControl() && options.getClipboardAutosync()) {
// If control and autosync are enabled, synchronize Android clipboard to the computer automatically
ClipboardManager clipboardManager = ServiceManager.getClipboardManager();
Expand Down Expand Up @@ -224,6 +250,10 @@ public synchronized void setRotationListener(RotationListener rotationListener)
this.rotationListener = rotationListener;
}

public synchronized void setFoldListener(FoldListener foldlistener) {
this.foldListener = foldlistener;
}

public synchronized void setClipboardListener(ClipboardListener clipboardListener) {
this.clipboardListener = clipboardListener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class ScreenEncoder implements Device.RotationListener, AsyncProcessor {
public class ScreenEncoder implements Device.RotationListener, Device.FoldListener, AsyncProcessor {

private static final int DEFAULT_I_FRAME_INTERVAL = 10; // seconds
private static final int REPEAT_FRAME_DELAY_US = 100_000; // repeat after 100ms
Expand Down Expand Up @@ -53,6 +53,11 @@ public ScreenEncoder(Device device, Streamer streamer, int videoBitRate, int max
this.downsizeOnError = downsizeOnError;
}

@Override
public void onFoldChanged(int displayId, boolean folded) {
resetCapture.set(true);
}

@Override
public void onRotationChanged(int rotation) {
resetCapture.set(true);
Expand All @@ -68,6 +73,7 @@ private void streamScreen() throws IOException, ConfigurationException {
MediaFormat format = createFormat(codec.getMimeType(), videoBitRate, maxFps, codecOptions);
IBinder display = createDisplay();
device.setRotationListener(this);
device.setFoldListener(this);

streamer.writeVideoHeader(device.getScreenInfo().getVideoSize());

Expand Down Expand Up @@ -115,6 +121,7 @@ private void streamScreen() throws IOException, ConfigurationException {
} finally {
mediaCodec.release();
device.setRotationListener(null);
device.setFoldListener(null);
SurfaceControl.destroyDisplay(display);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import android.os.IInterface;
import android.view.IRotationWatcher;
import android.view.IDisplayFoldListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -108,4 +109,13 @@ public void registerRotationWatcher(IRotationWatcher rotationWatcher, int displa
throw new AssertionError(e);
}
}

public void registerDisplayFoldListener(IDisplayFoldListener foldListener) {
try {
Class<?> cls = manager.getClass();
cls.getMethod("registerDisplayFoldListener", IDisplayFoldListener.class).invoke(manager, foldListener);
} catch (Exception e) {
throw new AssertionError(e);
}
}
}

0 comments on commit 6e1aebc

Please sign in to comment.