-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnvr.c
251 lines (226 loc) · 9.32 KB
/
nvr.c
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
#include "nvr.h"
#include <libavformat/avformat.h>
#include "utils.h"
#include "log.h"
void ffmpeg_init() {
av_log_set_callback(nvr_log_ffmpeg);
av_register_all();
avcodec_register_all();
avformat_network_init();
}
void ffmpeg_deinit() {
avformat_network_deinit();
}
static int decode_interrupt_cb(void *arg) {
NVRThreadParams *p = (NVRThreadParams *) arg;
int check_conn_timeout = !p->connected && *p->conn_timeout;
int check_recv_timeout = p->connected && *p->recv_timeout;
if (*p->running == 0)
return 1;
if (check_conn_timeout || check_recv_timeout) {
struct timeval t2;
long delta_ms;
gettimeofday(&t2, NULL);
if (check_conn_timeout) {
delta_ms = (t2.tv_sec - p->start_time.tv_sec) * 1000;
delta_ms += (t2.tv_usec - p->start_time.tv_usec) / 1000;
if (delta_ms >= *p->conn_timeout) {
if (!p->failed) {
nvr_log(NVR_LOG_ERROR, "connection timeout");
p->failed = 1;
}
return 1;
}
} else /* if (check_recv_timeout) */ {
delta_ms = (t2.tv_sec - p->last_packet_time.tv_sec) * 1000;
delta_ms += (t2.tv_usec - p->last_packet_time.tv_usec) / 1000;
if (delta_ms >= *p->recv_timeout) {
if (!p->failed) {
nvr_log(NVR_LOG_ERROR, "packet timeout");
p->failed = 1;
}
return 1;
}
}
}
return 0;
}
void record(Camera *camera, Settings *settings, NVRThreadParams *params) {
AVFormatContext *icontext = NULL;
AVDictionary *ioptions = NULL;
AVStream *istream = NULL;
AVStream *ostream = NULL;
AVFormatContext *ocontext = NULL;
AVCodecContext *codec_context = NULL;
AVPacket packet;
int ret;
int ivideo_stream_index = -1;
int ocontext_initialized = 0;
int64_t o_start_dts = AV_NOPTS_VALUE;
int64_t o_last_dts = AV_NOPTS_VALUE;
int64_t i_last_dts = 0;
int got_keyframe = 0, header_written = 0;
char current_date[9], current_time[7], dirname[1024], filename[sizeof(dirname) + 64];
time_t raw_time;
struct tm *time_info;
params->conn_timeout = &settings->conn_timeout;
params->recv_timeout = &settings->recv_timeout;
params->running = &camera->running;
params->connected = 0;
params->failed = 0;
gettimeofday(¶ms->start_time, NULL);
av_dict_set(&ioptions, "rtsp_transport", "tcp", 0);
av_init_packet(&packet);
icontext = avformat_alloc_context();
icontext->interrupt_callback.callback = decode_interrupt_cb;
icontext->interrupt_callback.opaque = params;
icontext->protocol_whitelist = av_strdup("rtsp,rtp,tcp,udp");
nvr_log(NVR_LOG_DEBUG, "connecting");
ret = avformat_open_input(&icontext, camera->uri, NULL, &ioptions);
if (ret != 0) {
avformat_close_input(&icontext);
av_dict_free(&ioptions);
if (camera->running)
nvr_log(NVR_LOG_ERROR, "avformat_open_input failed with code %d", ret);
else
nvr_log(NVR_LOG_INFO, "stopped");
return;
}
nvr_log(NVR_LOG_DEBUG, "retrieving stream info");
if ((ret = avformat_find_stream_info(icontext, NULL)) != 0) {
avformat_close_input(&icontext);
av_dict_free(&ioptions);
if (camera->running)
nvr_log(NVR_LOG_ERROR, "avformat_find_stream_info failed with code %d", ret);
else
nvr_log(NVR_LOG_INFO, "stopped");
return;
}
for (unsigned int i = 0; i < icontext->nb_streams; i++)
if (icontext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
ivideo_stream_index = i;
istream = icontext->streams[ivideo_stream_index];
break;
}
if (ivideo_stream_index < 0) {
avformat_close_input(&icontext);
av_dict_free(&ioptions);
nvr_log(NVR_LOG_ERROR, "video stream not found");
return;
}
if (params->failed) {
avformat_close_input(&icontext);
av_dict_free(&ioptions);
nvr_log(NVR_LOG_ERROR, "failed");
return;
}
if (camera->running && !params->failed) {
params->connected = 1;
gettimeofday(¶ms->last_packet_time, NULL);
nvr_log(NVR_LOG_INFO, "connected");
}
while (av_read_frame(icontext, &packet) >= 0 && camera->running && !params->failed) {
gettimeofday(¶ms->last_packet_time, NULL);
if (packet.stream_index == ivideo_stream_index) {
if (packet.flags & AV_PKT_FLAG_KEY && packet.pts != AV_NOPTS_VALUE && packet.duration >= 0) {
got_keyframe = 1;
if (!header_written ||
(settings->segment_length > 0 && o_last_dts >= settings->segment_length * 100)) {
if (header_written) {
av_write_trailer(ocontext);
avio_close(ocontext->pb);
}
header_written = 0;
if (ocontext_initialized) {
avformat_free_context(ocontext);
}
avformat_alloc_output_context2(&ocontext, NULL, "mp4", NULL);
ocontext_initialized = 1;
codec_context = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(codec_context, istream->codecpar);
ostream = avformat_new_stream(ocontext, codec_context->codec);
avcodec_parameters_from_context(ostream->codecpar, codec_context);
avcodec_free_context(&codec_context);
time(&raw_time);
time_info = localtime(&raw_time);
strftime(current_date, sizeof(current_date), "%Y%m%d", time_info);
strftime(current_time, sizeof(current_time), "%H%M%S", time_info);
snprintf(dirname, sizeof(dirname), "%s/%s/%s", settings->storage_dir, current_date, camera->name);
snprintf(filename, sizeof(filename), "%s/%s.mp4", dirname, current_time);
if ((ret = mkdirs(dirname, 0755)) != 0) {
nvr_log(NVR_LOG_ERROR,
"failed to create directory \"%s\" with code %d", dirname, ret);
break;
}
if ((ret = avio_open2(&ocontext->pb, filename, AVIO_FLAG_WRITE, NULL, NULL)) != 0) {
nvr_log(NVR_LOG_ERROR,
"failed to open \"%s\" with code %d", filename, ret);
break;
}
nvr_log(NVR_LOG_DEBUG, "writing to \"%s\"", filename);
if ((ret = avformat_write_header(ocontext, NULL)) != 0) {
nvr_log(NVR_LOG_ERROR,
"failed to write header to \"%s\" with code %d", filename, ret);
break;
}
header_written = 1;
o_start_dts = av_rescale_q(
packet.dts,
istream->time_base,
ostream->time_base
);
}
}
if (got_keyframe) {
packet.stream_index = istream->index;
if (packet.dts != AV_NOPTS_VALUE && packet.pts != AV_NOPTS_VALUE && packet.dts > packet.pts) {
packet.pts = packet.dts = packet.pts + packet.dts + i_last_dts + 1
- MIN3(packet.pts, packet.dts, i_last_dts + 1)
- MAX3(packet.pts, packet.dts, i_last_dts + 1);
}
if (packet.dts != AV_NOPTS_VALUE && i_last_dts != AV_NOPTS_VALUE) {
int64_t max = i_last_dts + 1;
if (packet.dts < max) {
if (packet.pts >= packet.dts)
packet.pts = MAX(packet.pts, max);
packet.dts = max;
}
}
i_last_dts = packet.dts;
packet.pts = av_rescale_q(
packet.pts,
istream->time_base,
ostream->time_base
) - o_start_dts;
packet.dts = av_rescale_q(
packet.dts,
istream->time_base,
ostream->time_base
) - o_start_dts;
o_last_dts = packet.dts;
if ((ret = av_interleaved_write_frame(ocontext, &packet)) != 0) {
nvr_log(NVR_LOG_ERROR, "failed to write frame to \"%s\" with code %d", filename, ret);
av_packet_unref(&packet);
break;
}
}
}
av_packet_unref(&packet);
av_init_packet(&packet);
}
av_packet_unref(&packet);
if (header_written) {
av_write_trailer(ocontext);
avio_close(ocontext->pb);
}
av_dict_free(&ioptions);
avformat_close_input(&icontext);
if (ocontext_initialized)
avformat_free_context(ocontext);
params->connected = 0;
if (camera->running)
nvr_log(NVR_LOG_ERROR, "failed");
else
nvr_log(NVR_LOG_INFO, "stopped");
return;
}