VirtualBox

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

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

Audio: We don't return IPRT status codes, but VBox status codes. duh. bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 43.8 KB
Line 
1/* $Id: DrvAudioRec.cpp 88300 2021-03-26 14:31:55Z 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,pfnInit}
638 */
639static DECLCALLBACK(int) drvAudioVideoRecHA_Init(PPDMIHOSTAUDIO pInterface)
640{
641 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
642
643 LogFlowFuncEnter();
644
645 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
646
647 LogRel(("Recording: Audio driver is using %RU32Hz, %RU16bit, %RU8 channel%s\n",
648 PDMAudioPropsHz(&pThis->CodecParms.PCMProps), PDMAudioPropsSampleBits(&pThis->CodecParms.PCMProps),
649 PDMAudioPropsChannels(&pThis->CodecParms.PCMProps), PDMAudioPropsChannels(&pThis->CodecParms.PCMProps) == 1 ? "" : "s"));
650
651 int rc = avRecSinkInit(pThis, &pThis->Sink, &pThis->ContainerParms, &pThis->CodecParms);
652 if (RT_FAILURE(rc))
653 LogRel(("Recording: Audio recording driver failed to initialize, rc=%Rrc\n", rc));
654 else
655 LogRel2(("Recording: Audio recording driver initialized\n"));
656
657 return rc;
658}
659
660
661/**
662 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
663 */
664static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
665 void *pvBuf, uint32_t uBufSize, uint32_t *puRead)
666{
667 RT_NOREF(pInterface, pStream, pvBuf, uBufSize);
668
669 if (puRead)
670 *puRead = 0;
671
672 return VINF_SUCCESS;
673}
674
675
676/**
677 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
678 */
679static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
680 const void *pvBuf, uint32_t uBufSize, uint32_t *puWritten)
681{
682 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
683 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
684 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
685 AssertReturn(uBufSize, VERR_INVALID_PARAMETER);
686 /* puWritten is optional. */
687
688 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
689 RT_NOREF(pThis);
690 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
691
692 int rc = VINF_SUCCESS;
693
694 uint32_t cbWrittenTotal = 0;
695
696 /*
697 * Call the encoder with the data.
698 */
699#ifdef VBOX_WITH_LIBOPUS
700 PAVRECSINK pSink = pStreamAV->pSink;
701 AssertPtr(pSink);
702 PAVRECCODEC pCodec = &pSink->Codec;
703 AssertPtr(pCodec);
704 PRTCIRCBUF pCircBuf = pStreamAV->pCircBuf;
705 AssertPtr(pCircBuf);
706
707 void *pvCircBuf;
708 size_t cbCircBuf;
709
710 uint32_t cbToWrite = uBufSize;
711
712 /*
713 * Fetch as much as we can into our internal ring buffer.
714 */
715 while ( cbToWrite
716 && RTCircBufFree(pCircBuf))
717 {
718 RTCircBufAcquireWriteBlock(pCircBuf, cbToWrite, &pvCircBuf, &cbCircBuf);
719
720 if (cbCircBuf)
721 {
722 memcpy(pvCircBuf, (uint8_t *)pvBuf + cbWrittenTotal, cbCircBuf),
723 cbWrittenTotal += (uint32_t)cbCircBuf;
724 Assert(cbToWrite >= cbCircBuf);
725 cbToWrite -= (uint32_t)cbCircBuf;
726 }
727
728 RTCircBufReleaseWriteBlock(pCircBuf, cbCircBuf);
729
730 if ( RT_FAILURE(rc)
731 || !cbCircBuf)
732 {
733 break;
734 }
735 }
736
737 /*
738 * Process our internal ring buffer and encode the data.
739 */
740
741 uint32_t cbSrc;
742
743 /* Only encode data if we have data for the given time period (or more). */
744 while (RTCircBufUsed(pCircBuf) >= pCodec->Opus.cbFrame)
745 {
746 LogFunc(("cbAvail=%zu, csFrame=%RU32, cbFrame=%RU32\n",
747 RTCircBufUsed(pCircBuf), pCodec->Opus.csFrame, pCodec->Opus.cbFrame));
748
749 cbSrc = 0;
750
751 while (cbSrc < pCodec->Opus.cbFrame)
752 {
753 RTCircBufAcquireReadBlock(pCircBuf, pCodec->Opus.cbFrame - cbSrc, &pvCircBuf, &cbCircBuf);
754
755 if (cbCircBuf)
756 {
757 memcpy((uint8_t *)pStreamAV->pvSrcBuf + cbSrc, pvCircBuf, cbCircBuf);
758
759 cbSrc += (uint32_t)cbCircBuf;
760 Assert(cbSrc <= pStreamAV->cbSrcBuf);
761 }
762
763 RTCircBufReleaseReadBlock(pCircBuf, cbCircBuf);
764
765 if (!cbCircBuf)
766 break;
767 }
768
769# ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
770 RTFILE fh;
771 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm",
772 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
773 RTFileWrite(fh, pStreamAV->pvSrcBuf, cbSrc, NULL);
774 RTFileClose(fh);
775# endif
776
777 Assert(cbSrc == pCodec->Opus.cbFrame);
778
779 /*
780 * Opus always encodes PER "OPUS FRAME", that is, exactly 2.5, 5, 10, 20, 40 or 60 ms of audio data.
781 *
782 * A packet can have up to 120ms worth of audio data.
783 * Anything > 120ms of data will result in a "corrupted package" error message by
784 * by decoding application.
785 */
786
787 /* Call the encoder to encode one "Opus frame" per iteration. */
788 opus_int32 cbWritten = opus_encode(pSink->Codec.Opus.pEnc,
789 (opus_int16 *)pStreamAV->pvSrcBuf, pCodec->Opus.csFrame,
790 (uint8_t *)pStreamAV->pvDstBuf, (opus_int32)pStreamAV->cbDstBuf);
791 if (cbWritten > 0)
792 {
793 /* Get overall frames encoded. */
794 const uint32_t cEncFrames = opus_packet_get_nb_frames((uint8_t *)pStreamAV->pvDstBuf, cbWritten);
795
796# ifdef VBOX_WITH_STATISTICS
797 pSink->Codec.STAM.cEncFrames += cEncFrames;
798 pSink->Codec.STAM.msEncTotal += pSink->Codec.Opus.msFrame * cEncFrames;
799# endif
800 Assert((uint32_t)cbWritten <= (uint32_t)pStreamAV->cbDstBuf);
801 const uint32_t cbDst = RT_MIN((uint32_t)cbWritten, (uint32_t)pStreamAV->cbDstBuf);
802
803 Assert(cEncFrames == 1);
804
805 if (pStreamAV->uLastPTSMs == 0)
806 pStreamAV->uLastPTSMs = RTTimeProgramMilliTS(); /* We want the absolute time (in ms) since program start. */
807
808 const uint64_t uDurationMs = pSink->Codec.Opus.msFrame * cEncFrames;
809 const uint64_t uPTSMs = pStreamAV->uLastPTSMs;
810
811 pStreamAV->uLastPTSMs += uDurationMs;
812
813 switch (pSink->Con.Parms.enmType)
814 {
815 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
816 {
817 HRESULT hr = pSink->Con.Main.pConsole->i_recordingSendAudio(pStreamAV->pvDstBuf, cbDst, uPTSMs);
818 Assert(hr == S_OK);
819 RT_NOREF(hr);
820
821 break;
822 }
823
824 case AVRECCONTAINERTYPE_WEBM:
825 {
826 WebMWriter::BlockData_Opus blockData = { pStreamAV->pvDstBuf, cbDst, uPTSMs };
827 rc = pSink->Con.WebM.pWebM->WriteBlock(pSink->Con.WebM.uTrack, &blockData, sizeof(blockData));
828 AssertRC(rc);
829
830 break;
831 }
832
833 default:
834 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
835 break;
836 }
837 }
838 else if (cbWritten < 0)
839 {
840 AssertMsgFailed(("Encoding failed: %s\n", opus_strerror(cbWritten)));
841 rc = VERR_INVALID_PARAMETER;
842 }
843
844 if (RT_FAILURE(rc))
845 break;
846 }
847
848 if (puWritten)
849 *puWritten = cbWrittenTotal;
850#else
851 /* Report back all data as being processed. */
852 if (puWritten)
853 *puWritten = uBufSize;
854
855 rc = VERR_NOT_SUPPORTED;
856#endif /* VBOX_WITH_LIBOPUS */
857
858 LogFlowFunc(("csReadTotal=%RU32, rc=%Rrc\n", cbWrittenTotal, rc));
859 return rc;
860}
861
862
863/**
864 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
865 */
866static DECLCALLBACK(int) drvAudioVideoRecHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
867{
868 RT_NOREF(pInterface);
869 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
870
871 RTStrPrintf2(pBackendCfg->szName, sizeof(pBackendCfg->szName), "VideoRec");
872
873 pBackendCfg->cbStreamOut = sizeof(AVRECSTREAM);
874 pBackendCfg->cbStreamIn = 0;
875 pBackendCfg->cMaxStreamsIn = 0;
876 pBackendCfg->cMaxStreamsOut = UINT32_MAX;
877
878 return VINF_SUCCESS;
879}
880
881
882/**
883 * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
884 */
885static DECLCALLBACK(void) drvAudioVideoRecHA_Shutdown(PPDMIHOSTAUDIO pInterface)
886{
887 LogFlowFuncEnter();
888
889 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
890
891 avRecSinkShutdown(&pThis->Sink);
892}
893
894
895/**
896 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
897 */
898static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvAudioVideoRecHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
899{
900 RT_NOREF(enmDir);
901 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
902
903 return PDMAUDIOBACKENDSTS_RUNNING;
904}
905
906
907/**
908 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
909 */
910static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
911 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
912{
913 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
914 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
915 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
916
917 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
918 return VERR_NOT_SUPPORTED;
919
920 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
921
922 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
923 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
924
925 /* For now we only have one sink, namely the driver's one.
926 * Later each stream could have its own one, to e.g. router different stream to different sinks .*/
927 PAVRECSINK pSink = &pThis->Sink;
928
929 int rc = avRecCreateStreamOut(pThis, pStreamAV, pSink, pCfgReq, pCfgAcq);
930 if (RT_SUCCESS(rc))
931 {
932 pStreamAV->pCfg = PDMAudioStrmCfgDup(pCfgAcq);
933 if (!pStreamAV->pCfg)
934 rc = VERR_NO_MEMORY;
935 }
936
937 return rc;
938}
939
940
941/**
942 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
943 */
944static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
945{
946 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
947 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
948
949 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
950 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
951
952 if (!pStreamAV->pCfg) /* Not (yet) configured? Skip. */
953 return VINF_SUCCESS;
954
955 int rc = VINF_SUCCESS;
956
957 if (pStreamAV->pCfg->enmDir == PDMAUDIODIR_OUT)
958 rc = avRecDestroyStreamOut(pThis, pStreamAV);
959
960 if (RT_SUCCESS(rc))
961 {
962 PDMAudioStrmCfgFree(pStreamAV->pCfg);
963 pStreamAV->pCfg = NULL;
964 }
965
966 return rc;
967}
968
969
970/**
971 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
972 */
973static DECLCALLBACK(int) drvAudioVideoRecHA_StreamControl(PPDMIHOSTAUDIO pInterface,
974 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
975{
976 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
977 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
978
979 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
980 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
981
982 if (!pStreamAV->pCfg) /* Not (yet) configured? Skip. */
983 return VINF_SUCCESS;
984
985 if (pStreamAV->pCfg->enmDir == PDMAUDIODIR_OUT)
986 return avRecControlStreamOut(pThis, pStreamAV, enmStreamCmd);
987
988 return VINF_SUCCESS;
989}
990
991
992/**
993 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
994 */
995static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
996{
997 RT_NOREF(pInterface, pStream);
998
999 return 0; /* Video capturing does not provide any input. */
1000}
1001
1002
1003/**
1004 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
1005 */
1006static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1007{
1008 RT_NOREF(pInterface, pStream);
1009
1010 return UINT32_MAX;
1011}
1012
1013
1014/**
1015 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
1016 */
1017static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvAudioVideoRecHA_StreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1018{
1019 RT_NOREF(pInterface, pStream);
1020
1021 return PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED | PDMAUDIOSTREAMSTS_FLAGS_ENABLED;
1022}
1023
1024
1025/**
1026 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
1027 */
1028static DECLCALLBACK(int) drvAudioVideoRecHA_StreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1029{
1030 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1031 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1032
1033 LogFlowFuncEnter();
1034
1035 /* Nothing to do here for video recording. */
1036 return VINF_SUCCESS;
1037}
1038
1039
1040/**
1041 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1042 */
1043static DECLCALLBACK(void *) drvAudioVideoRecQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1044{
1045 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1046 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1047
1048 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1049 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
1050 return NULL;
1051}
1052
1053
1054AudioVideoRec::AudioVideoRec(Console *pConsole)
1055 : AudioDriver(pConsole)
1056 , mpDrv(NULL)
1057{
1058}
1059
1060
1061AudioVideoRec::~AudioVideoRec(void)
1062{
1063 if (mpDrv)
1064 {
1065 mpDrv->pAudioVideoRec = NULL;
1066 mpDrv = NULL;
1067 }
1068}
1069
1070
1071/**
1072 * Applies a video recording configuration to this driver instance.
1073 *
1074 * @returns VBox status code.
1075 * @param Settings Capturing configuration to apply.
1076 */
1077int AudioVideoRec::applyConfiguration(const settings::RecordingSettings &Settings)
1078{
1079 /** @todo Do some validation here. */
1080 mVideoRecCfg = Settings; /* Note: Does have an own copy operator. */
1081 return VINF_SUCCESS;
1082}
1083
1084
1085/**
1086 * @copydoc AudioDriver::configureDriver
1087 */
1088int AudioVideoRec::configureDriver(PCFGMNODE pLunCfg)
1089{
1090 int rc = CFGMR3InsertInteger(pLunCfg, "Object", (uintptr_t)mpConsole->i_recordingGetAudioDrv());
1091 AssertRCReturn(rc, rc);
1092 rc = CFGMR3InsertInteger(pLunCfg, "ObjectConsole", (uintptr_t)mpConsole);
1093 AssertRCReturn(rc, rc);
1094
1095 /** @todo For now we're using the configuration of the first screen here audio-wise. */
1096 Assert(mVideoRecCfg.mapScreens.size() >= 1);
1097 const settings::RecordingScreenSettings &Screen0Settings = mVideoRecCfg.mapScreens[0];
1098
1099 rc = CFGMR3InsertInteger(pLunCfg, "ContainerType", (uint64_t)Screen0Settings.enmDest);
1100 AssertRCReturn(rc, rc);
1101 if (Screen0Settings.enmDest == RecordingDestination_File)
1102 {
1103 rc = CFGMR3InsertString(pLunCfg, "ContainerFileName", Utf8Str(Screen0Settings.File.strName).c_str());
1104 AssertRCReturn(rc, rc);
1105 }
1106 rc = CFGMR3InsertInteger(pLunCfg, "CodecHz", Screen0Settings.Audio.uHz);
1107 AssertRCReturn(rc, rc);
1108 rc = CFGMR3InsertInteger(pLunCfg, "CodecBits", Screen0Settings.Audio.cBits);
1109 AssertRCReturn(rc, rc);
1110 rc = CFGMR3InsertInteger(pLunCfg, "CodecChannels", Screen0Settings.Audio.cChannels);
1111 AssertRCReturn(rc, rc);
1112 rc = CFGMR3InsertInteger(pLunCfg, "CodecBitrate", 0); /* Let Opus decide for now. */
1113 AssertRCReturn(rc, rc);
1114
1115 return AudioDriver::configureDriver(pLunCfg);
1116}
1117
1118
1119/**
1120 * Construct a audio video recording driver instance.
1121 *
1122 * @copydoc FNPDMDRVCONSTRUCT
1123 */
1124/* static */
1125DECLCALLBACK(int) AudioVideoRec::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1126{
1127 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1128 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1129 RT_NOREF(fFlags);
1130
1131 LogRel(("Audio: Initializing video recording audio driver\n"));
1132 LogFlowFunc(("fFlags=0x%x\n", fFlags));
1133
1134 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1135 ("Configuration error: Not possible to attach anything to this driver!\n"),
1136 VERR_PDM_DRVINS_NO_ATTACH);
1137
1138 /*
1139 * Init the static parts.
1140 */
1141 pThis->pDrvIns = pDrvIns;
1142 /* IBase */
1143 pDrvIns->IBase.pfnQueryInterface = drvAudioVideoRecQueryInterface;
1144 /* IHostAudio */
1145 pThis->IHostAudio.pfnInit = drvAudioVideoRecHA_Init;
1146 pThis->IHostAudio.pfnShutdown = drvAudioVideoRecHA_Shutdown;
1147 pThis->IHostAudio.pfnGetConfig = drvAudioVideoRecHA_GetConfig;
1148 pThis->IHostAudio.pfnGetStatus = drvAudioVideoRecHA_GetStatus;
1149 pThis->IHostAudio.pfnStreamCreate = drvAudioVideoRecHA_StreamCreate;
1150 pThis->IHostAudio.pfnStreamDestroy = drvAudioVideoRecHA_StreamDestroy;
1151 pThis->IHostAudio.pfnStreamControl = drvAudioVideoRecHA_StreamControl;
1152 pThis->IHostAudio.pfnStreamGetReadable = drvAudioVideoRecHA_StreamGetReadable;
1153 pThis->IHostAudio.pfnStreamGetWritable = drvAudioVideoRecHA_StreamGetWritable;
1154 pThis->IHostAudio.pfnStreamGetStatus = drvAudioVideoRecHA_StreamGetStatus;
1155 pThis->IHostAudio.pfnStreamIterate = drvAudioVideoRecHA_StreamIterate;
1156 pThis->IHostAudio.pfnStreamPlay = drvAudioVideoRecHA_StreamPlay;
1157 pThis->IHostAudio.pfnStreamCapture = drvAudioVideoRecHA_StreamCapture;
1158 pThis->IHostAudio.pfnSetCallback = NULL;
1159 pThis->IHostAudio.pfnGetDevices = NULL;
1160 pThis->IHostAudio.pfnStreamGetPending = NULL;
1161 pThis->IHostAudio.pfnStreamPlayBegin = NULL;
1162 pThis->IHostAudio.pfnStreamPlayEnd = NULL;
1163 pThis->IHostAudio.pfnStreamCaptureBegin = NULL;
1164 pThis->IHostAudio.pfnStreamCaptureEnd = NULL;
1165
1166 /*
1167 * Get the Console object pointer.
1168 */
1169 void *pvUser;
1170 int rc = CFGMR3QueryPtr(pCfg, "ObjectConsole", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1171 AssertRCReturn(rc, rc);
1172
1173 /* CFGM tree saves the pointer to Console in the Object node of AudioVideoRec. */
1174 pThis->pConsole = (Console *)pvUser;
1175 AssertReturn(!pThis->pConsole.isNull(), VERR_INVALID_POINTER);
1176
1177 /*
1178 * Get the pointer to the audio driver instance.
1179 */
1180 rc = CFGMR3QueryPtr(pCfg, "Object", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1181 AssertRCReturn(rc, rc);
1182
1183 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1184 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1185
1186 /*
1187 * Get the recording container and codec parameters from the audio driver instance.
1188 */
1189 PAVRECCONTAINERPARMS pConParams = &pThis->ContainerParms;
1190 PAVRECCODECPARMS pCodecParms = &pThis->CodecParms;
1191
1192 RT_ZERO(pThis->ContainerParms);
1193 RT_ZERO(pThis->CodecParms);
1194
1195 rc = CFGMR3QueryU32(pCfg, "ContainerType", (uint32_t *)&pConParams->enmType);
1196 AssertRCReturn(rc, rc);
1197
1198 switch (pConParams->enmType)
1199 {
1200 case AVRECCONTAINERTYPE_WEBM:
1201 rc = CFGMR3QueryStringAlloc(pCfg, "ContainerFileName", &pConParams->WebM.pszFile);
1202 AssertRCReturn(rc, rc);
1203 break;
1204
1205 default:
1206 break;
1207 }
1208
1209 uint32_t uHz = 0;
1210 rc = CFGMR3QueryU32(pCfg, "CodecHz", &uHz);
1211 AssertRCReturn(rc, rc);
1212
1213 uint8_t cSampleBits = 0;
1214 rc = CFGMR3QueryU8(pCfg, "CodecBits", &cSampleBits); /** @todo CodecBits != CodecBytes */
1215 AssertRCReturn(rc, rc);
1216
1217 uint8_t cChannels = 0;
1218 rc = CFGMR3QueryU8(pCfg, "CodecChannels", &cChannels);
1219 AssertRCReturn(rc, rc);
1220
1221 PDMAudioPropsInit(&pCodecParms->PCMProps, cSampleBits / 8, true /*fSigned*/, cChannels, uHz);
1222 AssertMsgReturn(PDMAudioPropsAreValid(&pCodecParms->PCMProps),
1223 ("Configuration error: Audio configuration is invalid!\n"), VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES); /** @todo wrong status code. */
1224
1225 rc = CFGMR3QueryU32(pCfg, "CodecBitrate", &pCodecParms->uBitrate);
1226 AssertRCReturn(rc, rc);
1227
1228 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1229 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1230
1231 pThis->pAudioVideoRec->mpDrv = pThis;
1232
1233 /*
1234 * Get the interface for the above driver (DrvAudio) to make mixer/conversion calls.
1235 * Described in CFGM tree.
1236 */
1237 pThis->pDrvAudio = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIAUDIOCONNECTOR);
1238 AssertMsgReturn(pThis->pDrvAudio, ("Configuration error: No upper interface specified!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
1239
1240#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
1241 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.webm");
1242 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm");
1243#endif
1244
1245 return VINF_SUCCESS;
1246}
1247
1248
1249/**
1250 * @interface_method_impl{PDMDRVREG,pfnDestruct}
1251 */
1252/* static */
1253DECLCALLBACK(void) AudioVideoRec::drvDestruct(PPDMDRVINS pDrvIns)
1254{
1255 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1256 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1257
1258 LogFlowFuncEnter();
1259
1260 switch (pThis->ContainerParms.enmType)
1261 {
1262 case AVRECCONTAINERTYPE_WEBM:
1263 {
1264 avRecSinkShutdown(&pThis->Sink);
1265 RTStrFree(pThis->ContainerParms.WebM.pszFile);
1266 break;
1267 }
1268
1269 default:
1270 break;
1271 }
1272
1273 /*
1274 * If the AudioVideoRec object is still alive, we must clear it's reference to
1275 * us since we'll be invalid when we return from this method.
1276 */
1277 if (pThis->pAudioVideoRec)
1278 {
1279 pThis->pAudioVideoRec->mpDrv = NULL;
1280 pThis->pAudioVideoRec = NULL;
1281 }
1282
1283 LogFlowFuncLeave();
1284}
1285
1286
1287/**
1288 * @interface_method_impl{PDMDRVREG,pfnAttach}
1289 */
1290/* static */
1291DECLCALLBACK(int) AudioVideoRec::drvAttach(PPDMDRVINS pDrvIns, uint32_t fFlags)
1292{
1293 RT_NOREF(pDrvIns, fFlags);
1294
1295 LogFlowFuncEnter();
1296
1297 return VINF_SUCCESS;
1298}
1299
1300/**
1301 * @interface_method_impl{PDMDRVREG,pfnDetach}
1302 */
1303/* static */
1304DECLCALLBACK(void) AudioVideoRec::drvDetach(PPDMDRVINS pDrvIns, uint32_t fFlags)
1305{
1306 RT_NOREF(pDrvIns, fFlags);
1307
1308 LogFlowFuncEnter();
1309}
1310
1311/**
1312 * Video recording audio driver registration record.
1313 */
1314const PDMDRVREG AudioVideoRec::DrvReg =
1315{
1316 PDM_DRVREG_VERSION,
1317 /* szName */
1318 "AudioVideoRec",
1319 /* szRCMod */
1320 "",
1321 /* szR0Mod */
1322 "",
1323 /* pszDescription */
1324 "Audio driver for video recording",
1325 /* fFlags */
1326 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1327 /* fClass. */
1328 PDM_DRVREG_CLASS_AUDIO,
1329 /* cMaxInstances */
1330 ~0U,
1331 /* cbInstance */
1332 sizeof(DRVAUDIORECORDING),
1333 /* pfnConstruct */
1334 AudioVideoRec::drvConstruct,
1335 /* pfnDestruct */
1336 AudioVideoRec::drvDestruct,
1337 /* pfnRelocate */
1338 NULL,
1339 /* pfnIOCtl */
1340 NULL,
1341 /* pfnPowerOn */
1342 NULL,
1343 /* pfnReset */
1344 NULL,
1345 /* pfnSuspend */
1346 NULL,
1347 /* pfnResume */
1348 NULL,
1349 /* pfnAttach */
1350 AudioVideoRec::drvAttach,
1351 /* pfnDetach */
1352 AudioVideoRec::drvDetach,
1353 /* pfnPowerOff */
1354 NULL,
1355 /* pfnSoftReset */
1356 NULL,
1357 /* u32EndVersion */
1358 PDM_DRVREG_VERSION
1359};
1360
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