blob: 5c6f72675789938f97d06d5a9aa5faad1dc498f2 [file] [log] [blame]
Gerd Hoffmanncf2c1832010-11-11 13:07:52 +01001/*
2 * Copyright (C) 2010 Red Hat, Inc.
3 *
4 * maintained by Gerd Hoffmann <kraxel@redhat.com>
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
Gerd Hoffmann3e313752010-11-09 17:29:46 +010020#include "hw/hw.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010021#include "qemu/timer.h"
Gerd Hoffmann3e313752010-11-09 17:29:46 +010022#include "ui/qemu-spice.h"
23
24#define AUDIO_CAP "spice"
25#include "audio.h"
26#include "audio_int.h"
27
Jeremy White795ca112014-01-02 09:25:56 -060028#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
29#define LINE_OUT_SAMPLES (480 * 4)
30#else
31#define LINE_OUT_SAMPLES (256 * 4)
32#endif
33
34#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
35#define LINE_IN_SAMPLES (480 * 4)
36#else
37#define LINE_IN_SAMPLES (256 * 4)
38#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +010039
40typedef struct SpiceRateCtl {
41 int64_t start_ticks;
42 int64_t bytes_sent;
43} SpiceRateCtl;
44
45typedef struct SpiceVoiceOut {
46 HWVoiceOut hw;
47 SpicePlaybackInstance sin;
48 SpiceRateCtl rate;
49 int active;
50 uint32_t *frame;
51 uint32_t *fpos;
52 uint32_t fsize;
53} SpiceVoiceOut;
54
55typedef struct SpiceVoiceIn {
56 HWVoiceIn hw;
57 SpiceRecordInstance sin;
58 SpiceRateCtl rate;
59 int active;
60 uint32_t samples[LINE_IN_SAMPLES];
61} SpiceVoiceIn;
62
63static const SpicePlaybackInterface playback_sif = {
64 .base.type = SPICE_INTERFACE_PLAYBACK,
65 .base.description = "playback",
66 .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
67 .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
68};
69
70static const SpiceRecordInterface record_sif = {
71 .base.type = SPICE_INTERFACE_RECORD,
72 .base.description = "record",
73 .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
74 .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
75};
76
77static void *spice_audio_init (void)
78{
79 if (!using_spice) {
80 return NULL;
81 }
82 return &spice_audio_init;
83}
84
85static void spice_audio_fini (void *opaque)
86{
87 /* nothing */
88}
89
90static void rate_start (SpiceRateCtl *rate)
91{
92 memset (rate, 0, sizeof (*rate));
Alex Blighbc72ad62013-08-21 16:03:08 +010093 rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Gerd Hoffmann3e313752010-11-09 17:29:46 +010094}
95
96static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
97{
98 int64_t now;
99 int64_t ticks;
100 int64_t bytes;
101 int64_t samples;
102
Alex Blighbc72ad62013-08-21 16:03:08 +0100103 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100104 ticks = now - rate->start_ticks;
105 bytes = muldiv64 (ticks, info->bytes_per_second, get_ticks_per_sec ());
106 samples = (bytes - rate->bytes_sent) >> info->shift;
107 if (samples < 0 || samples > 65536) {
Le Tan69e99502014-05-10 07:55:20 +0800108 error_report("Resetting rate control (%" PRId64 " samples)", samples);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100109 rate_start (rate);
110 samples = 0;
111 }
112 rate->bytes_sent += samples << info->shift;
113 return samples;
114}
115
116/* playback */
117
Kővágó, Zoltán5706db12015-06-03 23:03:47 +0200118static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
119 void *drv_opaque)
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100120{
121 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
122 struct audsettings settings;
123
Jeremy White795ca112014-01-02 09:25:56 -0600124#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
125 settings.freq = spice_server_get_best_playback_rate(NULL);
126#else
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100127 settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
Jeremy White795ca112014-01-02 09:25:56 -0600128#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100129 settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
130 settings.fmt = AUD_FMT_S16;
131 settings.endianness = AUDIO_HOST_ENDIANNESS;
132
133 audio_pcm_init_info (&hw->info, &settings);
134 hw->samples = LINE_OUT_SAMPLES;
135 out->active = 0;
136
137 out->sin.base.sif = &playback_sif.base;
138 qemu_spice_add_interface (&out->sin.base);
Jeremy White795ca112014-01-02 09:25:56 -0600139#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
140 spice_server_set_playback_rate(&out->sin, settings.freq);
141#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100142 return 0;
143}
144
145static void line_out_fini (HWVoiceOut *hw)
146{
147 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
148
149 spice_server_remove_interface (&out->sin.base);
150}
151
152static int line_out_run (HWVoiceOut *hw, int live)
153{
154 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
155 int rpos, decr;
156 int samples;
157
158 if (!live) {
159 return 0;
160 }
161
162 decr = rate_get_samples (&hw->info, &out->rate);
163 decr = audio_MIN (live, decr);
164
165 samples = decr;
166 rpos = hw->rpos;
167 while (samples) {
168 int left_till_end_samples = hw->samples - rpos;
169 int len = audio_MIN (samples, left_till_end_samples);
170
171 if (!out->frame) {
172 spice_server_playback_get_buffer (&out->sin, &out->frame, &out->fsize);
173 out->fpos = out->frame;
174 }
175 if (out->frame) {
176 len = audio_MIN (len, out->fsize);
177 hw->clip (out->fpos, hw->mix_buf + rpos, len);
178 out->fsize -= len;
179 out->fpos += len;
180 if (out->fsize == 0) {
181 spice_server_playback_put_samples (&out->sin, out->frame);
182 out->frame = out->fpos = NULL;
183 }
184 }
185 rpos = (rpos + len) % hw->samples;
186 samples -= len;
187 }
188 hw->rpos = rpos;
189 return decr;
190}
191
192static int line_out_write (SWVoiceOut *sw, void *buf, int len)
193{
194 return audio_pcm_sw_write (sw, buf, len);
195}
196
197static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
198{
199 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
200
201 switch (cmd) {
202 case VOICE_ENABLE:
203 if (out->active) {
204 break;
205 }
206 out->active = 1;
207 rate_start (&out->rate);
208 spice_server_playback_start (&out->sin);
209 break;
210 case VOICE_DISABLE:
211 if (!out->active) {
212 break;
213 }
214 out->active = 0;
215 if (out->frame) {
216 memset (out->fpos, 0, out->fsize << 2);
217 spice_server_playback_put_samples (&out->sin, out->frame);
218 out->frame = out->fpos = NULL;
219 }
220 spice_server_playback_stop (&out->sin);
221 break;
Marc-André Lureaua70c99c2012-04-17 14:32:40 +0200222 case VOICE_VOLUME:
223 {
224#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
225 SWVoiceOut *sw;
226 va_list ap;
227 uint16_t vol[2];
228
229 va_start (ap, cmd);
230 sw = va_arg (ap, SWVoiceOut *);
231 va_end (ap);
232
233 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
234 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
235 spice_server_playback_set_volume (&out->sin, 2, vol);
236 spice_server_playback_set_mute (&out->sin, sw->vol.mute);
237#endif
238 break;
239 }
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100240 }
Marc-André Lureaua70c99c2012-04-17 14:32:40 +0200241
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100242 return 0;
243}
244
245/* record */
246
Kővágó, Zoltán5706db12015-06-03 23:03:47 +0200247static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100248{
249 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
250 struct audsettings settings;
251
Jeremy White795ca112014-01-02 09:25:56 -0600252#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
253 settings.freq = spice_server_get_best_record_rate(NULL);
254#else
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100255 settings.freq = SPICE_INTERFACE_RECORD_FREQ;
Jeremy White795ca112014-01-02 09:25:56 -0600256#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100257 settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
258 settings.fmt = AUD_FMT_S16;
259 settings.endianness = AUDIO_HOST_ENDIANNESS;
260
261 audio_pcm_init_info (&hw->info, &settings);
262 hw->samples = LINE_IN_SAMPLES;
263 in->active = 0;
264
265 in->sin.base.sif = &record_sif.base;
266 qemu_spice_add_interface (&in->sin.base);
Jeremy White795ca112014-01-02 09:25:56 -0600267#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
268 spice_server_set_record_rate(&in->sin, settings.freq);
269#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100270 return 0;
271}
272
273static void line_in_fini (HWVoiceIn *hw)
274{
275 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
276
277 spice_server_remove_interface (&in->sin.base);
278}
279
280static int line_in_run (HWVoiceIn *hw)
281{
282 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
283 int num_samples;
284 int ready;
285 int len[2];
286 uint64_t delta_samp;
287 const uint32_t *samples;
288
289 if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) {
290 return 0;
291 }
292
293 delta_samp = rate_get_samples (&hw->info, &in->rate);
294 num_samples = audio_MIN (num_samples, delta_samp);
295
296 ready = spice_server_record_get_samples (&in->sin, in->samples, num_samples);
297 samples = in->samples;
298 if (ready == 0) {
299 static const uint32_t silence[LINE_IN_SAMPLES];
300 samples = silence;
301 ready = LINE_IN_SAMPLES;
302 }
303
304 num_samples = audio_MIN (ready, num_samples);
305
306 if (hw->wpos + num_samples > hw->samples) {
307 len[0] = hw->samples - hw->wpos;
308 len[1] = num_samples - len[0];
309 } else {
310 len[0] = num_samples;
311 len[1] = 0;
312 }
313
Michael Walle00e07672011-01-05 01:05:47 +0100314 hw->conv (hw->conv_buf + hw->wpos, samples, len[0]);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100315
316 if (len[1]) {
Michael Walle00e07672011-01-05 01:05:47 +0100317 hw->conv (hw->conv_buf, samples + len[0], len[1]);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100318 }
319
320 hw->wpos = (hw->wpos + num_samples) % hw->samples;
321
322 return num_samples;
323}
324
325static int line_in_read (SWVoiceIn *sw, void *buf, int size)
326{
327 return audio_pcm_sw_read (sw, buf, size);
328}
329
330static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
331{
332 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
333
334 switch (cmd) {
335 case VOICE_ENABLE:
336 if (in->active) {
337 break;
338 }
339 in->active = 1;
340 rate_start (&in->rate);
341 spice_server_record_start (&in->sin);
342 break;
343 case VOICE_DISABLE:
344 if (!in->active) {
345 break;
346 }
347 in->active = 0;
348 spice_server_record_stop (&in->sin);
349 break;
Marc-André Lureaua70c99c2012-04-17 14:32:40 +0200350 case VOICE_VOLUME:
351 {
352#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
353 SWVoiceIn *sw;
354 va_list ap;
355 uint16_t vol[2];
356
357 va_start (ap, cmd);
358 sw = va_arg (ap, SWVoiceIn *);
359 va_end (ap);
360
361 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
362 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
363 spice_server_record_set_volume (&in->sin, 2, vol);
364 spice_server_record_set_mute (&in->sin, sw->vol.mute);
365#endif
366 break;
367 }
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100368 }
Marc-André Lureaua70c99c2012-04-17 14:32:40 +0200369
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100370 return 0;
371}
372
373static struct audio_option audio_options[] = {
374 { /* end of list */ },
375};
376
377static 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
391struct audio_driver spice_audio_driver = {
392 .name = "spice",
393 .descr = "spice audio driver",
394 .options = audio_options,
395 .init = spice_audio_init,
396 .fini = spice_audio_fini,
397 .pcm_ops = &audio_callbacks,
398 .max_voices_out = 1,
399 .max_voices_in = 1,
400 .voice_size_out = sizeof (SpiceVoiceOut),
401 .voice_size_in = sizeof (SpiceVoiceIn),
Marc-André Lureaua70c99c2012-04-17 14:32:40 +0200402#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
403 .ctl_caps = VOICE_VOLUME_CAP
404#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100405};
406
407void qemu_spice_audio_init (void)
408{
409 spice_audio_driver.can_be_default = 1;
410}