Resume unknown episode positions from progress, not 0
The web player only knows a podcast episode's position from a handoff or a seek. When Next, auto-advance, a click or a play command makes an episode current, it now reports `position_unknown` instead of 0, and the server takes the resume position (and the register generation) from the relayed progress register before it hands the state to another device. Remote Next/Prev/play commands to a device do the same. Next with repeat all or shuffle wraps past a trailing unplayable item instead of stopping on it. Prev leaves an unplayable item instead of "restarting" it, and seeking such an item moves its kept position. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ab7202fc73
commit
d1be583d1e
8 files changed
+435
-30
No files matched your search
+94
-13
@@ -22,14 +22,16 @@ use std::time::Duration;
|
||||
use anyhow::{Context, Result};
|
||||
use music_dht::device_sync::{
|
||||
DEVICE_SYNC_PROTOCOL_VERSION, DeviceProfileWire, InviteWire, ListenEndReason, ListenEvent,
|
||||
ListenTrackMetadata, PlaybackCommand, PlaybackRepeat, PlaybackSnapshot, PlaybackStateWire,
|
||||
PlaybackTrack, SnapshotLike, SnapshotLikeTombstone, SnapshotPlaylist, SnapshotPlaylistItem,
|
||||
SnapshotPlaylistItemTombstone, SnapshotPlaylistTombstone, SyncOpPayload, SyncOpWire,
|
||||
SyncSnapshot, SyncedFedTrack, WireMessage, encode_invite, hash_secret, op_allowed_for_peer,
|
||||
parse_invite, playback_allowed_for_peer, playback_command_allowed_from_peer,
|
||||
podcast_snapshot_budget, profile_protocol_version, random_hex, relayed_protocol_version,
|
||||
supports_listen_history, supports_podcasts, ticket_endpoint_id,
|
||||
ListenTrackMetadata, PlaybackCommand, PlaybackRepeat, PlaybackSnapshot, PlaybackSource,
|
||||
PlaybackStateWire, PlaybackTrack, SnapshotLike, SnapshotLikeTombstone, SnapshotPlaylist,
|
||||
SnapshotPlaylistItem, SnapshotPlaylistItemTombstone, SnapshotPlaylistTombstone, SyncOpPayload,
|
||||
SyncOpWire, SyncSnapshot, SyncedFedTrack, WireMessage, encode_invite, hash_secret,
|
||||
op_allowed_for_peer, parse_invite, playback_allowed_for_peer,
|
||||
playback_command_allowed_from_peer, podcast_snapshot_budget, profile_protocol_version,
|
||||
random_hex, relayed_protocol_version, supports_listen_history, supports_podcasts,
|
||||
ticket_endpoint_id,
|
||||
};
|
||||
use music_dht::podcast::ProgressRecord;
|
||||
use music_dht::{ByteStream, MusicDhtService, NetworkId, PeerTicket, SecretKey, StreamAcceptor};
|
||||
use serde::Serialize;
|
||||
use sqlx::Row;
|
||||
@@ -2893,8 +2895,14 @@ pub async fn record_web_playback_command(
|
||||
current_state: Option<serde_json::Value>,
|
||||
) -> Result<serde_json::Value> {
|
||||
ensure_web_playback_target(pool, user_id, target_device_id).await?;
|
||||
let next_state = browser_state_for_command(command, payload, current_state)?;
|
||||
let wire = playback_state_from_browser_json(pool, next_state.clone()).await?;
|
||||
let mut next_state = browser_state_for_command(command, payload, current_state)?;
|
||||
let wire = playback_state_from_browser_json(pool, user_id, next_state.clone()).await?;
|
||||
// Show the resume position that was sent, not the 0 the browser picked.
|
||||
if let Some(object) = next_state.as_object_mut()
|
||||
&& object.remove(queue_items::POSITION_UNKNOWN_KEY).is_some()
|
||||
{
|
||||
object.insert("position_seconds".to_string(), json_f64(wire.position_secs));
|
||||
}
|
||||
ensure_playback_supported_by(pool, user_id, target_device_id, &wire).await?;
|
||||
record_local_op(
|
||||
pool,
|
||||
@@ -2923,7 +2931,7 @@ pub async fn record_web_active_transfer(
|
||||
state: serde_json::Value,
|
||||
) -> Result<()> {
|
||||
ensure_web_playback_target(pool, user_id, target_device_id).await?;
|
||||
let wire = playback_state_from_browser_json(pool, state).await?;
|
||||
let wire = playback_state_from_browser_json(pool, user_id, state).await?;
|
||||
// Refuse instead of stripping podcast episodes from the queue.
|
||||
ensure_playback_supported_by(pool, user_id, target_device_id, &wire).await?;
|
||||
let target_name = web_playback_target_name(pool, user_id, target_device_id).await?;
|
||||
@@ -2977,7 +2985,7 @@ pub async fn check_web_transfer(
|
||||
target_device_id: &str,
|
||||
state: serde_json::Value,
|
||||
) -> Result<()> {
|
||||
let wire = playback_state_from_browser_json(pool, state).await?;
|
||||
let wire = playback_state_from_browser_json(pool, user_id, state).await?;
|
||||
ensure_playback_supported_by(pool, user_id, target_device_id, &wire).await
|
||||
}
|
||||
|
||||
@@ -2989,7 +2997,7 @@ pub async fn record_web_active_takeover(
|
||||
) -> Result<()> {
|
||||
ensure_web_playback_target(pool, user_id, previous_device_id).await?;
|
||||
let identity = ensure_identity(pool, user_id, "").await?;
|
||||
let wire = playback_state_from_browser_json(pool, state).await?;
|
||||
let wire = playback_state_from_browser_json(pool, user_id, state).await?;
|
||||
with_playback_engine(pool, user_id, &identity, |engine| {
|
||||
engine.transfer(&identity.device_id, playback_clock())
|
||||
})
|
||||
@@ -3088,6 +3096,11 @@ fn browser_state_for_command(
|
||||
current_state.unwrap_or_else(empty_browser_playback_state)
|
||||
};
|
||||
normalize_browser_playback_state(&mut state);
|
||||
// Only a transfer carries the sender's knowledge of the position; the
|
||||
// commands below decide for themselves.
|
||||
if command != "transfer_state" {
|
||||
browser_object_mut(&mut state).remove(queue_items::POSITION_UNKNOWN_KEY);
|
||||
}
|
||||
|
||||
match command {
|
||||
"play_track" | "play_from_index" | "transfer_state" => {
|
||||
@@ -3097,6 +3110,11 @@ fn browser_state_for_command(
|
||||
"paused",
|
||||
browser_bool(&payload, "paused").unwrap_or(false),
|
||||
);
|
||||
if command != "transfer_state" {
|
||||
// The browser always starts the item at 0. For a podcast
|
||||
// episode, ordinary Play resumes from its progress register.
|
||||
mark_browser_position_unknown(&mut state);
|
||||
}
|
||||
}
|
||||
"pause" => set_browser_bool(&mut state, "paused", true),
|
||||
"resume" => {
|
||||
@@ -3314,11 +3332,20 @@ fn advance_browser_state(state: &mut serde_json::Value, payload: &serde_json::Va
|
||||
set_browser_tracks(state, tracks, next);
|
||||
set_browser_number(state, "position_seconds", 0.0);
|
||||
set_browser_bool(state, "paused", false);
|
||||
if next != current {
|
||||
mark_browser_position_unknown(state);
|
||||
}
|
||||
} else {
|
||||
set_browser_bool(state, "paused", true);
|
||||
}
|
||||
}
|
||||
|
||||
/// The current item changed without its position: a podcast episode resumes
|
||||
/// from its progress register (see `playback_state_from_browser_json`).
|
||||
fn mark_browser_position_unknown(state: &mut serde_json::Value) {
|
||||
set_browser_bool(state, queue_items::POSITION_UNKNOWN_KEY, true);
|
||||
}
|
||||
|
||||
fn rewind_browser_state(state: &mut serde_json::Value) {
|
||||
let tracks = browser_tracks(state);
|
||||
if tracks.is_empty() {
|
||||
@@ -3349,6 +3376,9 @@ fn rewind_browser_state(state: &mut serde_json::Value) {
|
||||
if let Some(previous) = previous {
|
||||
set_browser_tracks(state, tracks, previous);
|
||||
set_browser_bool(state, "paused", false);
|
||||
if previous != current {
|
||||
mark_browser_position_unknown(state);
|
||||
}
|
||||
}
|
||||
set_browser_number(state, "position_seconds", 0.0);
|
||||
}
|
||||
@@ -3495,7 +3525,9 @@ async fn local_playback_snapshot(
|
||||
let state = hub
|
||||
.playback_state_json_for_commands(user_id)
|
||||
.unwrap_or_else(|| serde_json::json!({}));
|
||||
let wire = playback_state_from_browser_json(pool, state).await.ok()?;
|
||||
let wire = playback_state_from_browser_json(pool, user_id, state)
|
||||
.await
|
||||
.ok()?;
|
||||
if !playback_allowed_for_peer(&wire, peer_version) {
|
||||
return None;
|
||||
}
|
||||
@@ -3622,7 +3654,56 @@ fn playback_clock() -> u64 {
|
||||
.as_millis() as u64
|
||||
}
|
||||
|
||||
/// Wire playback state of a browser state.
|
||||
///
|
||||
/// When the browser does not know the position of its current item
|
||||
/// ([`queue_items::POSITION_UNKNOWN_KEY`]) and that item is a podcast
|
||||
/// episode, the position comes from the relayed progress register: handing
|
||||
/// 0 to the next device would be written there as an explicit action and
|
||||
/// restart the episode everywhere.
|
||||
async fn playback_state_from_browser_json(
|
||||
pool: &sqlx::PgPool,
|
||||
user_id: i64,
|
||||
state: serde_json::Value,
|
||||
) -> Result<PlaybackStateWire> {
|
||||
let unknown = browser_bool(&state, queue_items::POSITION_UNKNOWN_KEY) == Some(true);
|
||||
let mut wire = browser_json_to_wire(pool, state).await?;
|
||||
if unknown
|
||||
&& let Some(episode) = wire
|
||||
.queue
|
||||
.get(wire.queue_pos)
|
||||
.and_then(PlaybackTrack::podcast_episode_ref)
|
||||
{
|
||||
let record =
|
||||
podcast_relay::progress_record(pool, user_id, episode.episode_id.as_str()).await?;
|
||||
apply_resume_position(&mut wire, record.as_ref());
|
||||
}
|
||||
Ok(wire)
|
||||
}
|
||||
|
||||
/// Sets the position of the current podcast episode to where ordinary Play
|
||||
/// resumes it (`ProgressRecord::resume_position_ms`, 0 without a register),
|
||||
/// and records the register's generation as seen for the handoff.
|
||||
fn apply_resume_position(state: &mut PlaybackStateWire, record: Option<&ProgressRecord>) {
|
||||
let Some(track) = state.queue.get_mut(state.queue_pos) else {
|
||||
return;
|
||||
};
|
||||
let Some(PlaybackSource::PodcastEpisode(episode)) = track.source.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let Some(record) = record.filter(|record| record.episode_id == episode.episode_id) else {
|
||||
state.position_secs = 0.0;
|
||||
return;
|
||||
};
|
||||
state.position_secs = record.resume_position_ms().max(0) as f64 / 1000.0;
|
||||
episode.progress_generation = Some(
|
||||
episode
|
||||
.progress_generation
|
||||
.map_or(record.generation, |seen| seen.max(record.generation)),
|
||||
);
|
||||
}
|
||||
|
||||
async fn browser_json_to_wire(
|
||||
pool: &sqlx::PgPool,
|
||||
state: serde_json::Value,
|
||||
) -> Result<PlaybackStateWire> {
|
||||
|
||||
@@ -381,7 +381,7 @@ async fn podcast_queue_survives_server_browser_server_round_trip() {
|
||||
}));
|
||||
|
||||
// browser -> server -> peer
|
||||
let outgoing = playback_state_from_browser_json(&pool, browser)
|
||||
let outgoing = playback_state_from_browser_json(&pool, 1, browser)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(outgoing.queue[..4], queue[..]);
|
||||
@@ -412,9 +412,164 @@ async fn malformed_browser_items_keep_the_position_on_its_item() {
|
||||
"index": 2,
|
||||
"track": {"id": 6},
|
||||
});
|
||||
let wire = playback_state_from_browser_json(&pool, browser)
|
||||
let wire = playback_state_from_browser_json(&pool, 1, browser)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(wire.queue.len(), 2);
|
||||
assert_eq!(wire.queue[wire.queue_pos].id, 6);
|
||||
}
|
||||
|
||||
fn progress_record(position_ms: i64, status: ProgressStatus, generation: u64) -> ProgressRecord {
|
||||
let episode = test_episode();
|
||||
ProgressRecord {
|
||||
episode_id: episode.episode_id,
|
||||
podcast_id: episode.podcast_id,
|
||||
position_ms,
|
||||
status,
|
||||
generation,
|
||||
session_id: "session-1".into(),
|
||||
hlc_ms: 1_700_000_000_000,
|
||||
device_id: "tui".into(),
|
||||
op_id: "op-1".into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Browser state of `[library song, episode]` with the song current at 200 s.
|
||||
fn song_then_episode() -> serde_json::Value {
|
||||
let episode = PlaybackTrack::podcast_episode(test_episode());
|
||||
let song = serde_json::json!({
|
||||
"id": 77, "title": "Library song", "content_id": CONTENT_ID,
|
||||
"duration_seconds": 300.0, "stream_url": "/api/player/stream/77",
|
||||
});
|
||||
serde_json::json!({
|
||||
"tracks": [song.clone(), queue_items::external_track_json(&episode, 1, None)],
|
||||
"index": 0,
|
||||
"track": song,
|
||||
"position_seconds": 200.0,
|
||||
"paused": false,
|
||||
"repeat_mode": "off",
|
||||
})
|
||||
}
|
||||
|
||||
fn position_unknown(state: &serde_json::Value) -> bool {
|
||||
state[queue_items::POSITION_UNKNOWN_KEY] == true
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commands_that_select_an_item_do_not_know_its_position() {
|
||||
let current = song_then_episode();
|
||||
// Next and Prev onto another item, and play commands (which the browser
|
||||
// always sends with position 0), leave the position to the server.
|
||||
let next =
|
||||
browser_state_for_command("next", serde_json::json!({}), Some(current.clone())).unwrap();
|
||||
assert_eq!(next["index"], 1);
|
||||
assert_eq!(next["position_seconds"], 0.0);
|
||||
assert!(position_unknown(&next));
|
||||
let mut play = current.clone();
|
||||
play["index"] = serde_json::json!(1);
|
||||
let played = browser_state_for_command("play_from_index", play.clone(), None).unwrap();
|
||||
assert!(position_unknown(&played));
|
||||
let prev =
|
||||
browser_state_for_command("prev", serde_json::json!({}), Some(next.clone())).unwrap();
|
||||
assert_eq!(prev["index"], 0);
|
||||
assert!(position_unknown(&prev));
|
||||
|
||||
// Restarting the same item and seeking are explicit positions.
|
||||
let mut late = next.clone();
|
||||
late["position_seconds"] = serde_json::json!(900.0);
|
||||
let restart = browser_state_for_command("prev", serde_json::json!({}), Some(late)).unwrap();
|
||||
assert_eq!(restart["index"], 1);
|
||||
assert!(!position_unknown(&restart));
|
||||
let repeat = browser_state_for_command(
|
||||
"next",
|
||||
serde_json::json!({"repeat_mode": "one"}),
|
||||
Some(next.clone()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!position_unknown(&repeat));
|
||||
let seek =
|
||||
browser_state_for_command("seek", serde_json::json!({"time": 60.0}), Some(next)).unwrap();
|
||||
assert_eq!(seek["position_seconds"], 60.0);
|
||||
assert!(!position_unknown(&seek));
|
||||
|
||||
// A transfer keeps what the sender knew.
|
||||
let mut handed_off = play;
|
||||
handed_off["position_seconds"] = serde_json::json!(1200.0);
|
||||
let transfer = browser_state_for_command("transfer_state", handed_off.clone(), None).unwrap();
|
||||
assert_eq!(transfer["position_seconds"], 1200.0);
|
||||
assert!(!position_unknown(&transfer));
|
||||
handed_off[queue_items::POSITION_UNKNOWN_KEY] = serde_json::json!(true);
|
||||
let transfer = browser_state_for_command("transfer_state", handed_off, None).unwrap();
|
||||
assert!(position_unknown(&transfer));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unknown_episode_position_resumes_from_the_progress_register() {
|
||||
let pool = offline_pool();
|
||||
let next = browser_state_for_command("next", serde_json::json!({}), Some(song_then_episode()))
|
||||
.unwrap();
|
||||
let mut wire = browser_json_to_wire(&pool, next).await.unwrap();
|
||||
assert_eq!(wire.queue_pos, 1);
|
||||
assert_eq!(wire.position_secs, 0.0);
|
||||
|
||||
// In progress at 1200 s, generation 9 (the queue item has seen 7).
|
||||
let mut resumed = wire.clone();
|
||||
apply_resume_position(
|
||||
&mut resumed,
|
||||
Some(&progress_record(1_200_000, ProgressStatus::InProgress, 9)),
|
||||
);
|
||||
assert_eq!(resumed.position_secs, 1200.0);
|
||||
let episode = resumed.queue[1].podcast_episode_ref().unwrap();
|
||||
assert_eq!(episode.progress_generation, Some(9));
|
||||
// The receiver's explicit write then orders after the register.
|
||||
assert_eq!(
|
||||
ProgressRecord::explicit_generation(episode.progress_generation),
|
||||
10
|
||||
);
|
||||
|
||||
// Completed: ordinary Play starts from the beginning; an older register
|
||||
// does not lower the generation the item has seen.
|
||||
let mut completed = wire.clone();
|
||||
apply_resume_position(
|
||||
&mut completed,
|
||||
Some(&progress_record(1_790_000, ProgressStatus::Completed, 3)),
|
||||
);
|
||||
assert_eq!(completed.position_secs, 0.0);
|
||||
assert_eq!(
|
||||
completed.queue[1]
|
||||
.podcast_episode_ref()
|
||||
.unwrap()
|
||||
.progress_generation,
|
||||
Some(7)
|
||||
);
|
||||
|
||||
// Music keeps the position it has.
|
||||
wire.queue_pos = 0;
|
||||
wire.position_secs = 5.0;
|
||||
let before = wire.clone();
|
||||
apply_resume_position(
|
||||
&mut wire,
|
||||
Some(&progress_record(1_200_000, ProgressStatus::InProgress, 9)),
|
||||
);
|
||||
assert_eq!(wire, before);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn known_positions_and_music_never_read_the_progress_register() {
|
||||
// The offline pool fails any query, so these conversions must not query.
|
||||
let pool = offline_pool();
|
||||
let mut handed_off = song_then_episode();
|
||||
handed_off["index"] = serde_json::json!(1);
|
||||
handed_off["position_seconds"] = serde_json::json!(1200.0);
|
||||
let wire = playback_state_from_browser_json(&pool, 1, handed_off)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(wire.position_secs, 1200.0);
|
||||
|
||||
let mut music = song_then_episode();
|
||||
music[queue_items::POSITION_UNKNOWN_KEY] = serde_json::json!(true);
|
||||
let wire = playback_state_from_browser_json(&pool, 1, music)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(wire.position_secs, 200.0);
|
||||
}
|
||||
@@ -336,6 +336,24 @@ async fn store(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Relayed progress register of an episode, if any.
|
||||
pub(crate) async fn progress_record(
|
||||
pool: &sqlx::PgPool,
|
||||
user_id: i64,
|
||||
episode_id: &str,
|
||||
) -> Result<Option<ProgressRecord>> {
|
||||
let value: Option<Value> = sqlx::query_scalar(
|
||||
"SELECT record_json FROM furumusic__fed_podcast_relay
|
||||
WHERE user_id = $1 AND section = $2 AND record_key = $3",
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(SECTION_PROGRESS)
|
||||
.bind(episode_id)
|
||||
.fetch_optional(pool)
|
||||
.await?;
|
||||
Ok(value.as_ref().and_then(parse))
|
||||
}
|
||||
|
||||
/// Adds the relayed podcast registers to `snapshot`.
|
||||
///
|
||||
/// The sections are bounded later, per message, with the contract budget
|
||||
|
||||
@@ -274,6 +274,10 @@ pub(super) struct PlayerDevicePlaybackStateDto {
|
||||
pub(super) volume: f64,
|
||||
#[serde(default)]
|
||||
pub(super) updated_at_ms: i64,
|
||||
/// The browser does not know the position of its current item, a podcast
|
||||
/// episode it cannot play (`queue_items::POSITION_UNKNOWN_KEY`).
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub(super) position_unknown: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
|
||||
@@ -1472,6 +1472,7 @@ mod device_tests {
|
||||
repeat_mode: "off".to_string(),
|
||||
volume: 0.7,
|
||||
updated_at_ms: current_millis(),
|
||||
position_unknown: false,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1806,6 +1807,7 @@ mod jam_command_tests {
|
||||
repeat_mode: "off".into(),
|
||||
volume: 0.5,
|
||||
updated_at_ms: 0,
|
||||
position_unknown: false,
|
||||
};
|
||||
let redacted = redact_playback_state_for_jam(playback.clone());
|
||||
assert_eq!(redacted.duration_seconds, 0.0);
|
||||
|
||||
@@ -24,6 +24,13 @@ use serde_json::Value;
|
||||
pub(crate) const WIRE_TRACK_KEY: &str = "wire_track";
|
||||
/// Browser track key holding the explicit [`PlaybackSource`] of an external item.
|
||||
pub(crate) const SOURCE_KEY: &str = "source";
|
||||
/// Browser playback state key: `true` when the browser does not know the
|
||||
/// position of its current item. That happens when it selects a podcast
|
||||
/// episode it cannot play (Next, auto-advance, a click or a play command):
|
||||
/// it is never told where the episode was left, and inventing 0 would
|
||||
/// restart the episode for every device. The server then takes the
|
||||
/// position from the episode's progress register.
|
||||
pub(crate) const POSITION_UNKNOWN_KEY: &str = "position_unknown";
|
||||
|
||||
/// Reason shown for federated music that is not in this library.
|
||||
pub(crate) const FED_UNAVAILABLE_REASON: &str = "Not available on this web player yet";
|
||||
|
||||
@@ -757,6 +757,9 @@ document.addEventListener('alpine:init', () => {
|
||||
_listenedSeconds: 0,
|
||||
_lastAudioTime: 0,
|
||||
_localSourceTrackId: null,
|
||||
// The current item is one this player cannot play and nobody told it
|
||||
// that item's position (see _showUnplayableQueueItem).
|
||||
_positionUnknown: false,
|
||||
_remoteExecuting: false,
|
||||
_remoteStateBaseTime: 0,
|
||||
_remoteStateReceivedAt: 0,
|
||||
@@ -888,10 +891,16 @@ document.addEventListener('alpine:init', () => {
|
||||
}
|
||||
if (this._isUnplayableQueueItem(track)) {
|
||||
// Podcast episodes stay in the queue; this player skips them.
|
||||
const playable = this._playableQueueIndex(queue.tracks, idx, idx >= previousIndex ? 1 : -1);
|
||||
const step = idx >= previousIndex ? 1 : -1;
|
||||
let playable = this._playableQueueIndex(queue.tracks, idx, step);
|
||||
if (playable < 0 && (this.repeatMode === 'all' || this.shuffle)) {
|
||||
// Wrap around like the queue itself does.
|
||||
playable = this._playableQueueIndex(queue.tracks, step > 0 ? 0 : queue.tracks.length - 1, step);
|
||||
}
|
||||
if (playable < 0) {
|
||||
// Staying on the current item keeps its position.
|
||||
this._showUnplayableQueueItem(track, track === this.currentTrack ? this.currentTime : 0);
|
||||
// Staying on the current item keeps its position; any
|
||||
// other item's position is unknown here, never 0.
|
||||
this._showUnplayableQueueItem(track, track === this.currentTrack ? this._knownPosition() : null);
|
||||
return;
|
||||
}
|
||||
queue.currentIndex = playable;
|
||||
@@ -924,6 +933,7 @@ document.addEventListener('alpine:init', () => {
|
||||
return;
|
||||
}
|
||||
this._recordHistory('replaced');
|
||||
this._positionUnknown = false;
|
||||
this.currentTrack = track;
|
||||
this._historyTrack = track;
|
||||
this._prefetchedQueueKey = null;
|
||||
@@ -994,12 +1004,17 @@ document.addEventListener('alpine:init', () => {
|
||||
let idx = this._playableQueueIndex(tracks, Number(queue?.currentIndex ?? -1) + 1, 1);
|
||||
if (idx < 0 && this.repeatMode === 'all') idx = this._playableQueueIndex(tracks, 0, 1);
|
||||
if (idx < 0) {
|
||||
this._showUnplayableQueueItem(this.currentTrack, this.currentTime);
|
||||
this._showUnplayableQueueItem(this.currentTrack, this._knownPosition());
|
||||
return;
|
||||
}
|
||||
this.playQueueIndex(idx);
|
||||
},
|
||||
|
||||
_knownPosition() {
|
||||
// The current item's position, or null when this player was never told it.
|
||||
return this._positionUnknown ? null : this.currentTime;
|
||||
},
|
||||
|
||||
toggle() {
|
||||
if (!this.currentTrack) return;
|
||||
if (this.isPlaying) { this.pause(); }
|
||||
@@ -1020,6 +1035,11 @@ document.addEventListener('alpine:init', () => {
|
||||
this._sendRemote('seek', { time: nextTime });
|
||||
return;
|
||||
}
|
||||
if (this._isUnplayableQueueItem(this.currentTrack)) {
|
||||
// Nothing is loaded in <audio>: the seek sets the item's position.
|
||||
this._showUnplayableQueueItem(this.currentTrack, nextTime);
|
||||
return;
|
||||
}
|
||||
audio.currentTime = nextTime;
|
||||
this._lastAudioTime = audio.currentTime || 0;
|
||||
},
|
||||
@@ -1027,7 +1047,7 @@ document.addEventListener('alpine:init', () => {
|
||||
seekRelative(delta) {
|
||||
if (!this.currentTrack) return;
|
||||
const duration = this.duration || audio.duration || 0;
|
||||
const current = this._shouldSendRemote() ? this.currentTime : audio.currentTime;
|
||||
const current = this._shouldSendRemote() || this._isUnplayableQueueItem(this.currentTrack) ? this.currentTime : audio.currentTime;
|
||||
const max = duration > 0 ? duration : Number.MAX_SAFE_INTEGER;
|
||||
this.seek(Math.max(0, Math.min(max, current + delta)));
|
||||
},
|
||||
@@ -1105,7 +1125,10 @@ document.addEventListener('alpine:init', () => {
|
||||
this._sendRemote('prev');
|
||||
return;
|
||||
}
|
||||
if (this.currentTime > 3) {
|
||||
// An item this player cannot play is not playing here, so there
|
||||
// is nothing to restart: Prev goes to the previous item.
|
||||
const restartable = !this._isUnplayableQueueItem(this.currentTrack);
|
||||
if (restartable && this.currentTime > 3) {
|
||||
this.seek(0);
|
||||
return;
|
||||
}
|
||||
@@ -1117,7 +1140,7 @@ document.addEventListener('alpine:init', () => {
|
||||
if (this.repeatMode === 'all') {
|
||||
prevIdx = queue.tracks.length - 1;
|
||||
} else {
|
||||
this.seek(0);
|
||||
if (restartable) this.seek(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1256,11 +1279,15 @@ document.addEventListener('alpine:init', () => {
|
||||
const position = this._currentItemHasLocalAudio(track)
|
||||
? (audio.currentTime || this.currentTime || 0)
|
||||
: (this.currentTime || 0);
|
||||
return this._remotePlaybackPayload(track, {
|
||||
const payload = this._remotePlaybackPayload(track, {
|
||||
position_seconds: position,
|
||||
duration_seconds: this._trackDuration(),
|
||||
paused: !this.isPlaying,
|
||||
});
|
||||
// The server fills the episode's resume position from its progress
|
||||
// register instead of handing 0 to the next device.
|
||||
if (this._positionUnknown && !this._currentItemHasLocalAudio(track)) payload.position_unknown = true;
|
||||
return payload;
|
||||
},
|
||||
|
||||
_mirrorRemoteTrack(track, playing, positionSeconds = null) {
|
||||
@@ -1322,7 +1349,7 @@ document.addEventListener('alpine:init', () => {
|
||||
this._remoteStateReceivedAt = Date.now();
|
||||
|
||||
if (this._isUnplayableQueueItem(track)) {
|
||||
this._showUnplayableQueueItem(track, desiredTime);
|
||||
this._showUnplayableQueueItem(track, state.position_unknown ? null : desiredTime);
|
||||
this._updateMediaSession();
|
||||
return;
|
||||
}
|
||||
@@ -1388,7 +1415,10 @@ document.addEventListener('alpine:init', () => {
|
||||
// This device is now active, so _applyRemotePlaybackState
|
||||
// does not set the time: keep the handed-off position as
|
||||
// the item's position and unload the previous audio.
|
||||
this._showUnplayableQueueItem(track, payload.position_seconds);
|
||||
// Play commands start an item from 0, which for an
|
||||
// episode means "its resume position", unknown here.
|
||||
const known = command.command === 'transfer_state' && payload.position_unknown !== true;
|
||||
this._showUnplayableQueueItem(track, known ? payload.position_seconds : null);
|
||||
} else if (track) {
|
||||
this._playLocal(track, payload);
|
||||
}
|
||||
@@ -1469,7 +1499,10 @@ document.addEventListener('alpine:init', () => {
|
||||
return !!track && !track.unavailable && !!track.stream_url && !this._isUnplayableQueueItem(track);
|
||||
},
|
||||
|
||||
_showUnplayableQueueItem(track, positionSeconds = 0) {
|
||||
_showUnplayableQueueItem(track, positionSeconds = null) {
|
||||
// `positionSeconds` null: nobody told this player where the item
|
||||
// was left (it only knows positions from handoffs and seeks).
|
||||
this._positionUnknown = positionSeconds == null;
|
||||
// Unload the previous source so a later play() cannot resume it.
|
||||
audio.pause();
|
||||
audio.removeAttribute('src');
|
||||
@@ -1596,7 +1629,9 @@ document.addEventListener('alpine:init', () => {
|
||||
queue.currentIndex = idx;
|
||||
const currentTrack = restored.tracks[idx];
|
||||
if (this._isUnplayableQueueItem(currentTrack)) {
|
||||
this._showUnplayableQueueItem(currentTrack, (state.position_ms || 0) / 1000);
|
||||
// A saved 0 is treated as unknown: the server then
|
||||
// uses the episode's progress register.
|
||||
this._showUnplayableQueueItem(currentTrack, state.position_ms ? state.position_ms / 1000 : null);
|
||||
} else if (currentTrack) {
|
||||
this.currentTrack = currentTrack;
|
||||
this._historyTrack = currentTrack;
|
||||
|
||||
@@ -105,13 +105,14 @@ function method(name) {
|
||||
return template.slice(start + 1, end + 11);
|
||||
}
|
||||
|
||||
function activePlayer(queueTracks, currentIndex, audioState = {}) {
|
||||
function activePlayer(queueTracks, currentIndex, audioState = {}, options = {}) {
|
||||
// The browser as the active (local) playback device.
|
||||
const names = [
|
||||
'_executeRemoteCommand', '_applyRemotePlaybackState', '_tickRemoteProgress',
|
||||
'_devicePlaybackStatePayload', '_remotePlaybackPayload', '_trackDuration',
|
||||
'_pauseLocal', '_playLocal', 'resume', 'next', 'playQueueIndex', 'seek',
|
||||
'_skipUnplayableCurrentItem', '_currentItemHasLocalAudio', '_showUnplayableQueueItem',
|
||||
'_pauseLocal', '_playLocal', 'resume', 'next', 'prev', 'playQueueIndex', 'seek',
|
||||
'_skipUnplayableCurrentItem', '_knownPosition', '_currentItemHasLocalAudio',
|
||||
'_showUnplayableQueueItem', '_syncLocalPlaybackState',
|
||||
'_queueItemSourceKind', '_isUnplayableQueueItem', '_playableQueueIndex',
|
||||
];
|
||||
const audio = Object.assign({
|
||||
@@ -133,6 +134,7 @@ function activePlayer(queueTracks, currentIndex, audioState = {}) {
|
||||
};
|
||||
const stores = {queue, devices: {heartbeat() {}}};
|
||||
const context = vm.createContext({audio, Alpine: {store: name => stores[name]}});
|
||||
if (options.random != null) vm.runInContext(`Math.random = () => ${options.random};`, context);
|
||||
vm.runInContext('this.methods = {' + names.map(method).join('\n') + '};', context);
|
||||
return Object.assign(context.methods, {
|
||||
audio, queue,
|
||||
@@ -162,6 +164,7 @@ test('handoff of a podcast episode keeps its position for the next handoff', ()
|
||||
assert.equal(p.audio.src, '', 'the previous track is unloaded');
|
||||
const reported = p._devicePlaybackStatePayload();
|
||||
assert.equal(reported.position_seconds, 1200);
|
||||
assert.equal(reported.position_unknown, undefined, 'a handed-off position is known');
|
||||
assert.equal(reported.index, 1);
|
||||
assert.equal(reported.paused, true);
|
||||
}
|
||||
@@ -196,3 +199,103 @@ test('resume on an unplayable item skips it, also with repeat one', () => {
|
||||
wrap.resume();
|
||||
assert.equal(wrap.currentTrack.id, 1);
|
||||
});
|
||||
|
||||
test('an episode reached without its position reports it as unknown, never as 0', () => {
|
||||
// Next from the last music track lands on a trailing episode.
|
||||
const p = activePlayer([song(1), episode], 0, {currentTime: 200, src: '/api/player/stream/1'});
|
||||
p.isPlaying = true;
|
||||
p.currentTime = 200;
|
||||
p.next();
|
||||
assert.equal(p.currentTrack, episode);
|
||||
assert.equal(p.isPlaying, false);
|
||||
let reported = p._devicePlaybackStatePayload();
|
||||
assert.equal(reported.index, 1);
|
||||
assert.equal(reported.position_unknown, true);
|
||||
// Still unknown after Play finds nothing to skip to.
|
||||
p.resume();
|
||||
assert.equal(p._devicePlaybackStatePayload().position_unknown, true);
|
||||
// A seek is an explicit position.
|
||||
p.seek(600);
|
||||
reported = p._devicePlaybackStatePayload();
|
||||
assert.equal(reported.position_seconds, 600);
|
||||
assert.equal(reported.position_unknown, undefined);
|
||||
// Music never reports the flag.
|
||||
p.playQueueIndex(0);
|
||||
assert.equal(p.currentTrack.id, 1);
|
||||
assert.equal(p._devicePlaybackStatePayload().position_unknown, undefined);
|
||||
|
||||
// Another browser or a Jam guest plays the episode row (position 0 sent).
|
||||
const remote = activePlayer([song(1), episode], 0, {currentTime: 30, src: '/api/player/stream/1'});
|
||||
remote._executeRemoteCommand({command: 'play_from_index', payload: {
|
||||
tracks: [song(1), episode], index: 1, track: episode, position_seconds: 0, paused: false,
|
||||
}});
|
||||
assert.equal(remote.currentTrack, episode);
|
||||
assert.equal(remote._devicePlaybackStatePayload().position_unknown, true);
|
||||
// A transfer passes on what the previous device did not know.
|
||||
const relay = activePlayer([song(1)], 0);
|
||||
relay._executeRemoteCommand({command: 'transfer_state', payload: {
|
||||
tracks: [song(1), episode], index: 1, track: episode, position_seconds: 0,
|
||||
position_unknown: true, paused: true,
|
||||
}});
|
||||
assert.equal(relay._devicePlaybackStatePayload().position_unknown, true);
|
||||
// A mirrored state with a known position keeps it.
|
||||
const mirror = activePlayer([song(1), episode], 0);
|
||||
mirror._syncLocalPlaybackState({tracks: [song(1), episode], index: 1, track: episode, position_seconds: 900, paused: true});
|
||||
assert.equal(mirror._devicePlaybackStatePayload().position_seconds, 900);
|
||||
assert.equal(mirror._devicePlaybackStatePayload().position_unknown, undefined);
|
||||
});
|
||||
|
||||
test('repeat all and shuffle continue past a trailing unplayable item', () => {
|
||||
const wrap = activePlayer([song(1), episode], 0, {currentTime: 200, src: '/api/player/stream/1'});
|
||||
wrap.repeatMode = 'all';
|
||||
wrap.isPlaying = true;
|
||||
wrap.next();
|
||||
assert.equal(wrap.currentTrack.id, 1);
|
||||
assert.equal(wrap.queue.currentIndex, 0);
|
||||
assert.deepEqual(wrap.audio.played, ['/api/player/stream/1']);
|
||||
|
||||
// Shuffle picks the episode (index 1 of 3) and continues with music
|
||||
// (searching towards the pick from the current item).
|
||||
const shuffled = activePlayer([song(1), episode, song(2)], 2, {src: '/api/player/stream/2'}, {random: 0.5});
|
||||
shuffled.shuffle = true;
|
||||
shuffled.next();
|
||||
assert.equal(shuffled.currentTrack.id, 1);
|
||||
assert.equal(shuffled.isPlaying, false, 'the stub audio does not fire play events');
|
||||
assert.deepEqual(shuffled.audio.played, ['/api/player/stream/1']);
|
||||
// A trailing episode picked by shuffle wraps to the first playable item.
|
||||
const trailing = activePlayer([song(1), song(2), episode], 1, {src: '/api/player/stream/2'}, {random: 0.9});
|
||||
trailing.shuffle = true;
|
||||
trailing.next();
|
||||
assert.equal(trailing.currentTrack.id, 1);
|
||||
|
||||
// Without repeat the queue ends on the episode, as before.
|
||||
const stop = activePlayer([song(1), episode], 0, {src: '/api/player/stream/1'});
|
||||
stop.next();
|
||||
assert.equal(stop.currentTrack, episode);
|
||||
assert.equal(stop.isPlaying, false);
|
||||
});
|
||||
|
||||
test('prev and seek work on a handed-off episode', () => {
|
||||
const p = activePlayer([song(1), song(3)], 0, {currentTime: 95, src: '/api/player/stream/1'});
|
||||
p._executeRemoteCommand({command: 'transfer_state', payload: {
|
||||
tracks: [song(1), episode], index: 1, track: episode,
|
||||
position_seconds: 1200, duration_seconds: 1800, paused: false,
|
||||
}});
|
||||
// Seeking moves the episode's own position (nothing is loaded in <audio>).
|
||||
p.seek(1500);
|
||||
assert.equal(p.currentTime, 1500);
|
||||
assert.equal(p._devicePlaybackStatePayload().position_seconds, 1500);
|
||||
// Prev goes to the previous item instead of restarting the episode.
|
||||
p.prev();
|
||||
assert.equal(p.currentTrack.id, 1);
|
||||
assert.deepEqual(p.audio.played, ['/api/player/stream/1']);
|
||||
|
||||
// At the start of the queue Prev leaves the episode and its position alone.
|
||||
const first = activePlayer([episode, song(1)], 0);
|
||||
first._executeRemoteCommand({command: 'transfer_state', payload: {
|
||||
tracks: [episode, song(1)], index: 0, track: episode, position_seconds: 1200, paused: true,
|
||||
}});
|
||||
first.prev();
|
||||
assert.equal(first.currentTrack, episode);
|
||||
assert.equal(first.currentTime, 1200);
|
||||
});
|
||||
Reference in new issue
Block a user