VNOX-Client/src/ui/update.rs
loki5512344 29883a3187 feat(client): TLS 1.3 control connection with SPKI pinning and LNEx v3
Replace the custom X25519/HKDF/ChaCha20 TCP layer with rustls (TLS 1.3 only,
ALPN vnox-lnex/3, no plaintext or fallback). Add a trust store (pin, trust on
first use, public CA), block changed fingerprints and identities, bind the
Ed25519 auth signature to the TLS exporter, add RU/EN trust dialog and
update the interop test and e2e voice tool.
2026-10-24 22:05:19 +02:00

952 lines
34 KiB
Rust

use std::collections::HashMap;
use vnox_client::audio::{IncomingVoice, PipelineConfig};
use vnox_client::net::{ChatMsg, NetCommand, NetEvent, NetHandle, PresenceInfo};
use crate::ui::state::{ChatMessage, ConnState, UiState, VoiceUiState};
use crate::AppState;
pub fn process_net_events(state: &mut AppState) {
while let Some(event) = state.net.try_recv() {
match event {
NetEvent::VoiceRevoked { channel_id } => {
if state.voice_channel.as_deref() == Some(channel_id.as_str())
|| state.ui.voice_target.as_deref() == Some(channel_id.as_str())
{
state.ui.voice = VoiceUiState::Off;
state.ui.voice_target = None;
state.voice_channel = None;
state.audio = None;
state.last_pipeline_config = None;
}
}
NetEvent::MediaEvent(event) => crate::ui::media::on_event(event),
NetEvent::WebhookResult(result) => {
if let Some(ui) = &state.webhooks {
ui.receive_result(&result.request_id, result.hook, result.secret);
}
}
NetEvent::WebhookList(result) => {
if let Some(ui) = &state.webhooks {
ui.receive_list(&result.request_id, result.hooks);
}
}
NetEvent::WebhookError(result) => {
if let Some(ui) = &state.webhooks {
ui.receive_error(&result.request_id, result.code);
}
}
NetEvent::Connected {
session_id,
node_name,
restored,
private_mode,
} => {
state.connection_generation = state.connection_generation.wrapping_add(1);
state.ui.conn = ConnState::Connected {
session_id,
node_name: node_name.clone(),
};
state.ui.private_mode = private_mode;
if restored {
state.ui.status_msg = Some("Reconnected".into());
let text_channel = state.ui.active_channel.clone();
let voice_channel = state.ui.voice_target.clone();
let net = state.net.clone();
tokio::spawn(async move {
if let Some(channel_id) = text_channel {
net.send(NetCommand::JoinChannel { channel_id }).await;
}
if let Some(channel_id) = voice_channel {
net.send(NetCommand::JoinChannel { channel_id }).await;
}
});
} else {
state.ui.status_msg = None;
let net = state.net.clone();
tokio::spawn(async move {
net.send(NetCommand::ChannelList).await;
net.send(NetCommand::GuildList).await;
net.send(NetCommand::FriendList).await;
net.send(NetCommand::BlockList).await;
});
}
}
NetEvent::TrustProblem(problem) => {
use crate::ui::state::{TrustPrompt, TrustPromptKind};
use vnox_client::net::TrustProblem;
state.ui.trust_prompt = Some(match problem {
TrustProblem::Untrusted {
address,
fingerprint,
} => TrustPrompt {
kind: TrustPromptKind::FirstUse,
address,
received: fingerprint,
expected: String::new(),
},
TrustProblem::FingerprintChanged {
address,
expected,
actual,
} => TrustPrompt {
kind: TrustPromptKind::FingerprintChanged,
address,
received: actual,
expected,
},
TrustProblem::IdentityChanged {
address,
expected,
actual,
} => TrustPrompt {
kind: TrustPromptKind::IdentityChanged,
address,
received: actual,
expected,
},
});
}
NetEvent::Disconnected { reason, will_retry } => {
if let Some(ui) = &state.webhooks {
ui.disconnected();
}
state.ui.voice = VoiceUiState::Off;
state.audio = None;
state.voice_channel = None;
state.last_pipeline_config = None;
if will_retry {
state.ui.status_msg = Some(crate::ui::i18n::Message::with(
"Connection lost: {reason}",
vec![("reason", reason.to_string())],
));
} else {
state.ui.conn = ConnState::Disconnected;
state.ui.voice_target = None;
state.ui.showing_voice = false;
state.ui.clear_server_conversations();
state.ui.channels.clear();
state.ui.active_channel = None;
state.ui.user_names.clear();
state.ui.rtt_ms = None;
state.ui.status_msg = if state.ui.trust_prompt.is_some() {
None
} else {
Some(crate::ui::i18n::Message::with(
"Disconnected: {reason}",
vec![("reason", reason.to_string())],
))
};
}
if !will_retry {
state.ui.dm_conversations.clear();
state.ui.dm_messages.clear();
state.ui.active_dm_id = None;
state.ui.new_dm_input = None;
state.ui.private_mode = false;
}
}
NetEvent::Reconnecting {
attempt,
delay_secs,
} => {
state.ui.voice = VoiceUiState::Off;
state.audio = None;
state.voice_channel = None;
state.last_pipeline_config = None;
state.ui.conn = ConnState::Reconnecting { attempt };
state.ui.status_msg = Some(crate::ui::i18n::Message::with(
"Reconnecting in {delay_secs}s (attempt {attempt})",
vec![
("delay_secs", delay_secs.to_string()),
("attempt", attempt.to_string()),
],
));
}
NetEvent::VoicePacket {
channel_id,
voice_seq,
timestamp,
opus_data,
sender_id,
..
} => {
if !sender_id.is_empty() {
state.ui.last_remote_speaker_id = sender_id.clone();
state.ui.last_voice_packet_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0);
}
if !state.ui.deafen
&& let Some(pipeline) = state.audio.as_ref()
{
pipeline.push_voice(IncomingVoice {
voice_seq,
timestamp,
channel_id,
opus_data,
});
}
}
NetEvent::E2eeDmKeyExchange {
dm_id,
e2ee_public_key,
} => {
let (secret, public) = vnox_client::e2ee::derive_e2ee_keypair(&state.identity);
let shared = vnox_client::e2ee::compute_shared_secret(&secret, &e2ee_public_key);
if let Some(conv) = state
.ui
.dm_conversations
.iter_mut()
.find(|c| c.dm_id == dm_id)
{
conv.e2ee_enabled = true;
conv.e2ee_peer_public_key = Some(e2ee_public_key);
conv.e2ee_shared_secret = Some(shared);
}
let pubkey_bytes = public.to_bytes().to_vec();
let net = state.net.clone();
tokio::spawn(async move {
net.send(NetCommand::E2eeDmKeyExchange {
dm_id,
e2ee_public_key: pubkey_bytes,
})
.await;
});
}
NetEvent::E2eeDmKeyExchangeAck { dm_id } => {
if let Some(conv) = state
.ui
.dm_conversations
.iter_mut()
.find(|c| c.dm_id == dm_id)
{
conv.e2ee_enabled = true;
}
}
NetEvent::E2eeDmMessage {
dm_id,
sender_id,
ciphertext,
timestamp,
} => {
let shared = state
.ui
.dm_conversations
.iter()
.find(|c| c.dm_id == dm_id)
.and_then(|c| c.e2ee_shared_secret);
let Some(shared) = shared else {
continue;
};
let msg_id = uuid::Uuid::new_v4().to_string();
let content = vnox_client::e2ee::decrypt_message(&shared, &msg_id, &ciphertext);
state
.ui
.dm_messages
.entry(dm_id.clone())
.or_default()
.push(ChatMessage {
message_id: msg_id,
sender_id,
content,
timestamp,
edited: false,
reactions: HashMap::new(),
reply_to: None,
});
if state.ui.active_dm_id.as_deref() != Some(&dm_id)
&& let Some(c) = state
.ui
.dm_conversations
.iter_mut()
.find(|c| c.dm_id == dm_id)
{
c.unread_count += 1;
}
}
NetEvent::E2eeDmHistory { dm_id, messages } => {
let shared = state
.ui
.dm_conversations
.iter()
.find(|c| c.dm_id == dm_id)
.and_then(|c| c.e2ee_shared_secret);
let Some(shared) = shared else {
continue;
};
let decrypted: Vec<ChatMessage> = messages
.into_iter()
.map(|m| {
let msg_id = uuid::Uuid::new_v4().to_string();
let content =
vnox_client::e2ee::decrypt_message(&shared, &msg_id, &m.ciphertext);
ChatMessage {
message_id: msg_id,
sender_id: m.sender_id,
content,
timestamp: m.timestamp,
edited: false,
reactions: HashMap::new(),
reply_to: None,
}
})
.collect();
state.ui.dm_messages.insert(dm_id, decrypted);
}
event => process_net_event(&mut state.ui, &state.net, event),
}
}
// Capture/playback runs independently from incoming control packets.
sync_voice_audio(state);
flush_encoded_voice(state);
if let Some(pipeline) = state.audio.as_ref() {
pipeline.sync_controls(vnox_client::audio::AudioSync {
mic_enabled: state.ui.mic_enabled && !state.ui.deafen,
vad_mode: state.ui.vad_mode,
vad_threshold: 40.0,
input_volume: 100.0,
ptt_held: state.ui.ptt_held,
output_volume: if state.ui.deafen {
0.0
} else {
state.ui.output_volume
},
jitter_size: 1,
adaptive_buffer: true,
voice_bitrate_kbps: state.ui.voice_bitrate,
noise_suppress: false,
});
state.ui.local_speaking = pipeline.local_speaking();
state.ui.remote_speaking = pipeline.remote_speaking(500);
} else {
state.ui.local_speaking = false;
state.ui.remote_speaking = false;
}
if !state.ui.remote_speaking {
state.ui.last_remote_speaker_id.clear();
}
}
fn process_net_event(ui: &mut UiState, net: &NetHandle, event: NetEvent) {
match &event {
NetEvent::ChannelState { .. }
| NetEvent::ChannelCreated { .. }
| NetEvent::ChannelDeleted { .. }
| NetEvent::ChannelListEvent { .. }
| NetEvent::ChatMessage { .. }
| NetEvent::ChatHistory { .. }
| NetEvent::UserJoin { .. }
| NetEvent::UserLeave { .. }
| NetEvent::Error { .. }
| NetEvent::ReactionAdded { .. }
| NetEvent::ReactionRemoved { .. }
| NetEvent::MessageEdited { .. }
| NetEvent::MessageDeleted { .. }
| NetEvent::DmStart { .. }
| NetEvent::DmMessage { .. }
| NetEvent::DmHistory { .. }
| NetEvent::ReadReceiptBroadcast { .. }
| NetEvent::UserColor { .. } => chat_handle(ui, net, event),
NetEvent::GuildList { .. }
| NetEvent::GuildCreated { .. }
| NetEvent::GuildDeleted { .. }
| NetEvent::GuildMemberJoined { .. }
| NetEvent::GuildMemberLeft { .. }
| NetEvent::GuildMemberKicked { .. }
| NetEvent::InviteCreated { .. }
| NetEvent::InviteAccepted { .. }
| NetEvent::InviteDeleted { .. }
| NetEvent::RoleCreated { .. }
| NetEvent::RoleDeleted { .. }
| NetEvent::GuildAuditLog { .. }
| NetEvent::GuildMemberList { .. }
| NetEvent::GuildRoleList { .. }
| NetEvent::FriendList { .. }
| NetEvent::FriendRequested { .. }
| NetEvent::FriendAccepted { .. }
| NetEvent::FriendRemoved { .. }
| NetEvent::BlockList { .. }
| NetEvent::BlockedUser { .. }
| NetEvent::UnblockedUser { .. } => social_handle(ui, event),
NetEvent::LatencyUpdate { .. }
| NetEvent::TypingStart { .. }
| NetEvent::PresenceSync { .. }
| NetEvent::PresenceUpdated { .. } => status_handle(ui, net, event),
NetEvent::VoiceRevoked { .. }
| NetEvent::MediaEvent(_)
| NetEvent::WebhookResult(_)
| NetEvent::WebhookList(_)
| NetEvent::WebhookError(_) => unreachable!("handled before generic UI dispatch"),
NetEvent::Connected { .. }
| NetEvent::Disconnected { .. }
| NetEvent::TrustProblem(_)
| NetEvent::Reconnecting { .. }
| NetEvent::VoicePacket { .. }
| NetEvent::E2eeDmKeyExchange { .. }
| NetEvent::E2eeDmKeyExchangeAck { .. }
| NetEvent::E2eeDmMessage { .. }
| NetEvent::E2eeDmHistory { .. } => {}
}
}
fn into_msg(m: ChatMsg) -> ChatMessage {
ChatMessage {
message_id: m.message_id,
sender_id: m.sender_id,
content: m.content,
timestamp: m.timestamp,
edited: false,
reactions: HashMap::new(),
reply_to: m.reply_to,
}
}
fn parse_hex_color(hex: &str) -> Option<String> {
let hex = hex.trim_start_matches('#');
if hex.len() != 6 {
return None;
}
u32::from_str_radix(hex, 16).ok()?;
Some(hex.to_string())
}
fn chat_handle(ui: &mut UiState, net: &NetHandle, event: NetEvent) {
match event {
NetEvent::ChannelState {
channel_id,
channel_name,
kind,
members,
guild_id,
..
} => {
let mems: Vec<String> = members
.iter()
.map(|m| {
ui.user_names.insert(m.user_id.clone(), m.nickname.clone());
m.nickname.clone()
})
.collect();
if let Some(ch) = ui.channels.iter_mut().find(|c| c.id == channel_id) {
ch.members = mems;
if guild_id.is_some() {
ch.guild_id = guild_id.clone();
}
} else {
ui.channels.push(crate::ui::state::Channel {
id: channel_id.clone(),
name: channel_name,
kind: kind.clone(),
guild_id: guild_id.clone(),
members: mems,
});
}
if kind == "voice" {
ui.voice_target = Some(channel_id.clone());
// A newly acknowledged join is also an explicit retry after an audio error.
if matches!(ui.voice, VoiceUiState::Error(_)) {
ui.voice = VoiceUiState::Off;
}
} else if ui.active_channel.is_none() && ui.active_dm_id.is_none() {
ui.select_text_channel(channel_id.clone());
}
let net = net.clone();
let ch_id = channel_id;
tokio::spawn(async move {
net.send(NetCommand::ReadReceipt {
channel_id: ch_id,
last_read_message_id: uuid::Uuid::new_v4().to_string(),
})
.await;
});
}
NetEvent::ChannelCreated {
channel_id,
channel_name,
kind,
guild_id,
} => {
if !ui.channels.iter().any(|c| c.id == channel_id) {
ui.channels.push(crate::ui::state::Channel {
id: channel_id,
name: channel_name,
kind,
guild_id,
members: Vec::new(),
});
}
}
NetEvent::ChannelDeleted { channel_id } => {
ui.channels.retain(|c| c.id != channel_id);
if ui.active_channel.as_deref() == Some(&channel_id) {
ui.active_channel = ui.channels.first().map(|c| c.id.clone());
}
ui.messages.remove(&channel_id);
if ui.voice_target.as_deref() == Some(&channel_id) {
ui.voice_target = None;
ui.showing_voice = false;
}
}
NetEvent::ChannelListEvent { channels } => {
let old: std::collections::HashMap<String, crate::ui::state::Channel> =
std::mem::take(&mut ui.channels)
.into_iter()
.map(|c| (c.id.clone(), c))
.collect();
ui.channels = channels
.into_iter()
.map(|c| {
let members = old
.get(&c.channel_id)
.map(|o| o.members.clone())
.unwrap_or_default();
crate::ui::state::Channel {
id: c.channel_id,
name: c.channel_name,
kind: c.kind,
guild_id: c.guild_id,
members,
}
})
.collect();
if ui.active_channel.is_none()
&& ui.active_dm_id.is_none()
&& !ui.browsing_dms
&& let Some(id) = ui
.channels
.iter()
.find(|c| c.kind == "text")
.map(|c| c.id.clone())
{
ui.select_text_channel(id.clone());
let net = net.clone();
tokio::spawn(async move {
net.send(NetCommand::JoinChannel { channel_id: id }).await;
});
}
}
NetEvent::UserJoin {
channel_id,
user_id,
nickname,
..
} => {
ui.user_names.insert(user_id, nickname.clone());
if let Some(ch) = ui.channels.iter_mut().find(|c| c.id == channel_id)
&& !ch.members.contains(&nickname)
{
ch.members.push(nickname);
}
}
NetEvent::UserLeave {
channel_id,
user_id,
} => {
if let Some(ch) = ui.channels.iter_mut().find(|c| c.id == channel_id) {
let nickname = ui.user_names.remove(&user_id);
ch.members.retain(|m| {
m != &user_id && nickname.as_deref().map(|n| m != n).unwrap_or(true)
});
}
}
NetEvent::DmStart {
dm_id,
other_nickname,
messages,
unread_count,
..
} => {
if !ui.dm_conversations.iter().any(|c| c.dm_id == dm_id) {
ui.dm_conversations.push(crate::ui::state::DmConversation {
dm_id: dm_id.clone(),
other_nickname,
unread_count,
e2ee_enabled: false,
e2ee_peer_public_key: None,
e2ee_shared_secret: None,
});
}
ui.dm_messages
.insert(dm_id.clone(), messages.into_iter().map(into_msg).collect());
ui.select_dm(dm_id);
}
NetEvent::ChatMessage {
message_id,
channel_id,
sender_id,
content,
timestamp,
reply_to,
..
} => {
ui.messages
.entry(channel_id)
.or_default()
.push(ChatMessage {
message_id,
sender_id,
content,
timestamp,
edited: false,
reactions: HashMap::new(),
reply_to,
});
ui.scroll_bottom = true;
}
NetEvent::ChatHistory {
channel_id,
messages,
} => {
ui.messages
.insert(channel_id, messages.into_iter().map(into_msg).collect());
ui.scroll_bottom = true;
}
NetEvent::Error { message, .. } => {
if message.starts_with("media:") {
crate::ui::media::on_error(&message);
}
ui.status_msg = Some(crate::ui::i18n::Message::with(
"Server error: {message}",
vec![("message", message.to_string())],
));
}
NetEvent::ReactionAdded {
channel_id,
message_id,
user_id,
emoji,
} => {
if let Some(msgs) = ui.messages.get_mut(&channel_id)
&& let Some(msg) = msgs.iter_mut().find(|m| m.message_id == message_id)
{
msg.reactions.entry(emoji).or_default().push(user_id);
}
}
NetEvent::ReactionRemoved {
channel_id,
message_id,
user_id,
emoji,
} => {
if let Some(msgs) = ui.messages.get_mut(&channel_id)
&& let Some(msg) = msgs.iter_mut().find(|m| m.message_id == message_id)
&& let Some(users) = msg.reactions.get_mut(&emoji)
{
users.retain(|u| u != &user_id);
if users.is_empty() {
msg.reactions.remove(&emoji);
}
}
}
NetEvent::MessageEdited {
channel_id,
message_id,
content,
..
} => {
if let Some(msgs) = ui.messages.get_mut(&channel_id)
&& let Some(msg) = msgs.iter_mut().find(|m| m.message_id == message_id)
{
msg.content = content;
msg.edited = true;
}
}
NetEvent::MessageDeleted {
channel_id,
message_id,
} => {
if let Some(msgs) = ui.messages.get_mut(&channel_id) {
msgs.retain(|m| m.message_id != message_id);
}
}
NetEvent::DmMessage {
dm_id,
sender_id,
content,
timestamp,
..
} => {
ui.dm_messages
.entry(dm_id.clone())
.or_default()
.push(ChatMessage {
message_id: uuid::Uuid::new_v4().to_string(),
sender_id,
content,
timestamp,
edited: false,
reactions: HashMap::new(),
reply_to: None,
});
if ui.active_dm_id.as_deref() != Some(&dm_id)
&& let Some(c) = ui.dm_conversations.iter_mut().find(|c| c.dm_id == dm_id)
{
c.unread_count += 1;
}
}
NetEvent::DmHistory { dm_id, messages } => {
ui.dm_messages
.insert(dm_id, messages.into_iter().map(into_msg).collect());
}
NetEvent::ReadReceiptBroadcast {
channel_id,
user_id,
last_read_message_id,
} => {
ui.read_receipts
.entry(channel_id)
.or_default()
.insert(user_id, last_read_message_id);
}
NetEvent::UserColor { user_id, color } => {
if let Some(c) = parse_hex_color(&color) {
ui.user_colors.insert(user_id, c);
}
}
_ => {}
}
}
fn social_handle(ui: &mut UiState, event: NetEvent) {
match event {
NetEvent::GuildAuditLog { .. } => {}
NetEvent::GuildMemberList { .. } => {}
NetEvent::GuildRoleList { .. } => {}
NetEvent::BlockList { blocked } => {
ui.blocked_users = blocked;
}
NetEvent::BlockedUser { user_id } => {
if !ui.blocked_users.contains(&user_id) {
ui.blocked_users.push(user_id);
}
}
NetEvent::UnblockedUser { user_id } => {
ui.blocked_users.retain(|u| u != &user_id);
}
NetEvent::GuildList { guilds } => {
ui.guilds = guilds
.into_iter()
.map(|g| crate::ui::state::GuildState {
guild_id: g.guild_id,
name: g.name,
member_count: g.member_count,
})
.collect();
}
NetEvent::GuildCreated { guild_id, name, .. } => {
ui.guilds.push(crate::ui::state::GuildState {
guild_id: guild_id.clone(),
name,
member_count: 1,
});
ui.active_guild_id = Some(guild_id);
}
NetEvent::GuildDeleted { guild_id } => {
ui.guilds.retain(|g| g.guild_id != guild_id);
if ui.active_guild_id.as_deref() == Some(&guild_id) {
ui.active_guild_id = None;
}
}
NetEvent::GuildMemberJoined {
guild_id,
user_id: _,
nickname: _,
} => {
if let Some(g) = ui.guilds.iter_mut().find(|g| g.guild_id == guild_id) {
g.member_count += 1;
}
}
NetEvent::GuildMemberLeft {
guild_id,
user_id: _,
} => {
if let Some(g) = ui.guilds.iter_mut().find(|g| g.guild_id == guild_id) {
g.member_count = g.member_count.saturating_sub(1);
}
}
NetEvent::GuildMemberKicked {
guild_id,
user_id: _,
} => {
if let Some(g) = ui.guilds.iter_mut().find(|g| g.guild_id == guild_id) {
g.member_count = g.member_count.saturating_sub(1);
}
}
NetEvent::InviteCreated { .. } => {}
NetEvent::InviteAccepted {
guild_id,
guild_name,
} if !ui.guilds.iter().any(|g| g.guild_id == guild_id) => {
ui.guilds.push(crate::ui::state::GuildState {
guild_id,
name: guild_name,
member_count: 1,
});
}
NetEvent::InviteDeleted { .. } => {}
NetEvent::RoleCreated { .. } => {}
NetEvent::RoleDeleted { .. } => {}
NetEvent::FriendList { friends } => {
ui.friends = friends
.into_iter()
.map(|f| crate::ui::state::FriendState {
user_id: f.user_id,
nickname: f.nickname,
})
.collect();
}
NetEvent::FriendRequested { user_id, nickname } => {
ui.pending_friend_requests
.push(crate::ui::state::FriendState { user_id, nickname });
}
NetEvent::FriendAccepted { user_id, nickname } => {
ui.pending_friend_requests.retain(|f| f.user_id != user_id);
if !ui.friends.iter().any(|f| f.user_id == user_id) {
ui.friends
.push(crate::ui::state::FriendState { user_id, nickname });
}
}
NetEvent::FriendRemoved { user_id } => {
ui.friends.retain(|f| f.user_id != user_id);
ui.pending_friend_requests.retain(|f| f.user_id != user_id);
}
_ => {}
}
}
fn status_handle(ui: &mut UiState, _net: &NetHandle, event: NetEvent) {
match event {
NetEvent::LatencyUpdate { rtt_ms } => {
ui.rtt_ms = Some(rtt_ms);
}
NetEvent::TypingStart {
user_id,
nickname,
channel_id,
} => {
let entry = ui.typing_users.entry(channel_id).or_default();
entry.retain(|(u, _)| u != &user_id);
entry.push((nickname, std::time::Instant::now()));
}
NetEvent::PresenceSync { presences } => {
for p in presences {
ui.presences.insert(p.user_id.clone(), p);
}
}
NetEvent::PresenceUpdated {
user_id,
status,
custom_status,
activity,
} => {
ui.presences.insert(
user_id.clone(),
PresenceInfo {
user_id,
status,
custom_status,
activity,
activity_text: None,
},
);
}
_ => {}
}
}
pub fn pipeline_config(channel_id: &str, state: &AppState) -> PipelineConfig {
PipelineConfig {
channel_key: vnox_client::net::voice::channel_key(channel_id),
mic_device_index: state.ui.mic_device,
output_device_index: state.ui.output_device,
voice_bitrate_kbps: state.ui.voice_bitrate,
packet_interval: state.ui.packet_interval,
}
}
pub fn start_voice(state: &mut AppState, channel_id: &str) {
let cfg = pipeline_config(channel_id, state);
if matches!(state.ui.voice, VoiceUiState::Error(_))
&& state.last_pipeline_config.as_ref() == Some(&cfg)
{
return;
}
if state.voice_channel.as_deref() == Some(channel_id)
&& state.audio.is_some()
&& state.last_pipeline_config.as_ref() == Some(&cfg)
{
return;
}
state.audio = None;
match vnox_client::audio::start(cfg.clone()) {
Ok(pipeline) => {
tracing::info!("voice active on channel {channel_id}");
state.audio = Some(pipeline);
state.voice_channel = Some(channel_id.to_string());
state.last_pipeline_config = Some(cfg);
state.ui.voice = VoiceUiState::Active {
channel: channel_id.to_string(),
};
if state.ui.voice_joined_at.is_none() {
state.ui.voice_joined_at = Some(std::time::Instant::now());
}
state.ui.status_msg = None;
}
Err(e) => {
tracing::warn!("audio start failed: {e}");
state.voice_channel = None;
state.last_pipeline_config = Some(cfg);
state.ui.voice = VoiceUiState::Error(e.to_string());
state.ui.status_msg = Some(crate::ui::i18n::Message::with(
"Microphone error: {e}",
vec![("e", e.to_string())],
));
}
}
}
pub fn stop_voice(state: &mut AppState) {
state.audio = None;
state.voice_channel = None;
state.last_pipeline_config = None;
state.ui.voice = VoiceUiState::Off;
state.ui.voice_joined_at = None;
}
pub fn sync_voice_audio(state: &mut AppState) {
if !matches!(state.ui.conn, ConnState::Connected { .. }) {
stop_voice(state);
return;
}
let active_voice = state.ui.voice_target.clone();
match active_voice {
Some(id) => start_voice(state, &id),
None => stop_voice(state),
}
}
pub fn flush_encoded_voice(state: &mut AppState) {
let Some(pipeline) = state.audio.as_ref() else {
return;
};
while let Some(frame) = pipeline.poll_encoded() {
let net = state.net.clone();
tokio::spawn(async move {
net.send(NetCommand::SendVoice {
channel_id: frame.channel_id,
voice_seq: frame.voice_seq,
timestamp: frame.timestamp,
opus_data: frame.data,
})
.await;
});
}
}