@howaboua/pi-durable-gippity-voice 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/activation.js +369 -148
- package/dist/index.js +369 -148
- package/dist/packages/execution/src/tool-contract.d.ts +2 -2
- package/dist/packages/gippity-voice/src/agent-observation.d.ts +13 -0
- package/dist/packages/gippity-voice/src/host.d.ts +6 -0
- package/dist/packages/gippity-voice/src/index.d.ts +2 -1
- package/dist/packages/gippity-voice/src/voice/conversation/peer.d.ts +4 -0
- package/dist/packages/gippity-voice/src/voice/conversation/public-turns.d.ts +2 -1
- package/dist/packages/gippity-voice/src/voice/conversation/session.d.ts +1 -0
- package/dist/packages/gippity-voice/src/voice/helper-protocol.d.ts +35 -0
- package/dist/packages/gippity-voice/src/voice/lan/browser-clients.d.ts +1 -1
- package/dist/packages/gippity-voice/src/voice/lan/browser-connections.d.ts +17 -0
- package/dist/packages/gippity-voice/src/voice/lan/browser-session.d.ts +4 -2
- package/dist/packages/gippity-voice/src/voice/lan/server.d.ts +4 -2
- package/dist/packages/gippity-voice/src/voice/realtime-auth.d.ts +3 -2
- package/dist/packages/gippity-voice/src/voice/turns.d.ts +2 -1
- package/dist/voice/bin/darwin-arm64/pi-codex-voice +0 -0
- package/dist/voice/bin/darwin-x64/pi-codex-voice +0 -0
- package/dist/voice/bin/linux-arm64/pi-codex-voice +0 -0
- package/dist/voice/bin/linux-x64/pi-codex-voice +0 -0
- package/dist/voice/bin/win32-arm64/pi-codex-voice.exe +0 -0
- package/dist/voice/bin/win32-x64/pi-codex-voice.exe +0 -0
- package/package.json +6 -5
- package/src/voice/rust/src/audio.rs +2 -2
- package/src/voice/rust/src/playout.rs +68 -8
- package/src/voice/rust/src/protocol.rs +43 -1
- package/src/voice/rust/src/v3_media.rs +138 -31
|
@@ -7,7 +7,6 @@ use std::time::{Duration, Instant as StdInstant};
|
|
|
7
7
|
use anyhow::{Context as _, Result};
|
|
8
8
|
use base64::Engine;
|
|
9
9
|
use base64::engine::general_purpose::STANDARD as BASE64;
|
|
10
|
-
use bytes::Bytes;
|
|
11
10
|
use crossbeam_queue::ArrayQueue;
|
|
12
11
|
use rtc::media::Sample;
|
|
13
12
|
use rtc::peer_connection::configuration::media_engine::MIME_TYPE_OPUS;
|
|
@@ -24,8 +23,8 @@ use webrtc::peer_connection::PeerConnection;
|
|
|
24
23
|
use webrtc::rtp_transceiver::RtpSender;
|
|
25
24
|
|
|
26
25
|
use crate::audio::{self, PlaybackSample, SpeakerControl, SpeakerState};
|
|
27
|
-
use crate::playout::{PacketPlayout, PlayoutClock, PlayoutFrame};
|
|
28
|
-
use crate::protocol::Event;
|
|
26
|
+
use crate::playout::{PacketAdmission, PacketPlayout, PlayoutClock, PlayoutFrame};
|
|
27
|
+
use crate::protocol::{AudioDiagnostics, Event};
|
|
29
28
|
use crate::resample::LinearResampler;
|
|
30
29
|
|
|
31
30
|
#[path = "v3_capture.rs"]
|
|
@@ -43,7 +42,7 @@ pub(crate) const TRANSITION_DRAIN_PACKET_BUDGET: usize = 256;
|
|
|
43
42
|
|
|
44
43
|
#[derive(Debug, PartialEq, Eq)]
|
|
45
44
|
pub(crate) enum ReadyDrain<E> {
|
|
46
|
-
Pending,
|
|
45
|
+
Pending(usize),
|
|
47
46
|
ReadFailed(E),
|
|
48
47
|
Exhausted,
|
|
49
48
|
}
|
|
@@ -53,17 +52,17 @@ where
|
|
|
53
52
|
F: Future<Output = std::result::Result<T, E>>,
|
|
54
53
|
N: FnMut() -> Pin<Box<F>>,
|
|
55
54
|
{
|
|
56
|
-
for
|
|
55
|
+
for drained in 0..TRANSITION_DRAIN_PACKET_BUDGET {
|
|
57
56
|
match poll_once(read.as_mut()) {
|
|
58
57
|
Poll::Ready(Ok(_)) => *read = next(),
|
|
59
58
|
Poll::Ready(Err(error)) => return ReadyDrain::ReadFailed(error),
|
|
60
|
-
Poll::Pending => return ReadyDrain::Pending,
|
|
59
|
+
Poll::Pending => return ReadyDrain::Pending(drained),
|
|
61
60
|
}
|
|
62
61
|
}
|
|
63
62
|
match poll_once(read.as_mut()) {
|
|
64
63
|
Poll::Ready(Ok(_)) => ReadyDrain::Exhausted,
|
|
65
64
|
Poll::Ready(Err(error)) => ReadyDrain::ReadFailed(error),
|
|
66
|
-
Poll::Pending => ReadyDrain::Pending,
|
|
65
|
+
Poll::Pending => ReadyDrain::Pending(TRANSITION_DRAIN_PACKET_BUDGET),
|
|
67
66
|
}
|
|
68
67
|
}
|
|
69
68
|
|
|
@@ -204,7 +203,14 @@ pub async fn play_remote_track(
|
|
|
204
203
|
let mut converted = Vec::new();
|
|
205
204
|
let mut bridge_pending = Vec::new();
|
|
206
205
|
let mut last_frame_samples = OPUS_FRAME_SAMPLES;
|
|
206
|
+
let mut last_packet_config = None;
|
|
207
207
|
let mut last_activity = None;
|
|
208
|
+
let mut diagnostics = AudioDiagnostics::default();
|
|
209
|
+
let mut diagnostics_since = Instant::now();
|
|
210
|
+
let mut first_packet_since = None;
|
|
211
|
+
let mut last_packet_read: Option<Instant> = None;
|
|
212
|
+
let mut diagnostics_tick = tokio::time::interval(Duration::from_secs(5));
|
|
213
|
+
diagnostics_tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
|
208
214
|
// Keep one remote-event read alive
|
|
209
215
|
// across every select and transition-boundary readiness probe.
|
|
210
216
|
let mut packet_read = Box::pin(read_rtp(&remote));
|
|
@@ -215,11 +221,12 @@ pub async fn play_remote_track(
|
|
|
215
221
|
if changed.is_err() {
|
|
216
222
|
return;
|
|
217
223
|
}
|
|
218
|
-
//
|
|
219
|
-
//
|
|
220
|
-
//
|
|
221
|
-
|
|
222
|
-
|
|
224
|
+
// RTP lacks arrival/response identity. Drain before replacing the
|
|
225
|
+
// epoch, including on release, to isolate interrupted speech.
|
|
226
|
+
// New onset RTP already readable on release may also be lost;
|
|
227
|
+
// measure release drains rather than guessing packet identity.
|
|
228
|
+
let drained = match drain_ready_packets(&mut packet_read, || Box::pin(read_rtp(&remote))) {
|
|
229
|
+
ReadyDrain::Pending(drained) => drained as u32,
|
|
223
230
|
ReadyDrain::ReadFailed(error) => {
|
|
224
231
|
report_terminal_error(
|
|
225
232
|
&output_events,
|
|
@@ -234,13 +241,21 @@ pub async fn play_remote_track(
|
|
|
234
241
|
).await;
|
|
235
242
|
return;
|
|
236
243
|
}
|
|
237
|
-
}
|
|
244
|
+
};
|
|
238
245
|
control = *controls.borrow_and_update();
|
|
246
|
+
diagnostics.drained_packets = diagnostics.drained_packets.saturating_add(drained);
|
|
247
|
+
if !control.suppressed {
|
|
248
|
+
diagnostics.release_drained_packets = diagnostics.release_drained_packets.saturating_add(drained);
|
|
249
|
+
}
|
|
250
|
+
diagnostics.transitions = diagnostics.transitions.saturating_add(1);
|
|
251
|
+
last_packet_read = None;
|
|
252
|
+
first_packet_since = (!control.suppressed).then(Instant::now);
|
|
239
253
|
playout.reset();
|
|
240
254
|
playout_clock.stop();
|
|
241
255
|
resampler.reset();
|
|
242
256
|
bridge_pending.clear();
|
|
243
257
|
last_frame_samples = OPUS_FRAME_SAMPLES;
|
|
258
|
+
last_packet_config = None;
|
|
244
259
|
match opus::Decoder::new(OPUS_RATE, opus::Channels::Stereo) {
|
|
245
260
|
Ok(replacement) => decoder = replacement,
|
|
246
261
|
Err(error) => {
|
|
@@ -264,39 +279,87 @@ pub async fn play_remote_track(
|
|
|
264
279
|
}
|
|
265
280
|
};
|
|
266
281
|
packet_read = Box::pin(read_rtp(&remote));
|
|
282
|
+
diagnostics.received_packets = diagnostics.received_packets.saturating_add(1);
|
|
283
|
+
let now = Instant::now();
|
|
284
|
+
if let Some(last) = last_packet_read.replace(now) {
|
|
285
|
+
let gap = now.saturating_duration_since(last);
|
|
286
|
+
diagnostics.max_read_gap_ms = diagnostics.max_read_gap_ms.max(gap.as_millis().min(u32::MAX as u128) as u32);
|
|
287
|
+
if gap >= Duration::from_millis(40) {
|
|
288
|
+
diagnostics.read_gaps_over_40ms = diagnostics.read_gaps_over_40ms.saturating_add(1);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
267
291
|
if control.accepts_packet(speaker.get()) {
|
|
268
|
-
|
|
292
|
+
if let Some(since) = first_packet_since.take() {
|
|
293
|
+
diagnostics.first_packet_ms = Some(since.elapsed().as_millis().min(u32::MAX as u128) as u32);
|
|
294
|
+
}
|
|
295
|
+
match playout.push(packet.header.sequence_number, packet.payload) {
|
|
296
|
+
PacketAdmission::Accepted { reordered } => {
|
|
297
|
+
diagnostics.reordered_packets = diagnostics.reordered_packets.saturating_add(u32::from(reordered));
|
|
298
|
+
}
|
|
299
|
+
PacketAdmission::Late => diagnostics.late_packets = diagnostics.late_packets.saturating_add(1),
|
|
300
|
+
PacketAdmission::Duplicate => diagnostics.duplicate_packets = diagnostics.duplicate_packets.saturating_add(1),
|
|
301
|
+
PacketAdmission::Resynchronized { discarded } => {
|
|
302
|
+
diagnostics.resyncs = diagnostics.resyncs.saturating_add(1);
|
|
303
|
+
diagnostics.resync_discarded_packets = diagnostics.resync_discarded_packets.saturating_add(discarded as u32);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
269
306
|
if playout.ready() {
|
|
270
307
|
playout_clock.start(Instant::now());
|
|
271
308
|
}
|
|
309
|
+
} else {
|
|
310
|
+
diagnostics.rejected_packets = diagnostics.rejected_packets.saturating_add(1);
|
|
272
311
|
}
|
|
273
312
|
}
|
|
274
|
-
_ = wait_for_playout(playout_clock.deadline()) => {
|
|
313
|
+
_ = wait_for_playout(playout_clock.deadline().or_else(|| playout.buffering_deadline())) => {
|
|
314
|
+
if let Some(deadline) = playout_clock.deadline() {
|
|
315
|
+
let late = Instant::now().saturating_duration_since(deadline);
|
|
316
|
+
diagnostics.max_playout_lateness_us = diagnostics.max_playout_lateness_us.max(late.as_micros().min(u32::MAX as u128) as u32);
|
|
317
|
+
}
|
|
275
318
|
if control.suppressed || speaker.get() != control {
|
|
276
319
|
playout.reset();
|
|
277
320
|
playout_clock.stop();
|
|
278
321
|
bridge_pending.clear();
|
|
279
322
|
continue;
|
|
280
323
|
}
|
|
324
|
+
let flushing_tail = !playout.ready() && playout.buffering_deadline().is_some();
|
|
281
325
|
let frame = playout.next();
|
|
326
|
+
let received_frame = matches!(&frame, PlayoutFrame::Packet(_));
|
|
282
327
|
let (payload, decoded_output) = match frame {
|
|
283
328
|
PlayoutFrame::Buffering => {
|
|
329
|
+
diagnostics.buffering_entries = diagnostics.buffering_entries.saturating_add(1);
|
|
284
330
|
playout_clock.stop();
|
|
285
331
|
continue;
|
|
286
332
|
}
|
|
287
333
|
PlayoutFrame::Packet(payload) => (payload, &mut decoded[..]),
|
|
288
|
-
PlayoutFrame::Missing => {
|
|
289
|
-
|
|
334
|
+
PlayoutFrame::Missing { successor } => {
|
|
335
|
+
diagnostics.missing_packets = diagnostics.missing_packets.saturating_add(1);
|
|
336
|
+
(successor.unwrap_or_default(), &mut decoded[..last_frame_samples * 2])
|
|
290
337
|
}
|
|
291
338
|
};
|
|
292
|
-
|
|
339
|
+
if flushing_tail {
|
|
340
|
+
diagnostics.tail_flushes = diagnostics.tail_flushes.saturating_add(1);
|
|
341
|
+
}
|
|
342
|
+
playout_clock.start(Instant::now());
|
|
343
|
+
let decoded_frame = if received_frame {
|
|
293
344
|
decoder.decode_float(&payload, decoded_output, false)
|
|
294
|
-
else {
|
|
295
|
-
|
|
345
|
+
} else {
|
|
346
|
+
decode_missing_frame(&mut decoder, last_packet_config, &payload, decoded_output)
|
|
347
|
+
};
|
|
348
|
+
let Ok(samples_per_channel) = decoded_frame else {
|
|
349
|
+
diagnostics.decode_failures = diagnostics.decode_failures.saturating_add(1);
|
|
350
|
+
diagnostics.clock_rebases = diagnostics.clock_rebases.saturating_add(u32::from(playout_clock.advance(last_frame_samples, OPUS_RATE)));
|
|
296
351
|
continue;
|
|
297
352
|
};
|
|
298
353
|
last_frame_samples = samples_per_channel;
|
|
299
|
-
|
|
354
|
+
if received_frame {
|
|
355
|
+
last_packet_config = payload.first().map(|toc| toc & 0xfc);
|
|
356
|
+
let samples = samples_per_channel as u32;
|
|
357
|
+
diagnostics.decoded_packets = diagnostics.decoded_packets.saturating_add(1);
|
|
358
|
+
diagnostics.decoded_packet_samples = diagnostics.decoded_packet_samples.saturating_add(samples);
|
|
359
|
+
diagnostics.min_decoded_packet_samples = if diagnostics.min_decoded_packet_samples == 0 { samples } else { diagnostics.min_decoded_packet_samples.min(samples) };
|
|
360
|
+
diagnostics.max_decoded_packet_samples = diagnostics.max_decoded_packet_samples.max(samples);
|
|
361
|
+
}
|
|
362
|
+
diagnostics.clock_rebases = diagnostics.clock_rebases.saturating_add(u32::from(playout_clock.advance(samples_per_channel, OPUS_RATE)));
|
|
300
363
|
let mut mono = Vec::with_capacity(samples_per_channel);
|
|
301
364
|
for pair in decoded[..samples_per_channel * 2].as_chunks::<2>().0 {
|
|
302
365
|
mono.push((pair[0] + pair[1]) * 0.5);
|
|
@@ -346,26 +409,70 @@ pub async fn play_remote_track(
|
|
|
346
409
|
last_activity = Some(now);
|
|
347
410
|
}
|
|
348
411
|
}
|
|
349
|
-
|
|
350
|
-
.
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
.
|
|
357
|
-
|
|
358
|
-
|
|
412
|
+
let event = Event::Pcm {
|
|
413
|
+
audio: BASE64.encode(bytes),
|
|
414
|
+
sample_rate: BRIDGE_RATE,
|
|
415
|
+
num_channels: 1,
|
|
416
|
+
epoch: Some(control.epoch),
|
|
417
|
+
};
|
|
418
|
+
if output_events.capacity() == 0 {
|
|
419
|
+
diagnostics.bridge_channel_full = diagnostics.bridge_channel_full.saturating_add(1);
|
|
420
|
+
}
|
|
421
|
+
let send_started = Instant::now();
|
|
422
|
+
let sent = output_events.send(event).await;
|
|
423
|
+
let send_time = send_started.elapsed();
|
|
424
|
+
diagnostics.bridge_frames = diagnostics.bridge_frames.saturating_add(1);
|
|
425
|
+
diagnostics.max_bridge_send_us = diagnostics.max_bridge_send_us.max(send_time.as_micros().min(u32::MAX as u128) as u32);
|
|
426
|
+
if send_time >= Duration::from_millis(1) {
|
|
427
|
+
diagnostics.bridge_sends_over_1ms = diagnostics.bridge_sends_over_1ms.saturating_add(1);
|
|
428
|
+
}
|
|
429
|
+
if sent.is_err() {
|
|
359
430
|
return;
|
|
360
431
|
}
|
|
361
432
|
}
|
|
362
433
|
}
|
|
363
434
|
}
|
|
364
435
|
}
|
|
436
|
+
_ = diagnostics_tick.tick() => {
|
|
437
|
+
if diagnostics.received_packets > 0 || diagnostics.transitions > 0 || diagnostics.rejected_packets > 0
|
|
438
|
+
|| diagnostics.buffering_entries > 0 || diagnostics.clock_rebases > 0
|
|
439
|
+
|| diagnostics.decode_failures > 0 || diagnostics.first_packet_ms.is_some()
|
|
440
|
+
{
|
|
441
|
+
diagnostics.epoch = control.epoch;
|
|
442
|
+
diagnostics.suppressed = control.suppressed;
|
|
443
|
+
diagnostics.elapsed_ms = diagnostics_since.elapsed().as_millis().min(u32::MAX as u128) as u32;
|
|
444
|
+
if output_events.try_send(Event::AudioDiagnostics { diagnostics: diagnostics.clone() }).is_ok() {
|
|
445
|
+
diagnostics = AudioDiagnostics::default();
|
|
446
|
+
diagnostics_since = Instant::now();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
365
450
|
}
|
|
366
451
|
}
|
|
367
452
|
}
|
|
368
453
|
|
|
454
|
+
fn decode_missing_frame(
|
|
455
|
+
decoder: &mut opus::Decoder,
|
|
456
|
+
previous_config: Option<u8>,
|
|
457
|
+
successor: &[u8],
|
|
458
|
+
output: &mut [f32],
|
|
459
|
+
) -> Result<usize, opus::Error> {
|
|
460
|
+
let samples = output.len() / 2;
|
|
461
|
+
// Match the prior mode/bandwidth/frame size/channels (TOC without framing).
|
|
462
|
+
// Only a matching single 10/20ms frame can safely repair this slot.
|
|
463
|
+
// No FEC in the successor is normal: libopus performs PLC instead.
|
|
464
|
+
if matches!(samples, 480 | 960)
|
|
465
|
+
&& previous_config.is_some()
|
|
466
|
+
&& successor.first().map(|toc| toc & 0xfc) == previous_config
|
|
467
|
+
&& matches!(opus::packet::get_nb_frames(successor), Ok(1))
|
|
468
|
+
&& opus::packet::get_nb_samples(successor, OPUS_RATE).is_ok_and(|count| count == samples)
|
|
469
|
+
&& let Ok(samples) = decoder.decode_float(successor, output, true)
|
|
470
|
+
{
|
|
471
|
+
return Ok(samples);
|
|
472
|
+
}
|
|
473
|
+
decoder.decode_float(&[], output, false)
|
|
474
|
+
}
|
|
475
|
+
|
|
369
476
|
async fn read_rtp(remote: &Arc<dyn TrackRemote>) -> Result<rtc::rtp::Packet> {
|
|
370
477
|
while let Some(event) = remote.poll().await {
|
|
371
478
|
match event {
|