sonos-stream
⚠️ INTERNAL CRATE - NOT FOR DIRECT USE This crate is an internal implementation detail of the sonos-sdk workspace, specifically designed to be used exclusively by
sonos-state. It is not intended for direct use by end-users and may change at any time without notice.
Overview
sonos-stream provides low-level event streaming and subscription management for Sonos devices with automatic fallback between UPnP events and polling. It serves as the event pipeline foundation for the higher-level sonos-state crate's reactive state management system.
Architecture Role
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ End Users │────▶│ sonos-state │────▶│ sonos-stream │
│ │ │ (Public API) │ │ (Internal) │
└─────────────────┘ └──────────────────┘ └─────────────────┘
▲ ▲
│ │
Property Watchers Event Streaming
State Management UPnP Subscriptions
Why This Crate Exists
This crate was extracted from sonos-state to separate concerns:
- Event Processing: Raw UPnP event handling, parsing, and enrichment
- Subscription Lifecycle: UPnP service subscription management with fallback
- Network Resilience: Automatic firewall detection and polling fallback
- Event Iteration: Optimized sync/async event iteration interfaces
Key Features
- 🔄 Transparent Event/Polling Switching: Automatically switches between UPnP events and polling based on network conditions
- 🔥 Proactive Firewall Detection: Immediately detects firewall blocking and starts polling without delay
- 📡 Complete Event Enrichment: Full event data with source attribution and timing information
- ⚡ Optimized Iteration: Both sync and async iterator patterns for different use cases
- 🛡️ Intelligent Fallback: Automatic fallback to polling when UPnP events become unavailable
- 🔧 Resource Efficient: Shared HTTP clients and connection pools
Internal API Overview
For sonos-state integration only:
// Main broker interface (used by sonos-state)
let mut broker = new.await?;
let reg = broker.register_speaker_service.await?;
// Event consumption (used by sonos-state's StateManager)
let mut events = broker.event_iterator?;
while let Some = events.next_async.await
Event Types
The crate produces EnrichedEvent instances containing:
-
Event Data: Complete state information for each UPnP service
AVTransportEvent- Transport state, track info, position, metadataRenderingControlEvent- Volume, mute, bass, treble, loudnessDevicePropertiesEvent- Zone name, model info, software versionZoneGroupTopologyEvent- Group membership and network topology
-
Event Source: Whether the event came from UPnP notifications or polling
-
Context: Registration ID, speaker IP, service type, timestamp
Network Resilience
The crate handles various network conditions transparently:
- UPnP Events Available: Real-time event notifications (preferred)
- Firewall Blocked: Automatic detection and immediate polling fallback
- Event Timeout: Graceful switching to polling when events stop arriving
- Subscription Failures: Robust error handling with polling as safety net
Dependencies
This internal crate depends on several other internal crates:
callback-server- HTTP server for UPnP event callbackssonos-api- Core Sonos UPnP API definitionssoap-client- Low-level SOAP communicationsonos-discovery- Device discovery utilities
Performance Characteristics
- Low Latency: Direct UPnP event processing when available
- Adaptive: Automatically adjusts polling intervals based on activity
- Memory Efficient: Shared HTTP connection pools and event processors
- CPU Efficient: Event-driven architecture with polling only as fallback
Configuration
The broker supports internal configuration through BrokerConfig:
let config = default
.with_callback_port_range
.with_polling_interval
.with_firewall_detection;
Examples (For Development/Testing Only)
While not intended for end-user consumption, the crate includes examples for development and testing:
# Basic event streaming example
# Async real-time processing
# Firewall handling demonstration
# Filtering and batch processing
Integration with sonos-state
The sonos-state crate uses this crate as follows:
- Event Processing:
StateManagercreates anEventBrokerinternally - Event Conversion: Converts
EnrichedEvent→RawEvent→PropertyUpdate - State Updates: Processes property updates and notifies watchers
- Subscription Management: Automatic service subscriptions based on property demands
Error Handling
The crate provides structured error types:
BrokerError- Event broker operational errorsRegistryError- Speaker/service registration errorsSubscriptionError- UPnP subscription management errorsPollingError- Polling fallback errors
Thread Safety
All public APIs are thread-safe:
EventBrokercan be shared across threads withArc- Event iterators are
Send + Sync - Internal state is protected with appropriate synchronization primitives
Development Notes
For sonos-sdk workspace maintainers:
- Event enrichment happens in
events/processor.rs - Subscription lifecycle in
subscription/manager.rs - Polling strategies in
polling/strategies.rs - Firewall detection integration in
broker.rs
License
MIT OR Apache-2.0
See Also
sonos-state- reactive state managementcallback-server- UPnP event callback infrastructuresonos-api- Core Sonos UPnP API definitions