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/error-report.h"
23 #include "qemu/timer.h"
24 #include "ui/qemu-spice.h"
26 #define AUDIO_CAP "spice"
28 #include "audio_int.h"
30 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
31 #define LINE_OUT_SAMPLES (480 * 4)
33 #define LINE_OUT_SAMPLES (256 * 4)
36 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
37 #define LINE_IN_SAMPLES (480 * 4)
39 #define LINE_IN_SAMPLES (256 * 4)
42 typedef struct SpiceRateCtl
{
47 typedef struct SpiceVoiceOut
{
49 SpicePlaybackInstance sin
;
57 typedef struct SpiceVoiceIn
{
59 SpiceRecordInstance sin
;
62 uint32_t samples
[LINE_IN_SAMPLES
];
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)
84 return &spice_audio_init
;
87 static void spice_audio_fini (void *opaque
)
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
)
105 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
106 ticks
= now
- rate
->start_ticks
;
107 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
108 samples
= (bytes
- rate
->bytes_sent
) >> info
->shift
;
109 if (samples
< 0 || samples
> 65536) {
110 error_report("Resetting rate control (%" PRId64
" samples)", samples
);
114 rate
->bytes_sent
+= samples
<< info
->shift
;
120 static int line_out_init(HWVoiceOut
*hw
, struct audsettings
*as
,
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
);
129 settings
.freq
= SPICE_INTERFACE_PLAYBACK_FREQ
;
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
;
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
);
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
);
164 decr
= rate_get_samples (&hw
->info
, &out
->rate
);
165 decr
= audio_MIN (live
, decr
);
170 int left_till_end_samples
= hw
->samples
- rpos
;
171 int len
= audio_MIN (samples
, left_till_end_samples
);
174 spice_server_playback_get_buffer (&out
->sin
, &out
->frame
, &out
->fsize
);
175 out
->fpos
= out
->frame
;
178 len
= audio_MIN (len
, out
->fsize
);
179 hw
->clip (out
->fpos
, hw
->mix_buf
+ rpos
, 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
;
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
);
209 rate_start (&out
->rate
);
210 spice_server_playback_start (&out
->sin
);
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
);
226 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
232 sw
= va_arg (ap
, SWVoiceOut
*);
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
);
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
);
257 settings
.freq
= SPICE_INTERFACE_RECORD_FREQ
;
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
;
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
);
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
);
289 const uint32_t *samples
;
291 if (!(num_samples
= hw
->samples
- audio_pcm_hw_get_live_in (hw
))) {
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
;
301 static const uint32_t silence
[LINE_IN_SAMPLES
];
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];
312 len
[0] = num_samples
;
316 hw
->conv (hw
->conv_buf
+ hw
->wpos
, samples
, len
[0]);
319 hw
->conv (hw
->conv_buf
, samples
+ len
[0], len
[1]);
322 hw
->wpos
= (hw
->wpos
+ num_samples
) % hw
->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
);
342 rate_start (&in
->rate
);
343 spice_server_record_start (&in
->sin
);
350 spice_server_record_stop (&in
->sin
);
354 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
360 sw
= va_arg (ap
, SWVoiceIn
*);
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
);
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
= {
395 .descr
= "spice audio driver",
396 .options
= audio_options
,
397 .init
= spice_audio_init
,
398 .fini
= spice_audio_fini
,
399 .pcm_ops
= &audio_callbacks
,
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
409 void qemu_spice_audio_init (void)
411 spice_audio_driver
.can_be_default
= 1;