VirtualBox

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

Last change on this file since 94521 was 94326, checked in by vboxsync, 2 years ago

Main/DrvAudioRec: Drop passing pointers through CFGM in favor of using VMM2USERMETHODS::pfnQueryGenericObject, bugref:10053

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 43.1 KB
Line 
1/* $Id: DrvAudioRec.cpp 94326 2022-03-22 15:14:54Z 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-2022 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/vmm/vmmr3vtable.h>
99#include <VBox/err.h>
100
101#ifdef VBOX_WITH_LIBOPUS
102# include <opus.h>
103#endif
104
105
106/*********************************************************************************************************************************
107* Defines *
108*********************************************************************************************************************************/
109#define AVREC_OPUS_HZ_MAX 48000 /**< Maximum sample rate (in Hz) Opus can handle. */
110#define AVREC_OPUS_FRAME_MS_DEFAULT 20 /**< Default Opus frame size (in ms). */
111
112
113/*********************************************************************************************************************************
114* Structures and Typedefs *
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 Register these 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 } Stats;
229#endif
230} AVRECCODEC, *PAVRECCODEC;
231
232typedef struct AVRECSINK
233{
234 /** @todo Add types for container / codec as soon as we implement more stuff. */
235
236 /** Container data to use for data processing. */
237 AVRECCONTAINER Con;
238 /** Codec data this sink uses for encoding. */
239 AVRECCODEC Codec;
240 /** Timestamp (in ms) of when the sink was created. */
241 uint64_t tsStartMs;
242} AVRECSINK, *PAVRECSINK;
243
244/**
245 * Audio video recording (output) stream.
246 */
247typedef struct AVRECSTREAM
248{
249 /** Common part. */
250 PDMAUDIOBACKENDSTREAM Core;
251 /** The stream's acquired configuration. */
252 PDMAUDIOSTREAMCFG Cfg;
253 /** (Audio) frame buffer. */
254 PRTCIRCBUF pCircBuf;
255 /** Pointer to sink to use for writing. */
256 PAVRECSINK pSink;
257 /** Last encoded PTS (in ms). */
258 uint64_t uLastPTSMs;
259 /** Temporary buffer for the input (source) data to encode. */
260 void *pvSrcBuf;
261 /** Size (in bytes) of the temporary buffer holding the input (source) data to encode. */
262 size_t cbSrcBuf;
263 /** Temporary buffer for the encoded output (destination) data. */
264 void *pvDstBuf;
265 /** Size (in bytes) of the temporary buffer holding the encoded output (destination) data. */
266 size_t cbDstBuf;
267} AVRECSTREAM, *PAVRECSTREAM;
268
269/**
270 * Video recording audio driver instance data.
271 */
272typedef struct DRVAUDIORECORDING
273{
274 /** Pointer to audio video recording object. */
275 AudioVideoRec *pAudioVideoRec;
276 /** Pointer to the driver instance structure. */
277 PPDMDRVINS pDrvIns;
278 /** Pointer to host audio interface. */
279 PDMIHOSTAUDIO IHostAudio;
280 /** Pointer to the console object. */
281 ComPtr<Console> pConsole;
282 /** Pointer to the DrvAudio port interface that is above us. */
283 PPDMIAUDIOCONNECTOR pDrvAudio;
284 /** The driver's configured container parameters. */
285 AVRECCONTAINERPARMS ContainerParms;
286 /** The driver's configured codec parameters. */
287 AVRECCODECPARMS CodecParms;
288 /** The driver's sink for writing output to. */
289 AVRECSINK Sink;
290} DRVAUDIORECORDING, *PDRVAUDIORECORDING;
291
292
293AudioVideoRec::AudioVideoRec(Console *pConsole)
294 : AudioDriver(pConsole)
295 , mpDrv(NULL)
296{
297}
298
299
300AudioVideoRec::~AudioVideoRec(void)
301{
302 if (mpDrv)
303 {
304 mpDrv->pAudioVideoRec = NULL;
305 mpDrv = NULL;
306 }
307}
308
309
310/**
311 * Applies a video recording configuration to this driver instance.
312 *
313 * @returns VBox status code.
314 * @param Settings Capturing configuration to apply.
315 */
316int AudioVideoRec::applyConfiguration(const settings::RecordingSettings &Settings)
317{
318 /** @todo Do some validation here. */
319 mVideoRecCfg = Settings; /* Note: Does have an own copy operator. */
320 return VINF_SUCCESS;
321}
322
323
324int AudioVideoRec::configureDriver(PCFGMNODE pLunCfg, PCVMMR3VTABLE pVMM)
325{
326 /** @todo For now we're using the configuration of the first screen here audio-wise. */
327 Assert(mVideoRecCfg.mapScreens.size() >= 1);
328 const settings::RecordingScreenSettings &Screen0Settings = mVideoRecCfg.mapScreens[0];
329
330 int rc = pVMM->pfnCFGMR3InsertInteger(pLunCfg, "ContainerType", (uint64_t)Screen0Settings.enmDest);
331 AssertRCReturn(rc, rc);
332 if (Screen0Settings.enmDest == RecordingDestination_File)
333 {
334 rc = pVMM->pfnCFGMR3InsertString(pLunCfg, "ContainerFileName", Utf8Str(Screen0Settings.File.strName).c_str());
335 AssertRCReturn(rc, rc);
336 }
337 rc = pVMM->pfnCFGMR3InsertInteger(pLunCfg, "CodecHz", Screen0Settings.Audio.uHz);
338 AssertRCReturn(rc, rc);
339 rc = pVMM->pfnCFGMR3InsertInteger(pLunCfg, "CodecBits", Screen0Settings.Audio.cBits);
340 AssertRCReturn(rc, rc);
341 rc = pVMM->pfnCFGMR3InsertInteger(pLunCfg, "CodecChannels", Screen0Settings.Audio.cChannels);
342 AssertRCReturn(rc, rc);
343 rc = pVMM->pfnCFGMR3InsertInteger(pLunCfg, "CodecBitrate", 0); /* Let Opus decide for now. */
344 AssertRCReturn(rc, rc);
345
346 return AudioDriver::configureDriver(pLunCfg, pVMM);
347}
348
349
350/*********************************************************************************************************************************
351* PDMIHOSTAUDIO *
352*********************************************************************************************************************************/
353
354/**
355 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
356 */
357static DECLCALLBACK(int) drvAudioVideoRecHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
358{
359 RT_NOREF(pInterface);
360 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
361
362 /*
363 * Fill in the config structure.
364 */
365 RTStrCopy(pBackendCfg->szName, sizeof(pBackendCfg->szName), "VideoRec");
366 pBackendCfg->cbStream = sizeof(AVRECSTREAM);
367 pBackendCfg->fFlags = 0;
368 pBackendCfg->cMaxStreamsIn = 0;
369 pBackendCfg->cMaxStreamsOut = UINT32_MAX;
370
371 return VINF_SUCCESS;
372}
373
374
375/**
376 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
377 */
378static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvAudioVideoRecHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
379{
380 RT_NOREF(pInterface, enmDir);
381 return PDMAUDIOBACKENDSTS_RUNNING;
382}
383
384
385/**
386 * Creates an audio output stream and associates it with the specified recording sink.
387 *
388 * @returns VBox status code.
389 * @param pThis Driver instance.
390 * @param pStreamAV Audio output stream to create.
391 * @param pSink Recording sink to associate audio output stream to.
392 * @param pCfgReq Requested configuration by the audio backend.
393 * @param pCfgAcq Acquired configuration by the audio output stream.
394 */
395static int avRecCreateStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV,
396 PAVRECSINK pSink, PCPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
397{
398 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
399 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
400 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
401 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
402 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
403
404 if (pCfgReq->enmPath != PDMAUDIOPATH_OUT_FRONT)
405 {
406 LogRel2(("Recording: Support for surround audio not implemented yet\n"));
407 AssertFailed();
408 return VERR_NOT_SUPPORTED;
409 }
410
411#ifdef VBOX_WITH_LIBOPUS
412 int rc = RTCircBufCreate(&pStreamAV->pCircBuf, pSink->Codec.Opus.cbFrame * 2 /* Use "double buffering" */);
413 if (RT_SUCCESS(rc))
414 {
415 size_t cbScratchBuf = pSink->Codec.Opus.cbFrame;
416 pStreamAV->pvSrcBuf = RTMemAlloc(cbScratchBuf);
417 if (pStreamAV->pvSrcBuf)
418 {
419 pStreamAV->cbSrcBuf = cbScratchBuf;
420 pStreamAV->pvDstBuf = RTMemAlloc(cbScratchBuf);
421 if (pStreamAV->pvDstBuf)
422 {
423 pStreamAV->cbDstBuf = cbScratchBuf;
424
425 pStreamAV->pSink = pSink; /* Assign sink to stream. */
426 pStreamAV->uLastPTSMs = 0;
427
428 /* Make sure to let the driver backend know that we need the audio data in
429 * a specific sampling rate Opus is optimized for. */
430/** @todo r=bird: pCfgAcq->Props isn't initialized at all, except for uHz... */
431 pCfgAcq->Props.uHz = pSink->Codec.Parms.PCMProps.uHz;
432// pCfgAcq->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfgAcq->Props.cbSample, pCfgAcq->Props.cChannels);
433
434 /* Every Opus frame marks a period for now. Optimize this later. */
435 pCfgAcq->Backend.cFramesPeriod = PDMAudioPropsMilliToFrames(&pCfgAcq->Props, pSink->Codec.Opus.msFrame);
436 pCfgAcq->Backend.cFramesBufferSize = PDMAudioPropsMilliToFrames(&pCfgAcq->Props, 100 /*ms*/); /** @todo Make this configurable. */
437 pCfgAcq->Backend.cFramesPreBuffering = pCfgAcq->Backend.cFramesPeriod * 2;
438 }
439 else
440 rc = VERR_NO_MEMORY;
441 }
442 else
443 rc = VERR_NO_MEMORY;
444 }
445#else
446 RT_NOREF(pThis, pSink, pStreamAV, pCfgReq, pCfgAcq);
447 int rc = VERR_NOT_SUPPORTED;
448#endif /* VBOX_WITH_LIBOPUS */
449
450 LogFlowFuncLeaveRC(rc);
451 return rc;
452}
453
454
455/**
456 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
457 */
458static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
459 PCPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
460{
461 PDRVAUDIORECORDING pThis = RT_FROM_CPP_MEMBER(pInterface, DRVAUDIORECORDING, IHostAudio);
462 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
463 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
464 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
465 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
466
467 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
468 return VERR_NOT_SUPPORTED;
469
470 /* For now we only have one sink, namely the driver's one.
471 * Later each stream could have its own one, to e.g. router different stream to different sinks .*/
472 PAVRECSINK pSink = &pThis->Sink;
473
474 int rc = avRecCreateStreamOut(pThis, pStreamAV, pSink, pCfgReq, pCfgAcq);
475 PDMAudioStrmCfgCopy(&pStreamAV->Cfg, pCfgAcq);
476
477 return rc;
478}
479
480
481/**
482 * Destroys (closes) an audio output stream.
483 *
484 * @returns VBox status code.
485 * @param pThis Driver instance.
486 * @param pStreamAV Audio output stream to destroy.
487 */
488static int avRecDestroyStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV)
489{
490 RT_NOREF(pThis);
491
492 if (pStreamAV->pCircBuf)
493 {
494 RTCircBufDestroy(pStreamAV->pCircBuf);
495 pStreamAV->pCircBuf = NULL;
496 }
497
498 if (pStreamAV->pvSrcBuf)
499 {
500 Assert(pStreamAV->cbSrcBuf);
501 RTMemFree(pStreamAV->pvSrcBuf);
502 pStreamAV->pvSrcBuf = NULL;
503 pStreamAV->cbSrcBuf = 0;
504 }
505
506 if (pStreamAV->pvDstBuf)
507 {
508 Assert(pStreamAV->cbDstBuf);
509 RTMemFree(pStreamAV->pvDstBuf);
510 pStreamAV->pvDstBuf = NULL;
511 pStreamAV->cbDstBuf = 0;
512 }
513
514 return VINF_SUCCESS;
515}
516
517
518/**
519 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
520 */
521static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
522 bool fImmediate)
523{
524 PDRVAUDIORECORDING pThis = RT_FROM_CPP_MEMBER(pInterface, DRVAUDIORECORDING, IHostAudio);
525 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
526 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
527 RT_NOREF(fImmediate);
528
529 int rc = VINF_SUCCESS;
530 if (pStreamAV->Cfg.enmDir == PDMAUDIODIR_OUT)
531 rc = avRecDestroyStreamOut(pThis, pStreamAV);
532
533 return rc;
534}
535
536
537/**
538 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamEnable}
539 */
540static DECLCALLBACK(int) drvAudioVideoRecHA_StreamEnable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
541{
542 RT_NOREF(pInterface, pStream);
543 return VINF_SUCCESS;
544}
545
546
547/**
548 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDisable}
549 */
550static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDisable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
551{
552 RT_NOREF(pInterface, pStream);
553 return VINF_SUCCESS;
554}
555
556
557/**
558 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPause}
559 */
560static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPause(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
561{
562 RT_NOREF(pInterface, pStream);
563 return VINF_SUCCESS;
564}
565
566
567/**
568 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamResume}
569 */
570static DECLCALLBACK(int) drvAudioVideoRecHA_StreamResume(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
571{
572 RT_NOREF(pInterface, pStream);
573 return VINF_SUCCESS;
574}
575
576
577/**
578 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDrain}
579 */
580static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDrain(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
581{
582 RT_NOREF(pInterface, pStream);
583 return VINF_SUCCESS;
584}
585
586
587/**
588 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetState}
589 */
590static DECLCALLBACK(PDMHOSTAUDIOSTREAMSTATE) drvAudioVideoRecHA_StreamGetState(PPDMIHOSTAUDIO pInterface,
591 PPDMAUDIOBACKENDSTREAM pStream)
592{
593 RT_NOREF(pInterface);
594 AssertPtrReturn(pStream, PDMHOSTAUDIOSTREAMSTATE_INVALID);
595 return PDMHOSTAUDIOSTREAMSTATE_OKAY;
596}
597
598
599/**
600 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
601 */
602static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
603{
604 RT_NOREF(pInterface, pStream);
605 return UINT32_MAX;
606}
607
608
609/**
610 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
611 */
612static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
613 const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
614{
615 RT_NOREF(pInterface);
616 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
617 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
618 if (cbBuf)
619 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
620 AssertReturn(pcbWritten, VERR_INVALID_PARAMETER);
621
622 int rc = VINF_SUCCESS;
623
624 uint32_t cbWrittenTotal = 0;
625
626 /*
627 * Call the encoder with the data.
628 */
629#ifdef VBOX_WITH_LIBOPUS
630 PAVRECSINK pSink = pStreamAV->pSink;
631 AssertPtr(pSink);
632 PAVRECCODEC pCodec = &pSink->Codec;
633 AssertPtr(pCodec);
634 PRTCIRCBUF pCircBuf = pStreamAV->pCircBuf;
635 AssertPtr(pCircBuf);
636
637 uint32_t cbToWrite = cbBuf;
638
639 /*
640 * Write as much as we can into our internal ring buffer.
641 */
642 while ( cbToWrite > 0
643 && RTCircBufFree(pCircBuf))
644 {
645 void *pvCircBuf = NULL;
646 size_t cbCircBuf = 0;
647 RTCircBufAcquireWriteBlock(pCircBuf, cbToWrite, &pvCircBuf, &cbCircBuf);
648
649 if (cbCircBuf)
650 {
651 memcpy(pvCircBuf, (uint8_t *)pvBuf + cbWrittenTotal, cbCircBuf),
652 cbWrittenTotal += (uint32_t)cbCircBuf;
653 Assert(cbToWrite >= cbCircBuf);
654 cbToWrite -= (uint32_t)cbCircBuf;
655 }
656
657 RTCircBufReleaseWriteBlock(pCircBuf, cbCircBuf);
658 AssertBreak(cbCircBuf);
659 }
660
661 /*
662 * Process our internal ring buffer and encode the data.
663 */
664
665 /* Only encode data if we have data for the given time period (or more). */
666 while (RTCircBufUsed(pCircBuf) >= pCodec->Opus.cbFrame)
667 {
668 LogFunc(("cbAvail=%zu, csFrame=%RU32, cbFrame=%RU32\n",
669 RTCircBufUsed(pCircBuf), pCodec->Opus.csFrame, pCodec->Opus.cbFrame));
670
671 uint32_t cbSrc = 0;
672 while (cbSrc < pCodec->Opus.cbFrame)
673 {
674 void *pvCircBuf = NULL;
675 size_t cbCircBuf = 0;
676 RTCircBufAcquireReadBlock(pCircBuf, pCodec->Opus.cbFrame - cbSrc, &pvCircBuf, &cbCircBuf);
677
678 if (cbCircBuf)
679 {
680 memcpy((uint8_t *)pStreamAV->pvSrcBuf + cbSrc, pvCircBuf, cbCircBuf);
681
682 cbSrc += (uint32_t)cbCircBuf;
683 Assert(cbSrc <= pStreamAV->cbSrcBuf);
684 }
685
686 RTCircBufReleaseReadBlock(pCircBuf, cbCircBuf);
687 AssertBreak(cbCircBuf);
688 }
689
690 Assert(cbSrc == pCodec->Opus.cbFrame);
691
692# ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH
693 RTFILE fh;
694 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm",
695 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
696 RTFileWrite(fh, pStreamAV->pvSrcBuf, cbSrc, NULL);
697 RTFileClose(fh);
698# endif
699
700 /*
701 * Opus always encodes PER "OPUS FRAME", that is, exactly 2.5, 5, 10, 20, 40 or 60 ms of audio data.
702 *
703 * A packet can have up to 120ms worth of audio data.
704 * Anything > 120ms of data will result in a "corrupted package" error message by
705 * by decoding application.
706 */
707
708 /* Call the encoder to encode one "Opus frame" per iteration. */
709 opus_int32 cbWritten = opus_encode(pSink->Codec.Opus.pEnc,
710 (opus_int16 *)pStreamAV->pvSrcBuf, pCodec->Opus.csFrame,
711 (uint8_t *)pStreamAV->pvDstBuf, (opus_int32)pStreamAV->cbDstBuf);
712 if (cbWritten > 0)
713 {
714 /* Get overall frames encoded. */
715 const uint32_t cEncFrames = opus_packet_get_nb_frames((uint8_t *)pStreamAV->pvDstBuf, cbWritten);
716
717# ifdef VBOX_WITH_STATISTICS
718 pSink->Codec.Stats.cEncFrames += cEncFrames;
719 pSink->Codec.Stats.msEncTotal += pSink->Codec.Opus.msFrame * cEncFrames;
720# endif
721 Assert((uint32_t)cbWritten <= (uint32_t)pStreamAV->cbDstBuf);
722 const uint32_t cbDst = RT_MIN((uint32_t)cbWritten, (uint32_t)pStreamAV->cbDstBuf);
723
724 Assert(cEncFrames == 1);
725
726 if (pStreamAV->uLastPTSMs == 0)
727 pStreamAV->uLastPTSMs = RTTimeProgramMilliTS(); /* We want the absolute time (in ms) since program start. */
728
729 const uint64_t uDurationMs = pSink->Codec.Opus.msFrame * cEncFrames;
730 const uint64_t uPTSMs = pStreamAV->uLastPTSMs;
731
732 pStreamAV->uLastPTSMs += uDurationMs;
733
734 switch (pSink->Con.Parms.enmType)
735 {
736 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
737 {
738 HRESULT hr = pSink->Con.Main.pConsole->i_recordingSendAudio(pStreamAV->pvDstBuf, cbDst, uPTSMs);
739 Assert(hr == S_OK);
740 RT_NOREF(hr);
741 break;
742 }
743
744 case AVRECCONTAINERTYPE_WEBM:
745 {
746 WebMWriter::BlockData_Opus blockData = { pStreamAV->pvDstBuf, cbDst, uPTSMs };
747 rc = pSink->Con.WebM.pWebM->WriteBlock(pSink->Con.WebM.uTrack, &blockData, sizeof(blockData));
748 AssertRC(rc);
749 break;
750 }
751
752 default:
753 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
754 break;
755 }
756 }
757 else if (cbWritten < 0)
758 {
759 AssertMsgFailed(("Encoding failed: %s\n", opus_strerror(cbWritten)));
760 rc = VERR_INVALID_PARAMETER;
761 }
762
763 if (RT_FAILURE(rc))
764 break;
765 }
766
767 *pcbWritten = cbWrittenTotal;
768#else
769 /* Report back all data as being processed. */
770 *pcbWritten = cbBuf;
771
772 rc = VERR_NOT_SUPPORTED;
773#endif /* VBOX_WITH_LIBOPUS */
774
775 LogFlowFunc(("csReadTotal=%RU32, rc=%Rrc\n", cbWrittenTotal, rc));
776 return rc;
777}
778
779
780/**
781 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
782 */
783static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
784{
785 RT_NOREF(pInterface, pStream);
786 return 0; /* Video capturing does not provide any input. */
787}
788
789
790/**
791 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
792 */
793static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
794 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
795{
796 RT_NOREF(pInterface, pStream, pvBuf, cbBuf);
797 *pcbRead = 0;
798 return VINF_SUCCESS;
799}
800
801
802/*********************************************************************************************************************************
803* PDMIBASE *
804*********************************************************************************************************************************/
805
806/**
807 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
808 */
809static DECLCALLBACK(void *) drvAudioVideoRecQueryInterface(PPDMIBASE pInterface, const char *pszIID)
810{
811 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
812 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
813
814 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
815 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
816 return NULL;
817}
818
819
820/*********************************************************************************************************************************
821* PDMDRVREG *
822*********************************************************************************************************************************/
823
824/**
825 * Shuts down (closes) a recording sink,
826 *
827 * @returns VBox status code.
828 * @param pSink Recording sink to shut down.
829 */
830static void avRecSinkShutdown(PAVRECSINK pSink)
831{
832 AssertPtrReturnVoid(pSink);
833
834#ifdef VBOX_WITH_LIBOPUS
835 if (pSink->Codec.Opus.pEnc)
836 {
837 opus_encoder_destroy(pSink->Codec.Opus.pEnc);
838 pSink->Codec.Opus.pEnc = NULL;
839 }
840#endif
841 switch (pSink->Con.Parms.enmType)
842 {
843 case AVRECCONTAINERTYPE_WEBM:
844 {
845 if (pSink->Con.WebM.pWebM)
846 {
847 LogRel2(("Recording: Finished recording audio to file '%s' (%zu bytes)\n",
848 pSink->Con.WebM.pWebM->GetFileName().c_str(), pSink->Con.WebM.pWebM->GetFileSize()));
849
850 int rc2 = pSink->Con.WebM.pWebM->Close();
851 AssertRC(rc2);
852
853 delete pSink->Con.WebM.pWebM;
854 pSink->Con.WebM.pWebM = NULL;
855 }
856 break;
857 }
858
859 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
860 default:
861 break;
862 }
863}
864
865
866/**
867 * @interface_method_impl{PDMDRVREG,pfnPowerOff}
868 */
869/*static*/ DECLCALLBACK(void) AudioVideoRec::drvPowerOff(PPDMDRVINS pDrvIns)
870{
871 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
872 LogFlowFuncEnter();
873 avRecSinkShutdown(&pThis->Sink);
874}
875
876
877/**
878 * @interface_method_impl{PDMDRVREG,pfnDestruct}
879 */
880/*static*/ DECLCALLBACK(void) AudioVideoRec::drvDestruct(PPDMDRVINS pDrvIns)
881{
882 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
883 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
884
885 LogFlowFuncEnter();
886
887 switch (pThis->ContainerParms.enmType)
888 {
889 case AVRECCONTAINERTYPE_WEBM:
890 {
891 avRecSinkShutdown(&pThis->Sink);
892 RTStrFree(pThis->ContainerParms.WebM.pszFile);
893 break;
894 }
895
896 default:
897 break;
898 }
899
900 /*
901 * If the AudioVideoRec object is still alive, we must clear it's reference to
902 * us since we'll be invalid when we return from this method.
903 */
904 if (pThis->pAudioVideoRec)
905 {
906 pThis->pAudioVideoRec->mpDrv = NULL;
907 pThis->pAudioVideoRec = NULL;
908 }
909
910 LogFlowFuncLeave();
911}
912
913
914/**
915 * Initializes a recording sink.
916 *
917 * @returns VBox status code.
918 * @param pThis Driver instance.
919 * @param pSink Sink to initialize.
920 * @param pConParms Container parameters to set.
921 * @param pCodecParms Codec parameters to set.
922 */
923static int avRecSinkInit(PDRVAUDIORECORDING pThis, PAVRECSINK pSink, PAVRECCONTAINERPARMS pConParms, PAVRECCODECPARMS pCodecParms)
924{
925 uint32_t uHz = PDMAudioPropsHz(&pCodecParms->PCMProps);
926 uint8_t const cbSample = PDMAudioPropsSampleSize(&pCodecParms->PCMProps);
927 uint8_t cChannels = PDMAudioPropsChannels(&pCodecParms->PCMProps);
928 uint32_t uBitrate = pCodecParms->uBitrate;
929
930 /* Opus only supports certain input sample rates in an efficient manner.
931 * So make sure that we use those by resampling the data to the requested rate. */
932 if (uHz > 24000) uHz = AVREC_OPUS_HZ_MAX;
933 else if (uHz > 16000) uHz = 24000;
934 else if (uHz > 12000) uHz = 16000;
935 else if (uHz > 8000 ) uHz = 12000;
936 else uHz = 8000;
937
938 if (cChannels > 2)
939 {
940 LogRel(("Recording: Warning: More than 2 (stereo) channels are not supported at the moment\n"));
941 cChannels = 2;
942 }
943
944 int orc;
945 OpusEncoder *pEnc = opus_encoder_create(uHz, cChannels, OPUS_APPLICATION_AUDIO, &orc);
946 if (orc != OPUS_OK)
947 {
948 LogRel(("Recording: Audio codec failed to initialize: %s\n", opus_strerror(orc)));
949 return VERR_AUDIO_BACKEND_INIT_FAILED;
950 }
951
952 AssertPtr(pEnc);
953
954 if (uBitrate) /* Only explicitly set the bitrate if we specified one. Otherwise let Opus decide. */
955 {
956 opus_encoder_ctl(pEnc, OPUS_SET_BITRATE(uBitrate));
957 if (orc != OPUS_OK)
958 {
959 opus_encoder_destroy(pEnc);
960 pEnc = NULL;
961
962 LogRel(("Recording: Audio codec failed to set bitrate (%RU32): %s\n", uBitrate, opus_strerror(orc)));
963 return VERR_AUDIO_BACKEND_INIT_FAILED;
964 }
965 }
966
967 const bool fUseVBR = true; /** Use Variable Bit Rate (VBR) by default. @todo Make this configurable? */
968
969 orc = opus_encoder_ctl(pEnc, OPUS_SET_VBR(fUseVBR ? 1 : 0));
970 if (orc != OPUS_OK)
971 {
972 opus_encoder_destroy(pEnc);
973 pEnc = NULL;
974
975 LogRel(("Recording: Audio codec failed to %s VBR mode: %s\n", fUseVBR ? "enable" : "disable", opus_strerror(orc)));
976 return VERR_AUDIO_BACKEND_INIT_FAILED;
977 }
978
979 int rc = VINF_SUCCESS;
980
981 try
982 {
983 switch (pConParms->enmType)
984 {
985 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
986 {
987 if (pThis->pConsole)
988 {
989 pSink->Con.Main.pConsole = pThis->pConsole;
990 }
991 else
992 rc = VERR_NOT_SUPPORTED;
993 break;
994 }
995
996 case AVRECCONTAINERTYPE_WEBM:
997 {
998 /* If we only record audio, create our own WebM writer instance here. */
999 if (!pSink->Con.WebM.pWebM) /* Do we already have our WebM writer instance? */
1000 {
1001 /** @todo Add sink name / number to file name. */
1002 const char *pszFile = pSink->Con.Parms.WebM.pszFile;
1003 AssertPtr(pszFile);
1004
1005 pSink->Con.WebM.pWebM = new WebMWriter();
1006 rc = pSink->Con.WebM.pWebM->Open(pszFile,
1007 /** @todo Add option to add some suffix if file exists instead of overwriting? */
1008 RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE,
1009 WebMWriter::AudioCodec_Opus, WebMWriter::VideoCodec_None);
1010 if (RT_SUCCESS(rc))
1011 {
1012 rc = pSink->Con.WebM.pWebM->AddAudioTrack(uHz, cChannels, cbSample * 8 /* Bits */,
1013 &pSink->Con.WebM.uTrack);
1014 if (RT_SUCCESS(rc))
1015 {
1016 LogRel(("Recording: Recording audio to audio file '%s'\n", pszFile));
1017 }
1018 else
1019 LogRel(("Recording: Error creating audio track for audio file '%s' (%Rrc)\n", pszFile, rc));
1020 }
1021 else
1022 LogRel(("Recording: Error creating audio file '%s' (%Rrc)\n", pszFile, rc));
1023 }
1024 break;
1025 }
1026
1027 default:
1028 rc = VERR_NOT_SUPPORTED;
1029 break;
1030 }
1031 }
1032 catch (std::bad_alloc &)
1033 {
1034 rc = VERR_NO_MEMORY;
1035 }
1036
1037 if (RT_SUCCESS(rc))
1038 {
1039 pSink->Con.Parms.enmType = pConParms->enmType;
1040
1041 PAVRECCODEC pCodec = &pSink->Codec;
1042
1043 PDMAudioPropsInit(&pCodec->Parms.PCMProps, cbSample, pCodecParms->PCMProps.fSigned, cChannels, uHz);
1044 pCodec->Parms.uBitrate = uBitrate;
1045
1046 pCodec->Opus.pEnc = pEnc;
1047 pCodec->Opus.msFrame = AVREC_OPUS_FRAME_MS_DEFAULT;
1048
1049 if (!pCodec->Opus.msFrame)
1050 pCodec->Opus.msFrame = AVREC_OPUS_FRAME_MS_DEFAULT; /* 20ms by default; to prevent division by zero. */
1051 pCodec->Opus.csFrame = pSink->Codec.Parms.PCMProps.uHz / (1000 /* s in ms */ / pSink->Codec.Opus.msFrame);
1052 pCodec->Opus.cbFrame = PDMAudioPropsFramesToBytes(&pSink->Codec.Parms.PCMProps, pCodec->Opus.csFrame);
1053
1054#ifdef VBOX_WITH_STATISTICS
1055 pSink->Codec.Stats.cEncFrames = 0;
1056 pSink->Codec.Stats.msEncTotal = 0;
1057#endif
1058 pSink->tsStartMs = RTTimeMilliTS();
1059 }
1060 else
1061 {
1062 if (pEnc)
1063 {
1064 opus_encoder_destroy(pEnc);
1065 pEnc = NULL;
1066 }
1067
1068 LogRel(("Recording: Error creating sink (%Rrc)\n", rc));
1069 }
1070
1071 return rc;
1072}
1073
1074
1075/**
1076 * Construct a audio video recording driver instance.
1077 *
1078 * @copydoc FNPDMDRVCONSTRUCT
1079 */
1080/*static*/ DECLCALLBACK(int) AudioVideoRec::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1081{
1082 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1083 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1084 RT_NOREF(fFlags);
1085
1086 LogRel(("Audio: Initializing video recording audio driver\n"));
1087 LogFlowFunc(("fFlags=0x%x\n", fFlags));
1088
1089 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1090 ("Configuration error: Not possible to attach anything to this driver!\n"),
1091 VERR_PDM_DRVINS_NO_ATTACH);
1092
1093 /*
1094 * Init the static parts.
1095 */
1096 pThis->pDrvIns = pDrvIns;
1097 /* IBase */
1098 pDrvIns->IBase.pfnQueryInterface = drvAudioVideoRecQueryInterface;
1099 /* IHostAudio */
1100 pThis->IHostAudio.pfnGetConfig = drvAudioVideoRecHA_GetConfig;
1101 pThis->IHostAudio.pfnGetDevices = NULL;
1102 pThis->IHostAudio.pfnSetDevice = NULL;
1103 pThis->IHostAudio.pfnGetStatus = drvAudioVideoRecHA_GetStatus;
1104 pThis->IHostAudio.pfnDoOnWorkerThread = NULL;
1105 pThis->IHostAudio.pfnStreamConfigHint = NULL;
1106 pThis->IHostAudio.pfnStreamCreate = drvAudioVideoRecHA_StreamCreate;
1107 pThis->IHostAudio.pfnStreamInitAsync = NULL;
1108 pThis->IHostAudio.pfnStreamDestroy = drvAudioVideoRecHA_StreamDestroy;
1109 pThis->IHostAudio.pfnStreamNotifyDeviceChanged = NULL;
1110 pThis->IHostAudio.pfnStreamEnable = drvAudioVideoRecHA_StreamEnable;
1111 pThis->IHostAudio.pfnStreamDisable = drvAudioVideoRecHA_StreamDisable;
1112 pThis->IHostAudio.pfnStreamPause = drvAudioVideoRecHA_StreamPause;
1113 pThis->IHostAudio.pfnStreamResume = drvAudioVideoRecHA_StreamResume;
1114 pThis->IHostAudio.pfnStreamDrain = drvAudioVideoRecHA_StreamDrain;
1115 pThis->IHostAudio.pfnStreamGetState = drvAudioVideoRecHA_StreamGetState;
1116 pThis->IHostAudio.pfnStreamGetPending = NULL;
1117 pThis->IHostAudio.pfnStreamGetWritable = drvAudioVideoRecHA_StreamGetWritable;
1118 pThis->IHostAudio.pfnStreamPlay = drvAudioVideoRecHA_StreamPlay;
1119 pThis->IHostAudio.pfnStreamGetReadable = drvAudioVideoRecHA_StreamGetReadable;
1120 pThis->IHostAudio.pfnStreamCapture = drvAudioVideoRecHA_StreamCapture;
1121
1122 /*
1123 * Read configuration.
1124 */
1125 PCPDMDRVHLPR3 const pHlp = pDrvIns->pHlpR3;
1126 /** @todo validate it. */
1127
1128 /*
1129 * Get the Console object pointer.
1130 */
1131 com::Guid ConsoleUuid(COM_IIDOF(IConsole));
1132 IConsole *pIConsole = (IConsole *)PDMDrvHlpQueryGenericUserObject(pDrvIns, ConsoleUuid.raw());
1133 AssertLogRelReturn(pIConsole, VERR_INTERNAL_ERROR_3);
1134 Console *pConsole = static_cast<Console *>(pIConsole);
1135 AssertLogRelReturn(pConsole, VERR_INTERNAL_ERROR_3);
1136
1137 pThis->pConsole = pConsole;
1138 AssertReturn(!pThis->pConsole.isNull(), VERR_INVALID_POINTER);
1139 pThis->pAudioVideoRec = pConsole->i_recordingGetAudioDrv();
1140 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1141
1142 pThis->pAudioVideoRec->mpDrv = pThis;
1143
1144
1145 /*
1146 * Get the recording container and codec parameters from the audio driver instance.
1147 */
1148 PAVRECCONTAINERPARMS pConParams = &pThis->ContainerParms;
1149 PAVRECCODECPARMS pCodecParms = &pThis->CodecParms;
1150
1151 RT_ZERO(pThis->ContainerParms);
1152 RT_ZERO(pThis->CodecParms);
1153
1154 int rc = pHlp->pfnCFGMQueryU32(pCfg, "ContainerType", (uint32_t *)&pConParams->enmType);
1155 AssertRCReturn(rc, rc);
1156
1157 switch (pConParams->enmType)
1158 {
1159 case AVRECCONTAINERTYPE_WEBM:
1160 rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "ContainerFileName", &pConParams->WebM.pszFile);
1161 AssertRCReturn(rc, rc);
1162 break;
1163
1164 default:
1165 break;
1166 }
1167
1168 uint32_t uHz = 0;
1169 rc = pHlp->pfnCFGMQueryU32(pCfg, "CodecHz", &uHz);
1170 AssertRCReturn(rc, rc);
1171
1172 uint8_t cSampleBits = 0;
1173 rc = pHlp->pfnCFGMQueryU8(pCfg, "CodecBits", &cSampleBits); /** @todo CodecBits != CodecBytes */
1174 AssertRCReturn(rc, rc);
1175
1176 uint8_t cChannels = 0;
1177 rc = pHlp->pfnCFGMQueryU8(pCfg, "CodecChannels", &cChannels);
1178 AssertRCReturn(rc, rc);
1179
1180 PDMAudioPropsInit(&pCodecParms->PCMProps, cSampleBits / 8, true /*fSigned*/, cChannels, uHz);
1181 AssertMsgReturn(PDMAudioPropsAreValid(&pCodecParms->PCMProps),
1182 ("Configuration error: Audio configuration is invalid!\n"), VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES); /** @todo wrong status code. */
1183
1184 rc = pHlp->pfnCFGMQueryU32(pCfg, "CodecBitrate", &pCodecParms->uBitrate);
1185 AssertRCReturn(rc, rc);
1186
1187 /*
1188 * Get the interface for the above driver (DrvAudio) to make mixer/conversion calls.
1189 * Described in CFGM tree.
1190 */
1191/** @todo r=bird: What on earth do you think you need this for?!? It's not an
1192 * interface lower drivers are supposed to be messing with! */
1193 pThis->pDrvAudio = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIAUDIOCONNECTOR);
1194 AssertMsgReturn(pThis->pDrvAudio, ("Configuration error: No upper interface specified!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
1195
1196#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH
1197 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.webm");
1198 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm");
1199#endif
1200
1201 /*
1202 * Init the recording sink.
1203 */
1204 LogRel(("Recording: Audio driver is using %RU32Hz, %RU16bit, %RU8 channel%s\n",
1205 PDMAudioPropsHz(&pThis->CodecParms.PCMProps), PDMAudioPropsSampleBits(&pThis->CodecParms.PCMProps),
1206 PDMAudioPropsChannels(&pThis->CodecParms.PCMProps), PDMAudioPropsChannels(&pThis->CodecParms.PCMProps) == 1 ? "" : "s"));
1207
1208 rc = avRecSinkInit(pThis, &pThis->Sink, &pThis->ContainerParms, &pThis->CodecParms);
1209 if (RT_SUCCESS(rc))
1210 LogRel2(("Recording: Audio recording driver initialized\n"));
1211 else
1212 LogRel(("Recording: Audio recording driver initialization failed: %Rrc\n", rc));
1213
1214 return rc;
1215}
1216
1217
1218/**
1219 * Video recording audio driver registration record.
1220 */
1221const PDMDRVREG AudioVideoRec::DrvReg =
1222{
1223 PDM_DRVREG_VERSION,
1224 /* szName */
1225 "AudioVideoRec",
1226 /* szRCMod */
1227 "",
1228 /* szR0Mod */
1229 "",
1230 /* pszDescription */
1231 "Audio driver for video recording",
1232 /* fFlags */
1233 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1234 /* fClass. */
1235 PDM_DRVREG_CLASS_AUDIO,
1236 /* cMaxInstances */
1237 ~0U,
1238 /* cbInstance */
1239 sizeof(DRVAUDIORECORDING),
1240 /* pfnConstruct */
1241 AudioVideoRec::drvConstruct,
1242 /* pfnDestruct */
1243 AudioVideoRec::drvDestruct,
1244 /* pfnRelocate */
1245 NULL,
1246 /* pfnIOCtl */
1247 NULL,
1248 /* pfnPowerOn */
1249 NULL,
1250 /* pfnReset */
1251 NULL,
1252 /* pfnSuspend */
1253 NULL,
1254 /* pfnResume */
1255 NULL,
1256 /* pfnAttach */
1257 NULL,
1258 /* pfnDetach */
1259 NULL,
1260 /* pfnPowerOff */
1261 AudioVideoRec::drvPowerOff,
1262 /* pfnSoftReset */
1263 NULL,
1264 /* u32EndVersion */
1265 PDM_DRVREG_VERSION
1266};
1267
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use