mirror of
https://github.com/narrrl/proton-stream.git
synced 2026-11-03 16:41:26 +01:00
perf(android,ios): warm what an episode opens with, and the next one near the end
The bridge's open_stream started no fetch until mpv asked, so every open was the head and then the tail, one after the other. It now warms both edges as the desktop does, and the resume block too when the episode resumes. warm_episode fetches another episode's opening blocks into the cache without playing it. Both phones call it for the next episode in the last two minutes or once the credits start, as the desktop already did, so autoplay starts from the cache.
This commit is contained in:
1 parent
8d795c29e6
commit
2546bab0a6
3 files changed
+108
-2
No files matched your search
@@ -118,6 +118,9 @@ private const val CONTROLS_SECONDS = 4L
|
|||||||
/** How long the "up next" card counts down before it loads the next episode. */
|
/** How long the "up next" card counts down before it loads the next episode. */
|
||||||
private const val UP_NEXT_SECONDS = 10
|
private const val UP_NEXT_SECONDS = 10
|
||||||
|
|
||||||
|
/** How close to the end the next episode's opening blocks are fetched. */
|
||||||
|
private const val WARM_NEXT_SECONDS = 120.0
|
||||||
|
|
||||||
/** Past this much of an episode it counts as watched. */
|
/** Past this much of an episode it counts as watched. */
|
||||||
internal const val WATCHED_FRACTION = 0.9
|
internal const val WATCHED_FRACTION = 0.9
|
||||||
|
|
||||||
@@ -360,6 +363,21 @@ fun PlayerScreen(
|
|||||||
if (!playingThis) return@remember null
|
if (!playingThis) return@remember null
|
||||||
runCatching { skipOffer(chapters, state.position) }.getOrNull()
|
runCatching { skipOffer(chapters, state.position) }.getOrNull()
|
||||||
}
|
}
|
||||||
|
// Near the end, fetch the next episode's opening blocks, so autoplay or
|
||||||
|
// "Play now" starts from the cache. The last two minutes or the credits,
|
||||||
|
// whichever comes first: late enough that stopping after one episode has
|
||||||
|
// not cost a download, early enough to matter.
|
||||||
|
val nearEnd = playingThis && state.duration > 0.0 && (
|
||||||
|
state.duration - state.position < WARM_NEXT_SECONDS ||
|
||||||
|
chapters.creditsStart?.let { state.position >= it } == true
|
||||||
|
)
|
||||||
|
LaunchedEffect(key, nearEnd) {
|
||||||
|
val upcoming = next ?: return@LaunchedEffect
|
||||||
|
if (!nearEnd) return@LaunchedEffect
|
||||||
|
runCatching {
|
||||||
|
NativeRuntime.engine().warmEpisode(upcoming.shareId, upcoming.volumeId, upcoming.linkId)
|
||||||
|
}
|
||||||
|
}
|
||||||
// Auto-skip is offered once per chapter: seeking back into an opening on
|
// Auto-skip is offered once per chapter: seeking back into an opening on
|
||||||
// purpose must not be undone by the setting that skipped it.
|
// purpose must not be undone by the setting that skipped it.
|
||||||
val skipped = remember(key) { mutableSetOf<Long>() }
|
val skipped = remember(key) { mutableSetOf<Long>() }
|
||||||
|
|||||||
@@ -1972,6 +1972,47 @@ impl AndroidEngine {
|
|||||||
.map_err(BridgeError::from_display)?
|
.map_err(BridgeError::from_display)?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch the blocks an episode opens with into the cache, without playing
|
||||||
|
/// it: the next episode, near the end of this one, so that autoplay or
|
||||||
|
/// "Play now" starts from the cache rather than from two round trips.
|
||||||
|
///
|
||||||
|
/// Quietly nothing for a downloaded episode, and on any failure: all a
|
||||||
|
/// failure costs is the head start.
|
||||||
|
pub async fn warm_episode(
|
||||||
|
self: Arc<Self>,
|
||||||
|
share_id: String,
|
||||||
|
volume_id: String,
|
||||||
|
link_id: String,
|
||||||
|
) {
|
||||||
|
let runtime = Arc::clone(&self.runtime);
|
||||||
|
let warmed = spawned(&runtime, async move {
|
||||||
|
let offline = self
|
||||||
|
.catalog
|
||||||
|
.lock()
|
||||||
|
.offline_file(&share_id, &link_id)
|
||||||
|
.ok()
|
||||||
|
.flatten()
|
||||||
|
.is_some();
|
||||||
|
if offline {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let Ok(connection) = self.connection().await else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
match self
|
||||||
|
.open_from_source(&connection, &share_id, &node_uid(&volume_id, &link_id))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(stream) => stream.warm_edges(),
|
||||||
|
Err(error) => log::debug!("warming the next episode failed: {error}"),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
if let Err(error) = warmed {
|
||||||
|
log::debug!("warming the next episode stopped: {error}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn download_episode(
|
pub async fn download_episode(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
share_id: String,
|
share_id: String,
|
||||||
@@ -2670,8 +2711,35 @@ impl AndroidEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let connection = self.connection().await?;
|
let connection = self.connection().await?;
|
||||||
self.open_from_source(&connection, share_id, &node_uid(volume_id, link_id))
|
let stream = self
|
||||||
.await
|
.open_from_source(&connection, share_id, &node_uid(volume_id, link_id))
|
||||||
|
.await?;
|
||||||
|
// Before mpv has the handle: building its core and surface takes a
|
||||||
|
// moment, and the network can spend that moment on the two reads
|
||||||
|
// every file opens with — and, resuming, on the one it seeks to.
|
||||||
|
match self.resume_fraction(share_id, link_id) {
|
||||||
|
Some(fraction) => stream.warm_resume(fraction),
|
||||||
|
None => stream.warm_edges(),
|
||||||
|
}
|
||||||
|
Ok(stream)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How far into the file an episode resumes, as a fraction of its
|
||||||
|
/// running time, from what was saved the last time it played. `None` for
|
||||||
|
/// one that starts from the beginning, as the clients do for a finished
|
||||||
|
/// episode or one barely begun.
|
||||||
|
fn resume_fraction(&self, share_id: &str, link_id: &str) -> Option<f64> {
|
||||||
|
let state = self
|
||||||
|
.catalog
|
||||||
|
.lock()
|
||||||
|
.watch_state(share_id, link_id)
|
||||||
|
.ok()
|
||||||
|
.flatten()?;
|
||||||
|
if state.watched || state.position_secs < pstr_core::library::RESUME_FLOOR_SECS {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let duration = state.duration_secs.filter(|duration| *duration > 0.0)?;
|
||||||
|
Some(state.position_secs / duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn download_episode_inner(
|
async fn download_episode_inner(
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ private let watchedFraction = 0.9
|
|||||||
/// How long a stall lasts before the player offers to open the episode again.
|
/// How long a stall lasts before the player offers to open the episode again.
|
||||||
private let stallLimit: TimeInterval = 20
|
private let stallLimit: TimeInterval = 20
|
||||||
|
|
||||||
|
/// How close to the end the next episode's opening blocks are fetched.
|
||||||
|
private let warmNextSeconds = 120.0
|
||||||
|
|
||||||
/// How an episode is named in watch state and player readings.
|
/// How an episode is named in watch state and player readings.
|
||||||
func mediaKey(_ episode: EpisodeRecord) -> String { "\(episode.shareId):\(episode.linkId)" }
|
func mediaKey(_ episode: EpisodeRecord) -> String { "\(episode.shareId):\(episode.linkId)" }
|
||||||
|
|
||||||
@@ -115,6 +118,8 @@ final class PlayerHost {
|
|||||||
/// Chapters already auto-skipped in this episode: seeking back into an
|
/// Chapters already auto-skipped in this episode: seeking back into an
|
||||||
/// opening on purpose must not be undone by the setting that skipped it.
|
/// opening on purpose must not be undone by the setting that skipped it.
|
||||||
@ObservationIgnored private var skipped = Set<Int64>()
|
@ObservationIgnored private var skipped = Set<Int64>()
|
||||||
|
/// The episode whose next one has been warmed; see `tick`.
|
||||||
|
@ObservationIgnored private var warmedAfter: String?
|
||||||
@ObservationIgnored private var pausedByInterruption = false
|
@ObservationIgnored private var pausedByInterruption = false
|
||||||
@ObservationIgnored private var artworkFor: String?
|
@ObservationIgnored private var artworkFor: String?
|
||||||
|
|
||||||
@@ -390,6 +395,21 @@ final class PlayerHost {
|
|||||||
skipped.insert(chapter.index).inserted {
|
skipped.insert(chapter.index).inserted {
|
||||||
player.seek(to: offer.target)
|
player.seek(to: offer.target)
|
||||||
}
|
}
|
||||||
|
// Near the end, fetch the next episode's opening blocks, so autoplay
|
||||||
|
// or "Play now" starts from the cache. The last two minutes or the
|
||||||
|
// credits, whichever comes first: late enough that stopping after one
|
||||||
|
// episode has not cost a download, early enough to matter.
|
||||||
|
let nearEnd = player.duration > 0 && (
|
||||||
|
player.duration - player.position < warmNextSeconds
|
||||||
|
|| chapters.creditsStart.map { player.position >= $0 } == true
|
||||||
|
)
|
||||||
|
if nearEnd, let key, warmedAfter != key, let next {
|
||||||
|
warmedAfter = key
|
||||||
|
let (shareId, volumeId, linkId) = (next.shareId, next.volumeId, next.linkId)
|
||||||
|
Task.detached {
|
||||||
|
try? await NativeRuntime.engine().warmEpisode(shareId: shareId, volumeId: volumeId, linkId: linkId)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Re-read the chapters when there is a reason to: a different file, or a
|
/// Re-read the chapters when there is a reason to: a different file, or a
|
||||||
|
|||||||
Reference in new issue
Block a user