VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioMixer.cpp@ 69119

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

Audio: More cleanups (missing keywords, incorrect #endif docs, stuff)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.7 KB
Line 
1/* $Id: AudioMixer.cpp 69119 2017-10-17 19:08:38Z vboxsync $ */
2/** @file
3 * Audio mixing routines for multiplexing audio sources in device emulations.
4 *
5 * This mixer acts as a layer between the audio connector interface and
6 * the actual device emulation, providing mechanisms for audio sources (input)
7 * and audio sinks (output).
8 *
9 * Think of this mixer as kind of a high(er) level interface for the audio
10 * connector interface, abstracting common tasks such as creating and managing
11 * various audio sources and sinks. This mixer class is purely optional and can
12 * be left out when implementing a new device emulation, using only the audi
13 * connector interface instead. For example, the SB16 emulation does not use
14 * this mixer and does all its stream management on its own.
15 *
16 * As audio driver instances are handled as LUNs on the device level, this
17 * audio mixer then can take care of e.g. mixing various inputs/outputs to/from
18 * a specific source/sink.
19 *
20 * How and which audio streams are connected to sinks/sources depends on how
21 * the audio mixer has been set up.
22 *
23 * A sink can connect multiple output streams together, whereas a source
24 * does this with input streams. Each sink / source consists of one or more
25 * so-called mixer streams, which then in turn have pointers to the actual
26 * PDM audio input/output streams.
27 */
28
29/*
30 * Copyright (C) 2014-2017 Oracle Corporation
31 *
32 * This file is part of VirtualBox Open Source Edition (OSE), as
33 * available from http://www.virtualbox.org. This file is free software;
34 * you can redistribute it and/or modify it under the terms of the GNU
35 * General Public License (GPL) as published by the Free Software
36 * Foundation, in version 2 as it comes in the "COPYING" file of the
37 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
38 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
39 */
40
41
42/*********************************************************************************************************************************
43* Header Files *
44*********************************************************************************************************************************/
45#define LOG_GROUP LOG_GROUP_AUDIO_MIXER
46#include <VBox/log.h>
47#include "AudioMixer.h"
48#include "AudioMixBuffer.h"
49#include "DrvAudio.h"
50
51#include <VBox/vmm/pdm.h>
52#include <VBox/err.h>
53#include <VBox/vmm/mm.h>
54#include <VBox/vmm/pdmaudioifs.h>
55
56#include <iprt/alloc.h>
57#include <iprt/asm-math.h>
58#include <iprt/assert.h>
59#include <iprt/string.h>
60
61
62/*********************************************************************************************************************************
63* Internal Functions *
64*********************************************************************************************************************************/
65static int audioMixerRemoveSinkInternal(PAUDIOMIXER pMixer, PAUDMIXSINK pSink);
66
67static void audioMixerSinkDestroyInternal(PAUDMIXSINK pSink);
68static int audioMixerSinkUpdateVolume(PAUDMIXSINK pSink, const PPDMAUDIOVOLUME pVolMaster);
69static void audioMixerSinkRemoveAllStreamsInternal(PAUDMIXSINK pSink);
70static int audioMixerSinkRemoveStreamInternal(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream);
71static void audioMixerSinkReset(PAUDMIXSINK pSink);
72static int audioMixerSinkUpdateInternal(PAUDMIXSINK pSink);
73
74int audioMixerStreamCtlInternal(PAUDMIXSTREAM pMixStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl);
75static void audioMixerStreamDestroyInternal(PAUDMIXSTREAM pStream);
76
77
78#ifdef LOG_ENABLED
79/**
80 * Converts a mixer sink status to a string.
81 *
82 * @returns Stringified mixer sink flags. Must be free'd with RTStrFree().
83 * "NONE" if no flags set.
84 * @param fFlags Mixer sink flags to convert.
85 */
86static char *dbgAudioMixerSinkStatusToStr(AUDMIXSINKSTS fStatus)
87{
88#define APPEND_FLAG_TO_STR(_aFlag) \
89 if (fStatus & AUDMIXSINK_STS_##_aFlag) \
90 { \
91 if (pszFlags) \
92 { \
93 rc2 = RTStrAAppend(&pszFlags, " "); \
94 if (RT_FAILURE(rc2)) \
95 break; \
96 } \
97 \
98 rc2 = RTStrAAppend(&pszFlags, #_aFlag); \
99 if (RT_FAILURE(rc2)) \
100 break; \
101 } \
102
103 char *pszFlags = NULL;
104 int rc2 = VINF_SUCCESS;
105
106 do
107 {
108 APPEND_FLAG_TO_STR(NONE);
109 APPEND_FLAG_TO_STR(RUNNING);
110 APPEND_FLAG_TO_STR(PENDING_DISABLE);
111 APPEND_FLAG_TO_STR(DIRTY);
112
113 } while (0);
114
115 if ( RT_FAILURE(rc2)
116 && pszFlags)
117 {
118 RTStrFree(pszFlags);
119 pszFlags = NULL;
120 }
121
122#undef APPEND_FLAG_TO_STR
123
124 return pszFlags;
125}
126#endif /* DEBUG */
127
128/**
129 * Creates an audio sink and attaches it to the given mixer.
130 *
131 * @returns IPRT status code.
132 * @param pMixer Mixer to attach created sink to.
133 * @param pszName Name of the sink to create.
134 * @param enmDir Direction of the sink to create.
135 * @param ppSink Pointer which returns the created sink on success.
136 */
137int AudioMixerCreateSink(PAUDIOMIXER pMixer, const char *pszName, AUDMIXSINKDIR enmDir, PAUDMIXSINK *ppSink)
138{
139 AssertPtrReturn(pMixer, VERR_INVALID_POINTER);
140 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
141 /* ppSink is optional. */
142
143 int rc = RTCritSectEnter(&pMixer->CritSect);
144 if (RT_FAILURE(rc))
145 return rc;
146
147 PAUDMIXSINK pSink = (PAUDMIXSINK)RTMemAllocZ(sizeof(AUDMIXSINK));
148 if (pSink)
149 {
150 pSink->pszName = RTStrDup(pszName);
151 if (!pSink->pszName)
152 rc = VERR_NO_MEMORY;
153
154 if (RT_SUCCESS(rc))
155 rc = RTCritSectInit(&pSink->CritSect);
156
157 if (RT_SUCCESS(rc))
158 {
159 pSink->pParent = pMixer;
160 pSink->enmDir = enmDir;
161 RTListInit(&pSink->lstStreams);
162
163 /* Set initial volume to max. */
164 pSink->Volume.fMuted = false;
165 pSink->Volume.uLeft = PDMAUDIO_VOLUME_MAX;
166 pSink->Volume.uRight = PDMAUDIO_VOLUME_MAX;
167
168 /* Ditto for the combined volume. */
169 pSink->VolumeCombined.fMuted = false;
170 pSink->VolumeCombined.uLeft = PDMAUDIO_VOLUME_MAX;
171 pSink->VolumeCombined.uRight = PDMAUDIO_VOLUME_MAX;
172
173 RTListAppend(&pMixer->lstSinks, &pSink->Node);
174 pMixer->cSinks++;
175
176 LogFlowFunc(("pMixer=%p, pSink=%p, cSinks=%RU8\n",
177 pMixer, pSink, pMixer->cSinks));
178
179 if (ppSink)
180 *ppSink = pSink;
181 }
182
183 if (RT_FAILURE(rc))
184 {
185 RTCritSectDelete(&pSink->CritSect);
186
187 if (pSink)
188 {
189 RTMemFree(pSink);
190 pSink = NULL;
191 }
192 }
193 }
194 else
195 rc = VERR_NO_MEMORY;
196
197 int rc2 = RTCritSectLeave(&pMixer->CritSect);
198 AssertRC(rc2);
199
200 return rc;
201}
202
203/**
204 * Creates an audio mixer.
205 *
206 * @returns IPRT status code.
207 * @param pszName Name of the audio mixer.
208 * @param fFlags Creation flags. Not used at the moment and must be 0.
209 * @param ppMixer Pointer which returns the created mixer object.
210 */
211int AudioMixerCreate(const char *pszName, uint32_t fFlags, PAUDIOMIXER *ppMixer)
212{
213 RT_NOREF(fFlags);
214 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
215 /** @todo Add fFlags validation. */
216 AssertPtrReturn(ppMixer, VERR_INVALID_POINTER);
217
218 int rc = VINF_SUCCESS;
219
220 PAUDIOMIXER pMixer = (PAUDIOMIXER)RTMemAllocZ(sizeof(AUDIOMIXER));
221 if (pMixer)
222 {
223 pMixer->pszName = RTStrDup(pszName);
224 if (!pMixer->pszName)
225 rc = VERR_NO_MEMORY;
226
227 if (RT_SUCCESS(rc))
228 rc = RTCritSectInit(&pMixer->CritSect);
229
230 if (RT_SUCCESS(rc))
231 {
232 pMixer->cSinks = 0;
233 RTListInit(&pMixer->lstSinks);
234
235 /* Set master volume to the max. */
236 pMixer->VolMaster.fMuted = false;
237 pMixer->VolMaster.uLeft = PDMAUDIO_VOLUME_MAX;
238 pMixer->VolMaster.uRight = PDMAUDIO_VOLUME_MAX;
239
240 LogFlowFunc(("Created mixer '%s'\n", pMixer->pszName));
241
242 *ppMixer = pMixer;
243 }
244 else
245 RTMemFree(pMixer);
246 }
247 else
248 rc = VERR_NO_MEMORY;
249
250 LogFlowFuncLeaveRC(rc);
251 return rc;
252}
253
254/**
255 * Helper function for the internal debugger to print the mixer's current
256 * state, along with the attached sinks.
257 *
258 * @param pMixer Mixer to print debug output for.
259 * @param pHlp Debug info helper to use.
260 * @param pszArgs Optional arguments. Not being used at the moment.
261 */
262void AudioMixerDebug(PAUDIOMIXER pMixer, PCDBGFINFOHLP pHlp, const char *pszArgs)
263{
264 RT_NOREF(pszArgs);
265 PAUDMIXSINK pSink;
266 unsigned iSink = 0;
267
268 int rc2 = RTCritSectEnter(&pMixer->CritSect);
269 if (RT_FAILURE(rc2))
270 return;
271
272 pHlp->pfnPrintf(pHlp, "[Master] %s: lVol=%u, rVol=%u, fMuted=%RTbool\n", pMixer->pszName,
273 pMixer->VolMaster.uLeft, pMixer->VolMaster.uRight, pMixer->VolMaster.fMuted);
274
275 RTListForEach(&pMixer->lstSinks, pSink, AUDMIXSINK, Node)
276 {
277 pHlp->pfnPrintf(pHlp, "[Sink %u] %s: lVol=%u, rVol=%u, fMuted=%RTbool\n", iSink, pSink->pszName,
278 pSink->Volume.uLeft, pSink->Volume.uRight, pSink->Volume.fMuted);
279 ++iSink;
280 }
281
282 rc2 = RTCritSectLeave(&pMixer->CritSect);
283 AssertRC(rc2);
284}
285
286/**
287 * Destroys an audio mixer.
288 *
289 * @param pMixer Audio mixer to destroy.
290 */
291void AudioMixerDestroy(PAUDIOMIXER pMixer)
292{
293 if (!pMixer)
294 return;
295
296 int rc2 = RTCritSectEnter(&pMixer->CritSect);
297 AssertRC(rc2);
298
299 LogFlowFunc(("Destroying %s ...\n", pMixer->pszName));
300
301 PAUDMIXSINK pSink, pSinkNext;
302 RTListForEachSafe(&pMixer->lstSinks, pSink, pSinkNext, AUDMIXSINK, Node)
303 {
304 /* Save a pointer to the sink to remove, as pSink
305 * will not be valid anymore after calling audioMixerRemoveSinkInternal(). */
306 PAUDMIXSINK pSinkToRemove = pSink;
307
308 audioMixerRemoveSinkInternal(pMixer, pSinkToRemove);
309 audioMixerSinkDestroyInternal(pSinkToRemove);
310 }
311
312 pMixer->cSinks = 0;
313
314 if (pMixer->pszName)
315 {
316 RTStrFree(pMixer->pszName);
317 pMixer->pszName = NULL;
318 }
319
320 rc2 = RTCritSectLeave(&pMixer->CritSect);
321 AssertRC(rc2);
322
323 RTCritSectDelete(&pMixer->CritSect);
324
325 RTMemFree(pMixer);
326 pMixer = NULL;
327}
328
329/**
330 * Invalidates all internal data, internal version.
331 *
332 * @returns IPRT status code.
333 * @param pMixer Mixer to invalidate data for.
334 */
335int audioMixerInvalidateInternal(PAUDIOMIXER pMixer)
336{
337 AssertPtrReturn(pMixer, VERR_INVALID_POINTER);
338
339 LogFlowFunc(("[%s]\n", pMixer->pszName));
340
341 /* Propagate new master volume to all connected sinks. */
342 PAUDMIXSINK pSink;
343 RTListForEach(&pMixer->lstSinks, pSink, AUDMIXSINK, Node)
344 {
345 int rc2 = audioMixerSinkUpdateVolume(pSink, &pMixer->VolMaster);
346 AssertRC(rc2);
347 }
348
349 return VINF_SUCCESS;
350}
351
352/**
353 * Invalidates all internal data.
354 *
355 * @returns IPRT status code.
356 * @param pMixer Mixer to invalidate data for.
357 */
358void AudioMixerInvalidate(PAUDIOMIXER pMixer)
359{
360 AssertPtrReturnVoid(pMixer);
361
362 int rc2 = RTCritSectEnter(&pMixer->CritSect);
363 AssertRC(rc2);
364
365 LogFlowFunc(("[%s]\n", pMixer->pszName));
366
367 rc2 = audioMixerInvalidateInternal(pMixer);
368 AssertRC(rc2);
369
370 rc2 = RTCritSectLeave(&pMixer->CritSect);
371 AssertRC(rc2);
372}
373
374/**
375 * Removes a formerly attached audio sink for an audio mixer, internal version.
376 *
377 * @returns IPRT status code.
378 * @param pMixer Mixer to remove sink from.
379 * @param pSink Sink to remove.
380 */
381static int audioMixerRemoveSinkInternal(PAUDIOMIXER pMixer, PAUDMIXSINK pSink)
382{
383 AssertPtrReturn(pMixer, VERR_INVALID_POINTER);
384 if (!pSink)
385 return VERR_NOT_FOUND;
386
387 AssertMsgReturn(pSink->pParent == pMixer, ("%s: Is not part of mixer '%s'\n",
388 pSink->pszName, pMixer->pszName), VERR_NOT_FOUND);
389
390 LogFlowFunc(("[%s] pSink=%s, cSinks=%RU8\n",
391 pMixer->pszName, pSink->pszName, pMixer->cSinks));
392
393 /* Remove sink from mixer. */
394 RTListNodeRemove(&pSink->Node);
395 Assert(pMixer->cSinks);
396
397 /* Set mixer to NULL so that we know we're not part of any mixer anymore. */
398 pSink->pParent = NULL;
399
400 return VINF_SUCCESS;
401}
402
403/**
404 * Removes a formerly attached audio sink for an audio mixer.
405 *
406 * @returns IPRT status code.
407 * @param pMixer Mixer to remove sink from.
408 * @param pSink Sink to remove.
409 */
410void AudioMixerRemoveSink(PAUDIOMIXER pMixer, PAUDMIXSINK pSink)
411{
412 int rc2 = RTCritSectEnter(&pMixer->CritSect);
413 AssertRC(rc2);
414
415 audioMixerSinkRemoveAllStreamsInternal(pSink);
416 audioMixerRemoveSinkInternal(pMixer, pSink);
417
418 rc2 = RTCritSectLeave(&pMixer->CritSect);
419}
420
421/**
422 * Sets the mixer's master volume.
423 *
424 * @returns IPRT status code.
425 * @param pMixer Mixer to set master volume for.
426 * @param pVol Volume to set.
427 */
428int AudioMixerSetMasterVolume(PAUDIOMIXER pMixer, PPDMAUDIOVOLUME pVol)
429{
430 AssertPtrReturn(pMixer, VERR_INVALID_POINTER);
431 AssertPtrReturn(pVol, VERR_INVALID_POINTER);
432
433 int rc = RTCritSectEnter(&pMixer->CritSect);
434 if (RT_FAILURE(rc))
435 return rc;
436
437 memcpy(&pMixer->VolMaster, pVol, sizeof(PDMAUDIOVOLUME));
438
439 LogFlowFunc(("[%s] lVol=%RU32, rVol=%RU32 => fMuted=%RTbool, lVol=%RU32, rVol=%RU32\n",
440 pMixer->pszName, pVol->uLeft, pVol->uRight,
441 pMixer->VolMaster.fMuted, pMixer->VolMaster.uLeft, pMixer->VolMaster.uRight));
442
443 rc = audioMixerInvalidateInternal(pMixer);
444
445 int rc2 = RTCritSectLeave(&pMixer->CritSect);
446 AssertRC(rc2);
447
448 return rc;
449}
450
451/*********************************************************************************************************************************
452 * Mixer Sink implementation.
453 ********************************************************************************************************************************/
454
455/**
456 * Adds an audio stream to a specific audio sink.
457 *
458 * @returns IPRT status code.
459 * @param pSink Sink to add audio stream to.
460 * @param pStream Stream to add.
461 */
462int AudioMixerSinkAddStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream)
463{
464 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
465 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
466
467 int rc = RTCritSectEnter(&pSink->CritSect);
468 if (RT_FAILURE(rc))
469 return rc;
470
471 if (pSink->cStreams == UINT8_MAX) /* 255 streams per sink max. */
472 {
473 int rc2 = RTCritSectLeave(&pSink->CritSect);
474 AssertRC(rc2);
475
476 return VERR_NO_MORE_HANDLES;
477 }
478
479 LogFlowFuncEnter();
480
481#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
482 /* Make sure only compatible streams are added. */
483 if (pStream->enmDir == PDMAUDIODIR_IN)
484 {
485 if (DrvAudioHlpPCMPropsAreEqual(&pSink->PCMProps, &pStream->InOut.pIn->Props))
486 {
487#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
488 /* Chain: Stream (Child) -> Sink (Child) -> Guest (Parent). */
489 PPDMAUDIOMIXBUF pHstIn = &pStream->InOut.pIn->pHstStrmIn->MixBuf;
490 PPDMAUDIOMIXBUF pGstIn = &pStream->InOut.pIn->MixBuf;
491
492 /* Unlink any former parent from host input. */
493 AudioMixBufUnlink(pHstIn);
494
495 /* Link host input to this sink as a parent. */
496 rc = AudioMixBufLinkTo(pHstIn, &pSink->MixBuf);
497 AssertRC(rc);
498
499 /* Unlink any former parent from this sink. */
500 AudioMixBufUnlink(&pSink->MixBuf);
501
502 /* Link guest input to this sink as a parent. */
503 rc = AudioMixBufLinkTo(&pSink->MixBuf, pGstIn);
504 AssertRC(rc);
505# ifdef DEBUG
506 AudioMixBufDbgPrintChain(&pStream->InOut.pIn->MixBuf);
507# endif
508#endif /* VBOX_AUDIO_MIXER_WITH_MIXBUF */
509 }
510 else
511 AssertFailedStmt(rc = VERR_WRONG_TYPE);
512 }
513 else if (pStream->enmDir == PDMAUDIODIR_OUT)
514 {
515 if (DrvAudioHlpPCMPropsAreEqual(&pSink->PCMProps, &pStream->InOut.pOut->Props))
516 {
517#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
518 /* Chain: Guest (Child) -> Sink (Child) -> Stream (Parent). */
519 rc = AudioMixBufLinkTo(&pStream->InOut.pOut->pHstStrmOut->MixBuf, &pSink->MixBuf);
520# ifdef DEBUG
521 AudioMixBufDbgPrintChain(&pSink->MixBuf);
522# endif
523#endif /* VBOX_AUDIO_MIXER_WITH_MIXBUF */
524 }
525 else
526 AssertFailedStmt(rc = VERR_WRONG_TYPE);
527 }
528 else
529 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
530#else
531 rc = VINF_SUCCESS;
532#endif
533
534 if (RT_SUCCESS(rc))
535 {
536 /** @todo Check if stream already is assigned to (another) sink. */
537
538 /* If the sink is running and not in pending disable mode,
539 * make sure that the added stream also is enabled. */
540 if ( (pSink->fStatus & AUDMIXSINK_STS_RUNNING)
541 && !(pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE))
542 {
543 rc = audioMixerStreamCtlInternal(pStream, PDMAUDIOSTREAMCMD_ENABLE, AUDMIXSTRMCTL_FLAG_NONE);
544 }
545
546 if (RT_SUCCESS(rc))
547 {
548 /* Apply the sink's combined volume to the stream. */
549 rc = pStream->pConn->pfnStreamSetVolume(pStream->pConn, pStream->pStream, &pSink->VolumeCombined);
550 AssertRC(rc);
551 }
552
553 if (RT_SUCCESS(rc))
554 {
555 /* Save pointer to sink the stream is attached to. */
556 pStream->pSink = pSink;
557
558 /* Append stream to sink's list. */
559 RTListAppend(&pSink->lstStreams, &pStream->Node);
560 pSink->cStreams++;
561 }
562 }
563
564 LogFlowFunc(("[%s] cStreams=%RU8, rc=%Rrc\n", pSink->pszName, pSink->cStreams, rc));
565
566 int rc2 = RTCritSectLeave(&pSink->CritSect);
567 AssertRC(rc2);
568
569 return rc;
570}
571
572/**
573 * Creates an audio mixer stream.
574 *
575 * @returns IPRT status code.
576 * @param pSink Sink to use for creating the stream.
577 * @param pConn Audio connector interface to use.
578 * @param pCfg Audio stream configuration to use.
579 * @param fFlags Stream creation flags. Currently unused, set to 0.
580 * @param ppStream Pointer which receives the newly created audio stream.
581 */
582int AudioMixerSinkCreateStream(PAUDMIXSINK pSink,
583 PPDMIAUDIOCONNECTOR pConn, PPDMAUDIOSTREAMCFG pCfg, uint32_t fFlags, PAUDMIXSTREAM *ppStream)
584{
585 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
586 AssertPtrReturn(pConn, VERR_INVALID_POINTER);
587 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
588 /** @todo Validate fFlags. */
589 /* ppStream is optional. */
590
591 PAUDMIXSTREAM pMixStream = (PAUDMIXSTREAM)RTMemAllocZ(sizeof(AUDMIXSTREAM));
592 if (!pMixStream)
593 return VERR_NO_MEMORY;
594
595 pMixStream->pszName = RTStrDup(pCfg->szName);
596 if (!pMixStream->pszName)
597 {
598 RTMemFree(pMixStream);
599 return VERR_NO_MEMORY;
600 }
601
602 int rc = RTCritSectEnter(&pSink->CritSect);
603 if (RT_FAILURE(rc))
604 return rc;
605
606 LogFlowFunc(("[%s] fFlags=0x%x (enmDir=%ld, %RU8 bits, %RU8 channels, %RU32Hz)\n",
607 pSink->pszName, fFlags, pCfg->enmDir, pCfg->Props.cBits, pCfg->Props.cChannels, pCfg->Props.uHz));
608
609 /*
610 * Initialize the host-side configuration for the stream to be created.
611 * Always use the sink's PCM audio format as the host side when creating a stream for it.
612 */
613 PDMAUDIOSTREAMCFG CfgHost;
614 rc = DrvAudioHlpPCMPropsToStreamCfg(&pSink->PCMProps, &CfgHost);
615 AssertRCReturn(rc, rc);
616
617 /* Apply the sink's direction for the configuration to use to
618 * create the stream. */
619 if (pSink->enmDir == AUDMIXSINKDIR_INPUT)
620 {
621 CfgHost.DestSource.Source = pCfg->DestSource.Source;
622 CfgHost.enmDir = PDMAUDIODIR_IN;
623 CfgHost.enmLayout = pCfg->enmLayout;
624 }
625 else
626 {
627 CfgHost.DestSource.Dest = pCfg->DestSource.Dest;
628 CfgHost.enmDir = PDMAUDIODIR_OUT;
629 CfgHost.enmLayout = pCfg->enmLayout;
630 }
631
632 RTStrPrintf(CfgHost.szName, sizeof(CfgHost.szName), "%s", pCfg->szName);
633
634 rc = RTCritSectInit(&pMixStream->CritSect);
635 if (RT_SUCCESS(rc))
636 {
637 PPDMAUDIOSTREAM pStream;
638 rc = pConn->pfnStreamCreate(pConn, &CfgHost, pCfg, &pStream);
639 if (RT_SUCCESS(rc))
640 {
641 /* Save the audio stream pointer to this mixing stream. */
642 pMixStream->pStream = pStream;
643
644 /* Increase the stream's reference count to let others know
645 * we're reyling on it to be around now. */
646 pConn->pfnStreamRetain(pConn, pStream);
647 }
648 }
649
650 if (RT_SUCCESS(rc))
651 {
652 pMixStream->fFlags = fFlags;
653 pMixStream->pConn = pConn;
654
655 if (ppStream)
656 *ppStream = pMixStream;
657 }
658 else if (pMixStream)
659 {
660 int rc2 = RTCritSectDelete(&pMixStream->CritSect);
661 AssertRC(rc2);
662
663 if (pMixStream->pszName)
664 {
665 RTStrFree(pMixStream->pszName);
666 pMixStream->pszName = NULL;
667 }
668
669 RTMemFree(pMixStream);
670 pMixStream = NULL;
671 }
672
673 int rc2 = RTCritSectLeave(&pSink->CritSect);
674 AssertRC(rc2);
675
676 return rc;
677}
678
679/**
680 * Static helper function to translate a sink command
681 * to a PDM audio stream command.
682 *
683 * @returns PDM audio stream command, or PDMAUDIOSTREAMCMD_UNKNOWN if not found.
684 * @param enmCmd Mixer sink command to translate.
685 */
686static PDMAUDIOSTREAMCMD audioMixerSinkToStreamCmd(AUDMIXSINKCMD enmCmd)
687{
688 switch (enmCmd)
689 {
690 case AUDMIXSINKCMD_ENABLE: return PDMAUDIOSTREAMCMD_ENABLE;
691 case AUDMIXSINKCMD_DISABLE: return PDMAUDIOSTREAMCMD_DISABLE;
692 case AUDMIXSINKCMD_PAUSE: return PDMAUDIOSTREAMCMD_PAUSE;
693 case AUDMIXSINKCMD_RESUME: return PDMAUDIOSTREAMCMD_RESUME;
694 default: break;
695 }
696
697 AssertMsgFailed(("Unsupported sink command %d\n", enmCmd));
698 return PDMAUDIOSTREAMCMD_UNKNOWN;
699}
700
701/**
702 * Controls a mixer sink.
703 *
704 * @returns IPRT status code.
705 * @param pSink Mixer sink to control.
706 * @param enmSinkCmd Sink command to set.
707 */
708int AudioMixerSinkCtl(PAUDMIXSINK pSink, AUDMIXSINKCMD enmSinkCmd)
709{
710 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
711
712 PDMAUDIOSTREAMCMD enmCmdStream = audioMixerSinkToStreamCmd(enmSinkCmd);
713 if (enmCmdStream == PDMAUDIOSTREAMCMD_UNKNOWN)
714 return VERR_NOT_SUPPORTED;
715
716 int rc = RTCritSectEnter(&pSink->CritSect);
717 if (RT_FAILURE(rc))
718 return rc;
719
720 PAUDMIXSTREAM pStream;
721 RTListForEach(&pSink->lstStreams, pStream, AUDMIXSTREAM, Node)
722 {
723 int rc2 = audioMixerStreamCtlInternal(pStream, enmCmdStream, AUDMIXSTRMCTL_FLAG_NONE);
724 if (RT_SUCCESS(rc))
725 rc = rc2;
726 /* Keep going. Flag? */
727 }
728
729 if (enmSinkCmd == AUDMIXSINKCMD_ENABLE)
730 {
731 /* Make sure to clear any other former flags again by assigning AUDMIXSINK_STS_RUNNING directly. */
732 pSink->fStatus = AUDMIXSINK_STS_RUNNING;
733 }
734 else if (enmSinkCmd == AUDMIXSINKCMD_DISABLE)
735 {
736 if (pSink->fStatus & AUDMIXSINK_STS_RUNNING)
737 {
738 /* Set the sink in a pending disable state first.
739 * The final status (disabled) will be set in the sink's iteration. */
740 pSink->fStatus |= AUDMIXSINK_STS_PENDING_DISABLE;
741 }
742 }
743
744#ifdef LOG_ENABLED
745 char *pszStatus = dbgAudioMixerSinkStatusToStr(pSink->fStatus);
746 LogFlowFunc(("[%s] enmCmd=%d, fStatus=%s, rc=%Rrc\n", pSink->pszName, enmSinkCmd, pszStatus, rc));
747 RTStrFree(pszStatus);
748#endif
749
750 int rc2 = RTCritSectLeave(&pSink->CritSect);
751 AssertRC(rc2);
752
753 return rc;
754}
755
756/**
757 * Destroys a mixer sink and removes it from the attached mixer (if any).
758 *
759 * @param pSink Mixer sink to destroy.
760 */
761void AudioMixerSinkDestroy(PAUDMIXSINK pSink)
762{
763 if (!pSink)
764 return;
765
766 int rc2 = RTCritSectEnter(&pSink->CritSect);
767 AssertRC(rc2);
768
769 if (pSink->pParent)
770 {
771 /* Save mixer pointer, as after audioMixerRemoveSinkInternal() the
772 * pointer will be gone from the stream. */
773 PAUDIOMIXER pMixer = pSink->pParent;
774 AssertPtr(pMixer);
775
776 audioMixerRemoveSinkInternal(pMixer, pSink);
777
778 Assert(pMixer->cSinks);
779 pMixer->cSinks--;
780 }
781
782 rc2 = RTCritSectLeave(&pSink->CritSect);
783 AssertRC(rc2);
784
785 audioMixerSinkDestroyInternal(pSink);
786}
787
788/**
789 * Destroys a mixer sink.
790 *
791 * @param pSink Mixer sink to destroy.
792 */
793static void audioMixerSinkDestroyInternal(PAUDMIXSINK pSink)
794{
795 AssertPtrReturnVoid(pSink);
796
797 LogFunc(("%s\n", pSink->pszName));
798
799 PAUDMIXSTREAM pStream, pStreamNext;
800 RTListForEachSafe(&pSink->lstStreams, pStream, pStreamNext, AUDMIXSTREAM, Node)
801 {
802 /* Save a pointer to the stream to remove, as pStream
803 * will not be valid anymore after calling audioMixerSinkRemoveStreamInternal(). */
804 PAUDMIXSTREAM pStreamToRemove = pStream;
805
806 audioMixerSinkRemoveStreamInternal(pSink, pStreamToRemove);
807 audioMixerStreamDestroyInternal(pStreamToRemove);
808 }
809
810 if (pSink->pszName)
811 {
812 RTStrFree(pSink->pszName);
813 pSink->pszName = NULL;
814 }
815
816 RTCritSectDelete(&pSink->CritSect);
817
818 RTMemFree(pSink);
819 pSink = NULL;
820}
821
822/**
823 * Returns the amount of bytes ready to be read from a sink since the last call
824 * to AudioMixerSinkUpdate().
825 *
826 * @returns Amount of bytes ready to be read from the sink.
827 * @param pSink Sink to return number of available bytes for.
828 */
829uint32_t AudioMixerSinkGetReadable(PAUDMIXSINK pSink)
830{
831 AssertPtrReturn(pSink, 0);
832
833 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_INPUT, ("%s: Can't read from a non-input sink\n", pSink->pszName));
834
835 int rc = RTCritSectEnter(&pSink->CritSect);
836 if (RT_FAILURE(rc))
837 return 0;
838
839 uint32_t cbReadable = 0;
840
841#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
842# error "Implement me!"
843#else
844 /* The hosts sets the pace --
845 * so we try to find the maximum of readable data of all connected streams to this sink. */
846 PAUDMIXSTREAM pMixStream;
847 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
848 {
849 if (!(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream) & PDMAUDIOSTREAMSTS_FLAG_ENABLED))
850 {
851 Log3Func(("[%s] Stream '%s' disabled, skipping ...\n", pSink->pszName, pMixStream->pszName));
852 continue;
853 }
854
855 cbReadable = RT_MAX(cbReadable,
856 pMixStream->pConn->pfnStreamGetReadable(pMixStream->pConn, pMixStream->pStream));
857
858 break; /** @todo For now we only support recording by the first stream added. */
859 }
860#endif
861
862 Log3Func(("[%s] cbReadable=%RU32\n", pSink->pszName, cbReadable));
863
864 int rc2 = RTCritSectLeave(&pSink->CritSect);
865 AssertRC(rc2);
866
867 return cbReadable;
868}
869
870/**
871 * Returns the amount of bytes ready to be written to a sink since the last call
872 * to AudioMixerSinkUpdate().
873 *
874 * @returns Amount of bytes ready to be written to the sink.
875 * @param pSink Sink to return number of available bytes for.
876 */
877uint32_t AudioMixerSinkGetWritable(PAUDMIXSINK pSink)
878{
879 AssertPtrReturn(pSink, 0);
880
881 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_OUTPUT, ("%s: Can't write to a non-output sink\n", pSink->pszName));
882
883 int rc = RTCritSectEnter(&pSink->CritSect);
884 if (RT_FAILURE(rc))
885 return 0;
886
887 uint32_t cbWritable = UINT32_MAX;
888
889 if ( (pSink->fStatus & AUDMIXSINK_STS_RUNNING)
890 && !(pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE))
891 {
892#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
893# error "Implement me!"
894#else
895 /* The hosts sets the pace --
896 * so we try to find the minimum of writable data to all connected streams to this sink. */
897 PAUDMIXSTREAM pMixStream;
898 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
899 {
900 const uint32_t cbWritableStream = pMixStream->pConn->pfnStreamGetWritable(pMixStream->pConn, pMixStream->pStream);
901
902 if (cbWritableStream < cbWritable)
903 cbWritable = cbWritableStream;
904 }
905#endif
906 }
907
908 if (cbWritable == UINT32_MAX)
909 cbWritable = 0;
910
911 Log3Func(("[%s] cbWritable=%RU32\n", pSink->pszName, cbWritable));
912
913 int rc2 = RTCritSectLeave(&pSink->CritSect);
914 AssertRC(rc2);
915
916 return cbWritable;
917}
918
919/**
920 * Returns the sink's mixing direction.
921 *
922 * @returns Mixing direction.
923 * @param pSink Sink to return direction for.
924 */
925AUDMIXSINKDIR AudioMixerSinkGetDir(PAUDMIXSINK pSink)
926{
927 AssertPtrReturn(pSink, AUDMIXSINKDIR_UNKNOWN);
928
929 int rc = RTCritSectEnter(&pSink->CritSect);
930 if (RT_FAILURE(rc))
931 return AUDMIXSINKDIR_UNKNOWN;
932
933 AUDMIXSINKDIR enmDir = pSink->enmDir;
934
935 int rc2 = RTCritSectLeave(&pSink->CritSect);
936 AssertRC(rc2);
937
938 return enmDir;
939}
940
941/**
942 * Returns a specific mixer stream from a sink, based on its index.
943 *
944 * @returns Mixer stream if found, or NULL if not found.
945 * @param pSink Sink to retrieve mixer stream from.
946 * @param uIndex Index of the mixer stream to return.
947 */
948PAUDMIXSTREAM AudioMixerSinkGetStream(PAUDMIXSINK pSink, uint8_t uIndex)
949{
950 AssertPtrReturn(pSink, NULL);
951
952 int rc = RTCritSectEnter(&pSink->CritSect);
953 if (RT_FAILURE(rc))
954 return NULL;
955
956 AssertMsgReturn(uIndex < pSink->cStreams,
957 ("Index %RU8 exceeds stream count (%RU8)", uIndex, pSink->cStreams), NULL);
958
959 /* Slow lookup, d'oh. */
960 PAUDMIXSTREAM pStream = RTListGetFirst(&pSink->lstStreams, AUDMIXSTREAM, Node);
961 while (uIndex)
962 {
963 pStream = RTListGetNext(&pSink->lstStreams, pStream, AUDMIXSTREAM, Node);
964 uIndex--;
965 }
966
967 /** @todo Do we need to raise the stream's reference count here? */
968
969 int rc2 = RTCritSectLeave(&pSink->CritSect);
970 AssertRC(rc2);
971
972 AssertPtr(pStream);
973 return pStream;
974}
975
976/**
977 * Returns the current status of a mixer sink.
978 *
979 * @returns The sink's current status.
980 * @param pSink Mixer sink to return status for.
981 */
982AUDMIXSINKSTS AudioMixerSinkGetStatus(PAUDMIXSINK pSink)
983{
984 if (!pSink)
985 return AUDMIXSINK_STS_NONE;
986
987 int rc2 = RTCritSectEnter(&pSink->CritSect);
988 if (RT_FAILURE(rc2))
989 return AUDMIXSINK_STS_NONE;
990
991 /* If the dirty flag is set, there is unprocessed data in the sink. */
992 AUDMIXSINKSTS stsSink = pSink->fStatus;
993
994 rc2 = RTCritSectLeave(&pSink->CritSect);
995 AssertRC(rc2);
996
997 return stsSink;
998}
999
1000/**
1001 * Returns the number of attached mixer streams to a mixer sink.
1002 *
1003 * @returns The number of attached mixer streams.
1004 * @param pSink Mixer sink to return number for.
1005 */
1006uint8_t AudioMixerSinkGetStreamCount(PAUDMIXSINK pSink)
1007{
1008 if (!pSink)
1009 return 0;
1010
1011 int rc2 = RTCritSectEnter(&pSink->CritSect);
1012 if (RT_FAILURE(rc2))
1013 return 0;
1014
1015 uint8_t cStreams = pSink->cStreams;
1016
1017 rc2 = RTCritSectLeave(&pSink->CritSect);
1018 AssertRC(rc2);
1019
1020 return cStreams;
1021}
1022
1023/**
1024 * Returns whether the sink is in an active state or not.
1025 * Note: The pending disable state also counts as active.
1026 *
1027 * @returns True if active, false if not.
1028 * @param pSink Sink to return active state for.
1029 */
1030bool AudioMixerSinkIsActive(PAUDMIXSINK pSink)
1031{
1032 if (!pSink)
1033 return false;
1034
1035 int rc2 = RTCritSectEnter(&pSink->CritSect);
1036 if (RT_FAILURE(rc2))
1037 return false;
1038
1039 bool fIsActive = pSink->fStatus & AUDMIXSINK_STS_RUNNING;
1040 /* Note: AUDMIXSINK_STS_PENDING_DISABLE implies AUDMIXSINK_STS_RUNNING. */
1041
1042 Log3Func(("[%s] fActive=%RTbool\n", pSink->pszName, fIsActive));
1043
1044 rc2 = RTCritSectLeave(&pSink->CritSect);
1045 AssertRC(rc2);
1046
1047 return fIsActive;
1048}
1049
1050/**
1051 * Reads audio data from a mixer sink.
1052 *
1053 * @returns IPRT status code.
1054 * @param pSink Mixer sink to read data from.
1055 * @param enmOp Mixer operation to use for reading the data.
1056 * @param pvBuf Buffer where to store the read data.
1057 * @param cbBuf Buffer size (in bytes) where to store the data.
1058 * @param pcbRead Number of bytes read. Optional.
1059 */
1060int AudioMixerSinkRead(PAUDMIXSINK pSink, AUDMIXOP enmOp, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
1061{
1062 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1063 RT_NOREF(enmOp);
1064 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1065 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1066 /* pcbRead is optional. */
1067
1068 /** @todo Handle mixing operation enmOp! */
1069
1070 int rc = RTCritSectEnter(&pSink->CritSect);
1071 if (RT_FAILURE(rc))
1072 return rc;
1073
1074 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_INPUT,
1075 ("Can't read from a sink which is not an input sink\n"));
1076
1077#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
1078# error "Implement me!"
1079#else
1080 uint8_t *pvMixBuf = (uint8_t *)RTMemAlloc(cbBuf);
1081 if (!pvMixBuf)
1082 {
1083 int rc2 = RTCritSectLeave(&pSink->CritSect);
1084 AssertRC(rc2);
1085
1086 return VERR_NO_MEMORY;
1087 }
1088#endif
1089
1090 uint32_t cbRead = 0;
1091
1092 /* Flag indicating whether this sink is in a 'clean' state,
1093 * e.g. there is no more data to read from. */
1094 bool fClean = true;
1095
1096 PAUDMIXSTREAM pMixStream;
1097 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1098 {
1099 if (!(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream) & PDMAUDIOSTREAMSTS_FLAG_ENABLED))
1100 {
1101 Log3Func(("[%s] Stream '%s' disabled, skipping ...\n", pSink->pszName, pMixStream->pszName));
1102 continue;
1103 }
1104
1105 uint32_t cbTotalRead = 0;
1106 uint32_t cbToRead = cbBuf;
1107
1108 int rc2 = VINF_SUCCESS;
1109
1110 while (cbToRead)
1111 {
1112 uint32_t cbReadStrm;
1113 AssertPtr(pMixStream->pConn);
1114#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
1115# error "Implement me!"
1116#else
1117 rc2 = pMixStream->pConn->pfnStreamRead(pMixStream->pConn, pMixStream->pStream,
1118 (uint8_t *)pvMixBuf + cbTotalRead, cbToRead, &cbReadStrm);
1119#endif
1120 if (RT_FAILURE(rc2))
1121 LogFunc(("[%s] Failed reading from stream '%s': %Rrc\n", pSink->pszName, pMixStream->pszName, rc2));
1122
1123 Log3Func(("[%s] Stream '%s': Read %RU32 bytes\n", pSink->pszName, pMixStream->pszName, cbReadStrm));
1124
1125 if ( RT_FAILURE(rc2)
1126 || !cbReadStrm)
1127 break;
1128
1129 /** @todo Right now we only handle one stream (the last one added in fact). */
1130
1131 AssertBreakStmt(cbReadStrm <= cbToRead, rc = VERR_BUFFER_OVERFLOW);
1132 cbToRead -= cbReadStrm;
1133 cbTotalRead += cbReadStrm;
1134 }
1135
1136 if (RT_FAILURE(rc2))
1137 continue;
1138
1139 cbRead = RT_MAX(cbRead, cbTotalRead);
1140
1141 uint32_t cbReadable = pMixStream->pConn->pfnStreamGetReadable(pMixStream->pConn, pMixStream->pStream);
1142
1143 /* Still some data available? Then sink is not clean (yet). */
1144 if (cbReadable)
1145 fClean = false;
1146 }
1147
1148 if (RT_SUCCESS(rc))
1149 {
1150 if (fClean)
1151 pSink->fStatus &= ~AUDMIXSINK_STS_DIRTY;
1152
1153#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
1154# error "Implement me!"
1155#else
1156 if (cbRead)
1157 memcpy(pvBuf, pvMixBuf, cbRead);
1158#endif
1159 if (pcbRead)
1160 *pcbRead = cbRead;
1161 }
1162
1163#ifndef VBOX_AUDIO_MIXER_WITH_MIXBUF
1164 RTMemFree(pvMixBuf);
1165#endif
1166
1167
1168#ifdef LOG_ENABLED
1169 char *pszStatus = dbgAudioMixerSinkStatusToStr(pSink->fStatus);
1170 Log2Func(("[%s] cbRead=%RU32, fClean=%RTbool, fStatus=%s, rc=%Rrc\n", pSink->pszName, cbRead, fClean, pszStatus, rc));
1171 RTStrFree(pszStatus);
1172#endif
1173
1174 int rc2 = RTCritSectLeave(&pSink->CritSect);
1175 AssertRC(rc2);
1176
1177 return rc;
1178}
1179
1180/**
1181 * Removes a mixer stream from a mixer sink, internal version.
1182 *
1183 * @returns IPRT status code.
1184 * @param pSink Sink to remove mixer stream from.
1185 * @param pStream Stream to remove.
1186 */
1187static int audioMixerSinkRemoveStreamInternal(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream)
1188{
1189 AssertPtrReturn(pSink, VERR_INVALID_PARAMETER);
1190 if ( !pStream
1191 || !pStream->pSink) /* Not part of a sink anymore? */
1192 {
1193 return VERR_NOT_FOUND;
1194 }
1195
1196 AssertMsgReturn(pStream->pSink == pSink, ("Stream '%s' is not part of sink '%s'\n",
1197 pStream->pszName, pSink->pszName), VERR_NOT_FOUND);
1198
1199 LogFlowFunc(("[%s] (Stream = %s), cStreams=%RU8\n",
1200 pSink->pszName, pStream->pStream->szName, pSink->cStreams));
1201
1202#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
1203 /* Unlink mixing buffer. */
1204 AudioMixBufUnlink(&pStream->pStream->MixBuf);
1205#endif
1206
1207 /* Remove stream from sink. */
1208 RTListNodeRemove(&pStream->Node);
1209
1210 /* Set sink to NULL so that we know we're not part of any sink anymore. */
1211 pStream->pSink = NULL;
1212
1213 return VINF_SUCCESS;
1214}
1215
1216/**
1217 * Removes a mixer stream from a mixer sink.
1218 *
1219 * @param pSink Sink to remove mixer stream from.
1220 * @param pStream Stream to remove.
1221 */
1222void AudioMixerSinkRemoveStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream)
1223{
1224 int rc2 = RTCritSectEnter(&pSink->CritSect);
1225 AssertRC(rc2);
1226
1227 rc2 = audioMixerSinkRemoveStreamInternal(pSink, pStream);
1228 if (RT_SUCCESS(rc2))
1229 {
1230 Assert(pSink->cStreams);
1231 pSink->cStreams--;
1232 }
1233
1234 rc2 = RTCritSectLeave(&pSink->CritSect);
1235 AssertRC(rc2);
1236}
1237
1238/**
1239 * Removes all attached streams from a given sink.
1240 *
1241 * @param pSink Sink to remove attached streams from.
1242 */
1243static void audioMixerSinkRemoveAllStreamsInternal(PAUDMIXSINK pSink)
1244{
1245 if (!pSink)
1246 return;
1247
1248 LogFunc(("%s\n", pSink->pszName));
1249
1250 PAUDMIXSTREAM pStream, pStreamNext;
1251 RTListForEachSafe(&pSink->lstStreams, pStream, pStreamNext, AUDMIXSTREAM, Node)
1252 audioMixerSinkRemoveStreamInternal(pSink, pStream);
1253}
1254
1255/**
1256 * Resets the sink's state.
1257 *
1258 * @param pSink Sink to reset.
1259 */
1260static void audioMixerSinkReset(PAUDMIXSINK pSink)
1261{
1262 if (!pSink)
1263 return;
1264
1265 LogFunc(("[%s]\n", pSink->pszName));
1266
1267 if (pSink->enmDir == AUDMIXSINKDIR_INPUT)
1268 {
1269#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
1270 AudioMixBufReset(&pSink->MixBuf);
1271#else
1272 pSink->In.cbReadable = 0;
1273#endif
1274 }
1275 else if (pSink->enmDir == AUDMIXSINKDIR_OUTPUT)
1276 {
1277#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
1278 AudioMixBufReset(&pSink->MixBuf);
1279#else
1280 pSink->Out.cbWritable = 0;
1281#endif
1282 }
1283
1284 /* Update last updated timestamp. */
1285 pSink->tsLastUpdatedMS = RTTimeMilliTS();
1286
1287 /* Reset status. */
1288 pSink->fStatus = AUDMIXSINK_STS_NONE;
1289}
1290
1291/**
1292 * Removes all attached streams from a given sink.
1293 *
1294 * @param pSink Sink to remove attached streams from.
1295 */
1296void AudioMixerSinkRemoveAllStreams(PAUDMIXSINK pSink)
1297{
1298 if (!pSink)
1299 return;
1300
1301 int rc2 = RTCritSectEnter(&pSink->CritSect);
1302 AssertRC(rc2);
1303
1304 audioMixerSinkRemoveAllStreamsInternal(pSink);
1305
1306 pSink->cStreams = 0;
1307
1308 rc2 = RTCritSectLeave(&pSink->CritSect);
1309 AssertRC(rc2);
1310}
1311
1312/**
1313 * Resets a sink. This will immediately stop all processing.
1314 *
1315 * @param pSink Sink to reset.
1316 */
1317void AudioMixerSinkReset(PAUDMIXSINK pSink)
1318{
1319 if (!pSink)
1320 return;
1321
1322 int rc2 = RTCritSectEnter(&pSink->CritSect);
1323 AssertRC(rc2);
1324
1325 LogFlowFunc(("[%s]\n", pSink->pszName));
1326
1327 audioMixerSinkReset(pSink);
1328
1329 rc2 = RTCritSectLeave(&pSink->CritSect);
1330 AssertRC(rc2);
1331}
1332
1333/**
1334 * Returns the audio format of a mixer sink.
1335 *
1336 * @param pSink Sink to retrieve audio format for.
1337 * @param pPCMProps Where to the returned audio format.
1338 */
1339void AudioMixerSinkGetFormat(PAUDMIXSINK pSink, PPDMAUDIOPCMPROPS pPCMProps)
1340{
1341 AssertPtrReturnVoid(pSink);
1342 AssertPtrReturnVoid(pPCMProps);
1343
1344 int rc2 = RTCritSectEnter(&pSink->CritSect);
1345 if (RT_FAILURE(rc2))
1346 return;
1347
1348 memcpy(pPCMProps, &pSink->PCMProps, sizeof(PDMAUDIOPCMPROPS));
1349
1350 rc2 = RTCritSectLeave(&pSink->CritSect);
1351 AssertRC(rc2);
1352}
1353
1354/**
1355 * Sets the audio format of a mixer sink.
1356 *
1357 * @returns IPRT status code.
1358 * @param pSink Sink to set audio format for.
1359 * @param pPCMProps Audio format (PCM properties) to set.
1360 */
1361int AudioMixerSinkSetFormat(PAUDMIXSINK pSink, PPDMAUDIOPCMPROPS pPCMProps)
1362{
1363 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1364 AssertPtrReturn(pPCMProps, VERR_INVALID_POINTER);
1365
1366 int rc = RTCritSectEnter(&pSink->CritSect);
1367 if (RT_FAILURE(rc))
1368 return rc;
1369
1370 if (DrvAudioHlpPCMPropsAreEqual(&pSink->PCMProps, pPCMProps)) /* Bail out early if PCM properties are equal. */
1371 {
1372 rc = RTCritSectLeave(&pSink->CritSect);
1373 AssertRC(rc);
1374
1375 return rc;
1376 }
1377
1378 if (pSink->PCMProps.uHz)
1379 LogFlowFunc(("[%s] Old format: %RU8 bit, %RU8 channels, %RU32Hz\n",
1380 pSink->pszName, pSink->PCMProps.cBits, pSink->PCMProps.cChannels, pSink->PCMProps.uHz));
1381
1382 memcpy(&pSink->PCMProps, pPCMProps, sizeof(PDMAUDIOPCMPROPS));
1383
1384 LogFlowFunc(("[%s] New format %RU8 bit, %RU8 channels, %RU32Hz\n",
1385 pSink->pszName, pSink->PCMProps.cBits, pSink->PCMProps.cChannels, pSink->PCMProps.uHz));
1386
1387#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF
1388 /* Also update the sink's mixing buffer format. */
1389 AudioMixBufDestroy(&pSink->MixBuf);
1390 rc = AudioMixBufInit(&pSink->MixBuf, pSink->pszName, pPCMProps, _4K /** @todo Make configurable? */);
1391 if (RT_SUCCESS(rc))
1392 {
1393 PAUDMIXSTREAM pStream;
1394 RTListForEach(&pSink->lstStreams, pStream, AUDMIXSTREAM, Node)
1395 {
1396 /** @todo Invalidate mix buffers! */
1397 }
1398 }
1399#endif /* VBOX_AUDIO_MIXER_WITH_MIXBUF */
1400
1401 int rc2 = RTCritSectLeave(&pSink->CritSect);
1402 AssertRC(rc2);
1403
1404 LogFlowFuncLeaveRC(rc);
1405 return rc;
1406}
1407
1408/**
1409 * Set the volume of an individual sink.
1410 *
1411 * @returns IPRT status code.
1412 * @param pSink Sink to set volume for.
1413 * @param pVol Volume to set.
1414 */
1415int AudioMixerSinkSetVolume(PAUDMIXSINK pSink, PPDMAUDIOVOLUME pVol)
1416{
1417 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1418 AssertPtrReturn(pVol, VERR_INVALID_POINTER);
1419
1420 int rc = RTCritSectEnter(&pSink->CritSect);
1421 if (RT_FAILURE(rc))
1422 return rc;
1423
1424 memcpy(&pSink->Volume, pVol, sizeof(PDMAUDIOVOLUME));
1425
1426 LogFlowFunc(("[%s] fMuted=%RTbool, lVol=%RU32, rVol=%RU32\n",
1427 pSink->pszName, pSink->Volume.fMuted, pSink->Volume.uLeft, pSink->Volume.uRight));
1428
1429 AssertPtr(pSink->pParent);
1430 rc = audioMixerSinkUpdateVolume(pSink, &pSink->pParent->VolMaster);
1431
1432 int rc2 = RTCritSectLeave(&pSink->CritSect);
1433 AssertRC(rc2);
1434
1435 return rc;
1436}
1437
1438/**
1439 * Updates a mixer sink, internal version.
1440 *
1441 * @returns IPRT status code.
1442 * @param pSink Mixer sink to update.
1443 */
1444static int audioMixerSinkUpdateInternal(PAUDMIXSINK pSink)
1445{
1446 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1447
1448 int rc = VINF_SUCCESS;
1449
1450#ifdef LOG_ENABLED
1451 char *pszStatus = dbgAudioMixerSinkStatusToStr(pSink->fStatus);
1452 Log3Func(("[%s] fStatus=%s\n", pSink->pszName, pszStatus));
1453 RTStrFree(pszStatus);
1454#endif
1455
1456 /* Sink disabled? Take a shortcut. */
1457 if (!(pSink->fStatus & AUDMIXSINK_STS_RUNNING))
1458 return rc;
1459
1460 /* Number of detected disabled streams of this sink. */
1461 uint8_t cStreamsDisabled = 0;
1462
1463 PAUDMIXSTREAM pMixStream, pMixStreamNext;
1464 RTListForEachSafe(&pSink->lstStreams, pMixStream, pMixStreamNext, AUDMIXSTREAM, Node)
1465 {
1466 PPDMAUDIOSTREAM pStream = pMixStream->pStream;
1467 AssertPtr(pStream);
1468
1469 PPDMIAUDIOCONNECTOR pConn = pMixStream->pConn;
1470 AssertPtr(pConn);
1471
1472 uint32_t cfProc = 0;
1473
1474 int rc2 = pConn->pfnStreamIterate(pConn, pStream);
1475 if (RT_SUCCESS(rc2))
1476 {
1477 if (pSink->enmDir == AUDMIXSINKDIR_INPUT)
1478 {
1479 rc = pConn->pfnStreamCapture(pConn, pStream, &cfProc);
1480 if (RT_FAILURE(rc2))
1481 {
1482 LogFunc(("%s: Failed capturing stream '%s', rc=%Rrc\n", pSink->pszName, pStream->szName, rc2));
1483 if (RT_SUCCESS(rc))
1484 rc = rc2;
1485 continue;
1486 }
1487
1488 if (cfProc)
1489 pSink->fStatus |= AUDMIXSINK_STS_DIRTY;
1490 }
1491 else if (pSink->enmDir == AUDMIXSINKDIR_OUTPUT)
1492 {
1493 rc2 = pConn->pfnStreamPlay(pConn, pStream, &cfProc);
1494 if (RT_FAILURE(rc2))
1495 {
1496 LogFunc(("%s: Failed playing stream '%s', rc=%Rrc\n", pSink->pszName, pStream->szName, rc2));
1497 if (RT_SUCCESS(rc))
1498 rc = rc2;
1499 continue;
1500 }
1501 }
1502 else
1503 {
1504 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
1505 continue;
1506 }
1507
1508 rc2 = pConn->pfnStreamIterate(pConn, pStream);
1509 if (RT_FAILURE(rc2))
1510 {
1511 LogFunc(("%s: Failed re-iterating stream '%s', rc=%Rrc\n", pSink->pszName, pStream->szName, rc2));
1512 if (RT_SUCCESS(rc))
1513 rc = rc2;
1514 continue;
1515 }
1516
1517 PDMAUDIOSTREAMSTS strmSts = pConn->pfnStreamGetStatus(pConn, pStream);
1518
1519 /* Is the stream not enabled and also is not in a pending disable state anymore? */
1520 if ( !(strmSts & PDMAUDIOSTREAMSTS_FLAG_ENABLED)
1521 && !(strmSts & PDMAUDIOSTREAMSTS_FLAG_PENDING_DISABLE))
1522 {
1523 cStreamsDisabled++;
1524 }
1525 }
1526
1527 Log3Func(("\t%s: cPlayed/cCaptured=%RU32, rc2=%Rrc\n", pStream->szName, cfProc, rc2));
1528 }
1529
1530 Log3Func(("[%s] fPendingDisable=%RTbool, %RU8/%RU8 streams disabled\n",
1531 pSink->pszName, RT_BOOL(pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE), cStreamsDisabled, pSink->cStreams));
1532
1533 /* Update last updated timestamp. */
1534 pSink->tsLastUpdatedMS = RTTimeMilliTS();
1535
1536 /* All streams disabled and the sink is in pending disable mode? */
1537 if ( cStreamsDisabled == pSink->cStreams
1538 && (pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE))
1539 {
1540 audioMixerSinkReset(pSink);
1541 }
1542
1543 Log3Func(("[%s] cbReadable=%RU32, cbWritable=%RU32, rc=%Rrc\n",
1544 pSink->pszName, pSink->In.cbReadable, pSink->Out.cbWritable, rc));
1545
1546 return rc;
1547}
1548
1549/**
1550 * Updates (invalidates) a mixer sink.
1551 *
1552 * @returns IPRT status code.
1553 * @param pSink Mixer sink to update.
1554 */
1555int AudioMixerSinkUpdate(PAUDMIXSINK pSink)
1556{
1557 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1558
1559 int rc = RTCritSectEnter(&pSink->CritSect);
1560 if (RT_FAILURE(rc))
1561 return rc;
1562
1563 rc = audioMixerSinkUpdateInternal(pSink);
1564
1565 int rc2 = RTCritSectLeave(&pSink->CritSect);
1566 AssertRC(rc2);
1567
1568 return rc;
1569}
1570
1571/**
1572 * Updates the (master) volume of a mixer sink.
1573 *
1574 * @returns IPRT status code.
1575 * @param pSink Mixer sink to update volume for.
1576 * @param pVolMaster Master volume to set.
1577 */
1578static int audioMixerSinkUpdateVolume(PAUDMIXSINK pSink, const PPDMAUDIOVOLUME pVolMaster)
1579{
1580 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1581 AssertPtrReturn(pVolMaster, VERR_INVALID_POINTER);
1582
1583 LogFlowFunc(("[%s] Master fMuted=%RTbool, lVol=%RU32, rVol=%RU32\n",
1584 pSink->pszName, pVolMaster->fMuted, pVolMaster->uLeft, pVolMaster->uRight));
1585 LogFlowFunc(("[%s] fMuted=%RTbool, lVol=%RU32, rVol=%RU32 ",
1586 pSink->pszName, pSink->Volume.fMuted, pSink->Volume.uLeft, pSink->Volume.uRight));
1587
1588 /** @todo Very crude implementation for now -- needs more work! */
1589
1590 pSink->VolumeCombined.fMuted = pVolMaster->fMuted || pSink->Volume.fMuted;
1591
1592 pSink->VolumeCombined.uLeft = ( (pSink->Volume.uLeft ? pSink->Volume.uLeft : 1)
1593 * (pVolMaster->uLeft ? pVolMaster->uLeft : 1)) / PDMAUDIO_VOLUME_MAX;
1594
1595 pSink->VolumeCombined.uRight = ( (pSink->Volume.uRight ? pSink->Volume.uRight : 1)
1596 * (pVolMaster->uRight ? pVolMaster->uRight : 1)) / PDMAUDIO_VOLUME_MAX;
1597
1598 LogFlow(("-> fMuted=%RTbool, lVol=%RU32, rVol=%RU32\n",
1599 pSink->VolumeCombined.fMuted, pSink->VolumeCombined.uLeft, pSink->VolumeCombined.uRight));
1600
1601 /* Propagate new sink volume to all streams in the sink. */
1602 PAUDMIXSTREAM pMixStream;
1603 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1604 {
1605 int rc2 = pMixStream->pConn->pfnStreamSetVolume(pMixStream->pConn, pMixStream->pStream, &pSink->VolumeCombined);
1606 AssertRC(rc2);
1607 }
1608
1609 return VINF_SUCCESS;
1610}
1611
1612/**
1613 * Writes data to a mixer sink.
1614 *
1615 * @returns IPRT status code.
1616 * @param pSink Sink to write data to.
1617 * @param enmOp Mixer operation to use when writing data to the sink.
1618 * @param pvBuf Buffer containing the audio data to write.
1619 * @param cbBuf Size (in bytes) of the buffer containing the audio data.
1620 * @param pcbWritten Number of bytes written. Optional.
1621 */
1622int AudioMixerSinkWrite(PAUDMIXSINK pSink, AUDMIXOP enmOp, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
1623{
1624 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1625 RT_NOREF(enmOp);
1626 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1627 AssertReturn (cbBuf, VERR_INVALID_PARAMETER);
1628 /* pcbWritten is optional. */
1629
1630 int rc = RTCritSectEnter(&pSink->CritSect);
1631 if (RT_FAILURE(rc))
1632 return rc;
1633
1634 AssertMsg(pSink->fStatus & AUDMIXSINK_STS_RUNNING,
1635 ("%s: Can't write to a sink which is not running (anymore) (status 0x%x)\n", pSink->pszName, pSink->fStatus));
1636 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_OUTPUT,
1637 ("%s: Can't write to a sink which is not an output sink\n", pSink->pszName));
1638
1639 Log3Func(("[%s] enmOp=%d, cbBuf=%RU32\n", pSink->pszName, enmOp, cbBuf));
1640
1641 uint32_t cbWritten = UINT32_MAX;
1642
1643 PAUDMIXSTREAM pMixStream;
1644 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1645 {
1646 uint32_t cbProcessed = 0;
1647 int rc2 = pMixStream->pConn->pfnStreamWrite(pMixStream->pConn, pMixStream->pStream, pvBuf, cbBuf, &cbProcessed);
1648 if (RT_FAILURE(rc2))
1649 LogFunc(("[%s] Failed writing to stream '%s': %Rrc\n", pSink->pszName, pMixStream->pszName, rc2));
1650
1651 Log3Func(("[%s] Written %RU32 to '%s'\n", pSink->pszName, cbProcessed, pMixStream->pszName));
1652
1653 if (cbProcessed)
1654 {
1655 /* Set dirty bit. */
1656 pSink->fStatus |= AUDMIXSINK_STS_DIRTY;
1657
1658 /*
1659 * Return the minimum bytes processed by all connected streams.
1660 * The host sets the pace, so all backends have to behave accordingly.
1661 */
1662 if (cbWritten > cbProcessed)
1663 cbWritten = cbProcessed;
1664 }
1665 }
1666
1667 if (cbWritten == UINT32_MAX)
1668 cbWritten = 0;
1669
1670 Log3Func(("[%s] cbWritten=%RU32\n", pSink->pszName, cbWritten));
1671
1672 if (pcbWritten)
1673 *pcbWritten = cbWritten;
1674
1675 int rc2 = RTCritSectLeave(&pSink->CritSect);
1676 AssertRC(rc2);
1677
1678 return rc;
1679}
1680
1681/*********************************************************************************************************************************
1682 * Mixer Stream implementation.
1683 ********************************************************************************************************************************/
1684
1685/**
1686 * Controls a mixer stream, internal version.
1687 *
1688 * @returns IPRT status code.
1689 * @param pMixStream Mixer stream to control.
1690 * @param enmCmd Mixer stream command to use.
1691 * @param fCtl Additional control flags. Pass 0.
1692 */
1693int audioMixerStreamCtlInternal(PAUDMIXSTREAM pMixStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl)
1694{
1695 AssertPtr(pMixStream->pConn);
1696 AssertPtr(pMixStream->pStream);
1697
1698 RT_NOREF(fCtl);
1699
1700 int rc = pMixStream->pConn->pfnStreamControl(pMixStream->pConn, pMixStream->pStream, enmCmd);
1701
1702 LogFlowFunc(("[%s] enmCmd=%ld, rc=%Rrc\n", pMixStream->pszName, enmCmd, rc));
1703
1704 return rc;
1705}
1706
1707/**
1708 * Controls a mixer stream.
1709 *
1710 * @returns IPRT status code.
1711 * @param pMixStream Mixer stream to control.
1712 * @param enmCmd Mixer stream command to use.
1713 * @param fCtl Additional control flags. Pass 0.
1714 */
1715int AudioMixerStreamCtl(PAUDMIXSTREAM pMixStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl)
1716{
1717 RT_NOREF(fCtl);
1718 AssertPtrReturn(pMixStream, VERR_INVALID_POINTER);
1719 /** @todo Validate fCtl. */
1720
1721 int rc = RTCritSectEnter(&pMixStream->CritSect);
1722 if (RT_FAILURE(rc))
1723 return rc;
1724
1725 rc = audioMixerStreamCtlInternal(pMixStream, enmCmd, fCtl);
1726
1727 int rc2 = RTCritSectLeave(&pMixStream->CritSect);
1728 if (RT_SUCCESS(rc))
1729 rc = rc2;
1730
1731 return rc;
1732}
1733
1734/**
1735 * Destroys a mixer stream, internal version.
1736 *
1737 * @param pMixStream Mixer stream to destroy.
1738 */
1739static void audioMixerStreamDestroyInternal(PAUDMIXSTREAM pMixStream)
1740{
1741 AssertPtrReturnVoid(pMixStream);
1742
1743 LogFunc(("%s\n", pMixStream->pszName));
1744
1745 if (pMixStream->pConn) /* Stream has a connector interface present? */
1746 {
1747 if (pMixStream->pStream)
1748 {
1749 pMixStream->pConn->pfnStreamRelease(pMixStream->pConn, pMixStream->pStream);
1750 pMixStream->pConn->pfnStreamDestroy(pMixStream->pConn, pMixStream->pStream);
1751
1752 pMixStream->pStream = NULL;
1753 }
1754
1755 pMixStream->pConn = NULL;
1756 }
1757
1758 if (pMixStream->pszName)
1759 {
1760 RTStrFree(pMixStream->pszName);
1761 pMixStream->pszName = NULL;
1762 }
1763
1764 int rc2 = RTCritSectDelete(&pMixStream->CritSect);
1765 AssertRC(rc2);
1766
1767 RTMemFree(pMixStream);
1768 pMixStream = NULL;
1769}
1770
1771/**
1772 * Destroys a mixer stream.
1773 *
1774 * @param pMixStream Mixer stream to destroy.
1775 */
1776void AudioMixerStreamDestroy(PAUDMIXSTREAM pMixStream)
1777{
1778 if (!pMixStream)
1779 return;
1780
1781 int rc2 = RTCritSectEnter(&pMixStream->CritSect);
1782 AssertRC(rc2);
1783
1784 LogFunc(("%s\n", pMixStream->pszName));
1785
1786 if (pMixStream->pSink) /* Is the stream part of a sink? */
1787 {
1788 /* Save sink pointer, as after audioMixerSinkRemoveStreamInternal() the
1789 * pointer will be gone from the stream. */
1790 PAUDMIXSINK pSink = pMixStream->pSink;
1791
1792 rc2 = audioMixerSinkRemoveStreamInternal(pSink, pMixStream);
1793 if (RT_SUCCESS(rc2))
1794 {
1795 Assert(pSink->cStreams);
1796 pSink->cStreams--;
1797 }
1798 }
1799 else
1800 rc2 = VINF_SUCCESS;
1801
1802 int rc3 = RTCritSectLeave(&pMixStream->CritSect);
1803 AssertRC(rc3);
1804
1805 if (RT_SUCCESS(rc2))
1806 {
1807 audioMixerStreamDestroyInternal(pMixStream);
1808 pMixStream = NULL;
1809 }
1810
1811 LogFlowFunc(("Returning %Rrc\n", rc2));
1812}
1813
1814/**
1815 * Returns whether a mixer stream currently is active (playing/recording) or not.
1816 *
1817 * @returns @c true if playing/recording, @c false if not.
1818 * @param pMixStream Mixer stream to return status for.
1819 */
1820bool AudioMixerStreamIsActive(PAUDMIXSTREAM pMixStream)
1821{
1822 int rc2 = RTCritSectEnter(&pMixStream->CritSect);
1823 if (RT_FAILURE(rc2))
1824 return false;
1825
1826 AssertPtr(pMixStream->pConn);
1827 AssertPtr(pMixStream->pStream);
1828
1829 bool fIsActive;
1830
1831 if ( pMixStream->pConn
1832 && pMixStream->pStream
1833 && RT_BOOL(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream) & PDMAUDIOSTREAMSTS_FLAG_ENABLED))
1834 {
1835 fIsActive = true;
1836 }
1837 else
1838 fIsActive = false;
1839
1840 rc2 = RTCritSectLeave(&pMixStream->CritSect);
1841 AssertRC(rc2);
1842
1843 return fIsActive;
1844}
1845
1846/**
1847 * Returns whether a mixer stream is valid (e.g. initialized and in a working state) or not.
1848 *
1849 * @returns @c true if valid, @c false if not.
1850 * @param pMixStream Mixer stream to return status for.
1851 */
1852bool AudioMixerStreamIsValid(PAUDMIXSTREAM pMixStream)
1853{
1854 if (!pMixStream)
1855 return false;
1856
1857 int rc2 = RTCritSectEnter(&pMixStream->CritSect);
1858 if (RT_FAILURE(rc2))
1859 return false;
1860
1861 bool fIsValid;
1862
1863 if ( pMixStream->pConn
1864 && pMixStream->pStream
1865 && RT_BOOL(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream) & PDMAUDIOSTREAMSTS_FLAG_INITIALIZED))
1866 {
1867 fIsValid = true;
1868 }
1869 else
1870 fIsValid = false;
1871
1872 rc2 = RTCritSectLeave(&pMixStream->CritSect);
1873 AssertRC(rc2);
1874
1875 return fIsValid;
1876}
1877
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