Skip to content

feat(player): add gapless queue handoff and SPA navigation#248

Open
tui2019 wants to merge 50 commits into
sozercan:mainfrom
tui2019:feature/implement-queues
Open

feat(player): add gapless queue handoff and SPA navigation#248
tui2019 wants to merge 50 commits into
sozercan:mainfrom
tui2019:feature/implement-queues

Conversation

@tui2019

@tui2019 tui2019 commented May 9, 2026

Copy link
Copy Markdown

Description

This PR adds best-effort gapless playback by mirroring Kaset's expected next queue occurrence into YouTube Music's native Up Next queue. It also keeps the YouTube Music app shell warm and prefers the web player's SPA router before falling back to a full watch page load.

Kaset's local queue remains the source of truth. Manual Next/Previous actions—including media keys—advance Kaset's queue through deterministic play(song:) / loadVideo(videoId:) navigation. Natural track-end playback uses the native queue only after the expected source/target relationship has been confirmed. Kaset commits the handoff only when the media-bound observer reports the expected target; stale events, wrong tracks, queue edits, failed injection, or timeout all fall back to deterministic loading.

AI Prompt (Optional)

🤖 AI Prompt Used
Implement gapless playback by intercepting YouTube Music's native "Up Next" queue and injecting Kaset's local queue items via JavaScript. Update the track navigation to use the web player's SPA router (`navigateEndpoint`) instead of fully reloading the page.

AI Tools: Gemini CLI for the initial implementation; Codex for follow-up diagnosis, hardening, tests, and documentation.

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 📚 Documentation update
  • 🎨 UI/UX improvement
  • ♻️ Refactoring (no functional changes)
  • 🧪 Test update
  • 🔧 Build/CI configuration

Related Issues

Fixes #241
Fixes #245

Changes Made

  • Native queue handoff: Injects the exact expected next queue occurrence through YouTube Music's own Play next command, then verifies the rendered queue before trusting it.
  • Best-effort gapless playback: Lets natural track end use YouTube Music's native auto-advance only after confirmed injection; a wrong target or three-second timeout falls back to deterministic Kaset navigation.
  • Occurrence-scoped playback identity: Binds bridge events to document, observer epoch, media generation, and media video ID so stale metadata or duplicate ended events cannot move the queue backward or double-advance it.
  • Deterministic manual navigation: Keeps Next/Previous buttons and media keys on Kaset's local queue path, with bounded/coalesced recovery when stale WebView metadata conflicts with the selected target.
  • Queue maintenance hardening: Cancels stale radio/mix maintenance after external queue replacement, preserves task-local mutations, handles queue edits during handoff, and follows ephemeral Smart Shuffle entries even when the persisted signature is unchanged.
  • SPA routing and preload: Keeps the singleton playback WebView mounted when needed, preloads the YouTube Music app shell, prefers watchEndpoint router navigation, and generation-guards full-load fallbacks.
  • Restoration and autoplay safety: Restores deferred sessions and saved seek positions without hidden playback, while explicit Resume/Next/Previous actions unblock autoplay.
  • API exploration: Adds api-explorer queue-probe for inspecting YouTube Music next/radio queue response shapes.
  • Regression coverage: Adds focused tests for queue injection, native handoff, playback identity, persistence synchronization, navigation recovery, repeat/duplicate tracks, and stale async work; serializes CI unit tests and replaces an equalizer timing sleep with an explicit persistence await.
  • Documentation and agent guidance: Adds ADR-0026, updates docs/playback.md, and expands the playback WebView debugging skill with the new queue/handoff components.

Testing

  • swift build
  • swiftlint --strict --quiet
  • swift test --skip KasetUITests --disable-xctest --no-parallel
  • GitHub Actions build, API Explorer, lint/format, unit-test, and UI-test checks pass
  • Structured autoreview/adversarial review passed with no accepted/actionable findings
  • Manual playback testing performed on macOS 26+

Checklist

  • My code follows the project's style guidelines
  • I have run swiftlint --strict && swiftformat .
  • I have added tests that prove my fix/feature works
  • New and existing unit tests pass locally
  • I have updated documentation if needed
  • I have checked for any performance implications
  • My changes generate no new warnings

Screenshots

Bildschirmaufnahme.2026-05-09.um.8.35.21.PM.mov

Additional Notes

This is best-effort gapless playback through YouTube Music's WebView player, not sample-perfect native audio-engine gapless playback. Real-world seamlessness still depends on YouTube Music buffering, WebKit timing, network state, and YouTube's internal player behavior. When native queue state cannot be proven correct, Kaset deliberately prioritizes queue correctness over gaplessness.

Copilot AI review requested due to automatic review settings May 9, 2026 18:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds tighter WebView/PlayerService coordination to improve playback continuity: native YouTube Music queue injection for gapless transitions, router-based navigation to reduce full reloads, and startup autoplay suppression/restoration handling.

Changes:

  • Inject the expected next track into YouTube Music’s native “Up Next” queue and advance locally when YTM auto-advances.
  • Add startup autoplay blocking knobs and restoration flow that waits for a server-restored track before falling back.
  • Prefer in-page router navigation and preload the YTM home shell once logged in.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
Sources/Kaset/Views/SingletonPlayerWebView+QueueInjection.swift Adds JS-based “Play next” injection via DOM + JSON.stringify interception.
Sources/Kaset/Views/SingletonPlayerWebView+PlaybackControls.swift Adds a JS toggle to block/unblock autoplay at runtime.
Sources/Kaset/Views/SingletonPlayerWebView+ObserverScript.swift Honors the autoplay-block flag in playback observers.
Sources/Kaset/Views/MiniPlayerWebView.swift Adds home preload, router navigation, updated autoplay bootstrap flags.
Sources/Kaset/Views/MiniPlayerViews.swift Allows the persistent player view to exist without an immediate videoId.
Sources/Kaset/Views/MainWindow.swift Keeps the persistent web view alive while logged in (even without pending video).
Sources/Kaset/Services/Player/PlayerService.swift Tracks restoration wait state and last injected web-queue videoId.
Sources/Kaset/Services/Player/PlayerService+WebQueueSync.swift Implements web-queue sync/injection and restoration metadata application.
Sources/Kaset/Services/Player/PlayerService+Queue.swift Re-syncs web queue when persisting queue/session changes.
Sources/Kaset/Services/Player/PlayerService+PlaybackRestoration.swift Defers restored playback until server-restored track is observed (or times out).
Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift Syncs web queue after playback start; changes resume/next/previous autoplay behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift
Comment thread Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift
Comment thread Sources/Kaset/Services/Player/PlayerService+WebQueueSync.swift Outdated
Comment thread Sources/Kaset/Views/MiniPlayerWebView.swift Outdated
Comment thread Sources/Kaset/Views/SingletonPlayerWebView+PlaybackControls.swift
Comment thread Sources/Kaset/Views/MiniPlayerWebView.swift Outdated
Comment thread Sources/Kaset/Views/MiniPlayerWebView.swift
sozercan added 2 commits July 6, 2026 20:19
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>

# Conflicts:
#	Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift
#	Sources/Kaset/Services/Player/PlayerService+Queue.swift
#	Sources/Kaset/Views/MiniPlayerWebView.swift
#	Sources/Kaset/Views/SingletonPlayerWebView+PlaybackControls.swift
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Copilot AI review requested due to automatic review settings July 7, 2026 06:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 14/14 changed files
  • Comments generated: 1
  • Review effort level: Medium

Comment thread Sources/Kaset/Services/Player/PlayerService+WebQueueSync.swift Outdated
@sozercan
sozercan force-pushed the feature/implement-queues branch from db07ab2 to 1e4f415 Compare July 7, 2026 07:01
Copilot AI review requested due to automatic review settings July 7, 2026 07:23
@sozercan
sozercan force-pushed the feature/implement-queues branch from 1e4f415 to 695f39c Compare July 7, 2026 07:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 15/15 changed files
  • Comments generated: 0 new
  • Review effort level: Medium

Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
@sozercan
sozercan force-pushed the feature/implement-queues branch from 695f39c to f49ee0b Compare July 7, 2026 18:19
Copilot AI review requested due to automatic review settings July 7, 2026 19:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 21/21 changed files
  • Comments generated: 0 new
  • Review effort level: Medium

sozercan added 2 commits July 7, 2026 19:23
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Copilot AI review requested due to automatic review settings July 8, 2026 02:31
@sozercan
sozercan force-pushed the feature/implement-queues branch from 2a10062 to b2f23e3 Compare July 8, 2026 02:31
Copilot AI review requested due to automatic review settings July 12, 2026 01:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 37/37 changed files
  • Comments generated: 3
  • Review effort level: Medium

Comment thread Sources/Kaset/Services/Player/PlayerService+WebQueueSync.swift Outdated
Comment thread Sources/Kaset/Services/Player/PlayerService+ManualSeek.swift Outdated
Comment thread Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift Outdated
@sozercan sozercan changed the title Implement queues, native router feat(player): add gapless queue handoff and SPA navigation Jul 12, 2026
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Copilot AI review requested due to automatic review settings July 12, 2026 02:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 38/38 changed files
  • Comments generated: 1
  • Review effort level: Medium

Comment thread Tests/KasetTests/PlayerServiceWebQueueSyncFollowUpTests.swift Outdated
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Copilot AI review requested due to automatic review settings July 12, 2026 03:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 39/39 changed files
  • Comments generated: 1
  • Review effort level: Medium

Comment thread Sources/Kaset/Services/Player/PlayerService.swift Outdated
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Copilot AI review requested due to automatic review settings July 12, 2026 03:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 39/39 changed files
  • Comments generated: 2
  • Review effort level: Medium

Comment thread Sources/Kaset/Views/SingletonPlayerWebView+NavigationState.swift
Comment thread Sources/Kaset/Views/SingletonPlayerWebView+QueueInjection.swift
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Copilot AI review requested due to automatic review settings July 12, 2026 03:48
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 40/40 changed files
  • Comments generated: 4
  • Review effort level: Medium

Comment thread Sources/Kaset/Services/Player/PlayerService+Queue.swift
Comment thread Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift
Comment thread Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift Outdated
Comment thread Sources/Kaset/Services/Player/PlayerService+WebQueueSync.swift

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

sozercan added 2 commits July 12, 2026 00:41
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 46/46 changed files
  • Comments generated: 3
  • Review effort level: Medium

Comment thread Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift
Comment thread Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift
Comment thread Sources/Kaset/Views/SingletonPlayerWebView+ObserverScript.swift

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Signed-off-by: Sertac Ozercan <sozercan@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 46/46 changed files
  • Comments generated: 2
  • Review effort level: Medium

Comment thread Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift
Comment thread Sources/Kaset/Services/Player/PlayerService+PlaybackControls.swift
sozercan added 2 commits July 12, 2026 05:32
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Reject outgoing media observations during a receipt-time-bounded navigation grace, while allowing persistent mismatches to reach recovery. Wait for YouTube Music's selected source queue row before invoking Play Next so its empty-queue fallback cannot start the target immediately.

Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Next track does not auto-play [Feature]: Seamless switching between songs

3 participants