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 "qemu/host-utils.h"
22 #include "qemu/module.h"
23 #include "qemu/error-report.h"
24 #include "qemu/timer.h"
25 #include "ui/qemu-spice.h"
27 #define AUDIO_CAP "spice"
29 #include "audio_int.h"
31 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
32 #define LINE_OUT_SAMPLES (480 * 4)
34 #define LINE_OUT_SAMPLES (256 * 4)
37 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
38 #define LINE_IN_SAMPLES (480 * 4)
40 #define LINE_IN_SAMPLES (256 * 4)
43 typedef struct SpiceRateCtl
{
48 typedef struct SpiceVoiceOut
{
50 SpicePlaybackInstance sin
;
58 typedef struct SpiceVoiceIn
{
60 SpiceRecordInstance sin
;
63 uint32_t samples
[LINE_IN_SAMPLES
];
66 static const SpicePlaybackInterface playback_sif
= {
67 .base
.type
= SPICE_INTERFACE_PLAYBACK
,
68 .base
.description
= "playback",
69 .base
.major_version
= SPICE_INTERFACE_PLAYBACK_MAJOR
,
70 .base
.minor_version
= SPICE_INTERFACE_PLAYBACK_MINOR
,
73 static const SpiceRecordInterface record_sif
= {
74 .base
.type
= SPICE_INTERFACE_RECORD
,
75 .base
.description
= "record",
76 .base
.major_version
= SPICE_INTERFACE_RECORD_MAJOR
,
77 .base
.minor_version
= SPICE_INTERFACE_RECORD_MINOR
,
80 static void *spice_audio_init(Audiodev
*dev
)
85 return &spice_audio_init
;
88 static void spice_audio_fini (void *opaque
)
93 static void rate_start (SpiceRateCtl
*rate
)
95 memset (rate
, 0, sizeof (*rate
));
96 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
99 static int rate_get_samples (struct audio_pcm_info
*info
, SpiceRateCtl
*rate
)
106 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
107 ticks
= now
- rate
->start_ticks
;
108 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
109 samples
= (bytes
- rate
->bytes_sent
) >> info
->shift
;
110 if (samples
< 0 || samples
> 65536) {
111 error_report("Resetting rate control (%" PRId64
" samples)", samples
);
115 rate
->bytes_sent
+= samples
<< info
->shift
;
121 static int line_out_init(HWVoiceOut
*hw
, struct audsettings
*as
,
124 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
125 struct audsettings settings
;
127 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
128 settings
.freq
= spice_server_get_best_playback_rate(NULL
);
130 settings
.freq
= SPICE_INTERFACE_PLAYBACK_FREQ
;
132 settings
.nchannels
= SPICE_INTERFACE_PLAYBACK_CHAN
;
133 settings
.fmt
= AUDIO_FORMAT_S16
;
134 settings
.endianness
= AUDIO_HOST_ENDIANNESS
;
136 audio_pcm_init_info (&hw
->info
, &settings
);
137 hw
->samples
= LINE_OUT_SAMPLES
;
140 out
->sin
.base
.sif
= &playback_sif
.base
;
141 qemu_spice_add_interface (&out
->sin
.base
);
142 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
143 spice_server_set_playback_rate(&out
->sin
, settings
.freq
);
148 static void line_out_fini (HWVoiceOut
*hw
)
150 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
152 spice_server_remove_interface (&out
->sin
.base
);
155 static size_t line_out_run (HWVoiceOut
*hw
, size_t live
)
157 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
165 decr
= rate_get_samples (&hw
->info
, &out
->rate
);
166 decr
= MIN (live
, decr
);
171 int left_till_end_samples
= hw
->samples
- rpos
;
172 int len
= MIN (samples
, left_till_end_samples
);
175 spice_server_playback_get_buffer (&out
->sin
, &out
->frame
, &out
->fsize
);
176 out
->fpos
= out
->frame
;
179 len
= MIN (len
, out
->fsize
);
180 hw
->clip (out
->fpos
, hw
->mix_buf
+ rpos
, len
);
183 if (out
->fsize
== 0) {
184 spice_server_playback_put_samples (&out
->sin
, out
->frame
);
185 out
->frame
= out
->fpos
= NULL
;
188 rpos
= (rpos
+ len
) % hw
->samples
;
195 static int line_out_ctl (HWVoiceOut
*hw
, int cmd
, ...)
197 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
205 rate_start (&out
->rate
);
206 spice_server_playback_start (&out
->sin
);
214 memset (out
->fpos
, 0, out
->fsize
<< 2);
215 spice_server_playback_put_samples (&out
->sin
, out
->frame
);
216 out
->frame
= out
->fpos
= NULL
;
218 spice_server_playback_stop (&out
->sin
);
222 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
228 sw
= va_arg (ap
, SWVoiceOut
*);
231 vol
[0] = sw
->vol
.l
/ ((1ULL << 16) + 1);
232 vol
[1] = sw
->vol
.r
/ ((1ULL << 16) + 1);
233 spice_server_playback_set_volume (&out
->sin
, 2, vol
);
234 spice_server_playback_set_mute (&out
->sin
, sw
->vol
.mute
);
245 static int line_in_init(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
247 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
248 struct audsettings settings
;
250 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
251 settings
.freq
= spice_server_get_best_record_rate(NULL
);
253 settings
.freq
= SPICE_INTERFACE_RECORD_FREQ
;
255 settings
.nchannels
= SPICE_INTERFACE_RECORD_CHAN
;
256 settings
.fmt
= AUDIO_FORMAT_S16
;
257 settings
.endianness
= AUDIO_HOST_ENDIANNESS
;
259 audio_pcm_init_info (&hw
->info
, &settings
);
260 hw
->samples
= LINE_IN_SAMPLES
;
263 in
->sin
.base
.sif
= &record_sif
.base
;
264 qemu_spice_add_interface (&in
->sin
.base
);
265 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
266 spice_server_set_record_rate(&in
->sin
, settings
.freq
);
271 static void line_in_fini (HWVoiceIn
*hw
)
273 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
275 spice_server_remove_interface (&in
->sin
.base
);
278 static size_t line_in_run(HWVoiceIn
*hw
)
280 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
285 const uint32_t *samples
;
287 if (!(num_samples
= hw
->samples
- audio_pcm_hw_get_live_in (hw
))) {
291 delta_samp
= rate_get_samples (&hw
->info
, &in
->rate
);
292 num_samples
= MIN (num_samples
, delta_samp
);
294 ready
= spice_server_record_get_samples (&in
->sin
, in
->samples
, num_samples
);
295 samples
= in
->samples
;
297 static const uint32_t silence
[LINE_IN_SAMPLES
];
299 ready
= LINE_IN_SAMPLES
;
302 num_samples
= MIN (ready
, num_samples
);
304 if (hw
->wpos
+ num_samples
> hw
->samples
) {
305 len
[0] = hw
->samples
- hw
->wpos
;
306 len
[1] = num_samples
- len
[0];
308 len
[0] = num_samples
;
312 hw
->conv (hw
->conv_buf
+ hw
->wpos
, samples
, len
[0]);
315 hw
->conv (hw
->conv_buf
, samples
+ len
[0], len
[1]);
318 hw
->wpos
= (hw
->wpos
+ num_samples
) % hw
->samples
;
323 static int line_in_ctl (HWVoiceIn
*hw
, int cmd
, ...)
325 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
333 rate_start (&in
->rate
);
334 spice_server_record_start (&in
->sin
);
341 spice_server_record_stop (&in
->sin
);
345 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
351 sw
= va_arg (ap
, SWVoiceIn
*);
354 vol
[0] = sw
->vol
.l
/ ((1ULL << 16) + 1);
355 vol
[1] = sw
->vol
.r
/ ((1ULL << 16) + 1);
356 spice_server_record_set_volume (&in
->sin
, 2, vol
);
357 spice_server_record_set_mute (&in
->sin
, sw
->vol
.mute
);
366 static struct audio_pcm_ops audio_callbacks
= {
367 .init_out
= line_out_init
,
368 .fini_out
= line_out_fini
,
369 .run_out
= line_out_run
,
370 .ctl_out
= line_out_ctl
,
372 .init_in
= line_in_init
,
373 .fini_in
= line_in_fini
,
374 .run_in
= line_in_run
,
375 .ctl_in
= line_in_ctl
,
378 static struct audio_driver spice_audio_driver
= {
380 .descr
= "spice audio driver",
381 .init
= spice_audio_init
,
382 .fini
= spice_audio_fini
,
383 .pcm_ops
= &audio_callbacks
,
386 .voice_size_out
= sizeof (SpiceVoiceOut
),
387 .voice_size_in
= sizeof (SpiceVoiceIn
),
388 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
389 .ctl_caps
= VOICE_VOLUME_CAP
393 void qemu_spice_audio_init (void)
395 spice_audio_driver
.can_be_default
= 1;
398 static void register_audio_spice(void)
400 audio_driver_register(&spice_audio_driver
);
402 type_init(register_audio_spice
);