VirtualBox

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

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

Audio: Merged the cbStreamOut and cbStreamIn fields in PDMAUDIOBACKENDCFG (into cbStream). Added a fFlags member to PDMAUDIOBACKENDCFG, currently must-be-zero. bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.6 KB
Line 
1/* $Id: DrvAudioRec.cpp 88534 2021-04-15 12:16:56Z 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 /** The stream's acquired configuration. */
249 PDMAUDIOSTREAMCFG Cfg;
250 /** (Audio) frame buffer. */
251 PRTCIRCBUF pCircBuf;
252 /** Pointer to sink to use for writing. */
253 PAVRECSINK pSink;
254 /** Last encoded PTS (in ms). */
255 uint64_t uLastPTSMs;
256 /** Temporary buffer for the input (source) data to encode. */
257 void *pvSrcBuf;
258 /** Size (in bytes) of the temporary buffer holding the input (source) data to encode. */
259 size_t cbSrcBuf;
260 /** Temporary buffer for the encoded output (destination) data. */
261 void *pvDstBuf;
262 /** Size (in bytes) of the temporary buffer holding the encoded output (destination) data. */
263 size_t cbDstBuf;
264} AVRECSTREAM, *PAVRECSTREAM;
265
266/**
267 * Video recording audio driver instance data.
268 */
269typedef struct DRVAUDIORECORDING
270{
271 /** Pointer to audio video recording object. */
272 AudioVideoRec *pAudioVideoRec;
273 /** Pointer to the driver instance structure. */
274 PPDMDRVINS pDrvIns;
275 /** Pointer to host audio interface. */
276 PDMIHOSTAUDIO IHostAudio;
277 /** Pointer to the console object. */
278 ComPtr<Console> pConsole;
279 /** Pointer to the DrvAudio port interface that is above us. */
280 PPDMIAUDIOCONNECTOR pDrvAudio;
281 /** The driver's configured container parameters. */
282 AVRECCONTAINERPARMS ContainerParms;
283 /** The driver's configured codec parameters. */
284 AVRECCODECPARMS CodecParms;
285 /** The driver's sink for writing output to. */
286 AVRECSINK Sink;
287} DRVAUDIORECORDING, *PDRVAUDIORECORDING;
288
289
290AudioVideoRec::AudioVideoRec(Console *pConsole)
291 : AudioDriver(pConsole)
292 , mpDrv(NULL)
293{
294}
295
296
297AudioVideoRec::~AudioVideoRec(void)
298{
299 if (mpDrv)
300 {
301 mpDrv->pAudioVideoRec = NULL;
302 mpDrv = NULL;
303 }
304}
305
306
307/**
308 * Applies a video recording configuration to this driver instance.
309 *
310 * @returns VBox status code.
311 * @param Settings Capturing configuration to apply.
312 */
313int AudioVideoRec::applyConfiguration(const settings::RecordingSettings &Settings)
314{
315 /** @todo Do some validation here. */
316 mVideoRecCfg = Settings; /* Note: Does have an own copy operator. */
317 return VINF_SUCCESS;
318}
319
320
321/**
322 * @copydoc AudioDriver::configureDriver
323 */
324int AudioVideoRec::configureDriver(PCFGMNODE pLunCfg)
325{
326 int rc = CFGMR3InsertInteger(pLunCfg, "Object", (uintptr_t)mpConsole->i_recordingGetAudioDrv());
327 AssertRCReturn(rc, rc);
328 rc = CFGMR3InsertInteger(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 = CFGMR3InsertInteger(pLunCfg, "ContainerType", (uint64_t)Screen0Settings.enmDest);
336 AssertRCReturn(rc, rc);
337 if (Screen0Settings.enmDest == RecordingDestination_File)
338 {
339 rc = CFGMR3InsertString(pLunCfg, "ContainerFileName", Utf8Str(Screen0Settings.File.strName).c_str());
340 AssertRCReturn(rc, rc);
341 }
342 rc = CFGMR3InsertInteger(pLunCfg, "CodecHz", Screen0Settings.Audio.uHz);
343 AssertRCReturn(rc, rc);
344 rc = CFGMR3InsertInteger(pLunCfg, "CodecBits", Screen0Settings.Audio.cBits);
345 AssertRCReturn(rc, rc);
346 rc = CFGMR3InsertInteger(pLunCfg, "CodecChannels", Screen0Settings.Audio.cChannels);
347 AssertRCReturn(rc, rc);
348 rc = CFGMR3InsertInteger(pLunCfg, "CodecBitrate", 0); /* Let Opus decide for now. */
349 AssertRCReturn(rc, rc);
350
351 return AudioDriver::configureDriver(pLunCfg);
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, PPDMAUDIOSTREAMCFG 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->u.enmDst != PDMAUDIOPLAYBACKDST_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 PPDMAUDIOSTREAMCFG 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{
528 PDRVAUDIORECORDING pThis = RT_FROM_CPP_MEMBER(pInterface, DRVAUDIORECORDING, IHostAudio);
529 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
530 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
531
532 int rc = VINF_SUCCESS;
533 if (pStreamAV->Cfg.enmDir == PDMAUDIODIR_OUT)
534 rc = avRecDestroyStreamOut(pThis, pStreamAV);
535
536 return rc;
537}
538
539
540/**
541 * Controls an audio output stream
542 *
543 * @returns VBox status code.
544 * @param pThis Driver instance.
545 * @param pStreamAV Audio output stream to control.
546 * @param enmStreamCmd Stream command to issue.
547 */
548static int avRecControlStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV, PDMAUDIOSTREAMCMD enmStreamCmd)
549{
550 RT_NOREF(pThis, pStreamAV);
551
552 int rc;
553 switch (enmStreamCmd)
554 {
555 case PDMAUDIOSTREAMCMD_ENABLE:
556 case PDMAUDIOSTREAMCMD_DISABLE:
557 case PDMAUDIOSTREAMCMD_RESUME:
558 case PDMAUDIOSTREAMCMD_PAUSE:
559 rc = VINF_SUCCESS;
560 break;
561
562 default:
563 rc = VERR_NOT_SUPPORTED;
564 break;
565 }
566
567 return rc;
568}
569
570
571/**
572 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
573 */
574static DECLCALLBACK(int) drvAudioVideoRecHA_StreamControl(PPDMIHOSTAUDIO pInterface,
575 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
576{
577 PDRVAUDIORECORDING pThis = RT_FROM_CPP_MEMBER(pInterface, DRVAUDIORECORDING, IHostAudio);
578 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
579 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
580
581 if (pStreamAV->Cfg.enmDir == PDMAUDIODIR_OUT)
582 return avRecControlStreamOut(pThis, pStreamAV, enmStreamCmd);
583
584 return VINF_SUCCESS;
585}
586
587
588/**
589 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
590 */
591static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
592{
593 RT_NOREF(pInterface, pStream);
594 return 0; /* Video capturing does not provide any input. */
595}
596
597
598/**
599 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
600 */
601static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
602{
603 RT_NOREF(pInterface, pStream);
604 return UINT32_MAX;
605}
606
607
608/**
609 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
610 */
611static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvAudioVideoRecHA_StreamGetStatus(PPDMIHOSTAUDIO pInterface,
612 PPDMAUDIOBACKENDSTREAM pStream)
613{
614 RT_NOREF(pInterface, pStream);
615 return PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED | PDMAUDIOSTREAMSTS_FLAGS_ENABLED;
616}
617
618
619/**
620 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
621 */
622static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
623 const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
624{
625 RT_NOREF(pInterface);
626 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
627 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
628 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
629 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
630 AssertReturn(pcbWritten, VERR_INVALID_PARAMETER);
631
632 int rc = VINF_SUCCESS;
633
634 uint32_t cbWrittenTotal = 0;
635
636 /*
637 * Call the encoder with the data.
638 */
639#ifdef VBOX_WITH_LIBOPUS
640 PAVRECSINK pSink = pStreamAV->pSink;
641 AssertPtr(pSink);
642 PAVRECCODEC pCodec = &pSink->Codec;
643 AssertPtr(pCodec);
644 PRTCIRCBUF pCircBuf = pStreamAV->pCircBuf;
645 AssertPtr(pCircBuf);
646
647 uint32_t cbToWrite = cbBuf;
648
649 /*
650 * Write as much as we can into our internal ring buffer.
651 */
652 while ( cbToWrite
653 && RTCircBufFree(pCircBuf))
654 {
655 void *pvCircBuf = NULL;
656 size_t cbCircBuf = 0;
657 RTCircBufAcquireWriteBlock(pCircBuf, cbToWrite, &pvCircBuf, &cbCircBuf);
658
659 if (cbCircBuf)
660 {
661 memcpy(pvCircBuf, (uint8_t *)pvBuf + cbWrittenTotal, cbCircBuf),
662 cbWrittenTotal += (uint32_t)cbCircBuf;
663 Assert(cbToWrite >= cbCircBuf);
664 cbToWrite -= (uint32_t)cbCircBuf;
665 }
666
667 RTCircBufReleaseWriteBlock(pCircBuf, cbCircBuf);
668 AssertBreak(cbCircBuf);
669 }
670
671 /*
672 * Process our internal ring buffer and encode the data.
673 */
674
675 /* Only encode data if we have data for the given time period (or more). */
676 while (RTCircBufUsed(pCircBuf) >= pCodec->Opus.cbFrame)
677 {
678 LogFunc(("cbAvail=%zu, csFrame=%RU32, cbFrame=%RU32\n",
679 RTCircBufUsed(pCircBuf), pCodec->Opus.csFrame, pCodec->Opus.cbFrame));
680
681 uint32_t cbSrc = 0;
682 while (cbSrc < pCodec->Opus.cbFrame)
683 {
684 void *pvCircBuf = NULL;
685 size_t cbCircBuf = 0;
686 RTCircBufAcquireReadBlock(pCircBuf, pCodec->Opus.cbFrame - cbSrc, &pvCircBuf, &cbCircBuf);
687
688 if (cbCircBuf)
689 {
690 memcpy((uint8_t *)pStreamAV->pvSrcBuf + cbSrc, pvCircBuf, cbCircBuf);
691
692 cbSrc += (uint32_t)cbCircBuf;
693 Assert(cbSrc <= pStreamAV->cbSrcBuf);
694 }
695
696 RTCircBufReleaseReadBlock(pCircBuf, cbCircBuf);
697 AssertBreak(cbCircBuf);
698 }
699
700 Assert(cbSrc == pCodec->Opus.cbFrame);
701
702# ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
703 RTFILE fh;
704 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm",
705 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
706 RTFileWrite(fh, pStreamAV->pvSrcBuf, cbSrc, NULL);
707 RTFileClose(fh);
708# endif
709
710 /*
711 * Opus always encodes PER "OPUS FRAME", that is, exactly 2.5, 5, 10, 20, 40 or 60 ms of audio data.
712 *
713 * A packet can have up to 120ms worth of audio data.
714 * Anything > 120ms of data will result in a "corrupted package" error message by
715 * by decoding application.
716 */
717
718 /* Call the encoder to encode one "Opus frame" per iteration. */
719 opus_int32 cbWritten = opus_encode(pSink->Codec.Opus.pEnc,
720 (opus_int16 *)pStreamAV->pvSrcBuf, pCodec->Opus.csFrame,
721 (uint8_t *)pStreamAV->pvDstBuf, (opus_int32)pStreamAV->cbDstBuf);
722 if (cbWritten > 0)
723 {
724 /* Get overall frames encoded. */
725 const uint32_t cEncFrames = opus_packet_get_nb_frames((uint8_t *)pStreamAV->pvDstBuf, cbWritten);
726
727# ifdef VBOX_WITH_STATISTICS
728 pSink->Codec.Stats.cEncFrames += cEncFrames;
729 pSink->Codec.Stats.msEncTotal += pSink->Codec.Opus.msFrame * cEncFrames;
730# endif
731 Assert((uint32_t)cbWritten <= (uint32_t)pStreamAV->cbDstBuf);
732 const uint32_t cbDst = RT_MIN((uint32_t)cbWritten, (uint32_t)pStreamAV->cbDstBuf);
733
734 Assert(cEncFrames == 1);
735
736 if (pStreamAV->uLastPTSMs == 0)
737 pStreamAV->uLastPTSMs = RTTimeProgramMilliTS(); /* We want the absolute time (in ms) since program start. */
738
739 const uint64_t uDurationMs = pSink->Codec.Opus.msFrame * cEncFrames;
740 const uint64_t uPTSMs = pStreamAV->uLastPTSMs;
741
742 pStreamAV->uLastPTSMs += uDurationMs;
743
744 switch (pSink->Con.Parms.enmType)
745 {
746 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
747 {
748 HRESULT hr = pSink->Con.Main.pConsole->i_recordingSendAudio(pStreamAV->pvDstBuf, cbDst, uPTSMs);
749 Assert(hr == S_OK);
750 RT_NOREF(hr);
751 break;
752 }
753
754 case AVRECCONTAINERTYPE_WEBM:
755 {
756 WebMWriter::BlockData_Opus blockData = { pStreamAV->pvDstBuf, cbDst, uPTSMs };
757 rc = pSink->Con.WebM.pWebM->WriteBlock(pSink->Con.WebM.uTrack, &blockData, sizeof(blockData));
758 AssertRC(rc);
759 break;
760 }
761
762 default:
763 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
764 break;
765 }
766 }
767 else if (cbWritten < 0)
768 {
769 AssertMsgFailed(("Encoding failed: %s\n", opus_strerror(cbWritten)));
770 rc = VERR_INVALID_PARAMETER;
771 }
772
773 if (RT_FAILURE(rc))
774 break;
775 }
776
777 *pcbWritten = cbWrittenTotal;
778#else
779 /* Report back all data as being processed. */
780 *pcbWritten = cbBuf;
781
782 rc = VERR_NOT_SUPPORTED;
783#endif /* VBOX_WITH_LIBOPUS */
784
785 LogFlowFunc(("csReadTotal=%RU32, rc=%Rrc\n", cbWrittenTotal, rc));
786 return rc;
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.pfnGetStatus = drvAudioVideoRecHA_GetStatus;
1103 pThis->IHostAudio.pfnStreamCreate = drvAudioVideoRecHA_StreamCreate;
1104 pThis->IHostAudio.pfnStreamDestroy = drvAudioVideoRecHA_StreamDestroy;
1105 pThis->IHostAudio.pfnStreamControl = drvAudioVideoRecHA_StreamControl;
1106 pThis->IHostAudio.pfnStreamGetReadable = drvAudioVideoRecHA_StreamGetReadable;
1107 pThis->IHostAudio.pfnStreamGetWritable = drvAudioVideoRecHA_StreamGetWritable;
1108 pThis->IHostAudio.pfnStreamGetPending = NULL;
1109 pThis->IHostAudio.pfnStreamGetStatus = drvAudioVideoRecHA_StreamGetStatus;
1110 pThis->IHostAudio.pfnStreamPlay = drvAudioVideoRecHA_StreamPlay;
1111 pThis->IHostAudio.pfnStreamCapture = drvAudioVideoRecHA_StreamCapture;
1112
1113 /*
1114 * Get the Console object pointer.
1115 */
1116 void *pvUser;
1117 int rc = CFGMR3QueryPtr(pCfg, "ObjectConsole", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1118 AssertRCReturn(rc, rc);
1119
1120 /* CFGM tree saves the pointer to Console in the Object node of AudioVideoRec. */
1121 pThis->pConsole = (Console *)pvUser;
1122 AssertReturn(!pThis->pConsole.isNull(), VERR_INVALID_POINTER);
1123
1124 /*
1125 * Get the pointer to the audio driver instance.
1126 */
1127 rc = CFGMR3QueryPtr(pCfg, "Object", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1128 AssertRCReturn(rc, rc);
1129
1130 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1131 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1132
1133 /*
1134 * Get the recording container and codec parameters from the audio driver instance.
1135 */
1136 PAVRECCONTAINERPARMS pConParams = &pThis->ContainerParms;
1137 PAVRECCODECPARMS pCodecParms = &pThis->CodecParms;
1138
1139 RT_ZERO(pThis->ContainerParms);
1140 RT_ZERO(pThis->CodecParms);
1141
1142 rc = CFGMR3QueryU32(pCfg, "ContainerType", (uint32_t *)&pConParams->enmType);
1143 AssertRCReturn(rc, rc);
1144
1145 switch (pConParams->enmType)
1146 {
1147 case AVRECCONTAINERTYPE_WEBM:
1148 rc = CFGMR3QueryStringAlloc(pCfg, "ContainerFileName", &pConParams->WebM.pszFile);
1149 AssertRCReturn(rc, rc);
1150 break;
1151
1152 default:
1153 break;
1154 }
1155
1156 uint32_t uHz = 0;
1157 rc = CFGMR3QueryU32(pCfg, "CodecHz", &uHz);
1158 AssertRCReturn(rc, rc);
1159
1160 uint8_t cSampleBits = 0;
1161 rc = CFGMR3QueryU8(pCfg, "CodecBits", &cSampleBits); /** @todo CodecBits != CodecBytes */
1162 AssertRCReturn(rc, rc);
1163
1164 uint8_t cChannels = 0;
1165 rc = CFGMR3QueryU8(pCfg, "CodecChannels", &cChannels);
1166 AssertRCReturn(rc, rc);
1167
1168 PDMAudioPropsInit(&pCodecParms->PCMProps, cSampleBits / 8, true /*fSigned*/, cChannels, uHz);
1169 AssertMsgReturn(PDMAudioPropsAreValid(&pCodecParms->PCMProps),
1170 ("Configuration error: Audio configuration is invalid!\n"), VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES); /** @todo wrong status code. */
1171
1172 rc = CFGMR3QueryU32(pCfg, "CodecBitrate", &pCodecParms->uBitrate);
1173 AssertRCReturn(rc, rc);
1174
1175 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1176 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1177
1178 pThis->pAudioVideoRec->mpDrv = pThis;
1179
1180 /*
1181 * Get the interface for the above driver (DrvAudio) to make mixer/conversion calls.
1182 * Described in CFGM tree.
1183 */
1184/** @todo r=bird: What on earth do you think you need this for?!? It's not an
1185 * interface lower drivers are supposed to be messing with! */
1186 pThis->pDrvAudio = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIAUDIOCONNECTOR);
1187 AssertMsgReturn(pThis->pDrvAudio, ("Configuration error: No upper interface specified!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
1188
1189#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
1190 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.webm");
1191 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm");
1192#endif
1193
1194 /*
1195 * Init the recording sink.
1196 */
1197 LogRel(("Recording: Audio driver is using %RU32Hz, %RU16bit, %RU8 channel%s\n",
1198 PDMAudioPropsHz(&pThis->CodecParms.PCMProps), PDMAudioPropsSampleBits(&pThis->CodecParms.PCMProps),
1199 PDMAudioPropsChannels(&pThis->CodecParms.PCMProps), PDMAudioPropsChannels(&pThis->CodecParms.PCMProps) == 1 ? "" : "s"));
1200
1201 rc = avRecSinkInit(pThis, &pThis->Sink, &pThis->ContainerParms, &pThis->CodecParms);
1202 if (RT_SUCCESS(rc))
1203 LogRel2(("Recording: Audio recording driver initialized\n"));
1204 else
1205 LogRel(("Recording: Audio recording driver initialization failed: %Rrc\n", rc));
1206
1207 return rc;
1208}
1209
1210
1211/**
1212 * Video recording audio driver registration record.
1213 */
1214const PDMDRVREG AudioVideoRec::DrvReg =
1215{
1216 PDM_DRVREG_VERSION,
1217 /* szName */
1218 "AudioVideoRec",
1219 /* szRCMod */
1220 "",
1221 /* szR0Mod */
1222 "",
1223 /* pszDescription */
1224 "Audio driver for video recording",
1225 /* fFlags */
1226 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1227 /* fClass. */
1228 PDM_DRVREG_CLASS_AUDIO,
1229 /* cMaxInstances */
1230 ~0U,
1231 /* cbInstance */
1232 sizeof(DRVAUDIORECORDING),
1233 /* pfnConstruct */
1234 AudioVideoRec::drvConstruct,
1235 /* pfnDestruct */
1236 AudioVideoRec::drvDestruct,
1237 /* pfnRelocate */
1238 NULL,
1239 /* pfnIOCtl */
1240 NULL,
1241 /* pfnPowerOn */
1242 NULL,
1243 /* pfnReset */
1244 NULL,
1245 /* pfnSuspend */
1246 NULL,
1247 /* pfnResume */
1248 NULL,
1249 /* pfnAttach */
1250 NULL,
1251 /* pfnDetach */
1252 NULL,
1253 /* pfnPowerOff */
1254 AudioVideoRec::drvPowerOff,
1255 /* pfnSoftReset */
1256 NULL,
1257 /* u32EndVersion */
1258 PDM_DRVREG_VERSION
1259};
1260
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