VirtualBox

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

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

Build fix.

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