audiosniffer.c revision ee4d840f54fd2dcea8a73b1b86d5ec0db370b05d
59190ecd61435d19ba3515b876272aee7bd12298vboxsync/* $Id$ */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync/** @file
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * VBox audio device: Audio sniffer device
59190ecd61435d19ba3515b876272aee7bd12298vboxsync */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync/*
772269936494ffaddd0750ba9e28e805ba81398cvboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
59190ecd61435d19ba3515b876272aee7bd12298vboxsync *
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * available from http://www.virtualbox.org. This file is free software;
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * General Public License (GPL) as published by the Free Software
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
59190ecd61435d19ba3515b876272aee7bd12298vboxsync *
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
772269936494ffaddd0750ba9e28e805ba81398cvboxsync * additional information or have any questions.
59190ecd61435d19ba3515b876272aee7bd12298vboxsync */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#define LOG_GROUP LOG_GROUP_DEV_AUDIO
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#define AUDIO_CAP "sniffer"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <VBox/pdm.h>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <VBox/err.h>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <VBox/log.h>
78bdce9b23466b28a538c6e2f69c225d1393eb3bvboxsync#include <iprt/assert.h>
78bdce9b23466b28a538c6e2f69c225d1393eb3bvboxsync#include <iprt/uuid.h>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <iprt/string.h>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <iprt/alloc.h>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include "Builtins.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include "../../vl_vbox.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include "audio.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include "audio_int.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsynctypedef struct _AUDIOSNIFFERSTATE
59190ecd61435d19ba3515b876272aee7bd12298vboxsync{
bbc0a3fc49446bf8fa1fcfe669c10875701692bcvboxsync /** If the device is enabled. */
bbc0a3fc49446bf8fa1fcfe669c10875701692bcvboxsync bool fEnabled;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
bbc0a3fc49446bf8fa1fcfe669c10875701692bcvboxsync /** Whether audio should reach the host driver too. */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync bool fKeepHostAudio;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync /** Pointer to device instance. */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync PPDMDEVINS pDevIns;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync /** Audio Sniffer port base interface. */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync PDMIBASE Base;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync /** Audio Sniffer port interface. */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync PDMIAUDIOSNIFFERPORT Port;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync /** Pointer to base interface of the driver. */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync PPDMIBASE pDrvBase;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync /** Audio Sniffer connector interface */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync PPDMIAUDIOSNIFFERCONNECTOR pDrv;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync} AUDIOSNIFFERSTATE;
a01baba2de7ca040689fb847c5e6b5e282e10fc4vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic AUDIOSNIFFERSTATE *g_pData = NULL;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync/*
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * Public sniffer callbacks to be called from audio driver.
59190ecd61435d19ba3515b876272aee7bd12298vboxsync */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync/* *** Subject to change ***
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * Process audio output. The function is called when an audio output
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * driver is about to play audio samples.
59190ecd61435d19ba3515b876272aee7bd12298vboxsync *
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * It is expected that there is only one audio data flow,
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * i.e. one voice.
772269936494ffaddd0750ba9e28e805ba81398cvboxsync *
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * @param hw Audio samples information.
* @param pvSamples Pointer to audio samples.
* @param cSamples Number of audio samples in the buffer.
* @returns 'true' if audio also to be played back by the output driver.
* 'false' if audio should not be played.
*/
DECLCALLBACK(bool) sniffer_run_out (HWVoiceOut *hw, void *pvSamples, unsigned cSamples)
{
int samplesPerSec;
int nChannels;
int bitsPerSample;
bool fUnsigned;
if (!g_pData || !g_pData->pDrv || !g_pData->fEnabled)
{
return true;
}
samplesPerSec = hw->info.freq;
nChannels = hw->info.nchannels;
bitsPerSample = hw->info.bits;
fUnsigned = (hw->info.sign == 0);
g_pData->pDrv->pfnAudioSamplesOut (g_pData->pDrv, pvSamples, cSamples,
samplesPerSec, nChannels, bitsPerSample, fUnsigned);
return g_pData->fKeepHostAudio;
}
/*
* Audio Sniffer PDM device.
*/
static DECLCALLBACK(int) iface_Setup (PPDMIAUDIOSNIFFERPORT pInterface, bool fEnable, bool fKeepHostAudio)
{
AUDIOSNIFFERSTATE *pThis = RT_FROM_MEMBER(pInterface, AUDIOSNIFFERSTATE, Port);
Assert(g_pData == pThis);
pThis->fEnabled = fEnable;
pThis->fKeepHostAudio = fKeepHostAudio;
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
static DECLCALLBACK(void *) iface_QueryInterface(PPDMIBASE pInterface, const char *pszIID)
{
AUDIOSNIFFERSTATE *pThis = RT_FROM_MEMBER(pInterface, AUDIOSNIFFERSTATE, Base);
if (RTUuidCompare2Strs(pszIID, PDMIBASE_IID) == 0)
return &pThis->Base;
if (RTUuidCompare2Strs(pszIID, PDMINTERFACE_AUDIO_SNIFFER_PORT) == 0)
return &pThis->Port;
return NULL;
}
/**
* Construct a device instance for a VM.
*
* @returns VBox status.
* @param pDevIns The device instance data.
* If the registration structure is needed, pDevIns->pDevReg points to it.
* @param iInstance Instance number. Use this to figure out which registers and such to use.
* The device number is also found in pDevIns->iInstance, but since it's
* likely to be freqently used PDM passes it as parameter.
* @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
* of the device instance. It's also found in pDevIns->pCfgHandle, but like
* iInstance it's expected to be used a bit in this function.
*/
static DECLCALLBACK(int) audioSnifferR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
{
int rc = VINF_SUCCESS;
AUDIOSNIFFERSTATE *pThis = PDMINS_2_DATA(pDevIns, AUDIOSNIFFERSTATE *);
Assert(iInstance == 0);
/*
* Validate configuration.
*/
if (!CFGMR3AreValuesValid(pCfgHandle, "\0"))
{
return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
}
/*
* Initialize data.
*/
pThis->fEnabled = false;
pThis->fKeepHostAudio = true;
pThis->pDrv = NULL;
/*
* Interfaces
*/
/* Base */
pThis->Base.pfnQueryInterface = iface_QueryInterface;
/* Audio Sniffer port */
pThis->Port.pfnSetup = iface_Setup;
/*
* Get the corresponding connector interface
*/
rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->Base, &pThis->pDrvBase, "Audio Sniffer Port");
if (RT_SUCCESS(rc))
{
pThis->pDrv = (PPDMIAUDIOSNIFFERCONNECTOR)pThis->pDrvBase->pfnQueryInterface(pThis->pDrvBase, PDMINTERFACE_AUDIO_SNIFFER_CONNECTOR);
if (!pThis->pDrv)
{
AssertMsgFailed(("LUN #0 doesn't have a Audio Sniffer connector interface rc=%Rrc\n", rc));
rc = VERR_PDM_MISSING_INTERFACE;
}
}
else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
{
Log(("%s/%d: warning: no driver attached to LUN #0.\n", pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
rc = VINF_SUCCESS;
}
else
{
AssertMsgFailed(("Failed to attach LUN #0. rc=%Rrc\n", rc));
}
if (RT_SUCCESS (rc))
{
/* Save PDM device instance data for future reference. */
pThis->pDevIns = pDevIns;
/* Save the pointer to created instance in the global variable, so other
* functions could reach it.
*/
g_pData = pThis;
}
return rc;
}
/**
* Destruct a device instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
static DECLCALLBACK(int) audioSnifferR3Destruct(PPDMDEVINS pDevIns)
{
/* Zero the global pointer. */
g_pData = NULL;
return VINF_SUCCESS;
}
/**
* The Audio Sniffer device registration structure.
*/
const PDMDEVREG g_DeviceAudioSniffer =
{
/* u32Version */
PDM_DEVREG_VERSION,
/* szDeviceName */
"AudioSniffer",
/* szRCMod */
"",
/* szR0Mod */
"",
/* pszDescription */
"Audio Sniffer device. Redirects audio data to sniffer driver.",
/* fFlags */
PDM_DEVREG_FLAGS_DEFAULT_BITS,
/* fClass */
PDM_DEVREG_CLASS_AUDIO,
/* cMaxInstances */
1,
/* cbInstance */
sizeof(AUDIOSNIFFERSTATE),
/* pfnConstruct */
audioSnifferR3Construct,
/* pfnDestruct */
audioSnifferR3Destruct,
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
PDM_DEVREG_VERSION
};