forked from willku1024/Qt-CamWatch-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideothread.cpp
74 lines (64 loc) · 1.57 KB
/
videothread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "videothread.h"
#include "videowindow.h"
#include <QDebug>
VideoThread::VideoThread(QString ip, int port, QObject *ptr)
:QThread(ptr)
{
this->ip = ip;
this->port = port;
}
void VideoThread::run()
{
QByteArray dataStream;
QByteArray frame;
QTcpSocket socket;
socket.connectToHost(this->ip,this->port);
if(socket.waitForConnected(3000))
{
qDebug()<<"conn success";
emit hasConnected(true);
}else
{
qDebug()<<"conn failed";
emit hasConnected(false);
return ;
}
QByteArray req;
req.append("?action=stream\n");
socket.write(req);
if(socket.waitForBytesWritten(3000))
{
qDebug()<<"write success";
}else
{
qDebug()<<"write failed";
return ;
}
while (!dataStream.contains("\r\n\r\n"))
{
if(socket.waitForReadyRead())
{
dataStream.append(socket.readAll());
}
}
dataStream.remove(0,dataStream.indexOf("\r\n\r\n")+4);
while (1) {
//帧头+图片
if(isPause)
{
msleep(618);//睡眠黄金比例1000*0.618ms
// socket.readAll();
continue ;
}
while (!dataStream.contains("\r\n--")) {
if(socket.waitForReadyRead())
{
dataStream.append(socket.readAll());
}
}
dataStream.remove(0,dataStream.indexOf("\n\n")+2);
frame = dataStream.mid(0,dataStream.indexOf("\r\n--"));
emit hasDataArrived(frame);
dataStream.remove(0,frame.size()+4);
}
}