diff --git a/src/decoder.cpp b/src/decoder.cpp index 8f485e1..152bb08 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -44,16 +44,37 @@ qint32 Decoder::recvData(quint8* buf, qint32 bufSize) return 0; } if (m_deviceSocket) { - while (m_deviceSocket->bytesAvailable() < bufSize) { - m_deviceSocket->waitForReadyRead(); + while (!m_quit && m_deviceSocket->bytesAvailable() < bufSize) { + if (!m_deviceSocket->waitForReadyRead(300) + && QTcpSocket::SocketTimeoutError != m_deviceSocket->error()) { + break; + } + if (QTcpSocket::SocketTimeoutError == m_deviceSocket->error()) { + qDebug() << "QTcpSocket::SocketTimeoutError"; + } } - qint64 a = m_deviceSocket->read((char*)buf, bufSize); - qDebug() << "++++++++++recv data " << a; - return a; + qDebug() << "recv data " << bufSize; + return m_deviceSocket->read((char*)buf, bufSize); } return 0; } +bool Decoder::startDecode() +{ + if (!m_deviceSocket) { + return false; + } + m_quit = false; + start(); + return true; +} + +void Decoder::stopDecode() +{ + m_quit = true; + wait(); +} + void Decoder::run() { unsigned char *decoderBuffer = Q_NULLPTR; @@ -123,7 +144,7 @@ void Decoder::run() packet.data = Q_NULLPTR; packet.size = 0; - while (!av_read_frame(formatCtx, &packet)) { + while (!m_quit && !av_read_frame(formatCtx, &packet)) { // the new decoding/encoding API has been introduced by: // #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0) @@ -180,5 +201,10 @@ runQuit: if (codecCtx) { avcodec_free_context(&codecCtx); } + + if (m_deviceSocket) { + m_deviceSocket->disconnectFromHost(); + delete m_deviceSocket; + } //notify_stopped(); } diff --git a/src/decoder.h b/src/decoder.h index bf660d6..ff56eba 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -23,12 +23,15 @@ public: void setDeviceSocket(QTcpSocket* deviceSocket); qint32 recvData(quint8* buf, qint32 bufSize); + bool startDecode(); + void stopDecode(); protected: void run(); private: QPointer m_deviceSocket = Q_NULLPTR; + bool m_quit = false; }; #endif // DECODER_H diff --git a/src/dialog.cpp b/src/dialog.cpp index bafab85..2ce2cf3 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -20,7 +20,7 @@ Dialog::Dialog(QWidget *parent) : connect(server, &Server::connectToResult, this, [this](bool success){ if (success) { decoder.setDeviceSocket(server->getDeviceSocketByThread(&decoder)); - decoder.start(); + decoder.startDecode(); } }); } @@ -48,5 +48,6 @@ void Dialog::on_startServerBtn_clicked() void Dialog::on_stopServerBtn_clicked() { + decoder.stopDecode(); server->stop(); } diff --git a/src/server.cpp b/src/server.cpp index e7a6fdf..120de3b 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -253,7 +253,6 @@ void Server::stop() m_serverCopiedToDevice = false; } m_serverSocket.close(); - qDebug() << "current thread"<< QThread::currentThread(); if (m_deviceSocket) { m_deviceSocket->close(); m_deviceSocket->deleteLater();