VirtualBox

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

Last change on this file since 76815 was 76777, checked in by vboxsync, 6 years ago

Audio/Mixer: Removed VBOX_AUDIO_MIXER_WITH_MIXBUF define, as this is the default anyway since quite a while.

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