xref: /openbmc/qemu/audio/audio_int.h (revision d3860a57)
1 /*
2  * QEMU Audio subsystem header
3  *
4  * Copyright (c) 2003-2005 Vassili Karpov (malc)
5  *
6  * 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  */
24 
25 #ifndef QEMU_AUDIO_INT_H
26 #define QEMU_AUDIO_INT_H
27 
28 #ifdef CONFIG_AUDIO_COREAUDIO
29 #define FLOAT_MIXENG
30 /* #define RECIPROCAL */
31 #endif
32 #include "mixeng.h"
33 
34 #ifdef CONFIG_GIO
35 #include <gio/gio.h>
36 #endif
37 
38 struct audio_pcm_ops;
39 
40 struct audio_callback {
41     void *opaque;
42     audio_callback_fn fn;
43 };
44 
45 struct audio_pcm_info {
46     int bits;
47     bool is_signed;
48     bool is_float;
49     int freq;
50     int nchannels;
51     int bytes_per_frame;
52     int bytes_per_second;
53     int swap_endianness;
54 };
55 
56 typedef struct AudioState AudioState;
57 typedef struct SWVoiceCap SWVoiceCap;
58 
59 typedef struct STSampleBuffer {
60     size_t pos, size;
61     st_sample *buffer;
62 } STSampleBuffer;
63 
64 typedef struct HWVoiceOut {
65     AudioState *s;
66     int enabled;
67     int poll_mode;
68     int pending_disable;
69     struct audio_pcm_info info;
70 
71     f_sample *clip;
72     uint64_t ts_helper;
73 
74     STSampleBuffer mix_buf;
75     void *buf_emul;
76     size_t pos_emul, pending_emul, size_emul;
77 
78     size_t samples;
79     QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
80     QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head;
81     struct audio_pcm_ops *pcm_ops;
82     QLIST_ENTRY (HWVoiceOut) entries;
83 } HWVoiceOut;
84 
85 typedef struct HWVoiceIn {
86     AudioState *s;
87     int enabled;
88     int poll_mode;
89     struct audio_pcm_info info;
90 
91     t_sample *conv;
92 
93     size_t total_samples_captured;
94     uint64_t ts_helper;
95 
96     STSampleBuffer conv_buf;
97     void *buf_emul;
98     size_t pos_emul, pending_emul, size_emul;
99 
100     size_t samples;
101     QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
102     struct audio_pcm_ops *pcm_ops;
103     QLIST_ENTRY (HWVoiceIn) entries;
104 } HWVoiceIn;
105 
106 struct SWVoiceOut {
107     QEMUSoundCard *card;
108     AudioState *s;
109     struct audio_pcm_info info;
110     t_sample *conv;
111     STSampleBuffer resample_buf;
112     void *rate;
113     size_t total_hw_samples_mixed;
114     int active;
115     int empty;
116     HWVoiceOut *hw;
117     char *name;
118     struct mixeng_volume vol;
119     struct audio_callback callback;
120     QLIST_ENTRY (SWVoiceOut) entries;
121 };
122 
123 struct SWVoiceIn {
124     QEMUSoundCard *card;
125     AudioState *s;
126     int active;
127     struct audio_pcm_info info;
128     void *rate;
129     size_t total_hw_samples_acquired;
130     STSampleBuffer resample_buf;
131     f_sample *clip;
132     HWVoiceIn *hw;
133     char *name;
134     struct mixeng_volume vol;
135     struct audio_callback callback;
136     QLIST_ENTRY (SWVoiceIn) entries;
137 };
138 
139 typedef struct audio_driver audio_driver;
140 struct audio_driver {
141     const char *name;
142     const char *descr;
143     void *(*init) (Audiodev *);
144     void (*fini) (void *);
145 #ifdef CONFIG_GIO
146     void (*set_dbus_server) (AudioState *s, GDBusObjectManagerServer *manager);
147 #endif
148     struct audio_pcm_ops *pcm_ops;
149     int can_be_default;
150     int max_voices_out;
151     int max_voices_in;
152     size_t voice_size_out;
153     size_t voice_size_in;
154     QLIST_ENTRY(audio_driver) next;
155 };
156 
157 struct audio_pcm_ops {
158     int    (*init_out)(HWVoiceOut *hw, audsettings *as, void *drv_opaque);
159     void   (*fini_out)(HWVoiceOut *hw);
160     size_t (*write)   (HWVoiceOut *hw, void *buf, size_t size);
161     void   (*run_buffer_out)(HWVoiceOut *hw);
162     /*
163      * Get the free output buffer size. This is an upper limit. The size
164      * returned by function get_buffer_out may be smaller.
165      */
166     size_t (*buffer_get_free)(HWVoiceOut *hw);
167     /*
168      * get a buffer that after later can be passed to put_buffer_out; optional
169      * returns the buffer, and writes it's size to size (in bytes)
170      */
171     void  *(*get_buffer_out)(HWVoiceOut *hw, size_t *size);
172     /*
173      * put back the buffer returned by get_buffer_out; optional
174      * buf must be equal the pointer returned by get_buffer_out,
175      * size may be smaller
176      */
177     size_t (*put_buffer_out)(HWVoiceOut *hw, void *buf, size_t size);
178     void   (*enable_out)(HWVoiceOut *hw, bool enable);
179     void   (*volume_out)(HWVoiceOut *hw, Volume *vol);
180 
181     int    (*init_in) (HWVoiceIn *hw, audsettings *as, void *drv_opaque);
182     void   (*fini_in) (HWVoiceIn *hw);
183     size_t (*read)    (HWVoiceIn *hw, void *buf, size_t size);
184     void   (*run_buffer_in)(HWVoiceIn *hw);
185     void  *(*get_buffer_in)(HWVoiceIn *hw, size_t *size);
186     void   (*put_buffer_in)(HWVoiceIn *hw, void *buf, size_t size);
187     void   (*enable_in)(HWVoiceIn *hw, bool enable);
188     void   (*volume_in)(HWVoiceIn *hw, Volume *vol);
189 };
190 
191 void audio_generic_run_buffer_in(HWVoiceIn *hw);
192 void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size);
193 void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size);
194 void audio_generic_run_buffer_out(HWVoiceOut *hw);
195 size_t audio_generic_buffer_get_free(HWVoiceOut *hw);
196 void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size);
197 size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size);
198 size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size);
199 size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size);
200 
201 struct capture_callback {
202     struct audio_capture_ops ops;
203     void *opaque;
204     QLIST_ENTRY (capture_callback) entries;
205 };
206 
207 struct CaptureVoiceOut {
208     HWVoiceOut hw;
209     void *buf;
210     QLIST_HEAD (cb_listhead, capture_callback) cb_head;
211     QLIST_ENTRY (CaptureVoiceOut) entries;
212 };
213 
214 struct SWVoiceCap {
215     SWVoiceOut sw;
216     CaptureVoiceOut *cap;
217     QLIST_ENTRY (SWVoiceCap) entries;
218 };
219 
220 typedef struct AudioState {
221     struct audio_driver *drv;
222     Audiodev *dev;
223     void *drv_opaque;
224 
225     QEMUTimer *ts;
226     QLIST_HEAD (card_listhead, QEMUSoundCard) card_head;
227     QLIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
228     QLIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
229     QLIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head;
230     int nb_hw_voices_out;
231     int nb_hw_voices_in;
232     int vm_running;
233     int64_t period_ticks;
234 
235     bool timer_running;
236     uint64_t timer_last;
237 
238     QTAILQ_ENTRY(AudioState) list;
239 } AudioState;
240 
241 extern const struct mixeng_volume nominal_volume;
242 
243 extern const char *audio_prio_list[];
244 
245 void audio_driver_register(audio_driver *drv);
246 audio_driver *audio_driver_lookup(const char *name);
247 
248 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
249 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
250 
251 int audio_bug (const char *funcname, int cond);
252 
253 void audio_run(AudioState *s, const char *msg);
254 
255 const char *audio_application_name(void);
256 
257 typedef struct RateCtl {
258     int64_t start_ticks;
259     int64_t bytes_sent;
260 } RateCtl;
261 
262 void audio_rate_start(RateCtl *rate);
263 size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info);
264 void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used);
265 size_t audio_rate_get_bytes(RateCtl *rate, struct audio_pcm_info *info,
266                             size_t bytes_avail);
267 
268 static inline size_t audio_ring_dist(size_t dst, size_t src, size_t len)
269 {
270     return (dst >= src) ? (dst - src) : (len - src + dst);
271 }
272 
273 /**
274  * audio_ring_posb() - returns new position in ringbuffer in backward
275  * direction at given distance
276  *
277  * @pos: current position in ringbuffer
278  * @dist: distance in ringbuffer to walk in reverse direction
279  * @len: size of ringbuffer
280  */
281 static inline size_t audio_ring_posb(size_t pos, size_t dist, size_t len)
282 {
283     return pos >= dist ? pos - dist : len - dist + pos;
284 }
285 
286 #define dolog(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
287 
288 #ifdef DEBUG
289 #define ldebug(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
290 #else
291 #define ldebug(fmt, ...) (void)0
292 #endif
293 
294 typedef struct AudiodevListEntry {
295     Audiodev *dev;
296     QSIMPLEQ_ENTRY(AudiodevListEntry) next;
297 } AudiodevListEntry;
298 
299 typedef QSIMPLEQ_HEAD(, AudiodevListEntry) AudiodevListHead;
300 AudiodevListHead audio_handle_legacy_opts(void);
301 
302 void audio_free_audiodev_list(AudiodevListHead *head);
303 
304 void audio_create_pdos(Audiodev *dev);
305 AudiodevPerDirectionOptions *audio_get_pdo_in(Audiodev *dev);
306 AudiodevPerDirectionOptions *audio_get_pdo_out(Audiodev *dev);
307 
308 #endif /* QEMU_AUDIO_INT_H */
309