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"
22 #include "qemu/host-utils.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 int line_out_run (HWVoiceOut
*hw
, int live
)
157 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
165 decr
= rate_get_samples (&hw
->info
, &out
->rate
);
166 decr
= audio_MIN (live
, decr
);
171 int left_till_end_samples
= hw
->samples
- rpos
;
172 int len
= audio_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
= audio_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_write (SWVoiceOut
*sw
, void *buf
, int len
)
197 return audio_pcm_sw_write (sw
, buf
, len
);
200 static int line_out_ctl (HWVoiceOut
*hw
, int cmd
, ...)
202 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
210 rate_start (&out
->rate
);
211 spice_server_playback_start (&out
->sin
);
219 memset (out
->fpos
, 0, out
->fsize
<< 2);
220 spice_server_playback_put_samples (&out
->sin
, out
->frame
);
221 out
->frame
= out
->fpos
= NULL
;
223 spice_server_playback_stop (&out
->sin
);
227 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
233 sw
= va_arg (ap
, SWVoiceOut
*);
236 vol
[0] = sw
->vol
.l
/ ((1ULL << 16) + 1);
237 vol
[1] = sw
->vol
.r
/ ((1ULL << 16) + 1);
238 spice_server_playback_set_volume (&out
->sin
, 2, vol
);
239 spice_server_playback_set_mute (&out
->sin
, sw
->vol
.mute
);
250 static int line_in_init(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
252 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
253 struct audsettings settings
;
255 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
256 settings
.freq
= spice_server_get_best_record_rate(NULL
);
258 settings
.freq
= SPICE_INTERFACE_RECORD_FREQ
;
260 settings
.nchannels
= SPICE_INTERFACE_RECORD_CHAN
;
261 settings
.fmt
= AUDIO_FORMAT_S16
;
262 settings
.endianness
= AUDIO_HOST_ENDIANNESS
;
264 audio_pcm_init_info (&hw
->info
, &settings
);
265 hw
->samples
= LINE_IN_SAMPLES
;
268 in
->sin
.base
.sif
= &record_sif
.base
;
269 qemu_spice_add_interface (&in
->sin
.base
);
270 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
271 spice_server_set_record_rate(&in
->sin
, settings
.freq
);
276 static void line_in_fini (HWVoiceIn
*hw
)
278 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
280 spice_server_remove_interface (&in
->sin
.base
);
283 static int line_in_run (HWVoiceIn
*hw
)
285 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
290 const uint32_t *samples
;
292 if (!(num_samples
= hw
->samples
- audio_pcm_hw_get_live_in (hw
))) {
296 delta_samp
= rate_get_samples (&hw
->info
, &in
->rate
);
297 num_samples
= audio_MIN (num_samples
, delta_samp
);
299 ready
= spice_server_record_get_samples (&in
->sin
, in
->samples
, num_samples
);
300 samples
= in
->samples
;
302 static const uint32_t silence
[LINE_IN_SAMPLES
];
304 ready
= LINE_IN_SAMPLES
;
307 num_samples
= audio_MIN (ready
, num_samples
);
309 if (hw
->wpos
+ num_samples
> hw
->samples
) {
310 len
[0] = hw
->samples
- hw
->wpos
;
311 len
[1] = num_samples
- len
[0];
313 len
[0] = num_samples
;
317 hw
->conv (hw
->conv_buf
+ hw
->wpos
, samples
, len
[0]);
320 hw
->conv (hw
->conv_buf
, samples
+ len
[0], len
[1]);
323 hw
->wpos
= (hw
->wpos
+ num_samples
) % hw
->samples
;
328 static int line_in_read (SWVoiceIn
*sw
, void *buf
, int size
)
330 return audio_pcm_sw_read (sw
, buf
, size
);
333 static int line_in_ctl (HWVoiceIn
*hw
, int cmd
, ...)
335 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
343 rate_start (&in
->rate
);
344 spice_server_record_start (&in
->sin
);
351 spice_server_record_stop (&in
->sin
);
355 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
361 sw
= va_arg (ap
, SWVoiceIn
*);
364 vol
[0] = sw
->vol
.l
/ ((1ULL << 16) + 1);
365 vol
[1] = sw
->vol
.r
/ ((1ULL << 16) + 1);
366 spice_server_record_set_volume (&in
->sin
, 2, vol
);
367 spice_server_record_set_mute (&in
->sin
, sw
->vol
.mute
);
376 static struct audio_pcm_ops audio_callbacks
= {
377 .init_out
= line_out_init
,
378 .fini_out
= line_out_fini
,
379 .run_out
= line_out_run
,
380 .write
= line_out_write
,
381 .ctl_out
= line_out_ctl
,
383 .init_in
= line_in_init
,
384 .fini_in
= line_in_fini
,
385 .run_in
= line_in_run
,
386 .read
= line_in_read
,
387 .ctl_in
= line_in_ctl
,
390 static struct audio_driver spice_audio_driver
= {
392 .descr
= "spice audio driver",
393 .init
= spice_audio_init
,
394 .fini
= spice_audio_fini
,
395 .pcm_ops
= &audio_callbacks
,
398 .voice_size_out
= sizeof (SpiceVoiceOut
),
399 .voice_size_in
= sizeof (SpiceVoiceIn
),
400 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
401 .ctl_caps
= VOICE_VOLUME_CAP
405 void qemu_spice_audio_init (void)
407 spice_audio_driver
.can_be_default
= 1;
410 static void register_audio_spice(void)
412 audio_driver_register(&spice_audio_driver
);
414 type_init(register_audio_spice
);