Casting media
Casting lets the player discover compatible playback targets on the local network and send the current media player output to one selected target. The feature keeps discovery state separate from the player view and exposes a small control surface from the player controls. See the external media integrations overview and the related Lively Wallpaper integration page.
Cast contexts
Cast state
The cast context is the shared source of truth for the current discovery watcher and active external target. It is shared by the cast view model and the rest of the core layer so the selected target remains tied to the app session rather than to one view instance.
References: Screenbox.Core/…/CastContext.cs• L7-14 Screenbox.Core/…/ServiceHelpers.cs• L59-64
Renderer model
A renderer is the app's user-facing model for an external playback target. It supplies the target name, type, icon, and audio/video capabilities, while its availability marks whether the target can still receive playback.
References: Screenbox.Core/…/Renderer.cs• L5-31
Casting service
Service contract
The cast service provides two operations: create a watcher for the current media player and set or clear its active renderer. This contract keeps target discovery and player configuration out of the view model while returning a success result for the change of target.
References: Screenbox.Core/…/ICastService.cs• L7-17
Player bridge
The current implementation connects casting to the VLC-backed media player. It creates a renderer watcher only for that player type and passes the selected renderer's native target to VLC; clearing the renderer returns playback to local output.
References: Screenbox.Core/…/CastService.cs• L8-22 Screenbox.Core/…/VlcMediaPlayer.cs• L16-17 Screenbox.Core/…/VlcMediaPlayer.cs• L264-268
Renderer discovery
The discovery flow uses the media engine's renderer discovery facility to track external targets as they appear and disappear. The watcher owns this short-lived process and reports changes through found and lost events.
flowchart LR
A[Cast flyout opens] --> B[Create renderer watcher]
B --> C[Start discovery]
C --> D[Renderer found]
D --> E[Add target to cast list]
C --> F[Renderer lost]
F --> G[Remove target from cast list]
H[Cast flyout closes] --> I[Stop and dispose watcher]
Watcher lifecycle
The watcher starts discovery once and retains the active renderer models until it stops. Stopping releases the discovery resource and invalidates the renderer models, so a later session must discover targets again.
References: Screenbox.Core/…/RendererWatcher.cs• L11-30 Screenbox.Core/…/RendererWatcher.cs• L33-70
Target events
A found event adds a renderer to the available target set. A lost event removes the matching renderer and tells the consumer that the target is no longer usable.
References: Screenbox.Core/…/RendererWatcher.cs• L72-87 Screenbox.Core/…/RendererEventArgs.cs• L6-24
Cast-control view model
Discovery controls
The cast-control view model turns watcher events into an observable target list for the cast flyout. It starts discovery when the flyout opens and stops it when the flyout closes, which limits network discovery to the time when the user can act on its results.
References: Screenbox.Core/…/CastControlViewModel.cs• L14-53 Screenbox/…/CastControl.xaml.cs• L19-26
Cast selection
The view model permits casting only when the selected renderer is still available. A successful service call records the active renderer in the shared cast context and changes the view model to its casting state.
References: Screenbox.Core/…/CastControlViewModel.cs• L18-23 Screenbox.Core/…/CastControlViewModel.cs• L71-88
Cast stopping
Stopping casting clears the renderer on the current media player and in the shared context. The view model then returns to discovery so the user can choose another target.
References: Screenbox.Core/…/CastControlViewModel.cs• L90-99
Cast-control view
The cast flyout presents discovered renderer names and lets the user select one. During casting it replaces the discovery list with the active device and a stop action, so the view reflects the same state that the view model and context hold.
References: Screenbox/…/CastControl.xaml• L22-55 Screenbox/…/CastControl.xaml• L57-125 Screenbox/…/PlayerControls.xaml.cs• L66-70
Cast flow
The complete user flow connects the player controls, discovery watcher, service contract, and shared state as one path.
sequenceDiagram
participant User
participant Controls as Player controls
participant VM as Cast-control view model
participant Service as Cast service
participant Player as VLC media player
participant Target as External renderer
User->>Controls: Open cast flyout
Controls->>VM: Start discovery
VM->>Service: Create watcher
Service-->>VM: Renderer watcher
VM->>Target: Discover available targets
Target-->>VM: Found renderer
User->>VM: Select target and cast
VM->>Service: Set active renderer
Service->>Player: Set renderer target
Player->>Target: Send playback output
User->>VM: Stop casting
VM->>Service: Clear active renderer
Service->>Player: Clear renderer target
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the
Rendereravailability rules aligned with the watcher's lost-target events and the view model's cast command guard. - Keep
ICastServicebehavior aligned with the media-player types accepted byCastService; a new player type needs both watcher creation and renderer selection support. - Keep flyout open and close behavior aligned with watcher ownership so discovery does not continue after the cast view is gone.
- Keep successful renderer changes synchronized across the media player,
CastContext.ActiveRenderer, and the view model's casting state.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No current media player exists when discovery starts | The cast list stays empty and no watcher is created | StartDiscovering returns until PlayerContext.MediaPlayer is available |
The current player is not a VlcMediaPlayer | Watcher creation throws, or renderer selection returns false | Keep casting tied to the supported player bridge |
| The selected renderer is no longer available | The cast command cannot run, and a lost target is removed from the list | Renderer.IsAvailable gates casting and lost events clear the selection |
| Renderer discovery cannot start | No targets are reported to the cast view | Check the watcher start result when changing discovery behavior |
Key files
| File | Why to read it |
|---|---|
| Screenbox.Core/…/CastContext.cs | Shared active renderer and watcher state |
| Screenbox.Core/…/Renderer.cs | User-facing target model and availability state |
| Screenbox.Core/…/RendererWatcher.cs | Discovery lifecycle and target events |
| Screenbox.Core/…/ICastService.cs | Casting service contract |
| Screenbox.Core/…/CastService.cs | Bridge from the service contract to the media player |
| Screenbox.Core/…/CastControlViewModel.cs | Discovery, selection, cast, and stop behavior |
Screenbox/Controls/CastControl.xaml | Cast flyout states and user actions |
| Screenbox/…/CastControl.xaml.cs | Flyout lifecycle and view-model binding |
| Screenbox/…/PlayerControls.xaml.cs | Entry point from the player controls |