-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtsp_stream.cpp
More file actions
356 lines (288 loc) · 13.4 KB
/
Copy pathrtsp_stream.cpp
File metadata and controls
356 lines (288 loc) · 13.4 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "rtsp_stream.hpp"
#include "utils.hpp"
#include "packet_pool.hpp"
#include "config.h"
#include <chrono>
#include <algorithm>
#include <cstring>
static auto codec_params_deleter = [](AVCodecParameters* p) { if (p) avcodec_parameters_free(&p); };
constexpr int MAX_REASONABLE_PACKET_SIZE = 3 * 1024 * 1024; // 3 MB
RTSPStream::RTSPStream(Config cfg) : cfg_(std::move(cfg)) {
if (cfg_.read_timeout_ms <= 0) {
cfg_.read_timeout_ms = 10000;
}
}
RTSPStream::~RTSPStream() { stop(); }
void RTSPStream::start(PacketCallback cb) {
if (worker_.joinable()) {
LOG_WARN("RTSP", "Already running.");
return;
}
packet_cb_ = std::move(cb);
LOG_INFO("RTSP", "Starting stream: ", cfg_.url);
LOG_INFO("RTSP", "Read timeout configured: ", cfg_.read_timeout_ms, "ms");
worker_ = std::jthread([this](std::stop_token st) { run(st); });
watchdog_ = std::jthread([this](std::stop_token st) { watchdog_loop(st); });
}
void RTSPStream::stop() {
if (!worker_.joinable()) return;
LOG_INFO("RTSP", "Stopping stream...");
watchdog_enabled_.store(false, std::memory_order_release);
interrupt_flag_.store(true, std::memory_order_release);
if (watchdog_.joinable()) {
watchdog_.request_stop();
watchdog_.join();
}
worker_.request_stop();
worker_.join();
{
std::lock_guard lock(io_mtx_);
close_input();
}
interrupt_flag_.store(false, std::memory_order_release);
LOG_INFO("RTSP", "Stream stopped.");
}
bool RTSPStream::is_alive() const noexcept {
return alive_.load(std::memory_order_acquire);
}
auto RTSPStream::get_codec_params() const -> std::unique_ptr<AVCodecParameters, std::function<void(AVCodecParameters*)>> {
std::lock_guard lock(io_mtx_);
if (!codec_params_) return nullptr;
auto clone = std::unique_ptr<AVCodecParameters, std::function<void(AVCodecParameters*)>>(
avcodec_parameters_alloc(), codec_params_deleter);
if (avcodec_parameters_copy(clone.get(), codec_params_.get()) < 0) return nullptr;
return clone;
}
RTSPStream::StreamInfo RTSPStream::get_stream_info() const {
std::lock_guard lock(io_mtx_);
return StreamInfo{
.time_base = stream_time_base_,
.frame_rate = stream_frame_rate_,
.valid = stream_info_valid_
};
}
void RTSPStream::watchdog_loop(std::stop_token st) {
LOG_DEBUG("RTSP_WD", "Watchdog started with timeout: ", cfg_.read_timeout_ms, "ms");
while (!st.stop_requested()) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (!watchdog_enabled_.load(std::memory_order_acquire)) {
continue;
}
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
auto last_packet_ms = last_packet_time_ms_.load(std::memory_order_acquire);
uint64_t elapsed = static_cast<uint64_t>(now_ms - last_packet_ms);
if (elapsed > static_cast<uint64_t>(cfg_.read_timeout_ms)) {
LOG_ERROR("RTSP_WD", "TIMEOUT! No data received for ", elapsed, "ms (limit: ",
cfg_.read_timeout_ms, "ms). Forcing disconnect.");
interrupt_flag_.store(true, std::memory_order_release);
watchdog_enabled_.store(false, std::memory_order_release);
}
}
LOG_DEBUG("RTSP_WD", "Watchdog stopped");
}
void RTSPStream::run(std::stop_token st) {
int reconnect_attempts = 0;
int64_t current_delay = cfg_.reconnect_delay_ms;
bool was_connected = false;
while (!st.stop_requested()) {
interrupt_flag_.store(false, std::memory_order_release);
watchdog_enabled_.store(false, std::memory_order_release);
if (!open_input()) {
close_input();
alive_.store(false, std::memory_order_release);
if (was_connected) {
if (cfg_.internal_lost_callback) {
LOG_DEBUG("RTSP", "Calling internal_lost_callback");
cfg_.internal_lost_callback();
}
if (!cfg_.on_rtsp_lost.empty() && cfg_.camera_config) {
LOG_WARN("RTSP", "Calling on_rtsp_lost callback");
execute_script_async(cfg_.on_rtsp_lost, *cfg_.camera_config);
}
was_connected = false;
}
if (cfg_.max_reconnect_attempts > 0 && reconnect_attempts >= cfg_.max_reconnect_attempts) {
LOG_ERROR("RTSP", "Max reconnect attempts reached.");
return;
}
LOG_WARN("RTSP", "Reconnecting in ", current_delay, "ms (attempt ", reconnect_attempts + 1, ")");
for (int i = 0; i < current_delay / 100 && !st.stop_requested(); ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
current_delay = std::min(current_delay * 2, static_cast<int64_t>(cfg_.reconnect_max_delay_ms));
reconnect_attempts++;
continue;
}
reconnect_attempts = 0;
current_delay = cfg_.reconnect_delay_ms;
alive_.store(true, std::memory_order_release);
if (!was_connected) {
if (cfg_.internal_found_callback) {
LOG_DEBUG("RTSP", "Calling internal_found_callback");
cfg_.internal_found_callback();
}
if (!cfg_.on_rtsp_found.empty() && cfg_.camera_config) {
LOG_INFO("RTSP", "Calling on_rtsp_found callback");
execute_script_async(cfg_.on_rtsp_found, *cfg_.camera_config);
}
was_connected = true;
}
LOG_INFO("RTSP", "Connected to stream, starting watchdog");
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
last_packet_time_ms_.store(now_ms, std::memory_order_release);
watchdog_enabled_.store(true, std::memory_order_release);
AVPacket* pkt = PacketPool::instance().acquire();
bool connection_lost = false;
// Внутренний цикл чтения
while (!st.stop_requested()) {
if (interrupt_flag_.load(std::memory_order_acquire)) {
LOG_ERROR("RTSP", "Interrupt flag set, breaking read loop");
connection_lost = true;
break;
}
int ret = av_read_frame(fmt_ctx_, pkt);
if (ret < 0) {
av_packet_unref(pkt);
if (ret == AVERROR_EXIT) {
LOG_INFO("RTSP", "Received AVERROR_EXIT");
break;
}
if (ret == AVERROR(EAGAIN) || ret == AVERROR(EWOULDBLOCK)) {
LOG_DEBUG("RTSP", "Temporary unavailable, retrying");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
if (st.stop_requested()) {
break;
}
char err[128];
av_strerror(ret, err, sizeof(err));
if (ret == AVERROR(ETIMEDOUT) || ret == AVERROR(EIO) ||
ret == AVERROR(ECONNRESET) || ret == AVERROR(EPIPE)) {
LOG_ERROR("RTSP", "Connection error: ", err, " (code: ", ret, ")");
connection_lost = true;
} else {
LOG_WARN("RTSP", "Read error: ", err, " (code: ", ret, ")");
connection_lost = true;
}
break;
}
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
last_packet_time_ms_.store(now_ms, std::memory_order_release);
if (pkt->stream_index == video_stream_idx_) {
if (pkt->size == 0 || pkt->size > MAX_REASONABLE_PACKET_SIZE) {
LOG_WARN("RTSP", "Dropping abnormal packet: size=", pkt->size, " bytes");
av_packet_unref(pkt);
continue;
}
if (!pkt->data) {
LOG_WARN("RTSP", "Dropping packet with NULL data pointer");
av_packet_unref(pkt);
continue;
}
total_bytes_read_.fetch_add(pkt->size, std::memory_order_relaxed);
AVPacket* clone = av_packet_clone(pkt);
if (clone && packet_cb_) {
packet_cb_(clone);
} else if (clone) {
av_packet_free(&clone);
} else {
LOG_ERROR("RTSP", "Failed to clone packet (size=", pkt->size, ")");
}
}
av_packet_unref(pkt);
}
watchdog_enabled_.store(false, std::memory_order_release);
PacketPool::instance().release(pkt); // Теперь ВСЕГДА достигается
close_input();
alive_.store(false, std::memory_order_release);
if (connection_lost && !st.stop_requested()) {
if (was_connected) {
if (cfg_.internal_lost_callback) {
LOG_DEBUG("RTSP", "Connection lost during operation, calling internal_lost_callback");
cfg_.internal_lost_callback();
}
if (!cfg_.on_rtsp_lost.empty() && cfg_.camera_config) {
LOG_WARN("RTSP", "Connection lost during operation, calling on_rtsp_lost callback");
execute_script_async(cfg_.on_rtsp_lost, *cfg_.camera_config);
}
was_connected = false;
}
LOG_WARN("RTSP", "Connection lost. Attempting to reconnect...");
} else if (st.stop_requested()) {
LOG_INFO("RTSP", "Stop requested, exiting");
break;
}
}
}
bool RTSPStream::open_input() {
std::lock_guard lock(io_mtx_);
if (fmt_ctx_) return true;
AVFormatContext* raw_ctx = avformat_alloc_context();
if (!raw_ctx) {
LOG_ERROR("RTSP", "Alloc context failed");
return false;
}
raw_ctx->interrupt_callback = { interrupt_cb, &interrupt_flag_ };
AVDictionary* opts = nullptr;
av_dict_set(&opts, "rtsp_transport", cfg_.tcp_only ? "tcp" : "udp", 0);
av_dict_set_int(&opts, "stimeout", cfg_.timeout_ms * 1000LL, 0);
av_dict_set_int(&opts, "rw_timeout", cfg_.timeout_ms * 1000LL, 0);
av_dict_set(&opts, "fflags", "nobuffer+discardcorrupt", 0);
av_dict_set(&opts, "err_detect", "ignore_err", 0);
av_dict_set_int(&opts, "max_delay", 200000, 0);
av_dict_set(&opts, "reorder_queue_size", "0", 0);
av_dict_set(&opts, "buffer_size", "1024000", 0);
av_dict_set(&opts, "rtsp_flags", "prefer_tcp", 0);
av_dict_set(&opts, "flags", "low_delay", 0);
int ret = avformat_open_input(&raw_ctx, cfg_.url.c_str(), nullptr, &opts);
av_dict_free(&opts);
if (ret < 0) {
char e[128];
av_strerror(ret, e, sizeof(e));
LOG_ERROR("RTSP", "Open failed: ", e, " (code: ", ret, ")");
avformat_free_context(raw_ctx);
return false;
}
raw_ctx->max_analyze_duration = 2 * AV_TIME_BASE;
raw_ctx->probesize = 1024 * 1024;
if (avformat_find_stream_info(raw_ctx, nullptr) < 0) {
LOG_ERROR("RTSP", "Stream info failed");
avformat_close_input(&raw_ctx);
return false;
}
video_stream_idx_ = av_find_best_stream(raw_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
if (video_stream_idx_ < 0) {
LOG_ERROR("RTSP", "No video stream found");
avformat_close_input(&raw_ctx);
return false;
}
AVStream* stream = raw_ctx->streams[video_stream_idx_];
stream_time_base_ = stream->time_base;
stream_frame_rate_ = stream->avg_frame_rate;
stream_info_valid_ = true;
LOG_INFO("RTSP", "Stream parameters: time_base=", stream_time_base_.num, "/", stream_time_base_.den,
" fps=", stream_frame_rate_.num, "/", stream_frame_rate_.den);
codec_params_ = std::unique_ptr<AVCodecParameters, std::function<void(AVCodecParameters*)>>(
avcodec_parameters_alloc(), codec_params_deleter);
if (avcodec_parameters_copy(codec_params_.get(), stream->codecpar) < 0) {
avformat_close_input(&raw_ctx);
codec_params_.reset();
stream_info_valid_ = false;
return false;
}
fmt_ctx_ = raw_ctx;
return true;
}
void RTSPStream::close_input() {
if (!fmt_ctx_) return;
LOG_DEBUG("RTSP", "Closing input context");
avformat_close_input(&fmt_ctx_);
fmt_ctx_ = nullptr;
video_stream_idx_ = -1;
codec_params_.reset();
stream_info_valid_ = false;
}