Overview
The server handles the heavy lifting for video streaming:- Torrent Streaming: Direct magnet/torrent link streaming with piece prioritization
- HLS Transcoding: Adaptive bitrate streaming with multiple quality levels
- Session Management: Multi-user streaming sessions with cleanup
- Chromecast Support: Cast token generation and stream routing
- Local File Playback: Serve local media files with range request support
Tech Stack
- Language: Go 1.25+
- Torrent Engine: anacrolix/torrent 1.59.1 (libtorrent-based)
- Database: SQLite (zombiezen.com/go/sqlite 0.13.1)
- WebRTC: pion/webrtc v4.0.0 for peer connections
- HTTP Server: Standard library net/http
- Concurrency: Goroutines with context-based cancellation
Architecture
Server Structure
Key Components
Torrent Streamer (src/stream)
- Manages torrent downloads with streaming optimization
- Piece prioritization for sequential playback
- DHT and tracker support
- Temporary file management in system temp directory
src/stream/hls)
- Transcodes video streams to HLS format
- Multiple quality profiles (1080p, 720p, 480p, 360p)
- Segment-based streaming with M3U8 playlists
- Orphaned session cleanup every 30 seconds
src/session)
- In-memory session tracking
- Concurrent access with mutex protection
- Session expiration and cleanup
HTTP API
The server runs on127.0.0.1:6969 by default (configurable via RAFFI_SERVER_ADDR).
Endpoints
POST /sessions
Create a new streaming session.
Request Body:
GET /sessions/{id}
Get session status and metadata.
Response:
DELETE /sessions/{id}
Stop and cleanup a streaming session.
Response: 204 No Content
GET /sessions/{id}/stream.m3u8
HLS master playlist.
Response (M3U8):
GET /torrents/{infohash}
Direct torrent file access (for range requests).
Headers:
Range: bytes=0-1023(optional)
206 Partial Content
POST /cast/token
Generate a cast token for Chromecast playback.
Request Body:
POST /cleanup
Manually trigger orphaned session cleanup.
Response: 200 OK
GET /community-addons
List available Stremio community addons.
Response:
Bundled with Desktop
The server is compiled as a static binary and bundled with each desktop release:Platform-Specific Binaries
- Windows
- Linux
- macOS
Binary: Bundled Location:
decoder-windows-amd64.exeBuild Configuration:raffi-desktop/electron/decoder-windows-amd64.exeFeatures:- Static linking with MinGW GCC
- No external DLL dependencies
- SQLite with load extension disabled for security
- Stripped symbols for smaller binary size
Automatic Startup
The desktop app automatically manages the server lifecycle:-
On App Launch (electron/services/decoder.cjs):
- Detects platform and architecture
- Spawns correct binary from
electron/directory - Sets
RAFFI_SERVER_ADDR=127.0.0.1:6969 - Redirects stdout/stderr to logs
-
During Runtime:
- Desktop communicates via HTTP (fetch to localhost:6969)
- Server handles all torrent/transcoding operations
- Background cleanup runs every 30 seconds
-
On App Close:
- Sends SIGTERM to server process
- Server cleanup routine:
- Closes all torrent clients
- Removes torrent temp directory (
/tmp/raffi-torrents) - Removes app temp directory (
/tmp/raffi)
- Graceful shutdown with timeout
Manual Server Launch
For debugging or standalone use:Building from Source
Prerequisites
- Go: 1.25 or later (
go version) - CGO: C compiler required
- Windows: MinGW, MSYS2, or TDM-GCC
- macOS: Xcode Command Line Tools (
xcode-select --install) - Linux: GCC (
sudo apt install build-essential)
Build Script
Use the desktop’s build script (cross-platform):- Detects your platform (Windows/Linux/macOS)
- Sets correct CGO flags and compiler
- Builds static binary to
electron/directory - Applies strip flags to reduce binary size
Manual Build
Cross-Compilation
Windows from Linux:Standalone Server for Mobile
You can run the server standalone to provide transcoding for mobile devices:Setup
- Build server binary (see above)
-
Run on local network:
-
Find your local IP:
Look for
192.168.x.xor10.0.x.xaddress -
Update mobile app:
Edit
raffi-mobile/app/player.tsx: -
Configure firewall:
Docker Deployment
Dockerfile:Configuration
Environment Variables
RAFFI_SERVER_ADDR
- Default:
127.0.0.1:6969 - Format:
host:portor:port - Example:
RAFFI_SERVER_ADDR=0.0.0.0:8080
Torrent Storage
- Location:
os.TempDir() + "/raffi-torrents"- Windows:
%TEMP%\raffi-torrents - macOS:
/var/folders/.../raffi-torrents - Linux:
/tmp/raffi-torrents
- Windows:
- Auto-cleanup: On server shutdown and session expiration
- Disk Usage: Depends on video file sizes (can be GB)
Session Cleanup
- Interval: Every 30 seconds (background goroutine)
- Orphaned Sessions: Sessions with no active connections
- Expired Cast Tokens: Tokens past expiration time
Dependencies
Key Go modules fromgo.mod:
Torrent Engine
Database
WebRTC (for peer connections)
Networking
Utilities
Performance
Benchmarks
- Concurrent Sessions: 10+ simultaneous streams on modest hardware
- Memory Usage: ~50-200 MB per active torrent (depends on piece cache)
- CPU Usage: Low when streaming, spikes during transcoding
- Network: Saturates gigabit on fast torrents with many peers
Optimization Tips
- SSD Storage: Use SSD for temp directory to reduce seek times
- Memory Limit: Monitor RAM usage with many concurrent torrents
- Port Forwarding: Forward a port for better torrent peer connectivity
- DHT Bootstrap: Server automatically bootstraps DHT for faster peer discovery
Troubleshooting
Server won’t start
Port already in use:Torrent not starting
- Check DHT: Server needs time to bootstrap DHT (30-60 seconds)
- Trackers: Public torrents need working trackers
- Firewall: Ensure outbound connections allowed
- Logs: Check server output for errors
Transcoding issues
- FFmpeg Required: Server assumes FFmpeg is available (bundled with desktop)
- Codec Support: Some exotic codecs may not transcode properly
- GPU Acceleration: Not enabled by default (CPU transcoding only)
High memory usage
Security Considerations
Localhost Only (Default)
By default, server binds to127.0.0.1 (localhost only):
- Safe: Only accessible from same machine
- No external exposure: Not accessible from internet or LAN
LAN Mode (Mobile Support)
When using0.0.0.0:6969 for mobile:
- LAN only: Accessible from local network
- Firewall required: Don’t expose port 6969 to internet
- No authentication: Anyone on LAN can use the server
- HTTPS: Not implemented (use VPN for remote access)
Recommendations
- Never expose to internet: Use VPN (Tailscale, WireGuard) for remote access
- Firewall: Block port 6969 on WAN interface
- Updates: Keep server binary updated with desktop app
- Monitoring: Check logs for suspicious activity
Next Steps
- Desktop App - Learn how the server integrates with desktop
- Mobile App - Configure mobile to use standalone server
- Explore anacrolix/torrent for advanced torrent features