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/>.
21 #include "qemu/timer.h"
22 #include "ui/qemu-spice.h"
24 #define AUDIO_CAP "spice"
26 #include "audio_int.h"
28 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
29 #define LINE_OUT_SAMPLES (480 * 4)
31 #define LINE_OUT_SAMPLES (256 * 4)
34 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
35 #define LINE_IN_SAMPLES (480 * 4)
37 #define LINE_IN_SAMPLES (256 * 4)
40 typedef struct SpiceRateCtl
{
45 typedef struct SpiceVoiceOut
{
47 SpicePlaybackInstance sin
;
55 typedef struct SpiceVoiceIn
{
57 SpiceRecordInstance sin
;
60 uint32_t samples
[LINE_IN_SAMPLES
];
63 static 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
,
70 static 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
,
77 static void *spice_audio_init (void)
82 return &spice_audio_init
;
85 static void spice_audio_fini (void *opaque
)
90 static void rate_start (SpiceRateCtl
*rate
)
92 memset (rate
, 0, sizeof (*rate
));
93 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
96 static int rate_get_samples (struct audio_pcm_info
*info
, SpiceRateCtl
*rate
)
103 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
104 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) {
108 error_report("Resetting rate control (%" PRId64
" samples)", samples
);
112 rate
->bytes_sent
+= samples
<< info
->shift
;
118 static int line_out_init (HWVoiceOut
*hw
, struct audsettings
*as
)
120 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
121 struct audsettings settings
;
123 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
124 settings
.freq
= spice_server_get_best_playback_rate(NULL
);
126 settings
.freq
= SPICE_INTERFACE_PLAYBACK_FREQ
;
128 settings
.nchannels
= SPICE_INTERFACE_PLAYBACK_CHAN
;
129 settings
.fmt
= AUD_FMT_S16
;
130 settings
.endianness
= AUDIO_HOST_ENDIANNESS
;
132 audio_pcm_init_info (&hw
->info
, &settings
);
133 hw
->samples
= LINE_OUT_SAMPLES
;
136 out
->sin
.base
.sif
= &playback_sif
.base
;
137 qemu_spice_add_interface (&out
->sin
.base
);
138 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
139 spice_server_set_playback_rate(&out
->sin
, settings
.freq
);
144 static void line_out_fini (HWVoiceOut
*hw
)
146 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
148 spice_server_remove_interface (&out
->sin
.base
);
151 static int line_out_run (HWVoiceOut
*hw
, int live
)
153 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
161 decr
= rate_get_samples (&hw
->info
, &out
->rate
);
162 decr
= audio_MIN (live
, decr
);
167 int left_till_end_samples
= hw
->samples
- rpos
;
168 int len
= audio_MIN (samples
, left_till_end_samples
);
171 spice_server_playback_get_buffer (&out
->sin
, &out
->frame
, &out
->fsize
);
172 out
->fpos
= out
->frame
;
175 len
= audio_MIN (len
, out
->fsize
);
176 hw
->clip (out
->fpos
, hw
->mix_buf
+ rpos
, len
);
179 if (out
->fsize
== 0) {
180 spice_server_playback_put_samples (&out
->sin
, out
->frame
);
181 out
->frame
= out
->fpos
= NULL
;
184 rpos
= (rpos
+ len
) % hw
->samples
;
191 static int line_out_write (SWVoiceOut
*sw
, void *buf
, int len
)
193 return audio_pcm_sw_write (sw
, buf
, len
);
196 static int line_out_ctl (HWVoiceOut
*hw
, int cmd
, ...)
198 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
206 rate_start (&out
->rate
);
207 spice_server_playback_start (&out
->sin
);
215 memset (out
->fpos
, 0, out
->fsize
<< 2);
216 spice_server_playback_put_samples (&out
->sin
, out
->frame
);
217 out
->frame
= out
->fpos
= NULL
;
219 spice_server_playback_stop (&out
->sin
);
223 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
229 sw
= va_arg (ap
, SWVoiceOut
*);
232 vol
[0] = sw
->vol
.l
/ ((1ULL << 16) + 1);
233 vol
[1] = sw
->vol
.r
/ ((1ULL << 16) + 1);
234 spice_server_playback_set_volume (&out
->sin
, 2, vol
);
235 spice_server_playback_set_mute (&out
->sin
, sw
->vol
.mute
);
246 static int line_in_init (HWVoiceIn
*hw
, struct audsettings
*as
)
248 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
249 struct audsettings settings
;
251 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
252 settings
.freq
= spice_server_get_best_record_rate(NULL
);
254 settings
.freq
= SPICE_INTERFACE_RECORD_FREQ
;
256 settings
.nchannels
= SPICE_INTERFACE_RECORD_CHAN
;
257 settings
.fmt
= AUD_FMT_S16
;
258 settings
.endianness
= AUDIO_HOST_ENDIANNESS
;
260 audio_pcm_init_info (&hw
->info
, &settings
);
261 hw
->samples
= LINE_IN_SAMPLES
;
264 in
->sin
.base
.sif
= &record_sif
.base
;
265 qemu_spice_add_interface (&in
->sin
.base
);
266 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
267 spice_server_set_record_rate(&in
->sin
, settings
.freq
);
272 static void line_in_fini (HWVoiceIn
*hw
)
274 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
276 spice_server_remove_interface (&in
->sin
.base
);
279 static int line_in_run (HWVoiceIn
*hw
)
281 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
286 const uint32_t *samples
;
288 if (!(num_samples
= hw
->samples
- audio_pcm_hw_get_live_in (hw
))) {
292 delta_samp
= rate_get_samples (&hw
->info
, &in
->rate
);
293 num_samples
= audio_MIN (num_samples
, delta_samp
);
295 ready
= spice_server_record_get_samples (&in
->sin
, in
->samples
, num_samples
);
296 samples
= in
->samples
;
298 static const uint32_t silence
[LINE_IN_SAMPLES
];
300 ready
= LINE_IN_SAMPLES
;
303 num_samples
= audio_MIN (ready
, num_samples
);
305 if (hw
->wpos
+ num_samples
> hw
->samples
) {
306 len
[0] = hw
->samples
- hw
->wpos
;
307 len
[1] = num_samples
- len
[0];
309 len
[0] = num_samples
;
313 hw
->conv (hw
->conv_buf
+ hw
->wpos
, samples
, len
[0]);
316 hw
->conv (hw
->conv_buf
, samples
+ len
[0], len
[1]);
319 hw
->wpos
= (hw
->wpos
+ num_samples
) % hw
->samples
;
324 static int line_in_read (SWVoiceIn
*sw
, void *buf
, int size
)
326 return audio_pcm_sw_read (sw
, buf
, size
);
329 static int line_in_ctl (HWVoiceIn
*hw
, int cmd
, ...)
331 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
339 rate_start (&in
->rate
);
340 spice_server_record_start (&in
->sin
);
347 spice_server_record_stop (&in
->sin
);
351 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
357 sw
= va_arg (ap
, SWVoiceIn
*);
360 vol
[0] = sw
->vol
.l
/ ((1ULL << 16) + 1);
361 vol
[1] = sw
->vol
.r
/ ((1ULL << 16) + 1);
362 spice_server_record_set_volume (&in
->sin
, 2, vol
);
363 spice_server_record_set_mute (&in
->sin
, sw
->vol
.mute
);
372 static struct audio_option audio_options
[] = {
373 { /* end of list */ },
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 struct audio_driver spice_audio_driver
= {
392 .descr
= "spice audio driver",
393 .options
= audio_options
,
394 .init
= spice_audio_init
,
395 .fini
= spice_audio_fini
,
396 .pcm_ops
= &audio_callbacks
,
399 .voice_size_out
= sizeof (SpiceVoiceOut
),
400 .voice_size_in
= sizeof (SpiceVoiceIn
),
401 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
402 .ctl_caps
= VOICE_VOLUME_CAP
406 void qemu_spice_audio_init (void)
408 spice_audio_driver
.can_be_default
= 1;