blob: 3f14842709bf19cce7f9ce73b99061f87bcb829e [file] [log] [blame]
bellardfb065182004-11-09 23:09:44 +00001/*
2 * QEMU Audio subsystem header
bellard1d14ffa2005-10-30 18:58:22 +00003 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 *
bellardfb065182004-11-09 23:09:44 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbruster175de522016-06-29 15:29:06 +020024
bellardfb065182004-11-09 23:09:44 +000025#ifndef QEMU_AUDIO_INT_H
26#define QEMU_AUDIO_INT_H
27
Gerd Hoffmann1ef1ec22018-03-01 11:05:46 +010028#ifdef CONFIG_AUDIO_COREAUDIO
bellard1d14ffa2005-10-30 18:58:22 +000029#define FLOAT_MIXENG
30/* #define RECIPROCAL */
31#endif
32#include "mixeng.h"
bellardfb065182004-11-09 23:09:44 +000033
bellard1d14ffa2005-10-30 18:58:22 +000034struct audio_pcm_ops;
35
bellard1d14ffa2005-10-30 18:58:22 +000036struct audio_callback {
37 void *opaque;
malccb4f03e2009-10-15 02:40:17 +040038 audio_callback_fn fn;
bellard1d14ffa2005-10-30 18:58:22 +000039};
40
41struct audio_pcm_info {
42 int bits;
43 int sign;
44 int freq;
45 int nchannels;
46 int align;
47 int shift;
48 int bytes_per_second;
bellardd929eba2006-07-04 21:47:22 +000049 int swap_endianness;
bellard1d14ffa2005-10-30 18:58:22 +000050};
51
bellardec36b692006-07-16 18:57:03 +000052typedef struct SWVoiceCap SWVoiceCap;
53
bellard1d14ffa2005-10-30 18:58:22 +000054typedef struct HWVoiceOut {
bellardfb065182004-11-09 23:09:44 +000055 int enabled;
malc713a98f2009-09-12 02:28:45 +040056 int poll_mode;
bellardfb065182004-11-09 23:09:44 +000057 int pending_disable;
bellard1d14ffa2005-10-30 18:58:22 +000058 struct audio_pcm_info info;
bellardfb065182004-11-09 23:09:44 +000059
60 f_sample *clip;
bellardfb065182004-11-09 23:09:44 +000061
62 int rpos;
bellard1d14ffa2005-10-30 18:58:22 +000063 uint64_t ts_helper;
bellardfb065182004-11-09 23:09:44 +000064
malc1ea879e2008-12-03 22:48:44 +000065 struct st_sample *mix_buf;
bellardfb065182004-11-09 23:09:44 +000066
67 int samples;
Blue Swirl72cf2d42009-09-12 07:36:22 +000068 QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
69 QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head;
Marc-André Lureauc01b2452012-04-17 14:32:36 +020070 int ctl_caps;
blueswir135f4b582008-10-06 18:08:30 +000071 struct audio_pcm_ops *pcm_ops;
Blue Swirl72cf2d42009-09-12 07:36:22 +000072 QLIST_ENTRY (HWVoiceOut) entries;
bellard1d14ffa2005-10-30 18:58:22 +000073} HWVoiceOut;
bellardfb065182004-11-09 23:09:44 +000074
bellard1d14ffa2005-10-30 18:58:22 +000075typedef struct HWVoiceIn {
76 int enabled;
malc713a98f2009-09-12 02:28:45 +040077 int poll_mode;
bellard1d14ffa2005-10-30 18:58:22 +000078 struct audio_pcm_info info;
bellardfb065182004-11-09 23:09:44 +000079
80 t_sample *conv;
81
bellard1d14ffa2005-10-30 18:58:22 +000082 int wpos;
bellard1d14ffa2005-10-30 18:58:22 +000083 int total_samples_captured;
84 uint64_t ts_helper;
85
malc1ea879e2008-12-03 22:48:44 +000086 struct st_sample *conv_buf;
bellard1d14ffa2005-10-30 18:58:22 +000087
88 int samples;
Blue Swirl72cf2d42009-09-12 07:36:22 +000089 QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
Marc-André Lureauc01b2452012-04-17 14:32:36 +020090 int ctl_caps;
blueswir135f4b582008-10-06 18:08:30 +000091 struct audio_pcm_ops *pcm_ops;
Blue Swirl72cf2d42009-09-12 07:36:22 +000092 QLIST_ENTRY (HWVoiceIn) entries;
bellard1d14ffa2005-10-30 18:58:22 +000093} HWVoiceIn;
94
bellard1d14ffa2005-10-30 18:58:22 +000095struct SWVoiceOut {
malc1a7dafc2009-05-14 03:11:35 +040096 QEMUSoundCard *card;
bellard1d14ffa2005-10-30 18:58:22 +000097 struct audio_pcm_info info;
98 t_sample *conv;
bellardfb065182004-11-09 23:09:44 +000099 int64_t ratio;
malc1ea879e2008-12-03 22:48:44 +0000100 struct st_sample *buf;
bellardfb065182004-11-09 23:09:44 +0000101 void *rate;
bellard1d14ffa2005-10-30 18:58:22 +0000102 int total_hw_samples_mixed;
bellardfb065182004-11-09 23:09:44 +0000103 int active;
bellard1d14ffa2005-10-30 18:58:22 +0000104 int empty;
105 HWVoiceOut *hw;
bellardfb065182004-11-09 23:09:44 +0000106 char *name;
malc1ea879e2008-12-03 22:48:44 +0000107 struct mixeng_volume vol;
bellard1d14ffa2005-10-30 18:58:22 +0000108 struct audio_callback callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000109 QLIST_ENTRY (SWVoiceOut) entries;
bellardfb065182004-11-09 23:09:44 +0000110};
111
bellard1d14ffa2005-10-30 18:58:22 +0000112struct SWVoiceIn {
malc1a7dafc2009-05-14 03:11:35 +0400113 QEMUSoundCard *card;
bellard1d14ffa2005-10-30 18:58:22 +0000114 int active;
115 struct audio_pcm_info info;
116 int64_t ratio;
117 void *rate;
118 int total_hw_samples_acquired;
malc1ea879e2008-12-03 22:48:44 +0000119 struct st_sample *buf;
bellard1d14ffa2005-10-30 18:58:22 +0000120 f_sample *clip;
121 HWVoiceIn *hw;
122 char *name;
malc1ea879e2008-12-03 22:48:44 +0000123 struct mixeng_volume vol;
bellard1d14ffa2005-10-30 18:58:22 +0000124 struct audio_callback callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000125 QLIST_ENTRY (SWVoiceIn) entries;
bellardfb065182004-11-09 23:09:44 +0000126};
127
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100128typedef struct audio_driver audio_driver;
bellardc0fe3822005-11-05 18:55:28 +0000129struct audio_driver {
130 const char *name;
131 const char *descr;
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100132 void *(*init) (Audiodev *);
bellardc0fe3822005-11-05 18:55:28 +0000133 void (*fini) (void *);
blueswir135f4b582008-10-06 18:08:30 +0000134 struct audio_pcm_ops *pcm_ops;
bellardc0fe3822005-11-05 18:55:28 +0000135 int can_be_default;
136 int max_voices_out;
137 int max_voices_in;
138 int voice_size_out;
139 int voice_size_in;
Marc-André Lureauc01b2452012-04-17 14:32:36 +0200140 int ctl_caps;
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100141 QLIST_ENTRY(audio_driver) next;
bellardc0fe3822005-11-05 18:55:28 +0000142};
143
bellard1d14ffa2005-10-30 18:58:22 +0000144struct audio_pcm_ops {
Kővágó, Zoltán5706db12015-06-03 23:03:47 +0200145 int (*init_out)(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque);
bellard1d14ffa2005-10-30 18:58:22 +0000146 void (*fini_out)(HWVoiceOut *hw);
malcbdff2532009-09-18 11:37:39 +0400147 int (*run_out) (HWVoiceOut *hw, int live);
bellard1d14ffa2005-10-30 18:58:22 +0000148 int (*write) (SWVoiceOut *sw, void *buf, int size);
149 int (*ctl_out) (HWVoiceOut *hw, int cmd, ...);
bellardfb065182004-11-09 23:09:44 +0000150
Kővágó, Zoltán5706db12015-06-03 23:03:47 +0200151 int (*init_in) (HWVoiceIn *hw, struct audsettings *as, void *drv_opaque);
bellard1d14ffa2005-10-30 18:58:22 +0000152 void (*fini_in) (HWVoiceIn *hw);
153 int (*run_in) (HWVoiceIn *hw);
154 int (*read) (SWVoiceIn *sw, void *buf, int size);
155 int (*ctl_in) (HWVoiceIn *hw, int cmd, ...);
156};
bellardfb065182004-11-09 23:09:44 +0000157
bellard8ead62c2006-07-04 16:51:32 +0000158struct capture_callback {
159 struct audio_capture_ops ops;
160 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000161 QLIST_ENTRY (capture_callback) entries;
bellard8ead62c2006-07-04 16:51:32 +0000162};
163
bellardec36b692006-07-16 18:57:03 +0000164struct CaptureVoiceOut {
bellard8ead62c2006-07-04 16:51:32 +0000165 HWVoiceOut hw;
166 void *buf;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000167 QLIST_HEAD (cb_listhead, capture_callback) cb_head;
168 QLIST_ENTRY (CaptureVoiceOut) entries;
bellardec36b692006-07-16 18:57:03 +0000169};
170
171struct SWVoiceCap {
172 SWVoiceOut sw;
173 CaptureVoiceOut *cap;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000174 QLIST_ENTRY (SWVoiceCap) entries;
bellardec36b692006-07-16 18:57:03 +0000175};
bellard8ead62c2006-07-04 16:51:32 +0000176
Philippe Mathieu-Daudéfd5283f2019-01-11 15:08:53 +0100177typedef struct AudioState {
bellardc0fe3822005-11-05 18:55:28 +0000178 struct audio_driver *drv;
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100179 Audiodev *dev;
bellardc0fe3822005-11-05 18:55:28 +0000180 void *drv_opaque;
181
182 QEMUTimer *ts;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000183 QLIST_HEAD (card_listhead, QEMUSoundCard) card_head;
184 QLIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
185 QLIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
186 QLIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head;
bellardc0fe3822005-11-05 18:55:28 +0000187 int nb_hw_voices_out;
188 int nb_hw_voices_in;
malc978dd632009-02-18 20:44:04 +0000189 int vm_running;
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100190 int64_t period_ticks;
Philippe Mathieu-Daudéfd5283f2019-01-11 15:08:53 +0100191} AudioState;
bellardc0fe3822005-11-05 18:55:28 +0000192
Michael Walle00e07672011-01-05 01:05:47 +0100193extern const struct mixeng_volume nominal_volume;
bellardc0fe3822005-11-05 18:55:28 +0000194
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100195extern const char *audio_prio_list[];
196
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100197void audio_driver_register(audio_driver *drv);
198audio_driver *audio_driver_lookup(const char *name);
199
malc1ea879e2008-12-03 22:48:44 +0000200void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
bellard1d14ffa2005-10-30 18:58:22 +0000201void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
bellardfb065182004-11-09 23:09:44 +0000202
bellard1d14ffa2005-10-30 18:58:22 +0000203int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int len);
204int audio_pcm_hw_get_live_in (HWVoiceIn *hw);
bellardfb065182004-11-09 23:09:44 +0000205
bellard1d14ffa2005-10-30 18:58:22 +0000206int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int len);
bellardfb065182004-11-09 23:09:44 +0000207
malcddabec72009-09-18 10:59:38 +0400208int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
209 int live, int pending);
210
bellardc0fe3822005-11-05 18:55:28 +0000211int audio_bug (const char *funcname, int cond);
212void *audio_calloc (const char *funcname, int nmemb, size_t size);
213
malc713a98f2009-09-12 02:28:45 +0400214void audio_run (const char *msg);
215
bellardfb065182004-11-09 23:09:44 +0000216#define VOICE_ENABLE 1
217#define VOICE_DISABLE 2
Marc-André Lureau6c95ab92012-04-17 14:32:35 +0200218#define VOICE_VOLUME 3
bellardfb065182004-11-09 23:09:44 +0000219
Marc-André Lureauc01b2452012-04-17 14:32:36 +0200220#define VOICE_VOLUME_CAP (1 << VOICE_VOLUME)
221
bellard1d14ffa2005-10-30 18:58:22 +0000222static inline int audio_ring_dist (int dst, int src, int len)
223{
224 return (dst >= src) ? (dst - src) : (len - src + dst);
225}
226
Stefan Weil87e613e2013-06-16 11:19:31 +0200227#define dolog(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
bellard1d14ffa2005-10-30 18:58:22 +0000228
229#ifdef DEBUG
Stefan Weil87e613e2013-06-16 11:19:31 +0200230#define ldebug(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
bellard1d14ffa2005-10-30 18:58:22 +0000231#else
Stefan Weil87e613e2013-06-16 11:19:31 +0200232#define ldebug(fmt, ...) (void)0
bellard1d14ffa2005-10-30 18:58:22 +0000233#endif
bellard1d14ffa2005-10-30 18:58:22 +0000234
235#define AUDIO_STRINGIFY_(n) #n
236#define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
237
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100238typedef struct AudiodevListEntry {
239 Audiodev *dev;
240 QSIMPLEQ_ENTRY(AudiodevListEntry) next;
241} AudiodevListEntry;
242
243typedef QSIMPLEQ_HEAD(, AudiodevListEntry) AudiodevListHead;
244AudiodevListHead audio_handle_legacy_opts(void);
245
246void audio_free_audiodev_list(AudiodevListHead *head);
247
248void audio_create_pdos(Audiodev *dev);
249AudiodevPerDirectionOptions *audio_get_pdo_in(Audiodev *dev);
250AudiodevPerDirectionOptions *audio_get_pdo_out(Audiodev *dev);
251
Markus Armbruster175de522016-06-29 15:29:06 +0200252#endif /* QEMU_AUDIO_INT_H */