xref: /openbmc/qemu/audio/dsoundaudio.c (revision ebe15582)
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 
29 #include "qemu/osdep.h"
30 #include "audio.h"
31 
32 #define AUDIO_CAP "dsound"
33 #include "audio_int.h"
34 #include "qemu/host-utils.h"
35 #include "qemu/module.h"
36 
37 #include <windows.h>
38 #include <mmsystem.h>
39 #include <objbase.h>
40 #include <dsound.h>
41 
42 #include "audio_win_int.h"
43 
44 /* #define DEBUG_DSOUND */
45 
46 typedef struct {
47     LPDIRECTSOUND dsound;
48     LPDIRECTSOUNDCAPTURE dsound_capture;
49     struct audsettings settings;
50     Audiodev *dev;
51 } dsound;
52 
53 typedef struct {
54     HWVoiceOut hw;
55     LPDIRECTSOUNDBUFFER dsound_buffer;
56     DWORD old_pos;
57     int first_time;
58     dsound *s;
59 #ifdef DEBUG_DSOUND
60     DWORD old_ppos;
61     DWORD played;
62     DWORD mixed;
63 #endif
64 } DSoundVoiceOut;
65 
66 typedef struct {
67     HWVoiceIn hw;
68     int first_time;
69     LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
70     dsound *s;
71 } DSoundVoiceIn;
72 
73 static void dsound_log_hresult (HRESULT hr)
74 {
75     const char *str = "BUG";
76 
77     switch (hr) {
78     case DS_OK:
79         str = "The method succeeded";
80         break;
81 #ifdef DS_NO_VIRTUALIZATION
82     case DS_NO_VIRTUALIZATION:
83         str = "The buffer was created, but another 3D algorithm was substituted";
84         break;
85 #endif
86 #ifdef DS_INCOMPLETE
87     case DS_INCOMPLETE:
88         str = "The method succeeded, but not all the optional effects were obtained";
89         break;
90 #endif
91 #ifdef DSERR_ACCESSDENIED
92     case DSERR_ACCESSDENIED:
93         str = "The request failed because access was denied";
94         break;
95 #endif
96 #ifdef DSERR_ALLOCATED
97     case DSERR_ALLOCATED:
98         str = "The request failed because resources, such as a priority level, were already in use by another caller";
99         break;
100 #endif
101 #ifdef DSERR_ALREADYINITIALIZED
102     case DSERR_ALREADYINITIALIZED:
103         str = "The object is already initialized";
104         break;
105 #endif
106 #ifdef DSERR_BADFORMAT
107     case DSERR_BADFORMAT:
108         str = "The specified wave format is not supported";
109         break;
110 #endif
111 #ifdef DSERR_BADSENDBUFFERGUID
112     case DSERR_BADSENDBUFFERGUID:
113         str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
114         break;
115 #endif
116 #ifdef DSERR_BUFFERLOST
117     case DSERR_BUFFERLOST:
118         str = "The buffer memory has been lost and must be restored";
119         break;
120 #endif
121 #ifdef DSERR_BUFFERTOOSMALL
122     case DSERR_BUFFERTOOSMALL:
123         str = "The buffer size is not great enough to enable effects processing";
124         break;
125 #endif
126 #ifdef DSERR_CONTROLUNAVAIL
127     case DSERR_CONTROLUNAVAIL:
128         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";
129         break;
130 #endif
131 #ifdef DSERR_DS8_REQUIRED
132     case DSERR_DS8_REQUIRED:
133         str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
134         break;
135 #endif
136 #ifdef DSERR_FXUNAVAILABLE
137     case DSERR_FXUNAVAILABLE:
138         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";
139         break;
140 #endif
141 #ifdef DSERR_GENERIC
142     case DSERR_GENERIC :
143         str = "An undetermined error occurred inside the DirectSound subsystem";
144         break;
145 #endif
146 #ifdef DSERR_INVALIDCALL
147     case DSERR_INVALIDCALL:
148         str = "This function is not valid for the current state of this object";
149         break;
150 #endif
151 #ifdef DSERR_INVALIDPARAM
152     case DSERR_INVALIDPARAM:
153         str = "An invalid parameter was passed to the returning function";
154         break;
155 #endif
156 #ifdef DSERR_NOAGGREGATION
157     case DSERR_NOAGGREGATION:
158         str = "The object does not support aggregation";
159         break;
160 #endif
161 #ifdef DSERR_NODRIVER
162     case DSERR_NODRIVER:
163         str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
164         break;
165 #endif
166 #ifdef DSERR_NOINTERFACE
167     case DSERR_NOINTERFACE:
168         str = "The requested COM interface is not available";
169         break;
170 #endif
171 #ifdef DSERR_OBJECTNOTFOUND
172     case DSERR_OBJECTNOTFOUND:
173         str = "The requested object was not found";
174         break;
175 #endif
176 #ifdef DSERR_OTHERAPPHASPRIO
177     case DSERR_OTHERAPPHASPRIO:
178         str = "Another application has a higher priority level, preventing this call from succeeding";
179         break;
180 #endif
181 #ifdef DSERR_OUTOFMEMORY
182     case DSERR_OUTOFMEMORY:
183         str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
184         break;
185 #endif
186 #ifdef DSERR_PRIOLEVELNEEDED
187     case DSERR_PRIOLEVELNEEDED:
188         str = "A cooperative level of DSSCL_PRIORITY or higher is required";
189         break;
190 #endif
191 #ifdef DSERR_SENDLOOP
192     case DSERR_SENDLOOP:
193         str = "A circular loop of send effects was detected";
194         break;
195 #endif
196 #ifdef DSERR_UNINITIALIZED
197     case DSERR_UNINITIALIZED:
198         str = "The Initialize method has not been called or has not been called successfully before other methods were called";
199         break;
200 #endif
201 #ifdef DSERR_UNSUPPORTED
202     case DSERR_UNSUPPORTED:
203         str = "The function called is not supported at this time";
204         break;
205 #endif
206     default:
207         AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
208         return;
209     }
210 
211     AUD_log (AUDIO_CAP, "Reason: %s\n", str);
212 }
213 
214 static void GCC_FMT_ATTR (2, 3) dsound_logerr (
215     HRESULT hr,
216     const char *fmt,
217     ...
218     )
219 {
220     va_list ap;
221 
222     va_start (ap, fmt);
223     AUD_vlog (AUDIO_CAP, fmt, ap);
224     va_end (ap);
225 
226     dsound_log_hresult (hr);
227 }
228 
229 static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
230     HRESULT hr,
231     const char *typ,
232     const char *fmt,
233     ...
234     )
235 {
236     va_list ap;
237 
238     AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
239     va_start (ap, fmt);
240     AUD_vlog (AUDIO_CAP, fmt, ap);
241     va_end (ap);
242 
243     dsound_log_hresult (hr);
244 }
245 
246 static uint64_t usecs_to_bytes(struct audio_pcm_info *info, uint32_t usecs)
247 {
248     return muldiv64(usecs, info->bytes_per_second, 1000000);
249 }
250 
251 #ifdef DEBUG_DSOUND
252 static void print_wave_format (WAVEFORMATEX *wfx)
253 {
254     dolog ("tag             = %d\n", wfx->wFormatTag);
255     dolog ("nChannels       = %d\n", wfx->nChannels);
256     dolog ("nSamplesPerSec  = %ld\n", wfx->nSamplesPerSec);
257     dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
258     dolog ("nBlockAlign     = %d\n", wfx->nBlockAlign);
259     dolog ("wBitsPerSample  = %d\n", wfx->wBitsPerSample);
260     dolog ("cbSize          = %d\n", wfx->cbSize);
261 }
262 #endif
263 
264 static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
265 {
266     HRESULT hr;
267 
268     hr = IDirectSoundBuffer_Restore (dsb);
269 
270     if (hr != DS_OK) {
271         dsound_logerr (hr, "Could not restore playback buffer\n");
272         return -1;
273     }
274     return 0;
275 }
276 
277 #include "dsound_template.h"
278 #define DSBTYPE_IN
279 #include "dsound_template.h"
280 #undef DSBTYPE_IN
281 
282 static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
283                                   dsound *s)
284 {
285     HRESULT hr;
286 
287     hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
288     if (FAILED (hr)) {
289         dsound_logerr (hr, "Could not get playback buffer status\n");
290         return -1;
291     }
292 
293     if (*statusp & DSERR_BUFFERLOST) {
294         dsound_restore_out(dsb, s);
295         return -1;
296     }
297 
298     return 0;
299 }
300 
301 static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
302                                  DWORD *statusp)
303 {
304     HRESULT hr;
305 
306     hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
307     if (FAILED (hr)) {
308         dsound_logerr (hr, "Could not get capture buffer status\n");
309         return -1;
310     }
311 
312     return 0;
313 }
314 
315 static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
316 {
317     int src_len1 = dst_len;
318     int src_len2 = 0;
319     int pos = hw->rpos + dst_len;
320     struct st_sample *src1 = hw->mix_buf + hw->rpos;
321     struct st_sample *src2 = NULL;
322 
323     if (pos > hw->samples) {
324         src_len1 = hw->samples - hw->rpos;
325         src2 = hw->mix_buf;
326         src_len2 = dst_len - src_len1;
327         pos = src_len2;
328     }
329 
330     if (src_len1) {
331         hw->clip (dst, src1, src_len1);
332     }
333 
334     if (src_len2) {
335         dst = advance (dst, src_len1 << hw->info.shift);
336         hw->clip (dst, src2, src_len2);
337     }
338 
339     hw->rpos = pos % hw->samples;
340 }
341 
342 static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
343                                  dsound *s)
344 {
345     int err;
346     LPVOID p1, p2;
347     DWORD blen1, blen2, len1, len2;
348 
349     err = dsound_lock_out (
350         dsb,
351         &hw->info,
352         0,
353         hw->samples << hw->info.shift,
354         &p1, &p2,
355         &blen1, &blen2,
356         1,
357         s
358         );
359     if (err) {
360         return;
361     }
362 
363     len1 = blen1 >> hw->info.shift;
364     len2 = blen2 >> hw->info.shift;
365 
366 #ifdef DEBUG_DSOUND
367     dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
368            p1, blen1, len1,
369            p2, blen2, len2);
370 #endif
371 
372     if (p1 && len1) {
373         audio_pcm_info_clear_buf (&hw->info, p1, len1);
374     }
375 
376     if (p2 && len2) {
377         audio_pcm_info_clear_buf (&hw->info, p2, len2);
378     }
379 
380     dsound_unlock_out (dsb, p1, p2, blen1, blen2);
381 }
382 
383 static int dsound_open (dsound *s)
384 {
385     HRESULT hr;
386     HWND hwnd;
387 
388     hwnd = GetForegroundWindow ();
389     hr = IDirectSound_SetCooperativeLevel (
390         s->dsound,
391         hwnd,
392         DSSCL_PRIORITY
393         );
394 
395     if (FAILED (hr)) {
396         dsound_logerr (hr, "Could not set cooperative level for window %p\n",
397                        hwnd);
398         return -1;
399     }
400 
401     return 0;
402 }
403 
404 static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
405 {
406     HRESULT hr;
407     DWORD status;
408     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
409     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
410     dsound *s = ds->s;
411 
412     if (!dsb) {
413         dolog ("Attempt to control voice without a buffer\n");
414         return 0;
415     }
416 
417     switch (cmd) {
418     case VOICE_ENABLE:
419         if (dsound_get_status_out (dsb, &status, s)) {
420             return -1;
421         }
422 
423         if (status & DSBSTATUS_PLAYING) {
424             dolog ("warning: Voice is already playing\n");
425             return 0;
426         }
427 
428         dsound_clear_sample (hw, dsb, s);
429 
430         hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
431         if (FAILED (hr)) {
432             dsound_logerr (hr, "Could not start playing buffer\n");
433             return -1;
434         }
435         break;
436 
437     case VOICE_DISABLE:
438         if (dsound_get_status_out (dsb, &status, s)) {
439             return -1;
440         }
441 
442         if (status & DSBSTATUS_PLAYING) {
443             hr = IDirectSoundBuffer_Stop (dsb);
444             if (FAILED (hr)) {
445                 dsound_logerr (hr, "Could not stop playing buffer\n");
446                 return -1;
447             }
448         }
449         else {
450             dolog ("warning: Voice is not playing\n");
451         }
452         break;
453     }
454     return 0;
455 }
456 
457 static size_t dsound_run_out(HWVoiceOut *hw, size_t live)
458 {
459     int err;
460     HRESULT hr;
461     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
462     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
463     size_t len;
464     int hwshift;
465     DWORD blen1, blen2;
466     DWORD len1, len2;
467     DWORD decr;
468     DWORD wpos, ppos, old_pos;
469     LPVOID p1, p2;
470     size_t bufsize;
471     dsound *s = ds->s;
472     AudiodevDsoundOptions *dso = &s->dev->u.dsound;
473 
474     if (!dsb) {
475         dolog ("Attempt to run empty with playback buffer\n");
476         return 0;
477     }
478 
479     hwshift = hw->info.shift;
480     bufsize = hw->samples << hwshift;
481 
482     hr = IDirectSoundBuffer_GetCurrentPosition (
483         dsb,
484         &ppos,
485         ds->first_time ? &wpos : NULL
486         );
487     if (FAILED (hr)) {
488         dsound_logerr (hr, "Could not get playback buffer position\n");
489         return 0;
490     }
491 
492     len = live << hwshift;
493 
494     if (ds->first_time) {
495         if (dso->latency) {
496             DWORD cur_blat;
497 
498             cur_blat = audio_ring_dist (wpos, ppos, bufsize);
499             ds->first_time = 0;
500             old_pos = wpos;
501             old_pos +=
502                 usecs_to_bytes(&hw->info, dso->latency) - cur_blat;
503             old_pos %= bufsize;
504             old_pos &= ~hw->info.align;
505         }
506         else {
507             old_pos = wpos;
508         }
509 #ifdef DEBUG_DSOUND
510         ds->played = 0;
511         ds->mixed = 0;
512 #endif
513     }
514     else {
515         if (ds->old_pos == ppos) {
516 #ifdef DEBUG_DSOUND
517             dolog ("old_pos == ppos\n");
518 #endif
519             return 0;
520         }
521 
522 #ifdef DEBUG_DSOUND
523         ds->played += audio_ring_dist (ds->old_pos, ppos, hw->bufsize);
524 #endif
525         old_pos = ds->old_pos;
526     }
527 
528     if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
529         len = ppos - old_pos;
530     }
531     else {
532         if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
533             len = bufsize - old_pos + ppos;
534         }
535     }
536 
537     if (audio_bug(__func__, len > bufsize)) {
538         dolog("len=%zu bufsize=%zu old_pos=%ld ppos=%ld\n",
539               len, bufsize, old_pos, ppos);
540         return 0;
541     }
542 
543     len &= ~hw->info.align;
544     if (!len) {
545         return 0;
546     }
547 
548 #ifdef DEBUG_DSOUND
549     ds->old_ppos = ppos;
550 #endif
551     err = dsound_lock_out (
552         dsb,
553         &hw->info,
554         old_pos,
555         len,
556         &p1, &p2,
557         &blen1, &blen2,
558         0,
559         s
560         );
561     if (err) {
562         return 0;
563     }
564 
565     len1 = blen1 >> hwshift;
566     len2 = blen2 >> hwshift;
567     decr = len1 + len2;
568 
569     if (p1 && len1) {
570         dsound_write_sample (hw, p1, len1);
571     }
572 
573     if (p2 && len2) {
574         dsound_write_sample (hw, p2, len2);
575     }
576 
577     dsound_unlock_out (dsb, p1, p2, blen1, blen2);
578     ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
579 
580 #ifdef DEBUG_DSOUND
581     ds->mixed += decr << hwshift;
582 
583     dolog ("played %lu mixed %lu diff %ld sec %f\n",
584            ds->played,
585            ds->mixed,
586            ds->mixed - ds->played,
587            abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
588 #endif
589     return decr;
590 }
591 
592 static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
593 {
594     HRESULT hr;
595     DWORD status;
596     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
597     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
598 
599     if (!dscb) {
600         dolog ("Attempt to control capture voice without a buffer\n");
601         return -1;
602     }
603 
604     switch (cmd) {
605     case VOICE_ENABLE:
606         if (dsound_get_status_in (dscb, &status)) {
607             return -1;
608         }
609 
610         if (status & DSCBSTATUS_CAPTURING) {
611             dolog ("warning: Voice is already capturing\n");
612             return 0;
613         }
614 
615         /* clear ?? */
616 
617         hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
618         if (FAILED (hr)) {
619             dsound_logerr (hr, "Could not start capturing\n");
620             return -1;
621         }
622         break;
623 
624     case VOICE_DISABLE:
625         if (dsound_get_status_in (dscb, &status)) {
626             return -1;
627         }
628 
629         if (status & DSCBSTATUS_CAPTURING) {
630             hr = IDirectSoundCaptureBuffer_Stop (dscb);
631             if (FAILED (hr)) {
632                 dsound_logerr (hr, "Could not stop capturing\n");
633                 return -1;
634             }
635         }
636         else {
637             dolog ("warning: Voice is not capturing\n");
638         }
639         break;
640     }
641     return 0;
642 }
643 
644 static size_t dsound_run_in(HWVoiceIn *hw)
645 {
646     int err;
647     HRESULT hr;
648     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
649     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
650     size_t live, len, dead;
651     DWORD blen1, blen2;
652     DWORD len1, len2;
653     DWORD decr;
654     DWORD cpos, rpos;
655     LPVOID p1, p2;
656     int hwshift;
657     dsound *s = ds->s;
658 
659     if (!dscb) {
660         dolog ("Attempt to run without capture buffer\n");
661         return 0;
662     }
663 
664     hwshift = hw->info.shift;
665 
666     live = audio_pcm_hw_get_live_in (hw);
667     dead = hw->samples - live;
668     if (!dead) {
669         return 0;
670     }
671 
672     hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
673         dscb,
674         &cpos,
675         ds->first_time ? &rpos : NULL
676         );
677     if (FAILED (hr)) {
678         dsound_logerr (hr, "Could not get capture buffer position\n");
679         return 0;
680     }
681 
682     if (ds->first_time) {
683         ds->first_time = 0;
684         if (rpos & hw->info.align) {
685             ldebug ("warning: Misaligned capture read position %ld(%d)\n",
686                     rpos, hw->info.align);
687         }
688         hw->wpos = rpos >> hwshift;
689     }
690 
691     if (cpos & hw->info.align) {
692         ldebug ("warning: Misaligned capture position %ld(%d)\n",
693                 cpos, hw->info.align);
694     }
695     cpos >>= hwshift;
696 
697     len = audio_ring_dist (cpos, hw->wpos, hw->samples);
698     if (!len) {
699         return 0;
700     }
701     len = MIN (len, dead);
702 
703     err = dsound_lock_in (
704         dscb,
705         &hw->info,
706         hw->wpos << hwshift,
707         len << hwshift,
708         &p1,
709         &p2,
710         &blen1,
711         &blen2,
712         0,
713         s
714         );
715     if (err) {
716         return 0;
717     }
718 
719     len1 = blen1 >> hwshift;
720     len2 = blen2 >> hwshift;
721     decr = len1 + len2;
722 
723     if (p1 && len1) {
724         hw->conv (hw->conv_buf + hw->wpos, p1, len1);
725     }
726 
727     if (p2 && len2) {
728         hw->conv (hw->conv_buf, p2, len2);
729     }
730 
731     dsound_unlock_in (dscb, p1, p2, blen1, blen2);
732     hw->wpos = (hw->wpos + decr) % hw->samples;
733     return decr;
734 }
735 
736 static void dsound_audio_fini (void *opaque)
737 {
738     HRESULT hr;
739     dsound *s = opaque;
740 
741     if (!s->dsound) {
742         g_free(s);
743         return;
744     }
745 
746     hr = IDirectSound_Release (s->dsound);
747     if (FAILED (hr)) {
748         dsound_logerr (hr, "Could not release DirectSound\n");
749     }
750     s->dsound = NULL;
751 
752     if (!s->dsound_capture) {
753         g_free(s);
754         return;
755     }
756 
757     hr = IDirectSoundCapture_Release (s->dsound_capture);
758     if (FAILED (hr)) {
759         dsound_logerr (hr, "Could not release DirectSoundCapture\n");
760     }
761     s->dsound_capture = NULL;
762 
763     g_free(s);
764 }
765 
766 static void *dsound_audio_init(Audiodev *dev)
767 {
768     int err;
769     HRESULT hr;
770     dsound *s = g_malloc0(sizeof(dsound));
771     AudiodevDsoundOptions *dso;
772 
773     assert(dev->driver == AUDIODEV_DRIVER_DSOUND);
774     s->dev = dev;
775     dso = &dev->u.dsound;
776 
777     if (!dso->has_latency) {
778         dso->has_latency = true;
779         dso->latency = 10000; /* 10 ms */
780     }
781 
782     hr = CoInitialize (NULL);
783     if (FAILED (hr)) {
784         dsound_logerr (hr, "Could not initialize COM\n");
785         g_free(s);
786         return NULL;
787     }
788 
789     hr = CoCreateInstance (
790         &CLSID_DirectSound,
791         NULL,
792         CLSCTX_ALL,
793         &IID_IDirectSound,
794         (void **) &s->dsound
795         );
796     if (FAILED (hr)) {
797         dsound_logerr (hr, "Could not create DirectSound instance\n");
798         g_free(s);
799         return NULL;
800     }
801 
802     hr = IDirectSound_Initialize (s->dsound, NULL);
803     if (FAILED (hr)) {
804         dsound_logerr (hr, "Could not initialize DirectSound\n");
805 
806         hr = IDirectSound_Release (s->dsound);
807         if (FAILED (hr)) {
808             dsound_logerr (hr, "Could not release DirectSound\n");
809         }
810         g_free(s);
811         return NULL;
812     }
813 
814     hr = CoCreateInstance (
815         &CLSID_DirectSoundCapture,
816         NULL,
817         CLSCTX_ALL,
818         &IID_IDirectSoundCapture,
819         (void **) &s->dsound_capture
820         );
821     if (FAILED (hr)) {
822         dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
823     }
824     else {
825         hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
826         if (FAILED (hr)) {
827             dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
828 
829             hr = IDirectSoundCapture_Release (s->dsound_capture);
830             if (FAILED (hr)) {
831                 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
832             }
833             s->dsound_capture = NULL;
834         }
835     }
836 
837     err = dsound_open (s);
838     if (err) {
839         dsound_audio_fini (s);
840         return NULL;
841     }
842 
843     return s;
844 }
845 
846 static struct audio_pcm_ops dsound_pcm_ops = {
847     .init_out = dsound_init_out,
848     .fini_out = dsound_fini_out,
849     .run_out  = dsound_run_out,
850     .ctl_out  = dsound_ctl_out,
851 
852     .init_in  = dsound_init_in,
853     .fini_in  = dsound_fini_in,
854     .run_in   = dsound_run_in,
855     .ctl_in   = dsound_ctl_in
856 };
857 
858 static struct audio_driver dsound_audio_driver = {
859     .name           = "dsound",
860     .descr          = "DirectSound http://wikipedia.org/wiki/DirectSound",
861     .init           = dsound_audio_init,
862     .fini           = dsound_audio_fini,
863     .pcm_ops        = &dsound_pcm_ops,
864     .can_be_default = 1,
865     .max_voices_out = INT_MAX,
866     .max_voices_in  = 1,
867     .voice_size_out = sizeof (DSoundVoiceOut),
868     .voice_size_in  = sizeof (DSoundVoiceIn)
869 };
870 
871 static void register_audio_dsound(void)
872 {
873     audio_driver_register(&dsound_audio_driver);
874 }
875 type_init(register_audio_dsound);
876