VirtualBox

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

Last change on this file since 83640 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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

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