VirtualBox

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

Last change on this file since 104635 was 104635, checked in by vboxsync, 6 months ago

Main: Fixed warnings. ​bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.4 KB
Line 
1/* $Id: DrvAudioRec.cpp 104635 2024-05-15 09:29:32Z vboxsync $ */
2/** @file
3 * Video recording audio backend for Main.
4 *
5 * This driver is part of Main and is responsible for providing audio
6 * data to Main's video capturing feature.
7 *
8 * The driver itself implements a PDM host audio backend, which in turn
9 * provides the driver with the required audio data and audio events.
10 *
11 * For now there is support for the following destinations (called "sinks"):
12 *
13 * - Direct writing of .webm files to the host.
14 * - Communicating with Main via the Console object to send the encoded audio data to.
15 * The Console object in turn then will route the data to the Display / video capturing interface then.
16 */
17
18/*
19 * Copyright (C) 2016-2023 Oracle and/or its affiliates.
20 *
21 * This file is part of VirtualBox base platform packages, as
22 * available from https://www.virtualbox.org.
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * as published by the Free Software Foundation, in version 3 of the
27 * License.
28 *
29 * This program is distributed in the hope that it will be useful, but
30 * WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, see <https://www.gnu.org/licenses>.
36 *
37 * SPDX-License-Identifier: GPL-3.0-only
38 */
39
40
41/*********************************************************************************************************************************
42* Header Files *
43*********************************************************************************************************************************/
44#define LOG_GROUP LOG_GROUP_RECORDING
45#include "LoggingNew.h"
46
47#include "DrvAudioRec.h"
48#include "ConsoleImpl.h"
49
50#include "WebMWriter.h"
51
52#include <iprt/mem.h>
53#include <iprt/cdefs.h>
54
55#include "VBox/com/VirtualBox.h"
56#include <VBox/vmm/cfgm.h>
57#include <VBox/vmm/pdmdrv.h>
58#include <VBox/vmm/pdmaudioifs.h>
59#include <VBox/vmm/pdmaudioinline.h>
60#include <VBox/vmm/vmmr3vtable.h>
61#include <VBox/err.h>
62#include "VBox/settings.h"
63
64
65/*********************************************************************************************************************************
66* Structures and Typedefs *
67*********************************************************************************************************************************/
68/**
69 * Enumeration for specifying the recording container type.
70 */
71typedef enum AVRECCONTAINERTYPE
72{
73 /** Unknown / invalid container type. */
74 AVRECCONTAINERTYPE_UNKNOWN = 0,
75 /** Recorded data goes to Main / Console. */
76 AVRECCONTAINERTYPE_MAIN_CONSOLE = 1,
77 /** Recorded data will be written to a .webm file. */
78 AVRECCONTAINERTYPE_WEBM = 2
79} AVRECCONTAINERTYPE;
80
81/**
82 * Structure for keeping generic container parameters.
83 */
84typedef struct AVRECCONTAINERPARMS
85{
86 /** Stream index (hint). */
87 uint32_t idxStream;
88 /** The container's type. */
89 AVRECCONTAINERTYPE enmType;
90 union
91 {
92 /** WebM file specifics. */
93 struct
94 {
95 /** Allocated file name to write .webm file to. Must be free'd. */
96 char *pszFile;
97 } WebM;
98 };
99
100} AVRECCONTAINERPARMS, *PAVRECCONTAINERPARMS;
101
102/**
103 * Structure for keeping container-specific data.
104 */
105typedef struct AVRECCONTAINER
106{
107 /** Generic container parameters. */
108 AVRECCONTAINERPARMS Parms;
109
110 union
111 {
112 struct
113 {
114 /** Pointer to Console. */
115 Console *pConsole;
116 } Main;
117
118 struct
119 {
120 /** Pointer to WebM container to write recorded audio data to.
121 * See the AVRECMODE enumeration for more information. */
122 WebMWriter *pWebM;
123 /** Assigned track number from WebM container. */
124 uint8_t uTrack;
125 } WebM;
126 };
127} AVRECCONTAINER, *PAVRECCONTAINER;
128
129/**
130 * Audio video recording sink.
131 */
132typedef struct AVRECSINK
133{
134 /** Pointer (weak) to recording stream to bind to. */
135 RecordingStream *pRecStream;
136 /** Container data to use for data processing. */
137 AVRECCONTAINER Con;
138 /** Timestamp (in ms) of when the sink was created. */
139 uint64_t tsStartMs;
140} AVRECSINK, *PAVRECSINK;
141
142/**
143 * Audio video recording (output) stream.
144 */
145typedef struct AVRECSTREAM
146{
147 /** Common part. */
148 PDMAUDIOBACKENDSTREAM Core;
149 /** The stream's acquired configuration. */
150 PDMAUDIOSTREAMCFG Cfg;
151 /** (Audio) frame buffer. */
152 PRTCIRCBUF pCircBuf;
153 /** Pointer to sink to use for writing. */
154 PAVRECSINK pSink;
155 /** Last encoded PTS (in ms). */
156 uint64_t uLastPTSMs;
157 /** Temporary buffer for the input (source) data to encode. */
158 void *pvSrcBuf;
159 /** Size (in bytes) of the temporary buffer holding the input (source) data to encode. */
160 size_t cbSrcBuf;
161} AVRECSTREAM, *PAVRECSTREAM;
162
163/**
164 * Video recording audio driver instance data.
165 */
166typedef struct DRVAUDIORECORDING
167{
168 /** Pointer to audio video recording object. */
169 AudioVideoRec *pAudioVideoRec;
170 /** Pointer to the driver instance structure. */
171 PPDMDRVINS pDrvIns;
172 /** Pointer to host audio interface. */
173 PDMIHOSTAUDIO IHostAudio;
174 /** Pointer to the console object. */
175 ComPtr<Console> pConsole;
176 /** Pointer to the DrvAudio port interface that is above us. */
177 AVRECCONTAINERPARMS ContainerParms;
178 /** Weak pointer to recording context to use. */
179 RecordingContext *pRecCtx;
180 /** The driver's sink for writing output to. */
181 AVRECSINK Sink;
182} DRVAUDIORECORDING, *PDRVAUDIORECORDING;
183
184
185AudioVideoRec::AudioVideoRec(Console *pConsole)
186 : AudioDriver(pConsole)
187 , mpDrv(NULL)
188{
189}
190
191
192AudioVideoRec::~AudioVideoRec(void)
193{
194 if (mpDrv)
195 {
196 mpDrv->pAudioVideoRec = NULL;
197 mpDrv = NULL;
198 }
199}
200
201
202/**
203 * Applies recording settings to this driver instance.
204 *
205 * @returns VBox status code.
206 * @param Settings Recording settings to apply.
207 */
208int AudioVideoRec::applyConfiguration(const settings::RecordingSettings &Settings)
209{
210 /** @todo Do some validation here. */
211 mSettings = Settings; /* Note: Does have an own copy operator. */
212 return VINF_SUCCESS;
213}
214
215
216int AudioVideoRec::configureDriver(PCFGMNODE pLunCfg, PCVMMR3VTABLE pVMM)
217{
218 /** @todo For now we're using the configuration of the first screen (screen 0) here audio-wise. */
219 unsigned const idxScreen = 0;
220
221 AssertReturn(mSettings.mapScreens.size() >= 1, VERR_INVALID_PARAMETER);
222 const settings::RecordingScreenSettings &screenSettings = mSettings.mapScreens[idxScreen];
223
224 int vrc = pVMM->pfnCFGMR3InsertInteger(pLunCfg, "ContainerType", (uint64_t)screenSettings.enmDest);
225 AssertRCReturn(vrc, vrc);
226 if (screenSettings.enmDest == RecordingDestination_File)
227 {
228 vrc = pVMM->pfnCFGMR3InsertString(pLunCfg, "ContainerFileName", Utf8Str(screenSettings.File.strName).c_str());
229 AssertRCReturn(vrc, vrc);
230 }
231
232 vrc = pVMM->pfnCFGMR3InsertInteger(pLunCfg, "StreamIndex", (uint32_t)idxScreen);
233 AssertRCReturn(vrc, vrc);
234
235 return AudioDriver::configureDriver(pLunCfg, pVMM);
236}
237
238
239/*********************************************************************************************************************************
240* PDMIHOSTAUDIO *
241*********************************************************************************************************************************/
242
243/**
244 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
245 */
246static DECLCALLBACK(int) drvAudioVideoRecHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
247{
248 RT_NOREF(pInterface);
249 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
250
251 /*
252 * Fill in the config structure.
253 */
254 RTStrCopy(pBackendCfg->szName, sizeof(pBackendCfg->szName), "VideoRec");
255 pBackendCfg->cbStream = sizeof(AVRECSTREAM);
256 pBackendCfg->fFlags = 0;
257 pBackendCfg->cMaxStreamsIn = 0;
258 pBackendCfg->cMaxStreamsOut = UINT32_MAX;
259
260 return VINF_SUCCESS;
261}
262
263
264/**
265 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
266 */
267static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvAudioVideoRecHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
268{
269 RT_NOREF(pInterface, enmDir);
270 return PDMAUDIOBACKENDSTS_RUNNING;
271}
272
273
274/**
275 * Creates an audio output stream and associates it with the specified recording sink.
276 *
277 * @returns VBox status code.
278 * @param pThis Driver instance.
279 * @param pStreamAV Audio output stream to create.
280 * @param pSink Recording sink to associate audio output stream to.
281 * @param pCfgReq Requested configuration by the audio backend.
282 * @param pCfgAcq Acquired configuration by the audio output stream.
283 */
284static int avRecCreateStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV,
285 PAVRECSINK pSink, PCPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
286{
287 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
288 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
289 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
290 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
291 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
292
293 if (pCfgReq->enmPath != PDMAUDIOPATH_OUT_FRONT)
294 {
295 LogRel(("Recording: Support for surround audio not implemented yet\n"));
296 AssertFailed();
297 return VERR_NOT_SUPPORTED;
298 }
299
300 PRECORDINGCODEC pCodec = pSink->pRecStream->GetAudioCodec();
301
302 /* Stuff which has to be set by now. */
303 Assert(pCodec->Parms.cbFrame);
304 Assert(pCodec->Parms.msFrame);
305
306 int vrc = RTCircBufCreate(&pStreamAV->pCircBuf, pCodec->Parms.cbFrame * 2 /* Use "double buffering" */);
307 if (RT_SUCCESS(vrc))
308 {
309 size_t cbScratchBuf = pCodec->Parms.cbFrame;
310 pStreamAV->pvSrcBuf = RTMemAlloc(cbScratchBuf);
311 if (pStreamAV->pvSrcBuf)
312 {
313 pStreamAV->cbSrcBuf = cbScratchBuf;
314
315 pStreamAV->pSink = pSink; /* Assign sink to stream. */
316 pStreamAV->uLastPTSMs = 0;
317
318 /* Make sure to let the driver backend know that we need the audio data in
319 * a specific sampling rate the codec is optimized for. */
320 pCfgAcq->Props = pCodec->Parms.Audio.PCMProps;
321
322 /* Every codec frame marks a period for now. Optimize this later. */
323 pCfgAcq->Backend.cFramesPeriod = PDMAudioPropsMilliToFrames(&pCfgAcq->Props, pCodec->Parms.msFrame);
324 pCfgAcq->Backend.cFramesBufferSize = pCfgAcq->Backend.cFramesPeriod * 2;
325 pCfgAcq->Backend.cFramesPreBuffering = pCfgAcq->Backend.cFramesPeriod;
326 }
327 else
328 vrc = VERR_NO_MEMORY;
329 }
330
331 LogFlowFuncLeaveRC(vrc);
332 return vrc;
333}
334
335
336/**
337 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
338 */
339static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
340 PCPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
341{
342 PDRVAUDIORECORDING pThis = RT_FROM_CPP_MEMBER(pInterface, DRVAUDIORECORDING, IHostAudio);
343 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
344 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
345 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
346 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
347
348 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
349 return VERR_NOT_SUPPORTED;
350
351 /* For now we only have one sink, namely the driver's one.
352 * Later each stream could have its own one, to e.g. router different stream to different sinks .*/
353 PAVRECSINK pSink = &pThis->Sink;
354
355 int vrc = avRecCreateStreamOut(pThis, pStreamAV, pSink, pCfgReq, pCfgAcq);
356 PDMAudioStrmCfgCopy(&pStreamAV->Cfg, pCfgAcq);
357
358 return vrc;
359}
360
361
362/**
363 * Destroys (closes) an audio output stream.
364 *
365 * @returns VBox status code.
366 * @param pThis Driver instance.
367 * @param pStreamAV Audio output stream to destroy.
368 */
369static int avRecDestroyStreamOut(PDRVAUDIORECORDING pThis, PAVRECSTREAM pStreamAV)
370{
371 RT_NOREF(pThis);
372
373 if (pStreamAV->pCircBuf)
374 {
375 RTCircBufDestroy(pStreamAV->pCircBuf);
376 pStreamAV->pCircBuf = NULL;
377 }
378
379 if (pStreamAV->pvSrcBuf)
380 {
381 Assert(pStreamAV->cbSrcBuf);
382 RTMemFree(pStreamAV->pvSrcBuf);
383 pStreamAV->pvSrcBuf = NULL;
384 pStreamAV->cbSrcBuf = 0;
385 }
386
387 return VINF_SUCCESS;
388}
389
390
391/**
392 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
393 */
394static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
395 bool fImmediate)
396{
397 PDRVAUDIORECORDING pThis = RT_FROM_CPP_MEMBER(pInterface, DRVAUDIORECORDING, IHostAudio);
398 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
399 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
400 RT_NOREF(fImmediate);
401
402 int vrc = VINF_SUCCESS;
403 if (pStreamAV->Cfg.enmDir == PDMAUDIODIR_OUT)
404 vrc = avRecDestroyStreamOut(pThis, pStreamAV);
405
406 return vrc;
407}
408
409
410/**
411 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamEnable}
412 */
413static DECLCALLBACK(int) drvAudioVideoRecHA_StreamEnable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
414{
415 RT_NOREF(pInterface, pStream);
416 return VINF_SUCCESS;
417}
418
419
420/**
421 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDisable}
422 */
423static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDisable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
424{
425 RT_NOREF(pInterface, pStream);
426 return VINF_SUCCESS;
427}
428
429
430/**
431 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPause}
432 */
433static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPause(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
434{
435 RT_NOREF(pInterface, pStream);
436 return VINF_SUCCESS;
437}
438
439
440/**
441 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamResume}
442 */
443static DECLCALLBACK(int) drvAudioVideoRecHA_StreamResume(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
444{
445 RT_NOREF(pInterface, pStream);
446 return VINF_SUCCESS;
447}
448
449
450/**
451 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDrain}
452 */
453static DECLCALLBACK(int) drvAudioVideoRecHA_StreamDrain(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
454{
455 RT_NOREF(pInterface, pStream);
456 return VINF_SUCCESS;
457}
458
459
460/**
461 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetState}
462 */
463static DECLCALLBACK(PDMHOSTAUDIOSTREAMSTATE) drvAudioVideoRecHA_StreamGetState(PPDMIHOSTAUDIO pInterface,
464 PPDMAUDIOBACKENDSTREAM pStream)
465{
466 RT_NOREF(pInterface);
467 AssertPtrReturn(pStream, PDMHOSTAUDIOSTREAMSTATE_INVALID);
468 return PDMHOSTAUDIOSTREAMSTATE_OKAY;
469}
470
471
472/**
473 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
474 */
475static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
476{
477 RT_NOREF(pInterface);
478 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
479
480 RecordingStream *pRecStream = pStreamAV->pSink->pRecStream;
481 PRECORDINGCODEC pCodec = pRecStream->GetAudioCodec();
482
483 return pCodec->Parms.cbFrame;
484}
485
486
487/**
488 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
489 */
490static DECLCALLBACK(int) drvAudioVideoRecHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
491 const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
492{
493 RT_NOREF(pInterface);
494 PAVRECSTREAM pStreamAV = (PAVRECSTREAM)pStream;
495 AssertPtrReturn(pStreamAV, VERR_INVALID_POINTER);
496 if (cbBuf)
497 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
498 AssertReturn(pcbWritten, VERR_INVALID_PARAMETER);
499
500 int vrc = VINF_SUCCESS;
501
502 uint32_t cbWrittenTotal = 0;
503
504 PRTCIRCBUF pCircBuf = pStreamAV->pCircBuf;
505 AssertPtr(pCircBuf);
506 uint32_t const cbFree = (uint32_t)RTCircBufFree(pCircBuf);
507 uint32_t cbToWrite = RT_MIN(cbBuf, cbFree);
508 AssertReturn(cbToWrite, VERR_BUFFER_OVERFLOW);
509
510 /*
511 * Write as much as we can into our internal ring buffer.
512 */
513 while (cbToWrite)
514 {
515 void *pvCircBuf = NULL;
516 size_t cbCircBuf = 0;
517 RTCircBufAcquireWriteBlock(pCircBuf, cbToWrite, &pvCircBuf, &cbCircBuf);
518
519 Log3Func(("cbToWrite=%RU32, cbCircBuf=%zu\n", cbToWrite, cbCircBuf));
520
521 memcpy(pvCircBuf, (uint8_t *)pvBuf + cbWrittenTotal, cbCircBuf),
522 cbWrittenTotal += (uint32_t)cbCircBuf;
523 Assert(cbWrittenTotal <= cbBuf);
524 Assert(cbToWrite >= cbCircBuf);
525 cbToWrite -= (uint32_t)cbCircBuf;
526
527 RTCircBufReleaseWriteBlock(pCircBuf, cbCircBuf);
528 }
529
530 RecordingStream *pRecStream = pStreamAV->pSink->pRecStream;
531 PRECORDINGCODEC pCodec = pRecStream->GetAudioCodec();
532
533 /*
534 * Process our internal ring buffer and send the obtained audio data to our encoding thread.
535 */
536 cbToWrite = (uint32_t)RTCircBufUsed(pCircBuf);
537
538 /** @todo Can we encode more than a frame at a time? Optimize this! */
539 uint32_t const cbFrame = pCodec->Parms.cbFrame;
540
541 /* Only encode data if we have data for at least one full codec frame. */
542 while (cbToWrite >= cbFrame)
543 {
544 uint32_t cbSrc = 0;
545 do
546 {
547 void *pvCircBuf = NULL;
548 size_t cbCircBuf = 0;
549 RTCircBufAcquireReadBlock(pCircBuf, cbFrame - cbSrc, &pvCircBuf, &cbCircBuf);
550
551 Log3Func(("cbSrc=%RU32, cbCircBuf=%zu\n", cbSrc, cbCircBuf));
552
553 memcpy((uint8_t *)pStreamAV->pvSrcBuf + cbSrc, pvCircBuf, cbCircBuf);
554
555 cbSrc += (uint32_t)cbCircBuf;
556 Assert(cbSrc <= pStreamAV->cbSrcBuf);
557 Assert(cbSrc <= cbFrame);
558
559 RTCircBufReleaseReadBlock(pCircBuf, cbCircBuf);
560
561 if (cbSrc == cbFrame) /* Only send full codec frames. */
562 {
563 vrc = pRecStream->SendAudioFrame(pStreamAV->pvSrcBuf, cbSrc, RTTimeProgramMilliTS());
564 if (RT_FAILURE(vrc))
565 break;
566 }
567
568 } while (cbSrc < cbFrame);
569
570 Assert(cbToWrite >= cbFrame);
571 cbToWrite -= cbFrame;
572
573 if (RT_FAILURE(vrc))
574 break;
575
576 } /* while */
577
578 *pcbWritten = cbWrittenTotal;
579
580 LogFlowFunc(("cbBuf=%RU32, cbWrittenTotal=%RU32, vrc=%Rrc\n", cbBuf, cbWrittenTotal, vrc));
581 return VINF_SUCCESS; /* Don't propagate encoding errors to the caller. */
582}
583
584
585/**
586 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
587 */
588static DECLCALLBACK(uint32_t) drvAudioVideoRecHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
589{
590 RT_NOREF(pInterface, pStream);
591 return 0; /* Video capturing does not provide any input. */
592}
593
594
595/**
596 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
597 */
598static DECLCALLBACK(int) drvAudioVideoRecHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
599 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
600{
601 RT_NOREF(pInterface, pStream, pvBuf, cbBuf);
602 *pcbRead = 0;
603 return VINF_SUCCESS;
604}
605
606
607/*********************************************************************************************************************************
608* PDMIBASE *
609*********************************************************************************************************************************/
610
611/**
612 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
613 */
614static DECLCALLBACK(void *) drvAudioVideoRecQueryInterface(PPDMIBASE pInterface, const char *pszIID)
615{
616 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
617 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
618
619 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
620 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
621 return NULL;
622}
623
624
625/*********************************************************************************************************************************
626* PDMDRVREG *
627*********************************************************************************************************************************/
628
629/**
630 * Shuts down (closes) a recording sink,
631 *
632 * @param pSink Recording sink to shut down.
633 */
634static void avRecSinkShutdown(PAVRECSINK pSink)
635{
636 AssertPtrReturnVoid(pSink);
637
638 pSink->pRecStream = NULL;
639
640 switch (pSink->Con.Parms.enmType)
641 {
642 case AVRECCONTAINERTYPE_WEBM:
643 {
644 if (pSink->Con.WebM.pWebM)
645 {
646 LogRel2(("Recording: Finished recording audio to file '%s' (%zu bytes)\n",
647 pSink->Con.WebM.pWebM->GetFileName().c_str(), pSink->Con.WebM.pWebM->GetFileSize()));
648
649 int vrc2 = pSink->Con.WebM.pWebM->Close();
650 AssertRC(vrc2);
651
652 delete pSink->Con.WebM.pWebM;
653 pSink->Con.WebM.pWebM = NULL;
654 }
655 break;
656 }
657
658 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
659 RT_FALL_THROUGH();
660 default:
661 break;
662 }
663}
664
665
666/**
667 * @interface_method_impl{PDMDRVREG,pfnPowerOff}
668 */
669/*static*/ DECLCALLBACK(void) AudioVideoRec::drvPowerOff(PPDMDRVINS pDrvIns)
670{
671 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
672 LogFlowFuncEnter();
673 avRecSinkShutdown(&pThis->Sink);
674}
675
676
677/**
678 * @interface_method_impl{PDMDRVREG,pfnDestruct}
679 */
680/*static*/ DECLCALLBACK(void) AudioVideoRec::drvDestruct(PPDMDRVINS pDrvIns)
681{
682 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
683 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
684
685 LogFlowFuncEnter();
686
687 switch (pThis->ContainerParms.enmType)
688 {
689 case AVRECCONTAINERTYPE_WEBM:
690 {
691 avRecSinkShutdown(&pThis->Sink);
692 RTStrFree(pThis->ContainerParms.WebM.pszFile);
693 break;
694 }
695
696 default:
697 break;
698 }
699
700 /*
701 * If the AudioVideoRec object is still alive, we must clear it's reference to
702 * us since we'll be invalid when we return from this method.
703 */
704 if (pThis->pAudioVideoRec)
705 {
706 pThis->pAudioVideoRec->mpDrv = NULL;
707 pThis->pAudioVideoRec = NULL;
708 }
709
710 LogFlowFuncLeave();
711}
712
713
714/**
715 * Initializes a recording sink.
716 *
717 * @returns VBox status code.
718 * @param pThis Driver instance.
719 * @param pSink Sink to initialize.
720 * @param pConParms Container parameters to set.
721 * @param pStream Recording stream to asssign sink to.
722 */
723static int avRecSinkInit(PDRVAUDIORECORDING pThis, PAVRECSINK pSink, PAVRECCONTAINERPARMS pConParms, RecordingStream *pStream)
724{
725 pSink->pRecStream = pStream;
726
727 int vrc = VINF_SUCCESS;
728
729 /*
730 * Container setup.
731 */
732 try
733 {
734 switch (pConParms->enmType)
735 {
736 case AVRECCONTAINERTYPE_MAIN_CONSOLE:
737 {
738 if (pThis->pConsole)
739 {
740 pSink->Con.Main.pConsole = pThis->pConsole;
741 }
742 else
743 vrc = VERR_NOT_SUPPORTED;
744 break;
745 }
746
747 case AVRECCONTAINERTYPE_WEBM:
748 {
749 #if 0
750 /* If we only record audio, create our own WebM writer instance here. */
751 if (!pSink->Con.WebM.pWebM) /* Do we already have our WebM writer instance? */
752 {
753 /** @todo Add sink name / number to file name. */
754 const char *pszFile = pSink->Con.Parms.WebM.pszFile;
755 AssertPtr(pszFile);
756
757 pSink->Con.WebM.pWebM = new WebMWriter();
758 vrc = pSink->Con.WebM.pWebM->Open(pszFile,
759 /** @todo Add option to add some suffix if file exists instead of overwriting? */
760 RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE,
761 pSink->pCodec->Parms.enmAudioCodec, RecordingVideoCodec_None);
762 if (RT_SUCCESS(vrc))
763 {
764 const PPDMAUDIOPCMPROPS pPCMProps = &pCodec->Parms.Audio.PCMProps;
765
766 vrc = pSink->Con.WebM.pWebM->AddAudioTrack(pSink->pCodec,
767 PDMAudioPropsHz(pPCMProps), PDMAudioPropsChannels(pPCMProps),
768 PDMAudioPropsSampleBits(pPCMProps), &pSink->Con.WebM.uTrack);
769 if (RT_SUCCESS(vrc))
770 {
771 LogRel(("Recording: Recording audio to audio file '%s'\n", pszFile));
772 }
773 else
774 LogRel(("Recording: Error creating audio track for audio file '%s' (%Rrc)\n", pszFile, vrc));
775 }
776 else
777 LogRel(("Recording: Error creating audio file '%s' (%Rrc)\n", pszFile, vrc));
778 }
779 break;
780 #else
781 vrc = VERR_NOT_SUPPORTED;
782 break;
783 #endif
784 }
785
786 default:
787 vrc = VERR_NOT_SUPPORTED;
788 break;
789 }
790 }
791 catch (std::bad_alloc &)
792 {
793 vrc = VERR_NO_MEMORY;
794 }
795
796 if (RT_SUCCESS(vrc))
797 {
798 pSink->Con.Parms.enmType = pConParms->enmType;
799 pSink->tsStartMs = RTTimeMilliTS();
800
801 return VINF_SUCCESS;
802 }
803
804 LogRel(("Recording: Error creating sink (%Rrc)\n", vrc));
805 return vrc;
806}
807
808
809/**
810 * Construct a audio video recording driver instance.
811 *
812 * @copydoc FNPDMDRVCONSTRUCT
813 */
814/*static*/ DECLCALLBACK(int) AudioVideoRec::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
815{
816 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
817 PDRVAUDIORECORDING pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIORECORDING);
818 RT_NOREF(fFlags);
819
820 LogRel(("Audio: Initializing video recording audio driver\n"));
821 LogFlowFunc(("fFlags=0x%x\n", fFlags));
822
823 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
824 ("Configuration error: Not possible to attach anything to this driver!\n"),
825 VERR_PDM_DRVINS_NO_ATTACH);
826
827 /*
828 * Init the static parts.
829 */
830 pThis->pDrvIns = pDrvIns;
831 /* IBase */
832 pDrvIns->IBase.pfnQueryInterface = drvAudioVideoRecQueryInterface;
833 /* IHostAudio */
834 pThis->IHostAudio.pfnGetConfig = drvAudioVideoRecHA_GetConfig;
835 pThis->IHostAudio.pfnGetDevices = NULL;
836 pThis->IHostAudio.pfnSetDevice = NULL;
837 pThis->IHostAudio.pfnGetStatus = drvAudioVideoRecHA_GetStatus;
838 pThis->IHostAudio.pfnDoOnWorkerThread = NULL;
839 pThis->IHostAudio.pfnStreamConfigHint = NULL;
840 pThis->IHostAudio.pfnStreamCreate = drvAudioVideoRecHA_StreamCreate;
841 pThis->IHostAudio.pfnStreamInitAsync = NULL;
842 pThis->IHostAudio.pfnStreamDestroy = drvAudioVideoRecHA_StreamDestroy;
843 pThis->IHostAudio.pfnStreamNotifyDeviceChanged = NULL;
844 pThis->IHostAudio.pfnStreamEnable = drvAudioVideoRecHA_StreamEnable;
845 pThis->IHostAudio.pfnStreamDisable = drvAudioVideoRecHA_StreamDisable;
846 pThis->IHostAudio.pfnStreamPause = drvAudioVideoRecHA_StreamPause;
847 pThis->IHostAudio.pfnStreamResume = drvAudioVideoRecHA_StreamResume;
848 pThis->IHostAudio.pfnStreamDrain = drvAudioVideoRecHA_StreamDrain;
849 pThis->IHostAudio.pfnStreamGetState = drvAudioVideoRecHA_StreamGetState;
850 pThis->IHostAudio.pfnStreamGetPending = NULL;
851 pThis->IHostAudio.pfnStreamGetWritable = drvAudioVideoRecHA_StreamGetWritable;
852 pThis->IHostAudio.pfnStreamPlay = drvAudioVideoRecHA_StreamPlay;
853 pThis->IHostAudio.pfnStreamGetReadable = drvAudioVideoRecHA_StreamGetReadable;
854 pThis->IHostAudio.pfnStreamCapture = drvAudioVideoRecHA_StreamCapture;
855
856 /*
857 * Read configuration.
858 */
859 PCPDMDRVHLPR3 const pHlp = pDrvIns->pHlpR3;
860 /** @todo validate it. */
861
862 /*
863 * Get the Console object pointer.
864 */
865 com::Guid ConsoleUuid(COM_IIDOF(IConsole));
866 IConsole *pIConsole = (IConsole *)PDMDrvHlpQueryGenericUserObject(pDrvIns, ConsoleUuid.raw());
867 AssertLogRelReturn(pIConsole, VERR_INTERNAL_ERROR_3);
868 Console *pConsole = static_cast<Console *>(pIConsole);
869 AssertLogRelReturn(pConsole, VERR_INTERNAL_ERROR_3);
870
871 pThis->pConsole = pConsole;
872 AssertReturn(!pThis->pConsole.isNull(), VERR_INVALID_POINTER);
873 pThis->pAudioVideoRec = pConsole->i_recordingGetAudioDrv();
874 AssertPtrReturn(pThis->pAudioVideoRec, VERR_INVALID_POINTER);
875
876 pThis->pAudioVideoRec->mpDrv = pThis;
877
878 /*
879 * Get the recording container parameters from the audio driver instance.
880 */
881 RT_ZERO(pThis->ContainerParms);
882 PAVRECCONTAINERPARMS pConParams = &pThis->ContainerParms;
883
884 int vrc = pHlp->pfnCFGMQueryU32(pCfg, "StreamIndex", (uint32_t *)&pConParams->idxStream);
885 AssertRCReturn(vrc, vrc);
886
887 vrc = pHlp->pfnCFGMQueryU32(pCfg, "ContainerType", (uint32_t *)&pConParams->enmType);
888 AssertRCReturn(vrc, vrc);
889
890 switch (pConParams->enmType)
891 {
892 case AVRECCONTAINERTYPE_WEBM:
893 vrc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "ContainerFileName", &pConParams->WebM.pszFile);
894 AssertRCReturn(vrc, vrc);
895 break;
896
897 default:
898 break;
899 }
900
901 /*
902 * Obtain the recording context.
903 */
904 pThis->pRecCtx = pConsole->i_recordingGetContext();
905 AssertPtrReturn(pThis->pRecCtx, VERR_INVALID_POINTER);
906
907 /*
908 * Get the codec configuration.
909 */
910 RecordingStream *pStream = pThis->pRecCtx->GetStream(pConParams->idxStream);
911 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
912
913 /*
914 * Init the recording sink.
915 */
916 vrc = avRecSinkInit(pThis, &pThis->Sink, &pThis->ContainerParms, pStream);
917 if (RT_SUCCESS(vrc))
918 LogRel2(("Recording: Audio recording driver initialized\n"));
919 else
920 LogRel(("Recording: Audio recording driver initialization failed: %Rrc\n", vrc));
921
922 return vrc;
923}
924
925
926/**
927 * Video recording audio driver registration record.
928 */
929const PDMDRVREG AudioVideoRec::DrvReg =
930{
931 PDM_DRVREG_VERSION,
932 /* szName */
933 "AudioVideoRec",
934 /* szRCMod */
935 "",
936 /* szR0Mod */
937 "",
938 /* pszDescription */
939 "Audio driver for video recording",
940 /* fFlags */
941 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
942 /* fClass. */
943 PDM_DRVREG_CLASS_AUDIO,
944 /* cMaxInstances */
945 ~0U,
946 /* cbInstance */
947 sizeof(DRVAUDIORECORDING),
948 /* pfnConstruct */
949 AudioVideoRec::drvConstruct,
950 /* pfnDestruct */
951 AudioVideoRec::drvDestruct,
952 /* pfnRelocate */
953 NULL,
954 /* pfnIOCtl */
955 NULL,
956 /* pfnPowerOn */
957 NULL,
958 /* pfnReset */
959 NULL,
960 /* pfnSuspend */
961 NULL,
962 /* pfnResume */
963 NULL,
964 /* pfnAttach */
965 NULL,
966 /* pfnDetach */
967 NULL,
968 /* pfnPowerOff */
969 AudioVideoRec::drvPowerOff,
970 /* pfnSoftReset */
971 NULL,
972 /* u32EndVersion */
973 PDM_DRVREG_VERSION
974};
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