This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideothread.cpp
177 lines (147 loc) · 4.43 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
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
#include "videothread.h"
#include <QDir>
#include <QDateTime>
#include <chrono>
#include <iostream>
VideoThread::VideoThread(QObject *parent):
QThread(parent),
m_stop(false),
m_enableWriter(false),
m_inputUrl("udp://127.0.0.1:30010"),
m_recdir(QDir::homePath()),
m_osdMenu(0)
{
av_log_set_level(AV_LOG_ERROR);
}
void VideoThread::run()
{
m_stop = false;
FfmpegVideoReader reader;
FfmpegVideoWriter writer;
emit stateChanged(QAbstractSocket::ConnectingState);
m_ioMutex.lock();
std::string url = m_inputUrl.toStdString();
m_ioMutex.unlock();
reader.open(url);
if(!reader.isOpen())
{
reader.close();
return;
}
emit stateChanged(QAbstractSocket::ConnectedState);
while(!m_stop)
{
//reader
SmartFrame frame = reader.getFrame();
if(frame)
{
SmartFrame frameRgb32(av_frame_alloc(), frameDeleter);
frameRgb32->width = frame->width;
frameRgb32->height = frame->height;
frameRgb32->format = AV_PIX_FMT_RGB32;
av_frame_get_buffer(frameRgb32.get(), 32);
checkWriterState(writer, frame->width, frame->height, reader.getFps());
SmartSwsContext swsContext(sws_getContext(frame->width, frame->height,
AVPixelFormat(frame->format),
frameRgb32->width, frameRgb32->height,
AVPixelFormat(frameRgb32->format),
SWS_FAST_BILINEAR, 0, 0, 0),
swsContextDeleter);
sws_scale(swsContext.get(), frame->data, frame->linesize, 0, frame->height,
frameRgb32->data, frameRgb32->linesize);
QImage rgb32Wrapper(frameRgb32->data[0], frameRgb32->width, frameRgb32->height,
QImage::Format_RGB32);
QImage rgb32Copy = rgb32Wrapper.copy();
if(writer.isOpen())
{
QImage osdCopy = rgb32Copy.copy();
if(m_osdMenu)
m_osdMenu->drawOsdMenu(&osdCopy);
SmartFrame temp(av_frame_alloc(), frameDeleter);
qimage2avframe(temp, osdCopy);
writer.write(temp);
}
emit frameReceived(rgb32Copy);
}
msleep(1);
}
if(reader.isOpen())
reader.close();
if(writer.isOpen())
writer.close();
emit stateChanged(QAbstractSocket::UnconnectedState);
}
void VideoThread::setOsdMenu(OsdMenu *osdMenu)
{
m_osdMenu = osdMenu;
}
QString VideoThread::getRecdir()
{
QMutexLocker locker(&m_ioMutex);
return m_recdir;
}
void VideoThread::setRecdir(const QString &recdir)
{
QDir dir;
if(dir.mkpath(recdir))
{
dir.cd(recdir);
QMutexLocker locker(&m_ioMutex);
m_recdir = dir.absolutePath();
}
else
{
QString message = "Can't create path %1, using home dir...";
std::cerr << message.arg(recdir).toStdString() << std::endl;
QMutexLocker locker(&m_ioMutex);
m_recdir = QDir::homePath();
}
}
QString VideoThread::inputUrl()
{
QMutexLocker locker(&m_ioMutex);
return m_inputUrl;
}
void VideoThread::setInputUrl(const QString &inputUrl)
{
QMutexLocker locker(&m_ioMutex);
m_inputUrl = inputUrl;
}
QImage VideoThread::getFrame()
{
QMutexLocker locker(&m_ioMutex);
return m_frame;
}
void VideoThread::enableWriter()
{
m_enableWriter = true;
}
void VideoThread::disableWriter()
{
m_enableWriter = false;
}
void VideoThread::stop()
{
m_stop = true;
}
void VideoThread::qimage2avframe(SmartFrame frame, const QImage &image)
{
frame->format = AV_PIX_FMT_RGB32;
frame->width = image.width();
frame->height = image.height();
av_frame_get_buffer(frame.get(), 32);
memcpy(frame->data[0], image.bits(), image.byteCount());
}
void VideoThread::checkWriterState(FfmpegVideoWriter &writer, int width, int height, double fps)
{
if(m_enableWriter && !writer.isOpen())
{
QString currentDateTime = QDateTime::currentDateTime().toString("dd_mm_yyyyThh_mm_ss_zzz");
QString filename = m_recdir + "/" + currentDateTime + ".avi";
writer.open(filename.toStdString(), AV_CODEC_ID_H264, width, height, fps);
}
if(!m_enableWriter && writer.isOpen())
{
writer.close();
}
}