Skip to content

feat(app): add LAN companion remote, handoff, and cross-device streaming - #273

Open
Aryan447 wants to merge 3 commits into
mainfrom
companion-mode-v2
Open

Aryan447 wants to merge 3 commits into
mainfrom
companion-mode-v2

Conversation

@Aryan447

@Aryan447 Aryan447 commented Dec 6, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes the LocalPreferenceTheme not present crash in CompanionPreferencesScreen and expands Companion Mode with Watch Together sync playback, chapter navigation, remote sleep timer, moment bookmarks, searchable/filterable peer title browsing, and automatic Wi-Fi reconnect.

Fixes & Improvements

  • Settings Crash Fix: Wrapped CompanionPreferencesScreen in ProvidePreferenceLocals so HapticSwitchPreference has LocalPreferenceTheme.
  • Auto-Reconnect on Wi-Fi/LAN: Added ConnectivityManager.NetworkCallback in CompanionManager to automatically start/refresh server & NSD discovery when Wi-Fi or Ethernet connects.
  • Full Searchable Peer Titles Browser: Removed the 25-item cap when browsing a phone/peer's library from TV/tablet and added search + All/Movies/Shows filters.

New Companion Features

  • Watch Together (Sync Watch): Opens the peer's active stream on the phone at the current timestamp without pausing the TV/tablet so both screens play together.
  • Remote Sleep Timer: Set 15m, 30m, 45m, 60m, or End of video sleep timer on the TV/tablet from the phone with a live countdown.
  • Chapters Navigator: Browse all chapters of the playing video on the phone and tap any chapter to jump.
  • Moment Bookmarks: Save timestamped bookmarks while watching and jump back to them in one tap.

Before / After

  • Before: Opening Settings → Companion Mode crashed with LocalPreferenceTheme not present; peer title browser showed at most 25 unfiltered items; no sleep timer, chapter list, bookmarks, or Watch Together mode.
  • After: Settings → Companion Mode opens cleanly; full searchable peer title browsing, Watch Together, Sleep Timer, Chapters, and Moment Bookmarks work over LAN.

How measured

Unmeasured (crash fix verified by inspection + CI build/unit tests).

Verification

  • git diff --check: passed cleanly.
  • GitHub Actions CI Build and Unit Tests triggered on companion-mode-v2.

Changelog

  • Updated UNRELEASED.md under Added and Fixed.

Risks

  • Requires both devices on the same Wi-Fi/LAN subnet.

Summary by Sourcery

Expand Companion Mode with cross-device playback controls and browsing while making its settings screen open reliably.

New Features:

  • Add Watch Together playback that starts the peer’s active stream locally at its current position without interrupting the peer.
  • Add remote chapter navigation, sleep timers, and persistent moment bookmarks to Companion Mode.
  • Add search and movie/show filters to peer library browsing, showing the full list of titles.

Bug Fixes:

  • Prevent a crash when opening Companion Mode settings due to a missing preference theme local.

Enhancements:

  • Automatically refresh Companion Mode connectivity and discovery when Wi-Fi or Ethernet becomes available.

Tests:

  • Extend companion protocol tests to cover chapter and sleep-timer playback state and bookmark serialization.

Chores:

  • Update the unreleased changelog with the Companion Mode additions and settings crash fix.

…ing (#271)

* feat(app): add LAN companion remote, handoff, and cross-device streaming

Add local-network Companion Mode so a phone can control a tablet or TV on the same Wi-Fi/LAN, browse and stream titles in both directions, and hand off playback at the exact timestamp.

Uses Android NSD (_mpvium._tcp.), NanoHTTPD with Range 206 streaming and SSE state updates, 4-digit PIN pairing, a haptic jog wheel and gesture touchpad, second-screen track/subtitle/MediaInfo/Anime4K controls, URL throwing, and Late-Night Private Audio.

* fix(app): resolve Compose TV modifier and mpv command signatures in companion UI

* fix(app): use existing R.string.close in CompanionScreen
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Deploying mpvium with  Cloudflare Pages  Cloudflare Pages

Latest commit: 11742d2
Status: ✅  Deploy successful!
Preview URL: https://3c5b1cfd.mpvium.pages.dev
Branch Preview URL: https://companion-mode-v2.mpvium.pages.dev

View logs

@sourcery-ai

sourcery-ai Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Schema and migrations

Add a persistent preference key for storing Companion moment bookmarks.

  • app/src/main/java/app/aryan447/mpvium/preferences/CompanionPreferences.kt L37-41

Data structures

Extend the Companion protocol models with chapter and sleep-timer playback state, bookmark records, and commands for chapter navigation and sleep timers.

  • app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionModels.kt L74-90, L113-114, L181-182

API

Expose Watch Together playback and bookmark operations through CompanionManager so the UI can start synchronized playback, save moments, and revisit or delete bookmarks.

  • app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt L116-122, L367-427

Implementation

Reconnect Companion Mode networking and refresh discovery when Wi-Fi or Ethernet becomes available or is lost.

  • app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt L5-8, L671-709 (+3 small edits)

Publish chapter and sleep-timer state from the player bridge and handle remote chapter jumps and timer requests.

  • app/src/main/java/app/aryan447/mpvium/ui/player/CompanionPlayerBridge.kt L187-194, L289-296, L301-336 (+4 small edits)

UI

Add peer-library search and kind filters, Watch Together and bookmark actions, chapter navigation, sleep-timer controls, and a saved-bookmarks list to Companion Mode.

  • app/src/main/java/app/aryan447/mpvium/ui/companion/CompanionScreen.kt L1029-1057, L1407-1445, L1504-1597 (+13 small edits)

Provide the preference locals required by the Companion Mode preference controls to prevent the settings-screen crash.

  • app/src/main/java/app/aryan447/mpvium/ui/preferences/CompanionPreferencesScreen.kt L47, L77-84, L188

Add localized labels for Companion library filters and the new Watch Together, chapter, sleep-timer, and bookmark controls.

  • app/src/main/res/values/strings.xml L1141-1154

Documentation

Record the Companion Mode additions and the preference-screen crash fix in the unreleased changelog.

  • UNRELEASED.md L2, L15

Tests

Extend Companion protocol tests to verify serialization of the new playback-state fields and bookmark records.

  • app/src/test/java/app/aryan447/mpvium/domain/companion/CompanionNetworkAndProtocolTest.kt L44-45, L62-71

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 16 issues

Sourcery assessment

Needs a human reviewer. 15 findings to address first, and bookmarks are saved in preferences, so incorrect entries can remain after a rollback, but the list is capped and users can delete them. The new remote playback controls can cause ordinary playback mistakes, which reverting the change would stop.

Blocking findings: app/src/main/java/app/aryan447/mpvium/ui/player/CompanionPlayerBridge.kt:6, app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt:378, app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt:686, app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt:700, app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt:685, and 10 more


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

import app.aryan447.mpvium.domain.anime4k.Anime4KManager
import app.aryan447.mpvium.domain.companion.CompanionChapterInfo
import app.aryan447.mpvium.domain.companion.CompanionCommand
import app.aryan447.mpvium.domain.companion.CompanionChapterInfo

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Critical · App fails to compile

When the Kotlin compiler processes this file, the duplicate CompanionChapterInfo imports conflict, so compilation fails and the app cannot build.

Remove the duplicate import.

val url = when {
state.activeStreamUrl.startsWith("http://") || state.activeStreamUrl.startsWith("https://") -> state.activeStreamUrl
state.mediaId.isNotBlank() -> client.buildStreamUrl(peer, state.mediaId, token)
else -> return

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Watch Together skips local files

When the peer is playing a local file, CompanionPlayerBridge.updateActiveMediaMeta has no callers, leaving mediaId blank while activeStreamUrl falls back to the player path. syncWatchWithPeer returns without starting playback, and bookmarks save no media ID, so a later jump seeks whichever item is currently playing.

Populate and publish the active media ID for local playback, and use it in Watch Together and bookmarks.

Also at app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt:377, app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt:393, app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt:410.

if (preferences.enabled.get()) {
refreshNetworkStatus()
if (server == null) start() else discovery.startDiscovery(preferences.deviceId.get())
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Reconnected peer stays unreachable

When the peer's LAN address changes while this device reconnects, discovery.startDiscovery can find the peer's new address, but _connectedPeer and the SSE observer keep using the old CompanionDeviceInfo. Remote state and commands continue targeting the stale address, so the peer remains unreachable until the user reconnects manually.

Update the active peer connection and restart its observer with the newly discovered address.

}
}
}
networkCallback = callback

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Automatic reconnect stays disabled

When ConnectivityManager.registerNetworkCallback throws, networkCallback is assigned before registration, and runCatching swallows the exception. Later calls hit the non-null guard and skip registration, so automatic reconnect remains disabled for the manager's lifetime.

Clear networkCallback when registration fails so a later call can retry.

Also at app/src/main/java/app/aryan447/mpvium/domain/companion/CompanionManager.kt:701.

delay(400)
if (preferences.enabled.get()) {
refreshNetworkStatus()
if (server == null) start() else discovery.startDiscovery(preferences.deviceId.get())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Concurrent starts leak servers

When a manual start and an onAvailable callback (or two matching network callbacks) enter start() together, start() checks and assigns server without synchronization, so an explicit start and a network callback can both create and start servers before either assignment; one server is then overwritten and left running, with inconsistent discovery and port state.

Serialize server startup and recheck the running server inside that serialized section.


val filtered = remember(items, query, kindFilter) {
items.filter { item ->
val matchesKind = kindFilter == "all" || item.kind == kindFilter

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Series disappear from Shows filter

When a peer library contains items with kind series and the user selects Shows, the Shows chip sets kindFilter to episode, so matchesKind excludes peer library items whose kind is series; callers cannot find those titles while filtering for shows.

Treat both series and episode kinds as matches for the Shows filter.

sleepTimerRemainingSec = -1
sleepTimerJob = scope.launch(Dispatchers.Main) {
while (MpvPlayer.isReady) {
val pos = MpvPlayer.mpv.getPropertyDouble("time-pos") ?: 0.0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Sleep timer waits through next video

When the player advances to another queued video before the next timer poll, the end-of-video job samples the current video's time-pos and duration without tracking which media it started on. It then waits for the next video to end before pausing, so playback continues past the intended end.

Track the media the job started on and stop the timer when playback advances to another item.

Also at app/src/main/java/app/aryan447/mpvium/ui/player/CompanionPlayerBridge.kt:317-318.

"movie" to R.string.companion_filter_movies,
"episode" to R.string.companion_filter_shows,
).forEach { (key, labelRes) ->
FilterChip(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · TV controls lack focus rings

When the Companion screen is navigated with a TV remote, FilterChip and bookmark Surface/IconButton omit the project's tvFocusHighlight modifier, so TV users navigating to these controls get no clear D-Pad focus ring and cannot readily identify the current target.

Apply tvFocusHighlight to each newly added interactive control.

Also at app/src/main/java/app/aryan447/mpvium/ui/companion/CompanionScreen.kt:1545, app/src/main/java/app/aryan447/mpvium/ui/companion/CompanionScreen.kt:1568, app/src/main/java/app/aryan447/mpvium/ui/companion/CompanionScreen.kt:1587.

From AGENTS.md L125-128:

11. Keep the app fully navigable by remote control for Android TV and Google TV.
    Ensure interactive elements maintain clear D-Pad focus rings, handle
    DPAD/remote key events cleanly, and adapt layouts (e.g., width constraints
    on settings and sheets) for wide 16:9 television viewing.

onClick = {
haptic.performHapticFeedback(HapticFeedbackType.SegmentTick)
onCommand(CompanionCommand(action = CompanionCommand.ACTION_SET_SLEEP_TIMER, intValue = mins))
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Older peers silently ignore new controls

When the phone has this update but the connected host is running an older Companion Mode version, an older peer decodes the new action as a string but has no handler for it; its command path still reports success. The sleep timer appears selectable on the phone, but the host does nothing and the UI receives no error.

Advertise protocol capabilities and gate the new controls on host support, or make unsupported actions return an explicit error.

Comment thread UNRELEASED.md
@@ -1,4 +1,5 @@
### Added
- Added Watch Together sync playback, chapter navigation, remote sleep timer, moment bookmarks, searchable peer title filtering, and automatic Wi-Fi reconnect to Companion Mode.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚪ Low · Changelog remains stale

When the PR changes user-visible behavior, the user-facing additions and fix are recorded only in UNRELEASED.md, while CHANGELOG.md has no corresponding notes, leaving the repository's required paired release-note update incomplete.

Add corresponding user-facing notes to CHANGELOG.md in this same change.

Also at UNRELEASED.md:3, UNRELEASED.md:15.

From AGENTS.md L233:

- Update both files in the same change that alters user-visible behavior.

This branch has not been deployed

No deployments
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.

1 participant