]>
Commit | Line | Data |
---|---|---|
cf2c1839 GH |
1 | /* |
2 | * Copyright (C) 2010 Red Hat, Inc. | |
3 | * | |
4 | * maintained by Gerd Hoffmann <[email protected]> | |
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 GH |
42 | |
43 | typedef struct SpiceRateCtl { | |
44 | int64_t start_ticks; | |
45 | int64_t bytes_sent; | |
46 | } SpiceRateCtl; | |
47 | ||
48 | typedef struct SpiceVoiceOut { | |
49 | HWVoiceOut hw; | |
50 | SpicePlaybackInstance sin; | |
51 | SpiceRateCtl rate; | |
52 | int active; | |
53 | uint32_t *frame; | |
54 | uint32_t *fpos; | |
55 | uint32_t fsize; | |
56 | } SpiceVoiceOut; | |
57 | ||
58 | typedef struct SpiceVoiceIn { | |
59 | HWVoiceIn hw; | |
60 | SpiceRecordInstance sin; | |
61 | SpiceRateCtl rate; | |
62 | int active; | |
63 | uint32_t samples[LINE_IN_SAMPLES]; | |
64 | } SpiceVoiceIn; | |
65 | ||
66 | static const SpicePlaybackInterface playback_sif = { | |
67 | .base.type = SPICE_INTERFACE_PLAYBACK, | |
68 | .base.description = "playback", | |
69 | .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR, | |
70 | .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR, | |
71 | }; | |
72 | ||
73 | static const SpiceRecordInterface record_sif = { | |
74 | .base.type = SPICE_INTERFACE_RECORD, | |
75 | .base.description = "record", | |
76 | .base.major_version = SPICE_INTERFACE_RECORD_MAJOR, | |
77 | .base.minor_version = SPICE_INTERFACE_RECORD_MINOR, | |
78 | }; | |
79 | ||
71830221 | 80 | static void *spice_audio_init(Audiodev *dev) |
3e313753 GH |
81 | { |
82 | if (!using_spice) { | |
83 | return NULL; | |
84 | } | |
85 | return &spice_audio_init; | |
86 | } | |
87 | ||
88 | static void spice_audio_fini (void *opaque) | |
89 | { | |
90 | /* nothing */ | |
91 | } | |
92 | ||
93 | static void rate_start (SpiceRateCtl *rate) | |
94 | { | |
95 | memset (rate, 0, sizeof (*rate)); | |
bc72ad67 | 96 | rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
3e313753 GH |
97 | } |
98 | ||
99 | static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate) | |
100 | { | |
101 | int64_t now; | |
102 | int64_t ticks; | |
103 | int64_t bytes; | |
104 | int64_t samples; | |
105 | ||
bc72ad67 | 106 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
3e313753 | 107 | ticks = now - rate->start_ticks; |
73bcb24d | 108 | bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND); |
3e313753 GH |
109 | samples = (bytes - rate->bytes_sent) >> info->shift; |
110 | if (samples < 0 || samples > 65536) { | |
69e99504 | 111 | error_report("Resetting rate control (%" PRId64 " samples)", samples); |
73bcb24d | 112 | rate_start(rate); |
3e313753 GH |
113 | samples = 0; |
114 | } | |
115 | rate->bytes_sent += samples << info->shift; | |
116 | return samples; | |
117 | } | |
118 | ||
119 | /* playback */ | |
120 | ||
5706db1d KZ |
121 | static int line_out_init(HWVoiceOut *hw, struct audsettings *as, |
122 | void *drv_opaque) | |
3e313753 GH |
123 | { |
124 | SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw); | |
125 | struct audsettings settings; | |
126 | ||
795ca114 JW |
127 | #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3 |
128 | settings.freq = spice_server_get_best_playback_rate(NULL); | |
129 | #else | |
3e313753 | 130 | settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ; |
795ca114 | 131 | #endif |
3e313753 | 132 | settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN; |
85bc5852 | 133 | settings.fmt = AUDIO_FORMAT_S16; |
3e313753 GH |
134 | settings.endianness = AUDIO_HOST_ENDIANNESS; |
135 | ||
136 | audio_pcm_init_info (&hw->info, &settings); | |
137 | hw->samples = LINE_OUT_SAMPLES; | |
138 | out->active = 0; | |
139 | ||
140 | out->sin.base.sif = &playback_sif.base; | |
141 | qemu_spice_add_interface (&out->sin.base); | |
795ca114 JW |
142 | #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3 |
143 | spice_server_set_playback_rate(&out->sin, settings.freq); | |
144 | #endif | |
3e313753 GH |
145 | return 0; |
146 | } | |
147 | ||
148 | static void line_out_fini (HWVoiceOut *hw) | |
149 | { | |
150 | SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw); | |
151 | ||
152 | spice_server_remove_interface (&out->sin.base); | |
153 | } | |
154 | ||
7520462b | 155 | static size_t line_out_run (HWVoiceOut *hw, size_t live) |
3e313753 GH |
156 | { |
157 | SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw); | |
7520462b KZ |
158 | size_t rpos, decr; |
159 | size_t samples; | |
3e313753 GH |
160 | |
161 | if (!live) { | |
162 | return 0; | |
163 | } | |
164 | ||
165 | decr = rate_get_samples (&hw->info, &out->rate); | |
58935915 | 166 | decr = MIN (live, decr); |
3e313753 GH |
167 | |
168 | samples = decr; | |
169 | rpos = hw->rpos; | |
170 | while (samples) { | |
171 | int left_till_end_samples = hw->samples - rpos; | |
58935915 | 172 | int len = MIN (samples, left_till_end_samples); |
3e313753 GH |
173 | |
174 | if (!out->frame) { | |
175 | spice_server_playback_get_buffer (&out->sin, &out->frame, &out->fsize); | |
176 | out->fpos = out->frame; | |
177 | } | |
178 | if (out->frame) { | |
58935915 | 179 | len = MIN (len, out->fsize); |
3e313753 GH |
180 | hw->clip (out->fpos, hw->mix_buf + rpos, len); |
181 | out->fsize -= len; | |
182 | out->fpos += len; | |
183 | if (out->fsize == 0) { | |
184 | spice_server_playback_put_samples (&out->sin, out->frame); | |
185 | out->frame = out->fpos = NULL; | |
186 | } | |
187 | } | |
188 | rpos = (rpos + len) % hw->samples; | |
189 | samples -= len; | |
190 | } | |
191 | hw->rpos = rpos; | |
192 | return decr; | |
193 | } | |
194 | ||
3e313753 GH |
195 | static int line_out_ctl (HWVoiceOut *hw, int cmd, ...) |
196 | { | |
197 | SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw); | |
198 | ||
199 | switch (cmd) { | |
200 | case VOICE_ENABLE: | |
201 | if (out->active) { | |
202 | break; | |
203 | } | |
204 | out->active = 1; | |
205 | rate_start (&out->rate); | |
206 | spice_server_playback_start (&out->sin); | |
207 | break; | |
208 | case VOICE_DISABLE: | |
209 | if (!out->active) { | |
210 | break; | |
211 | } | |
212 | out->active = 0; | |
213 | if (out->frame) { | |
214 | memset (out->fpos, 0, out->fsize << 2); | |
215 | spice_server_playback_put_samples (&out->sin, out->frame); | |
216 | out->frame = out->fpos = NULL; | |
217 | } | |
218 | spice_server_playback_stop (&out->sin); | |
219 | break; | |
a70c99c6 MAL |
220 | case VOICE_VOLUME: |
221 | { | |
222 | #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2)) | |
223 | SWVoiceOut *sw; | |
224 | va_list ap; | |
225 | uint16_t vol[2]; | |
226 | ||
227 | va_start (ap, cmd); | |
228 | sw = va_arg (ap, SWVoiceOut *); | |
229 | va_end (ap); | |
230 | ||
231 | vol[0] = sw->vol.l / ((1ULL << 16) + 1); | |
232 | vol[1] = sw->vol.r / ((1ULL << 16) + 1); | |
233 | spice_server_playback_set_volume (&out->sin, 2, vol); | |
234 | spice_server_playback_set_mute (&out->sin, sw->vol.mute); | |
235 | #endif | |
236 | break; | |
237 | } | |
3e313753 | 238 | } |
a70c99c6 | 239 | |
3e313753 GH |
240 | return 0; |
241 | } | |
242 | ||
243 | /* record */ | |
244 | ||
5706db1d | 245 | static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) |
3e313753 GH |
246 | { |
247 | SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw); | |
248 | struct audsettings settings; | |
249 | ||
795ca114 JW |
250 | #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3 |
251 | settings.freq = spice_server_get_best_record_rate(NULL); | |
252 | #else | |
3e313753 | 253 | settings.freq = SPICE_INTERFACE_RECORD_FREQ; |
795ca114 | 254 | #endif |
3e313753 | 255 | settings.nchannels = SPICE_INTERFACE_RECORD_CHAN; |
85bc5852 | 256 | settings.fmt = AUDIO_FORMAT_S16; |
3e313753 GH |
257 | settings.endianness = AUDIO_HOST_ENDIANNESS; |
258 | ||
259 | audio_pcm_init_info (&hw->info, &settings); | |
260 | hw->samples = LINE_IN_SAMPLES; | |
261 | in->active = 0; | |
262 | ||
263 | in->sin.base.sif = &record_sif.base; | |
264 | qemu_spice_add_interface (&in->sin.base); | |
795ca114 JW |
265 | #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3 |
266 | spice_server_set_record_rate(&in->sin, settings.freq); | |
267 | #endif | |
3e313753 GH |
268 | return 0; |
269 | } | |
270 | ||
271 | static void line_in_fini (HWVoiceIn *hw) | |
272 | { | |
273 | SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw); | |
274 | ||
275 | spice_server_remove_interface (&in->sin.base); | |
276 | } | |
277 | ||
7520462b | 278 | static size_t line_in_run(HWVoiceIn *hw) |
3e313753 GH |
279 | { |
280 | SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw); | |
7520462b | 281 | size_t num_samples; |
3e313753 | 282 | int ready; |
7520462b | 283 | size_t len[2]; |
3e313753 GH |
284 | uint64_t delta_samp; |
285 | const uint32_t *samples; | |
286 | ||
287 | if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) { | |
288 | return 0; | |
289 | } | |
290 | ||
291 | delta_samp = rate_get_samples (&hw->info, &in->rate); | |
58935915 | 292 | num_samples = MIN (num_samples, delta_samp); |
3e313753 GH |
293 | |
294 | ready = spice_server_record_get_samples (&in->sin, in->samples, num_samples); | |
295 | samples = in->samples; | |
296 | if (ready == 0) { | |
297 | static const uint32_t silence[LINE_IN_SAMPLES]; | |
298 | samples = silence; | |
299 | ready = LINE_IN_SAMPLES; | |
300 | } | |
301 | ||
58935915 | 302 | num_samples = MIN (ready, num_samples); |
3e313753 GH |
303 | |
304 | if (hw->wpos + num_samples > hw->samples) { | |
305 | len[0] = hw->samples - hw->wpos; | |
306 | len[1] = num_samples - len[0]; | |
307 | } else { | |
308 | len[0] = num_samples; | |
309 | len[1] = 0; | |
310 | } | |
311 | ||
00e07679 | 312 | hw->conv (hw->conv_buf + hw->wpos, samples, len[0]); |
3e313753 GH |
313 | |
314 | if (len[1]) { | |
00e07679 | 315 | hw->conv (hw->conv_buf, samples + len[0], len[1]); |
3e313753 GH |
316 | } |
317 | ||
318 | hw->wpos = (hw->wpos + num_samples) % hw->samples; | |
319 | ||
320 | return num_samples; | |
321 | } | |
322 | ||
3e313753 GH |
323 | static int line_in_ctl (HWVoiceIn *hw, int cmd, ...) |
324 | { | |
325 | SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw); | |
326 | ||
327 | switch (cmd) { | |
328 | case VOICE_ENABLE: | |
329 | if (in->active) { | |
330 | break; | |
331 | } | |
332 | in->active = 1; | |
333 | rate_start (&in->rate); | |
334 | spice_server_record_start (&in->sin); | |
335 | break; | |
336 | case VOICE_DISABLE: | |
337 | if (!in->active) { | |
338 | break; | |
339 | } | |
340 | in->active = 0; | |
341 | spice_server_record_stop (&in->sin); | |
342 | break; | |
a70c99c6 MAL |
343 | case VOICE_VOLUME: |
344 | { | |
345 | #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2)) | |
346 | SWVoiceIn *sw; | |
347 | va_list ap; | |
348 | uint16_t vol[2]; | |
349 | ||
350 | va_start (ap, cmd); | |
351 | sw = va_arg (ap, SWVoiceIn *); | |
352 | va_end (ap); | |
353 | ||
354 | vol[0] = sw->vol.l / ((1ULL << 16) + 1); | |
355 | vol[1] = sw->vol.r / ((1ULL << 16) + 1); | |
356 | spice_server_record_set_volume (&in->sin, 2, vol); | |
357 | spice_server_record_set_mute (&in->sin, sw->vol.mute); | |
358 | #endif | |
359 | break; | |
360 | } | |
3e313753 | 361 | } |
a70c99c6 | 362 | |
3e313753 GH |
363 | return 0; |
364 | } | |
365 | ||
3e313753 GH |
366 | static struct audio_pcm_ops audio_callbacks = { |
367 | .init_out = line_out_init, | |
368 | .fini_out = line_out_fini, | |
369 | .run_out = line_out_run, | |
3e313753 GH |
370 | .ctl_out = line_out_ctl, |
371 | ||
372 | .init_in = line_in_init, | |
373 | .fini_in = line_in_fini, | |
374 | .run_in = line_in_run, | |
3e313753 GH |
375 | .ctl_in = line_in_ctl, |
376 | }; | |
377 | ||
d3893a39 | 378 | static struct audio_driver spice_audio_driver = { |
3e313753 GH |
379 | .name = "spice", |
380 | .descr = "spice audio driver", | |
3e313753 GH |
381 | .init = spice_audio_init, |
382 | .fini = spice_audio_fini, | |
383 | .pcm_ops = &audio_callbacks, | |
384 | .max_voices_out = 1, | |
385 | .max_voices_in = 1, | |
386 | .voice_size_out = sizeof (SpiceVoiceOut), | |
387 | .voice_size_in = sizeof (SpiceVoiceIn), | |
a70c99c6 MAL |
388 | #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2)) |
389 | .ctl_caps = VOICE_VOLUME_CAP | |
390 | #endif | |
3e313753 GH |
391 | }; |
392 | ||
393 | void qemu_spice_audio_init (void) | |
394 | { | |
395 | spice_audio_driver.can_be_default = 1; | |
396 | } | |
d3893a39 GH |
397 | |
398 | static void register_audio_spice(void) | |
399 | { | |
400 | audio_driver_register(&spice_audio_driver); | |
401 | } | |
402 | type_init(register_audio_spice); |