From 2acb11ac239a189d69fea8e7e79fab8a8022b2fe Mon Sep 17 00:00:00 2001 From: Barry <870709864@qq.com> Date: Sat, 9 Apr 2022 20:57:10 +0800 Subject: [PATCH] refactor: move config from server --- QtScrcpy/device/device.cpp | 2 ++ QtScrcpy/device/device.h | 2 ++ QtScrcpy/device/server/server.cpp | 14 +------------- QtScrcpy/device/server/server.h | 12 +++++------- QtScrcpy/ui/dialog.cpp | 17 ++++++++++++++++- QtScrcpy/ui/dialog.h | 1 + 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/QtScrcpy/device/device.cpp b/QtScrcpy/device/device.cpp index 697ec31..2f9694b 100644 --- a/QtScrcpy/device/device.cpp +++ b/QtScrcpy/device/device.cpp @@ -335,6 +335,8 @@ void Device::startServer() // only one devices, serial can be null // mark: crop input format: "width:height:x:y" or "" for no crop, for example: "100:200:0:0" Server::ServerParams params; + params.serverLocalPath = m_params.serverLocalPath; + params.serverRemotePath = m_params.serverRemotePath; params.serial = m_params.serial; params.localPort = m_params.localPort; params.maxSize = m_params.maxSize; diff --git a/QtScrcpy/device/device.h b/QtScrcpy/device/device.h index 063f518..01964c3 100644 --- a/QtScrcpy/device/device.h +++ b/QtScrcpy/device/device.h @@ -25,6 +25,8 @@ class Device : public QObject public: struct DeviceParams { + QString serverLocalPath = ""; // 本地安卓server路径 + QString serverRemotePath = ""; // 要推送到远端设备的server路径 QString recordFileName = ""; // 视频录制文件名 QString recordPath = ""; // 视频保存路径 QString serial = ""; // 设备序列号 diff --git a/QtScrcpy/device/server/server.cpp b/QtScrcpy/device/server/server.cpp index 3f50aac..cc69060 100644 --- a/QtScrcpy/device/server/server.cpp +++ b/QtScrcpy/device/server/server.cpp @@ -47,24 +47,12 @@ Server::Server(QObject *parent) : QObject(parent) Server::~Server() {} -const QString &Server::getServerPath() -{ - if (m_serverPath.isEmpty()) { - m_serverPath = QString::fromLocal8Bit(qgetenv("QTSCRCPY_SERVER_PATH")); - QFileInfo fileInfo(m_serverPath); - if (m_serverPath.isEmpty() || !fileInfo.isFile()) { - m_serverPath = QCoreApplication::applicationDirPath() + "/scrcpy-server"; - } - } - return m_serverPath; -} - bool Server::pushServer() { if (m_workProcess.isRuning()) { m_workProcess.kill(); } - m_workProcess.push(m_params.serial, getServerPath(), Config::getInstance().getServerPath()); + m_workProcess.push(m_params.serial, m_params.serverLocalPath, m_params.serverRemotePath); return true; } diff --git a/QtScrcpy/device/server/server.h b/QtScrcpy/device/server/server.h index a41867e..3f57741 100644 --- a/QtScrcpy/device/server/server.h +++ b/QtScrcpy/device/server/server.h @@ -26,12 +26,14 @@ class Server : public QObject public: struct ServerParams { + QString serverLocalPath = ""; // 本地安卓server路径 + QString serverRemotePath = ""; // 要推送到远端设备的server路径 QString serial = ""; // 设备序列号 quint16 localPort = 27183; // reverse时本地监听端口 quint16 maxSize = 720; // 视频分辨率 quint32 bitRate = 8000000; // 视频比特率 quint32 maxFps = 60; // 视频最大帧率 - QString crop = ""; // 视频裁剪 + QString crop = ""; // 视频裁剪 bool control = true; // 安卓端是否接收键鼠控制 bool useReverse = true; // true:先使用adb reverse,失败后自动使用adb forward;false:直接使用adb forward int lockVideoOrientation = -1; // 是否锁定视频方向 @@ -42,15 +44,12 @@ public: virtual ~Server(); bool start(Server::ServerParams params); - bool connectTo(); + void stop(); bool isReverse(); Server::ServerParams getParams(); - VideoSocket *getVideoSocket(); QTcpSocket *getControlSocket(); - void stop(); - signals: void serverStarted(bool success, const QString &deviceName = "", const QSize &size = QSize()); void serverStoped(); @@ -62,13 +61,13 @@ protected: void timerEvent(QTimerEvent *event); private: - const QString &getServerPath(); bool pushServer(); bool enableTunnelReverse(); bool disableTunnelReverse(); bool enableTunnelForward(); bool disableTunnelForward(); bool execute(); + bool connectTo(); bool startServerByStep(); bool readInfo(VideoSocket *videoSocket, QString &deviceName, QSize &size); void startAcceptTimeoutTimer(); @@ -78,7 +77,6 @@ private: void onConnectTimer(); private: - QString m_serverPath = ""; AdbProcess m_workProcess; AdbProcess m_serverProcess; TcpServer m_serverSocket; // only used if !tunnel_forward diff --git a/QtScrcpy/ui/dialog.cpp b/QtScrcpy/ui/dialog.cpp index 1651e1f..f3eb15c 100644 --- a/QtScrcpy/ui/dialog.cpp +++ b/QtScrcpy/ui/dialog.cpp @@ -292,6 +292,8 @@ void Dialog::on_startServerBtn_clicked() params.stayAwake = ui->stayAwakeCheck->isChecked(); params.framelessWindow = ui->framelessCheck->isChecked(); params.recordPath = ui->recordPathEdt->text().trimmed(); + params.serverLocalPath = getServerPath(); + params.serverRemotePath = Config::getInstance().getServerPath(); m_deviceManage.connectDevice(params); @@ -611,5 +613,18 @@ void Dialog::on_serialBox_currentIndexChanged(const QString &arg1) quint32 Dialog::getBitRate() { return ui->bitRateEdit->text().trimmed().toUInt() * - (ui->bitRateBox->currentText() == QString("Mbps") ? 1000000 : 1000); + (ui->bitRateBox->currentText() == QString("Mbps") ? 1000000 : 1000); +} + +const QString &Dialog::getServerPath() +{ + static QString serverPath; + if (serverPath.isEmpty()) { + serverPath = QString::fromLocal8Bit(qgetenv("QTSCRCPY_SERVER_PATH")); + QFileInfo fileInfo(serverPath); + if (serverPath.isEmpty() || !fileInfo.isFile()) { + serverPath = QCoreApplication::applicationDirPath() + "/scrcpy-server"; + } + } + return serverPath; } diff --git a/QtScrcpy/ui/dialog.h b/QtScrcpy/ui/dialog.h index 8803e3e..4ee39f2 100644 --- a/QtScrcpy/ui/dialog.h +++ b/QtScrcpy/ui/dialog.h @@ -65,6 +65,7 @@ private: void slotActivated(QSystemTrayIcon::ActivationReason reason); int findDeviceFromeSerialBox(bool wifi); quint32 getBitRate(); + const QString &getServerPath(); protected: void closeEvent(QCloseEvent *event);