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