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
use super::*;
// ---------------------------------------------------------------------------
// LocalPublisher
// ---------------------------------------------------------------------------
pub struct LocalPublisher {
pub(crate) inner: Arc<Mount>,
pub(crate) active: bool,
}
impl LocalPublisher {
/// Number of unique client sessions currently playing this mount.
pub fn subscriber_count(&self) -> usize {
self.inner.active_sessions.load(Ordering::Relaxed)
}
/// True after the mount is unregistered or its server begins shutdown.
pub fn is_closed(&self) -> bool {
self.inner.cancel.is_cancelled()
}
/// Subscribe to local-source keyframe requests (track id).
pub fn keyframe_requests(&self) -> tokio::sync::broadcast::Receiver<u8> {
self.inner.keyframe_requests.subscribe()
}
/// Request a keyframe on a track (used by RTCP feedback actions).
pub fn request_keyframe(&self, track_id: u8) {
self.inner.request_keyframe(track_id);
}
/// Push a frame without track validation (for protocol ingest like RTMP
/// where the description may not yet be populated).
pub fn push(&self, frame: AccessUnit) -> Result<PushResult> {
if frame.data.len() > self.inner.max_queued_bytes_per_session {
self.inner.stats.add_dropped(1);
return Ok(PushResult::Dropped);
}
if self.inner.frames.receiver_count() == 0 {
self.inner.stats.add_dropped(1);
// No subscribers: still keep the startup keyframe cache fresh
// (plus the per-track bootstrap for the RTP-Info rtptime base).
// Otherwise the snapshot a new PLAY sees is frozen at the last
// disconnect — replaying it jumps straight from stale frames
// to the live edge, which stalls VLC for seconds.
self.inner.auto_inject_fmtp(&frame);
self.inner.update_bootstrap(&frame);
if let Ok(mut cache) = self.inner.startup_cache.try_lock() {
cache.push(std::sync::Arc::new(frame));
}
return Ok(PushResult::DroppedNoSubscribers);
}
match self.inner.publish_cached(frame) {
Ok(_) => {
self.inner.stats.add_accepted(1);
Ok(PushResult::Accepted)
}
Err(_) => {
self.inner.stats.add_dropped(1);
Ok(PushResult::Dropped)
}
}
}
pub fn try_push(&self, frame: AccessUnit) -> Result<PushResult> {
// O(1) codec validation via the fixed lookup table; no per-frame
// MediaDescription clone + linear track search.
if frame.data.len() > self.inner.max_queued_bytes_per_session {
self.inner.stats.add_dropped(1);
return Ok(PushResult::Dropped);
}
let expected = self
.inner
.codec_by_track
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)[frame.track_id as usize];
let Some(expected) = expected else {
return Err(Error::Media(format!(
"mount has no track {}",
frame.track_id
)));
};
if expected != frame.codec {
return Err(Error::Media(format!(
"track {} expects {:?}, got {:?}",
frame.track_id, expected, frame.codec
)));
}
if self.inner.frames.receiver_count() == 0 {
self.inner.stats.add_dropped(1);
// No subscribers: still keep the startup keyframe cache fresh
// (plus the per-track bootstrap for the RTP-Info rtptime base).
// Otherwise the snapshot a new PLAY sees is frozen at the last
// disconnect — replaying it jumps straight from stale frames
// to the live edge, which stalls VLC for seconds.
self.inner.auto_inject_fmtp(&frame);
self.inner.update_bootstrap(&frame);
if let Ok(mut cache) = self.inner.startup_cache.try_lock() {
cache.push(std::sync::Arc::new(frame));
}
return Ok(PushResult::DroppedNoSubscribers);
}
match self.inner.publish_cached(frame) {
Ok(_) => {
self.inner.stats.add_accepted(1);
Ok(PushResult::Accepted)
}
Err(_) => {
self.inner.stats.add_dropped(1);
Ok(PushResult::Dropped)
}
}
}
pub async fn close(mut self) -> Result<()> {
self.release();
Ok(())
}
fn release(&mut self) {
if self.active {
self.inner.publishers.fetch_sub(1, Ordering::AcqRel);
self.active = false;
tracing::debug!(
mount = %self.inner.id,
publishers = self.inner.publishers.load(Ordering::Relaxed),
"publisher detached"
);
}
}
}
impl Drop for LocalPublisher {
fn drop(&mut self) {
self.release();
}
}