blob: 4775857bf24521767b3a2ead012f14cf53b3d360 [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;
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +010043 bool is_signed;
44 bool is_float;
bellard1d14ffa2005-10-30 18:58:22 +000045 int freq;
46 int nchannels;
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +020047 int bytes_per_frame;
bellard1d14ffa2005-10-30 18:58:22 +000048 int bytes_per_second;
bellardd929eba2006-07-04 21:47:22 +000049 int swap_endianness;
bellard1d14ffa2005-10-30 18:58:22 +000050};
51
Kővágó, Zoltán526fb052019-08-19 01:06:46 +020052typedef struct AudioState AudioState;
bellardec36b692006-07-16 18:57:03 +000053typedef struct SWVoiceCap SWVoiceCap;
54
Kővágó, Zoltándc88e382019-09-19 23:24:20 +020055typedef struct STSampleBuffer {
56 size_t pos, size;
57 st_sample samples[];
58} STSampleBuffer;
59
bellard1d14ffa2005-10-30 18:58:22 +000060typedef struct HWVoiceOut {
Kővágó, Zoltán526fb052019-08-19 01:06:46 +020061 AudioState *s;
bellardfb065182004-11-09 23:09:44 +000062 int enabled;
malc713a98f2009-09-12 02:28:45 +040063 int poll_mode;
bellardfb065182004-11-09 23:09:44 +000064 int pending_disable;
bellard1d14ffa2005-10-30 18:58:22 +000065 struct audio_pcm_info info;
bellardfb065182004-11-09 23:09:44 +000066
67 f_sample *clip;
bellard1d14ffa2005-10-30 18:58:22 +000068 uint64_t ts_helper;
bellardfb065182004-11-09 23:09:44 +000069
Kővágó, Zoltándc88e382019-09-19 23:24:20 +020070 STSampleBuffer *mix_buf;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +020071 void *buf_emul;
72 size_t pos_emul, pending_emul, size_emul;
bellardfb065182004-11-09 23:09:44 +000073
Kővágó, Zoltán75204622019-08-19 01:06:58 +020074 size_t samples;
Blue Swirl72cf2d42009-09-12 07:36:22 +000075 QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
76 QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head;
blueswir135f4b582008-10-06 18:08:30 +000077 struct audio_pcm_ops *pcm_ops;
Blue Swirl72cf2d42009-09-12 07:36:22 +000078 QLIST_ENTRY (HWVoiceOut) entries;
bellard1d14ffa2005-10-30 18:58:22 +000079} HWVoiceOut;
bellardfb065182004-11-09 23:09:44 +000080
bellard1d14ffa2005-10-30 18:58:22 +000081typedef struct HWVoiceIn {
Kővágó, Zoltán526fb052019-08-19 01:06:46 +020082 AudioState *s;
bellard1d14ffa2005-10-30 18:58:22 +000083 int enabled;
malc713a98f2009-09-12 02:28:45 +040084 int poll_mode;
bellard1d14ffa2005-10-30 18:58:22 +000085 struct audio_pcm_info info;
bellardfb065182004-11-09 23:09:44 +000086
87 t_sample *conv;
88
Kővágó, Zoltán75204622019-08-19 01:06:58 +020089 size_t total_samples_captured;
bellard1d14ffa2005-10-30 18:58:22 +000090 uint64_t ts_helper;
91
Kővágó, Zoltándc88e382019-09-19 23:24:20 +020092 STSampleBuffer *conv_buf;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +020093 void *buf_emul;
94 size_t pos_emul, pending_emul, size_emul;
bellard1d14ffa2005-10-30 18:58:22 +000095
Kővágó, Zoltán75204622019-08-19 01:06:58 +020096 size_t samples;
Blue Swirl72cf2d42009-09-12 07:36:22 +000097 QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
blueswir135f4b582008-10-06 18:08:30 +000098 struct audio_pcm_ops *pcm_ops;
Blue Swirl72cf2d42009-09-12 07:36:22 +000099 QLIST_ENTRY (HWVoiceIn) entries;
bellard1d14ffa2005-10-30 18:58:22 +0000100} HWVoiceIn;
101
bellard1d14ffa2005-10-30 18:58:22 +0000102struct SWVoiceOut {
malc1a7dafc2009-05-14 03:11:35 +0400103 QEMUSoundCard *card;
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200104 AudioState *s;
bellard1d14ffa2005-10-30 18:58:22 +0000105 struct audio_pcm_info info;
106 t_sample *conv;
bellardfb065182004-11-09 23:09:44 +0000107 int64_t ratio;
malc1ea879e2008-12-03 22:48:44 +0000108 struct st_sample *buf;
bellardfb065182004-11-09 23:09:44 +0000109 void *rate;
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200110 size_t total_hw_samples_mixed;
bellardfb065182004-11-09 23:09:44 +0000111 int active;
bellard1d14ffa2005-10-30 18:58:22 +0000112 int empty;
113 HWVoiceOut *hw;
bellardfb065182004-11-09 23:09:44 +0000114 char *name;
malc1ea879e2008-12-03 22:48:44 +0000115 struct mixeng_volume vol;
bellard1d14ffa2005-10-30 18:58:22 +0000116 struct audio_callback callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000117 QLIST_ENTRY (SWVoiceOut) entries;
bellardfb065182004-11-09 23:09:44 +0000118};
119
bellard1d14ffa2005-10-30 18:58:22 +0000120struct SWVoiceIn {
malc1a7dafc2009-05-14 03:11:35 +0400121 QEMUSoundCard *card;
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200122 AudioState *s;
bellard1d14ffa2005-10-30 18:58:22 +0000123 int active;
124 struct audio_pcm_info info;
125 int64_t ratio;
126 void *rate;
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200127 size_t total_hw_samples_acquired;
malc1ea879e2008-12-03 22:48:44 +0000128 struct st_sample *buf;
bellard1d14ffa2005-10-30 18:58:22 +0000129 f_sample *clip;
130 HWVoiceIn *hw;
131 char *name;
malc1ea879e2008-12-03 22:48:44 +0000132 struct mixeng_volume vol;
bellard1d14ffa2005-10-30 18:58:22 +0000133 struct audio_callback callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000134 QLIST_ENTRY (SWVoiceIn) entries;
bellardfb065182004-11-09 23:09:44 +0000135};
136
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100137typedef struct audio_driver audio_driver;
bellardc0fe3822005-11-05 18:55:28 +0000138struct audio_driver {
139 const char *name;
140 const char *descr;
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100141 void *(*init) (Audiodev *);
bellardc0fe3822005-11-05 18:55:28 +0000142 void (*fini) (void *);
blueswir135f4b582008-10-06 18:08:30 +0000143 struct audio_pcm_ops *pcm_ops;
bellardc0fe3822005-11-05 18:55:28 +0000144 int can_be_default;
145 int max_voices_out;
146 int max_voices_in;
147 int voice_size_out;
148 int voice_size_in;
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100149 QLIST_ENTRY(audio_driver) next;
bellardc0fe3822005-11-05 18:55:28 +0000150};
151
bellard1d14ffa2005-10-30 18:58:22 +0000152struct audio_pcm_ops {
Kővágó, Zoltánff095e52019-09-19 23:24:09 +0200153 int (*init_out)(HWVoiceOut *hw, audsettings *as, void *drv_opaque);
154 void (*fini_out)(HWVoiceOut *hw);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +0200155 size_t (*write) (HWVoiceOut *hw, void *buf, size_t size);
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +0100156 void (*run_buffer_out)(HWVoiceOut *hw);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +0200157 /*
158 * get a buffer that after later can be passed to put_buffer_out; optional
159 * returns the buffer, and writes it's size to size (in bytes)
160 * this is unrelated to the above buffer_size_out function
161 */
162 void *(*get_buffer_out)(HWVoiceOut *hw, size_t *size);
163 /*
164 * put back the buffer returned by get_buffer_out; optional
165 * buf must be equal the pointer returned by get_buffer_out,
166 * size may be smaller
167 */
168 size_t (*put_buffer_out)(HWVoiceOut *hw, void *buf, size_t size);
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200169 void (*enable_out)(HWVoiceOut *hw, bool enable);
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +0200170 void (*volume_out)(HWVoiceOut *hw, Volume *vol);
bellardfb065182004-11-09 23:09:44 +0000171
Kővágó, Zoltánff095e52019-09-19 23:24:09 +0200172 int (*init_in) (HWVoiceIn *hw, audsettings *as, void *drv_opaque);
173 void (*fini_in) (HWVoiceIn *hw);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +0200174 size_t (*read) (HWVoiceIn *hw, void *buf, size_t size);
175 void *(*get_buffer_in)(HWVoiceIn *hw, size_t *size);
176 void (*put_buffer_in)(HWVoiceIn *hw, void *buf, size_t size);
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200177 void (*enable_in)(HWVoiceIn *hw, bool enable);
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +0200178 void (*volume_in)(HWVoiceIn *hw, Volume *vol);
bellard1d14ffa2005-10-30 18:58:22 +0000179};
bellardfb065182004-11-09 23:09:44 +0000180
Kővágó, Zoltánff095e52019-09-19 23:24:09 +0200181void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size);
182void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size);
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +0100183void audio_generic_run_buffer_out(HWVoiceOut *hw);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +0200184void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size);
185size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +0200186size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size);
187size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size);
188
bellard8ead62c2006-07-04 16:51:32 +0000189struct capture_callback {
190 struct audio_capture_ops ops;
191 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000192 QLIST_ENTRY (capture_callback) entries;
bellard8ead62c2006-07-04 16:51:32 +0000193};
194
bellardec36b692006-07-16 18:57:03 +0000195struct CaptureVoiceOut {
bellard8ead62c2006-07-04 16:51:32 +0000196 HWVoiceOut hw;
197 void *buf;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000198 QLIST_HEAD (cb_listhead, capture_callback) cb_head;
199 QLIST_ENTRY (CaptureVoiceOut) entries;
bellardec36b692006-07-16 18:57:03 +0000200};
201
202struct SWVoiceCap {
203 SWVoiceOut sw;
204 CaptureVoiceOut *cap;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000205 QLIST_ENTRY (SWVoiceCap) entries;
bellardec36b692006-07-16 18:57:03 +0000206};
bellard8ead62c2006-07-04 16:51:32 +0000207
Philippe Mathieu-Daudéfd5283f2019-01-11 15:08:53 +0100208typedef struct AudioState {
bellardc0fe3822005-11-05 18:55:28 +0000209 struct audio_driver *drv;
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100210 Audiodev *dev;
bellardc0fe3822005-11-05 18:55:28 +0000211 void *drv_opaque;
212
213 QEMUTimer *ts;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000214 QLIST_HEAD (card_listhead, QEMUSoundCard) card_head;
215 QLIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
216 QLIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
217 QLIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head;
bellardc0fe3822005-11-05 18:55:28 +0000218 int nb_hw_voices_out;
219 int nb_hw_voices_in;
malc978dd632009-02-18 20:44:04 +0000220 int vm_running;
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100221 int64_t period_ticks;
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200222
223 bool timer_running;
224 uint64_t timer_last;
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +0200225
226 QTAILQ_ENTRY(AudioState) list;
Philippe Mathieu-Daudéfd5283f2019-01-11 15:08:53 +0100227} AudioState;
bellardc0fe3822005-11-05 18:55:28 +0000228
Michael Walle00e07672011-01-05 01:05:47 +0100229extern const struct mixeng_volume nominal_volume;
bellardc0fe3822005-11-05 18:55:28 +0000230
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100231extern const char *audio_prio_list[];
232
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100233void audio_driver_register(audio_driver *drv);
234audio_driver *audio_driver_lookup(const char *name);
235
malc1ea879e2008-12-03 22:48:44 +0000236void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
bellard1d14ffa2005-10-30 18:58:22 +0000237void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
bellardfb065182004-11-09 23:09:44 +0000238
bellardc0fe3822005-11-05 18:55:28 +0000239int audio_bug (const char *funcname, int cond);
240void *audio_calloc (const char *funcname, int nmemb, size_t size);
241
Kővágó, Zoltán18e2c172019-08-19 01:06:55 +0200242void audio_run(AudioState *s, const char *msg);
malc713a98f2009-09-12 02:28:45 +0400243
Kővágó, Zoltán857271a2019-09-19 23:24:21 +0200244typedef struct RateCtl {
245 int64_t start_ticks;
246 int64_t bytes_sent;
247} RateCtl;
248
249void audio_rate_start(RateCtl *rate);
250size_t audio_rate_get_bytes(struct audio_pcm_info *info, RateCtl *rate,
251 size_t bytes_avail);
252
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200253static inline size_t audio_ring_dist(size_t dst, size_t src, size_t len)
bellard1d14ffa2005-10-30 18:58:22 +0000254{
255 return (dst >= src) ? (dst - src) : (len - src + dst);
256}
257
Stefan Weil87e613e2013-06-16 11:19:31 +0200258#define dolog(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
bellard1d14ffa2005-10-30 18:58:22 +0000259
260#ifdef DEBUG
Stefan Weil87e613e2013-06-16 11:19:31 +0200261#define ldebug(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
bellard1d14ffa2005-10-30 18:58:22 +0000262#else
Stefan Weil87e613e2013-06-16 11:19:31 +0200263#define ldebug(fmt, ...) (void)0
bellard1d14ffa2005-10-30 18:58:22 +0000264#endif
bellard1d14ffa2005-10-30 18:58:22 +0000265
266#define AUDIO_STRINGIFY_(n) #n
267#define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
268
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100269typedef struct AudiodevListEntry {
270 Audiodev *dev;
271 QSIMPLEQ_ENTRY(AudiodevListEntry) next;
272} AudiodevListEntry;
273
274typedef QSIMPLEQ_HEAD(, AudiodevListEntry) AudiodevListHead;
275AudiodevListHead audio_handle_legacy_opts(void);
276
277void audio_free_audiodev_list(AudiodevListHead *head);
278
279void audio_create_pdos(Audiodev *dev);
280AudiodevPerDirectionOptions *audio_get_pdo_in(Audiodev *dev);
281AudiodevPerDirectionOptions *audio_get_pdo_out(Audiodev *dev);
282
Markus Armbruster175de522016-06-29 15:29:06 +0200283#endif /* QEMU_AUDIO_INT_H */