Driver: Split receive into its own feature (#141)

Adds the "receive" feature, which is disabled by default. When this is disabled, the UDP receive task is not compiled and not run, and as an optimisation the UDP receive buffer size is set to 0. All related events are also removed.

This also removes the UDP Tx task, and moves packet and keepalive sends back into the mixer thread. This allows us to entirely remove channels and various allocations between the mixer and an async task created only for sending data (i.e., fewer memcopies).

If "receive" is enabled, UDP sends are now non-blocking due to technical constraints -- failure to send is non-fatal, but *will* drop affected packets. Given that blocking on a UDP send indicates that the OS cannot clear send buffers fast enough, this should alleviate OS load.

Closes #131.
This commit is contained in:
Kyle Simpson
2022-08-01 15:54:20 +01:00
parent c1d93f790c
commit 2277595be4
27 changed files with 299 additions and 206 deletions

View File

@@ -10,7 +10,11 @@ use result::*;
use state::*;
pub use track::*;
use super::{disposal, error::Result, message::*};
use super::{
disposal,
error::{Error, Result},
message::*,
};
use crate::{
constants::*,
driver::MixMode,
@@ -26,6 +30,7 @@ use audiopus::{
Bitrate,
};
use discortp::{
discord::MutableKeepalivePacket,
rtp::{MutableRtpPacket, RtpPacket},
MutablePacket,
};
@@ -73,6 +78,9 @@ pub struct Mixer {
thread_pool: BlockyTaskPool,
pub ws: Option<Sender<WsMessage>>,
pub keepalive_deadline: Instant,
pub keepalive_packet: [u8; MutableKeepalivePacket::minimum_packet_size()],
pub tracks: Vec<InternalTrack>,
track_handles: Vec<TrackHandle>,
@@ -104,6 +112,7 @@ impl Mixer {
let soft_clip = SoftClip::new(config.mix_mode.to_opus());
let mut packet = [0u8; VOICE_PACKET_MAX];
let keepalive_packet = [0u8; MutableKeepalivePacket::minimum_packet_size()];
let mut rtp = MutableRtpPacket::new(&mut packet[..]).expect(
"FATAL: Too few bytes in self.packet for RTP header.\
@@ -146,12 +155,14 @@ impl Mixer {
SignalSpec::new_with_layout(SAMPLE_RATE_RAW as u32, Layout::Stereo),
);
let deadline = Instant::now();
Self {
bitrate,
config,
conn_active: None,
content_prep_sequence: 0,
deadline: Instant::now(),
deadline,
disposer,
encoder,
interconnect,
@@ -165,6 +176,9 @@ impl Mixer {
thread_pool,
ws: None,
keepalive_deadline: deadline,
keepalive_packet,
tracks,
track_handles,
@@ -213,7 +227,14 @@ impl Mixer {
// The above action may have invalidated the connection; need to re-check!
// Also, if we're in a test mode we should unconditionally run packet mixing code.
if self.conn_active.is_some() || ignore_check {
if let Err(e) = self.cycle().and_then(|_| self.audio_commands_events()) {
if let Err(e) = self
.cycle()
.and_then(|_| self.audio_commands_events())
.and_then(|_| {
self.check_and_send_keepalive()
.or_else(Error::disarm_would_block)
})
{
events_failure |= e.should_trigger_interconnect_rebuild();
conn_failure |= e.should_trigger_connect();
@@ -313,6 +334,11 @@ impl Mixer {
rtp.set_sequence(random::<u16>().into());
rtp.set_timestamp(random::<u32>().into());
self.deadline = Instant::now();
let mut ka = MutableKeepalivePacket::new(&mut self.keepalive_packet[..])
.expect("FATAL: Insufficient bytes given to keepalive packet.");
ka.set_ssrc(ssrc);
self.keepalive_deadline = self.deadline + UDP_KEEPALIVE_GAP;
Ok(())
},
MixerMessage::DropConn => {
@@ -321,9 +347,12 @@ impl Mixer {
},
MixerMessage::ReplaceInterconnect(i) => {
self.prevent_events = false;
if let Some(ws) = &self.ws {
conn_failure |= ws.send(WsMessage::ReplaceInterconnect(i.clone())).is_err();
}
#[cfg(feature = "receive")]
if let Some(conn) = &self.conn_active {
conn_failure |= conn
.udp_rx
@@ -357,13 +386,19 @@ impl Mixer {
);
}
self.config = Arc::new(new_config.clone());
self.config = Arc::new(
#[cfg(feature = "receive")]
new_config.clone(),
#[cfg(not(feature = "receive"))]
new_config,
);
if self.tracks.capacity() < self.config.preallocated_tracks {
self.tracks
.reserve(self.config.preallocated_tracks - self.tracks.len());
}
#[cfg(feature = "receive")]
if let Some(conn) = &self.conn_active {
conn_failure |= conn
.udp_rx
@@ -674,7 +709,7 @@ impl Mixer {
let send_buffer = self.config.use_softclip.then(|| &softclip_buffer[..]);
#[cfg(test)]
if let Some(OutputMode::Raw(tx)) = &self.config.override_connection {
let send_status = if let Some(OutputMode::Raw(tx)) = &self.config.override_connection {
let msg = match mix_len {
MixType::Passthrough(len) if len == SILENT_FRAME.len() => OutputMessage::Silent,
MixType::Passthrough(len) => {
@@ -693,12 +728,18 @@ impl Mixer {
};
drop(tx.send(msg.into()));
Ok(())
} else {
self.prep_and_send_packet(send_buffer, mix_len)?;
}
self.prep_and_send_packet(send_buffer, mix_len)
};
#[cfg(not(test))]
self.prep_and_send_packet(send_buffer, mix_len)?;
let send_status = self.prep_and_send_packet(send_buffer, mix_len);
send_status.or_else(Error::disarm_would_block)?;
self.advance_rtp_counters();
// Zero out all planes of the mix buffer if any audio was written.
if matches!(mix_len, MixType::MixedPcm(a) if a > 0) {
@@ -770,25 +811,36 @@ impl Mixer {
// Test mode: send unencrypted (compressed) packets to local receiver.
drop(tx.send(self.packet[..index].to_vec().into()));
} else {
conn.udp_tx.send(self.packet[..index].to_vec())?;
conn.udp_tx.send(&self.packet[..index])?;
}
#[cfg(not(test))]
{
// Normal operation: send encrypted payload to UDP Tx task.
// TODO: This is dog slow, don't do this.
// Can we replace this with a shared ring buffer + semaphore?
// or the BBQueue crate?
conn.udp_tx.send(self.packet[..index].to_vec())?;
conn.udp_tx.send(&self.packet[..index])?;
}
Ok(())
}
#[inline]
fn advance_rtp_counters(&mut self) {
let mut rtp = MutableRtpPacket::new(&mut self.packet[..]).expect(
"FATAL: Too few bytes in self.packet for RTP header.\
(Blame: VOICE_PACKET_MAX?)",
);
rtp.set_sequence(rtp.get_sequence() + 1);
rtp.set_timestamp(rtp.get_timestamp() + MONO_FRAME_SIZE as u32);
}
#[inline]
fn check_and_send_keepalive(&mut self) -> Result<()> {
if let Some(conn) = self.conn_active.as_mut() {
if Instant::now() >= self.keepalive_deadline {
conn.udp_tx.send(&self.keepalive_packet)?;
self.keepalive_deadline += UDP_KEEPALIVE_GAP;
}
}
Ok(())
}