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