-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtp_recv.cpp
More file actions
74 lines (66 loc) · 1.89 KB
/
Copy pathrtp_recv.cpp
File metadata and controls
74 lines (66 loc) · 1.89 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
#include "rtp/rtp_packet.h"
#include "rtp/udp_socket.h"
#include <time.h>
#include <chrono>
#include <iostream>
#include <string>
static void SleepMs(int ms) {
timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000L;
nanosleep(&ts, nullptr);
}
int main(int argc, char** argv) {
uint16_t port = 20000;
int seconds = 6;
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
if (a == "--port" && i + 1 < argc) {
port = static_cast<uint16_t>(std::stoi(argv[++i]));
} else if (a == "--seconds" && i + 1 < argc) {
seconds = std::stoi(argv[++i]);
}
}
rtp::UdpSocket sock;
if (!sock.Bind("0.0.0.0", port)) {
std::cerr << "bind " << port << " failed\n";
return 1;
}
sock.SetNonBlocking(true);
std::cout << "listening on :" << sock.bound_port() << "\n";
const auto deadline =
std::chrono::steady_clock::now() + std::chrono::seconds(seconds);
uint8_t buf[2048];
uint64_t count = 0;
uint64_t bytes = 0;
int last_seq = -1;
uint64_t gaps = 0;
while (std::chrono::steady_clock::now() < deadline) {
rtp::SockAddr from;
const int n = sock.RecvFrom(buf, sizeof(buf), &from);
if (n <= 0) {
SleepMs(5);
continue;
}
rtp::RtpPacket pkt;
if (!pkt.Parse(buf, static_cast<size_t>(n))) {
std::cout << "bad packet from " << from.ToString() << " len=" << n << "\n";
continue;
}
++count;
bytes += pkt.payload().size();
if (last_seq >= 0) {
const int expect = (last_seq + 1) & 0xffff;
if (pkt.sequence() != static_cast<uint16_t>(expect)) {
++gaps;
}
}
last_seq = pkt.sequence();
if (count <= 5 || count % 50 == 0) {
std::cout << "from=" << from.ToString() << " " << pkt.Describe() << "\n";
}
}
std::cout << "done packets=" << count << " payload_bytes=" << bytes
<< " seq_gaps=" << gaps << "\n";
return count > 0 ? 0 : 1;
}