2026-04-24 14:49:57 -03:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
|
2026-04-30 08:16:57 -03:00
|
|
|
use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};
|
2026-04-24 14:49:57 -03:00
|
|
|
use std::sync::{Arc, Mutex};
|
2026-04-25 16:48:20 -03:00
|
|
|
use std::time::Duration;
|
2026-04-25 22:25:24 -03:00
|
|
|
use tokio::sync::{Notify, OwnedSemaphorePermit, Semaphore};
|
2026-04-25 16:48:20 -03:00
|
|
|
use tokio::time::Instant;
|
2026-04-25 14:00:53 -03:00
|
|
|
use tracing::info;
|
2026-04-24 14:49:57 -03:00
|
|
|
|
2026-04-25 22:25:24 -03:00
|
|
|
mod config;
|
|
|
|
|
mod state;
|
2026-04-29 01:25:06 -03:00
|
|
|
mod types;
|
2026-04-25 22:25:24 -03:00
|
|
|
|
|
|
|
|
use config::{
|
2026-04-29 01:25:06 -03:00
|
|
|
apply_playout_offset, upstream_camera_startup_grace_us, upstream_pairing_master_slack,
|
|
|
|
|
upstream_playout_delay, upstream_playout_offset_us, upstream_reanchor_late_threshold,
|
2026-05-01 12:03:07 -03:00
|
|
|
upstream_reanchor_window_us, upstream_require_paired_startup, upstream_timing_trace_enabled,
|
2026-04-25 22:25:24 -03:00
|
|
|
};
|
|
|
|
|
use state::UpstreamClockState;
|
2026-04-29 01:25:06 -03:00
|
|
|
pub use types::{
|
|
|
|
|
PlannedUpstreamPacket, UpstreamMediaKind, UpstreamPlanDecision, UpstreamStreamLease,
|
|
|
|
|
};
|
2026-04-25 16:48:20 -03:00
|
|
|
|
2026-04-24 14:49:57 -03:00
|
|
|
/// Coordinate upstream stream ownership and keep audio/video on one timeline.
|
|
|
|
|
///
|
|
|
|
|
/// Inputs: stream-open/close events plus remote packet timestamps.
|
|
|
|
|
/// Outputs: active-stream leases and rebased local PTS values.
|
|
|
|
|
/// Why: live calls need one current webcam owner, one current microphone owner,
|
|
|
|
|
/// and one shared media clock so reconnects do not leave old sinks alive or let
|
|
|
|
|
/// audio/video drift onto separate timing islands.
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct UpstreamMediaRuntime {
|
|
|
|
|
next_session_id: AtomicU64,
|
|
|
|
|
next_camera_generation: AtomicU64,
|
|
|
|
|
next_microphone_generation: AtomicU64,
|
|
|
|
|
microphone_sink_gate: Arc<Semaphore>,
|
2026-04-25 22:25:24 -03:00
|
|
|
pairing_state_notify: Arc<Notify>,
|
|
|
|
|
audio_progress_notify: Arc<Notify>,
|
2026-04-30 08:16:57 -03:00
|
|
|
camera_playout_offset_us: AtomicI64,
|
|
|
|
|
microphone_playout_offset_us: AtomicI64,
|
2026-04-24 14:49:57 -03:00
|
|
|
state: Mutex<UpstreamClockState>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UpstreamMediaRuntime {
|
|
|
|
|
/// Build an empty upstream runtime.
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
next_session_id: AtomicU64::new(0),
|
|
|
|
|
next_camera_generation: AtomicU64::new(0),
|
|
|
|
|
next_microphone_generation: AtomicU64::new(0),
|
|
|
|
|
microphone_sink_gate: Arc::new(Semaphore::new(1)),
|
2026-04-25 22:25:24 -03:00
|
|
|
pairing_state_notify: Arc::new(Notify::new()),
|
|
|
|
|
audio_progress_notify: Arc::new(Notify::new()),
|
2026-04-30 08:16:57 -03:00
|
|
|
camera_playout_offset_us: AtomicI64::new(upstream_playout_offset_us(
|
|
|
|
|
UpstreamMediaKind::Camera,
|
|
|
|
|
)),
|
|
|
|
|
microphone_playout_offset_us: AtomicI64::new(upstream_playout_offset_us(
|
|
|
|
|
UpstreamMediaKind::Microphone,
|
|
|
|
|
)),
|
2026-04-24 14:49:57 -03:00
|
|
|
state: Mutex::new(UpstreamClockState::default()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 08:16:57 -03:00
|
|
|
/// Apply live upstream playout offsets without restarting the relay.
|
|
|
|
|
pub fn set_playout_offsets(&self, camera_offset_us: i64, microphone_offset_us: i64) {
|
|
|
|
|
self.camera_playout_offset_us
|
|
|
|
|
.store(camera_offset_us, Ordering::Relaxed);
|
|
|
|
|
self.microphone_playout_offset_us
|
|
|
|
|
.store(microphone_offset_us, Ordering::Relaxed);
|
2026-04-24 14:49:57 -03:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 08:16:57 -03:00
|
|
|
/// Return `(camera_offset_us, microphone_offset_us)` currently used for live playout.
|
2026-04-24 14:49:57 -03:00
|
|
|
#[must_use]
|
2026-04-30 08:16:57 -03:00
|
|
|
pub fn playout_offsets(&self) -> (i64, i64) {
|
|
|
|
|
(
|
|
|
|
|
self.camera_playout_offset_us.load(Ordering::Relaxed),
|
|
|
|
|
self.microphone_playout_offset_us.load(Ordering::Relaxed),
|
|
|
|
|
)
|
2026-04-24 14:49:57 -03:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 08:16:57 -03:00
|
|
|
fn playout_offset_us(&self, kind: UpstreamMediaKind) -> i64 {
|
|
|
|
|
match kind {
|
|
|
|
|
UpstreamMediaKind::Camera => self.camera_playout_offset_us.load(Ordering::Relaxed),
|
2026-04-24 14:49:57 -03:00
|
|
|
UpstreamMediaKind::Microphone => {
|
2026-04-30 08:16:57 -03:00
|
|
|
self.microphone_playout_offset_us.load(Ordering::Relaxed)
|
2026-04-24 14:49:57 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-30 08:16:57 -03:00
|
|
|
}
|
2026-04-24 14:49:57 -03:00
|
|
|
|
2026-04-30 08:16:57 -03:00
|
|
|
include!("upstream_media_runtime/lease_lifecycle.rs");
|
2026-04-24 14:49:57 -03:00
|
|
|
|
2026-04-30 08:16:57 -03:00
|
|
|
impl UpstreamMediaRuntime {
|
2026-04-24 14:49:57 -03:00
|
|
|
/// Rebase one upstream video packet timestamp onto the shared session clock.
|
|
|
|
|
#[must_use]
|
2026-04-25 22:25:24 -03:00
|
|
|
pub fn map_video_pts(&self, remote_pts_us: u64, frame_step_us: u64) -> Option<u64> {
|
|
|
|
|
match self.plan_video_pts(remote_pts_us, frame_step_us.max(1)) {
|
|
|
|
|
UpstreamPlanDecision::Play(plan) => Some(plan.local_pts_us),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
2026-04-25 16:48:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Rebase one upstream audio packet timestamp onto the shared session clock.
|
|
|
|
|
#[must_use]
|
2026-04-25 22:25:24 -03:00
|
|
|
pub fn map_audio_pts(&self, remote_pts_us: u64) -> Option<u64> {
|
|
|
|
|
match self.plan_audio_pts(remote_pts_us) {
|
|
|
|
|
UpstreamPlanDecision::Play(plan) => Some(plan.local_pts_us),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
2026-04-25 16:48:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Rebase and schedule one upstream video packet on the shared playout epoch.
|
|
|
|
|
#[must_use]
|
2026-04-25 22:25:24 -03:00
|
|
|
pub fn plan_video_pts(&self, remote_pts_us: u64, frame_step_us: u64) -> UpstreamPlanDecision {
|
2026-04-25 16:48:20 -03:00
|
|
|
self.plan_pts(
|
2026-04-24 14:49:57 -03:00
|
|
|
UpstreamMediaKind::Camera,
|
|
|
|
|
remote_pts_us,
|
|
|
|
|
frame_step_us.max(1),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 16:48:20 -03:00
|
|
|
/// Rebase and schedule one upstream audio packet on the shared playout epoch.
|
2026-04-24 14:49:57 -03:00
|
|
|
#[must_use]
|
2026-04-25 22:25:24 -03:00
|
|
|
pub fn plan_audio_pts(&self, remote_pts_us: u64) -> UpstreamPlanDecision {
|
2026-04-25 16:48:20 -03:00
|
|
|
self.plan_pts(UpstreamMediaKind::Microphone, remote_pts_us, 1)
|
2026-04-24 14:49:57 -03:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 22:25:24 -03:00
|
|
|
/// Hold video until the audio master has at least reached the same capture
|
|
|
|
|
/// moment, or give up once the frame can no longer be shown fresh.
|
|
|
|
|
pub async fn wait_for_audio_master(&self, video_local_pts_us: u64, due_at: Instant) -> bool {
|
|
|
|
|
let slack_us = upstream_pairing_master_slack()
|
|
|
|
|
.as_micros()
|
|
|
|
|
.min(u64::MAX as u128) as u64;
|
|
|
|
|
loop {
|
|
|
|
|
let notified = self.audio_progress_notify.notified();
|
|
|
|
|
{
|
|
|
|
|
let state = self
|
|
|
|
|
.state
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("upstream media state mutex poisoned");
|
|
|
|
|
if state.active_microphone_generation.is_none() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if state.last_audio_local_pts_us.is_some_and(|audio_pts_us| {
|
|
|
|
|
audio_pts_us.saturating_add(slack_us) >= video_local_pts_us
|
|
|
|
|
}) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if Instant::now() >= due_at {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
tokio::select! {
|
|
|
|
|
_ = notified => {}
|
|
|
|
|
_ = tokio::time::sleep_until(due_at) => return false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 16:48:20 -03:00
|
|
|
fn plan_pts(
|
|
|
|
|
&self,
|
|
|
|
|
kind: UpstreamMediaKind,
|
|
|
|
|
remote_pts_us: u64,
|
|
|
|
|
min_step_us: u64,
|
2026-04-25 22:25:24 -03:00
|
|
|
) -> UpstreamPlanDecision {
|
2026-04-24 14:49:57 -03:00
|
|
|
let mut state = self
|
|
|
|
|
.state
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("upstream media state mutex poisoned");
|
2026-04-25 14:00:53 -03:00
|
|
|
let session_id = state.session_id;
|
2026-04-25 15:49:30 -03:00
|
|
|
let packet_count = match kind {
|
|
|
|
|
UpstreamMediaKind::Camera => {
|
|
|
|
|
state.camera_packet_count = state.camera_packet_count.saturating_add(1);
|
|
|
|
|
state.camera_packet_count
|
|
|
|
|
}
|
|
|
|
|
UpstreamMediaKind::Microphone => {
|
|
|
|
|
state.microphone_packet_count = state.microphone_packet_count.saturating_add(1);
|
|
|
|
|
state.microphone_packet_count
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-04-27 00:41:00 -03:00
|
|
|
let mut first_remote_for_kind = match kind {
|
|
|
|
|
UpstreamMediaKind::Camera => {
|
|
|
|
|
let first_slot = &mut state.first_camera_remote_pts_us;
|
|
|
|
|
*first_slot.get_or_insert(remote_pts_us)
|
|
|
|
|
}
|
|
|
|
|
UpstreamMediaKind::Microphone => {
|
|
|
|
|
let first_slot = &mut state.first_microphone_remote_pts_us;
|
|
|
|
|
*first_slot.get_or_insert(remote_pts_us)
|
|
|
|
|
}
|
2026-04-25 14:00:53 -03:00
|
|
|
};
|
2026-04-27 00:41:00 -03:00
|
|
|
if kind == UpstreamMediaKind::Camera {
|
|
|
|
|
let startup_grace_us = upstream_camera_startup_grace_us();
|
|
|
|
|
if !state.camera_startup_ready
|
|
|
|
|
&& (startup_grace_us == 0
|
|
|
|
|
|| remote_pts_us.saturating_sub(first_remote_for_kind) >= startup_grace_us)
|
|
|
|
|
{
|
|
|
|
|
state.camera_startup_ready = true;
|
|
|
|
|
state.first_camera_remote_pts_us = Some(remote_pts_us);
|
|
|
|
|
first_remote_for_kind = remote_pts_us;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-25 22:25:24 -03:00
|
|
|
let now = Instant::now();
|
|
|
|
|
let pairing_deadline = *state
|
|
|
|
|
.pairing_anchor_deadline
|
|
|
|
|
.get_or_insert_with(|| now + upstream_playout_delay());
|
2026-04-26 12:42:17 -03:00
|
|
|
let playout_delay = upstream_playout_delay();
|
2026-04-25 22:25:24 -03:00
|
|
|
|
|
|
|
|
if state.session_base_remote_pts_us.is_none() {
|
|
|
|
|
if state.first_camera_remote_pts_us.is_some()
|
|
|
|
|
&& state.first_microphone_remote_pts_us.is_some()
|
2026-04-27 00:41:00 -03:00
|
|
|
&& state.camera_startup_ready
|
2026-04-25 22:25:24 -03:00
|
|
|
{
|
|
|
|
|
let first_camera_remote_pts_us =
|
|
|
|
|
state.first_camera_remote_pts_us.unwrap_or_default();
|
|
|
|
|
let first_microphone_remote_pts_us =
|
|
|
|
|
state.first_microphone_remote_pts_us.unwrap_or_default();
|
|
|
|
|
state.session_base_remote_pts_us =
|
|
|
|
|
Some(first_camera_remote_pts_us.max(first_microphone_remote_pts_us));
|
2026-04-26 12:42:17 -03:00
|
|
|
let overlap_epoch = now + playout_delay;
|
|
|
|
|
state.playout_epoch = Some(overlap_epoch);
|
|
|
|
|
state.pairing_anchor_deadline = Some(overlap_epoch);
|
2026-04-25 22:25:24 -03:00
|
|
|
if !state.startup_anchor_logged {
|
|
|
|
|
let startup_delta_us =
|
|
|
|
|
first_camera_remote_pts_us as i128 - first_microphone_remote_pts_us as i128;
|
|
|
|
|
info!(
|
|
|
|
|
session_id,
|
|
|
|
|
first_camera_remote_pts_us,
|
|
|
|
|
first_microphone_remote_pts_us,
|
|
|
|
|
overlap_base_remote_pts_us =
|
|
|
|
|
state.session_base_remote_pts_us.unwrap_or_default(),
|
|
|
|
|
startup_delta_us,
|
|
|
|
|
"upstream media overlap anchors established"
|
|
|
|
|
);
|
|
|
|
|
state.startup_anchor_logged = true;
|
|
|
|
|
}
|
|
|
|
|
self.pairing_state_notify.notify_waiters();
|
|
|
|
|
} else if now < pairing_deadline {
|
|
|
|
|
if upstream_timing_trace_enabled()
|
|
|
|
|
&& (packet_count <= 10 || packet_count.is_multiple_of(300))
|
|
|
|
|
{
|
|
|
|
|
info!(
|
|
|
|
|
session_id,
|
|
|
|
|
?kind,
|
|
|
|
|
packet_count,
|
|
|
|
|
remote_pts_us,
|
|
|
|
|
wait_ms = pairing_deadline.saturating_duration_since(now).as_millis(),
|
|
|
|
|
"upstream media packet buffered while awaiting the counterpart stream"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return UpstreamPlanDecision::AwaitingPair;
|
2026-04-27 01:08:06 -03:00
|
|
|
} else if state.first_camera_remote_pts_us.is_some() && !state.camera_startup_ready {
|
|
|
|
|
if upstream_timing_trace_enabled()
|
|
|
|
|
&& (packet_count <= 10 || packet_count.is_multiple_of(300))
|
|
|
|
|
{
|
|
|
|
|
info!(
|
|
|
|
|
session_id,
|
|
|
|
|
?kind,
|
|
|
|
|
packet_count,
|
|
|
|
|
remote_pts_us,
|
|
|
|
|
"upstream media packet buffered while camera startup warm-up is still in progress"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return UpstreamPlanDecision::AwaitingPair;
|
2026-05-01 12:03:07 -03:00
|
|
|
} else if upstream_require_paired_startup() {
|
|
|
|
|
let refreshed = refresh_unpaired_pairing_anchor(
|
|
|
|
|
&mut state,
|
|
|
|
|
kind,
|
|
|
|
|
remote_pts_us,
|
|
|
|
|
now + playout_delay,
|
|
|
|
|
);
|
|
|
|
|
if refreshed || upstream_timing_trace_enabled() {
|
|
|
|
|
info!(
|
|
|
|
|
session_id,
|
|
|
|
|
?kind,
|
|
|
|
|
packet_count,
|
|
|
|
|
remote_pts_us,
|
|
|
|
|
refreshed_anchor = refreshed,
|
|
|
|
|
healing_window_ms = playout_delay.as_millis(),
|
|
|
|
|
"upstream media pairing window expired; holding one-sided stream for synced startup"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return UpstreamPlanDecision::AwaitingPair;
|
2026-04-25 22:25:24 -03:00
|
|
|
} else {
|
|
|
|
|
let single_stream_base_remote_pts_us = match kind {
|
|
|
|
|
UpstreamMediaKind::Camera => {
|
|
|
|
|
state.first_camera_remote_pts_us.unwrap_or(remote_pts_us)
|
|
|
|
|
}
|
|
|
|
|
UpstreamMediaKind::Microphone => state
|
|
|
|
|
.first_microphone_remote_pts_us
|
|
|
|
|
.unwrap_or(remote_pts_us),
|
|
|
|
|
};
|
|
|
|
|
state.session_base_remote_pts_us = Some(single_stream_base_remote_pts_us);
|
2026-04-26 12:42:17 -03:00
|
|
|
let one_sided_epoch = now + playout_delay;
|
|
|
|
|
state.playout_epoch = Some(one_sided_epoch);
|
|
|
|
|
state.pairing_anchor_deadline = Some(one_sided_epoch);
|
2026-04-25 22:25:24 -03:00
|
|
|
info!(
|
|
|
|
|
session_id,
|
|
|
|
|
?kind,
|
|
|
|
|
single_stream_base_remote_pts_us,
|
|
|
|
|
"upstream media pairing window expired; continuing with one-sided playout"
|
|
|
|
|
);
|
|
|
|
|
self.pairing_state_notify.notify_waiters();
|
|
|
|
|
}
|
2026-04-25 14:00:53 -03:00
|
|
|
}
|
2026-04-25 22:25:24 -03:00
|
|
|
|
|
|
|
|
let session_base_remote_pts_us = state.session_base_remote_pts_us.unwrap_or(remote_pts_us);
|
|
|
|
|
if remote_pts_us < session_base_remote_pts_us {
|
|
|
|
|
if upstream_timing_trace_enabled()
|
|
|
|
|
&& (packet_count <= 10 || packet_count.is_multiple_of(300))
|
|
|
|
|
{
|
|
|
|
|
info!(
|
|
|
|
|
session_id,
|
|
|
|
|
?kind,
|
|
|
|
|
packet_count,
|
|
|
|
|
remote_pts_us,
|
|
|
|
|
session_base_remote_pts_us,
|
|
|
|
|
"upstream media packet dropped before the shared overlap base"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return UpstreamPlanDecision::DropBeforeOverlap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut local_pts_us = remote_pts_us.saturating_sub(session_base_remote_pts_us);
|
2026-04-24 14:49:57 -03:00
|
|
|
let last_slot = match kind {
|
|
|
|
|
UpstreamMediaKind::Camera => &mut state.last_video_local_pts_us,
|
|
|
|
|
UpstreamMediaKind::Microphone => &mut state.last_audio_local_pts_us,
|
|
|
|
|
};
|
|
|
|
|
if let Some(last_pts_us) = *last_slot
|
|
|
|
|
&& local_pts_us <= last_pts_us
|
|
|
|
|
{
|
|
|
|
|
local_pts_us = last_pts_us.saturating_add(min_step_us.max(1));
|
|
|
|
|
}
|
|
|
|
|
*last_slot = Some(local_pts_us);
|
2026-04-25 22:25:24 -03:00
|
|
|
let epoch = *state.playout_epoch.get_or_insert(pairing_deadline);
|
2026-04-30 08:16:57 -03:00
|
|
|
let sink_offset_us = self.playout_offset_us(kind);
|
2026-04-26 00:00:28 -03:00
|
|
|
let playout_delay = upstream_playout_delay();
|
|
|
|
|
let mut due_at =
|
2026-04-25 16:48:20 -03:00
|
|
|
apply_playout_offset(epoch + Duration::from_micros(local_pts_us), sink_offset_us);
|
2026-04-26 00:00:28 -03:00
|
|
|
let mut late_by = now.checked_duration_since(due_at).unwrap_or_default();
|
|
|
|
|
let reanchor_threshold = upstream_reanchor_late_threshold(playout_delay);
|
2026-04-27 02:13:19 -03:00
|
|
|
let reanchor_window_us = upstream_reanchor_window_us(playout_delay);
|
|
|
|
|
if !state.catastrophic_reanchor_done
|
|
|
|
|
&& local_pts_us <= reanchor_window_us
|
|
|
|
|
&& late_by > reanchor_threshold
|
|
|
|
|
{
|
2026-04-26 00:00:28 -03:00
|
|
|
let old_late_by = late_by;
|
|
|
|
|
let desired_due_at = now + playout_delay;
|
|
|
|
|
let unoffset_due_at = apply_playout_offset(desired_due_at, -sink_offset_us);
|
|
|
|
|
let recovered_epoch = unoffset_due_at
|
|
|
|
|
.checked_sub(Duration::from_micros(local_pts_us))
|
|
|
|
|
.unwrap_or(unoffset_due_at);
|
|
|
|
|
state.playout_epoch = Some(recovered_epoch);
|
|
|
|
|
state.pairing_anchor_deadline = Some(desired_due_at);
|
2026-04-26 01:02:27 -03:00
|
|
|
state.catastrophic_reanchor_done = true;
|
2026-04-26 00:00:28 -03:00
|
|
|
due_at = apply_playout_offset(
|
|
|
|
|
recovered_epoch + Duration::from_micros(local_pts_us),
|
|
|
|
|
sink_offset_us,
|
|
|
|
|
);
|
|
|
|
|
late_by = now.checked_duration_since(due_at).unwrap_or_default();
|
|
|
|
|
info!(
|
|
|
|
|
session_id,
|
|
|
|
|
?kind,
|
|
|
|
|
packet_count,
|
|
|
|
|
local_pts_us,
|
|
|
|
|
remote_pts_us,
|
|
|
|
|
old_late_by_ms = old_late_by.as_millis(),
|
|
|
|
|
recovery_buffer_ms = playout_delay.as_millis(),
|
|
|
|
|
reanchor_threshold_ms = reanchor_threshold.as_millis(),
|
|
|
|
|
"upstream media playout epoch reanchored after catastrophic lateness"
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-25 15:49:30 -03:00
|
|
|
if upstream_timing_trace_enabled()
|
|
|
|
|
&& (packet_count <= 10 || packet_count.is_multiple_of(300))
|
|
|
|
|
{
|
2026-04-26 00:00:28 -03:00
|
|
|
let playout_delay_us = due_at.saturating_duration_since(now).as_micros();
|
2026-04-25 16:48:20 -03:00
|
|
|
let late_by_us = late_by.as_micros();
|
2026-04-25 15:49:30 -03:00
|
|
|
info!(
|
|
|
|
|
session_id,
|
|
|
|
|
?kind,
|
|
|
|
|
packet_count,
|
|
|
|
|
remote_pts_us,
|
2026-04-25 22:25:24 -03:00
|
|
|
session_base_remote_pts_us,
|
2026-04-25 15:49:30 -03:00
|
|
|
first_remote_for_kind,
|
2026-04-25 22:25:24 -03:00
|
|
|
remote_elapsed_us = remote_pts_us.saturating_sub(session_base_remote_pts_us),
|
2026-04-25 15:49:30 -03:00
|
|
|
local_pts_us,
|
2026-04-25 16:48:20 -03:00
|
|
|
playout_delay_us,
|
|
|
|
|
sink_offset_us,
|
|
|
|
|
late_by_us,
|
2026-04-25 15:49:30 -03:00
|
|
|
"upstream media rebase sample"
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-25 22:25:24 -03:00
|
|
|
if kind == UpstreamMediaKind::Microphone {
|
|
|
|
|
self.audio_progress_notify.notify_waiters();
|
|
|
|
|
}
|
|
|
|
|
UpstreamPlanDecision::Play(PlannedUpstreamPacket {
|
2026-04-25 16:48:20 -03:00
|
|
|
local_pts_us,
|
|
|
|
|
due_at,
|
|
|
|
|
late_by,
|
2026-04-25 22:25:24 -03:00
|
|
|
})
|
2026-04-24 14:49:57 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 12:03:07 -03:00
|
|
|
fn refresh_unpaired_pairing_anchor(
|
|
|
|
|
state: &mut UpstreamClockState,
|
|
|
|
|
kind: UpstreamMediaKind,
|
|
|
|
|
remote_pts_us: u64,
|
|
|
|
|
next_deadline: Instant,
|
|
|
|
|
) -> bool {
|
|
|
|
|
state.pairing_anchor_deadline = Some(next_deadline);
|
|
|
|
|
match kind {
|
|
|
|
|
UpstreamMediaKind::Camera if state.first_microphone_remote_pts_us.is_none() => {
|
|
|
|
|
state.first_camera_remote_pts_us = Some(remote_pts_us);
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
UpstreamMediaKind::Microphone if state.first_camera_remote_pts_us.is_none() => {
|
|
|
|
|
state.first_microphone_remote_pts_us = Some(remote_pts_us);
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 01:25:06 -03:00
|
|
|
impl Default for UpstreamMediaRuntime {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 14:49:57 -03:00
|
|
|
#[cfg(test)]
|
2026-04-25 16:48:20 -03:00
|
|
|
mod tests;
|