VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/DrvAudioVideoRec.cpp@ 74962

Last change on this file since 74962 was 74955, checked in by vboxsync, 6 years ago

VideoRec/Main: Implemented applying audio driver configuration at runtime (when being attached).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.8 KB
Line 
1/* $Id: DrvAudioVideoRec.cpp 74955 2018-10-19 18:14:51Z vboxsync $ */
2/** @file
3 * Video recording audio backend for Main.
4 */
5
6/*
7 * Copyright (C) 2016-2018 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 "DrvAudioVideoRec.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
113
114/*********************************************************************************************************************************
115* Structures and Typedefs *
116*********************************************************************************************************************************/
117
118/**
119 * Enumeration for specifying the recording container type.
120 */
121typedef enum AVRECCONTAINERTYPE
122{
123 /** Unknown / invalid container type. */
124 AVRECCONTAINERTYPE_UNKNOWN = 0,
125 /** Recorded data goes to Main / Console. */
126 AVRECCONTAINERTYPE_MAIN_CONSOLE = 1,
127 /** Recorded data will be written to a .webm file. */
128 AVRECCONTAINERTYPE_WEBM = 2
129} AVRECCONTAINERTYPE;
130
131/**
132 * Structure for keeping generic container parameters.
133 */
134typedef struct AVRECCONTAINERPARMS
135{
136 /** The container's type. */
137 AVRECCONTAINERTYPE enmType;
138 union
139 {
140 /** WebM file specifics. */
141 struct
142 {
143 /** Allocated file name to write .webm file to. Must be free'd. */
144 char *pszFile;
145 } WebM;
146 };
147
148} AVRECCONTAINERPARMS, *PAVRECCONTAINERPARMS;
149
150/**
151 * Structure for keeping container-specific data.
152 */
153typedef struct AVRECCONTAINER
154{
155 /** Generic container parameters. */
156 AVRECCONTAINERPARMS Parms;
157
158 union
159 {
160 struct
161 {
162 /** Pointer to Console. */
163 Console *pConsole;
164 } Main;
165
166 struct
167 {
168 /** Pointer to WebM container to write recorded audio data to.
169 * See the AVRECMODE enumeration for more information. */
170 WebMWriter *pWebM;
171 /** Assigned track number from WebM container. */
172 uint8_t uTrack;
173 } WebM;
174 };
175} AVRECCONTAINER, *PAVRECCONTAINER;
176
177/**
178 * Structure for keeping generic codec parameters.
179 */
180typedef struct AVRECCODECPARMS
181{
182 /** The codec's used PCM properties. */
183 PDMAUDIOPCMPROPS PCMProps;
184 /** The codec's bitrate. 0 if not used / cannot be specified. */
185 uint32_t uBitrate;
186
187} AVRECCODECPARMS, *PAVRECCODECPARMS;
188
189/**
190 * Structure for keeping codec-specific data.
191 */
192typedef struct AVRECCODEC
193{
194 /** Generic codec parameters. */
195 AVRECCODECPARMS Parms;
196 union
197 {
198#ifdef VBOX_WITH_LIBOPUS
199 struct
200 {
201 /** Encoder we're going to use. */
202 OpusEncoder *pEnc;
203 /** Time (in ms) an (encoded) frame takes.
204 *
205 * For Opus, valid frame sizes are:
206 * ms Frame size
207 * 2.5 120
208 * 5 240
209 * 10 480
210 * 20 (Default) 960
211 * 40 1920
212 * 60 2880
213 */
214 uint32_t msFrame;
215 } Opus;
216#endif /* VBOX_WITH_LIBOPUS */
217 };
218
219#ifdef VBOX_WITH_STATISTICS /** @todo Make these real STAM values. */
220 struct
221 {
222 /** Number of frames encoded. */
223 uint64_t cEncFrames;
224 /** Total time (in ms) of already encoded audio data. */
225 uint64_t msEncTotal;
226 } STAM;
227#endif /* VBOX_WITH_STATISTICS */
228
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 /** The stream's acquired configuration. */
249 PPDMAUDIOSTREAMCFG pCfg;
250 /** (Audio) frame buffer. */
251 PRTCIRCBUF pCircBuf;
252 /** Pointer to sink to use for writing. */
253 PAVRECSINK pSink;
254 /** Last encoded PTS (in ms). */
255 uint64_t uLastPTSMs;
256} AVRECSTREAM, *PAVRECSTREAM;
257
258/**
259 * Video recording audio driver instance data.
260 */
261typedef struct DRVAUDIOVIDEOREC
262{
263 /** Pointer to audio video recording object. */
264 AudioVideoRec *pAudioVideoRec;
265 /** Pointer to the driver instance structure. */
266 PPDMDRVINS pDrvIns;
267 /** Pointer to host audio interface. */
268 PDMIHOSTAUDIO IHostAudio;
269 /** Pointer to the console object. */
270 ComPtr<Console> pConsole;
271 /** Pointer to the DrvAudio port interface that is above us. */
272 PPDMIAUDIOCONNECTOR pDrvAudio;
273 /** The driver's configured container parameters. */
274 AVRECCONTAINERPARMS ContainerParms;
275 /** The driver's configured codec parameters. */
276 AVRECCODECPARMS CodecParms;
277 /** The driver's sink for writing output to. */
278 AVRECSINK Sink;
279} DRVAUDIOVIDEOREC, *PDRVAUDIOVIDEOREC;
280
281/** Makes DRVAUDIOVIDEOREC out of PDMIHOSTAUDIO. */
282#define PDMIHOSTAUDIO_2_DRVAUDIOVIDEOREC(pInterface) /* (clang doesn't think it is a POD, thus _DYN.) */ \
283 ( (PDRVAUDIOVIDEOREC)((uintptr_t)pInterface - RT_UOFFSETOF_DYN(DRVAUDIOVIDEOREC, IHostAudio)) )
284
285/**
286 * Initializes a recording sink.
287 *
288 * @returns IPRT status code.
289 * @param pThis Driver instance.
290 * @param pSink Sink to initialize.
291 * @param pConParms Container parameters to set.
292 * @param pCodecParms Codec parameters to set.
293 */
294static int avRecSinkInit(PDRVAUDIOVIDEOREC pThis, PAVRECSINK pSink, PAVRECCONTAINERPARMS pConParms, PAVRECCODECPARMS pCodecParms)
295{
296 uint32_t uHz = pCodecParms->PCMProps.uHz;
297 uint8_t cBytes = pCodecParms->PCMProps.cBytes;
298 uint8_t cChannels = pCodecParms->PCMProps.cChannels;
299 uint32_t uBitrate = pCodecParms->uBitrate;
300
301 /* Opus only supports certain input sample rates in an efficient manner.
302 * So make sure that we use those by resampling the data to the requested rate. */
303 if (uHz > 24000) uHz = AVREC_OPUS_HZ_MAX;
304 else if (uHz > 16000) uHz = 24000;
305 else if (uHz > 12000) uHz = 16000;
306 else if (uHz > 8000 ) uHz = 12000;
307 else uHz = 8000;
308
309 if (cChannels > 2)
310 {
311 LogRel(("VideoRec: Warning: More than 2 (stereo) channels are not supported at the moment\n"));
312 cChannels = 2;
313 }
314
315 int orc;
316 OpusEncoder *pEnc = opus_encoder_create(uHz, cChannels, OPUS_APPLICATION_AUDIO, &orc);
317 if (orc != OPUS_OK)
318 {
319 LogRel(("VideoRec: Audio codec failed to initialize: %s\n", opus_strerror(orc)));
320 return VERR_AUDIO_BACKEND_INIT_FAILED;
321 }
322
323 AssertPtr(pEnc);
324
325 if (uBitrate) /* Only explicitly set the bitrate if we specified one. Otherwise let Opus decide. */
326 {
327 opus_encoder_ctl(pEnc, OPUS_SET_BITRATE(uBitrate));
328 if (orc != OPUS_OK)
329 {
330 opus_encoder_destroy(pEnc);
331 pEnc = NULL;
332
333 LogRel(("VideoRec: Audio codec failed to set bitrate (%RU32): %s\n", uBitrate, opus_strerror(orc)));
334 return VERR_AUDIO_BACKEND_INIT_FAILED;
335 }
336 }
337
338 const bool fUseVBR = true; /** Use Variable Bit Rate (VBR) by default. @todo Make this configurable? */
339
340 orc = opus_encoder_ctl(pEnc, OPUS_SET_VBR(fUseVBR ? 1 : 0));
341 if (orc != OPUS_OK)
342 {
343 opus_encoder_destroy(pEnc);
344 pEnc = NULL;
345
346 LogRel(("VideoRec: Audio codec failed to %s VBR mode: %s\n", fUseVBR ? "enable" : "disable", opus_strerror(orc)));
347 return VERR_AUDIO_BACKEND_INIT_FAILED;
348 }
349
350 int rc = VINF_SUCCESS;
351
352 try
353 {
354 switch (pConParms->enmType)
355 {
356 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
357 {
358 if (pThis->pConsole)
359 {
360 pSink->Con.Main.pConsole = pThis->pConsole;
361 }
362 else
363 rc = VERR_NOT_SUPPORTED;
364 break;
365 }
366
367 case AVRECCONTAINERTYPE_WEBM:
368 {
369 /* If we only record audio, create our own WebM writer instance here. */
370 if (!pSink->Con.WebM.pWebM) /* Do we already have our WebM writer instance? */
371 {
372 /** @todo Add sink name / number to file name. */
373 const char *pszFile = pSink->Con.Parms.WebM.pszFile;
374 AssertPtr(pszFile);
375
376 pSink->Con.WebM.pWebM = new WebMWriter();
377 rc = pSink->Con.WebM.pWebM->Open(pszFile,
378 /** @todo Add option to add some suffix if file exists instead of overwriting? */
379 RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE,
380 WebMWriter::AudioCodec_Opus, WebMWriter::VideoCodec_None);
381 if (RT_SUCCESS(rc))
382 {
383 rc = pSink->Con.WebM.pWebM->AddAudioTrack(uHz, cChannels, cBytes * 8 /* Bits */,
384 &pSink->Con.WebM.uTrack);
385 if (RT_SUCCESS(rc))
386 {
387 LogRel(("VideoRec: Recording audio to audio file '%s'\n", pszFile));
388 }
389 else
390 LogRel(("VideoRec: Error creating audio track for audio file '%s' (%Rrc)\n", pszFile, rc));
391 }
392 else
393 LogRel(("VideoRec: Error creating audio file '%s' (%Rrc)\n", pszFile, rc));
394 }
395 break;
396 }
397
398 default:
399 rc = VERR_NOT_SUPPORTED;
400 break;
401 }
402 }
403 catch (std::bad_alloc &)
404 {
405 rc = VERR_NO_MEMORY;
406 }
407
408 if (RT_SUCCESS(rc))
409 {
410 pSink->Con.Parms.enmType = pConParms->enmType;
411
412 pSink->Codec.Parms.PCMProps.uHz = uHz;
413 pSink->Codec.Parms.PCMProps.cChannels = cChannels;
414 pSink->Codec.Parms.PCMProps.cBytes = cBytes;
415 pSink->Codec.Parms.PCMProps.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pSink->Codec.Parms.PCMProps.cBytes,
416 pSink->Codec.Parms.PCMProps.cChannels);
417 pSink->Codec.Parms.uBitrate = uBitrate;
418
419 pSink->Codec.Opus.pEnc = pEnc;
420 pSink->Codec.Opus.msFrame = 20; /** @todo 20 ms of audio data. Make this configurable? */
421
422#ifdef VBOX_WITH_STATISTICS
423 pSink->Codec.STAM.cEncFrames = 0;
424 pSink->Codec.STAM.msEncTotal = 0;
425#endif
426
427 pSink->tsStartMs = RTTimeMilliTS();
428 }
429 else
430 {
431 if (pEnc)
432 {
433 opus_encoder_destroy(pEnc);
434 pEnc = NULL;
435 }
436
437 LogRel(("VideoRec: Error creating sink (%Rrc)\n", rc));
438 }
439
440 return rc;
441}
442
443
444/**
445 * Shuts down (closes) a recording sink,
446 *
447 * @returns IPRT status code.
448 * @param pSink Recording sink to shut down.
449 */
450static void avRecSinkShutdown(PAVRECSINK pSink)
451{
452 AssertPtrReturnVoid(pSink);
453
454#ifdef VBOX_WITH_LIBOPUS
455 if (pSink->Codec.Opus.pEnc)
456 {
457 opus_encoder_destroy(pSink->Codec.Opus.pEnc);
458 pSink->Codec.Opus.pEnc = NULL;
459 }
460#endif
461 switch (pSink->Con.Parms.enmType)
462 {
463 case AVRECCONTAINERTYPE_WEBM:
464 {
465 if (pSink->Con.WebM.pWebM)
466 {
467 LogRel2(("VideoRec: Finished recording audio to file '%s' (%zu bytes)\n",
468 pSink->Con.WebM.pWebM->GetFileName().c_str(), pSink->Con.WebM.pWebM->GetFileSize()));
469
470 int rc2 = pSink->Con.WebM.pWebM->Close();
471 AssertRC(rc2);
472
473 delete pSink->Con.WebM.pWebM;
474 pSink->Con.WebM.pWebM = NULL;
475 }
476 break;
477 }
478
479 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
480 default:
481 break;
482 }
483}
484
485
486/**
487 * Creates an audio output stream and associates it with the specified recording sink.
488 *
489 * @returns IPRT status code.
490 * @param pThis Driver instance.
491 * @param pStreamAV Audio output stream to create.
492 * @param pSink Recording sink to associate audio output stream to.
493 * @param pCfgReq Requested configuration by the audio backend.
494 * @param pCfgAcq Acquired configuration by the audio output stream.
495 */
496static int avRecCreateStreamOut(PDRVAUDIOVIDEOREC pThis, PAVRECSTREAM pStreamAV,
497 PAVRECSINK pSink, PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
498{
499 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
500 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
501 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
502 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
503 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
504
505 if (pCfgReq->DestSource.Dest != PDMAUDIOPLAYBACKDEST_FRONT)
506 {
507 AssertFailed();
508
509 LogRel2(("VideoRec: Support for surround audio not implemented yet\n"));
510 return VERR_NOT_SUPPORTED;
511 }
512
513 int rc = VINF_SUCCESS;
514
515#ifdef VBOX_WITH_LIBOPUS
516 const unsigned cFrames = 2; /** @todo Use the PreRoll param for that? */
517
518 const uint32_t csFrame = pSink->Codec.Parms.PCMProps.uHz / (1000 /* s in ms */ / pSink->Codec.Opus.msFrame);
519 const uint32_t cbFrame = DrvAudioHlpFramesToBytes(csFrame, &pSink->Codec.Parms.PCMProps);
520
521 rc = RTCircBufCreate(&pStreamAV->pCircBuf, cbFrame * cFrames);
522 if (RT_SUCCESS(rc))
523 {
524 pStreamAV->pSink = pSink; /* Assign sink to stream. */
525 pStreamAV->uLastPTSMs = 0;
526
527 if (pCfgAcq)
528 {
529 /* Make sure to let the driver backend know that we need the audio data in
530 * a specific sampling rate Opus is optimized for. */
531 pCfgAcq->Props.uHz = pSink->Codec.Parms.PCMProps.uHz;
532 pCfgAcq->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfgAcq->Props.cBytes, pCfgAcq->Props.cChannels);
533
534 /* Every Opus frame marks a period for now. Optimize this later. */
535 pCfgAcq->Backend.cfPeriod = DrvAudioHlpMilliToFrames(pSink->Codec.Opus.msFrame, &pCfgAcq->Props);
536 pCfgAcq->Backend.cfBufferSize = DrvAudioHlpMilliToFrames(100 /* ms */, &pCfgAcq->Props); /** @todo Make this configurable. */
537 pCfgAcq->Backend.cfPreBuf = pCfgAcq->Backend.cfPeriod * 2;
538 }
539 }
540#else
541 RT_NOREF(pThis, pSink, pStreamAV, pCfgReq, pCfgAcq);
542 rc = VERR_NOT_SUPPORTED;
543#endif /* VBOX_WITH_LIBOPUS */
544
545 LogFlowFuncLeaveRC(rc);
546 return rc;
547}
548
549
550/**
551 * Destroys (closes) an audio output stream.
552 *
553 * @returns IPRT status code.
554 * @param pThis Driver instance.
555 * @param pStreamAV Audio output stream to destroy.
556 */
557static int avRecDestroyStreamOut(PDRVAUDIOVIDEOREC pThis, PAVRECSTREAM pStreamAV)
558{
559 RT_NOREF(pThis);
560
561 if (pStreamAV->pCircBuf)
562 {
563 RTCircBufDestroy(pStreamAV->pCircBuf);
564 pStreamAV->pCircBuf = NULL;
565 }
566
567 return VINF_SUCCESS;
568}
569
570
571/**
572 * Controls an audio output stream
573 *
574 * @returns IPRT status code.
575 * @param pThis Driver instance.
576 * @param pStreamAV Audio output stream to control.
577 * @param enmStreamCmd Stream command to issue.
578 */
579static int avRecControlStreamOut(PDRVAUDIOVIDEOREC pThis,
580 PAVRECSTREAM pStreamAV, PDMAUDIOSTREAMCMD enmStreamCmd)
581{
582 RT_NOREF(pThis, pStreamAV);
583
584 int rc = VINF_SUCCESS;
585
586 switch (enmStreamCmd)
587 {
588 case PDMAUDIOSTREAMCMD_ENABLE:
589 case PDMAUDIOSTREAMCMD_DISABLE:
590 case PDMAUDIOSTREAMCMD_RESUME:
591 case PDMAUDIOSTREAMCMD_PAUSE:
592 break;
593
594 default:
595 rc = VERR_NOT_SUPPORTED;
596 break;
597 }
598
599 return rc;
600}
601
602
603/**
604 * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
605 */
606static DECLCALLBACK(int) drvAudioVideoRecInit(PPDMIHOSTAUDIO pInterface)
607{
608 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
609
610 LogFlowFuncEnter();
611
612 PDRVAUDIOVIDEOREC pThis = PDMIHOSTAUDIO_2_DRVAUDIOVIDEOREC(pInterface);
613
614 LogRel(("VideoRec: Audio driver is using %RU32Hz, %RU16bit, %RU8 %s\n",
615 pThis->CodecParms.PCMProps.uHz, pThis->CodecParms.PCMProps.cBytes * 8,
616 pThis->CodecParms.PCMProps.cChannels, pThis->CodecParms.PCMProps.cChannels == 1 ? "channel" : "channels"));
617
618 int rc = avRecSinkInit(pThis, &pThis->Sink, &pThis->ContainerParms, &pThis->CodecParms);
619 if (RT_FAILURE(rc))
620 {
621 LogRel(("VideoRec: Audio recording driver failed to initialize, rc=%Rrc\n", rc));
622 }
623 else
624 LogRel2(("VideoRec: Audio recording driver initialized\n"));
625
626 return rc;
627}
628
629
630/**
631 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
632 */
633static DECLCALLBACK(int) drvAudioVideoRecStreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
634 void *pvBuf, uint32_t cxBuf, uint32_t *pcxRead)
635{
636 RT_NOREF(pInterface, pStream, pvBuf, cxBuf);
637
638 if (pcxRead)
639 *pcxRead = 0;
640
641 return VINF_SUCCESS;
642}
643
644
645/**
646 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
647 */
648static DECLCALLBACK(int) drvAudioVideoRecStreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
649 const void *pvBuf, uint32_t cxBuf, uint32_t *pcxWritten)
650{
651 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
652 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
653 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
654 AssertReturn(cxBuf, VERR_INVALID_PARAMETER);
655 /* pcxWritten is optional. */
656
657 PDRVAUDIOVIDEOREC pThis = PDMIHOSTAUDIO_2_DRVAUDIOVIDEOREC(pInterface);
658 RT_NOREF(pThis);
659 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
660
661 int rc = VINF_SUCCESS;
662
663 uint32_t cbWrittenTotal = 0;
664
665 /*
666 * Call the encoder with the data.
667 */
668#ifdef VBOX_WITH_LIBOPUS
669 PAVRECSINK pSink = pStreamAV->pSink;
670 AssertPtr(pSink);
671 PRTCIRCBUF pCircBuf = pStreamAV->pCircBuf;
672 AssertPtr(pCircBuf);
673
674 void *pvCircBuf;
675 size_t cbCircBuf;
676
677 uint32_t cbToWrite = cxBuf;
678
679 /*
680 * Fetch as much as we can into our internal ring buffer.
681 */
682 while ( cbToWrite
683 && RTCircBufFree(pCircBuf))
684 {
685 RTCircBufAcquireWriteBlock(pCircBuf, cbToWrite, &pvCircBuf, &cbCircBuf);
686
687 if (cbCircBuf)
688 {
689 memcpy(pvCircBuf, (uint8_t *)pvBuf + cbWrittenTotal, cbCircBuf),
690 cbWrittenTotal += (uint32_t)cbCircBuf;
691 Assert(cbToWrite >= cbCircBuf);
692 cbToWrite -= (uint32_t)cbCircBuf;
693 }
694
695 RTCircBufReleaseWriteBlock(pCircBuf, cbCircBuf);
696
697 if ( RT_FAILURE(rc)
698 || !cbCircBuf)
699 {
700 break;
701 }
702 }
703
704 /*
705 * Process our internal ring buffer and encode the data.
706 */
707
708 uint8_t abSrc[_64K]; /** @todo Fix! */
709 size_t cbSrc;
710
711 const uint32_t csFrame = pSink->Codec.Parms.PCMProps.uHz / (1000 /* s in ms */ / pSink->Codec.Opus.msFrame);
712 const uint32_t cbFrame = DrvAudioHlpFramesToBytes(csFrame, &pSink->Codec.Parms.PCMProps);
713
714 /* Only encode data if we have data for the given time period (or more). */
715 while (RTCircBufUsed(pCircBuf) >= cbFrame)
716 {
717 cbSrc = 0;
718
719 while (cbSrc < cbFrame)
720 {
721 RTCircBufAcquireReadBlock(pCircBuf, cbFrame - cbSrc, &pvCircBuf, &cbCircBuf);
722
723 if (cbCircBuf)
724 {
725 memcpy(&abSrc[cbSrc], pvCircBuf, cbCircBuf);
726
727 cbSrc += cbCircBuf;
728 Assert(cbSrc <= sizeof(abSrc));
729 }
730
731 RTCircBufReleaseReadBlock(pCircBuf, cbCircBuf);
732
733 if (!cbCircBuf)
734 break;
735 }
736
737# ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
738 RTFILE fh;
739 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm",
740 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
741 RTFileWrite(fh, abSrc, cbSrc, NULL);
742 RTFileClose(fh);
743# endif
744
745 Assert(cbSrc == cbFrame);
746
747 /*
748 * Opus always encodes PER FRAME, that is, exactly 2.5, 5, 10, 20, 40 or 60 ms of audio data.
749 *
750 * A packet can have up to 120ms worth of audio data.
751 * Anything > 120ms of data will result in a "corrupted package" error message by
752 * by decoding application.
753 */
754 uint8_t abDst[_64K]; /** @todo Fix! */
755 size_t cbDst = sizeof(abDst);
756
757 /* Call the encoder to encode one frame per iteration. */
758 opus_int32 cbWritten = opus_encode(pSink->Codec.Opus.pEnc,
759 (opus_int16 *)abSrc, csFrame, abDst, (opus_int32)cbDst);
760 if (cbWritten > 0)
761 {
762 /* Get overall frames encoded. */
763 const uint32_t cEncFrames = opus_packet_get_nb_frames(abDst, cbWritten);
764
765# ifdef VBOX_WITH_STATISTICS
766 pSink->Codec.STAM.cEncFrames += cEncFrames;
767 pSink->Codec.STAM.msEncTotal += pSink->Codec.Opus.msFrame * cEncFrames;
768
769 LogFunc(("%RU64ms [%RU64 frames]: cbSrc=%zu, cbDst=%zu, cEncFrames=%RU32\n",
770 pSink->Codec.STAM.msEncTotal, pSink->Codec.STAM.cEncFrames, cbSrc, cbDst, cEncFrames));
771# endif
772 Assert((uint32_t)cbWritten <= cbDst);
773 cbDst = RT_MIN((uint32_t)cbWritten, cbDst); /* Update cbDst to actual bytes encoded (written). */
774
775 Assert(cEncFrames == 1); /* At the moment we encode exactly *one* frame per frame. */
776
777 if (pStreamAV->uLastPTSMs == 0)
778 pStreamAV->uLastPTSMs = RTTimeMilliTS() - pSink->tsStartMs;
779
780 const uint64_t uDurationMs = pSink->Codec.Opus.msFrame * cEncFrames;
781 const uint64_t uPTSMs = pStreamAV->uLastPTSMs + uDurationMs;
782
783 pStreamAV->uLastPTSMs += uDurationMs;
784
785 switch (pSink->Con.Parms.enmType)
786 {
787 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
788 {
789 HRESULT hr = pSink->Con.Main.pConsole->i_audioVideoRecSendAudio(abDst, cbDst, uPTSMs);
790 Assert(hr == S_OK);
791 RT_NOREF(hr);
792
793 break;
794 }
795
796 case AVRECCONTAINERTYPE_WEBM:
797 {
798 WebMWriter::BlockData_Opus blockData = { abDst, cbDst, uPTSMs };
799 rc = pSink->Con.WebM.pWebM->WriteBlock(pSink->Con.WebM.uTrack, &blockData, sizeof(blockData));
800 AssertRC(rc);
801
802 break;
803 }
804
805 default:
806 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
807 break;
808 }
809 }
810 else if (cbWritten < 0)
811 {
812 AssertMsgFailed(("Encoding failed: %s\n", opus_strerror(cbWritten)));
813 rc = VERR_INVALID_PARAMETER;
814 }
815
816 if (RT_FAILURE(rc))
817 break;
818 }
819
820 if (pcxWritten)
821 *pcxWritten = cbWrittenTotal;
822#else
823 /* Report back all data as being processed. */
824 if (pcxWritten)
825 *pcxWritten = cxBuf;
826
827 rc = VERR_NOT_SUPPORTED;
828#endif /* VBOX_WITH_LIBOPUS */
829
830 LogFlowFunc(("csReadTotal=%RU32, rc=%Rrc\n", cbWrittenTotal, rc));
831 return rc;
832}
833
834
835/**
836 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
837 */
838static DECLCALLBACK(int) drvAudioVideoRecGetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
839{
840 RT_NOREF(pInterface);
841 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
842
843 RTStrPrintf2(pBackendCfg->szName, sizeof(pBackendCfg->szName), "Video recording audio driver");
844
845 pBackendCfg->cbStreamOut = sizeof(AVRECSTREAM);
846 pBackendCfg->cbStreamIn = 0;
847 pBackendCfg->cMaxStreamsIn = 0;
848 pBackendCfg->cMaxStreamsOut = UINT32_MAX;
849
850 return VINF_SUCCESS;
851}
852
853
854/**
855 * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
856 */
857static DECLCALLBACK(void) drvAudioVideoRecShutdown(PPDMIHOSTAUDIO pInterface)
858{
859 LogFlowFuncEnter();
860
861 PDRVAUDIOVIDEOREC pThis = PDMIHOSTAUDIO_2_DRVAUDIOVIDEOREC(pInterface);
862
863 avRecSinkShutdown(&pThis->Sink);
864}
865
866
867/**
868 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
869 */
870static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvAudioVideoRecGetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
871{
872 RT_NOREF(enmDir);
873 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
874
875 return PDMAUDIOBACKENDSTS_RUNNING;
876}
877
878
879/**
880 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
881 */
882static DECLCALLBACK(int) drvAudioVideoRecStreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
883 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
884{
885 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
886 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
887 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
888
889 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
890 return VERR_NOT_SUPPORTED;
891
892 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
893
894 PDRVAUDIOVIDEOREC pThis = PDMIHOSTAUDIO_2_DRVAUDIOVIDEOREC(pInterface);
895 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
896
897 /* For now we only have one sink, namely the driver's one.
898 * Later each stream could have its own one, to e.g. router different stream to different sinks .*/
899 PAVRECSINK pSink = &pThis->Sink;
900
901 int rc = avRecCreateStreamOut(pThis, pStreamAV, pSink, pCfgReq, pCfgAcq);
902 if (RT_SUCCESS(rc))
903 {
904 pStreamAV->pCfg = DrvAudioHlpStreamCfgDup(pCfgAcq);
905 if (!pStreamAV->pCfg)
906 rc = VERR_NO_MEMORY;
907 }
908
909 return rc;
910}
911
912
913/**
914 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
915 */
916static DECLCALLBACK(int) drvAudioVideoRecStreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
917{
918 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
919 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
920
921 PDRVAUDIOVIDEOREC pThis = PDMIHOSTAUDIO_2_DRVAUDIOVIDEOREC(pInterface);
922 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
923
924 if (!pStreamAV->pCfg) /* Not (yet) configured? Skip. */
925 return VINF_SUCCESS;
926
927 int rc = VINF_SUCCESS;
928
929 if (pStreamAV->pCfg->enmDir == PDMAUDIODIR_OUT)
930 rc = avRecDestroyStreamOut(pThis, pStreamAV);
931
932 if (RT_SUCCESS(rc))
933 {
934 DrvAudioHlpStreamCfgFree(pStreamAV->pCfg);
935 pStreamAV->pCfg = NULL;
936 }
937
938 return rc;
939}
940
941
942/**
943 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
944 */
945static DECLCALLBACK(int) drvAudioVideoRecStreamControl(PPDMIHOSTAUDIO pInterface,
946 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
947{
948 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
949 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
950
951 PDRVAUDIOVIDEOREC pThis = PDMIHOSTAUDIO_2_DRVAUDIOVIDEOREC(pInterface);
952 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
953
954 if (!pStreamAV->pCfg) /* Not (yet) configured? Skip. */
955 return VINF_SUCCESS;
956
957 if (pStreamAV->pCfg->enmDir == PDMAUDIODIR_OUT)
958 return avRecControlStreamOut(pThis, pStreamAV, enmStreamCmd);
959
960 return VINF_SUCCESS;
961}
962
963
964/**
965 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
966 */
967static DECLCALLBACK(uint32_t) drvAudioVideoRecStreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
968{
969 RT_NOREF(pInterface, pStream);
970
971 return 0; /* Video capturing does not provide any input. */
972}
973
974
975/**
976 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
977 */
978static DECLCALLBACK(uint32_t) drvAudioVideoRecStreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
979{
980 RT_NOREF(pInterface, pStream);
981
982 return UINT32_MAX;
983}
984
985
986/**
987 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
988 */
989static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvAudioVideoRecStreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
990{
991 RT_NOREF(pInterface, pStream);
992
993 return (PDMAUDIOSTREAMSTS_FLAG_INITIALIZED | PDMAUDIOSTREAMSTS_FLAG_ENABLED);
994}
995
996
997/**
998 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
999 */
1000static DECLCALLBACK(int) drvAudioVideoRecStreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1001{
1002 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1003 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1004
1005 LogFlowFuncEnter();
1006
1007 /* Nothing to do here for video recording. */
1008 return VINF_SUCCESS;
1009}
1010
1011
1012/**
1013 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1014 */
1015static DECLCALLBACK(void *) drvAudioVideoRecQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1016{
1017 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1018 PDRVAUDIOVIDEOREC pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIOVIDEOREC);
1019
1020 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1021 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
1022 return NULL;
1023}
1024
1025
1026AudioVideoRec::AudioVideoRec(Console *pConsole)
1027 : AudioDriver(pConsole)
1028 , mpDrv(NULL)
1029{
1030}
1031
1032
1033AudioVideoRec::~AudioVideoRec(void)
1034{
1035 if (mpDrv)
1036 {
1037 mpDrv->pAudioVideoRec = NULL;
1038 mpDrv = NULL;
1039 }
1040}
1041
1042
1043/**
1044 * Applies a video recording configuration to this driver instance.
1045 *
1046 * @returns IPRT status code.
1047 * @param pVideoRecCfg Pointer to video recording configuration to apply.
1048 */
1049int AudioVideoRec::applyConfiguration(const PVIDEORECCFG pVideoRecCfg)
1050{
1051 /** @todo Do some validation here. */
1052 mVideoRecCfg = *pVideoRecCfg; /* Note: Does have an own copy operator. */
1053 return VINF_SUCCESS;
1054}
1055
1056
1057/**
1058 * @copydoc AudioDriver::configureDriver
1059 */
1060int AudioVideoRec::configureDriver(PCFGMNODE pLunCfg)
1061{
1062 int rc = CFGMR3InsertInteger(pLunCfg, "Object", (uintptr_t)mpConsole->i_getAudioVideoRec());
1063 AssertRCReturn(rc, rc);
1064 rc = CFGMR3InsertInteger(pLunCfg, "ObjectConsole", (uintptr_t)mpConsole);
1065 AssertRCReturn(rc, rc);
1066
1067 rc = CFGMR3InsertInteger(pLunCfg, "ContainerType", (uint64_t)mVideoRecCfg.enmDst);
1068 AssertRCReturn(rc, rc);
1069 if (mVideoRecCfg.enmDst == VIDEORECDEST_FILE)
1070 {
1071 rc = CFGMR3InsertString(pLunCfg, "ContainerFileName", Utf8Str(mVideoRecCfg.File.strName).c_str());
1072 AssertRCReturn(rc, rc);
1073 }
1074 rc = CFGMR3InsertInteger(pLunCfg, "CodecHz", mVideoRecCfg.Audio.uHz);
1075 AssertRCReturn(rc, rc);
1076 rc = CFGMR3InsertInteger(pLunCfg, "CodecBits", mVideoRecCfg.Audio.cBits);
1077 AssertRCReturn(rc, rc);
1078 rc = CFGMR3InsertInteger(pLunCfg, "CodecChannels", mVideoRecCfg.Audio.cChannels);
1079 AssertRCReturn(rc, rc);
1080 rc = CFGMR3InsertInteger(pLunCfg, "CodecBitrate", 0); /* Let Opus decide for now. */
1081 AssertRCReturn(rc, rc);
1082
1083 return rc;
1084}
1085
1086
1087/**
1088 * Construct a audio video recording driver instance.
1089 *
1090 * @copydoc FNPDMDRVCONSTRUCT
1091 */
1092/* static */
1093DECLCALLBACK(int) AudioVideoRec::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1094{
1095 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1096 PDRVAUDIOVIDEOREC pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIOVIDEOREC);
1097 RT_NOREF(fFlags);
1098
1099 LogRel(("Audio: Initializing video recording audio driver\n"));
1100 LogFlowFunc(("fFlags=0x%x\n", fFlags));
1101
1102 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1103 ("Configuration error: Not possible to attach anything to this driver!\n"),
1104 VERR_PDM_DRVINS_NO_ATTACH);
1105
1106 /*
1107 * Init the static parts.
1108 */
1109 pThis->pDrvIns = pDrvIns;
1110 /* IBase */
1111 pDrvIns->IBase.pfnQueryInterface = drvAudioVideoRecQueryInterface;
1112 /* IHostAudio */
1113 PDMAUDIO_IHOSTAUDIO_CALLBACKS(drvAudioVideoRec);
1114
1115 /*
1116 * Get the Console object pointer.
1117 */
1118 void *pvUser;
1119 int rc = CFGMR3QueryPtr(pCfg, "ObjectConsole", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1120 AssertRCReturn(rc, rc);
1121
1122 /* CFGM tree saves the pointer to Console in the Object node of AudioVideoRec. */
1123 pThis->pConsole = (Console *)pvUser;
1124 AssertReturn(!pThis->pConsole.isNull(), VERR_INVALID_POINTER);
1125
1126 /*
1127 * Get the pointer to the audio driver instance.
1128 */
1129 rc = CFGMR3QueryPtr(pCfg, "Object", &pvUser); /** @todo r=andy Get rid of this hack and use IHostAudio::SetCallback. */
1130 AssertRCReturn(rc, rc);
1131
1132 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1133 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1134
1135 /*
1136 * Get the recording container and codec parameters from the audio driver instance.
1137 */
1138 PAVRECCONTAINERPARMS pConParams = &pThis->ContainerParms;
1139 PAVRECCODECPARMS pCodecParms = &pThis->CodecParms;
1140 PPDMAUDIOPCMPROPS pPCMProps = &pCodecParms->PCMProps;
1141
1142 RT_ZERO(pThis->ContainerParms);
1143 RT_ZERO(pThis->CodecParms);
1144
1145 rc = CFGMR3QueryU32(pCfg, "ContainerType", (uint32_t *)&pConParams->enmType);
1146 AssertRCReturn(rc, rc);
1147
1148 switch (pConParams->enmType)
1149 {
1150 case AVRECCONTAINERTYPE_WEBM:
1151 rc = CFGMR3QueryStringAlloc(pCfg, "ContainerFileName", &pConParams->WebM.pszFile);
1152 AssertRCReturn(rc, rc);
1153 break;
1154
1155 default:
1156 break;
1157 }
1158
1159 rc = CFGMR3QueryU32(pCfg, "CodecHz", &pPCMProps->uHz);
1160 AssertRCReturn(rc, rc);
1161 rc = CFGMR3QueryU8(pCfg, "CodecBits", &pPCMProps->cBytes);
1162 AssertRCReturn(rc, rc);
1163 rc = CFGMR3QueryU8(pCfg, "CodecChannels", &pPCMProps->cChannels);
1164 AssertRCReturn(rc, rc);
1165 rc = CFGMR3QueryU32(pCfg, "CodecBitrate", &pCodecParms->uBitrate);
1166 AssertRCReturn(rc, rc);
1167
1168 pPCMProps->cBytes = pPCMProps->cBytes / 8; /* Bits to bytes. */
1169 pPCMProps->cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pPCMProps->cBytes, pPCMProps->cChannels);
1170 pPCMProps->fSigned = true;
1171 pPCMProps->fSwapEndian = false;
1172
1173 AssertMsgReturn(DrvAudioHlpPCMPropsAreValid(pPCMProps),
1174 ("Configuration error: Audio configuration is invalid!\n"), VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES);
1175
1176 pThis->pAudioVideoRec = (AudioVideoRec *)pvUser;
1177 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
1178
1179 pThis->pAudioVideoRec->mpDrv = pThis;
1180
1181 /*
1182 * Get the interface for the above driver (DrvAudio) to make mixer/conversion calls.
1183 * Described in CFGM tree.
1184 */
1185 pThis->pDrvAudio = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIAUDIOCONNECTOR);
1186 AssertMsgReturn(pThis->pDrvAudio, ("Configuration error: No upper interface specified!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
1187
1188#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
1189 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.webm");
1190 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "DrvAudioVideoRec.pcm");
1191#endif
1192
1193 return VINF_SUCCESS;
1194}
1195
1196
1197/**
1198 * @interface_method_impl{PDMDRVREG,pfnDestruct}
1199 */
1200/* static */
1201DECLCALLBACK(void) AudioVideoRec::drvDestruct(PPDMDRVINS pDrvIns)
1202{
1203 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1204 PDRVAUDIOVIDEOREC pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIOVIDEOREC);
1205
1206 LogFlowFuncEnter();
1207
1208 switch (pThis->ContainerParms.enmType)
1209 {
1210 case AVRECCONTAINERTYPE_WEBM:
1211 {
1212 avRecSinkShutdown(&pThis->Sink);
1213 RTStrFree(pThis->ContainerParms.WebM.pszFile);
1214 break;
1215 }
1216
1217 default:
1218 break;
1219 }
1220
1221 /*
1222 * If the AudioVideoRec object is still alive, we must clear it's reference to
1223 * us since we'll be invalid when we return from this method.
1224 */
1225 if (pThis->pAudioVideoRec)
1226 {
1227 pThis->pAudioVideoRec->mpDrv = NULL;
1228 pThis->pAudioVideoRec = NULL;
1229 }
1230
1231 LogFlowFuncLeave();
1232}
1233
1234
1235/**
1236 * @interface_method_impl{PDMDRVREG,pfnAttach}
1237 */
1238/* static */
1239DECLCALLBACK(int) AudioVideoRec::drvAttach(PPDMDRVINS pDrvIns, uint32_t fFlags)
1240{
1241 RT_NOREF(pDrvIns, fFlags);
1242
1243 LogFlowFuncEnter();
1244
1245 return VINF_SUCCESS;
1246}
1247
1248/**
1249 * @interface_method_impl{PDMDRVREG,pfnDetach}
1250 */
1251/* static */
1252DECLCALLBACK(void) AudioVideoRec::drvDetach(PPDMDRVINS pDrvIns, uint32_t fFlags)
1253{
1254 RT_NOREF(pDrvIns, fFlags);
1255
1256 LogFlowFuncEnter();
1257}
1258
1259/**
1260 * Video recording audio driver registration record.
1261 */
1262const PDMDRVREG AudioVideoRec::DrvReg =
1263{
1264 PDM_DRVREG_VERSION,
1265 /* szName */
1266 "AudioVideoRec",
1267 /* szRCMod */
1268 "",
1269 /* szR0Mod */
1270 "",
1271 /* pszDescription */
1272 "Audio driver for video recording",
1273 /* fFlags */
1274 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1275 /* fClass. */
1276 PDM_DRVREG_CLASS_AUDIO,
1277 /* cMaxInstances */
1278 ~0U,
1279 /* cbInstance */
1280 sizeof(DRVAUDIOVIDEOREC),
1281 /* pfnConstruct */
1282 AudioVideoRec::drvConstruct,
1283 /* pfnDestruct */
1284 AudioVideoRec::drvDestruct,
1285 /* pfnRelocate */
1286 NULL,
1287 /* pfnIOCtl */
1288 NULL,
1289 /* pfnPowerOn */
1290 NULL,
1291 /* pfnReset */
1292 NULL,
1293 /* pfnSuspend */
1294 NULL,
1295 /* pfnResume */
1296 NULL,
1297 /* pfnAttach */
1298 AudioVideoRec::drvAttach,
1299 /* pfnDetach */
1300 AudioVideoRec::drvDetach,
1301 /* pfnPowerOff */
1302 NULL,
1303 /* pfnSoftReset */
1304 NULL,
1305 /* u32EndVersion */
1306 PDM_DRVREG_VERSION
1307};
1308
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