-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtspcapturer.cpp
180 lines (157 loc) · 5.49 KB
/
rtspcapturer.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
#include "rtspcapturer.h"
#include <QDebug>
#include <QDateTime>
#ifdef Q_OS_WIN32
#else
#include <sys/time.h>
#include <unistd.h>
#endif
#include "global.h"
RtspCapturer::RtspCapturer(QString url)
: m_videoStream(-1)
, m_rtspURL(url)
, m_pFormatContext(nullptr)
, m_pCodec(nullptr)
, m_pCodecContext(nullptr)
, m_pSwsContext(nullptr)
, m_pPacket(nullptr)
, m_pFrame(nullptr)
, m_bWhiteBalance(false)
{
}
RtspCapturer::~RtspCapturer()
{
requestInterruption();
av_free(m_pPacket);
av_free(m_pFrame);
if(m_pCodecContext)
avcodec_close(m_pCodecContext);
if(m_pSwsContext)
sws_freeContext(m_pSwsContext);
quit();
wait();
}
void RtspCapturer::enableWhiteBalance(bool bEnable)
{
m_bWhiteBalance = bEnable;
}
void RtspCapturer::run()
{
if(!init(m_rtspURL))
return;
int nFrame = 0;
AVFrame *pFrameRGB = av_frame_alloc();
int numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, m_pCodecContext->width, m_pCodecContext->height);
uint8_t* pBuffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *)pFrameRGB, pBuffer, AV_PIX_FMT_RGB24,
m_pCodecContext->width, m_pCodecContext->height);
while (!isInterruptionRequested())
{
if (av_read_frame(m_pFormatContext, m_pPacket) < 0)
{
break; //这里认为视频读取完了
}
if (m_pPacket->stream_index == m_videoStream)
{
int got_picture;
int ret = avcodec_decode_video2(m_pCodecContext, m_pFrame, &got_picture, m_pPacket);
if (ret < 0)
{
// qDebug() << "decode error.";
continue;
}
if (got_picture)
{
sws_scale(m_pSwsContext, (uint8_t const * const *)m_pFrame->data,
m_pFrame->linesize, 0, m_pCodecContext->height,
pFrameRGB->data, pFrameRGB->linesize);
if(m_bWhiteBalance)
{
Mat img(m_pCodecContext->height, m_pCodecContext->width, CV_8UC3, pBuffer);
emit getFrame(mat2qimage(autoWhiteBalance(img)));
}
else
{
QImage tmpImg((uchar *)pBuffer, m_pCodecContext->width, m_pCodecContext->height, QImage::Format_RGB888);
emit getFrame(tmpImg); //发送信号
// qDebug() << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss") << ". get a frame";
}
}
}
msleep(5);
}
av_free(pBuffer);
av_free(pFrameRGB);
avcodec_close(m_pCodecContext);
avformat_close_input(&m_pFormatContext);
}
bool RtspCapturer::init(QString url)
{
//Allocate an AVFormatContext.
avformat_network_init();
av_register_all();
m_pFormatContext = avformat_alloc_context();
AVDictionary *avdic = nullptr;
char option_key[] = "rtsp_transport";
char option_value[] = "tcp";
av_dict_set(&avdic, option_key, option_value, 0);
char option_key2[] = "max_delay";
char option_value2[] = "100";
av_dict_set(&avdic, option_key2, option_value2, 0);
if (avformat_open_input(&m_pFormatContext, url.toStdString().c_str(), nullptr, &avdic) != 0)
{
qDebug() << "Can't open the RTSP stream " << url;
emit sendMessage(tr("Can't open the RTSP stream"));
return false;
}
if (avformat_find_stream_info(m_pFormatContext, nullptr) < 0)
{
qDebug() << "Could't find stream infomation.";
emit sendMessage(tr("Can't find stream infomation."));
return false;
}
for (int i = 0; i < m_pFormatContext->nb_streams; i++)
{
if (m_pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
m_videoStream = i;
}
}
//如果videoStream为-1 说明没有找到视频流
if (-1 == m_videoStream)
{
qDebug() << "Didn't find a video stream.";
emit sendMessage(tr("Didn't find a video stream."));
return false;
}
//查找解码器
m_pCodecContext = m_pFormatContext->streams[m_videoStream]->codec;
m_pCodecContext->bit_rate = 0; //初始化为0
m_pCodecContext->time_base.num = 1; //下面两行:一秒钟25帧
m_pCodecContext->time_base.den = 10;
m_pCodecContext->frame_number = 1; //每包一个视频帧
m_pCodec = avcodec_find_decoder(m_pCodecContext->codec_id);
if (nullptr == m_pCodec)
{
qDebug() << "Codec not found.";
emit sendMessage(tr("Codec not found."));
return false;
}
//打开解码器
if (avcodec_open2(m_pCodecContext, m_pCodec, nullptr) < 0)
{
qDebug() << "Could not open codec.";
emit sendMessage(tr("Could not open codec."));
return false;
}
int y_size = m_pCodecContext->width * m_pCodecContext->height;
m_pPacket = (AVPacket *)malloc(sizeof(AVPacket)); //分配一个packet
av_new_packet(m_pPacket, y_size); //分配packet的数据
// av_dump_format(m_pFormatContext, 0, url.toStdString().c_str(), 0);
m_pFrame = av_frame_alloc();
m_pSwsContext = sws_getContext(m_pCodecContext->width, m_pCodecContext->height,
m_pCodecContext->pix_fmt, m_pCodecContext->width,
m_pCodecContext->height, AV_PIX_FMT_RGB24,
SWS_BICUBIC, nullptr, nullptr, nullptr);
return true;
}