6.3 KiB
EchoionApi - Shared API Layer
Purpose
EchoionApi is the foundation that all other components build upon. It defines the common language, data structures, and contracts that enable Plugin, Core, and Desktop to communicate seamlessly.
Think of it as the "vocabulary" that all components share.
What's Inside
1. Core Types & Error Handling
Defines how the system represents success and failure:
flowchart LR
A[Operation] --> B{Success?}
B -->|Yes| C[Return Value]
B -->|No| D[Error Code + Message]
C --> E[Result<T>]
D --> E
Error Categories:
- Network errors (connection failed, timeout)
- Audio errors (decode failed, format unsupported)
- Permission errors (unauthorized, not admin)
- Input errors (invalid URL, not found)
2. Track & Playback Types
The data models for music:
classDiagram
class TrackInfo {
+string id
+string title
+string artist
+duration milliseconds
+TrackSource source
+string originalUrl
+string streamUrl
}
class QueueEntry {
+uint64 queueId
+TrackInfo track
+string addedBy
+bool isPlaying
}
class PlaybackState {
+QueueEntry currentTrack
+QueueEntry[] queue
+bool isPlaying
+bool isPaused
+float volume
+bool repeat
+bool shuffle
}
TrackInfo --> QueueEntry
QueueEntry --> PlaybackState
3. Core Interfaces
The contracts between components:
ICoreInterface (What Core provides)
classDiagram
class ICoreInterface {
+initialize(configPath)
+play(TrackInfo)
+pause()
+resume()
+stop()
+skip(count)
+queueTrack(TrackInfo, clientId)
+removeFromQueue(queueId)
+getQueue()
+prepareForHotReload() HotReloadState
+restoreAfterHotReload(state)
}
Key Methods:
- Playback Control: play, pause, resume, stop, skip
- Queue Management: queueTrack, removeFromQueue, clearQueue
- Hot Reload: prepareForHotReload, restoreAfterHotReload
IPluginInterface (What Plugin provides)
classDiagram
class IPluginInterface {
+sendTextMessage(targetId, message)
+sendChannelMessage(channelId, message)
+sendAudioData(data)
+setWhisperList(clientIds)
+getClients() ClientInfo[]
+moveClient(clientId, channelId)
+connectToServer(config)
}
Key Methods:
- Messaging: Send text to users/channels
- Audio: Send PCM audio data to TeamSpeak
- Client Management: Get user info, move users
4. Event System (EventBus)
A publish-subscribe mechanism for decoupled communication:
flowchart TB
subgraph "Publishers"
MM[MusicManager]
AD[AudioDecoder]
NC[NetworkClient]
end
subgraph "EventBus"
Q[Event Queue]
R[Router]
end
subgraph "Subscribers"
CM[CommandManager]
PI[PluginInterface]
RS[RpcServer]
LG[Logger]
end
MM -->|TrackQueued| Q
AD -->|PlaybackError| Q
NC -->|NetworkError| Q
Q --> R
R --> CM
R --> PI
R --> RS
R --> LG
Event Types:
- Playback events: PlaybackStarted, PlaybackPaused, TrackFinished
- Queue events: TrackQueued, TrackRemoved, QueueCleared
- Connection events: ClientConnected, ClientDisconnected
- Error events: PlaybackError, NetworkError
5. Cache System
Multi-tier caching for performance:
flowchart LR
Request --> Cache{In Cache?}
Cache -->|Yes| Return[Return Cached]
Cache -->|No| Fetch[Fetch from Source]
Fetch --> Store[Store in Cache]
Store --> Return
subgraph "Cache Types"
HTTP[HTTP Cache<br/>5 min TTL]
Track[Track Info<br/>1 hour TTL]
Meta[Metadata<br/>30 min TTL]
end
Features:
- LRU (Least Recently Used) eviction
- TTL (Time To Live) expiration
- Memory usage tracking
- Thread-safe access
6. Database Interface
Persistence layer for user data:
flowchart TB
subgraph "Database"
P[(Playlists)]
H[(Playback History)]
F[(Favorites)]
S[(Subscriptions)]
end
subgraph "Operations"
CRUD[CRUD Operations]
MIG[Schema Migrations]
Q[Queries]
end
CO[Core] --> CRUD
CRUD --> P
CRUD --> H
CRUD --> F
CRUD --> S
Stored Data:
- User playlists
- Playback history
- Favorite tracks
- Bot subscribers
Component Communication Flow
sequenceDiagram
participant PL as Plugin
participant API as EchoionApi
participant CO as Core
participant DB as Database
PL->>API: ICoreInterface.play(track)
API->>CO: Virtual call
CO->>CO: Process command
CO->>DB: Record in history
CO->>API: ECHOION_PUBLISH(PlaybackStarted)
API->>PL: Event callback
PL->>PL: Update UI
Design Principles
1. Type Safety
All data structures are strongly typed with no void* or raw pointers crossing component boundaries.
2. Error Explicitness
Every operation that can fail returns a Result - you must check for errors.
3. Thread Safety
All public APIs are thread-safe, using internal locking where needed.
4. Zero-Copy Where Possible
Audio data uses memory pooling to avoid unnecessary copying.
5. Version Compatibility
Interfaces are designed to be extended without breaking existing implementations.
Usage Pattern
flowchart LR
subgraph "Your Component"
A[Include headers]
B[Use types]
C[Implement interface]
end
subgraph "EchoionApi"
T[Types]
I[Interfaces]
U[Utilities]
end
A --> T
B --> T
C --> I
B --> U
- Include the API headers
- Use the shared types (TrackInfo, Result, etc.)
- Implement interfaces (ICoreInterface, IPluginInterface)
- Utilize utilities (EventBus, Cache, Database)
Hot Reload State
Special structure for preserving state during hot reload:
flowchart LR
subgraph "HotReloadState"
P[PlaybackState]
Q[Queue]
POS[Position]
CFG[Config]
end
Old[Old Core] -->|Extract| HotReloadState
HotReloadState -->|Restore| New[New Core]
This allows the new Core to resume exactly where the old one left off.