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
use super::segmenter::Segment;
use std::collections::VecDeque;
use std::sync::RwLock;
/// Hard upper bound for the sliding window. A typo in configuration must not
/// pin arbitrarily many segments (and their keyframes) in memory.
pub const MAX_SEGMENTS: usize = 128;
/// Bounded ring buffer for completed segments.
pub struct SegmentRing {
segments: RwLock<VecDeque<Segment>>,
/// Playlist window (`EXT-X-MEDIA-SEQUENCE` .. last).
playlist_segments: usize,
/// Total retained segments. Larger than the playlist window so a
/// client that falls behind (background tab, slow segment fetch) can
/// still fetch a segment that has just left the playlist instead of
/// retrying a 404 forever.
max_segments: usize,
/// Signalled after an accepted [`SegmentRing::push`], so blocking
/// playlist reloads wait for the next segment instead of polling.
pushed: tokio::sync::Notify,
}
impl SegmentRing {
pub fn new(max_segments: usize) -> Self {
Self::with_retention(max_segments, max_segments)
}
/// `playlist_segments` is the advertised window; `retained_segments`
/// (at least the window) is kept for late segment requests.
pub fn with_retention(playlist_segments: usize, retained_segments: usize) -> Self {
let retained = retained_segments.clamp(1, MAX_SEGMENTS);
let playlist = playlist_segments.clamp(1, retained);
Self {
segments: RwLock::new(VecDeque::with_capacity(retained)),
playlist_segments: playlist,
max_segments: retained,
pushed: tokio::sync::Notify::new(),
}
}
/// Push a new segment, evicting the oldest if at capacity.
pub fn push(&self, segment: Segment) {
let mut segments = self
.segments
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
// A segmenter restart or a racing producer must never insert an
// older sequence after a newer one: the playlist would go
// 114,115,116,115 and hls.js would treat it as a seek backwards.
if segments
.back()
.is_some_and(|last| segment.sequence <= last.sequence)
{
return;
}
if segments.len() >= self.max_segments {
segments.pop_front();
}
segments.push_back(segment);
drop(segments);
self.pushed.notify_waiters();
}
/// Get a snapshot of all segments (for playlist generation).
pub fn snapshot(&self) -> Vec<Segment> {
let segments = self
.segments
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let mut snapshot: Vec<Segment> = segments
.iter()
.rev()
.take(self.playlist_segments)
.cloned()
.collect();
// Push already enforces monotonic order, but sorting makes the
// playlist deterministic even if a future producer changes that.
snapshot.sort_by_key(|segment| segment.sequence);
snapshot
}
/// Sequence number of the newest segment.
///
/// Blocking playlist reloads only need the edge, not a cloned window:
/// `snapshot()` clones every retained segment and sorts it.
pub fn latest_sequence(&self) -> Option<u64> {
let segments = self
.segments
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
segments.back().map(|segment| segment.sequence)
}
/// Future that resolves when a segment is pushed.
pub fn notified(&self) -> tokio::sync::futures::Notified<'_> {
self.pushed.notified()
}
/// Get the latest init segment data.
pub fn latest_init(&self) -> Option<bytes::Bytes> {
let segments = self
.segments
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
segments.back().map(|s| s.init.clone())
}
/// Get a specific segment by sequence number.
pub fn get(&self, sequence: u64) -> Option<Segment> {
let segments = self
.segments
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let first = segments.front()?.sequence;
let last = segments.back()?.sequence;
if sequence < first || sequence > last {
return None;
}
// Do not index by (sequence - first): a dropped segment (e.g. a
// broadcast lag in an older producer) leaves a gap and would make
// every later segment 404, which hls.js retries indefinitely.
segments
.iter()
.find(|segment| segment.sequence == sequence)
.cloned()
}
/// Remove all segments (mount restart/reuse).
pub fn clear(&self) {
self.segments
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clear();
}
/// Number of segments currently stored.
pub fn len(&self) -> usize {
self.segments
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.len()
}
pub fn is_empty(&self) -> bool {
self.segments
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zero_capacity_is_normalized_without_panicking() {
let ring = SegmentRing::new(0);
assert!(ring.is_empty());
}
#[test]
fn oversized_window_is_clamped() {
let ring = SegmentRing::new(usize::MAX);
assert_eq!(ring.max_segments, MAX_SEGMENTS);
}
}
#[cfg(test)]
mod retention_tests {
use super::*;
use crate::segmenter::Segment;
use bytes::Bytes;
use std::time::Duration;
fn segment(sequence: u64) -> Segment {
Segment {
sequence,
duration: Duration::from_secs(1),
init: Bytes::new(),
data: Bytes::new(),
starts_with_keyframe: true,
discontinuity: false,
discontinuity_sequence: None,
timestamp: Duration::from_secs(sequence),
format: crate::segmenter::SegmentFormat::Fmp4,
}
}
#[test]
fn playlist_window_is_smaller_than_retention() {
let ring = SegmentRing::with_retention(2, 5);
for sequence in 0..5 {
ring.push(segment(sequence));
}
let playlist = ring.snapshot();
assert_eq!(
playlist.iter().map(|s| s.sequence).collect::<Vec<_>>(),
vec![3, 4]
);
assert!(
ring.get(0).is_some(),
"evicted playlist segment stays fetchable"
);
assert_eq!(ring.len(), 5);
}
}