]>
Commit | Line | Data |
---|---|---|
cf2c1839 GH |
1 | /* |
2 | * Copyright (C) 2010 Red Hat, Inc. | |
3 | * | |
4 | * maintained by Gerd Hoffmann <kraxel@redhat.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation; either version 2 or | |
9 | * (at your option) version 3 of the License. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
6086a565 | 20 | #include "qemu/osdep.h" |
87776ab7 | 21 | #include "qemu/host-utils.h" |
0b8fa32f | 22 | #include "qemu/module.h" |
d49b6836 | 23 | #include "qemu/error-report.h" |
1de7afc9 | 24 | #include "qemu/timer.h" |
3e313753 GH |
25 | #include "ui/qemu-spice.h" |
26 | ||
27 | #define AUDIO_CAP "spice" | |
28 | #include "audio.h" | |
29 | #include "audio_int.h" | |
30 | ||
795ca114 JW |
31 | #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3 |
32 | #define LINE_OUT_SAMPLES (480 * 4) | |
33 | #else | |
34 | #define LINE_OUT_SAMPLES (256 * 4) | |
35 | #endif | |
36 | ||
37 | #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3 | |
38 | #define LINE_IN_SAMPLES (480 * 4) | |
39 | #else | |
40 | #define LINE_IN_SAMPLES (256 * 4) | |
41 | #endif | |
3e313753 | 42 | |
3e313753 GH |
43 | typedef struct SpiceVoiceOut { |
44 | HWVoiceOut hw; | |
45 | SpicePlaybackInstance sin; | |
857271a2 | 46 | RateCtl rate; |
3e313753 GH |
47 | int active; |
48 | uint32_t *frame; | |
8c198ff0 | 49 | uint32_t fpos; |
3e313753 GH |
50 | uint32_t fsize; |
51 | } SpiceVoiceOut; | |
52 | ||
53 | typedef struct SpiceVoiceIn { | |
54 | HWVoiceIn hw; | |
55 | SpiceRecordInstance sin; | |
857271a2 | 56 | RateCtl rate; |
3e313753 | 57 | int active; |
3e313753 GH |
58 | } SpiceVoiceIn; |
59 | ||
60 | static const SpicePlaybackInterface playback_sif = { | |
61 | .base.type = SPICE_INTERFACE_PLAYBACK, | |
62 | .base.description = "playback", | |
63 | .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR, | |
64 | .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR, | |
65 | }; | |
66 | ||
67 | static const SpiceRecordInterface record_sif = { | |
68 | .base.type = SPICE_INTERFACE_RECORD, | |
69 | .base.description = "record", | |
70 | .base.major_version = SPICE_INTERFACE_RECORD_MAJOR, | |
71 | .base.minor_version = SPICE_INTERFACE_RECORD_MINOR, | |
72 | }; | |
73 | ||
71830221 | 74 | static void *spice_audio_init(Audiodev *dev) |
3e313753 GH |
75 | { |
76 | if (!using_spice) { | |
77 | return NULL; | |
78 | } | |
79 | return &spice_audio_init; | |
80 | } | |
81 | ||
82 | static void spice_audio_fini (void *opaque) | |
83 | { | |
84 | /* nothing */ | |
85 | } | |
86 | ||
3e313753 GH |
87 | /* playback */ |
88 | ||
5706db1d KZ |
89 | static int line_out_init(HWVoiceOut *hw, struct audsettings *as, |
90 | void *drv_opaque) | |
3e313753 GH |
91 | { |
92 | SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw); | |
93 | struct audsettings settings; | |
94 | ||
795ca114 JW |
95 | #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3 |
96 | settings.freq = spice_server_get_best_playback_rate(NULL); | |
97 | #else | |
3e313753 | 98 | settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ; |
795ca114 | 99 | #endif |
3e313753 | 100 | settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN; |
85bc5852 | 101 | settings.fmt = AUDIO_FORMAT_S16; |
3e313753 GH |
102 | settings.endianness = AUDIO_HOST_ENDIANNESS; |
103 | ||
104 | audio_pcm_init_info (&hw->info, &settings); | |
105 | hw->samples = LINE_OUT_SAMPLES; | |
106 | out->active = 0; | |
107 | ||
108 | out->sin.base.sif = &playback_sif.base; | |
05b53636 | 109 | qemu_spice.add_interface(&out->sin.base); |
795ca114 JW |
110 | #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3 |
111 | spice_server_set_playback_rate(&out->sin, settings.freq); | |
112 | #endif | |
3e313753 GH |
113 | return 0; |
114 | } | |
115 | ||
116 | static void line_out_fini (HWVoiceOut *hw) | |
117 | { | |
118 | SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw); | |
119 | ||
120 | spice_server_remove_interface (&out->sin.base); | |
121 | } | |
122 | ||
8c198ff0 | 123 | static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size) |
3e313753 | 124 | { |
8c198ff0 | 125 | SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw); |
3e313753 | 126 | |
8c198ff0 KZ |
127 | if (!out->frame) { |
128 | spice_server_playback_get_buffer(&out->sin, &out->frame, &out->fsize); | |
129 | out->fpos = 0; | |
3e313753 GH |
130 | } |
131 | ||
8c198ff0 | 132 | if (out->frame) { |
aec6d0dc | 133 | *size = MIN((out->fsize - out->fpos) << 2, *size); |
8c198ff0 | 134 | } |
aec6d0dc VR |
135 | |
136 | *size = audio_rate_get_bytes(&hw->info, &out->rate, *size); | |
137 | ||
8c198ff0 KZ |
138 | return out->frame + out->fpos; |
139 | } | |
3e313753 | 140 | |
8c198ff0 KZ |
141 | static size_t line_out_put_buffer(HWVoiceOut *hw, void *buf, size_t size) |
142 | { | |
143 | SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw); | |
144 | ||
d4b70fa4 VR |
145 | if (buf) { |
146 | assert(buf == out->frame + out->fpos && out->fpos <= out->fsize); | |
147 | out->fpos += size >> 2; | |
8c198ff0 | 148 | |
d4b70fa4 VR |
149 | if (out->fpos == out->fsize) { /* buffer full */ |
150 | spice_server_playback_put_samples(&out->sin, out->frame); | |
151 | out->frame = NULL; | |
152 | } | |
3e313753 | 153 | } |
8c198ff0 KZ |
154 | |
155 | return size; | |
3e313753 GH |
156 | } |
157 | ||
571a8c52 | 158 | static void line_out_enable(HWVoiceOut *hw, bool enable) |
3e313753 GH |
159 | { |
160 | SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw); | |
161 | ||
571a8c52 | 162 | if (enable) { |
3e313753 | 163 | if (out->active) { |
571a8c52 | 164 | return; |
3e313753 GH |
165 | } |
166 | out->active = 1; | |
857271a2 | 167 | audio_rate_start(&out->rate); |
3e313753 | 168 | spice_server_playback_start (&out->sin); |
571a8c52 | 169 | } else { |
3e313753 | 170 | if (!out->active) { |
571a8c52 | 171 | return; |
3e313753 GH |
172 | } |
173 | out->active = 0; | |
174 | if (out->frame) { | |
8c198ff0 | 175 | memset(out->frame + out->fpos, 0, (out->fsize - out->fpos) << 2); |
3e313753 | 176 | spice_server_playback_put_samples (&out->sin, out->frame); |
8c198ff0 | 177 | out->frame = NULL; |
3e313753 GH |
178 | } |
179 | spice_server_playback_stop (&out->sin); | |
3e313753 | 180 | } |
571a8c52 | 181 | } |
a70c99c6 | 182 | |
571a8c52 | 183 | #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2)) |
cecc1e79 | 184 | static void line_out_volume(HWVoiceOut *hw, Volume *vol) |
571a8c52 KZ |
185 | { |
186 | SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw); | |
187 | uint16_t svol[2]; | |
188 | ||
cecc1e79 KZ |
189 | assert(vol->channels == 2); |
190 | svol[0] = vol->vol[0] * 257; | |
191 | svol[1] = vol->vol[1] * 257; | |
571a8c52 KZ |
192 | spice_server_playback_set_volume(&out->sin, 2, svol); |
193 | spice_server_playback_set_mute(&out->sin, vol->mute); | |
3e313753 | 194 | } |
571a8c52 | 195 | #endif |
3e313753 GH |
196 | |
197 | /* record */ | |
198 | ||
5706db1d | 199 | static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) |
3e313753 GH |
200 | { |
201 | SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw); | |
202 | struct audsettings settings; | |
203 | ||
795ca114 JW |
204 | #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3 |
205 | settings.freq = spice_server_get_best_record_rate(NULL); | |
206 | #else | |
3e313753 | 207 | settings.freq = SPICE_INTERFACE_RECORD_FREQ; |
795ca114 | 208 | #endif |
3e313753 | 209 | settings.nchannels = SPICE_INTERFACE_RECORD_CHAN; |
85bc5852 | 210 | settings.fmt = AUDIO_FORMAT_S16; |
3e313753 GH |
211 | settings.endianness = AUDIO_HOST_ENDIANNESS; |
212 | ||
213 | audio_pcm_init_info (&hw->info, &settings); | |
214 | hw->samples = LINE_IN_SAMPLES; | |
215 | in->active = 0; | |
216 | ||
217 | in->sin.base.sif = &record_sif.base; | |
05b53636 | 218 | qemu_spice.add_interface(&in->sin.base); |
795ca114 JW |
219 | #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3 |
220 | spice_server_set_record_rate(&in->sin, settings.freq); | |
221 | #endif | |
3e313753 GH |
222 | return 0; |
223 | } | |
224 | ||
225 | static void line_in_fini (HWVoiceIn *hw) | |
226 | { | |
227 | SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw); | |
228 | ||
229 | spice_server_remove_interface (&in->sin.base); | |
230 | } | |
231 | ||
8c198ff0 | 232 | static size_t line_in_read(HWVoiceIn *hw, void *buf, size_t len) |
3e313753 GH |
233 | { |
234 | SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw); | |
857271a2 | 235 | uint64_t to_read = audio_rate_get_bytes(&hw->info, &in->rate, len) >> 2; |
8c198ff0 | 236 | size_t ready = spice_server_record_get_samples(&in->sin, buf, to_read); |
3e313753 | 237 | |
8c198ff0 | 238 | /* XXX: do we need this? */ |
3e313753 | 239 | if (ready == 0) { |
8c198ff0 KZ |
240 | memset(buf, 0, to_read << 2); |
241 | ready = to_read; | |
3e313753 GH |
242 | } |
243 | ||
8c198ff0 | 244 | return ready << 2; |
3e313753 GH |
245 | } |
246 | ||
571a8c52 | 247 | static void line_in_enable(HWVoiceIn *hw, bool enable) |
3e313753 GH |
248 | { |
249 | SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw); | |
250 | ||
571a8c52 | 251 | if (enable) { |
3e313753 | 252 | if (in->active) { |
571a8c52 | 253 | return; |
3e313753 GH |
254 | } |
255 | in->active = 1; | |
857271a2 | 256 | audio_rate_start(&in->rate); |
3e313753 | 257 | spice_server_record_start (&in->sin); |
571a8c52 | 258 | } else { |
3e313753 | 259 | if (!in->active) { |
571a8c52 | 260 | return; |
3e313753 GH |
261 | } |
262 | in->active = 0; | |
263 | spice_server_record_stop (&in->sin); | |
3e313753 | 264 | } |
571a8c52 | 265 | } |
a70c99c6 | 266 | |
571a8c52 | 267 | #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2)) |
cecc1e79 | 268 | static void line_in_volume(HWVoiceIn *hw, Volume *vol) |
571a8c52 KZ |
269 | { |
270 | SpiceVoiceIn *in = container_of(hw, SpiceVoiceIn, hw); | |
271 | uint16_t svol[2]; | |
272 | ||
cecc1e79 KZ |
273 | assert(vol->channels == 2); |
274 | svol[0] = vol->vol[0] * 257; | |
275 | svol[1] = vol->vol[1] * 257; | |
571a8c52 KZ |
276 | spice_server_record_set_volume(&in->sin, 2, svol); |
277 | spice_server_record_set_mute(&in->sin, vol->mute); | |
3e313753 | 278 | } |
571a8c52 | 279 | #endif |
3e313753 | 280 | |
3e313753 GH |
281 | static struct audio_pcm_ops audio_callbacks = { |
282 | .init_out = line_out_init, | |
283 | .fini_out = line_out_fini, | |
8c198ff0 KZ |
284 | .write = audio_generic_write, |
285 | .get_buffer_out = line_out_get_buffer, | |
286 | .put_buffer_out = line_out_put_buffer, | |
571a8c52 KZ |
287 | .enable_out = line_out_enable, |
288 | #if (SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && \ | |
289 | (SPICE_INTERFACE_PLAYBACK_MINOR >= 2) | |
290 | .volume_out = line_out_volume, | |
291 | #endif | |
3e313753 GH |
292 | |
293 | .init_in = line_in_init, | |
294 | .fini_in = line_in_fini, | |
8c198ff0 | 295 | .read = line_in_read, |
a2893c83 | 296 | .run_buffer_in = audio_generic_run_buffer_in, |
571a8c52 KZ |
297 | .enable_in = line_in_enable, |
298 | #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2)) | |
299 | .volume_in = line_in_volume, | |
300 | #endif | |
3e313753 GH |
301 | }; |
302 | ||
d3893a39 | 303 | static struct audio_driver spice_audio_driver = { |
3e313753 GH |
304 | .name = "spice", |
305 | .descr = "spice audio driver", | |
3e313753 GH |
306 | .init = spice_audio_init, |
307 | .fini = spice_audio_fini, | |
308 | .pcm_ops = &audio_callbacks, | |
309 | .max_voices_out = 1, | |
310 | .max_voices_in = 1, | |
311 | .voice_size_out = sizeof (SpiceVoiceOut), | |
312 | .voice_size_in = sizeof (SpiceVoiceIn), | |
313 | }; | |
314 | ||
d3893a39 GH |
315 | static void register_audio_spice(void) |
316 | { | |
317 | audio_driver_register(&spice_audio_driver); | |
318 | } | |
319 | type_init(register_audio_spice); |