-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtp_tests.cpp
More file actions
123 lines (107 loc) · 3.78 KB
/
Copy pathrtp_tests.cpp
File metadata and controls
123 lines (107 loc) · 3.78 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
#include "rtp/g711.h"
#include "rtp/rtp_packet.h"
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
namespace {
int g_failed = 0;
void Expect(bool cond, const std::string& msg) {
if (!cond) {
std::cerr << "FAIL: " << msg << "\n";
++g_failed;
}
}
void TestRoundTripBasic() {
rtp::RtpPacket p;
p.set_payload_type(0);
p.set_marker(true);
p.set_sequence(42);
p.set_timestamp(160);
p.set_ssrc(0xAABBCCDDu);
p.set_payload({1, 2, 3, 4, 5});
const auto wire = p.Serialize();
Expect(wire.size() == 17, "basic size 12+5");
Expect(wire[0] == 0x80, "V=2 P=0 X=0 CC=0");
Expect(wire[1] == 0x80, "M=1 PT=0");
rtp::RtpPacket q;
Expect(q.Parse(wire), "parse basic");
Expect(q.payload_type() == 0, "pt");
Expect(q.marker(), "marker");
Expect(q.sequence() == 42, "seq");
Expect(q.timestamp() == 160, "ts");
Expect(q.ssrc() == 0xAABBCCDDu, "ssrc");
Expect(q.payload() == std::vector<uint8_t>({1, 2, 3, 4, 5}), "payload");
}
void TestCsrcAndExtensionAndPadding() {
rtp::RtpPacket p;
p.set_payload_type(8);
p.set_sequence(7);
p.set_timestamp(99);
p.set_ssrc(1);
p.set_csrcs({0x11111111u, 0x22222222u});
p.set_extension(0xBEDE, {0x10, 0x20, 0x30, 0x40});
p.set_payload({9, 8, 7});
p.set_padding_count(4);
const auto wire = p.Serialize();
// 12 + 8 CSRCs + 4 ext hdr + 4 ext data + 3 payload + 4 pad = 35
Expect(wire.size() == 35, "complex size");
Expect((wire[0] & 0x20) != 0, "padding bit");
Expect((wire[0] & 0x10) != 0, "extension bit");
Expect((wire[0] & 0x0f) == 2, "cc=2");
Expect(wire.back() == 4, "pad count trailer");
rtp::RtpPacket q;
Expect(q.Parse(wire), "parse complex");
Expect(q.csrcs().size() == 2, "cc parse");
Expect(q.csrcs()[0] == 0x11111111u, "csrc0");
Expect(q.extension(), "ext");
Expect(q.extension_profile() == 0xBEDE, "ext profile");
Expect(q.extension_data().size() == 4, "ext data");
Expect(q.payload() == std::vector<uint8_t>({9, 8, 7}), "payload strips pad");
Expect(q.padding_count() == 4, "pad count");
}
void TestRejectsBad() {
rtp::RtpPacket p;
Expect(!p.Parse(nullptr, 12), "null");
const uint8_t shortbuf[8] = {0x80, 0x00};
Expect(!p.Parse(shortbuf, sizeof(shortbuf)), "too short");
uint8_t badver[12] = {};
badver[0] = 0x40; // V=1
Expect(!p.Parse(badver, sizeof(badver)), "bad version");
}
void TestG711Sanity() {
Expect(rtp::g711::LinearToUlaw(0) == 0xff, "ulaw 0 encodes to 0xFF");
Expect(rtp::g711::UlawToLinear(0xff) == 0, "ulaw 0xFF decodes to 0");
Expect(rtp::g711::LinearToAlaw(0) == 0xd5, "alaw 0 encodes to 0xD5");
int max_err_u = 0;
int max_err_a = 0;
for (int s = -30000; s <= 30000; s += 17) {
const int16_t pcm = static_cast<int16_t>(s);
const int16_t back_u = rtp::g711::UlawToLinear(rtp::g711::LinearToUlaw(pcm));
const int16_t back_a = rtp::g711::AlawToLinear(rtp::g711::LinearToAlaw(pcm));
max_err_u = std::max(max_err_u, std::abs(static_cast<int>(pcm) - back_u));
max_err_a = std::max(max_err_a, std::abs(static_cast<int>(pcm) - back_a));
}
Expect(max_err_u < 600, "ulaw max error " + std::to_string(max_err_u));
Expect(max_err_a < 600, "alaw max error " + std::to_string(max_err_a));
const auto ulaw = rtp::g711::LinearToUlaw(1234);
std::vector<uint8_t> in = {ulaw, rtp::g711::LinearToUlaw(0),
rtp::g711::LinearToUlaw(-8000)};
auto conv = rtp::g711::ConvertPayload(in, 0, 8);
Expect(conv.size() == in.size(), "convert size");
auto same = rtp::g711::ConvertPayload(in, 0, 0);
Expect(same == in, "same-pt passthrough");
}
} // namespace
int main() {
TestRoundTripBasic();
TestCsrcAndExtensionAndPadding();
TestRejectsBad();
TestG711Sanity();
if (g_failed != 0) {
std::cerr << g_failed << " assertion(s) failed\n";
return 1;
}
std::cout << "rtp_tests: all passed\n";
return 0;
}