-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_swoole_audio_server.php
More file actions
executable file
Β·342 lines (291 loc) Β· 11 KB
/
example_swoole_audio_server.php
File metadata and controls
executable file
Β·342 lines (291 loc) Β· 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* Swoole WebSocket Audio Streaming Server with Opus Codec
*
* Features:
* - Real-time audio streaming with Opus compression
* - Voice enhancement processing
* - Multi-client support with isolated processing
* - Low-latency coroutine-based processing
*
* Usage:
* php example_swoole_audio_server.php
*
* Test with WebSocket client on: ws://localhost:9501
*/
use Swoole\WebSocket\Server;
use Swoole\Http\Request;
use Swoole\WebSocket\Frame;
use Swoole\Coroutine;
if (!extension_loaded('swoole')) {
die("β Swoole extension required. Install: pecl install swoole\n");
}
if (!extension_loaded('opus')) {
die("β Opus extension required.\n");
}
$server = new Server("0.0.0.0", 9501);
// Server configuration
$server->set([
'worker_num' => 4,
'task_worker_num' => 2,
'enable_coroutine' => true,
'max_coroutine' => 3000,
]);
// Store client-specific Opus instances
$clients = new Swoole\Table(1024);
$clients->column('opus_id', Swoole\Table::TYPE_STRING, 64);
$clients->column('sample_rate', Swoole\Table::TYPE_INT);
$clients->column('channels', Swoole\Table::TYPE_INT);
$clients->column('connected_at', Swoole\Table::TYPE_INT);
$clients->create();
// Opus instance pool (per-worker)
class OpusPool {
private static $instances = [];
public static function get($id, $sample_rate = 48000, $channels = 1) {
if (!isset(self::$instances[$id])) {
self::$instances[$id] = new opusChannel($sample_rate, $channels);
echo "[OpusPool] Created instance: $id (rate: $sample_rate, ch: $channels)\n";
}
return self::$instances[$id];
}
public static function remove($id) {
if (isset(self::$instances[$id])) {
self::$instances[$id]->destroy();
unset(self::$instances[$id]);
echo "[OpusPool] Removed instance: $id\n";
}
}
public static function reset($id) {
if (isset(self::$instances[$id])) {
self::$instances[$id]->reset();
echo "[OpusPool] Reset instance: $id\n";
}
}
}
// WebSocket connection handler
$server->on('open', function (Server $server, Request $request) use ($clients) {
$fd = $request->fd;
echo "[Connect] Client #$fd connected from {$request->server['remote_addr']}\n";
// Store client info
$opus_id = "client_$fd";
$clients->set($fd, [
'opus_id' => $opus_id,
'sample_rate' => 48000,
'channels' => 1,
'connected_at' => time()
]);
// Send welcome message
$server->push($fd, json_encode([
'type' => 'welcome',
'message' => 'Connected to Opus Audio Server',
'client_id' => $fd,
'sample_rate' => 48000,
'channels' => 1
]));
});
// Message handler
$server->on('message', function (Server $server, Frame $frame) use ($clients) {
$fd = $frame->fd;
if (!$clients->exists($fd)) {
echo "[Error] Unknown client: $fd\n";
return;
}
$client = $clients->get($fd);
$opus_id = $client['opus_id'];
// Handle binary audio data
if ($frame->opcode === WEBSOCKET_OPCODE_BINARY) {
Coroutine::create(function() use ($server, $fd, $frame, $opus_id, $client) {
try {
$start = microtime(true);
// Get Opus instance for this client
$opus = OpusPool::get($opus_id, $client['sample_rate'], $client['channels']);
// Decode Opus data
$decoded_pcm = $opus->decode($frame->data);
// Apply voice enhancement
$enhanced_pcm = $opus->enhanceVoiceClarity($decoded_pcm, 1.2);
// Re-encode with better quality
$opus->setBitrate(64000);
// Split into frames if needed (20ms frames = 960 samples at 48kHz)
$frame_size = 960 * $client['channels'] * 2; // bytes
$total_size = strlen($enhanced_pcm);
if ($total_size >= $frame_size) {
// Take first frame
$frame_data = substr($enhanced_pcm, 0, $frame_size);
$encoded = $opus->encode($frame_data);
$elapsed = (microtime(true) - $start) * 1000;
// Send processed audio back
$server->push($fd, $encoded, WEBSOCKET_OPCODE_BINARY);
// Send stats
$server->push($fd, json_encode([
'type' => 'stats',
'processing_time_ms' => round($elapsed, 2),
'input_size' => strlen($frame->data),
'output_size' => strlen($encoded),
'compression_ratio' => round(strlen($frame->data) / strlen($encoded), 2)
]));
}
} catch (Throwable $e) {
echo "[Error] Client $fd processing failed: {$e->getMessage()}\n";
$server->push($fd, json_encode([
'type' => 'error',
'message' => $e->getMessage()
]));
}
});
}
// Handle JSON commands
else if ($frame->opcode === WEBSOCKET_OPCODE_TEXT) {
try {
$data = json_decode($frame->data, true);
if (!$data || !isset($data['command'])) {
return;
}
switch ($data['command']) {
case 'config':
// Update client configuration
if (isset($data['sample_rate'])) {
$client['sample_rate'] = $data['sample_rate'];
}
if (isset($data['channels'])) {
$client['channels'] = $data['channels'];
}
$clients->set($fd, $client);
// Recreate Opus instance
OpusPool::remove($opus_id);
OpusPool::get($opus_id, $client['sample_rate'], $client['channels']);
$server->push($fd, json_encode([
'type' => 'config_updated',
'sample_rate' => $client['sample_rate'],
'channels' => $client['channels']
]));
break;
case 'reset':
// Reset audio processor state
OpusPool::reset($opus_id);
$server->push($fd, json_encode([
'type' => 'reset_done'
]));
break;
case 'ping':
$server->push($fd, json_encode([
'type' => 'pong',
'timestamp' => time()
]));
break;
case 'stats':
$uptime = time() - $client['connected_at'];
$server->push($fd, json_encode([
'type' => 'stats',
'client_id' => $fd,
'uptime_seconds' => $uptime,
'sample_rate' => $client['sample_rate'],
'channels' => $client['channels']
]));
break;
}
} catch (Throwable $e) {
echo "[Error] Command processing failed: {$e->getMessage()}\n";
}
}
});
// Disconnect handler
$server->on('close', function (Server $server, $fd) use ($clients) {
if ($clients->exists($fd)) {
$client = $clients->get($fd);
$opus_id = $client['opus_id'];
// Cleanup Opus instance
OpusPool::remove($opus_id);
// Remove client
$clients->del($fd);
echo "[Disconnect] Client #$fd disconnected\n";
}
});
// HTTP handler (for status page)
$server->on('request', function (Request $request, Swoole\Http\Response $response) use ($clients) {
if ($request->server['request_uri'] === '/status') {
$stats = [
'server' => 'Swoole Opus Audio Server',
'swoole_version' => swoole_version(),
'opus_loaded' => extension_loaded('opus'),
'clients_connected' => $clients->count(),
'uptime' => time() - $GLOBALS['server_start_time'],
];
$response->header('Content-Type', 'application/json');
$response->end(json_encode($stats, JSON_PRETTY_PRINT));
} else {
$response->header('Content-Type', 'text/html');
$response->end(<<<HTML
<!DOCTYPE html>
<html>
<head>
<title>Opus Audio Server</title>
<style>
body { font-family: Arial; max-width: 800px; margin: 50px auto; padding: 20px; }
.status { background: #e8f5e9; padding: 20px; border-radius: 8px; }
.info { margin: 10px 0; }
code { background: #f5f5f5; padding: 2px 6px; border-radius: 3px; }
</style>
</head>
<body>
<h1>π΅ Swoole + Opus Audio Server</h1>
<div class="status">
<h2>Server Status</h2>
<div class="info"><strong>WebSocket:</strong> ws://localhost:9501</div>
<div class="info"><strong>Status API:</strong> <a href="/status">/status</a></div>
<div class="info"><strong>Clients:</strong> <span id="clients">0</span></div>
</div>
<h2>Example Usage (JavaScript)</h2>
<pre><code>const ws = new WebSocket('ws://localhost:9501');
ws.onopen = () => {
console.log('Connected');
// Send Opus-encoded audio (binary)
ws.send(opusData);
// Or send command (JSON)
ws.send(JSON.stringify({
command: 'config',
sample_rate: 48000,
channels: 1
}));
};
ws.onmessage = (event) => {
if (event.data instanceof Blob) {
// Received Opus audio data
console.log('Received audio');
} else {
// Received JSON message
const msg = JSON.parse(event.data);
console.log(msg);
}
};</code></pre>
<h2>Commands</h2>
<ul>
<li><code>{"command": "ping"}</code> - Ping server</li>
<li><code>{"command": "stats"}</code> - Get client stats</li>
<li><code>{"command": "reset"}</code> - Reset audio processor</li>
<li><code>{"command": "config", "sample_rate": 48000, "channels": 1}</code> - Change config</li>
</ul>
<script>
setInterval(() => {
fetch('/status')
.then(r => r.json())
.then(data => {
document.getElementById('clients').textContent = data.clients_connected;
});
}, 1000);
</script>
</body>
</html>
HTML
);
}
});
$GLOBALS['server_start_time'] = time();
echo "ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n";
echo "β Swoole + Opus Audio Streaming Server β\n";
echo "ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n";
echo "\n";
echo "WebSocket: ws://0.0.0.0:9501\n";
echo "HTTP: http://0.0.0.0:9501\n";
echo "\n";
echo "Server starting...\n\n";
$server->start();