VirtualBox

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

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

Audio: doxygen fix. bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.1 KB
Line 
1/* $Id: DrvAudioRec.cpp 88030 2021-03-08 23:18:23Z 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 IPRT 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 = pCodecParms->PCMProps.uHz;
307 uint8_t cBytes = pCodecParms->PCMProps.cbSample;
308 uint8_t cChannels = pCodecParms->PCMProps.cChannels;
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, cBytes * 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 pCodec->Parms.PCMProps.uHz = uHz;
425 pCodec->Parms.PCMProps.cChannels = cChannels;
426 pCodec->Parms.PCMProps.cbSample = cBytes;
427 pCodec->Parms.PCMProps.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pSink->Codec.Parms.PCMProps.cbSample,
428 pSink->Codec.Parms.PCMProps.cChannels);
429 pCodec->Parms.uBitrate = uBitrate;
430
431 pCodec->Opus.pEnc = pEnc;
432 pCodec->Opus.msFrame = AVREC_OPUS_FRAME_MS_DEFAULT;
433
434 if (!pCodec->Opus.msFrame)
435 pCodec->Opus.msFrame = AVREC_OPUS_FRAME_MS_DEFAULT; /* 20ms by default; to prevent division by zero. */
436 pCodec->Opus.csFrame = pSink->Codec.Parms.PCMProps.uHz / (1000 /* s in ms */ / pSink->Codec.Opus.msFrame);
437 pCodec->Opus.cbFrame = PDMAudioPropsFramesToBytes(&pSink->Codec.Parms.PCMProps, pCodec->Opus.csFrame);
438
439#ifdef VBOX_WITH_STATISTICS
440 pSink->Codec.STAM.cEncFrames = 0;
441 pSink->Codec.STAM.msEncTotal = 0;
442#endif
443 pSink->tsStartMs = RTTimeMilliTS();
444 }
445 else
446 {
447 if (pEnc)
448 {
449 opus_encoder_destroy(pEnc);
450 pEnc = NULL;
451 }
452
453 LogRel(("Recording: Error creating sink (%Rrc)\n", rc));
454 }
455
456 return rc;
457}
458
459
460/**
461 * Shuts down (closes) a recording sink,
462 *
463 * @returns IPRT status code.
464 * @param pSink Recording sink to shut down.
465 */
466static void avRecSinkShutdown(PAVRECSINK pSink)
467{
468 AssertPtrReturnVoid(pSink);
469
470#ifdef VBOX_WITH_LIBOPUS
471 if (pSink->Codec.Opus.pEnc)
472 {
473 opus_encoder_destroy(pSink->Codec.Opus.pEnc);
474 pSink->Codec.Opus.pEnc = NULL;
475 }
476#endif
477 switch (pSink->Con.Parms.enmType)
478 {
479 case AVRECCONTAINERTYPE_WEBM:
480 {
481 if (pSink->Con.WebM.pWebM)
482 {
483 LogRel2(("Recording: Finished recording audio to file '%s' (%zu bytes)\n",
484 pSink->Con.WebM.pWebM->GetFileName().c_str(), pSink->Con.WebM.pWebM->GetFileSize()));
485
486 int rc2 = pSink->Con.WebM.pWebM->Close();
487 AssertRC(rc2);
488
489 delete pSink->Con.WebM.pWebM;
490 pSink->Con.WebM.pWebM = NULL;
491 }
492 break;
493 }
494
495 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
496 default:
497 break;
498 }
499}
500
501
502/**
503 * Creates an audio output stream and associates it with the specified recording sink.
504 *
505 * @returns IPRT status code.
506 * @param pThis Driver instance.
507 * @param pStreamAV Audio output stream to create.
508 * @param pSink Recording sink to associate audio output stream to.
509 * @param pCfgReq Requested configuration by the audio backend.
510 * @param pCfgAcq Acquired configuration by the audio output stream.
511 */
512static int avRecCreateStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV,
513 PAVRECSINK pSink, PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
514{
515 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
516 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
517 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
518 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
519 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
520
521 if (pCfgReq->u.enmDst != PDMAUDIOPLAYBACKDST_FRONT)
522 {
523 LogRel2(("Recording: Support for surround audio not implemented yet\n"));
524 AssertFailed();
525 return VERR_NOT_SUPPORTED;
526 }
527
528#ifdef VBOX_WITH_LIBOPUS
529 int rc = RTCircBufCreate(&pStreamAV->pCircBuf, pSink->Codec.Opus.cbFrame * 2 /* Use "double buffering" */);
530 if (RT_SUCCESS(rc))
531 {
532 size_t cbScratchBuf = pSink->Codec.Opus.cbFrame;
533 pStreamAV->pvSrcBuf = RTMemAlloc(cbScratchBuf);
534 if (pStreamAV->pvSrcBuf)
535 {
536 pStreamAV->cbSrcBuf = cbScratchBuf;
537 pStreamAV->pvDstBuf = RTMemAlloc(cbScratchBuf);
538 if (pStreamAV->pvDstBuf)
539 {
540 pStreamAV->cbDstBuf = cbScratchBuf;
541
542 pStreamAV->pSink = pSink; /* Assign sink to stream. */
543 pStreamAV->uLastPTSMs = 0;
544
545 if (pCfgAcq)
546 {
547 /* Make sure to let the driver backend know that we need the audio data in
548 * a specific sampling rate Opus is optimized for. */
549 pCfgAcq->Props.uHz = pSink->Codec.Parms.PCMProps.uHz;
550 pCfgAcq->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfgAcq->Props.cbSample, pCfgAcq->Props.cChannels);
551
552 /* Every Opus frame marks a period for now. Optimize this later. */
553 pCfgAcq->Backend.cFramesPeriod = PDMAudioPropsMilliToFrames(&pCfgAcq->Props, pSink->Codec.Opus.msFrame);
554 pCfgAcq->Backend.cFramesBufferSize = PDMAudioPropsMilliToFrames(&pCfgAcq->Props, 100 /*ms*/); /** @todo Make this configurable. */
555 pCfgAcq->Backend.cFramesPreBuffering = pCfgAcq->Backend.cFramesPeriod * 2;
556 }
557 }
558 else
559 rc = VERR_NO_MEMORY;
560 }
561 else
562 rc = VERR_NO_MEMORY;
563 }
564#else
565 RT_NOREF(pThis, pSink, pStreamAV, pCfgReq, pCfgAcq);
566 int rc = VERR_NOT_SUPPORTED;
567#endif /* VBOX_WITH_LIBOPUS */
568
569 LogFlowFuncLeaveRC(rc);
570 return rc;
571}
572
573
574/**
575 * Destroys (closes) an audio output stream.
576 *
577 * @returns IPRT status code.
578 * @param pThis Driver instance.
579 * @param pStreamAV Audio output stream to destroy.
580 */
581static int avRecDestroyStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV)
582{
583 RT_NOREF(pThis);
584
585 if (pStreamAV->pCircBuf)
586 {
587 RTCircBufDestroy(pStreamAV->pCircBuf);
588 pStreamAV->pCircBuf = NULL;
589 }
590
591 if (pStreamAV->pvSrcBuf)
592 {
593 Assert(pStreamAV->cbSrcBuf);
594 RTMemFree(pStreamAV->pvSrcBuf);
595 pStreamAV->pvSrcBuf = NULL;
596 pStreamAV->cbSrcBuf = 0;
597 }
598
599 if (pStreamAV->pvDstBuf)
600 {
601 Assert(pStreamAV->cbDstBuf);
602 RTMemFree(pStreamAV->pvDstBuf);
603 pStreamAV->pvDstBuf = NULL;
604 pStreamAV->cbDstBuf = 0;
605 }
606
607 return VINF_SUCCESS;
608}
609
610
611/**
612 * Controls an audio output stream
613 *
614 * @returns IPRT status code.
615 * @param pThis Driver instance.
616 * @param pStreamAV Audio output stream to control.
617 * @param enmStreamCmd Stream command to issue.
618 */
619static int avRecControlStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV, PDMAUDIOSTREAMCMD enmStreamCmd)
620{
621 RT_NOREF(pThis, pStreamAV);
622
623 int rc = VINF_SUCCESS;
624
625 switch (enmStreamCmd)
626 {
627 case PDMAUDIOSTREAMCMD_ENABLE:
628 case PDMAUDIOSTREAMCMD_DISABLE:
629 case PDMAUDIOSTREAMCMD_RESUME:
630 case PDMAUDIOSTREAMCMD_PAUSE:
631 break;
632
633 default:
634 rc = VERR_NOT_SUPPORTED;
635 break;
636 }
637
638 return rc;
639}
640
641
642/**
643 * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
644 */
645static DECLCALLBACK(int) drvAudioVideoRecHA_Init(PPDMIHOSTAUDIO pInterface)
646{
647 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
648
649 LogFlowFuncEnter();
650
651 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
652
653 LogRel(("Recording: Audio driver is using %RU32Hz, %RU16bit, %RU8 %s\n",
654 pThis->CodecParms.PCMProps.uHz, pThis->CodecParms.PCMProps.cbSample * 8,
655 pThis->CodecParms.PCMProps.cChannels, pThis->CodecParms.PCMProps.cChannels == 1 ? "channel" : "channels"));
656
657 int rc = avRecSinkInit(pThis, &pThis->Sink, &pThis->ContainerParms, &pThis->CodecParms);
658 if (RT_FAILURE(rc))
659 {
660 LogRel(("Recording: Audio recording driver failed to initialize, rc=%Rrc\n", rc));
661 }
662 else
663 LogRel2(("Recording: Audio recording driver initialized\n"));
664
665 return rc;
666}
667
668
669/**
670 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
671 */
672static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
673 void *pvBuf, uint32_t uBufSize, uint32_t *puRead)
674{
675 RT_NOREF(pInterface, pStream, pvBuf, uBufSize);
676
677 if (puRead)
678 *puRead = 0;
679
680 return VINF_SUCCESS;
681}
682
683
684/**
685 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
686 */
687static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
688 const void *pvBuf, uint32_t uBufSize, uint32_t *puWritten)
689{
690 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
691 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
692 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
693 AssertReturn(uBufSize, VERR_INVALID_PARAMETER);
694 /* puWritten is optional. */
695
696 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
697 RT_NOREF(pThis);
698 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
699
700 int rc = VINF_SUCCESS;
701
702 uint32_t cbWrittenTotal = 0;
703
704 /*
705 * Call the encoder with the data.
706 */
707#ifdef VBOX_WITH_LIBOPUS
708 PAVRECSINK pSink = pStreamAV->pSink;
709 AssertPtr(pSink);
710 PAVRECCODEC pCodec = &pSink->Codec;
711 AssertPtr(pCodec);
712 PRTCIRCBUF pCircBuf = pStreamAV->pCircBuf;
713 AssertPtr(pCircBuf);
714
715 void *pvCircBuf;
716 size_t cbCircBuf;
717
718 uint32_t cbToWrite = uBufSize;
719
720 /*
721 * Fetch as much as we can into our internal ring buffer.
722 */
723 while ( cbToWrite
724 && RTCircBufFree(pCircBuf))
725 {
726 RTCircBufAcquireWriteBlock(pCircBuf, cbToWrite, &pvCircBuf, &cbCircBuf);
727
728 if (cbCircBuf)
729 {
730 memcpy(pvCircBuf, (uint8_t *)pvBuf + cbWrittenTotal, cbCircBuf),
731 cbWrittenTotal += (uint32_t)cbCircBuf;
732 Assert(cbToWrite >= cbCircBuf);
733 cbToWrite -= (uint32_t)cbCircBuf;
734 }
735
736 RTCircBufReleaseWriteBlock(pCircBuf, cbCircBuf);
737
738 if ( RT_FAILURE(rc)
739 || !cbCircBuf)
740 {
741 break;
742 }
743 }
744
745 /*
746 * Process our internal ring buffer and encode the data.
747 */
748
749 uint32_t cbSrc;
750
751 /* Only encode data if we have data for the given time period (or more). */
752 while (RTCircBufUsed(pCircBuf) >= pCodec->Opus.cbFrame)
753 {
754 LogFunc(("cbAvail=%zu, csFrame=%RU32, cbFrame=%RU32\n",
755 RTCircBufUsed(pCircBuf), pCodec->Opus.csFrame, pCodec->Opus.cbFrame));
756
757 cbSrc = 0;
758
759 while (cbSrc < pCodec->Opus.cbFrame)
760 {
761 RTCircBufAcquireReadBlock(pCircBuf, pCodec->Opus.cbFrame - cbSrc, &pvCircBuf, &cbCircBuf);
762
763 if (cbCircBuf)
764 {
765 memcpy((uint8_t *)pStreamAV->pvSrcBuf + cbSrc, pvCircBuf, cbCircBuf);
766
767 cbSrc += (uint32_t)cbCircBuf;
768 Assert(cbSrc <= pStreamAV->cbSrcBuf);
769 }
770
771 RTCircBufReleaseReadBlock(pCircBuf, cbCircBuf);
772
773 if (!cbCircBuf)
774 break;
775 }
776
777# ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
778 RTFILE fh;
779 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm",
780 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
781 RTFileWrite(fh, pStreamAV->pvSrcBuf, cbSrc, NULL);
782 RTFileClose(fh);
783# endif
784
785 Assert(cbSrc == pCodec->Opus.cbFrame);
786
787 /*
788 * Opus always encodes PER "OPUS FRAME", that is, exactly 2.5, 5, 10, 20, 40 or 60 ms of audio data.
789 *
790 * A packet can have up to 120ms worth of audio data.
791 * Anything > 120ms of data will result in a "corrupted package" error message by
792 * by decoding application.
793 */
794
795 /* Call the encoder to encode one "Opus frame" per iteration. */
796 opus_int32 cbWritten = opus_encode(pSink->Codec.Opus.pEnc,
797 (opus_int16 *)pStreamAV->pvSrcBuf, pCodec->Opus.csFrame,
798 (uint8_t *)pStreamAV->pvDstBuf, (opus_int32)pStreamAV->cbDstBuf);
799 if (cbWritten > 0)
800 {
801 /* Get overall frames encoded. */
802 const uint32_t cEncFrames = opus_packet_get_nb_frames((uint8_t *)pStreamAV->pvDstBuf, cbWritten);
803
804# ifdef VBOX_WITH_STATISTICS
805 pSink->Codec.STAM.cEncFrames += cEncFrames;
806 pSink->Codec.STAM.msEncTotal += pSink->Codec.Opus.msFrame * cEncFrames;
807# endif
808 Assert((uint32_t)cbWritten <= (uint32_t)pStreamAV->cbDstBuf);
809 const uint32_t cbDst = RT_MIN((uint32_t)cbWritten, (uint32_t)pStreamAV->cbDstBuf);
810
811 Assert(cEncFrames == 1);
812
813 if (pStreamAV->uLastPTSMs == 0)
814 pStreamAV->uLastPTSMs = RTTimeProgramMilliTS(); /* We want the absolute time (in ms) since program start. */
815
816 const uint64_t uDurationMs = pSink->Codec.Opus.msFrame * cEncFrames;
817 const uint64_t uPTSMs = pStreamAV->uLastPTSMs;
818
819 pStreamAV->uLastPTSMs += uDurationMs;
820
821 switch (pSink->Con.Parms.enmType)
822 {
823 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
824 {
825 HRESULT hr = pSink->Con.Main.pConsole->i_recordingSendAudio(pStreamAV->pvDstBuf, cbDst, uPTSMs);
826 Assert(hr == S_OK);
827 RT_NOREF(hr);
828
829 break;
830 }
831
832 case AVRECCONTAINERTYPE_WEBM:
833 {
834 WebMWriter::BlockData_Opus blockData = { pStreamAV->pvDstBuf, cbDst, uPTSMs };
835 rc = pSink->Con.WebM.pWebM->WriteBlock(pSink->Con.WebM.uTrack, &blockData, sizeof(blockData));
836 AssertRC(rc);
837
838 break;
839 }
840
841 default:
842 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
843 break;
844 }
845 }
846 else if (cbWritten < 0)
847 {
848 AssertMsgFailed(("Encoding failed: %s\n", opus_strerror(cbWritten)));
849 rc = VERR_INVALID_PARAMETER;
850 }
851
852 if (RT_FAILURE(rc))
853 break;
854 }
855
856 if (puWritten)
857 *puWritten = cbWrittenTotal;
858#else
859 /* Report back all data as being processed. */
860 if (puWritten)
861 *puWritten = uBufSize;
862
863 rc = VERR_NOT_SUPPORTED;
864#endif /* VBOX_WITH_LIBOPUS */
865
866 LogFlowFunc(("csReadTotal=%RU32, rc=%Rrc\n", cbWrittenTotal, rc));
867 return rc;
868}
869
870
871/**
872 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
873 */
874static DECLCALLBACK(int) drvAudioVideoRecHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
875{
876 RT_NOREF(pInterface);
877 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
878
879 RTStrPrintf2(pBackendCfg->szName, sizeof(pBackendCfg->szName), "VideoRec");
880
881 pBackendCfg->cbStreamOut = sizeof(AVRECSTREAM);
882 pBackendCfg->cbStreamIn = 0;
883 pBackendCfg->cMaxStreamsIn = 0;
884 pBackendCfg->cMaxStreamsOut = UINT32_MAX;
885
886 return VINF_SUCCESS;
887}
888
889
890/**
891 * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
892 */
893static DECLCALLBACK(void) drvAudioVideoRecHA_Shutdown(PPDMIHOSTAUDIO pInterface)
894{
895 LogFlowFuncEnter();
896
897 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
898
899 avRecSinkShutdown(&pThis->Sink);
900}
901
902
903/**
904 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
905 */
906static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvAudioVideoRecHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
907{
908 RT_NOREF(enmDir);
909 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
910
911 return PDMAUDIOBACKENDSTS_RUNNING;
912}
913
914
915/**
916 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
917 */
918static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
919 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
920{
921 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
922 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
923 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
924
925 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
926 return VERR_NOT_SUPPORTED;
927
928 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
929
930 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
931 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
932
933 /* For now we only have one sink, namely the driver's one.
934 * Later each stream could have its own one, to e.g. router different stream to different sinks .*/
935 PAVRECSINK pSink = &pThis->Sink;
936
937 int rc = avRecCreateStreamOut(pThis, pStreamAV, pSink, pCfgReq, pCfgAcq);
938 if (RT_SUCCESS(rc))
939 {
940 pStreamAV->pCfg = PDMAudioStrmCfgDup(pCfgAcq);
941 if (!pStreamAV->pCfg)
942 rc = VERR_NO_MEMORY;
943 }
944
945 return rc;
946}
947
948
949/**
950 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
951 */
952static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
953{
954 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
955 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
956
957 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
958 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
959
960 if (!pStreamAV->pCfg) /* Not (yet) configured? Skip. */
961 return VINF_SUCCESS;
962
963 int rc = VINF_SUCCESS;
964
965 if (pStreamAV->pCfg->enmDir == PDMAUDIODIR_OUT)
966 rc = avRecDestroyStreamOut(pThis, pStreamAV);
967
968 if (RT_SUCCESS(rc))
969 {
970 PDMAudioStrmCfgFree(pStreamAV->pCfg);
971 pStreamAV->pCfg = NULL;
972 }
973
974 return rc;
975}
976
977
978/**
979 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
980 */
981static DECLCALLBACK(int) drvAudioVideoRecHA_StreamControl(PPDMIHOSTAUDIO pInterface,
982 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
983{
984 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
985 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
986
987 PDRVAUDIORECORDING pThis = PDMIHOSTAUDIO_2_DRVAUDIORECORDING(pInterface);
988 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
989
990 if (!pStreamAV->pCfg) /* Not (yet) configured? Skip. */
991 return VINF_SUCCESS;
992
993 if (pStreamAV->pCfg->enmDir == PDMAUDIODIR_OUT)
994 return avRecControlStreamOut(pThis, pStreamAV, enmStreamCmd);
995
996 return VINF_SUCCESS;
997}
998
999
1000/**
1001 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
1002 */
1003static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1004{
1005 RT_NOREF(pInterface, pStream);
1006
1007 return 0; /* Video capturing does not provide any input. */
1008}
1009
1010
1011/**
1012 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
1013 */
1014static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1015{
1016 RT_NOREF(pInterface, pStream);
1017
1018 return UINT32_MAX;
1019}
1020
1021
1022/**
1023 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
1024 */
1025static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvAudioVideoRecHA_StreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1026{
1027 RT_NOREF(pInterface, pStream);
1028
1029 return PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED | PDMAUDIOSTREAMSTS_FLAGS_ENABLED;
1030}
1031
1032
1033/**
1034 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
1035 */
1036static DECLCALLBACK(int) drvAudioVideoRecHA_StreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1037{
1038 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1039 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1040
1041 LogFlowFuncEnter();
1042
1043 /* Nothing to do here for video recording. */
1044 return VINF_SUCCESS;
1045}
1046
1047
1048/**
1049 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1050 */
1051static DECLCALLBACK(void *) drvAudioVideoRecQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1052{
1053 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1054 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1055
1056 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1057 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
1058 return NULL;
1059}
1060
1061
1062AudioVideoRec::AudioVideoRec(Console *pConsole)
1063 : AudioDriver(pConsole)
1064 , mpDrv(NULL)
1065{
1066}
1067
1068
1069AudioVideoRec::~AudioVideoRec(void)
1070{
1071 if (mpDrv)
1072 {
1073 mpDrv->pAudioVideoRec = NULL;
1074 mpDrv = NULL;
1075 }
1076}
1077
1078
1079/**
1080 * Applies a video recording configuration to this driver instance.
1081 *
1082 * @returns IPRT status code.
1083 * @param Settings Capturing configuration to apply.
1084 */
1085int AudioVideoRec::applyConfiguration(const settings::RecordingSettings &Settings)
1086{
1087 /** @todo Do some validation here. */
1088 mVideoRecCfg = Settings; /* Note: Does have an own copy operator. */
1089 return VINF_SUCCESS;
1090}
1091
1092
1093/**
1094 * @copydoc AudioDriver::configureDriver
1095 */
1096int AudioVideoRec::configureDriver(PCFGMNODE pLunCfg)
1097{
1098 int rc = CFGMR3InsertInteger(pLunCfg, "Object", (uintptr_t)mpConsole->i_recordingGetAudioDrv());
1099 AssertRCReturn(rc, rc);
1100 rc = CFGMR3InsertInteger(pLunCfg, "ObjectConsole", (uintptr_t)mpConsole);
1101 AssertRCReturn(rc, rc);
1102
1103 /** @todo For now we're using the configuration of the first screen here audio-wise. */
1104 Assert(mVideoRecCfg.mapScreens.size() >= 1);
1105 const settings::RecordingScreenSettings &Screen0Settings = mVideoRecCfg.mapScreens[0];
1106
1107 rc = CFGMR3InsertInteger(pLunCfg, "ContainerType", (uint64_t)Screen0Settings.enmDest);
1108 AssertRCReturn(rc, rc);
1109 if (Screen0Settings.enmDest == RecordingDestination_File)
1110 {
1111 rc = CFGMR3InsertString(pLunCfg, "ContainerFileName", Utf8Str(Screen0Settings.File.strName).c_str());
1112 AssertRCReturn(rc, rc);
1113 }
1114 rc = CFGMR3InsertInteger(pLunCfg, "CodecHz", Screen0Settings.Audio.uHz);
1115 AssertRCReturn(rc, rc);
1116 rc = CFGMR3InsertInteger(pLunCfg, "CodecBits", Screen0Settings.Audio.cBits);
1117 AssertRCReturn(rc, rc);
1118 rc = CFGMR3InsertInteger(pLunCfg, "CodecChannels", Screen0Settings.Audio.cChannels);
1119 AssertRCReturn(rc, rc);
1120 rc = CFGMR3InsertInteger(pLunCfg, "CodecBitrate", 0); /* Let Opus decide for now. */
1121 AssertRCReturn(rc, rc);
1122
1123 return AudioDriver::configureDriver(pLunCfg);
1124}
1125
1126
1127/**
1128 * Construct a audio video recording driver instance.
1129 *
1130 * @copydoc FNPDMDRVCONSTRUCT
1131 */
1132/* static */
1133DECLCALLBACK(int) AudioVideoRec::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1134{
1135 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1136 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1137 RT_NOREF(fFlags);
1138
1139 LogRel(("Audio: Initializing video recording audio driver\n"));
1140 LogFlowFunc(("fFlags=0x%x\n", fFlags));
1141
1142 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1143 ("Configuration error: Not possible to attach anything to this driver!\n"),
1144 VERR_PDM_DRVINS_NO_ATTACH);
1145
1146 /*
1147 * Init the static parts.
1148 */
1149 pThis->pDrvIns = pDrvIns;
1150 /* IBase */
1151 pDrvIns->IBase.pfnQueryInterface = drvAudioVideoRecQueryInterface;
1152 /* IHostAudio */
1153 pThis->IHostAudio.pfnInit = drvAudioVideoRecHA_Init;
1154 pThis->IHostAudio.pfnShutdown = drvAudioVideoRecHA_Shutdown;
1155 pThis->IHostAudio.pfnGetConfig = drvAudioVideoRecHA_GetConfig;
1156 pThis->IHostAudio.pfnGetStatus = drvAudioVideoRecHA_GetStatus;
1157 pThis->IHostAudio.pfnStreamCreate = drvAudioVideoRecHA_StreamCreate;
1158 pThis->IHostAudio.pfnStreamDestroy = drvAudioVideoRecHA_StreamDestroy;
1159 pThis->IHostAudio.pfnStreamControl = drvAudioVideoRecHA_StreamControl;
1160 pThis->IHostAudio.pfnStreamGetReadable = drvAudioVideoRecHA_StreamGetReadable;
1161 pThis->IHostAudio.pfnStreamGetWritable = drvAudioVideoRecHA_StreamGetWritable;
1162 pThis->IHostAudio.pfnStreamGetStatus = drvAudioVideoRecHA_StreamGetStatus;
1163 pThis->IHostAudio.pfnStreamIterate = drvAudioVideoRecHA_StreamIterate;
1164 pThis->IHostAudio.pfnStreamPlay = drvAudioVideoRecHA_StreamPlay;
1165 pThis->IHostAudio.pfnStreamCapture = drvAudioVideoRecHA_StreamCapture;
1166 pThis->IHostAudio.pfnSetCallback = NULL;
1167 pThis->IHostAudio.pfnGetDevices = NULL;
1168 pThis->IHostAudio.pfnStreamGetPending = NULL;
1169 pThis->IHostAudio.pfnStreamPlayBegin = NULL;
1170 pThis->IHostAudio.pfnStreamPlayEnd = NULL;
1171 pThis->IHostAudio.pfnStreamCaptureBegin = NULL;
1172 pThis->IHostAudio.pfnStreamCaptureEnd = NULL;
1173
1174 /*
1175 * Get the Console object pointer.
1176 */
1177 void *pvUser;
1178 int rc = CFGMR3QueryPtr(pCfg, "ObjectConsole", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1179 AssertRCReturn(rc, rc);
1180
1181 /* CFGM tree saves the pointer to Console in the Object node of AudioVideoRec. */
1182 pThis->pConsole = (Console *)pvUser;
1183 AssertReturn(!pThis->pConsole.isNull(), VERR_INVALID_POINTER);
1184
1185 /*
1186 * Get the pointer to the audio driver instance.
1187 */
1188 rc = CFGMR3QueryPtr(pCfg, "Object", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1189 AssertRCReturn(rc, rc);
1190
1191 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1192 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1193
1194 /*
1195 * Get the recording container and codec parameters from the audio driver instance.
1196 */
1197 PAVRECCONTAINERPARMS pConParams = &pThis->ContainerParms;
1198 PAVRECCODECPARMS pCodecParms = &pThis->CodecParms;
1199 PPDMAUDIOPCMPROPS pPCMProps = &pCodecParms->PCMProps;
1200
1201 RT_ZERO(pThis->ContainerParms);
1202 RT_ZERO(pThis->CodecParms);
1203
1204 rc = CFGMR3QueryU32(pCfg, "ContainerType", (uint32_t *)&pConParams->enmType);
1205 AssertRCReturn(rc, rc);
1206
1207 switch (pConParams->enmType)
1208 {
1209 case AVRECCONTAINERTYPE_WEBM:
1210 rc = CFGMR3QueryStringAlloc(pCfg, "ContainerFileName", &pConParams->WebM.pszFile);
1211 AssertRCReturn(rc, rc);
1212 break;
1213
1214 default:
1215 break;
1216 }
1217
1218 rc = CFGMR3QueryU32(pCfg, "CodecHz", &pPCMProps->uHz);
1219 AssertRCReturn(rc, rc);
1220 rc = CFGMR3QueryU8(pCfg, "CodecBits", &pPCMProps->cbSample); /** @todo CodecBits != CodecBytes */
1221 AssertRCReturn(rc, rc);
1222 pPCMProps->cbSample /= 8; /* Bits to bytes. */
1223 rc = CFGMR3QueryU8(pCfg, "CodecChannels", &pPCMProps->cChannels);
1224 AssertRCReturn(rc, rc);
1225 rc = CFGMR3QueryU32(pCfg, "CodecBitrate", &pCodecParms->uBitrate);
1226 AssertRCReturn(rc, rc);
1227
1228 pPCMProps->cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pPCMProps->cbSample, pPCMProps->cChannels);
1229 pPCMProps->fSigned = true;
1230 pPCMProps->fSwapEndian = false;
1231
1232 AssertMsgReturn(PDMAudioPropsAreValid(pPCMProps),
1233 ("Configuration error: Audio configuration is invalid!\n"), VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES);
1234
1235 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1236 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1237
1238 pThis->pAudioVideoRec->mpDrv = pThis;
1239
1240 /*
1241 * Get the interface for the above driver (DrvAudio) to make mixer/conversion calls.
1242 * Described in CFGM tree.
1243 */
1244 pThis->pDrvAudio = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIAUDIOCONNECTOR);
1245 AssertMsgReturn(pThis->pDrvAudio, ("Configuration error: No upper interface specified!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
1246
1247#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
1248 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.webm");
1249 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm");
1250#endif
1251
1252 return VINF_SUCCESS;
1253}
1254
1255
1256/**
1257 * @interface_method_impl{PDMDRVREG,pfnDestruct}
1258 */
1259/* static */
1260DECLCALLBACK(void) AudioVideoRec::drvDestruct(PPDMDRVINS pDrvIns)
1261{
1262 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1263 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1264
1265 LogFlowFuncEnter();
1266
1267 switch (pThis->ContainerParms.enmType)
1268 {
1269 case AVRECCONTAINERTYPE_WEBM:
1270 {
1271 avRecSinkShutdown(&pThis->Sink);
1272 RTStrFree(pThis->ContainerParms.WebM.pszFile);
1273 break;
1274 }
1275
1276 default:
1277 break;
1278 }
1279
1280 /*
1281 * If the AudioVideoRec object is still alive, we must clear it's reference to
1282 * us since we'll be invalid when we return from this method.
1283 */
1284 if (pThis->pAudioVideoRec)
1285 {
1286 pThis->pAudioVideoRec->mpDrv = NULL;
1287 pThis->pAudioVideoRec = NULL;
1288 }
1289
1290 LogFlowFuncLeave();
1291}
1292
1293
1294/**
1295 * @interface_method_impl{PDMDRVREG,pfnAttach}
1296 */
1297/* static */
1298DECLCALLBACK(int) AudioVideoRec::drvAttach(PPDMDRVINS pDrvIns, uint32_t fFlags)
1299{
1300 RT_NOREF(pDrvIns, fFlags);
1301
1302 LogFlowFuncEnter();
1303
1304 return VINF_SUCCESS;
1305}
1306
1307/**
1308 * @interface_method_impl{PDMDRVREG,pfnDetach}
1309 */
1310/* static */
1311DECLCALLBACK(void) AudioVideoRec::drvDetach(PPDMDRVINS pDrvIns, uint32_t fFlags)
1312{
1313 RT_NOREF(pDrvIns, fFlags);
1314
1315 LogFlowFuncEnter();
1316}
1317
1318/**
1319 * Video recording audio driver registration record.
1320 */
1321const PDMDRVREG AudioVideoRec::DrvReg =
1322{
1323 PDM_DRVREG_VERSION,
1324 /* szName */
1325 "AudioVideoRec",
1326 /* szRCMod */
1327 "",
1328 /* szR0Mod */
1329 "",
1330 /* pszDescription */
1331 "Audio driver for video recording",
1332 /* fFlags */
1333 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1334 /* fClass. */
1335 PDM_DRVREG_CLASS_AUDIO,
1336 /* cMaxInstances */
1337 ~0U,
1338 /* cbInstance */
1339 sizeof(DRVAUDIORECORDING),
1340 /* pfnConstruct */
1341 AudioVideoRec::drvConstruct,
1342 /* pfnDestruct */
1343 AudioVideoRec::drvDestruct,
1344 /* pfnRelocate */
1345 NULL,
1346 /* pfnIOCtl */
1347 NULL,
1348 /* pfnPowerOn */
1349 NULL,
1350 /* pfnReset */
1351 NULL,
1352 /* pfnSuspend */
1353 NULL,
1354 /* pfnResume */
1355 NULL,
1356 /* pfnAttach */
1357 AudioVideoRec::drvAttach,
1358 /* pfnDetach */
1359 AudioVideoRec::drvDetach,
1360 /* pfnPowerOff */
1361 NULL,
1362 /* pfnSoftReset */
1363 NULL,
1364 /* u32EndVersion */
1365 PDM_DRVREG_VERSION
1366};
1367
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette