-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_opus_safety.php
More file actions
executable file
·327 lines (273 loc) · 8.7 KB
/
test_opus_safety.php
File metadata and controls
executable file
·327 lines (273 loc) · 8.7 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
<?php
/**
* Comprehensive safety test for Opus extension
* Tests for memory leaks, segfaults, and proper resource cleanup
*/
echo "=== Opus Extension Safety Tests ===\n\n";
// Test 1: Basic initialization and cleanup
echo "Test 1: Basic initialization and cleanup... ";
try {
$opus = new opusChannel(48000, 1);
$opus->destroy();
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 2: Multiple instances (check for static variable issues)
echo "Test 2: Multiple instances... ";
try {
$opus1 = new opusChannel(48000, 1);
$opus2 = new opusChannel(16000, 2);
$opus3 = new opusChannel(24000, 1);
// All should work independently
$opus1->setBitrate(64000);
$opus2->setBitrate(32000);
$opus3->setBitrate(48000);
$opus1->destroy();
$opus2->destroy();
$opus3->destroy();
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 3: Invalid parameters validation
echo "Test 3: Invalid parameters validation... ";
$errors = [];
try {
new opusChannel(44100, 1); // Invalid sample rate
$errors[] = "Invalid sample rate not caught";
} catch (Throwable $e) {
// Expected
}
try {
new opusChannel(48000, 3); // Invalid channels
$errors[] = "Invalid channels not caught";
} catch (Throwable $e) {
// Expected
}
try {
$opus = new opusChannel(48000, 1);
$opus->setBitrate(1000000); // Invalid bitrate
$errors[] = "Invalid bitrate not caught";
} catch (Throwable $e) {
// Expected
}
if (empty($errors)) {
echo "✓ PASS\n";
} else {
echo "✗ FAIL: " . implode(", ", $errors) . "\n";
}
// Test 4: Encode/Decode cycle
echo "Test 4: Encode/Decode cycle... ";
try {
$opus = new opusChannel(48000, 1);
// Generate test audio (20ms frame = 960 samples at 48kHz)
$samples = 960; // 20ms at 48kHz
$pcm = '';
for ($i = 0; $i < $samples; $i++) {
$value = (int)(sin(2 * M_PI * 440 * $i / 48000) * 16000);
$pcm .= pack('s', $value);
}
// Encode
$encoded = $opus->encode($pcm);
if (empty($encoded)) {
throw new Exception("Encode returned empty data");
}
// Decode
$decoded = $opus->decode($encoded);
if (empty($decoded)) {
throw new Exception("Decode returned empty data");
}
// Test multiple frames
for ($i = 0; $i < 10; $i++) {
$encoded = $opus->encode($pcm);
$decoded = $opus->decode($encoded);
}
$opus->destroy();
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 5: Resample functionality (per-instance state)
echo "Test 5: Resample with multiple instances... ";
try {
$opus1 = new opusChannel(48000, 1);
$opus2 = new opusChannel(48000, 1);
// Generate test audio
$pcm = '';
for ($i = 0; $i < 4800; $i++) {
$pcm .= pack('s', rand(-16000, 16000));
}
// Both should resample independently
$resampled1 = $opus1->resample($pcm, 48000, 8000);
$resampled2 = $opus2->resample($pcm, 48000, 16000);
if (strlen($resampled1) === 0 || strlen($resampled2) === 0) {
throw new Exception("Resample returned empty data");
}
// Different rates should produce different sizes
if (strlen($resampled1) === strlen($resampled2)) {
throw new Exception("Resample not working correctly");
}
$opus1->destroy();
$opus2->destroy();
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 6: enhanceVoiceClarity (per-instance state)
echo "Test 6: enhanceVoiceClarity with multiple instances... ";
try {
$opus1 = new opusChannel(48000, 1);
$opus2 = new opusChannel(48000, 1);
$pcm = '';
for ($i = 0; $i < 4800; $i++) {
$value = (int)(sin(2 * M_PI * 440 * $i / 48000) * 16000);
$pcm .= pack('s', $value);
}
// Both should process independently
$enhanced1 = $opus1->enhanceVoiceClarity($pcm, 0.5);
$enhanced2 = $opus2->enhanceVoiceClarity($pcm, 1.5);
if (strlen($enhanced1) !== strlen($pcm) || strlen($enhanced2) !== strlen($pcm)) {
throw new Exception("enhanceVoiceClarity changed output size");
}
$opus1->destroy();
$opus2->destroy();
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 7: spatialStereoEnhance (per-instance state)
echo "Test 7: spatialStereoEnhance with multiple instances... ";
try {
$opus1 = new opusChannel(48000, 1);
$opus2 = new opusChannel(48000, 2);
$pcm_mono = '';
for ($i = 0; $i < 4800; $i++) {
$pcm_mono .= pack('s', rand(-16000, 16000));
}
$pcm_stereo = '';
for ($i = 0; $i < 4800; $i++) {
$pcm_stereo .= pack('s', rand(-16000, 16000));
$pcm_stereo .= pack('s', rand(-16000, 16000));
}
// Both should process independently
$spatial1 = $opus1->spatialStereoEnhance($pcm_mono, 1.0, 0.5);
$spatial2 = $opus2->spatialStereoEnhance($pcm_stereo, 1.5, 0.8);
// Output should always be stereo
$expected_size = 4800 * 2 * 2; // samples * channels * bytes_per_sample
if (strlen($spatial1) !== $expected_size || strlen($spatial2) !== $expected_size) {
throw new Exception("spatialStereoEnhance output size mismatch");
}
$opus1->destroy();
$opus2->destroy();
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 8: Reset functionality
echo "Test 8: Reset functionality... ";
try {
$opus = new opusChannel(48000, 1);
$pcm = '';
for ($i = 0; $i < 4800; $i++) {
$pcm .= pack('s', rand(-16000, 16000));
}
// Process some data
$opus->enhanceVoiceClarity($pcm);
$opus->resample($pcm, 48000, 8000);
// Reset should clear all state
$opus->reset();
// Should still work after reset
$opus->enhanceVoiceClarity($pcm);
$opus->destroy();
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 9: Empty data handling
echo "Test 9: Empty data handling... ";
try {
$opus = new opusChannel(48000, 1);
// Empty data should not crash
$result1 = $opus->resample('', 48000, 8000);
$result2 = $opus->enhanceVoiceClarity('');
$result3 = $opus->spatialStereoEnhance('');
if (strlen($result1) !== 0 || strlen($result2) !== 0 || strlen($result3) !== 0) {
throw new Exception("Empty input should return empty output");
}
$opus->destroy();
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 10: Automatic cleanup (no explicit destroy)
echo "Test 10: Automatic cleanup (destructor)... ";
try {
for ($i = 0; $i < 10; $i++) {
$opus = new opusChannel(48000, 1);
// Not calling destroy() - should be cleaned up automatically
}
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 11: Stress test - many operations
echo "Test 11: Stress test (100 operations)... ";
try {
$opus = new opusChannel(48000, 1);
// 100ms frame = 4800 samples at 48kHz (valid Opus frame)
$pcm = '';
for ($i = 0; $i < 4800; $i++) {
$value = (int)(sin(2 * M_PI * 440 * $i / 48000) * 16000);
$pcm .= pack('s', $value);
}
for ($i = 0; $i < 100; $i++) {
$encoded = $opus->encode($pcm);
$decoded = $opus->decode($encoded);
$enhanced = $opus->enhanceVoiceClarity($decoded);
$resampled = $opus->resample($enhanced, 48000, 8000);
}
$opus->destroy();
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 12: Double destroy safety
echo "Test 12: Double destroy safety... ";
try {
$opus = new opusChannel(48000, 1);
$opus->destroy();
$opus->destroy(); // Should not crash
echo "✓ PASS\n";
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
// Test 13: Invalid data size handling
echo "Test 13: Invalid data size handling... ";
$errors = [];
try {
$opus = new opusChannel(48000, 1);
// Odd number of bytes (invalid for int16)
try {
$opus->encode("abc");
$errors[] = "Invalid encode data not caught";
} catch (Throwable $e) {
// Expected
}
try {
$opus->resample("abc", 48000, 8000);
$errors[] = "Invalid resample data not caught";
} catch (Throwable $e) {
// Expected
}
$opus->destroy();
if (empty($errors)) {
echo "✓ PASS\n";
} else {
echo "✗ FAIL: " . implode(", ", $errors) . "\n";
}
} catch (Throwable $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n";
}
echo "\n=== All tests completed ===\n";
echo "Check for memory leaks with: valgrind --leak-check=full php test_opus_safety.php\n";