VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/audiosniffer.c@ 18645

Last change on this file since 18645 was 12978, checked in by vboxsync, 16 years ago

PDM: PDM_DEVREG_FLAGS_DEFAULT_BITS convenience.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1/** @file
2 *
3 * VBox audio device:
4 * Audio sniffer device
5 */
6
7/*
8 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#define LOG_GROUP LOG_GROUP_DEV_AUDIO
24#define AUDIO_CAP "sniffer"
25#include <VBox/pdm.h>
26#include <VBox/err.h>
27
28#include <VBox/log.h>
29#include <iprt/assert.h>
30#include <iprt/uuid.h>
31#include <iprt/string.h>
32#include <iprt/alloc.h>
33
34#include "Builtins.h"
35#include "../../vl_vbox.h"
36
37#include "audio.h"
38#include "audio_int.h"
39
40typedef struct _AUDIOSNIFFERSTATE
41{
42 /** If the device is enabled. */
43 bool fEnabled;
44
45 /** Whether audio should reach the host driver too. */
46 bool fKeepHostAudio;
47
48 /** Pointer to device instance. */
49 PPDMDEVINS pDevIns;
50
51 /** Audio Sniffer port base interface. */
52 PDMIBASE Base;
53 /** Audio Sniffer port interface. */
54 PDMIAUDIOSNIFFERPORT Port;
55
56 /** Pointer to base interface of the driver. */
57 PPDMIBASE pDrvBase;
58 /** Audio Sniffer connector interface */
59 PPDMIAUDIOSNIFFERCONNECTOR pDrv;
60
61} AUDIOSNIFFERSTATE;
62
63static AUDIOSNIFFERSTATE *g_pData = NULL;
64
65/*
66 * Public sniffer callbacks to be called from audio driver.
67 */
68
69/* *** Subject to change ***
70 * Process audio output. The function is called when an audio output
71 * driver is about to play audio samples.
72 *
73 * It is expected that there is only one audio data flow,
74 * i.e. one voice.
75 *
76 * @param hw Audio samples information.
77 * @param pvSamples Pointer to audio samples.
78 * @param cSamples Number of audio samples in the buffer.
79 * @returns 'true' if audio also to be played back by the output driver.
80 * 'false' if audio should not be played.
81 */
82DECLCALLBACK(bool) sniffer_run_out (HWVoiceOut *hw, void *pvSamples, unsigned cSamples)
83{
84 int samplesPerSec;
85 int nChannels;
86 int bitsPerSample;
87 bool fUnsigned;
88
89 if (!g_pData || !g_pData->pDrv || !g_pData->fEnabled)
90 {
91 return true;
92 }
93
94 samplesPerSec = hw->info.freq;
95 nChannels = hw->info.nchannels;
96 bitsPerSample = hw->info.bits;
97 fUnsigned = (hw->info.sign == 0);
98
99 g_pData->pDrv->pfnAudioSamplesOut (g_pData->pDrv, pvSamples, cSamples,
100 samplesPerSec, nChannels, bitsPerSample, fUnsigned);
101
102 return g_pData->fKeepHostAudio;
103}
104
105
106/*
107 * Audio Sniffer PDM device.
108 */
109
110/* Converts a Audio Sniffer port interface pointer to a Audio Sniffer state pointer. */
111#define IAUDIOSNIFFERPORT_2_AUDIOSNIFFERSTATE(pInterface) ((AUDIOSNIFFERSTATE *)((uintptr_t)pInterface - RT_OFFSETOF(AUDIOSNIFFERSTATE, Port)))
112
113static DECLCALLBACK(int) iface_Setup (PPDMIAUDIOSNIFFERPORT pInterface, bool fEnable, bool fKeepHostAudio)
114{
115 AUDIOSNIFFERSTATE *pThis = IAUDIOSNIFFERPORT_2_AUDIOSNIFFERSTATE(pInterface);
116
117 Assert(g_pData == pThis);
118
119 pThis->fEnabled = fEnable;
120 pThis->fKeepHostAudio = fKeepHostAudio;
121
122 return VINF_SUCCESS;
123}
124
125/**
126 * Queries an interface to the device.
127 *
128 * @returns Pointer to interface.
129 * @returns NULL if the interface was not supported by the driver.
130 * @param pInterface Pointer to this interface structure.
131 * @param enmInterface The requested interface identification.
132 */
133static DECLCALLBACK(void *) iface_QueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
134{
135 AUDIOSNIFFERSTATE *pThis = (AUDIOSNIFFERSTATE *)((uintptr_t)pInterface - RT_OFFSETOF(AUDIOSNIFFERSTATE, Base));
136
137 switch (enmInterface)
138 {
139 case PDMINTERFACE_BASE:
140 return &pThis->Base;
141 case PDMINTERFACE_AUDIO_SNIFFER_PORT:
142 return &pThis->Port;
143 default:
144 return NULL;
145 }
146}
147
148/**
149 * Construct a device instance for a VM.
150 *
151 * @returns VBox status.
152 * @param pDevIns The device instance data.
153 * If the registration structure is needed, pDevIns->pDevReg points to it.
154 * @param iInstance Instance number. Use this to figure out which registers and such to use.
155 * The device number is also found in pDevIns->iInstance, but since it's
156 * likely to be freqently used PDM passes it as parameter.
157 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
158 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
159 * iInstance it's expected to be used a bit in this function.
160 */
161static DECLCALLBACK(int) audioSnifferR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
162{
163 int rc = VINF_SUCCESS;
164
165 AUDIOSNIFFERSTATE *pThis = PDMINS_2_DATA(pDevIns, AUDIOSNIFFERSTATE *);
166
167 Assert(iInstance == 0);
168
169 /*
170 * Validate configuration.
171 */
172 if (!CFGMR3AreValuesValid(pCfgHandle, "\0"))
173 {
174 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
175 }
176
177 /*
178 * Initialize data.
179 */
180 pThis->fEnabled = false;
181 pThis->fKeepHostAudio = true;
182 pThis->pDrv = NULL;
183
184 /*
185 * Interfaces
186 */
187 /* Base */
188 pThis->Base.pfnQueryInterface = iface_QueryInterface;
189
190 /* Audio Sniffer port */
191 pThis->Port.pfnSetup = iface_Setup;
192
193 /*
194 * Get the corresponding connector interface
195 */
196 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->Base, &pThis->pDrvBase, "Audio Sniffer Port");
197
198 if (RT_SUCCESS(rc))
199 {
200 pThis->pDrv = (PPDMIAUDIOSNIFFERCONNECTOR)pThis->pDrvBase->pfnQueryInterface(pThis->pDrvBase, PDMINTERFACE_AUDIO_SNIFFER_CONNECTOR);
201
202 if (!pThis->pDrv)
203 {
204 AssertMsgFailed(("LUN #0 doesn't have a Audio Sniffer connector interface rc=%Rrc\n", rc));
205 rc = VERR_PDM_MISSING_INTERFACE;
206 }
207 }
208 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
209 {
210 Log(("%s/%d: warning: no driver attached to LUN #0.\n", pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
211 rc = VINF_SUCCESS;
212 }
213 else
214 {
215 AssertMsgFailed(("Failed to attach LUN #0. rc=%Rrc\n", rc));
216 }
217
218 if (RT_SUCCESS (rc))
219 {
220 /* Save PDM device instance data for future reference. */
221 pThis->pDevIns = pDevIns;
222
223 /* Save the pointer to created instance in the global variable, so other
224 * functions could reach it.
225 */
226 g_pData = pThis;
227 }
228
229 return rc;
230}
231
232/**
233 * Destruct a device instance.
234 *
235 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
236 * resources can be freed correctly.
237 *
238 * @returns VBox status.
239 * @param pDevIns The device instance data.
240 */
241static DECLCALLBACK(int) audioSnifferR3Destruct(PPDMDEVINS pDevIns)
242{
243 /* Zero the global pointer. */
244 g_pData = NULL;
245
246 return VINF_SUCCESS;
247}
248
249/**
250 * The Audio Sniffer device registration structure.
251 */
252const PDMDEVREG g_DeviceAudioSniffer =
253{
254 /* u32Version */
255 PDM_DEVREG_VERSION,
256 /* szDeviceName */
257 "AudioSniffer",
258 /* szRCMod */
259 "",
260 /* szR0Mod */
261 "",
262 /* pszDescription */
263 "Audio Sniffer device. Redirects audio data to sniffer driver.",
264 /* fFlags */
265 PDM_DEVREG_FLAGS_DEFAULT_BITS,
266 /* fClass */
267 PDM_DEVREG_CLASS_AUDIO,
268 /* cMaxInstances */
269 1,
270 /* cbInstance */
271 sizeof(AUDIOSNIFFERSTATE),
272 /* pfnConstruct */
273 audioSnifferR3Construct,
274 /* pfnDestruct */
275 audioSnifferR3Destruct,
276 /* pfnRelocate */
277 NULL,
278 /* pfnIOCtl */
279 NULL,
280 /* pfnPowerOn */
281 NULL,
282 /* pfnReset */
283 NULL,
284 /* pfnSuspend */
285 NULL,
286 /* pfnResume */
287 NULL,
288 /* pfnAttach */
289 NULL,
290 /* pfnDetach */
291 NULL,
292 /* pfnQueryInterface */
293 NULL,
294 /* pfnInitComplete */
295 NULL,
296 /* pfnPowerOff */
297 NULL,
298 /* pfnSoftReset */
299 NULL,
300 PDM_DEVREG_VERSION
301};
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