-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreceiver.cc
217 lines (168 loc) · 6.11 KB
/
receiver.cc
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
#include "../util/util.hh"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libswscale/swscale.h>
}
#include <atomic>
#include <cstdio>
#include <chrono>
#include <thread>
#include <cstdlib>
#include <iostream>
#define SETUP_FFMPEG_PARAMETERS
struct thread_info {
size_t pkts;
size_t bytes;
std::chrono::high_resolution_clock::time_point start;
} *thread_info;
std::atomic<int> nready(0);
std::chrono::high_resolution_clock::time_point last;
size_t pkts = 0;
static int cb(void *ctx)
{
if (pkts) {
uint64_t diff = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - last
).count();
/* we haven't received a frame in the last 300 milliseconds, stop receiver */
if (diff >= 10)
return 1;
}
return 0;
}
static const AVIOInterruptCB int_cb = { cb, NULL };
void thread_func(int thread_num, int nthreads, std::string local_address, int local_port,
std::string remote_address, int remote_port, bool vvc, bool srtp)
{
AVFormatContext *format_ctx = avformat_alloc_context();
AVCodecContext *codec_ctx = NULL;
int video_stream_index = 0;
/* register everything */
av_register_all();
avformat_network_init();
format_ctx->interrupt_callback = int_cb;
av_log_set_level(AV_LOG_PANIC);
/* open rtsp */
AVDictionary *d = NULL;
av_dict_set(&d, "protocol_whitelist", "file,udp,rtp", 0);
char buf[256];
/* input buffer size */
snprintf(buf, sizeof(buf), "%d", 40 * 1000 * 1000);
av_dict_set(&d, "buffer_size", buf, 32);
#ifdef SETUP_FFMPEG_PARAMETERS
snprintf(buf, sizeof(buf), "%d", 10000000);
av_dict_set(&d, "max_delay", buf, 32);
snprintf(buf, sizeof(buf), "%d", 40 * 1000 * 1000);
av_dict_set(&d, "recv_buffer_size", buf, 32);
snprintf(buf, sizeof(buf), "%d", 40 * 1000 * 1000);
av_dict_set(&d, "rcvbuf", buf, 32);
/* avioflags flags (input/output)
*
* Possible values:
* ‘direct’
* Reduce buffering. */
snprintf(buf, sizeof(buf), "direct");
av_dict_set(&d, "avioflags", buf, 32);
/* Reduce the latency introduced by buffering during initial input streams analysis. */
av_dict_set(&d, "nobuffer", NULL, 32);
/* Set probing size in bytes, i.e. the size of the data to analyze to get stream information.
*
* A higher value will enable detecting more information in case it is dispersed into the stream,
* but will increase latency. Must be an integer not lesser than 32. It is 5000000 by default. */
snprintf(buf, sizeof(buf), "%d", 32);
av_dict_set(&d, "probesize", buf, 32);
/* Set number of frames used to probe fps. */
snprintf(buf, sizeof(buf), "%d", 2);
av_dict_set(&d, "fpsprobesize", buf, 32);
snprintf(buf, sizeof(buf), "%d", 1);
av_dict_set(&d, "stimeout", buf, 32);
snprintf(buf, sizeof(buf), "%d", 1);
av_dict_set(&d, "timeout", buf, 32);
snprintf(buf, sizeof(buf), "%d", 1);
av_dict_set(&d, "rw_timeout", buf, 32);
#endif
if (!strcmp(local_address.c_str(), "127.0.0.1"))
snprintf(buf, sizeof(buf), "ffmpeg/sdp/localhost/hevc_%d.sdp", nthreads);
else
snprintf(buf, sizeof(buf), "ffmpeg/sdp/lan/hevc_%d.sdp", nthreads);
if (avformat_open_input(&format_ctx, buf, NULL, &d)) {
fprintf(stderr, "failed to open input file\n");
nready++;
return;
}
if (avformat_find_stream_info(format_ctx, NULL) < 0) {
fprintf(stderr, "failed to find stream info!\n");
nready++;
return;
}
for (size_t i = 0; i < format_ctx->nb_streams; i++) {
if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
video_stream_index = i;
}
size_t size = 0;
AVPacket packet;
av_init_packet(&packet);
std::chrono::high_resolution_clock::time_point start;
start = std::chrono::high_resolution_clock::now();
/* start reading packets from stream */
av_read_play(format_ctx);
while (av_read_frame(format_ctx, &packet) >= 0) {
if (packet.stream_index == video_stream_index)
size += packet.size;
av_free_packet(&packet);
av_init_packet(&packet);
pkts += 1;
last = std::chrono::high_resolution_clock::now();
}
if (pkts == 598) {
fprintf(stderr, "%zu %zu %zu\n", size, pkts,
std::chrono::duration_cast<std::chrono::milliseconds>(last - start).count()
);
} else {
fprintf(stderr, "discard %zu %zu %zu\n", size, pkts,
std::chrono::duration_cast<std::chrono::milliseconds>(last - start).count()
);
}
av_read_pause(format_ctx);
nready++;
}
int main(int argc, char **argv)
{
if (argc != 9) {
fprintf(stderr, "usage: ./%s <result file> <local address> <local port> <remote address> <remote port> \
<number of threads> <format> <srtp>\n", __FILE__);
return EXIT_FAILURE;
}
std::string result_filename = argv[1];
std::string local_address = argv[2];
int local_port = atoi(argv[3]);
std::string remote_address = argv[4];
int remote_port = atoi(argv[5]);
int nthreads = atoi(argv[6]);
bool vvc_enabled = get_vvc_state(argv[7]);
bool srtp_enabled = get_srtp_state(argv[8]);
thread_info = (struct thread_info *)calloc(nthreads, sizeof(*thread_info));
std::vector<std::thread*> threads = {};
for (int i = 0; i < nthreads; ++i) {
threads.push_back(new std::thread(thread_func, i, nthreads, local_address, local_port,
remote_address, remote_port, vvc_enabled, srtp_enabled));
}
// wait all the thread executions to end and delete them
for (int i = 0; i < nthreads; ++i) {
if (threads[i]->joinable())
{
threads[i]->join();
}
delete threads[i];
threads[i] = nullptr;
}
/*
for (int i = 0; i < nthreads; ++i)
new std::thread(thread_func, argv[1], i * 2);
while (nready.load() != nthreads)
std::this_thread::sleep_for(std::chrono::milliseconds(20));
*/
return EXIT_SUCCESS;
}