-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
186 lines (149 loc) · 5.23 KB
/
Copy pathserver.cpp
File metadata and controls
186 lines (149 loc) · 5.23 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
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
#include <cstdint>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
#include <spdlog/spdlog.h>
#include "audio.hxx"
#include "opus.h"
#include "opus_defines.h"
#include "rtp.hxx"
#include "server.hxx"
#define OPUS_FRAME_SIZE 3840
std::string getline();
Server::Server(std::pair<std::string, uint16_t> local_socket,
std::pair<std::string, uint16_t> remote_socket, AudioDevice dev,
asio::io_context &io)
: local_address(local_socket.first), device(std::move(dev)),
rtp(local_socket, remote_socket, io) {
this->logger = spdlog::get("audpipe");
int error = OPUS_OK;
this->opusenc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &error);
if (error != OPUS_OK) {
auto error_msg = std::format("Failed to create opus encoder. Reason: {}",
opus_strerror(error));
this->logger->critical(error_msg);
throw std::runtime_error(error_msg);
}
opus_encoder_ctl(this->opusenc, OPUS_SET_BITRATE(192000));
opus_encoder_ctl(this->opusenc, OPUS_SET_COMPLEXITY(10));
opus_encoder_ctl(this->opusenc, OPUS_SET_DTX(1));
opus_encoder_ctl(
this->opusenc,
OPUS_SET_VBR(
1)); // TODO: Encryption needs to be handled specially with VBR on.
opus_encoder_ctl(this->opusenc, OPUS_SET_VBR_CONSTRAINT(0));
opus_encoder_ctl(this->opusenc, OPUS_SET_INBAND_FEC(0));
opus_encoder_ctl(this->opusenc, OPUS_SET_PACKET_LOSS_PERC(0));
opus_encoder_ctl(this->opusenc,
OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND));
opus_encoder_ctl(this->opusenc, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
this->opus_enc_outbuf = std::vector<uint8_t>();
this->opus_enc_outbuf.resize(OPUS_FRAME_SIZE);
this->opus_enc_outbuf_size = 0;
this->pcm_stereo_buf.resize(960 * 2);
auto &audio = AudioBackend::instance();
audio.set_data_callback([&](const uint8_t *data, size_t len) {
this->bytes_to_opus(data, len);
uint8_t *opus_data = static_cast<uint8_t *>(this->opus_enc_outbuf.data());
size_t opus_data_len = this->opus_enc_outbuf_size;
if (opus_data_len == 0) {
return;
}
// TODO: Make this check for errors/fail etc. For now just hand it off.
this->rtp.write_frames(opus_data, opus_data_len);
});
}
Server::~Server() {
stop();
if (this->opusenc != nullptr) {
opus_encoder_destroy(this->opusenc);
this->opusenc = nullptr;
}
}
void Server::run() {
this->running = true;
try {
AudioBackend::instance().record(this->device);
} catch (...) {
// We trust upstream code to log before throwing so just ignore the
// exception.
this->running = false;
throw;
}
this->running = false;
}
void Server::stop() {
this->running = false;
this->rtp.stop();
AudioBackend::instance().stop_recording();
}
AudioDevice Server::choose_device_interactive(std::vector<AudioDevice> inputs) {
for (int i = 0; i < inputs.size(); i++) {
std::cout << std::format("{})\t{}", i + 1, inputs.at(i).description)
<< std::endl;
}
std::cout << "Choose a device to record from (1): " << std::flush;
int idx = 1;
std::istringstream iss(getline());
if (!(iss >> idx) || (iss >> std::ws, !iss.eof()) || idx <= 0 ||
idx > inputs.size()) {
auto logger = spdlog::get("audpipe");
logger->debug("Invalid input, defaulting to 1.");
idx = 1;
}
AudioDevice dev = inputs.at(idx - 1);
return dev;
}
// Source - https://stackoverflow.com/a/546470
// Posted by Johannes Schaub - litb, modified by community. See post 'Timeline'
// for change history Retrieved 2026-03-13, License - CC BY-SA 3.0
std::string getline() {
std::string str;
std::getline(std::cin, str);
return str;
}
// Populates opus_enc_outbuf.
void Server::bytes_to_opus(const uint8_t *data, size_t len) {
// 960 samples/channel is 20 ms at 48 kHz.
const size_t max_bytes = OPUS_FRAME_SIZE;
this->opus_enc_outbuf_size = 0;
if (data == nullptr || len < sizeof(opus_int16)) {
return;
}
// Clear the outbuf.
memset(this->opus_enc_outbuf.data(), 0, this->opus_enc_outbuf.size());
const auto *in = reinterpret_cast<const opus_int16 *>(data);
const size_t samples_total = len / sizeof(opus_int16);
const opus_int16 *encode_in = nullptr;
if (this->device.channels == 1) {
if (samples_total < 960) {
return;
}
// Upmix mono capture to stereo expected by encoder.
for (size_t i = 0; i < 960; i++) {
auto sample = in[i];
this->pcm_stereo_buf[2 * i] = sample;
this->pcm_stereo_buf[2 * i + 1] = sample;
}
encode_in = this->pcm_stereo_buf.data();
} else {
if (samples_total < 1920) {
return;
}
encode_in = in;
}
auto res = opus_encode(this->opusenc, encode_in, 960,
this->opus_enc_outbuf.data(), max_bytes);
// TODO: Recover from this.
if (res < 0) {
auto error_msg = std::format("Failed to encode opus frame. Reason: {}",
opus_strerror(res));
this->logger->critical(error_msg);
throw std::runtime_error(error_msg);
}
// Else we have the size of the output frame. I would rather not reallocate
// the block of memory, seems easier to just store pointer and length.
this->opus_enc_outbuf_size = res;
this->logger->debug("Got opus frame with size {}", res);
}