1 | /* $Id: AudioMixer.h 77602 2019-03-07 15:10:01Z 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-2019 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 | * Structure for maintaining an audio mixer instance.
|
---|
34 | */
|
---|
35 | typedef struct AUDIOMIXER
|
---|
36 | {
|
---|
37 | /** The mixer's name. */
|
---|
38 | char *pszName;
|
---|
39 | /** The mixer's critical section. */
|
---|
40 | RTCRITSECT CritSect;
|
---|
41 | /** The master volume of this mixer. */
|
---|
42 | PDMAUDIOVOLUME VolMaster;
|
---|
43 | /** List of audio mixer sinks. */
|
---|
44 | RTLISTANCHOR lstSinks;
|
---|
45 | /** Number of used audio sinks. */
|
---|
46 | uint8_t cSinks;
|
---|
47 | } AUDIOMIXER, *PAUDIOMIXER;
|
---|
48 |
|
---|
49 | /** Defines an audio mixer stream's flags. */
|
---|
50 | #define AUDMIXSTREAMFLAGS uint32_t
|
---|
51 |
|
---|
52 | /** No flags specified. */
|
---|
53 | #define AUDMIXSTREAM_FLAG_NONE 0
|
---|
54 | /** The mixing stream is flagged as being enabled (active). */
|
---|
55 | #define AUDMIXSTREAM_FLAG_ENABLED RT_BIT(0)
|
---|
56 |
|
---|
57 | /** Defines an audio mixer stream's internal status. */
|
---|
58 | #define AUDMIXSTREAMSTATUS uint32_t
|
---|
59 |
|
---|
60 | /** No status set. */
|
---|
61 | #define AUDMIXSTREAM_STATUS_NONE 0
|
---|
62 | /** The mixing stream is enabled (active). */
|
---|
63 | #define AUDMIXSTREAM_STATUS_ENABLED RT_BIT(0)
|
---|
64 | /** The mixing stream can be read from. */
|
---|
65 | #define AUDMIXSTREAM_STATUS_CAN_READ RT_BIT(1)
|
---|
66 | /** The mixing stream can be written to. */
|
---|
67 | #define AUDMIXSTREAM_STATUS_CAN_WRITE RT_BIT(2)
|
---|
68 |
|
---|
69 |
|
---|
70 | /** Prototype needed for AUDMIXSTREAM struct definition. */
|
---|
71 | typedef struct AUDMIXSINK *PAUDMIXSINK;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Structure for maintaining an audio mixer stream.
|
---|
75 | */
|
---|
76 | typedef struct AUDMIXSTREAM
|
---|
77 | {
|
---|
78 | /** List node. */
|
---|
79 | RTLISTNODE Node;
|
---|
80 | /** Name of this stream. */
|
---|
81 | char *pszName;
|
---|
82 | /** The streams's critical section. */
|
---|
83 | RTCRITSECT CritSect;
|
---|
84 | /** Sink this stream is attached to. */
|
---|
85 | PAUDMIXSINK pSink;
|
---|
86 | /** Stream flags of type AUDMIXSTREAM_FLAG_. */
|
---|
87 | uint32_t fFlags;
|
---|
88 | /** Stream status of type AUDMIXSTREAM_STATUS_. */
|
---|
89 | uint32_t fStatus;
|
---|
90 | /** Pointer to audio connector being used. */
|
---|
91 | PPDMIAUDIOCONNECTOR pConn;
|
---|
92 | /** Pointer to PDM audio stream this mixer stream handles. */
|
---|
93 | PPDMAUDIOSTREAM pStream;
|
---|
94 | /** Last read (recording) / written (playback) timestamp (in ns). */
|
---|
95 | uint64_t tsLastReadWrittenNs;
|
---|
96 | /** The stream's circular buffer for temporarily
|
---|
97 | * holding (raw) device audio data. */
|
---|
98 | PRTCIRCBUF pCircBuf;
|
---|
99 | } AUDMIXSTREAM, *PAUDMIXSTREAM;
|
---|
100 |
|
---|
101 | /** Defines an audio sink's current status. */
|
---|
102 | #define AUDMIXSINKSTS uint32_t
|
---|
103 |
|
---|
104 | /** No status specified. */
|
---|
105 | #define AUDMIXSINK_STS_NONE 0
|
---|
106 | /** The sink is active and running. */
|
---|
107 | #define AUDMIXSINK_STS_RUNNING RT_BIT(0)
|
---|
108 | /** The sink is in a pending disable state. */
|
---|
109 | #define AUDMIXSINK_STS_PENDING_DISABLE RT_BIT(1)
|
---|
110 | /** Dirty flag.
|
---|
111 | * For output sinks this means that there is data in the
|
---|
112 | * sink which has not been played yet.
|
---|
113 | * For input sinks this means that there is data in the
|
---|
114 | * sink which has been recorded but not transferred to the
|
---|
115 | * destination yet. */
|
---|
116 | #define AUDMIXSINK_STS_DIRTY RT_BIT(2)
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Audio mixer sink direction.
|
---|
120 | */
|
---|
121 | typedef enum AUDMIXSINKDIR
|
---|
122 | {
|
---|
123 | /** Unknown direction. */
|
---|
124 | AUDMIXSINKDIR_UNKNOWN = 0,
|
---|
125 | /** Input (capturing from a device). */
|
---|
126 | AUDMIXSINKDIR_INPUT,
|
---|
127 | /** Output (playing to a device). */
|
---|
128 | AUDMIXSINKDIR_OUTPUT,
|
---|
129 | /** The usual 32-bit hack. */
|
---|
130 | AUDMIXSINKDIR_32BIT_HACK = 0x7fffffff
|
---|
131 | } AUDMIXSINKDIR;
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Audio mixer sink command.
|
---|
135 | */
|
---|
136 | typedef enum AUDMIXSINKCMD
|
---|
137 | {
|
---|
138 | /** Unknown command, do not use. */
|
---|
139 | AUDMIXSINKCMD_UNKNOWN = 0,
|
---|
140 | /** Enables the sink. */
|
---|
141 | AUDMIXSINKCMD_ENABLE,
|
---|
142 | /** Disables the sink. */
|
---|
143 | AUDMIXSINKCMD_DISABLE,
|
---|
144 | /** Pauses the sink. */
|
---|
145 | AUDMIXSINKCMD_PAUSE,
|
---|
146 | /** Resumes the sink. */
|
---|
147 | AUDMIXSINKCMD_RESUME,
|
---|
148 | /** Tells the sink's streams to drop all (buffered) data immediately. */
|
---|
149 | AUDMIXSINKCMD_DROP,
|
---|
150 | /** Hack to blow the type up to 32-bit. */
|
---|
151 | AUDMIXSINKCMD_32BIT_HACK = 0x7fffffff
|
---|
152 | } AUDMIXSINKCMD;
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Structure for keeping audio input sink specifics.
|
---|
156 | * Do not use directly. Instead, use AUDMIXSINK.
|
---|
157 | */
|
---|
158 | typedef struct AUDMIXSINKIN
|
---|
159 | {
|
---|
160 | /** The current recording source. Can be NULL if not set. */
|
---|
161 | PAUDMIXSTREAM pStreamRecSource;
|
---|
162 | } AUDMIXSINKIN;
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Structure for keeping audio output sink specifics.
|
---|
166 | * Do not use directly. Instead, use AUDMIXSINK.
|
---|
167 | */
|
---|
168 | typedef struct AUDMIXSINKOUT
|
---|
169 | {
|
---|
170 | } AUDMIXSINKOUT;
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Structure for maintaining an audio mixer sink.
|
---|
174 | */
|
---|
175 | typedef struct AUDMIXSINK
|
---|
176 | {
|
---|
177 | RTLISTNODE Node;
|
---|
178 | /** Pointer to mixer object this sink is bound to. */
|
---|
179 | PAUDIOMIXER pParent;
|
---|
180 | /** Name of this sink. */
|
---|
181 | char *pszName;
|
---|
182 | /** The sink direction, that is,
|
---|
183 | * if this sink handles input or output. */
|
---|
184 | AUDMIXSINKDIR enmDir;
|
---|
185 | /** The sink's critical section. */
|
---|
186 | RTCRITSECT CritSect;
|
---|
187 | /** This sink's mixing buffer, acting as
|
---|
188 | * a parent buffer for all streams this sink owns. */
|
---|
189 | PDMAUDIOMIXBUF MixBuf;
|
---|
190 | /** Union for input/output specifics. */
|
---|
191 | union
|
---|
192 | {
|
---|
193 | AUDMIXSINKIN In;
|
---|
194 | AUDMIXSINKOUT Out;
|
---|
195 | };
|
---|
196 | /** Sink status of type AUDMIXSINK_STS_XXX. */
|
---|
197 | AUDMIXSINKSTS fStatus;
|
---|
198 | /** The sink's PCM format. */
|
---|
199 | PDMAUDIOPCMPROPS PCMProps;
|
---|
200 | /** Number of streams assigned. */
|
---|
201 | uint8_t cStreams;
|
---|
202 | /** List of assigned streams.
|
---|
203 | * Note: All streams have the same PCM properties, so the
|
---|
204 | * mixer does not do any conversion. */
|
---|
205 | /** @todo Use something faster -- vector maybe? */
|
---|
206 | RTLISTANCHOR lstStreams;
|
---|
207 | /** The volume of this sink. The volume always will
|
---|
208 | * be combined with the mixer's master volume. */
|
---|
209 | PDMAUDIOVOLUME Volume;
|
---|
210 | /** The volume of this sink, combined with the last set master volume. */
|
---|
211 | PDMAUDIOVOLUME VolumeCombined;
|
---|
212 | /** Timestamp since last update (in ms). */
|
---|
213 | uint64_t tsLastUpdatedMs;
|
---|
214 | /** Last read (recording) / written (playback) timestamp (in ns). */
|
---|
215 | uint64_t tsLastReadWrittenNs;
|
---|
216 | #ifdef VBOX_AUDIO_MIXER_DEBUG
|
---|
217 | struct
|
---|
218 | {
|
---|
219 | PPDMAUDIOFILE pFile;
|
---|
220 | } Dbg;
|
---|
221 | #endif
|
---|
222 | } AUDMIXSINK, *PAUDMIXSINK;
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Audio mixer operation.
|
---|
226 | */
|
---|
227 | typedef enum AUDMIXOP
|
---|
228 | {
|
---|
229 | /** Invalid operation, do not use. */
|
---|
230 | AUDMIXOP_INVALID = 0,
|
---|
231 | /** Copy data from A to B, overwriting data in B. */
|
---|
232 | AUDMIXOP_COPY,
|
---|
233 | /** Blend data from A with (existing) data in B. */
|
---|
234 | AUDMIXOP_BLEND,
|
---|
235 | /** The usual 32-bit hack. */
|
---|
236 | AUDMIXOP_32BIT_HACK = 0x7fffffff
|
---|
237 | } AUDMIXOP;
|
---|
238 |
|
---|
239 | /** No flags specified. */
|
---|
240 | #define AUDMIXSTRMCTL_FLAG_NONE 0
|
---|
241 |
|
---|
242 | int AudioMixerCreate(const char *pszName, uint32_t uFlags, PAUDIOMIXER *ppMixer);
|
---|
243 | int AudioMixerCreateSink(PAUDIOMIXER pMixer, const char *pszName, AUDMIXSINKDIR enmDir, PAUDMIXSINK *ppSink);
|
---|
244 | void AudioMixerDestroy(PAUDIOMIXER pMixer);
|
---|
245 | void AudioMixerInvalidate(PAUDIOMIXER pMixer);
|
---|
246 | void AudioMixerRemoveSink(PAUDIOMIXER pMixer, PAUDMIXSINK pSink);
|
---|
247 | int AudioMixerSetMasterVolume(PAUDIOMIXER pMixer, PPDMAUDIOVOLUME pVol);
|
---|
248 | void AudioMixerDebug(PAUDIOMIXER pMixer, PCDBGFINFOHLP pHlp, const char *pszArgs);
|
---|
249 |
|
---|
250 | int AudioMixerSinkAddStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream);
|
---|
251 | int AudioMixerSinkCreateStream(PAUDMIXSINK pSink, PPDMIAUDIOCONNECTOR pConnector, PPDMAUDIOSTREAMCFG pCfg, AUDMIXSTREAMFLAGS fFlags, PAUDMIXSTREAM *ppStream);
|
---|
252 | int AudioMixerSinkCtl(PAUDMIXSINK pSink, AUDMIXSINKCMD enmCmd);
|
---|
253 | void AudioMixerSinkDestroy(PAUDMIXSINK pSink);
|
---|
254 | uint32_t AudioMixerSinkGetReadable(PAUDMIXSINK pSink);
|
---|
255 | uint32_t AudioMixerSinkGetWritable(PAUDMIXSINK pSink);
|
---|
256 | AUDMIXSINKDIR AudioMixerSinkGetDir(PAUDMIXSINK pSink);
|
---|
257 | const char *AudioMixerSinkGetName(const PAUDMIXSINK pSink);
|
---|
258 | PAUDMIXSTREAM AudioMixerSinkGetRecordingSource(PAUDMIXSINK pSink);
|
---|
259 | PAUDMIXSTREAM AudioMixerSinkGetStream(PAUDMIXSINK pSink, uint8_t uIndex);
|
---|
260 | AUDMIXSINKSTS AudioMixerSinkGetStatus(PAUDMIXSINK pSink);
|
---|
261 | uint8_t AudioMixerSinkGetStreamCount(PAUDMIXSINK pSink);
|
---|
262 | bool AudioMixerSinkIsActive(PAUDMIXSINK pSink);
|
---|
263 | int AudioMixerSinkRead(PAUDMIXSINK pSink, AUDMIXOP enmOp, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead);
|
---|
264 | void AudioMixerSinkRemoveStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream);
|
---|
265 | void AudioMixerSinkRemoveAllStreams(PAUDMIXSINK pSink);
|
---|
266 | void AudioMixerSinkReset(PAUDMIXSINK pSink);
|
---|
267 | void AudioMixerSinkGetFormat(PAUDMIXSINK pSink, PPDMAUDIOPCMPROPS pPCMProps);
|
---|
268 | int AudioMixerSinkSetFormat(PAUDMIXSINK pSink, PPDMAUDIOPCMPROPS pPCMProps);
|
---|
269 | int AudioMixerSinkSetRecordingSource(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream);
|
---|
270 | int AudioMixerSinkSetVolume(PAUDMIXSINK pSink, PPDMAUDIOVOLUME pVol);
|
---|
271 | int AudioMixerSinkWrite(PAUDMIXSINK pSink, AUDMIXOP enmOp, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten);
|
---|
272 | int AudioMixerSinkUpdate(PAUDMIXSINK pSink);
|
---|
273 |
|
---|
274 | int AudioMixerStreamCtl(PAUDMIXSTREAM pStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl);
|
---|
275 | void AudioMixerStreamDestroy(PAUDMIXSTREAM pStream);
|
---|
276 | bool AudioMixerStreamIsActive(PAUDMIXSTREAM pStream);
|
---|
277 | bool AudioMixerStreamIsValid(PAUDMIXSTREAM pStream);
|
---|
278 |
|
---|
279 | #endif /* !VBOX_INCLUDED_SRC_Audio_AudioMixer_h */
|
---|
280 |
|
---|