]>
Commit | Line | Data |
---|---|---|
1d14ffa9 FB |
1 | /* |
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 | ||
6086a565 | 29 | #include "qemu/osdep.h" |
749bc4bf | 30 | #include "audio.h" |
1d14ffa9 FB |
31 | |
32 | #define AUDIO_CAP "dsound" | |
33 | #include "audio_int.h" | |
4a3b8b34 | 34 | #include "qemu/host-utils.h" |
0b8fa32f | 35 | #include "qemu/module.h" |
1d14ffa9 FB |
36 | |
37 | #include <windows.h> | |
4fddf62a | 38 | #include <mmsystem.h> |
1d14ffa9 FB |
39 | #include <objbase.h> |
40 | #include <dsound.h> | |
41 | ||
d5631638 | 42 | #include "audio_win_int.h" |
43 | ||
1d14ffa9 FB |
44 | /* #define DEBUG_DSOUND */ |
45 | ||
1d14ffa9 FB |
46 | typedef struct { |
47 | LPDIRECTSOUND dsound; | |
48 | LPDIRECTSOUNDCAPTURE dsound_capture; | |
1ea879e5 | 49 | struct audsettings settings; |
4a3b8b34 | 50 | Audiodev *dev; |
1d14ffa9 FB |
51 | } dsound; |
52 | ||
1d14ffa9 FB |
53 | typedef struct { |
54 | HWVoiceOut hw; | |
55 | LPDIRECTSOUNDBUFFER dsound_buffer; | |
191e1f0a | 56 | dsound *s; |
1d14ffa9 FB |
57 | } DSoundVoiceOut; |
58 | ||
59 | typedef struct { | |
60 | HWVoiceIn hw; | |
1d14ffa9 | 61 | LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer; |
191e1f0a | 62 | dsound *s; |
1d14ffa9 FB |
63 | } DSoundVoiceIn; |
64 | ||
65 | static void dsound_log_hresult (HRESULT hr) | |
66 | { | |
67 | const char *str = "BUG"; | |
68 | ||
69 | switch (hr) { | |
70 | case DS_OK: | |
71 | str = "The method succeeded"; | |
72 | break; | |
73 | #ifdef DS_NO_VIRTUALIZATION | |
74 | case DS_NO_VIRTUALIZATION: | |
75 | str = "The buffer was created, but another 3D algorithm was substituted"; | |
76 | break; | |
77 | #endif | |
78 | #ifdef DS_INCOMPLETE | |
79 | case DS_INCOMPLETE: | |
80 | str = "The method succeeded, but not all the optional effects were obtained"; | |
81 | break; | |
82 | #endif | |
83 | #ifdef DSERR_ACCESSDENIED | |
84 | case DSERR_ACCESSDENIED: | |
85 | str = "The request failed because access was denied"; | |
86 | break; | |
87 | #endif | |
88 | #ifdef DSERR_ALLOCATED | |
89 | case DSERR_ALLOCATED: | |
90 | str = "The request failed because resources, such as a priority level, were already in use by another caller"; | |
91 | break; | |
92 | #endif | |
93 | #ifdef DSERR_ALREADYINITIALIZED | |
94 | case DSERR_ALREADYINITIALIZED: | |
95 | str = "The object is already initialized"; | |
96 | break; | |
97 | #endif | |
98 | #ifdef DSERR_BADFORMAT | |
99 | case DSERR_BADFORMAT: | |
100 | str = "The specified wave format is not supported"; | |
101 | break; | |
102 | #endif | |
103 | #ifdef DSERR_BADSENDBUFFERGUID | |
104 | case DSERR_BADSENDBUFFERGUID: | |
105 | str = "The GUID specified in an audiopath file does not match a valid mix-in buffer"; | |
106 | break; | |
107 | #endif | |
108 | #ifdef DSERR_BUFFERLOST | |
109 | case DSERR_BUFFERLOST: | |
110 | str = "The buffer memory has been lost and must be restored"; | |
111 | break; | |
112 | #endif | |
113 | #ifdef DSERR_BUFFERTOOSMALL | |
114 | case DSERR_BUFFERTOOSMALL: | |
115 | str = "The buffer size is not great enough to enable effects processing"; | |
116 | break; | |
117 | #endif | |
118 | #ifdef DSERR_CONTROLUNAVAIL | |
119 | case DSERR_CONTROLUNAVAIL: | |
120 | 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"; | |
121 | break; | |
122 | #endif | |
123 | #ifdef DSERR_DS8_REQUIRED | |
124 | case DSERR_DS8_REQUIRED: | |
125 | str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface"; | |
126 | break; | |
127 | #endif | |
128 | #ifdef DSERR_FXUNAVAILABLE | |
129 | case DSERR_FXUNAVAILABLE: | |
130 | 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"; | |
131 | break; | |
132 | #endif | |
133 | #ifdef DSERR_GENERIC | |
134 | case DSERR_GENERIC : | |
135 | str = "An undetermined error occurred inside the DirectSound subsystem"; | |
136 | break; | |
137 | #endif | |
138 | #ifdef DSERR_INVALIDCALL | |
139 | case DSERR_INVALIDCALL: | |
140 | str = "This function is not valid for the current state of this object"; | |
141 | break; | |
142 | #endif | |
143 | #ifdef DSERR_INVALIDPARAM | |
144 | case DSERR_INVALIDPARAM: | |
145 | str = "An invalid parameter was passed to the returning function"; | |
146 | break; | |
147 | #endif | |
148 | #ifdef DSERR_NOAGGREGATION | |
149 | case DSERR_NOAGGREGATION: | |
150 | str = "The object does not support aggregation"; | |
151 | break; | |
152 | #endif | |
153 | #ifdef DSERR_NODRIVER | |
154 | case DSERR_NODRIVER: | |
155 | str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID"; | |
156 | break; | |
157 | #endif | |
158 | #ifdef DSERR_NOINTERFACE | |
159 | case DSERR_NOINTERFACE: | |
160 | str = "The requested COM interface is not available"; | |
161 | break; | |
162 | #endif | |
163 | #ifdef DSERR_OBJECTNOTFOUND | |
164 | case DSERR_OBJECTNOTFOUND: | |
165 | str = "The requested object was not found"; | |
166 | break; | |
167 | #endif | |
168 | #ifdef DSERR_OTHERAPPHASPRIO | |
169 | case DSERR_OTHERAPPHASPRIO: | |
170 | str = "Another application has a higher priority level, preventing this call from succeeding"; | |
171 | break; | |
172 | #endif | |
173 | #ifdef DSERR_OUTOFMEMORY | |
174 | case DSERR_OUTOFMEMORY: | |
175 | str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request"; | |
176 | break; | |
177 | #endif | |
178 | #ifdef DSERR_PRIOLEVELNEEDED | |
179 | case DSERR_PRIOLEVELNEEDED: | |
180 | str = "A cooperative level of DSSCL_PRIORITY or higher is required"; | |
181 | break; | |
182 | #endif | |
183 | #ifdef DSERR_SENDLOOP | |
184 | case DSERR_SENDLOOP: | |
185 | str = "A circular loop of send effects was detected"; | |
186 | break; | |
187 | #endif | |
188 | #ifdef DSERR_UNINITIALIZED | |
189 | case DSERR_UNINITIALIZED: | |
190 | str = "The Initialize method has not been called or has not been called successfully before other methods were called"; | |
191 | break; | |
192 | #endif | |
193 | #ifdef DSERR_UNSUPPORTED | |
194 | case DSERR_UNSUPPORTED: | |
195 | str = "The function called is not supported at this time"; | |
196 | break; | |
197 | #endif | |
198 | default: | |
199 | AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr); | |
200 | return; | |
201 | } | |
202 | ||
203 | AUD_log (AUDIO_CAP, "Reason: %s\n", str); | |
204 | } | |
205 | ||
206 | static void GCC_FMT_ATTR (2, 3) dsound_logerr ( | |
207 | HRESULT hr, | |
208 | const char *fmt, | |
209 | ... | |
210 | ) | |
211 | { | |
212 | va_list ap; | |
213 | ||
214 | va_start (ap, fmt); | |
215 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
216 | va_end (ap); | |
217 | ||
218 | dsound_log_hresult (hr); | |
219 | } | |
220 | ||
221 | static void GCC_FMT_ATTR (3, 4) dsound_logerr2 ( | |
222 | HRESULT hr, | |
223 | const char *typ, | |
224 | const char *fmt, | |
225 | ... | |
226 | ) | |
227 | { | |
228 | va_list ap; | |
229 | ||
c0fe3827 | 230 | AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); |
1d14ffa9 FB |
231 | va_start (ap, fmt); |
232 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
233 | va_end (ap); | |
234 | ||
235 | dsound_log_hresult (hr); | |
236 | } | |
237 | ||
1d14ffa9 FB |
238 | #ifdef DEBUG_DSOUND |
239 | static void print_wave_format (WAVEFORMATEX *wfx) | |
240 | { | |
241 | dolog ("tag = %d\n", wfx->wFormatTag); | |
242 | dolog ("nChannels = %d\n", wfx->nChannels); | |
243 | dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec); | |
244 | dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec); | |
245 | dolog ("nBlockAlign = %d\n", wfx->nBlockAlign); | |
246 | dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample); | |
247 | dolog ("cbSize = %d\n", wfx->cbSize); | |
248 | } | |
249 | #endif | |
250 | ||
191e1f0a | 251 | static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s) |
1d14ffa9 FB |
252 | { |
253 | HRESULT hr; | |
1d14ffa9 | 254 | |
2762955f | 255 | hr = IDirectSoundBuffer_Restore (dsb); |
1d14ffa9 | 256 | |
2762955f KZ |
257 | if (hr != DS_OK) { |
258 | dsound_logerr (hr, "Could not restore playback buffer\n"); | |
259 | return -1; | |
1d14ffa9 | 260 | } |
2762955f | 261 | return 0; |
1d14ffa9 FB |
262 | } |
263 | ||
1d14ffa9 FB |
264 | #include "dsound_template.h" |
265 | #define DSBTYPE_IN | |
266 | #include "dsound_template.h" | |
267 | #undef DSBTYPE_IN | |
268 | ||
191e1f0a KZ |
269 | static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp, |
270 | dsound *s) | |
1d14ffa9 FB |
271 | { |
272 | HRESULT hr; | |
1d14ffa9 | 273 | |
2762955f KZ |
274 | hr = IDirectSoundBuffer_GetStatus (dsb, statusp); |
275 | if (FAILED (hr)) { | |
276 | dsound_logerr (hr, "Could not get playback buffer status\n"); | |
277 | return -1; | |
278 | } | |
1d14ffa9 | 279 | |
2762955f KZ |
280 | if (*statusp & DSERR_BUFFERLOST) { |
281 | dsound_restore_out(dsb, s); | |
282 | return -1; | |
1d14ffa9 FB |
283 | } |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb, | |
289 | DWORD *statusp) | |
290 | { | |
291 | HRESULT hr; | |
292 | ||
293 | hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp); | |
294 | if (FAILED (hr)) { | |
c0fe3827 | 295 | dsound_logerr (hr, "Could not get capture buffer status\n"); |
1d14ffa9 FB |
296 | return -1; |
297 | } | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
191e1f0a KZ |
302 | static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb, |
303 | dsound *s) | |
1d14ffa9 FB |
304 | { |
305 | int err; | |
306 | LPVOID p1, p2; | |
307 | DWORD blen1, blen2, len1, len2; | |
308 | ||
309 | err = dsound_lock_out ( | |
310 | dsb, | |
311 | &hw->info, | |
312 | 0, | |
7fa9754a | 313 | hw->size_emul, |
1d14ffa9 FB |
314 | &p1, &p2, |
315 | &blen1, &blen2, | |
191e1f0a KZ |
316 | 1, |
317 | s | |
1d14ffa9 FB |
318 | ); |
319 | if (err) { | |
320 | return; | |
321 | } | |
322 | ||
323 | len1 = blen1 >> hw->info.shift; | |
324 | len2 = blen2 >> hw->info.shift; | |
325 | ||
326 | #ifdef DEBUG_DSOUND | |
327 | dolog ("clear %p,%ld,%ld %p,%ld,%ld\n", | |
328 | p1, blen1, len1, | |
329 | p2, blen2, len2); | |
330 | #endif | |
331 | ||
332 | if (p1 && len1) { | |
333 | audio_pcm_info_clear_buf (&hw->info, p1, len1); | |
334 | } | |
335 | ||
336 | if (p2 && len2) { | |
337 | audio_pcm_info_clear_buf (&hw->info, p2, len2); | |
338 | } | |
339 | ||
340 | dsound_unlock_out (dsb, p1, p2, blen1, blen2); | |
341 | } | |
342 | ||
1d14ffa9 FB |
343 | static int dsound_open (dsound *s) |
344 | { | |
1d14ffa9 | 345 | HRESULT hr; |
1d14ffa9 FB |
346 | HWND hwnd; |
347 | ||
348 | hwnd = GetForegroundWindow (); | |
349 | hr = IDirectSound_SetCooperativeLevel ( | |
350 | s->dsound, | |
351 | hwnd, | |
352 | DSSCL_PRIORITY | |
353 | ); | |
354 | ||
355 | if (FAILED (hr)) { | |
c0fe3827 | 356 | dsound_logerr (hr, "Could not set cooperative level for window %p\n", |
1d14ffa9 FB |
357 | hwnd); |
358 | return -1; | |
359 | } | |
360 | ||
1d14ffa9 | 361 | return 0; |
1d14ffa9 FB |
362 | } |
363 | ||
364 | static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...) | |
365 | { | |
366 | HRESULT hr; | |
367 | DWORD status; | |
368 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; | |
369 | LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; | |
191e1f0a | 370 | dsound *s = ds->s; |
1d14ffa9 FB |
371 | |
372 | if (!dsb) { | |
373 | dolog ("Attempt to control voice without a buffer\n"); | |
374 | return 0; | |
375 | } | |
376 | ||
377 | switch (cmd) { | |
378 | case VOICE_ENABLE: | |
191e1f0a | 379 | if (dsound_get_status_out (dsb, &status, s)) { |
1d14ffa9 FB |
380 | return -1; |
381 | } | |
382 | ||
383 | if (status & DSBSTATUS_PLAYING) { | |
c0fe3827 | 384 | dolog ("warning: Voice is already playing\n"); |
1d14ffa9 FB |
385 | return 0; |
386 | } | |
387 | ||
191e1f0a | 388 | dsound_clear_sample (hw, dsb, s); |
1d14ffa9 FB |
389 | |
390 | hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING); | |
391 | if (FAILED (hr)) { | |
c0fe3827 | 392 | dsound_logerr (hr, "Could not start playing buffer\n"); |
1d14ffa9 FB |
393 | return -1; |
394 | } | |
395 | break; | |
396 | ||
397 | case VOICE_DISABLE: | |
191e1f0a | 398 | if (dsound_get_status_out (dsb, &status, s)) { |
1d14ffa9 FB |
399 | return -1; |
400 | } | |
401 | ||
402 | if (status & DSBSTATUS_PLAYING) { | |
403 | hr = IDirectSoundBuffer_Stop (dsb); | |
404 | if (FAILED (hr)) { | |
c0fe3827 | 405 | dsound_logerr (hr, "Could not stop playing buffer\n"); |
1d14ffa9 FB |
406 | return -1; |
407 | } | |
408 | } | |
409 | else { | |
c0fe3827 | 410 | dolog ("warning: Voice is not playing\n"); |
1d14ffa9 FB |
411 | } |
412 | break; | |
413 | } | |
414 | return 0; | |
415 | } | |
416 | ||
7fa9754a | 417 | static void *dsound_get_buffer_out(HWVoiceOut *hw, size_t *size) |
1d14ffa9 | 418 | { |
1d14ffa9 FB |
419 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; |
420 | LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; | |
7fa9754a KZ |
421 | HRESULT hr; |
422 | DWORD ppos, act_size; | |
423 | size_t req_size; | |
424 | int err; | |
425 | void *ret; | |
1d14ffa9 | 426 | |
7fa9754a KZ |
427 | hr = IDirectSoundBuffer_GetCurrentPosition(dsb, &ppos, NULL); |
428 | if (FAILED(hr)) { | |
429 | dsound_logerr(hr, "Could not get playback buffer position\n"); | |
430 | *size = 0; | |
431 | return NULL; | |
1d14ffa9 | 432 | } |
1d14ffa9 | 433 | |
7fa9754a KZ |
434 | req_size = audio_ring_dist(ppos, hw->pos_emul, hw->size_emul); |
435 | req_size = MIN(req_size, hw->size_emul - hw->pos_emul); | |
1d14ffa9 | 436 | |
7fa9754a KZ |
437 | err = dsound_lock_out(dsb, &hw->info, hw->pos_emul, req_size, &ret, NULL, |
438 | &act_size, NULL, false, ds->s); | |
439 | if (err) { | |
440 | dolog("Failed to lock buffer\n"); | |
441 | *size = 0; | |
442 | return NULL; | |
1d14ffa9 FB |
443 | } |
444 | ||
7fa9754a KZ |
445 | *size = act_size; |
446 | return ret; | |
447 | } | |
1d14ffa9 | 448 | |
7fa9754a KZ |
449 | static size_t dsound_put_buffer_out(HWVoiceOut *hw, void *buf, size_t len) |
450 | { | |
451 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; | |
452 | LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; | |
453 | int err = dsound_unlock_out(dsb, buf, NULL, len, 0); | |
1d14ffa9 | 454 | |
1d14ffa9 | 455 | if (err) { |
7fa9754a | 456 | dolog("Failed to unlock buffer!!\n"); |
1d14ffa9 FB |
457 | return 0; |
458 | } | |
7fa9754a | 459 | hw->pos_emul = (hw->pos_emul + len) % hw->size_emul; |
1d14ffa9 | 460 | |
7fa9754a | 461 | return len; |
1d14ffa9 FB |
462 | } |
463 | ||
464 | static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...) | |
465 | { | |
466 | HRESULT hr; | |
467 | DWORD status; | |
468 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; | |
469 | LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; | |
470 | ||
471 | if (!dscb) { | |
472 | dolog ("Attempt to control capture voice without a buffer\n"); | |
473 | return -1; | |
474 | } | |
475 | ||
476 | switch (cmd) { | |
477 | case VOICE_ENABLE: | |
478 | if (dsound_get_status_in (dscb, &status)) { | |
479 | return -1; | |
480 | } | |
481 | ||
482 | if (status & DSCBSTATUS_CAPTURING) { | |
c0fe3827 | 483 | dolog ("warning: Voice is already capturing\n"); |
1d14ffa9 FB |
484 | return 0; |
485 | } | |
486 | ||
487 | /* clear ?? */ | |
488 | ||
489 | hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING); | |
490 | if (FAILED (hr)) { | |
c0fe3827 | 491 | dsound_logerr (hr, "Could not start capturing\n"); |
1d14ffa9 FB |
492 | return -1; |
493 | } | |
494 | break; | |
495 | ||
496 | case VOICE_DISABLE: | |
497 | if (dsound_get_status_in (dscb, &status)) { | |
498 | return -1; | |
499 | } | |
500 | ||
501 | if (status & DSCBSTATUS_CAPTURING) { | |
502 | hr = IDirectSoundCaptureBuffer_Stop (dscb); | |
503 | if (FAILED (hr)) { | |
c0fe3827 | 504 | dsound_logerr (hr, "Could not stop capturing\n"); |
1d14ffa9 FB |
505 | return -1; |
506 | } | |
507 | } | |
508 | else { | |
c0fe3827 | 509 | dolog ("warning: Voice is not capturing\n"); |
1d14ffa9 FB |
510 | } |
511 | break; | |
512 | } | |
513 | return 0; | |
514 | } | |
515 | ||
7fa9754a | 516 | static void *dsound_get_buffer_in(HWVoiceIn *hw, size_t *size) |
1d14ffa9 | 517 | { |
1d14ffa9 FB |
518 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; |
519 | LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; | |
7fa9754a KZ |
520 | HRESULT hr; |
521 | DWORD cpos, act_size; | |
522 | size_t req_size; | |
523 | int err; | |
524 | void *ret; | |
1d14ffa9 | 525 | |
7fa9754a KZ |
526 | hr = IDirectSoundCaptureBuffer_GetCurrentPosition(dscb, &cpos, NULL); |
527 | if (FAILED(hr)) { | |
528 | dsound_logerr(hr, "Could not get capture buffer position\n"); | |
529 | *size = 0; | |
530 | return NULL; | |
1d14ffa9 | 531 | } |
1d14ffa9 | 532 | |
7fa9754a KZ |
533 | req_size = audio_ring_dist(cpos, hw->pos_emul, hw->size_emul); |
534 | req_size = MIN(req_size, hw->size_emul - hw->pos_emul); | |
1d14ffa9 | 535 | |
7fa9754a KZ |
536 | err = dsound_lock_in(dscb, &hw->info, hw->pos_emul, req_size, &ret, NULL, |
537 | &act_size, NULL, false, ds->s); | |
1d14ffa9 | 538 | if (err) { |
7fa9754a KZ |
539 | dolog("Failed to lock buffer\n"); |
540 | *size = 0; | |
541 | return NULL; | |
1d14ffa9 FB |
542 | } |
543 | ||
7fa9754a KZ |
544 | *size = act_size; |
545 | return ret; | |
546 | } | |
1d14ffa9 | 547 | |
7fa9754a KZ |
548 | static void dsound_put_buffer_in(HWVoiceIn *hw, void *buf, size_t len) |
549 | { | |
550 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; | |
551 | LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; | |
552 | int err = dsound_unlock_in(dscb, buf, NULL, len, 0); | |
1d14ffa9 | 553 | |
7fa9754a KZ |
554 | if (err) { |
555 | dolog("Failed to unlock buffer!!\n"); | |
556 | return; | |
1d14ffa9 | 557 | } |
7fa9754a | 558 | hw->pos_emul = (hw->pos_emul + len) % hw->size_emul; |
1d14ffa9 FB |
559 | } |
560 | ||
561 | static void dsound_audio_fini (void *opaque) | |
562 | { | |
563 | HRESULT hr; | |
564 | dsound *s = opaque; | |
565 | ||
566 | if (!s->dsound) { | |
191e1f0a | 567 | g_free(s); |
1d14ffa9 FB |
568 | return; |
569 | } | |
570 | ||
571 | hr = IDirectSound_Release (s->dsound); | |
572 | if (FAILED (hr)) { | |
c0fe3827 | 573 | dsound_logerr (hr, "Could not release DirectSound\n"); |
1d14ffa9 FB |
574 | } |
575 | s->dsound = NULL; | |
576 | ||
577 | if (!s->dsound_capture) { | |
191e1f0a | 578 | g_free(s); |
1d14ffa9 FB |
579 | return; |
580 | } | |
581 | ||
582 | hr = IDirectSoundCapture_Release (s->dsound_capture); | |
583 | if (FAILED (hr)) { | |
c0fe3827 | 584 | dsound_logerr (hr, "Could not release DirectSoundCapture\n"); |
1d14ffa9 FB |
585 | } |
586 | s->dsound_capture = NULL; | |
191e1f0a KZ |
587 | |
588 | g_free(s); | |
1d14ffa9 FB |
589 | } |
590 | ||
71830221 | 591 | static void *dsound_audio_init(Audiodev *dev) |
1d14ffa9 FB |
592 | { |
593 | int err; | |
594 | HRESULT hr; | |
191e1f0a | 595 | dsound *s = g_malloc0(sizeof(dsound)); |
4a3b8b34 KZ |
596 | AudiodevDsoundOptions *dso; |
597 | ||
598 | assert(dev->driver == AUDIODEV_DRIVER_DSOUND); | |
599 | s->dev = dev; | |
600 | dso = &dev->u.dsound; | |
601 | ||
602 | if (!dso->has_latency) { | |
603 | dso->has_latency = true; | |
604 | dso->latency = 10000; /* 10 ms */ | |
605 | } | |
1d14ffa9 FB |
606 | |
607 | hr = CoInitialize (NULL); | |
608 | if (FAILED (hr)) { | |
c0fe3827 | 609 | dsound_logerr (hr, "Could not initialize COM\n"); |
191e1f0a | 610 | g_free(s); |
1d14ffa9 FB |
611 | return NULL; |
612 | } | |
613 | ||
614 | hr = CoCreateInstance ( | |
615 | &CLSID_DirectSound, | |
616 | NULL, | |
617 | CLSCTX_ALL, | |
618 | &IID_IDirectSound, | |
619 | (void **) &s->dsound | |
620 | ); | |
621 | if (FAILED (hr)) { | |
c0fe3827 | 622 | dsound_logerr (hr, "Could not create DirectSound instance\n"); |
191e1f0a | 623 | g_free(s); |
1d14ffa9 FB |
624 | return NULL; |
625 | } | |
626 | ||
627 | hr = IDirectSound_Initialize (s->dsound, NULL); | |
628 | if (FAILED (hr)) { | |
c0fe3827 | 629 | dsound_logerr (hr, "Could not initialize DirectSound\n"); |
8ead62cf FB |
630 | |
631 | hr = IDirectSound_Release (s->dsound); | |
632 | if (FAILED (hr)) { | |
633 | dsound_logerr (hr, "Could not release DirectSound\n"); | |
634 | } | |
191e1f0a | 635 | g_free(s); |
1d14ffa9 FB |
636 | return NULL; |
637 | } | |
638 | ||
639 | hr = CoCreateInstance ( | |
640 | &CLSID_DirectSoundCapture, | |
641 | NULL, | |
642 | CLSCTX_ALL, | |
643 | &IID_IDirectSoundCapture, | |
644 | (void **) &s->dsound_capture | |
645 | ); | |
646 | if (FAILED (hr)) { | |
c0fe3827 | 647 | dsound_logerr (hr, "Could not create DirectSoundCapture instance\n"); |
1d14ffa9 FB |
648 | } |
649 | else { | |
650 | hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL); | |
651 | if (FAILED (hr)) { | |
c0fe3827 | 652 | dsound_logerr (hr, "Could not initialize DirectSoundCapture\n"); |
1d14ffa9 FB |
653 | |
654 | hr = IDirectSoundCapture_Release (s->dsound_capture); | |
655 | if (FAILED (hr)) { | |
c0fe3827 | 656 | dsound_logerr (hr, "Could not release DirectSoundCapture\n"); |
1d14ffa9 FB |
657 | } |
658 | s->dsound_capture = NULL; | |
659 | } | |
660 | } | |
661 | ||
662 | err = dsound_open (s); | |
663 | if (err) { | |
664 | dsound_audio_fini (s); | |
665 | return NULL; | |
666 | } | |
667 | ||
668 | return s; | |
669 | } | |
670 | ||
35f4b58c | 671 | static struct audio_pcm_ops dsound_pcm_ops = { |
1dd3e4d1 JQ |
672 | .init_out = dsound_init_out, |
673 | .fini_out = dsound_fini_out, | |
7fa9754a KZ |
674 | .write = audio_generic_write, |
675 | .get_buffer_out = dsound_get_buffer_out, | |
676 | .put_buffer_out = dsound_put_buffer_out, | |
1dd3e4d1 JQ |
677 | .ctl_out = dsound_ctl_out, |
678 | ||
679 | .init_in = dsound_init_in, | |
680 | .fini_in = dsound_fini_in, | |
7fa9754a KZ |
681 | .read = audio_generic_read, |
682 | .get_buffer_in = dsound_get_buffer_in, | |
683 | .put_buffer_in = dsound_put_buffer_in, | |
1dd3e4d1 | 684 | .ctl_in = dsound_ctl_in |
1d14ffa9 FB |
685 | }; |
686 | ||
d3893a39 | 687 | static struct audio_driver dsound_audio_driver = { |
bee37f32 JQ |
688 | .name = "dsound", |
689 | .descr = "DirectSound http://wikipedia.org/wiki/DirectSound", | |
bee37f32 JQ |
690 | .init = dsound_audio_init, |
691 | .fini = dsound_audio_fini, | |
692 | .pcm_ops = &dsound_pcm_ops, | |
693 | .can_be_default = 1, | |
694 | .max_voices_out = INT_MAX, | |
695 | .max_voices_in = 1, | |
696 | .voice_size_out = sizeof (DSoundVoiceOut), | |
15c875a3 | 697 | .voice_size_in = sizeof (DSoundVoiceIn) |
1d14ffa9 | 698 | }; |
d3893a39 GH |
699 | |
700 | static void register_audio_dsound(void) | |
701 | { | |
702 | audio_driver_register(&dsound_audio_driver); | |
703 | } | |
704 | type_init(register_audio_dsound); |