-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtp.cpp
More file actions
160 lines (118 loc) · 4.05 KB
/
Copy pathrtp.cpp
File metadata and controls
160 lines (118 loc) · 4.05 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
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
#include "rtp.hxx"
// #include "shutdown.hxx"
#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <string>
#include <osrng.h>
#include <secblock.h>
#include <sys/types.h>
#include <xed25519.h>
uvgrtp::context Rtp::ctx;
constexpr int DEFAULT_SEND_FLAGS =
RCE_RTCP | RCE_SYSTEM_CALL_CLUSTERING | RCE_SEND_ONLY;
constexpr int DEFAULT_RECV_FLAGS = RCE_RTCP | RCE_RECEIVE_ONLY;
// Confusingly the server connects to the client, this is my poor naming.
Rtp::Rtp(std::pair<std::string, uint16_t> local_socket,
std::pair<std::string, uint16_t> remote_socket, asio::io_context &io)
: io(io) {
this->logger = spdlog::get("audpipe");
this->session =
ctx.create_session(std::pair(local_socket.first, remote_socket.first));
handshake_server();
init_rtp(this, true, local_socket.first, local_socket.second,
remote_socket.second);
}
Rtp::Rtp(std::pair<std::string, uint16_t> local_socket,
std::pair<std::string, uint16_t> remote_socket,
std::pair<recv_hook_t, void *> cb, asio::io_context &io)
: io(io) {
this->logger = spdlog::get("audpipe");
this->recv_callback = cb;
this->session =
ctx.create_session(std::pair(local_socket.first, remote_socket.first));
handshake_client();
init_rtp(this, false, local_socket.first, local_socket.second,
remote_socket.second);
}
void Rtp::write_frames(uint8_t *data, size_t data_len) {
auto lock = std::scoped_lock(this->teardown_mutex);
if (this->stopped.load() || this->stream == nullptr) {
return;
}
rtp_error_t err = this->stream->push_frame(data, data_len, RCC_NO_FLAGS);
if (err != RTP_OK) {
this->logger->error("Failed to write RTP frame with error code: {}",
static_cast<int8_t>(err));
// TODO: Check if recoverable at all.
}
this->logger->debug("Wrote frame of length {}", data_len);
}
void Rtp::init_rtp(Rtp *rtp, bool sending, std::string &local_addr,
uint16_t local_port, uint16_t remote_port) {
assert(ctx.crypto_enabled());
if (!sending) {
// Invariant: We want the callback to definitely exist before a
// media_stream is created.
assert(rtp->recv_callback.first != nullptr);
}
if (rtp->session == nullptr) {
auto error_str = "Failed to create uvgRTP session. Must be OOM.";
rtp->logger->critical(error_str);
throw std::runtime_error(error_str);
}
int flags;
if (sending) {
flags = DEFAULT_SEND_FLAGS;
} else {
flags = DEFAULT_RECV_FLAGS;
};
rtp->stream = rtp->session->create_stream(local_port, remote_port,
RTP_FORMAT_OPUS, flags);
if (rtp->stream == nullptr) {
auto error_str =
std::format("Failed to create opus RTP stream with error code {}.",
static_cast<uint8_t>(rtp_errno));
rtp->logger->critical(error_str);
throw std::runtime_error(error_str);
}
if (!sending) {
rtp->stream->install_receive_hook(rtp->recv_callback.second,
rtp->recv_callback.first);
}
}
Rtp::~Rtp() { stop(); }
void Rtp::stop() {
auto lock = std::scoped_lock(this->teardown_mutex);
if (this->stopped.exchange(true)) {
return;
}
if (this->session != nullptr && this->stream != nullptr) {
this->session->destroy_stream(this->stream);
this->stream = nullptr;
}
if (this->session != nullptr) {
ctx.destroy_session(this->session);
this->session = nullptr;
}
}
bool Rtp::is_initialised() const {
return !this->stopped.load() && this->session != nullptr &&
this->stream != nullptr;
}
bool Rtp::is_stopped() const { return this->stopped.load(); }
// TODO: Authenticate with ed25519.
void Rtp::dhka(Role role) {
using namespace CryptoPP;
AutoSeededRandomPool prng;
x25519 ecdh(prng);
// Generate public and private keys.
SecByteBlock privkey(x25519::SECRET_KEYLENGTH);
SecByteBlock pubkey(x25519::PUBLIC_KEYLENGTH);
ecdh.GenerateKeyPair(prng, privkey, pubkey);
if (role == Role::SEND_FIRST) {
} else {
}
}
void Rtp::handshake_client() {}
void Rtp::handshake_server() {}