-
Notifications
You must be signed in to change notification settings - Fork 149
/
tox_av.h
1032 lines (857 loc) · 30.4 KB
/
tox_av.h
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static void av_start(int32_t call_index, void *arg)
{
ToxAvCSettings peer_settings;
int fid = toxav_get_peer_id(arg, call_index, 0);
toxav_get_peer_csettings(arg, call_index, 0, &peer_settings);
_Bool video = (peer_settings.call_type == av_TypeVideo);
debug("video for this call: %u\n", video);
if(toxav_prepare_transmission(arg, call_index, 1) == 0) {
if(video) {
postmessage(FRIEND_CALL_VIDEO, fid, call_index, (void*)(640 | (size_t)480 << 16));
} else {
postmessage(FRIEND_CALL_STATUS, fid, call_index, (void*)CALL_OK);
}
} else {
debug("A/V FAIL!\n");
}
}
static void callback_av_invite(void *arg, int32_t call_index, void *UNUSED(userdata))
{
int fid = toxav_get_peer_id(arg, call_index, 0);
ToxAvCSettings peer_settings;
toxav_get_peer_csettings(arg, call_index, 0, &peer_settings);
_Bool video = (peer_settings.call_type == av_TypeVideo);
postmessage(FRIEND_CALL_STATUS, fid, call_index, (void*)(size_t)(video ? CALL_INVITED_VIDEO : CALL_INVITED));
toxaudio_postmessage(AUDIO_PLAY_RINGTONE, call_index, 0, NULL);
debug("A/V Invite (%i)\n", call_index);
}
static void callback_av_start(void *arg, int32_t call_index, void *UNUSED(userdata))
{
av_start(call_index, arg);
toxaudio_postmessage(AUDIO_STOP_RINGTONE, call_index, 0, NULL);
debug("A/V Start (%i)\n", call_index);
}
#define endcall() \
int fid = toxav_get_peer_id(arg, call_index, 0); \
postmessage(FRIEND_CALL_STATUS, fid, call_index, (void*)(size_t)CALL_NONE); \
toxaudio_postmessage(AUDIO_STOP_RINGTONE, call_index, 0, NULL);
#define stopcall() \
toxav_kill_transmission(arg, call_index); \
endcall();
static void callback_av_cancel(void *arg, int32_t call_index, void *UNUSED(userdata))
{
stopcall();
debug("A/V Cancel (%i)\n", call_index);
}
static void callback_av_reject(void *arg, int32_t call_index, void *UNUSED(userdata))
{
endcall();
debug("A/V Reject (%i)\n", call_index);
}
static void callback_av_end(void *arg, int32_t call_index, void *UNUSED(userdata))
{
stopcall();
debug("A/V End (%i)\n", call_index);
}
static void callback_av_ringing(void *arg, int32_t call_index, void *UNUSED(userdata))
{
debug("A/V Ringing (%i)\n", call_index);
}
static void callback_av_requesttimeout(void *arg, int32_t call_index, void *UNUSED(userdata))
{
endcall();
debug("A/V ReqTimeout (%i)\n", call_index);
}
static void callback_av_peertimeout(void *arg, int32_t call_index, void *UNUSED(userdata))
{
stopcall();
debug("A/V PeerTimeout (%i)\n", call_index);
}
static void callback_av_selfmediachange(void *arg, int32_t call_index, void *UNUSED(userdata))
{
debug("A/V SelfMediachange (%i)\n", call_index);
}
static void callback_av_peermediachange(void *arg, int32_t call_index, void *UNUSED(userdata))
{
ToxAvCSettings settings;
toxav_get_peer_csettings(arg, call_index, 0, &settings);
int fid = toxav_get_peer_id(arg, call_index, 0);
postmessage(FRIEND_CALL_MEDIACHANGE, fid, call_index, (settings.call_type == av_TypeVideo) ? (void*)1 : NULL);
debug("A/V PeerMediachange (%i)\n", call_index);
}
uint8_t lbuffer[800 * 600 * 4]; //needs to be always large enough for encoded frames
static vpx_image_t input;
static _Bool openvideodevice(void *handle)
{
if(!video_init(handle)) {
debug("video_init() failed\n");
return 0;
}
vpx_img_alloc(&input, VPX_IMG_FMT_I420, video_width, video_height, 1);
return 1;
}
static void closevideodevice(void *handle)
{
video_close(handle);
vpx_img_free(&input);
}
static void video_thread(void *args)
{
ToxAv *av = args;
void *video_device;
_Bool video = 0;
uint8_t video_count = 0;
_Bool video_on = 0;
_Bool call[MAX_CALLS] = {0}, preview = 0, newinput = 1;
// Add always-present null video input device.
postmessage(NEW_VIDEO_DEVICE, STR_VIDEO_IN_NONE, 1, NULL);
video_device = video_detect();
if(video_device) {
video = openvideodevice(video_device);
}
video_thread_init = 1;
while(1) {
if(video_thread_msg) {
TOX_MSG *m = &video_msg;
if(!m->msg) {
break;
}
switch(m->msg) {
case VIDEO_SET: {
if(video_on) {
video_endread();
}
if(video) {
closevideodevice(video_device);
}
video_device = m->data;
if(video_device == NULL) {
video = 0;
video_on = 0;
if(m->param1) {
goto VIDEO_PREVIEW_END;
} else {
break;
}
}
video = openvideodevice(video_device);
if(video) {
if(video_count) {
video_on = video_startread();
}
} else {
video_on = 0;
}
newinput = 1;
break;
}
case VIDEO_PREVIEW_START: {
preview = 1;
video_count++;
if(video && !video_on) {
video_on = video_startread();
}
break;
}
case VIDEO_CALL_START: {
call[m->param1] = 1;
video_count++;
if(video && !video_on) {
video_on = video_startread();
}
break;
}
VIDEO_PREVIEW_END:
case VIDEO_PREVIEW_END: {
debug("preview end %u\n", video_count);
preview = 0;
video_count--;
if(!video_count && video_on) {
video_endread();
video_on = 0;
}
break;
}
case VIDEO_CALL_END: {
if(!call[m->param1]) {
break;
}
call[m->param1] = 0;
video_count--;
if(!video_count && video_on) {
video_endread();
video_on = 0;
}
break;
}
}
video_thread_msg = 0;
}
if(video_on) {
int r = video_getframe(input.planes[0], input.planes[1], input.planes[2], input.d_w, input.d_h);
if(r == 1) {
if(preview) {
uint8_t *img_data = malloc(input.d_w * input.d_h * 4);
yuv420tobgr(input.d_w, input.d_h, input.planes[0], input.planes[1], input.planes[2], input.d_w, input.d_w / 2, input.d_w / 2, img_data);
postmessage(PREVIEW_FRAME + newinput, input.d_w, input.d_h, img_data);
newinput = 0;
}
int i;
for(i = 0; i < MAX_CALLS; i++) {
if(call[i]) {
int rr, len;
if((len = toxav_prepare_video_frame(av, i, lbuffer, sizeof(lbuffer), &input)) < 0) {
debug("toxav_prepare_video_frame error %i\n", len);
continue;
}
debug("%u\n", len);
if((rr = toxav_send_video(av, i, (void*)lbuffer, len)) < 0) {
debug("toxav_send_video error %i %s\n", rr, strerror(errno));
}
}
}
} else if(r == -1) {
video_on = 0;
video = 0;
video_endread();
closevideodevice(video_device);
}
}
yieldcpu(5);
}
if(video_on) {
video_endread();
}
if(video) {
closevideodevice(video_device);
}
video_thread_msg = 0;
video_thread_init = 0;
}
#ifndef NATIVE_ANDROID_AUDIO
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
#ifdef AUDIO_FILTERING
#include <AL/alext.h>
#endif
/* include for compatibility with older versions of OpenAL */
#ifndef ALC_ALL_DEVICES_SPECIFIER
#include <AL/alext.h>
#endif
#endif
#ifdef AUDIO_FILTERING
#include <filter_audio.h>
#endif
static ALCdevice *device_out, *device_in;
static ALCcontext *context;
static ALuint source[MAX_CALLS];
static ALCdevice* alcopencapture(void *handle)
{
if(!handle) {
return NULL;
}
if(handle == (void*)1) {
return handle;
}
if (av_DefaultSettings.audio_channels == 1) {
return alcCaptureOpenDevice(handle, av_DefaultSettings.audio_sample_rate, AL_FORMAT_MONO16, (av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4) / 1000);
} else {
return alcCaptureOpenDevice(handle, av_DefaultSettings.audio_sample_rate, AL_FORMAT_STEREO16, ((av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4) / 1000) * av_DefaultSettings.audio_channels);
}
}
static void alccapturestart(void *handle)
{
if(handle == (void*)1) {
audio_init(handle);
return;
}
alcCaptureStart(handle);
}
static void alccapturestop(void *handle)
{
if(handle == (void*)1) {
audio_close(handle);
return;
}
alcCaptureStop(handle);
}
static void alccaptureclose(void *handle)
{
if(handle == (void*)1) {
return;
}
alcCaptureCloseDevice(handle);
}
static void sourceplaybuffer(int i, const int16_t *data, int samples, uint8_t channels, unsigned int sample_rate)
{
if(!channels || channels > 2) {
return;
}
ALuint bufid;
ALint processed = 0, queued = 16;
alGetSourcei(source[i], AL_BUFFERS_PROCESSED, &processed);
alGetSourcei(source[i], AL_BUFFERS_QUEUED, &queued);
alSourcei(source[i], AL_LOOPING, AL_FALSE);
if(processed) {
ALuint bufids[processed];
alSourceUnqueueBuffers(source[i], processed, bufids);
alDeleteBuffers(processed - 1, bufids + 1);
bufid = bufids[0];
} else if(queued < 16) {
alGenBuffers(1, &bufid);
} else {
debug("dropped audio frame\n");
return;
}
alBufferData(bufid, (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, data, samples * 2 * channels, sample_rate);
alSourceQueueBuffers(source[i], 1, &bufid);
ALint state;
alGetSourcei(source[i], AL_SOURCE_STATE, &state);
if(state != AL_PLAYING) {
alSourcePlay(source[i]);
debug("Starting source %u\n", i);
}
}
static void audio_thread(void *args)
{
ToxAv *av = args;
const char *device_list, *output_device = NULL;
void *audio_device = NULL;
_Bool call[MAX_CALLS] = {0}, preview = 0;
_Bool groups_audio[MAX_NUM_GROUPS] = {0};
int perframe = (av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate) / 1000;
uint8_t buf[perframe * 2 * av_DefaultSettings.audio_channels], dest[perframe * 2 * av_DefaultSettings.audio_channels];
memset(buf, 0, sizeof(buf));
uint8_t audio_count = 0;
_Bool record_on = 0;
#ifdef AUDIO_FILTERING
debug("Audio Filtering");
#ifdef ALC_LOOPBACK_CAPTURE_SAMPLES
debug(" and Echo cancellation");
#endif
debug(" enabled in this build\n");
#endif
debug("frame size: %u\n", perframe);
device_list = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
if(device_list) {
audio_device = (void*)device_list;
debug("Input Device List:\n");
while(*device_list) {
debug("%s\n", device_list);
postmessage(NEW_AUDIO_IN_DEVICE, UI_STRING_ID_INVALID, 0, (void*)device_list);
device_list += strlen(device_list) + 1;
}
}
postmessage(NEW_AUDIO_IN_DEVICE, STR_AUDIO_IN_NONE, 0, NULL);
audio_detect();
if (alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT"))
device_list = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
else
device_list = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
if(device_list) {
output_device = device_list;
debug("Output Device List:\n");
while(*device_list) {
debug("%s\n", device_list);
postmessage(NEW_AUDIO_OUT_DEVICE, 0, 0, (void*)device_list);
device_list += strlen(device_list) + 1;
}
}
device_out = alcOpenDevice(output_device);
if(!device_out) {
debug("alcOpenDevice() failed\n");
return;
}
int attrlist[] = { ALC_FREQUENCY, av_DefaultSettings.audio_sample_rate,
ALC_INVALID };
context = alcCreateContext(device_out, attrlist);
if(!alcMakeContextCurrent(context)) {
debug("alcMakeContextCurrent() failed\n");
alcCloseDevice(device_out);
return;
}
alGenSources(countof(source), source);
static ALuint ringSrc[MAX_CALLS];
alGenSources(MAX_CALLS, ringSrc);
/* Create buffer to store samples */
ALuint RingBuffer;
alGenBuffers(1, &RingBuffer);
{
float frequency1 = 441.f;
float frequency2 = 882.f;
int seconds = 4;
unsigned sample_rate = 22050;
size_t buf_size = seconds * sample_rate * 2; //16 bit (2 bytes per sample)
int16_t *samples = malloc(buf_size * sizeof(int16_t));
if (!samples)
return;
/*Generate an electronic ringer sound that quickly alternates between two frequencies*/
int index = 0;
for(index = 0; index < buf_size; ++index) {
if ((index / (sample_rate)) % 4 < 2 ) {//4 second ring cycle, first 2 secondsring, the rest(2 seconds) is silence
if((index / 1000) % 2 == 1) {
samples[index] = 5000 * sin((2.0 * 3.1415926 * frequency1) / sample_rate * index); //5000=amplitude(volume level). It can be from zero to 32700
} else {
samples[index] = 5000 * sin((2.0 * 3.1415926 * frequency2) / sample_rate * index);
}
} else {
samples[index] = 0;
}
}
alBufferData(RingBuffer, AL_FORMAT_MONO16, samples, buf_size, sample_rate);
free(samples);
}
{
unsigned int i;
for (i = 0; i < MAX_CALLS; ++i) {
alSourcei(ringSrc[i], AL_LOOPING, AL_TRUE);
alSourcei(ringSrc[i], AL_BUFFER, RingBuffer);
}
}
#ifdef AUDIO_FILTERING
Filter_Audio *f_a = NULL;
#endif
audio_thread_init = 1;
int16_t *preview_buffer = NULL;
unsigned int preview_buffer_index = 0;
#define PREVIEW_BUFFER_SIZE (av_DefaultSettings.audio_sample_rate / 2)
while(1) {
if(audio_thread_msg) {
TOX_MSG *m = &audio_msg;
if(!m->msg) {
break;
}
switch(m->msg) {
case AUDIO_SET_INPUT: {
audio_device = m->data;
if(record_on) {
alccapturestop(device_in);
alccaptureclose(device_in);
}
if(audio_count) {
device_in = alcopencapture(audio_device);
if(!device_in) {
record_on = 0;
} else {
alccapturestart(device_in);
record_on = 1;
}
}
debug("set audio in\n");
break;
}
case AUDIO_SET_OUTPUT: {
output_device = m->data;
ALCdevice *device = alcOpenDevice(output_device);
if(!device) {
debug("alcOpenDevice() failed\n");
break;
}
ALCcontext *con = alcCreateContext(device, NULL);
if(!alcMakeContextCurrent(con)) {
debug("alcMakeContextCurrent() failed\n");
alcCloseDevice(device);
break;
}
alcDestroyContext(context);
alcCloseDevice(device_out);
context = con;
device_out = device;
alGenSources(countof(source), source);
alGenSources(MAX_CALLS, ringSrc);
Tox *tox = toxav_get_tox(av);
uint32_t num_chats = tox_count_chatlist(tox);
if (num_chats != 0) {
int32_t chats[num_chats];
uint32_t max = tox_get_chatlist(tox, chats, num_chats);
unsigned int i;
for (i = 0; i < max; ++i) {
if (tox_group_get_type(tox, chats[i]) == TOX_GROUPCHAT_TYPE_AV) {
GROUPCHAT *g = &group[chats[i]];
alGenSources(g->peers, g->source);
}
}
}
debug("set audio out\n");
break;
}
case AUDIO_PREVIEW_START: {
preview = 1;
audio_count++;
preview_buffer = calloc(PREVIEW_BUFFER_SIZE, 2);
preview_buffer_index = 0;
if(!record_on) {
device_in = alcopencapture(audio_device);
if(device_in) {
alccapturestart(device_in);
record_on = 1;
debug("start\n");
}
}
break;
}
case AUDIO_CALL_START: {
call[m->param1] = 1;
audio_count++;
if(!record_on) {
device_in = alcopencapture(audio_device);
if(device_in) {
alccapturestart(device_in);
record_on = 1;
debug("start\n");
}
}
break;
}
case GROUP_AUDIO_CALL_START: {
audio_count++;
groups_audio[m->param1] = 1;
if(!record_on) {
device_in = alcopencapture(audio_device);
if(device_in) {
alccapturestart(device_in);
record_on = 1;
debug("start\n");
}
}
break;
}
case AUDIO_PREVIEW_END: {
preview = 0;
audio_count--;
free(preview_buffer);
preview_buffer = NULL;
if(!audio_count && record_on) {
alccapturestop(device_in);
alccaptureclose(device_in);
record_on = 0;
debug("stop\n");
}
break;
}
case AUDIO_CALL_END: {
if(!call[m->param1]) {
break;
}
call[m->param1] = 0;
audio_count--;
if(!audio_count && record_on) {
alccapturestop(device_in);
alccaptureclose(device_in);
record_on = 0;
debug("stop\n");
}
break;
}
case GROUP_AUDIO_CALL_END: {
if(!groups_audio[m->param1]) {
break;
}
audio_count--;
groups_audio[m->param1] = 0;
if(!audio_count && record_on) {
alccapturestop(device_in);
alccaptureclose(device_in);
record_on = 0;
debug("stop\n");
}
break;
}
case AUDIO_PLAY_RINGTONE: {
if(!audible_notifications_enabled) {
break;
}
alSourcePlay(ringSrc[m->param1]);
break;
}
case AUDIO_STOP_RINGTONE: {
ALint state;
alGetSourcei(ringSrc[m->param1], AL_SOURCE_STATE, &state);
if(state == AL_PLAYING) {
alSourceStop(ringSrc[m->param1]);
}
break;
}
}
audio_thread_msg = 0;
}
#ifdef AUDIO_FILTERING
if (!f_a && audio_filtering_enabled) {
f_a = new_filter_audio(av_DefaultSettings.audio_sample_rate);
if (!f_a) {
audio_filtering_enabled = 0;
debug("filter audio failed\n");
} else {
debug("filter audio on\n");
}
} else if (f_a && !audio_filtering_enabled) {
kill_filter_audio(f_a);
f_a = NULL;
debug("filter audio off\n");
}
#else
if (audio_filtering_enabled)
audio_filtering_enabled = 0;
#endif
_Bool sleep = 1;
if(record_on) {
_Bool frame = 0;
if(device_in == (void*)1) {
frame = audio_frame((void*)buf);
if (frame) {
sleep = 0;
}
} else {
ALint samples;
alcGetIntegerv(device_in, ALC_CAPTURE_SAMPLES, sizeof(samples), &samples);
if(samples >= perframe) {
alcCaptureSamples(device_in, buf, perframe);
frame = 1;
if (samples >= perframe * 2) {
sleep = 0;
}
}
}
#ifdef AUDIO_FILTERING
#ifdef ALC_LOOPBACK_CAPTURE_SAMPLES
if (f_a && audio_filtering_enabled) {
ALint samples;
alcGetIntegerv(device_out, ALC_LOOPBACK_CAPTURE_SAMPLES, sizeof(samples), &samples);
if(samples >= perframe) {
int16_t buffer[perframe];
alcCaptureSamplesLoopback(device_out, buffer, perframe);
pass_audio_output(f_a, buffer, perframe);
set_echo_delay_ms(f_a, av_DefaultSettings.audio_frame_duration);
if (samples >= perframe * 2) {
sleep = 0;
}
}
}
#endif
#endif
if (frame) {
_Bool voice = 1;
#ifdef AUDIO_FILTERING
if (f_a) {
int ret = filter_audio(f_a, (int16_t*)buf, perframe);
if (ret == -1) {
debug("filter audio error\n");
}
if (ret == 0) {
voice = 0;
}
}
#endif
if(preview) {
if (preview_buffer_index + perframe > PREVIEW_BUFFER_SIZE)
preview_buffer_index = 0;
sourceplaybuffer(0, preview_buffer + preview_buffer_index, perframe, av_DefaultSettings.audio_channels, av_DefaultSettings.audio_sample_rate);
if (voice) {
memcpy(preview_buffer + preview_buffer_index, buf, perframe * sizeof(int16_t));
} else {
memset(preview_buffer + preview_buffer_index, 0, perframe * sizeof(int16_t));
}
preview_buffer_index += perframe;
}
if (voice) {
int i;
for(i = 0; i < MAX_CALLS; i++) {
if(call[i]) {
int r;
if((r = toxav_prepare_audio_frame(av, i, dest, sizeof(dest), (void*)buf, perframe)) < 0) {
debug("toxav_prepare_audio_frame error %i\n", r);
continue;
}
if((r = toxav_send_audio(av, i, dest, r)) < 0) {
debug("toxav_send_audio error %i %s\n", r, strerror(errno));
}
}
}
Tox *tox = toxav_get_tox(av);
uint32_t num_chats = tox_count_chatlist(tox);
if (num_chats != 0) {
int32_t chats[num_chats];
uint32_t max = tox_get_chatlist(tox, chats, num_chats);
for (i = 0; i < max; ++i) {
if (groups_audio[chats[i]]) {
toxav_group_send_audio(tox, chats[i], (int16_t *)buf, perframe, av_DefaultSettings.audio_channels, av_DefaultSettings.audio_sample_rate);
}
}
}
}
}
}
if (sleep) {
yieldcpu(5);
}
}
#ifdef AUDIO_FILTERING
kill_filter_audio(f_a);
#endif
//missing some cleanup ?
alDeleteSources(MAX_CALLS, ringSrc);
alDeleteSources(countof(source), source);
alDeleteBuffers(1, &RingBuffer);
if(device_in) {
if(record_on) {
alcCaptureStop(device_in);
}
alcCaptureCloseDevice(device_in);
}
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
alcCloseDevice(device_out);
audio_thread_msg = 0;
audio_thread_init = 0;
}
static void callback_av_audio(void *av, int32_t call_index, const int16_t *data, uint16_t samples, void *UNUSED(userdata))
{
ToxAvCSettings dest;
if(toxav_get_peer_csettings(av, call_index, 0, &dest) == 0) {
sourceplaybuffer(call_index + 1, data, samples, dest.audio_channels, dest.audio_sample_rate);
}
}
void toxaudio_postmessage(uint8_t msg, uint32_t param1, uint32_t param2, void *data)
{
while(audio_thread_msg) {
yieldcpu(1);
}
audio_msg.msg = msg;
audio_msg.param1 = param1;
audio_msg.param2 = param2;
audio_msg.data = data;
audio_thread_msg = 1;
}
void callback_av_group_audio(Tox *tox, int groupnumber, int peernumber, const int16_t *pcm, unsigned int samples,
uint8_t channels, unsigned int sample_rate, void *userdata)
{
GROUPCHAT *g = &group[groupnumber];
uint64_t time = get_time();
if (time - g->last_recv_audio[peernumber] > (uint64_t)1 * 1000 * 1000 * 1000) {
postmessage(GROUP_UPDATE, groupnumber, peernumber, NULL);
}
g->last_recv_audio[peernumber] = time;
if(!channels || channels > 2 || g->muted) {
return;
}
ALuint bufid;
ALint processed = 0, queued = 16;
alGetSourcei(g->source[peernumber], AL_BUFFERS_PROCESSED, &processed);
alGetSourcei(g->source[peernumber], AL_BUFFERS_QUEUED, &queued);
alSourcei(g->source[peernumber], AL_LOOPING, AL_FALSE);
if(processed) {
ALuint bufids[processed];
alSourceUnqueueBuffers(g->source[peernumber], processed, bufids);
alDeleteBuffers(processed - 1, bufids + 1);
bufid = bufids[0];
} else if(queued < 16) {
alGenBuffers(1, &bufid);
} else {
debug("dropped audio frame %i %i\n", groupnumber, peernumber);
return;
}
alBufferData(bufid, (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, samples * 2 * channels, sample_rate);
alSourceQueueBuffers(g->source[peernumber], 1, &bufid);
ALint state;
alGetSourcei(g->source[peernumber], AL_SOURCE_STATE, &state);
if(state != AL_PLAYING) {
alSourcePlay(g->source[peernumber]);
debug("Starting source %i %i\n", groupnumber, peernumber);
}
}
void group_av_peer_add(GROUPCHAT *g, int peernumber)
{
alGenSources(1, &g->source[peernumber]);
}
void group_av_peer_remove(GROUPCHAT *g, int peernumber)
{
alDeleteSources(1, &g->source[peernumber]);
}
#else
static void audio_thread(void *args)
{
}
void toxaudio_postmessage(uint8_t msg, uint32_t param1, uint32_t param2, void *data)
{
switch(msg) {
case AUDIO_SET_INPUT: {;
break;
}
case AUDIO_SET_OUTPUT: {
break;
}
case AUDIO_PREVIEW_START: {
break;
}
case AUDIO_CALL_START: {
audio_begin(param1);
break;
}
case AUDIO_PREVIEW_END: {
break;
}
case AUDIO_CALL_END: {
audio_end(param1);
break;
}
}
}
static void callback_av_audio(ToxAv *av, int32_t call_index, int16_t *data, int length, void *userdata)
{
ToxAvCSettings dest;
if(toxav_get_peer_csettings(av, call_index, 0, &dest) == 0) {
audio_play(call_index, data, length, dest.audio_channels);
}
}
#endif
static void toxav_thread(void *args)
{
ToxAv *av = args;
toxav_thread_init = 1;
debug("Toxav thread init\n");
while (1) {
if(toxav_thread_msg) {
TOX_MSG *msg = &toxav_msg;
if(msg->msg == TOXAV_KILL) {
break;
}
/*
switch(m->msg) {
}*/
toxav_thread_msg = 0;
}
toxav_do(av);
yieldcpu(toxav_do_interval(av));
}
debug("Toxav thread die\n");
toxav_thread_msg = 0;