VirtualBox

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

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

Audio: Changed PDMIHOSTAUDIO::pfnStreamGetStatus into pfnStreamGetState and defined a simpler state enum (PDMHOSTAUDIOSTREAMSTATE) that fits what DrvAudio needs and the backends actually want to tell us. Fixes one VRDE issue. bugref:9890

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