VirtualBox

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

Last change on this file since 94267 was 93444, checked in by vboxsync, 3 years ago

VMM,Main,HostServices: Use a function table for accessing the VBoxVMM.dll/so/dylib functionality, and load it dynamically when the Console object is initialized. Also converted a few drivers in Main to use device helpers to get config values and such. bugref:10074

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