VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioMixer.h@ 87270

Last change on this file since 87270 was 87270, checked in by vboxsync, 4 years ago

Audio/Mixer: Got rid of the stack-allocated scratch buffer when mixing / multiplexing stuff. Instead, each sink has a heap allocated one now, to avoid hammering the stack. ticketoem2ref:36

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette