-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
213 lines (172 loc) · 5.59 KB
/
mainwindow.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QByteArray>
#include <QGraphicsPixmapItem>
#include <QDebug>
#include <arpa/inet.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
tcpSocket(new QTcpSocket(this)),
scene(new QGraphicsScene(this)),
reconnectTimer(new QTimer(this))
{
ui->setupUi(this);
ui->graphicsView->setScene(scene);
connect(ui->actionConnect, SIGNAL(triggered(bool)), this, SLOT(doConnect(bool)));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(this, SIGNAL(frameAvailable(QByteArray)), this, SLOT(decodeFrame(QByteArray)));
connect(this, SIGNAL(imageAvailable(QImage)), this, SLOT(showImage(QImage)));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
connect(reconnectTimer, SIGNAL(timeout()), this, SLOT(reconnect()));
avcodec_register_all();
codec = avcodec_find_decoder ( AV_CODEC_ID_H264 );
codecCtx = avcodec_alloc_context3 ( codec );
avcodec_open2 ( codecCtx, codec, NULL );
static const char initData[] = {
0x1
};
init = QByteArray::fromRawData(initData, sizeof(initData));
}
MainWindow::~MainWindow()
{
reconnectTimer->stop();
tcpSocket->close();
if (item != Q_NULLPTR)
{
scene->removeItem(item);
delete item;
}
delete ui;
}
void MainWindow::socketError(QAbstractSocket::SocketError socketError)
{
qDebug() << "Error with the socket:" << tcpSocket->errorString();
}
void MainWindow::disconnected()
{
qDebug() << "Disconnected, trying to reconnect.";
tcpSocket->close();
}
void MainWindow::doConnect(bool triggered)
{
qDebug() << "Connecting ...";
tcpSocket->connectToHost("192.168.168.1", 9876, QTcpSocket::ReadWrite);
// tcpSocket->connectToHost("10.98.32.1", 9876, QTcpSocket::ReadWrite);
if (!tcpSocket->waitForConnected())
{
qDebug() << "Connection timeout";
return;
}
qDebug() << "Connected.";
payload = 0;
remainingToRead = 0;
tcpSocket->write(init);
if (!tcpSocket->waitForBytesWritten())
{
qDebug() << "Unable to write initial bytes";
}
qDebug() << "Initial bytes written";
reconnectTimer->setInterval(100);
reconnectTimer->start();
}
void MainWindow::reconnect()
{
if (!tcpSocket->isOpen())
{
doConnect(false);
}
}
void MainWindow::readyRead()
{
// qDebug() << "Ready read:" << tcpSocket->bytesAvailable();
if (remainingToRead == 0) {
QByteArray payloadBytes = tcpSocket->read(4);
payload = ntohl(* (quint32*) payloadBytes.constData());
// qDebug() << "Payload size:" << payload;
remainingToRead = payload;
currentFrame = QByteArray();
}
const QByteArray frame = tcpSocket->read(tcpSocket->bytesAvailable());
currentFrame += frame;
remainingToRead -= frame.size();
// qDebug() << "Data readen:" << frame.size();
// qDebug() << "remainingToRead:" << remainingToRead;
if (remainingToRead <= 0) {
emit frameAvailable(currentFrame);
if (tcpSocket->isWritable())
{
tcpSocket->write(init);
}
}
}
void MainWindow::showImage(QImage img)
{
if (item != Q_NULLPTR)
{
scene->removeItem(item);
delete item;
item = Q_NULLPTR;
}
item = new QGraphicsPixmapItem(QPixmap::fromImage(img));
scene->addItem(item);
ui->graphicsView->show();
}
void MainWindow::decodeFrame(QByteArray frame)
{
AVFrame *avFrame = nullptr;
AVFrame *avFrameRGB = nullptr;
AVPacket packet;
int width = 1024;
int height = 768;
QImage img( width, height, QImage::Format_RGB888 );
// Set avpkt data and size here
avFrame = av_frame_alloc();
if(!avFrame)
{
qDebug() << "Could not allocate video frame, stop thread.";
return;
}
av_init_packet(&packet);
av_new_packet(&packet, frame.size());
memcpy(packet.data, frame.constData(), frame.size());
err = avcodec_decode_video2 ( codecCtx, avFrame, &frame_decoded, &packet );
if (err < 0)
{
qDebug() << "Unable to decode h264";
}
if (err == 0)
{
qDebug() << "No frame could be decompressed";
}
if (err > 0 && frame_decoded)
{
//qDebug() << "Frame width/height:" << codecCtx->width << codecCtx->height;
avFrameRGB = av_frame_alloc();
if(!avFrameRGB)
{
qDebug() << "Could not allocate video frame, stop thread.";
return;
}
//Allocate memory for the pixels of a picture and setup the AVPicture fields for it.
avpicture_alloc( ( AVPicture *) avFrameRGB, AV_PIX_FMT_RGB24, width, height);
if (imgConvertCtx == nullptr) {
qDebug() << "Image size:" << codecCtx->width << codecCtx->height;
imgConvertCtx = sws_getContext( codecCtx->width, codecCtx->height, codecCtx->pix_fmt, width, height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
}
sws_scale(imgConvertCtx, avFrame->data, avFrame->linesize, 0, codecCtx->height, avFrameRGB->data, avFrameRGB->linesize);
//setting QImage from frameRGB
for( int y = 0; y < height; ++y )
{
memcpy( img.scanLine(y), avFrameRGB->data[0]+y * avFrameRGB->linesize[0], avFrameRGB->linesize[0] );
}
if (!img.isNull())
{
emit imageAvailable(img);
}
av_frame_unref(avFrameRGB);
}
av_frame_unref(avFrame);
av_free_packet(&packet);
}