1 | /* $Id: AudioMixer.h 87852 2021-02-23 17:45:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox audio - Mixing routines.
|
---|
4 | *
|
---|
5 | * The mixing routines are mainly used by the various audio device emulations
|
---|
6 | * to achieve proper multiplexing from/to attached devices LUNs.
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * Copyright (C) 2014-2020 Oracle Corporation
|
---|
11 | *
|
---|
12 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | * available from http://www.virtualbox.org. This file is free software;
|
---|
14 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | * General Public License (GPL) as published by the Free Software
|
---|
16 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef VBOX_INCLUDED_SRC_Audio_AudioMixer_h
|
---|
22 | #define VBOX_INCLUDED_SRC_Audio_AudioMixer_h
|
---|
23 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
24 | # pragma once
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include <iprt/cdefs.h>
|
---|
28 | #include <iprt/critsect.h>
|
---|
29 |
|
---|
30 | #include <VBox/vmm/pdmaudioifs.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /** Pointer to an audio mixer sink. */
|
---|
34 | typedef struct AUDMIXSINK *PAUDMIXSINK;
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Audio mixer instance.
|
---|
39 | */
|
---|
40 | typedef struct AUDIOMIXER
|
---|
41 | {
|
---|
42 | /** The mixer's name. */
|
---|
43 | char *pszName;
|
---|
44 | /** The mixer's critical section. */
|
---|
45 | RTCRITSECT CritSect;
|
---|
46 | /** The master volume of this mixer. */
|
---|
47 | PDMAUDIOVOLUME VolMaster;
|
---|
48 | /** List of audio mixer sinks. */
|
---|
49 | RTLISTANCHOR lstSinks;
|
---|
50 | /** Number of used audio sinks. */
|
---|
51 | uint8_t cSinks;
|
---|
52 | /** Mixer flags. See AUDMIXER_FLAGS_XXX. */
|
---|
53 | uint32_t fFlags;
|
---|
54 | } AUDIOMIXER;
|
---|
55 | /** Pointer to an audio mixer instance. */
|
---|
56 | typedef AUDIOMIXER *PAUDIOMIXER;
|
---|
57 |
|
---|
58 | /** Defines an audio mixer stream's flags. */
|
---|
59 | #define AUDMIXSTREAMFLAGS uint32_t
|
---|
60 |
|
---|
61 | /** No flags specified. */
|
---|
62 | #define AUDMIXSTREAM_F_NONE 0
|
---|
63 | /** The mixing stream is flagged as being enabled (active). */
|
---|
64 | #define AUDMIXSTREAM_F_ENABLED RT_BIT(0)
|
---|
65 |
|
---|
66 | /** Defines an audio mixer stream's internal status. */
|
---|
67 | #define AUDMIXSTREAMSTATUS uint32_t
|
---|
68 |
|
---|
69 | /** No status set. */
|
---|
70 | #define AUDMIXSTREAM_STATUS_NONE 0
|
---|
71 | /** The mixing stream is enabled (active). */
|
---|
72 | #define AUDMIXSTREAM_STATUS_ENABLED RT_BIT(0)
|
---|
73 | /** The mixing stream can be read from. */
|
---|
74 | #define AUDMIXSTREAM_STATUS_CAN_READ RT_BIT(1)
|
---|
75 | /** The mixing stream can be written to. */
|
---|
76 | #define AUDMIXSTREAM_STATUS_CAN_WRITE RT_BIT(2)
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Audio mixer stream.
|
---|
81 | */
|
---|
82 | typedef struct AUDMIXSTREAM
|
---|
83 | {
|
---|
84 | /** List node. */
|
---|
85 | RTLISTNODE Node;
|
---|
86 | /** Name of this stream. */
|
---|
87 | char *pszName;
|
---|
88 | /** The streams's critical section. */
|
---|
89 | RTCRITSECT CritSect;
|
---|
90 | /** Sink this stream is attached to. */
|
---|
91 | PAUDMIXSINK pSink;
|
---|
92 | /** Stream flags of type AUDMIXSTREAM_F_. */
|
---|
93 | uint32_t fFlags;
|
---|
94 | /** Stream status of type AUDMIXSTREAM_STATUS_. */
|
---|
95 | uint32_t fStatus;
|
---|
96 | /** Pointer to audio connector being used. */
|
---|
97 | PPDMIAUDIOCONNECTOR pConn;
|
---|
98 | /** Pointer to PDM audio stream this mixer stream handles. */
|
---|
99 | PPDMAUDIOSTREAM pStream;
|
---|
100 | /** Last read (recording) / written (playback) timestamp (in ns). */
|
---|
101 | uint64_t tsLastReadWrittenNs;
|
---|
102 | /** The stream's circular buffer for temporarily
|
---|
103 | * holding (raw) device audio data. */
|
---|
104 | PRTCIRCBUF pCircBuf;
|
---|
105 | } AUDMIXSTREAM, *PAUDMIXSTREAM;
|
---|
106 |
|
---|
107 | /** Defines an audio sink's current status. */
|
---|
108 | #define AUDMIXSINKSTS uint32_t
|
---|
109 |
|
---|
110 | /** No status specified. */
|
---|
111 | #define AUDMIXSINK_STS_NONE 0
|
---|
112 | /** The sink is active and running. */
|
---|
113 | #define AUDMIXSINK_STS_RUNNING RT_BIT(0)
|
---|
114 | /** The sink is in a pending disable state. */
|
---|
115 | #define AUDMIXSINK_STS_PENDING_DISABLE RT_BIT(1)
|
---|
116 | /** Dirty flag.
|
---|
117 | * For output sinks this means that there is data in the
|
---|
118 | * sink which has not been played yet.
|
---|
119 | * For input sinks this means that there is data in the
|
---|
120 | * sink which has been recorded but not transferred to the
|
---|
121 | * destination yet. */
|
---|
122 | #define AUDMIXSINK_STS_DIRTY RT_BIT(2)
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Audio mixer sink direction.
|
---|
126 | */
|
---|
127 | typedef enum AUDMIXSINKDIR
|
---|
128 | {
|
---|
129 | /** Unknown direction. */
|
---|
130 | AUDMIXSINKDIR_UNKNOWN = 0,
|
---|
131 | /** Input (capturing from a device). */
|
---|
132 | AUDMIXSINKDIR_INPUT,
|
---|
133 | /** Output (playing to a device). */
|
---|
134 | AUDMIXSINKDIR_OUTPUT,
|
---|
135 | /** The usual 32-bit hack. */
|
---|
136 | AUDMIXSINKDIR_32BIT_HACK = 0x7fffffff
|
---|
137 | } AUDMIXSINKDIR;
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Audio mixer sink command.
|
---|
141 | */
|
---|
142 | typedef enum AUDMIXSINKCMD
|
---|
143 | {
|
---|
144 | /** Unknown command, do not use. */
|
---|
145 | AUDMIXSINKCMD_UNKNOWN = 0,
|
---|
146 | /** Enables the sink. */
|
---|
147 | AUDMIXSINKCMD_ENABLE,
|
---|
148 | /** Disables the sink. */
|
---|
149 | AUDMIXSINKCMD_DISABLE,
|
---|
150 | /** Pauses the sink. */
|
---|
151 | AUDMIXSINKCMD_PAUSE,
|
---|
152 | /** Resumes the sink. */
|
---|
153 | AUDMIXSINKCMD_RESUME,
|
---|
154 | /** Tells the sink's streams to drop all (buffered) data immediately. */
|
---|
155 | AUDMIXSINKCMD_DROP,
|
---|
156 | /** Hack to blow the type up to 32-bit. */
|
---|
157 | AUDMIXSINKCMD_32BIT_HACK = 0x7fffffff
|
---|
158 | } AUDMIXSINKCMD;
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Audio input sink specifics.
|
---|
162 | *
|
---|
163 | * Do not use directly. Instead, use AUDMIXSINK.
|
---|
164 | */
|
---|
165 | typedef struct AUDMIXSINKIN
|
---|
166 | {
|
---|
167 | /** The current recording source. Can be NULL if not set. */
|
---|
168 | PAUDMIXSTREAM pStreamRecSource;
|
---|
169 | } AUDMIXSINKIN;
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Audio output sink specifics.
|
---|
173 | *
|
---|
174 | * Do not use directly. Instead, use AUDMIXSINK.
|
---|
175 | */
|
---|
176 | typedef struct AUDMIXSINKOUT
|
---|
177 | {
|
---|
178 | } AUDMIXSINKOUT;
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Audio mixer sink.
|
---|
182 | */
|
---|
183 | typedef struct AUDMIXSINK
|
---|
184 | {
|
---|
185 | RTLISTNODE Node;
|
---|
186 | /** Pointer to mixer object this sink is bound to. */
|
---|
187 | PAUDIOMIXER pParent;
|
---|
188 | /** Name of this sink. */
|
---|
189 | char *pszName;
|
---|
190 | /** The sink direction, that is,
|
---|
191 | * if this sink handles input or output. */
|
---|
192 | AUDMIXSINKDIR enmDir;
|
---|
193 | /** The sink's critical section. */
|
---|
194 | RTCRITSECT CritSect;
|
---|
195 | /** This sink's mixing buffer, acting as
|
---|
196 | * a parent buffer for all streams this sink owns. */
|
---|
197 | PDMAUDIOMIXBUF MixBuf;
|
---|
198 | /** Scratch buffer for multiplexing / mixing. Might be NULL if not needed. */
|
---|
199 | uint8_t *pabScratchBuf;
|
---|
200 | /** Size (in bytes) of pabScratchBuf. Might be 0 if not needed. */
|
---|
201 | size_t cbScratchBuf;
|
---|
202 | /** Union for input/output specifics. */
|
---|
203 | union
|
---|
204 | {
|
---|
205 | AUDMIXSINKIN In;
|
---|
206 | AUDMIXSINKOUT Out;
|
---|
207 | };
|
---|
208 | /** Sink status of type AUDMIXSINK_STS_XXX. */
|
---|
209 | AUDMIXSINKSTS fStatus;
|
---|
210 | /** The sink's PCM format. */
|
---|
211 | PDMAUDIOPCMPROPS PCMProps;
|
---|
212 | /** Number of streams assigned. */
|
---|
213 | uint8_t cStreams;
|
---|
214 | /** List of assigned streams.
|
---|
215 | * Note: All streams have the same PCM properties, so the
|
---|
216 | * mixer does not do any conversion. */
|
---|
217 | /** @todo Use something faster -- vector maybe? */
|
---|
218 | RTLISTANCHOR lstStreams;
|
---|
219 | /** The volume of this sink. The volume always will
|
---|
220 | * be combined with the mixer's master volume. */
|
---|
221 | PDMAUDIOVOLUME Volume;
|
---|
222 | /** The volume of this sink, combined with the last set master volume. */
|
---|
223 | PDMAUDIOVOLUME VolumeCombined;
|
---|
224 | /** Timestamp since last update (in ms). */
|
---|
225 | uint64_t tsLastUpdatedMs;
|
---|
226 | /** Last read (recording) / written (playback) timestamp (in ns). */
|
---|
227 | uint64_t tsLastReadWrittenNs;
|
---|
228 | struct
|
---|
229 | {
|
---|
230 | PPDMAUDIOFILE pFile;
|
---|
231 | } Dbg;
|
---|
232 | } AUDMIXSINK;
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Audio mixer operation.
|
---|
236 | */
|
---|
237 | typedef enum AUDMIXOP
|
---|
238 | {
|
---|
239 | /** Invalid operation, do not use. */
|
---|
240 | AUDMIXOP_INVALID = 0,
|
---|
241 | /** Copy data from A to B, overwriting data in B. */
|
---|
242 | AUDMIXOP_COPY,
|
---|
243 | /** Blend data from A with (existing) data in B. */
|
---|
244 | AUDMIXOP_BLEND,
|
---|
245 | /** The usual 32-bit hack. */
|
---|
246 | AUDMIXOP_32BIT_HACK = 0x7fffffff
|
---|
247 | } AUDMIXOP;
|
---|
248 |
|
---|
249 | /** No flags specified. */
|
---|
250 | #define AUDMIXSTRMCTL_F_NONE 0
|
---|
251 |
|
---|
252 | /** No mixer flags specified. */
|
---|
253 | #define AUDMIXER_FLAGS_NONE 0
|
---|
254 | /** Debug mode enabled.
|
---|
255 | * This writes .WAV file to the host, usually to the temporary directory. */
|
---|
256 | #define AUDMIXER_FLAGS_DEBUG RT_BIT(0)
|
---|
257 | /** Validation mask. */
|
---|
258 | #define AUDMIXER_FLAGS_VALID_MASK UINT32_C(0x00000001)
|
---|
259 |
|
---|
260 | int AudioMixerCreate(const char *pszName, uint32_t fFlags, PAUDIOMIXER *ppMixer);
|
---|
261 | int AudioMixerCreateSink(PAUDIOMIXER pMixer, const char *pszName, AUDMIXSINKDIR enmDir, PAUDMIXSINK *ppSink);
|
---|
262 | void AudioMixerDestroy(PAUDIOMIXER pMixer);
|
---|
263 | void AudioMixerInvalidate(PAUDIOMIXER pMixer);
|
---|
264 | void AudioMixerRemoveSink(PAUDIOMIXER pMixer, PAUDMIXSINK pSink);
|
---|
265 | int AudioMixerSetMasterVolume(PAUDIOMIXER pMixer, PPDMAUDIOVOLUME pVol);
|
---|
266 | void AudioMixerDebug(PAUDIOMIXER pMixer, PCDBGFINFOHLP pHlp, const char *pszArgs);
|
---|
267 |
|
---|
268 | int AudioMixerSinkAddStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream);
|
---|
269 | int AudioMixerSinkCreateStream(PAUDMIXSINK pSink, PPDMIAUDIOCONNECTOR pConnector, PPDMAUDIOSTREAMCFG pCfg, AUDMIXSTREAMFLAGS fFlags, PAUDMIXSTREAM *ppStream);
|
---|
270 | int AudioMixerSinkCtl(PAUDMIXSINK pSink, AUDMIXSINKCMD enmCmd);
|
---|
271 | void AudioMixerSinkDestroy(PAUDMIXSINK pSink);
|
---|
272 | uint32_t AudioMixerSinkGetReadable(PAUDMIXSINK pSink);
|
---|
273 | uint32_t AudioMixerSinkGetWritable(PAUDMIXSINK pSink);
|
---|
274 | AUDMIXSINKDIR AudioMixerSinkGetDir(PAUDMIXSINK pSink);
|
---|
275 | const char *AudioMixerSinkGetName(const PAUDMIXSINK pSink);
|
---|
276 | PAUDMIXSTREAM AudioMixerSinkGetRecordingSource(PAUDMIXSINK pSink);
|
---|
277 | PAUDMIXSTREAM AudioMixerSinkGetStream(PAUDMIXSINK pSink, uint8_t uIndex);
|
---|
278 | AUDMIXSINKSTS AudioMixerSinkGetStatus(PAUDMIXSINK pSink);
|
---|
279 | uint8_t AudioMixerSinkGetStreamCount(PAUDMIXSINK pSink);
|
---|
280 | bool AudioMixerSinkIsActive(PAUDMIXSINK pSink);
|
---|
281 | int AudioMixerSinkRead(PAUDMIXSINK pSink, AUDMIXOP enmOp, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead);
|
---|
282 | void AudioMixerSinkRemoveStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream);
|
---|
283 | void AudioMixerSinkRemoveAllStreams(PAUDMIXSINK pSink);
|
---|
284 | void AudioMixerSinkReset(PAUDMIXSINK pSink);
|
---|
285 | void AudioMixerSinkGetFormat(PAUDMIXSINK pSink, PPDMAUDIOPCMPROPS pPCMProps);
|
---|
286 | int AudioMixerSinkSetFormat(PAUDMIXSINK pSink, PPDMAUDIOPCMPROPS pPCMProps);
|
---|
287 | int AudioMixerSinkSetRecordingSource(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream);
|
---|
288 | int AudioMixerSinkSetVolume(PAUDMIXSINK pSink, PPDMAUDIOVOLUME pVol);
|
---|
289 | int AudioMixerSinkWrite(PAUDMIXSINK pSink, AUDMIXOP enmOp, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten);
|
---|
290 | int AudioMixerSinkUpdate(PAUDMIXSINK pSink);
|
---|
291 |
|
---|
292 | int AudioMixerStreamCtl(PAUDMIXSTREAM pStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl);
|
---|
293 | void AudioMixerStreamDestroy(PAUDMIXSTREAM pStream);
|
---|
294 | bool AudioMixerStreamIsActive(PAUDMIXSTREAM pStream);
|
---|
295 | bool AudioMixerStreamIsValid(PAUDMIXSTREAM pStream);
|
---|
296 |
|
---|
297 | #endif /* !VBOX_INCLUDED_SRC_Audio_AudioMixer_h */
|
---|
298 |
|
---|