MCPcopy Create free account
hub / github.com/ClassicOldSong/Apollo / encodeThread

Function encodeThread

src/audio.cpp:86–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

84 };
85
86 void encodeThread(sample_queue_t samples, config_t config, void *channel_data) {
87 auto packets = mail::man->queue<packet_t>(mail::audio_packets);
88 auto stream = stream_configs[map_stream(config.channels, config.flags[config_t::HIGH_QUALITY])];
89 if (config.flags[config_t::CUSTOM_SURROUND_PARAMS]) {
90 apply_surround_params(stream, config.customStreamParams);
91 }
92
93 // Encoding takes place on this thread
94 platf::adjust_thread_priority(platf::thread_priority_e::high);
95
96 opus_t opus {opus_multistream_encoder_create(
97 stream.sampleRate,
98 stream.channelCount,
99 stream.streams,
100 stream.coupledStreams,
101 stream.mapping,
102 OPUS_APPLICATION_RESTRICTED_LOWDELAY,
103 nullptr
104 )};
105
106 opus_multistream_encoder_ctl(opus.get(), OPUS_SET_BITRATE(stream.bitrate));
107 opus_multistream_encoder_ctl(opus.get(), OPUS_SET_VBR(0));
108
109 BOOST_LOG(info) << "Opus initialized: "sv << stream.sampleRate / 1000 << " kHz, "sv
110 << stream.channelCount << " channels, "sv
111 << stream.bitrate / 1000 << " kbps (total), LOWDELAY"sv;
112
113 auto frame_size = config.packetDuration * stream.sampleRate / 1000;
114 while (auto sample = samples->pop()) {
115 buffer_t packet {1400};
116
117 int bytes = opus_multistream_encode_float(opus.get(), sample->data(), frame_size, std::begin(packet), packet.size());
118 if (bytes < 0) {
119 BOOST_LOG(error) << "Couldn't encode audio: "sv << opus_strerror(bytes);
120 packets->stop();
121
122 return;
123 }
124
125 packet.fake_resize(bytes);
126 packets->raise(channel_data, std::move(packet));
127 }
128 }
129
130 void capture(safe::mail_t mail, config_t config, void *channel_data) {
131 auto shutdown_event = mail->event<bool>(mail::shutdown);

Callers

nothing calls this directly

Calls 11

map_streamFunction · 0.85
apply_surround_paramsFunction · 0.85
moveFunction · 0.85
fake_resizeMethod · 0.80
adjust_thread_priorityFunction · 0.50
getMethod · 0.45
popMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45
stopMethod · 0.45
raiseMethod · 0.45

Tested by

no test coverage detected