VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/DrvAudioVRDE.cpp@ 73638

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

Main/DrvAudioVRDE: do not crash if PDM driver does not exist. bugref:9225

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.4 KB
Line 
1/* $Id: DrvAudioVRDE.cpp 73638 2018-08-13 14:25:23Z vboxsync $ */
2/** @file
3 * VRDE audio backend for Main.
4 */
5
6/*
7 * Copyright (C) 2013-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 "LoggingNew.h"
24
25#include <VBox/log.h>
26#include "DrvAudioVRDE.h"
27#include "ConsoleImpl.h"
28#include "ConsoleVRDPServer.h"
29
30#include "../../Devices/Audio/DrvAudio.h"
31
32#include <iprt/mem.h>
33#include <iprt/cdefs.h>
34#include <iprt/circbuf.h>
35
36#include <VBox/vmm/pdmaudioifs.h>
37#include <VBox/vmm/pdmdrv.h>
38#include <VBox/RemoteDesktop/VRDE.h>
39#include <VBox/vmm/cfgm.h>
40#include <VBox/err.h>
41
42
43/*********************************************************************************************************************************
44* Structures and Typedefs *
45*********************************************************************************************************************************/
46/**
47 * Audio VRDE driver instance data.
48 */
49typedef struct DRVAUDIOVRDE
50{
51 /** Pointer to audio VRDE object. */
52 AudioVRDE *pAudioVRDE;
53 /** Pointer to the driver instance structure. */
54 PPDMDRVINS pDrvIns;
55 /** Pointer to host audio interface. */
56 PDMIHOSTAUDIO IHostAudio;
57 /** Pointer to the VRDP's console object. */
58 ConsoleVRDPServer *pConsoleVRDPServer;
59 /** Pointer to the DrvAudio port interface that is above us. */
60 PPDMIAUDIOCONNECTOR pDrvAudio;
61 /** Number of connected clients to this VRDE instance. */
62 uint32_t cClients;
63} DRVAUDIOVRDE, *PDRVAUDIOVRDE;
64
65typedef struct VRDESTREAM
66{
67 /** The stream's acquired configuration. */
68 PPDMAUDIOSTREAMCFG pCfg;
69 union
70 {
71 struct
72 {
73 /** Circular buffer for holding the recorded audio frames from the host. */
74 PRTCIRCBUF pCircBuf;
75 } In;
76 };
77} VRDESTREAM, *PVRDESTREAM;
78
79/* Sanity. */
80AssertCompileSize(PDMAUDIOFRAME, sizeof(int64_t) * 2 /* st_sample_t using by VRDP server */);
81
82static int vrdeCreateStreamIn(PVRDESTREAM pStreamVRDE, PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
83{
84 RT_NOREF(pCfgReq);
85 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
86
87 pCfgAcq->Props.uHz = 22050; /* The VRDP server's internal frequency. */
88 pCfgAcq->Props.cChannels = 2;
89 pCfgAcq->Props.cBytes = 2; /* 16 bit. */
90 pCfgAcq->Props.fSigned = true;
91 pCfgAcq->Props.fSwapEndian = false;
92 pCfgAcq->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfgAcq->Props.cBytes, pCfgAcq->Props.cChannels);
93
94 /* According to the VRDP docs, the VRDP server stores audio in 200ms chunks. */
95 const uint32_t cfVRDPServer = DrvAudioHlpMilliToFrames(200 /* ms */, &pCfgAcq->Props);
96
97 int rc = RTCircBufCreate(&pStreamVRDE->In.pCircBuf, DrvAudioHlpFramesToBytes(cfVRDPServer, &pCfgAcq->Props));
98 if (RT_SUCCESS(rc))
99 {
100 /*
101 * Because of historical reasons the VRDP server operates on st_sample_t structures internally,
102 * which is 2 * int64_t for left/right (stereo) channels.
103 *
104 * As the audio connector also uses this format, set the layout to "raw" and just let pass through
105 * the data without any layout modification needed.
106 */
107 pCfgAcq->enmLayout = PDMAUDIOSTREAMLAYOUT_RAW;
108
109 pCfgAcq->Backend.cfPeriod = cfVRDPServer;
110 pCfgAcq->Backend.cfBufferSize = pCfgAcq->Backend.cfPeriod * 2; /* Use "double buffering". */
111 pCfgAcq->Backend.cfPreBuf = pCfgAcq->Backend.cfPeriod;
112 }
113
114 return rc;
115}
116
117
118static int vrdeCreateStreamOut(PVRDESTREAM pStreamVRDE, PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
119{
120 RT_NOREF(pStreamVRDE, pCfgReq);
121
122 if (pCfgAcq)
123 {
124 /*
125 * Because of historical reasons the VRDP server operates on st_sample_t structures internally,
126 * which is 2 * int64_t for left/right (stereo) channels.
127 *
128 * As the audio connector also uses this format, set the layout to "raw" and just let pass through
129 * the data without any layout modification needed.
130 */
131 pCfgAcq->enmLayout = PDMAUDIOSTREAMLAYOUT_RAW;
132
133 pCfgAcq->Props.uHz = 22050; /* The VRDP server's internal frequency. */
134 pCfgAcq->Props.cChannels = 2;
135 pCfgAcq->Props.cBytes = 2; /* 16 bit. */
136 pCfgAcq->Props.fSigned = true;
137 pCfgAcq->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfgAcq->Props.cBytes, pCfgAcq->Props.cChannels);
138
139 /* According to the VRDP docs, the VRDP server stores audio in 200ms chunks. */
140 pCfgAcq->Backend.cfPeriod = DrvAudioHlpMilliToFrames(10 /* ms */, &pCfgAcq->Props);
141 pCfgAcq->Backend.cfBufferSize = DrvAudioHlpMilliToFrames(200 /* ms */, &pCfgAcq->Props);
142 pCfgAcq->Backend.cfPreBuf = pCfgAcq->Backend.cfBufferSize;
143 }
144
145 return VINF_SUCCESS;
146}
147
148
149static int vrdeControlStreamOut(PDRVAUDIOVRDE pDrv, PVRDESTREAM pStreamVRDE, PDMAUDIOSTREAMCMD enmStreamCmd)
150{
151 RT_NOREF(pDrv, pStreamVRDE, enmStreamCmd);
152
153 LogFlowFunc(("enmStreamCmd=%ld\n", enmStreamCmd));
154
155 return VINF_SUCCESS;
156}
157
158
159static int vrdeControlStreamIn(PDRVAUDIOVRDE pDrv, PVRDESTREAM pStreamVRDE, PDMAUDIOSTREAMCMD enmStreamCmd)
160{
161 LogFlowFunc(("enmStreamCmd=%ld\n", enmStreamCmd));
162
163 if (!pDrv->pConsoleVRDPServer)
164 return VINF_SUCCESS;
165
166 int rc;
167
168 /* Initialize only if not already done. */
169 switch (enmStreamCmd)
170 {
171 case PDMAUDIOSTREAMCMD_ENABLE:
172 {
173 rc = pDrv->pConsoleVRDPServer->SendAudioInputBegin(NULL, pStreamVRDE,
174 DrvAudioHlpMilliToFrames(200 /* ms */, &pStreamVRDE->pCfg->Props),
175 pStreamVRDE->pCfg->Props.uHz, pStreamVRDE->pCfg->Props.cChannels,
176 pStreamVRDE->pCfg->Props.cBytes * 8 /* Bit */);
177 if (rc == VERR_NOT_SUPPORTED)
178 {
179 LogRel2(("Audio: No VRDE client connected, so no input recording available\n"));
180 rc = VINF_SUCCESS;
181 }
182
183 break;
184 }
185
186 case PDMAUDIOSTREAMCMD_DISABLE:
187 {
188 pDrv->pConsoleVRDPServer->SendAudioInputEnd(NULL /* pvUserCtx */);
189 rc = VINF_SUCCESS;
190
191 break;
192 }
193
194 case PDMAUDIOSTREAMCMD_PAUSE:
195 {
196 rc = VINF_SUCCESS;
197 break;
198 }
199
200 case PDMAUDIOSTREAMCMD_RESUME:
201 {
202 rc = VINF_SUCCESS;
203 break;
204 }
205
206 default:
207 {
208 rc = VERR_NOT_SUPPORTED;
209 break;
210 }
211 }
212
213 if (RT_FAILURE(rc))
214 LogFunc(("Failed with %Rrc\n", rc));
215
216 return rc;
217}
218
219
220/**
221 * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
222 */
223static DECLCALLBACK(int) drvAudioVRDEInit(PPDMIHOSTAUDIO pInterface)
224{
225 RT_NOREF(pInterface);
226 LogFlowFuncEnter();
227
228 return VINF_SUCCESS;
229}
230
231
232/**
233 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
234 */
235static DECLCALLBACK(int) drvAudioVRDEStreamCapture(PPDMIHOSTAUDIO pInterface,
236 PPDMAUDIOBACKENDSTREAM pStream, void *pvBuf, uint32_t cxBuf, uint32_t *pcxRead)
237{
238 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
239 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
240 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
241 AssertReturn(cxBuf, VERR_INVALID_PARAMETER);
242 /* pcxRead is optional. */
243
244 PVRDESTREAM pStreamVRDE = (PVRDESTREAM)pStream;
245
246 size_t cbData = 0;
247
248 if (RTCircBufUsed(pStreamVRDE->In.pCircBuf))
249 {
250 void *pvData;
251
252 RTCircBufAcquireReadBlock(pStreamVRDE->In.pCircBuf, cxBuf, &pvData, &cbData);
253
254 if (cbData)
255 memcpy(pvBuf, pvData, cbData);
256
257 RTCircBufReleaseReadBlock(pStreamVRDE->In.pCircBuf, cbData);
258 }
259
260 if (pcxRead)
261 *pcxRead = (uint32_t)cbData;
262
263 return VINF_SUCCESS;
264}
265
266
267/**
268 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
269 */
270static DECLCALLBACK(int) drvAudioVRDEStreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
271 const void *pvBuf, uint32_t cxBuf, uint32_t *pcxWritten)
272{
273 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
274 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
275 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
276 AssertReturn(cxBuf, VERR_INVALID_PARAMETER);
277 /* pcxWritten is optional. */
278
279 PDRVAUDIOVRDE pDrv = RT_FROM_MEMBER(pInterface, DRVAUDIOVRDE, IHostAudio);
280 PVRDESTREAM pStreamVRDE = (PVRDESTREAM)pStream;
281
282 if (!pDrv->pConsoleVRDPServer)
283 return VERR_NOT_AVAILABLE;
284
285 /* Note: We get the number of *frames* in cxBuf
286 * (since we specified PDMAUDIOSTREAMLAYOUT_RAW as the audio data layout) on stream creation. */
287 uint32_t cfLive = cxBuf;
288
289 PPDMAUDIOPCMPROPS pProps = &pStreamVRDE->pCfg->Props;
290
291 VRDEAUDIOFORMAT format = VRDE_AUDIO_FMT_MAKE(pProps->uHz,
292 pProps->cChannels,
293 pProps->cBytes * 8 /* Bit */,
294 pProps->fSigned);
295
296 /* Use the internal counter to track if we (still) can write to the VRDP server
297 * or if we need to wait another round (time slot). */
298 uint32_t cfToWrite = cfLive;
299
300 Log3Func(("cfLive=%RU32, cfToWrite=%RU32\n", cfLive, cfToWrite));
301
302 /* Don't play more than available. */
303 if (cfToWrite > cfLive)
304 cfToWrite = cfLive;
305
306 int rc = VINF_SUCCESS;
307
308 PPDMAUDIOFRAME paSampleBuf = (PPDMAUDIOFRAME)pvBuf;
309 AssertPtr(paSampleBuf);
310
311 /*
312 * Call the VRDP server with the data.
313 */
314 uint32_t cfWritten = 0;
315 while (cfToWrite)
316 {
317 uint32_t cfChunk = cfToWrite; /** @todo For now write all at once. */
318
319 if (!cfChunk) /* Nothing to send. Bail out. */
320 break;
321
322 /* Note: The VRDP server expects int64_t samples per channel, regardless of the actual
323 * sample bits (e.g 8 or 16 bits). */
324 pDrv->pConsoleVRDPServer->SendAudioSamples(paSampleBuf + cfWritten, cfChunk /* Frames */, format);
325
326 cfWritten += cfChunk;
327 Assert(cfWritten <= cfLive);
328
329 Assert(cfToWrite >= cfChunk);
330 cfToWrite -= cfChunk;
331 }
332
333 if (RT_SUCCESS(rc))
334 {
335 /* Return frames instead of bytes here
336 * (since we specified PDMAUDIOSTREAMLAYOUT_RAW as the audio data layout). */
337 if (pcxWritten)
338 *pcxWritten = cfWritten;
339 }
340
341 return rc;
342}
343
344
345static int vrdeDestroyStreamIn(PDRVAUDIOVRDE pDrv, PVRDESTREAM pStreamVRDE)
346{
347 if (pDrv->pConsoleVRDPServer)
348 pDrv->pConsoleVRDPServer->SendAudioInputEnd(NULL);
349
350 if (pStreamVRDE->In.pCircBuf)
351 {
352 RTCircBufDestroy(pStreamVRDE->In.pCircBuf);
353 pStreamVRDE->In.pCircBuf = NULL;
354 }
355
356 return VINF_SUCCESS;
357}
358
359
360static int vrdeDestroyStreamOut(PDRVAUDIOVRDE pDrv, PVRDESTREAM pStreamVRDE)
361{
362 RT_NOREF(pDrv, pStreamVRDE);
363
364 return VINF_SUCCESS;
365}
366
367
368/**
369 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
370 */
371static DECLCALLBACK(int) drvAudioVRDEGetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
372{
373 RT_NOREF(pInterface);
374 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
375
376 pBackendCfg->cbStreamOut = sizeof(VRDESTREAM);
377 pBackendCfg->cbStreamIn = sizeof(VRDESTREAM);
378 pBackendCfg->cMaxStreamsIn = UINT32_MAX;
379 pBackendCfg->cMaxStreamsOut = UINT32_MAX;
380
381 return VINF_SUCCESS;
382}
383
384
385/**
386 * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
387 */
388static DECLCALLBACK(void) drvAudioVRDEShutdown(PPDMIHOSTAUDIO pInterface)
389{
390 PDRVAUDIOVRDE pDrv = RT_FROM_MEMBER(pInterface, DRVAUDIOVRDE, IHostAudio);
391 AssertPtrReturnVoid(pDrv);
392
393 if (pDrv->pConsoleVRDPServer)
394 pDrv->pConsoleVRDPServer->SendAudioInputEnd(NULL);
395}
396
397
398/**
399 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
400 */
401static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvAudioVRDEGetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
402{
403 PDRVAUDIOVRDE pDrv = RT_FROM_MEMBER(pInterface, DRVAUDIOVRDE, IHostAudio);
404 AssertPtrReturn(pDrv, PDMAUDIOBACKENDSTS_ERROR);
405
406 RT_NOREF(enmDir);
407
408 return PDMAUDIOBACKENDSTS_RUNNING;
409}
410
411
412/**
413 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
414 */
415static DECLCALLBACK(int) drvAudioVRDEStreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
416 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
417{
418 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
419 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
420 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
421 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
422
423 RT_NOREF(pInterface);
424
425 PVRDESTREAM pStreamVRDE = (PVRDESTREAM)pStream;
426
427 int rc;
428 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
429 rc = vrdeCreateStreamIn (pStreamVRDE, pCfgReq, pCfgAcq);
430 else
431 rc = vrdeCreateStreamOut(pStreamVRDE, pCfgReq, pCfgAcq);
432
433 if (RT_SUCCESS(rc))
434 {
435 pStreamVRDE->pCfg = DrvAudioHlpStreamCfgDup(pCfgAcq);
436 if (!pStreamVRDE->pCfg)
437 rc = VERR_NO_MEMORY;
438 }
439
440 return rc;
441}
442
443
444/**
445 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
446 */
447static DECLCALLBACK(int) drvAudioVRDEStreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
448{
449 RT_NOREF(pInterface);
450 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
451
452 PDRVAUDIOVRDE pDrv = RT_FROM_MEMBER(pInterface, DRVAUDIOVRDE, IHostAudio);
453 PVRDESTREAM pStreamVRDE = (PVRDESTREAM)pStream;
454
455 if (!pStreamVRDE->pCfg) /* Not (yet) configured? Skip. */
456 return VINF_SUCCESS;
457
458 int rc;
459 if (pStreamVRDE->pCfg->enmDir == PDMAUDIODIR_IN)
460 rc = vrdeDestroyStreamIn(pDrv, pStreamVRDE);
461 else
462 rc = vrdeDestroyStreamOut(pDrv, pStreamVRDE);
463
464 if (RT_SUCCESS(rc))
465 {
466 DrvAudioHlpStreamCfgFree(pStreamVRDE->pCfg);
467 pStreamVRDE->pCfg = NULL;
468 }
469
470 return rc;
471}
472
473
474/**
475 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
476 */
477static DECLCALLBACK(int) drvAudioVRDEStreamControl(PPDMIHOSTAUDIO pInterface,
478 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
479{
480 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
481 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
482
483 PDRVAUDIOVRDE pDrv = RT_FROM_MEMBER(pInterface, DRVAUDIOVRDE, IHostAudio);
484 PVRDESTREAM pStreamVRDE = (PVRDESTREAM)pStream;
485
486 if (!pStreamVRDE->pCfg) /* Not (yet) configured? Skip. */
487 return VINF_SUCCESS;
488
489 int rc;
490 if (pStreamVRDE->pCfg->enmDir == PDMAUDIODIR_IN)
491 rc = vrdeControlStreamIn(pDrv, pStreamVRDE, enmStreamCmd);
492 else
493 rc = vrdeControlStreamOut(pDrv, pStreamVRDE, enmStreamCmd);
494
495 return rc;
496}
497
498
499/**
500 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
501 */
502static DECLCALLBACK(uint32_t) drvAudioVRDEStreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
503{
504 RT_NOREF(pInterface);
505
506 PVRDESTREAM pStreamVRDE = (PVRDESTREAM)pStream;
507
508 if (pStreamVRDE->pCfg->enmDir == PDMAUDIODIR_IN)
509 {
510 /* Return frames instead of bytes here
511 * (since we specified PDMAUDIOSTREAMLAYOUT_RAW as the audio data layout). */
512 return (uint32_t)PDMAUDIOSTREAMCFG_B2F(pStreamVRDE->pCfg, RTCircBufUsed(pStreamVRDE->In.pCircBuf));
513 }
514
515 return 0;
516}
517
518
519/**
520 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
521 */
522static DECLCALLBACK(uint32_t) drvAudioVRDEStreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
523{
524 PDRVAUDIOVRDE pDrv = RT_FROM_MEMBER(pInterface, DRVAUDIOVRDE, IHostAudio);
525 PVRDESTREAM pStreamVRDE = (PVRDESTREAM)pStream;
526
527 PPDMAUDIOPCMPROPS pProps = &pStreamVRDE->pCfg->Props;
528
529 RT_NOREF(pDrv, pProps);
530
531 /* Return frames instead of bytes here
532 * (since we specified PDMAUDIOSTREAMLAYOUT_RAW as the audio data layout). */
533 if (pDrv->cClients)
534 return _16K; /** @todo Find some sane value here. We probably need a VRDE API VRDE to specify this. */
535
536 return 0;
537}
538
539
540/**
541 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
542 */
543static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvAudioVRDEStreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
544{
545 PDRVAUDIOVRDE pDrv = RT_FROM_MEMBER(pInterface, DRVAUDIOVRDE, IHostAudio);
546 RT_NOREF(pStream);
547
548 PDMAUDIOSTREAMSTS streamSts = PDMAUDIOSTREAMSTS_FLAG_INITIALIZED;
549
550 if (pDrv->cClients) /* If any clients are connected, flag the stream as enabled. */
551 streamSts |= PDMAUDIOSTREAMSTS_FLAG_ENABLED;
552
553 return streamSts;
554}
555
556
557/**
558 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
559 */
560static DECLCALLBACK(int) drvAudioVRDEStreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
561{
562 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
563 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
564
565 /* Nothing to do here for VRDE. */
566 return VINF_SUCCESS;
567}
568
569
570/**
571 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
572 */
573static DECLCALLBACK(void *) drvAudioVRDEQueryInterface(PPDMIBASE pInterface, const char *pszIID)
574{
575 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
576 PDRVAUDIOVRDE pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIOVRDE);
577
578 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
579 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
580 return NULL;
581}
582
583
584AudioVRDE::AudioVRDE(Console *pConsole)
585 : AudioDriver(pConsole)
586 , mpDrv(NULL)
587{
588}
589
590
591AudioVRDE::~AudioVRDE(void)
592{
593 if (mpDrv)
594 {
595 mpDrv->pAudioVRDE = NULL;
596 mpDrv = NULL;
597 }
598}
599
600
601/**
602 * @copydoc AudioDriver::configureDriver
603 */
604int AudioVRDE::configureDriver(PCFGMNODE pLunCfg)
605{
606 CFGMR3InsertInteger(pLunCfg, "Object", (uintptr_t)this);
607 CFGMR3InsertInteger(pLunCfg, "ObjectVRDPServer", (uintptr_t)mpConsole->i_consoleVRDPServer());
608
609 return VINF_SUCCESS;
610}
611
612
613void AudioVRDE::onVRDEClientConnect(uint32_t uClientID)
614{
615 RT_NOREF(uClientID);
616
617 LogRel2(("Audio: VRDE client connected\n"));
618 if (mpDrv)
619 mpDrv->cClients++;
620}
621
622
623void AudioVRDE::onVRDEClientDisconnect(uint32_t uClientID)
624{
625 RT_NOREF(uClientID);
626
627 LogRel2(("Audio: VRDE client disconnected\n"));
628 Assert(mpDrv->cClients);
629 if (mpDrv)
630 mpDrv->cClients--;
631}
632
633
634int AudioVRDE::onVRDEControl(bool fEnable, uint32_t uFlags)
635{
636 RT_NOREF(fEnable, uFlags);
637 LogFlowThisFunc(("fEnable=%RTbool, uFlags=0x%x\n", fEnable, uFlags));
638
639 if (mpDrv == NULL)
640 return VERR_INVALID_STATE;
641
642 return VINF_SUCCESS; /* Never veto. */
643}
644
645
646/**
647 * Marks the beginning of sending captured audio data from a connected
648 * RDP client.
649 *
650 * @return IPRT status code.
651 * @param pvContext The context; in this case a pointer to a
652 * VRDESTREAMIN structure.
653 * @param pVRDEAudioBegin Pointer to a VRDEAUDIOINBEGIN structure.
654 */
655int AudioVRDE::onVRDEInputBegin(void *pvContext, PVRDEAUDIOINBEGIN pVRDEAudioBegin)
656{
657 AssertPtrReturn(pvContext, VERR_INVALID_POINTER);
658 AssertPtrReturn(pVRDEAudioBegin, VERR_INVALID_POINTER);
659
660 PVRDESTREAM pVRDEStrmIn = (PVRDESTREAM)pvContext;
661 AssertPtrReturn(pVRDEStrmIn, VERR_INVALID_POINTER);
662
663 VRDEAUDIOFORMAT audioFmt = pVRDEAudioBegin->fmt;
664
665 int iSampleHz = VRDE_AUDIO_FMT_SAMPLE_FREQ(audioFmt); RT_NOREF(iSampleHz);
666 int cChannels = VRDE_AUDIO_FMT_CHANNELS(audioFmt); RT_NOREF(cChannels);
667 int cBits = VRDE_AUDIO_FMT_BITS_PER_SAMPLE(audioFmt); RT_NOREF(cBits);
668 bool fUnsigned = VRDE_AUDIO_FMT_SIGNED(audioFmt); RT_NOREF(fUnsigned);
669
670 LogFlowFunc(("cbSample=%RU32, iSampleHz=%d, cChannels=%d, cBits=%d, fUnsigned=%RTbool\n",
671 VRDE_AUDIO_FMT_BYTES_PER_SAMPLE(audioFmt), iSampleHz, cChannels, cBits, fUnsigned));
672
673 return VINF_SUCCESS;
674}
675
676
677int AudioVRDE::onVRDEInputData(void *pvContext, const void *pvData, uint32_t cbData)
678{
679 PVRDESTREAM pStreamVRDE = (PVRDESTREAM)pvContext;
680 AssertPtrReturn(pStreamVRDE, VERR_INVALID_POINTER);
681
682 void *pvBuf;
683 size_t cbBuf;
684
685 RTCircBufAcquireWriteBlock(pStreamVRDE->In.pCircBuf, cbData, &pvBuf, &cbBuf);
686
687 if (cbBuf)
688 memcpy(pvBuf, pvData, cbBuf);
689
690 RTCircBufReleaseWriteBlock(pStreamVRDE->In.pCircBuf, cbBuf);
691
692 if (cbBuf < cbData)
693 LogRel(("VRDE: Capturing audio data lost %zu bytes\n", cbData - cbBuf)); /** @todo Use an error counter. */
694
695 return VINF_SUCCESS; /** @todo r=andy How to tell the caller if we were not able to handle *all* input data? */
696}
697
698
699int AudioVRDE::onVRDEInputEnd(void *pvContext)
700{
701 RT_NOREF(pvContext);
702
703 return VINF_SUCCESS;
704}
705
706
707int AudioVRDE::onVRDEInputIntercept(bool fEnabled)
708{
709 RT_NOREF(fEnabled);
710 return VINF_SUCCESS; /* Never veto. */
711}
712
713
714/**
715 * Construct a VRDE audio driver instance.
716 *
717 * @copydoc FNPDMDRVCONSTRUCT
718 */
719/* static */
720DECLCALLBACK(int) AudioVRDE::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
721{
722 RT_NOREF(fFlags);
723
724 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
725 PDRVAUDIOVRDE pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIOVRDE);
726
727 AssertPtrReturn(pDrvIns, VERR_INVALID_POINTER);
728 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
729
730 LogRel(("Audio: Initializing VRDE driver\n"));
731 LogFlowFunc(("fFlags=0x%x\n", fFlags));
732
733 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
734 ("Configuration error: Not possible to attach anything to this driver!\n"),
735 VERR_PDM_DRVINS_NO_ATTACH);
736
737 /*
738 * Init the static parts.
739 */
740 pThis->pDrvIns = pDrvIns;
741 /* IBase */
742 pDrvIns->IBase.pfnQueryInterface = drvAudioVRDEQueryInterface;
743 /* IHostAudio */
744 PDMAUDIO_IHOSTAUDIO_CALLBACKS(drvAudioVRDE);
745
746 /*
747 * Get the ConsoleVRDPServer object pointer.
748 */
749 void *pvUser;
750 int rc = CFGMR3QueryPtr(pCfg, "ObjectVRDPServer", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
751 AssertMsgRCReturn(rc, ("Confguration error: No/bad \"ObjectVRDPServer\" value, rc=%Rrc\n", rc), rc);
752
753 /* CFGM tree saves the pointer to ConsoleVRDPServer in the Object node of AudioVRDE. */
754 pThis->pConsoleVRDPServer = (ConsoleVRDPServer *)pvUser;
755 pThis->cClients = 0;
756
757 /*
758 * Get the AudioVRDE object pointer.
759 */
760 pvUser = NULL;
761 rc = CFGMR3QueryPtr(pCfg, "Object", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
762 AssertMsgRCReturn(rc, ("Confguration error: No/bad \"Object\" value, rc=%Rrc\n", rc), rc);
763
764 pThis->pAudioVRDE = (AudioVRDE *)pvUser;
765 pThis->pAudioVRDE->mpDrv = pThis;
766
767 /*
768 * Get the interface for the above driver (DrvAudio) to make mixer/conversion calls.
769 * Described in CFGM tree.
770 */
771 pThis->pDrvAudio = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIAUDIOCONNECTOR);
772 AssertMsgReturn(pThis->pDrvAudio, ("Configuration error: No upper interface specified!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
773
774 return VINF_SUCCESS;
775}
776
777
778/**
779 * @interface_method_impl{PDMDRVREG,pfnDestruct}
780 */
781/* static */
782DECLCALLBACK(void) AudioVRDE::drvDestruct(PPDMDRVINS pDrvIns)
783{
784 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
785 PDRVAUDIOVRDE pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIOVRDE);
786 LogFlowFuncEnter();
787
788 /*
789 * If the AudioVRDE object is still alive, we must clear it's reference to
790 * us since we'll be invalid when we return from this method.
791 */
792 if (pThis->pAudioVRDE)
793 {
794 pThis->pAudioVRDE->mpDrv = NULL;
795 pThis->pAudioVRDE = NULL;
796 }
797}
798
799/**
800 * @interface_method_impl{PDMDRVREG,pfnAttach}
801 */
802/* static */
803DECLCALLBACK(int) AudioVRDE::drvAttach(PPDMDRVINS pDrvIns, uint32_t fFlags)
804{
805 RT_NOREF(pDrvIns, fFlags);
806
807 LogFlowFuncEnter();
808
809 return VINF_SUCCESS;
810}
811
812/**
813 * @interface_method_impl{PDMDRVREG,pfnDetach}
814 */
815/* static */
816DECLCALLBACK(void) AudioVRDE::drvDetach(PPDMDRVINS pDrvIns, uint32_t fFlags)
817{
818 RT_NOREF(pDrvIns, fFlags);
819
820 LogFlowFuncEnter();
821}
822
823
824/**
825 * VRDE audio driver registration record.
826 */
827const PDMDRVREG AudioVRDE::DrvReg =
828{
829 PDM_DRVREG_VERSION,
830 /* szName */
831 "AudioVRDE",
832 /* szRCMod */
833 "",
834 /* szR0Mod */
835 "",
836 /* pszDescription */
837 "Audio driver for VRDE backend",
838 /* fFlags */
839 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
840 /* fClass. */
841 PDM_DRVREG_CLASS_AUDIO,
842 /* cMaxInstances */
843 ~0U,
844 /* cbInstance */
845 sizeof(DRVAUDIOVRDE),
846 /* pfnConstruct */
847 AudioVRDE::drvConstruct,
848 /* pfnDestruct */
849 AudioVRDE::drvDestruct,
850 /* pfnRelocate */
851 NULL,
852 /* pfnIOCtl */
853 NULL,
854 /* pfnPowerOn */
855 NULL,
856 /* pfnReset */
857 NULL,
858 /* pfnSuspend */
859 NULL,
860 /* pfnResume */
861 NULL,
862 /* pfnAttach */
863 AudioVRDE::drvAttach,
864 /* pfnDetach */
865 AudioVRDE::drvDetach,
866 /* pfnPowerOff */
867 NULL,
868 /* pfnSoftReset */
869 NULL,
870 /* u32EndVersion */
871 PDM_DRVREG_VERSION
872};
873
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