VirtualBox

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

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

Audio: Added PDMIHOSTAUDIO::pfnSetDevice with implementation for CoreAudio. Added CoreAudio config values OutputDeviceID and InputDeviceID for the same purpose. bugref:9890

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