feat(audio,bridge,flutter): v1 audio + PTT lifecycle implementation (SDD-094..097)
Implement the SDD-094 / SDD-095 / SDD-096 / SDD-097 detailed designs
committed in dfa84ee.
Rust side
- chanora_audio::TransmitMode enum (Ptt/Continuous/VoiceActivity) with
serde-friendly u8 repr (SDD-095).
- chanora_audio::TransmitModeSelector: lock-free Atomic-backed selector
that is the sole writer of transmit_active (per SAD-083), applying
hard_mute as a final clamp. VoiceActivity falls through to Continuous
for v1 (DEC-030 placeholder).
- chanora_audio::ReleaseTailTimer: tokio-task-owning struct driving the
selector's ptt_held input; default 200 ms tail, configurable 0–500 ms
with AtomicU32 hot read; pending JoinHandle held in a std::sync::Mutex
touched only on PTT edge transitions (SDD-096).
- chanora_storage: AudioMeta persisted as audio_meta.json next to
identity.dek; get/set_transmit_mode + get/set_release_tail_ms with
0..=500 clamp on write.
- chanora_core::ChanoraSession: voice_join(channel, password) and
voice_leave() are the new lifecycle entry points; ensure_audio_running
and shutdown_audio_if_idle are private helpers around the existing
Option<AudioEngine> field. SessionEvent::VoiceState carries the
in_channel / transmit_mode / mute / release_tail_ms tuple. Selector
state survives reconnect; supervisor rewires it to each fresh engine
gate.
- chanora_bridge: drop start_audio; add voice_join, voice_leave,
set/get_transmit_mode, set/get_release_tail_ms, set_hard_mute.
BridgeEvent::VoiceState mirrors the core event. AudioStarted/Stopped
kept for backwards compat but Flutter ignores them in the new UI.
Flutter side
- New apps/chanora_flutter/lib/widgets/voice_bar.dart replaces the
legacy _AudioControls widget. Renders channel pill, mode badge,
mute toggle, level meter, PttCapabilityBadge, leave button. No
manual Start affordance anywhere.
- New apps/chanora_flutter/lib/widgets/voice_settings.dart dialog with
TransmitMode radio group (VoiceActivity disabled with 'Coming soon'
trailing label per DEC-030), bind-key button, release-tail slider
0–500 ms step 25.
- main.dart: state fields _inChannel, _transmitMode, _hardMute,
_releaseTailMs driven by BridgeEvent_VoiceState. Channel-tap now
calls voiceJoin instead of moveToChannel. Removed _onStartAudio,
_audioStarted-gated branch, and the FilledButton.
- l10n: 11 new strings in app_en.arb + app_zh.arb.
Verification
- cargo check --workspace: clean.
- cargo test --workspace --lib: 72 passed / 0 failed / 1 ignored
(chanora_audio: +12 new tests for TransmitMode/Selector/ReleaseTail;
chanora_storage: +2 new tests for audio_meta round-trip).
- flutter analyze: 0 errors, 0 warnings; 6 infos are the Flutter 3.32
Radio.groupValue deprecation (pre-existing API usage).
- FRB Dart/Rust bindings regenerated via flutter_rust_bridge_codegen.
Follow-up (intentionally deferred)
- PttController and per-platform PTT backends still drive AudioTransmitGate
directly via the legacy set_ptt path; routing those key edges through
ChanoraSession::release_tail_timer().{key_down,key_up} so the tail
applies to native PTT input is a contained wiring change in a follow-up.
- Real audio-level RMS in BridgeAudioStats (current meter is binary).
- VoiceActivity backend (DEC-030).
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
//! Cross-platform transmit-mode selector (SAD-083).
|
||||
//!
|
||||
//! Single writer of `transmit_active` other than the missed-key-up
|
||||
//! watchdog (SAD-079). Computes the desired gate state from four
|
||||
//! lock-free inputs:
|
||||
//!
|
||||
//! * `mode` — current [`TransmitMode`]
|
||||
//! * `in_channel` — true when the session is in a voice channel
|
||||
//! * `hard_mute` — final clamp; forces `false` regardless of mode
|
||||
//! * `ptt_held` — raw key state (via [`crate::ReleaseTailTimer`]
|
||||
//! on PTT mode)
|
||||
//!
|
||||
//! Hard-mute is a final clamp; leaving the channel forces the gate
|
||||
//! to `false`. `VoiceActivity` is treated identically to
|
||||
//! `Continuous` per DEC-030 until a VAD implementation lands.
|
||||
//!
|
||||
//! All four inputs are stored as atomics so any thread can update
|
||||
//! them without taking a lock. After each update we call
|
||||
//! [`TransmitModeSelector::recompute`] which writes the resolved
|
||||
//! desired value through the [`AudioTransmitGate`].
|
||||
|
||||
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
||||
|
||||
use crate::ptt::AudioTransmitGate;
|
||||
use crate::transmit_mode::TransmitMode;
|
||||
|
||||
/// Selector that maps user/session state to the `transmit_active`
|
||||
/// gate (SAD-083).
|
||||
pub struct TransmitModeSelector {
|
||||
gate: std::sync::RwLock<AudioTransmitGate>,
|
||||
mode: AtomicU8,
|
||||
in_channel: AtomicBool,
|
||||
hard_mute: AtomicBool,
|
||||
ptt_held: AtomicBool,
|
||||
}
|
||||
|
||||
impl TransmitModeSelector {
|
||||
/// Construct a selector wired to `gate`. Defaults:
|
||||
/// [`TransmitMode::Ptt`], not in channel, not muted, key
|
||||
/// released. The gate's initial value is left untouched until
|
||||
/// the first mutating call (which then writes the resolved
|
||||
/// value).
|
||||
pub fn new(gate: AudioTransmitGate) -> Self {
|
||||
Self {
|
||||
gate: std::sync::RwLock::new(gate),
|
||||
mode: AtomicU8::new(TransmitMode::default().as_u8()),
|
||||
in_channel: AtomicBool::new(false),
|
||||
hard_mute: AtomicBool::new(false),
|
||||
ptt_held: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
/// Rewire the selector to a fresh [`AudioTransmitGate`]
|
||||
/// (typically the gate exposed by a newly-started
|
||||
/// [`crate::AudioEngine`]). Cached mode / channel / mute /
|
||||
/// ptt_held are preserved; the new gate is immediately
|
||||
/// updated to the resolved value.
|
||||
pub fn replace_gate(&self, gate: AudioTransmitGate) {
|
||||
if let Ok(mut g) = self.gate.write() {
|
||||
*g = gate;
|
||||
}
|
||||
self.recompute();
|
||||
}
|
||||
|
||||
/// Update the selected mode and re-evaluate the gate.
|
||||
pub fn set_mode(&self, m: TransmitMode) {
|
||||
self.mode.store(m.as_u8(), Ordering::Relaxed);
|
||||
self.recompute();
|
||||
}
|
||||
|
||||
/// Current selected mode.
|
||||
pub fn mode(&self) -> TransmitMode {
|
||||
TransmitMode::from_u8(self.mode.load(Ordering::Relaxed)).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Update channel membership and re-evaluate.
|
||||
pub fn set_in_channel(&self, v: bool) {
|
||||
self.in_channel.store(v, Ordering::Relaxed);
|
||||
self.recompute();
|
||||
}
|
||||
|
||||
/// Current channel-membership flag.
|
||||
pub fn in_channel(&self) -> bool {
|
||||
self.in_channel.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Final-clamp hard mute. When `true` the gate is forced to
|
||||
/// `false` regardless of mode.
|
||||
pub fn set_hard_mute(&self, v: bool) {
|
||||
self.hard_mute.store(v, Ordering::Relaxed);
|
||||
self.recompute();
|
||||
}
|
||||
|
||||
/// Current hard-mute flag.
|
||||
pub fn hard_mute(&self) -> bool {
|
||||
self.hard_mute.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// PTT key state (set by the platform input backend through
|
||||
/// [`crate::ReleaseTailTimer`]).
|
||||
pub fn set_ptt_held(&self, v: bool) {
|
||||
self.ptt_held.store(v, Ordering::Relaxed);
|
||||
self.recompute();
|
||||
}
|
||||
|
||||
/// Current PTT-held flag.
|
||||
pub fn ptt_held(&self) -> bool {
|
||||
self.ptt_held.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Shared snapshot of the underlying gate. Provided for the
|
||||
/// audio engine's hot read path. Returns a clone so callers
|
||||
/// don't hold the internal lock.
|
||||
pub fn gate(&self) -> AudioTransmitGate {
|
||||
self.gate.read().expect("selector gate lock poisoned").clone()
|
||||
}
|
||||
|
||||
fn compute(&self) -> bool {
|
||||
if self.hard_mute.load(Ordering::Relaxed) {
|
||||
return false;
|
||||
}
|
||||
if !self.in_channel.load(Ordering::Relaxed) {
|
||||
return false;
|
||||
}
|
||||
match self.mode() {
|
||||
TransmitMode::Ptt => self.ptt_held.load(Ordering::Relaxed),
|
||||
// DEC-030: VoiceActivity behaves as Continuous in v1.
|
||||
TransmitMode::Continuous | TransmitMode::VoiceActivity => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Recompute the desired gate state and publish it. Exposed
|
||||
/// for tests; callers normally trigger this implicitly through
|
||||
/// the setter methods.
|
||||
pub fn recompute(&self) {
|
||||
let desired = self.compute();
|
||||
if let Ok(g) = self.gate.read() {
|
||||
g.set(desired);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn fresh() -> (AudioTransmitGate, TransmitModeSelector) {
|
||||
let g = AudioTransmitGate::new(false);
|
||||
let s = TransmitModeSelector::new(g.clone());
|
||||
(g, s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ptt_requires_channel_and_key() {
|
||||
let (g, s) = fresh();
|
||||
s.set_mode(TransmitMode::Ptt);
|
||||
s.set_ptt_held(true);
|
||||
assert!(!g.load(), "no channel -> false");
|
||||
s.set_in_channel(true);
|
||||
assert!(g.load(), "channel + key -> true");
|
||||
s.set_ptt_held(false);
|
||||
assert!(!g.load(), "key released -> false");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn continuous_ignores_ptt_held() {
|
||||
let (g, s) = fresh();
|
||||
s.set_mode(TransmitMode::Continuous);
|
||||
s.set_in_channel(true);
|
||||
assert!(g.load(), "channel + continuous -> true");
|
||||
s.set_ptt_held(true);
|
||||
assert!(g.load());
|
||||
s.set_ptt_held(false);
|
||||
assert!(g.load(), "continuous independent of key state");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn voice_activity_matches_continuous_v1() {
|
||||
let (g, s) = fresh();
|
||||
s.set_mode(TransmitMode::VoiceActivity);
|
||||
s.set_in_channel(true);
|
||||
assert!(g.load());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hard_mute_clamps() {
|
||||
let (g, s) = fresh();
|
||||
s.set_mode(TransmitMode::Continuous);
|
||||
s.set_in_channel(true);
|
||||
assert!(g.load());
|
||||
s.set_hard_mute(true);
|
||||
assert!(!g.load(), "hard mute clamps to false");
|
||||
s.set_hard_mute(false);
|
||||
assert!(g.load());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaving_channel_forces_false() {
|
||||
let (g, s) = fresh();
|
||||
s.set_mode(TransmitMode::Continuous);
|
||||
s.set_in_channel(true);
|
||||
assert!(g.load());
|
||||
s.set_in_channel(false);
|
||||
assert!(!g.load());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user