refactor: stream and socket decoupling

This commit is contained in:
Barry 2022-03-30 19:21:50 +08:00
parent e0d879b11c
commit dbbe5297b0
3 changed files with 20 additions and 19 deletions

View file

@ -37,7 +37,14 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params
m_videoForm->setDevice(this);
}
m_stream = new Stream(this);
m_stream = new Stream([this](quint8 *buf, qint32 bufSize) -> qint32 {
auto videoSocket = m_server->getVideoSocket();
if (!videoSocket) {
return 0;
}
return videoSocket->subThreadRecvData(buf, bufSize);
}, this);
if (m_decoder) {
m_stream->setDecoder(m_decoder);
}
@ -245,7 +252,6 @@ void Device::initSignals()
}
// init decoder
m_stream->setVideoSocket(m_server->getVideoSocket());
m_stream->startDecode();
// init controller

View file

@ -13,7 +13,10 @@
typedef qint32 (*ReadPacketFunc)(void *, quint8 *, qint32);
Stream::Stream(QObject *parent) : QThread(parent) {}
Stream::Stream(std::function<qint32(quint8*, qint32)> recvData, QObject *parent)
: QThread(parent)
, m_recvData(recvData)
{}
Stream::~Stream() {}
@ -81,11 +84,6 @@ static quint64 bufferRead64be(quint8 *buf)
return (static_cast<quint64>(msb) << 32) | lsb;
}
void Stream::setVideoSocket(VideoSocket *videoSocket)
{
m_videoSocket = videoSocket;
}
void Stream::setRecoder(Recorder *recorder)
{
m_recorder = recorder;
@ -93,19 +91,17 @@ void Stream::setRecoder(Recorder *recorder)
qint32 Stream::recvData(quint8 *buf, qint32 bufSize)
{
if (!buf) {
if (!buf || !m_recvData) {
return 0;
}
if (m_videoSocket) {
qint32 len = m_videoSocket->subThreadRecvData(buf, bufSize);
return len;
}
return 0;
qint32 len = m_recvData(buf, bufSize);
return len;
}
bool Stream::startDecode()
{
if (!m_videoSocket) {
if (!m_recvData) {
return false;
}
start();

View file

@ -17,7 +17,7 @@ class Stream : public QThread
{
Q_OBJECT
public:
Stream(QObject *parent = Q_NULLPTR);
Stream(std::function<qint32(quint8*, qint32)> recvData, QObject *parent = Q_NULLPTR);
virtual ~Stream();
public:
@ -26,8 +26,6 @@ public:
void setDecoder(Decoder *decoder);
void setRecoder(Recorder *recorder);
void setVideoSocket(VideoSocket *deviceSocket);
qint32 recvData(quint8 *buf, qint32 bufSize);
bool startDecode();
void stopDecode();
@ -41,9 +39,10 @@ protected:
bool processConfigPacket(AVPacket *packet);
bool parse(AVPacket *packet);
bool processFrame(AVPacket *packet);
qint32 recvData(quint8 *buf, qint32 bufSize);
private:
QPointer<VideoSocket> m_videoSocket;
std::function<qint32(quint8*, qint32)> m_recvData = nullptr;
// for recorder
Recorder *m_recorder = Q_NULLPTR;
Decoder *m_decoder = Q_NULLPTR;