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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
|
/*
* QEMU DirectSound audio driver
*
* Copyright (c) 2005 Vassili Karpov (malc)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* SEAL 1.07 by Carlos 'pel' Hasan was used as documentation
*/
/*
* Patched audio jack hot-plug event handling
*
* Contributors:
* - S-Core Co., Ltd
*
* Contact:
* Byeongki Shin <bk0121.shin@samsung.com>
*/
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "audio.h"
#include "qemu/thread.h"
#define AUDIO_CAP "dsound"
#define COBJMACROS
#include "audio_int.h"
#include <windows.h>
#include <mmsystem.h>
#include <objbase.h>
#include <dsound.h>
#include <initguid.h>
#include <mmdeviceapi.h>
#include "audio_win_int.h"
/* #define DEBUG_DSOUND */
#ifdef DEBUG_DSOUND
#define dolog2 dolog
#else
#define dolog2(...)
#endif
/* Assume that each plugging event cannot occur at the same time */
enum dsound_plugging_event {
OUT_UNPLUGGED,
OUT_PLUGGED,
IN_UNPLUGGED,
IN_PLUGGED,
NOT_SUPPORTED
};
typedef struct {
int bufsize_in;
int bufsize_out;
int latency_millis;
} DSoundConf;
typedef struct {
HWVoiceOut hw;
LPDIRECTSOUNDBUFFER dsound_buffer;
DWORD old_pos;
int first_time;
struct dsound *s;
#ifdef DEBUG_DSOUND
DWORD old_ppos;
DWORD played;
DWORD mixed;
#endif
} DSoundVoiceOut;
typedef struct {
HWVoiceIn hw;
int first_time;
LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
struct dsound *s;
} DSoundVoiceIn;
typedef struct dsound {
LPDIRECTSOUND dsound;
LPDIRECTSOUNDCAPTURE dsound_capture;
DSoundVoiceIn *dsi;
DSoundVoiceOut *dso;
struct audsettings settings;
DSoundConf conf;
IMMDeviceEnumerator *mmdev_enumerator;
IMMNotificationClient noti_client;
enum dsound_plugging_event event;
bool out_init_success;
bool in_init_success;
QemuThread th;
QemuMutex th_mutex;
QemuMutex init_mutex;
QemuCond th_cond;
QemuCond init_cond;
bool th_initialized;
bool th_exit;
} dsound;
static void dsound_audio_fini (void *opaque);
static void dsound_log_hresult (HRESULT hr)
{
const char *str = "BUG";
switch (hr) {
case DS_OK:
str = "The method succeeded";
break;
#ifdef DS_NO_VIRTUALIZATION
case DS_NO_VIRTUALIZATION:
str = "The buffer was created, but another 3D algorithm was substituted";
break;
#endif
#ifdef DS_INCOMPLETE
case DS_INCOMPLETE:
str = "The method succeeded, but not all the optional effects were obtained";
break;
#endif
#ifdef DSERR_ACCESSDENIED
case DSERR_ACCESSDENIED:
str = "The request failed because access was denied";
break;
#endif
#ifdef DSERR_ALLOCATED
case DSERR_ALLOCATED:
str = "The request failed because resources, such as a priority level, were already in use by another caller";
break;
#endif
#ifdef DSERR_ALREADYINITIALIZED
case DSERR_ALREADYINITIALIZED:
str = "The object is already initialized";
break;
#endif
#ifdef DSERR_BADFORMAT
case DSERR_BADFORMAT:
str = "The specified wave format is not supported";
break;
#endif
#ifdef DSERR_BADSENDBUFFERGUID
case DSERR_BADSENDBUFFERGUID:
str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
break;
#endif
#ifdef DSERR_BUFFERLOST
case DSERR_BUFFERLOST:
str = "The buffer memory has been lost and must be restored";
break;
#endif
#ifdef DSERR_BUFFERTOOSMALL
case DSERR_BUFFERTOOSMALL:
str = "The buffer size is not great enough to enable effects processing";
break;
#endif
#ifdef DSERR_CONTROLUNAVAIL
case DSERR_CONTROLUNAVAIL:
str = "The buffer control (volume, pan, and so on) requested by the caller is not available. Controls must be specified when the buffer is created, using the dwFlags member of DSBUFFERDESC";
break;
#endif
#ifdef DSERR_DS8_REQUIRED
case DSERR_DS8_REQUIRED:
str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
break;
#endif
#ifdef DSERR_FXUNAVAILABLE
case DSERR_FXUNAVAILABLE:
str = "The effects requested could not be found on the system, or they are in the wrong order or in the wrong location; for example, an effect expected in hardware was found in software";
break;
#endif
#ifdef DSERR_GENERIC
case DSERR_GENERIC :
str = "An undetermined error occurred inside the DirectSound subsystem";
break;
#endif
#ifdef DSERR_INVALIDCALL
case DSERR_INVALIDCALL:
str = "This function is not valid for the current state of this object";
break;
#endif
#ifdef DSERR_INVALIDPARAM
case DSERR_INVALIDPARAM:
str = "An invalid parameter was passed to the returning function";
break;
#endif
#ifdef DSERR_NOAGGREGATION
case DSERR_NOAGGREGATION:
str = "The object does not support aggregation";
break;
#endif
#ifdef DSERR_NODRIVER
case DSERR_NODRIVER:
str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
break;
#endif
#ifdef DSERR_NOINTERFACE
case DSERR_NOINTERFACE:
str = "The requested COM interface is not available";
break;
#endif
#ifdef DSERR_OBJECTNOTFOUND
case DSERR_OBJECTNOTFOUND:
str = "The requested object was not found";
break;
#endif
#ifdef DSERR_OTHERAPPHASPRIO
case DSERR_OTHERAPPHASPRIO:
str = "Another application has a higher priority level, preventing this call from succeeding";
break;
#endif
#ifdef DSERR_OUTOFMEMORY
case DSERR_OUTOFMEMORY:
str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
break;
#endif
#ifdef DSERR_PRIOLEVELNEEDED
case DSERR_PRIOLEVELNEEDED:
str = "A cooperative level of DSSCL_PRIORITY or higher is required";
break;
#endif
#ifdef DSERR_SENDLOOP
case DSERR_SENDLOOP:
str = "A circular loop of send effects was detected";
break;
#endif
#ifdef DSERR_UNINITIALIZED
case DSERR_UNINITIALIZED:
str = "The Initialize method has not been called or has not been called successfully before other methods were called";
break;
#endif
#ifdef DSERR_UNSUPPORTED
case DSERR_UNSUPPORTED:
str = "The function called is not supported at this time";
break;
#endif
default:
AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
return;
}
AUD_log (AUDIO_CAP, "Reason: %s\n", str);
}
static void GCC_FMT_ATTR (2, 3) dsound_logerr (
HRESULT hr,
const char *fmt,
...
)
{
va_list ap;
va_start (ap, fmt);
AUD_vlog (AUDIO_CAP, fmt, ap);
va_end (ap);
dsound_log_hresult (hr);
}
static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
HRESULT hr,
const char *typ,
const char *fmt,
...
)
{
va_list ap;
AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
va_start (ap, fmt);
AUD_vlog (AUDIO_CAP, fmt, ap);
va_end (ap);
dsound_log_hresult (hr);
}
static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis)
{
return (millis * info->bytes_per_second) / 1000;
}
#ifdef DEBUG_DSOUND
static void print_wave_format (WAVEFORMATEX *wfx)
{
dolog ("tag = %d\n", wfx->wFormatTag);
dolog ("nChannels = %d\n", wfx->nChannels);
dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec);
dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
dolog ("nBlockAlign = %d\n", wfx->nBlockAlign);
dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample);
dolog ("cbSize = %d\n", wfx->cbSize);
}
#endif
static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
{
HRESULT hr;
hr = IDirectSoundBuffer_Restore (dsb);
if (hr != DS_OK) {
dsound_logerr (hr, "Could not restore playback buffer\n");
return -1;
}
return 0;
}
#include "dsound_template.h"
#define DSBTYPE_IN
#include "dsound_template.h"
#undef DSBTYPE_IN
static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
dsound *s)
{
HRESULT hr;
hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not get playback buffer status\n");
return -1;
}
/* Because ctl_out of pcm_ops changed to be checked its success,
* this function should be able to return success
* in the DSERR_BUFFERLOST case.
*/
if (*statusp & DSERR_BUFFERLOST) {
return dsound_restore_out(dsb, s);
}
return 0;
}
static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
DWORD *statusp)
{
HRESULT hr;
hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not get capture buffer status\n");
return -1;
}
return 0;
}
static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
{
int src_len1 = dst_len;
int src_len2 = 0;
int pos = hw->rpos + dst_len;
struct st_sample *src1 = hw->mix_buf + hw->rpos;
struct st_sample *src2 = NULL;
if (pos > hw->samples) {
src_len1 = hw->samples - hw->rpos;
src2 = hw->mix_buf;
src_len2 = dst_len - src_len1;
pos = src_len2;
}
if (src_len1) {
hw->clip (dst, src1, src_len1);
}
if (src_len2) {
dst = advance (dst, src_len1 << hw->info.shift);
hw->clip (dst, src2, src_len2);
}
hw->rpos = pos % hw->samples;
}
static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
dsound *s)
{
int err;
LPVOID p1, p2;
DWORD blen1, blen2, len1, len2;
err = dsound_lock_out (
dsb,
&hw->info,
0,
hw->samples << hw->info.shift,
&p1, &p2,
&blen1, &blen2,
1,
s
);
if (err) {
return;
}
len1 = blen1 >> hw->info.shift;
len2 = blen2 >> hw->info.shift;
#ifdef DEBUG_DSOUND
dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
p1, blen1, len1,
p2, blen2, len2);
#endif
if (p1 && len1) {
audio_pcm_info_clear_buf (&hw->info, p1, len1);
}
if (p2 && len2) {
audio_pcm_info_clear_buf (&hw->info, p2, len2);
}
dsound_unlock_out (dsb, p1, p2, blen1, blen2);
}
static int dsound_open (dsound *s)
{
HRESULT hr;
HWND hwnd;
hwnd = GetForegroundWindow ();
hr = IDirectSound_SetCooperativeLevel (
s->dsound,
hwnd,
DSSCL_PRIORITY
);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not set cooperative level for window %p\n",
hwnd);
return -1;
}
return 0;
}
static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
{
HRESULT hr;
DWORD status;
DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
dsound *s = ds->s;
if (!dsb) {
dolog2 ("Attempt to control voice without a buffer\n");
return -1;
}
dolog2 ("(%s) ds(%p), ds->dsound_buffer(%p)\n", __func__,
ds, &ds->dsound_buffer);
switch (cmd) {
case VOICE_ENABLE:
if (dsound_get_status_out (dsb, &status, s)) {
return -1;
}
if (status & DSBSTATUS_PLAYING) {
dolog ("warning: Voice is already playing\n");
return 0;
}
dsound_clear_sample (hw, dsb, s);
hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not start playing buffer\n");
return -1;
}
break;
case VOICE_DISABLE:
if (dsound_get_status_out (dsb, &status, s)) {
return -1;
}
if (status & DSBSTATUS_PLAYING) {
hr = IDirectSoundBuffer_Stop (dsb);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not stop playing buffer\n");
return -1;
}
}
else {
dolog ("warning: Voice is not playing\n");
}
break;
}
return 0;
}
static int dsound_write (SWVoiceOut *sw, void *buf, int len)
{
return audio_pcm_sw_write (sw, buf, len);
}
static int dsound_run_out (HWVoiceOut *hw, int live)
{
int err;
HRESULT hr;
DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
int len, hwshift;
DWORD blen1, blen2;
DWORD len1, len2;
DWORD decr;
DWORD wpos, ppos, old_pos;
LPVOID p1, p2;
int bufsize;
dsound *s = ds->s;
DSoundConf *conf = &s->conf;
if (!dsb) {
dolog2 ("Attempt to run empty with playback buffer\n");
return 0;
}
hwshift = hw->info.shift;
bufsize = hw->samples << hwshift;
hr = IDirectSoundBuffer_GetCurrentPosition (
dsb,
&ppos,
ds->first_time ? &wpos : NULL
);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not get playback buffer position\n");
return 0;
}
len = live << hwshift;
if (ds->first_time) {
if (conf->latency_millis) {
DWORD cur_blat;
cur_blat = audio_ring_dist (wpos, ppos, bufsize);
ds->first_time = 0;
old_pos = wpos;
old_pos +=
millis_to_bytes (&hw->info, conf->latency_millis) - cur_blat;
old_pos %= bufsize;
old_pos &= ~hw->info.align;
}
else {
old_pos = wpos;
}
#ifdef DEBUG_DSOUND
ds->played = 0;
ds->mixed = 0;
#endif
}
else {
if (ds->old_pos == ppos) {
#ifdef DEBUG_DSOUND
dolog ("old_pos == ppos\n");
#endif
return 0;
}
#ifdef DEBUG_DSOUND
ds->played += audio_ring_dist (ds->old_pos, ppos, bufsize);
#endif
old_pos = ds->old_pos;
}
if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
len = ppos - old_pos;
}
else {
if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
len = bufsize - old_pos + ppos;
}
}
if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) {
dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
len, bufsize, old_pos, ppos);
return 0;
}
len &= ~hw->info.align;
if (!len) {
return 0;
}
#ifdef DEBUG_DSOUND
ds->old_ppos = ppos;
#endif
err = dsound_lock_out (
dsb,
&hw->info,
old_pos,
len,
&p1, &p2,
&blen1, &blen2,
0,
s
);
if (err) {
return 0;
}
len1 = blen1 >> hwshift;
len2 = blen2 >> hwshift;
decr = len1 + len2;
if (p1 && len1) {
dsound_write_sample (hw, p1, len1);
}
if (p2 && len2) {
dsound_write_sample (hw, p2, len2);
}
dsound_unlock_out (dsb, p1, p2, blen1, blen2);
ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
#ifdef DEBUG_DSOUND
ds->mixed += decr << hwshift;
dolog ("played %lu mixed %lu diff %ld sec %f\n",
ds->played,
ds->mixed,
ds->mixed - ds->played,
abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
#endif
return decr;
}
static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
{
HRESULT hr;
DWORD status;
DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
if (!dscb) {
dolog2 ("Attempt to control capture voice without a buffer\n");
return -1;
}
switch (cmd) {
case VOICE_ENABLE:
if (dsound_get_status_in (dscb, &status)) {
return -1;
}
if (status & DSCBSTATUS_CAPTURING) {
dolog ("warning: Voice is already capturing\n");
return 0;
}
/* clear ?? */
hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not start capturing\n");
return -1;
}
break;
case VOICE_DISABLE:
if (dsound_get_status_in (dscb, &status)) {
return -1;
}
if (status & DSCBSTATUS_CAPTURING) {
hr = IDirectSoundCaptureBuffer_Stop (dscb);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not stop capturing\n");
return -1;
}
}
else {
dolog ("warning: Voice is not capturing\n");
}
break;
}
return 0;
}
static int dsound_read (SWVoiceIn *sw, void *buf, int len)
{
return audio_pcm_sw_read (sw, buf, len);
}
static int dsound_run_in (HWVoiceIn *hw)
{
int err;
HRESULT hr;
DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
int live, len, dead;
DWORD blen1, blen2;
DWORD len1, len2;
DWORD decr;
DWORD cpos, rpos;
LPVOID p1, p2;
int hwshift;
dsound *s = ds->s;
if (!dscb) {
dolog2 ("Attempt to run without capture buffer\n");
return 0;
}
hwshift = hw->info.shift;
live = audio_pcm_hw_get_live_in (hw);
dead = hw->samples - live;
if (!dead) {
return 0;
}
hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
dscb,
&cpos,
ds->first_time ? &rpos : NULL
);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not get capture buffer position\n");
return 0;
}
if (ds->first_time) {
ds->first_time = 0;
if (rpos & hw->info.align) {
ldebug ("warning: Misaligned capture read position %ld(%d)\n",
rpos, hw->info.align);
}
hw->wpos = rpos >> hwshift;
}
if (cpos & hw->info.align) {
ldebug ("warning: Misaligned capture position %ld(%d)\n",
cpos, hw->info.align);
}
cpos >>= hwshift;
len = audio_ring_dist (cpos, hw->wpos, hw->samples);
if (!len) {
return 0;
}
len = audio_MIN (len, dead);
err = dsound_lock_in (
dscb,
&hw->info,
hw->wpos << hwshift,
len << hwshift,
&p1,
&p2,
&blen1,
&blen2,
0,
s
);
if (err) {
return 0;
}
len1 = blen1 >> hwshift;
len2 = blen2 >> hwshift;
decr = len1 + len2;
if (p1 && len1) {
hw->conv (hw->conv_buf + hw->wpos, p1, len1);
}
if (p2 && len2) {
hw->conv (hw->conv_buf, p2, len2);
}
dsound_unlock_in (dscb, p1, p2, blen1, blen2);
hw->wpos = (hw->wpos + decr) % hw->samples;
return decr;
}
static enum dsound_plugging_event dsound_determine_event(DWORD state, EDataFlow flow)
{
enum dsound_plugging_event event;
/* XXX: is it possible for speaker and mic to be plugged at the same time? */
if ((state != DEVICE_STATE_ACTIVE && state != DEVICE_STATE_UNPLUGGED) ||
(flow != eRender && flow != eCapture)) {
dolog("(%s) not supported event. state=%#lx, flow=%d\n", __func__, state, flow);
return NOT_SUPPORTED;
}
if (state == DEVICE_STATE_ACTIVE) {
if (flow == eRender) {
event = OUT_PLUGGED;
} else if (flow == eCapture) {
event = IN_PLUGGED;
}
} else {
if (flow == eRender) {
event = OUT_UNPLUGGED;
} else if (flow == eCapture) {
event = IN_UNPLUGGED;
}
}
return event;
}
static int dsound_audio_out_init(dsound *s)
{
HRESULT hr;
int err;
dolog2("(%s) Starting. s->dsound(%p)\n", __func__, s->dsound);
hr = IDirectSound_Initialize (s->dsound, NULL);
if (hr != DSERR_ALREADYINITIALIZED && FAILED (hr)) {
dsound_logerr (hr, "Could not initialize DirectSound\n");
return -1;
}
err = dsound_open (s);
if (err) {
dsound_audio_fini (s);
return -1;
}
dolog2("(%s) success\n", __func__);
return 0;
}
static int dsound_audio_in_init(dsound *s)
{
HRESULT hr;
dolog2("(%s) Starting. s->dsound_capture(%p)\n", __func__, s->dsound_capture);
hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
if (hr != DSERR_ALREADYINITIALIZED && FAILED (hr)) {
dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
return -1;
}
dolog("(%s) success\n", __func__);
return 0;
}
/* Implementation of IMMNotificationClient callbacks */
static HRESULT STDMETHODCALLTYPE dsound_QueryInterface(IMMNotificationClient *This,
REFIID riid, void **ppvObject)
{
dolog2("(%s)\n", __func__);
return E_NOTIMPL;
}
static ULONG STDMETHODCALLTYPE dsound_AddRef(IMMNotificationClient *This)
{
dolog2("(%s)\n", __func__);
return E_NOTIMPL;
}
static ULONG STDMETHODCALLTYPE dsound_Release(IMMNotificationClient *This)
{
dolog2("(%s)\n", __func__);
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE dsound_OnDeviceStateChanged(IMMNotificationClient* This,
LPCWSTR pwstrDeviceId, DWORD dwNewState)
{
dsound *s = CONTAINING_RECORD(This, dsound, noti_client);
HRESULT hr;
IMMDevice *mmdev;
IMMEndpoint *ep;
EDataFlow flow;
/* To decide whether IN or OUT, get IMMEndpoint interface */
hr = IMMDeviceEnumerator_GetDevice(s->mmdev_enumerator, pwstrDeviceId, &mmdev);
if (FAILED (hr)) {
dolog("(%s) Could not get mmdevice\n", __func__);
return E_FAIL;
}
dolog2("(%s) mmdev(%p)\n", __func__, mmdev);
hr = IMMDevice_QueryInterface(mmdev, &IID_IMMEndpoint, (void **)&ep);
if (FAILED (hr)) {
dolog("(%s) Could not get enpoint\n", __func__);
return E_FAIL;
}
hr = IMMEndpoint_GetDataFlow(ep, &flow);
if (FAILED (hr)) {
dolog("(%s) Could not get dataflow\n", __func__);
return E_FAIL;
}
dolog2("(%s) ep(%p), flow=%d\n", __func__, ep, flow);
qemu_mutex_lock(&s->th_mutex);
s->event = dsound_determine_event(dwNewState, flow);
if (s->event != NOT_SUPPORTED) {
qemu_cond_signal(&s->th_cond);
}
qemu_mutex_unlock(&s->th_mutex);
return S_OK;
}
static HRESULT STDMETHODCALLTYPE dsound_OnDeviceAdded(IMMNotificationClient* This,
LPCWSTR pwstrDeviceId)
{
dolog2("(%s)\n", __func__);
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE dsound_OnDeviceRemoved(IMMNotificationClient* This,
LPCWSTR pwstrDeviceId)
{
dolog2("(%s)\n", __func__);
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE dsound_OnDefaultDeviceChanged(IMMNotificationClient* This,
EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId)
{
dolog2("(%s)\n", __func__);
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE dsound_OnPropertyValueChanged(IMMNotificationClient* This,
LPCWSTR pwstrDeviceId, const PROPERTYKEY key)
{
dolog2("(%s)\n", __func__);
return E_NOTIMPL;
}
static IMMNotificationClientVtbl dsound_dsound_vtbl = {
dsound_QueryInterface,
dsound_AddRef,
dsound_Release,
dsound_OnDeviceStateChanged,
dsound_OnDeviceAdded,
dsound_OnDeviceRemoved,
dsound_OnDefaultDeviceChanged,
dsound_OnPropertyValueChanged
};
static DSoundConf glob_conf = {
.bufsize_in = 16384,
.bufsize_out = 16384,
.latency_millis = 10
};
static void dsound_audio_fini (void *opaque)
{
HRESULT hr;
dsound *s = opaque;
qemu_mutex_lock(&s->th_mutex);
s->th_exit = true;
qemu_cond_signal(&s->th_cond);
qemu_mutex_unlock(&s->th_mutex);
qemu_thread_join(&s->th);
qemu_cond_destroy(&s->th_cond);
qemu_cond_destroy(&s->init_cond);
qemu_mutex_destroy(&s->th_mutex);
qemu_mutex_destroy(&s->init_mutex);
if (!s->dsound) {
g_free(s);
return;
}
hr = IDirectSound_Release (s->dsound);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not release DirectSound\n");
}
s->dsound = NULL;
if (!s->dsound_capture) {
g_free(s);
return;
}
hr = IDirectSoundCapture_Release (s->dsound_capture);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not release DirectSoundCapture\n");
}
s->dsound_capture = NULL;
g_free(s);
}
/* Because a trial initializing in noti-handler makes some deadlock,
* the initialization of DirectSound with COM
* should be executed by one thread.
*/
static void *dsound_initialization_thread(void *opaque)
{
HRESULT hr;
dsound *s = (dsound *)opaque;
HWVoiceOut *hwo;
HWVoiceIn *hwi;
dolog2("(%s) Starting. dsound(%p)\n", __func__, s);
hr = CoInitialize (NULL);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not initialize COM\n");
goto CoFailed;
}
dolog2("(%s) CoInitialize succeeded\n", __func__);
/* For Playback */
hr = CoCreateInstance (
&CLSID_DirectSound,
NULL,
CLSCTX_ALL,
&IID_IDirectSound,
(void **) &s->dsound
);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not create DirectSound instance\n");
goto CoFailed;
}
dolog2("(%s) CoCreateInstance succeeded. s->dsound(%p)\n", __func__, s->dsound);
/* try once at init time */
if (dsound_audio_out_init(s) < 0) {
dolog2("(%s) dsound_audio_out_init failed.\n", __func__);
s->out_init_success = false;
} else {
dolog2("(%s) dsound_audio_out_init success.\n", __func__);
s->out_init_success = true;
}
/* For Capture */
hr = CoCreateInstance (
&CLSID_DirectSoundCapture,
NULL,
CLSCTX_ALL,
&IID_IDirectSoundCapture,
(void **) &s->dsound_capture
);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
goto CoFailed;
}
/* try once at init time */
if (dsound_audio_in_init(s) < 0) {
dolog2("(%s) dsound_audio_in_init failed.\n", __func__);
s->in_init_success = false;
} else {
dolog2("(%s) dsound_audio_in_init success.\n", __func__);
s->in_init_success = true;
}
CoFailed:
qemu_mutex_lock(&s->init_mutex);
s->th_initialized = true;
qemu_cond_signal(&s->init_cond);
qemu_mutex_unlock(&s->init_mutex);
if (FAILED (hr)) {
dolog("(%s) terminating.\n", __func__);
return NULL;
}
/* event handling loop */
qemu_mutex_lock(&s->th_mutex);
while (1) {
qemu_cond_wait(&s->th_cond, &s->th_mutex);
if (s->th_exit) {
break;
}
switch (s->event) {
case OUT_UNPLUGGED:
hwo = (HWVoiceOut *)s->dso;
assert(hwo != NULL);
assert(s->dsound != NULL);
dsound_fini_out(hwo);
s->out_init_success = false;
dolog("(%s) handled OUT_UNPLUGGED.\n", __func__);
break;
case OUT_PLUGGED:
hwo = (HWVoiceOut *)s->dso;
assert(hwo != NULL);
if (dsound_audio_out_init(s) < 0) {
dolog("(%s) dsound_audio_out_init failed.\n", __func__);
s->out_init_success = false;
break;
}
s->out_init_success = true;
/* After audio system init finished,
* pcm_ops.init_out is not called any more.
* So, we should update backend data here.
*/
if (dsound_init_out(hwo, &s->settings, s) < 0 ) {
dolog2("(%s) dsound_init_out failed\n", __func__);
} else {
dolog2("(%s) dsound_init_out succeeded\n", __func__);
}
dolog("(%s) handled OUT_PLUGGED.\n", __func__);
break;
case IN_UNPLUGGED:
hwi = (HWVoiceIn *)s->dsi;
assert(hwi != NULL);
assert(s->dsound_capture != NULL);
dsound_fini_in(hwi);
s->in_init_success = false;
dolog("(%s) handled IN_UNPLUGGED\n", __func__);
break;
case IN_PLUGGED:
hwi = (HWVoiceIn *)s->dsi;
assert(hwi != NULL);
if (dsound_audio_in_init(s) < 0) {
dolog("(%s) dsound_audio_in_init failed.\n", __func__);
s->in_init_success = false;
break;
}
s->in_init_success = true;
/* Since, IN has same reason to out case */
if (dsound_init_in(hwi, &s->settings, s) < 0 ) {
dolog2("(%s) dsound_init_in failed\n", __func__);
} else {
dolog2("(%s) dsound_init_in succeeded\n", __func__);
}
dolog("(%s) handled IN_PLUGGED\n", __func__);
break;
default:
dolog("(%s) Unknown event\n", __func__);
break;
}
}
qemu_mutex_unlock(&s->th_mutex);
return NULL;
}
static void dsound_create_thread(dsound *s)
{
qemu_cond_init(&s->th_cond);
qemu_cond_init(&s->init_cond);
qemu_mutex_init(&s->th_mutex);
qemu_mutex_init(&s->init_mutex);
qemu_thread_create(&s->th,
"dsound-init-thread",
dsound_initialization_thread,
(void *)s,
QEMU_THREAD_JOINABLE);
dolog("(%s) Done\n", __func__);
}
static void *dsound_audio_init (void)
{
HRESULT hr;
dsound *s = g_malloc0(sizeof(dsound));
s->conf = glob_conf;
hr = CoInitialize (NULL);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not initialize COM\n");
g_free(s);
return NULL;
}
hr = CoCreateInstance (
&CLSID_MMDeviceEnumerator,
NULL,
CLSCTX_ALL,
&IID_IMMDeviceEnumerator,
(void **) &s->mmdev_enumerator
);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not create MMDeviceEnumerator instance\n");
g_free(s);
return NULL;
}
s->noti_client.lpVtbl = &dsound_dsound_vtbl;
hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(s->mmdev_enumerator, &s->noti_client);
if (FAILED (hr)) {
dsound_logerr (hr, "Could not register endpoint notification callbacks\n");
g_free(s);
return NULL;
}
dolog("Registering endpoint notification callback succeeded.\n");
dsound_create_thread(s);
/* For serialization with init thread
* If audio.c invoks pcm_ops.init_out before the init thread finishes it's work,
* init_out callback works with undetermined status that is 'out_init_success'.
*/
qemu_mutex_lock(&s->init_mutex);
while (!s->th_initialized) {
dolog2("(%s) Waiting for finishing initialization of thread\n", __func__);
qemu_cond_wait(&s->init_cond, &s->init_mutex);
}
qemu_mutex_unlock(&s->init_mutex);
dolog ("(%s) finished\n", __func__);
return s;
}
static struct audio_option dsound_options[] = {
{
.name = "LATENCY_MILLIS",
.tag = AUD_OPT_INT,
.valp = &glob_conf.latency_millis,
.descr = "(undocumented)"
},
{
.name = "BUFSIZE_OUT",
.tag = AUD_OPT_INT,
.valp = &glob_conf.bufsize_out,
.descr = "(undocumented)"
},
{
.name = "BUFSIZE_IN",
.tag = AUD_OPT_INT,
.valp = &glob_conf.bufsize_in,
.descr = "(undocumented)"
},
{ /* End of list */ }
};
static struct audio_pcm_ops dsound_pcm_ops = {
.init_out = dsound_init_out,
.fini_out = dsound_fini_out,
.run_out = dsound_run_out,
.write = dsound_write,
.ctl_out = dsound_ctl_out,
.init_in = dsound_init_in,
.fini_in = dsound_fini_in,
.run_in = dsound_run_in,
.read = dsound_read,
.ctl_in = dsound_ctl_in
};
struct audio_driver dsound_audio_driver = {
.name = "dsound",
.descr = "DirectSound http://wikipedia.org/wiki/DirectSound",
.options = dsound_options,
.init = dsound_audio_init,
.fini = dsound_audio_fini,
.pcm_ops = &dsound_pcm_ops,
.can_be_default = 1,
.max_voices_out = INT_MAX,
.max_voices_in = 1,
.voice_size_out = sizeof (DSoundVoiceOut),
.voice_size_in = sizeof (DSoundVoiceIn)
};
|