VirtualBox

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

Last change on this file since 92154 was 89510, checked in by vboxsync, 3 years ago

Audio: Split up PDMIHOSTAUDIO::pfnStreamControl into individual methods for each command. bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 43.4 KB
Line 
1/* $Id: DrvAudioRec.cpp 89510 2021-06-04 13:20:02Z 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, PCPDMAUDIOSTREAMCFG 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->enmPath != PDMAUDIOPATH_OUT_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 PCPDMAUDIOSTREAMCFG 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 bool fImmediate)
530{
531 PDRVAUDIORECORDING pThis = RT_FROM_CPP_MEMBER(pInterface, DRVAUDIORECORDING, IHostAudio);
532 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
533 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
534 RT_NOREF(fImmediate);
535
536 int rc = VINF_SUCCESS;
537 if (pStreamAV->Cfg.enmDir == PDMAUDIODIR_OUT)
538 rc = avRecDestroyStreamOut(pThis, pStreamAV);
539
540 return rc;
541}
542
543
544/**
545 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamEnable}
546 */
547static DECLCALLBACK(int) drvAudioVideoRecHA_StreamEnable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
548{
549 RT_NOREF(pInterface, pStream);
550 return VINF_SUCCESS;
551}
552
553
554/**
555 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDisable}
556 */
557static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDisable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
558{
559 RT_NOREF(pInterface, pStream);
560 return VINF_SUCCESS;
561}
562
563
564/**
565 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPause}
566 */
567static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPause(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
568{
569 RT_NOREF(pInterface, pStream);
570 return VINF_SUCCESS;
571}
572
573
574/**
575 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamResume}
576 */
577static DECLCALLBACK(int) drvAudioVideoRecHA_StreamResume(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
578{
579 RT_NOREF(pInterface, pStream);
580 return VINF_SUCCESS;
581}
582
583
584/**
585 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDrain}
586 */
587static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDrain(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
588{
589 RT_NOREF(pInterface, pStream);
590 return VINF_SUCCESS;
591}
592
593
594/**
595 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetState}
596 */
597static DECLCALLBACK(PDMHOSTAUDIOSTREAMSTATE) drvAudioVideoRecHA_StreamGetState(PPDMIHOSTAUDIO pInterface,
598 PPDMAUDIOBACKENDSTREAM pStream)
599{
600 RT_NOREF(pInterface);
601 AssertPtrReturn(pStream, PDMHOSTAUDIOSTREAMSTATE_INVALID);
602 return PDMHOSTAUDIOSTREAMSTATE_OKAY;
603}
604
605
606/**
607 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
608 */
609static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
610{
611 RT_NOREF(pInterface, pStream);
612 return UINT32_MAX;
613}
614
615
616/**
617 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
618 */
619static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
620 const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
621{
622 RT_NOREF(pInterface);
623 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
624 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
625 if (cbBuf)
626 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
627 AssertReturn(pcbWritten, VERR_INVALID_PARAMETER);
628
629 int rc = VINF_SUCCESS;
630
631 uint32_t cbWrittenTotal = 0;
632
633 /*
634 * Call the encoder with the data.
635 */
636#ifdef VBOX_WITH_LIBOPUS
637 PAVRECSINK pSink = pStreamAV->pSink;
638 AssertPtr(pSink);
639 PAVRECCODEC pCodec = &pSink->Codec;
640 AssertPtr(pCodec);
641 PRTCIRCBUF pCircBuf = pStreamAV->pCircBuf;
642 AssertPtr(pCircBuf);
643
644 uint32_t cbToWrite = cbBuf;
645
646 /*
647 * Write as much as we can into our internal ring buffer.
648 */
649 while ( cbToWrite > 0
650 && RTCircBufFree(pCircBuf))
651 {
652 void *pvCircBuf = NULL;
653 size_t cbCircBuf = 0;
654 RTCircBufAcquireWriteBlock(pCircBuf, cbToWrite, &pvCircBuf, &cbCircBuf);
655
656 if (cbCircBuf)
657 {
658 memcpy(pvCircBuf, (uint8_t *)pvBuf + cbWrittenTotal, cbCircBuf),
659 cbWrittenTotal += (uint32_t)cbCircBuf;
660 Assert(cbToWrite >= cbCircBuf);
661 cbToWrite -= (uint32_t)cbCircBuf;
662 }
663
664 RTCircBufReleaseWriteBlock(pCircBuf, cbCircBuf);
665 AssertBreak(cbCircBuf);
666 }
667
668 /*
669 * Process our internal ring buffer and encode the data.
670 */
671
672 /* Only encode data if we have data for the given time period (or more). */
673 while (RTCircBufUsed(pCircBuf) >= pCodec->Opus.cbFrame)
674 {
675 LogFunc(("cbAvail=%zu, csFrame=%RU32, cbFrame=%RU32\n",
676 RTCircBufUsed(pCircBuf), pCodec->Opus.csFrame, pCodec->Opus.cbFrame));
677
678 uint32_t cbSrc = 0;
679 while (cbSrc < pCodec->Opus.cbFrame)
680 {
681 void *pvCircBuf = NULL;
682 size_t cbCircBuf = 0;
683 RTCircBufAcquireReadBlock(pCircBuf, pCodec->Opus.cbFrame - cbSrc, &pvCircBuf, &cbCircBuf);
684
685 if (cbCircBuf)
686 {
687 memcpy((uint8_t *)pStreamAV->pvSrcBuf + cbSrc, pvCircBuf, cbCircBuf);
688
689 cbSrc += (uint32_t)cbCircBuf;
690 Assert(cbSrc <= pStreamAV->cbSrcBuf);
691 }
692
693 RTCircBufReleaseReadBlock(pCircBuf, cbCircBuf);
694 AssertBreak(cbCircBuf);
695 }
696
697 Assert(cbSrc == pCodec->Opus.cbFrame);
698
699# ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH
700 RTFILE fh;
701 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm",
702 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
703 RTFileWrite(fh, pStreamAV->pvSrcBuf, cbSrc, NULL);
704 RTFileClose(fh);
705# endif
706
707 /*
708 * Opus always encodes PER "OPUS FRAME", that is, exactly 2.5, 5, 10, 20, 40 or 60 ms of audio data.
709 *
710 * A packet can have up to 120ms worth of audio data.
711 * Anything > 120ms of data will result in a "corrupted package" error message by
712 * by decoding application.
713 */
714
715 /* Call the encoder to encode one "Opus frame" per iteration. */
716 opus_int32 cbWritten = opus_encode(pSink->Codec.Opus.pEnc,
717 (opus_int16 *)pStreamAV->pvSrcBuf, pCodec->Opus.csFrame,
718 (uint8_t *)pStreamAV->pvDstBuf, (opus_int32)pStreamAV->cbDstBuf);
719 if (cbWritten > 0)
720 {
721 /* Get overall frames encoded. */
722 const uint32_t cEncFrames = opus_packet_get_nb_frames((uint8_t *)pStreamAV->pvDstBuf, cbWritten);
723
724# ifdef VBOX_WITH_STATISTICS
725 pSink->Codec.Stats.cEncFrames += cEncFrames;
726 pSink->Codec.Stats.msEncTotal += pSink->Codec.Opus.msFrame * cEncFrames;
727# endif
728 Assert((uint32_t)cbWritten <= (uint32_t)pStreamAV->cbDstBuf);
729 const uint32_t cbDst = RT_MIN((uint32_t)cbWritten, (uint32_t)pStreamAV->cbDstBuf);
730
731 Assert(cEncFrames == 1);
732
733 if (pStreamAV->uLastPTSMs == 0)
734 pStreamAV->uLastPTSMs = RTTimeProgramMilliTS(); /* We want the absolute time (in ms) since program start. */
735
736 const uint64_t uDurationMs = pSink->Codec.Opus.msFrame * cEncFrames;
737 const uint64_t uPTSMs = pStreamAV->uLastPTSMs;
738
739 pStreamAV->uLastPTSMs += uDurationMs;
740
741 switch (pSink->Con.Parms.enmType)
742 {
743 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
744 {
745 HRESULT hr = pSink->Con.Main.pConsole->i_recordingSendAudio(pStreamAV->pvDstBuf, cbDst, uPTSMs);
746 Assert(hr == S_OK);
747 RT_NOREF(hr);
748 break;
749 }
750
751 case AVRECCONTAINERTYPE_WEBM:
752 {
753 WebMWriter::BlockData_Opus blockData = { pStreamAV->pvDstBuf, cbDst, uPTSMs };
754 rc = pSink->Con.WebM.pWebM->WriteBlock(pSink->Con.WebM.uTrack, &blockData, sizeof(blockData));
755 AssertRC(rc);
756 break;
757 }
758
759 default:
760 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
761 break;
762 }
763 }
764 else if (cbWritten < 0)
765 {
766 AssertMsgFailed(("Encoding failed: %s\n", opus_strerror(cbWritten)));
767 rc = VERR_INVALID_PARAMETER;
768 }
769
770 if (RT_FAILURE(rc))
771 break;
772 }
773
774 *pcbWritten = cbWrittenTotal;
775#else
776 /* Report back all data as being processed. */
777 *pcbWritten = cbBuf;
778
779 rc = VERR_NOT_SUPPORTED;
780#endif /* VBOX_WITH_LIBOPUS */
781
782 LogFlowFunc(("csReadTotal=%RU32, rc=%Rrc\n", cbWrittenTotal, rc));
783 return rc;
784}
785
786
787/**
788 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
789 */
790static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
791{
792 RT_NOREF(pInterface, pStream);
793 return 0; /* Video capturing does not provide any input. */
794}
795
796
797/**
798 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
799 */
800static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
801 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
802{
803 RT_NOREF(pInterface, pStream, pvBuf, cbBuf);
804 *pcbRead = 0;
805 return VINF_SUCCESS;
806}
807
808
809/*********************************************************************************************************************************
810* PDMIBASE *
811*********************************************************************************************************************************/
812
813/**
814 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
815 */
816static DECLCALLBACK(void *) drvAudioVideoRecQueryInterface(PPDMIBASE pInterface, const char *pszIID)
817{
818 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
819 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
820
821 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
822 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
823 return NULL;
824}
825
826
827/*********************************************************************************************************************************
828* PDMDRVREG *
829*********************************************************************************************************************************/
830
831/**
832 * Shuts down (closes) a recording sink,
833 *
834 * @returns VBox status code.
835 * @param pSink Recording sink to shut down.
836 */
837static void avRecSinkShutdown(PAVRECSINK pSink)
838{
839 AssertPtrReturnVoid(pSink);
840
841#ifdef VBOX_WITH_LIBOPUS
842 if (pSink->Codec.Opus.pEnc)
843 {
844 opus_encoder_destroy(pSink->Codec.Opus.pEnc);
845 pSink->Codec.Opus.pEnc = NULL;
846 }
847#endif
848 switch (pSink->Con.Parms.enmType)
849 {
850 case AVRECCONTAINERTYPE_WEBM:
851 {
852 if (pSink->Con.WebM.pWebM)
853 {
854 LogRel2(("Recording: Finished recording audio to file '%s' (%zu bytes)\n",
855 pSink->Con.WebM.pWebM->GetFileName().c_str(), pSink->Con.WebM.pWebM->GetFileSize()));
856
857 int rc2 = pSink->Con.WebM.pWebM->Close();
858 AssertRC(rc2);
859
860 delete pSink->Con.WebM.pWebM;
861 pSink->Con.WebM.pWebM = NULL;
862 }
863 break;
864 }
865
866 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
867 default:
868 break;
869 }
870}
871
872
873/**
874 * @interface_method_impl{PDMDRVREG,pfnPowerOff}
875 */
876/*static*/ DECLCALLBACK(void) AudioVideoRec::drvPowerOff(PPDMDRVINS pDrvIns)
877{
878 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
879 LogFlowFuncEnter();
880 avRecSinkShutdown(&pThis->Sink);
881}
882
883
884/**
885 * @interface_method_impl{PDMDRVREG,pfnDestruct}
886 */
887/*static*/ DECLCALLBACK(void) AudioVideoRec::drvDestruct(PPDMDRVINS pDrvIns)
888{
889 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
890 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
891
892 LogFlowFuncEnter();
893
894 switch (pThis->ContainerParms.enmType)
895 {
896 case AVRECCONTAINERTYPE_WEBM:
897 {
898 avRecSinkShutdown(&pThis->Sink);
899 RTStrFree(pThis->ContainerParms.WebM.pszFile);
900 break;
901 }
902
903 default:
904 break;
905 }
906
907 /*
908 * If the AudioVideoRec object is still alive, we must clear it's reference to
909 * us since we'll be invalid when we return from this method.
910 */
911 if (pThis->pAudioVideoRec)
912 {
913 pThis->pAudioVideoRec->mpDrv = NULL;
914 pThis->pAudioVideoRec = NULL;
915 }
916
917 LogFlowFuncLeave();
918}
919
920
921/**
922 * Initializes a recording sink.
923 *
924 * @returns VBox status code.
925 * @param pThis Driver instance.
926 * @param pSink Sink to initialize.
927 * @param pConParms Container parameters to set.
928 * @param pCodecParms Codec parameters to set.
929 */
930static int avRecSinkInit(PDRVAUDIORECORDING pThis, PAVRECSINK pSink, PAVRECCONTAINERPARMS pConParms, PAVRECCODECPARMS pCodecParms)
931{
932 uint32_t uHz = PDMAudioPropsHz(&pCodecParms->PCMProps);
933 uint8_t const cbSample = PDMAudioPropsSampleSize(&pCodecParms->PCMProps);
934 uint8_t cChannels = PDMAudioPropsChannels(&pCodecParms->PCMProps);
935 uint32_t uBitrate = pCodecParms->uBitrate;
936
937 /* Opus only supports certain input sample rates in an efficient manner.
938 * So make sure that we use those by resampling the data to the requested rate. */
939 if (uHz > 24000) uHz = AVREC_OPUS_HZ_MAX;
940 else if (uHz > 16000) uHz = 24000;
941 else if (uHz > 12000) uHz = 16000;
942 else if (uHz > 8000 ) uHz = 12000;
943 else uHz = 8000;
944
945 if (cChannels > 2)
946 {
947 LogRel(("Recording: Warning: More than 2 (stereo) channels are not supported at the moment\n"));
948 cChannels = 2;
949 }
950
951 int orc;
952 OpusEncoder *pEnc = opus_encoder_create(uHz, cChannels, OPUS_APPLICATION_AUDIO, &orc);
953 if (orc != OPUS_OK)
954 {
955 LogRel(("Recording: Audio codec failed to initialize: %s\n", opus_strerror(orc)));
956 return VERR_AUDIO_BACKEND_INIT_FAILED;
957 }
958
959 AssertPtr(pEnc);
960
961 if (uBitrate) /* Only explicitly set the bitrate if we specified one. Otherwise let Opus decide. */
962 {
963 opus_encoder_ctl(pEnc, OPUS_SET_BITRATE(uBitrate));
964 if (orc != OPUS_OK)
965 {
966 opus_encoder_destroy(pEnc);
967 pEnc = NULL;
968
969 LogRel(("Recording: Audio codec failed to set bitrate (%RU32): %s\n", uBitrate, opus_strerror(orc)));
970 return VERR_AUDIO_BACKEND_INIT_FAILED;
971 }
972 }
973
974 const bool fUseVBR = true; /** Use Variable Bit Rate (VBR) by default. @todo Make this configurable? */
975
976 orc = opus_encoder_ctl(pEnc, OPUS_SET_VBR(fUseVBR ? 1 : 0));
977 if (orc != OPUS_OK)
978 {
979 opus_encoder_destroy(pEnc);
980 pEnc = NULL;
981
982 LogRel(("Recording: Audio codec failed to %s VBR mode: %s\n", fUseVBR ? "enable" : "disable", opus_strerror(orc)));
983 return VERR_AUDIO_BACKEND_INIT_FAILED;
984 }
985
986 int rc = VINF_SUCCESS;
987
988 try
989 {
990 switch (pConParms->enmType)
991 {
992 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
993 {
994 if (pThis->pConsole)
995 {
996 pSink->Con.Main.pConsole = pThis->pConsole;
997 }
998 else
999 rc = VERR_NOT_SUPPORTED;
1000 break;
1001 }
1002
1003 case AVRECCONTAINERTYPE_WEBM:
1004 {
1005 /* If we only record audio, create our own WebM writer instance here. */
1006 if (!pSink->Con.WebM.pWebM) /* Do we already have our WebM writer instance? */
1007 {
1008 /** @todo Add sink name / number to file name. */
1009 const char *pszFile = pSink->Con.Parms.WebM.pszFile;
1010 AssertPtr(pszFile);
1011
1012 pSink->Con.WebM.pWebM = new WebMWriter();
1013 rc = pSink->Con.WebM.pWebM->Open(pszFile,
1014 /** @todo Add option to add some suffix if file exists instead of overwriting? */
1015 RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE,
1016 WebMWriter::AudioCodec_Opus, WebMWriter::VideoCodec_None);
1017 if (RT_SUCCESS(rc))
1018 {
1019 rc = pSink->Con.WebM.pWebM->AddAudioTrack(uHz, cChannels, cbSample * 8 /* Bits */,
1020 &pSink->Con.WebM.uTrack);
1021 if (RT_SUCCESS(rc))
1022 {
1023 LogRel(("Recording: Recording audio to audio file '%s'\n", pszFile));
1024 }
1025 else
1026 LogRel(("Recording: Error creating audio track for audio file '%s' (%Rrc)\n", pszFile, rc));
1027 }
1028 else
1029 LogRel(("Recording: Error creating audio file '%s' (%Rrc)\n", pszFile, rc));
1030 }
1031 break;
1032 }
1033
1034 default:
1035 rc = VERR_NOT_SUPPORTED;
1036 break;
1037 }
1038 }
1039 catch (std::bad_alloc &)
1040 {
1041 rc = VERR_NO_MEMORY;
1042 }
1043
1044 if (RT_SUCCESS(rc))
1045 {
1046 pSink->Con.Parms.enmType = pConParms->enmType;
1047
1048 PAVRECCODEC pCodec = &pSink->Codec;
1049
1050 PDMAudioPropsInit(&pCodec->Parms.PCMProps, cbSample, pCodecParms->PCMProps.fSigned, cChannels, uHz);
1051 pCodec->Parms.uBitrate = uBitrate;
1052
1053 pCodec->Opus.pEnc = pEnc;
1054 pCodec->Opus.msFrame = AVREC_OPUS_FRAME_MS_DEFAULT;
1055
1056 if (!pCodec->Opus.msFrame)
1057 pCodec->Opus.msFrame = AVREC_OPUS_FRAME_MS_DEFAULT; /* 20ms by default; to prevent division by zero. */
1058 pCodec->Opus.csFrame = pSink->Codec.Parms.PCMProps.uHz / (1000 /* s in ms */ / pSink->Codec.Opus.msFrame);
1059 pCodec->Opus.cbFrame = PDMAudioPropsFramesToBytes(&pSink->Codec.Parms.PCMProps, pCodec->Opus.csFrame);
1060
1061#ifdef VBOX_WITH_STATISTICS
1062 pSink->Codec.Stats.cEncFrames = 0;
1063 pSink->Codec.Stats.msEncTotal = 0;
1064#endif
1065 pSink->tsStartMs = RTTimeMilliTS();
1066 }
1067 else
1068 {
1069 if (pEnc)
1070 {
1071 opus_encoder_destroy(pEnc);
1072 pEnc = NULL;
1073 }
1074
1075 LogRel(("Recording: Error creating sink (%Rrc)\n", rc));
1076 }
1077
1078 return rc;
1079}
1080
1081
1082/**
1083 * Construct a audio video recording driver instance.
1084 *
1085 * @copydoc FNPDMDRVCONSTRUCT
1086 */
1087/*static*/ DECLCALLBACK(int) AudioVideoRec::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1088{
1089 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1090 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
1091 RT_NOREF(fFlags);
1092
1093 LogRel(("Audio: Initializing video recording audio driver\n"));
1094 LogFlowFunc(("fFlags=0x%x\n", fFlags));
1095
1096 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1097 ("Configuration error: Not possible to attach anything to this driver!\n"),
1098 VERR_PDM_DRVINS_NO_ATTACH);
1099
1100 /*
1101 * Init the static parts.
1102 */
1103 pThis->pDrvIns = pDrvIns;
1104 /* IBase */
1105 pDrvIns->IBase.pfnQueryInterface = drvAudioVideoRecQueryInterface;
1106 /* IHostAudio */
1107 pThis->IHostAudio.pfnGetConfig = drvAudioVideoRecHA_GetConfig;
1108 pThis->IHostAudio.pfnGetDevices = NULL;
1109 pThis->IHostAudio.pfnSetDevice = NULL;
1110 pThis->IHostAudio.pfnGetStatus = drvAudioVideoRecHA_GetStatus;
1111 pThis->IHostAudio.pfnDoOnWorkerThread = NULL;
1112 pThis->IHostAudio.pfnStreamConfigHint = NULL;
1113 pThis->IHostAudio.pfnStreamCreate = drvAudioVideoRecHA_StreamCreate;
1114 pThis->IHostAudio.pfnStreamInitAsync = NULL;
1115 pThis->IHostAudio.pfnStreamDestroy = drvAudioVideoRecHA_StreamDestroy;
1116 pThis->IHostAudio.pfnStreamNotifyDeviceChanged = NULL;
1117 pThis->IHostAudio.pfnStreamEnable = drvAudioVideoRecHA_StreamEnable;
1118 pThis->IHostAudio.pfnStreamDisable = drvAudioVideoRecHA_StreamDisable;
1119 pThis->IHostAudio.pfnStreamPause = drvAudioVideoRecHA_StreamPause;
1120 pThis->IHostAudio.pfnStreamResume = drvAudioVideoRecHA_StreamResume;
1121 pThis->IHostAudio.pfnStreamDrain = drvAudioVideoRecHA_StreamDrain;
1122 pThis->IHostAudio.pfnStreamGetState = drvAudioVideoRecHA_StreamGetState;
1123 pThis->IHostAudio.pfnStreamGetPending = NULL;
1124 pThis->IHostAudio.pfnStreamGetWritable = drvAudioVideoRecHA_StreamGetWritable;
1125 pThis->IHostAudio.pfnStreamPlay = drvAudioVideoRecHA_StreamPlay;
1126 pThis->IHostAudio.pfnStreamGetReadable = drvAudioVideoRecHA_StreamGetReadable;
1127 pThis->IHostAudio.pfnStreamCapture = drvAudioVideoRecHA_StreamCapture;
1128
1129 /*
1130 * Get the Console object pointer.
1131 */
1132 void *pvUser;
1133 int rc = CFGMR3QueryPtr(pCfg, "ObjectConsole", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1134 AssertRCReturn(rc, rc);
1135
1136 /* CFGM tree saves the pointer to Console in the Object node of AudioVideoRec. */
1137 pThis->pConsole = (Console *)pvUser;
1138 AssertReturn(!pThis->pConsole.isNull(), VERR_INVALID_POINTER);
1139
1140 /*
1141 * Get the pointer to the audio driver instance.
1142 */
1143 rc = CFGMR3QueryPtr(pCfg, "Object", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1144 AssertRCReturn(rc, rc);
1145
1146 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1147 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1148
1149 /*
1150 * Get the recording container and codec parameters from the audio driver instance.
1151 */
1152 PAVRECCONTAINERPARMS pConParams = &pThis->ContainerParms;
1153 PAVRECCODECPARMS pCodecParms = &pThis->CodecParms;
1154
1155 RT_ZERO(pThis->ContainerParms);
1156 RT_ZERO(pThis->CodecParms);
1157
1158 rc = CFGMR3QueryU32(pCfg, "ContainerType", (uint32_t *)&pConParams->enmType);
1159 AssertRCReturn(rc, rc);
1160
1161 switch (pConParams->enmType)
1162 {
1163 case AVRECCONTAINERTYPE_WEBM:
1164 rc = CFGMR3QueryStringAlloc(pCfg, "ContainerFileName", &pConParams->WebM.pszFile);
1165 AssertRCReturn(rc, rc);
1166 break;
1167
1168 default:
1169 break;
1170 }
1171
1172 uint32_t uHz = 0;
1173 rc = CFGMR3QueryU32(pCfg, "CodecHz", &uHz);
1174 AssertRCReturn(rc, rc);
1175
1176 uint8_t cSampleBits = 0;
1177 rc = CFGMR3QueryU8(pCfg, "CodecBits", &cSampleBits); /** @todo CodecBits != CodecBytes */
1178 AssertRCReturn(rc, rc);
1179
1180 uint8_t cChannels = 0;
1181 rc = CFGMR3QueryU8(pCfg, "CodecChannels", &cChannels);
1182 AssertRCReturn(rc, rc);
1183
1184 PDMAudioPropsInit(&pCodecParms->PCMProps, cSampleBits / 8, true /*fSigned*/, cChannels, uHz);
1185 AssertMsgReturn(PDMAudioPropsAreValid(&pCodecParms->PCMProps),
1186 ("Configuration error: Audio configuration is invalid!\n"), VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES); /** @todo wrong status code. */
1187
1188 rc = CFGMR3QueryU32(pCfg, "CodecBitrate", &pCodecParms->uBitrate);
1189 AssertRCReturn(rc, rc);
1190
1191 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1192 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1193
1194 pThis->pAudioVideoRec->mpDrv = pThis;
1195
1196 /*
1197 * Get the interface for the above driver (DrvAudio) to make mixer/conversion calls.
1198 * Described in CFGM tree.
1199 */
1200/** @todo r=bird: What on earth do you think you need this for?!? It's not an
1201 * interface lower drivers are supposed to be messing with! */
1202 pThis->pDrvAudio = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIAUDIOCONNECTOR);
1203 AssertMsgReturn(pThis->pDrvAudio, ("Configuration error: No upper interface specified!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
1204
1205#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH
1206 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.webm");
1207 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm");
1208#endif
1209
1210 /*
1211 * Init the recording sink.
1212 */
1213 LogRel(("Recording: Audio driver is using %RU32Hz, %RU16bit, %RU8 channel%s\n",
1214 PDMAudioPropsHz(&pThis->CodecParms.PCMProps), PDMAudioPropsSampleBits(&pThis->CodecParms.PCMProps),
1215 PDMAudioPropsChannels(&pThis->CodecParms.PCMProps), PDMAudioPropsChannels(&pThis->CodecParms.PCMProps) == 1 ? "" : "s"));
1216
1217 rc = avRecSinkInit(pThis, &pThis->Sink, &pThis->ContainerParms, &pThis->CodecParms);
1218 if (RT_SUCCESS(rc))
1219 LogRel2(("Recording: Audio recording driver initialized\n"));
1220 else
1221 LogRel(("Recording: Audio recording driver initialization failed: %Rrc\n", rc));
1222
1223 return rc;
1224}
1225
1226
1227/**
1228 * Video recording audio driver registration record.
1229 */
1230const PDMDRVREG AudioVideoRec::DrvReg =
1231{
1232 PDM_DRVREG_VERSION,
1233 /* szName */
1234 "AudioVideoRec",
1235 /* szRCMod */
1236 "",
1237 /* szR0Mod */
1238 "",
1239 /* pszDescription */
1240 "Audio driver for video recording",
1241 /* fFlags */
1242 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1243 /* fClass. */
1244 PDM_DRVREG_CLASS_AUDIO,
1245 /* cMaxInstances */
1246 ~0U,
1247 /* cbInstance */
1248 sizeof(DRVAUDIORECORDING),
1249 /* pfnConstruct */
1250 AudioVideoRec::drvConstruct,
1251 /* pfnDestruct */
1252 AudioVideoRec::drvDestruct,
1253 /* pfnRelocate */
1254 NULL,
1255 /* pfnIOCtl */
1256 NULL,
1257 /* pfnPowerOn */
1258 NULL,
1259 /* pfnReset */
1260 NULL,
1261 /* pfnSuspend */
1262 NULL,
1263 /* pfnResume */
1264 NULL,
1265 /* pfnAttach */
1266 NULL,
1267 /* pfnDetach */
1268 NULL,
1269 /* pfnPowerOff */
1270 AudioVideoRec::drvPowerOff,
1271 /* pfnSoftReset */
1272 NULL,
1273 /* u32EndVersion */
1274 PDM_DRVREG_VERSION
1275};
1276
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use