VirtualBox

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

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

Audio: Added geberuc asynchronous init to DrvAudio for use in WAS (and maybe others). bugref:9890

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