Repository navigation
Expand file tree
/
Copy pathvoice.cpp
More file actions
435 lines (354 loc) · 9.2 KB
/
Copy pathvoice.cpp
File metadata and controls
435 lines (354 loc) · 9.2 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "voice.h"
#include <winsock.h>
#include "queue.h"
#include "sock.h"
queue_t snd_squeue;
queue_t snd_rqueue;
unsigned char snd_rbuffer[MIC_BUFFER_SIZE * 5];
unsigned char snd_sbuffer[MIC_BUFFER_SIZE * 5];
Voice::Voice()
{
}
int Voice::init(Audio &audio)
{
#ifdef VOICECHAT
#ifndef DEDICATED
int ret;
alGenSources(1, &mic_source);
alGenBuffers(NUM_PONG, (unsigned int *)&mic_buffer[0]);
alSourcei(mic_source, AL_SOURCE_RELATIVE, AL_TRUE);
alGenSources(1, &decode_source);
alGenBuffers(NUM_PONG, (unsigned int *)&decode_buffer[0]);
alSourcei(decode_source, AL_SOURCE_RELATIVE, AL_TRUE);
#endif
//OPUS_APPLICATION_AUDIO -- music
//OPUS_APPLICATION_VOIP -- voice
//OPUS_APPLICATION_RESTRICTED_LOWDELAY -- low delay voice
// Codec runs at 16kHz (output of the AEC→NS→AGC2→SRC pipeline)
encoder = opus_encoder_create(VOICE_ENCODE_SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP, &ret);
if (ret < 0)
{
printf("failed to create an encoder: %s\n", opus_strerror(ret));
return -1;
}
int complexity = 10;
ret = opus_encoder_ctl(encoder, OPUS_SET_COMPLEXITY(complexity));
if (ret < 0)
{
printf("failed to set complexity: %s\n", opus_strerror(ret));
return -1;
}
/*
ret = opus_encoder_ctl(encoder, OPUS_SET_BITRATE(BITRATE));
if (ret < 0)
{
printf("failed to set bitrate: %s\n", opus_strerror(ret));
return -1;
}
*/
decoder = opus_decoder_create(VOICE_ENCODE_SAMPLE_RATE, 1, &ret);
if (ret < 0)
{
printf("failed to create decoder: %s\n", opus_strerror(ret));
return -1;
}
#endif
return 0;
}
int Voice::encode(unsigned short *pcm, unsigned int size, unsigned char *data, int &num_bytes)
{
// Encode the frame (size = number of samples per channel, e.g. 1920 @16kHz).
num_bytes = opus_encode(encoder, (opus_int16 *)pcm, (int)size, data, MAX_PACKET_SIZE);
if (num_bytes < 0)
{
printf("encode failed: %s\n", opus_strerror(num_bytes));
return -1;
}
return 0;
}
int Voice::decode(unsigned char *data, int compressed_size, unsigned short *pcm, unsigned int max_size)
{
int frame_size;
// alBufferData(uiBuffer, AL_FORMAT_MONO16, mic_pcm[pong], pcm_size, VOICE_SAMPLE_RATE);
frame_size = opus_decode(decoder, data, compressed_size, (opus_int16 *)pcm, (max_size), 0);
if (frame_size < 0)
{
printf("decoder failed: %s\n", opus_strerror(frame_size));
return -1;
}
return frame_size * 2;
}
int Voice::voice_send(Audio &audio, int sock, const char *ip, int port)
{
static int pong = 0;
static bool buffers_full = false; // first time around buffers are empty, different logic required
int buffersProcessed = 0;
bool local_echo = false;
static voicemsg_t msg;
int pcm_size;
unsigned int uiBuffer;
if (audio.microphone == NULL)
{
return 0;
}
if (buffers_full)
{
if (local_echo)
{
alGetSourcei(mic_source, AL_BUFFERS_PROCESSED, &buffersProcessed);
if (buffersProcessed == 0)
{
ALenum state;
alGetSourcei(mic_source, AL_SOURCE_STATE, &state);
if (state == AL_STOPPED)
{
audio.play(mic_source);
}
return 0;
}
alSourceUnqueueBuffers(mic_source, 1, &uiBuffer);
int al_err = alGetError();
if (al_err != AL_NO_ERROR)
{
return 0;
}
}
pcm_size = MIC_BUFFER_SIZE;
audio.capture_sample(mic_pcm[pong], pcm_size);
if (local_echo)
{
alBufferData(uiBuffer, AL_FORMAT_MONO16, mic_pcm[pong], pcm_size, VOICE_SAMPLE_RATE);
alSourceQueueBuffers(mic_source, 1, &uiBuffer);
}
}
else
{
pcm_size = MIC_BUFFER_SIZE;
int ret = audio.capture_sample(mic_pcm[pong], pcm_size);
if (ret == 0)
{
// microphone doesnt have enough data, take a break thread
//Sleep(0);
}
if (local_echo)
{
alBufferData(mic_buffer[pong], AL_FORMAT_MONO16, mic_pcm[pong], pcm_size, VOICE_SAMPLE_RATE);
int al_err = alGetError();
if (al_err != AL_NO_ERROR)
{
printf("Error alBufferData\n");
}
alSourceQueueBuffers(mic_source, 1, &mic_buffer[pong]);
}
}
if (pcm_size == 0)
{
return 0;
}
// AEC → NS → AGC2 → SRC: 48kHz mic → 16kHz codec PCM
int proc_out = audio.process_pipeline((const short *)mic_pcm[pong], pcm_size >> 1,
(short *)proc_pcm[pong], MIC_BUFFER_SIZE / 3);
if (proc_out <= 0)
{
return 0;
}
int num_bytes = 0;
encode(proc_pcm[pong], proc_out, msg.data, num_bytes);
static int seq = 0;
msg.magic = 1337;
msg.sequence = seq++;
msg.size = num_bytes + VOICE_HEADER;
enqueue(&snd_squeue, (unsigned char *)&msg, msg.size);
while (snd_squeue.size > 0 && sock != -1)
{
int dsize = dequeue_peek(&snd_squeue, &snd_sbuffer[0], VOICE_HEADER);
voicemsg_t *head = (voicemsg_t *)&snd_sbuffer[0];
if (head->magic == 1337)
{
dsize = dequeue(&snd_squeue, &snd_sbuffer[0], head->size);
}
else
{
// missing header, drop 4 bytes and try again
dsize = dequeue(&snd_squeue, &snd_sbuffer[0], 4);
continue;
}
int sent = socket_sendto(sock, (char *)&snd_sbuffer[0], dsize, ip, port);
if (sent == -1)
{
int ret = WSAGetLastError();
switch (ret)
{
case WSAEHOSTUNREACH:
printf("Fatal Error: router sent ICMP packet (destination unreachable)\n");
break;
case WSAEWOULDBLOCK:
printf("Would block, using select()\r\n");
return 0;
case WSAENETUNREACH:
printf("network unreachable\r\n");
break;
default:
printf("Fatal Error: %d\n", ret);
break;
}
}
if (sent > 0 && sent < dsize)
{
enqueue_front(&snd_squeue, &snd_sbuffer[sent], dsize - sent);
}
}
pong++;
if (pong >= NUM_PONG)
{
pong = 0;
if (local_echo)
{
if (buffers_full == false)
{
buffers_full = true;
audio.play(mic_source);
}
}
}
return 0;
}
int Voice::voice_recv(Audio &audio, int sock, const char *ip, int port)
{
unsigned int pcm_size;
int ret = 1;
static int pong = 0;
static bool buffers_full = false;
int buffersProcessed = 0;
unsigned int uiBuffer;
bool remote_echo = true;
static voicemsg_t msg;
// recv any data on socket and add to rqueue
do
{
char client[MAX_PATH];
strncpy(client, ip, MAX_PATH - 1);
ret = socket_recvfrom(sock, (char *)&snd_rbuffer[0], MIC_BUFFER_SIZE, client, port, MAX_PATH);
if (ret > 0)
{
enqueue(&snd_rqueue, (unsigned char *)&snd_rbuffer[0], ret);
if (snd_rqueue.size > 1024)
break;
}
else if (ret == -1)
{
int err = WSAGetLastError();
if (err != WSAEWOULDBLOCK)
{
printf("recv returned -1 error %d\r\n", err);
return -1;
}
}
} while (ret > 0);
// check queue for a sound segment and add it to sound buffer chain
while (snd_rqueue.size > 0)
{
dequeue_peek(&snd_rqueue, (unsigned char *)&msg, VOICE_HEADER);
if (snd_rqueue.size >= msg.size && msg.magic == 1337)
{
dequeue(&snd_rqueue, (unsigned char *)&msg, msg.size);
if (remote_echo)
{
// decode opus data
pcm_size = decode(msg.data, msg.size - VOICE_HEADER, decode_pcm[pong], MIC_BUFFER_SIZE);
if (msg.sequence < 100)
{
printf("PCM decode msg.size %d pcm_size %d sequence %d\r\n", msg.size, pcm_size, msg.sequence);
}
else if (msg.sequence == 100)
{
printf("Got 100 packets, probably working\r\n");
}
static unsigned int last_sequence = 0;
if (msg.sequence < last_sequence)
{
printf("out of sequence %d < %d, dropping\r\n", msg.sequence, last_sequence);
return 0;
}
last_sequence = msg.sequence;
// Feed remote PCM (16kHz) to the AEC as far-end reference
if (pcm_size > 0 && pcm_size <= MIC_BUFFER_SIZE)
{
audio.push_farend((const short *)decode_pcm[pong], pcm_size / 2);
}
if (buffers_full)
{
alSourceUnqueueBuffers(decode_source, 1, &uiBuffer);
alBufferData(uiBuffer, AL_FORMAT_MONO16, decode_pcm[pong], pcm_size, VOICE_ENCODE_SAMPLE_RATE);
alSourceQueueBuffers(decode_source, 1, &uiBuffer);
}
else
{
alBufferData(decode_buffer[pong], AL_FORMAT_MONO16, decode_pcm[pong], pcm_size, VOICE_ENCODE_SAMPLE_RATE);
alSourceQueueBuffers(decode_source, 1, &decode_buffer[pong]);
}
pong++;
if (pong >= NUM_PONG)
{
pong = 0;
if (buffers_full == false)
{
// now that we have some sound queued up we can start playing
buffers_full = true;
ALenum state;
alGetSourcei(decode_source, AL_SOURCE_STATE, &state);
if (state == AL_STOPPED || AL_INITIAL)
{
// if we stopped due to a data hiccup, start playing again
if (remote_echo)
audio.play(decode_source);
}
}
}
}
}
else
{
// Either bad data or not enough data
if (msg.magic != 1337)
{
// unexpected data, throw away four bytes and try again
dequeue(&snd_rqueue, (unsigned char *)&msg, 4);
printf("Unexpected data, throwing away 4 bytes from rqueue\r\n");
}
else
{
break;
}
}
}
// Check if we have any queued sound buffers to play
if (remote_echo)
{
#ifndef DEDICATED
if (buffers_full)
{
alGetSourcei(decode_source, AL_BUFFERS_PROCESSED, &buffersProcessed);
if (buffersProcessed == 0)
{
ALenum state;
alGetSourcei(decode_source, AL_SOURCE_STATE, &state);
if (state == AL_STOPPED)
{
// if we stopped due to a data hiccup, start playing again
audio.play(decode_source);
}
return 0;
}
}
#endif
}
return 0;
}
void Voice::destroy()
{
#ifdef VOICECHAT
opus_encoder_destroy(encoder);
opus_decoder_destroy(decoder);
#endif
}