解决decoder退出

This commit is contained in:
Barry 2018-10-15 00:02:57 +08:00
commit effb3387ea
4 changed files with 37 additions and 8 deletions

View file

@ -44,16 +44,37 @@ qint32 Decoder::recvData(quint8* buf, qint32 bufSize)
return 0; return 0;
} }
if (m_deviceSocket) { if (m_deviceSocket) {
while (m_deviceSocket->bytesAvailable() < bufSize) { while (!m_quit && m_deviceSocket->bytesAvailable() < bufSize) {
m_deviceSocket->waitForReadyRead(); if (!m_deviceSocket->waitForReadyRead(300)
&& QTcpSocket::SocketTimeoutError != m_deviceSocket->error()) {
break;
} }
qint64 a = m_deviceSocket->read((char*)buf, bufSize); if (QTcpSocket::SocketTimeoutError == m_deviceSocket->error()) {
qDebug() << "++++++++++recv data " << a; qDebug() << "QTcpSocket::SocketTimeoutError";
return a; }
}
qDebug() << "recv data " << bufSize;
return m_deviceSocket->read((char*)buf, bufSize);
} }
return 0; 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() void Decoder::run()
{ {
unsigned char *decoderBuffer = Q_NULLPTR; unsigned char *decoderBuffer = Q_NULLPTR;
@ -123,7 +144,7 @@ void Decoder::run()
packet.data = Q_NULLPTR; packet.data = Q_NULLPTR;
packet.size = 0; 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: // the new decoding/encoding API has been introduced by:
// <http://git.videolan.org/?p=ffmpeg.git;a=commitdiff;h=7fc329e2dd6226dfecaa4a1d7adf353bf2773726> // <http://git.videolan.org/?p=ffmpeg.git;a=commitdiff;h=7fc329e2dd6226dfecaa4a1d7adf353bf2773726>
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0) #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0)
@ -180,5 +201,10 @@ runQuit:
if (codecCtx) { if (codecCtx) {
avcodec_free_context(&codecCtx); avcodec_free_context(&codecCtx);
} }
if (m_deviceSocket) {
m_deviceSocket->disconnectFromHost();
delete m_deviceSocket;
}
//notify_stopped(); //notify_stopped();
} }

View file

@ -23,12 +23,15 @@ public:
void setDeviceSocket(QTcpSocket* deviceSocket); void setDeviceSocket(QTcpSocket* deviceSocket);
qint32 recvData(quint8* buf, qint32 bufSize); qint32 recvData(quint8* buf, qint32 bufSize);
bool startDecode();
void stopDecode();
protected: protected:
void run(); void run();
private: private:
QPointer<QTcpSocket> m_deviceSocket = Q_NULLPTR; QPointer<QTcpSocket> m_deviceSocket = Q_NULLPTR;
bool m_quit = false;
}; };
#endif // DECODER_H #endif // DECODER_H

View file

@ -20,7 +20,7 @@ Dialog::Dialog(QWidget *parent) :
connect(server, &Server::connectToResult, this, [this](bool success){ connect(server, &Server::connectToResult, this, [this](bool success){
if (success) { if (success) {
decoder.setDeviceSocket(server->getDeviceSocketByThread(&decoder)); decoder.setDeviceSocket(server->getDeviceSocketByThread(&decoder));
decoder.start(); decoder.startDecode();
} }
}); });
} }
@ -48,5 +48,6 @@ void Dialog::on_startServerBtn_clicked()
void Dialog::on_stopServerBtn_clicked() void Dialog::on_stopServerBtn_clicked()
{ {
decoder.stopDecode();
server->stop(); server->stop();
} }

View file

@ -253,7 +253,6 @@ void Server::stop()
m_serverCopiedToDevice = false; m_serverCopiedToDevice = false;
} }
m_serverSocket.close(); m_serverSocket.close();
qDebug() << "current thread"<< QThread::currentThread();
if (m_deviceSocket) { if (m_deviceSocket) {
m_deviceSocket->close(); m_deviceSocket->close();
m_deviceSocket->deleteLater(); m_deviceSocket->deleteLater();