/
rtspvideocapturer.cpp
executable file
·325 lines (283 loc) · 10.4 KB
/
rtspvideocapturer.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* ---------------------------------------------------------------------------
** This software is in the public domain, furnished "as is", without technical
** support, and with no warranty, express or implied, as to its usefulness for
** any purpose.
**
** rtspvideocapturer.cpp
**
** -------------------------------------------------------------------------*/
#ifdef HAVE_LIVE555
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#endif
#include <chrono>
#include "rtc_base/timeutils.h"
#include "rtc_base/logging.h"
#include "common_video/h264/h264_common.h"
#include "common_video/h264/sps_parser.h"
#include "modules/video_coding/h264_sprop_parameter_sets.h"
#include "api/video/i420_buffer.h"
#include "libyuv/video_common.h"
#include "libyuv/convert.h"
#include "rtspvideocapturer.h"
uint8_t marker[] = { 0, 0, 0, 1};
RTSPVideoCapturer::RTSPVideoCapturer(const std::string & uri, const std::map<std::string,std::string> & opts)
: m_connection(m_env, this, uri.c_str(), RTSPConnection::decodeTimeoutOption(opts), RTSPConnection::decodeRTPTransport(opts), rtc::LogMessage::GetLogToDebug()<=3),
m_width(0), m_height(0), m_fps(0)
{
RTC_LOG(INFO) << "RTSPVideoCapturer " << uri ;
if (opts.find("width") != opts.end()) {
m_width = std::stoi(opts.at("width"));
}
if (opts.find("height") != opts.end()) {
m_height = std::stoi(opts.at("height"));
}
if (opts.find("fps") != opts.end()) {
m_fps = std::stoi(opts.at("fps"));
}
}
RTSPVideoCapturer::~RTSPVideoCapturer()
{
}
bool RTSPVideoCapturer::onNewSession(const char* id,const char* media, const char* codec, const char* sdp)
{
bool success = false;
if (strcmp(media, "video") == 0) {
RTC_LOG(INFO) << "RTSPVideoCapturer::onNewSession " << media << "/" << codec << " " << sdp;
if (strcmp(codec, "H264") == 0)
{
m_codec = codec;
const char* pattern="sprop-parameter-sets=";
const char* sprop=strstr(sdp, pattern);
if (sprop)
{
std::string sdpstr(sprop+strlen(pattern));
size_t pos = sdpstr.find_first_of(" ;\r\n");
if (pos != std::string::npos)
{
sdpstr.erase(pos);
}
webrtc::H264SpropParameterSets sprops;
if (sprops.DecodeSprop(sdpstr))
{
struct timeval presentationTime;
timerclear(&presentationTime);
std::vector<uint8_t> sps;
sps.insert(sps.end(), marker, marker+sizeof(marker));
sps.insert(sps.end(), sprops.sps_nalu().begin(), sprops.sps_nalu().end());
onData(id, sps.data(), sps.size(), presentationTime);
std::vector<uint8_t> pps;
pps.insert(pps.end(), marker, marker+sizeof(marker));
pps.insert(pps.end(), sprops.pps_nalu().begin(), sprops.pps_nalu().end());
onData(id, pps.data(), pps.size(), presentationTime);
}
else
{
RTC_LOG(WARNING) << "Cannot decode SPS:" << sprop;
}
}
success = true;
}
else if (strcmp(codec, "JPEG") == 0)
{
m_codec = codec;
success = true;
}
}
return success;
}
bool RTSPVideoCapturer::onData(const char* id, unsigned char* buffer, ssize_t size, struct timeval presentationTime)
{
int64_t ts = presentationTime.tv_sec;
ts = ts*1000 + presentationTime.tv_usec/1000;
RTC_LOG(LS_VERBOSE) << "RTSPVideoCapturer:onData size:" << size << " ts:" << ts;
int res = 0;
if (m_codec == "H264") {
webrtc::H264::NaluType nalu_type = webrtc::H264::ParseNaluType(buffer[sizeof(marker)]);
if (nalu_type == webrtc::H264::NaluType::kSps) {
RTC_LOG(LS_VERBOSE) << "RTSPVideoCapturer:onData SPS";
m_cfg.clear();
m_cfg.insert(m_cfg.end(), buffer, buffer+size);
absl::optional<webrtc::SpsParser::SpsState> sps = webrtc::SpsParser::ParseSps(buffer+sizeof(marker)+webrtc::H264::kNaluTypeSize, size-sizeof(marker)-webrtc::H264::kNaluTypeSize);
if (!sps) {
RTC_LOG(LS_ERROR) << "cannot parse sps";
res = -1;
}
else
{
int fps = 25;
if (m_decoder.get()) {
if ( (GetCaptureFormat()->width != sps->width) || (GetCaptureFormat()->height != sps->height) ) {
RTC_LOG(INFO) << "format changed => set format from " << GetCaptureFormat()->width << "x" << GetCaptureFormat()->height << " to " << sps->width << "x" << sps->height;
m_decoder.reset(NULL);
}
}
if (!m_decoder.get()) {
RTC_LOG(INFO) << "RTSPVideoCapturer:onData SPS set format " << sps->width << "x" << sps->height << " fps:" << fps;
cricket::VideoFormat videoFormat(sps->width, sps->height, cricket::VideoFormat::FpsToInterval(fps), cricket::FOURCC_I420);
SetCaptureFormat(&videoFormat);
m_decoder=m_factory.CreateVideoDecoder(webrtc::SdpVideoFormat(cricket::kH264CodecName));
webrtc::VideoCodec codec_settings;
codec_settings.codecType = webrtc::VideoCodecType::kVideoCodecH264;
m_decoder->InitDecode(&codec_settings,2);
m_decoder->RegisterDecodeCompleteCallback(this);
}
}
}
else if (nalu_type == webrtc::H264::NaluType::kPps) {
RTC_LOG(LS_VERBOSE) << "RTSPVideoCapturer:onData PPS";
m_cfg.insert(m_cfg.end(), buffer, buffer+size);
}
else if (m_decoder.get()) {
std::vector<uint8_t> content;
if (nalu_type == webrtc::H264::NaluType::kIdr) {
RTC_LOG(LS_VERBOSE) << "RTSPVideoCapturer:onData IDR";
content.insert(content.end(), m_cfg.begin(), m_cfg.end());
}
else {
RTC_LOG(LS_VERBOSE) << "RTSPVideoCapturer:onData SLICE NALU:" << nalu_type;
}
content.insert(content.end(), buffer, buffer+size);
Frame frame(std::move(content), ts);
{
std::unique_lock<std::mutex> lock(m_queuemutex);
m_queue.push(frame);
}
m_queuecond.notify_all();
} else {
RTC_LOG(LS_ERROR) << "RTSPVideoCapturer:onData no decoder";
res = -1;
}
} else if (m_codec == "JPEG") {
int32_t width = 0;
int32_t height = 0;
if (libyuv::MJPGSize(buffer, size, &width, &height) == 0) {
int stride_y = width;
int stride_uv = (width + 1) / 2;
rtc::scoped_refptr<webrtc::I420Buffer> I420buffer = webrtc::I420Buffer::Create(width, height, stride_y, stride_uv, stride_uv);
const int conversionResult = libyuv::ConvertToI420((const uint8_t*)buffer, size,
I420buffer->MutableDataY(), I420buffer->StrideY(),
I420buffer->MutableDataU(), I420buffer->StrideU(),
I420buffer->MutableDataV(), I420buffer->StrideV(),
0, 0,
width, height,
width, height,
libyuv::kRotate0, ::libyuv::FOURCC_MJPG);
if (conversionResult >= 0) {
webrtc::VideoFrame frame(I420buffer, 0, ts, webrtc::kVideoRotation_0);
this->Decoded(frame);
} else {
RTC_LOG(LS_ERROR) << "RTSPVideoCapturer:onData decoder error:" << conversionResult;
res = -1;
}
} else {
RTC_LOG(LS_ERROR) << "RTSPVideoCapturer:onData cannot JPEG dimension";
res = -1;
}
}
return (res == 0);
}
void RTSPVideoCapturer::onError(RTSPConnection& connection, const char* error) {
RTC_LOG(LS_ERROR) << "RTSPVideoCapturer:onError url:" << m_connection.getUrl() << " error:" << error;
connection.start(1);
}
void RTSPVideoCapturer::DecoderThread()
{
while (IsRunning()) {
std::unique_lock<std::mutex> mlock(m_queuemutex);
while (m_queue.empty())
{
m_queuecond.wait(mlock);
}
Frame frame = m_queue.front();
m_queue.pop();
mlock.unlock();
RTC_LOG(LS_VERBOSE) << "RTSPVideoCapturer:DecoderThread size:" << frame.m_content.size() << " ts:" << frame.m_timestamp_ms;
uint8_t* data = frame.m_content.data();
ssize_t size = frame.m_content.size();
if (size) {
size_t allocsize = size + webrtc::EncodedImage::GetBufferPaddingBytes(webrtc::VideoCodecType::kVideoCodecH264);
uint8_t* buf = new uint8_t[allocsize];
memcpy( buf, data, size );
webrtc::EncodedImage input_image(buf, size, allocsize);
input_image.SetTimestamp(frame.m_timestamp_ms); // store time in ms that overflow the 32bits
m_decoder->Decode(input_image, false, NULL,0);
delete [] buf;
}
}
}
int32_t RTSPVideoCapturer::Decoded(webrtc::VideoFrame& decodedImage)
{
int64_t ts = std::chrono::high_resolution_clock::now().time_since_epoch().count()/1000/1000;
RTC_LOG(LS_VERBOSE) << "RTSPVideoCapturer::Decoded size:" << decodedImage.size()
<< " decode ts:" << decodedImage.timestamp()
<< " source ts:" << ts;
// avoid to block the encoder that drop frames when sent too quickly
static int64_t previmagets = 0;
static int64_t prevts = 0;
if (prevts != 0) {
int64_t periodSource = decodedImage.timestamp() - previmagets;
int64_t periodDecode = ts-prevts;
RTC_LOG(LS_VERBOSE) << "RTSPVideoCapturer::Decoded interframe decode:" << periodDecode << " source:" << periodSource;
int64_t delayms = periodSource-periodDecode;
if ( (delayms > 0) && (delayms < 1000) ) {
std::this_thread::sleep_for(std::chrono::milliseconds(delayms));
}
}
if ( (m_height == 0) && (m_width == 0) ) {
this->OnFrame(decodedImage, decodedImage.height(), decodedImage.width());
} else {
int height = m_height;
int width = m_width;
if (height == 0) {
height = (decodedImage.height() * width) / decodedImage.width();
}
else if (width == 0) {
width = (decodedImage.width() * height) / decodedImage.height();
}
int stride_y = width;
int stride_uv = (width + 1) / 2;
rtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer = webrtc::I420Buffer::Create(width, height, stride_y, stride_uv, stride_uv);
scaled_buffer->ScaleFrom(*decodedImage.video_frame_buffer()->ToI420());
webrtc::VideoFrame frame = webrtc::VideoFrame(scaled_buffer, decodedImage.timestamp(),
decodedImage.render_time_ms(), webrtc::kVideoRotation_0);
this->OnFrame(frame, frame.width(), frame.height());
}
previmagets = decodedImage.timestamp();
prevts = std::chrono::high_resolution_clock::now().time_since_epoch().count()/1000/1000;
return true;
}
cricket::CaptureState RTSPVideoCapturer::Start(const cricket::VideoFormat& format)
{
RTC_LOG(INFO) << "RTSPVideoCapturer::start format" << format.ToString();
SetCaptureFormat(&format);
SetCaptureState(cricket::CS_RUNNING);
SetName("RTSPVideoCapturer", NULL);
rtc::Thread::Start();
m_decoderthread = std::thread(&RTSPVideoCapturer::DecoderThread, this);
return cricket::CS_RUNNING;
}
void RTSPVideoCapturer::Stop()
{
RTC_LOG(INFO) << "RTSPVideoCapturer::stop";
m_env.stop();
rtc::Thread::Stop();
SetCaptureFormat(NULL);
SetCaptureState(cricket::CS_STOPPED);
Frame frame;
{
std::unique_lock<std::mutex> lock(m_queuemutex);
m_queue.push(frame);
}
m_queuecond.notify_all();
m_decoderthread.join();
}
void RTSPVideoCapturer::Run()
{
m_env.mainloop();
}
bool RTSPVideoCapturer::GetPreferredFourccs(std::vector<unsigned int>* fourccs)
{
return true;
}
#endif