End a sizeless episode stream where it ends when only the feed knew its length

A stream without a file size whose decoder cannot measure its duration
only has the feed's itunes:duration, which is often longer than the
audio. Treat that duration as a hint: a clean close past 80 % of it is
the end. A duration the decoder measured keeps the strict completion
window. The interrupted message for a stream that cannot seek now says
to start over or mark the episode played instead of "press play".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-19 22:11:33 +02:00
1 parent 96e615bf1d
commit 42ecc310fc
6 files changed
+236 -26

No files matched your search

+4 -1
View File
@@ -38,7 +38,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
exit. Completion and listening sessions follow the shared FRID rules. A
stale audio URL triggers one feed check for the episode's new address. A
lost connection stops playback at the saved position (the episode is not
completed and the queue does not move on); a superseded or stalled stream
completed and the queue does not move on). Without a file size from the
server, a closed stream counts as the end within the completion window of
the duration the audio file states, or past 80 % of a duration only the
feed announced; a superseded or stalled stream
is cancelled at the next start or stop; pause pressed while an episode
connects is kept; seeks that do not move the audio are not saved.
- Podcast key bindings in the `podcasts` context: `o`, `/`, `Shift+R`,
+8
View File
@@ -228,6 +228,14 @@ Episodes play in the same queue as music:
the episode is not marked played and the queue does not move on; play
continues from that position. A pause pressed while an episode is still
connecting is kept when the audio arrives.
- Some servers send episodes without a file size. There a closed connection
looks like the end of the file, so Furumi compares the position with the
episode's duration: the one the audio file states, or else the
`itunes:duration` of the feed. Feed durations are often a little off, so
reaching 80 % of one is enough; an episode that stops earlier is treated as
a lost connection. Such servers do not allow seeking either, so play cannot
continue in the middle: start the episode over (`b`) or mark it played
(`m`).
- The position is saved every 20 seconds while playing and on pause, seek,
stop, track change and exit. An episode counts as played within the last
30 seconds (or 5 %) or when its stream reaches its end; a listen enters the episode's
+2
View File
@@ -251,6 +251,8 @@ pub enum AppEvent {
episode: Box<music_dht::podcast::PodcastEpisodeRef>,
start_ms: i64,
duration_ms: Option<i64>,
/// The decoder measured `duration_ms` (else the feed announced it).
duration_measured: bool,
restart: bool,
/// The server allows seeking in the stream.
seekable: bool,
+87 -6
View File
@@ -53,6 +53,9 @@ pub struct EpisodePlayback {
/// The stream plays; until then nothing is written.
started: bool,
duration_ms: Option<i64>,
/// `duration_ms` was measured by the decoder, not only announced by
/// the feed (`PreparedEpisode::duration_measured`).
duration_measured: bool,
/// Last position seen while this playback was current.
position_ms: i64,
/// Actual listening time (no pauses, no seeks).
@@ -342,6 +345,7 @@ pub fn handle_event(
episode,
start_ms,
duration_ms,
duration_measured,
restart,
seekable,
probe,
@@ -354,6 +358,7 @@ pub fn handle_event(
Started {
start_ms,
duration_ms,
duration_measured,
restart,
seekable,
probe,
@@ -446,6 +451,7 @@ pub(crate) fn start_episode(
key: super::state::track_key(track),
started: false,
duration_ms: queued.duration_ms,
duration_measured: false,
position_ms: 0,
listened_ms: 0,
last_tick: Instant::now(),
@@ -482,6 +488,7 @@ pub(crate) fn start_episode(
episode: Box::new(prepared.episode),
start_ms: prepared.start_ms,
duration_ms: prepared.duration_ms,
duration_measured: prepared.duration_measured,
restart,
seekable: prepared.seekable,
probe: prepared.probe,
@@ -503,6 +510,7 @@ pub(crate) fn start_episode(
struct Started {
start_ms: i64,
duration_ms: Option<i64>,
duration_measured: bool,
restart: bool,
seekable: bool,
probe: StreamProbe,
@@ -518,6 +526,7 @@ fn episode_started(
let Started {
start_ms,
duration_ms,
duration_measured,
restart,
seekable,
probe,
@@ -534,6 +543,7 @@ fn episode_started(
playback.probe = Some(probe);
playback.position_ms = start_ms;
playback.duration_ms = duration_ms;
playback.duration_measured = duration_measured;
playback.last_tick = Instant::now();
playback.last_checkpoint = Instant::now();
playback.episode = episode.clone();
@@ -639,7 +649,12 @@ impl EpisodePlayback {
/// last seen position stays what is saved. Returns the commands to send
/// and whether it was played to the end.
fn finish(&mut self, end: StreamEnd, now_ms: i64) -> (Vec<ProgressCommand>, bool) {
if !playback::played_to_the_end(end, self.position_ms, self.duration_ms) {
if !playback::played_to_the_end(
end,
self.position_ms,
self.duration_ms,
self.duration_measured,
) {
return (Vec::new(), false);
}
let position_ms = self
@@ -833,6 +848,7 @@ pub(crate) fn finished(state: &mut AppState, runtime: &mut Runtime) -> Finished
.map_or(StreamEnd::Complete, StreamProbe::end);
let (commands, reached) = playback.finish(end, now_ms());
let position_ms = playback.position_ms;
let seekable = playback.seekable;
for command in commands {
send_progress(runtime, command);
}
@@ -843,11 +859,26 @@ pub(crate) fn finished(state: &mut AppState, runtime: &mut Runtime) -> Finished
tracing::warn!(?end, position_ms, "podcast stream stopped before the end");
runtime.player_start_pending = false;
stop_on_episode(state);
Finished::Interrupted(format!(
"podcast: the audio stopped at {} (the publisher's server stopped sending it); \
the position is saved, press play to continue",
format_clock(position_ms)
))
Finished::Interrupted(interrupted_message(position_ms, seekable))
}
/// Status for an episode whose stream stopped before its end. Without
/// seeking (no file size from the server) Play cannot continue there, so
/// the way out is to start over or mark it played.
fn interrupted_message(position_ms: i64, seekable: bool) -> String {
let at = format_clock(position_ms);
if seekable {
format!(
"podcast: the audio stopped at {at} (the publisher's server stopped sending it); \
the position is saved, press play to continue"
)
} else {
format!(
"podcast: the audio stopped at {at} (the publisher's server stopped sending it); \
the position is saved, but this server does not allow continuing there: start it \
over (b) or mark it played (m) in Podcasts"
)
}
}
/// The audio thread failed while an episode played or started.
@@ -913,6 +944,7 @@ mod tests {
episode,
started: true,
duration_ms,
duration_measured: duration_ms.is_some(),
position_ms: 0,
listened_ms: 0,
last_tick: start,
@@ -1014,6 +1046,55 @@ mod tests {
assert!(unknown.finish(StreamEnd::Unknown, 4).1);
}
/// A stream without a size whose decoder could not measure it (CBR MP3
/// without a Xing header, say) only has the feed's `itunes:duration`,
/// which may be longer than the audio by more than the completion
/// window. Played to its real end, it completes and the queue moves on.
#[test]
fn a_stream_shorter_than_its_feed_duration_completes_at_its_end() {
// The feed says 60 min; the audio ends at 55 min (more than the
// 30 s completion window earlier).
let (mut feed_only, start) = tracked(Some(3_600_000));
feed_only.duration_measured = false;
feed_only.observe(3_300_000, false, at(start, 250), 0);
let (commands, reached) = feed_only.finish(StreamEnd::Unknown, 1);
assert!(reached, "the clean close is the end");
assert!(matches!(
commands.as_slice(),
[ProgressCommand::Complete {
position_ms: 3_600_000,
..
}]
));
// An early close (a fifth of the announced length) is still not.
let (mut early, start) = tracked(Some(3_600_000));
early.duration_measured = false;
early.observe(600_000, false, at(start, 250), 0);
assert!(!early.finish(StreamEnd::Unknown, 2).1);
// A duration the decoder measured keeps the strict window.
let (mut measured, start) = tracked(Some(3_600_000));
measured.observe(3_300_000, false, at(start, 250), 0);
assert!(!measured.finish(StreamEnd::Unknown, 3).1);
// A failed read never counts outside the window.
let (mut failed, start) = tracked(Some(3_600_000));
failed.duration_measured = false;
failed.observe(3_300_000, false, at(start, 250), 0);
assert!(!failed.finish(StreamEnd::Failed, 4).1);
}
#[test]
fn an_interrupted_stream_without_seeking_says_how_to_move_on() {
let message = interrupted_message(65_000, true);
assert!(
message.contains("1:05") && message.contains("press play"),
"{message}"
);
let message = interrupted_message(65_000, false);
assert!(!message.contains("press play"), "{message}");
assert!(message.contains("start it over (b)"), "{message}");
assert!(message.contains("mark it played (m)"), "{message}");
}
#[test]
fn seeks_are_refused_while_connecting_and_without_range_support() {
let (mut playback, _) = tracked(Some(3_600_000));
+36 -10
View File
@@ -44,6 +44,10 @@ pub struct PreparedEpisode {
pub start_ms: i64,
/// Duration from the decoder, else the one the feed announced.
pub duration_ms: Option<i64>,
/// `duration_ms` was measured by the decoder (container header, frame
/// count or file size); otherwise it is only the feed's announcement,
/// which is often off (rounded `itunes:duration`, inserted ads).
pub duration_measured: bool,
/// The server allows seeking (it sent the file size).
pub seekable: bool,
/// How the stream ended, once it did.
@@ -115,17 +119,39 @@ pub fn resolve(
Ok((episode, start_ms))
}
/// Share of a feed-announced duration (percent) that a stream of unknown
/// size must have played before its clean close counts as its end.
const FEED_DURATION_END_PERCENT: i64 = 80;
/// Whether the end of an episode's stream means it was played to the end:
/// a stream that failed (connection lost, URL expired, outage) only counts
/// when it failed within the shared completion window, and a stream of
/// unknown size (where a closed connection looks like the end) only when
/// the position is there too or nothing else is known.
pub fn played_to_the_end(end: StreamEnd, position_ms: i64, duration_ms: Option<i64>) -> bool {
///
/// * a stream that ended where the server said it would is the end;
/// * a stream that failed (connection lost, URL expired, outage) only
/// counts when it failed within the shared completion window;
/// * a stream of unknown size that closed cleanly (where an early close
/// looks like the end) counts when the position is within the completion
/// window of a duration the decoder measured. A duration that only the
/// feed announced is a hint: then reaching [`FEED_DURATION_END_PERCENT`]
/// of it is enough, and with no duration at all the close is the end.
pub fn played_to_the_end(
end: StreamEnd,
position_ms: i64,
duration_ms: Option<i64>,
duration_measured: bool,
) -> bool {
let near_end = duration_ms.is_some() && reaches_completion(position_ms, duration_ms);
match end {
StreamEnd::Complete => true,
StreamEnd::Unknown => duration_ms.is_none() || near_end,
StreamEnd::Failed => near_end,
StreamEnd::Unknown => match duration_ms {
None => true,
Some(_) if duration_measured => near_end,
Some(ms) => {
near_end
|| position_ms.saturating_mul(100)
>= ms.saturating_mul(FEED_DURATION_END_PERCENT)
}
},
}
}
@@ -177,16 +203,16 @@ pub fn open_at(
.into());
}
}
let duration_ms = source
let measured = source
.total_duration()
.and_then(|duration| i64::try_from(duration.as_millis()).ok())
.filter(|ms| *ms > 0)
.or(episode.duration_ms);
.filter(|ms| *ms > 0);
Ok(PreparedEpisode {
episode: episode.clone(),
source,
start_ms,
duration_ms,
duration_ms: measured.or(episode.duration_ms),
duration_measured: measured.is_some(),
seekable,
probe,
})
+99 -9
View File
@@ -618,7 +618,8 @@ async fn a_lost_connection_does_not_complete_the_episode_and_keeps_its_position(
assert!(!playback::played_to_the_end(
prepared.probe.end(),
position_ms,
prepared.duration_ms
prepared.duration_ms,
prepared.duration_measured
));
// What the runtime then saves: the last position, not a completion.
@@ -664,16 +665,35 @@ async fn a_stream_of_unknown_length_that_closes_early_is_not_the_end() {
assert!((29_000..=30_500).contains(&position_ms), "{position_ms} ms");
// A closed connection looks like the end of the file here...
assert_eq!(prepared.probe.end(), StreamEnd::Unknown);
// ...but the duration says otherwise.
// ...but the duration the decoder read from the header says otherwise.
assert_eq!(prepared.duration_ms, Some(120_000));
assert!(prepared.duration_measured);
assert!(!playback::played_to_the_end(
StreamEnd::Unknown,
position_ms,
Some(120_000)
Some(120_000),
true
));
assert!(playback::played_to_the_end(
StreamEnd::Unknown,
119_000,
Some(120_000)
Some(120_000),
true
));
// A duration only the feed announced is a hint: an audio file that ends
// more than the completion window before it still ends there...
assert!(playback::played_to_the_end(
StreamEnd::Unknown,
100_000,
Some(120_000),
false
));
// ...while a close at a quarter of it is still a lost connection.
assert!(!playback::played_to_the_end(
StreamEnd::Unknown,
position_ms,
Some(120_000),
false
));
// A whole file with a size is played to its end.
@@ -693,13 +713,83 @@ async fn a_stream_of_unknown_length_that_closes_early_is_not_the_end() {
assert!(playback::played_to_the_end(
StreamEnd::Complete,
3_000,
prepared.duration_ms
prepared.duration_ms,
prepared.duration_measured
));
// A failure in the last seconds still counts as the end.
// A failure in the last seconds still counts as the end, whatever
// announced the duration; earlier it never does.
for measured in [true, false] {
assert!(playback::played_to_the_end(
StreamEnd::Failed,
119_500,
Some(120_000),
measured
));
assert!(!playback::played_to_the_end(
StreamEnd::Failed,
100_000,
Some(120_000),
measured
));
}
}
/// About `seconds` of silent MPEG-1 Layer III (mono, 44.1 kHz, 128 kbit/s)
/// frames with no Xing/Info header: sent without a size, the decoder
/// cannot measure its duration.
fn mp3_without_header(seconds: u32) -> Vec<u8> {
const FRAME_LEN: usize = 417; // 144 * 128_000 / 44_100
let frames = (u64::from(seconds) * 44_100 / 1_152) as usize;
let mut frame = vec![0u8; FRAME_LEN];
frame[..4].copy_from_slice(&[0xFF, 0xFB, 0x90, 0xC0]);
frame.repeat(frames)
}
/// Decodes `source` to its end; returns the position reached.
fn play_out_any(source: &mut crate::player::DecodedSource) -> i64 {
let per_second = i64::from(source.sample_rate().get()) * i64::from(source.channels().get());
let mut samples = 0i64;
while source.next().is_some() {
samples += 1;
}
samples * 1_000 / per_second
}
/// Without a size from the server and a duration the decoder could
/// measure, only the feed's duration is known, and it may be longer than
/// the audio by more than the completion window. The clean end of the
/// stream then still completes the episode.
#[tokio::test]
async fn a_stream_shorter_than_the_feed_says_completes_at_its_end() {
let f = fixture();
let (_, mut episode) = subscribed(&f).await;
f.server.set(
"/audio/a.mp3",
Route::new(mp3_without_header(100), "audio/mpeg").no_length(),
);
// The feed announces two minutes, 20 s more than the audio.
episode.duration_ms = Some(120_000);
let prepared =
tokio::task::spawn_blocking(move || playback::open_at(&episode, 0, &CancelToken::never()))
.await
.unwrap()
.unwrap();
assert!(!prepared.seekable);
assert!(!prepared.duration_measured, "{:?}", prepared.duration_ms);
assert_eq!(prepared.duration_ms, Some(120_000));
let mut source = prepared.source;
let position_ms = play_out_any(&mut source);
assert!(
(99_000..=100_100).contains(&position_ms),
"{position_ms} ms"
);
assert_eq!(prepared.probe.end(), StreamEnd::Unknown);
assert!(!reaches_completion(position_ms, Some(120_000)));
assert!(playback::played_to_the_end(
StreamEnd::Failed,
119_500,
Some(120_000)
prepared.probe.end(),
position_ms,
prepared.duration_ms,
prepared.duration_measured
));
}