VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostPulseAudio.cpp@ 75802

Last change on this file since 75802 was 73840, checked in by vboxsync, 6 years ago

Audio/DrvHostPulseAudio: Fixed input stream creation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.4 KB
Line 
1/* $Id: DrvHostPulseAudio.cpp 73840 2018-08-22 16:48:52Z vboxsync $ */
2/** @file
3 * VBox audio devices: Pulse Audio audio driver.
4 */
5
6/*
7 * Copyright (C) 2006-2018 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
23#include <VBox/log.h>
24#include <VBox/vmm/pdmaudioifs.h>
25
26#include <stdio.h>
27
28#include <iprt/alloc.h>
29#include <iprt/mem.h>
30#include <iprt/uuid.h>
31
32RT_C_DECLS_BEGIN
33 #include "pulse_mangling.h"
34 #include "pulse_stubs.h"
35RT_C_DECLS_END
36
37#include <pulse/pulseaudio.h>
38
39#include "DrvAudio.h"
40#include "VBoxDD.h"
41
42
43/*********************************************************************************************************************************
44* Defines *
45*********************************************************************************************************************************/
46#define VBOX_PULSEAUDIO_MAX_LOG_REL_ERRORS 32 /** @todo Make this configurable thru driver options. */
47
48#ifndef PA_STREAM_NOFLAGS
49# define PA_STREAM_NOFLAGS (pa_context_flags_t)0x0000U /* since 0.9.19 */
50#endif
51
52#ifndef PA_CONTEXT_NOFLAGS
53# define PA_CONTEXT_NOFLAGS (pa_context_flags_t)0x0000U /* since 0.9.19 */
54#endif
55
56/** No flags specified. */
57#define PULSEAUDIOENUMCBFLAGS_NONE 0
58/** (Release) log found devices. */
59#define PULSEAUDIOENUMCBFLAGS_LOG RT_BIT(0)
60
61/** Makes DRVHOSTPULSEAUDIO out of PDMIHOSTAUDIO. */
62#define PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface) \
63 ( (PDRVHOSTPULSEAUDIO)((uintptr_t)pInterface - RT_UOFFSETOF(DRVHOSTPULSEAUDIO, IHostAudio)) )
64
65
66/*********************************************************************************************************************************
67* Structures *
68*********************************************************************************************************************************/
69
70/**
71 * Host Pulse audio driver instance data.
72 * @implements PDMIAUDIOCONNECTOR
73 */
74typedef struct DRVHOSTPULSEAUDIO
75{
76 /** Pointer to the driver instance structure. */
77 PPDMDRVINS pDrvIns;
78 /** Pointer to PulseAudio's threaded main loop. */
79 pa_threaded_mainloop *pMainLoop;
80 /**
81 * Pointer to our PulseAudio context.
82 * Note: We use a pMainLoop in a separate thread (pContext).
83 * So either use callback functions or protect these functions
84 * by pa_threaded_mainloop_lock() / pa_threaded_mainloop_unlock().
85 */
86 pa_context *pContext;
87 /** Shutdown indicator. */
88 volatile bool fAbortLoop;
89 /** Enumeration operation successful? */
90 volatile bool fEnumOpSuccess;
91 /** Pointer to host audio interface. */
92 PDMIHOSTAUDIO IHostAudio;
93 /** Error count for not flooding the release log.
94 * Specify UINT32_MAX for unlimited logging. */
95 uint32_t cLogErrors;
96 /** The stream (base) name; needed for distinguishing
97 * streams in the PulseAudio mixer controls if multiple
98 * VMs are running at the same time. */
99 char szStreamName[64];
100} DRVHOSTPULSEAUDIO, *PDRVHOSTPULSEAUDIO;
101
102typedef struct PULSEAUDIOSTREAM
103{
104 /** The stream's acquired configuration. */
105 PPDMAUDIOSTREAMCFG pCfg;
106 /** Pointer to driver instance. */
107 PDRVHOSTPULSEAUDIO pDrv;
108 /** Pointer to opaque PulseAudio stream. */
109 pa_stream *pStream;
110 /** Pulse sample format and attribute specification. */
111 pa_sample_spec SampleSpec;
112 /** Pulse playback and buffer metrics. */
113 pa_buffer_attr BufAttr;
114 int fOpSuccess;
115 /** Pointer to Pulse sample peeking buffer. */
116 const uint8_t *pu8PeekBuf;
117 /** Current size (in bytes) of peeking data in
118 * buffer. */
119 size_t cbPeekBuf;
120 /** Our offset (in bytes) in peeking buffer. */
121 size_t offPeekBuf;
122 pa_operation *pDrainOp;
123 /** Number of occurred audio data underflows. */
124 uint32_t cUnderflows;
125 /** Current latency (in us). */
126 uint64_t curLatencyUs;
127#ifdef LOG_ENABLED
128 /** Start time stamp (in us) of stream playback / recording. */
129 pa_usec_t tsStartUs;
130 /** Time stamp (in us) when last read from / written to the stream. */
131 pa_usec_t tsLastReadWrittenUs;
132#endif
133} PULSEAUDIOSTREAM, *PPULSEAUDIOSTREAM;
134
135/**
136 * Callback context for server enumeration callbacks.
137 */
138typedef struct PULSEAUDIOENUMCBCTX
139{
140 /** Pointer to host backend driver. */
141 PDRVHOSTPULSEAUDIO pDrv;
142 /** Enumeration flags. */
143 uint32_t fFlags;
144 /** Number of found input devices. */
145 uint8_t cDevIn;
146 /** Number of found output devices. */
147 uint8_t cDevOut;
148 /** Name of default sink being used. Must be free'd using RTStrFree(). */
149 char *pszDefaultSink;
150 /** Name of default source being used. Must be free'd using RTStrFree(). */
151 char *pszDefaultSource;
152} PULSEAUDIOENUMCBCTX, *PPULSEAUDIOENUMCBCTX;
153
154#ifndef PA_CONTEXT_IS_GOOD /* To allow running on systems with PulseAudio < 0.9.11. */
155static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x) {
156 return
157 x == PA_CONTEXT_CONNECTING ||
158 x == PA_CONTEXT_AUTHORIZING ||
159 x == PA_CONTEXT_SETTING_NAME ||
160 x == PA_CONTEXT_READY;
161}
162#endif /* !PA_CONTEXT_IS_GOOD */
163
164#ifndef PA_STREAM_IS_GOOD /* To allow running on systems with PulseAudio < 0.9.11. */
165static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x) {
166 return
167 x == PA_STREAM_CREATING ||
168 x == PA_STREAM_READY;
169}
170#endif /* !PA_STREAM_IS_GOOD */
171
172
173/*********************************************************************************************************************************
174* Prototypes *
175*********************************************************************************************************************************/
176
177static int paEnumerate(PDRVHOSTPULSEAUDIO pThis, PPDMAUDIOBACKENDCFG pCfg, uint32_t fEnum);
178static int paError(PDRVHOSTPULSEAUDIO pThis, const char *szMsg);
179#ifdef DEBUG
180static void paStreamCbUnderflow(pa_stream *pStream, void *pvContext);
181static void paStreamCbReqWrite(pa_stream *pStream, size_t cbLen, void *pvContext);
182#endif
183static void paStreamCbSuccess(pa_stream *pStream, int fSuccess, void *pvContext);
184
185
186/**
187 * Signal the main loop to abort. Just signalling isn't sufficient as the
188 * mainloop might not have been entered yet.
189 */
190static void paSignalWaiter(PDRVHOSTPULSEAUDIO pThis)
191{
192 if (!pThis)
193 return;
194
195 pThis->fAbortLoop = true;
196 pa_threaded_mainloop_signal(pThis->pMainLoop, 0);
197}
198
199
200static pa_sample_format_t paAudioPropsToPulse(PPDMAUDIOPCMPROPS pProps)
201{
202 switch (pProps->cBytes)
203 {
204 case 1:
205 if (!pProps->fSigned)
206 return PA_SAMPLE_U8;
207 break;
208
209 case 2:
210 if (pProps->fSigned)
211 return PA_SAMPLE_S16LE;
212 break;
213
214#ifdef PA_SAMPLE_S32LE
215 case 4:
216 if (pProps->fSigned)
217 return PA_SAMPLE_S32LE;
218 break;
219#endif
220
221 default:
222 break;
223 }
224
225 AssertMsgFailed(("%RU8%s not supported\n", pProps->cBytes, pProps->fSigned ? "S" : "U"));
226 return PA_SAMPLE_INVALID;
227}
228
229
230static int paPulseToAudioProps(pa_sample_format_t pulsefmt, PPDMAUDIOPCMPROPS pProps)
231{
232 switch (pulsefmt)
233 {
234 case PA_SAMPLE_U8:
235 pProps->cBytes = 1;
236 pProps->fSigned = false;
237 break;
238
239 case PA_SAMPLE_S16LE:
240 pProps->cBytes = 2;
241 pProps->fSigned = true;
242 break;
243
244 case PA_SAMPLE_S16BE:
245 pProps->cBytes = 2;
246 pProps->fSigned = true;
247 /** @todo Handle Endianess. */
248 break;
249
250#ifdef PA_SAMPLE_S32LE
251 case PA_SAMPLE_S32LE:
252 pProps->cBytes = 4;
253 pProps->fSigned = true;
254 break;
255#endif
256
257#ifdef PA_SAMPLE_S32BE
258 case PA_SAMPLE_S32BE:
259 pProps->cBytes = 4;
260 pProps->fSigned = true;
261 /** @todo Handle Endianess. */
262 break;
263#endif
264
265 default:
266 AssertLogRelMsgFailed(("PulseAudio: Format (%ld) not supported\n", pulsefmt));
267 return VERR_NOT_SUPPORTED;
268 }
269
270 return VINF_SUCCESS;
271}
272
273
274/**
275 * Synchronously wait until an operation completed.
276 */
277static int paWaitForEx(PDRVHOSTPULSEAUDIO pThis, pa_operation *pOP, RTMSINTERVAL cMsTimeout)
278{
279 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
280 AssertPtrReturn(pOP, VERR_INVALID_POINTER);
281
282 int rc = VINF_SUCCESS;
283
284 uint64_t u64StartMs = RTTimeMilliTS();
285 while (pa_operation_get_state(pOP) == PA_OPERATION_RUNNING)
286 {
287 if (!pThis->fAbortLoop)
288 {
289 AssertPtr(pThis->pMainLoop);
290 pa_threaded_mainloop_wait(pThis->pMainLoop);
291 if ( !pThis->pContext
292 || pa_context_get_state(pThis->pContext) != PA_CONTEXT_READY)
293 {
294 LogRel(("PulseAudio: pa_context_get_state context not ready\n"));
295 break;
296 }
297 }
298 pThis->fAbortLoop = false;
299
300 uint64_t u64ElapsedMs = RTTimeMilliTS() - u64StartMs;
301 if (u64ElapsedMs >= cMsTimeout)
302 {
303 rc = VERR_TIMEOUT;
304 break;
305 }
306 }
307
308 pa_operation_unref(pOP);
309
310 return rc;
311}
312
313
314static int paWaitFor(PDRVHOSTPULSEAUDIO pThis, pa_operation *pOP)
315{
316 return paWaitForEx(pThis, pOP, 10 * 1000 /* 10s timeout */);
317}
318
319
320/**
321 * Context status changed.
322 */
323static void paContextCbStateChanged(pa_context *pCtx, void *pvUser)
324{
325 AssertPtrReturnVoid(pCtx);
326
327 PDRVHOSTPULSEAUDIO pThis = (PDRVHOSTPULSEAUDIO)pvUser;
328 AssertPtrReturnVoid(pThis);
329
330 switch (pa_context_get_state(pCtx))
331 {
332 case PA_CONTEXT_READY:
333 case PA_CONTEXT_TERMINATED:
334 case PA_CONTEXT_FAILED:
335 paSignalWaiter(pThis);
336 break;
337
338 default:
339 break;
340 }
341}
342
343
344/**
345 * Callback called when our pa_stream_drain operation was completed.
346 */
347static void paStreamCbDrain(pa_stream *pStream, int fSuccess, void *pvUser)
348{
349 AssertPtrReturnVoid(pStream);
350
351 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pvUser;
352 AssertPtrReturnVoid(pStreamPA);
353
354 pStreamPA->fOpSuccess = fSuccess;
355 if (fSuccess)
356 {
357 pa_operation_unref(pa_stream_cork(pStream, 1,
358 paStreamCbSuccess, pvUser));
359 }
360 else
361 paError(pStreamPA->pDrv, "Failed to drain stream");
362
363 if (pStreamPA->pDrainOp)
364 {
365 pa_operation_unref(pStreamPA->pDrainOp);
366 pStreamPA->pDrainOp = NULL;
367 }
368}
369
370
371/**
372 * Stream status changed.
373 */
374static void paStreamCbStateChanged(pa_stream *pStream, void *pvUser)
375{
376 AssertPtrReturnVoid(pStream);
377
378 PDRVHOSTPULSEAUDIO pThis = (PDRVHOSTPULSEAUDIO)pvUser;
379 AssertPtrReturnVoid(pThis);
380
381 switch (pa_stream_get_state(pStream))
382 {
383 case PA_STREAM_READY:
384 case PA_STREAM_FAILED:
385 case PA_STREAM_TERMINATED:
386 paSignalWaiter(pThis);
387 break;
388
389 default:
390 break;
391 }
392}
393
394
395#ifdef DEBUG
396static void paStreamCbReqWrite(pa_stream *pStream, size_t cbLen, void *pvContext)
397{
398 RT_NOREF(cbLen, pvContext);
399
400 PPULSEAUDIOSTREAM pStrm = (PPULSEAUDIOSTREAM)pvContext;
401 AssertPtrReturnVoid(pStrm);
402
403 pa_usec_t usec = 0;
404 int neg = 0;
405 pa_stream_get_latency(pStream, &usec, &neg);
406
407 Log2Func(("Requested %zu bytes -- Current latency is %RU64ms\n", cbLen, usec / 1000));
408}
409
410
411static void paStreamCbUnderflow(pa_stream *pStream, void *pvContext)
412{
413 PPULSEAUDIOSTREAM pStrm = (PPULSEAUDIOSTREAM)pvContext;
414 AssertPtrReturnVoid(pStrm);
415
416 pStrm->cUnderflows++;
417
418 LogRel2(("PulseAudio: Warning: Hit underflow #%RU32\n", pStrm->cUnderflows));
419
420 if ( pStrm->cUnderflows >= 6 /** @todo Make this check configurable. */
421 && pStrm->curLatencyUs < 2000000 /* 2s */)
422 {
423 pStrm->curLatencyUs = (pStrm->curLatencyUs * 3) / 2;
424
425 LogRel2(("PulseAudio: Output latency increased to %RU64ms\n", pStrm->curLatencyUs / 1000 /* ms */));
426
427 pStrm->BufAttr.maxlength = pa_usec_to_bytes(pStrm->curLatencyUs, &pStrm->SampleSpec);
428 pStrm->BufAttr.tlength = pa_usec_to_bytes(pStrm->curLatencyUs, &pStrm->SampleSpec);
429
430 pa_stream_set_buffer_attr(pStream, &pStrm->BufAttr, NULL, NULL);
431
432 pStrm->cUnderflows = 0;
433 }
434
435# ifdef LOG_ENABLED
436 pa_usec_t curLatencyUs = 0;
437 pa_stream_get_latency(pStream, &curLatencyUs, NULL /* Neg */);
438
439 const pa_timing_info *pTInfo = pa_stream_get_timing_info(pStream);
440 const pa_sample_spec *pSpec = pa_stream_get_sample_spec(pStream);
441
442 pa_usec_t curPosWritesUs = pa_bytes_to_usec(pTInfo->write_index, pSpec);
443 pa_usec_t curTsUs = pa_rtclock_now() - pStrm->tsStartUs;
444
445 Log2Func(("curPosWrite=%RU64ms, curTs=%RU64ms, curDelta=%RI64ms, curLatency=%RU64ms\n",
446 curPosWritesUs / 1000, curTsUs / 1000, (((int64_t)curPosWritesUs - (int64_t)curTsUs) / 1000), curLatencyUs / 1000));
447# endif
448}
449
450
451static void paStreamCbOverflow(pa_stream *pStream, void *pvContext)
452{
453 RT_NOREF(pStream, pvContext);
454
455 Log2Func(("Warning: Hit overflow\n"));
456}
457#endif /* DEBUG */
458
459
460static void paStreamCbSuccess(pa_stream *pStream, int fSuccess, void *pvUser)
461{
462 AssertPtrReturnVoid(pStream);
463
464 PPULSEAUDIOSTREAM pStrm = (PPULSEAUDIOSTREAM)pvUser;
465 AssertPtrReturnVoid(pStrm);
466
467 pStrm->fOpSuccess = fSuccess;
468
469 if (fSuccess)
470 paSignalWaiter(pStrm->pDrv);
471 else
472 paError(pStrm->pDrv, "Failed to finish stream operation");
473}
474
475
476static int paStreamOpen(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA, bool fIn, const char *pszName)
477{
478 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
479 AssertPtrReturn(pStreamPA, VERR_INVALID_POINTER);
480 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
481
482 int rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
483
484 pa_stream *pStream = NULL;
485 uint32_t flags = PA_STREAM_NOFLAGS;
486
487 pa_threaded_mainloop_lock(pThis->pMainLoop);
488
489 do
490 {
491 pa_sample_spec *pSampleSpec = &pStreamPA->SampleSpec;
492
493 LogFunc(("Opening '%s', rate=%dHz, channels=%d, format=%s\n",
494 pszName, pSampleSpec->rate, pSampleSpec->channels,
495 pa_sample_format_to_string(pSampleSpec->format)));
496
497 if (!pa_sample_spec_valid(pSampleSpec))
498 {
499 LogRel(("PulseAudio: Unsupported sample specification for stream '%s'\n", pszName));
500 break;
501 }
502
503 pa_buffer_attr *pBufAttr = &pStreamPA->BufAttr;
504
505 /** @todo r=andy Use pa_stream_new_with_proplist instead. */
506 if (!(pStream = pa_stream_new(pThis->pContext, pszName, pSampleSpec, NULL /* pa_channel_map */)))
507 {
508 LogRel(("PulseAudio: Could not create stream '%s'\n", pszName));
509 rc = VERR_NO_MEMORY;
510 break;
511 }
512
513#ifdef DEBUG
514 pa_stream_set_write_callback (pStream, paStreamCbReqWrite, pStreamPA);
515 pa_stream_set_underflow_callback (pStream, paStreamCbUnderflow, pStreamPA);
516 if (!fIn) /* Only for output streams. */
517 pa_stream_set_overflow_callback(pStream, paStreamCbOverflow, pStreamPA);
518#endif
519 pa_stream_set_state_callback (pStream, paStreamCbStateChanged, pThis);
520
521#if PA_API_VERSION >= 12
522 /* XXX */
523 flags |= PA_STREAM_ADJUST_LATENCY;
524#endif
525 /* For using pa_stream_get_latency() and pa_stream_get_time(). */
526 flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
527
528 /* No input/output right away after the stream was started. */
529 flags |= PA_STREAM_START_CORKED;
530
531 if (fIn)
532 {
533 LogFunc(("Input stream attributes: maxlength=%d fragsize=%d\n",
534 pBufAttr->maxlength, pBufAttr->fragsize));
535
536 if (pa_stream_connect_record(pStream, /*dev=*/NULL, pBufAttr, (pa_stream_flags_t)flags) < 0)
537 {
538 LogRel(("PulseAudio: Could not connect input stream '%s': %s\n",
539 pszName, pa_strerror(pa_context_errno(pThis->pContext))));
540 break;
541 }
542 }
543 else
544 {
545 LogFunc(("Output buffer attributes: maxlength=%d tlength=%d prebuf=%d minreq=%d\n",
546 pBufAttr->maxlength, pBufAttr->tlength, pBufAttr->prebuf, pBufAttr->minreq));
547
548 if (pa_stream_connect_playback(pStream, /*dev=*/NULL, pBufAttr, (pa_stream_flags_t)flags,
549 /*cvolume=*/NULL, /*sync_stream=*/NULL) < 0)
550 {
551 LogRel(("PulseAudio: Could not connect playback stream '%s': %s\n",
552 pszName, pa_strerror(pa_context_errno(pThis->pContext))));
553 break;
554 }
555 }
556
557 /* Wait until the stream is ready. */
558 for (;;)
559 {
560 if (!pThis->fAbortLoop)
561 pa_threaded_mainloop_wait(pThis->pMainLoop);
562 pThis->fAbortLoop = false;
563
564 pa_stream_state_t streamSt = pa_stream_get_state(pStream);
565 if (streamSt == PA_STREAM_READY)
566 break;
567 else if ( streamSt == PA_STREAM_FAILED
568 || streamSt == PA_STREAM_TERMINATED)
569 {
570 LogRel(("PulseAudio: Failed to initialize stream '%s' (state %ld)\n", pszName, streamSt));
571 break;
572 }
573 }
574
575#ifdef LOG_ENABLED
576 pStreamPA->tsStartUs = pa_rtclock_now();
577#endif
578 const pa_buffer_attr *pBufAttrObtained = pa_stream_get_buffer_attr(pStream);
579 AssertPtr(pBufAttrObtained);
580 memcpy(pBufAttr, pBufAttrObtained, sizeof(pa_buffer_attr));
581
582 LogFunc(("Obtained %s buffer attributes: tLength=%RU32, maxLength=%RU32, minReq=%RU32, fragSize=%RU32, preBuf=%RU32\n",
583 fIn ? "capture" : "playback",
584 pBufAttr->tlength, pBufAttr->maxlength, pBufAttr->minreq, pBufAttr->fragsize, pBufAttr->prebuf));
585
586 pStreamPA->pStream = pStream;
587
588 rc = VINF_SUCCESS;
589
590 } while (0);
591
592 if ( RT_FAILURE(rc)
593 && pStream)
594 pa_stream_disconnect(pStream);
595
596 pa_threaded_mainloop_unlock(pThis->pMainLoop);
597
598 if (RT_FAILURE(rc))
599 {
600 if (pStream)
601 pa_stream_unref(pStream);
602 }
603
604 LogFlowFuncLeaveRC(rc);
605 return rc;
606}
607
608
609/**
610 * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
611 */
612static DECLCALLBACK(int) drvHostPulseAudioInit(PPDMIHOSTAUDIO pInterface)
613{
614 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
615
616 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
617
618 LogFlowFuncEnter();
619
620 int rc = audioLoadPulseLib();
621 if (RT_FAILURE(rc))
622 {
623 LogRel(("PulseAudio: Failed to load the PulseAudio shared library! Error %Rrc\n", rc));
624 return rc;
625 }
626
627 LogRel(("PulseAudio: Using v%s\n", pa_get_library_version()));
628
629 pThis->fAbortLoop = false;
630 pThis->pMainLoop = NULL;
631
632 bool fLocked = false;
633
634 do
635 {
636 if (!(pThis->pMainLoop = pa_threaded_mainloop_new()))
637 {
638 LogRel(("PulseAudio: Failed to allocate main loop: %s\n",
639 pa_strerror(pa_context_errno(pThis->pContext))));
640 rc = VERR_NO_MEMORY;
641 break;
642 }
643
644 if (!(pThis->pContext = pa_context_new(pa_threaded_mainloop_get_api(pThis->pMainLoop), "VirtualBox")))
645 {
646 LogRel(("PulseAudio: Failed to allocate context: %s\n",
647 pa_strerror(pa_context_errno(pThis->pContext))));
648 rc = VERR_NO_MEMORY;
649 break;
650 }
651
652 if (pa_threaded_mainloop_start(pThis->pMainLoop) < 0)
653 {
654 LogRel(("PulseAudio: Failed to start threaded mainloop: %s\n",
655 pa_strerror(pa_context_errno(pThis->pContext))));
656 break;
657 }
658
659 /* Install a global callback to known if something happens to our acquired context. */
660 pa_context_set_state_callback(pThis->pContext, paContextCbStateChanged, pThis /* pvUserData */);
661
662 pa_threaded_mainloop_lock(pThis->pMainLoop);
663 fLocked = true;
664
665 if (pa_context_connect(pThis->pContext, NULL /* pszServer */,
666 PA_CONTEXT_NOFLAGS, NULL) < 0)
667 {
668 LogRel(("PulseAudio: Failed to connect to server: %s\n",
669 pa_strerror(pa_context_errno(pThis->pContext))));
670 break;
671 }
672
673 /* Wait until the pThis->pContext is ready. */
674 for (;;)
675 {
676 if (!pThis->fAbortLoop)
677 pa_threaded_mainloop_wait(pThis->pMainLoop);
678 pThis->fAbortLoop = false;
679
680 pa_context_state_t cstate = pa_context_get_state(pThis->pContext);
681 if (cstate == PA_CONTEXT_READY)
682 break;
683 else if ( cstate == PA_CONTEXT_TERMINATED
684 || cstate == PA_CONTEXT_FAILED)
685 {
686 LogRel(("PulseAudio: Failed to initialize context (state %d)\n", cstate));
687 break;
688 }
689 }
690 }
691 while (0);
692
693 if (fLocked)
694 pa_threaded_mainloop_unlock(pThis->pMainLoop);
695
696 if (RT_FAILURE(rc))
697 {
698 if (pThis->pMainLoop)
699 pa_threaded_mainloop_stop(pThis->pMainLoop);
700
701 if (pThis->pContext)
702 {
703 pa_context_disconnect(pThis->pContext);
704 pa_context_unref(pThis->pContext);
705 pThis->pContext = NULL;
706 }
707
708 if (pThis->pMainLoop)
709 {
710 pa_threaded_mainloop_free(pThis->pMainLoop);
711 pThis->pMainLoop = NULL;
712 }
713 }
714
715 LogFlowFuncLeaveRC(rc);
716 return rc;
717}
718
719
720static int paCreateStreamOut(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA,
721 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
722{
723 pStreamPA->pDrainOp = NULL;
724
725 pStreamPA->SampleSpec.format = paAudioPropsToPulse(&pCfgReq->Props);
726 pStreamPA->SampleSpec.rate = pCfgReq->Props.uHz;
727 pStreamPA->SampleSpec.channels = pCfgReq->Props.cChannels;
728
729 pStreamPA->curLatencyUs = DrvAudioHlpFramesToMilli(pCfgReq->Backend.cfBufferSize, &pCfgReq->Props) * RT_US_1MS;
730
731 const uint32_t cbLatency = pa_usec_to_bytes(pStreamPA->curLatencyUs, &pStreamPA->SampleSpec);
732
733 LogRel2(("PulseAudio: Initial output latency is %RU64ms (%RU32 bytes)\n", pStreamPA->curLatencyUs / RT_US_1MS, cbLatency));
734
735 pStreamPA->BufAttr.tlength = cbLatency;
736 pStreamPA->BufAttr.maxlength = -1; /* Let the PulseAudio server choose the biggest size it can handle. */
737 pStreamPA->BufAttr.prebuf = cbLatency;
738 pStreamPA->BufAttr.minreq = DrvAudioHlpFramesToBytes(pCfgReq->Backend.cfPeriod, &pCfgReq->Props);
739
740 LogFunc(("Requested: BufAttr tlength=%RU32, maxLength=%RU32, minReq=%RU32\n",
741 pStreamPA->BufAttr.tlength, pStreamPA->BufAttr.maxlength, pStreamPA->BufAttr.minreq));
742
743 Assert(pCfgReq->enmDir == PDMAUDIODIR_OUT);
744
745 char szName[256];
746 RTStrPrintf2(szName, sizeof(szName), "VirtualBox %s [%s]",
747 DrvAudioHlpPlaybackDstToStr(pCfgReq->DestSource.Dest), pThis->szStreamName);
748
749 /* Note that the struct BufAttr is updated to the obtained values after this call! */
750 int rc = paStreamOpen(pThis, pStreamPA, false /* fIn */, szName);
751 if (RT_FAILURE(rc))
752 return rc;
753
754 rc = paPulseToAudioProps(pStreamPA->SampleSpec.format, &pCfgAcq->Props);
755 if (RT_FAILURE(rc))
756 {
757 LogRel(("PulseAudio: Cannot find audio output format %ld\n", pStreamPA->SampleSpec.format));
758 return rc;
759 }
760
761 pCfgAcq->Props.uHz = pStreamPA->SampleSpec.rate;
762 pCfgAcq->Props.cChannels = pStreamPA->SampleSpec.channels;
763 pCfgAcq->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfgAcq->Props.cBytes, pCfgAcq->Props.cChannels);
764
765 LogFunc(("Acquired: BufAttr tlength=%RU32, maxLength=%RU32, minReq=%RU32\n",
766 pStreamPA->BufAttr.tlength, pStreamPA->BufAttr.maxlength, pStreamPA->BufAttr.minreq));
767
768 pCfgAcq->Backend.cfPeriod = PDMAUDIOSTREAMCFG_B2F(pCfgAcq, pStreamPA->BufAttr.minreq);
769 pCfgAcq->Backend.cfBufferSize = PDMAUDIOSTREAMCFG_B2F(pCfgAcq, pStreamPA->BufAttr.tlength);
770 pCfgAcq->Backend.cfPreBuf = PDMAUDIOSTREAMCFG_B2F(pCfgAcq, pStreamPA->BufAttr.prebuf);
771
772 pStreamPA->pDrv = pThis;
773
774 return rc;
775}
776
777
778static int paCreateStreamIn(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA,
779 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
780{
781 pStreamPA->SampleSpec.format = paAudioPropsToPulse(&pCfgReq->Props);
782 pStreamPA->SampleSpec.rate = pCfgReq->Props.uHz;
783 pStreamPA->SampleSpec.channels = pCfgReq->Props.cChannels;
784
785 pStreamPA->BufAttr.fragsize = DrvAudioHlpFramesToBytes(pCfgReq->Backend.cfPeriod, &pCfgReq->Props);
786 pStreamPA->BufAttr.maxlength = -1; /* Let the PulseAudio server choose the biggest size it can handle. */
787
788 Assert(pCfgReq->enmDir == PDMAUDIODIR_IN);
789
790 char szName[256];
791 RTStrPrintf2(szName, sizeof(szName), "VirtualBox %s [%s]",
792 DrvAudioHlpRecSrcToStr(pCfgReq->DestSource.Source), pThis->szStreamName);
793
794 /* Note: Other members of BufAttr are ignored for record streams. */
795 int rc = paStreamOpen(pThis, pStreamPA, true /* fIn */, szName);
796 if (RT_FAILURE(rc))
797 return rc;
798
799 rc = paPulseToAudioProps(pStreamPA->SampleSpec.format, &pCfgAcq->Props);
800 if (RT_FAILURE(rc))
801 {
802 LogRel(("PulseAudio: Cannot find audio capture format %ld\n", pStreamPA->SampleSpec.format));
803 return rc;
804 }
805
806 pStreamPA->pDrv = pThis;
807 pStreamPA->pu8PeekBuf = NULL;
808
809 pCfgAcq->Props.uHz = pStreamPA->SampleSpec.rate;
810 pCfgAcq->Props.cChannels = pStreamPA->SampleSpec.channels;
811
812 pCfgAcq->Backend.cfPeriod = PDMAUDIOSTREAMCFG_B2F(pCfgAcq, pStreamPA->BufAttr.fragsize);
813 pCfgAcq->Backend.cfBufferSize = pCfgAcq->Backend.cfBufferSize;
814 pCfgAcq->Backend.cfPreBuf = pCfgAcq->Backend.cfPeriod;
815
816 LogFlowFuncLeaveRC(rc);
817 return rc;
818}
819
820
821/**
822 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
823 */
824static DECLCALLBACK(int) drvHostPulseAudioStreamCapture(PPDMIHOSTAUDIO pInterface,
825 PPDMAUDIOBACKENDSTREAM pStream, void *pvBuf, uint32_t cxBuf, uint32_t *pcxRead)
826{
827 RT_NOREF(pvBuf, cxBuf);
828 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
829 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
830 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
831 AssertReturn(cxBuf, VERR_INVALID_PARAMETER);
832 /* pcbRead is optional. */
833
834 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
835 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
836
837 /* We should only call pa_stream_readable_size() once and trust the first value. */
838 pa_threaded_mainloop_lock(pThis->pMainLoop);
839 size_t cbAvail = pa_stream_readable_size(pStreamPA->pStream);
840 pa_threaded_mainloop_unlock(pThis->pMainLoop);
841
842 if (cbAvail == (size_t)-1)
843 return paError(pStreamPA->pDrv, "Failed to determine input data size");
844
845 /* If the buffer was not dropped last call, add what remains. */
846 if (pStreamPA->pu8PeekBuf)
847 {
848 Assert(pStreamPA->cbPeekBuf >= pStreamPA->offPeekBuf);
849 cbAvail += (pStreamPA->cbPeekBuf - pStreamPA->offPeekBuf);
850 }
851
852 Log3Func(("cbAvail=%zu\n", cbAvail));
853
854 if (!cbAvail) /* No data? Bail out. */
855 {
856 if (pcxRead)
857 *pcxRead = 0;
858 return VINF_SUCCESS;
859 }
860
861 int rc = VINF_SUCCESS;
862
863 size_t cbToRead = RT_MIN(cbAvail, cxBuf);
864
865 Log3Func(("cbToRead=%zu, cbAvail=%zu, offPeekBuf=%zu, cbPeekBuf=%zu\n",
866 cbToRead, cbAvail, pStreamPA->offPeekBuf, pStreamPA->cbPeekBuf));
867
868 uint32_t cbReadTotal = 0;
869
870 while (cbToRead)
871 {
872 /* If there is no data, do another peek. */
873 if (!pStreamPA->pu8PeekBuf)
874 {
875 pa_threaded_mainloop_lock(pThis->pMainLoop);
876 pa_stream_peek(pStreamPA->pStream,
877 (const void**)&pStreamPA->pu8PeekBuf, &pStreamPA->cbPeekBuf);
878 pa_threaded_mainloop_unlock(pThis->pMainLoop);
879
880 pStreamPA->offPeekBuf = 0;
881
882 /* No data anymore?
883 * Note: If there's a data hole (cbPeekBuf then contains the length of the hole)
884 * we need to drop the stream lateron. */
885 if ( !pStreamPA->pu8PeekBuf
886 && !pStreamPA->cbPeekBuf)
887 {
888 break;
889 }
890 }
891
892 Assert(pStreamPA->cbPeekBuf >= pStreamPA->offPeekBuf);
893 size_t cbToWrite = RT_MIN(pStreamPA->cbPeekBuf - pStreamPA->offPeekBuf, cbToRead);
894
895 Log3Func(("cbToRead=%zu, cbToWrite=%zu, offPeekBuf=%zu, cbPeekBuf=%zu, pu8PeekBuf=%p\n",
896 cbToRead, cbToWrite,
897 pStreamPA->offPeekBuf, pStreamPA->cbPeekBuf, pStreamPA->pu8PeekBuf));
898
899 if ( cbToWrite
900 /* Only copy data if it's not a data hole (see above). */
901 && pStreamPA->pu8PeekBuf
902 && pStreamPA->cbPeekBuf)
903 {
904 memcpy((uint8_t *)pvBuf + cbReadTotal, pStreamPA->pu8PeekBuf + pStreamPA->offPeekBuf, cbToWrite);
905
906 Assert(cbToRead >= cbToWrite);
907 cbToRead -= cbToWrite;
908 cbReadTotal += cbToWrite;
909
910 pStreamPA->offPeekBuf += cbToWrite;
911 Assert(pStreamPA->offPeekBuf <= pStreamPA->cbPeekBuf);
912 }
913
914 if (/* Nothing to write anymore? Drop the buffer. */
915 !cbToWrite
916 /* Was there a hole in the peeking buffer? Drop it. */
917 || !pStreamPA->pu8PeekBuf
918 /* If the buffer is done, drop it. */
919 || pStreamPA->offPeekBuf == pStreamPA->cbPeekBuf)
920 {
921 pa_threaded_mainloop_lock(pThis->pMainLoop);
922 pa_stream_drop(pStreamPA->pStream);
923 pa_threaded_mainloop_unlock(pThis->pMainLoop);
924
925 pStreamPA->pu8PeekBuf = NULL;
926 }
927 }
928
929 if (RT_SUCCESS(rc))
930 {
931 if (pcxRead)
932 *pcxRead = cbReadTotal;
933 }
934
935 return rc;
936}
937
938
939/**
940 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
941 */
942static DECLCALLBACK(int) drvHostPulseAudioStreamPlay(PPDMIHOSTAUDIO pInterface,
943 PPDMAUDIOBACKENDSTREAM pStream, const void *pvBuf, uint32_t cxBuf,
944 uint32_t *pcxWritten)
945{
946 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
947 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
948 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
949 AssertReturn(cxBuf, VERR_INVALID_PARAMETER);
950 /* pcxWritten is optional. */
951
952 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
953 PPULSEAUDIOSTREAM pPAStream = (PPULSEAUDIOSTREAM)pStream;
954
955 int rc = VINF_SUCCESS;
956
957 uint32_t cbWrittenTotal = 0;
958
959 pa_threaded_mainloop_lock(pThis->pMainLoop);
960
961#ifdef LOG_ENABLED
962 const pa_usec_t tsNowUs = pa_rtclock_now();
963 const pa_usec_t tsDeltaPlayedUs = tsNowUs - pPAStream->tsLastReadWrittenUs;
964
965 Log3Func(("tsDeltaPlayedMs=%RU64\n", tsDeltaPlayedUs / 1000 /* ms */));
966
967 pPAStream->tsLastReadWrittenUs = tsNowUs;
968#endif
969
970 do
971 {
972 size_t cbWriteable = pa_stream_writable_size(pPAStream->pStream);
973 if (cbWriteable == (size_t)-1)
974 {
975 rc = paError(pPAStream->pDrv, "Failed to determine output data size");
976 break;
977 }
978
979 size_t cbLeft = RT_MIN(cbWriteable, cxBuf);
980 Assert(cbLeft); /* At this point we better have *something* to write. */
981
982 while (cbLeft)
983 {
984 uint32_t cbChunk = cbLeft; /* Write all at once for now. */
985
986 if (pa_stream_write(pPAStream->pStream, (uint8_t *)pvBuf + cbWrittenTotal, cbChunk, NULL /* Cleanup callback */,
987 0, PA_SEEK_RELATIVE) < 0)
988 {
989 rc = paError(pPAStream->pDrv, "Failed to write to output stream");
990 break;
991 }
992
993 Assert(cbLeft >= cbChunk);
994 cbLeft -= cbChunk;
995 cbWrittenTotal += cbChunk;
996 }
997
998 } while (0);
999
1000 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1001
1002 if (RT_SUCCESS(rc))
1003 {
1004 if (pcxWritten)
1005 *pcxWritten = cbWrittenTotal;
1006 }
1007
1008 return rc;
1009}
1010
1011
1012/** @todo Implement va handling. */
1013static int paError(PDRVHOSTPULSEAUDIO pThis, const char *szMsg)
1014{
1015 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1016 AssertPtrReturn(szMsg, VERR_INVALID_POINTER);
1017
1018 if (pThis->cLogErrors++ < VBOX_PULSEAUDIO_MAX_LOG_REL_ERRORS)
1019 {
1020 int rc2 = pa_context_errno(pThis->pContext);
1021 LogRel2(("PulseAudio: %s: %s\n", szMsg, pa_strerror(rc2)));
1022 }
1023
1024 /** @todo Implement some PulseAudio -> IPRT mapping here. */
1025 return VERR_GENERAL_FAILURE;
1026}
1027
1028
1029static void paEnumSinkCb(pa_context *pCtx, const pa_sink_info *pInfo, int eol, void *pvUserData)
1030{
1031 if (eol > 0)
1032 return;
1033
1034 PPULSEAUDIOENUMCBCTX pCbCtx = (PPULSEAUDIOENUMCBCTX)pvUserData;
1035 AssertPtrReturnVoid(pCbCtx);
1036 PDRVHOSTPULSEAUDIO pThis = pCbCtx->pDrv;
1037 AssertPtrReturnVoid(pThis);
1038 if (eol < 0)
1039 {
1040 pThis->fEnumOpSuccess = false;
1041 pa_threaded_mainloop_signal(pCbCtx->pDrv->pMainLoop, 0);
1042 return;
1043 }
1044
1045 AssertPtrReturnVoid(pCtx);
1046 AssertPtrReturnVoid(pInfo);
1047
1048 LogRel2(("PulseAudio: Using output sink '%s'\n", pInfo->name));
1049
1050 /** @todo Store sinks + channel mapping in callback context as soon as we have surround support. */
1051 pCbCtx->cDevOut++;
1052
1053 pThis->fEnumOpSuccess = true;
1054 pa_threaded_mainloop_signal(pCbCtx->pDrv->pMainLoop, 0);
1055}
1056
1057
1058static void paEnumSourceCb(pa_context *pCtx, const pa_source_info *pInfo, int eol, void *pvUserData)
1059{
1060 if (eol > 0)
1061 return;
1062
1063 PPULSEAUDIOENUMCBCTX pCbCtx = (PPULSEAUDIOENUMCBCTX)pvUserData;
1064 AssertPtrReturnVoid(pCbCtx);
1065 PDRVHOSTPULSEAUDIO pThis = pCbCtx->pDrv;
1066 AssertPtrReturnVoid(pThis);
1067 if (eol < 0)
1068 {
1069 pThis->fEnumOpSuccess = false;
1070 pa_threaded_mainloop_signal(pCbCtx->pDrv->pMainLoop, 0);
1071 return;
1072 }
1073
1074 AssertPtrReturnVoid(pCtx);
1075 AssertPtrReturnVoid(pInfo);
1076
1077 LogRel2(("PulseAudio: Using input source '%s'\n", pInfo->name));
1078
1079 /** @todo Store sources + channel mapping in callback context as soon as we have surround support. */
1080 pCbCtx->cDevIn++;
1081
1082 pThis->fEnumOpSuccess = true;
1083 pa_threaded_mainloop_signal(pCbCtx->pDrv->pMainLoop, 0);
1084}
1085
1086
1087static void paEnumServerCb(pa_context *pCtx, const pa_server_info *pInfo, void *pvUserData)
1088{
1089 AssertPtrReturnVoid(pCtx);
1090 PPULSEAUDIOENUMCBCTX pCbCtx = (PPULSEAUDIOENUMCBCTX)pvUserData;
1091 AssertPtrReturnVoid(pCbCtx);
1092 PDRVHOSTPULSEAUDIO pThis = pCbCtx->pDrv;
1093 AssertPtrReturnVoid(pThis);
1094
1095 if (!pInfo)
1096 {
1097 pThis->fEnumOpSuccess = false;
1098 pa_threaded_mainloop_signal(pCbCtx->pDrv->pMainLoop, 0);
1099 return;
1100 }
1101
1102 if (pInfo->default_sink_name)
1103 {
1104 Assert(RTStrIsValidEncoding(pInfo->default_sink_name));
1105 pCbCtx->pszDefaultSink = RTStrDup(pInfo->default_sink_name);
1106 }
1107
1108 if (pInfo->default_sink_name)
1109 {
1110 Assert(RTStrIsValidEncoding(pInfo->default_source_name));
1111 pCbCtx->pszDefaultSource = RTStrDup(pInfo->default_source_name);
1112 }
1113
1114 pThis->fEnumOpSuccess = true;
1115 pa_threaded_mainloop_signal(pThis->pMainLoop, 0);
1116}
1117
1118
1119static int paEnumerate(PDRVHOSTPULSEAUDIO pThis, PPDMAUDIOBACKENDCFG pCfg, uint32_t fEnum)
1120{
1121 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1122 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1123
1124 PDMAUDIOBACKENDCFG Cfg;
1125 RT_ZERO(Cfg);
1126
1127 RTStrPrintf2(Cfg.szName, sizeof(Cfg.szName), "PulseAudio driver");
1128
1129 Cfg.cbStreamOut = sizeof(PULSEAUDIOSTREAM);
1130 Cfg.cbStreamIn = sizeof(PULSEAUDIOSTREAM);
1131 Cfg.cMaxStreamsOut = UINT32_MAX;
1132 Cfg.cMaxStreamsIn = UINT32_MAX;
1133
1134 PULSEAUDIOENUMCBCTX CbCtx;
1135 RT_ZERO(CbCtx);
1136
1137 CbCtx.pDrv = pThis;
1138 CbCtx.fFlags = fEnum;
1139
1140 bool fLog = (fEnum & PULSEAUDIOENUMCBFLAGS_LOG);
1141
1142 pa_threaded_mainloop_lock(pThis->pMainLoop);
1143
1144 pThis->fEnumOpSuccess = false;
1145 int rc = paWaitFor(pThis, pa_context_get_server_info(pThis->pContext, paEnumServerCb, &CbCtx));
1146 if (RT_SUCCESS(rc) && !pThis->fEnumOpSuccess)
1147 rc = VERR_AUDIO_BACKEND_INIT_FAILED; /* error code does not matter */
1148 if (RT_SUCCESS(rc))
1149 {
1150 if (CbCtx.pszDefaultSink)
1151 {
1152 if (fLog)
1153 LogRel2(("PulseAudio: Default output sink is '%s'\n", CbCtx.pszDefaultSink));
1154
1155 pThis->fEnumOpSuccess = false;
1156 rc = paWaitFor(pThis, pa_context_get_sink_info_by_name(pThis->pContext, CbCtx.pszDefaultSink,
1157 paEnumSinkCb, &CbCtx));
1158 if (RT_SUCCESS(rc) && !pThis->fEnumOpSuccess)
1159 rc = VERR_AUDIO_BACKEND_INIT_FAILED; /* error code does not matter */
1160 if ( RT_FAILURE(rc)
1161 && fLog)
1162 {
1163 LogRel(("PulseAudio: Error enumerating properties for default output sink '%s'\n", CbCtx.pszDefaultSink));
1164 }
1165 }
1166 else if (fLog)
1167 LogRel2(("PulseAudio: No default output sink found\n"));
1168
1169 if (RT_SUCCESS(rc))
1170 {
1171 if (CbCtx.pszDefaultSource)
1172 {
1173 if (fLog)
1174 LogRel2(("PulseAudio: Default input source is '%s'\n", CbCtx.pszDefaultSource));
1175
1176 pThis->fEnumOpSuccess = false;
1177 rc = paWaitFor(pThis, pa_context_get_source_info_by_name(pThis->pContext, CbCtx.pszDefaultSource,
1178 paEnumSourceCb, &CbCtx));
1179 if ( (RT_FAILURE(rc) || !pThis->fEnumOpSuccess)
1180 && fLog)
1181 {
1182 LogRel(("PulseAudio: Error enumerating properties for default input source '%s'\n", CbCtx.pszDefaultSource));
1183 }
1184 }
1185 else if (fLog)
1186 LogRel2(("PulseAudio: No default input source found\n"));
1187 }
1188
1189 if (RT_SUCCESS(rc))
1190 {
1191 if (fLog)
1192 {
1193 LogRel2(("PulseAudio: Found %RU8 host playback device(s)\n", CbCtx.cDevOut));
1194 LogRel2(("PulseAudio: Found %RU8 host capturing device(s)\n", CbCtx.cDevIn));
1195 }
1196
1197 if (pCfg)
1198 memcpy(pCfg, &Cfg, sizeof(PDMAUDIOBACKENDCFG));
1199 }
1200
1201 if (CbCtx.pszDefaultSink)
1202 {
1203 RTStrFree(CbCtx.pszDefaultSink);
1204 CbCtx.pszDefaultSink = NULL;
1205 }
1206
1207 if (CbCtx.pszDefaultSource)
1208 {
1209 RTStrFree(CbCtx.pszDefaultSource);
1210 CbCtx.pszDefaultSource = NULL;
1211 }
1212 }
1213 else if (fLog)
1214 LogRel(("PulseAudio: Error enumerating PulseAudio server properties\n"));
1215
1216 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1217
1218 LogFlowFuncLeaveRC(rc);
1219 return rc;
1220}
1221
1222
1223static int paDestroyStreamIn(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA)
1224{
1225 LogFlowFuncEnter();
1226
1227 if (pStreamPA->pStream)
1228 {
1229 pa_threaded_mainloop_lock(pThis->pMainLoop);
1230
1231 pa_stream_disconnect(pStreamPA->pStream);
1232 pa_stream_unref(pStreamPA->pStream);
1233
1234 pStreamPA->pStream = NULL;
1235
1236 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1237 }
1238
1239 return VINF_SUCCESS;
1240}
1241
1242
1243static int paDestroyStreamOut(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA)
1244{
1245 if (pStreamPA->pStream)
1246 {
1247 pa_threaded_mainloop_lock(pThis->pMainLoop);
1248
1249 /* Make sure to cancel a pending draining operation, if any. */
1250 if (pStreamPA->pDrainOp)
1251 {
1252 pa_operation_cancel(pStreamPA->pDrainOp);
1253 pStreamPA->pDrainOp = NULL;
1254 }
1255
1256 pa_stream_disconnect(pStreamPA->pStream);
1257 pa_stream_unref(pStreamPA->pStream);
1258
1259 pStreamPA->pStream = NULL;
1260
1261 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1262 }
1263
1264 return VINF_SUCCESS;
1265}
1266
1267
1268static int paControlStreamOut(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA, PDMAUDIOSTREAMCMD enmStreamCmd)
1269{
1270 int rc = VINF_SUCCESS;
1271
1272 switch (enmStreamCmd)
1273 {
1274 case PDMAUDIOSTREAMCMD_ENABLE:
1275 case PDMAUDIOSTREAMCMD_RESUME:
1276 {
1277 pa_threaded_mainloop_lock(pThis->pMainLoop);
1278
1279 if ( pStreamPA->pDrainOp
1280 && pa_operation_get_state(pStreamPA->pDrainOp) != PA_OPERATION_DONE)
1281 {
1282 pa_operation_cancel(pStreamPA->pDrainOp);
1283 pa_operation_unref(pStreamPA->pDrainOp);
1284
1285 pStreamPA->pDrainOp = NULL;
1286 }
1287 else
1288 {
1289 /* Uncork (resume) stream. */
1290 rc = paWaitFor(pThis, pa_stream_cork(pStreamPA->pStream, 0 /* Uncork */, paStreamCbSuccess, pStreamPA));
1291 }
1292
1293 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1294 break;
1295 }
1296
1297 case PDMAUDIOSTREAMCMD_DISABLE:
1298 case PDMAUDIOSTREAMCMD_PAUSE:
1299 {
1300 /* Pause audio output (the Pause bit of the AC97 x_CR register is set).
1301 * Note that we must return immediately from here! */
1302 pa_threaded_mainloop_lock(pThis->pMainLoop);
1303 if (!pStreamPA->pDrainOp)
1304 {
1305 rc = paWaitFor(pThis, pa_stream_trigger(pStreamPA->pStream, paStreamCbSuccess, pStreamPA));
1306 if (RT_SUCCESS(rc))
1307 pStreamPA->pDrainOp = pa_stream_drain(pStreamPA->pStream, paStreamCbDrain, pStreamPA);
1308 }
1309 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1310 break;
1311 }
1312
1313 default:
1314 rc = VERR_NOT_SUPPORTED;
1315 break;
1316 }
1317
1318 LogFlowFuncLeaveRC(rc);
1319 return rc;
1320}
1321
1322
1323static int paControlStreamIn(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA, PDMAUDIOSTREAMCMD enmStreamCmd)
1324{
1325 int rc = VINF_SUCCESS;
1326
1327 LogFlowFunc(("enmStreamCmd=%ld\n", enmStreamCmd));
1328
1329 switch (enmStreamCmd)
1330 {
1331 case PDMAUDIOSTREAMCMD_ENABLE:
1332 case PDMAUDIOSTREAMCMD_RESUME:
1333 {
1334 pa_threaded_mainloop_lock(pThis->pMainLoop);
1335 rc = paWaitFor(pThis, pa_stream_cork(pStreamPA->pStream, 0 /* Play / resume */, paStreamCbSuccess, pStreamPA));
1336 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1337 break;
1338 }
1339
1340 case PDMAUDIOSTREAMCMD_DISABLE:
1341 case PDMAUDIOSTREAMCMD_PAUSE:
1342 {
1343 pa_threaded_mainloop_lock(pThis->pMainLoop);
1344 if (pStreamPA->pu8PeekBuf) /* Do we need to drop the peek buffer?*/
1345 {
1346 pa_stream_drop(pStreamPA->pStream);
1347 pStreamPA->pu8PeekBuf = NULL;
1348 }
1349
1350 rc = paWaitFor(pThis, pa_stream_cork(pStreamPA->pStream, 1 /* Stop / pause */, paStreamCbSuccess, pStreamPA));
1351 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1352 break;
1353 }
1354
1355 default:
1356 rc = VERR_NOT_SUPPORTED;
1357 break;
1358 }
1359
1360 return rc;
1361}
1362
1363
1364/**
1365 * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
1366 */
1367static DECLCALLBACK(void) drvHostPulseAudioShutdown(PPDMIHOSTAUDIO pInterface)
1368{
1369 AssertPtrReturnVoid(pInterface);
1370
1371 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1372
1373 LogFlowFuncEnter();
1374
1375 if (pThis->pMainLoop)
1376 pa_threaded_mainloop_stop(pThis->pMainLoop);
1377
1378 if (pThis->pContext)
1379 {
1380 pa_context_disconnect(pThis->pContext);
1381 pa_context_unref(pThis->pContext);
1382 pThis->pContext = NULL;
1383 }
1384
1385 if (pThis->pMainLoop)
1386 {
1387 pa_threaded_mainloop_free(pThis->pMainLoop);
1388 pThis->pMainLoop = NULL;
1389 }
1390
1391 LogFlowFuncLeave();
1392}
1393
1394
1395/**
1396 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
1397 */
1398static DECLCALLBACK(int) drvHostPulseAudioGetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
1399{
1400 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1401 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
1402
1403 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1404
1405 return paEnumerate(pThis, pBackendCfg, PULSEAUDIOENUMCBFLAGS_LOG /* fEnum */);
1406}
1407
1408
1409/**
1410 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
1411 */
1412static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostPulseAudioGetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
1413{
1414 RT_NOREF(enmDir);
1415 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
1416
1417 return PDMAUDIOBACKENDSTS_RUNNING;
1418}
1419
1420
1421/**
1422 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
1423 */
1424static DECLCALLBACK(int) drvHostPulseAudioStreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
1425 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
1426{
1427 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1428 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1429 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
1430 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
1431
1432 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1433 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1434
1435 int rc;
1436 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
1437 rc = paCreateStreamIn (pThis, pStreamPA, pCfgReq, pCfgAcq);
1438 else if (pCfgReq->enmDir == PDMAUDIODIR_OUT)
1439 rc = paCreateStreamOut(pThis, pStreamPA, pCfgReq, pCfgAcq);
1440 else
1441 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
1442
1443 if (RT_SUCCESS(rc))
1444 {
1445 pStreamPA->pCfg = DrvAudioHlpStreamCfgDup(pCfgAcq);
1446 if (!pStreamPA->pCfg)
1447 rc = VERR_NO_MEMORY;
1448 }
1449
1450 return rc;
1451}
1452
1453
1454/**
1455 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
1456 */
1457static DECLCALLBACK(int) drvHostPulseAudioStreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1458{
1459 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1460 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1461
1462 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1463 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1464
1465 if (!pStreamPA->pCfg) /* Not (yet) configured? Skip. */
1466 return VINF_SUCCESS;
1467
1468 int rc;
1469 if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_IN)
1470 rc = paDestroyStreamIn (pThis, pStreamPA);
1471 else if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_OUT)
1472 rc = paDestroyStreamOut(pThis, pStreamPA);
1473 else
1474 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
1475
1476 if (RT_SUCCESS(rc))
1477 {
1478 DrvAudioHlpStreamCfgFree(pStreamPA->pCfg);
1479 pStreamPA->pCfg = NULL;
1480 }
1481
1482 return rc;
1483}
1484
1485
1486/**
1487 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
1488 */
1489static DECLCALLBACK(int) drvHostPulseAudioStreamControl(PPDMIHOSTAUDIO pInterface,
1490 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
1491{
1492 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1493 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1494
1495 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1496 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1497
1498 if (!pStreamPA->pCfg) /* Not (yet) configured? Skip. */
1499 return VINF_SUCCESS;
1500
1501 int rc;
1502 if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_IN)
1503 rc = paControlStreamIn (pThis, pStreamPA, enmStreamCmd);
1504 else if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_OUT)
1505 rc = paControlStreamOut(pThis, pStreamPA, enmStreamCmd);
1506 else
1507 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
1508
1509 return rc;
1510}
1511
1512
1513static uint32_t paStreamGetAvail(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA)
1514{
1515 pa_threaded_mainloop_lock(pThis->pMainLoop);
1516
1517 uint32_t cbAvail = 0;
1518
1519 if (PA_STREAM_IS_GOOD(pa_stream_get_state(pStreamPA->pStream)))
1520 {
1521 if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_IN)
1522 {
1523 cbAvail = (uint32_t)pa_stream_readable_size(pStreamPA->pStream);
1524 Log3Func(("cbReadable=%RU32\n", cbAvail));
1525 }
1526 else if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_OUT)
1527 {
1528 size_t cbWritable = pa_stream_writable_size(pStreamPA->pStream);
1529
1530 Log3Func(("cbWritable=%zu, maxLength=%RU32, minReq=%RU32\n",
1531 cbWritable, pStreamPA->BufAttr.maxlength, pStreamPA->BufAttr.minreq));
1532
1533 /* Don't report more writable than the PA server can handle. */
1534 if (cbWritable > pStreamPA->BufAttr.maxlength)
1535 cbWritable = pStreamPA->BufAttr.maxlength;
1536
1537 cbAvail = (uint32_t)cbWritable;
1538 }
1539 else
1540 AssertFailed();
1541 }
1542
1543 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1544
1545 return cbAvail;
1546}
1547
1548
1549/**
1550 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
1551 */
1552static DECLCALLBACK(uint32_t) drvHostPulseAudioStreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1553{
1554 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1555 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1556
1557 return paStreamGetAvail(pThis, pStreamPA);
1558}
1559
1560
1561/**
1562 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
1563 */
1564static DECLCALLBACK(uint32_t) drvHostPulseAudioStreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1565{
1566 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1567 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1568
1569 return paStreamGetAvail(pThis, pStreamPA);
1570}
1571
1572
1573/**
1574 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
1575 */
1576static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvHostPulseAudioStreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1577{
1578 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1579 RT_NOREF(pStream);
1580
1581 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1582
1583 PDMAUDIOSTREAMSTS strmSts = PDMAUDIOSTREAMSTS_FLAG_NONE;
1584
1585 /* Check PulseAudio's general status. */
1586 if ( pThis->pContext
1587 && PA_CONTEXT_IS_GOOD(pa_context_get_state(pThis->pContext)))
1588 {
1589 strmSts = PDMAUDIOSTREAMSTS_FLAG_INITIALIZED | PDMAUDIOSTREAMSTS_FLAG_ENABLED;
1590 }
1591
1592 return strmSts;
1593}
1594
1595
1596/**
1597 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
1598 */
1599static DECLCALLBACK(int) drvHostPulseAudioStreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1600{
1601 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1602 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1603
1604 LogFlowFuncEnter();
1605
1606 /* Nothing to do here for PulseAudio. */
1607 return VINF_SUCCESS;
1608}
1609
1610
1611/**
1612 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1613 */
1614static DECLCALLBACK(void *) drvHostPulseAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1615{
1616 AssertPtrReturn(pInterface, NULL);
1617 AssertPtrReturn(pszIID, NULL);
1618
1619 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1620 PDRVHOSTPULSEAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPULSEAUDIO);
1621 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1622 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
1623
1624 return NULL;
1625}
1626
1627
1628/**
1629 * Destructs a PulseAudio Audio driver instance.
1630 *
1631 * @copydoc FNPDMDRVDESTRUCT
1632 */
1633static DECLCALLBACK(void) drvHostPulseAudioDestruct(PPDMDRVINS pDrvIns)
1634{
1635 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1636 LogFlowFuncEnter();
1637}
1638
1639
1640/**
1641 * Constructs a PulseAudio Audio driver instance.
1642 *
1643 * @copydoc FNPDMDRVCONSTRUCT
1644 */
1645static DECLCALLBACK(int) drvHostPulseAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1646{
1647 RT_NOREF(pCfg, fFlags);
1648 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1649 AssertPtrReturn(pDrvIns, VERR_INVALID_POINTER);
1650
1651 PDRVHOSTPULSEAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPULSEAUDIO);
1652 LogRel(("Audio: Initializing PulseAudio driver\n"));
1653
1654 pThis->pDrvIns = pDrvIns;
1655 /* IBase */
1656 pDrvIns->IBase.pfnQueryInterface = drvHostPulseAudioQueryInterface;
1657 /* IHostAudio */
1658 PDMAUDIO_IHOSTAUDIO_CALLBACKS(drvHostPulseAudio);
1659
1660 int rc2 = CFGMR3QueryString(pCfg, "StreamName", pThis->szStreamName, sizeof(pThis->szStreamName));
1661 AssertMsgRCReturn(rc2, ("Confguration error: No/bad \"StreamName\" value, rc=%Rrc\n", rc2), rc2);
1662
1663 return VINF_SUCCESS;
1664}
1665
1666
1667/**
1668 * Pulse audio driver registration record.
1669 */
1670const PDMDRVREG g_DrvHostPulseAudio =
1671{
1672 /* u32Version */
1673 PDM_DRVREG_VERSION,
1674 /* szName */
1675 "PulseAudio",
1676 /* szRCMod */
1677 "",
1678 /* szR0Mod */
1679 "",
1680 /* pszDescription */
1681 "Pulse Audio host driver",
1682 /* fFlags */
1683 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1684 /* fClass. */
1685 PDM_DRVREG_CLASS_AUDIO,
1686 /* cMaxInstances */
1687 ~0U,
1688 /* cbInstance */
1689 sizeof(DRVHOSTPULSEAUDIO),
1690 /* pfnConstruct */
1691 drvHostPulseAudioConstruct,
1692 /* pfnDestruct */
1693 drvHostPulseAudioDestruct,
1694 /* pfnRelocate */
1695 NULL,
1696 /* pfnIOCtl */
1697 NULL,
1698 /* pfnPowerOn */
1699 NULL,
1700 /* pfnReset */
1701 NULL,
1702 /* pfnSuspend */
1703 NULL,
1704 /* pfnResume */
1705 NULL,
1706 /* pfnAttach */
1707 NULL,
1708 /* pfnDetach */
1709 NULL,
1710 /* pfnPowerOff */
1711 NULL,
1712 /* pfnSoftReset */
1713 NULL,
1714 /* u32EndVersion */
1715 PDM_DRVREG_VERSION
1716};
1717
1718#if 0 /* unused */
1719static struct audio_option pulse_options[] =
1720{
1721 {"DAC_MS", AUD_OPT_INT, &s_pulseCfg.buffer_msecs_out,
1722 "DAC period size in milliseconds", NULL, 0},
1723 {"ADC_MS", AUD_OPT_INT, &s_pulseCfg.buffer_msecs_in,
1724 "ADC period size in milliseconds", NULL, 0}
1725};
1726#endif
1727
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