# EchoionPlugin - TeamSpeak Integration
## Purpose
EchoionPlugin is the bridge between EchoionCore and TeamSpeak 3. It handles all TeamSpeak-specific functionality, translating between the TeamSpeak SDK's C API and Echoion's modern C++ interfaces.
```mermaid
flowchart TB
subgraph "TeamSpeak 3"
TS[TeamSpeak Client]
SDK[TeamSpeak SDK
C API]
end
subgraph "EchoionPlugin"
PI[PluginInterface
IPluginInterface]
CL[CoreLoader
Dynamic Loader]
EP[Entry Points
ts3plugin_*]
end
subgraph "EchoionCore"
CO[Core Library
Shared Object]
end
TS <-->|Voice & Chat| SDK
SDK <-->|Callbacks| EP
EP --> PI
PI <-->|ICoreInterface| CO
CL <-->|Load/Unload| CO
```
## Architecture
### Three Layers
```mermaid
flowchart TB
subgraph "Layer 1: TeamSpeak SDK Interface"
EP[Entry Points]
CB[Event Callbacks]
EP -->|ts3plugin_init| Init
EP -->|ts3plugin_shutdown| Shutdown
CB -->|onTextMessage| Events
CB -->|onClientMove| Events
end
subgraph "Layer 2: Plugin Interface"
PI[PluginInterface]
PI -->|sendTextMessage| Out
PI -->|sendAudioData| Out
PI -->|getClients| Query
end
subgraph "Layer 3: Core Management"
CL[CoreLoader]
FW[FileWatcher]
CL -->|load| Core
CL -->|unload| Core
CL -->|hotReload| Core
FW -->|trigger| CL
end
```
## Component Details
### 1. Entry Points (TS3 Callbacks)
TeamSpeak calls these functions:
```mermaid
sequenceDiagram
participant TS as TeamSpeak
participant EP as Entry Points
participant PI as PluginInterface
participant CO as Core
TS->>EP: ts3plugin_init()
EP->>EP: deferredInitialization()
EP->>PI: initialize()
EP->>CL: load Core
CL->>CO: createCore()
CO-->>EP: Core ready
TS->>EP: onTextMessageEvent(msg)
EP->>PI: onTextMessageReceived(msg)
PI->>CO: onPluginEvent(event)
CO->>CO: processCommand()
CO-->>PI: send reply
PI-->>TS: display message
TS->>EP: ts3plugin_shutdown()
EP->>CL: unload Core
EP->>PI: shutdown()
```
**Key Events Handled:**
- `onTextMessageEvent` - User sends chat message
- `onClientMoveEvent` - User joins/leaves/moves
- `onConnectStatusChange` - Connection state changes
- `onPluginCommandEvent` - Plugin menu commands
### 2. PluginInterface (The Bridge)
Implements IPluginInterface for Core communication:
```mermaid
classDiagram
class IPluginInterface {
+sendTextMessage(target, message)
+sendAudioData(pcmData)
+getClients()
+moveClient(clientId, channel)
}
class PluginInterface {
-TS3Functions* ts3Functions
-audioQueue
+initialize(ts3Functions)
+onTextMessageReceived(msg)
+onClientConnected(id)
+registerAudioDevice()
}
IPluginInterface <|-- PluginInterface
```
**Audio Flow:**
```mermaid
flowchart LR
Core -->|Float PCM| Convert[Convert to Int16]
Convert -->|Queue| AQ[Audio Queue]
AQ -->|20ms chunks| TS[TeamSpeak SDK]
TS -->|Voice| Channel[Voice Channel]
```
### 3. CoreLoader (Dynamic Manager)
Manages the Core shared library lifecycle:
```mermaid
stateDiagram-v2
[*] --> Unloaded
Unloaded --> Loading: load()
Loading --> Loaded: Success
Loading --> Error: Failure
Loaded --> Saving: hotReload()
Saving --> Unloading: extract state
Unloading --> Loading: load new
Loading --> Restoring: restore state
Restoring --> Loaded: Success
Loaded --> Unloading: unload()
Unloading --> Unloaded
Error --> Unloaded
```
**Features:**
- Dynamic library loading
- Symbol resolution
- Hot reload state preservation
- Auto-reload on file change
### 4. FileWatcher (Auto-Reload)
Monitors Core library for updates:
```mermaid
flowchart TB
File[Core Library File] -->|Modify| FW[FileWatcher]
FW -->|Debounce| Trigger{Check}
Trigger -->|Different| Reload[Trigger Hot Reload]
Trigger -->|Same| Ignore[Ignore]
Reload -->|Success| Updated[New Version Active]
Reload -->|Fail| Rollback[Keep Old Version]
```
## Initialization Sequence
```mermaid
sequenceDiagram
participant TS as TeamSpeak
participant EP as Entry Points
participant PI as PluginInterface
participant CL as CoreLoader
participant CO as Core
TS->>EP: ts3plugin_init()
Note over EP: Start deferred thread
par Deferred Initialization
EP->>EP: Wait 3 seconds
EP->>EP: Wait for TS functions
and Continue
EP-->>TS: Return 0 (success)
end
EP->>PI: initialize(TS3Functions)
PI->>PI: Register audio device
EP->>CL: setConfigPath()
EP->>CL: load(libraryPath)
CL->>CO: createCore()
CO->>CO: initialize(config)
EP->>CO: setPluginInterface(PI)
CO->>CO: triggerAutoConnect()
opt Auto Reload Enabled
CL->>FW: startWatching()
end
```
## Event Handling
### Text Message Flow
```mermaid
flowchart TB
User[User Types] -->|!play song| TS[TeamSpeak]
TS -->|Callback| EP[Entry Point]
EP -->|Parse| Filter{Filter}
Filter -->|From self| Drop[Ignore]
Filter -->|From other| PI[PluginInterface]
PI -->|Create Event| Event[PluginEvent]
Event -->|TextMessageReceived| CO[Core]
CO -->|Dispatch| CM[CommandManager]
CM -->|Execute| Handler[Command Handler]
Handler -->|Result| CO
CO -->|Reply| PI
PI -->|Send| TS2[TeamSpeak]
TS2 -->|Display| User2[All Users]
```
### Client Movement Flow
```mermaid
flowchart LR
User[User Action] -->|Join/Leave/Move| TS[TeamSpeak]
TS -->|onClientMoveEvent| EP[Entry Points]
EP -->|Determine type| Type{Type}
Type -->|oldChannel=0| Connect[ClientConnected]
Type -->|newChannel=0| Disconnect[ClientDisconnected]
Type -->|Both non-zero| Move[ClientMoved]
Connect --> PI[PluginInterface]
Disconnect --> PI
Move --> PI
PI -->|Event| CO[Core]
CO -->|Update| Lists[Admin/Subscriber Lists]
```
## Audio System
### Custom Audio Device
```mermaid
flowchart TB
subgraph "Registration"
PI[PluginInterface] -->|registerCustomDevice| SDK[TS3 SDK]
SDK -->|Device ID| Device[echoion_audio_device]
end
subgraph "Usage"
CO[Core] -->|PCM Data| PI2[PluginInterface]
PI2 -->|Queue| AQ[Audio Queue]
AQ -->|Thread| Play[Playback Thread]
Play -->|processCustomCaptureData| SDK2[TS3 SDK]
SDK2 -->|Voice| VC[Voice Channel]
end
```
**Device Configuration:**
- Sample Rate: 192 kHz
- Channels: Stereo (2)
- Format: 16-bit signed integer
- Chunk Size: 20ms
### Audio Queue Management
```mermaid
flowchart LR
Core -->|Push| Queue[Audio Queue]
Queue -->|Pop| Thread[Audio Thread]
Thread -->|Play| TS[TeamSpeak]
Queue -.->|Full| Wait[Core Waits]
Queue -.->|Empty| Silence[Send Silence]
```
## Hot Reload Integration
### Manual Reload
```mermaid
sequenceDiagram
participant User as User Menu
participant EP as Entry Points
participant CL as CoreLoader
participant Old as Old Core
participant New as New Core
User->>EP: Click "Reload Core"
EP->>CL: hotReload()
CL->>Old: prepareForHotReload()
Old-->>CL: HotReloadState
CL->>Old: shutdown()
CL->>Old: unload()
CL->>New: load()
CL->>New: initialize()
CL->>New: restoreAfterHotReload(state)
New->>CL: Ready
CL-->>EP: Success
```
### Auto Reload
```mermaid
flowchart TB
Dev[Developer] -->|Build| New[New Core Binary]
New -->|File Change| FW[FileWatcher]
FW -->|Debounce 1s| Check{Loaded?}
Check -->|Yes| Reload[Trigger Hot Reload]
Check -->|No| Wait
Reload -->|Success| Active[New Version]
Reload -->|Fail| Keep[Keep Old]
```
## Menu Integration
### Right-Click Menu Items
```mermaid
flowchart TB
User[User Right-Click] --> Menu[TS3 Menu]
Menu -->|Play/Pause| A[Toggle Playback]
Menu -->|Skip| B[Next Track]
Menu -->|Stop| C[Stop Playback]
Menu -->|Reload Core| D[Hot Reload]
Menu -->|Connect/Disconnect| E[Toggle Connection]
A --> EP[Entry Point]
B --> EP
C --> EP
D --> EP
E --> EP
EP -->|Menu ID| Handler[Handle Menu Event]
Handler -->|1| PlayPause[Core::pause/resume]
Handler -->|2| SkipTrack[Core::skip]
Handler -->|3| StopTrack[Core::stop]
Handler -->|4| Reload[CoreLoader::hotReload]
Handler -->|5| Connect[Toggle Connection]
```
## Shutdown Sequence
```mermaid
sequenceDiagram
participant TS as TeamSpeak
participant EP as Entry Points
participant PI as PluginInterface
participant CL as CoreLoader
participant CO as Core
TS->>EP: ts3plugin_shutdown()
EP->>EP: Stop deferred init if running
EP->>PI: disconnectFromServer()
EP->>CL: disableAutoReload()
EP->>CL: unload()
CL->>CO: shutdown()
CL->>CO: destroyCore()
EP->>PI: shutdown()
PI->>PI: unregisterAudioDevice()
EP->>EP: Cleanup globals
```
## Thread Safety
```mermaid
flowchart TB
subgraph "Main Thread (TS3 Callbacks)"
A[Text Messages]
B[Client Events]
C[Menu Events]
end
subgraph "Audio Thread"
D[Audio Queue Processing]
end
subgraph "Core Threads"
E[Playback]
F[Decode]
G[Network]
end
A -->|Mutex| Shared[Shared State]
B -->|Mutex| Shared
C -->|Mutex| Shared
D -->|Mutex| Shared
```
## Platform Differences
### Windows
- DLL files (.dll)
- Requires temp copy for hot reload (file locking)
- Uses Windows-specific paths
### Linux
- Shared objects (.so)
- Can overwrite loaded libraries
- Uses XDG directories
### macOS
- Bundles (.dylib)
- Similar to Linux
- Uses macOS-specific paths