VirtualBox

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

Last change on this file since 94368 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 60.6 KB
Line 
1/* $Id: DrvHostAudioAlsa.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * Host audio driver - Advanced Linux Sound Architecture (ALSA).
4 */
5
6/*
7 * Copyright (C) 2006-2022 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#if 0 /* Disabled in the hope to resolve testboxes not being able to drain + crashing when closing the PCM streams. */
699 /*
700 * Channel config (not fatal).
701 */
702 if (PDMAudioPropsChannels(&pCfgAcq->Props) == PDMAudioPropsChannels(&pCfgReq->Props))
703 {
704 err = snd_pcm_set_chmap(hPCM, &u.Map);
705 if (err < 0)
706 {
707 if (err == -ENXIO)
708 LogRel2(("ALSA: Audio device does not support channel maps, skipping\n"));
709 else
710 LogRel2(("ALSA: snd_pcm_set_chmap failed: %s (%d)\n", snd_strerror(err), err));
711 }
712 }
713#endif
714
715 return 0;
716}
717
718
719/**
720 * Opens (creates) an ALSA stream.
721 *
722 * @returns VBox status code.
723 * @param pThis The alsa driver instance data.
724 * @param enmAlsaFmt The ALSA format to use.
725 * @param pCfgReq Requested configuration to create stream with (PDM).
726 * @param pCfgAcq The actual stream configuration (PDM). This is assumed
727 * to be a copy of pCfgReq on input, at least for
728 * properties handled here. On output some of the
729 * properties may be updated to match the actual stream
730 * configuration.
731 * @param phPCM Where to store the ALSA stream handle on success.
732 */
733static int alsaStreamOpen(PDRVHSTAUDALSA pThis, snd_pcm_format_t enmAlsaFmt, PCPDMAUDIOSTREAMCFG pCfgReq,
734 PPDMAUDIOSTREAMCFG pCfgAcq, snd_pcm_t **phPCM)
735{
736 /*
737 * Open the stream.
738 */
739 int rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
740 const char * const pszType = pCfgReq->enmDir == PDMAUDIODIR_IN ? "input" : "output";
741 const char * const pszDev = pCfgReq->enmDir == PDMAUDIODIR_IN ? pThis->szInputDev : pThis->szOutputDev;
742 snd_pcm_stream_t enmType = pCfgReq->enmDir == PDMAUDIODIR_IN ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK;
743
744 snd_pcm_t *hPCM = NULL;
745 LogRel(("ALSA: Using %s device \"%s\"\n", pszType, pszDev));
746 int err = snd_pcm_open(&hPCM, pszDev, enmType, SND_PCM_NONBLOCK);
747 if (err >= 0)
748 {
749 err = snd_pcm_nonblock(hPCM, 1);
750 if (err >= 0)
751 {
752 /*
753 * Configure hardware stream parameters.
754 */
755 err = alsaStreamSetHwParams(hPCM, enmAlsaFmt, pCfgReq, pCfgAcq);
756 if (err >= 0)
757 {
758 /*
759 * Prepare it.
760 */
761 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
762 err = snd_pcm_prepare(hPCM);
763 if (err >= 0)
764 {
765 /*
766 * Configure software stream parameters.
767 */
768 rc = alsaStreamSetSWParams(hPCM, pCfgReq, pCfgAcq);
769 if (RT_SUCCESS(rc))
770 {
771 *phPCM = hPCM;
772 return VINF_SUCCESS;
773 }
774 }
775 else
776 LogRel(("ALSA: snd_pcm_prepare failed: %s\n", snd_strerror(err)));
777 }
778 }
779 else
780 LogRel(("ALSA: Error setting non-blocking mode for %s stream: %s\n", pszType, snd_strerror(err)));
781 drvHstAudAlsaStreamClose(&hPCM);
782 }
783 else
784 LogRel(("ALSA: Failed to open \"%s\" as %s device: %s\n", pszDev, pszType, snd_strerror(err)));
785 *phPCM = NULL;
786 return rc;
787}
788
789
790/**
791 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
792 */
793static DECLCALLBACK(int) drvHstAudAlsaHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
794 PCPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
795{
796 PDRVHSTAUDALSA pThis = RT_FROM_MEMBER(pInterface, DRVHSTAUDALSA, IHostAudio);
797 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
798 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
799 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
800 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
801
802 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
803 PDMAudioStrmCfgCopy(&pStreamALSA->Cfg, pCfgReq);
804
805 int rc;
806 snd_pcm_format_t const enmFmt = alsaAudioPropsToALSA(&pCfgReq->Props);
807 if (enmFmt != SND_PCM_FORMAT_UNKNOWN)
808 {
809 rc = alsaStreamOpen(pThis, enmFmt, pCfgReq, pCfgAcq, &pStreamALSA->hPCM);
810 if (RT_SUCCESS(rc))
811 {
812 /* We have no objections to the pre-buffering that DrvAudio applies,
813 only we need to adjust it relative to the actual buffer size. */
814 pCfgAcq->Backend.cFramesPreBuffering = (uint64_t)pCfgReq->Backend.cFramesPreBuffering
815 * pCfgAcq->Backend.cFramesBufferSize
816 / RT_MAX(pCfgReq->Backend.cFramesBufferSize, 1);
817
818 PDMAudioStrmCfgCopy(&pStreamALSA->Cfg, pCfgAcq);
819 LogFlowFunc(("returns success - hPCM=%p\n", pStreamALSA->hPCM));
820 return rc;
821 }
822 }
823 else
824 rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
825 LogFunc(("returns %Rrc\n", rc));
826 return rc;
827}
828
829
830/**
831 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
832 */
833static DECLCALLBACK(int) drvHstAudAlsaHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream, bool fImmediate)
834{
835 RT_NOREF(pInterface);
836 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
837 AssertPtrReturn(pStreamALSA, VERR_INVALID_POINTER);
838 RT_NOREF(fImmediate);
839
840 LogRelFlowFunc(("Stream '%s' state is '%s'\n", pStreamALSA->Cfg.szName, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
841
842 /** @todo r=bird: It's not like we can do much with a bad status... Check
843 * what the caller does... */
844 int rc = drvHstAudAlsaStreamClose(&pStreamALSA->hPCM);
845
846 LogRelFlowFunc(("returns %Rrc\n", rc));
847
848 return rc;
849}
850
851
852/**
853 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamEnable}
854 */
855static DECLCALLBACK(int) drvHstAudAlsaHA_StreamEnable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
856{
857 RT_NOREF(pInterface);
858 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
859
860 /*
861 * Prepare the stream.
862 */
863 int rc = snd_pcm_prepare(pStreamALSA->hPCM);
864 if (rc >= 0)
865 {
866 Assert(snd_pcm_state(pStreamALSA->hPCM) == SND_PCM_STATE_PREPARED);
867
868 /*
869 * Input streams should be started now, whereas output streams must
870 * pre-buffer sufficent data before starting.
871 */
872 if (pStreamALSA->Cfg.enmDir == PDMAUDIODIR_IN)
873 {
874 rc = snd_pcm_start(pStreamALSA->hPCM);
875 if (rc >= 0)
876 rc = VINF_SUCCESS;
877 else
878 {
879 LogRel(("ALSA: Error starting input stream '%s': %s (%d)\n", pStreamALSA->Cfg.szName, snd_strerror(rc), rc));
880 rc = RTErrConvertFromErrno(-rc);
881 }
882 }
883 else
884 rc = VINF_SUCCESS;
885 }
886 else
887 {
888 LogRel(("ALSA: Error preparing stream '%s': %s (%d)\n", pStreamALSA->Cfg.szName, snd_strerror(rc), rc));
889 rc = RTErrConvertFromErrno(-rc);
890 }
891 LogFlowFunc(("returns %Rrc (state %s)\n", rc, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
892 return rc;
893}
894
895
896/**
897 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDisable}
898 */
899static DECLCALLBACK(int) drvHstAudAlsaHA_StreamDisable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
900{
901 RT_NOREF(pInterface);
902 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
903
904 int rc = snd_pcm_drop(pStreamALSA->hPCM);
905 if (rc >= 0)
906 rc = VINF_SUCCESS;
907 else
908 {
909 LogRel(("ALSA: Error stopping stream '%s': %s (%d)\n", pStreamALSA->Cfg.szName, snd_strerror(rc), rc));
910 rc = RTErrConvertFromErrno(-rc);
911 }
912 LogFlowFunc(("returns %Rrc (state %s)\n", rc, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
913 return rc;
914}
915
916
917/**
918 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPause}
919 */
920static DECLCALLBACK(int) drvHstAudAlsaHA_StreamPause(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
921{
922 /* Same as disable. */
923 /** @todo r=bird: Try use pause and fallback on disable/enable if it isn't
924 * supported or doesn't work. */
925 return drvHstAudAlsaHA_StreamDisable(pInterface, pStream);
926}
927
928
929/**
930 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamResume}
931 */
932static DECLCALLBACK(int) drvHstAudAlsaHA_StreamResume(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
933{
934 /* Same as enable. */
935 return drvHstAudAlsaHA_StreamEnable(pInterface, pStream);
936}
937
938
939/**
940 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDrain}
941 */
942static DECLCALLBACK(int) drvHstAudAlsaHA_StreamDrain(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
943{
944 RT_NOREF(pInterface);
945 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
946
947 snd_pcm_state_t const enmState = snd_pcm_state(pStreamALSA->hPCM);
948 LogRelFlowFunc(("Stream '%s' input state: %s (%d)\n", pStreamALSA->Cfg.szName, snd_pcm_state_name(enmState), enmState));
949
950 /* Only for output streams. */
951 AssertReturn(pStreamALSA->Cfg.enmDir == PDMAUDIODIR_OUT, VERR_WRONG_ORDER);
952
953 int rc;
954 switch (enmState)
955 {
956 case SND_PCM_STATE_RUNNING:
957 case SND_PCM_STATE_PREPARED: /* not yet started */
958 {
959 /* Do not change to blocking here! */
960 rc = snd_pcm_drain(pStreamALSA->hPCM);
961 if (rc >= 0 || rc == -EAGAIN)
962 rc = VINF_SUCCESS;
963 else
964 {
965 snd_pcm_state_t const enmState2 = snd_pcm_state(pStreamALSA->hPCM);
966 if (rc == -EPIPE && enmState2 == enmState)
967 {
968 /* Not entirely sure, but possibly an underrun, so just disable the stream. */
969 LogRel2(("ALSA: snd_pcm_drain failed with -EPIPE, stopping stream (%s)\n", pStreamALSA->Cfg.szName));
970 rc = snd_pcm_drop(pStreamALSA->hPCM);
971 if (rc >= 0)
972 rc = VINF_SUCCESS;
973 else
974 {
975 LogRel(("ALSA: Error draining/stopping stream '%s': %s (%d)\n", pStreamALSA->Cfg.szName, snd_strerror(rc), rc));
976 rc = RTErrConvertFromErrno(-rc);
977 }
978 }
979 else
980 {
981 LogRel(("ALSA: Error draining output of '%s': %s (%d; %s -> %s)\n", pStreamALSA->Cfg.szName, snd_strerror(rc),
982 rc, snd_pcm_state_name(enmState), snd_pcm_state_name(enmState2)));
983 rc = RTErrConvertFromErrno(-rc);
984 }
985 }
986 break;
987 }
988
989 default:
990 rc = VINF_SUCCESS;
991 break;
992 }
993 LogRelFlowFunc(("returns %Rrc (state %s)\n", rc, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
994 return rc;
995}
996
997
998/**
999 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetState}
1000 */
1001static DECLCALLBACK(PDMHOSTAUDIOSTREAMSTATE) drvHstAudAlsaHA_StreamGetState(PPDMIHOSTAUDIO pInterface,
1002 PPDMAUDIOBACKENDSTREAM pStream)
1003{
1004 RT_NOREF(pInterface);
1005 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1006 AssertPtrReturn(pStreamALSA, PDMHOSTAUDIOSTREAMSTATE_INVALID);
1007
1008 PDMHOSTAUDIOSTREAMSTATE enmStreamState = PDMHOSTAUDIOSTREAMSTATE_OKAY;
1009 snd_pcm_state_t enmAlsaState = snd_pcm_state(pStreamALSA->hPCM);
1010 if (enmAlsaState == SND_PCM_STATE_DRAINING)
1011 {
1012 /* We're operating in non-blocking mode, so we must (at least for a demux
1013 config) call snd_pcm_drain again to drive it forward. Otherwise we
1014 might be stuck in the drain state forever. */
1015 Log5Func(("Calling snd_pcm_drain again...\n"));
1016 snd_pcm_drain(pStreamALSA->hPCM);
1017 enmAlsaState = snd_pcm_state(pStreamALSA->hPCM);
1018 }
1019
1020 if (enmAlsaState == SND_PCM_STATE_DRAINING)
1021 enmStreamState = PDMHOSTAUDIOSTREAMSTATE_DRAINING;
1022#if (((SND_LIB_MAJOR) << 16) | ((SND_LIB_MAJOR) << 8) | (SND_LIB_SUBMINOR)) >= 0x10002 /* was added in 1.0.2 */
1023 else if (enmAlsaState == SND_PCM_STATE_DISCONNECTED)
1024 enmStreamState = PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING;
1025#endif
1026
1027 Log5Func(("Stream '%s': ALSA state=%s -> %s\n",
1028 pStreamALSA->Cfg.szName, snd_pcm_state_name(enmAlsaState), PDMHostAudioStreamStateGetName(enmStreamState) ));
1029 return enmStreamState;
1030}
1031
1032
1033/**
1034 * Returns the available audio frames queued.
1035 *
1036 * @returns VBox status code.
1037 * @param hPCM ALSA stream handle.
1038 * @param pcFramesAvail Where to store the available frames.
1039 */
1040static int alsaStreamGetAvail(snd_pcm_t *hPCM, snd_pcm_sframes_t *pcFramesAvail)
1041{
1042 AssertPtr(hPCM);
1043 AssertPtr(pcFramesAvail);
1044
1045 int rc;
1046 snd_pcm_sframes_t cFramesAvail = snd_pcm_avail_update(hPCM);
1047 if (cFramesAvail > 0)
1048 {
1049 LogFunc(("cFramesAvail=%ld\n", cFramesAvail));
1050 *pcFramesAvail = cFramesAvail;
1051 return VINF_SUCCESS;
1052 }
1053
1054 /*
1055 * We can maybe recover from an EPIPE...
1056 */
1057 if (cFramesAvail == -EPIPE)
1058 {
1059 rc = drvHstAudAlsaStreamRecover(hPCM);
1060 if (RT_SUCCESS(rc))
1061 {
1062 cFramesAvail = snd_pcm_avail_update(hPCM);
1063 if (cFramesAvail >= 0)
1064 {
1065 LogFunc(("cFramesAvail=%ld\n", cFramesAvail));
1066 *pcFramesAvail = cFramesAvail;
1067 return VINF_SUCCESS;
1068 }
1069 }
1070 else
1071 {
1072 *pcFramesAvail = 0;
1073 return rc;
1074 }
1075 }
1076
1077 rc = RTErrConvertFromErrno(-(int)cFramesAvail);
1078 LogFunc(("failed - cFramesAvail=%ld rc=%Rrc\n", cFramesAvail, rc));
1079 *pcFramesAvail = 0;
1080 return rc;
1081}
1082
1083
1084/**
1085 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetPending}
1086 */
1087static DECLCALLBACK(uint32_t) drvHstAudAlsaHA_StreamGetPending(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1088{
1089 RT_NOREF(pInterface);
1090 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1091 AssertPtrReturn(pStreamALSA, 0);
1092
1093 /*
1094 * This is only relevant to output streams (input streams can't have
1095 * any pending, unplayed data).
1096 */
1097 uint32_t cbPending = 0;
1098 if (pStreamALSA->Cfg.enmDir == PDMAUDIODIR_OUT)
1099 {
1100 /*
1101 * Getting the delay (in audio frames) reports the time it will take
1102 * to hear a new sample after all queued samples have been played out.
1103 *
1104 * We use snd_pcm_avail_delay instead of snd_pcm_delay here as it will
1105 * update the buffer positions, and we can use the extra value against
1106 * the buffer size to double check since the delay value may include
1107 * fixed built-in delays in the processing chain and hardware.
1108 */
1109 snd_pcm_sframes_t cFramesAvail = 0;
1110 snd_pcm_sframes_t cFramesDelay = 0;
1111 int rc = snd_pcm_avail_delay(pStreamALSA->hPCM, &cFramesAvail, &cFramesDelay);
1112
1113 /*
1114 * We now also get the state as the pending value should be zero when
1115 * we're not in a playing state.
1116 */
1117 snd_pcm_state_t enmState = snd_pcm_state(pStreamALSA->hPCM);
1118 switch (enmState)
1119 {
1120 case SND_PCM_STATE_RUNNING:
1121 case SND_PCM_STATE_DRAINING:
1122 if (rc >= 0)
1123 {
1124 if ((uint32_t)cFramesAvail >= pStreamALSA->Cfg.Backend.cFramesBufferSize)
1125 cbPending = 0;
1126 else
1127 cbPending = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cFramesDelay);
1128 }
1129 break;
1130
1131 default:
1132 break;
1133 }
1134 Log2Func(("returns %u (%#x) - cFramesBufferSize=%RU32 cFramesAvail=%ld cFramesDelay=%ld rc=%d; enmState=%s (%d) \n",
1135 cbPending, cbPending, pStreamALSA->Cfg.Backend.cFramesBufferSize, cFramesAvail, cFramesDelay, rc,
1136 snd_pcm_state_name(enmState), enmState));
1137 }
1138 return cbPending;
1139}
1140
1141
1142/**
1143 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
1144 */
1145static DECLCALLBACK(uint32_t) drvHstAudAlsaHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1146{
1147 RT_NOREF(pInterface);
1148 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1149
1150 uint32_t cbAvail = 0;
1151 snd_pcm_sframes_t cFramesAvail = 0;
1152 int rc = alsaStreamGetAvail(pStreamALSA->hPCM, &cFramesAvail);
1153 if (RT_SUCCESS(rc))
1154 cbAvail = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cFramesAvail);
1155
1156 return cbAvail;
1157}
1158
1159
1160/**
1161 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
1162 */
1163static DECLCALLBACK(int) drvHstAudAlsaHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
1164 const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
1165{
1166 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1167 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1168 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1169 AssertPtrReturn(pcbWritten, VERR_INVALID_POINTER);
1170 Log4Func(("@%#RX64: pvBuf=%p cbBuf=%#x (%u) state=%s - %s\n", pStreamALSA->offInternal, pvBuf, cbBuf, cbBuf,
1171 snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM)), pStreamALSA->Cfg.szName));
1172 if (cbBuf)
1173 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1174 else
1175 {
1176 /* Fend off draining calls. */
1177 *pcbWritten = 0;
1178 return VINF_SUCCESS;
1179 }
1180
1181 /*
1182 * Determine how much we can write (caller actually did this
1183 * already, but we repeat it just to be sure or something).
1184 */
1185 snd_pcm_sframes_t cFramesAvail;
1186 int rc = alsaStreamGetAvail(pStreamALSA->hPCM, &cFramesAvail);
1187 if (RT_SUCCESS(rc))
1188 {
1189 Assert(cFramesAvail);
1190 if (cFramesAvail)
1191 {
1192 PCPDMAUDIOPCMPROPS pProps = &pStreamALSA->Cfg.Props;
1193 uint32_t cbToWrite = PDMAudioPropsFramesToBytes(pProps, (uint32_t)cFramesAvail);
1194 if (cbToWrite)
1195 {
1196 if (cbToWrite > cbBuf)
1197 cbToWrite = cbBuf;
1198
1199 /*
1200 * Try write the data.
1201 */
1202 uint32_t cFramesToWrite = PDMAudioPropsBytesToFrames(pProps, cbToWrite);
1203 snd_pcm_sframes_t cFramesWritten = snd_pcm_writei(pStreamALSA->hPCM, pvBuf, cFramesToWrite);
1204 if (cFramesWritten > 0)
1205 {
1206 Log4Func(("snd_pcm_writei w/ cbToWrite=%u -> %ld (frames) [cFramesAvail=%ld]\n",
1207 cbToWrite, cFramesWritten, cFramesAvail));
1208 *pcbWritten = PDMAudioPropsFramesToBytes(pProps, cFramesWritten);
1209 pStreamALSA->offInternal += *pcbWritten;
1210 return VINF_SUCCESS;
1211 }
1212 LogFunc(("snd_pcm_writei w/ cbToWrite=%u -> %ld [cFramesAvail=%ld]\n", cbToWrite, cFramesWritten, cFramesAvail));
1213
1214
1215 /*
1216 * There are a couple of error we can recover from, try to do so.
1217 * Only don't try too many times.
1218 */
1219 for (unsigned iTry = 0;
1220 (cFramesWritten == -EPIPE || cFramesWritten == -ESTRPIPE) && iTry < ALSA_RECOVERY_TRIES_MAX;
1221 iTry++)
1222 {
1223 if (cFramesWritten == -EPIPE)
1224 {
1225 /* Underrun occurred. */
1226 rc = drvHstAudAlsaStreamRecover(pStreamALSA->hPCM);
1227 if (RT_FAILURE(rc))
1228 break;
1229 LogFlowFunc(("Recovered from playback (iTry=%u)\n", iTry));
1230 }
1231 else
1232 {
1233 /* An suspended event occurred, needs resuming. */
1234 rc = drvHstAudAlsaStreamResume(pStreamALSA->hPCM);
1235 if (RT_FAILURE(rc))
1236 {
1237 LogRel(("ALSA: Failed to resume output stream (iTry=%u, rc=%Rrc)\n", iTry, rc));
1238 break;
1239 }
1240 LogFlowFunc(("Resumed suspended output stream (iTry=%u)\n", iTry));
1241 }
1242
1243 cFramesWritten = snd_pcm_writei(pStreamALSA->hPCM, pvBuf, cFramesToWrite);
1244 if (cFramesWritten > 0)
1245 {
1246 Log4Func(("snd_pcm_writei w/ cbToWrite=%u -> %ld (frames) [cFramesAvail=%ld]\n",
1247 cbToWrite, cFramesWritten, cFramesAvail));
1248 *pcbWritten = PDMAudioPropsFramesToBytes(pProps, cFramesWritten);
1249 pStreamALSA->offInternal += *pcbWritten;
1250 return VINF_SUCCESS;
1251 }
1252 LogFunc(("snd_pcm_writei w/ cbToWrite=%u -> %ld [cFramesAvail=%ld, iTry=%d]\n", cbToWrite, cFramesWritten, cFramesAvail, iTry));
1253 }
1254
1255 /* Make sure we return with an error status. */
1256 if (RT_SUCCESS_NP(rc))
1257 {
1258 if (cFramesWritten == 0)
1259 rc = VERR_ACCESS_DENIED;
1260 else
1261 {
1262 rc = RTErrConvertFromErrno(-(int)cFramesWritten);
1263 LogFunc(("Failed to write %RU32 bytes: %ld (%Rrc)\n", cbToWrite, cFramesWritten, rc));
1264 }
1265 }
1266 }
1267 }
1268 }
1269 else
1270 LogFunc(("Error getting number of playback frames, rc=%Rrc\n", rc));
1271 *pcbWritten = 0;
1272 return rc;
1273}
1274
1275
1276/**
1277 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
1278 */
1279static DECLCALLBACK(uint32_t) drvHstAudAlsaHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1280{
1281 RT_NOREF(pInterface);
1282 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1283
1284 uint32_t cbAvail = 0;
1285 snd_pcm_sframes_t cFramesAvail = 0;
1286 int rc = alsaStreamGetAvail(pStreamALSA->hPCM, &cFramesAvail);
1287 if (RT_SUCCESS(rc))
1288 cbAvail = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cFramesAvail);
1289
1290 return cbAvail;
1291}
1292
1293
1294/**
1295 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
1296 */
1297static DECLCALLBACK(int) drvHstAudAlsaHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
1298 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
1299{
1300 RT_NOREF_PV(pInterface);
1301 PDRVHSTAUDALSASTREAM pStreamALSA = (PDRVHSTAUDALSASTREAM)pStream;
1302 AssertPtrReturn(pStreamALSA, VERR_INVALID_POINTER);
1303 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1304 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1305 AssertPtrReturn(pcbRead, VERR_INVALID_POINTER);
1306 Log4Func(("@%#RX64: pvBuf=%p cbBuf=%#x (%u) state=%s - %s\n", pStreamALSA->offInternal, pvBuf, cbBuf, cbBuf,
1307 snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM)), pStreamALSA->Cfg.szName));
1308
1309 /*
1310 * Figure out how much we can read without trouble (we're doing
1311 * non-blocking reads, but whatever).
1312 */
1313 snd_pcm_sframes_t cAvail;
1314 int rc = alsaStreamGetAvail(pStreamALSA->hPCM, &cAvail);
1315 if (RT_SUCCESS(rc))
1316 {
1317 if (!cAvail) /* No data yet? */
1318 {
1319 snd_pcm_state_t enmState = snd_pcm_state(pStreamALSA->hPCM);
1320 switch (enmState)
1321 {
1322 case SND_PCM_STATE_PREPARED:
1323 /** @todo r=bird: explain the logic here... */
1324 cAvail = PDMAudioPropsBytesToFrames(&pStreamALSA->Cfg.Props, cbBuf);
1325 break;
1326
1327 case SND_PCM_STATE_SUSPENDED:
1328 rc = drvHstAudAlsaStreamResume(pStreamALSA->hPCM);
1329 if (RT_SUCCESS(rc))
1330 {
1331 LogFlowFunc(("Resumed suspended input stream.\n"));
1332 break;
1333 }
1334 LogFunc(("Failed resuming suspended input stream: %Rrc\n", rc));
1335 return rc;
1336
1337 default:
1338 LogFlow(("No frames available: state=%s (%d)\n", snd_pcm_state_name(enmState), enmState));
1339 break;
1340 }
1341 if (!cAvail)
1342 {
1343 *pcbRead = 0;
1344 return VINF_SUCCESS;
1345 }
1346 }
1347 }
1348 else
1349 {
1350 LogFunc(("Error getting number of captured frames, rc=%Rrc\n", rc));
1351 return rc;
1352 }
1353
1354 size_t cbToRead = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cAvail);
1355 cbToRead = RT_MIN(cbToRead, cbBuf);
1356 LogFlowFunc(("cbToRead=%zu, cAvail=%RI32\n", cbToRead, cAvail));
1357
1358 /*
1359 * Read loop.
1360 */
1361 uint32_t cbReadTotal = 0;
1362 while (cbToRead > 0)
1363 {
1364 /*
1365 * Do the reading.
1366 */
1367 snd_pcm_uframes_t const cFramesToRead = PDMAudioPropsBytesToFrames(&pStreamALSA->Cfg.Props, cbToRead);
1368 AssertBreakStmt(cFramesToRead > 0, rc = VERR_NO_DATA);
1369
1370 snd_pcm_sframes_t cFramesRead = snd_pcm_readi(pStreamALSA->hPCM, pvBuf, cFramesToRead);
1371 if (cFramesRead > 0)
1372 {
1373 /*
1374 * We should not run into a full mixer buffer or we lose samples and
1375 * run into an endless loop if ALSA keeps producing samples ("null"
1376 * capture device for example).
1377 */
1378 uint32_t const cbRead = PDMAudioPropsFramesToBytes(&pStreamALSA->Cfg.Props, cFramesRead);
1379 Assert(cbRead <= cbToRead);
1380
1381 cbToRead -= cbRead;
1382 cbReadTotal += cbRead;
1383 pvBuf = (uint8_t *)pvBuf + cbRead;
1384 pStreamALSA->offInternal += cbRead;
1385 }
1386 else
1387 {
1388 /*
1389 * Try recover from overrun and re-try.
1390 * Other conditions/errors we cannot and will just quit the loop.
1391 */
1392 if (cFramesRead == -EPIPE)
1393 {
1394 rc = drvHstAudAlsaStreamRecover(pStreamALSA->hPCM);
1395 if (RT_SUCCESS(rc))
1396 {
1397 LogFlowFunc(("Successfully recovered from overrun\n"));
1398 continue;
1399 }
1400 LogFunc(("Failed to recover from overrun: %Rrc\n", rc));
1401 }
1402 else if (cFramesRead == -EAGAIN)
1403 LogFunc(("No input frames available (EAGAIN)\n"));
1404 else if (cFramesRead == 0)
1405 LogFunc(("No input frames available (0)\n"));
1406 else
1407 {
1408 rc = RTErrConvertFromErrno(-(int)cFramesRead);
1409 LogFunc(("Failed to read input frames: %s (%ld, %Rrc)\n", snd_strerror(cFramesRead), cFramesRead, rc));
1410 }
1411
1412 /* If we've read anything, suppress the error. */
1413 if (RT_FAILURE(rc) && cbReadTotal > 0)
1414 {
1415 LogFunc(("Suppressing %Rrc because %#x bytes has been read already\n", rc, cbReadTotal));
1416 rc = VINF_SUCCESS;
1417 }
1418 break;
1419 }
1420 }
1421
1422 LogFlowFunc(("returns %Rrc and %#x (%d) bytes (%u bytes left); state %s\n",
1423 rc, cbReadTotal, cbReadTotal, cbToRead, snd_pcm_state_name(snd_pcm_state(pStreamALSA->hPCM))));
1424 *pcbRead = cbReadTotal;
1425 return rc;
1426}
1427
1428
1429/*********************************************************************************************************************************
1430* PDMIBASE *
1431*********************************************************************************************************************************/
1432
1433/**
1434 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1435 */
1436static DECLCALLBACK(void *) drvHstAudAlsaQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1437{
1438 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1439 PDRVHSTAUDALSA pThis = PDMINS_2_DATA(pDrvIns, PDRVHSTAUDALSA);
1440 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1441 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
1442 return NULL;
1443}
1444
1445
1446/*********************************************************************************************************************************
1447* PDMDRVREG *
1448*********************************************************************************************************************************/
1449
1450/**
1451 * @interface_method_impl{PDMDRVREG,pfnDestruct,
1452 * Destructs an ALSA host audio driver instance.}
1453 */
1454static DECLCALLBACK(void) drvHstAudAlsaDestruct(PPDMDRVINS pDrvIns)
1455{
1456 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1457 PDRVHSTAUDALSA pThis = PDMINS_2_DATA(pDrvIns, PDRVHSTAUDALSA);
1458 LogFlowFuncEnter();
1459
1460 if (RTCritSectIsInitialized(&pThis->CritSect))
1461 {
1462 RTCritSectEnter(&pThis->CritSect);
1463 pThis->pIHostAudioPort = NULL;
1464 RTCritSectLeave(&pThis->CritSect);
1465 RTCritSectDelete(&pThis->CritSect);
1466 }
1467
1468 LogFlowFuncLeave();
1469}
1470
1471
1472/**
1473 * @interface_method_impl{PDMDRVREG,pfnConstruct,
1474 * Construct an ALSA host audio driver instance.}
1475 */
1476static DECLCALLBACK(int) drvHstAudAlsaConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1477{
1478 RT_NOREF(fFlags);
1479 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1480 PDRVHSTAUDALSA pThis = PDMINS_2_DATA(pDrvIns, PDRVHSTAUDALSA);
1481 PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
1482 LogRel(("Audio: Initializing ALSA driver\n"));
1483
1484 /*
1485 * Init the static parts.
1486 */
1487 pThis->pDrvIns = pDrvIns;
1488 int rc = RTCritSectInit(&pThis->CritSect);
1489 AssertRCReturn(rc, rc);
1490 /* IBase */
1491 pDrvIns->IBase.pfnQueryInterface = drvHstAudAlsaQueryInterface;
1492 /* IHostAudio */
1493 pThis->IHostAudio.pfnGetConfig = drvHstAudAlsaHA_GetConfig;
1494 pThis->IHostAudio.pfnGetDevices = drvHstAudAlsaHA_GetDevices;
1495 pThis->IHostAudio.pfnSetDevice = drvHstAudAlsaHA_SetDevice;
1496 pThis->IHostAudio.pfnGetStatus = drvHstAudAlsaHA_GetStatus;
1497 pThis->IHostAudio.pfnDoOnWorkerThread = NULL;
1498 pThis->IHostAudio.pfnStreamConfigHint = NULL;
1499 pThis->IHostAudio.pfnStreamCreate = drvHstAudAlsaHA_StreamCreate;
1500 pThis->IHostAudio.pfnStreamInitAsync = NULL;
1501 pThis->IHostAudio.pfnStreamDestroy = drvHstAudAlsaHA_StreamDestroy;
1502 pThis->IHostAudio.pfnStreamNotifyDeviceChanged = NULL;
1503 pThis->IHostAudio.pfnStreamEnable = drvHstAudAlsaHA_StreamEnable;
1504 pThis->IHostAudio.pfnStreamDisable = drvHstAudAlsaHA_StreamDisable;
1505 pThis->IHostAudio.pfnStreamPause = drvHstAudAlsaHA_StreamPause;
1506 pThis->IHostAudio.pfnStreamResume = drvHstAudAlsaHA_StreamResume;
1507 pThis->IHostAudio.pfnStreamDrain = drvHstAudAlsaHA_StreamDrain;
1508 pThis->IHostAudio.pfnStreamGetPending = drvHstAudAlsaHA_StreamGetPending;
1509 pThis->IHostAudio.pfnStreamGetState = drvHstAudAlsaHA_StreamGetState;
1510 pThis->IHostAudio.pfnStreamGetWritable = drvHstAudAlsaHA_StreamGetWritable;
1511 pThis->IHostAudio.pfnStreamPlay = drvHstAudAlsaHA_StreamPlay;
1512 pThis->IHostAudio.pfnStreamGetReadable = drvHstAudAlsaHA_StreamGetReadable;
1513 pThis->IHostAudio.pfnStreamCapture = drvHstAudAlsaHA_StreamCapture;
1514
1515 /*
1516 * Read configuration.
1517 */
1518 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "OutputDeviceID|InputDeviceID", "");
1519
1520 rc = pHlp->pfnCFGMQueryStringDef(pCfg, "InputDeviceID", pThis->szInputDev, sizeof(pThis->szInputDev), "default");
1521 AssertRCReturn(rc, rc);
1522 rc = pHlp->pfnCFGMQueryStringDef(pCfg, "OutputDeviceID", pThis->szOutputDev, sizeof(pThis->szOutputDev), "default");
1523 AssertRCReturn(rc, rc);
1524
1525 /*
1526 * Init the alsa library.
1527 */
1528 rc = audioLoadAlsaLib();
1529 if (RT_FAILURE(rc))
1530 {
1531 LogRel(("ALSA: Failed to load the ALSA shared library: %Rrc\n", rc));
1532 return rc;
1533 }
1534
1535 /*
1536 * Query the notification interface from the driver/device above us.
1537 */
1538 pThis->pIHostAudioPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIHOSTAUDIOPORT);
1539 AssertReturn(pThis->pIHostAudioPort, VERR_PDM_MISSING_INTERFACE_ABOVE);
1540
1541#ifdef DEBUG
1542 /*
1543 * Some debug stuff we don't use for anything at all.
1544 */
1545 snd_lib_error_set_handler(drvHstAudAlsaDbgErrorHandler);
1546#endif
1547 return VINF_SUCCESS;
1548}
1549
1550
1551/**
1552 * ALSA audio driver registration record.
1553 */
1554const PDMDRVREG g_DrvHostALSAAudio =
1555{
1556 /* u32Version */
1557 PDM_DRVREG_VERSION,
1558 /* szName */
1559 "ALSAAudio",
1560 /* szRCMod */
1561 "",
1562 /* szR0Mod */
1563 "",
1564 /* pszDescription */
1565 "ALSA host audio driver",
1566 /* fFlags */
1567 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1568 /* fClass. */
1569 PDM_DRVREG_CLASS_AUDIO,
1570 /* cMaxInstances */
1571 ~0U,
1572 /* cbInstance */
1573 sizeof(DRVHSTAUDALSA),
1574 /* pfnConstruct */
1575 drvHstAudAlsaConstruct,
1576 /* pfnDestruct */
1577 drvHstAudAlsaDestruct,
1578 /* pfnRelocate */
1579 NULL,
1580 /* pfnIOCtl */
1581 NULL,
1582 /* pfnPowerOn */
1583 NULL,
1584 /* pfnReset */
1585 NULL,
1586 /* pfnSuspend */
1587 NULL,
1588 /* pfnResume */
1589 NULL,
1590 /* pfnAttach */
1591 NULL,
1592 /* pfnDetach */
1593 NULL,
1594 /* pfnPowerOff */
1595 NULL,
1596 /* pfnSoftReset */
1597 NULL,
1598 /* u32EndVersion */
1599 PDM_DRVREG_VERSION
1600};
1601
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