Move voice packet types into voelin-audio
voelin-audio no longer depends on tsclientlib or tsproto-packets. Its boundary is now plain types in packet.rs: TalkerId, VoiceCodec, VoicePacket (talker, seq, codec, whisper, payload) and EncodedVoice (codec, payload; an empty payload ends the transmission). The jitter buffer moves from tsclientlib::audio into jitter.rs and reads VoicePacket fields; the encoder returns EncodedVoice and the mixer is keyed by TalkerId. In voelin-core, voice.rs is the only place that wraps EncodedVoice into a TeamSpeak audio packet and reads one into a VoicePacket (codecs other than Opus are dropped there, where the jitter buffer rejected them before); audio.rs and session.rs work on the new types. voelinctl converts at its own edge. Behaviour is unchanged: the one-hour drift simulation gives the same latencies as before. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1bee9c94e8
commit
df7be67d4c
15 files changed
+1352
-227
No files matched your search
@@ -4,16 +4,43 @@ use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use tokio::time::Instant;
|
||||
use tsclientlib::{ClientId, Connection, StreamItem};
|
||||
use tsproto_packets::packets::AudioData;
|
||||
use tsclientlib::{Connection, StreamItem};
|
||||
use tsproto_packets::packets::{AudioData, CodecType, InAudioBuf, OutAudio, OutPacket};
|
||||
use voelin_audio::pcm::{self, FRAME_SAMPLES, SAMPLE_RATE};
|
||||
use voelin_audio::{Framer, Mixer, VoiceCodec, VoiceEncoder, resample, wav};
|
||||
use voelin_audio::{
|
||||
EncodedVoice, Framer, Mixer, TalkerId, VoiceCodec, VoiceEncoder, VoicePacket, resample, wav,
|
||||
};
|
||||
|
||||
use crate::VoiceCommand;
|
||||
use crate::session::{Flow, PumpEnd, pump};
|
||||
|
||||
const FRAME: Duration = Duration::from_millis(20);
|
||||
|
||||
/// A voice frame as a TeamSpeak audio packet; the connection numbers it.
|
||||
fn out_audio(voice: &EncodedVoice) -> OutPacket {
|
||||
let codec = match voice.codec {
|
||||
VoiceCodec::Voice => CodecType::OpusVoice,
|
||||
VoiceCodec::Music => CodecType::OpusMusic,
|
||||
};
|
||||
OutAudio::new(&AudioData::C2S { id: 0, codec, data: &voice.payload })
|
||||
}
|
||||
|
||||
/// A received TeamSpeak audio packet for the mixer; `None` for anything but
|
||||
/// Opus voice.
|
||||
fn voice_packet(packet: &InAudioBuf) -> Option<VoicePacket> {
|
||||
let (seq, from, codec, data, whisper) = match packet.data().data() {
|
||||
AudioData::S2C { id, from, codec, data } => (*id, *from, *codec, *data, false),
|
||||
AudioData::S2CWhisper { id, from, codec, data } => (*id, *from, *codec, *data, true),
|
||||
_ => return None,
|
||||
};
|
||||
let codec = match codec {
|
||||
CodecType::OpusVoice => VoiceCodec::Voice,
|
||||
CodecType::OpusMusic => VoiceCodec::Music,
|
||||
_ => return None,
|
||||
};
|
||||
Some(VoicePacket { talker: TalkerId(from), seq, codec, whisper, payload: data.to_vec() })
|
||||
}
|
||||
|
||||
pub async fn run(con: &mut Connection, command: &VoiceCommand) -> Result<()> {
|
||||
match command {
|
||||
VoiceCommand::Send { file, tone, seconds, music } => {
|
||||
@@ -68,13 +95,13 @@ async fn send(con: &mut Connection, codec: VoiceCodec, samples: &[f32]) -> Resul
|
||||
|
||||
let mut next = Instant::now();
|
||||
for frame in &frames {
|
||||
con.send_audio(encoder.encode(frame)?)?;
|
||||
con.send_audio(out_audio(&encoder.encode(frame)?))?;
|
||||
next += FRAME;
|
||||
if pump(con, Some(next), |_, _| Ok(Flow::Continue)).await? == PumpEnd::Interrupted {
|
||||
break;
|
||||
}
|
||||
}
|
||||
con.send_audio(encoder.end_of_stream())?;
|
||||
con.send_audio(out_audio(&encoder.end_of_stream()))?;
|
||||
// Let the last packets go out.
|
||||
pump(con, Some(Instant::now() + Duration::from_millis(200)), |_, _| Ok(Flow::Continue)).await?;
|
||||
println!("sent {} frames ({:.1} s)", frames.len(), frames.len() as f32 * 0.02);
|
||||
@@ -89,13 +116,11 @@ async fn record(con: &mut Connection, length: Duration) -> Result<Vec<f32>> {
|
||||
let mut next = Instant::now() + FRAME;
|
||||
while next <= end {
|
||||
let end = pump(con, Some(next), |_, item| {
|
||||
if let StreamItem::Audio(packet) = item {
|
||||
let from = match packet.data().data() {
|
||||
AudioData::S2C { from, .. } | AudioData::S2CWhisper { from, .. } => *from,
|
||||
_ => return Ok(Flow::Continue),
|
||||
};
|
||||
if let StreamItem::Audio(packet) = item
|
||||
&& let Some(packet) = voice_packet(&packet)
|
||||
{
|
||||
// Late or duplicate packets are expected on UDP; the mixer drops them.
|
||||
let _ = mixer.handle_packet(ClientId(from), packet);
|
||||
let _ = mixer.handle_packet(packet);
|
||||
}
|
||||
Ok(Flow::Continue)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user