-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtp_send.cpp
More file actions
88 lines (79 loc) · 2.63 KB
/
Copy pathrtp_send.cpp
File metadata and controls
88 lines (79 loc) · 2.63 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
#include "rtp/g711.h"
#include "rtp/rtp_packet.h"
#include "rtp/sine.h"
#include "rtp/udp_socket.h"
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
// Sends 20 ms G.711 RTP frames (160 samples @ 8 kHz) to a destination.
// Usage:
// rtp_send --dest 127.0.0.1:10000 --pt 0 --ssrc 1 --seconds 5
int main(int argc, char** argv) {
std::string dest_ip = "127.0.0.1";
uint16_t dest_port = 10000;
uint8_t pt = 0;
uint32_t ssrc = 1;
int seconds = 3;
int freq = 440;
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
auto need = [&](const char* name) -> std::string {
if (i + 1 >= argc) {
std::cerr << "missing value for " << name << "\n";
std::exit(2);
}
return argv[++i];
};
if (a == "--dest") {
const std::string v = need("--dest");
const auto colon = v.rfind(':');
dest_ip = v.substr(0, colon);
dest_port = static_cast<uint16_t>(std::stoi(v.substr(colon + 1)));
} else if (a == "--pt") {
pt = static_cast<uint8_t>(std::stoi(need("--pt")));
} else if (a == "--ssrc") {
ssrc = static_cast<uint32_t>(std::stoul(need("--ssrc")));
} else if (a == "--seconds") {
seconds = std::stoi(need("--seconds"));
} else if (a == "--freq") {
freq = std::stoi(need("--freq"));
} else if (a == "--help") {
std::cout << "rtp_send --dest IP:PORT [--pt 0|8] [--ssrc N] [--seconds N]\n";
return 0;
}
}
rtp::UdpSocket sock;
if (!sock.Bind("0.0.0.0", 0)) {
std::cerr << "bind failed\n";
return 1;
}
const int frame = 160;
const auto pcm = rtp::MakeSinePcm(8000, freq, 8000 * seconds);
uint16_t seq = 0;
uint32_t ts = 0;
rtp::SockAddr dest{dest_ip, dest_port};
std::cout << "sending " << seconds << "s PT=" << static_cast<int>(pt)
<< " to " << dest.ToString() << " from :" << sock.bound_port()
<< "\n";
for (size_t off = 0; off + frame <= pcm.size(); off += frame) {
std::vector<uint8_t> payload(frame);
for (int i = 0; i < frame; ++i) {
const int16_t s = pcm[off + static_cast<size_t>(i)];
payload[static_cast<size_t>(i)] =
(pt == rtp::g711::kPayloadTypePcma) ? rtp::g711::LinearToAlaw(s)
: rtp::g711::LinearToUlaw(s);
}
rtp::RtpPacket pkt;
pkt.set_payload_type(pt);
pkt.set_sequence(seq++);
pkt.set_timestamp(ts);
pkt.set_ssrc(ssrc);
pkt.set_payload(std::move(payload));
ts += static_cast<uint32_t>(frame);
const auto wire = pkt.Serialize();
sock.SendTo(wire, dest);
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
return 0;
}