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 SpiceVoiceOut
{
45 SpicePlaybackInstance sin
;
53 typedef struct SpiceVoiceIn
{
55 SpiceRecordInstance sin
;
60 static const SpicePlaybackInterface playback_sif
= {
61 .base
.type
= SPICE_INTERFACE_PLAYBACK
,
62 .base
.description
= "playback",
63 .base
.major_version
= SPICE_INTERFACE_PLAYBACK_MAJOR
,
64 .base
.minor_version
= SPICE_INTERFACE_PLAYBACK_MINOR
,
67 static const SpiceRecordInterface record_sif
= {
68 .base
.type
= SPICE_INTERFACE_RECORD
,
69 .base
.description
= "record",
70 .base
.major_version
= SPICE_INTERFACE_RECORD_MAJOR
,
71 .base
.minor_version
= SPICE_INTERFACE_RECORD_MINOR
,
74 static void *spice_audio_init(Audiodev
*dev
)
79 return &spice_audio_init
;
82 static void spice_audio_fini (void *opaque
)
89 static int line_out_init(HWVoiceOut
*hw
, struct audsettings
*as
,
92 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
93 struct audsettings settings
;
95 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
96 settings
.freq
= spice_server_get_best_playback_rate(NULL
);
98 settings
.freq
= SPICE_INTERFACE_PLAYBACK_FREQ
;
100 settings
.nchannels
= SPICE_INTERFACE_PLAYBACK_CHAN
;
101 settings
.fmt
= AUDIO_FORMAT_S16
;
102 settings
.endianness
= AUDIO_HOST_ENDIANNESS
;
104 audio_pcm_init_info (&hw
->info
, &settings
);
105 hw
->samples
= LINE_OUT_SAMPLES
;
108 out
->sin
.base
.sif
= &playback_sif
.base
;
109 qemu_spice
.add_interface(&out
->sin
.base
);
110 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
111 spice_server_set_playback_rate(&out
->sin
, settings
.freq
);
116 static void line_out_fini (HWVoiceOut
*hw
)
118 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
120 spice_server_remove_interface (&out
->sin
.base
);
123 static void *line_out_get_buffer(HWVoiceOut
*hw
, size_t *size
)
125 SpiceVoiceOut
*out
= container_of(hw
, SpiceVoiceOut
, hw
);
128 spice_server_playback_get_buffer(&out
->sin
, &out
->frame
, &out
->fsize
);
133 *size
= MIN((out
->fsize
- out
->fpos
) << 2, *size
);
136 *size
= audio_rate_get_bytes(&hw
->info
, &out
->rate
, *size
);
138 return out
->frame
+ out
->fpos
;
141 static size_t line_out_put_buffer(HWVoiceOut
*hw
, void *buf
, size_t size
)
143 SpiceVoiceOut
*out
= container_of(hw
, SpiceVoiceOut
, hw
);
146 assert(buf
== out
->frame
+ out
->fpos
&& out
->fpos
<= out
->fsize
);
147 out
->fpos
+= size
>> 2;
149 if (out
->fpos
== out
->fsize
) { /* buffer full */
150 spice_server_playback_put_samples(&out
->sin
, out
->frame
);
158 static void line_out_enable(HWVoiceOut
*hw
, bool enable
)
160 SpiceVoiceOut
*out
= container_of (hw
, SpiceVoiceOut
, hw
);
167 audio_rate_start(&out
->rate
);
168 spice_server_playback_start (&out
->sin
);
175 memset(out
->frame
+ out
->fpos
, 0, (out
->fsize
- out
->fpos
) << 2);
176 spice_server_playback_put_samples (&out
->sin
, out
->frame
);
179 spice_server_playback_stop (&out
->sin
);
183 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
184 static void line_out_volume(HWVoiceOut
*hw
, Volume
*vol
)
186 SpiceVoiceOut
*out
= container_of(hw
, SpiceVoiceOut
, hw
);
189 assert(vol
->channels
== 2);
190 svol
[0] = vol
->vol
[0] * 257;
191 svol
[1] = vol
->vol
[1] * 257;
192 spice_server_playback_set_volume(&out
->sin
, 2, svol
);
193 spice_server_playback_set_mute(&out
->sin
, vol
->mute
);
199 static int line_in_init(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
201 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
202 struct audsettings settings
;
204 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
205 settings
.freq
= spice_server_get_best_record_rate(NULL
);
207 settings
.freq
= SPICE_INTERFACE_RECORD_FREQ
;
209 settings
.nchannels
= SPICE_INTERFACE_RECORD_CHAN
;
210 settings
.fmt
= AUDIO_FORMAT_S16
;
211 settings
.endianness
= AUDIO_HOST_ENDIANNESS
;
213 audio_pcm_init_info (&hw
->info
, &settings
);
214 hw
->samples
= LINE_IN_SAMPLES
;
217 in
->sin
.base
.sif
= &record_sif
.base
;
218 qemu_spice
.add_interface(&in
->sin
.base
);
219 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
220 spice_server_set_record_rate(&in
->sin
, settings
.freq
);
225 static void line_in_fini (HWVoiceIn
*hw
)
227 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
229 spice_server_remove_interface (&in
->sin
.base
);
232 static size_t line_in_read(HWVoiceIn
*hw
, void *buf
, size_t len
)
234 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
235 uint64_t to_read
= audio_rate_get_bytes(&hw
->info
, &in
->rate
, len
) >> 2;
236 size_t ready
= spice_server_record_get_samples(&in
->sin
, buf
, to_read
);
238 /* XXX: do we need this? */
240 memset(buf
, 0, to_read
<< 2);
247 static void line_in_enable(HWVoiceIn
*hw
, bool enable
)
249 SpiceVoiceIn
*in
= container_of (hw
, SpiceVoiceIn
, hw
);
256 audio_rate_start(&in
->rate
);
257 spice_server_record_start (&in
->sin
);
263 spice_server_record_stop (&in
->sin
);
267 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
268 static void line_in_volume(HWVoiceIn
*hw
, Volume
*vol
)
270 SpiceVoiceIn
*in
= container_of(hw
, SpiceVoiceIn
, hw
);
273 assert(vol
->channels
== 2);
274 svol
[0] = vol
->vol
[0] * 257;
275 svol
[1] = vol
->vol
[1] * 257;
276 spice_server_record_set_volume(&in
->sin
, 2, svol
);
277 spice_server_record_set_mute(&in
->sin
, vol
->mute
);
281 static struct audio_pcm_ops audio_callbacks
= {
282 .init_out
= line_out_init
,
283 .fini_out
= line_out_fini
,
284 .write
= audio_generic_write
,
285 .get_buffer_out
= line_out_get_buffer
,
286 .put_buffer_out
= line_out_put_buffer
,
287 .enable_out
= line_out_enable
,
288 #if (SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && \
289 (SPICE_INTERFACE_PLAYBACK_MINOR >= 2)
290 .volume_out
= line_out_volume
,
293 .init_in
= line_in_init
,
294 .fini_in
= line_in_fini
,
295 .read
= line_in_read
,
296 .run_buffer_in
= audio_generic_run_buffer_in
,
297 .enable_in
= line_in_enable
,
298 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
299 .volume_in
= line_in_volume
,
303 static struct audio_driver spice_audio_driver
= {
305 .descr
= "spice audio driver",
306 .init
= spice_audio_init
,
307 .fini
= spice_audio_fini
,
308 .pcm_ops
= &audio_callbacks
,
311 .voice_size_out
= sizeof (SpiceVoiceOut
),
312 .voice_size_in
= sizeof (SpiceVoiceIn
),
315 static void register_audio_spice(void)
317 audio_driver_register(&spice_audio_driver
);
319 type_init(register_audio_spice
);
321 module_dep("ui-spice-core");