VirtualBox

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

Last change on this file since 91999 was 91861, checked in by vboxsync, 3 years ago

Devices/Audio: Change audio drivers to access the CFGM API through the driver helper callback table only, bugref:10074

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