Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20150715' into...
[qemu/cris-port.git] / audio / spiceaudio.c
blob42ae4a45f7bde246bfe99c15904a1a6e53ea407a
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 "hw/hw.h"
21 #include "qemu/error-report.h"
22 #include "qemu/timer.h"
23 #include "ui/qemu-spice.h"
25 #define AUDIO_CAP "spice"
26 #include "audio.h"
27 #include "audio_int.h"
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
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
41 typedef struct SpiceRateCtl {
42 int64_t start_ticks;
43 int64_t bytes_sent;
44 } SpiceRateCtl;
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;
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;
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,
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,
78 static void *spice_audio_init (void)
80 if (!using_spice) {
81 return NULL;
83 return &spice_audio_init;
86 static void spice_audio_fini (void *opaque)
88 /* nothing */
91 static void rate_start (SpiceRateCtl *rate)
93 memset (rate, 0, sizeof (*rate));
94 rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
97 static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
99 int64_t now;
100 int64_t ticks;
101 int64_t bytes;
102 int64_t samples;
104 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
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) {
109 error_report("Resetting rate control (%" PRId64 " samples)", samples);
110 rate_start (rate);
111 samples = 0;
113 rate->bytes_sent += samples << info->shift;
114 return samples;
117 /* playback */
119 static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
120 void *drv_opaque)
122 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
123 struct audsettings settings;
125 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
126 settings.freq = spice_server_get_best_playback_rate(NULL);
127 #else
128 settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
129 #endif
130 settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
131 settings.fmt = AUD_FMT_S16;
132 settings.endianness = AUDIO_HOST_ENDIANNESS;
134 audio_pcm_init_info (&hw->info, &settings);
135 hw->samples = LINE_OUT_SAMPLES;
136 out->active = 0;
138 out->sin.base.sif = &playback_sif.base;
139 qemu_spice_add_interface (&out->sin.base);
140 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
141 spice_server_set_playback_rate(&out->sin, settings.freq);
142 #endif
143 return 0;
146 static void line_out_fini (HWVoiceOut *hw)
148 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
150 spice_server_remove_interface (&out->sin.base);
153 static int line_out_run (HWVoiceOut *hw, int live)
155 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
156 int rpos, decr;
157 int samples;
159 if (!live) {
160 return 0;
163 decr = rate_get_samples (&hw->info, &out->rate);
164 decr = audio_MIN (live, decr);
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);
172 if (!out->frame) {
173 spice_server_playback_get_buffer (&out->sin, &out->frame, &out->fsize);
174 out->fpos = out->frame;
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;
186 rpos = (rpos + len) % hw->samples;
187 samples -= len;
189 hw->rpos = rpos;
190 return decr;
193 static int line_out_write (SWVoiceOut *sw, void *buf, int len)
195 return audio_pcm_sw_write (sw, buf, len);
198 static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
200 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
202 switch (cmd) {
203 case VOICE_ENABLE:
204 if (out->active) {
205 break;
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;
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;
221 spice_server_playback_stop (&out->sin);
222 break;
223 case VOICE_VOLUME:
225 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
226 SWVoiceOut *sw;
227 va_list ap;
228 uint16_t vol[2];
230 va_start (ap, cmd);
231 sw = va_arg (ap, SWVoiceOut *);
232 va_end (ap);
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;
243 return 0;
246 /* record */
248 static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
250 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
251 struct audsettings settings;
253 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
254 settings.freq = spice_server_get_best_record_rate(NULL);
255 #else
256 settings.freq = SPICE_INTERFACE_RECORD_FREQ;
257 #endif
258 settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
259 settings.fmt = AUD_FMT_S16;
260 settings.endianness = AUDIO_HOST_ENDIANNESS;
262 audio_pcm_init_info (&hw->info, &settings);
263 hw->samples = LINE_IN_SAMPLES;
264 in->active = 0;
266 in->sin.base.sif = &record_sif.base;
267 qemu_spice_add_interface (&in->sin.base);
268 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
269 spice_server_set_record_rate(&in->sin, settings.freq);
270 #endif
271 return 0;
274 static void line_in_fini (HWVoiceIn *hw)
276 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
278 spice_server_remove_interface (&in->sin.base);
281 static int line_in_run (HWVoiceIn *hw)
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;
290 if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) {
291 return 0;
294 delta_samp = rate_get_samples (&hw->info, &in->rate);
295 num_samples = audio_MIN (num_samples, delta_samp);
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;
305 num_samples = audio_MIN (ready, num_samples);
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;
315 hw->conv (hw->conv_buf + hw->wpos, samples, len[0]);
317 if (len[1]) {
318 hw->conv (hw->conv_buf, samples + len[0], len[1]);
321 hw->wpos = (hw->wpos + num_samples) % hw->samples;
323 return num_samples;
326 static int line_in_read (SWVoiceIn *sw, void *buf, int size)
328 return audio_pcm_sw_read (sw, buf, size);
331 static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
333 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
335 switch (cmd) {
336 case VOICE_ENABLE:
337 if (in->active) {
338 break;
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;
348 in->active = 0;
349 spice_server_record_stop (&in->sin);
350 break;
351 case VOICE_VOLUME:
353 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
354 SWVoiceIn *sw;
355 va_list ap;
356 uint16_t vol[2];
358 va_start (ap, cmd);
359 sw = va_arg (ap, SWVoiceIn *);
360 va_end (ap);
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;
371 return 0;
374 static struct audio_option audio_options[] = {
375 { /* end of list */ },
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,
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,
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),
403 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
404 .ctl_caps = VOICE_VOLUME_CAP
405 #endif
408 void qemu_spice_audio_init (void)
410 spice_audio_driver.can_be_default = 1;