VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/DrvAudioRec.cpp@ 88395

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

DrvAudioRec,DrvHostAudioValidationKit: doxygen fixes. some cleanups. [build fix] bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.2 KB
Line 
1/* $Id: DrvAudioRec.cpp 88395 2021-04-07 11:06:11Z vboxsync $ */
2/** @file
3 * Video recording audio backend for Main.
4 *
5 * This driver is part of Main and is responsible for providing audio
6 * data to Main's video capturing feature.
7 *
8 * The driver itself implements a PDM host audio backend, which in turn
9 * provides the driver with the required audio data and audio events.
10 *
11 * For now there is support for the following destinations (called "sinks"):
12 *
13 * - Direct writing of .webm files to the host.
14 * - Communicating with Main via the Console object to send the encoded audio data to.
15 * The Console object in turn then will route the data to the Display / video capturing interface then.
16 */
17
18/*
19 * Copyright (C) 2016-2020 Oracle Corporation
20 *
21 * This file is part of VirtualBox Open Source Edition (OSE), as
22 * available from http://www.virtualbox.org. This file is free software;
23 * you can redistribute it and/or modify it under the terms of the GNU
24 * General Public License (GPL) as published by the Free Software
25 * Foundation, in version 2 as it comes in the "COPYING" file of the
26 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
27 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
28 */
29
30/* This code makes use of the Opus codec (libopus):
31 *
32 * Copyright 2001-2011 Xiph.Org, Skype Limited, Octasic,
33 * Jean-Marc Valin, Timothy B. Terriberry,
34 * CSIRO, Gregory Maxwell, Mark Borgerding,
35 * Erik de Castro Lopo
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 *
41 * - Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 *
44 * - Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 *
48 * - Neither the name of Internet Society, IETF or IETF Trust, nor the
49 * names of specific contributors, may be used to endorse or promote
50 * products derived from this software without specific prior written
51 * permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
57 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 *
65 * Opus is subject to the royalty-free patent licenses which are
66 * specified at:
67 *
68 * Xiph.Org Foundation:
69 * https://datatracker.ietf.org/ipr/1524/
70 *
71 * Microsoft Corporation:
72 * https://datatracker.ietf.org/ipr/1914/
73 *
74 * Broadcom Corporation:
75 * https://datatracker.ietf.org/ipr/1526/
76 *
77 */
78
79
80/*********************************************************************************************************************************
81* Header Files *
82*********************************************************************************************************************************/
83#define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
84#include "LoggingNew.h"
85
86#include "DrvAudioRec.h"
87#include "ConsoleImpl.h"
88
89#include "WebMWriter.h"
90
91#include <iprt/mem.h>
92#include <iprt/cdefs.h>
93
94#include <VBox/vmm/cfgm.h>
95#include <VBox/vmm/pdmdrv.h>
96#include <VBox/vmm/pdmaudioifs.h>
97#include <VBox/vmm/pdmaudioinline.h>
98#include <VBox/err.h>
99
100#ifdef VBOX_WITH_LIBOPUS
101# include <opus.h>
102#endif
103
104
105/*********************************************************************************************************************************
106* Defines *
107*********************************************************************************************************************************/
108#define AVREC_OPUS_HZ_MAX 48000 /**< Maximum sample rate (in Hz) Opus can handle. */
109#define AVREC_OPUS_FRAME_MS_DEFAULT 20 /**< Default Opus frame size (in ms). */
110
111
112/*********************************************************************************************************************************
113* Structures and Typedefs *
114*********************************************************************************************************************************/
115
116/**
117 * Enumeration for specifying the recording container type.
118 */
119typedef enum AVRECCONTAINERTYPE
120{
121 /** Unknown / invalid container type. */
122 AVRECCONTAINERTYPE_UNKNOWN = 0,
123 /** Recorded data goes to Main / Console. */
124 AVRECCONTAINERTYPE_MAIN_CONSOLE = 1,
125 /** Recorded data will be written to a .webm file. */
126 AVRECCONTAINERTYPE_WEBM = 2
127} AVRECCONTAINERTYPE;
128
129/**
130 * Structure for keeping generic container parameters.
131 */
132typedef struct AVRECCONTAINERPARMS
133{
134 /** The container's type. */
135 AVRECCONTAINERTYPE enmType;
136 union
137 {
138 /** WebM file specifics. */
139 struct
140 {
141 /** Allocated file name to write .webm file to. Must be free'd. */
142 char *pszFile;
143 } WebM;
144 };
145
146} AVRECCONTAINERPARMS, *PAVRECCONTAINERPARMS;
147
148/**
149 * Structure for keeping container-specific data.
150 */
151typedef struct AVRECCONTAINER
152{
153 /** Generic container parameters. */
154 AVRECCONTAINERPARMS Parms;
155
156 union
157 {
158 struct
159 {
160 /** Pointer to Console. */
161 Console *pConsole;
162 } Main;
163
164 struct
165 {
166 /** Pointer to WebM container to write recorded audio data to.
167 * See the AVRECMODE enumeration for more information. */
168 WebMWriter *pWebM;
169 /** Assigned track number from WebM container. */
170 uint8_t uTrack;
171 } WebM;
172 };
173} AVRECCONTAINER, *PAVRECCONTAINER;
174
175/**
176 * Structure for keeping generic codec parameters.
177 */
178typedef struct AVRECCODECPARMS
179{
180 /** The codec's used PCM properties. */
181 PDMAUDIOPCMPROPS PCMProps;
182 /** The codec's bitrate. 0 if not used / cannot be specified. */
183 uint32_t uBitrate;
184
185} AVRECCODECPARMS, *PAVRECCODECPARMS;
186
187/**
188 * Structure for keeping codec-specific data.
189 */
190typedef struct AVRECCODEC
191{
192 /** Generic codec parameters. */
193 AVRECCODECPARMS Parms;
194 union
195 {
196#ifdef VBOX_WITH_LIBOPUS
197 struct
198 {
199 /** Encoder we're going to use. */
200 OpusEncoder *pEnc;
201 /** Time (in ms) an (encoded) frame takes.
202 *
203 * For Opus, valid frame sizes are:
204 * ms Frame size
205 * 2.5 120
206 * 5 240
207 * 10 480
208 * 20 (Default) 960
209 * 40 1920
210 * 60 2880
211 */
212 uint32_t msFrame;
213 /** The frame size in bytes (based on msFrame). */
214 uint32_t cbFrame;
215 /** The frame size in samples per frame (based on msFrame). */
216 uint32_t csFrame;
217 } Opus;
218#endif /* VBOX_WITH_LIBOPUS */
219 };
220
221#ifdef VBOX_WITH_STATISTICS /** @todo Make these real STAM values. */
222 struct
223 {
224 /** Number of frames encoded. */
225 uint64_t cEncFrames;
226 /** Total time (in ms) of already encoded audio data. */
227 uint64_t msEncTotal;
228 } STAM;
229#endif /* VBOX_WITH_STATISTICS */
230
231} AVRECCODEC, *PAVRECCODEC;
232
233typedef struct AVRECSINK
234{
235 /** @todo Add types for container / codec as soon as we implement more stuff. */
236
237 /** Container data to use for data processing. */
238 AVRECCONTAINER Con;
239 /** Codec data this sink uses for encoding. */
240 AVRECCODEC Codec;
241 /** Timestamp (in ms) of when the sink was created. */
242 uint64_t tsStartMs;
243} AVRECSINK, *PAVRECSINK;
244
245/**
246 * Audio video recording (output) stream.
247 */
248typedef struct AVRECSTREAM
249{
250 /** The stream's acquired configuration. */
251 PPDMAUDIOSTREAMCFG pCfg;
252 /** (Audio) frame buffer. */
253 PRTCIRCBUF pCircBuf;
254 /** Pointer to sink to use for writing. */
255 PAVRECSINK pSink;
256 /** Last encoded PTS (in ms). */
257 uint64_t uLastPTSMs;
258 /** Temporary buffer for the input (source) data to encode. */
259 void *pvSrcBuf;
260 /** Size (in bytes) of the temporary buffer holding the input (source) data to encode. */
261 size_t cbSrcBuf;
262 /** Temporary buffer for the encoded output (destination) data. */
263 void *pvDstBuf;
264 /** Size (in bytes) of the temporary buffer holding the encoded output (destination) data. */
265 size_t cbDstBuf;
266} AVRECSTREAM, *PAVRECSTREAM;
267
268/**
269 * Video recording audio driver instance data.
270 */
271typedef struct DRVAUDIORECORDING
272{
273 /** Pointer to audio video recording object. */
274 AudioVideoRec *pAudioVideoRec;
275 /** Pointer to the driver instance structure. */
276 PPDMDRVINS pDrvIns;
277 /** Pointer to host audio interface. */
278 PDMIHOSTAUDIO IHostAudio;
279 /** Pointer to the console object. */
280 ComPtr<Console> pConsole;
281 /** Pointer to the DrvAudio port interface that is above us. */
282 PPDMIAUDIOCONNECTOR pDrvAudio;
283 /** The driver's configured container parameters. */
284 AVRECCONTAINERPARMS ContainerParms;
285 /** The driver's configured codec parameters. */
286 AVRECCODECPARMS CodecParms;
287 /** The driver's sink for writing output to. */
288 AVRECSINK Sink;
289} DRVAUDIORECORDING, *PDRVAUDIORECORDING;
290
291/** Makes DRVAUDIORECORDING out of PDMIHOSTAUDIO. */
292#define PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface) /* (clang doesn't think it is a POD, thus _DYN.) */ \
293 ( (PDRVAUDIORECORDING)((uintptr_t)pInterface - RT_UOFFSETOF_DYN(DRVAUDIORECORDING, IHostAudio)) )
294
295/**
296 * Initializes a recording sink.
297 *
298 * @returns VBox status code.
299 * @param pThis Driver instance.
300 * @param pSink Sink to initialize.
301 * @param pConParms Container parameters to set.
302 * @param pCodecParms Codec parameters to set.
303 */
304static int avRecSinkInit(PDRVAUDIORECORDING pThis, PAVRECSINK pSink, PAVRECCONTAINERPARMS pConParms, PAVRECCODECPARMS pCodecParms)
305{
306 uint32_t uHz = PDMAudioPropsHz(&pCodecParms->PCMProps);
307 uint8_t const cbSample = PDMAudioPropsSampleSize(&pCodecParms->PCMProps);
308 uint8_t cChannels = PDMAudioPropsChannels(&pCodecParms->PCMProps);
309 uint32_t uBitrate = pCodecParms->uBitrate;
310
311 /* Opus only supports certain input sample rates in an efficient manner.
312 * So make sure that we use those by resampling the data to the requested rate. */
313 if (uHz > 24000) uHz = AVREC_OPUS_HZ_MAX;
314 else if (uHz > 16000) uHz = 24000;
315 else if (uHz > 12000) uHz = 16000;
316 else if (uHz > 8000 ) uHz = 12000;
317 else uHz = 8000;
318
319 if (cChannels > 2)
320 {
321 LogRel(("Recording: Warning: More than 2 (stereo) channels are not supported at the moment\n"));
322 cChannels = 2;
323 }
324
325 int orc;
326 OpusEncoder *pEnc = opus_encoder_create(uHz, cChannels, OPUS_APPLICATION_AUDIO, &orc);
327 if (orc != OPUS_OK)
328 {
329 LogRel(("Recording: Audio codec failed to initialize: %s\n", opus_strerror(orc)));
330 return VERR_AUDIO_BACKEND_INIT_FAILED;
331 }
332
333 AssertPtr(pEnc);
334
335 if (uBitrate) /* Only explicitly set the bitrate if we specified one. Otherwise let Opus decide. */
336 {
337 opus_encoder_ctl(pEnc, OPUS_SET_BITRATE(uBitrate));
338 if (orc != OPUS_OK)
339 {
340 opus_encoder_destroy(pEnc);
341 pEnc = NULL;
342
343 LogRel(("Recording: Audio codec failed to set bitrate (%RU32): %s\n", uBitrate, opus_strerror(orc)));
344 return VERR_AUDIO_BACKEND_INIT_FAILED;
345 }
346 }
347
348 const bool fUseVBR = true; /** Use Variable Bit Rate (VBR) by default. @todo Make this configurable? */
349
350 orc = opus_encoder_ctl(pEnc, OPUS_SET_VBR(fUseVBR ? 1 : 0));
351 if (orc != OPUS_OK)
352 {
353 opus_encoder_destroy(pEnc);
354 pEnc = NULL;
355
356 LogRel(("Recording: Audio codec failed to %s VBR mode: %s\n", fUseVBR ? "enable" : "disable", opus_strerror(orc)));
357 return VERR_AUDIO_BACKEND_INIT_FAILED;
358 }
359
360 int rc = VINF_SUCCESS;
361
362 try
363 {
364 switch (pConParms->enmType)
365 {
366 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
367 {
368 if (pThis->pConsole)
369 {
370 pSink->Con.Main.pConsole = pThis->pConsole;
371 }
372 else
373 rc = VERR_NOT_SUPPORTED;
374 break;
375 }
376
377 case AVRECCONTAINERTYPE_WEBM:
378 {
379 /* If we only record audio, create our own WebM writer instance here. */
380 if (!pSink->Con.WebM.pWebM) /* Do we already have our WebM writer instance? */
381 {
382 /** @todo Add sink name / number to file name. */
383 const char *pszFile = pSink->Con.Parms.WebM.pszFile;
384 AssertPtr(pszFile);
385
386 pSink->Con.WebM.pWebM = new WebMWriter();
387 rc = pSink->Con.WebM.pWebM->Open(pszFile,
388 /** @todo Add option to add some suffix if file exists instead of overwriting? */
389 RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE,
390 WebMWriter::AudioCodec_Opus, WebMWriter::VideoCodec_None);
391 if (RT_SUCCESS(rc))
392 {
393 rc = pSink->Con.WebM.pWebM->AddAudioTrack(uHz, cChannels, cbSample * 8 /* Bits */,
394 &pSink->Con.WebM.uTrack);
395 if (RT_SUCCESS(rc))
396 {
397 LogRel(("Recording: Recording audio to audio file '%s'\n", pszFile));
398 }
399 else
400 LogRel(("Recording: Error creating audio track for audio file '%s' (%Rrc)\n", pszFile, rc));
401 }
402 else
403 LogRel(("Recording: Error creating audio file '%s' (%Rrc)\n", pszFile, rc));
404 }
405 break;
406 }
407
408 default:
409 rc = VERR_NOT_SUPPORTED;
410 break;
411 }
412 }
413 catch (std::bad_alloc &)
414 {
415 rc = VERR_NO_MEMORY;
416 }
417
418 if (RT_SUCCESS(rc))
419 {
420 pSink->Con.Parms.enmType = pConParms->enmType;
421
422 PAVRECCODEC pCodec = &pSink->Codec;
423
424 PDMAudioPropsInit(&pCodec->Parms.PCMProps, cbSample, pCodecParms->PCMProps.fSigned, cChannels, uHz);
425 pCodec->Parms.uBitrate = uBitrate;
426
427 pCodec->Opus.pEnc = pEnc;
428 pCodec->Opus.msFrame = AVREC_OPUS_FRAME_MS_DEFAULT;
429
430 if (!pCodec->Opus.msFrame)
431 pCodec->Opus.msFrame = AVREC_OPUS_FRAME_MS_DEFAULT; /* 20ms by default; to prevent division by zero. */
432 pCodec->Opus.csFrame = pSink->Codec.Parms.PCMProps.uHz / (1000 /* s in ms */ / pSink->Codec.Opus.msFrame);
433 pCodec->Opus.cbFrame = PDMAudioPropsFramesToBytes(&pSink->Codec.Parms.PCMProps, pCodec->Opus.csFrame);
434
435#ifdef VBOX_WITH_STATISTICS
436 pSink->Codec.STAM.cEncFrames = 0;
437 pSink->Codec.STAM.msEncTotal = 0;
438#endif
439 pSink->tsStartMs = RTTimeMilliTS();
440 }
441 else
442 {
443 if (pEnc)
444 {
445 opus_encoder_destroy(pEnc);
446 pEnc = NULL;
447 }
448
449 LogRel(("Recording: Error creating sink (%Rrc)\n", rc));
450 }
451
452 return rc;
453}
454
455
456/**
457 * Shuts down (closes) a recording sink,
458 *
459 * @returns VBox status code.
460 * @param pSink Recording sink to shut down.
461 */
462static void avRecSinkShutdown(PAVRECSINK pSink)
463{
464 AssertPtrReturnVoid(pSink);
465
466#ifdef VBOX_WITH_LIBOPUS
467 if (pSink->Codec.Opus.pEnc)
468 {
469 opus_encoder_destroy(pSink->Codec.Opus.pEnc);
470 pSink->Codec.Opus.pEnc = NULL;
471 }
472#endif
473 switch (pSink->Con.Parms.enmType)
474 {
475 case AVRECCONTAINERTYPE_WEBM:
476 {
477 if (pSink->Con.WebM.pWebM)
478 {
479 LogRel2(("Recording: Finished recording audio to file '%s' (%zu bytes)\n",
480 pSink->Con.WebM.pWebM->GetFileName().c_str(), pSink->Con.WebM.pWebM->GetFileSize()));
481
482 int rc2 = pSink->Con.WebM.pWebM->Close();
483 AssertRC(rc2);
484
485 delete pSink->Con.WebM.pWebM;
486 pSink->Con.WebM.pWebM = NULL;
487 }
488 break;
489 }
490
491 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
492 default:
493 break;
494 }
495}
496
497
498/**
499 * Creates an audio output stream and associates it with the specified recording sink.
500 *
501 * @returns VBox status code.
502 * @param pThis Driver instance.
503 * @param pStreamAV Audio output stream to create.
504 * @param pSink Recording sink to associate audio output stream to.
505 * @param pCfgReq Requested configuration by the audio backend.
506 * @param pCfgAcq Acquired configuration by the audio output stream.
507 */
508static int avRecCreateStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV,
509 PAVRECSINK pSink, PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
510{
511 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
512 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
513 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
514 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
515 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
516
517 if (pCfgReq->u.enmDst != PDMAUDIOPLAYBACKDST_FRONT)
518 {
519 LogRel2(("Recording: Support for surround audio not implemented yet\n"));
520 AssertFailed();
521 return VERR_NOT_SUPPORTED;
522 }
523
524#ifdef VBOX_WITH_LIBOPUS
525 int rc = RTCircBufCreate(&pStreamAV->pCircBuf, pSink->Codec.Opus.cbFrame * 2 /* Use "double buffering" */);
526 if (RT_SUCCESS(rc))
527 {
528 size_t cbScratchBuf = pSink->Codec.Opus.cbFrame;
529 pStreamAV->pvSrcBuf = RTMemAlloc(cbScratchBuf);
530 if (pStreamAV->pvSrcBuf)
531 {
532 pStreamAV->cbSrcBuf = cbScratchBuf;
533 pStreamAV->pvDstBuf = RTMemAlloc(cbScratchBuf);
534 if (pStreamAV->pvDstBuf)
535 {
536 pStreamAV->cbDstBuf = cbScratchBuf;
537
538 pStreamAV->pSink = pSink; /* Assign sink to stream. */
539 pStreamAV->uLastPTSMs = 0;
540
541 /* Make sure to let the driver backend know that we need the audio data in
542 * a specific sampling rate Opus is optimized for. */
543/** @todo r=bird: pCfgAcq->Props isn't initialized at all, except for uHz... */
544 pCfgAcq->Props.uHz = pSink->Codec.Parms.PCMProps.uHz;
545// pCfgAcq->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfgAcq->Props.cbSample, pCfgAcq->Props.cChannels);
546
547 /* Every Opus frame marks a period for now. Optimize this later. */
548 pCfgAcq->Backend.cFramesPeriod = PDMAudioPropsMilliToFrames(&pCfgAcq->Props, pSink->Codec.Opus.msFrame);
549 pCfgAcq->Backend.cFramesBufferSize = PDMAudioPropsMilliToFrames(&pCfgAcq->Props, 100 /*ms*/); /** @todo Make this configurable. */
550 pCfgAcq->Backend.cFramesPreBuffering = pCfgAcq->Backend.cFramesPeriod * 2;
551 }
552 else
553 rc = VERR_NO_MEMORY;
554 }
555 else
556 rc = VERR_NO_MEMORY;
557 }
558#else
559 RT_NOREF(pThis, pSink, pStreamAV, pCfgReq, pCfgAcq);
560 int rc = VERR_NOT_SUPPORTED;
561#endif /* VBOX_WITH_LIBOPUS */
562
563 LogFlowFuncLeaveRC(rc);
564 return rc;
565}
566
567
568/**
569 * Destroys (closes) an audio output stream.
570 *
571 * @returns VBox status code.
572 * @param pThis Driver instance.
573 * @param pStreamAV Audio output stream to destroy.
574 */
575static int avRecDestroyStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV)
576{
577 RT_NOREF(pThis);
578
579 if (pStreamAV->pCircBuf)
580 {
581 RTCircBufDestroy(pStreamAV->pCircBuf);
582 pStreamAV->pCircBuf = NULL;
583 }
584
585 if (pStreamAV->pvSrcBuf)
586 {
587 Assert(pStreamAV->cbSrcBuf);
588 RTMemFree(pStreamAV->pvSrcBuf);
589 pStreamAV->pvSrcBuf = NULL;
590 pStreamAV->cbSrcBuf = 0;
591 }
592
593 if (pStreamAV->pvDstBuf)
594 {
595 Assert(pStreamAV->cbDstBuf);
596 RTMemFree(pStreamAV->pvDstBuf);
597 pStreamAV->pvDstBuf = NULL;
598 pStreamAV->cbDstBuf = 0;
599 }
600
601 return VINF_SUCCESS;
602}
603
604
605/**
606 * Controls an audio output stream
607 *
608 * @returns VBox status code.
609 * @param pThis Driver instance.
610 * @param pStreamAV Audio output stream to control.
611 * @param enmStreamCmd Stream command to issue.
612 */
613static int avRecControlStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV, PDMAUDIOSTREAMCMD enmStreamCmd)
614{
615 RT_NOREF(pThis, pStreamAV);
616
617 int rc = VINF_SUCCESS;
618
619 switch (enmStreamCmd)
620 {
621 case PDMAUDIOSTREAMCMD_ENABLE:
622 case PDMAUDIOSTREAMCMD_DISABLE:
623 case PDMAUDIOSTREAMCMD_RESUME:
624 case PDMAUDIOSTREAMCMD_PAUSE:
625 break;
626
627 default:
628 rc = VERR_NOT_SUPPORTED;
629 break;
630 }
631
632 return rc;
633}
634
635
636/**
637 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
638 */
639static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
640 void *pvBuf, uint32_t uBufSize, uint32_t *puRead)
641{
642 RT_NOREF(pInterface, pStream, pvBuf, uBufSize);
643
644 if (puRead)
645 *puRead = 0;
646
647 return VINF_SUCCESS;
648}
649
650
651/**
652 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
653 */
654static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
655 const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
656{
657 RT_NOREF(pInterface);
658 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
659 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
660 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
661 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
662 AssertReturn(pcbWritten, VERR_INVALID_PARAMETER);
663
664 int rc = VINF_SUCCESS;
665
666 uint32_t cbWrittenTotal = 0;
667
668 /*
669 * Call the encoder with the data.
670 */
671#ifdef VBOX_WITH_LIBOPUS
672 PAVRECSINK pSink = pStreamAV->pSink;
673 AssertPtr(pSink);
674 PAVRECCODEC pCodec = &pSink->Codec;
675 AssertPtr(pCodec);
676 PRTCIRCBUF pCircBuf = pStreamAV->pCircBuf;
677 AssertPtr(pCircBuf);
678
679 void *pvCircBuf;
680 size_t cbCircBuf;
681
682 uint32_t cbToWrite = cbBuf;
683
684 /*
685 * Fetch as much as we can into our internal ring buffer.
686 */
687 while ( cbToWrite
688 && RTCircBufFree(pCircBuf))
689 {
690 RTCircBufAcquireWriteBlock(pCircBuf, cbToWrite, &pvCircBuf, &cbCircBuf);
691
692 if (cbCircBuf)
693 {
694 memcpy(pvCircBuf, (uint8_t *)pvBuf + cbWrittenTotal, cbCircBuf),
695 cbWrittenTotal += (uint32_t)cbCircBuf;
696 Assert(cbToWrite >= cbCircBuf);
697 cbToWrite -= (uint32_t)cbCircBuf;
698 }
699
700 RTCircBufReleaseWriteBlock(pCircBuf, cbCircBuf);
701
702 if ( RT_FAILURE(rc)
703 || !cbCircBuf)
704 {
705 break;
706 }
707 }
708
709 /*
710 * Process our internal ring buffer and encode the data.
711 */
712
713 uint32_t cbSrc;
714
715 /* Only encode data if we have data for the given time period (or more). */
716 while (RTCircBufUsed(pCircBuf) >= pCodec->Opus.cbFrame)
717 {
718 LogFunc(("cbAvail=%zu, csFrame=%RU32, cbFrame=%RU32\n",
719 RTCircBufUsed(pCircBuf), pCodec->Opus.csFrame, pCodec->Opus.cbFrame));
720
721 cbSrc = 0;
722
723 while (cbSrc < pCodec->Opus.cbFrame)
724 {
725 RTCircBufAcquireReadBlock(pCircBuf, pCodec->Opus.cbFrame - cbSrc, &pvCircBuf, &cbCircBuf);
726
727 if (cbCircBuf)
728 {
729 memcpy((uint8_t *)pStreamAV->pvSrcBuf + cbSrc, pvCircBuf, cbCircBuf);
730
731 cbSrc += (uint32_t)cbCircBuf;
732 Assert(cbSrc <= pStreamAV->cbSrcBuf);
733 }
734
735 RTCircBufReleaseReadBlock(pCircBuf, cbCircBuf);
736
737 if (!cbCircBuf)
738 break;
739 }
740
741# ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
742 RTFILE fh;
743 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm",
744 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
745 RTFileWrite(fh, pStreamAV->pvSrcBuf, cbSrc, NULL);
746 RTFileClose(fh);
747# endif
748
749 Assert(cbSrc == pCodec->Opus.cbFrame);
750
751 /*
752 * Opus always encodes PER "OPUS FRAME", that is, exactly 2.5, 5, 10, 20, 40 or 60 ms of audio data.
753 *
754 * A packet can have up to 120ms worth of audio data.
755 * Anything > 120ms of data will result in a "corrupted package" error message by
756 * by decoding application.
757 */
758
759 /* Call the encoder to encode one "Opus frame" per iteration. */
760 opus_int32 cbWritten = opus_encode(pSink->Codec.Opus.pEnc,
761 (opus_int16 *)pStreamAV->pvSrcBuf, pCodec->Opus.csFrame,
762 (uint8_t *)pStreamAV->pvDstBuf, (opus_int32)pStreamAV->cbDstBuf);
763 if (cbWritten > 0)
764 {
765 /* Get overall frames encoded. */
766 const uint32_t cEncFrames = opus_packet_get_nb_frames((uint8_t *)pStreamAV->pvDstBuf, cbWritten);
767
768# ifdef VBOX_WITH_STATISTICS
769 pSink->Codec.STAM.cEncFrames += cEncFrames;
770 pSink->Codec.STAM.msEncTotal += pSink->Codec.Opus.msFrame * cEncFrames;
771# endif
772 Assert((uint32_t)cbWritten <= (uint32_t)pStreamAV->cbDstBuf);
773 const uint32_t cbDst = RT_MIN((uint32_t)cbWritten, (uint32_t)pStreamAV->cbDstBuf);
774
775 Assert(cEncFrames == 1);
776
777 if (pStreamAV->uLastPTSMs == 0)
778 pStreamAV->uLastPTSMs = RTTimeProgramMilliTS(); /* We want the absolute time (in ms) since program start. */
779
780 const uint64_t uDurationMs = pSink->Codec.Opus.msFrame * cEncFrames;
781 const uint64_t uPTSMs = pStreamAV->uLastPTSMs;
782
783 pStreamAV->uLastPTSMs += uDurationMs;
784
785 switch (pSink->Con.Parms.enmType)
786 {
787 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
788 {
789 HRESULT hr = pSink->Con.Main.pConsole->i_recordingSendAudio(pStreamAV->pvDstBuf, cbDst, uPTSMs);
790 Assert(hr == S_OK);
791 RT_NOREF(hr);
792
793 break;
794 }
795
796 case AVRECCONTAINERTYPE_WEBM:
797 {
798 WebMWriter::BlockData_Opus blockData = { pStreamAV->pvDstBuf, cbDst, uPTSMs };
799 rc = pSink->Con.WebM.pWebM->WriteBlock(pSink->Con.WebM.uTrack, &blockData, sizeof(blockData));
800 AssertRC(rc);
801
802 break;
803 }
804
805 default:
806 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
807 break;
808 }
809 }
810 else if (cbWritten < 0)
811 {
812 AssertMsgFailed(("Encoding failed: %s\n", opus_strerror(cbWritten)));
813 rc = VERR_INVALID_PARAMETER;
814 }
815
816 if (RT_FAILURE(rc))
817 break;
818 }
819
820 *pcbWritten = cbWrittenTotal;
821#else
822 /* Report back all data as being processed. */
823 *pcbWritten = cbBuf;
824
825 rc = VERR_NOT_SUPPORTED;
826#endif /* VBOX_WITH_LIBOPUS */
827
828 LogFlowFunc(("csReadTotal=%RU32, rc=%Rrc\n", cbWrittenTotal, rc));
829 return rc;
830}
831
832
833/**
834 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
835 */
836static DECLCALLBACK(int) drvAudioVideoRecHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
837{
838 RT_NOREF(pInterface);
839 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
840
841 RTStrPrintf2(pBackendCfg->szName, sizeof(pBackendCfg->szName), "VideoRec");
842
843 pBackendCfg->cbStreamOut = sizeof(AVRECSTREAM);
844 pBackendCfg->cbStreamIn = 0;
845 pBackendCfg->cMaxStreamsIn = 0;
846 pBackendCfg->cMaxStreamsOut = UINT32_MAX;
847
848 return VINF_SUCCESS;
849}
850
851
852/**
853 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
854 */
855static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvAudioVideoRecHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
856{
857 RT_NOREF(enmDir);
858 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
859
860 return PDMAUDIOBACKENDSTS_RUNNING;
861}
862
863
864/**
865 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
866 */
867static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
868 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
869{
870 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
871 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
872 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
873
874 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
875 return VERR_NOT_SUPPORTED;
876
877 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
878
879 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
880 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
881
882 /* For now we only have one sink, namely the driver's one.
883 * Later each stream could have its own one, to e.g. router different stream to different sinks .*/
884 PAVRECSINK pSink = &pThis->Sink;
885
886 int rc = avRecCreateStreamOut(pThis, pStreamAV, pSink, pCfgReq, pCfgAcq);
887 if (RT_SUCCESS(rc))
888 {
889 pStreamAV->pCfg = PDMAudioStrmCfgDup(pCfgAcq);
890 if (!pStreamAV->pCfg)
891 rc = VERR_NO_MEMORY;
892 }
893
894 return rc;
895}
896
897
898/**
899 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
900 */
901static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
902{
903 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
904 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
905
906 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
907 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
908
909 if (!pStreamAV->pCfg) /* Not (yet) configured? Skip. */
910 return VINF_SUCCESS;
911
912 int rc = VINF_SUCCESS;
913
914 if (pStreamAV->pCfg->enmDir == PDMAUDIODIR_OUT)
915 rc = avRecDestroyStreamOut(pThis, pStreamAV);
916
917 if (RT_SUCCESS(rc))
918 {
919 PDMAudioStrmCfgFree(pStreamAV->pCfg);
920 pStreamAV->pCfg = NULL;
921 }
922
923 return rc;
924}
925
926
927/**
928 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
929 */
930static DECLCALLBACK(int) drvAudioVideoRecHA_StreamControl(PPDMIHOSTAUDIO pInterface,
931 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
932{
933 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
934 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
935
936 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
937 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
938
939 if (!pStreamAV->pCfg) /* Not (yet) configured? Skip. */
940 return VINF_SUCCESS;
941
942 if (pStreamAV->pCfg->enmDir == PDMAUDIODIR_OUT)
943 return avRecControlStreamOut(pThis, pStreamAV, enmStreamCmd);
944
945 return VINF_SUCCESS;
946}
947
948
949/**
950 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
951 */
952static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
953{
954 RT_NOREF(pInterface, pStream);
955
956 return 0; /* Video capturing does not provide any input. */
957}
958
959
960/**
961 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
962 */
963static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
964{
965 RT_NOREF(pInterface, pStream);
966
967 return UINT32_MAX;
968}
969
970
971/**
972 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
973 */
974static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvAudioVideoRecHA_StreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
975{
976 RT_NOREF(pInterface, pStream);
977
978 return PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED | PDMAUDIOSTREAMSTS_FLAGS_ENABLED;
979}
980
981
982/**
983 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
984 */
985static DECLCALLBACK(void *) drvAudioVideoRecQueryInterface(PPDMIBASE pInterface, const char *pszIID)
986{
987 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
988 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
989
990 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
991 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
992 return NULL;
993}
994
995
996AudioVideoRec::AudioVideoRec(Console *pConsole)
997 : AudioDriver(pConsole)
998 , mpDrv(NULL)
999{
1000}
1001
1002
1003AudioVideoRec::~AudioVideoRec(void)
1004{
1005 if (mpDrv)
1006 {
1007 mpDrv->pAudioVideoRec = NULL;
1008 mpDrv = NULL;
1009 }
1010}
1011
1012
1013/**
1014 * Applies a video recording configuration to this driver instance.
1015 *
1016 * @returns VBox status code.
1017 * @param Settings Capturing configuration to apply.
1018 */
1019int AudioVideoRec::applyConfiguration(const settings::RecordingSettings &Settings)
1020{
1021 /** @todo Do some validation here. */
1022 mVideoRecCfg = Settings; /* Note: Does have an own copy operator. */
1023 return VINF_SUCCESS;
1024}
1025
1026
1027/**
1028 * @copydoc AudioDriver::configureDriver
1029 */
1030int AudioVideoRec::configureDriver(PCFGMNODE pLunCfg)
1031{
1032 int rc = CFGMR3InsertInteger(pLunCfg, "Object", (uintptr_t)mpConsole->i_recordingGetAudioDrv());
1033 AssertRCReturn(rc, rc);
1034 rc = CFGMR3InsertInteger(pLunCfg, "ObjectConsole", (uintptr_t)mpConsole);
1035 AssertRCReturn(rc, rc);
1036
1037 /** @todo For now we're using the configuration of the first screen here audio-wise. */
1038 Assert(mVideoRecCfg.mapScreens.size() >= 1);
1039 const settings::RecordingScreenSettings &Screen0Settings = mVideoRecCfg.mapScreens[0];
1040
1041 rc = CFGMR3InsertInteger(pLunCfg, "ContainerType", (uint64_t)Screen0Settings.enmDest);
1042 AssertRCReturn(rc, rc);
1043 if (Screen0Settings.enmDest == RecordingDestination_File)
1044 {
1045 rc = CFGMR3InsertString(pLunCfg, "ContainerFileName", Utf8Str(Screen0Settings.File.strName).c_str());
1046 AssertRCReturn(rc, rc);
1047 }
1048 rc = CFGMR3InsertInteger(pLunCfg, "CodecHz", Screen0Settings.Audio.uHz);
1049 AssertRCReturn(rc, rc);
1050 rc = CFGMR3InsertInteger(pLunCfg, "CodecBits", Screen0Settings.Audio.cBits);
1051 AssertRCReturn(rc, rc);
1052 rc = CFGMR3InsertInteger(pLunCfg, "CodecChannels", Screen0Settings.Audio.cChannels);
1053 AssertRCReturn(rc, rc);
1054 rc = CFGMR3InsertInteger(pLunCfg, "CodecBitrate", 0); /* Let Opus decide for now. */
1055 AssertRCReturn(rc, rc);
1056
1057 return AudioDriver::configureDriver(pLunCfg);
1058}
1059
1060
1061/**
1062 * @interface_method_impl{PDMDRVREG,pfnPowerOff}
1063 */
1064/*static*/ DECLCALLBACK(void) AudioVideoRec::drvPowerOff(PPDMDRVINS pDrvIns)
1065{
1066 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1067 LogFlowFuncEnter();
1068 avRecSinkShutdown(&pThis->Sink);
1069}
1070
1071
1072/**
1073 * @interface_method_impl{PDMDRVREG,pfnDestruct}
1074 */
1075/*static*/ DECLCALLBACK(void) AudioVideoRec::drvDestruct(PPDMDRVINS pDrvIns)
1076{
1077 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1078 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1079
1080 LogFlowFuncEnter();
1081
1082 switch (pThis->ContainerParms.enmType)
1083 {
1084 case AVRECCONTAINERTYPE_WEBM:
1085 {
1086 avRecSinkShutdown(&pThis->Sink);
1087 RTStrFree(pThis->ContainerParms.WebM.pszFile);
1088 break;
1089 }
1090
1091 default:
1092 break;
1093 }
1094
1095 /*
1096 * If the AudioVideoRec object is still alive, we must clear it's reference to
1097 * us since we'll be invalid when we return from this method.
1098 */
1099 if (pThis->pAudioVideoRec)
1100 {
1101 pThis->pAudioVideoRec->mpDrv = NULL;
1102 pThis->pAudioVideoRec = NULL;
1103 }
1104
1105 LogFlowFuncLeave();
1106}
1107
1108
1109/**
1110 * Construct a audio video recording driver instance.
1111 *
1112 * @copydoc FNPDMDRVCONSTRUCT
1113 */
1114/*static*/ DECLCALLBACK(int) AudioVideoRec::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1115{
1116 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1117 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1118 RT_NOREF(fFlags);
1119
1120 LogRel(("Audio: Initializing video recording audio driver\n"));
1121 LogFlowFunc(("fFlags=0x%x\n", fFlags));
1122
1123 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1124 ("Configuration error: Not possible to attach anything to this driver!\n"),
1125 VERR_PDM_DRVINS_NO_ATTACH);
1126
1127 /*
1128 * Init the static parts.
1129 */
1130 pThis->pDrvIns = pDrvIns;
1131 /* IBase */
1132 pDrvIns->IBase.pfnQueryInterface = drvAudioVideoRecQueryInterface;
1133 /* IHostAudio */
1134 pThis->IHostAudio.pfnGetConfig = drvAudioVideoRecHA_GetConfig;
1135 pThis->IHostAudio.pfnGetStatus = drvAudioVideoRecHA_GetStatus;
1136 pThis->IHostAudio.pfnStreamCreate = drvAudioVideoRecHA_StreamCreate;
1137 pThis->IHostAudio.pfnStreamDestroy = drvAudioVideoRecHA_StreamDestroy;
1138 pThis->IHostAudio.pfnStreamControl = drvAudioVideoRecHA_StreamControl;
1139 pThis->IHostAudio.pfnStreamGetReadable = drvAudioVideoRecHA_StreamGetReadable;
1140 pThis->IHostAudio.pfnStreamGetWritable = drvAudioVideoRecHA_StreamGetWritable;
1141 pThis->IHostAudio.pfnStreamGetStatus = drvAudioVideoRecHA_StreamGetStatus;
1142 pThis->IHostAudio.pfnStreamPlay = drvAudioVideoRecHA_StreamPlay;
1143 pThis->IHostAudio.pfnStreamCapture = drvAudioVideoRecHA_StreamCapture;
1144 pThis->IHostAudio.pfnGetDevices = NULL;
1145 pThis->IHostAudio.pfnStreamGetPending = NULL;
1146
1147 /*
1148 * Get the Console object pointer.
1149 */
1150 void *pvUser;
1151 int rc = CFGMR3QueryPtr(pCfg, "ObjectConsole", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1152 AssertRCReturn(rc, rc);
1153
1154 /* CFGM tree saves the pointer to Console in the Object node of AudioVideoRec. */
1155 pThis->pConsole = (Console *)pvUser;
1156 AssertReturn(!pThis->pConsole.isNull(), VERR_INVALID_POINTER);
1157
1158 /*
1159 * Get the pointer to the audio driver instance.
1160 */
1161 rc = CFGMR3QueryPtr(pCfg, "Object", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1162 AssertRCReturn(rc, rc);
1163
1164 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1165 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1166
1167 /*
1168 * Get the recording container and codec parameters from the audio driver instance.
1169 */
1170 PAVRECCONTAINERPARMS pConParams = &pThis->ContainerParms;
1171 PAVRECCODECPARMS pCodecParms = &pThis->CodecParms;
1172
1173 RT_ZERO(pThis->ContainerParms);
1174 RT_ZERO(pThis->CodecParms);
1175
1176 rc = CFGMR3QueryU32(pCfg, "ContainerType", (uint32_t *)&pConParams->enmType);
1177 AssertRCReturn(rc, rc);
1178
1179 switch (pConParams->enmType)
1180 {
1181 case AVRECCONTAINERTYPE_WEBM:
1182 rc = CFGMR3QueryStringAlloc(pCfg, "ContainerFileName", &pConParams->WebM.pszFile);
1183 AssertRCReturn(rc, rc);
1184 break;
1185
1186 default:
1187 break;
1188 }
1189
1190 uint32_t uHz = 0;
1191 rc = CFGMR3QueryU32(pCfg, "CodecHz", &uHz);
1192 AssertRCReturn(rc, rc);
1193
1194 uint8_t cSampleBits = 0;
1195 rc = CFGMR3QueryU8(pCfg, "CodecBits", &cSampleBits); /** @todo CodecBits != CodecBytes */
1196 AssertRCReturn(rc, rc);
1197
1198 uint8_t cChannels = 0;
1199 rc = CFGMR3QueryU8(pCfg, "CodecChannels", &cChannels);
1200 AssertRCReturn(rc, rc);
1201
1202 PDMAudioPropsInit(&pCodecParms->PCMProps, cSampleBits / 8, true /*fSigned*/, cChannels, uHz);
1203 AssertMsgReturn(PDMAudioPropsAreValid(&pCodecParms->PCMProps),
1204 ("Configuration error: Audio configuration is invalid!\n"), VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES); /** @todo wrong status code. */
1205
1206 rc = CFGMR3QueryU32(pCfg, "CodecBitrate", &pCodecParms->uBitrate);
1207 AssertRCReturn(rc, rc);
1208
1209 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1210 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1211
1212 pThis->pAudioVideoRec->mpDrv = pThis;
1213
1214 /*
1215 * Get the interface for the above driver (DrvAudio) to make mixer/conversion calls.
1216 * Described in CFGM tree.
1217 */
1218/** @todo r=bird: What on earth do you think you need this for?!? It's not an
1219 * interface lower drivers are supposed to be messing with! */
1220 pThis->pDrvAudio = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIAUDIOCONNECTOR);
1221 AssertMsgReturn(pThis->pDrvAudio, ("Configuration error: No upper interface specified!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
1222
1223#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
1224 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.webm");
1225 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm");
1226#endif
1227
1228 /*
1229 * Init the recording sink.
1230 */
1231 LogRel(("Recording: Audio driver is using %RU32Hz, %RU16bit, %RU8 channel%s\n",
1232 PDMAudioPropsHz(&pThis->CodecParms.PCMProps), PDMAudioPropsSampleBits(&pThis->CodecParms.PCMProps),
1233 PDMAudioPropsChannels(&pThis->CodecParms.PCMProps), PDMAudioPropsChannels(&pThis->CodecParms.PCMProps) == 1 ? "" : "s"));
1234
1235 rc = avRecSinkInit(pThis, &pThis->Sink, &pThis->ContainerParms, &pThis->CodecParms);
1236 if (RT_SUCCESS(rc))
1237 LogRel2(("Recording: Audio recording driver initialized\n"));
1238 else
1239 LogRel(("Recording: Audio recording driver initialization failed: %Rrc\n", rc));
1240
1241 return rc;
1242}
1243
1244
1245/**
1246 * Video recording audio driver registration record.
1247 */
1248const PDMDRVREG AudioVideoRec::DrvReg =
1249{
1250 PDM_DRVREG_VERSION,
1251 /* szName */
1252 "AudioVideoRec",
1253 /* szRCMod */
1254 "",
1255 /* szR0Mod */
1256 "",
1257 /* pszDescription */
1258 "Audio driver for video recording",
1259 /* fFlags */
1260 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1261 /* fClass. */
1262 PDM_DRVREG_CLASS_AUDIO,
1263 /* cMaxInstances */
1264 ~0U,
1265 /* cbInstance */
1266 sizeof(DRVAUDIORECORDING),
1267 /* pfnConstruct */
1268 AudioVideoRec::drvConstruct,
1269 /* pfnDestruct */
1270 AudioVideoRec::drvDestruct,
1271 /* pfnRelocate */
1272 NULL,
1273 /* pfnIOCtl */
1274 NULL,
1275 /* pfnPowerOn */
1276 NULL,
1277 /* pfnReset */
1278 NULL,
1279 /* pfnSuspend */
1280 NULL,
1281 /* pfnResume */
1282 NULL,
1283 /* pfnAttach */
1284 NULL,
1285 /* pfnDetach */
1286 NULL,
1287 /* pfnPowerOff */
1288 AudioVideoRec::drvPowerOff,
1289 /* pfnSoftReset */
1290 NULL,
1291 /* u32EndVersion */
1292 PDM_DRVREG_VERSION
1293};
1294
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