VirtualBox

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

最後變更 在這個檔案從77237是 77184,由 vboxsync 提交於 6 年 前

AudioMixer: Destroy the mix buffer when the sink gets destroyed

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 68.7 KB
 
1/* $Id: AudioMixer.cpp 77184 2019-02-06 18:53:14Z 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.alldomusa.eu.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 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 (!DrvAudioHlpStreamStatusCanRead(
1151 pStreamRecSource->pConn->pfnStreamGetStatus(pStreamRecSource->pConn, pStreamRecSource->pStream)))
1152 {
1153 Log3Func(("[%s] Stream '%s' disabled, skipping ...\n", pSink->pszName, pStreamRecSource->pszName));
1154 }
1155 else
1156 {
1157 uint32_t cbToRead = cbBuf;
1158 while (cbToRead)
1159 {
1160 uint32_t cbReadStrm;
1161 AssertPtr(pStreamRecSource->pConn);
1162#ifdef VBOX_AUDIO_MIXER_WITH_MIXBUF_IN
1163# error "Implement me!"
1164#else
1165 rc = pStreamRecSource->pConn->pfnStreamRead(pStreamRecSource->pConn, pStreamRecSource->pStream,
1166 (uint8_t *)pvBuf + cbRead, cbToRead, &cbReadStrm);
1167#endif
1168 if (RT_FAILURE(rc))
1169 LogFunc(("[%s] Failed reading from stream '%s': %Rrc\n", pSink->pszName, pStreamRecSource->pszName, rc));
1170
1171 Log3Func(("[%s] Stream '%s': Read %RU32 bytes\n", pSink->pszName, pStreamRecSource->pszName, cbReadStrm));
1172
1173 if ( RT_FAILURE(rc)
1174 || !cbReadStrm)
1175 break;
1176
1177 AssertBreakStmt(cbReadStrm <= cbToRead, rc = VERR_BUFFER_OVERFLOW);
1178 cbToRead -= cbReadStrm;
1179 cbRead += cbReadStrm;
1180 Assert(cbRead <= cbBuf);
1181 }
1182
1183 uint32_t cbReadable = pStreamRecSource->pConn->pfnStreamGetReadable(pStreamRecSource->pConn, pStreamRecSource->pStream);
1184
1185 /* Still some data available? Then sink is not clean (yet). */
1186 if (cbReadable)
1187 fClean = false;
1188
1189 if (RT_SUCCESS(rc))
1190 {
1191 if (fClean)
1192 pSink->fStatus &= ~AUDMIXSINK_STS_DIRTY;
1193
1194 /* Update our last read time stamp. */
1195 pSink->tsLastReadWrittenNs = RTTimeNanoTS();
1196
1197#ifdef VBOX_AUDIO_MIXER_DEBUG
1198 int rc2 = DrvAudioHlpFileWrite(pSink->Dbg.pFile, pvBuf, cbRead, 0 /* fFlags */);
1199 AssertRC(rc2);
1200#endif
1201 }
1202 }
1203
1204#ifdef LOG_ENABLED
1205 char *pszStatus = dbgAudioMixerSinkStatusToStr(pSink->fStatus);
1206 Log2Func(("[%s] cbRead=%RU32, fClean=%RTbool, fStatus=%s, rc=%Rrc\n", pSink->pszName, cbRead, fClean, pszStatus, rc));
1207 RTStrFree(pszStatus);
1208#endif
1209
1210 if (pcbRead)
1211 *pcbRead = cbRead;
1212
1213 int rc2 = RTCritSectLeave(&pSink->CritSect);
1214 AssertRC(rc2);
1215
1216 return rc;
1217}
1218
1219/**
1220 * Removes a mixer stream from a mixer sink, internal version.
1221 *
1222 * @returns IPRT status code.
1223 * @param pSink Sink to remove mixer stream from.
1224 * @param pStream Stream to remove.
1225 */
1226static int audioMixerSinkRemoveStreamInternal(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream)
1227{
1228 AssertPtrReturn(pSink, VERR_INVALID_PARAMETER);
1229 if ( !pStream
1230 || !pStream->pSink) /* Not part of a sink anymore? */
1231 {
1232 return VERR_NOT_FOUND;
1233 }
1234
1235 AssertMsgReturn(pStream->pSink == pSink, ("Stream '%s' is not part of sink '%s'\n",
1236 pStream->pszName, pSink->pszName), VERR_NOT_FOUND);
1237
1238 LogFlowFunc(("[%s] (Stream = %s), cStreams=%RU8\n",
1239 pSink->pszName, pStream->pStream->szName, pSink->cStreams));
1240
1241 /* Remove stream from sink. */
1242 RTListNodeRemove(&pStream->Node);
1243
1244 int rc = VINF_SUCCESS;
1245
1246 if (pSink->enmDir == AUDMIXSINKDIR_INPUT)
1247 {
1248 /* Make sure to also un-set the recording source if this stream was set
1249 * as the recording source before. */
1250 if (pStream == pSink->In.pStreamRecSource)
1251 rc = audioMixerSinkSetRecSourceInternal(pSink, NULL);
1252 }
1253
1254 /* Set sink to NULL so that we know we're not part of any sink anymore. */
1255 pStream->pSink = NULL;
1256
1257 return rc;
1258}
1259
1260/**
1261 * Removes a mixer stream from a mixer sink.
1262 *
1263 * @param pSink Sink to remove mixer stream from.
1264 * @param pStream Stream to remove.
1265 */
1266void AudioMixerSinkRemoveStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream)
1267{
1268 int rc2 = RTCritSectEnter(&pSink->CritSect);
1269 AssertRC(rc2);
1270
1271 rc2 = audioMixerSinkRemoveStreamInternal(pSink, pStream);
1272 if (RT_SUCCESS(rc2))
1273 {
1274 Assert(pSink->cStreams);
1275 pSink->cStreams--;
1276 }
1277
1278 rc2 = RTCritSectLeave(&pSink->CritSect);
1279 AssertRC(rc2);
1280}
1281
1282/**
1283 * Removes all attached streams from a given sink.
1284 *
1285 * @param pSink Sink to remove attached streams from.
1286 */
1287static void audioMixerSinkRemoveAllStreamsInternal(PAUDMIXSINK pSink)
1288{
1289 if (!pSink)
1290 return;
1291
1292 LogFunc(("%s\n", pSink->pszName));
1293
1294 PAUDMIXSTREAM pStream, pStreamNext;
1295 RTListForEachSafe(&pSink->lstStreams, pStream, pStreamNext, AUDMIXSTREAM, Node)
1296 audioMixerSinkRemoveStreamInternal(pSink, pStream);
1297}
1298
1299/**
1300 * Resets the sink's state.
1301 *
1302 * @param pSink Sink to reset.
1303 */
1304static void audioMixerSinkReset(PAUDMIXSINK pSink)
1305{
1306 if (!pSink)
1307 return;
1308
1309 LogFunc(("[%s]\n", pSink->pszName));
1310
1311 AudioMixBufReset(&pSink->MixBuf);
1312
1313 /* Update last updated timestamp. */
1314 pSink->tsLastUpdatedMs = 0;
1315
1316 /* Reset status. */
1317 pSink->fStatus = AUDMIXSINK_STS_NONE;
1318}
1319
1320/**
1321 * Removes all attached streams from a given sink.
1322 *
1323 * @param pSink Sink to remove attached streams from.
1324 */
1325void AudioMixerSinkRemoveAllStreams(PAUDMIXSINK pSink)
1326{
1327 if (!pSink)
1328 return;
1329
1330 int rc2 = RTCritSectEnter(&pSink->CritSect);
1331 AssertRC(rc2);
1332
1333 audioMixerSinkRemoveAllStreamsInternal(pSink);
1334
1335 pSink->cStreams = 0;
1336
1337 rc2 = RTCritSectLeave(&pSink->CritSect);
1338 AssertRC(rc2);
1339}
1340
1341/**
1342 * Resets a sink. This will immediately stop all processing.
1343 *
1344 * @param pSink Sink to reset.
1345 */
1346void AudioMixerSinkReset(PAUDMIXSINK pSink)
1347{
1348 if (!pSink)
1349 return;
1350
1351 int rc2 = RTCritSectEnter(&pSink->CritSect);
1352 AssertRC(rc2);
1353
1354 LogFlowFunc(("[%s]\n", pSink->pszName));
1355
1356 audioMixerSinkReset(pSink);
1357
1358 rc2 = RTCritSectLeave(&pSink->CritSect);
1359 AssertRC(rc2);
1360}
1361
1362/**
1363 * Returns the audio format of a mixer sink.
1364 *
1365 * @param pSink Sink to retrieve audio format for.
1366 * @param pPCMProps Where to the returned audio format.
1367 */
1368void AudioMixerSinkGetFormat(PAUDMIXSINK pSink, PPDMAUDIOPCMPROPS pPCMProps)
1369{
1370 AssertPtrReturnVoid(pSink);
1371 AssertPtrReturnVoid(pPCMProps);
1372
1373 int rc2 = RTCritSectEnter(&pSink->CritSect);
1374 if (RT_FAILURE(rc2))
1375 return;
1376
1377 memcpy(pPCMProps, &pSink->PCMProps, sizeof(PDMAUDIOPCMPROPS));
1378
1379 rc2 = RTCritSectLeave(&pSink->CritSect);
1380 AssertRC(rc2);
1381}
1382
1383/**
1384 * Sets the audio format of a mixer sink.
1385 *
1386 * @returns IPRT status code.
1387 * @param pSink Sink to set audio format for.
1388 * @param pPCMProps Audio format (PCM properties) to set.
1389 */
1390int AudioMixerSinkSetFormat(PAUDMIXSINK pSink, PPDMAUDIOPCMPROPS pPCMProps)
1391{
1392 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1393 AssertPtrReturn(pPCMProps, VERR_INVALID_POINTER);
1394 AssertReturn(DrvAudioHlpPCMPropsAreValid(pPCMProps), VERR_INVALID_PARAMETER);
1395
1396 int rc = RTCritSectEnter(&pSink->CritSect);
1397 if (RT_FAILURE(rc))
1398 return rc;
1399
1400 if (DrvAudioHlpPCMPropsAreEqual(&pSink->PCMProps, pPCMProps)) /* Bail out early if PCM properties are equal. */
1401 {
1402 rc = RTCritSectLeave(&pSink->CritSect);
1403 AssertRC(rc);
1404
1405 return rc;
1406 }
1407
1408 if (pSink->PCMProps.uHz)
1409 LogFlowFunc(("[%s] Old format: %RU8 bit, %RU8 channels, %RU32Hz\n",
1410 pSink->pszName, pSink->PCMProps.cBytes * 8, pSink->PCMProps.cChannels, pSink->PCMProps.uHz));
1411
1412 memcpy(&pSink->PCMProps, pPCMProps, sizeof(PDMAUDIOPCMPROPS));
1413
1414 LogFlowFunc(("[%s] New format %RU8 bit, %RU8 channels, %RU32Hz\n",
1415 pSink->pszName, pSink->PCMProps.cBytes * 8, pSink->PCMProps.cChannels, pSink->PCMProps.uHz));
1416
1417 /* Also update the sink's mixing buffer format. */
1418 AudioMixBufDestroy(&pSink->MixBuf);
1419 rc = AudioMixBufInit(&pSink->MixBuf, pSink->pszName, &pSink->PCMProps,
1420 DrvAudioHlpMilliToFrames(100 /* ms */, &pSink->PCMProps)); /** @todo Make this configurable? */
1421 if (RT_SUCCESS(rc))
1422 {
1423 PAUDMIXSTREAM pStream;
1424 RTListForEach(&pSink->lstStreams, pStream, AUDMIXSTREAM, Node)
1425 {
1426 /** @todo Invalidate mix buffers! */
1427 }
1428 }
1429
1430#ifdef VBOX_AUDIO_MIXER_DEBUG
1431 if (RT_SUCCESS(rc))
1432 {
1433 DrvAudioHlpFileClose(pSink->Dbg.pFile);
1434
1435 char szTemp[RTPATH_MAX];
1436 int rc2 = RTPathTemp(szTemp, sizeof(szTemp));
1437 if (RT_SUCCESS(rc2))
1438 {
1439 /** @todo Sanitize sink name. */
1440
1441 char szName[64];
1442 RTStrPrintf(szName, sizeof(szName), "MixerSink-%s", pSink->pszName);
1443
1444 char szFile[RTPATH_MAX + 1];
1445 rc2 = DrvAudioHlpFileNameGet(szFile, RT_ELEMENTS(szFile), szTemp, szName,
1446 0 /* Instance */, PDMAUDIOFILETYPE_WAV, PDMAUDIOFILENAME_FLAG_NONE);
1447 if (RT_SUCCESS(rc2))
1448 {
1449 rc2 = DrvAudioHlpFileCreate(PDMAUDIOFILETYPE_WAV, szFile, PDMAUDIOFILE_FLAG_NONE,
1450 &pSink->Dbg.pFile);
1451 if (RT_SUCCESS(rc2))
1452 rc2 = DrvAudioHlpFileOpen(pSink->Dbg.pFile, PDMAUDIOFILE_DEFAULT_OPEN_FLAGS, &pSink->PCMProps);
1453 }
1454 }
1455 }
1456#endif
1457
1458 int rc2 = RTCritSectLeave(&pSink->CritSect);
1459 AssertRC(rc2);
1460
1461 LogFlowFuncLeaveRC(rc);
1462 return rc;
1463}
1464
1465/**
1466 * Set the current recording source of an input mixer sink, internal version.
1467 *
1468 * @return IPRT status code.
1469 * @param pSink Input mixer sink to set recording source for.
1470 * @param pStream Mixer stream to set as current recording source. Must be an input stream.
1471 * Specify NULL to un-set the current recording source.
1472 */
1473static int audioMixerSinkSetRecSourceInternal(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream)
1474{
1475 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_INPUT, ("Specified sink is not an input sink\n"));
1476
1477 int rc;
1478
1479 if (pSink->In.pStreamRecSource) /* Disable old recording source, if any set. */
1480 {
1481 const PPDMIAUDIOCONNECTOR pConn = pSink->In.pStreamRecSource->pConn;
1482 AssertPtr(pConn);
1483 rc = pConn->pfnEnable(pConn, PDMAUDIODIR_IN, false /* Disable */);
1484 }
1485 else
1486 rc = VINF_SUCCESS;
1487
1488 if (RT_SUCCESS(rc))
1489 {
1490 if (pStream) /* Can be NULL if un-setting. */
1491 {
1492 AssertPtr(pStream->pStream);
1493 AssertMsg(pStream->pStream->enmDir == PDMAUDIODIR_IN, ("Specified stream is not an input stream\n"));
1494 }
1495
1496 pSink->In.pStreamRecSource = pStream;
1497
1498 if (pSink->In.pStreamRecSource)
1499 {
1500 const PPDMIAUDIOCONNECTOR pConn = pSink->In.pStreamRecSource->pConn;
1501 AssertPtr(pConn);
1502 rc = pConn->pfnEnable(pConn, PDMAUDIODIR_IN, true /* Enable */);
1503 }
1504 }
1505
1506 LogFunc(("[%s] Recording source is now '%s', rc=%Rrc\n",
1507 pSink->pszName, pSink->In.pStreamRecSource ? pSink->In.pStreamRecSource->pszName : "<None>", rc));
1508
1509 return rc;
1510}
1511
1512/**
1513 * Set the current recording source of an input mixer sink.
1514 *
1515 * @return IPRT status code.
1516 * @param pSink Input mixer sink to set recording source for.
1517 * @param pStream Mixer stream to set as current recording source. Must be an input stream.
1518 * Set to NULL to un-set the current recording source.
1519 */
1520int AudioMixerSinkSetRecordingSource(PAUDMIXSINK pSink, PAUDMIXSTREAM pStream)
1521{
1522 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1523
1524 int rc = RTCritSectEnter(&pSink->CritSect);
1525 if (RT_FAILURE(rc))
1526 return rc;
1527
1528 rc = audioMixerSinkSetRecSourceInternal(pSink, pStream);
1529
1530 int rc2 = RTCritSectLeave(&pSink->CritSect);
1531 AssertRC(rc2);
1532
1533 return rc;
1534}
1535
1536/**
1537 * Sets the volume of an individual sink.
1538 *
1539 * @returns IPRT status code.
1540 * @param pSink Sink to set volume for.
1541 * @param pVol Volume to set.
1542 */
1543int AudioMixerSinkSetVolume(PAUDMIXSINK pSink, PPDMAUDIOVOLUME pVol)
1544{
1545 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1546 AssertPtrReturn(pVol, VERR_INVALID_POINTER);
1547
1548 int rc = RTCritSectEnter(&pSink->CritSect);
1549 if (RT_FAILURE(rc))
1550 return rc;
1551
1552 memcpy(&pSink->Volume, pVol, sizeof(PDMAUDIOVOLUME));
1553
1554 LogFlowFunc(("[%s] fMuted=%RTbool, lVol=%RU8, rVol=%RU8\n",
1555 pSink->pszName, pSink->Volume.fMuted, pSink->Volume.uLeft, pSink->Volume.uRight));
1556
1557 LogRel2(("Mixer: Setting volume of sink '%s' to %RU8/%RU8 (%s)\n",
1558 pSink->pszName, pVol->uLeft, pVol->uRight, pVol->fMuted ? "Muted" : "Unmuted"));
1559
1560 AssertPtr(pSink->pParent);
1561 rc = audioMixerSinkUpdateVolume(pSink, &pSink->pParent->VolMaster);
1562
1563 int rc2 = RTCritSectLeave(&pSink->CritSect);
1564 AssertRC(rc2);
1565
1566 return rc;
1567}
1568
1569/**
1570 * Updates a mixer sink, internal version.
1571 *
1572 * @returns IPRT status code.
1573 * @param pSink Mixer sink to update.
1574 */
1575static int audioMixerSinkUpdateInternal(PAUDMIXSINK pSink)
1576{
1577 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1578
1579 int rc = VINF_SUCCESS;
1580
1581#ifdef LOG_ENABLED
1582 char *pszStatus = dbgAudioMixerSinkStatusToStr(pSink->fStatus);
1583 Log3Func(("[%s] fStatus=%s\n", pSink->pszName, pszStatus));
1584 RTStrFree(pszStatus);
1585#endif
1586
1587 /* Sink disabled? Take a shortcut. */
1588 if (!(pSink->fStatus & AUDMIXSINK_STS_RUNNING))
1589 return rc;
1590
1591 /* Input sink and no recording source set? Bail out early. */
1592 if ( pSink->enmDir == AUDMIXSINKDIR_INPUT
1593 && pSink->In.pStreamRecSource == NULL)
1594 return rc;
1595
1596 /* Number of disabled streams of this sink. */
1597 uint8_t cStreamsDisabled = pSink->cStreams;
1598
1599 /* Next, try to write (multiplex) as much audio data as possible to all connected mixer streams. */
1600 uint32_t cbToWriteToStreams = AudioMixBufUsedBytes(&pSink->MixBuf);
1601
1602 uint8_t arrChunkBuf[_1K]; /** @todo Hm ... some zero copy / shared buffers would be nice! */
1603 while (cbToWriteToStreams)
1604 {
1605 uint32_t cfChunk;
1606 rc = AudioMixBufAcquireReadBlock(&pSink->MixBuf, arrChunkBuf, RT_MIN(cbToWriteToStreams, sizeof(arrChunkBuf)), &cfChunk);
1607 if (RT_FAILURE(rc))
1608 break;
1609
1610 const uint32_t cbChunk = DrvAudioHlpFramesToBytes(cfChunk, &pSink->PCMProps);
1611 Assert(cbChunk <= sizeof(arrChunkBuf));
1612
1613 /* Multiplex the current chunk in a synchronized fashion to all connected streams. */
1614 uint32_t cbChunkWrittenMin = 0;
1615 rc = audioMixerSinkMultiplexSync(pSink, AUDMIXOP_COPY, arrChunkBuf, cbChunk, &cbChunkWrittenMin);
1616 if (RT_SUCCESS(rc))
1617 {
1618 PAUDMIXSTREAM pMixStream;
1619 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1620 {
1621 int rc2 = audioMixerSinkWriteToStream(pSink, pMixStream);
1622 AssertRC(rc2);
1623 }
1624 }
1625
1626 Log3Func(("[%s] cbChunk=%RU32, cbChunkWrittenMin=%RU32\n", pSink->pszName, cbChunk, cbChunkWrittenMin));
1627
1628 AudioMixBufReleaseReadBlock(&pSink->MixBuf, AUDIOMIXBUF_B2F(&pSink->MixBuf, cbChunkWrittenMin));
1629
1630 if ( RT_FAILURE(rc)
1631 || cbChunkWrittenMin == 0)
1632 break;
1633
1634 Assert(cbToWriteToStreams >= cbChunkWrittenMin);
1635 cbToWriteToStreams -= cbChunkWrittenMin;
1636 }
1637
1638 if ( !(pSink->fStatus & AUDMIXSINK_STS_DIRTY)
1639 && AudioMixBufUsed(&pSink->MixBuf)) /* Still audio output data left? Consider the sink as being "dirty" then. */
1640 {
1641 /* Set dirty bit. */
1642 pSink->fStatus |= AUDMIXSINK_STS_DIRTY;
1643 }
1644
1645 PAUDMIXSTREAM pMixStream, pMixStreamNext;
1646 RTListForEachSafe(&pSink->lstStreams, pMixStream, pMixStreamNext, AUDMIXSTREAM, Node)
1647 {
1648 /* Input sink and not the recording source? Skip. */
1649 if ( pSink->enmDir == AUDMIXSINKDIR_INPUT
1650 && pSink->In.pStreamRecSource != pMixStream)
1651 continue;
1652
1653 PPDMAUDIOSTREAM pStream = pMixStream->pStream;
1654 AssertPtr(pStream);
1655
1656 PPDMIAUDIOCONNECTOR pConn = pMixStream->pConn;
1657 AssertPtr(pConn);
1658
1659 uint32_t cfProc = 0;
1660
1661 if (!DrvAudioHlpStreamStatusIsReady(pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream)))
1662 continue;
1663
1664 int rc2 = pConn->pfnStreamIterate(pConn, pStream);
1665 if (RT_SUCCESS(rc2))
1666 {
1667 if (pSink->enmDir == AUDMIXSINKDIR_INPUT)
1668 {
1669 rc2 = pConn->pfnStreamCapture(pConn, pStream, &cfProc);
1670 if (RT_FAILURE(rc2))
1671 {
1672 LogFunc(("%s: Failed capturing stream '%s', rc=%Rrc\n", pSink->pszName, pStream->szName, rc2));
1673 continue;
1674 }
1675
1676 if (cfProc)
1677 pSink->fStatus |= AUDMIXSINK_STS_DIRTY;
1678 }
1679 else if (pSink->enmDir == AUDMIXSINKDIR_OUTPUT)
1680 {
1681 rc2 = pConn->pfnStreamPlay(pConn, pStream, &cfProc);
1682 if (RT_FAILURE(rc2))
1683 {
1684 LogFunc(("%s: Failed playing stream '%s', rc=%Rrc\n", pSink->pszName, pStream->szName, rc2));
1685 continue;
1686 }
1687 }
1688 else
1689 {
1690 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
1691 continue;
1692 }
1693 }
1694
1695 PDMAUDIOSTREAMSTS strmSts = pConn->pfnStreamGetStatus(pConn, pStream);
1696
1697 /* Is the stream enabled or in pending disable state?
1698 * Don't consider this stream as being disabled then. */
1699 if ( (strmSts & PDMAUDIOSTREAMSTS_FLAG_ENABLED)
1700 || (strmSts & PDMAUDIOSTREAMSTS_FLAG_PENDING_DISABLE))
1701 {
1702 cStreamsDisabled--;
1703 }
1704
1705 Log3Func(("\t%s: cPlayed/cCaptured=%RU32, rc2=%Rrc\n", pStream->szName, cfProc, rc2));
1706 }
1707
1708 Log3Func(("[%s] fPendingDisable=%RTbool, %RU8/%RU8 streams disabled\n",
1709 pSink->pszName, RT_BOOL(pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE), cStreamsDisabled, pSink->cStreams));
1710
1711 /* Update last updated timestamp. */
1712 pSink->tsLastUpdatedMs = RTTimeMilliTS();
1713
1714 /* All streams disabled and the sink is in pending disable mode? */
1715 if ( cStreamsDisabled == pSink->cStreams
1716 && (pSink->fStatus & AUDMIXSINK_STS_PENDING_DISABLE))
1717 {
1718 audioMixerSinkReset(pSink);
1719 }
1720
1721 return rc;
1722}
1723
1724/**
1725 * Updates (invalidates) a mixer sink.
1726 *
1727 * @returns IPRT status code.
1728 * @param pSink Mixer sink to update.
1729 */
1730int AudioMixerSinkUpdate(PAUDMIXSINK pSink)
1731{
1732 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1733
1734 int rc = RTCritSectEnter(&pSink->CritSect);
1735 if (RT_FAILURE(rc))
1736 return rc;
1737
1738 rc = audioMixerSinkUpdateInternal(pSink);
1739
1740 int rc2 = RTCritSectLeave(&pSink->CritSect);
1741 AssertRC(rc2);
1742
1743 return rc;
1744}
1745
1746/**
1747 * Updates the (master) volume of a mixer sink.
1748 *
1749 * @returns IPRT status code.
1750 * @param pSink Mixer sink to update volume for.
1751 * @param pVolMaster Master volume to set.
1752 */
1753static int audioMixerSinkUpdateVolume(PAUDMIXSINK pSink, const PPDMAUDIOVOLUME pVolMaster)
1754{
1755 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1756 AssertPtrReturn(pVolMaster, VERR_INVALID_POINTER);
1757
1758 LogFlowFunc(("[%s] Master fMuted=%RTbool, lVol=%RU32, rVol=%RU32\n",
1759 pSink->pszName, pVolMaster->fMuted, pVolMaster->uLeft, pVolMaster->uRight));
1760 LogFlowFunc(("[%s] fMuted=%RTbool, lVol=%RU32, rVol=%RU32 ",
1761 pSink->pszName, pSink->Volume.fMuted, pSink->Volume.uLeft, pSink->Volume.uRight));
1762
1763 /** @todo Very crude implementation for now -- needs more work! */
1764
1765 pSink->VolumeCombined.fMuted = pVolMaster->fMuted || pSink->Volume.fMuted;
1766
1767 pSink->VolumeCombined.uLeft = ( (pSink->Volume.uLeft ? pSink->Volume.uLeft : 1)
1768 * (pVolMaster->uLeft ? pVolMaster->uLeft : 1)) / PDMAUDIO_VOLUME_MAX;
1769
1770 pSink->VolumeCombined.uRight = ( (pSink->Volume.uRight ? pSink->Volume.uRight : 1)
1771 * (pVolMaster->uRight ? pVolMaster->uRight : 1)) / PDMAUDIO_VOLUME_MAX;
1772
1773 LogFlow(("-> fMuted=%RTbool, lVol=%RU32, rVol=%RU32\n",
1774 pSink->VolumeCombined.fMuted, pSink->VolumeCombined.uLeft, pSink->VolumeCombined.uRight));
1775
1776 /* Propagate new sink volume to all streams in the sink. */
1777 PAUDMIXSTREAM pMixStream;
1778 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1779 {
1780 int rc2 = pMixStream->pConn->pfnStreamSetVolume(pMixStream->pConn, pMixStream->pStream, &pSink->VolumeCombined);
1781 AssertRC(rc2);
1782 }
1783
1784 return VINF_SUCCESS;
1785}
1786
1787/**
1788 * Writes (buffered) output data of a sink's stream to the bound audio connector stream.
1789 *
1790 * @returns IPRT status code.
1791 * @param pSink Sink of stream that contains the mixer stream.
1792 * @param pMixStream Mixer stream to write output data for.
1793 */
1794static int audioMixerSinkWriteToStream(PAUDMIXSINK pSink, PAUDMIXSTREAM pMixStream)
1795{
1796 if (!pMixStream->pCircBuf)
1797 return VINF_SUCCESS;
1798
1799 return audioMixerSinkWriteToStreamEx(pSink, pMixStream, (uint32_t)RTCircBufUsed(pMixStream->pCircBuf), NULL /* pcbWritten */);
1800}
1801
1802/**
1803 * Writes (buffered) output data of a sink's stream to the bound audio connector stream, extended version.
1804 *
1805 * @returns IPRT status code.
1806 * @param pSink Sink of stream that contains the mixer stream.
1807 * @param pMixStream Mixer stream to write output data for.
1808 * @param cbToWrite Size (in bytes) to write.
1809 * @param pcbWritten Size (in bytes) written on success. Optional.
1810 */
1811static int audioMixerSinkWriteToStreamEx(PAUDMIXSINK pSink, PAUDMIXSTREAM pMixStream, uint32_t cbToWrite, uint32_t *pcbWritten)
1812{
1813 /* pcbWritten is optional. */
1814
1815 if ( !cbToWrite
1816 || !DrvAudioHlpStreamStatusCanWrite(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream)))
1817 {
1818 if (pcbWritten)
1819 *pcbWritten = 0;
1820
1821 return VINF_SUCCESS;
1822 }
1823
1824 PRTCIRCBUF pCircBuf = pMixStream->pCircBuf;
1825
1826 const uint32_t cbWritableStream = pMixStream->pConn->pfnStreamGetWritable(pMixStream->pConn, pMixStream->pStream);
1827 cbToWrite = RT_MIN(cbToWrite, RT_MIN((uint32_t)RTCircBufUsed(pCircBuf), cbWritableStream));
1828
1829 Log3Func(("[%s] cbWritableStream=%RU32, cbToWrite=%RU32\n",
1830 pMixStream->pszName, cbWritableStream, cbToWrite));
1831
1832 uint32_t cbWritten = 0;
1833
1834 int rc = VINF_SUCCESS;
1835
1836 while (cbToWrite)
1837 {
1838 void *pvChunk;
1839 size_t cbChunk;
1840 RTCircBufAcquireReadBlock(pCircBuf, cbToWrite, &pvChunk, &cbChunk);
1841
1842 Log3Func(("[%s] cbChunk=%RU32\n", pMixStream->pszName, cbChunk));
1843
1844 uint32_t cbChunkWritten = 0;
1845 if (cbChunk)
1846 {
1847 rc = pMixStream->pConn->pfnStreamWrite(pMixStream->pConn, pMixStream->pStream, pvChunk, (uint32_t)cbChunk,
1848 &cbChunkWritten);
1849 if (RT_FAILURE(rc))
1850 {
1851 if (rc == VERR_BUFFER_OVERFLOW)
1852 {
1853 LogRel2(("Mixer: Buffer overrun for mixer stream '%s' (sink '%s')\n", pMixStream->pszName, pSink->pszName));
1854 break;
1855 }
1856 else if (rc == VERR_AUDIO_STREAM_NOT_READY)
1857 {
1858 /* Stream is not enabled, just skip. */
1859 rc = VINF_SUCCESS;
1860 }
1861 else
1862 LogRel2(("Mixer: Writing to mixer stream '%s' (sink '%s') failed, rc=%Rrc\n",
1863 pMixStream->pszName, pSink->pszName, rc));
1864
1865 if (RT_FAILURE(rc))
1866 LogFunc(("[%s] Failed writing to stream '%s': %Rrc\n", pSink->pszName, pMixStream->pszName, rc));
1867 }
1868 }
1869
1870 RTCircBufReleaseReadBlock(pCircBuf, cbChunkWritten);
1871
1872 if ( RT_FAILURE(rc)
1873 || !cbChunkWritten)
1874 break;
1875
1876 Assert(cbToWrite >= cbChunkWritten);
1877 cbToWrite -= (uint32_t)cbChunkWritten;
1878
1879 cbWritten += (uint32_t)cbChunkWritten;
1880 }
1881
1882 Log3Func(("[%s] cbWritten=%RU32\n", pMixStream->pszName, cbWritten));
1883
1884 if (pcbWritten)
1885 *pcbWritten = cbWritten;
1886
1887#ifdef DEBUG_andy
1888 AssertRC(rc);
1889#endif
1890
1891 return rc;
1892}
1893
1894/**
1895 * Multiplexes audio output data to all connected mixer streams in a synchronized fashion, e.g.
1896 * only multiplex as much data as all streams can handle at this time.
1897 *
1898 * @returns IPRT status code.
1899 * @param pSink Sink to write audio output to.
1900 * @param enmOp What mixing operation to use. Currently not implemented.
1901 * @param pvBuf Pointer to audio data to write.
1902 * @param cbBuf Size (in bytes) of audio data to write.
1903 * @param pcbWrittenMin Returns minimum size (in bytes) successfully written to all mixer streams. Optional.
1904 */
1905static int audioMixerSinkMultiplexSync(PAUDMIXSINK pSink, AUDMIXOP enmOp, const void *pvBuf, uint32_t cbBuf,
1906 uint32_t *pcbWrittenMin)
1907{
1908 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1909 RT_NOREF(enmOp);
1910
1911 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_OUTPUT,
1912 ("%s: Can't multiplex to a sink which is not an output sink\n", pSink->pszName));
1913
1914 int rc = VINF_SUCCESS;
1915
1916 uint32_t cbToWriteMin = UINT32_MAX;
1917
1918 Log3Func(("[%s] cbBuf=%RU32\n", pSink->pszName, cbBuf));
1919
1920 PAUDMIXSTREAM pMixStream;
1921 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1922 {
1923 if (!DrvAudioHlpStreamStatusCanWrite(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream)))
1924 continue;
1925
1926 cbToWriteMin = RT_MIN(cbBuf, RT_MIN(cbToWriteMin, (uint32_t)RTCircBufFree(pMixStream->pCircBuf)));
1927 }
1928
1929 if (cbToWriteMin == UINT32_MAX) /* No space at all? */
1930 cbToWriteMin = 0;
1931
1932 if (cbToWriteMin)
1933 {
1934 RTListForEach(&pSink->lstStreams, pMixStream, AUDMIXSTREAM, Node)
1935 {
1936 PRTCIRCBUF pCircBuf = pMixStream->pCircBuf;
1937 void *pvChunk;
1938 size_t cbChunk;
1939
1940 uint32_t cbWrittenBuf = 0;
1941 uint32_t cbToWriteBuf = cbToWriteMin;
1942
1943 while (cbToWriteBuf)
1944 {
1945 RTCircBufAcquireWriteBlock(pCircBuf, cbToWriteBuf, &pvChunk, &cbChunk);
1946
1947 if (cbChunk)
1948 memcpy(pvChunk, (uint8_t *)pvBuf + cbWrittenBuf, cbChunk);
1949
1950 RTCircBufReleaseWriteBlock(pCircBuf, cbChunk);
1951
1952 cbWrittenBuf += (uint32_t)cbChunk;
1953 Assert(cbWrittenBuf <= cbBuf);
1954
1955 Assert(cbToWriteBuf >= cbChunk);
1956 cbToWriteBuf -= (uint32_t)cbChunk;
1957 }
1958
1959 if (cbWrittenBuf) /* Update the mixer stream's last written time stamp. */
1960 pMixStream->tsLastReadWrittenNs = RTTimeNanoTS();
1961
1962 Log3Func(("[%s] Mixer stream '%s' -> cbWrittenBuf=%RU32\n", pSink->pszName, pMixStream->pszName, cbWrittenBuf));
1963 }
1964 }
1965
1966 Log3Func(("[%s] cbBuf=%RU32, cbToWriteMin=%RU32\n", pSink->pszName, cbBuf, cbToWriteMin));
1967
1968 if (pcbWrittenMin)
1969 *pcbWrittenMin = cbToWriteMin;
1970
1971 return rc;
1972}
1973
1974/**
1975 * Writes data to a mixer sink.
1976 *
1977 * @returns IPRT status code.
1978 * @param pSink Sink to write data to.
1979 * @param enmOp Mixer operation to use when writing data to the sink.
1980 * @param pvBuf Buffer containing the audio data to write.
1981 * @param cbBuf Size (in bytes) of the buffer containing the audio data.
1982 * @param pcbWritten Number of bytes written. Optional.
1983 */
1984int AudioMixerSinkWrite(PAUDMIXSINK pSink, AUDMIXOP enmOp, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
1985{
1986 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1987 RT_NOREF(enmOp);
1988 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1989 AssertReturn (cbBuf, VERR_INVALID_PARAMETER);
1990 /* pcbWritten is optional. */
1991
1992 int rc = RTCritSectEnter(&pSink->CritSect);
1993 if (RT_FAILURE(rc))
1994 return rc;
1995
1996 AssertMsg(pSink->fStatus & AUDMIXSINK_STS_RUNNING,
1997 ("%s: Can't write to a sink which is not running (anymore) (status 0x%x)\n", pSink->pszName, pSink->fStatus));
1998 AssertMsg(pSink->enmDir == AUDMIXSINKDIR_OUTPUT,
1999 ("%s: Can't write to a sink which is not an output sink\n", pSink->pszName));
2000
2001 Assert(cbBuf <= AudioMixBufFreeBytes(&pSink->MixBuf));
2002
2003 uint32_t cbWritten = 0;
2004 uint32_t cbToWrite = cbBuf;
2005 while (cbToWrite)
2006 {
2007 /* First, write the data to the mixer sink's own mixing buffer.
2008 * Here the audio data can be transformed into the mixer sink's format. */
2009 uint32_t cfWritten = 0;
2010 rc = AudioMixBufWriteCirc(&pSink->MixBuf, (uint8_t *)pvBuf + cbWritten, cbToWrite, &cfWritten);
2011 if (RT_FAILURE(rc))
2012 break;
2013
2014 const uint32_t cbWrittenChunk = DrvAudioHlpFramesToBytes(cfWritten, &pSink->PCMProps);
2015
2016 Assert(cbToWrite >= cbWrittenChunk);
2017 cbToWrite -= cbWrittenChunk;
2018 cbWritten += cbWrittenChunk;
2019 }
2020
2021 Assert(cbWritten == cbBuf);
2022
2023 /* Update the sink's last written time stamp. */
2024 pSink->tsLastReadWrittenNs = RTTimeNanoTS();
2025
2026 if (pcbWritten)
2027 *pcbWritten = cbWritten;
2028
2029 int rc2 = RTCritSectLeave(&pSink->CritSect);
2030 AssertRC(rc2);
2031
2032 return rc;
2033}
2034
2035/*********************************************************************************************************************************
2036 * Mixer Stream implementation.
2037 ********************************************************************************************************************************/
2038
2039/**
2040 * Controls a mixer stream, internal version.
2041 *
2042 * @returns IPRT status code.
2043 * @param pMixStream Mixer stream to control.
2044 * @param enmCmd Mixer stream command to use.
2045 * @param fCtl Additional control flags. Pass 0.
2046 */
2047int audioMixerStreamCtlInternal(PAUDMIXSTREAM pMixStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl)
2048{
2049 AssertPtr(pMixStream->pConn);
2050 AssertPtr(pMixStream->pStream);
2051
2052 RT_NOREF(fCtl);
2053
2054 int rc = pMixStream->pConn->pfnStreamControl(pMixStream->pConn, pMixStream->pStream, enmCmd);
2055
2056 LogFlowFunc(("[%s] enmCmd=%ld, rc=%Rrc\n", pMixStream->pszName, enmCmd, rc));
2057
2058 return rc;
2059}
2060
2061/**
2062 * Controls a mixer stream.
2063 *
2064 * @returns IPRT status code.
2065 * @param pMixStream Mixer stream to control.
2066 * @param enmCmd Mixer stream command to use.
2067 * @param fCtl Additional control flags. Pass 0.
2068 */
2069int AudioMixerStreamCtl(PAUDMIXSTREAM pMixStream, PDMAUDIOSTREAMCMD enmCmd, uint32_t fCtl)
2070{
2071 RT_NOREF(fCtl);
2072 AssertPtrReturn(pMixStream, VERR_INVALID_POINTER);
2073 /** @todo Validate fCtl. */
2074
2075 int rc = RTCritSectEnter(&pMixStream->CritSect);
2076 if (RT_FAILURE(rc))
2077 return rc;
2078
2079 rc = audioMixerStreamCtlInternal(pMixStream, enmCmd, fCtl);
2080
2081 int rc2 = RTCritSectLeave(&pMixStream->CritSect);
2082 if (RT_SUCCESS(rc))
2083 rc = rc2;
2084
2085 return rc;
2086}
2087
2088/**
2089 * Destroys a mixer stream, internal version.
2090 *
2091 * @param pMixStream Mixer stream to destroy.
2092 */
2093static void audioMixerStreamDestroyInternal(PAUDMIXSTREAM pMixStream)
2094{
2095 AssertPtrReturnVoid(pMixStream);
2096
2097 LogFunc(("%s\n", pMixStream->pszName));
2098
2099 if (pMixStream->pConn) /* Stream has a connector interface present? */
2100 {
2101 if (pMixStream->pStream)
2102 {
2103 pMixStream->pConn->pfnStreamRelease(pMixStream->pConn, pMixStream->pStream);
2104 pMixStream->pConn->pfnStreamDestroy(pMixStream->pConn, pMixStream->pStream);
2105
2106 pMixStream->pStream = NULL;
2107 }
2108
2109 pMixStream->pConn = NULL;
2110 }
2111
2112 if (pMixStream->pszName)
2113 {
2114 RTStrFree(pMixStream->pszName);
2115 pMixStream->pszName = NULL;
2116 }
2117
2118 if (pMixStream->pCircBuf)
2119 {
2120 RTCircBufDestroy(pMixStream->pCircBuf);
2121 pMixStream->pCircBuf = NULL;
2122 }
2123
2124 int rc2 = RTCritSectDelete(&pMixStream->CritSect);
2125 AssertRC(rc2);
2126
2127 RTMemFree(pMixStream);
2128 pMixStream = NULL;
2129}
2130
2131/**
2132 * Destroys a mixer stream.
2133 *
2134 * @param pMixStream Mixer stream to destroy.
2135 */
2136void AudioMixerStreamDestroy(PAUDMIXSTREAM pMixStream)
2137{
2138 if (!pMixStream)
2139 return;
2140
2141 int rc2 = RTCritSectEnter(&pMixStream->CritSect);
2142 AssertRC(rc2);
2143
2144 LogFunc(("%s\n", pMixStream->pszName));
2145
2146 if (pMixStream->pSink) /* Is the stream part of a sink? */
2147 {
2148 /* Save sink pointer, as after audioMixerSinkRemoveStreamInternal() the
2149 * pointer will be gone from the stream. */
2150 PAUDMIXSINK pSink = pMixStream->pSink;
2151
2152 rc2 = audioMixerSinkRemoveStreamInternal(pSink, pMixStream);
2153 if (RT_SUCCESS(rc2))
2154 {
2155 Assert(pSink->cStreams);
2156 pSink->cStreams--;
2157 }
2158 }
2159 else
2160 rc2 = VINF_SUCCESS;
2161
2162 int rc3 = RTCritSectLeave(&pMixStream->CritSect);
2163 AssertRC(rc3);
2164
2165 if (RT_SUCCESS(rc2))
2166 {
2167 audioMixerStreamDestroyInternal(pMixStream);
2168 pMixStream = NULL;
2169 }
2170
2171 LogFlowFunc(("Returning %Rrc\n", rc2));
2172}
2173
2174/**
2175 * Returns whether a mixer stream currently is active (playing/recording) or not.
2176 *
2177 * @returns @c true if playing/recording, @c false if not.
2178 * @param pMixStream Mixer stream to return status for.
2179 */
2180bool AudioMixerStreamIsActive(PAUDMIXSTREAM pMixStream)
2181{
2182 int rc2 = RTCritSectEnter(&pMixStream->CritSect);
2183 if (RT_FAILURE(rc2))
2184 return false;
2185
2186 AssertPtr(pMixStream->pConn);
2187 AssertPtr(pMixStream->pStream);
2188
2189 bool fIsActive;
2190
2191 if ( pMixStream->pConn
2192 && pMixStream->pStream
2193 && RT_BOOL(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream) & PDMAUDIOSTREAMSTS_FLAG_ENABLED))
2194 {
2195 fIsActive = true;
2196 }
2197 else
2198 fIsActive = false;
2199
2200 rc2 = RTCritSectLeave(&pMixStream->CritSect);
2201 AssertRC(rc2);
2202
2203 return fIsActive;
2204}
2205
2206/**
2207 * Returns whether a mixer stream is valid (e.g. initialized and in a working state) or not.
2208 *
2209 * @returns @c true if valid, @c false if not.
2210 * @param pMixStream Mixer stream to return status for.
2211 */
2212bool AudioMixerStreamIsValid(PAUDMIXSTREAM pMixStream)
2213{
2214 if (!pMixStream)
2215 return false;
2216
2217 int rc2 = RTCritSectEnter(&pMixStream->CritSect);
2218 if (RT_FAILURE(rc2))
2219 return false;
2220
2221 bool fIsValid;
2222
2223 if ( pMixStream->pConn
2224 && pMixStream->pStream
2225 && RT_BOOL(pMixStream->pConn->pfnStreamGetStatus(pMixStream->pConn, pMixStream->pStream) & PDMAUDIOSTREAMSTS_FLAG_INITIALIZED))
2226 {
2227 fIsValid = true;
2228 }
2229 else
2230 fIsValid = false;
2231
2232 rc2 = RTCritSectLeave(&pMixStream->CritSect);
2233 AssertRC(rc2);
2234
2235 return fIsValid;
2236}
2237
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette