Switchy UPnP
UPnP/DLNA library for the MoosicBox ecosystem, providing device discovery and basic media renderer control functionality for Universal Plug and Play devices.
Features
- UPnP Device Discovery: Automatic discovery of UPnP devices on the network
- Device Caching: Cache discovered devices and services for efficient access
- Media Renderer Control: Basic control of UPnP/DLNA media renderers
- Transport Control: Play, pause, stop, and seek operations
- Volume Control: Get and set volume levels on UPnP devices
- Service Management: Access and interact with UPnP services
- Event Subscriptions: Subscribe to UPnP device state changes
- Metadata Handling: Basic media metadata support
Installation
Add this to your Cargo.toml:
[]
= "0.1.4"
Usage
Device Discovery and Caching
use ;
// Scan for UPnP devices
scan_devices.await?;
// Get all discovered devices
let device_list = devices.await;
for device in device_list
// Get specific device by UDN
let device = get_device?;
Media Transport Control
use ;
// Get the device and AVTransport service
let device = get_device?;
let service = get_service?;
let device_url = device.url;
// Control playback
play.await?; // Play at normal speed
pause.await?; // Pause
stop.await?; // Stop
seek.await?; // Seek to 2 minutes
Setting Media URI
use ;
// Get the device and AVTransport service
let device = get_device?;
let service = get_service?;
let device_url = device.url;
// Set the media to play
set_av_transport_uri.await?;
Volume Control
use ;
// Get the device and RenderingControl service
let device = get_device?;
let service = get_service?;
let device_url = device.url;
// Get current volume
let volume_info = get_volume.await?;
let current_volume = volume_info.get.map_or;
println!;
// Set volume to 75
set_volume.await?;
Getting Device Information
use ;
// Get the device and AVTransport service
let device = get_device?;
let service = get_service?;
let device_url = device.url;
// Get transport state
let transport = get_transport_info.await?;
println!;
// Get position information
let position = get_position_info.await?;
println!;
// Get media information
let media = get_media_info.await?;
println!;
Event Subscriptions
use ;
use StreamExt;
// Get the device and service
let device = get_device?;
let service = get_service?;
let device_url = device.url;
// Subscribe to device events
let = subscribe_events.await?;
// Handle events
while let Some = event_stream.next.await
Error Types
The library provides several error types:
ActionError: Errors when performing UPnP actionsScanError: Errors during device discovery and scanningUpnpDeviceScannerError: Errors in the device scanner
Core Types
Feature Flags
api: Enable Actix Web API endpointsopenapi: Enable OpenAPI documentationsimulator: Enable device simulation for testingfail-on-warnings: Treat warnings as errors for this crate and core dependencies
Web API Endpoints
When the api feature is enabled:
GET /scan-devices - Scan and return list of UPnP devices
GET /transport-info - Get transport information
GET /media-info - Get media information
GET /position-info - Get position information
GET /volume - Get volume
POST /volume - Set volume
POST /subscribe - Subscribe to device events
POST /pause - Pause playback
POST /play - Play
POST /seek - Seek to position
API Usage Examples
# Discover UPnP devices
# Get transport info
# Control playback
# Set volume
Testing
# Run all tests
# Run with specific features
# Test with simulator
Troubleshooting
Common Issues
No Devices Found
- Check network connectivity and firewall settings
- Ensure devices are on the same network subnet
- Verify multicast is enabled on network interface
- Try increasing discovery timeout
Connection Refused
- Verify device URLs are accessible
- Check if devices require authentication
- Ensure correct protocol (HTTP vs HTTPS)
- Validate device descriptions and service URLs
Playback Issues
- Verify media format compatibility
- Check network bandwidth and stability
- Ensure media URLs are accessible from renderer
- Validate media metadata and MIME types
Event Subscription Failures
- Check if device supports event subscriptions
- Verify callback URL is accessible from device
- Ensure subscription timeout is reasonable
- Check for network address translation issues
See Also
moosicbox_player- Audio playback enginemoosicbox_audio_output- Audio output backendsmoosicbox_session- Session managementmoosicbox_music_api- Music API abstractionsswitchy_http- HTTP client utilities