This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Restore dependencies
dotnet restore SharpCaster.sln
# Build all projects
dotnet build SharpCaster.sln
# Build specific project (main library)
dotnet build Sharpcaster/Sharpcaster.csproj
# Build in Release mode
dotnet build --configuration Release Sharpcaster/Sharpcaster.csproj# Run all tests
dotnet test SharpCaster.sln
# Run tests for specific project
dotnet test Sharpcaster.Test/Sharpcaster.Test.csproj
# Run AOT compatibility tests
dotnet test Sharpcaster.Test.Aot/Sharpcaster.Test.Aot.csproj
# Run tests with live output
dotnet test --logger "console;verbosity=detailed"# Create NuGet package
dotnet pack --configuration Release Sharpcaster/Sharpcaster.csprojChromecastClient (Sharpcaster/ChromeCastClient.cs) - Main entry point for Chromecast communication. Manages TCP connection, SSL stream, and coordinates all channels.
Channel-based Architecture - Communication is organized through specialized channels in Sharpcaster/Channels/:
ChromecastChannel- Base class for all channelsMediaChannel- Media playback control (play, pause, stop, seek, queue management)ReceiverChannel- App launching and receiver statusConnectionChannel- Connection managementHeartbeatChannel- Keep-alive functionalityMultiZoneChannel- Multi-room audio supportSpotifyChannel- Spotify-specific integration
Message System - Type-safe message handling in Sharpcaster/Messages/:
- Protocol Buffer messages for low-level communication
- JSON messages for application-level communication
- Strongly-typed message classes with System.Text.Json serialization
Dependency Injection - Uses Microsoft.Extensions.DependencyInjection for channel management and logging.
Async/Await - All operations are async with proper cancellation token support.
Message Routing - Messages are routed by namespace and request ID using SharpCasterTaskCompletionSource for request-response correlation.
Protobuf Integration - Uses Google.Protobuf for efficient binary communication with Chromecast devices.
- .NET Standard 2.0 (broad compatibility)
- .NET 9 (latest features, AOT compatibility)
- Main test suite uses xUnit with Moq for mocking
- Separate AOT test project for Native AOT compatibility verification
- Integration tests require actual Chromecast devices on network
- Test environment always has Chromecast devices connected and available
- Important: Tests can take up to 10 minutes to complete as they perform real network operations with Chromecast devices
Google.Protobuf- Protocol buffer serialization for Cast protocol communicationSystem.Text.Json- JSON serialization (replaced Newtonsoft.Json)Zeroconf- mDNS device discovery for finding Chromecast devices on networkMicrosoft.Extensions.DependencyInjection- Dependency injection containerMicrosoft.Extensions.Logging.Abstractions- Logging framework
The MediaChannel now supports comprehensive Google Cast SDK functionality:
PlayAsync()- Play mediaPauseAsync()- Pause mediaStopAsync()- Stop mediaSeekAsync(double seconds)- Seek to specific timeSetVolumeAsync(double level)- Set media stream volume levelSetMuteAsync(bool muted)- Set media stream mute state
QueueLoadAsync(QueueItem[] items, RepeatModeType repeatMode, int startIndex)- Load queueQueueNextAsync()- Skip to next itemQueuePrevAsync()- Go to previous itemQueueShuffleAsync(bool shuffle)- Enable/disable shuffleQueueSetRepeatModeAsync(RepeatModeType repeatMode)- Set repeat modeQueueInsertAsync(QueueItem[] items, int? insertBefore)- Insert itemsQueueRemoveAsync(int[] itemIds)- Remove itemsQueueReorderAsync(int[] itemIds, int? insertBefore)- Reorder itemsQueueUpdateAsync(QueueItem[] items)- Update itemsQueueGetItemsAsync(int[] ids)- Get queue itemsQueueGetItemIdsAsync()- Get all queue item IDs
SendUserActionAsync(UserAction userAction)- Send user interactionsEditTracksAsync(EditTracksInfoRequest editTracksInfo)- Edit track informationStreamTransferAsync(object transferRequest)- Transfer stream to another deviceSetPlaybackRateAsync(double playbackRate)- Change playback speed
MediaCommand is a comprehensive flags enum supporting all Google Cast SDK commands:
- Basic commands:
PAUSE,SEEK,STREAM_VOLUME,STREAM_MUTE - Queue commands:
QUEUE_NEXT,QUEUE_PREV,QUEUE_SHUFFLE,QUEUE_REPEAT_ALL,QUEUE_REPEAT_ONE - Advanced commands:
EDIT_TRACKS,PLAYBACK_RATE,STREAM_TRANSFER - Social commands:
LIKE,DISLIKE,FOLLOW,UNFOLLOW
GetIndividualCommands()- Extract individual commands from combined flagsSupportsCommand(MediaCommand command)- Check if specific command is supportedGetCommandNames()- Get human-readable command names
- All MediaChannel methods return nullable
MediaStatus?for error handling - Use
MediaStatus.SupportedMediaCommandsto check available commands - Monitor
MediaChannel.StatusChangedevent for real-time updates
The console application (SharpCaster.Console) provides both interactive and command-line interfaces for controlling Chromecast devices. The executable is distributed as sharpcaster for end users.
# Build console application
dotnet build SharpCaster.Console/SharpCaster.Console.csproj
# Run in interactive mode
dotnet run --project SharpCaster.Console/SharpCaster.Console.csproj
# Run with command line arguments
dotnet run --project SharpCaster.Console/SharpCaster.Console.csproj -- <args>help- Show usage informationlist- List available Chromecast devices on networkversion- Show application version information
play <url>- Cast and play media from URLpause- Pause current mediastop- Stop current mediavolume <0.0-1.0>- Set device volume level (ReceiverChannel)media-volume <0.0-1.0>- Set media stream volume level (MediaChannel)seek <seconds>- Seek to specific timestatus- Show current device and media status
-
Discovery Mode (Default):
sharpcaster <device-name> <command>- Uses mDNS discovery to find devices on network
- Supports partial name matching (case-insensitive)
- Example:
sharpcaster "Living Room TV" play "https://example.com/video.mp4"
-
Direct IP Mode:
sharpcaster --ip <ip-address> <command>- Connects directly to device IP address, bypassing discovery
- Faster connection when IP is known
- Useful for automation and scripting
- Example:
sharpcaster --ip 192.168.1.100 play "https://example.com/video.mp4"
--title <title>or-t <title>- Set custom media title--ip <ip-address>or-i <ip-address>- Connect directly to IP (skips discovery)
# Interactive mode
sharpcaster
# List devices
sharpcaster list
# Play media on discovered device
sharpcaster "Office TV" play "https://example.com/video.mp4" --title "My Video"
# Connect directly to IP and play media
sharpcaster --ip 192.168.1.100 play "https://example.com/video.mp4"
# Control playback
sharpcaster "Kitchen Speaker" pause
sharpcaster --ip 192.168.1.100 volume 0.7
sharpcaster "Living Room TV" media-volume 0.5
sharpcaster "Bedroom TV" seek 120
# Check status
sharpcaster "Living Room TV" status- Program.cs - Entry point with dependency injection setup
- CommandLineArgs.cs - Command line argument parsing and validation
- CommandExecutor.cs - Executes commands in non-interactive mode
- DeviceService.cs - Device discovery and connection management
- ApplicationFlows.cs - Interactive mode user interface flows
- Controllers/ - Media and queue control logic
- UI/UIHelper.cs - Console UI utilities using Spectre.Console
When --ip option is used:
- IP address validation using
System.Net.IPAddress.TryParse() - Creates
ChromecastReceiverobject directly with IP:8009 - Skips mDNS discovery entirely for faster connection
- Handles connection failures with clear error messages
- Works with all media control commands