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/error-report.h"
22 #include "qemu/timer.h"
23 #include "ui/qemu-spice.h"
25 #define AUDIO_CAP "spice"
27 #include "audio_int.h"
29 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
30 #define LINE_OUT_SAMPLES (480 * 4)
32 #define LINE_OUT_SAMPLES (256 * 4)
35 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
36 #define LINE_IN_SAMPLES (480 * 4)
38 #define LINE_IN_SAMPLES (256 * 4)
41 typedef struct SpiceRateCtl
{
46 typedef struct SpiceVoiceOut
{
48 SpicePlaybackInstance sin
;
56 typedef struct SpiceVoiceIn
{
58 SpiceRecordInstance sin
;
61 uint32_t samples
[LINE_IN_SAMPLES
];
64 static const SpicePlaybackInterface playback_sif
= {
65 .base
.type
= SPICE_INTERFACE_PLAYBACK
,
66 .base
.description
= "playback",
67 .base
.major_version
= SPICE_INTERFACE_PLAYBACK_MAJOR
,
68 .base
.minor_version
= SPICE_INTERFACE_PLAYBACK_MINOR
,
71 static const SpiceRecordInterface record_sif
= {
72 .base
.type
= SPICE_INTERFACE_RECORD
,
73 .base
.description
= "record",
74 .base
.major_version
= SPICE_INTERFACE_RECORD_MAJOR
,
75 .base
.minor_version
= SPICE_INTERFACE_RECORD_MINOR
,
78 static void *spice_audio_init (void)
83 return &spice_audio_init
;
86 static void spice_audio_fini (void *opaque
)
91 static void rate_start (SpiceRateCtl
*rate
)
93 memset (rate
, 0, sizeof (*rate
));
94 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
97 static int rate_get_samples (struct audio_pcm_info
*info
, SpiceRateCtl
*rate
)
104 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
105 ticks
= now
- rate
->start_ticks
;
106 bytes
= muldiv64 (ticks
, info
->bytes_per_second
, get_ticks_per_sec ());
107 samples
= (bytes
- rate
->bytes_sent
) >> info
->shift
;
108 if (samples
< 0 || samples
> 65536) {
109 error_report("Resetting rate control (%" PRId64
" samples)", samples
);
113 rate
->bytes_sent
+= samples
<< info
->shift
;
119 static int line_out_init(HWVoiceOut
*hw
, struct audsettings
*as
,
122 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
123 struct audsettings settings
;
125 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
126 settings
.freq
= spice_server_get_best_playback_rate(NULL
);
128 settings
.freq
= SPICE_INTERFACE_PLAYBACK_FREQ
;
130 settings
.nchannels
= SPICE_INTERFACE_PLAYBACK_CHAN
;
131 settings
.fmt
= AUD_FMT_S16
;
132 settings
.endianness
= AUDIO_HOST_ENDIANNESS
;
134 audio_pcm_init_info (&hw
->info
, &settings
);
135 hw
->samples
= LINE_OUT_SAMPLES
;
138 out
->sin
.base
.sif
= &playback_sif
.base
;
139 qemu_spice_add_interface (&out
->sin
.base
);
140 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
141 spice_server_set_playback_rate(&out
->sin
, settings
.freq
);
146 static void line_out_fini (HWVoiceOut
*hw
)
148 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
150 spice_server_remove_interface (&out
->sin
.base
);
153 static int line_out_run (HWVoiceOut
*hw
, int live
)
155 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
163 decr
= rate_get_samples (&hw
->info
, &out
->rate
);
164 decr
= audio_MIN (live
, decr
);
169 int left_till_end_samples
= hw
->samples
- rpos
;
170 int len
= audio_MIN (samples
, left_till_end_samples
);
173 spice_server_playback_get_buffer (&out
->sin
, &out
->frame
, &out
->fsize
);
174 out
->fpos
= out
->frame
;
177 len
= audio_MIN (len
, out
->fsize
);
178 hw
->clip (out
->fpos
, hw
->mix_buf
+ rpos
, len
);
181 if (out
->fsize
== 0) {
182 spice_server_playback_put_samples (&out
->sin
, out
->frame
);
183 out
->frame
= out
->fpos
= NULL
;
186 rpos
= (rpos
+ len
) % hw
->samples
;
193 static int line_out_write (SWVoiceOut
*sw
, void *buf
, int len
)
195 return audio_pcm_sw_write (sw
, buf
, len
);
198 static int line_out_ctl (HWVoiceOut
*hw
, int cmd
, ...)
200 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
208 rate_start (&out
->rate
);
209 spice_server_playback_start (&out
->sin
);
217 memset (out
->fpos
, 0, out
->fsize
<< 2);
218 spice_server_playback_put_samples (&out
->sin
, out
->frame
);
219 out
->frame
= out
->fpos
= NULL
;
221 spice_server_playback_stop (&out
->sin
);
225 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
231 sw
= va_arg (ap
, SWVoiceOut
*);
234 vol
[0] = sw
->vol
.l
/ ((1ULL << 16) + 1);
235 vol
[1] = sw
->vol
.r
/ ((1ULL << 16) + 1);
236 spice_server_playback_set_volume (&out
->sin
, 2, vol
);
237 spice_server_playback_set_mute (&out
->sin
, sw
->vol
.mute
);
248 static int line_in_init(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
250 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
251 struct audsettings settings
;
253 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
254 settings
.freq
= spice_server_get_best_record_rate(NULL
);
256 settings
.freq
= SPICE_INTERFACE_RECORD_FREQ
;
258 settings
.nchannels
= SPICE_INTERFACE_RECORD_CHAN
;
259 settings
.fmt
= AUD_FMT_S16
;
260 settings
.endianness
= AUDIO_HOST_ENDIANNESS
;
262 audio_pcm_init_info (&hw
->info
, &settings
);
263 hw
->samples
= LINE_IN_SAMPLES
;
266 in
->sin
.base
.sif
= &record_sif
.base
;
267 qemu_spice_add_interface (&in
->sin
.base
);
268 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
269 spice_server_set_record_rate(&in
->sin
, settings
.freq
);
274 static void line_in_fini (HWVoiceIn
*hw
)
276 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
278 spice_server_remove_interface (&in
->sin
.base
);
281 static int line_in_run (HWVoiceIn
*hw
)
283 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
288 const uint32_t *samples
;
290 if (!(num_samples
= hw
->samples
- audio_pcm_hw_get_live_in (hw
))) {
294 delta_samp
= rate_get_samples (&hw
->info
, &in
->rate
);
295 num_samples
= audio_MIN (num_samples
, delta_samp
);
297 ready
= spice_server_record_get_samples (&in
->sin
, in
->samples
, num_samples
);
298 samples
= in
->samples
;
300 static const uint32_t silence
[LINE_IN_SAMPLES
];
302 ready
= LINE_IN_SAMPLES
;
305 num_samples
= audio_MIN (ready
, num_samples
);
307 if (hw
->wpos
+ num_samples
> hw
->samples
) {
308 len
[0] = hw
->samples
- hw
->wpos
;
309 len
[1] = num_samples
- len
[0];
311 len
[0] = num_samples
;
315 hw
->conv (hw
->conv_buf
+ hw
->wpos
, samples
, len
[0]);
318 hw
->conv (hw
->conv_buf
, samples
+ len
[0], len
[1]);
321 hw
->wpos
= (hw
->wpos
+ num_samples
) % hw
->samples
;
326 static int line_in_read (SWVoiceIn
*sw
, void *buf
, int size
)
328 return audio_pcm_sw_read (sw
, buf
, size
);
331 static int line_in_ctl (HWVoiceIn
*hw
, int cmd
, ...)
333 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
341 rate_start (&in
->rate
);
342 spice_server_record_start (&in
->sin
);
349 spice_server_record_stop (&in
->sin
);
353 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
359 sw
= va_arg (ap
, SWVoiceIn
*);
362 vol
[0] = sw
->vol
.l
/ ((1ULL << 16) + 1);
363 vol
[1] = sw
->vol
.r
/ ((1ULL << 16) + 1);
364 spice_server_record_set_volume (&in
->sin
, 2, vol
);
365 spice_server_record_set_mute (&in
->sin
, sw
->vol
.mute
);
374 static struct audio_option audio_options
[] = {
375 { /* end of list */ },
378 static struct audio_pcm_ops audio_callbacks
= {
379 .init_out
= line_out_init
,
380 .fini_out
= line_out_fini
,
381 .run_out
= line_out_run
,
382 .write
= line_out_write
,
383 .ctl_out
= line_out_ctl
,
385 .init_in
= line_in_init
,
386 .fini_in
= line_in_fini
,
387 .run_in
= line_in_run
,
388 .read
= line_in_read
,
389 .ctl_in
= line_in_ctl
,
392 struct audio_driver spice_audio_driver
= {
394 .descr
= "spice audio driver",
395 .options
= audio_options
,
396 .init
= spice_audio_init
,
397 .fini
= spice_audio_fini
,
398 .pcm_ops
= &audio_callbacks
,
401 .voice_size_out
= sizeof (SpiceVoiceOut
),
402 .voice_size_in
= sizeof (SpiceVoiceIn
),
403 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
404 .ctl_caps
= VOICE_VOLUME_CAP
408 void qemu_spice_audio_init (void)
410 spice_audio_driver
.can_be_default
= 1;