Skip to content

Commit 2de5ce4

Browse files
committed
fix: drag ui delay recv video
1 parent 999cf58 commit 2de5ce4

File tree

7 files changed

+39
-102
lines changed

7 files changed

+39
-102
lines changed

src/device/device.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,7 @@ Device::Device(DeviceParams params, QObject *parent) : IDevice(parent), m_params
3636
}, params.gameScript, this);
3737
}
3838

39-
m_stream = new Stream([this](quint8 *buf, qint32 bufSize) -> qint32 {
40-
auto videoSocket = m_server->getVideoSocket();
41-
if (!videoSocket) {
42-
return 0;
43-
}
44-
45-
return videoSocket->subThreadRecvData(buf, bufSize);
46-
}, this);
39+
m_stream = new Stream(this);
4740

4841
m_server = new Server(this);
4942
if (m_params.recordFile && !m_params.recordPath.trimmed().isEmpty()) {
@@ -191,7 +184,8 @@ void Device::initSignals()
191184
m_decoder->open();
192185
}
193186

194-
// init decoder
187+
// init stream
188+
m_stream->installVideoSocket(m_server->removeVideoSocket());
195189
m_stream->startDecode();
196190

197191
// recv device msg

src/device/server/server.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,11 @@ void Server::timerEvent(QTimerEvent *event)
216216
}
217217
}
218218

219-
VideoSocket *Server::getVideoSocket()
219+
VideoSocket* Server::removeVideoSocket()
220220
{
221-
return m_videoSocket;
221+
VideoSocket* socket = m_videoSocket;
222+
m_videoSocket = Q_NULLPTR;
223+
return socket;
222224
}
223225

224226
QTcpSocket *Server::getControlSocket()
@@ -234,10 +236,6 @@ void Server::stop()
234236
stopAcceptTimeoutTimer();
235237
}
236238

237-
if (m_videoSocket) {
238-
m_videoSocket->close();
239-
m_videoSocket->deleteLater();
240-
}
241239
if (m_controlSocket) {
242240
m_controlSocket->close();
243241
m_controlSocket->deleteLater();

src/device/server/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Server : public QObject
6060
void stop();
6161
bool isReverse();
6262
Server::ServerParams getParams();
63-
VideoSocket *getVideoSocket();
63+
VideoSocket *removeVideoSocket();
6464
QTcpSocket *getControlSocket();
6565

6666
signals:

src/device/server/videosocket.cpp

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,30 @@
22
#include <QDebug>
33
#include <QThread>
44

5-
#include "qscrcpyevent.h"
65
#include "videosocket.h"
76

87
VideoSocket::VideoSocket(QObject *parent) : QTcpSocket(parent)
98
{
10-
connect(this, &VideoSocket::readyRead, this, &VideoSocket::onReadyRead);
11-
connect(this, &VideoSocket::aboutToClose, this, &VideoSocket::quitNotify);
12-
connect(this, &VideoSocket::disconnected, this, &VideoSocket::quitNotify);
139
}
1410

1511
VideoSocket::~VideoSocket()
1612
{
17-
quitNotify();
1813
}
1914

2015
qint32 VideoSocket::subThreadRecvData(quint8 *buf, qint32 bufSize)
2116
{
22-
// this function cant call in main thread
23-
Q_ASSERT(QCoreApplication::instance()->thread() != QThread::currentThread());
24-
if (m_quit) {
17+
if (!buf) {
2518
return 0;
2619
}
27-
QMutexLocker locker(&m_mutex);
28-
29-
m_buffer = buf;
30-
m_bufferSize = bufSize;
31-
m_dataSize = 0;
32-
33-
// post event
34-
VideoSocketEvent *getDataEvent = new VideoSocketEvent();
35-
QCoreApplication::postEvent(this, getDataEvent);
36-
37-
// wait
38-
while (!m_recvData) {
39-
m_recvDataCond.wait(&m_mutex);
40-
}
41-
42-
m_recvData = false;
43-
return m_dataSize;
44-
}
45-
46-
bool VideoSocket::event(QEvent *event)
47-
{
48-
if (static_cast<QScrcpyEvent::Type>(event->type()) == QScrcpyEvent::VideoSocket) {
49-
onReadyRead();
50-
return true;
51-
}
52-
return QTcpSocket::event(event);
53-
}
54-
55-
void VideoSocket::onReadyRead()
56-
{
57-
QMutexLocker locker(&m_mutex);
58-
if (m_buffer && m_bufferSize <= bytesAvailable()) {
59-
// recv data
60-
qint64 readSize = qMin(bytesAvailable(), (qint64)m_bufferSize);
61-
m_dataSize = read((char *)m_buffer, readSize);
20+
// this function cant call in main thread
21+
Q_ASSERT(QCoreApplication::instance()->thread() != QThread::currentThread());
6222

63-
m_buffer = Q_NULLPTR;
64-
m_bufferSize = 0;
65-
m_recvData = true;
66-
m_recvDataCond.wakeOne();
23+
while (bytesAvailable() < bufSize) {
24+
if (!waitForReadyRead(-1)) {
25+
return 0;
26+
}
6727
}
68-
}
6928

70-
void VideoSocket::quitNotify()
71-
{
72-
m_quit = true;
73-
QMutexLocker locker(&m_mutex);
74-
if (m_buffer) {
75-
m_buffer = Q_NULLPTR;
76-
m_bufferSize = 0;
77-
m_recvData = true;
78-
m_dataSize = 0;
79-
m_recvDataCond.wakeOne();
80-
}
29+
// recv data
30+
return read((char *)buf, bufSize);
8131
}

src/device/server/videosocket.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#ifndef VIDEOSOCKET_H
22
#define VIDEOSOCKET_H
33

4-
#include <QEvent>
5-
#include <QMutex>
64
#include <QTcpSocket>
7-
#include <QWaitCondition>
85

96
class VideoSocket : public QTcpSocket
107
{
@@ -14,22 +11,6 @@ class VideoSocket : public QTcpSocket
1411
virtual ~VideoSocket();
1512

1613
qint32 subThreadRecvData(quint8 *buf, qint32 bufSize);
17-
18-
protected:
19-
bool event(QEvent *event);
20-
21-
protected slots:
22-
void onReadyRead();
23-
void quitNotify();
24-
25-
private:
26-
QMutex m_mutex;
27-
QWaitCondition m_recvDataCond;
28-
bool m_recvData = false;
29-
quint8 *m_buffer = Q_NULLPTR;
30-
qint32 m_bufferSize = 0;
31-
qint32 m_dataSize = 0;
32-
bool m_quit = false;
3314
};
3415

3516
#endif // VIDEOSOCKET_H

src/device/stream/stream.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@
33

44
#include "compat.h"
55
#include "stream.h"
6+
#include "videosocket.h"
67

78
#define BUFSIZE 0x10000
89
#define HEADER_SIZE 12
910
#define NO_PTS UINT64_MAX
1011

1112
typedef qint32 (*ReadPacketFunc)(void *, quint8 *, qint32);
1213

13-
Stream::Stream(std::function<qint32(quint8*, qint32)> recvData, QObject *parent)
14+
Stream::Stream(QObject *parent)
1415
: QThread(parent)
15-
, m_recvData(recvData)
1616
{}
1717

18-
Stream::~Stream() {}
18+
Stream::~Stream() {
19+
if (m_videoSocket) {
20+
m_videoSocket->close();
21+
delete m_videoSocket;
22+
m_videoSocket = Q_NULLPTR;
23+
}
24+
}
1925

2026
static void avLogCallback(void *avcl, int level, const char *fmt, va_list vl)
2127
{
@@ -64,6 +70,12 @@ void Stream::deInit()
6470
avformat_network_deinit(); // ignore failure
6571
}
6672

73+
void Stream::installVideoSocket(VideoSocket *videoSocket)
74+
{
75+
videoSocket->moveToThread(this);
76+
m_videoSocket = videoSocket;
77+
}
78+
6779
static quint32 bufferRead32be(quint8 *buf)
6880
{
6981
return static_cast<quint32>((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
@@ -78,17 +90,17 @@ static quint64 bufferRead64be(quint8 *buf)
7890

7991
qint32 Stream::recvData(quint8 *buf, qint32 bufSize)
8092
{
81-
if (!buf || !m_recvData) {
93+
if (!buf || !m_videoSocket) {
8294
return 0;
8395
}
8496

85-
qint32 len = m_recvData(buf, bufSize);
97+
qint32 len = m_videoSocket->subThreadRecvData(buf, bufSize);
8698
return len;
8799
}
88100

89101
bool Stream::startDecode()
90102
{
91-
if (!m_recvData) {
103+
if (!m_videoSocket) {
92104
return false;
93105
}
94106
start();

src/device/stream/stream.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ extern "C"
1010
#include "libavformat/avformat.h"
1111
}
1212

13+
class VideoSocket;
1314
class Stream : public QThread
1415
{
1516
Q_OBJECT
1617
public:
17-
Stream(std::function<qint32(quint8*, qint32)> recvData, QObject *parent = Q_NULLPTR);
18+
Stream(QObject *parent = Q_NULLPTR);
1819
virtual ~Stream();
1920

2021
public:
2122
static bool init();
2223
static void deInit();
2324

25+
void installVideoSocket(VideoSocket* videoSocket);
2426
bool startDecode();
2527
void stopDecode();
2628

@@ -39,7 +41,7 @@ class Stream : public QThread
3941
qint32 recvData(quint8 *buf, qint32 bufSize);
4042

4143
private:
42-
std::function<qint32(quint8*, qint32)> m_recvData = nullptr;
44+
QPointer<VideoSocket> m_videoSocket = Q_NULLPTR;
4345

4446
AVCodecContext *m_codecCtx = Q_NULLPTR;
4547
AVCodecParserContext *m_parser = Q_NULLPTR;

0 commit comments

Comments
 (0)