VirtualBox

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

Last change on this file since 73441 was 73421, checked in by vboxsync, 7 years ago

Audio: (Pre-)Buffering fixes; use nanoseconds to have a higher resolution when it comes to calculating the transfer sizes.

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

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