VirtualBox

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

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

Audio: Try to fix a hang w/ VRDE audio driver enabled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 70.6 KB
Line 
1/* $Id: AudioMixer.cpp 78506 2019-05-14 14:28:16Z 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
105static int audioMixerStreamCtlInternal(PAUDMIXSTREAM pMixStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl);
106static void audioMixerStreamDestroyInternal(PAUDMIXSTREAM pStream);
107static int audioMixerStreamUpdateStatus(PAUDMIXSTREAM pMixStream);
108
109
110#ifdef LOG_ENABLED
111/**
112 * Converts a mixer sink status to a string.
113 *
114 * @returns Stringified mixer sink flags. Must be free'd with RTStrFree().
115 * "NONE" if no flags set.
116 * @param fFlags Mixer sink flags to convert.
117 */
118static char *dbgAudioMixerSinkStatusToStr(AUDMIXSINKSTS fStatus)
119{
120#define APPEND_FLAG_TO_STR(_aFlag) \
121 if (fStatus & AUDMIXSINK_STS_##_aFlag) \
122 { \
123 if (pszFlags) \
124 { \
125 rc2 = RTStrAAppend(&pszFlags, " "); \
126 if (RT_FAILURE(rc2)) \
127 break; \
128 } \
129 \
130 rc2 = RTStrAAppend(&pszFlags, #_aFlag); \
131 if (RT_FAILURE(rc2)) \
132 break; \
133 } \
134
135 char *pszFlags = NULL;
136 int rc2 = VINF_SUCCESS;
137
138 do
139 {
140 APPEND_FLAG_TO_STR(NONE);
141 APPEND_FLAG_TO_STR(RUNNING);
142 APPEND_FLAG_TO_STR(PENDING_DISABLE);
143 APPEND_FLAG_TO_STR(DIRTY);
144
145 } while (0);
146
147 if ( RT_FAILURE(rc2)
148 && pszFlags)
149 {
150 RTStrFree(pszFlags);
151 pszFlags = NULL;
152 }
153
154#undef APPEND_FLAG_TO_STR
155
156 return pszFlags;
157}
158#endif /* DEBUG */
159
160/**
161 * Creates an audio sink and attaches it to the given mixer.
162 *
163 * @returns IPRT status code.
164 * @param pMixer Mixer to attach created sink to.
165 * @param pszName Name of the sink to create.
166 * @param enmDir Direction of the sink to create.
167 * @param ppSink Pointer which returns the created sink on success.
168 */
169int AudioMixerCreateSink(PAUDIOMIXER pMixer, const char *pszName, AUDMIXSINKDIR enmDir, PAUDMIXSINK *ppSink)
170{
171 AssertPtrReturn(pMixer, VERR_INVALID_POINTER);
172 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
173 /* ppSink is optional. */
174
175 int rc = RTCritSectEnter(&pMixer->CritSect);
176 if (RT_FAILURE(rc))
177 return rc;
178
179 PAUDMIXSINK pSink = (PAUDMIXSINK)RTMemAllocZ(sizeof(AUDMIXSINK));
180 if (pSink)
181 {
182 pSink->pszName = RTStrDup(pszName);
183 if (!pSink->pszName)
184 rc = VERR_NO_MEMORY;
185
186 if (RT_SUCCESS(rc))
187 rc = RTCritSectInit(&pSink->CritSect);
188
189 if (RT_SUCCESS(rc))
190 {
191 pSink->pParent = pMixer;
192 pSink->enmDir = enmDir;
193 RTListInit(&pSink->lstStreams);
194
195 /* Set initial volume to max. */
196 pSink->Volume.fMuted = false;
197 pSink->Volume.uLeft = PDMAUDIO_VOLUME_MAX;
198 pSink->Volume.uRight = PDMAUDIO_VOLUME_MAX;
199
200 /* Ditto for the combined volume. */
201 pSink->VolumeCombined.fMuted = false;
202 pSink->VolumeCombined.uLeft = PDMAUDIO_VOLUME_MAX;
203 pSink->VolumeCombined.uRight = PDMAUDIO_VOLUME_MAX;
204
205 RTListAppend(&pMixer->lstSinks, &pSink->Node);
206 pMixer->cSinks++;
207
208 LogFlowFunc(("pMixer=%p, pSink=%p, cSinks=%RU8\n",
209 pMixer, pSink, pMixer->cSinks));
210
211 if (ppSink)
212 *ppSink = pSink;
213 }
214
215 if (RT_FAILURE(rc))
216 {
217 RTCritSectDelete(&pSink->CritSect);
218
219 if (pSink)
220 {
221 RTMemFree(pSink);
222 pSink = NULL;
223 }
224 }
225 }
226 else
227 rc = VERR_NO_MEMORY;
228
229 int rc2 = RTCritSectLeave(&pMixer->CritSect);
230 AssertRC(rc2);
231
232 return rc;
233}
234
235/**
236 * Creates an audio mixer.
237 *
238 * @returns IPRT status code.
239 * @param pszName Name of the audio mixer.
240 * @param fFlags Creation flags. Not used at the moment and must be 0.
241 * @param ppMixer Pointer which returns the created mixer object.
242 */
243int AudioMixerCreate(const char *pszName, uint32_t fFlags, PAUDIOMIXER *ppMixer)
244{
245 RT_NOREF(fFlags);
246 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
247 /** @todo Add fFlags validation. */
248 AssertPtrReturn(ppMixer, VERR_INVALID_POINTER);
249
250 int rc = VINF_SUCCESS;
251
252 PAUDIOMIXER pMixer = (PAUDIOMIXER)RTMemAllocZ(sizeof(AUDIOMIXER));
253 if (pMixer)
254 {
255 pMixer->pszName = RTStrDup(pszName);
256 if (!pMixer->pszName)
257 rc = VERR_NO_MEMORY;
258
259 if (RT_SUCCESS(rc))
260 rc = RTCritSectInit(&pMixer->CritSect);
261
262 if (RT_SUCCESS(rc))
263 {
264 pMixer->cSinks = 0;
265 RTListInit(&pMixer->lstSinks);
266
267 /* Set master volume to the max. */
268 pMixer->VolMaster.fMuted = false;
269 pMixer->VolMaster.uLeft = PDMAUDIO_VOLUME_MAX;
270 pMixer->VolMaster.uRight = PDMAUDIO_VOLUME_MAX;
271
272 LogFlowFunc(("Created mixer '%s'\n", pMixer->pszName));
273
274 *ppMixer = pMixer;
275 }
276 else
277 RTMemFree(pMixer);
278 }
279 else
280 rc = VERR_NO_MEMORY;
281
282 LogFlowFuncLeaveRC(rc);
283 return rc;
284}
285
286/**
287 * Helper function for the internal debugger to print the mixer's current
288 * state, along with the attached sinks.
289 *
290 * @param pMixer Mixer to print debug output for.
291 * @param pHlp Debug info helper to use.
292 * @param pszArgs Optional arguments. Not being used at the moment.
293 */
294void AudioMixerDebug(PAUDIOMIXER pMixer, PCDBGFINFOHLP pHlp, const char *pszArgs)
295{
296 RT_NOREF(pszArgs);
297 PAUDMIXSINK pSink;
298 unsigned iSink = 0;
299
300 int rc2 = RTCritSectEnter(&pMixer->CritSect);
301 if (RT_FAILURE(rc2))
302 return;
303
304 pHlp->pfnPrintf(pHlp, "[Master] %s: lVol=%u, rVol=%u, fMuted=%RTbool\n", pMixer->pszName,
305 pMixer->VolMaster.uLeft, pMixer->VolMaster.uRight, pMixer->VolMaster.fMuted);
306
307 RTListForEach(&pMixer->lstSinks, pSink, AUDMIXSINK, Node)
308 {
309 pHlp->pfnPrintf(pHlp, "[Sink %u] %s: lVol=%u, rVol=%u, fMuted=%RTbool\n", iSink, pSink->pszName,
310 pSink->Volume.uLeft, pSink->Volume.uRight, pSink->Volume.fMuted);
311 ++iSink;
312 }
313
314 rc2 = RTCritSectLeave(&pMixer->CritSect);
315 AssertRC(rc2);
316}
317
318/**
319 * Destroys an audio mixer.
320 *
321 * @param pMixer Audio mixer to destroy.
322 */
323void AudioMixerDestroy(PAUDIOMIXER pMixer)
324{
325 if (!pMixer)
326 return;
327
328 int rc2 = RTCritSectEnter(&pMixer->CritSect);
329 AssertRC(rc2);
330
331 LogFlowFunc(("Destroying %s ...\n", pMixer->pszName));
332
333 PAUDMIXSINK pSink, pSinkNext;
334 RTListForEachSafe(&pMixer->lstSinks, pSink, pSinkNext, AUDMIXSINK, Node)
335 {
336 /* Save a pointer to the sink to remove, as pSink
337 * will not be valid anymore after calling audioMixerRemoveSinkInternal(). */
338 PAUDMIXSINK pSinkToRemove = pSink;
339
340 audioMixerRemoveSinkInternal(pMixer, pSinkToRemove);
341 audioMixerSinkDestroyInternal(pSinkToRemove);
342 }
343
344 pMixer->cSinks = 0;
345
346 if (pMixer->pszName)
347 {
348 RTStrFree(pMixer->pszName);
349 pMixer->pszName = NULL;
350 }
351
352 rc2 = RTCritSectLeave(&pMixer->CritSect);
353 AssertRC(rc2);
354
355 RTCritSectDelete(&pMixer->CritSect);
356
357 RTMemFree(pMixer);
358 pMixer = NULL;
359}
360
361/**
362 * Invalidates all internal data, internal version.
363 *
364 * @returns IPRT status code.
365 * @param pMixer Mixer to invalidate data for.
366 */
367int audioMixerInvalidateInternal(PAUDIOMIXER pMixer)
368{
369 AssertPtrReturn(pMixer, VERR_INVALID_POINTER);
370
371 LogFlowFunc(("[%s]\n", pMixer->pszName));
372
373 /* Propagate new master volume to all connected sinks. */
374 PAUDMIXSINK pSink;
375 RTListForEach(&pMixer->lstSinks, pSink, AUDMIXSINK, Node)
376 {
377 int rc2 = audioMixerSinkUpdateVolume(pSink, &pMixer->VolMaster);
378 AssertRC(rc2);
379 }
380
381 return VINF_SUCCESS;
382}
383
384/**
385 * Invalidates all internal data.
386 *
387 * @returns IPRT status code.
388 * @param pMixer Mixer to invalidate data for.
389 */
390void AudioMixerInvalidate(PAUDIOMIXER pMixer)
391{
392 AssertPtrReturnVoid(pMixer);
393
394 int rc2 = RTCritSectEnter(&pMixer->CritSect);
395 AssertRC(rc2);
396
397 LogFlowFunc(("[%s]\n", pMixer->pszName));
398
399 rc2 = audioMixerInvalidateInternal(pMixer);
400 AssertRC(rc2);
401
402 rc2 = RTCritSectLeave(&pMixer->CritSect);
403 AssertRC(rc2);
404}
405
406/**
407 * Removes a formerly attached audio sink for an audio mixer, internal version.
408 *
409 * @returns IPRT status code.
410 * @param pMixer Mixer to remove sink from.
411 * @param pSink Sink to remove.
412 */
413static int audioMixerRemoveSinkInternal(PAUDIOMIXER pMixer, PAUDMIXSINK pSink)
414{
415 AssertPtrReturn(pMixer, VERR_INVALID_POINTER);
416 if (!pSink)
417 return VERR_NOT_FOUND;
418
419 AssertMsgReturn(pSink->pParent == pMixer, ("%s: Is not part of mixer '%s'\n",
420 pSink->pszName, pMixer->pszName), VERR_NOT_FOUND);
421
422 LogFlowFunc(("[%s] pSink=%s, cSinks=%RU8\n",
423 pMixer->pszName, pSink->pszName, pMixer->cSinks));
424
425 /* Remove sink from mixer. */
426 RTListNodeRemove(&pSink->Node);
427 Assert(pMixer->cSinks);
428
429 /* Set mixer to NULL so that we know we're not part of any mixer anymore. */
430 pSink->pParent = NULL;
431
432 return VINF_SUCCESS;
433}
434
435/**
436 * Removes a formerly attached audio sink for an audio mixer.
437 *
438 * @returns IPRT status code.
439 * @param pMixer Mixer to remove sink from.
440 * @param pSink Sink to remove.
441 */
442void AudioMixerRemoveSink(PAUDIOMIXER pMixer, PAUDMIXSINK pSink)
443{
444 int rc2 = RTCritSectEnter(&pMixer->CritSect);
445 AssertRC(rc2);
446
447 audioMixerSinkRemoveAllStreamsInternal(pSink);
448 audioMixerRemoveSinkInternal(pMixer, pSink);
449
450 rc2 = RTCritSectLeave(&pMixer->CritSect);
451}
452
453/**
454 * Sets the mixer's master volume.
455 *
456 * @returns IPRT status code.
457 * @param pMixer Mixer to set master volume for.
458 * @param pVol Volume to set.
459 */
460int AudioMixerSetMasterVolume(PAUDIOMIXER pMixer, PPDMAUDIOVOLUME pVol)
461{
462 AssertPtrReturn(pMixer, VERR_INVALID_POINTER);
463 AssertPtrReturn(pVol, VERR_INVALID_POINTER);
464
465 int rc = RTCritSectEnter(&pMixer->CritSect);
466 if (RT_FAILURE(rc))
467 return rc;
468
469 memcpy(&pMixer->VolMaster, pVol, sizeof(PDMAUDIOVOLUME));
470
471 LogFlowFunc(("[%s] lVol=%RU32, rVol=%RU32 => fMuted=%RTbool, lVol=%RU32, rVol=%RU32\n",
472 pMixer->pszName, pVol->uLeft, pVol->uRight,
473 pMixer->VolMaster.fMuted, pMixer->VolMaster.uLeft, pMixer->VolMaster.uRight));
474
475 rc = audioMixerInvalidateInternal(pMixer);
476
477 int rc2 = RTCritSectLeave(&pMixer->CritSect);
478 AssertRC(rc2);
479
480 return rc;
481}
482
483/*********************************************************************************************************************************
484 * Mixer Sink implementation.
485 ********************************************************************************************************************************/
486
487/**
488 * Adds an audio stream to a specific audio sink.
489 *
490 * @returns IPRT status code.
491 * @param pSink Sink to add audio stream to.
492 * @param pStream Stream to add.
493 */
494int AudioMixerSinkAddStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream)
495{
496 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
497 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
498
499 int rc = RTCritSectEnter(&pSink->CritSect);
500 if (RT_FAILURE(rc))
501 return rc;
502
503 if (pSink->cStreams == UINT8_MAX) /* 255 streams per sink max. */
504 {
505 int rc2 = RTCritSectLeave(&pSink->CritSect);
506 AssertRC(rc2);
507
508 return VERR_NO_MORE_HANDLES;
509 }
510
511 LogFlowFuncEnter();
512
513 /** @todo Check if stream already is assigned to (another) sink. */
514
515 /* If the sink is running and not in pending disable mode,
516 * make sure that the added stream also is enabled. */
517 if ( (pSink->fStatus & AUDMIXSINK_STS_RUNNING)
518 && !(pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE))
519 {
520 rc = audioMixerStreamCtlInternal(pStream, PDMAUDIOSTREAMCMD_ENABLE, AUDMIXSTRMCTL_FLAG_NONE);
521 }
522
523 if (RT_SUCCESS(rc))
524 {
525 /* Apply the sink's combined volume to the stream. */
526 rc = pStream->pConn->pfnStreamSetVolume(pStream->pConn, pStream->pStream, &pSink->VolumeCombined);
527 AssertRC(rc);
528 }
529
530 if (RT_SUCCESS(rc))
531 {
532 /* Save pointer to sink the stream is attached to. */
533 pStream->pSink = pSink;
534
535 /* Append stream to sink's list. */
536 RTListAppend(&pSink->lstStreams, &pStream->Node);
537 pSink->cStreams++;
538 }
539
540 LogFlowFunc(("[%s] cStreams=%RU8, rc=%Rrc\n", pSink->pszName, pSink->cStreams, rc));
541
542 int rc2 = RTCritSectLeave(&pSink->CritSect);
543 AssertRC(rc2);
544
545 return rc;
546}
547
548/**
549 * Creates an audio mixer stream.
550 *
551 * @returns IPRT status code.
552 * @param pSink Sink to use for creating the stream.
553 * @param pConn Audio connector interface to use.
554 * @param pCfg Audio stream configuration to use.
555 * @param fFlags Stream flags. Currently unused, set to 0.
556 * @param ppStream Pointer which receives the newly created audio stream.
557 */
558int AudioMixerSinkCreateStream(PAUDMIXSINK pSink,
559 PPDMIAUDIOCONNECTOR pConn, PPDMAUDIOSTREAMCFG pCfg, AUDMIXSTREAMFLAGS fFlags, PAUDMIXSTREAM *ppStream)
560{
561 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
562 AssertPtrReturn(pConn, VERR_INVALID_POINTER);
563 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
564 /** @todo Validate fFlags. */
565 /* ppStream is optional. */
566
567 if (pConn->pfnGetStatus(pConn, PDMAUDIODIR_ANY) == PDMAUDIOBACKENDSTS_NOT_ATTACHED)
568 return VERR_AUDIO_BACKEND_NOT_ATTACHED;
569
570 PAUDMIXSTREAM pMixStream = (PAUDMIXSTREAM)RTMemAllocZ(sizeof(AUDMIXSTREAM));
571 if (!pMixStream)
572 return VERR_NO_MEMORY;
573
574 pMixStream->pszName = RTStrDup(pCfg->szName);
575 if (!pMixStream->pszName)
576 {
577 RTMemFree(pMixStream);
578 return VERR_NO_MEMORY;
579 }
580
581 int rc = RTCritSectEnter(&pSink->CritSect);
582 if (RT_FAILURE(rc))
583 return rc;
584
585 LogFlowFunc(("[%s] fFlags=0x%x (enmDir=%ld, %RU8 bits, %RU8 channels, %RU32Hz)\n",
586 pSink->pszName, fFlags, pCfg->enmDir, pCfg->Props.cBytes * 8, pCfg->Props.cChannels, pCfg->Props.uHz));
587
588 /*
589 * Initialize the host-side configuration for the stream to be created.
590 * Always use the sink's PCM audio format as the host side when creating a stream for it.
591 */
592 AssertMsg(DrvAudioHlpPCMPropsAreValid(&pSink->PCMProps),
593 ("%s: Does not (yet) have a format set when it must\n", pSink->pszName));
594
595 PDMAUDIOSTREAMCFG CfgHost;
596 rc = DrvAudioHlpPCMPropsToStreamCfg(&pSink->PCMProps, &CfgHost);
597 AssertRCReturn(rc, rc);
598
599 /* Apply the sink's direction for the configuration to use to
600 * create the stream. */
601 if (pSink->enmDir == AUDMIXSINKDIR_INPUT)
602 {
603 CfgHost.DestSource.Source = pCfg->DestSource.Source;
604 CfgHost.enmDir = PDMAUDIODIR_IN;
605 CfgHost.enmLayout = pCfg->enmLayout;
606 }
607 else
608 {
609 CfgHost.DestSource.Dest = pCfg->DestSource.Dest;
610 CfgHost.enmDir = PDMAUDIODIR_OUT;
611 CfgHost.enmLayout = pCfg->enmLayout;
612 }
613
614 RTStrPrintf(CfgHost.szName, sizeof(CfgHost.szName), "%s", pCfg->szName);
615
616 rc = RTCritSectInit(&pMixStream->CritSect);
617 if (RT_SUCCESS(rc))
618 {
619 PPDMAUDIOSTREAM pStream;
620 rc = pConn->pfnStreamCreate(pConn, &CfgHost, pCfg, &pStream);
621 if (RT_SUCCESS(rc))
622 {
623 /* Save the audio stream pointer to this mixing stream. */
624 pMixStream->pStream = pStream;
625
626 /* Increase the stream's reference count to let others know
627 * we're reyling on it to be around now. */
628 pConn->pfnStreamRetain(pConn, pStream);
629 }
630 }
631
632 if (RT_SUCCESS(rc))
633 {
634 rc = RTCircBufCreate(&pMixStream->pCircBuf, DrvAudioHlpMilliToBytes(100 /* ms */, &pSink->PCMProps)); /** @todo Make this configurable. */
635 AssertRC(rc);
636 }
637
638 if (RT_SUCCESS(rc))
639 {
640 pMixStream->fFlags = fFlags;
641 pMixStream->pConn = pConn;
642
643 if (ppStream)
644 *ppStream = pMixStream;
645 }
646 else if (pMixStream)
647 {
648 int rc2 = RTCritSectDelete(&pMixStream->CritSect);
649 AssertRC(rc2);
650
651 if (pMixStream->pszName)
652 {
653 RTStrFree(pMixStream->pszName);
654 pMixStream->pszName = NULL;
655 }
656
657 RTMemFree(pMixStream);
658 pMixStream = NULL;
659 }
660
661 int rc2 = RTCritSectLeave(&pSink->CritSect);
662 AssertRC(rc2);
663
664 return rc;
665}
666
667/**
668 * Static helper function to translate a sink command
669 * to a PDM audio stream command.
670 *
671 * @returns PDM audio stream command, or PDMAUDIOSTREAMCMD_UNKNOWN if not found.
672 * @param enmCmd Mixer sink command to translate.
673 */
674static PDMAUDIOSTREAMCMD audioMixerSinkToStreamCmd(AUDMIXSINKCMD enmCmd)
675{
676 switch (enmCmd)
677 {
678 case AUDMIXSINKCMD_ENABLE: return PDMAUDIOSTREAMCMD_ENABLE;
679 case AUDMIXSINKCMD_DISABLE: return PDMAUDIOSTREAMCMD_DISABLE;
680 case AUDMIXSINKCMD_PAUSE: return PDMAUDIOSTREAMCMD_PAUSE;
681 case AUDMIXSINKCMD_RESUME: return PDMAUDIOSTREAMCMD_RESUME;
682 case AUDMIXSINKCMD_DROP: return PDMAUDIOSTREAMCMD_DROP;
683 default: break;
684 }
685
686 AssertMsgFailed(("Unsupported sink command %d\n", enmCmd));
687 return PDMAUDIOSTREAMCMD_UNKNOWN;
688}
689
690/**
691 * Controls a mixer sink.
692 *
693 * @returns IPRT status code.
694 * @param pSink Mixer sink to control.
695 * @param enmSinkCmd Sink command to set.
696 */
697int AudioMixerSinkCtl(PAUDMIXSINK pSink, AUDMIXSINKCMD enmSinkCmd)
698{
699 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
700
701 PDMAUDIOSTREAMCMD enmCmdStream = audioMixerSinkToStreamCmd(enmSinkCmd);
702 if (enmCmdStream == PDMAUDIOSTREAMCMD_UNKNOWN)
703 return VERR_NOT_SUPPORTED;
704
705 int rc = RTCritSectEnter(&pSink->CritSect);
706 if (RT_FAILURE(rc))
707 return rc;
708
709 /* Input sink and no recording source set? Bail out early. */
710 if ( pSink->enmDir == AUDMIXSINKDIR_INPUT
711 && pSink->In.pStreamRecSource == NULL)
712 {
713 int rc2 = RTCritSectLeave(&pSink->CritSect);
714 AssertRC(rc2);
715
716 return rc;
717 }
718
719 PAUDMIXSTREAM pStream;
720 if ( pSink->enmDir == AUDMIXSINKDIR_INPUT
721 && pSink->In.pStreamRecSource) /* Any recording source set? */
722 {
723 RTListForEach(&pSink->lstStreams, pStream, AUDMIXSTREAM, Node)
724 {
725 if (pStream == pSink->In.pStreamRecSource)
726 {
727 int rc2 = audioMixerStreamCtlInternal(pStream, enmCmdStream, AUDMIXSTRMCTL_FLAG_NONE);
728 if (rc2 == VERR_NOT_SUPPORTED)
729 rc2 = VINF_SUCCESS;
730
731 if (RT_SUCCESS(rc))
732 rc = rc2;
733 /* Keep going. Flag? */
734 }
735 }
736 }
737 else if (pSink->enmDir == AUDMIXSINKDIR_OUTPUT)
738 {
739 RTListForEach(&pSink->lstStreams, pStream, AUDMIXSTREAM, Node)
740 {
741 int rc2 = audioMixerStreamCtlInternal(pStream, enmCmdStream, AUDMIXSTRMCTL_FLAG_NONE);
742 if (rc2 == VERR_NOT_SUPPORTED)
743 rc2 = VINF_SUCCESS;
744
745 if (RT_SUCCESS(rc))
746 rc = rc2;
747 /* Keep going. Flag? */
748 }
749 }
750
751 switch (enmSinkCmd)
752 {
753 case AUDMIXSINKCMD_ENABLE:
754 {
755 /* Make sure to clear any other former flags again by assigning AUDMIXSINK_STS_RUNNING directly. */
756 pSink->fStatus = AUDMIXSINK_STS_RUNNING;
757 break;
758 }
759
760 case AUDMIXSINKCMD_DISABLE:
761 {
762 if (pSink->fStatus & AUDMIXSINK_STS_RUNNING)
763 {
764 /* Set the sink in a pending disable state first.
765 * The final status (disabled) will be set in the sink's iteration. */
766 pSink->fStatus |= AUDMIXSINK_STS_PENDING_DISABLE;
767 }
768 break;
769 }
770
771 case AUDMIXSINKCMD_DROP:
772 {
773 AudioMixBufReset(&pSink->MixBuf);
774
775 /* Clear dirty bit, keep others. */
776 pSink->fStatus &= ~AUDMIXSINK_STS_DIRTY;
777 break;
778 }
779
780 default:
781 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
782 break;
783 }
784
785 char *pszStatus = dbgAudioMixerSinkStatusToStr(pSink->fStatus);
786 LogRel2(("Mixer: Set new status of sink '%s' to %s\n", pSink->pszName, pszStatus));
787 LogFlowFunc(("[%s] enmCmd=%RU32, fStatus=%s, rc=%Rrc\n", pSink->pszName, enmSinkCmd, pszStatus, rc));
788 RTStrFree(pszStatus);
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 AudioMixBufDestroy(&pSink->MixBuf);
862 RTCritSectDelete(&pSink->CritSect);
863
864 RTMemFree(pSink);
865 pSink = NULL;
866}
867
868/**
869 * Returns the amount of bytes ready to be read from a sink since the last call
870 * to AudioMixerSinkUpdate().
871 *
872 * @returns Amount of bytes ready to be read from the sink.
873 * @param pSink Sink to return number of available bytes for.
874 */
875uint32_t AudioMixerSinkGetReadable(PAUDMIXSINK pSink)
876{
877 AssertPtrReturn(pSink, 0);
878
879 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_INPUT, ("%s: Can't read from a non-input sink\n", pSink->pszName));
880
881 int rc = RTCritSectEnter(&pSink->CritSect);
882 if (RT_FAILURE(rc))
883 return 0;
884
885 uint32_t cbReadable = 0;
886
887 if (pSink->fStatus & AUDMIXSINK_STS_RUNNING)
888 {
889#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF_IN
890# error "Implement me!"
891#else
892 PAUDMIXSTREAM pStreamRecSource = pSink->In.pStreamRecSource;
893 if (!pStreamRecSource)
894 {
895 Log3Func(("[%s] No recording source specified, skipping ...\n", pSink->pszName));
896 }
897 else
898 {
899 AssertPtr(pStreamRecSource->pConn);
900 cbReadable = pStreamRecSource->pConn->pfnStreamGetReadable(pStreamRecSource->pConn, pStreamRecSource->pStream);
901 }
902#endif
903 }
904
905 Log3Func(("[%s] cbReadable=%RU32\n", pSink->pszName, cbReadable));
906
907 int rc2 = RTCritSectLeave(&pSink->CritSect);
908 AssertRC(rc2);
909
910 return cbReadable;
911}
912
913/**
914 * Returns the sink's current recording source.
915 *
916 * @return Mixer stream which currently is set as current recording source, NULL if none is set.
917 * @param pSink Audio mixer sink to return current recording source for.
918 */
919PAUDMIXSTREAM AudioMixerSinkGetRecordingSource(PAUDMIXSINK pSink)
920{
921 int rc = RTCritSectEnter(&pSink->CritSect);
922 if (RT_FAILURE(rc))
923 return NULL;
924
925 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_INPUT, ("Specified sink is not an input sink\n"));
926
927 PAUDMIXSTREAM pStream = pSink->In.pStreamRecSource;
928
929 int rc2 = RTCritSectLeave(&pSink->CritSect);
930 AssertRC(rc2);
931
932 return pStream;
933}
934
935/**
936 * Returns the amount of bytes ready to be written to a sink since the last call
937 * to AudioMixerSinkUpdate().
938 *
939 * @returns Amount of bytes ready to be written to the sink.
940 * @param pSink Sink to return number of available bytes for.
941 */
942uint32_t AudioMixerSinkGetWritable(PAUDMIXSINK pSink)
943{
944 AssertPtrReturn(pSink, 0);
945
946 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_OUTPUT, ("%s: Can't write to a non-output sink\n", pSink->pszName));
947
948 int rc = RTCritSectEnter(&pSink->CritSect);
949 if (RT_FAILURE(rc))
950 return 0;
951
952 uint32_t cbWritable = 0;
953
954 if ( (pSink->fStatus & AUDMIXSINK_STS_RUNNING)
955 && !(pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE))
956 {
957 cbWritable = AudioMixBufFreeBytes(&pSink->MixBuf);
958 }
959
960 Log3Func(("[%s] cbWritable=%RU32 (%RU64ms)\n",
961 pSink->pszName, cbWritable, DrvAudioHlpBytesToMilli(cbWritable, &pSink->PCMProps)));
962
963 int rc2 = RTCritSectLeave(&pSink->CritSect);
964 AssertRC(rc2);
965
966 return cbWritable;
967}
968
969/**
970 * Returns the sink's mixing direction.
971 *
972 * @returns Mixing direction.
973 * @param pSink Sink to return direction for.
974 */
975AUDMIXSINKDIR AudioMixerSinkGetDir(PAUDMIXSINK pSink)
976{
977 AssertPtrReturn(pSink, AUDMIXSINKDIR_UNKNOWN);
978
979 int rc = RTCritSectEnter(&pSink->CritSect);
980 if (RT_FAILURE(rc))
981 return AUDMIXSINKDIR_UNKNOWN;
982
983 AUDMIXSINKDIR enmDir = pSink->enmDir;
984
985 int rc2 = RTCritSectLeave(&pSink->CritSect);
986 AssertRC(rc2);
987
988 return enmDir;
989}
990
991/**
992 * Returns the sink's (friendly) name.
993 *
994 * @returns The sink's (friendly) name.
995 */
996const char *AudioMixerSinkGetName(const PAUDMIXSINK pSink)
997{
998 AssertPtrReturn(pSink, "<Unknown>");
999
1000 return pSink->pszName;
1001}
1002
1003/**
1004 * Returns a specific mixer stream from a sink, based on its index.
1005 *
1006 * @returns Mixer stream if found, or NULL if not found.
1007 * @param pSink Sink to retrieve mixer stream from.
1008 * @param uIndex Index of the mixer stream to return.
1009 */
1010PAUDMIXSTREAM AudioMixerSinkGetStream(PAUDMIXSINK pSink, uint8_t uIndex)
1011{
1012 AssertPtrReturn(pSink, NULL);
1013
1014 int rc = RTCritSectEnter(&pSink->CritSect);
1015 if (RT_FAILURE(rc))
1016 return NULL;
1017
1018 AssertMsgReturn(uIndex < pSink->cStreams,
1019 ("Index %RU8 exceeds stream count (%RU8)", uIndex, pSink->cStreams), NULL);
1020
1021 /* Slow lookup, d'oh. */
1022 PAUDMIXSTREAM pStream = RTListGetFirst(&pSink->lstStreams, AUDMIXSTREAM, Node);
1023 while (uIndex)
1024 {
1025 pStream = RTListGetNext(&pSink->lstStreams, pStream, AUDMIXSTREAM, Node);
1026 uIndex--;
1027 }
1028
1029 /** @todo Do we need to raise the stream's reference count here? */
1030
1031 int rc2 = RTCritSectLeave(&pSink->CritSect);
1032 AssertRC(rc2);
1033
1034 AssertPtr(pStream);
1035 return pStream;
1036}
1037
1038/**
1039 * Returns the current status of a mixer sink.
1040 *
1041 * @returns The sink's current status.
1042 * @param pSink Mixer sink to return status for.
1043 */
1044AUDMIXSINKSTS AudioMixerSinkGetStatus(PAUDMIXSINK pSink)
1045{
1046 if (!pSink)
1047 return AUDMIXSINK_STS_NONE;
1048
1049 int rc2 = RTCritSectEnter(&pSink->CritSect);
1050 if (RT_FAILURE(rc2))
1051 return AUDMIXSINK_STS_NONE;
1052
1053 /* If the dirty flag is set, there is unprocessed data in the sink. */
1054 AUDMIXSINKSTS stsSink = pSink->fStatus;
1055
1056 rc2 = RTCritSectLeave(&pSink->CritSect);
1057 AssertRC(rc2);
1058
1059 return stsSink;
1060}
1061
1062/**
1063 * Returns the number of attached mixer streams to a mixer sink.
1064 *
1065 * @returns The number of attached mixer streams.
1066 * @param pSink Mixer sink to return number for.
1067 */
1068uint8_t AudioMixerSinkGetStreamCount(PAUDMIXSINK pSink)
1069{
1070 if (!pSink)
1071 return 0;
1072
1073 int rc2 = RTCritSectEnter(&pSink->CritSect);
1074 if (RT_FAILURE(rc2))
1075 return 0;
1076
1077 uint8_t cStreams = pSink->cStreams;
1078
1079 rc2 = RTCritSectLeave(&pSink->CritSect);
1080 AssertRC(rc2);
1081
1082 return cStreams;
1083}
1084
1085/**
1086 * Returns whether the sink is in an active state or not.
1087 * Note: The pending disable state also counts as active.
1088 *
1089 * @returns True if active, false if not.
1090 * @param pSink Sink to return active state for.
1091 */
1092bool AudioMixerSinkIsActive(PAUDMIXSINK pSink)
1093{
1094 if (!pSink)
1095 return false;
1096
1097 int rc2 = RTCritSectEnter(&pSink->CritSect);
1098 if (RT_FAILURE(rc2))
1099 return false;
1100
1101 bool fIsActive = pSink->fStatus & AUDMIXSINK_STS_RUNNING;
1102 /* Note: AUDMIXSINK_STS_PENDING_DISABLE implies AUDMIXSINK_STS_RUNNING. */
1103
1104 Log3Func(("[%s] fActive=%RTbool\n", pSink->pszName, fIsActive));
1105
1106 rc2 = RTCritSectLeave(&pSink->CritSect);
1107 AssertRC(rc2);
1108
1109 return fIsActive;
1110}
1111
1112/**
1113 * Reads audio data from a mixer sink.
1114 *
1115 * @returns IPRT status code.
1116 * @param pSink Mixer sink to read data from.
1117 * @param enmOp Mixer operation to use for reading the data.
1118 * @param pvBuf Buffer where to store the read data.
1119 * @param cbBuf Buffer size (in bytes) where to store the data.
1120 * @param pcbRead Number of bytes read. Optional.
1121 */
1122int AudioMixerSinkRead(PAUDMIXSINK pSink, AUDMIXOP enmOp, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
1123{
1124 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1125 RT_NOREF(enmOp);
1126 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1127 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1128 /* pcbRead is optional. */
1129
1130 /** @todo Handle mixing operation enmOp! */
1131
1132 int rc = RTCritSectEnter(&pSink->CritSect);
1133 if (RT_FAILURE(rc))
1134 return rc;
1135
1136 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_INPUT,
1137 ("Can't read from a sink which is not an input sink\n"));
1138
1139 uint32_t cbRead = 0;
1140
1141 /* Flag indicating whether this sink is in a 'clean' state,
1142 * e.g. there is no more data to read from. */
1143 bool fClean = true;
1144
1145 PAUDMIXSTREAM pStreamRecSource = pSink->In.pStreamRecSource;
1146 if (!pStreamRecSource)
1147 {
1148 Log3Func(("[%s] No recording source specified, skipping ...\n", pSink->pszName));
1149 }
1150 else if (!(pStreamRecSource->fStatus & AUDMIXSTREAM_STATUS_ENABLED))
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 /* Update each mixing sink stream's status. */
1596 PAUDMIXSTREAM pMixStream;
1597 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1598 {
1599 int rc2 = audioMixerStreamUpdateStatus(pMixStream);
1600 AssertRC(rc2);
1601 }
1602
1603 /* Number of disabled streams of this sink. */
1604 uint8_t cStreamsDisabled = pSink->cStreams;
1605
1606 /* Next, try to write (multiplex) as much audio data as possible to all connected mixer streams. */
1607 uint32_t cbToWriteToStreams = AudioMixBufUsedBytes(&pSink->MixBuf);
1608
1609 uint8_t arrChunkBuf[_1K]; /** @todo Hm ... some zero copy / shared buffers would be nice! */
1610 while (cbToWriteToStreams)
1611 {
1612 uint32_t cfChunk;
1613 rc = AudioMixBufAcquireReadBlock(&pSink->MixBuf, arrChunkBuf, RT_MIN(cbToWriteToStreams, sizeof(arrChunkBuf)), &cfChunk);
1614 if (RT_FAILURE(rc))
1615 break;
1616
1617 const uint32_t cbChunk = DrvAudioHlpFramesToBytes(cfChunk, &pSink->PCMProps);
1618 Assert(cbChunk <= sizeof(arrChunkBuf));
1619
1620 /* Multiplex the current chunk in a synchronized fashion to all connected streams. */
1621 uint32_t cbChunkWrittenMin = 0;
1622 rc = audioMixerSinkMultiplexSync(pSink, AUDMIXOP_COPY, arrChunkBuf, cbChunk, &cbChunkWrittenMin);
1623 if (RT_SUCCESS(rc))
1624 {
1625 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1626 {
1627 int rc2 = audioMixerSinkWriteToStream(pSink, pMixStream);
1628 AssertRC(rc2);
1629 }
1630 }
1631
1632 Log3Func(("[%s] cbChunk=%RU32, cbChunkWrittenMin=%RU32\n", pSink->pszName, cbChunk, cbChunkWrittenMin));
1633
1634 AudioMixBufReleaseReadBlock(&pSink->MixBuf, AUDIOMIXBUF_B2F(&pSink->MixBuf, cbChunkWrittenMin));
1635
1636 if ( RT_FAILURE(rc)
1637 || cbChunkWrittenMin == 0)
1638 break;
1639
1640 Assert(cbToWriteToStreams >= cbChunkWrittenMin);
1641 cbToWriteToStreams -= cbChunkWrittenMin;
1642 }
1643
1644 if ( !(pSink->fStatus & AUDMIXSINK_STS_DIRTY)
1645 && AudioMixBufUsed(&pSink->MixBuf)) /* Still audio output data left? Consider the sink as being "dirty" then. */
1646 {
1647 /* Set dirty bit. */
1648 pSink->fStatus |= AUDMIXSINK_STS_DIRTY;
1649 }
1650
1651 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1652 {
1653 /* Input sink and not the recording source? Skip. */
1654 if ( pSink->enmDir == AUDMIXSINKDIR_INPUT
1655 && pSink->In.pStreamRecSource != pMixStream)
1656 continue;
1657
1658 PPDMAUDIOSTREAM pStream = pMixStream->pStream;
1659 AssertPtr(pStream);
1660
1661 PPDMIAUDIOCONNECTOR pConn = pMixStream->pConn;
1662 AssertPtr(pConn);
1663
1664 uint32_t cfProc = 0;
1665
1666 if (!(pMixStream->fStatus & AUDMIXSTREAM_STATUS_ENABLED))
1667 continue;
1668
1669 int rc2 = pConn->pfnStreamIterate(pConn, pStream);
1670 if (RT_SUCCESS(rc2))
1671 {
1672 if (pSink->enmDir == AUDMIXSINKDIR_INPUT)
1673 {
1674 rc2 = pConn->pfnStreamCapture(pConn, pStream, &cfProc);
1675 if (RT_FAILURE(rc2))
1676 {
1677 LogFunc(("%s: Failed capturing stream '%s', rc=%Rrc\n", pSink->pszName, pStream->szName, rc2));
1678 continue;
1679 }
1680
1681 if (cfProc)
1682 pSink->fStatus |= AUDMIXSINK_STS_DIRTY;
1683 }
1684 else if (pSink->enmDir == AUDMIXSINKDIR_OUTPUT)
1685 {
1686 rc2 = pConn->pfnStreamPlay(pConn, pStream, &cfProc);
1687 if (RT_FAILURE(rc2))
1688 {
1689 LogFunc(("%s: Failed playing stream '%s', rc=%Rrc\n", pSink->pszName, pStream->szName, rc2));
1690 continue;
1691 }
1692 }
1693 else
1694 {
1695 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
1696 continue;
1697 }
1698 }
1699
1700 const PDMAUDIOSTREAMSTS streamStatusNew = pConn->pfnStreamGetStatus(pConn, pStream);
1701
1702 /* Is the stream enabled or in pending disable state?
1703 * Don't consider this stream as being disabled then. */
1704 if ( (streamStatusNew & PDMAUDIOSTREAMSTS_FLAG_ENABLED)
1705 || (streamStatusNew & PDMAUDIOSTREAMSTS_FLAG_PENDING_DISABLE))
1706 {
1707 cStreamsDisabled--;
1708 }
1709 /* Note: The mixer stream's internal status will be updated in the next iteration of this function. */
1710
1711 Log3Func(("\t%s: cPlayed/cCaptured=%RU32, rc2=%Rrc\n", pStream->szName, cfProc, rc2));
1712 }
1713
1714 Log3Func(("[%s] fPendingDisable=%RTbool, %RU8/%RU8 streams disabled\n",
1715 pSink->pszName, RT_BOOL(pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE), cStreamsDisabled, pSink->cStreams));
1716
1717 /* Update last updated timestamp. */
1718 pSink->tsLastUpdatedMs = RTTimeMilliTS();
1719
1720 /* All streams disabled and the sink is in pending disable mode? */
1721 if ( cStreamsDisabled == pSink->cStreams
1722 && (pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE))
1723 {
1724 audioMixerSinkReset(pSink);
1725 }
1726
1727 return rc;
1728}
1729
1730/**
1731 * Updates (invalidates) a mixer sink.
1732 *
1733 * @returns IPRT status code.
1734 * @param pSink Mixer sink to update.
1735 */
1736int AudioMixerSinkUpdate(PAUDMIXSINK pSink)
1737{
1738 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1739
1740 int rc = RTCritSectEnter(&pSink->CritSect);
1741 if (RT_FAILURE(rc))
1742 return rc;
1743
1744 rc = audioMixerSinkUpdateInternal(pSink);
1745
1746 int rc2 = RTCritSectLeave(&pSink->CritSect);
1747 AssertRC(rc2);
1748
1749 return rc;
1750}
1751
1752/**
1753 * Updates the (master) volume of a mixer sink.
1754 *
1755 * @returns IPRT status code.
1756 * @param pSink Mixer sink to update volume for.
1757 * @param pVolMaster Master volume to set.
1758 */
1759static int audioMixerSinkUpdateVolume(PAUDMIXSINK pSink, const PPDMAUDIOVOLUME pVolMaster)
1760{
1761 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1762 AssertPtrReturn(pVolMaster, VERR_INVALID_POINTER);
1763
1764 LogFlowFunc(("[%s] Master fMuted=%RTbool, lVol=%RU32, rVol=%RU32\n",
1765 pSink->pszName, pVolMaster->fMuted, pVolMaster->uLeft, pVolMaster->uRight));
1766 LogFlowFunc(("[%s] fMuted=%RTbool, lVol=%RU32, rVol=%RU32 ",
1767 pSink->pszName, pSink->Volume.fMuted, pSink->Volume.uLeft, pSink->Volume.uRight));
1768
1769 /** @todo Very crude implementation for now -- needs more work! */
1770
1771 pSink->VolumeCombined.fMuted = pVolMaster->fMuted || pSink->Volume.fMuted;
1772
1773 pSink->VolumeCombined.uLeft = ( (pSink->Volume.uLeft ? pSink->Volume.uLeft : 1)
1774 * (pVolMaster->uLeft ? pVolMaster->uLeft : 1)) / PDMAUDIO_VOLUME_MAX;
1775
1776 pSink->VolumeCombined.uRight = ( (pSink->Volume.uRight ? pSink->Volume.uRight : 1)
1777 * (pVolMaster->uRight ? pVolMaster->uRight : 1)) / PDMAUDIO_VOLUME_MAX;
1778
1779 LogFlow(("-> fMuted=%RTbool, lVol=%RU32, rVol=%RU32\n",
1780 pSink->VolumeCombined.fMuted, pSink->VolumeCombined.uLeft, pSink->VolumeCombined.uRight));
1781
1782 /* Propagate new sink volume to all streams in the sink. */
1783 PAUDMIXSTREAM pMixStream;
1784 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1785 {
1786 int rc2 = pMixStream->pConn->pfnStreamSetVolume(pMixStream->pConn, pMixStream->pStream, &pSink->VolumeCombined);
1787 AssertRC(rc2);
1788 }
1789
1790 return VINF_SUCCESS;
1791}
1792
1793/**
1794 * Writes (buffered) output data of a sink's stream to the bound audio connector stream.
1795 *
1796 * @returns IPRT status code.
1797 * @param pSink Sink of stream that contains the mixer stream.
1798 * @param pMixStream Mixer stream to write output data for.
1799 */
1800static int audioMixerSinkWriteToStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pMixStream)
1801{
1802 if (!pMixStream->pCircBuf)
1803 return VINF_SUCCESS;
1804
1805 return audioMixerSinkWriteToStreamEx(pSink, pMixStream, (uint32_t)RTCircBufUsed(pMixStream->pCircBuf), NULL /* pcbWritten */);
1806}
1807
1808/**
1809 * Writes (buffered) output data of a sink's stream to the bound audio connector stream, extended version.
1810 *
1811 * @returns IPRT status code.
1812 * @param pSink Sink of stream that contains the mixer stream.
1813 * @param pMixStream Mixer stream to write output data for.
1814 * @param cbToWrite Size (in bytes) to write.
1815 * @param pcbWritten Size (in bytes) written on success. Optional.
1816 */
1817static int audioMixerSinkWriteToStreamEx(PAUDMIXSINK pSink, PAUDMIXSTREAM pMixStream, uint32_t cbToWrite, uint32_t *pcbWritten)
1818{
1819 /* pcbWritten is optional. */
1820
1821 if ( !cbToWrite
1822 || !(pMixStream->fStatus & AUDMIXSTREAM_STATUS_ENABLED))
1823 {
1824 if (pcbWritten)
1825 *pcbWritten = 0;
1826
1827 return VINF_SUCCESS;
1828 }
1829
1830 PRTCIRCBUF pCircBuf = pMixStream->pCircBuf;
1831
1832 const uint32_t cbWritableStream = pMixStream->pConn->pfnStreamGetWritable(pMixStream->pConn, pMixStream->pStream);
1833 cbToWrite = RT_MIN(cbToWrite, RT_MIN((uint32_t)RTCircBufUsed(pCircBuf), cbWritableStream));
1834
1835 Log3Func(("[%s] cbWritableStream=%RU32, cbToWrite=%RU32\n",
1836 pMixStream->pszName, cbWritableStream, cbToWrite));
1837
1838 uint32_t cbWritten = 0;
1839
1840 int rc = VINF_SUCCESS;
1841
1842 while (cbToWrite)
1843 {
1844 void *pvChunk;
1845 size_t cbChunk;
1846 RTCircBufAcquireReadBlock(pCircBuf, cbToWrite, &pvChunk, &cbChunk);
1847
1848 Log3Func(("[%s] cbChunk=%RU32\n", pMixStream->pszName, cbChunk));
1849
1850 uint32_t cbChunkWritten = 0;
1851 if (cbChunk)
1852 {
1853 rc = pMixStream->pConn->pfnStreamWrite(pMixStream->pConn, pMixStream->pStream, pvChunk, (uint32_t)cbChunk,
1854 &cbChunkWritten);
1855 if (RT_FAILURE(rc))
1856 {
1857 if (rc == VERR_BUFFER_OVERFLOW)
1858 {
1859 LogRel2(("Mixer: Buffer overrun for mixer stream '%s' (sink '%s')\n", pMixStream->pszName, pSink->pszName));
1860 break;
1861 }
1862 else if (rc == VERR_AUDIO_STREAM_NOT_READY)
1863 {
1864 /* Stream is not enabled, just skip. */
1865 rc = VINF_SUCCESS;
1866 }
1867 else
1868 LogRel2(("Mixer: Writing to mixer stream '%s' (sink '%s') failed, rc=%Rrc\n",
1869 pMixStream->pszName, pSink->pszName, rc));
1870
1871 if (RT_FAILURE(rc))
1872 LogFunc(("[%s] Failed writing to stream '%s': %Rrc\n", pSink->pszName, pMixStream->pszName, rc));
1873 }
1874 }
1875
1876 RTCircBufReleaseReadBlock(pCircBuf, cbChunkWritten);
1877
1878 if ( RT_FAILURE(rc)
1879 || !cbChunkWritten)
1880 break;
1881
1882 Assert(cbToWrite >= cbChunkWritten);
1883 cbToWrite -= (uint32_t)cbChunkWritten;
1884
1885 cbWritten += (uint32_t)cbChunkWritten;
1886 }
1887
1888 Log3Func(("[%s] cbWritten=%RU32\n", pMixStream->pszName, cbWritten));
1889
1890 if (pcbWritten)
1891 *pcbWritten = cbWritten;
1892
1893#ifdef DEBUG_andy
1894 AssertRC(rc);
1895#endif
1896
1897 return rc;
1898}
1899
1900/**
1901 * Multiplexes audio output data to all connected mixer streams in a synchronized fashion, e.g.
1902 * only multiplex as much data as all streams can handle at this time.
1903 *
1904 * @returns IPRT status code.
1905 * @param pSink Sink to write audio output to.
1906 * @param enmOp What mixing operation to use. Currently not implemented.
1907 * @param pvBuf Pointer to audio data to write.
1908 * @param cbBuf Size (in bytes) of audio data to write.
1909 * @param pcbWrittenMin Returns minimum size (in bytes) successfully written to all mixer streams. Optional.
1910 */
1911static int audioMixerSinkMultiplexSync(PAUDMIXSINK pSink, AUDMIXOP enmOp, const void *pvBuf, uint32_t cbBuf,
1912 uint32_t *pcbWrittenMin)
1913{
1914 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1915 RT_NOREF(enmOp);
1916
1917 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_OUTPUT,
1918 ("%s: Can't multiplex to a sink which is not an output sink\n", pSink->pszName));
1919
1920 int rc = VINF_SUCCESS;
1921
1922 uint32_t cbToWriteMin = UINT32_MAX;
1923
1924 Log3Func(("[%s] cbBuf=%RU32\n", pSink->pszName, cbBuf));
1925
1926 PAUDMIXSTREAM pMixStream;
1927 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1928 {
1929 if (!(pMixStream->fStatus & AUDMIXSTREAM_STATUS_ENABLED)) /* Mixing stream not enabled? Skip handling. */
1930 {
1931 Log3Func(("[%s] Stream '%s' disabled, skipping ...\n", pSink->pszName, pMixStream->pszName));
1932 continue;
1933 }
1934
1935 cbToWriteMin = RT_MIN(cbBuf, RT_MIN(cbToWriteMin, (uint32_t)RTCircBufFree(pMixStream->pCircBuf)));
1936 }
1937
1938 if (cbToWriteMin == UINT32_MAX) /* No space at all? */
1939 cbToWriteMin = 0;
1940
1941 if (cbToWriteMin)
1942 {
1943 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1944 {
1945 if (!(pMixStream->fStatus & AUDMIXSTREAM_STATUS_ENABLED)) /* Mixing stream not enabled? Skip handling. */
1946 continue;
1947
1948 PRTCIRCBUF pCircBuf = pMixStream->pCircBuf;
1949 void *pvChunk;
1950 size_t cbChunk;
1951
1952 uint32_t cbWrittenBuf = 0;
1953 uint32_t cbToWriteBuf = cbToWriteMin;
1954
1955 while (cbToWriteBuf)
1956 {
1957 RTCircBufAcquireWriteBlock(pCircBuf, cbToWriteBuf, &pvChunk, &cbChunk);
1958
1959 if (cbChunk)
1960 memcpy(pvChunk, (uint8_t *)pvBuf + cbWrittenBuf, cbChunk);
1961
1962 RTCircBufReleaseWriteBlock(pCircBuf, cbChunk);
1963
1964 cbWrittenBuf += (uint32_t)cbChunk;
1965 Assert(cbWrittenBuf <= cbBuf);
1966
1967 Assert(cbToWriteBuf >= cbChunk);
1968 cbToWriteBuf -= (uint32_t)cbChunk;
1969 }
1970
1971 if (cbWrittenBuf) /* Update the mixer stream's last written time stamp. */
1972 pMixStream->tsLastReadWrittenNs = RTTimeNanoTS();
1973
1974 Log3Func(("[%s] Mixer stream '%s' -> cbWrittenBuf=%RU32\n", pSink->pszName, pMixStream->pszName, cbWrittenBuf));
1975 }
1976 }
1977
1978 Log3Func(("[%s] cbBuf=%RU32, cbToWriteMin=%RU32\n", pSink->pszName, cbBuf, cbToWriteMin));
1979
1980 if (pcbWrittenMin)
1981 *pcbWrittenMin = cbToWriteMin;
1982
1983 return rc;
1984}
1985
1986/**
1987 * Writes data to a mixer sink.
1988 *
1989 * @returns IPRT status code.
1990 * @param pSink Sink to write data to.
1991 * @param enmOp Mixer operation to use when writing data to the sink.
1992 * @param pvBuf Buffer containing the audio data to write.
1993 * @param cbBuf Size (in bytes) of the buffer containing the audio data.
1994 * @param pcbWritten Number of bytes written. Optional.
1995 */
1996int AudioMixerSinkWrite(PAUDMIXSINK pSink, AUDMIXOP enmOp, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
1997{
1998 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1999 RT_NOREF(enmOp);
2000 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
2001 AssertReturn (cbBuf, VERR_INVALID_PARAMETER);
2002 /* pcbWritten is optional. */
2003
2004 int rc = RTCritSectEnter(&pSink->CritSect);
2005 if (RT_FAILURE(rc))
2006 return rc;
2007
2008 AssertMsg(pSink->fStatus & AUDMIXSINK_STS_RUNNING,
2009 ("%s: Can't write to a sink which is not running (anymore) (status 0x%x)\n", pSink->pszName, pSink->fStatus));
2010 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_OUTPUT,
2011 ("%s: Can't write to a sink which is not an output sink\n", pSink->pszName));
2012
2013 uint32_t cbWritten = 0;
2014 uint32_t cbToWrite = RT_MIN(AudioMixBufFreeBytes(&pSink->MixBuf), cbBuf);
2015 while (cbToWrite)
2016 {
2017 /* First, write the data to the mixer sink's own mixing buffer.
2018 * Here the audio data can be transformed into the mixer sink's format. */
2019 uint32_t cfWritten = 0;
2020 rc = AudioMixBufWriteCirc(&pSink->MixBuf, (uint8_t *)pvBuf + cbWritten, cbToWrite, &cfWritten);
2021 if (RT_FAILURE(rc))
2022 break;
2023
2024 const uint32_t cbWrittenChunk = DrvAudioHlpFramesToBytes(cfWritten, &pSink->PCMProps);
2025
2026 Assert(cbToWrite >= cbWrittenChunk);
2027 cbToWrite -= cbWrittenChunk;
2028 cbWritten += cbWrittenChunk;
2029 }
2030
2031 Log3Func(("[%s] cbBuf=%RU32 -> cbWritten=%RU32\n", pSink->pszName, cbBuf, cbWritten));
2032
2033 /* Update the sink's last written time stamp. */
2034 pSink->tsLastReadWrittenNs = RTTimeNanoTS();
2035
2036 if (pcbWritten)
2037 *pcbWritten = cbWritten;
2038
2039 int rc2 = RTCritSectLeave(&pSink->CritSect);
2040 AssertRC(rc2);
2041
2042 return rc;
2043}
2044
2045/*********************************************************************************************************************************
2046 * Mixer Stream implementation.
2047 ********************************************************************************************************************************/
2048
2049/**
2050 * Controls a mixer stream, internal version.
2051 *
2052 * @returns IPRT status code.
2053 * @param pMixStream Mixer stream to control.
2054 * @param enmCmd Mixer stream command to use.
2055 * @param fCtl Additional control flags. Pass 0.
2056 */
2057static int audioMixerStreamCtlInternal(PAUDMIXSTREAM pMixStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl)
2058{
2059 AssertPtr(pMixStream->pConn);
2060 AssertPtr(pMixStream->pStream);
2061
2062 RT_NOREF(fCtl);
2063
2064 int rc = pMixStream->pConn->pfnStreamControl(pMixStream->pConn, pMixStream->pStream, enmCmd);
2065
2066 LogFlowFunc(("[%s] enmCmd=%ld, rc=%Rrc\n", pMixStream->pszName, enmCmd, rc));
2067
2068 return rc;
2069}
2070
2071/**
2072 * Updates a mixer stream's internal status.
2073 *
2074 * @returns VBox status code.
2075 * @param pMixStream Mixer stream to to update internal status for.
2076 */
2077static int audioMixerStreamUpdateStatus(PAUDMIXSTREAM pMixStream)
2078{
2079 pMixStream->fStatus = AUDMIXSTREAM_STATUS_NONE;
2080
2081 if (pMixStream->pConn) /* Audio connector available? */
2082 {
2083 const uint32_t fStreamStatus = pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream);
2084
2085 if (DrvAudioHlpStreamStatusIsReady(fStreamStatus))
2086 pMixStream->fStatus |= AUDMIXSTREAM_STATUS_ENABLED;
2087
2088 AssertPtr(pMixStream->pSink);
2089 switch (pMixStream->pSink->enmDir)
2090 {
2091 case AUDMIXSINKDIR_INPUT:
2092 if (DrvAudioHlpStreamStatusCanRead(fStreamStatus))
2093 pMixStream->fStatus |= AUDMIXSTREAM_STATUS_CAN_READ;
2094 break;
2095
2096 case AUDMIXSINKDIR_OUTPUT:
2097 if (DrvAudioHlpStreamStatusCanWrite(fStreamStatus))
2098 pMixStream->fStatus |= AUDMIXSTREAM_STATUS_CAN_WRITE;
2099 break;
2100
2101 default:
2102 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
2103 break;
2104 }
2105 }
2106
2107 LogFlowFunc(("[%s] -> 0x%x\n", pMixStream->pszName, pMixStream->fStatus));
2108 return VINF_SUCCESS;
2109}
2110
2111/**
2112 * Controls a mixer stream.
2113 *
2114 * @returns IPRT status code.
2115 * @param pMixStream Mixer stream to control.
2116 * @param enmCmd Mixer stream command to use.
2117 * @param fCtl Additional control flags. Pass 0.
2118 */
2119int AudioMixerStreamCtl(PAUDMIXSTREAM pMixStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl)
2120{
2121 RT_NOREF(fCtl);
2122 AssertPtrReturn(pMixStream, VERR_INVALID_POINTER);
2123 /** @todo Validate fCtl. */
2124
2125 int rc = RTCritSectEnter(&pMixStream->CritSect);
2126 if (RT_FAILURE(rc))
2127 return rc;
2128
2129 rc = audioMixerStreamCtlInternal(pMixStream, enmCmd, fCtl);
2130
2131 int rc2 = RTCritSectLeave(&pMixStream->CritSect);
2132 if (RT_SUCCESS(rc))
2133 rc = rc2;
2134
2135 return rc;
2136}
2137
2138/**
2139 * Destroys a mixer stream, internal version.
2140 *
2141 * @param pMixStream Mixer stream to destroy.
2142 */
2143static void audioMixerStreamDestroyInternal(PAUDMIXSTREAM pMixStream)
2144{
2145 AssertPtrReturnVoid(pMixStream);
2146
2147 LogFunc(("%s\n", pMixStream->pszName));
2148
2149 if (pMixStream->pConn) /* Stream has a connector interface present? */
2150 {
2151 if (pMixStream->pStream)
2152 {
2153 pMixStream->pConn->pfnStreamRelease(pMixStream->pConn, pMixStream->pStream);
2154 pMixStream->pConn->pfnStreamDestroy(pMixStream->pConn, pMixStream->pStream);
2155
2156 pMixStream->pStream = NULL;
2157 }
2158
2159 pMixStream->pConn = NULL;
2160 }
2161
2162 if (pMixStream->pszName)
2163 {
2164 RTStrFree(pMixStream->pszName);
2165 pMixStream->pszName = NULL;
2166 }
2167
2168 if (pMixStream->pCircBuf)
2169 {
2170 RTCircBufDestroy(pMixStream->pCircBuf);
2171 pMixStream->pCircBuf = NULL;
2172 }
2173
2174 int rc2 = RTCritSectDelete(&pMixStream->CritSect);
2175 AssertRC(rc2);
2176
2177 RTMemFree(pMixStream);
2178 pMixStream = NULL;
2179}
2180
2181/**
2182 * Destroys a mixer stream.
2183 *
2184 * @param pMixStream Mixer stream to destroy.
2185 */
2186void AudioMixerStreamDestroy(PAUDMIXSTREAM pMixStream)
2187{
2188 if (!pMixStream)
2189 return;
2190
2191 int rc2 = RTCritSectEnter(&pMixStream->CritSect);
2192 AssertRC(rc2);
2193
2194 LogFunc(("%s\n", pMixStream->pszName));
2195
2196 if (pMixStream->pSink) /* Is the stream part of a sink? */
2197 {
2198 /* Save sink pointer, as after audioMixerSinkRemoveStreamInternal() the
2199 * pointer will be gone from the stream. */
2200 PAUDMIXSINK pSink = pMixStream->pSink;
2201
2202 rc2 = audioMixerSinkRemoveStreamInternal(pSink, pMixStream);
2203 if (RT_SUCCESS(rc2))
2204 {
2205 Assert(pSink->cStreams);
2206 pSink->cStreams--;
2207 }
2208 }
2209 else
2210 rc2 = VINF_SUCCESS;
2211
2212 int rc3 = RTCritSectLeave(&pMixStream->CritSect);
2213 AssertRC(rc3);
2214
2215 if (RT_SUCCESS(rc2))
2216 {
2217 audioMixerStreamDestroyInternal(pMixStream);
2218 pMixStream = NULL;
2219 }
2220
2221 LogFlowFunc(("Returning %Rrc\n", rc2));
2222}
2223
2224/**
2225 * Returns whether a mixer stream currently is active (playing/recording) or not.
2226 *
2227 * @returns @c true if playing/recording, @c false if not.
2228 * @param pMixStream Mixer stream to return status for.
2229 */
2230bool AudioMixerStreamIsActive(PAUDMIXSTREAM pMixStream)
2231{
2232 int rc2 = RTCritSectEnter(&pMixStream->CritSect);
2233 if (RT_FAILURE(rc2))
2234 return false;
2235
2236 AssertPtr(pMixStream->pConn);
2237 AssertPtr(pMixStream->pStream);
2238
2239 bool fIsActive;
2240
2241 if ( pMixStream->pConn
2242 && pMixStream->pStream
2243 && RT_BOOL(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream) & PDMAUDIOSTREAMSTS_FLAG_ENABLED))
2244 {
2245 fIsActive = true;
2246 }
2247 else
2248 fIsActive = false;
2249
2250 rc2 = RTCritSectLeave(&pMixStream->CritSect);
2251 AssertRC(rc2);
2252
2253 return fIsActive;
2254}
2255
2256/**
2257 * Returns whether a mixer stream is valid (e.g. initialized and in a working state) or not.
2258 *
2259 * @returns @c true if valid, @c false if not.
2260 * @param pMixStream Mixer stream to return status for.
2261 */
2262bool AudioMixerStreamIsValid(PAUDMIXSTREAM pMixStream)
2263{
2264 if (!pMixStream)
2265 return false;
2266
2267 int rc2 = RTCritSectEnter(&pMixStream->CritSect);
2268 if (RT_FAILURE(rc2))
2269 return false;
2270
2271 bool fIsValid;
2272
2273 if ( pMixStream->pConn
2274 && pMixStream->pStream
2275 && RT_BOOL(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream) & PDMAUDIOSTREAMSTS_FLAG_INITIALIZED))
2276 {
2277 fIsValid = true;
2278 }
2279 else
2280 fIsValid = false;
2281
2282 rc2 = RTCritSectLeave(&pMixStream->CritSect);
2283 AssertRC(rc2);
2284
2285 return fIsValid;
2286}
2287
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