Skip to content

Commit

Permalink
feat: sync scrcpy server
Browse files Browse the repository at this point in the history
  • Loading branch information
rankun committed Jan 14, 2020
1 parent 2c2ad2a commit a26620f
Show file tree
Hide file tree
Showing 40 changed files with 817 additions and 455 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
/server/gradlew
/server/gradlew.bat
/server/local.properties
build
build-*
*.DS_Store
4 changes: 2 additions & 2 deletions QtScrcpy/QtScrcpy.pro
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ INCLUDEPATH += \

# 统一版本号入口,只修改这一个地方即可
VERSION_MAJOR = 1
VERSION_MINOR = 0
VERSION_PATCH = 4
VERSION_MINOR = 12
VERSION_PATCH = 1

# qmake变量的方式定义版本号
VERSION = $${VERSION_MAJOR}.$${VERSION_MINOR}.$${VERSION_PATCH}
Expand Down
4 changes: 2 additions & 2 deletions QtScrcpy/device/controller/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void Controller::postControlMsg(ControlMsg *controlMsg)

void Controller::test(QRect rc)
{
ControlMsg* controlMsg = new ControlMsg(ControlMsg::CMT_INJECT_MOUSE);
controlMsg->setInjectMouseMsgData(AMOTION_EVENT_ACTION_DOWN, AMOTION_EVENT_BUTTON_PRIMARY, rc);
ControlMsg* controlMsg = new ControlMsg(ControlMsg::CMT_INJECT_TOUCH);
controlMsg->setInjectTouchMsgData(POINTER_ID_MOUSE, AMOTION_EVENT_ACTION_DOWN, AMOTION_EVENT_BUTTON_PRIMARY, rc, 1.0f);
postControlMsg(controlMsg);
}

Expand Down
39 changes: 22 additions & 17 deletions QtScrcpy/device/controller/inputconvert/controlmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,13 @@ void ControlMsg::setInjectTextMsgData(QString& text)
m_data.injectText.text[tmp.length()] = '\0';
}

void ControlMsg::setInjectMouseMsgData(AndroidMotioneventAction action, AndroidMotioneventButtons buttons, QRect position)
void ControlMsg::setInjectTouchMsgData(quint64 id, AndroidMotioneventAction action, AndroidMotioneventButtons buttons, QRect position, float pressure)
{
m_data.injectMouse.action = action;
m_data.injectMouse.buttons = buttons;
m_data.injectMouse.position = position;
}

void ControlMsg::setInjectTouchMsgData(quint32 id, AndroidMotioneventAction action, QRect position)
{
m_data.injectTouch.action = action;
m_data.injectTouch.id = id;
m_data.injectTouch.action = action;
m_data.injectTouch.buttons = buttons;
m_data.injectTouch.position = position;
m_data.injectTouch.pressure = pressure;
}

void ControlMsg::setInjectScrollMsgData(QRect position, qint32 hScroll, qint32 vScroll)
Expand Down Expand Up @@ -85,12 +80,22 @@ void ControlMsg::setSetScreenPowerModeData(ControlMsg::ScreenPowerMode mode)

void ControlMsg::writePosition(QBuffer &buffer, const QRect& value)
{
BufferUtil::write16(buffer, value.left());
BufferUtil::write16(buffer, value.top());
BufferUtil::write32(buffer, value.left());
BufferUtil::write32(buffer, value.top());
BufferUtil::write16(buffer, value.width());
BufferUtil::write16(buffer, value.height());
}

quint16 ControlMsg::toFixedPoint16(float f)
{
assert(f >= 0.0f && f <= 1.0f);
quint32 u = f * 0x1p16f; // 2^16
if (u >= 0xffff) {
u = 0xffff;
}
return (quint16) u;
}

QByteArray ControlMsg::serializeData()
{
QByteArray byteArray;
Expand All @@ -108,15 +113,15 @@ QByteArray ControlMsg::serializeData()
BufferUtil::write16(buffer, strlen(m_data.injectText.text));
buffer.write(m_data.injectText.text, strlen(m_data.injectText.text));
break;
case CMT_INJECT_MOUSE:
buffer.putChar(m_data.injectMouse.action);
BufferUtil::write32(buffer, m_data.injectMouse.buttons);
writePosition(buffer, m_data.injectMouse.position);
break;
case CMT_INJECT_TOUCH:
buffer.putChar(m_data.injectTouch.id);
{
buffer.putChar(m_data.injectTouch.action);
BufferUtil::write64(buffer, m_data.injectTouch.id);
writePosition(buffer, m_data.injectTouch.position);
quint16 pressure = toFixedPoint16(m_data.injectTouch.pressure);
BufferUtil::write16(buffer, pressure);
BufferUtil::write32(buffer, m_data.injectTouch.buttons);
}
break;
case CMT_INJECT_SCROLL:
writePosition(buffer, m_data.injectScroll.position);
Expand Down
18 changes: 7 additions & 11 deletions QtScrcpy/device/controller/inputconvert/controlmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#define CONTROL_MSG_TEXT_MAX_LENGTH 300
#define CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH 4093
#define POINTER_ID_MOUSE UINT64_C(-1)
// ControlMsg
class ControlMsg : public QScrcpyEvent
{
Expand All @@ -19,16 +20,14 @@ class ControlMsg : public QScrcpyEvent
CMT_NULL = -1,
CMT_INJECT_KEYCODE = 0,
CMT_INJECT_TEXT,
CMT_INJECT_MOUSE,
CMT_INJECT_TOUCH,
CMT_INJECT_SCROLL,
CMT_BACK_OR_SCREEN_ON,
CMT_EXPAND_NOTIFICATION_PANEL,
CMT_COLLAPSE_NOTIFICATION_PANEL,
CMT_GET_CLIPBOARD,
CMT_SET_CLIPBOARD,
CMT_SET_SCREEN_POWER_MODE,

CMT_INJECT_TOUCH,
CMT_SET_SCREEN_POWER_MODE
};

enum ScreenPowerMode {
Expand All @@ -42,11 +41,10 @@ class ControlMsg : public QScrcpyEvent

void setInjectKeycodeMsgData(AndroidKeyeventAction action, AndroidKeycode keycode, AndroidMetastate metastate);
void setInjectTextMsgData(QString& text);
void setInjectMouseMsgData(AndroidMotioneventAction action, AndroidMotioneventButtons buttons, QRect position);
// id 代表一个触摸点,最多支持10个触摸点[0,9]
// action 只能是AMOTION_EVENT_ACTION_DOWN,AMOTION_EVENT_ACTION_UP,AMOTION_EVENT_ACTION_MOVE
// position action动作对应的位置
void setInjectTouchMsgData(quint32 id, AndroidMotioneventAction action, QRect position);
void setInjectTouchMsgData(quint64 id, AndroidMotioneventAction action, AndroidMotioneventButtons buttons, QRect position, float pressure);
void setInjectScrollMsgData(QRect position, qint32 hScroll, qint32 vScroll);
void setSetClipboardMsgData(QString& text);
void setSetScreenPowerModeData(ControlMsg::ScreenPowerMode mode);
Expand All @@ -55,6 +53,7 @@ class ControlMsg : public QScrcpyEvent

private:
void writePosition(QBuffer& buffer, const QRect& value);
quint16 toFixedPoint16(float f);

private:
struct ControlMsgData {
Expand All @@ -69,14 +68,11 @@ class ControlMsg : public QScrcpyEvent
char* text = Q_NULLPTR;
} injectText;
struct {
quint64 id;
AndroidMotioneventAction action;
AndroidMotioneventButtons buttons;
QRect position;
} injectMouse;
struct {
quint32 id;
AndroidMotioneventAction action;
QRect position;
float pressure;
} injectTouch;
struct {
QRect position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void InputConvertGame::sendTouchEvent(int id, QPointF pos, AndroidMotioneventAct
if (!controlMsg) {
return;
}
controlMsg->setInjectTouchMsgData(id, action, QRect(calcFrameAbsolutePos(pos).toPoint(), m_frameSize));
controlMsg->setInjectTouchMsgData(id, action, (AndroidMotioneventButtons)0, QRect(calcFrameAbsolutePos(pos).toPoint(), m_frameSize), 1.0f);
sendControlMsg(controlMsg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ void InputConvertNormal::mouseEvent(const QMouseEvent* from, const QSize& frameS
pos.setY(pos.y() * frameSize.height() / showSize.height());

// set data
ControlMsg* controlMsg = new ControlMsg(ControlMsg::CMT_INJECT_MOUSE);
ControlMsg* controlMsg = new ControlMsg(ControlMsg::CMT_INJECT_TOUCH);
if (!controlMsg) {
return;
}
controlMsg->setInjectMouseMsgData(action, convertMouseButtons(from->buttons()), QRect(pos.toPoint(), frameSize));
controlMsg->setInjectTouchMsgData(POINTER_ID_MOUSE, action, convertMouseButtons(from->buttons()), QRect(pos.toPoint(), frameSize), 1.0f);
sendControlMsg(controlMsg);
}

Expand Down
1 change: 1 addition & 0 deletions QtScrcpy/device/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ void Device::startServer()
params.localPort = m_params.localPort;
params.maxSize = m_params.maxSize;
params.bitRate = m_params.bitRate;
params.maxFps = m_params.maxFps;
params.crop = "-";
params.sendFrameMeta = m_recorder ? true : false;
params.control = true;
Expand Down
1 change: 1 addition & 0 deletions QtScrcpy/device/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Device : public QObject
quint16 localPort = 27183; // reverse时本地监听端口
quint16 maxSize = 720; // 视频分辨率
quint32 bitRate = 8000000; // 视频比特率
quint32 maxFps = 60; // 视频最大帧率
bool closeScreen = false; // 启动时自动息屏
bool useReverse = true; // true:先使用adb reverse,失败后自动使用adb forward;false:直接使用adb forward
bool display = true; // 是否显示画面(或者仅仅后台录制)
Expand Down
7 changes: 6 additions & 1 deletion QtScrcpy/device/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#define DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar"
#define DEVICE_NAME_FIELD_LENGTH 64
#define SOCKET_NAME "qtscrcpy"
#define SOCKET_NAME "scrcpy"
#define MAX_CONNECT_COUNT 30
#define MAX_RESTART_COUNT 1

Expand Down Expand Up @@ -129,8 +129,13 @@ bool Server::execute()
args << "app_process";
args << "/"; // unused;
args << "com.genymobile.scrcpy.Server";
// version
QStringList versionList = QCoreApplication::applicationVersion().split(".");
QString version = versionList[0] + "." + versionList[1] + "." + versionList[2];
args << version;
args << QString::number(m_params.maxSize);
args << QString::number(m_params.bitRate);
args << QString::number(m_params.maxFps);
args << (m_tunnelForward ? "true" : "false");
if (m_params.crop.isEmpty()) {
args << "-";
Expand Down
1 change: 1 addition & 0 deletions QtScrcpy/device/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Server : public QObject
quint16 localPort = 27183; // reverse时本地监听端口
quint16 maxSize = 720; // 视频分辨率
quint32 bitRate = 8000000; // 视频比特率
quint32 maxFps = 60; // 视频最大帧率
QString crop = "-"; // 视频裁剪
bool sendFrameMeta = false; // 是否发送mp4帧数据
bool control = true; // 安卓端是否接收键鼠控制
Expand Down
2 changes: 2 additions & 0 deletions QtScrcpy/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ void Dialog::on_startServerBtn_clicked()
params.serial = ui->serialBox->currentText().trimmed();
params.maxSize = videoSize;
params.bitRate = bitRate;
// on devices with Android >= 10, the capture frame rate can be limited
params.maxFps = 60;
params.recordFileName = absFilePath;
params.closeScreen = ui->closeScreenCheck->isChecked();
params.useReverse = ui->useReverseCheck->isChecked();
Expand Down
6 changes: 6 additions & 0 deletions QtScrcpy/util/bufferutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ void BufferUtil::write32(QBuffer &buffer, quint32 value)
buffer.putChar(value);
}

void BufferUtil::write64(QBuffer &buffer, quint64 value)
{
write32(buffer, value >> 32);
write32(buffer, (quint32) value);
}

void BufferUtil::write16(QBuffer &buffer, quint32 value)
{
buffer.putChar(value >> 8);
Expand Down
5 changes: 3 additions & 2 deletions QtScrcpy/util/bufferutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

class BufferUtil
{
public:
static void write32(QBuffer& buffer, quint32 value);
public:
static void write16(QBuffer& buffer, quint32 value);
static void write32(QBuffer& buffer, quint32 value);
static void write64(QBuffer& buffer, quint64 value);
static quint16 read16(QBuffer& buffer);
static quint32 read32(QBuffer& buffer);
static quint64 read64(QBuffer& buffer);
Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Mac OS平台,你可以直接使用我编译好的可执行程序:
### Android端 (没有修改需求的话直接使用自带的scrcpy-server.jar即可)
1. 目标平台上搭建Android开发环境
2. 使用Android Studio打开项目根目录中的server项目
3. 第一次打开如果你没有对应版本的gradle会提示找不到gradle,是否升级gradle并创建,选择取消,取消后会弹出gradle选择已有gradle的位置,同样取消即可(会自动下载)
3. 第一次打开如果你没有对应版本的gradle会提示找不到gradle,是否升级gradle并创建,选择取消,取消后会弹出选择已有gradle的位置,同样取消即可(会自动下载)
4. 按需编辑代码即可,当然也可以不编辑
4. 编译出apk以后改名为scrcpy-server.jar并替换third_party/scrcpy-server.jar即可

Expand Down
2 changes: 1 addition & 1 deletion server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 5
versionName "1.4"
versionName "1.12.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Loading

0 comments on commit a26620f

Please sign in to comment.