VirtualBox

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

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

Audio: Removed the pfnInit and pfnShutdown methods from PDMIHOSTAUDIO. These methods duplicates PDMDRVREG callbacks and were therefore superfluous. bugref:9890

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