-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommands.rs
More file actions
271 lines (247 loc) · 9.16 KB
/
Copy pathcommands.rs
File metadata and controls
271 lines (247 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Command surface — the messages the channel actor accepts from outside.
//
// Mirrors the JS-facing API in index.js: close / remote / mix / unmix / dtmf /
// echo / play / record / playrecord / direction. Each mutating command that
// JS awaits includes a oneshot ack so the caller's Promise can resolve.
use std::net::SocketAddr;
use tokio::sync::oneshot;
#[derive(Debug, Clone)]
pub struct RemoteConfig {
pub addr: SocketAddr,
pub payload_type: u8,
pub ilbc_payload_type: Option<u8>,
pub rfc2833_payload_type: Option<u8>,
pub dtls: Option<RemoteDtls>,
/// RFC 5761 rtcp-mux: when true the peer carries RTCP over the RTP port /
/// 5-tuple, so we send/receive RTCP there instead of on the separate P+1
/// control port. Default false = classic split ports (SIP softphones).
pub rtcpmux: bool,
/// ICE password of the *remote* agent. Per RFC 8445 §7.1.1, the remote
/// uses our local icepwd to sign STUN Binding Requests it sends us; our
/// Binding Responses are signed with the same key. This field is kept
/// for completeness (and for future outgoing connectivity checks) even
/// though the inbound request-integrity check uses `state.local_icepwd`.
pub icepwd: Option<String>,
}
#[derive(Debug, Clone)]
pub struct RemoteDtls {
/// The peer's SDP `a=fingerprint` value. Verified against the certificate
/// presented in the DTLS handshake (see `dtls_session::PeerFingerprint`).
pub fingerprint: String,
pub setup: DtlsSetup,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
pub enum DtlsSetup {
Active,
Passive,
}
/// The play config carried on `Command::Play`. Alias for the parsed spec in
/// `channel/player.rs` — commands used to carry a raw JSON string, but the
/// facade now parses at the JS boundary so actors never see unparsed input.
pub type SoundSoup = super::player::SoundSoupSpec;
/// Same struct the recorder subsystem uses — the facade does all JS→Rust
/// parsing, so Command payloads are already fully-typed.
pub type RecordConfig = super::recorder::RecorderConfig;
#[derive(Debug, Clone)]
pub struct PlayRecordConfig {
pub player: SoundSoup,
pub recorder: RecordConfig,
pub interrupt: bool,
pub bargein_power: Option<i32>,
pub bargein_packets: Option<u32>,
/// Concurrent-capture mode. When true the recorder is opened immediately and
/// runs *alongside* the player (staying gated by `start_above_power` until the
/// caller speaks); the player is never auto-interrupted by inbound energy.
/// The controller stops the prompt explicitly with `StopPlay` once it has an
/// acceptable answer. Mutually exclusive with `interrupt` (concurrent wins).
pub concurrent: bool,
}
#[derive(Debug, Clone, Copy)]
pub struct Direction {
pub send: bool,
pub recv: bool,
}
impl Default for Direction {
fn default() -> Self {
Self {
send: true,
recv: true,
}
}
}
// Ack types —
// `()` for fire-and-forget completion; richer types introduced as needed.
pub type Ack<T = ()> = oneshot::Sender<T>;
// ChannelId is a u64 rather than Uuid to keep the command channel cheap; the
// public UUID lives on the JS-facing handle, not on the internal wire.
pub type ChannelId = u64;
pub enum Command {
Remote {
cfg: RemoteConfig,
ack: Ack,
},
Play {
cfg: SoundSoup,
ack: Ack,
},
Record {
cfg: RecordConfig,
ack: Ack,
},
/// `channel.record({ finish: true, file: "..." })` — finalize the named
/// recorder (multiple can coexist, keyed by file path).
RecordFinish {
file: std::path::PathBuf,
},
/// `channel.record({ pause: true, file: "..." })` — pause/resume toggle.
/// `resume=true` flips from Paused to Active; `resume=false` pauses.
RecordSetPaused {
file: std::path::PathBuf,
paused: bool,
},
PlayRecord {
cfg: PlayRecordConfig,
ack: Ack,
},
/// `channel.createReadStream({...})` — register a new audio reader.
/// The id is generated facade-side (via `audio_reader::next_reader_id`)
/// and passed in so JS can refer to this reader when it needs to
/// destroy it. The mpsc sender is also facade-side; the forwarder task
/// holding the matching receiver runs alongside.
CreateReadStream {
id: u64,
cfg: super::audio_reader::ReaderConfig,
sender: tokio::sync::mpsc::Sender<Vec<u8>>,
},
/// `readable.destroy()` from JS. Removes the reader with the given
/// id — dropping it closes the mpsc and ends the forwarder task.
DestroyReadStream {
id: u64,
},
/// `channel.createWriteStream({...})` — register a writer. Id is
/// generated facade-side so JS can reference this specific writer
/// for later byte pushes / end / destroy. The Receiver is
/// created facade-side alongside the id; the matching Sender is
/// kept on the ChannelObject so JS's `_write` can push to it
/// without going through the actor channel per frame.
CreateWriteStream {
id: u64,
cfg: super::audio_writer::WriterConfig,
receiver: tokio::sync::mpsc::Receiver<Vec<u8>>,
},
/// `writable.destroy()` from JS. Removes the writer immediately
/// — any buffered samples are discarded.
DestroyWriteStream {
id: u64,
},
Dtmf {
digits: String,
},
Echo {
enabled: bool,
},
/// `channel.stopplay()` — stop the active player immediately without
/// disturbing any running recorder. Fire-and-forget (no ack). Companion to
/// `playrecord({ concurrent: true })`: JS captures the answer while the
/// prompt plays, then fires this to interrupt once the answer is acceptable.
StopPlay,
/// `channel.pauseplay()` / `channel.resumeplay()` — freeze/unfreeze the
/// active player in place (position, loops) without destroying it, leaving
/// any recorder untouched. Fire-and-forget. Lets a controller pause the
/// prompt the moment the caller speaks and *resume* (not restart) it when
/// the capture turns out not to be an answer (e.g. a cough).
PausePlay {
pause: bool,
},
Direction(Direction),
/// Migrate this channel's state + subs into a mix group actor. The actor
/// transitions from Local to Mixed mode; subsequent commands are
/// forwarded into the mixer. `ack` fires once the migration completes.
EnterMix {
mix: super::mixer::MixHandle,
ack: Ack,
},
/// Migrate state + subs back out of the mix group. The actor returns to
/// Local mode and resumes its own ticker. Used for `channel.unmix()`.
LeaveMix {
ack: Ack,
},
Close {
reason: String,
},
}
// The public handle JS holds (indirectly, via a wrapper in channel/mod.rs).
// Cheap to clone — it's an mpsc Sender + a couple of ids.
#[derive(Clone)]
pub struct Handle {
pub id: ChannelId,
pub(crate) cmd: tokio::sync::mpsc::Sender<Command>,
}
#[allow(dead_code)]
impl Handle {
pub async fn close(&self, reason: impl Into<String>) {
let _ = self
.cmd
.send(Command::Close {
reason: reason.into(),
})
.await;
}
pub async fn dtmf(&self, digits: impl Into<String>) {
let _ = self
.cmd
.send(Command::Dtmf {
digits: digits.into(),
})
.await;
}
pub async fn echo(&self, on: bool) {
let _ = self.cmd.send(Command::Echo { enabled: on }).await;
}
pub async fn direction(&self, d: Direction) {
let _ = self.cmd.send(Command::Direction(d)).await;
}
pub async fn remote(&self, cfg: RemoteConfig) -> Result<(), ()> {
let (tx, rx) = oneshot::channel();
self.cmd
.send(Command::Remote { cfg, ack: tx })
.await
.map_err(|_| ())?;
rx.await.map_err(|_| ())
}
pub async fn play(&self, cfg: SoundSoup) -> Result<(), ()> {
let (tx, rx) = oneshot::channel();
self.cmd
.send(Command::Play { cfg, ack: tx })
.await
.map_err(|_| ())?;
rx.await.map_err(|_| ())
}
pub async fn record(&self, cfg: RecordConfig) -> Result<(), ()> {
let (tx, rx) = oneshot::channel();
self.cmd
.send(Command::Record { cfg, ack: tx })
.await
.map_err(|_| ())?;
rx.await.map_err(|_| ())
}
pub async fn play_record(&self, cfg: PlayRecordConfig) -> Result<(), ()> {
let (tx, rx) = oneshot::channel();
self.cmd
.send(Command::PlayRecord { cfg, ack: tx })
.await
.map_err(|_| ())?;
rx.await.map_err(|_| ())
}
/// Stop the active player without disturbing any recorder. Fire-and-forget
/// (no ack) — companion to `playrecord({ concurrent: true })`.
pub async fn stop_play(&self) {
let _ = self.cmd.send(Command::StopPlay).await;
}
/// Pause (true) or resume (false) the active player in place without
/// destroying it. Fire-and-forget (no ack).
pub async fn pause_play(&self, pause: bool) {
let _ = self.cmd.send(Command::PausePlay { pause }).await;
}
}