target-ppc: Include missing MMU models for SDR1 in info registers
[qemu/kevin.git] / audio / spiceaudio.c
blob297fd416ed4bbbdbf0d065bc2324435de95bc0b7
1 /*
2 * Copyright (C) 2010 Red Hat, Inc.
4 * maintained by Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "hw/hw.h"
22 #include "qemu/error-report.h"
23 #include "qemu/timer.h"
24 #include "ui/qemu-spice.h"
26 #define AUDIO_CAP "spice"
27 #include "audio.h"
28 #include "audio_int.h"
30 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
31 #define LINE_OUT_SAMPLES (480 * 4)
32 #else
33 #define LINE_OUT_SAMPLES (256 * 4)
34 #endif
36 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
37 #define LINE_IN_SAMPLES (480 * 4)
38 #else
39 #define LINE_IN_SAMPLES (256 * 4)
40 #endif
42 typedef struct SpiceRateCtl {
43 int64_t start_ticks;
44 int64_t bytes_sent;
45 } SpiceRateCtl;
47 typedef struct SpiceVoiceOut {
48 HWVoiceOut hw;
49 SpicePlaybackInstance sin;
50 SpiceRateCtl rate;
51 int active;
52 uint32_t *frame;
53 uint32_t *fpos;
54 uint32_t fsize;
55 } SpiceVoiceOut;
57 typedef struct SpiceVoiceIn {
58 HWVoiceIn hw;
59 SpiceRecordInstance sin;
60 SpiceRateCtl rate;
61 int active;
62 uint32_t samples[LINE_IN_SAMPLES];
63 } SpiceVoiceIn;
65 static const SpicePlaybackInterface playback_sif = {
66 .base.type = SPICE_INTERFACE_PLAYBACK,
67 .base.description = "playback",
68 .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
69 .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
72 static const SpiceRecordInterface record_sif = {
73 .base.type = SPICE_INTERFACE_RECORD,
74 .base.description = "record",
75 .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
76 .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
79 static void *spice_audio_init (void)
81 if (!using_spice) {
82 return NULL;
84 return &spice_audio_init;
87 static void spice_audio_fini (void *opaque)
89 /* nothing */
92 static void rate_start (SpiceRateCtl *rate)
94 memset (rate, 0, sizeof (*rate));
95 rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
98 static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
100 int64_t now;
101 int64_t ticks;
102 int64_t bytes;
103 int64_t samples;
105 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
106 ticks = now - rate->start_ticks;
107 bytes = muldiv64 (ticks, info->bytes_per_second, get_ticks_per_sec ());
108 samples = (bytes - rate->bytes_sent) >> info->shift;
109 if (samples < 0 || samples > 65536) {
110 error_report("Resetting rate control (%" PRId64 " samples)", samples);
111 rate_start (rate);
112 samples = 0;
114 rate->bytes_sent += samples << info->shift;
115 return samples;
118 /* playback */
120 static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
121 void *drv_opaque)
123 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
124 struct audsettings settings;
126 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
127 settings.freq = spice_server_get_best_playback_rate(NULL);
128 #else
129 settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
130 #endif
131 settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
132 settings.fmt = AUD_FMT_S16;
133 settings.endianness = AUDIO_HOST_ENDIANNESS;
135 audio_pcm_init_info (&hw->info, &settings);
136 hw->samples = LINE_OUT_SAMPLES;
137 out->active = 0;
139 out->sin.base.sif = &playback_sif.base;
140 qemu_spice_add_interface (&out->sin.base);
141 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
142 spice_server_set_playback_rate(&out->sin, settings.freq);
143 #endif
144 return 0;
147 static void line_out_fini (HWVoiceOut *hw)
149 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
151 spice_server_remove_interface (&out->sin.base);
154 static int line_out_run (HWVoiceOut *hw, int live)
156 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
157 int rpos, decr;
158 int samples;
160 if (!live) {
161 return 0;
164 decr = rate_get_samples (&hw->info, &out->rate);
165 decr = audio_MIN (live, decr);
167 samples = decr;
168 rpos = hw->rpos;
169 while (samples) {
170 int left_till_end_samples = hw->samples - rpos;
171 int len = audio_MIN (samples, left_till_end_samples);
173 if (!out->frame) {
174 spice_server_playback_get_buffer (&out->sin, &out->frame, &out->fsize);
175 out->fpos = out->frame;
177 if (out->frame) {
178 len = audio_MIN (len, out->fsize);
179 hw->clip (out->fpos, hw->mix_buf + rpos, len);
180 out->fsize -= len;
181 out->fpos += len;
182 if (out->fsize == 0) {
183 spice_server_playback_put_samples (&out->sin, out->frame);
184 out->frame = out->fpos = NULL;
187 rpos = (rpos + len) % hw->samples;
188 samples -= len;
190 hw->rpos = rpos;
191 return decr;
194 static int line_out_write (SWVoiceOut *sw, void *buf, int len)
196 return audio_pcm_sw_write (sw, buf, len);
199 static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
201 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
203 switch (cmd) {
204 case VOICE_ENABLE:
205 if (out->active) {
206 break;
208 out->active = 1;
209 rate_start (&out->rate);
210 spice_server_playback_start (&out->sin);
211 break;
212 case VOICE_DISABLE:
213 if (!out->active) {
214 break;
216 out->active = 0;
217 if (out->frame) {
218 memset (out->fpos, 0, out->fsize << 2);
219 spice_server_playback_put_samples (&out->sin, out->frame);
220 out->frame = out->fpos = NULL;
222 spice_server_playback_stop (&out->sin);
223 break;
224 case VOICE_VOLUME:
226 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
227 SWVoiceOut *sw;
228 va_list ap;
229 uint16_t vol[2];
231 va_start (ap, cmd);
232 sw = va_arg (ap, SWVoiceOut *);
233 va_end (ap);
235 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
236 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
237 spice_server_playback_set_volume (&out->sin, 2, vol);
238 spice_server_playback_set_mute (&out->sin, sw->vol.mute);
239 #endif
240 break;
244 return 0;
247 /* record */
249 static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
251 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
252 struct audsettings settings;
254 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
255 settings.freq = spice_server_get_best_record_rate(NULL);
256 #else
257 settings.freq = SPICE_INTERFACE_RECORD_FREQ;
258 #endif
259 settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
260 settings.fmt = AUD_FMT_S16;
261 settings.endianness = AUDIO_HOST_ENDIANNESS;
263 audio_pcm_init_info (&hw->info, &settings);
264 hw->samples = LINE_IN_SAMPLES;
265 in->active = 0;
267 in->sin.base.sif = &record_sif.base;
268 qemu_spice_add_interface (&in->sin.base);
269 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
270 spice_server_set_record_rate(&in->sin, settings.freq);
271 #endif
272 return 0;
275 static void line_in_fini (HWVoiceIn *hw)
277 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
279 spice_server_remove_interface (&in->sin.base);
282 static int line_in_run (HWVoiceIn *hw)
284 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
285 int num_samples;
286 int ready;
287 int len[2];
288 uint64_t delta_samp;
289 const uint32_t *samples;
291 if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) {
292 return 0;
295 delta_samp = rate_get_samples (&hw->info, &in->rate);
296 num_samples = audio_MIN (num_samples, delta_samp);
298 ready = spice_server_record_get_samples (&in->sin, in->samples, num_samples);
299 samples = in->samples;
300 if (ready == 0) {
301 static const uint32_t silence[LINE_IN_SAMPLES];
302 samples = silence;
303 ready = LINE_IN_SAMPLES;
306 num_samples = audio_MIN (ready, num_samples);
308 if (hw->wpos + num_samples > hw->samples) {
309 len[0] = hw->samples - hw->wpos;
310 len[1] = num_samples - len[0];
311 } else {
312 len[0] = num_samples;
313 len[1] = 0;
316 hw->conv (hw->conv_buf + hw->wpos, samples, len[0]);
318 if (len[1]) {
319 hw->conv (hw->conv_buf, samples + len[0], len[1]);
322 hw->wpos = (hw->wpos + num_samples) % hw->samples;
324 return num_samples;
327 static int line_in_read (SWVoiceIn *sw, void *buf, int size)
329 return audio_pcm_sw_read (sw, buf, size);
332 static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
334 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
336 switch (cmd) {
337 case VOICE_ENABLE:
338 if (in->active) {
339 break;
341 in->active = 1;
342 rate_start (&in->rate);
343 spice_server_record_start (&in->sin);
344 break;
345 case VOICE_DISABLE:
346 if (!in->active) {
347 break;
349 in->active = 0;
350 spice_server_record_stop (&in->sin);
351 break;
352 case VOICE_VOLUME:
354 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
355 SWVoiceIn *sw;
356 va_list ap;
357 uint16_t vol[2];
359 va_start (ap, cmd);
360 sw = va_arg (ap, SWVoiceIn *);
361 va_end (ap);
363 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
364 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
365 spice_server_record_set_volume (&in->sin, 2, vol);
366 spice_server_record_set_mute (&in->sin, sw->vol.mute);
367 #endif
368 break;
372 return 0;
375 static struct audio_option audio_options[] = {
376 { /* end of list */ },
379 static struct audio_pcm_ops audio_callbacks = {
380 .init_out = line_out_init,
381 .fini_out = line_out_fini,
382 .run_out = line_out_run,
383 .write = line_out_write,
384 .ctl_out = line_out_ctl,
386 .init_in = line_in_init,
387 .fini_in = line_in_fini,
388 .run_in = line_in_run,
389 .read = line_in_read,
390 .ctl_in = line_in_ctl,
393 struct audio_driver spice_audio_driver = {
394 .name = "spice",
395 .descr = "spice audio driver",
396 .options = audio_options,
397 .init = spice_audio_init,
398 .fini = spice_audio_fini,
399 .pcm_ops = &audio_callbacks,
400 .max_voices_out = 1,
401 .max_voices_in = 1,
402 .voice_size_out = sizeof (SpiceVoiceOut),
403 .voice_size_in = sizeof (SpiceVoiceIn),
404 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
405 .ctl_caps = VOICE_VOLUME_CAP
406 #endif
409 void qemu_spice_audio_init (void)
411 spice_audio_driver.can_be_default = 1;