1 | /* $Id: dsound_template.h 53417 2014-12-01 13:27:17Z vboxsync $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Copyright (C) 2014 Oracle Corporation
|
---|
5 | *
|
---|
6 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
7 | * available from http://www.virtualbox.org. This file is free software;
|
---|
8 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
9 | * General Public License (GPL) as published by the Free Software
|
---|
10 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
11 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
12 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
13 | * --------------------------------------------------------------------
|
---|
14 | *
|
---|
15 | * QEMU DirectSound audio driver header
|
---|
16 | *
|
---|
17 | * Copyright (c) 2005 Vassili Karpov (malc)
|
---|
18 | *
|
---|
19 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
20 | * of this software and associated documentation files (the "Software"), to deal
|
---|
21 | * in the Software without restriction, including without limitation the rights
|
---|
22 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
23 | * copies of the Software, and to permit persons to whom the Software is
|
---|
24 | * furnished to do so, subject to the following conditions:
|
---|
25 | *
|
---|
26 | * The above copyright notice and this permission notice shall be included in
|
---|
27 | * all copies or substantial portions of the Software.
|
---|
28 | *
|
---|
29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
30 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
31 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
32 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
33 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
34 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
35 | * THE SOFTWARE.
|
---|
36 | */
|
---|
37 | #ifdef DSBTYPE_IN
|
---|
38 | #define NAME "capture buffer"
|
---|
39 | #define TYPE in
|
---|
40 | #define IFACE IDirectSoundCaptureBuffer
|
---|
41 | #define BUFPTR LPDIRECTSOUNDCAPTUREBUFFER
|
---|
42 | #define FIELD dsound_capture_buffer
|
---|
43 | #else
|
---|
44 | #define NAME "playback buffer"
|
---|
45 | #define TYPE out
|
---|
46 | #define IFACE IDirectSoundBuffer
|
---|
47 | #define BUFPTR LPDIRECTSOUNDBUFFER
|
---|
48 | #define FIELD dsound_buffer
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | static int glue (dsound_unlock_, TYPE) (
|
---|
52 | BUFPTR buf,
|
---|
53 | LPVOID p1,
|
---|
54 | LPVOID p2,
|
---|
55 | DWORD blen1,
|
---|
56 | DWORD blen2
|
---|
57 | )
|
---|
58 | {
|
---|
59 | HRESULT hr;
|
---|
60 |
|
---|
61 | hr = glue (IFACE, _Unlock) (buf, p1, blen1, p2, blen2);
|
---|
62 | if (FAILED (hr)) {
|
---|
63 | dsound_logerr (hr, "Could not unlock " NAME "\n");
|
---|
64 | return -1;
|
---|
65 | }
|
---|
66 |
|
---|
67 | return 0;
|
---|
68 | }
|
---|
69 |
|
---|
70 | static int glue (dsound_lock_, TYPE) (
|
---|
71 | BUFPTR buf,
|
---|
72 | struct audio_pcm_info *info,
|
---|
73 | DWORD pos,
|
---|
74 | DWORD len,
|
---|
75 | LPVOID *p1p,
|
---|
76 | LPVOID *p2p,
|
---|
77 | DWORD *blen1p,
|
---|
78 | DWORD *blen2p,
|
---|
79 | int entire
|
---|
80 | )
|
---|
81 | {
|
---|
82 | HRESULT hr;
|
---|
83 | int i;
|
---|
84 | LPVOID p1 = NULL, p2 = NULL;
|
---|
85 | DWORD blen1 = 0, blen2 = 0;
|
---|
86 | DWORD flag;
|
---|
87 |
|
---|
88 | #ifdef DSBTYPE_IN
|
---|
89 | flag = entire ? DSCBLOCK_ENTIREBUFFER : 0;
|
---|
90 | #else
|
---|
91 | flag = entire ? DSBLOCK_ENTIREBUFFER : 0;
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | for (i = 0; i < conf.lock_retries; ++i) {
|
---|
95 | hr = glue (IFACE, _Lock) (
|
---|
96 | buf,
|
---|
97 | pos,
|
---|
98 | len,
|
---|
99 | &p1,
|
---|
100 | &blen1,
|
---|
101 | &p2,
|
---|
102 | &blen2,
|
---|
103 | flag
|
---|
104 | );
|
---|
105 |
|
---|
106 | if (FAILED (hr)) {
|
---|
107 | #ifndef DSBTYPE_IN
|
---|
108 | if (hr == DSERR_BUFFERLOST) {
|
---|
109 | if (glue (dsound_restore_, TYPE) (buf)) {
|
---|
110 | dsound_logerr (hr, "Could not lock " NAME "\n");
|
---|
111 | goto fail;
|
---|
112 | }
|
---|
113 | continue;
|
---|
114 | }
|
---|
115 | #endif
|
---|
116 | dsound_logerr (hr, "Could not lock " NAME "\n");
|
---|
117 | goto fail;
|
---|
118 | }
|
---|
119 |
|
---|
120 | break;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (i == conf.lock_retries) {
|
---|
124 | dolog ("%d attempts to lock " NAME " failed\n", i);
|
---|
125 | goto fail;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if ((p1 && (blen1 & info->align)) || (p2 && (blen2 & info->align))) {
|
---|
129 | dolog ("DirectSound returned misaligned buffer %ld %ld\n",
|
---|
130 | blen1, blen2);
|
---|
131 | glue (dsound_unlock_, TYPE) (buf, p1, p2, blen1, blen2);
|
---|
132 | goto fail;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (!p1 && blen1) {
|
---|
136 | dolog ("warning: !p1 && blen1=%ld\n", blen1);
|
---|
137 | blen1 = 0;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (!p2 && blen2) {
|
---|
141 | dolog ("warning: !p2 && blen2=%ld\n", blen2);
|
---|
142 | blen2 = 0;
|
---|
143 | }
|
---|
144 |
|
---|
145 | *p1p = p1;
|
---|
146 | *p2p = p2;
|
---|
147 | *blen1p = blen1;
|
---|
148 | *blen2p = blen2;
|
---|
149 | return 0;
|
---|
150 |
|
---|
151 | fail:
|
---|
152 | *p1p = NULL;
|
---|
153 | *p2p = NULL;
|
---|
154 | *blen1p = -1;
|
---|
155 | *blen2p = -1;
|
---|
156 | return -1;
|
---|
157 | }
|
---|
158 |
|
---|
159 | #ifdef DSBTYPE_IN
|
---|
160 | static void dsound_fini_in (HWVoiceIn *hw)
|
---|
161 | {
|
---|
162 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
|
---|
163 | dsoundCaptureClose (ds);
|
---|
164 | ds->last_read_pos = 0;
|
---|
165 | ds->capture_buffer_size = 0;
|
---|
166 | memset (&ds->as, 0, sizeof(ds->as));
|
---|
167 | }
|
---|
168 | #else
|
---|
169 | static void dsound_fini_out (HWVoiceOut *hw)
|
---|
170 | {
|
---|
171 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
|
---|
172 | dsoundPlayClose (ds);
|
---|
173 | ds->old_pos = 0;
|
---|
174 | ds->first_time = 1;
|
---|
175 | ds->playback_buffer_size = 0;
|
---|
176 | memset (&ds->as, 0, sizeof(ds->as));
|
---|
177 | }
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | #ifdef DSBTYPE_IN
|
---|
181 | static int dsound_init_in (HWVoiceIn *hw, audsettings_t *as)
|
---|
182 | {
|
---|
183 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
|
---|
184 |
|
---|
185 | ds->last_read_pos = 0;
|
---|
186 | ds->capture_buffer_size = 0;
|
---|
187 | ds->dsound_capture = NULL;
|
---|
188 | ds->dsound_capture_buffer = NULL;
|
---|
189 | ds->as = *as;
|
---|
190 |
|
---|
191 | /* Init default settings. */
|
---|
192 | audio_pcm_init_info (&hw->info, &ds->as);
|
---|
193 | hw->samples = conf.bufsize_in >> hw->info.shift;
|
---|
194 |
|
---|
195 | /* Try to open capture in case the device is already there. */
|
---|
196 | dsoundCaptureOpen (ds);
|
---|
197 |
|
---|
198 | return 0;
|
---|
199 | }
|
---|
200 | #else
|
---|
201 | static int dsound_init_out (HWVoiceOut *hw, audsettings_t *as)
|
---|
202 | {
|
---|
203 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
|
---|
204 |
|
---|
205 | ds->dsound = NULL;
|
---|
206 | ds->dsound_buffer = NULL;
|
---|
207 | ds->old_pos = 0;
|
---|
208 | ds->first_time = 1;
|
---|
209 | ds->playback_buffer_size = 0;
|
---|
210 | ds->as = *as;
|
---|
211 |
|
---|
212 | /* Init default settings. */
|
---|
213 | audio_pcm_init_info (&hw->info, &ds->as);
|
---|
214 | hw->samples = conf.bufsize_out >> hw->info.shift;
|
---|
215 |
|
---|
216 | /* Try to open playback in case the device is already there. */
|
---|
217 | dsoundPlayOpen (ds);
|
---|
218 |
|
---|
219 | return 0;
|
---|
220 | }
|
---|
221 | #endif
|
---|
222 |
|
---|
223 | #undef NAME
|
---|
224 | #undef TYPE
|
---|
225 | #undef IFACE
|
---|
226 | #undef BUFPTR
|
---|
227 | #undef FIELD
|
---|