refactor: devicemanage create&destroy device

This commit is contained in:
Barry 2022-04-10 10:45:31 +08:00
commit c480f30b88
5 changed files with 56 additions and 46 deletions

View file

@ -18,7 +18,6 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params
{ {
if (!params.display && m_params.recordFileName.trimmed().isEmpty()) { if (!params.display && m_params.recordFileName.trimmed().isEmpty()) {
qCritical("not display must be recorded"); qCritical("not display must be recorded");
deleteLater();
return; return;
} }
@ -51,40 +50,14 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params
m_server = new Server(this); m_server = new Server(this);
if (!m_params.recordFileName.trimmed().isEmpty()) { if (!m_params.recordFileName.trimmed().isEmpty()) {
m_recorder = new Recorder(m_params.recordFileName); m_recorder = new Recorder(m_params.recordFileName, this);
} }
initSignals(); initSignals();
startServer();
} }
Device::~Device() Device::~Device()
{ {
if (m_server) { disconnectDevice();
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);
} }
VideoForm *Device::getVideoForm() VideoForm *Device::getVideoForm()
@ -179,11 +152,6 @@ void Device::initSignals()
connect(this, &Device::postTextInput, m_controller, &Controller::onPostTextInput); connect(this, &Device::postTextInput, m_controller, &Controller::onPostTextInput);
} }
if (m_videoForm) { if (m_videoForm) {
connect(m_videoForm, &VideoForm::destroyed, this, [this](QObject *obj) {
Q_UNUSED(obj)
deleteLater();
});
connect(this, &Device::switchFullScreen, m_videoForm, &VideoForm::onSwitchFullScreen); connect(this, &Device::switchFullScreen, m_videoForm, &VideoForm::onSwitchFullScreen);
} }
if (m_fileHandler) { if (m_fileHandler) {
@ -218,11 +186,12 @@ void Device::initSignals()
if (m_server) { if (m_server) {
connect(m_server, &Server::serverStarted, this, [this](bool success, const QString &deviceName, const QSize &size) { 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) { if (success) {
double diff = m_startTimeCount.elapsed() / 1000.0; double diff = m_startTimeCount.elapsed() / 1000.0;
qInfo() << QString("server start finish in %1s").arg(diff).toStdString().c_str(); qInfo() << QString("server start finish in %1s").arg(diff).toStdString().c_str();
// update ui // update ui
if (m_videoForm) { if (m_videoForm) {
// must be show before updateShowSize // must be show before updateShowSize
@ -289,18 +258,18 @@ void Device::initSignals()
emit m_controller->onSetScreenPowerMode(ControlMsg::SPM_OFF); emit m_controller->onSetScreenPowerMode(ControlMsg::SPM_OFF);
} }
} else { } else {
deleteLater(); disconnectDevice();
} }
}); });
connect(m_server, &Server::serverStoped, this, [this]() { connect(m_server, &Server::serverStoped, this, [this]() {
deleteLater(); disconnectDevice();
qDebug() << "server process stop"; qDebug() << "server process stop";
}); });
} }
if (m_stream) { if (m_stream) {
connect(m_stream, &Stream::onStreamStop, this, [this]() { connect(m_stream, &Stream::onStreamStop, this, [this]() {
deleteLater(); disconnectDevice();
qDebug() << "stream thread stop"; qDebug() << "stream thread stop";
}); });
connect(m_stream, &Stream::getFrame, this, [this](AVPacket *packet) { 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 // fix: macos cant recv finished signel, timer is ok
QTimer::singleShot(0, this, [this]() { QTimer::singleShot(0, this, [this]() {
m_startTimeCount.start(); 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) void Device::onSetControlState(Device *device, Device::GroupControlState state)
{ {
Q_UNUSED(device) Q_UNUSED(device)

View file

@ -52,6 +52,9 @@ public:
explicit Device(DeviceParams params, QObject *parent = nullptr); explicit Device(DeviceParams params, QObject *parent = nullptr);
virtual ~Device(); virtual ~Device();
void connectDevice();
void disconnectDevice();
VideoForm *getVideoForm(); VideoForm *getVideoForm();
Server *getServer(); Server *getServer();
const QString &getSerial(); const QString &getSerial();
@ -63,7 +66,8 @@ public:
bool isCurrentCustomKeymap(); bool isCurrentCustomKeymap();
signals: signals:
void deviceDisconnect(QString serial); void deviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size);
void deviceDisconnected(QString serial);
// tool bar // tool bar
void switchFullScreen(); void switchFullScreen();
@ -109,7 +113,6 @@ public slots:
private: private:
void initSignals(); void initSignals();
void startServer();
bool saveFrame(int width, int height, uint8_t* dataRGB32); bool saveFrame(int width, int height, uint8_t* dataRGB32);
private: private:
@ -119,7 +122,7 @@ private:
QPointer<Controller> m_controller; QPointer<Controller> m_controller;
QPointer<FileHandler> m_fileHandler; QPointer<FileHandler> m_fileHandler;
QPointer<Stream> m_stream; QPointer<Stream> m_stream;
Recorder *m_recorder = Q_NULLPTR; QPointer<Recorder> m_recorder = Q_NULLPTR;
// ui // ui
QPointer<VideoForm> m_videoForm; QPointer<VideoForm> m_videoForm;

View file

@ -40,9 +40,10 @@ bool DeviceManage::connectDevice(Device::DeviceParams params)
} }
*/ */
Device *device = new Device(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); connect(device, &Device::controlStateChange, this, &DeviceManage::onControlStateChange);
m_devices[params.serial] = device; m_devices[params.serial] = device;
device->connectDevice();
if (!m_script.isEmpty()) { if (!m_script.isEmpty()) {
device->updateScript(m_script); 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 (!serial.isEmpty() && m_devices.contains(serial)) {
if (m_devices[serial]->controlState() == Device::GroupControlState::GCS_HOST) { if (m_devices[serial]->controlState() == Device::GroupControlState::GCS_HOST) {
setGroupControlHost(nullptr, false); setGroupControlHost(nullptr, false);
} }
m_devices[serial]->deleteLater();
m_devices.remove(serial); m_devices.remove(serial);
} }
} }

View file

@ -26,7 +26,7 @@ protected:
void setGroupControlHost(Device *host, bool install); void setGroupControlHost(Device *host, bool install);
protected slots: protected slots:
void onDeviceDisconnect(QString serial); void onDeviceDisconnected(QString serial);
void onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState); void onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState);
// neend convert frameSize to its frameSize // neend convert frameSize to its frameSize

View file

@ -48,7 +48,6 @@ VideoForm::~VideoForm()
void VideoForm::initUI() void VideoForm::initUI()
{ {
setAttribute(Qt::WA_DeleteOnClose);
if (m_skin) { if (m_skin) {
QPixmap phone; QPixmap phone;
if (phone.load(":/res/phone.png")) { if (phone.load(":/res/phone.png")) {
@ -720,6 +719,7 @@ void VideoForm::closeEvent(QCloseEvent *event)
return; return;
} }
Config::getInstance().setRect(m_device->getSerial(), geometry()); Config::getInstance().setRect(m_device->getSerial(), geometry());
m_device->disconnectDevice();
} }
void VideoForm::dragEnterEvent(QDragEnterEvent *event) void VideoForm::dragEnterEvent(QDragEnterEvent *event)