1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox audio device:
|
---|
4 | * Audio sniffer device
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
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 |
|
---|
40 | typedef 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 |
|
---|
63 | static 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 | */
|
---|
82 | DECLCALLBACK(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 |
|
---|
113 | static DECLCALLBACK(int) iface_Setup (PPDMIAUDIOSNIFFERPORT pInterface, bool fEnable, bool fKeepHostAudio)
|
---|
114 | {
|
---|
115 | AUDIOSNIFFERSTATE *pData = IAUDIOSNIFFERPORT_2_AUDIOSNIFFERSTATE(pInterface);
|
---|
116 |
|
---|
117 | Assert(g_pData == pData);
|
---|
118 |
|
---|
119 | pData->fEnabled = fEnable;
|
---|
120 | pData->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 | */
|
---|
133 | static DECLCALLBACK(void *) iface_QueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
|
---|
134 | {
|
---|
135 | AUDIOSNIFFERSTATE *pData = (AUDIOSNIFFERSTATE *)((uintptr_t)pInterface - RT_OFFSETOF(AUDIOSNIFFERSTATE, Base));
|
---|
136 |
|
---|
137 | switch (enmInterface)
|
---|
138 | {
|
---|
139 | case PDMINTERFACE_BASE:
|
---|
140 | return &pData->Base;
|
---|
141 | case PDMINTERFACE_AUDIO_SNIFFER_PORT:
|
---|
142 | return &pData->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 | */
|
---|
161 | static DECLCALLBACK(int) audioSnifferR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
|
---|
162 | {
|
---|
163 | int rc = VINF_SUCCESS;
|
---|
164 |
|
---|
165 | AUDIOSNIFFERSTATE *pData = PDMINS2DATA(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 | pData->fEnabled = false;
|
---|
181 | pData->fKeepHostAudio = true;
|
---|
182 | pData->pDrv = NULL;
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Interfaces
|
---|
186 | */
|
---|
187 | /* Base */
|
---|
188 | pData->Base.pfnQueryInterface = iface_QueryInterface;
|
---|
189 |
|
---|
190 | /* Audio Sniffer port */
|
---|
191 | pData->Port.pfnSetup = iface_Setup;
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Get the corresponding connector interface
|
---|
195 | */
|
---|
196 | rc = PDMDevHlpDriverAttach(pDevIns, 0, &pData->Base, &pData->pDrvBase, "Audio Sniffer Port");
|
---|
197 |
|
---|
198 | if (VBOX_SUCCESS(rc))
|
---|
199 | {
|
---|
200 | pData->pDrv = (PPDMIAUDIOSNIFFERCONNECTOR)pData->pDrvBase->pfnQueryInterface(pData->pDrvBase, PDMINTERFACE_AUDIO_SNIFFER_CONNECTOR);
|
---|
201 |
|
---|
202 | if (!pData->pDrv)
|
---|
203 | {
|
---|
204 | AssertMsgFailed(("LUN #0 doesn't have a Audio Sniffer connector interface rc=%Vrc\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=%Vrc\n", rc));
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (VBOX_SUCCESS (rc))
|
---|
219 | {
|
---|
220 | /* Save PDM device instance data for future reference. */
|
---|
221 | pData->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 = pData;
|
---|
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 | */
|
---|
241 | static 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 | */
|
---|
252 | const PDMDEVREG g_DeviceAudioSniffer =
|
---|
253 | {
|
---|
254 | /* u32Version */
|
---|
255 | PDM_DEVREG_VERSION,
|
---|
256 | /* szDeviceName */
|
---|
257 | "AudioSniffer",
|
---|
258 | /* szGCMod */
|
---|
259 | "",
|
---|
260 | /* szR0Mod */
|
---|
261 | "",
|
---|
262 | /* pszDescription */
|
---|
263 | "Audio Sniffer device. Redirects audio data to sniffer driver.",
|
---|
264 | /* fFlags */
|
---|
265 | PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT,
|
---|
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 | };
|
---|