blob: f3bb48d00737a27af81a4184165cce205912a904 [file] [log] [blame]
bellard1d14ffa2005-10-30 18:58:22 +00001/*
2 * QEMU DirectSound audio driver
3 *
4 * Copyright (c) 2005 Vassili Karpov (malc)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/*
26 * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation
27 */
28
Peter Maydell6086a562016-01-18 17:33:52 +000029#include "qemu/osdep.h"
pbrook749bc4b2007-11-17 17:35:54 +000030#include "audio.h"
bellard1d14ffa2005-10-30 18:58:22 +000031
32#define AUDIO_CAP "dsound"
33#include "audio_int.h"
Kővágó, Zoltán4a3b8b32019-03-08 23:34:18 +010034#include "qemu/host-utils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020035#include "qemu/module.h"
bellard1d14ffa2005-10-30 18:58:22 +000036
37#include <windows.h>
ths4fddf622007-12-17 04:42:29 +000038#include <mmsystem.h>
bellard1d14ffa2005-10-30 18:58:22 +000039#include <objbase.h>
40#include <dsound.h>
41
malcd5631632009-10-10 01:13:41 +040042#include "audio_win_int.h"
43
bellard1d14ffa2005-10-30 18:58:22 +000044/* #define DEBUG_DSOUND */
45
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +020046typedef struct {
bellard1d14ffa2005-10-30 18:58:22 +000047 LPDIRECTSOUND dsound;
48 LPDIRECTSOUNDCAPTURE dsound_capture;
malc1ea879e2008-12-03 22:48:44 +000049 struct audsettings settings;
Kővágó, Zoltán4a3b8b32019-03-08 23:34:18 +010050 Audiodev *dev;
bellard1d14ffa2005-10-30 18:58:22 +000051} dsound;
52
bellard1d14ffa2005-10-30 18:58:22 +000053typedef struct {
54 HWVoiceOut hw;
55 LPDIRECTSOUNDBUFFER dsound_buffer;
Kővágó, Zoltánfb35c2c2020-02-03 00:02:23 +010056 bool first_time;
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +020057 dsound *s;
bellard1d14ffa2005-10-30 18:58:22 +000058} DSoundVoiceOut;
59
60typedef struct {
61 HWVoiceIn hw;
bellard1d14ffa2005-10-30 18:58:22 +000062 LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
Kővágó, Zoltánfb35c2c2020-02-03 00:02:23 +010063 bool first_time;
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +020064 dsound *s;
bellard1d14ffa2005-10-30 18:58:22 +000065} DSoundVoiceIn;
66
67static void dsound_log_hresult (HRESULT hr)
68{
69 const char *str = "BUG";
70
71 switch (hr) {
72 case DS_OK:
73 str = "The method succeeded";
74 break;
75#ifdef DS_NO_VIRTUALIZATION
76 case DS_NO_VIRTUALIZATION:
77 str = "The buffer was created, but another 3D algorithm was substituted";
78 break;
79#endif
80#ifdef DS_INCOMPLETE
81 case DS_INCOMPLETE:
82 str = "The method succeeded, but not all the optional effects were obtained";
83 break;
84#endif
85#ifdef DSERR_ACCESSDENIED
86 case DSERR_ACCESSDENIED:
87 str = "The request failed because access was denied";
88 break;
89#endif
90#ifdef DSERR_ALLOCATED
91 case DSERR_ALLOCATED:
Zhang Hanc60840c2021-01-15 09:24:28 +080092 str = "The request failed because resources, "
93 "such as a priority level, were already in use "
94 "by another caller";
bellard1d14ffa2005-10-30 18:58:22 +000095 break;
96#endif
97#ifdef DSERR_ALREADYINITIALIZED
98 case DSERR_ALREADYINITIALIZED:
99 str = "The object is already initialized";
100 break;
101#endif
102#ifdef DSERR_BADFORMAT
103 case DSERR_BADFORMAT:
104 str = "The specified wave format is not supported";
105 break;
106#endif
107#ifdef DSERR_BADSENDBUFFERGUID
108 case DSERR_BADSENDBUFFERGUID:
Zhang Hanc60840c2021-01-15 09:24:28 +0800109 str = "The GUID specified in an audiopath file "
110 "does not match a valid mix-in buffer";
bellard1d14ffa2005-10-30 18:58:22 +0000111 break;
112#endif
113#ifdef DSERR_BUFFERLOST
114 case DSERR_BUFFERLOST:
115 str = "The buffer memory has been lost and must be restored";
116 break;
117#endif
118#ifdef DSERR_BUFFERTOOSMALL
119 case DSERR_BUFFERTOOSMALL:
Zhang Hanc60840c2021-01-15 09:24:28 +0800120 str = "The buffer size is not great enough to "
121 "enable effects processing";
bellard1d14ffa2005-10-30 18:58:22 +0000122 break;
123#endif
124#ifdef DSERR_CONTROLUNAVAIL
125 case DSERR_CONTROLUNAVAIL:
Zhang Hanc60840c2021-01-15 09:24:28 +0800126 str = "The buffer control (volume, pan, and so on) "
127 "requested by the caller is not available. "
128 "Controls must be specified when the buffer is created, "
129 "using the dwFlags member of DSBUFFERDESC";
bellard1d14ffa2005-10-30 18:58:22 +0000130 break;
131#endif
132#ifdef DSERR_DS8_REQUIRED
133 case DSERR_DS8_REQUIRED:
Zhang Hanc60840c2021-01-15 09:24:28 +0800134 str = "A DirectSound object of class CLSID_DirectSound8 or later "
135 "is required for the requested functionality. "
136 "For more information, see IDirectSound8 Interface";
bellard1d14ffa2005-10-30 18:58:22 +0000137 break;
138#endif
139#ifdef DSERR_FXUNAVAILABLE
140 case DSERR_FXUNAVAILABLE:
Zhang Hanc60840c2021-01-15 09:24:28 +0800141 str = "The effects requested could not be found on the system, "
142 "or they are in the wrong order or in the wrong location; "
143 "for example, an effect expected in hardware "
144 "was found in software";
bellard1d14ffa2005-10-30 18:58:22 +0000145 break;
146#endif
147#ifdef DSERR_GENERIC
Zhang Han3c8de962021-01-15 09:24:26 +0800148 case DSERR_GENERIC:
bellard1d14ffa2005-10-30 18:58:22 +0000149 str = "An undetermined error occurred inside the DirectSound subsystem";
150 break;
151#endif
152#ifdef DSERR_INVALIDCALL
153 case DSERR_INVALIDCALL:
154 str = "This function is not valid for the current state of this object";
155 break;
156#endif
157#ifdef DSERR_INVALIDPARAM
158 case DSERR_INVALIDPARAM:
159 str = "An invalid parameter was passed to the returning function";
160 break;
161#endif
162#ifdef DSERR_NOAGGREGATION
163 case DSERR_NOAGGREGATION:
164 str = "The object does not support aggregation";
165 break;
166#endif
167#ifdef DSERR_NODRIVER
168 case DSERR_NODRIVER:
Zhang Hanc60840c2021-01-15 09:24:28 +0800169 str = "No sound driver is available for use, "
170 "or the given GUID is not a valid DirectSound device ID";
bellard1d14ffa2005-10-30 18:58:22 +0000171 break;
172#endif
173#ifdef DSERR_NOINTERFACE
174 case DSERR_NOINTERFACE:
175 str = "The requested COM interface is not available";
176 break;
177#endif
178#ifdef DSERR_OBJECTNOTFOUND
179 case DSERR_OBJECTNOTFOUND:
180 str = "The requested object was not found";
181 break;
182#endif
183#ifdef DSERR_OTHERAPPHASPRIO
184 case DSERR_OTHERAPPHASPRIO:
Zhang Hanc60840c2021-01-15 09:24:28 +0800185 str = "Another application has a higher priority level, "
186 "preventing this call from succeeding";
bellard1d14ffa2005-10-30 18:58:22 +0000187 break;
188#endif
189#ifdef DSERR_OUTOFMEMORY
190 case DSERR_OUTOFMEMORY:
Zhang Hanc60840c2021-01-15 09:24:28 +0800191 str = "The DirectSound subsystem could not allocate "
192 "sufficient memory to complete the caller's request";
bellard1d14ffa2005-10-30 18:58:22 +0000193 break;
194#endif
195#ifdef DSERR_PRIOLEVELNEEDED
196 case DSERR_PRIOLEVELNEEDED:
197 str = "A cooperative level of DSSCL_PRIORITY or higher is required";
198 break;
199#endif
200#ifdef DSERR_SENDLOOP
201 case DSERR_SENDLOOP:
202 str = "A circular loop of send effects was detected";
203 break;
204#endif
205#ifdef DSERR_UNINITIALIZED
206 case DSERR_UNINITIALIZED:
Zhang Hanc60840c2021-01-15 09:24:28 +0800207 str = "The Initialize method has not been called "
208 "or has not been called successfully "
209 "before other methods were called";
bellard1d14ffa2005-10-30 18:58:22 +0000210 break;
211#endif
212#ifdef DSERR_UNSUPPORTED
213 case DSERR_UNSUPPORTED:
214 str = "The function called is not supported at this time";
215 break;
216#endif
217 default:
Zhang Handea7d842021-01-15 09:24:29 +0800218 AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT 0x%lx)\n", hr);
bellard1d14ffa2005-10-30 18:58:22 +0000219 return;
220 }
221
222 AUD_log (AUDIO_CAP, "Reason: %s\n", str);
223}
224
Marc-André Lureau9edc6312022-02-20 20:39:25 +0400225static void G_GNUC_PRINTF (2, 3) dsound_logerr (
bellard1d14ffa2005-10-30 18:58:22 +0000226 HRESULT hr,
227 const char *fmt,
228 ...
229 )
230{
231 va_list ap;
232
233 va_start (ap, fmt);
234 AUD_vlog (AUDIO_CAP, fmt, ap);
235 va_end (ap);
236
237 dsound_log_hresult (hr);
238}
239
Marc-André Lureau9edc6312022-02-20 20:39:25 +0400240static void G_GNUC_PRINTF (3, 4) dsound_logerr2 (
bellard1d14ffa2005-10-30 18:58:22 +0000241 HRESULT hr,
242 const char *typ,
243 const char *fmt,
244 ...
245 )
246{
247 va_list ap;
248
bellardc0fe3822005-11-05 18:55:28 +0000249 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
bellard1d14ffa2005-10-30 18:58:22 +0000250 va_start (ap, fmt);
251 AUD_vlog (AUDIO_CAP, fmt, ap);
252 va_end (ap);
253
254 dsound_log_hresult (hr);
255}
256
bellard1d14ffa2005-10-30 18:58:22 +0000257#ifdef DEBUG_DSOUND
258static void print_wave_format (WAVEFORMATEX *wfx)
259{
260 dolog ("tag = %d\n", wfx->wFormatTag);
261 dolog ("nChannels = %d\n", wfx->nChannels);
262 dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec);
263 dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
264 dolog ("nBlockAlign = %d\n", wfx->nBlockAlign);
265 dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample);
266 dolog ("cbSize = %d\n", wfx->cbSize);
267}
268#endif
269
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200270static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
bellard1d14ffa2005-10-30 18:58:22 +0000271{
272 HRESULT hr;
bellard1d14ffa2005-10-30 18:58:22 +0000273
Kővágó, Zoltán27629552015-06-12 14:33:04 +0200274 hr = IDirectSoundBuffer_Restore (dsb);
bellard1d14ffa2005-10-30 18:58:22 +0000275
Kővágó, Zoltán27629552015-06-12 14:33:04 +0200276 if (hr != DS_OK) {
277 dsound_logerr (hr, "Could not restore playback buffer\n");
278 return -1;
bellard1d14ffa2005-10-30 18:58:22 +0000279 }
Kővágó, Zoltán27629552015-06-12 14:33:04 +0200280 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000281}
282
bellard1d14ffa2005-10-30 18:58:22 +0000283#include "dsound_template.h"
284#define DSBTYPE_IN
285#include "dsound_template.h"
286#undef DSBTYPE_IN
287
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200288static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
289 dsound *s)
bellard1d14ffa2005-10-30 18:58:22 +0000290{
291 HRESULT hr;
bellard1d14ffa2005-10-30 18:58:22 +0000292
Kővágó, Zoltán27629552015-06-12 14:33:04 +0200293 hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
294 if (FAILED (hr)) {
295 dsound_logerr (hr, "Could not get playback buffer status\n");
296 return -1;
297 }
bellard1d14ffa2005-10-30 18:58:22 +0000298
Volker Rümelin4ba664c2020-04-05 09:50:15 +0200299 if (*statusp & DSBSTATUS_BUFFERLOST) {
Kővágó, Zoltán27629552015-06-12 14:33:04 +0200300 dsound_restore_out(dsb, s);
301 return -1;
bellard1d14ffa2005-10-30 18:58:22 +0000302 }
303
304 return 0;
305}
306
307static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
308 DWORD *statusp)
309{
310 HRESULT hr;
311
312 hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
313 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000314 dsound_logerr (hr, "Could not get capture buffer status\n");
bellard1d14ffa2005-10-30 18:58:22 +0000315 return -1;
316 }
317
318 return 0;
319}
320
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200321static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
322 dsound *s)
bellard1d14ffa2005-10-30 18:58:22 +0000323{
324 int err;
325 LPVOID p1, p2;
326 DWORD blen1, blen2, len1, len2;
327
328 err = dsound_lock_out (
329 dsb,
330 &hw->info,
331 0,
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200332 hw->size_emul,
bellard1d14ffa2005-10-30 18:58:22 +0000333 &p1, &p2,
334 &blen1, &blen2,
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200335 1,
336 s
bellard1d14ffa2005-10-30 18:58:22 +0000337 );
338 if (err) {
339 return;
340 }
341
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200342 len1 = blen1 / hw->info.bytes_per_frame;
343 len2 = blen2 / hw->info.bytes_per_frame;
bellard1d14ffa2005-10-30 18:58:22 +0000344
345#ifdef DEBUG_DSOUND
346 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
347 p1, blen1, len1,
348 p2, blen2, len2);
349#endif
350
351 if (p1 && len1) {
352 audio_pcm_info_clear_buf (&hw->info, p1, len1);
353 }
354
355 if (p2 && len2) {
356 audio_pcm_info_clear_buf (&hw->info, p2, len2);
357 }
358
359 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
360}
361
Volker Rümelin3c18e432021-01-10 11:02:37 +0100362static int dsound_set_cooperative_level(dsound *s)
bellard1d14ffa2005-10-30 18:58:22 +0000363{
bellard1d14ffa2005-10-30 18:58:22 +0000364 HRESULT hr;
bellard1d14ffa2005-10-30 18:58:22 +0000365 HWND hwnd;
366
Volker Rümelin401dcf02021-01-10 11:02:36 +0100367 hwnd = GetDesktopWindow();
bellard1d14ffa2005-10-30 18:58:22 +0000368 hr = IDirectSound_SetCooperativeLevel (
369 s->dsound,
370 hwnd,
371 DSSCL_PRIORITY
372 );
373
374 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000375 dsound_logerr (hr, "Could not set cooperative level for window %p\n",
bellard1d14ffa2005-10-30 18:58:22 +0000376 hwnd);
377 return -1;
378 }
379
bellard1d14ffa2005-10-30 18:58:22 +0000380 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000381}
382
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200383static void dsound_enable_out(HWVoiceOut *hw, bool enable)
bellard1d14ffa2005-10-30 18:58:22 +0000384{
385 HRESULT hr;
386 DWORD status;
387 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
388 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200389 dsound *s = ds->s;
bellard1d14ffa2005-10-30 18:58:22 +0000390
391 if (!dsb) {
392 dolog ("Attempt to control voice without a buffer\n");
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200393 return;
bellard1d14ffa2005-10-30 18:58:22 +0000394 }
395
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200396 if (enable) {
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200397 if (dsound_get_status_out (dsb, &status, s)) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200398 return;
bellard1d14ffa2005-10-30 18:58:22 +0000399 }
400
401 if (status & DSBSTATUS_PLAYING) {
bellardc0fe3822005-11-05 18:55:28 +0000402 dolog ("warning: Voice is already playing\n");
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200403 return;
bellard1d14ffa2005-10-30 18:58:22 +0000404 }
405
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200406 dsound_clear_sample (hw, dsb, s);
bellard1d14ffa2005-10-30 18:58:22 +0000407
408 hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
409 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000410 dsound_logerr (hr, "Could not start playing buffer\n");
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200411 return;
bellard1d14ffa2005-10-30 18:58:22 +0000412 }
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200413 } else {
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200414 if (dsound_get_status_out (dsb, &status, s)) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200415 return;
bellard1d14ffa2005-10-30 18:58:22 +0000416 }
417
418 if (status & DSBSTATUS_PLAYING) {
419 hr = IDirectSoundBuffer_Stop (dsb);
420 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000421 dsound_logerr (hr, "Could not stop playing buffer\n");
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200422 return;
bellard1d14ffa2005-10-30 18:58:22 +0000423 }
Zhang Han6c6886b2021-01-15 09:24:25 +0800424 } else {
bellardc0fe3822005-11-05 18:55:28 +0000425 dolog ("warning: Voice is not playing\n");
bellard1d14ffa2005-10-30 18:58:22 +0000426 }
bellard1d14ffa2005-10-30 18:58:22 +0000427 }
bellard1d14ffa2005-10-30 18:58:22 +0000428}
429
Volker Rümelinc93a5932022-03-01 20:13:08 +0100430static size_t dsound_buffer_get_free(HWVoiceOut *hw)
bellard1d14ffa2005-10-30 18:58:22 +0000431{
bellard1d14ffa2005-10-30 18:58:22 +0000432 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
433 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200434 HRESULT hr;
Volker Rümelinc93a5932022-03-01 20:13:08 +0100435 DWORD ppos, wpos;
bellard1d14ffa2005-10-30 18:58:22 +0000436
Kővágó, Zoltánfb35c2c2020-02-03 00:02:23 +0100437 hr = IDirectSoundBuffer_GetCurrentPosition(
438 dsb, &ppos, ds->first_time ? &wpos : NULL);
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200439 if (FAILED(hr)) {
440 dsound_logerr(hr, "Could not get playback buffer position\n");
Volker Rümelinc93a5932022-03-01 20:13:08 +0100441 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000442 }
443
Kővágó, Zoltánfb35c2c2020-02-03 00:02:23 +0100444 if (ds->first_time) {
445 hw->pos_emul = wpos;
446 ds->first_time = false;
447 }
448
Volker Rümelinc93a5932022-03-01 20:13:08 +0100449 return audio_ring_dist(ppos, hw->pos_emul, hw->size_emul);
450}
bellard1d14ffa2005-10-30 18:58:22 +0000451
Volker Rümelinc93a5932022-03-01 20:13:08 +0100452static void *dsound_get_buffer_out(HWVoiceOut *hw, size_t *size)
453{
454 DSoundVoiceOut *ds = (DSoundVoiceOut *)hw;
455 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
456 DWORD act_size;
457 size_t req_size;
458 int err;
459 void *ret;
460
461 req_size = MIN(*size, hw->size_emul - hw->pos_emul);
462 assert(req_size > 0);
Kővágó, Zoltánfb35c2c2020-02-03 00:02:23 +0100463
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200464 err = dsound_lock_out(dsb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
465 &act_size, NULL, false, ds->s);
bellard1d14ffa2005-10-30 18:58:22 +0000466 if (err) {
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200467 dolog("Failed to lock buffer\n");
468 *size = 0;
469 return NULL;
470 }
471
472 *size = act_size;
473 return ret;
474}
475
476static size_t dsound_put_buffer_out(HWVoiceOut *hw, void *buf, size_t len)
477{
478 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
479 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
480 int err = dsound_unlock_out(dsb, buf, NULL, len, 0);
481
482 if (err) {
483 dolog("Failed to unlock buffer!!\n");
bellard1d14ffa2005-10-30 18:58:22 +0000484 return 0;
485 }
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200486 hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
bellard1d14ffa2005-10-30 18:58:22 +0000487
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200488 return len;
bellard1d14ffa2005-10-30 18:58:22 +0000489}
490
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200491static void dsound_enable_in(HWVoiceIn *hw, bool enable)
bellard1d14ffa2005-10-30 18:58:22 +0000492{
493 HRESULT hr;
494 DWORD status;
495 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
496 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
497
498 if (!dscb) {
499 dolog ("Attempt to control capture voice without a buffer\n");
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200500 return;
bellard1d14ffa2005-10-30 18:58:22 +0000501 }
502
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200503 if (enable) {
bellard1d14ffa2005-10-30 18:58:22 +0000504 if (dsound_get_status_in (dscb, &status)) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200505 return;
bellard1d14ffa2005-10-30 18:58:22 +0000506 }
507
508 if (status & DSCBSTATUS_CAPTURING) {
bellardc0fe3822005-11-05 18:55:28 +0000509 dolog ("warning: Voice is already capturing\n");
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200510 return;
bellard1d14ffa2005-10-30 18:58:22 +0000511 }
512
513 /* clear ?? */
514
515 hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
516 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000517 dsound_logerr (hr, "Could not start capturing\n");
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200518 return;
bellard1d14ffa2005-10-30 18:58:22 +0000519 }
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200520 } else {
bellard1d14ffa2005-10-30 18:58:22 +0000521 if (dsound_get_status_in (dscb, &status)) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200522 return;
bellard1d14ffa2005-10-30 18:58:22 +0000523 }
524
525 if (status & DSCBSTATUS_CAPTURING) {
526 hr = IDirectSoundCaptureBuffer_Stop (dscb);
527 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000528 dsound_logerr (hr, "Could not stop capturing\n");
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200529 return;
bellard1d14ffa2005-10-30 18:58:22 +0000530 }
Zhang Han6c6886b2021-01-15 09:24:25 +0800531 } else {
bellardc0fe3822005-11-05 18:55:28 +0000532 dolog ("warning: Voice is not capturing\n");
bellard1d14ffa2005-10-30 18:58:22 +0000533 }
bellard1d14ffa2005-10-30 18:58:22 +0000534 }
bellard1d14ffa2005-10-30 18:58:22 +0000535}
536
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200537static void *dsound_get_buffer_in(HWVoiceIn *hw, size_t *size)
bellard1d14ffa2005-10-30 18:58:22 +0000538{
bellard1d14ffa2005-10-30 18:58:22 +0000539 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
540 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200541 HRESULT hr;
Volker Rümelin9d90ceb2021-12-26 16:40:16 +0100542 DWORD rpos, act_size;
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200543 size_t req_size;
544 int err;
545 void *ret;
bellard1d14ffa2005-10-30 18:58:22 +0000546
Volker Rümelin9d90ceb2021-12-26 16:40:16 +0100547 hr = IDirectSoundCaptureBuffer_GetCurrentPosition(dscb, NULL, &rpos);
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200548 if (FAILED(hr)) {
549 dsound_logerr(hr, "Could not get capture buffer position\n");
550 *size = 0;
551 return NULL;
bellard1d14ffa2005-10-30 18:58:22 +0000552 }
553
Kővágó, Zoltánfb35c2c2020-02-03 00:02:23 +0100554 if (ds->first_time) {
555 hw->pos_emul = rpos;
556 ds->first_time = false;
557 }
558
Volker Rümelin9d90ceb2021-12-26 16:40:16 +0100559 req_size = audio_ring_dist(rpos, hw->pos_emul, hw->size_emul);
Volker Rümelin8d1439b2020-04-05 09:50:17 +0200560 req_size = MIN(*size, MIN(req_size, hw->size_emul - hw->pos_emul));
bellard1d14ffa2005-10-30 18:58:22 +0000561
Volker Rümelin17470292020-04-05 09:50:16 +0200562 if (req_size == 0) {
563 *size = 0;
564 return NULL;
565 }
566
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200567 err = dsound_lock_in(dscb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
568 &act_size, NULL, false, ds->s);
bellard1d14ffa2005-10-30 18:58:22 +0000569 if (err) {
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200570 dolog("Failed to lock buffer\n");
571 *size = 0;
572 return NULL;
bellard1d14ffa2005-10-30 18:58:22 +0000573 }
574
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200575 *size = act_size;
576 return ret;
577}
bellard1d14ffa2005-10-30 18:58:22 +0000578
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200579static void dsound_put_buffer_in(HWVoiceIn *hw, void *buf, size_t len)
580{
581 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
582 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
583 int err = dsound_unlock_in(dscb, buf, NULL, len, 0);
584
585 if (err) {
586 dolog("Failed to unlock buffer!!\n");
587 return;
bellard1d14ffa2005-10-30 18:58:22 +0000588 }
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200589 hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
bellard1d14ffa2005-10-30 18:58:22 +0000590}
591
592static void dsound_audio_fini (void *opaque)
593{
594 HRESULT hr;
595 dsound *s = opaque;
596
597 if (!s->dsound) {
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200598 g_free(s);
bellard1d14ffa2005-10-30 18:58:22 +0000599 return;
600 }
601
602 hr = IDirectSound_Release (s->dsound);
603 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000604 dsound_logerr (hr, "Could not release DirectSound\n");
bellard1d14ffa2005-10-30 18:58:22 +0000605 }
606 s->dsound = NULL;
607
608 if (!s->dsound_capture) {
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200609 g_free(s);
bellard1d14ffa2005-10-30 18:58:22 +0000610 return;
611 }
612
613 hr = IDirectSoundCapture_Release (s->dsound_capture);
614 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000615 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
bellard1d14ffa2005-10-30 18:58:22 +0000616 }
617 s->dsound_capture = NULL;
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200618
619 g_free(s);
bellard1d14ffa2005-10-30 18:58:22 +0000620}
621
Paolo Bonzinif6061732023-09-22 19:13:44 +0200622static void *dsound_audio_init(Audiodev *dev, Error **errp)
bellard1d14ffa2005-10-30 18:58:22 +0000623{
624 int err;
625 HRESULT hr;
Markus Armbrusterb21e2382022-03-15 15:41:56 +0100626 dsound *s = g_new0(dsound, 1);
Kővágó, Zoltán4a3b8b32019-03-08 23:34:18 +0100627 AudiodevDsoundOptions *dso;
bellard1d14ffa2005-10-30 18:58:22 +0000628
Kővágó, Zoltán4a3b8b32019-03-08 23:34:18 +0100629 assert(dev->driver == AUDIODEV_DRIVER_DSOUND);
630 s->dev = dev;
631 dso = &dev->u.dsound;
632
633 if (!dso->has_latency) {
634 dso->has_latency = true;
635 dso->latency = 10000; /* 10 ms */
636 }
637
bellard1d14ffa2005-10-30 18:58:22 +0000638 hr = CoInitialize (NULL);
639 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000640 dsound_logerr (hr, "Could not initialize COM\n");
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200641 g_free(s);
bellard1d14ffa2005-10-30 18:58:22 +0000642 return NULL;
643 }
644
645 hr = CoCreateInstance (
646 &CLSID_DirectSound,
647 NULL,
648 CLSCTX_ALL,
649 &IID_IDirectSound,
650 (void **) &s->dsound
651 );
652 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000653 dsound_logerr (hr, "Could not create DirectSound instance\n");
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200654 g_free(s);
bellard1d14ffa2005-10-30 18:58:22 +0000655 return NULL;
656 }
657
658 hr = IDirectSound_Initialize (s->dsound, NULL);
659 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000660 dsound_logerr (hr, "Could not initialize DirectSound\n");
bellard8ead62c2006-07-04 16:51:32 +0000661
662 hr = IDirectSound_Release (s->dsound);
663 if (FAILED (hr)) {
664 dsound_logerr (hr, "Could not release DirectSound\n");
665 }
Kővágó, Zoltán191e1f02015-06-03 23:03:52 +0200666 g_free(s);
bellard1d14ffa2005-10-30 18:58:22 +0000667 return NULL;
668 }
669
670 hr = CoCreateInstance (
671 &CLSID_DirectSoundCapture,
672 NULL,
673 CLSCTX_ALL,
674 &IID_IDirectSoundCapture,
675 (void **) &s->dsound_capture
676 );
677 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000678 dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
Zhang Han6c6886b2021-01-15 09:24:25 +0800679 } else {
bellard1d14ffa2005-10-30 18:58:22 +0000680 hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
681 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000682 dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
bellard1d14ffa2005-10-30 18:58:22 +0000683
684 hr = IDirectSoundCapture_Release (s->dsound_capture);
685 if (FAILED (hr)) {
bellardc0fe3822005-11-05 18:55:28 +0000686 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
bellard1d14ffa2005-10-30 18:58:22 +0000687 }
688 s->dsound_capture = NULL;
689 }
690 }
691
Volker Rümelin3c18e432021-01-10 11:02:37 +0100692 err = dsound_set_cooperative_level(s);
bellard1d14ffa2005-10-30 18:58:22 +0000693 if (err) {
694 dsound_audio_fini (s);
695 return NULL;
696 }
697
698 return s;
699}
700
blueswir135f4b582008-10-06 18:08:30 +0000701static struct audio_pcm_ops dsound_pcm_ops = {
Juan Quintela1dd3e4d2009-08-11 02:31:15 +0200702 .init_out = dsound_init_out,
703 .fini_out = dsound_fini_out,
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200704 .write = audio_generic_write,
Volker Rümelinc93a5932022-03-01 20:13:08 +0100705 .buffer_get_free = dsound_buffer_get_free,
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200706 .get_buffer_out = dsound_get_buffer_out,
707 .put_buffer_out = dsound_put_buffer_out,
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200708 .enable_out = dsound_enable_out,
bellard1d14ffa2005-10-30 18:58:22 +0000709
Juan Quintela1dd3e4d2009-08-11 02:31:15 +0200710 .init_in = dsound_init_in,
711 .fini_in = dsound_fini_in,
Kővágó, Zoltán7fa97542019-09-19 23:24:12 +0200712 .read = audio_generic_read,
713 .get_buffer_in = dsound_get_buffer_in,
714 .put_buffer_in = dsound_put_buffer_in,
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200715 .enable_in = dsound_enable_in,
bellard1d14ffa2005-10-30 18:58:22 +0000716};
717
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100718static struct audio_driver dsound_audio_driver = {
Juan Quintelabee37f32009-08-11 02:31:14 +0200719 .name = "dsound",
720 .descr = "DirectSound http://wikipedia.org/wiki/DirectSound",
Juan Quintelabee37f32009-08-11 02:31:14 +0200721 .init = dsound_audio_init,
722 .fini = dsound_audio_fini,
723 .pcm_ops = &dsound_pcm_ops,
Juan Quintelabee37f32009-08-11 02:31:14 +0200724 .max_voices_out = INT_MAX,
725 .max_voices_in = 1,
726 .voice_size_out = sizeof (DSoundVoiceOut),
Consul15c875a2009-08-25 14:48:50 -0700727 .voice_size_in = sizeof (DSoundVoiceIn)
bellard1d14ffa2005-10-30 18:58:22 +0000728};
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100729
730static void register_audio_dsound(void)
731{
732 audio_driver_register(&dsound_audio_driver);
733}
734type_init(register_audio_dsound);