]>
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; | |
fb35c2ce | 56 | bool first_time; |
191e1f0a | 57 | dsound *s; |
1d14ffa9 FB |
58 | } DSoundVoiceOut; |
59 | ||
60 | typedef struct { | |
61 | HWVoiceIn hw; | |
1d14ffa9 | 62 | LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer; |
fb35c2ce | 63 | bool first_time; |
191e1f0a | 64 | dsound *s; |
1d14ffa9 FB |
65 | } DSoundVoiceIn; |
66 | ||
67 | static 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: | |
c60840c7 ZH |
92 | str = "The request failed because resources, " |
93 | "such as a priority level, were already in use " | |
94 | "by another caller"; | |
1d14ffa9 FB |
95 | 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: | |
c60840c7 ZH |
109 | str = "The GUID specified in an audiopath file " |
110 | "does not match a valid mix-in buffer"; | |
1d14ffa9 FB |
111 | 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: | |
c60840c7 ZH |
120 | str = "The buffer size is not great enough to " |
121 | "enable effects processing"; | |
1d14ffa9 FB |
122 | break; |
123 | #endif | |
124 | #ifdef DSERR_CONTROLUNAVAIL | |
125 | case DSERR_CONTROLUNAVAIL: | |
c60840c7 ZH |
126 | 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"; | |
1d14ffa9 FB |
130 | break; |
131 | #endif | |
132 | #ifdef DSERR_DS8_REQUIRED | |
133 | case DSERR_DS8_REQUIRED: | |
c60840c7 ZH |
134 | str = "A DirectSound object of class CLSID_DirectSound8 or later " |
135 | "is required for the requested functionality. " | |
136 | "For more information, see IDirectSound8 Interface"; | |
1d14ffa9 FB |
137 | break; |
138 | #endif | |
139 | #ifdef DSERR_FXUNAVAILABLE | |
140 | case DSERR_FXUNAVAILABLE: | |
c60840c7 ZH |
141 | 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"; | |
1d14ffa9 FB |
145 | break; |
146 | #endif | |
147 | #ifdef DSERR_GENERIC | |
3c8de96c | 148 | case DSERR_GENERIC: |
1d14ffa9 FB |
149 | 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: | |
c60840c7 ZH |
169 | str = "No sound driver is available for use, " |
170 | "or the given GUID is not a valid DirectSound device ID"; | |
1d14ffa9 FB |
171 | 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: | |
c60840c7 ZH |
185 | str = "Another application has a higher priority level, " |
186 | "preventing this call from succeeding"; | |
1d14ffa9 FB |
187 | break; |
188 | #endif | |
189 | #ifdef DSERR_OUTOFMEMORY | |
190 | case DSERR_OUTOFMEMORY: | |
c60840c7 ZH |
191 | str = "The DirectSound subsystem could not allocate " |
192 | "sufficient memory to complete the caller's request"; | |
1d14ffa9 FB |
193 | 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: | |
c60840c7 ZH |
207 | str = "The Initialize method has not been called " |
208 | "or has not been called successfully " | |
209 | "before other methods were called"; | |
1d14ffa9 FB |
210 | 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: | |
dea7d84f | 218 | AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT 0x%lx)\n", hr); |
1d14ffa9 FB |
219 | return; |
220 | } | |
221 | ||
222 | AUD_log (AUDIO_CAP, "Reason: %s\n", str); | |
223 | } | |
224 | ||
225 | static void GCC_FMT_ATTR (2, 3) dsound_logerr ( | |
226 | 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 | ||
240 | static void GCC_FMT_ATTR (3, 4) dsound_logerr2 ( | |
241 | HRESULT hr, | |
242 | const char *typ, | |
243 | const char *fmt, | |
244 | ... | |
245 | ) | |
246 | { | |
247 | va_list ap; | |
248 | ||
c0fe3827 | 249 | AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); |
1d14ffa9 FB |
250 | va_start (ap, fmt); |
251 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
252 | va_end (ap); | |
253 | ||
254 | dsound_log_hresult (hr); | |
255 | } | |
256 | ||
1d14ffa9 FB |
257 | #ifdef DEBUG_DSOUND |
258 | static 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 | ||
191e1f0a | 270 | static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s) |
1d14ffa9 FB |
271 | { |
272 | HRESULT hr; | |
1d14ffa9 | 273 | |
2762955f | 274 | hr = IDirectSoundBuffer_Restore (dsb); |
1d14ffa9 | 275 | |
2762955f KZ |
276 | if (hr != DS_OK) { |
277 | dsound_logerr (hr, "Could not restore playback buffer\n"); | |
278 | return -1; | |
1d14ffa9 | 279 | } |
2762955f | 280 | return 0; |
1d14ffa9 FB |
281 | } |
282 | ||
1d14ffa9 FB |
283 | #include "dsound_template.h" |
284 | #define DSBTYPE_IN | |
285 | #include "dsound_template.h" | |
286 | #undef DSBTYPE_IN | |
287 | ||
191e1f0a KZ |
288 | static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp, |
289 | dsound *s) | |
1d14ffa9 FB |
290 | { |
291 | HRESULT hr; | |
1d14ffa9 | 292 | |
2762955f KZ |
293 | hr = IDirectSoundBuffer_GetStatus (dsb, statusp); |
294 | if (FAILED (hr)) { | |
295 | dsound_logerr (hr, "Could not get playback buffer status\n"); | |
296 | return -1; | |
297 | } | |
1d14ffa9 | 298 | |
4ba664cb | 299 | if (*statusp & DSBSTATUS_BUFFERLOST) { |
2762955f KZ |
300 | dsound_restore_out(dsb, s); |
301 | return -1; | |
1d14ffa9 FB |
302 | } |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
307 | static 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)) { | |
c0fe3827 | 314 | dsound_logerr (hr, "Could not get capture buffer status\n"); |
1d14ffa9 FB |
315 | return -1; |
316 | } | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
191e1f0a KZ |
321 | static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb, |
322 | dsound *s) | |
1d14ffa9 FB |
323 | { |
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, | |
7fa9754a | 332 | hw->size_emul, |
1d14ffa9 FB |
333 | &p1, &p2, |
334 | &blen1, &blen2, | |
191e1f0a KZ |
335 | 1, |
336 | s | |
1d14ffa9 FB |
337 | ); |
338 | if (err) { | |
339 | return; | |
340 | } | |
341 | ||
2b9cce8c KZ |
342 | len1 = blen1 / hw->info.bytes_per_frame; |
343 | len2 = blen2 / hw->info.bytes_per_frame; | |
1d14ffa9 FB |
344 | |
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 | ||
3c18e431 | 362 | static int dsound_set_cooperative_level(dsound *s) |
1d14ffa9 | 363 | { |
1d14ffa9 | 364 | HRESULT hr; |
1d14ffa9 FB |
365 | HWND hwnd; |
366 | ||
401dcf05 | 367 | hwnd = GetDesktopWindow(); |
1d14ffa9 FB |
368 | hr = IDirectSound_SetCooperativeLevel ( |
369 | s->dsound, | |
370 | hwnd, | |
371 | DSSCL_PRIORITY | |
372 | ); | |
373 | ||
374 | if (FAILED (hr)) { | |
c0fe3827 | 375 | dsound_logerr (hr, "Could not set cooperative level for window %p\n", |
1d14ffa9 FB |
376 | hwnd); |
377 | return -1; | |
378 | } | |
379 | ||
1d14ffa9 | 380 | return 0; |
1d14ffa9 FB |
381 | } |
382 | ||
571a8c52 | 383 | static void dsound_enable_out(HWVoiceOut *hw, bool enable) |
1d14ffa9 FB |
384 | { |
385 | HRESULT hr; | |
386 | DWORD status; | |
387 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; | |
388 | LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; | |
191e1f0a | 389 | dsound *s = ds->s; |
1d14ffa9 FB |
390 | |
391 | if (!dsb) { | |
392 | dolog ("Attempt to control voice without a buffer\n"); | |
571a8c52 | 393 | return; |
1d14ffa9 FB |
394 | } |
395 | ||
571a8c52 | 396 | if (enable) { |
191e1f0a | 397 | if (dsound_get_status_out (dsb, &status, s)) { |
571a8c52 | 398 | return; |
1d14ffa9 FB |
399 | } |
400 | ||
401 | if (status & DSBSTATUS_PLAYING) { | |
c0fe3827 | 402 | dolog ("warning: Voice is already playing\n"); |
571a8c52 | 403 | return; |
1d14ffa9 FB |
404 | } |
405 | ||
191e1f0a | 406 | dsound_clear_sample (hw, dsb, s); |
1d14ffa9 FB |
407 | |
408 | hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING); | |
409 | if (FAILED (hr)) { | |
c0fe3827 | 410 | dsound_logerr (hr, "Could not start playing buffer\n"); |
571a8c52 | 411 | return; |
1d14ffa9 | 412 | } |
571a8c52 | 413 | } else { |
191e1f0a | 414 | if (dsound_get_status_out (dsb, &status, s)) { |
571a8c52 | 415 | return; |
1d14ffa9 FB |
416 | } |
417 | ||
418 | if (status & DSBSTATUS_PLAYING) { | |
419 | hr = IDirectSoundBuffer_Stop (dsb); | |
420 | if (FAILED (hr)) { | |
c0fe3827 | 421 | dsound_logerr (hr, "Could not stop playing buffer\n"); |
571a8c52 | 422 | return; |
1d14ffa9 | 423 | } |
6c6886bd | 424 | } else { |
c0fe3827 | 425 | dolog ("warning: Voice is not playing\n"); |
1d14ffa9 | 426 | } |
1d14ffa9 | 427 | } |
1d14ffa9 FB |
428 | } |
429 | ||
7fa9754a | 430 | static void *dsound_get_buffer_out(HWVoiceOut *hw, size_t *size) |
1d14ffa9 | 431 | { |
1d14ffa9 FB |
432 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; |
433 | LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; | |
7fa9754a | 434 | HRESULT hr; |
fb35c2ce | 435 | DWORD ppos, wpos, act_size; |
7fa9754a KZ |
436 | size_t req_size; |
437 | int err; | |
438 | void *ret; | |
1d14ffa9 | 439 | |
fb35c2ce KZ |
440 | hr = IDirectSoundBuffer_GetCurrentPosition( |
441 | dsb, &ppos, ds->first_time ? &wpos : NULL); | |
7fa9754a KZ |
442 | if (FAILED(hr)) { |
443 | dsound_logerr(hr, "Could not get playback buffer position\n"); | |
444 | *size = 0; | |
445 | return NULL; | |
1d14ffa9 | 446 | } |
1d14ffa9 | 447 | |
fb35c2ce KZ |
448 | if (ds->first_time) { |
449 | hw->pos_emul = wpos; | |
450 | ds->first_time = false; | |
451 | } | |
452 | ||
7fa9754a KZ |
453 | req_size = audio_ring_dist(ppos, hw->pos_emul, hw->size_emul); |
454 | req_size = MIN(req_size, hw->size_emul - hw->pos_emul); | |
1d14ffa9 | 455 | |
fb35c2ce KZ |
456 | if (req_size == 0) { |
457 | *size = 0; | |
458 | return NULL; | |
459 | } | |
460 | ||
7fa9754a KZ |
461 | err = dsound_lock_out(dsb, &hw->info, hw->pos_emul, req_size, &ret, NULL, |
462 | &act_size, NULL, false, ds->s); | |
463 | if (err) { | |
464 | dolog("Failed to lock buffer\n"); | |
465 | *size = 0; | |
466 | return NULL; | |
1d14ffa9 FB |
467 | } |
468 | ||
7fa9754a KZ |
469 | *size = act_size; |
470 | return ret; | |
471 | } | |
1d14ffa9 | 472 | |
7fa9754a KZ |
473 | static size_t dsound_put_buffer_out(HWVoiceOut *hw, void *buf, size_t len) |
474 | { | |
475 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; | |
476 | LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; | |
477 | int err = dsound_unlock_out(dsb, buf, NULL, len, 0); | |
1d14ffa9 | 478 | |
1d14ffa9 | 479 | if (err) { |
7fa9754a | 480 | dolog("Failed to unlock buffer!!\n"); |
1d14ffa9 FB |
481 | return 0; |
482 | } | |
7fa9754a | 483 | hw->pos_emul = (hw->pos_emul + len) % hw->size_emul; |
1d14ffa9 | 484 | |
7fa9754a | 485 | return len; |
1d14ffa9 FB |
486 | } |
487 | ||
571a8c52 | 488 | static void dsound_enable_in(HWVoiceIn *hw, bool enable) |
1d14ffa9 FB |
489 | { |
490 | HRESULT hr; | |
491 | DWORD status; | |
492 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; | |
493 | LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; | |
494 | ||
495 | if (!dscb) { | |
496 | dolog ("Attempt to control capture voice without a buffer\n"); | |
571a8c52 | 497 | return; |
1d14ffa9 FB |
498 | } |
499 | ||
571a8c52 | 500 | if (enable) { |
1d14ffa9 | 501 | if (dsound_get_status_in (dscb, &status)) { |
571a8c52 | 502 | return; |
1d14ffa9 FB |
503 | } |
504 | ||
505 | if (status & DSCBSTATUS_CAPTURING) { | |
c0fe3827 | 506 | dolog ("warning: Voice is already capturing\n"); |
571a8c52 | 507 | return; |
1d14ffa9 FB |
508 | } |
509 | ||
510 | /* clear ?? */ | |
511 | ||
512 | hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING); | |
513 | if (FAILED (hr)) { | |
c0fe3827 | 514 | dsound_logerr (hr, "Could not start capturing\n"); |
571a8c52 | 515 | return; |
1d14ffa9 | 516 | } |
571a8c52 | 517 | } else { |
1d14ffa9 | 518 | if (dsound_get_status_in (dscb, &status)) { |
571a8c52 | 519 | return; |
1d14ffa9 FB |
520 | } |
521 | ||
522 | if (status & DSCBSTATUS_CAPTURING) { | |
523 | hr = IDirectSoundCaptureBuffer_Stop (dscb); | |
524 | if (FAILED (hr)) { | |
c0fe3827 | 525 | dsound_logerr (hr, "Could not stop capturing\n"); |
571a8c52 | 526 | return; |
1d14ffa9 | 527 | } |
6c6886bd | 528 | } else { |
c0fe3827 | 529 | dolog ("warning: Voice is not capturing\n"); |
1d14ffa9 | 530 | } |
1d14ffa9 | 531 | } |
1d14ffa9 FB |
532 | } |
533 | ||
7fa9754a | 534 | static void *dsound_get_buffer_in(HWVoiceIn *hw, size_t *size) |
1d14ffa9 | 535 | { |
1d14ffa9 FB |
536 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; |
537 | LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; | |
7fa9754a | 538 | HRESULT hr; |
fb35c2ce | 539 | DWORD cpos, rpos, act_size; |
7fa9754a KZ |
540 | size_t req_size; |
541 | int err; | |
542 | void *ret; | |
1d14ffa9 | 543 | |
fb35c2ce KZ |
544 | hr = IDirectSoundCaptureBuffer_GetCurrentPosition( |
545 | dscb, &cpos, ds->first_time ? &rpos : NULL); | |
7fa9754a KZ |
546 | if (FAILED(hr)) { |
547 | dsound_logerr(hr, "Could not get capture buffer position\n"); | |
548 | *size = 0; | |
549 | return NULL; | |
1d14ffa9 | 550 | } |
1d14ffa9 | 551 | |
fb35c2ce KZ |
552 | if (ds->first_time) { |
553 | hw->pos_emul = rpos; | |
554 | ds->first_time = false; | |
555 | } | |
556 | ||
7fa9754a | 557 | req_size = audio_ring_dist(cpos, hw->pos_emul, hw->size_emul); |
8d1439b6 | 558 | req_size = MIN(*size, MIN(req_size, hw->size_emul - hw->pos_emul)); |
1d14ffa9 | 559 | |
17470298 VR |
560 | if (req_size == 0) { |
561 | *size = 0; | |
562 | return NULL; | |
563 | } | |
564 | ||
7fa9754a KZ |
565 | err = dsound_lock_in(dscb, &hw->info, hw->pos_emul, req_size, &ret, NULL, |
566 | &act_size, NULL, false, ds->s); | |
1d14ffa9 | 567 | if (err) { |
7fa9754a KZ |
568 | dolog("Failed to lock buffer\n"); |
569 | *size = 0; | |
570 | return NULL; | |
1d14ffa9 FB |
571 | } |
572 | ||
7fa9754a KZ |
573 | *size = act_size; |
574 | return ret; | |
575 | } | |
1d14ffa9 | 576 | |
7fa9754a KZ |
577 | static void dsound_put_buffer_in(HWVoiceIn *hw, void *buf, size_t len) |
578 | { | |
579 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; | |
580 | LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; | |
581 | int err = dsound_unlock_in(dscb, buf, NULL, len, 0); | |
1d14ffa9 | 582 | |
7fa9754a KZ |
583 | if (err) { |
584 | dolog("Failed to unlock buffer!!\n"); | |
585 | return; | |
1d14ffa9 | 586 | } |
7fa9754a | 587 | hw->pos_emul = (hw->pos_emul + len) % hw->size_emul; |
1d14ffa9 FB |
588 | } |
589 | ||
590 | static void dsound_audio_fini (void *opaque) | |
591 | { | |
592 | HRESULT hr; | |
593 | dsound *s = opaque; | |
594 | ||
595 | if (!s->dsound) { | |
191e1f0a | 596 | g_free(s); |
1d14ffa9 FB |
597 | return; |
598 | } | |
599 | ||
600 | hr = IDirectSound_Release (s->dsound); | |
601 | if (FAILED (hr)) { | |
c0fe3827 | 602 | dsound_logerr (hr, "Could not release DirectSound\n"); |
1d14ffa9 FB |
603 | } |
604 | s->dsound = NULL; | |
605 | ||
606 | if (!s->dsound_capture) { | |
191e1f0a | 607 | g_free(s); |
1d14ffa9 FB |
608 | return; |
609 | } | |
610 | ||
611 | hr = IDirectSoundCapture_Release (s->dsound_capture); | |
612 | if (FAILED (hr)) { | |
c0fe3827 | 613 | dsound_logerr (hr, "Could not release DirectSoundCapture\n"); |
1d14ffa9 FB |
614 | } |
615 | s->dsound_capture = NULL; | |
191e1f0a KZ |
616 | |
617 | g_free(s); | |
1d14ffa9 FB |
618 | } |
619 | ||
71830221 | 620 | static void *dsound_audio_init(Audiodev *dev) |
1d14ffa9 FB |
621 | { |
622 | int err; | |
623 | HRESULT hr; | |
191e1f0a | 624 | dsound *s = g_malloc0(sizeof(dsound)); |
4a3b8b34 KZ |
625 | AudiodevDsoundOptions *dso; |
626 | ||
627 | assert(dev->driver == AUDIODEV_DRIVER_DSOUND); | |
628 | s->dev = dev; | |
629 | dso = &dev->u.dsound; | |
630 | ||
631 | if (!dso->has_latency) { | |
632 | dso->has_latency = true; | |
633 | dso->latency = 10000; /* 10 ms */ | |
634 | } | |
1d14ffa9 FB |
635 | |
636 | hr = CoInitialize (NULL); | |
637 | if (FAILED (hr)) { | |
c0fe3827 | 638 | dsound_logerr (hr, "Could not initialize COM\n"); |
191e1f0a | 639 | g_free(s); |
1d14ffa9 FB |
640 | return NULL; |
641 | } | |
642 | ||
643 | hr = CoCreateInstance ( | |
644 | &CLSID_DirectSound, | |
645 | NULL, | |
646 | CLSCTX_ALL, | |
647 | &IID_IDirectSound, | |
648 | (void **) &s->dsound | |
649 | ); | |
650 | if (FAILED (hr)) { | |
c0fe3827 | 651 | dsound_logerr (hr, "Could not create DirectSound instance\n"); |
191e1f0a | 652 | g_free(s); |
1d14ffa9 FB |
653 | return NULL; |
654 | } | |
655 | ||
656 | hr = IDirectSound_Initialize (s->dsound, NULL); | |
657 | if (FAILED (hr)) { | |
c0fe3827 | 658 | dsound_logerr (hr, "Could not initialize DirectSound\n"); |
8ead62cf FB |
659 | |
660 | hr = IDirectSound_Release (s->dsound); | |
661 | if (FAILED (hr)) { | |
662 | dsound_logerr (hr, "Could not release DirectSound\n"); | |
663 | } | |
191e1f0a | 664 | g_free(s); |
1d14ffa9 FB |
665 | return NULL; |
666 | } | |
667 | ||
668 | hr = CoCreateInstance ( | |
669 | &CLSID_DirectSoundCapture, | |
670 | NULL, | |
671 | CLSCTX_ALL, | |
672 | &IID_IDirectSoundCapture, | |
673 | (void **) &s->dsound_capture | |
674 | ); | |
675 | if (FAILED (hr)) { | |
c0fe3827 | 676 | dsound_logerr (hr, "Could not create DirectSoundCapture instance\n"); |
6c6886bd | 677 | } else { |
1d14ffa9 FB |
678 | hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL); |
679 | if (FAILED (hr)) { | |
c0fe3827 | 680 | dsound_logerr (hr, "Could not initialize DirectSoundCapture\n"); |
1d14ffa9 FB |
681 | |
682 | hr = IDirectSoundCapture_Release (s->dsound_capture); | |
683 | if (FAILED (hr)) { | |
c0fe3827 | 684 | dsound_logerr (hr, "Could not release DirectSoundCapture\n"); |
1d14ffa9 FB |
685 | } |
686 | s->dsound_capture = NULL; | |
687 | } | |
688 | } | |
689 | ||
3c18e431 | 690 | err = dsound_set_cooperative_level(s); |
1d14ffa9 FB |
691 | if (err) { |
692 | dsound_audio_fini (s); | |
693 | return NULL; | |
694 | } | |
695 | ||
696 | return s; | |
697 | } | |
698 | ||
35f4b58c | 699 | static struct audio_pcm_ops dsound_pcm_ops = { |
1dd3e4d1 JQ |
700 | .init_out = dsound_init_out, |
701 | .fini_out = dsound_fini_out, | |
7fa9754a KZ |
702 | .write = audio_generic_write, |
703 | .get_buffer_out = dsound_get_buffer_out, | |
704 | .put_buffer_out = dsound_put_buffer_out, | |
571a8c52 | 705 | .enable_out = dsound_enable_out, |
1dd3e4d1 JQ |
706 | |
707 | .init_in = dsound_init_in, | |
708 | .fini_in = dsound_fini_in, | |
7fa9754a KZ |
709 | .read = audio_generic_read, |
710 | .get_buffer_in = dsound_get_buffer_in, | |
711 | .put_buffer_in = dsound_put_buffer_in, | |
571a8c52 | 712 | .enable_in = dsound_enable_in, |
1d14ffa9 FB |
713 | }; |
714 | ||
d3893a39 | 715 | static struct audio_driver dsound_audio_driver = { |
bee37f32 JQ |
716 | .name = "dsound", |
717 | .descr = "DirectSound http://wikipedia.org/wiki/DirectSound", | |
bee37f32 JQ |
718 | .init = dsound_audio_init, |
719 | .fini = dsound_audio_fini, | |
720 | .pcm_ops = &dsound_pcm_ops, | |
721 | .can_be_default = 1, | |
722 | .max_voices_out = INT_MAX, | |
723 | .max_voices_in = 1, | |
724 | .voice_size_out = sizeof (DSoundVoiceOut), | |
15c875a3 | 725 | .voice_size_in = sizeof (DSoundVoiceIn) |
1d14ffa9 | 726 | }; |
d3893a39 GH |
727 | |
728 | static void register_audio_dsound(void) | |
729 | { | |
730 | audio_driver_register(&dsound_audio_driver); | |
731 | } | |
732 | type_init(register_audio_dsound); |