VirtualBox

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

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

Build fix.

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