mirror of
https://github.com/barry-ran/QtScrcpy.git
synced 2025-04-20 03:25:02 +00:00
refactor: devicemanage create&destroy device
This commit is contained in:
parent
2acb11ac23
commit
c480f30b88
5 changed files with 56 additions and 46 deletions
|
@ -18,7 +18,6 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params
|
|||
{
|
||||
if (!params.display && m_params.recordFileName.trimmed().isEmpty()) {
|
||||
qCritical("not display must be recorded");
|
||||
deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -51,40 +50,14 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params
|
|||
|
||||
m_server = new Server(this);
|
||||
if (!m_params.recordFileName.trimmed().isEmpty()) {
|
||||
m_recorder = new Recorder(m_params.recordFileName);
|
||||
m_recorder = new Recorder(m_params.recordFileName, this);
|
||||
}
|
||||
initSignals();
|
||||
startServer();
|
||||
}
|
||||
|
||||
Device::~Device()
|
||||
{
|
||||
if (m_server) {
|
||||
m_server->stop();
|
||||
}
|
||||
|
||||
if (m_stream) {
|
||||
m_stream->stopDecode();
|
||||
}
|
||||
|
||||
// server must stop before decoder, because decoder block main thread
|
||||
if (m_decoder) {
|
||||
m_decoder->close();
|
||||
}
|
||||
|
||||
if (m_recorder) {
|
||||
if (m_recorder->isRunning()) {
|
||||
m_recorder->stopRecorder();
|
||||
m_recorder->wait();
|
||||
}
|
||||
m_recorder->close();
|
||||
delete m_recorder;
|
||||
}
|
||||
if (m_videoForm) {
|
||||
m_videoForm->close();
|
||||
delete m_videoForm;
|
||||
}
|
||||
emit deviceDisconnect(m_params.serial);
|
||||
disconnectDevice();
|
||||
}
|
||||
|
||||
VideoForm *Device::getVideoForm()
|
||||
|
@ -179,11 +152,6 @@ void Device::initSignals()
|
|||
connect(this, &Device::postTextInput, m_controller, &Controller::onPostTextInput);
|
||||
}
|
||||
if (m_videoForm) {
|
||||
connect(m_videoForm, &VideoForm::destroyed, this, [this](QObject *obj) {
|
||||
Q_UNUSED(obj)
|
||||
deleteLater();
|
||||
});
|
||||
|
||||
connect(this, &Device::switchFullScreen, m_videoForm, &VideoForm::onSwitchFullScreen);
|
||||
}
|
||||
if (m_fileHandler) {
|
||||
|
@ -218,11 +186,12 @@ void Device::initSignals()
|
|||
|
||||
if (m_server) {
|
||||
connect(m_server, &Server::serverStarted, this, [this](bool success, const QString &deviceName, const QSize &size) {
|
||||
Q_UNUSED(deviceName);
|
||||
emit deviceConnected(success, m_params.serial, deviceName, size);
|
||||
if (success) {
|
||||
double diff = m_startTimeCount.elapsed() / 1000.0;
|
||||
qInfo() << QString("server start finish in %1s").arg(diff).toStdString().c_str();
|
||||
|
||||
|
||||
// update ui
|
||||
if (m_videoForm) {
|
||||
// must be show before updateShowSize
|
||||
|
@ -289,18 +258,18 @@ void Device::initSignals()
|
|||
emit m_controller->onSetScreenPowerMode(ControlMsg::SPM_OFF);
|
||||
}
|
||||
} else {
|
||||
deleteLater();
|
||||
disconnectDevice();
|
||||
}
|
||||
});
|
||||
connect(m_server, &Server::serverStoped, this, [this]() {
|
||||
deleteLater();
|
||||
disconnectDevice();
|
||||
qDebug() << "server process stop";
|
||||
});
|
||||
}
|
||||
|
||||
if (m_stream) {
|
||||
connect(m_stream, &Stream::onStreamStop, this, [this]() {
|
||||
deleteLater();
|
||||
disconnectDevice();
|
||||
qDebug() << "stream thread stop";
|
||||
});
|
||||
connect(m_stream, &Stream::getFrame, this, [this](AVPacket *packet) {
|
||||
|
@ -324,8 +293,12 @@ void Device::initSignals()
|
|||
}
|
||||
}
|
||||
|
||||
void Device::startServer()
|
||||
void Device::connectDevice()
|
||||
{
|
||||
if (!m_server) {
|
||||
return;
|
||||
}
|
||||
|
||||
// fix: macos cant recv finished signel, timer is ok
|
||||
QTimer::singleShot(0, this, [this]() {
|
||||
m_startTimeCount.start();
|
||||
|
@ -351,6 +324,38 @@ void Device::startServer()
|
|||
});
|
||||
}
|
||||
|
||||
void Device::disconnectDevice()
|
||||
{
|
||||
if (!m_server) {
|
||||
return;
|
||||
}
|
||||
m_server->stop();
|
||||
m_server = Q_NULLPTR;
|
||||
|
||||
if (m_stream) {
|
||||
m_stream->stopDecode();
|
||||
}
|
||||
|
||||
// server must stop before decoder, because decoder block main thread
|
||||
if (m_decoder) {
|
||||
m_decoder->close();
|
||||
}
|
||||
|
||||
if (m_recorder) {
|
||||
if (m_recorder->isRunning()) {
|
||||
m_recorder->stopRecorder();
|
||||
m_recorder->wait();
|
||||
}
|
||||
m_recorder->close();
|
||||
}
|
||||
if (m_videoForm) {
|
||||
m_videoForm->close();
|
||||
m_videoForm->deleteLater();
|
||||
}
|
||||
|
||||
emit deviceDisconnected(m_params.serial);
|
||||
}
|
||||
|
||||
void Device::onSetControlState(Device *device, Device::GroupControlState state)
|
||||
{
|
||||
Q_UNUSED(device)
|
||||
|
|
|
@ -52,6 +52,9 @@ public:
|
|||
explicit Device(DeviceParams params, QObject *parent = nullptr);
|
||||
virtual ~Device();
|
||||
|
||||
void connectDevice();
|
||||
void disconnectDevice();
|
||||
|
||||
VideoForm *getVideoForm();
|
||||
Server *getServer();
|
||||
const QString &getSerial();
|
||||
|
@ -63,7 +66,8 @@ public:
|
|||
bool isCurrentCustomKeymap();
|
||||
|
||||
signals:
|
||||
void deviceDisconnect(QString serial);
|
||||
void deviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size);
|
||||
void deviceDisconnected(QString serial);
|
||||
|
||||
// tool bar
|
||||
void switchFullScreen();
|
||||
|
@ -109,7 +113,6 @@ public slots:
|
|||
|
||||
private:
|
||||
void initSignals();
|
||||
void startServer();
|
||||
bool saveFrame(int width, int height, uint8_t* dataRGB32);
|
||||
|
||||
private:
|
||||
|
@ -119,7 +122,7 @@ private:
|
|||
QPointer<Controller> m_controller;
|
||||
QPointer<FileHandler> m_fileHandler;
|
||||
QPointer<Stream> m_stream;
|
||||
Recorder *m_recorder = Q_NULLPTR;
|
||||
QPointer<Recorder> m_recorder = Q_NULLPTR;
|
||||
|
||||
// ui
|
||||
QPointer<VideoForm> m_videoForm;
|
||||
|
|
|
@ -40,9 +40,10 @@ bool DeviceManage::connectDevice(Device::DeviceParams params)
|
|||
}
|
||||
*/
|
||||
Device *device = new Device(params);
|
||||
connect(device, &Device::deviceDisconnect, this, &DeviceManage::onDeviceDisconnect);
|
||||
connect(device, &Device::deviceDisconnected, this, &DeviceManage::onDeviceDisconnected);
|
||||
connect(device, &Device::controlStateChange, this, &DeviceManage::onControlStateChange);
|
||||
m_devices[params.serial] = device;
|
||||
device->connectDevice();
|
||||
if (!m_script.isEmpty()) {
|
||||
device->updateScript(m_script);
|
||||
}
|
||||
|
@ -186,12 +187,13 @@ void DeviceManage::setGroupControlHost(Device *host, bool install)
|
|||
}
|
||||
}
|
||||
|
||||
void DeviceManage::onDeviceDisconnect(QString serial)
|
||||
void DeviceManage::onDeviceDisconnected(QString serial)
|
||||
{
|
||||
if (!serial.isEmpty() && m_devices.contains(serial)) {
|
||||
if (m_devices[serial]->controlState() == Device::GroupControlState::GCS_HOST) {
|
||||
setGroupControlHost(nullptr, false);
|
||||
}
|
||||
m_devices[serial]->deleteLater();
|
||||
m_devices.remove(serial);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ protected:
|
|||
void setGroupControlHost(Device *host, bool install);
|
||||
|
||||
protected slots:
|
||||
void onDeviceDisconnect(QString serial);
|
||||
void onDeviceDisconnected(QString serial);
|
||||
void onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState);
|
||||
|
||||
// neend convert frameSize to its frameSize
|
||||
|
|
|
@ -48,7 +48,6 @@ VideoForm::~VideoForm()
|
|||
|
||||
void VideoForm::initUI()
|
||||
{
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
if (m_skin) {
|
||||
QPixmap phone;
|
||||
if (phone.load(":/res/phone.png")) {
|
||||
|
@ -720,6 +719,7 @@ void VideoForm::closeEvent(QCloseEvent *event)
|
|||
return;
|
||||
}
|
||||
Config::getInstance().setRect(m_device->getSerial(), geometry());
|
||||
m_device->disconnectDevice();
|
||||
}
|
||||
|
||||
void VideoForm::dragEnterEvent(QDragEnterEvent *event)
|
||||
|
|
Loading…
Add table
Reference in a new issue