forked from ozsun88/obs-asio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasio-input.cpp
More file actions
1660 lines (1468 loc) · 52.9 KB
/
asio-input.cpp
File metadata and controls
1660 lines (1468 loc) · 52.9 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
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
/*
Copyright (C) 2017 by pkv <pkv.stream@gmail.com>, andersama <anderson.john.alexander@gmail.com>
Based on Pulse Input plugin by Leonhard Oelke.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <util/bmem.h>
#include <util/platform.h>
#include <util/threading.h>
#include <util/circlebuf.h>
#include <obs-module.h>
#include <vector>
#include <list>
#include <unordered_map>
#include <stdio.h>
#include <string>
#include <sstream>
#include <windows.h>
#include <util/windows/WinHandle.hpp>
#include <bassasio.h>
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("win-asio", "en-US")
#define blog(level, msg, ...) blog(level, "asio-input: " msg, ##__VA_ARGS__)
#define NSEC_PER_SEC 1000000000LL
#define TEXT_BUFFER_SIZE obs_module_text("BufferSize")
#define TEXT_BUFFER_64_SAMPLES obs_module_text("64_samples")
#define TEXT_BUFFER_128_SAMPLES obs_module_text("128_samples")
#define TEXT_BUFFER_256_SAMPLES obs_module_text("256_samples")
#define TEXT_BUFFER_512_SAMPLES obs_module_text("512_samples")
#define TEXT_BUFFER_1024_SAMPLES obs_module_text("1024_samples")
#define TEXT_BITDEPTH obs_module_text("BitDepth")
/* ======================================================================= */
/* conversion between BASS_ASIO and obs */
enum audio_format asio_to_obs_audio_format(DWORD format)
{
switch (format) {
case BASS_ASIO_FORMAT_16BIT: return AUDIO_FORMAT_16BIT;
case BASS_ASIO_FORMAT_32BIT: return AUDIO_FORMAT_32BIT;
case BASS_ASIO_FORMAT_FLOAT: return AUDIO_FORMAT_FLOAT;
default: break;
}
return AUDIO_FORMAT_UNKNOWN;
}
int bytedepth_format(audio_format format)
{
return (int)get_audio_bytes_per_channel(format);
}
int bytedepth_format(DWORD format) {
return bytedepth_format(asio_to_obs_audio_format(format));
}
DWORD obs_to_asio_audio_format(audio_format format)
{
switch (format) {
case AUDIO_FORMAT_16BIT:
return BASS_ASIO_FORMAT_16BIT;
// obs doesn't have 24 bit
case AUDIO_FORMAT_32BIT:
return BASS_ASIO_FORMAT_32BIT;
case AUDIO_FORMAT_FLOAT:
default:
return BASS_ASIO_FORMAT_FLOAT;
}
// default to 32 float samples for best quality
}
enum speaker_layout asio_channels_to_obs_speakers(unsigned int channels)
{
switch (channels) {
case 1: return SPEAKERS_MONO;
case 2: return SPEAKERS_STEREO;
case 3: return SPEAKERS_2POINT1;
case 4: return SPEAKERS_4POINT0;
case 5: return SPEAKERS_4POINT1;
case 6: return SPEAKERS_5POINT1;
/* no layout for 7 channels */
case 8: return SPEAKERS_7POINT1;
}
return SPEAKERS_UNKNOWN;
}
/* ======================================================================= */
/* asio structs and classes */
struct asio_source_audio {
uint8_t *data[MAX_AUDIO_CHANNELS];
uint32_t frames;
volatile long speakers;
enum audio_format format;
uint32_t samples_per_sec;
uint64_t timestamp;
};
audio_format get_planar_format(audio_format format) {
switch (format) {
case AUDIO_FORMAT_U8BIT:
return AUDIO_FORMAT_U8BIT_PLANAR;
case AUDIO_FORMAT_16BIT:
return AUDIO_FORMAT_16BIT_PLANAR;
case AUDIO_FORMAT_32BIT:
return AUDIO_FORMAT_32BIT_PLANAR;
case AUDIO_FORMAT_FLOAT:
return AUDIO_FORMAT_FLOAT_PLANAR;
}
return format;
}
audio_format get_interleaved_format(audio_format format) {
switch (format) {
case AUDIO_FORMAT_U8BIT_PLANAR:
return AUDIO_FORMAT_U8BIT;
case AUDIO_FORMAT_16BIT_PLANAR:
return AUDIO_FORMAT_16BIT;
case AUDIO_FORMAT_32BIT_PLANAR:
return AUDIO_FORMAT_32BIT;
case AUDIO_FORMAT_FLOAT_PLANAR:
return AUDIO_FORMAT_FLOAT;
}
return format;
}
int bytedepth_format(audio_format format);
#define CAPTURE_INTERVAL INFINITE
struct device_source_audio {
uint8_t **data;
uint32_t frames;
long input_chs;
enum audio_format format;
uint32_t samples_per_sec;
uint64_t timestamp;
};
class device_data;
class asio_data;
struct listener_pair {
asio_data *asio_listener;
device_data *device;
};
class asio_data {
private:
uint8_t* silent_buffer = NULL;
size_t silent_buffer_size = 0;
public:
CRITICAL_SECTION settings_mutex;
obs_source_t *source;
/*asio device and info */
const char *device;
uint8_t device_index;
uint64_t first_ts; //first timestamp
uint32_t retry_limit;
bool device_timeout;
/* channels info */
DWORD input_channels; //total number of input channels
DWORD output_channels; // number of output channels of device (not used)
DWORD recorded_channels; // number of channels passed from device (including muted) to OBS; is at most 8
long route[MAX_AUDIO_CHANNELS]; // stores the channel re-ordering info
std::vector<short> unmuted_chs;
std::vector<short> muted_chs;
std::vector<long> required_signals;
//signals
WinHandle stop_listening_signal;
//WinHandle reconnectThread;
WinHandle captureThread;
bool isASIOActive = false;
bool reconnecting = false;
bool previouslyFailed = false;
bool useDeviceTiming = false;
std::string get_id() {
const void * address = static_cast<const void*>(source);
std::stringstream ss;
ss << "0x" << std::uppercase << (int)address;
std::string name = ss.str();
return name;
}
asio_data() : source(NULL), first_ts(0), device_index(-1) {
device_timeout = false;
retry_limit = 0;
InitializeCriticalSection(&settings_mutex);
memset(&route[0], -1, sizeof(long) * 8);
stop_listening_signal = CreateEvent(nullptr, true, false, nullptr);
}
~asio_data() {
DeleteCriticalSection(&settings_mutex);
if (silent_buffer)
free(silent_buffer);
}
bool disconnect() {
isASIOActive = false;
SetEvent(stop_listening_signal);
if (captureThread.Valid())
WaitForSingleObject(captureThread, INFINITE);
ResetEvent(stop_listening_signal);
return true;
}
bool render_audio(device_source_audio *asio_buffer, long route[], bool force_silent) {
struct obs_audio_info aoi;
obs_get_audio_info(&aoi);
int index = BASS_ASIO_GetDevice();
//blog(LOG_INFO, "dv index in render_audio is %i", index);
obs_source_audio out;
out.format = asio_buffer->format;
if (!is_audio_planar(out.format)) {
blog(LOG_ERROR, "that was a goof %i should be %i", out.format, get_planar_format(out.format));
return false;
}
if (out.format == AUDIO_FORMAT_UNKNOWN) {
blog(LOG_INFO, "unknown format");
return false;
}
out.frames = asio_buffer->frames;
out.samples_per_sec = asio_buffer->samples_per_sec;
out.timestamp = asio_buffer->timestamp;
if (!first_ts) {
first_ts = out.timestamp;
blog(LOG_INFO, "first timestamp");
return false;
}
//cache a silent buffer
size_t buffer_size = (out.frames * sizeof(bytedepth_format(out.format)));
if (silent_buffer_size < buffer_size) {
if (silent_buffer)
free(silent_buffer);
silent_buffer = (uint8_t*)calloc(buffer_size, sizeof(uint8_t));
silent_buffer_size = buffer_size;
blog(LOG_INFO, "caching silent buffer");
}
if (unmuted_chs.size() == 0) {
blog(LOG_INFO, "all chs muted");
return 0;
}
for (short i = 0; i < aoi.speakers; i++) {
if (route[i] >= 0 && route[i] < asio_buffer->input_chs && !force_silent) {
out.data[i] = asio_buffer->data[route[i]];
} else if (route[i] == -1) {
out.data[i] = silent_buffer;
} else {
out.data[i] = silent_buffer;
}
}
out.speakers = aoi.speakers;
obs_source_output_audio(source, &out);
//blog(LOG_DEBUG, "output frames %lu", buffer_size);
return true;
}
static std::vector<short> _get_muted_chs(long route_array[]) {
std::vector<short> silent_chs;
silent_chs.reserve(MAX_AUDIO_CHANNELS);
for (short i = 0; i < MAX_AUDIO_CHANNELS; i++) {
if (route_array[i] == -1)
silent_chs.push_back(i);
}
return silent_chs;
}
static std::vector<short> _get_unmuted_chs(long route_array[]) {
std::vector<short> unmuted_chs;
unmuted_chs.reserve(MAX_AUDIO_CHANNELS);
for (short i = 0; i < MAX_AUDIO_CHANNELS; i++) {
if (route_array[i] >= 0) {
unmuted_chs.push_back(i);
}
}
return unmuted_chs;
}
};
class device_data {
private:
size_t write_index;
size_t buffer_count;
size_t buffer_size;
uint32_t frames;
long input_chs;
audio_format format;
//not in use...
WinHandle *receive_signals;
//create a square tick signal w/ two events
WinHandle all_received_signal;
WinHandle all_received_signal_2;
//to close out the device
WinHandle stop_listening_signal;
//tell listeners to to reinit
//WinHandle wait_for_reset_signal;
bool all_prepped = false;
bool buffer_prepped = false;
bool circle_buffer_prepped = false;
bool reallocate_buffer = false;
bool events_prepped = false;
circlebuf audio_buffer;
public:
uint32_t samples_per_sec;
uint32_t retry_count;
const WinHandle * get_handles() {
return receive_signals;
}
WinHandle on_buffer() {
return all_received_signal;
}
long get_input_channels() {
return input_chs;
}
long device_index;
BASS_ASIO_DEVICEINFO device_info;
device_source_audio* get_writeable_source_audio() {
return (device_source_audio*)circlebuf_data(&audio_buffer,
write_index * sizeof(device_source_audio));
}
device_source_audio* get_source_audio(size_t index) {
return (device_source_audio*)circlebuf_data(&audio_buffer,
index * sizeof(device_source_audio));
}
device_data() {
all_prepped = false;
buffer_prepped = false;
circle_buffer_prepped = false;
reallocate_buffer = false;
events_prepped = false;
format = AUDIO_FORMAT_UNKNOWN;
write_index = 0;
buffer_count = 32;
retry_count = 0;
all_received_signal = CreateEvent(nullptr, true, false, nullptr);
all_received_signal_2 = CreateEvent(nullptr, true, true, nullptr);
stop_listening_signal = CreateEvent(nullptr, true, false, nullptr);
}
device_data(size_t buffers, audio_format audioformat) {
all_prepped = false;
buffer_prepped = false;
circle_buffer_prepped = false;
reallocate_buffer = false;
events_prepped = false;
format = audioformat;
write_index = 0;
buffer_count = buffers ? buffers : 32;
retry_count = 0;
all_received_signal = CreateEvent(nullptr, true, false, nullptr);
all_received_signal_2 = CreateEvent(nullptr, true, true, nullptr);
stop_listening_signal = CreateEvent(nullptr, true, false, nullptr);
}
~device_data() {
//free resources?
if (all_prepped) {
delete receive_signals;
for (int i = 0; i < buffer_count; i++) {
device_source_audio* _source_audio = get_source_audio(i);
int input_chs = _source_audio->input_chs;
for (int j = 0; j < input_chs; j++) {
if (_source_audio->data[j]) {
bfree(_source_audio->data[j]);
}
}
bfree(_source_audio->data);
}
circlebuf_free(&audio_buffer);
}
}
//check that all the required device settings have been set
void check_all() {
if (buffer_prepped && circle_buffer_prepped && events_prepped) {
all_prepped = true;
} else {
all_prepped = false;
}
}
void prep_circle_buffer(BASS_ASIO_INFO &info) {
prep_circle_buffer(info.bufpref);
}
void prep_circle_buffer(DWORD bufpref) {
if (!circle_buffer_prepped) {
//create a buffer w/ a minimum of 4 slots and a target of a fraction of 2048 samples
buffer_count = max(4, ceil(2048 / bufpref));
circlebuf_init(&audio_buffer);
circlebuf_reserve(&audio_buffer, buffer_count * sizeof(device_source_audio));
for (int i = 0; i < buffer_count; i++) {
circlebuf_push_back(&audio_buffer, &device_source_audio(), sizeof(device_source_audio));
//initialize # of buffers
}
circle_buffer_prepped = true;
}
}
void prep_events(BASS_ASIO_INFO &info) {
prep_events(info.inputs);
}
void prep_events(long input_chs) {
if (!events_prepped) {
receive_signals = (WinHandle*)calloc(input_chs, sizeof(WinHandle));
for (int i = 0; i < input_chs; i++) {
receive_signals[i] = CreateEvent(nullptr, true, false, nullptr);
}
events_prepped = true;
}
}
void re_prep_buffers() {
all_prepped = false;
buffer_prepped = false;
BASS_ASIO_INFO info;
bool ret = BASS_ASIO_GetInfo(&info);
prep_buffers(info, format, samples_per_sec);
}
void re_prep_buffers(BASS_ASIO_INFO &info) {
all_prepped = false;
prep_buffers(info, format, samples_per_sec);
}
void update_sample_rate(uint32_t in_samples_per_sec) {
all_prepped = false;
this->samples_per_sec = in_samples_per_sec;
check_all();
}
void prep_buffers(BASS_ASIO_INFO &info, audio_format in_format, uint32_t in_samples_per_sec) {
prep_buffers(info.bufpref, info.inputs, in_format, in_samples_per_sec);
}
void prep_buffers(uint32_t frames, long in_chs, audio_format format, uint32_t samples_per_sec) {
if (frames * bytedepth_format(format) > this->buffer_size) {
if (buffer_prepped) {
reallocate_buffer = true;
}
} else {
reallocate_buffer = false;
}
prep_events(in_chs);
if (circle_buffer_prepped && (!buffer_prepped || reallocate_buffer)) {
this->frames = frames;
this->input_chs = in_chs;
this->format = format;
this->samples_per_sec = samples_per_sec;
this->buffer_size = frames * bytedepth_format(format);
for (int i = 0; i < buffer_count; i++) {
device_source_audio* _source_audio = get_source_audio(i);
_source_audio->data = (uint8_t **)bzalloc(input_chs * sizeof(uint8_t*))/*calloc(input_chs, sizeof(uint8_t*))*/;
for (int j = 0; j < input_chs; j++) {
if (!buffer_prepped) {
_source_audio->data[j] = (uint8_t*)bzalloc(buffer_size)/*calloc(buffer_size, 1)*/;
} else if (reallocate_buffer) {
uint8_t* tmp = (uint8_t*)realloc(_source_audio->data[j], buffer_size);
if (tmp == NULL) {
buffer_prepped = false;
all_prepped = false;
return;
} else if (tmp == _source_audio->data[j]) {
free(tmp);
tmp = NULL;
} else {
_source_audio->data[j] = tmp;
tmp = NULL;
}
}
}
_source_audio->input_chs = input_chs;
_source_audio->frames = frames;
_source_audio->format = format;
_source_audio->samples_per_sec = samples_per_sec;
}
buffer_prepped = true;
}
check_all();
}
void write_buffer_interleaved(void* buffer, DWORD BufSize) {
if (!all_prepped) {
blog(LOG_INFO, "%s device %i is not prepared", __FUNCTION__, device_index);
return;
}
ResetEvent(all_received_signal);
SetEvent(all_received_signal_2);
//get as much information from the device that called this function
BASS_ASIO_INFO info;
bool ret = BASS_ASIO_GetInfo(&info);
uint8_t * input_buffer = (uint8_t*)buffer;
size_t ch_buffer_size = BufSize / info.inputs;
if (ch_buffer_size > buffer_size) {
blog(LOG_WARNING, "%s device needs to reallocate memory");
}
int byte_depth = bytedepth_format(format);
size_t interleaved_frame_size = info.inputs * byte_depth;
//calculate on the spot
size_t frames_count = BufSize / interleaved_frame_size;
//use cached value
//size_t frames_count = frames;
device_source_audio* _source_audio = get_writeable_source_audio();
if (!_source_audio) {
blog(LOG_INFO, "%s _source_audio = NULL", __FUNCTION__);
return;
}
audio_format planar_format = get_planar_format(format);
//deinterleave directly into buffer (planar)
for (size_t i = 0; i < frames_count; i++) {
for (size_t j = 0; j < info.inputs; j++) {
memcpy(_source_audio->data[j] + (i * byte_depth), input_buffer + (j * byte_depth) + (i * interleaved_frame_size), byte_depth);
}
}
_source_audio->format = planar_format;
_source_audio->frames = frames_count;
_source_audio->input_chs = info.inputs;
_source_audio->samples_per_sec = samples_per_sec;
_source_audio->timestamp = _source_audio->timestamp = os_gettime_ns() - ((_source_audio->frames * NSEC_PER_SEC) / _source_audio->samples_per_sec);
write_index++;
write_index = write_index % buffer_count;
SetEvent(all_received_signal);
ResetEvent(all_received_signal_2);
}
static DWORD WINAPI capture_thread(void *data) {
listener_pair *pair = static_cast<listener_pair*>(data);
asio_data *source = pair->asio_listener;//static_cast<asio_data*>(data);
device_data *device = pair->device;//static_cast<device_data*>(data);
struct obs_audio_info aoi;
obs_get_audio_info(&aoi);
std::string thread_name = "asio capture: ";//source->device;
thread_name += source->get_id();
thread_name += ":";
thread_name += device->device_info.name;//thread_name += " capture thread";
os_set_thread_name(thread_name.c_str());
HANDLE signals_1[3] = { device->all_received_signal, device->stop_listening_signal, source->stop_listening_signal };
HANDLE signals_2[3] = { device->all_received_signal_2, device->stop_listening_signal, source->stop_listening_signal };
long route[MAX_AUDIO_CHANNELS];
for (short i = 0; i < aoi.speakers; i++) {
route[i] = source->route[i];
}
source->isASIOActive = true;
ResetEvent(source->stop_listening_signal);
blog(LOG_INFO, "listener for device %lu created: source: %x", device->device_index, source->get_id());
size_t read_index = device->write_index;//0;
int waitResult;
uint64_t buffer_time = ((device->frames * NSEC_PER_SEC) / device->samples_per_sec);
DWORD wait_time = DWORD((buffer_time / 1000000L) * 8);
blog(LOG_INFO, "device %lu wait_time=%d", device->device_index, wait_time);
while (source && device) {
DWORD ms = source->device_timeout ? wait_time : INFINITE;
waitResult = WaitForMultipleObjects(3, signals_1, false, ms);
waitResult = WaitForMultipleObjects(3, signals_2, false, ms);
//not entirely sure that all of these conditions are correct (at the very least this is)
if (waitResult == WAIT_OBJECT_0 || waitResult == WAIT_TIMEOUT) {
if (waitResult == WAIT_TIMEOUT) {
// In case of timeout increment the retry_count
device->retry_count++;
if (source->retry_limit != -1 && device->retry_count > source->retry_limit) {
// We reached our retry_limit :(
blog(LOG_INFO, "%s closing due to timeout, device %lu WAIT_TIMEOUT retry_count=%u retry_limit=%u", thread_name.c_str(), device->device_index, device->retry_count, source->retry_limit);
delete pair;
return 0;
}
} else if (device->retry_count >= 1) {
// waitResult is now WAIT_OBJECT_0 after some WAIT_TIMEOUT occurred, reset the retry_count to zero
device->retry_count = 0;
}
while (read_index != device->write_index) {
device_source_audio* in = device->get_source_audio(read_index);//device->get_writeable_source_audio();
source->render_audio(in, route, waitResult == WAIT_TIMEOUT);
read_index++;
read_index = read_index % device->buffer_count;
}
if (source->device_index != device->device_index) {
blog(LOG_INFO, "source device index %lu is not device index %lu", source->device_index, device->device_index);
blog(LOG_INFO, "%s closing", thread_name.c_str());
delete pair;
return 0;
} else if (!source->isASIOActive) {
blog(LOG_INFO, "%x indicated it wanted to disconnect", source->get_id());
blog(LOG_INFO, "%s closing", thread_name.c_str());
delete pair;
return 0;
}
} else if (waitResult == WAIT_OBJECT_0 + 1) {
blog(LOG_INFO, "device %l indicated it wanted to disconnect", device->device_index);
blog(LOG_INFO, "%s closing", thread_name.c_str());
delete pair;
return 0;
} else if (waitResult == WAIT_OBJECT_0 + 2) {
blog(LOG_INFO, "%x indicated it wanted to disconnect", source->get_id());
blog(LOG_INFO, "%s closing", thread_name.c_str());
delete pair;
return 0;
} else if (waitResult == WAIT_ABANDONED_0) {
blog(LOG_INFO, "a mutex for %s was abandoned while listening to", thread_name.c_str(), device->device_index);
blog(LOG_INFO, "%s closing", thread_name.c_str());
delete pair;
return 0;
} else if (waitResult == WAIT_ABANDONED_0 + 1) {
blog(LOG_INFO, "a mutex for %s was abandoned while listening to", thread_name.c_str(), device->device_index);
blog(LOG_INFO, "%s closing", thread_name.c_str());
delete pair;
return 0;
} else if (waitResult == WAIT_ABANDONED_0 + 2) {
blog(LOG_INFO, "a mutex for %s was abandoned while listening to", thread_name.c_str(), device->device_index);
blog(LOG_INFO, "%s closing", thread_name.c_str());
delete pair;
return 0;
} else if (waitResult == WAIT_FAILED) {
blog(LOG_INFO, "listener thread wait %lu failed with 0x%x", device->device_index, GetLastError());
blog(LOG_INFO, "%s closing", thread_name.c_str());
delete pair;
return 0;
} else {
blog(LOG_INFO, "unexpected wait result = %i", waitResult);
blog(LOG_INFO, "%s closing", thread_name.c_str());
delete pair;
return 0;
}
}
delete pair;
return 0;
}
//adds a listener thread between an asio_data object and this device
void add_listener(asio_data *listener) {
if (!all_prepped) {
return;
}
listener_pair* parameters = new listener_pair();
parameters->asio_listener = listener;
parameters->device = this;
blog(LOG_INFO, "disconnecting any previous connections (source_id: %x)", listener->get_id());
listener->disconnect();
//CloseHandle(listener->captureThread);
blog(LOG_INFO, "adding listener for %lu (source: %lu)", device_index, listener->device_index);
listener->captureThread = CreateThread(nullptr, 0, this->capture_thread, parameters, 0, nullptr);
}
};
std::vector<device_data*> device_list;
/*****************************************************************************/
// get number of output channels
DWORD get_obs_output_channels() {
// get channel number from output speaker layout set by obs
struct obs_audio_info aoi;
obs_get_audio_info(&aoi);
DWORD recorded_channels = get_audio_channels(aoi.speakers);
return recorded_channels;
}
// get device number
uint8_t getDeviceCount() {
uint8_t a, count = 0;
BASS_ASIO_DEVICEINFO info;
for (a = 0; BASS_ASIO_GetDeviceInfo(a, &info); a++) {
blog(LOG_INFO, "device index is : %i and name is : %s", a, info.name);
count++;
}
return count;
}
// get the device index from a device name : the current index can be retrieved from DWORD BASS_ASIO_GetDevice();
DWORD get_device_index(const char *device_info_name) {
int res;
BASS_ASIO_SetUnicode(false);
BASS_ASIO_DEVICEINFO info;
bool ret;
uint32_t i;
for (i = 0; BASS_ASIO_GetDeviceInfo(i, &info); i++) {
res = strcmp(info.name, device_info_name);
if (res == 0) {
return i;
}
}
return -1;
}
DWORD get_device_index(BASS_ASIO_DEVICEINFO device_info) {
return get_device_index(device_info.name);
}
bool is_device_index_valid(DWORD index) {
return index < getDeviceCount();
}
DWORD get_device_buffer_index(BASS_ASIO_DEVICEINFO device_info) {
uint32_t i;
for (i = 0; i < device_list.size(); i++) {
if (strcmp(device_list[i]->device_info.name, device_info.name) == 0) {
return i;
}
}
return -1;
}
// call the control panel
static bool DeviceControlPanel(obs_properties_t *props,
obs_property_t *property, void *data) {
if (!BASS_ASIO_ControlPanel()) {
switch (BASS_ASIO_ErrorGetCode()) {
case BASS_ERROR_INIT:
blog(LOG_ERROR, "Init not called\n");
break;
case BASS_ERROR_UNKNOWN:
blog(LOG_ERROR, "Unknown error\n");
}
return false;
} else {
int device_index = BASS_ASIO_GetDevice();
BASS_ASIO_INFO info;
BASS_ASIO_GetInfo(&info);
blog(LOG_INFO, "Console loaded for device %s with index %i\n",
info.name, device_index);
}
return true;
}
/*****************************************************************************/
void asio_update(void *vptr, obs_data_t *settings);
void asio_destroy(void *vptr);
//creates the device list
void fill_out_devices(obs_property_t *list) {
int res;
BASS_ASIO_SetUnicode(false);
BASS_ASIO_DEVICEINFO devinfo;
bool ret;
//int numOfDevices = getDeviceCount();
uint32_t i;
for (i = 0; BASS_ASIO_GetDeviceInfo(i, &devinfo); i++) {
obs_property_list_add_string(list, devinfo.name, devinfo.name);
/* the device list includes all drivers in registry.
* The following hack checks whether the device is hooked or not.
* For that initialization is attempted.
*/
ret = BASS_ASIO_Init(i, BASS_ASIO_THREAD);
if (!ret) {
res = BASS_ASIO_ErrorGetCode();
switch (res) {
case BASS_ERROR_DEVICE:
blog(LOG_ERROR, "The device number specified is invalid.\n");
break;
case BASS_ERROR_ALREADY:
blog(LOG_ERROR, "The device has already been initialized\n");
break;
case BASS_ERROR_DRIVER:
blog(LOG_ERROR, "The driver could not be initialized\n");
break;
}
/* Disable entry if device is not connected (BASS_ERROR_DRIVER)
* or if the driver's been uninstalled (BASS_ERROR_DEVICE)
* but not if the driver's already initialized.
* If the device is not multi-client, this may be an issue.
* But this is the only way to enable several asio sources
* in obs with same device but possibly different channel
* selections.
*/
if (res != BASS_ERROR_ALREADY)
obs_property_list_item_disable(list, i, true);
} else {
ret = BASS_ASIO_Free();
if (!ret)
blog(LOG_ERROR, "Should never have reached here, wtf uh oh.\n");
}
}
}
/* Creates list of input channels ; a muted channel has route value -1 and
* is recorded. The user can unmute the channel later.
*/
static bool fill_out_channels_modified(obs_properties_t *props, obs_property_t *list, obs_data_t *settings) {
BASS_ASIO_INFO info;
bool ret = BASS_ASIO_GetInfo(&info);
BASS_ASIO_DEVICEINFO devinfo;
int index = BASS_ASIO_GetDevice();
ret = BASS_ASIO_GetDeviceInfo(index, &devinfo);
if (!ret) {
blog(LOG_ERROR, "Unable to retrieve info on the current driver \n"
"error number is : %i \n; check BASS_ASIO_ErrorGetCode\n",
BASS_ASIO_ErrorGetCode());
}
// DEBUG: check that the current device in bass thread is the correct one
// once code is fine the check can be removed
const char* device = obs_data_get_string(settings, "device_id");
if (ret && strcmp(device, devinfo.name) != 0) {
blog(LOG_ERROR, "Device loaded is not the one in settings\n");
}
//get the device info
DWORD input_channels = ret ? info.inputs : 0;
obs_property_list_clear(list);
obs_property_list_add_int(list, "mute", -1);
BASS_ASIO_CHANNELINFO ch_info;
for (DWORD i = 0; i < input_channels; i++) {
BASS_ASIO_ChannelGetInfo(1, i, &ch_info);
std::string test = info.name;
test = test + " " + std::to_string(i);
test = test + " " + ch_info.name;
obs_property_list_add_int(list, test.c_str(), i);
}
return true;
}
//creates list of input sample rates supported by the device and OBS (obs supports only 44100 and 48000)
static bool fill_out_sample_rates(obs_properties_t *props, obs_property_t *list, obs_data_t *settings) {
BASS_ASIO_DEVICEINFO devinfo;
int index = BASS_ASIO_GetDevice();
bool ret = BASS_ASIO_GetDeviceInfo(index, &devinfo);
bool ret2;
if (!ret) {
blog(LOG_ERROR, "Unable to retrieve info on the current driver \n"
"error number is : %i \n; check BASS_ASIO_ErrorGetCode\n",
BASS_ASIO_ErrorGetCode());
}
// DEBUG: check that the current device in bass thread is the correct one
// once code is fine the check can be removed
const char* device = obs_data_get_string(settings, "device_id");
if (ret && strcmp(device, devinfo.name) != 0) {
blog(LOG_ERROR, "Device loaded is not the one in settings\n");
}
obs_property_list_clear(list);
//get the device info
if (ret)
ret2 = BASS_ASIO_CheckRate(44100);
if (ret2) {
std::string rate = "44100 Hz";
char* cstr = new char[rate.length() + 1];
strcpy(cstr, rate.c_str());
obs_property_list_add_int(list, cstr, 44100);
delete cstr;
} else {
blog(LOG_INFO, "Device loaded does not support 44100 Hz sample rate\n");
}
if (ret)
ret2 = BASS_ASIO_CheckRate(48000);
if (ret2) {
std::string rate = "48000 Hz";
char* cstr = new char[rate.length() + 1];
strcpy(cstr, rate.c_str());
obs_property_list_add_int(list, cstr, 48000);
delete cstr;
} else {
blog(LOG_INFO, "Device loaded does not support 48000 Hz sample rate\n");
}
return true;
}
//create list of supported audio formats
static bool fill_out_bit_depths(obs_properties_t *props, obs_property_t *list, obs_data_t *settings) {
BASS_ASIO_DEVICEINFO devinfo;
int index = BASS_ASIO_GetDevice();
bool ret = BASS_ASIO_GetDeviceInfo(index, &devinfo);
bool ret2;
if (!ret) {
blog(LOG_ERROR, "Unable to retrieve info on the current driver \n"
"error number is : %i \n; check BASS_ASIO_ErrorGetCode\n",
BASS_ASIO_ErrorGetCode());
}
// DEBUG: check that the current device in bass thread is the correct one
// once code is fine the check can be removed
const char* device = obs_data_get_string(settings, "device_id");
if (ret && strcmp(device, devinfo.name) != 0) {
blog(LOG_ERROR, "Device loaded is not the one in settings\n");
}
//get the device channel info
BASS_ASIO_CHANNELINFO channelInfo;
if (ret)
ret2 = BASS_ASIO_ChannelGetInfo(true, 0, &channelInfo);
if (!ret2) {
blog(LOG_ERROR, "Unable to retrieve channel info on the current driver \n"
"error number is : %i \n; check BASS_ASIO_ErrorGetCode\n",
BASS_ASIO_ErrorGetCode());
}
obs_property_list_clear(list);
//these settings are ignored, optimal is picked between float and native for least
//amount of processing possible
if (ret && ret2) {
if (channelInfo.format == BASS_ASIO_FORMAT_16BIT) {
obs_property_list_add_int(list, "16 bit (native)", AUDIO_FORMAT_16BIT);
obs_property_list_add_int(list, "32 bit", AUDIO_FORMAT_32BIT);
obs_property_list_add_int(list, "32 bit float", AUDIO_FORMAT_FLOAT);
} else if (channelInfo.format == BASS_ASIO_FORMAT_32BIT) {
obs_property_list_add_int(list, "16 bit", AUDIO_FORMAT_16BIT);
obs_property_list_add_int(list, "32 bit (native)", AUDIO_FORMAT_32BIT);
obs_property_list_add_int(list, "32 bit float", AUDIO_FORMAT_FLOAT);
} else if (channelInfo.format == BASS_ASIO_FORMAT_FLOAT) {
obs_property_list_add_int(list, "16 bit", AUDIO_FORMAT_16BIT);
obs_property_list_add_int(list, "32 bit", AUDIO_FORMAT_32BIT);
obs_property_list_add_int(list, "32 bit float (native)", AUDIO_FORMAT_FLOAT);
} else {
blog(LOG_ERROR, "Your device uses unsupported bit depth.\n"
"Only 16 bit, 32 bit signed int and 32 bit float are supported.\n"
"Change accordingly your device settings.\n"
"Forcing bit depth to 32 bit float");
obs_property_list_add_int(list, "32 bit float", AUDIO_FORMAT_FLOAT);
return false;
}
return true;
}
return false;
}
//create list of device supported buffer sizes
static bool fill_out_buffer_sizes(obs_properties_t *props, obs_property_t *list, obs_data_t *settings) {
BASS_ASIO_INFO info;
bool ret = BASS_ASIO_GetInfo(&info);
if (!ret) {
blog(LOG_ERROR, "Unable to retrieve info on the current driver \n"
"error number is : %i \n; check BASS_ASIO_ErrorGetCode\n",
BASS_ASIO_ErrorGetCode());
}
obs_property_list_clear(list);
if (ret) {
if (info.bufgran == -1) {
long long gran_buffer = info.bufmin;
while (gran_buffer <= info.bufmax) {
int n = snprintf(NULL, 0, "%llu%s", gran_buffer, (gran_buffer == info.bufpref ? " (preferred)" : ""));