Skip to content

Commit

Permalink
fix: use the crypto_mode determined by negotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
tignear committed Nov 11, 2024
1 parent a8657b7 commit fc13d65
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/driver/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl Connection {
cipher: cipher.clone(),
#[cfg(not(feature = "receive"))]
cipher,
crypto_state: config.crypto_mode.into(),
crypto_state: chosen_crypto.into(),
#[cfg(feature = "receive")]
udp_rx: udp_receiver_msg_tx,
udp_tx,
Expand Down Expand Up @@ -244,6 +244,7 @@ impl Connection {
interconnect.clone(),
udp_receiver_msg_rx,
cipher,
chosen_crypto,
config.clone(),
udp_rx,
ssrc_tracker,
Expand Down
17 changes: 13 additions & 4 deletions src/driver/tasks/mixer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use super::{
};
use crate::{
constants::*,
driver::MixMode,
driver::{CryptoMode, MixMode},
events::EventStore,
input::{Input, Parsed},
tracks::{Action, LoopState, PlayError, PlayMode, TrackCommand, TrackHandle, TrackState, View},
Expand Down Expand Up @@ -510,6 +510,15 @@ impl Mixer {
}
}

pub fn crypto_mode(&self) -> CryptoMode {
let mode = self.conn_active.as_ref().map(|v| v.crypto_state.kind());
if cfg!(not(test)) {
mode.expect("Shouldn't be mixing packets without access to a cipher + UDP dest.")
} else {
mode.unwrap_or_else(|| self.config.crypto_mode)
}
}

#[inline]
pub fn mix_and_build_packet(&mut self, packet: &mut [u8]) -> Result<usize> {
// symph_mix is an `AudioBuffer` (planar format), we need to convert this
Expand Down Expand Up @@ -544,7 +553,7 @@ impl Mixer {
);

let payload = rtp.payload_mut();
let pre_len = self.config.crypto_mode.payload_prefix_len2();
let pre_len = self.crypto_mode().payload_prefix_len2();

payload[pre_len..pre_len + SILENT_FRAME.len()].copy_from_slice(&SILENT_FRAME[..]);

Expand Down Expand Up @@ -581,7 +590,7 @@ impl Mixer {
);
let payload = rtp.payload();
let opus_frame =
(payload[self.config.crypto_mode.payload_prefix_len2()..][..len]).to_vec();
(payload[self.crypto_mode().payload_prefix_len2()..][..len]).to_vec();

OutputMessage::Passthrough(opus_frame)
},
Expand Down Expand Up @@ -745,7 +754,7 @@ impl Mixer {
(Blame: VOICE_PACKET_MAX?)",
);
let payload = rtp.payload_mut();
let opus_frame = &mut payload[self.config.crypto_mode.payload_prefix_len2()..];
let opus_frame = &mut payload[self.crypto_mode().payload_prefix_len2()..];

// Opus frame passthrough.
// This requires that we have only one PLAYING track, who has volume 1.0, and an
Expand Down
8 changes: 6 additions & 2 deletions src/driver/tasks/udp_rx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod ssrc_state;
use self::{decode_sizes::*, playout_buffer::*, ssrc_state::*};

use super::message::*;
use crate::driver::CryptoMode;
use crate::{
constants::*,
driver::crypto::Cipher,
Expand Down Expand Up @@ -32,6 +33,7 @@ type RtpSsrc = u32;

struct UdpRx {
cipher: Cipher,
crypto_mode: CryptoMode,
decoder_map: HashMap<RtpSsrc, SsrcState>,
config: Config,
rx: Receiver<UdpRxMessage>,
Expand Down Expand Up @@ -142,7 +144,7 @@ impl UdpRx {
// For simplicity, if the event task fails then we nominate the mixing thread
// to rebuild their context etc. (hence, the `let _ =` statements.), as it will
// try to make contact every 20ms.
let crypto_mode = self.config.crypto_mode;
let crypto_mode = self.crypto_mode;

match demux::demux_mut(packet.as_mut()) {
DemuxedMut::Rtp(mut rtp) => {
Expand Down Expand Up @@ -178,7 +180,7 @@ impl UdpRx {
let entry = self
.decoder_map
.entry(rtp.get_ssrc())
.or_insert_with(|| SsrcState::new(&rtp, &self.config));
.or_insert_with(|| SsrcState::new(&rtp, crypto_mode, &self.config));

// Only do this on RTP, rather than RTCP -- this pins decoder state liveness
// to *speech* rather than just presence.
Expand Down Expand Up @@ -242,6 +244,7 @@ pub(crate) async fn runner(
mut interconnect: Interconnect,
rx: Receiver<UdpRxMessage>,
cipher: Cipher,
crypto_mode: CryptoMode,
config: Config,
udp_socket: UdpSocket,
ssrc_signalling: Arc<SsrcTracker>,
Expand All @@ -250,6 +253,7 @@ pub(crate) async fn runner(

let mut state = UdpRx {
cipher,
crypto_mode,
decoder_map: HashMap::new(),
config,
rx,
Expand Down
8 changes: 5 additions & 3 deletions src/driver/tasks/udp_rx/ssrc_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ use tracing::{error, warn};
#[derive(Debug)]
pub struct SsrcState {
playout_buffer: PlayoutBuffer,
crypto_mode: CryptoMode,
decoder: OpusDecoder,
decode_size: PacketDecodeSize,
pub(crate) prune_time: Instant,
pub(crate) disconnected: bool,
}

impl SsrcState {
pub fn new(pkt: &RtpPacket<'_>, config: &Config) -> Self {
pub fn new(pkt: &RtpPacket<'_>, crypto_mode: CryptoMode, config: &Config) -> Self {
let playout_capacity = config.playout_buffer_length.get() + config.playout_spike_length;

Self {
playout_buffer: PlayoutBuffer::new(playout_capacity, pkt.get_sequence().0),
crypto_mode,
decoder: OpusDecoder::new(SAMPLE_RATE, Channels::Stereo)
.expect("Failed to create new Opus decoder for source."),
decode_size: PacketDecodeSize::TwentyMillis,
Expand Down Expand Up @@ -72,8 +74,8 @@ impl SsrcState {
let extensions = rtp.get_extension() != 0;

let payload = rtp.payload();
let payload_offset = config.crypto_mode.payload_prefix_len2();
let payload_end_pad = payload.len() - config.crypto_mode.payload_suffix_len();
let payload_offset = self.crypto_mode.payload_prefix_len2();
let payload_end_pad = payload.len() - self.crypto_mode.payload_suffix_len();

// We still need to compute missed packets here in case of long loss chains or similar.
// This occurs due to the fallback in 'store_packet' (i.e., empty buffer and massive seq difference).
Expand Down

0 comments on commit fc13d65

Please sign in to comment.