VirtualBox

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

Last change on this file since 77863 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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