VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostAudioAlsa.cpp@ 92057

Last change on this file since 92057 was 92057, checked in by vboxsync, 4 years ago

Audio/Validation Kit: Logging tweaks. ​bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 60.4 KB
Line 
1/* $Id: DrvHostAudioAlsa.cpp 92057 2021-10-26 07:02:32Z vboxsync $ */
2/** @file
3 * Host audio driver - Advanced Linux Sound Architecture (ALSA).
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 * --------------------------------------------------------------------
17 *
18 * This code is based on: alsaaudio.c
19 *
20 * QEMU ALSA audio driver
21 *
22 * Copyright (c) 2005 Vassili Karpov (malc)
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
37 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40 * THE SOFTWARE.
41 */
42
43
44/*********************************************************************************************************************************
45* Header Files *
46*********************************************************************************************************************************/
47#define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
48#include <VBox/log.h>
49#include <iprt/alloc.h>
50#include <iprt/uuid.h> /* For PDMIBASE_2_PDMDRV. */
51#include <VBox/vmm/pdmaudioifs.h>
52#include <VBox/vmm/pdmaudioinline.h>
53#include <VBox/vmm/pdmaudiohostenuminline.h>
54
55#include "DrvHostAudioAlsaStubsMangling.h"
56#include <alsa/asoundlib.h>
57#include <alsa/control.h> /* For device enumeration. */
58#include <alsa/version.h>
59#include "DrvHostAudioAlsaStubs.h"
60
61#include "VBoxDD.h"
62
63
64/*********************************************************************************************************************************
65* Defined Constants And Macros *
66*********************************************************************************************************************************/
67/** Maximum number of tries to recover a broken pipe. */
68#define ALSA_RECOVERY_TRIES_MAX 5
69
70
71/*********************************************************************************************************************************
72* Structures *
73*********************************************************************************************************************************/
74/**
75 * ALSA host audio specific stream data.
76 */
77typedef struct DRVHSTAUDALSASTREAM
78{
79 /** Common part. */
80 PDMAUDIOBACKENDSTREAM Core;
81
82 /** Handle to the ALSA PCM stream. */
83 snd_pcm_t *hPCM;
84 /** Internal stream offset (for debugging). */
85 uint64_t offInternal;
86
87 /** The stream's acquired configuration. */
88 PDMAUDIOSTREAMCFG Cfg;
89} DRVHSTAUDALSASTREAM;
90/** Pointer to the ALSA host audio specific stream data. */
91typedef DRVHSTAUDALSASTREAM *PDRVHSTAUDALSASTREAM;
92
93
94/**
95 * Host Alsa audio driver instance data.
96 * @implements PDMIAUDIOCONNECTOR
97 */
98typedef struct DRVHSTAUDALSA
99{
100 /** Pointer to the driver instance structure. */
101 PPDMDRVINS pDrvIns;
102 /** Pointer to host audio interface. */
103 PDMIHOSTAUDIO IHostAudio;
104 /** Error count for not flooding the release log.
105 * UINT32_MAX for unlimited logging. */
106 uint32_t cLogErrors;
107
108 /** Critical section protecting the default device strings. */
109 RTCRITSECT CritSect;
110 /** Default input device name. */
111 char szInputDev[256];
112 /** Default output device name. */
113 char szOutputDev[256];
114 /** Upwards notification interface. */
115 PPDMIHOSTAUDIOPORT pIHostAudioPort;
116} DRVHSTAUDALSA;
117/** Pointer to the instance data of an ALSA host audio driver. */
118typedef DRVHSTAUDALSA *PDRVHSTAUDALSA;
119
120
121
122/**
123 * Closes an ALSA stream
124 *
125 * @returns VBox status code.
126 * @param phPCM Pointer to the ALSA stream handle to close. Will be set to
127 * NULL.
128 */
129static int drvHstAudAlsaStreamClose(snd_pcm_t **phPCM)
130{
131 if (!phPCM || !*phPCM)
132 return VINF_SUCCESS;
133
134 LogRelFlowFuncEnter();
135
136 int rc;
137 int rc2 = snd_pcm_close(*phPCM);
138 if (rc2 == 0)
139 {
140 *phPCM = NULL;
141 rc = VINF_SUCCESS;
142 }
143 else
144 {
145 rc = RTErrConvertFromErrno(-rc2);
146 LogRel(("ALSA: Closing PCM descriptor failed: %s (%d, %Rrc)\n", snd_strerror(rc2), rc2, rc));
147 }
148
149 LogRelFlowFuncLeaveRC(rc);
150 return rc;
151}
152
153
154#ifdef DEBUG
155static void drvHstAudAlsaDbgErrorHandler(const char *file, int line, const char *function,
156 int err, const char *fmt, ...)
157{
158 /** @todo Implement me! */
159 RT_NOREF(file, line, function, err, fmt);
160}
161#endif
162
163
164/**
165 * Tries to recover an ALSA stream.
166 *
167 * @returns VBox status code.
168 * @param hPCM ALSA stream handle.
169 */
170static int drvHstAudAlsaStreamRecover(snd_pcm_t *hPCM)
171{
172 AssertPtrReturn(hPCM, VERR_INVALID_POINTER);
173
174 int rc = snd_pcm_prepare(hPCM);
175 if (rc >= 0)
176 {
177 LogFlowFunc(("Successfully recovered %p.\n", hPCM));
178 return VINF_SUCCESS;
179 }
180 LogFunc(("Failed to recover stream %p: %s (%d)\n", hPCM, snd_strerror(rc), rc));
181 return RTErrConvertFromErrno(-rc);
182}
183
184
185/**
186 * Resumes an ALSA stream.
187 *
188 * Used by drvHstAudAlsaHA_StreamPlay() and drvHstAudAlsaHA_StreamCapture().
189 *
190 * @returns VBox status code.
191 * @param hPCM ALSA stream to resume.
192 */
193static int drvHstAudAlsaStreamResume(snd_pcm_t *hPCM)
194{
195 AssertPtrReturn(hPCM, VERR_INVALID_POINTER);
196
197 int rc = snd_pcm_resume(hPCM);
198 if (rc >= 0)
199 {
200 LogFlowFunc(("Successfuly resumed %p.\n", hPCM));
201 return VINF_SUCCESS;
202 }
203 LogFunc(("Failed to resume stream %p: %s (%d)\n", hPCM, snd_strerror(rc), rc));
204 return RTErrConvertFromErrno(-rc);
205}
206
207
208/**
209 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
210 */
211static DECLCALLBACK(int) drvHstAudAlsaHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
212{
213 RT_NOREF(pInterface);
214 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
215
216 /*
217 * Fill in the config structure.
218 */
219 RTStrCopy(pBackendCfg->szName, sizeof(pBackendCfg->szName), "ALSA");
220 pBackendCfg->cbStream = sizeof(DRVHSTAUDALSASTREAM);
221 pBackendCfg->fFlags = 0;
222 /* ALSA allows exactly one input and one output used at a time for the selected device(s). */
223 pBackendCfg->cMaxStreamsIn = 1;
224 pBackendCfg->cMaxStreamsOut = 1;
225
226 return VINF_SUCCESS;
227}
228
229
230/**
231 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetDevices}
232 */
233static DECLCALLBACK(int) drvHstAudAlsaHA_GetDevices(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHOSTENUM pDeviceEnum)
234{
235 RT_NOREF(pInterface);
236 PDMAudioHostEnumInit(pDeviceEnum);
237
238 char **papszHints = NULL;
239 int rc = snd_device_name_hint(-1 /* All cards */, "pcm", (void***)&papszHints);
240 if (rc == 0)
241 {
242 rc = VINF_SUCCESS;
243 for (size_t iHint = 0; papszHints[iHint] != NULL && RT_SUCCESS(rc); iHint++)
244 {
245 /*
246 * Retrieve the available info:
247 */
248 const char * const pszHint = papszHints[iHint];
249 char * const pszDev = snd_device_name_get_hint(pszHint, "NAME");
250 char * const pszInOutId = snd_device_name_get_hint(pszHint, "IOID");
251 char * const pszDesc = snd_device_name_get_hint(pszHint, "DESC");
252
253 if (pszDev && RTStrICmpAscii(pszDev, "null") != 0)
254 {
255 /* Detect and log presence of pulse audio plugin. */
256 if (RTStrIStr("pulse", pszDev) != NULL)
257 LogRel(("ALSA: The ALSAAudio plugin for pulse audio is being used (%s).\n", pszDev));
258
259 /*
260 * Add an entry to the enumeration result.
261 * We engage in some trickery here to deal with device names that
262 * are more than 63 characters long.
263 */
264 size_t const cbId = pszDev ? strlen(pszDev) + 1 : 1;
265 size_t const cbName = pszDesc ? strlen(pszDesc) + 2 + 1 : cbId;
266 PPDMAUDIOHOSTDEV pDev = PDMAudioHostDevAlloc(sizeof(*pDev), cbName, cbId);
267 if (pDev)
268 {
269 RTStrCopy(pDev->pszId, cbId, pszDev);
270 if (pDev->pszId)
271 {
272 pDev->fFlags = PDMAUDIOHOSTDEV_F_NONE;
273 pDev->enmType = PDMAUDIODEVICETYPE_UNKNOWN;
274
275 if (pszInOutId == NULL)
276 {
277 pDev->enmUsage = PDMAUDIODIR_DUPLEX;
278 pDev->cMaxInputChannels = 2;
279 pDev->cMaxOutputChannels = 2;
280 }
281 else if (RTStrICmpAscii(pszInOutId, "Input") == 0)
282 {
283 pDev->enmUsage = PDMAUDIODIR_IN;
284 pDev->cMaxInputChannels = 2;
285 pDev->cMaxOutputChannels = 0;
286 }
287 else
288 {
289 AssertMsg(RTStrICmpAscii(pszInOutId, "Output") == 0, ("%s (%s)\n", pszInOutId, pszHint));
290 pDev->enmUsage = PDMAUDIODIR_OUT;
291 pDev->cMaxInputChannels = 0;
292 pDev->cMaxOutputChannels = 2;
293 }
294
295 if (pszDesc && *pszDesc)
296 {
297 char *pszDesc2 = strchr(pszDesc, '\n');
298 if (!pszDesc2)
299 RTStrCopy(pDev->pszName, cbName, pszDesc);
300 else
301 {
302 *pszDesc2++ = '\0';
303 char *psz;
304 while ((psz = strchr(pszDesc2, '\n')) != NULL)
305 *psz = ' ';
306 RTStrPrintf(pDev->pszName, cbName, "%s (%s)", pszDesc2, pszDesc);
307 }
308 }
309 else
310 RTStrCopy(pDev->pszName, cbName, pszDev);
311
312 PDMAudioHostEnumAppend(pDeviceEnum, pDev);
313
314 LogRel2(("ALSA: Device #%u: '%s' enmDir=%s: %s\n", iHint, pszDev,
315 PDMAudioDirGetName(pDev->enmUsage), pszDesc));
316 }
317 else
318 {
319 PDMAudioHostDevFree(pDev);
320 rc = VERR_NO_STR_MEMORY;
321 }
322 }
323 else
324 rc = VERR_NO_MEMORY;
325 }
326
327 /*
328 * Clean up.
329 */
330 if (pszInOutId)
331 free(pszInOutId);
332 if (pszDesc)
333 free(pszDesc);
334 if (pszDev)
335 free(pszDev);
336 }
337
338 snd_device_name_free_hint((void **)papszHints);
339
340 if (RT_FAILURE(rc))
341 {
342 PDMAudioHostEnumDelete(pDeviceEnum);
343 PDMAudioHostEnumInit(pDeviceEnum);
344 }
345 }
346 else
347 {
348 int rc2 = RTErrConvertFromErrno(-rc);
349 LogRel2(("ALSA: Error enumerating PCM devices: %Rrc (%d)\n", rc2, rc));
350 rc = rc2;
351 }
352 return rc;
353}
354
355
356/**
357 * @interface_method_impl{PDMIHOSTAUDIO,pfnSetDevice}
358 */
359static DECLCALLBACK(int) drvHstAudAlsaHA_SetDevice(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir, const char *pszId)
360{
361 PDRVHSTAUDALSA pThis = RT_FROM_MEMBER(pInterface, DRVHSTAUDALSA, IHostAudio);
362
363 /*
364 * Validate and normalize input.
365 */
366 AssertReturn(enmDir == PDMAUDIODIR_IN || enmDir == PDMAUDIODIR_OUT || enmDir == PDMAUDIODIR_DUPLEX, VERR_INVALID_PARAMETER);
367 AssertPtrNullReturn(pszId, VERR_INVALID_POINTER);
368 if (!pszId || !*pszId)
369 pszId = "default";
370 else
371 {
372 size_t cch = strlen(pszId);
373 AssertReturn(cch < sizeof(pThis->szInputDev), VERR_INVALID_NAME);
374 }
375 LogFunc(("enmDir=%d pszId=%s\n", enmDir, pszId));
376
377 /*
378 * Update input.
379 */
380 if (enmDir == PDMAUDIODIR_IN || enmDir == PDMAUDIODIR_DUPLEX)
381 {
382 int rc = RTCritSectEnter(&pThis->CritSect);
383 AssertRCReturn(rc, rc);
384 if (strcmp(pThis->szInputDev, pszId) == 0)
385 RTCritSectLeave(&pThis->CritSect);
386 else
387 {
388 LogRel(("ALSA: Changing input device: '%s' -> '%s'\n", pThis->szInputDev, pszId));
389 RTStrCopy(pThis->szInputDev, sizeof(pThis->szInputDev), pszId);
390 PPDMIHOSTAUDIOPORT pIHostAudioPort = pThis->pIHostAudioPort;
391 RTCritSectLeave(&pThis->CritSect);
392 if (pIHostAudioPort)
393 {
394 LogFlowFunc(("Notifying parent driver about input device change...\n"));
395 pIHostAudioPort->pfnNotifyDeviceChanged(pIHostAudioPort, PDMAUDIODIR_IN, NULL /*pvUser*/);
396 }
397 }
398 }
399
400 /*
401 * Update output.
402 */
403 if (enmDir == PDMAUDIODIR_OUT || enmDir == PDMAUDIODIR_DUPLEX)
404 {
405 int rc = RTCritSectEnter(&pThis->CritSect);
406 AssertRCReturn(rc, rc);
407 if (strcmp(pThis->szOutputDev, pszId) == 0)
408 RTCritSectLeave(&pThis->CritSect);
409 else
410 {
411 LogRel(("ALSA: Changing output device: '%s' -> '%s'\n", pThis->szOutputDev, pszId));
412 RTStrCopy(pThis->szOutputDev, sizeof(pThis->szOutputDev), pszId);
413 PPDMIHOSTAUDIOPORT pIHostAudioPort = pThis->pIHostAudioPort;
414 RTCritSectLeave(&pThis->CritSect);
415 if (pIHostAudioPort)
416 {
417 LogFlowFunc(("Notifying parent driver about output device change...\n"));
418 pIHostAudioPort->pfnNotifyDeviceChanged(pIHostAudioPort, PDMAUDIODIR_OUT, NULL /*pvUser*/);
419 }
420 }
421 }
422
423 return VINF_SUCCESS;
424}
425
426
427/**
428 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
429 */
430static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHstAudAlsaHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
431{
432 RT_NOREF(enmDir);
433 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
434
435 return PDMAUDIOBACKENDSTS_RUNNING;
436}
437
438
439/**
440 * Converts internal audio PCM properties to an ALSA PCM format.
441 *
442 * @returns Converted ALSA PCM format.
443 * @param pProps Internal audio PCM configuration to convert.
444 */
445static snd_pcm_format_t alsaAudioPropsToALSA(PCPDMAUDIOPCMPROPS pProps)
446{
447 switch (PDMAudioPropsSampleSize(pProps))
448 {
449 case 1:
450 return pProps->fSigned ? SND_PCM_FORMAT_S8 : SND_PCM_FORMAT_U8;
451
452 case 2:
453 if (PDMAudioPropsIsLittleEndian(pProps))
454 return pProps->fSigned ? SND_PCM_FORMAT_S16_LE : SND_PCM_FORMAT_U16_LE;
455 return pProps->fSigned ? SND_PCM_FORMAT_S16_BE : SND_PCM_FORMAT_U16_BE;
456
457 case 4:
458 if (PDMAudioPropsIsLittleEndian(pProps))
459 return pProps->fSigned ? SND_PCM_FORMAT_S32_LE : SND_PCM_FORMAT_U32_LE;
460 return pProps->fSigned ? SND_PCM_FORMAT_S32_BE : SND_PCM_FORMAT_U32_BE;
461
462 default:
463 AssertLogRelMsgFailed(("%RU8 bytes not supported\n", PDMAudioPropsSampleSize(pProps)));
464 return SND_PCM_FORMAT_UNKNOWN;
465 }
466}
467
468
469/**
470 * Sets the software parameters of an ALSA stream.
471 *
472 * @returns 0 on success, negative errno on failure.
473 * @param hPCM ALSA stream to set software parameters for.
474 * @param pCfgReq Requested stream configuration (PDM).
475 * @param pCfgAcq The actual stream configuration (PDM). Updated as
476 * needed.
477 */
478static int alsaStreamSetSWParams(snd_pcm_t *hPCM, PCPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
479{
480 if (pCfgReq->enmDir == PDMAUDIODIR_IN) /* For input streams there's nothing to do in here right now. */
481 return 0;
482
483 snd_pcm_sw_params_t *pSWParms = NULL;
484 snd_pcm_sw_params_alloca(&pSWParms);
485 AssertReturn(pSWParms, -ENOMEM);
486
487 int err = snd_pcm_sw_params_current(hPCM, pSWParms);
488 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to get current software parameters: %s\n", snd_strerror(err)), err);
489
490 /* Under normal circumstance, we don't need to set a playback threshold
491 because DrvAudio will do the pre-buffering and hand us everything in
492 one continuous chunk when we should start playing. But since it is
493 configurable, we'll set a reasonable minimum of two DMA periods or
494 max 50 milliseconds (the pAlsaCfgReq->threshold value).
495
496 Of course we also have to make sure the threshold is below the buffer
497 size, or ALSA will never start playing. */
498 unsigned long const cFramesMax = PDMAudioPropsMilliToFrames(&pCfgAcq->Props, 50);
499 unsigned long cFramesThreshold = RT_MIN(pCfgAcq->Backend.cFramesPeriod * 2, cFramesMax);
500 if (cFramesThreshold >= pCfgAcq->Backend.cFramesBufferSize - pCfgAcq->Backend.cFramesBufferSize / 16)
501 cFramesThreshold = pCfgAcq->Backend.cFramesBufferSize - pCfgAcq->Backend.cFramesBufferSize / 16;
502
503 err = snd_pcm_sw_params_set_start_threshold(hPCM, pSWParms, cFramesThreshold);
504 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to set software threshold to %lu: %s\n", cFramesThreshold, snd_strerror(err)), err);
505
506 err = snd_pcm_sw_params_set_avail_min(hPCM, pSWParms, pCfgReq->Backend.cFramesPeriod);
507 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to set available minimum to %u: %s\n",
508 pCfgReq->Backend.cFramesPeriod, snd_strerror(err)), err);
509
510 /* Commit the software parameters: */
511 err = snd_pcm_sw_params(hPCM, pSWParms);
512 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to set new software parameters: %s\n", snd_strerror(err)), err);
513
514 /* Get the actual parameters: */
515 snd_pcm_uframes_t cFramesThresholdActual = cFramesThreshold;
516 err = snd_pcm_sw_params_get_start_threshold(pSWParms, &cFramesThresholdActual);
517 AssertLogRelMsgStmt(err >= 0, ("ALSA: Failed to get start threshold: %s\n", snd_strerror(err)),
518 cFramesThresholdActual = cFramesThreshold);
519
520 LogRel2(("ALSA: SW params: %lu frames threshold, %u frames avail minimum\n",
521 cFramesThresholdActual, pCfgAcq->Backend.cFramesPeriod));
522 return 0;
523}
524
525
526/**
527 * Maps a PDM channel ID to an ASLA channel map position.
528 */
529static unsigned int drvHstAudAlsaPdmChToAlsa(PDMAUDIOCHANNELID enmId, uint8_t cChannels)
530{
531 switch (enmId)
532 {
533 case PDMAUDIOCHANNELID_UNKNOWN: return SND_CHMAP_UNKNOWN;
534 case PDMAUDIOCHANNELID_UNUSED_ZERO: return SND_CHMAP_NA;
535 case PDMAUDIOCHANNELID_UNUSED_SILENCE: return SND_CHMAP_NA;
536
537 case PDMAUDIOCHANNELID_FRONT_LEFT: return SND_CHMAP_FL;
538 case PDMAUDIOCHANNELID_FRONT_RIGHT: return SND_CHMAP_FR;
539 case PDMAUDIOCHANNELID_FRONT_CENTER: return cChannels == 1 ? SND_CHMAP_MONO : SND_CHMAP_FC;
540 case PDMAUDIOCHANNELID_LFE: return SND_CHMAP_LFE;
541 case PDMAUDIOCHANNELID_REAR_LEFT: return SND_CHMAP_RL;
542 case PDMAUDIOCHANNELID_REAR_RIGHT: return SND_CHMAP_RR;
543 case PDMAUDIOCHANNELID_FRONT_LEFT_OF_CENTER: return SND_CHMAP_FLC;
544 case PDMAUDIOCHANNELID_FRONT_RIGHT_OF_CENTER: return SND_CHMAP_FRC;
545 case PDMAUDIOCHANNELID_REAR_CENTER: return SND_CHMAP_RC;
546 case PDMAUDIOCHANNELID_SIDE_LEFT: return SND_CHMAP_SL;
547 case PDMAUDIOCHANNELID_SIDE_RIGHT: return SND_CHMAP_SR;
548 case PDMAUDIOCHANNELID_TOP_CENTER: return SND_CHMAP_TC;
549 case PDMAUDIOCHANNELID_FRONT_LEFT_HEIGHT: return SND_CHMAP_TFL;
550 case PDMAUDIOCHANNELID_FRONT_CENTER_HEIGHT: return SND_CHMAP_TFC;
551 case PDMAUDIOCHANNELID_FRONT_RIGHT_HEIGHT: return SND_CHMAP_TFR;
552 case PDMAUDIOCHANNELID_REAR_LEFT_HEIGHT: return SND_CHMAP_TRL;
553 case PDMAUDIOCHANNELID_REAR_CENTER_HEIGHT: return SND_CHMAP_TRC;
554 case PDMAUDIOCHANNELID_REAR_RIGHT_HEIGHT: return SND_CHMAP_TRR;
555
556 case PDMAUDIOCHANNELID_INVALID:
557 case PDMAUDIOCHANNELID_END:
558 case PDMAUDIOCHANNELID_32BIT_HACK:
559 break;
560 }
561 AssertFailed();
562 return SND_CHMAP_NA;
563}
564
565
566/**
567 * Sets the hardware parameters of an ALSA stream.
568 *
569 * @returns 0 on success, negative errno on failure.
570 * @param hPCM ALSA stream to set software parameters for.
571 * @param enmAlsaFmt The ALSA format to use.
572 * @param pCfgReq Requested stream configuration (PDM).
573 * @param pCfgAcq The actual stream configuration (PDM). This is assumed
574 * to be a copy of pCfgReq on input, at least for
575 * properties handled here. On output some of the
576 * properties may be updated to match the actual stream
577 * configuration.
578 */
579static int alsaStreamSetHwParams(snd_pcm_t *hPCM, snd_pcm_format_t enmAlsaFmt,
580 PCPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
581{
582 /*
583 * Get the current hardware parameters.
584 */
585 snd_pcm_hw_params_t *pHWParms = NULL;
586 snd_pcm_hw_params_alloca(&pHWParms);
587 AssertReturn(pHWParms, -ENOMEM);
588
589 int err = snd_pcm_hw_params_any(hPCM, pHWParms);
590 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to initialize hardware parameters: %s\n", snd_strerror(err)), err);
591
592 /*
593 * Modify them according to pAlsaCfgReq.
594 * We update pAlsaCfgObt as we go for parameters set by "near" methods.
595 */
596 /* We'll use snd_pcm_writei/snd_pcm_readi: */
597 err = snd_pcm_hw_params_set_access(hPCM, pHWParms, SND_PCM_ACCESS_RW_INTERLEAVED);
598 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to set access type: %s\n", snd_strerror(err)), err);
599
600 /* Set the format and frequency. */
601 err = snd_pcm_hw_params_set_format(hPCM, pHWParms, enmAlsaFmt);
602 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to set audio format to %d: %s\n", enmAlsaFmt, snd_strerror(err)), err);
603
604 unsigned int uFreq = PDMAudioPropsHz(&pCfgReq->Props);
605 err = snd_pcm_hw_params_set_rate_near(hPCM, pHWParms, &uFreq, NULL /*dir*/);
606 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to set frequency to %uHz: %s\n",
607 PDMAudioPropsHz(&pCfgReq->Props), snd_strerror(err)), err);
608 pCfgAcq->Props.uHz = uFreq;
609
610 /* Channel count currently does not change with the mapping translations,
611 as ALSA can express both silent and unknown channel positions. */
612 union
613 {
614 snd_pcm_chmap_t Map;
615 unsigned int padding[1 + PDMAUDIO_MAX_CHANNELS];
616 } u;
617 uint8_t aidSrcChannels[PDMAUDIO_MAX_CHANNELS];
618 unsigned int *aidDstChannels = u.Map.pos;
619 unsigned int cChannels = u.Map.channels = PDMAudioPropsChannels(&pCfgReq->Props);
620 unsigned int iDst = 0;
621 for (unsigned int iSrc = 0; iSrc < cChannels; iSrc++)
622 {
623 uint8_t const idSrc = pCfgReq->Props.aidChannels[iSrc];
624 aidSrcChannels[iDst] = idSrc;
625 aidDstChannels[iDst] = drvHstAudAlsaPdmChToAlsa((PDMAUDIOCHANNELID)idSrc, cChannels);
626 iDst++;
627 }
628 u.Map.channels = cChannels = iDst;
629 for (; iDst < PDMAUDIO_MAX_CHANNELS; iDst++)
630 {
631 aidSrcChannels[iDst] = PDMAUDIOCHANNELID_INVALID;
632 aidDstChannels[iDst] = SND_CHMAP_NA;
633 }
634
635 err = snd_pcm_hw_params_set_channels_near(hPCM, pHWParms, &cChannels);
636 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to set number of channels to %d\n", PDMAudioPropsChannels(&pCfgReq->Props)),
637 err);
638 if (cChannels == PDMAudioPropsChannels(&pCfgReq->Props))
639 memcpy(pCfgAcq->Props.aidChannels, aidSrcChannels, sizeof(pCfgAcq->Props.aidChannels));
640 else
641 {
642 LogRel2(("ALSA: Requested %u channels, got %u\n", u.Map.channels, cChannels));
643 AssertLogRelMsgReturn(cChannels > 0 && cChannels <= PDMAUDIO_MAX_CHANNELS,
644 ("ALSA: Unsupported channel count: %u (requested %d)\n",
645 cChannels, PDMAudioPropsChannels(&pCfgReq->Props)), -ERANGE);
646 PDMAudioPropsSetChannels(&pCfgAcq->Props, (uint8_t)cChannels);
647 /** @todo Can we somehow guess channel IDs? snd_pcm_get_chmap? */
648 }
649
650 /* The period size (reportedly frame count per hw interrupt): */
651 int dir = 0;
652 snd_pcm_uframes_t minval = pCfgReq->Backend.cFramesPeriod;
653 err = snd_pcm_hw_params_get_period_size_min(pHWParms, &minval, &dir);
654 AssertLogRelMsgReturn(err >= 0, ("ALSA: Could not determine minimal period size: %s\n", snd_strerror(err)), err);
655
656 snd_pcm_uframes_t period_size_f = pCfgReq->Backend.cFramesPeriod;
657 if (period_size_f < minval)
658 period_size_f = minval;
659 err = snd_pcm_hw_params_set_period_size_near(hPCM, pHWParms, &period_size_f, 0);
660 LogRel2(("ALSA: Period size is: %lu frames (min %lu, requested %u)\n", period_size_f, minval, pCfgReq->Backend.cFramesPeriod));
661 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to set period size %d (%s)\n", period_size_f, snd_strerror(err)), err);
662
663 /* The buffer size: */
664 minval = pCfgReq->Backend.cFramesBufferSize;
665 err = snd_pcm_hw_params_get_buffer_size_min(pHWParms, &minval);
666 AssertLogRelMsgReturn(err >= 0, ("ALSA: Could not retrieve minimal buffer size: %s\n", snd_strerror(err)), err);
667
668 snd_pcm_uframes_t buffer_size_f = pCfgReq->Backend.cFramesBufferSize;
669 if (buffer_size_f < minval)
670 buffer_size_f = minval;
671 err = snd_pcm_hw_params_set_buffer_size_near(hPCM, pHWParms, &buffer_size_f);
672 LogRel2(("ALSA: Buffer size is: %lu frames (min %lu, requested %u)\n", buffer_size_f, minval, pCfgReq->Backend.cFramesBufferSize));
673 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to set near buffer size %RU32: %s\n", buffer_size_f, snd_strerror(err)), err);
674
675 /*
676 * Set the hardware parameters.
677 */
678 err = snd_pcm_hw_params(hPCM, pHWParms);
679 AssertLogRelMsgReturn(err >= 0, ("ALSA: Failed to apply audio parameters: %s\n", snd_strerror(err)), err);
680
681 /*
682 * Get relevant parameters and put them in the pAlsaCfgObt structure.
683 */
684 snd_pcm_uframes_t obt_buffer_size = buffer_size_f;
685 err = snd_pcm_hw_params_get_buffer_size(pHWParms, &obt_buffer_size);
686 AssertLogRelMsgStmt(err >= 0, ("ALSA: Failed to get buffer size: %s\n", snd_strerror(err)), obt_buffer_size = buffer_size_f);
687 pCfgAcq->Backend.cFramesBufferSize = obt_buffer_size;
688
689 snd_pcm_uframes_t obt_period_size = period_size_f;
690 err = snd_pcm_hw_params_get_period_size(pHWParms, &obt_period_size, &dir);
691 AssertLogRelMsgStmt(err >= 0, ("ALSA: Failed to get period size: %s\n", snd_strerror(err)), obt_period_size = period_size_f);
692 pCfgAcq->Backend.cFramesPeriod = obt_period_size;
693
694 LogRel2(("ALSA: HW params: %u Hz, %u frames period, %u frames buffer, %u channel(s), enmAlsaFmt=%d\n",
695 PDMAudioPropsHz(&pCfgAcq->Props), pCfgAcq->Backend.cFramesPeriod, pCfgAcq->Backend.cFramesBufferSize,
696 PDMAudioPropsChannels(&pCfgAcq->Props), enmAlsaFmt));
697
698 /*
699 * Channel config (not fatal).
700 */
701 if (PDMAudioPropsChannels(&pCfgAcq->Props) == PDMAudioPropsChannels(&pCfgReq->Props))
702 {
703 err = snd_pcm_set_chmap(hPCM, &u.Map);
704 if (err < 0)
705 {
706 if (err == -ENXIO)
707 LogRel2(("ALSA: Audio device does not support channel maps, skipping\n"));
708 else
709 LogRel2(("ALSA: snd_pcm_set_chmap failed: %s (%d)\n", snd_strerror(err), err));
710 }
711 }
712
713 return 0;
714}
715
716
717/**
718 * Opens (creates) an ALSA stream.
719 *
720 * @returns VBox status code.
721 * @param pThis The alsa driver instance data.
722 * @param enmAlsaFmt The ALSA format to use.
723 * @param pCfgReq Requested configuration to create stream with (PDM).
724 * @param pCfgAcq The actual stream configuration (PDM). This is assumed
725 * to be a copy of pCfgReq on input, at least for
726 * properties handled here. On output some of the
727 * properties may be updated to match the actual stream
728 * configuration.
729 * @param phPCM Where to store the ALSA stream handle on success.
730 */
731static int alsaStreamOpen(PDRVHSTAUDALSA pThis, snd_pcm_format_t enmAlsaFmt, PCPDMAUDIOSTREAMCFG pCfgReq,
732 PPDMAUDIOSTREAMCFG pCfgAcq, snd_pcm_t **phPCM)
733{
734 /*
735 * Open the stream.
736 */
737 int rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
738 const char * const pszType = pCfgReq->enmDir == PDMAUDIODIR_IN ? "input" : "output";
739 const char * const pszDev = pCfgReq->enmDir == PDMAUDIODIR_IN ? pThis->szInputDev : pThis->szOutputDev;
740 snd_pcm_stream_t enmType = pCfgReq->enmDir == PDMAUDIODIR_IN ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK;
741
742 snd_pcm_t *hPCM = NULL;
743 LogRel(("ALSA: Using %s device \"%s\"\n", pszType, pszDev));
744 int err = snd_pcm_open(&hPCM, pszDev, enmType, SND_PCM_NONBLOCK);
745 if (err >= 0)
746 {
747 err = snd_pcm_nonblock(hPCM, 1);
748 if (err >= 0)
749 {
750 /*
751 * Configure hardware stream parameters.
752 */
753 err = alsaStreamSetHwParams(hPCM, enmAlsaFmt, pCfgReq, pCfgAcq);
754 if (err >= 0)
755 {
756 /*
757 * Prepare it.
758 */
759 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
760 err = snd_pcm_prepare(hPCM);
761 if (err >= 0)
762 {
763 /*
764 * Configure software stream parameters.
765 */
766 rc = alsaStreamSetSWParams(hPCM, pCfgReq, pCfgAcq);
767 if (RT_SUCCESS(rc))
768 {
769 *phPCM = hPCM;
770 return VINF_SUCCESS;
771 }
772 }
773 else
774 LogRel(("ALSA: snd_pcm_prepare failed: %s\n", snd_strerror(err)));
775 }
776 }
777 else
778 LogRel(("ALSA: Error setting non-blocking mode for %s stream: %s\n", pszType, snd_strerror(err)));
779 drvHstAudAlsaStreamClose(&hPCM);
780 }
781 else
782 LogRel(("ALSA: Failed to open \"%s\" as %s device: %s\n", pszDev, pszType, snd_strerror(err)));
783 *phPCM = NULL;
784 return rc;
785}
786
787
788/**
789 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
790 */
791static DECLCALLBACK(int) drvHstAudAlsaHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
792 PCPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
793{
794 PDRVHSTAUDALSA pThis = RT_FROM_MEMBER(pInterface, DRVHSTAUDALSA, IHostAudio);
795 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
796 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
797 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
798 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
799
800 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
801 PDMAudioStrmCfgCopy(&pStreamALSA->Cfg, pCfgReq);
802
803 int rc;
804 snd_pcm_format_t const enmFmt = alsaAudioPropsToALSA(&pCfgReq->Props);
805 if (enmFmt != SND_PCM_FORMAT_UNKNOWN)
806 {
807 rc = alsaStreamOpen(pThis, enmFmt, pCfgReq, pCfgAcq, &pStreamALSA->hPCM);
808 if (RT_SUCCESS(rc))
809 {
810 /* We have no objections to the pre-buffering that DrvAudio applies,
811 only we need to adjust it relative to the actual buffer size. */
812 pCfgAcq->Backend.cFramesPreBuffering = (uint64_t)pCfgReq->Backend.cFramesPreBuffering
813 * pCfgAcq->Backend.cFramesBufferSize
814 / RT_MAX(pCfgReq->Backend.cFramesBufferSize, 1);
815
816 PDMAudioStrmCfgCopy(&pStreamALSA->Cfg, pCfgAcq);
817 LogFlowFunc(("returns success - hPCM=%p\n", pStreamALSA->hPCM));
818 return rc;
819 }
820 }
821 else
822 rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
823 LogFunc(("returns %Rrc\n", rc));
824 return rc;
825}
826
827
828/**
829 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
830 */
831static DECLCALLBACK(int) drvHstAudAlsaHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream, bool fImmediate)
832{
833 RT_NOREF(pInterface);
834 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
835 AssertPtrReturn(pStreamALSA, VERR_INVALID_POINTER);
836 RT_NOREF(fImmediate);
837
838 LogRelFlowFunc(("Stream '%s' state is '%s'\n", pStreamALSA->Cfg.szName, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
839
840 /** @todo r=bird: It's not like we can do much with a bad status... Check
841 * what the caller does... */
842 int rc = drvHstAudAlsaStreamClose(&pStreamALSA->hPCM);
843
844 LogRelFlowFunc(("returns %Rrc\n", rc));
845
846 return rc;
847}
848
849
850/**
851 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamEnable}
852 */
853static DECLCALLBACK(int) drvHstAudAlsaHA_StreamEnable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
854{
855 RT_NOREF(pInterface);
856 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
857
858 /*
859 * Prepare the stream.
860 */
861 int rc = snd_pcm_prepare(pStreamALSA->hPCM);
862 if (rc >= 0)
863 {
864 Assert(snd_pcm_state(pStreamALSA->hPCM) == SND_PCM_STATE_PREPARED);
865
866 /*
867 * Input streams should be started now, whereas output streams must
868 * pre-buffer sufficent data before starting.
869 */
870 if (pStreamALSA->Cfg.enmDir == PDMAUDIODIR_IN)
871 {
872 rc = snd_pcm_start(pStreamALSA->hPCM);
873 if (rc >= 0)
874 rc = VINF_SUCCESS;
875 else
876 {
877 LogRel(("ALSA: Error starting input stream '%s': %s (%d)\n", pStreamALSA->Cfg.szName, snd_strerror(rc), rc));
878 rc = RTErrConvertFromErrno(-rc);
879 }
880 }
881 else
882 rc = VINF_SUCCESS;
883 }
884 else
885 {
886 LogRel(("ALSA: Error preparing stream '%s': %s (%d)\n", pStreamALSA->Cfg.szName, snd_strerror(rc), rc));
887 rc = RTErrConvertFromErrno(-rc);
888 }
889 LogFlowFunc(("returns %Rrc (state %s)\n", rc, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
890 return rc;
891}
892
893
894/**
895 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDisable}
896 */
897static DECLCALLBACK(int) drvHstAudAlsaHA_StreamDisable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
898{
899 RT_NOREF(pInterface);
900 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
901
902 int rc = snd_pcm_drop(pStreamALSA->hPCM);
903 if (rc >= 0)
904 rc = VINF_SUCCESS;
905 else
906 {
907 LogRel(("ALSA: Error stopping stream '%s': %s (%d)\n", pStreamALSA->Cfg.szName, snd_strerror(rc), rc));
908 rc = RTErrConvertFromErrno(-rc);
909 }
910 LogFlowFunc(("returns %Rrc (state %s)\n", rc, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
911 return rc;
912}
913
914
915/**
916 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPause}
917 */
918static DECLCALLBACK(int) drvHstAudAlsaHA_StreamPause(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
919{
920 /* Same as disable. */
921 /** @todo r=bird: Try use pause and fallback on disable/enable if it isn't
922 * supported or doesn't work. */
923 return drvHstAudAlsaHA_StreamDisable(pInterface, pStream);
924}
925
926
927/**
928 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamResume}
929 */
930static DECLCALLBACK(int) drvHstAudAlsaHA_StreamResume(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
931{
932 /* Same as enable. */
933 return drvHstAudAlsaHA_StreamEnable(pInterface, pStream);
934}
935
936
937/**
938 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDrain}
939 */
940static DECLCALLBACK(int) drvHstAudAlsaHA_StreamDrain(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
941{
942 RT_NOREF(pInterface);
943 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
944
945 snd_pcm_state_t const enmState = snd_pcm_state(pStreamALSA->hPCM);
946 LogRelFlowFunc(("Stream '%s' input state: %s (%d)\n", pStreamALSA->Cfg.szName, snd_pcm_state_name(enmState), enmState));
947
948 /* Only for output streams. */
949 AssertReturn(pStreamALSA->Cfg.enmDir == PDMAUDIODIR_OUT, VERR_WRONG_ORDER);
950
951 int rc;
952 switch (enmState)
953 {
954 case SND_PCM_STATE_RUNNING:
955 case SND_PCM_STATE_PREPARED: /* not yet started */
956 {
957 /* Do not change to blocking here! */
958 rc = snd_pcm_drain(pStreamALSA->hPCM);
959 if (rc >= 0 || rc == -EAGAIN)
960 rc = VINF_SUCCESS;
961 else
962 {
963 snd_pcm_state_t const enmState2 = snd_pcm_state(pStreamALSA->hPCM);
964 if (rc == -EPIPE && enmState2 == enmState)
965 {
966 /* Not entirely sure, but possibly an underrun, so just disable the stream. */
967 LogRel2(("ALSA: snd_pcm_drain failed with -EPIPE, stopping stream (%s)\n", pStreamALSA->Cfg.szName));
968 rc = snd_pcm_drop(pStreamALSA->hPCM);
969 if (rc >= 0)
970 rc = VINF_SUCCESS;
971 else
972 {
973 LogRel(("ALSA: Error draining/stopping stream '%s': %s (%d)\n", pStreamALSA->Cfg.szName, snd_strerror(rc), rc));
974 rc = RTErrConvertFromErrno(-rc);
975 }
976 }
977 else
978 {
979 LogRel(("ALSA: Error draining output of '%s': %s (%d; %s -> %s)\n", pStreamALSA->Cfg.szName, snd_strerror(rc),
980 rc, snd_pcm_state_name(enmState), snd_pcm_state_name(enmState2)));
981 rc = RTErrConvertFromErrno(-rc);
982 }
983 }
984 break;
985 }
986
987 default:
988 rc = VINF_SUCCESS;
989 break;
990 }
991 LogRelFlowFunc(("returns %Rrc (state %s)\n", rc, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
992 return rc;
993}
994
995
996/**
997 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetState}
998 */
999static DECLCALLBACK(PDMHOSTAUDIOSTREAMSTATE) drvHstAudAlsaHA_StreamGetState(PPDMIHOSTAUDIO pInterface,
1000 PPDMAUDIOBACKENDSTREAM pStream)
1001{
1002 RT_NOREF(pInterface);
1003 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1004 AssertPtrReturn(pStreamALSA, PDMHOSTAUDIOSTREAMSTATE_INVALID);
1005
1006 PDMHOSTAUDIOSTREAMSTATE enmStreamState = PDMHOSTAUDIOSTREAMSTATE_OKAY;
1007 snd_pcm_state_t enmAlsaState = snd_pcm_state(pStreamALSA->hPCM);
1008 if (enmAlsaState == SND_PCM_STATE_DRAINING)
1009 {
1010 /* We're operating in non-blocking mode, so we must (at least for a demux
1011 config) call snd_pcm_drain again to drive it forward. Otherwise we
1012 might be stuck in the drain state forever. */
1013 Log5Func(("Calling snd_pcm_drain again...\n"));
1014 snd_pcm_drain(pStreamALSA->hPCM);
1015 enmAlsaState = snd_pcm_state(pStreamALSA->hPCM);
1016 }
1017
1018 if (enmAlsaState == SND_PCM_STATE_DRAINING)
1019 enmStreamState = PDMHOSTAUDIOSTREAMSTATE_DRAINING;
1020#if (((SND_LIB_MAJOR) << 16) | ((SND_LIB_MAJOR) << 8) | (SND_LIB_SUBMINOR)) >= 0x10002 /* was added in 1.0.2 */
1021 else if (enmAlsaState == SND_PCM_STATE_DISCONNECTED)
1022 enmStreamState = PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING;
1023#endif
1024
1025 Log5Func(("Stream '%s': ALSA state=%s -> %s\n",
1026 pStreamALSA->Cfg.szName, snd_pcm_state_name(enmAlsaState), PDMHostAudioStreamStateGetName(enmStreamState) ));
1027 return enmStreamState;
1028}
1029
1030
1031/**
1032 * Returns the available audio frames queued.
1033 *
1034 * @returns VBox status code.
1035 * @param hPCM ALSA stream handle.
1036 * @param pcFramesAvail Where to store the available frames.
1037 */
1038static int alsaStreamGetAvail(snd_pcm_t *hPCM, snd_pcm_sframes_t *pcFramesAvail)
1039{
1040 AssertPtr(hPCM);
1041 AssertPtr(pcFramesAvail);
1042
1043 int rc;
1044 snd_pcm_sframes_t cFramesAvail = snd_pcm_avail_update(hPCM);
1045 if (cFramesAvail > 0)
1046 {
1047 LogFunc(("cFramesAvail=%ld\n", cFramesAvail));
1048 *pcFramesAvail = cFramesAvail;
1049 return VINF_SUCCESS;
1050 }
1051
1052 /*
1053 * We can maybe recover from an EPIPE...
1054 */
1055 if (cFramesAvail == -EPIPE)
1056 {
1057 rc = drvHstAudAlsaStreamRecover(hPCM);
1058 if (RT_SUCCESS(rc))
1059 {
1060 cFramesAvail = snd_pcm_avail_update(hPCM);
1061 if (cFramesAvail >= 0)
1062 {
1063 LogFunc(("cFramesAvail=%ld\n", cFramesAvail));
1064 *pcFramesAvail = cFramesAvail;
1065 return VINF_SUCCESS;
1066 }
1067 }
1068 else
1069 {
1070 *pcFramesAvail = 0;
1071 return rc;
1072 }
1073 }
1074
1075 rc = RTErrConvertFromErrno(-(int)cFramesAvail);
1076 LogFunc(("failed - cFramesAvail=%ld rc=%Rrc\n", cFramesAvail, rc));
1077 *pcFramesAvail = 0;
1078 return rc;
1079}
1080
1081
1082/**
1083 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetPending}
1084 */
1085static DECLCALLBACK(uint32_t) drvHstAudAlsaHA_StreamGetPending(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1086{
1087 RT_NOREF(pInterface);
1088 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1089 AssertPtrReturn(pStreamALSA, 0);
1090
1091 /*
1092 * This is only relevant to output streams (input streams can't have
1093 * any pending, unplayed data).
1094 */
1095 uint32_t cbPending = 0;
1096 if (pStreamALSA->Cfg.enmDir == PDMAUDIODIR_OUT)
1097 {
1098 /*
1099 * Getting the delay (in audio frames) reports the time it will take
1100 * to hear a new sample after all queued samples have been played out.
1101 *
1102 * We use snd_pcm_avail_delay instead of snd_pcm_delay here as it will
1103 * update the buffer positions, and we can use the extra value against
1104 * the buffer size to double check since the delay value may include
1105 * fixed built-in delays in the processing chain and hardware.
1106 */
1107 snd_pcm_sframes_t cFramesAvail = 0;
1108 snd_pcm_sframes_t cFramesDelay = 0;
1109 int rc = snd_pcm_avail_delay(pStreamALSA->hPCM, &cFramesAvail, &cFramesDelay);
1110
1111 /*
1112 * We now also get the state as the pending value should be zero when
1113 * we're not in a playing state.
1114 */
1115 snd_pcm_state_t enmState = snd_pcm_state(pStreamALSA->hPCM);
1116 switch (enmState)
1117 {
1118 case SND_PCM_STATE_RUNNING:
1119 case SND_PCM_STATE_DRAINING:
1120 if (rc >= 0)
1121 {
1122 if ((uint32_t)cFramesAvail >= pStreamALSA->Cfg.Backend.cFramesBufferSize)
1123 cbPending = 0;
1124 else
1125 cbPending = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cFramesDelay);
1126 }
1127 break;
1128
1129 default:
1130 break;
1131 }
1132 Log2Func(("returns %u (%#x) - cFramesBufferSize=%RU32 cFramesAvail=%ld cFramesDelay=%ld rc=%d; enmState=%s (%d) \n",
1133 cbPending, cbPending, pStreamALSA->Cfg.Backend.cFramesBufferSize, cFramesAvail, cFramesDelay, rc,
1134 snd_pcm_state_name(enmState), enmState));
1135 }
1136 return cbPending;
1137}
1138
1139
1140/**
1141 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
1142 */
1143static DECLCALLBACK(uint32_t) drvHstAudAlsaHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1144{
1145 RT_NOREF(pInterface);
1146 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1147
1148 uint32_t cbAvail = 0;
1149 snd_pcm_sframes_t cFramesAvail = 0;
1150 int rc = alsaStreamGetAvail(pStreamALSA->hPCM, &cFramesAvail);
1151 if (RT_SUCCESS(rc))
1152 cbAvail = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cFramesAvail);
1153
1154 return cbAvail;
1155}
1156
1157
1158/**
1159 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
1160 */
1161static DECLCALLBACK(int) drvHstAudAlsaHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
1162 const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
1163{
1164 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1165 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1166 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1167 AssertPtrReturn(pcbWritten, VERR_INVALID_POINTER);
1168 Log4Func(("@%#RX64: pvBuf=%p cbBuf=%#x (%u) state=%s - %s\n", pStreamALSA->offInternal, pvBuf, cbBuf, cbBuf,
1169 snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM)), pStreamALSA->Cfg.szName));
1170 if (cbBuf)
1171 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1172 else
1173 {
1174 /* Fend off draining calls. */
1175 *pcbWritten = 0;
1176 return VINF_SUCCESS;
1177 }
1178
1179 /*
1180 * Determine how much we can write (caller actually did this
1181 * already, but we repeat it just to be sure or something).
1182 */
1183 snd_pcm_sframes_t cFramesAvail;
1184 int rc = alsaStreamGetAvail(pStreamALSA->hPCM, &cFramesAvail);
1185 if (RT_SUCCESS(rc))
1186 {
1187 Assert(cFramesAvail);
1188 if (cFramesAvail)
1189 {
1190 PCPDMAUDIOPCMPROPS pProps = &pStreamALSA->Cfg.Props;
1191 uint32_t cbToWrite = PDMAudioPropsFramesToBytes(pProps, (uint32_t)cFramesAvail);
1192 if (cbToWrite)
1193 {
1194 if (cbToWrite > cbBuf)
1195 cbToWrite = cbBuf;
1196
1197 /*
1198 * Try write the data.
1199 */
1200 uint32_t cFramesToWrite = PDMAudioPropsBytesToFrames(pProps, cbToWrite);
1201 snd_pcm_sframes_t cFramesWritten = snd_pcm_writei(pStreamALSA->hPCM, pvBuf, cFramesToWrite);
1202 if (cFramesWritten > 0)
1203 {
1204 Log4Func(("snd_pcm_writei w/ cbToWrite=%u -> %ld (frames) [cFramesAvail=%ld]\n",
1205 cbToWrite, cFramesWritten, cFramesAvail));
1206 *pcbWritten = PDMAudioPropsFramesToBytes(pProps, cFramesWritten);
1207 pStreamALSA->offInternal += *pcbWritten;
1208 return VINF_SUCCESS;
1209 }
1210 LogFunc(("snd_pcm_writei w/ cbToWrite=%u -> %ld [cFramesAvail=%ld]\n", cbToWrite, cFramesWritten, cFramesAvail));
1211
1212
1213 /*
1214 * There are a couple of error we can recover from, try to do so.
1215 * Only don't try too many times.
1216 */
1217 for (unsigned iTry = 0;
1218 (cFramesWritten == -EPIPE || cFramesWritten == -ESTRPIPE) && iTry < ALSA_RECOVERY_TRIES_MAX;
1219 iTry++)
1220 {
1221 if (cFramesWritten == -EPIPE)
1222 {
1223 /* Underrun occurred. */
1224 rc = drvHstAudAlsaStreamRecover(pStreamALSA->hPCM);
1225 if (RT_FAILURE(rc))
1226 break;
1227 LogFlowFunc(("Recovered from playback (iTry=%u)\n", iTry));
1228 }
1229 else
1230 {
1231 /* An suspended event occurred, needs resuming. */
1232 rc = drvHstAudAlsaStreamResume(pStreamALSA->hPCM);
1233 if (RT_FAILURE(rc))
1234 {
1235 LogRel(("ALSA: Failed to resume output stream (iTry=%u, rc=%Rrc)\n", iTry, rc));
1236 break;
1237 }
1238 LogFlowFunc(("Resumed suspended output stream (iTry=%u)\n", iTry));
1239 }
1240
1241 cFramesWritten = snd_pcm_writei(pStreamALSA->hPCM, pvBuf, cFramesToWrite);
1242 if (cFramesWritten > 0)
1243 {
1244 Log4Func(("snd_pcm_writei w/ cbToWrite=%u -> %ld (frames) [cFramesAvail=%ld]\n",
1245 cbToWrite, cFramesWritten, cFramesAvail));
1246 *pcbWritten = PDMAudioPropsFramesToBytes(pProps, cFramesWritten);
1247 pStreamALSA->offInternal += *pcbWritten;
1248 return VINF_SUCCESS;
1249 }
1250 LogFunc(("snd_pcm_writei w/ cbToWrite=%u -> %ld [cFramesAvail=%ld, iTry=%d]\n", cbToWrite, cFramesWritten, cFramesAvail, iTry));
1251 }
1252
1253 /* Make sure we return with an error status. */
1254 if (RT_SUCCESS_NP(rc))
1255 {
1256 if (cFramesWritten == 0)
1257 rc = VERR_ACCESS_DENIED;
1258 else
1259 {
1260 rc = RTErrConvertFromErrno(-(int)cFramesWritten);
1261 LogFunc(("Failed to write %RU32 bytes: %ld (%Rrc)\n", cbToWrite, cFramesWritten, rc));
1262 }
1263 }
1264 }
1265 }
1266 }
1267 else
1268 LogFunc(("Error getting number of playback frames, rc=%Rrc\n", rc));
1269 *pcbWritten = 0;
1270 return rc;
1271}
1272
1273
1274/**
1275 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
1276 */
1277static DECLCALLBACK(uint32_t) drvHstAudAlsaHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1278{
1279 RT_NOREF(pInterface);
1280 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1281
1282 uint32_t cbAvail = 0;
1283 snd_pcm_sframes_t cFramesAvail = 0;
1284 int rc = alsaStreamGetAvail(pStreamALSA->hPCM, &cFramesAvail);
1285 if (RT_SUCCESS(rc))
1286 cbAvail = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cFramesAvail);
1287
1288 return cbAvail;
1289}
1290
1291
1292/**
1293 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
1294 */
1295static DECLCALLBACK(int) drvHstAudAlsaHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
1296 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
1297{
1298 RT_NOREF_PV(pInterface);
1299 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1300 AssertPtrReturn(pStreamALSA, VERR_INVALID_POINTER);
1301 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1302 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1303 AssertPtrReturn(pcbRead, VERR_INVALID_POINTER);
1304 Log4Func(("@%#RX64: pvBuf=%p cbBuf=%#x (%u) state=%s - %s\n", pStreamALSA->offInternal, pvBuf, cbBuf, cbBuf,
1305 snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM)), pStreamALSA->Cfg.szName));
1306
1307 /*
1308 * Figure out how much we can read without trouble (we're doing
1309 * non-blocking reads, but whatever).
1310 */
1311 snd_pcm_sframes_t cAvail;
1312 int rc = alsaStreamGetAvail(pStreamALSA->hPCM, &cAvail);
1313 if (RT_SUCCESS(rc))
1314 {
1315 if (!cAvail) /* No data yet? */
1316 {
1317 snd_pcm_state_t enmState = snd_pcm_state(pStreamALSA->hPCM);
1318 switch (enmState)
1319 {
1320 case SND_PCM_STATE_PREPARED:
1321 /** @todo r=bird: explain the logic here... */
1322 cAvail = PDMAudioPropsBytesToFrames(&pStreamALSA->Cfg.Props, cbBuf);
1323 break;
1324
1325 case SND_PCM_STATE_SUSPENDED:
1326 rc = drvHstAudAlsaStreamResume(pStreamALSA->hPCM);
1327 if (RT_SUCCESS(rc))
1328 {
1329 LogFlowFunc(("Resumed suspended input stream.\n"));
1330 break;
1331 }
1332 LogFunc(("Failed resuming suspended input stream: %Rrc\n", rc));
1333 return rc;
1334
1335 default:
1336 LogFlow(("No frames available: state=%s (%d)\n", snd_pcm_state_name(enmState), enmState));
1337 break;
1338 }
1339 if (!cAvail)
1340 {
1341 *pcbRead = 0;
1342 return VINF_SUCCESS;
1343 }
1344 }
1345 }
1346 else
1347 {
1348 LogFunc(("Error getting number of captured frames, rc=%Rrc\n", rc));
1349 return rc;
1350 }
1351
1352 size_t cbToRead = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cAvail);
1353 cbToRead = RT_MIN(cbToRead, cbBuf);
1354 LogFlowFunc(("cbToRead=%zu, cAvail=%RI32\n", cbToRead, cAvail));
1355
1356 /*
1357 * Read loop.
1358 */
1359 uint32_t cbReadTotal = 0;
1360 while (cbToRead > 0)
1361 {
1362 /*
1363 * Do the reading.
1364 */
1365 snd_pcm_uframes_t const cFramesToRead = PDMAudioPropsBytesToFrames(&pStreamALSA->Cfg.Props, cbToRead);
1366 AssertBreakStmt(cFramesToRead > 0, rc = VERR_NO_DATA);
1367
1368 snd_pcm_sframes_t cFramesRead = snd_pcm_readi(pStreamALSA->hPCM, pvBuf, cFramesToRead);
1369 if (cFramesRead > 0)
1370 {
1371 /*
1372 * We should not run into a full mixer buffer or we lose samples and
1373 * run into an endless loop if ALSA keeps producing samples ("null"
1374 * capture device for example).
1375 */
1376 uint32_t const cbRead = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cFramesRead);
1377 Assert(cbRead <= cbToRead);
1378
1379 cbToRead -= cbRead;
1380 cbReadTotal += cbRead;
1381 pvBuf = (uint8_t *)pvBuf + cbRead;
1382 pStreamALSA->offInternal += cbRead;
1383 }
1384 else
1385 {
1386 /*
1387 * Try recover from overrun and re-try.
1388 * Other conditions/errors we cannot and will just quit the loop.
1389 */
1390 if (cFramesRead == -EPIPE)
1391 {
1392 rc = drvHstAudAlsaStreamRecover(pStreamALSA->hPCM);
1393 if (RT_SUCCESS(rc))
1394 {
1395 LogFlowFunc(("Successfully recovered from overrun\n"));
1396 continue;
1397 }
1398 LogFunc(("Failed to recover from overrun: %Rrc\n", rc));
1399 }
1400 else if (cFramesRead == -EAGAIN)
1401 LogFunc(("No input frames available (EAGAIN)\n"));
1402 else if (cFramesRead == 0)
1403 LogFunc(("No input frames available (0)\n"));
1404 else
1405 {
1406 rc = RTErrConvertFromErrno(-(int)cFramesRead);
1407 LogFunc(("Failed to read input frames: %s (%ld, %Rrc)\n", snd_strerror(cFramesRead), cFramesRead, rc));
1408 }
1409
1410 /* If we've read anything, suppress the error. */
1411 if (RT_FAILURE(rc) && cbReadTotal > 0)
1412 {
1413 LogFunc(("Suppressing %Rrc because %#x bytes has been read already\n", rc, cbReadTotal));
1414 rc = VINF_SUCCESS;
1415 }
1416 break;
1417 }
1418 }
1419
1420 LogFlowFunc(("returns %Rrc and %#x (%d) bytes (%u bytes left); state %s\n",
1421 rc, cbReadTotal, cbReadTotal, cbToRead, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
1422 *pcbRead = cbReadTotal;
1423 return rc;
1424}
1425
1426
1427/*********************************************************************************************************************************
1428* PDMIBASE *
1429*********************************************************************************************************************************/
1430
1431/**
1432 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1433 */
1434static DECLCALLBACK(void *) drvHstAudAlsaQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1435{
1436 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1437 PDRVHSTAUDALSA pThis = PDMINS_2_DATA(pDrvIns, PDRVHSTAUDALSA);
1438 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1439 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
1440 return NULL;
1441}
1442
1443
1444/*********************************************************************************************************************************
1445* PDMDRVREG *
1446*********************************************************************************************************************************/
1447
1448/**
1449 * @interface_method_impl{PDMDRVREG,pfnDestruct,
1450 * Destructs an ALSA host audio driver instance.}
1451 */
1452static DECLCALLBACK(void) drvHstAudAlsaDestruct(PPDMDRVINS pDrvIns)
1453{
1454 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1455 PDRVHSTAUDALSA pThis = PDMINS_2_DATA(pDrvIns, PDRVHSTAUDALSA);
1456 LogFlowFuncEnter();
1457
1458 if (RTCritSectIsInitialized(&pThis->CritSect))
1459 {
1460 RTCritSectEnter(&pThis->CritSect);
1461 pThis->pIHostAudioPort = NULL;
1462 RTCritSectLeave(&pThis->CritSect);
1463 RTCritSectDelete(&pThis->CritSect);
1464 }
1465
1466 LogFlowFuncLeave();
1467}
1468
1469
1470/**
1471 * @interface_method_impl{PDMDRVREG,pfnConstruct,
1472 * Construct an ALSA host audio driver instance.}
1473 */
1474static DECLCALLBACK(int) drvHstAudAlsaConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1475{
1476 RT_NOREF(fFlags);
1477 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1478 PDRVHSTAUDALSA pThis = PDMINS_2_DATA(pDrvIns, PDRVHSTAUDALSA);
1479 PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
1480 LogRel(("Audio: Initializing ALSA driver\n"));
1481
1482 /*
1483 * Init the static parts.
1484 */
1485 pThis->pDrvIns = pDrvIns;
1486 int rc = RTCritSectInit(&pThis->CritSect);
1487 AssertRCReturn(rc, rc);
1488 /* IBase */
1489 pDrvIns->IBase.pfnQueryInterface = drvHstAudAlsaQueryInterface;
1490 /* IHostAudio */
1491 pThis->IHostAudio.pfnGetConfig = drvHstAudAlsaHA_GetConfig;
1492 pThis->IHostAudio.pfnGetDevices = drvHstAudAlsaHA_GetDevices;
1493 pThis->IHostAudio.pfnSetDevice = drvHstAudAlsaHA_SetDevice;
1494 pThis->IHostAudio.pfnGetStatus = drvHstAudAlsaHA_GetStatus;
1495 pThis->IHostAudio.pfnDoOnWorkerThread = NULL;
1496 pThis->IHostAudio.pfnStreamConfigHint = NULL;
1497 pThis->IHostAudio.pfnStreamCreate = drvHstAudAlsaHA_StreamCreate;
1498 pThis->IHostAudio.pfnStreamInitAsync = NULL;
1499 pThis->IHostAudio.pfnStreamDestroy = drvHstAudAlsaHA_StreamDestroy;
1500 pThis->IHostAudio.pfnStreamNotifyDeviceChanged = NULL;
1501 pThis->IHostAudio.pfnStreamEnable = drvHstAudAlsaHA_StreamEnable;
1502 pThis->IHostAudio.pfnStreamDisable = drvHstAudAlsaHA_StreamDisable;
1503 pThis->IHostAudio.pfnStreamPause = drvHstAudAlsaHA_StreamPause;
1504 pThis->IHostAudio.pfnStreamResume = drvHstAudAlsaHA_StreamResume;
1505 pThis->IHostAudio.pfnStreamDrain = drvHstAudAlsaHA_StreamDrain;
1506 pThis->IHostAudio.pfnStreamGetPending = drvHstAudAlsaHA_StreamGetPending;
1507 pThis->IHostAudio.pfnStreamGetState = drvHstAudAlsaHA_StreamGetState;
1508 pThis->IHostAudio.pfnStreamGetWritable = drvHstAudAlsaHA_StreamGetWritable;
1509 pThis->IHostAudio.pfnStreamPlay = drvHstAudAlsaHA_StreamPlay;
1510 pThis->IHostAudio.pfnStreamGetReadable = drvHstAudAlsaHA_StreamGetReadable;
1511 pThis->IHostAudio.pfnStreamCapture = drvHstAudAlsaHA_StreamCapture;
1512
1513 /*
1514 * Read configuration.
1515 */
1516 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "OutputDeviceID|InputDeviceID", "");
1517
1518 rc = pHlp->pfnCFGMQueryStringDef(pCfg, "InputDeviceID", pThis->szInputDev, sizeof(pThis->szInputDev), "default");
1519 AssertRCReturn(rc, rc);
1520 rc = pHlp->pfnCFGMQueryStringDef(pCfg, "OutputDeviceID", pThis->szOutputDev, sizeof(pThis->szOutputDev), "default");
1521 AssertRCReturn(rc, rc);
1522
1523 /*
1524 * Init the alsa library.
1525 */
1526 rc = audioLoadAlsaLib();
1527 if (RT_FAILURE(rc))
1528 {
1529 LogRel(("ALSA: Failed to load the ALSA shared library: %Rrc\n", rc));
1530 return rc;
1531 }
1532
1533 /*
1534 * Query the notification interface from the driver/device above us.
1535 */
1536 pThis->pIHostAudioPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIHOSTAUDIOPORT);
1537 AssertReturn(pThis->pIHostAudioPort, VERR_PDM_MISSING_INTERFACE_ABOVE);
1538
1539#ifdef DEBUG
1540 /*
1541 * Some debug stuff we don't use for anything at all.
1542 */
1543 snd_lib_error_set_handler(drvHstAudAlsaDbgErrorHandler);
1544#endif
1545 return VINF_SUCCESS;
1546}
1547
1548
1549/**
1550 * ALSA audio driver registration record.
1551 */
1552const PDMDRVREG g_DrvHostALSAAudio =
1553{
1554 /* u32Version */
1555 PDM_DRVREG_VERSION,
1556 /* szName */
1557 "ALSAAudio",
1558 /* szRCMod */
1559 "",
1560 /* szR0Mod */
1561 "",
1562 /* pszDescription */
1563 "ALSA host audio driver",
1564 /* fFlags */
1565 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1566 /* fClass. */
1567 PDM_DRVREG_CLASS_AUDIO,
1568 /* cMaxInstances */
1569 ~0U,
1570 /* cbInstance */
1571 sizeof(DRVHSTAUDALSA),
1572 /* pfnConstruct */
1573 drvHstAudAlsaConstruct,
1574 /* pfnDestruct */
1575 drvHstAudAlsaDestruct,
1576 /* pfnRelocate */
1577 NULL,
1578 /* pfnIOCtl */
1579 NULL,
1580 /* pfnPowerOn */
1581 NULL,
1582 /* pfnReset */
1583 NULL,
1584 /* pfnSuspend */
1585 NULL,
1586 /* pfnResume */
1587 NULL,
1588 /* pfnAttach */
1589 NULL,
1590 /* pfnDetach */
1591 NULL,
1592 /* pfnPowerOff */
1593 NULL,
1594 /* pfnSoftReset */
1595 NULL,
1596 /* u32EndVersion */
1597 PDM_DRVREG_VERSION
1598};
1599
Note: See TracBrowser for help on using the repository browser.

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