VirtualBox

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

Last change on this file since 68737 was 68737, checked in by vboxsync, 7 years ago

Build fix.

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