VirtualBox

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

Last change on this file since 63615 was 63615, checked in by vboxsync, 8 years ago

Audio/AudioMixer: Added critical sections for thread safety.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/* $Id: AudioMixer.h 63615 2016-08-22 20:53:07Z vboxsync $ */
2/** @file
3 * VBox audio: Mixing routines, mainly used by the various audio device
4 * emulations to achieve proper multiplexing from/to attached
5 * devices LUNs.
6 */
7
8/*
9 * Copyright (C) 2014-2016 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#ifndef AUDIO_MIXER_H
21#define AUDIO_MIXER_H
22
23#include <iprt/cdefs.h>
24#include <iprt/critsect.h>
25
26#include <VBox/vmm/pdmaudioifs.h>
27
28/**
29 * Structure for maintaining an audio mixer instance.
30 */
31typedef struct AUDIOMIXER
32{
33 /** The mixer's name. */
34 char *pszName;
35 /** The mixer's critical section. */
36 RTCRITSECT CritSect;
37 /** The master volume of this mixer. */
38 PDMAUDIOVOLUME VolMaster;
39 /** List of audio mixer sinks. */
40 RTLISTANCHOR lstSinks;
41 /** Number of used audio sinks. */
42 uint8_t cSinks;
43} AUDIOMIXER, *PAUDIOMIXER;
44
45/** No flags specified. */
46#define AUDMIXSTREAM_FLAG_NONE 0
47
48/** Prototype needed for AUDMIXSTREAM struct definition. */
49typedef struct AUDMIXSINK *PAUDMIXSINK;
50
51/**
52 * Structure for maintaining an audio mixer stream.
53 */
54typedef struct AUDMIXSTREAM
55{
56 /** List node. */
57 RTLISTNODE Node;
58 /** Name of this stream. */
59 char *pszName;
60 /** The streams's critical section. */
61 RTCRITSECT CritSect;
62 /** Sink this stream is attached to. */
63 PAUDMIXSINK pSink;
64 /** Stream flags of type AUDMIXSTREAM_FLAG_. */
65 uint32_t fFlags;
66 /** Pointer to audio connector being used. */
67 PPDMIAUDIOCONNECTOR pConn;
68 /** Pointer to PDM audio stream this mixer stream handles. */
69 PPDMAUDIOSTREAM pStream;
70} AUDMIXSTREAM, *PAUDMIXSTREAM;
71
72/** Defines an audio sink's current status. */
73#define AUDMIXSINKSTS uint32_t
74
75/** No status specified. */
76#define AUDMIXSINK_STS_NONE 0
77/** The sink is active and running. */
78#define AUDMIXSINK_STS_RUNNING RT_BIT(0)
79/** The sink is in a pending disable state. */
80#define AUDMIXSINK_STS_PENDING_DISABLE RT_BIT(1)
81/** Dirty flag.
82 * For output sinks this means that there is data in the
83 * sink which has not been played yet.
84 * For input sinks this means that there is data in the
85 * sink which has been recorded but not transferred to the
86 * destination yet. */
87#define AUDMIXSINK_STS_DIRTY RT_BIT(2)
88
89/**
90 * Audio mixer sink direction.
91 */
92typedef enum AUDMIXSINKDIR
93{
94 /** Unknown direction. */
95 AUDMIXSINKDIR_UNKNOWN = 0,
96 /** Input (capturing from a device). */
97 AUDMIXSINKDIR_INPUT,
98 /** Output (playing to a device). */
99 AUDMIXSINKDIR_OUTPUT,
100 /** The usual 32-bit hack. */
101 AUDMIXSINKDIR_32BIT_HACK = 0x7fffffff
102} AUDMIXSINKDIR;
103
104/**
105 * Audio mixer sink command.
106 */
107typedef enum AUDMIXSINKCMD
108{
109 /** Unknown command, do not use. */
110 AUDMIXSINKCMD_UNKNOWN = 0,
111 /** Enables the sink. */
112 AUDMIXSINKCMD_ENABLE,
113 /** Disables the sink. */
114 AUDMIXSINKCMD_DISABLE,
115 /** Pauses the sink. */
116 AUDMIXSINKCMD_PAUSE,
117 /** Resumes the sink. */
118 AUDMIXSINKCMD_RESUME,
119 /** Hack to blow the type up to 32-bit. */
120 AUDMIXSINKCMD_32BIT_HACK = 0x7fffffff
121} AUDMIXSINKCMD;
122
123/**
124 * Structure for keeping audio input sink specifics.
125 * Do not use directly. Instead, use AUDMIXSINK.
126 */
127typedef struct AUDMIXSINKIN
128{
129#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
130 /** This sink's mixing buffer, acting as
131 * a parent buffer for all streams this sink owns. */
132 PDMAUDIOMIXBUF MixBuf;
133#else
134 /** Number of bytes available to read from the sink. */
135 uint32_t cbReadable;
136#endif
137} AUDMIXSINKIN;
138
139/**
140 * Structure for keeping audio output sink specifics.
141 * Do not use directly. Instead, use AUDMIXSINK.
142 */
143typedef struct AUDMIXSINKOUT
144{
145#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
146 /** This sink's mixing buffer, acting as
147 * a parent buffer for all streams this sink owns. */
148 PDMAUDIOMIXBUF MixBuf;
149#else
150 /** Number of bytes available to write to the sink. */
151 uint32_t cbWritable;
152#endif
153} AUDMIXSINKOUT;
154
155/**
156 * Structure for maintaining an audio mixer sink.
157 */
158typedef struct AUDMIXSINK
159{
160 RTLISTNODE Node;
161 /** Pointer to mixer object this sink is bound to. */
162 PAUDIOMIXER pParent;
163 /** Name of this sink. */
164 char *pszName;
165 /** The sink direction, that is,
166 * if this sink handles input or output. */
167 AUDMIXSINKDIR enmDir;
168 /** The sink's critical section. */
169 RTCRITSECT CritSect;
170 /** Union for input/output specifics. */
171 union
172 {
173 AUDMIXSINKIN In;
174 AUDMIXSINKOUT Out;
175 };
176 /** Sink status of type AUDMIXSINK_STS_XXX. */
177 AUDMIXSINKSTS fStatus;
178 /** The sink's PCM format. */
179 PDMAUDIOPCMPROPS PCMProps;
180 /** Number of streams assigned. */
181 uint8_t cStreams;
182 /** List of assigned streams.
183 * Note: All streams have the same PCM properties, so the
184 * mixer does not do any conversion. */
185 /** @todo Use something faster -- vector maybe? */
186 RTLISTANCHOR lstStreams;
187 /** The volume of this sink. The volume always will
188 * be combined with the mixer's master volume. */
189 PDMAUDIOVOLUME Volume;
190 /** The volume of this sink, combined with the last set master volume. */
191 PDMAUDIOVOLUME VolumeCombined;
192 /** Timestamp (in ms) since last update. */
193 uint64_t tsLastUpdatedMS;
194} AUDMIXSINK, *PAUDMIXSINK;
195
196/**
197 * Audio mixer operation.
198 */
199typedef enum AUDMIXOP
200{
201 /** Invalid operation, do not use. */
202 AUDMIXOP_INVALID = 0,
203 /** Copy data from A to B, overwriting data in B. */
204 AUDMIXOP_COPY,
205 /** Blend data from A with (existing) data in B. */
206 AUDMIXOP_BLEND,
207 /** The usual 32-bit hack. */
208 AUDMIXOP_32BIT_HACK = 0x7fffffff
209} AUDMIXOP;
210
211/** No flags specified. */
212#define AUDMIXSTRMCTL_FLAG_NONE 0
213
214int AudioMixerCreate(const char *pszName, uint32_t uFlags, PAUDIOMIXER *ppMixer);
215int AudioMixerCreateSink(PAUDIOMIXER pMixer, const char *pszName, AUDMIXSINKDIR enmDir, PAUDMIXSINK *ppSink);
216void AudioMixerDestroy(PAUDIOMIXER pMixer);
217void AudioMixerInvalidate(PAUDIOMIXER pMixer);
218void AudioMixerRemoveSink(PAUDIOMIXER pMixer, PAUDMIXSINK pSink);
219int AudioMixerSetMasterVolume(PAUDIOMIXER pMixer, PPDMAUDIOVOLUME pVol);
220#ifdef DEBUG
221void AudioMixerDebug(PAUDIOMIXER pMixer, PCDBGFINFOHLP pHlp, const char *pszArgs);
222#endif
223
224int AudioMixerSinkAddStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream);
225int AudioMixerSinkCreateStream(PAUDMIXSINK pSink, PPDMIAUDIOCONNECTOR pConnector, PPDMAUDIOSTREAMCFG pCfg, uint32_t fFlags, PAUDMIXSTREAM *ppStream);
226int AudioMixerSinkCtl(PAUDMIXSINK pSink, AUDMIXSINKCMD enmCmd);
227void AudioMixerSinkDestroy(PAUDMIXSINK pSink);
228uint32_t AudioMixerSinkGetReadable(PAUDMIXSINK pSink);
229uint32_t AudioMixerSinkGetWritable(PAUDMIXSINK pSink);
230AUDMIXSINKDIR AudioMixerSinkGetDir(PAUDMIXSINK pSink);
231PAUDMIXSTREAM AudioMixerSinkGetStream(PAUDMIXSINK pSink, uint8_t uIndex);
232AUDMIXSINKSTS AudioMixerSinkGetStatus(PAUDMIXSINK pSink);
233uint8_t AudioMixerSinkGetStreamCount(PAUDMIXSINK pSink);
234int AudioMixerSinkRead(PAUDMIXSINK pSink, AUDMIXOP enmOp, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead);
235void AudioMixerSinkRemoveStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream);
236void AudioMixerSinkRemoveAllStreams(PAUDMIXSINK pSink);
237int AudioMixerSinkSetFormat(PAUDMIXSINK pSink, PPDMAUDIOPCMPROPS pPCMProps);
238int AudioMixerSinkSetVolume(PAUDMIXSINK pSink, PPDMAUDIOVOLUME pVol);
239int AudioMixerSinkWrite(PAUDMIXSINK pSink, AUDMIXOP enmOp, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten);
240int AudioMixerSinkUpdate(PAUDMIXSINK pSink);
241
242int AudioMixerStreamCtl(PAUDMIXSTREAM pStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl);
243void AudioMixerStreamDestroy(PAUDMIXSTREAM pStream);
244bool AudioMixerStreamIsActive(PAUDMIXSTREAM pStream);
245bool AudioMixerStreamIsValid(PAUDMIXSTREAM pStream);
246
247#endif /* AUDIO_MIXER_H */
248
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