SampleAudioDevice.cpp [plain text]
#include "SampleAudioDevice.h"
#include "SampleAudioEngine.h"
#include <IOKit/audio/IOAudioControl.h>
#include <IOKit/audio/IOAudioLevelControl.h>
#include <IOKit/audio/IOAudioToggleControl.h>
#include <IOKit/audio/IOAudioDefines.h>
#include <IOKit/IOLib.h>
#define super IOAudioDevice
OSDefineMetaClassAndStructors(SampleAudioDevice, IOAudioDevice)
bool SampleAudioDevice::initHardware(IOService *provider)
{
bool result = false;
IOLog("SampleAudioDevice[%p]::initHardware(%p)\n", this, provider);
if (!super::initHardware(provider)) {
goto Done;
}
setDeviceName("Sample Audio Device");
setDeviceShortName("SampleAudio");
setManufacturerName("My Company");
#error Put your own hardware initialization code here...and in other routines!!
if (!createAudioEngine()) {
goto Done;
}
result = true;
Done:
return result;
}
void SampleAudioDevice::free()
{
super::free();
}
bool SampleAudioDevice::createAudioEngine()
{
bool result = false;
SampleAudioEngine *audioEngine = NULL;
IOAudioControl *control;
IOLog("SampleAudioDevice[%p]::createAudioEngine()\n", this);
audioEngine = new SampleAudioEngine;
if (!audioEngine) {
goto Done;
}
if (!audioEngine->init()) {
goto Done;
}
control = IOAudioLevelControl::createVolumeControl(65535, 0, 65535, (-22 << 16) + (32768), 0, kIOAudioControlChannelIDDefaultLeft,
kIOAudioControlChannelNameLeft,
0, kIOAudioControlUsageOutput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)volumeChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioLevelControl::createVolumeControl(65535, 0, 65535, (-22 << 16) + (32768), 0, kIOAudioControlChannelIDDefaultRight, kIOAudioControlChannelNameRight,
0, kIOAudioControlUsageOutput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)volumeChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioToggleControl::createMuteControl(false, kIOAudioControlChannelIDAll, kIOAudioControlChannelNameAll,
0, kIOAudioControlUsageOutput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)outputMuteChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioLevelControl::createVolumeControl(65535, 0, 65535, 0, (22 << 16) + (32768), kIOAudioControlChannelIDDefaultLeft,
kIOAudioControlChannelNameLeft,
0, kIOAudioControlUsageInput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)gainChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioLevelControl::createVolumeControl(65535, 0, 65535, 0, (22 << 16) + (32768), kIOAudioControlChannelIDDefaultRight, kIOAudioControlChannelNameRight,
0, kIOAudioControlUsageInput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)gainChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioToggleControl::createMuteControl(false, kIOAudioControlChannelIDAll, kIOAudioControlChannelNameAll,
0, kIOAudioControlUsageInput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)inputMuteChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
activateAudioEngine(audioEngine);
audioEngine->release();
result = true;
Done:
if (!result && (audioEngine != NULL)) {
audioEngine->release();
}
return result;
}
IOReturn SampleAudioDevice::volumeChangeHandler(IOService *target, IOAudioControl *volumeControl, SInt32 oldValue, SInt32 newValue)
{
IOReturn result = kIOReturnBadArgument;
SampleAudioDevice *audioDevice;
audioDevice = (SampleAudioDevice *)target;
if (audioDevice) {
result = audioDevice->volumeChanged(volumeControl, oldValue, newValue);
}
return result;
}
IOReturn SampleAudioDevice::volumeChanged(IOAudioControl *volumeControl, SInt32 oldValue, SInt32 newValue)
{
IOLog("SampleAudioDevice[%p]::volumeChanged(%p, %ld, %ld)\n", this, volumeControl, oldValue, newValue);
if (volumeControl) {
IOLog("\t-> Channel %ld\n", volumeControl->getChannelID());
}
return kIOReturnSuccess;
}
IOReturn SampleAudioDevice::outputMuteChangeHandler(IOService *target, IOAudioControl *muteControl, SInt32 oldValue, SInt32 newValue)
{
IOReturn result = kIOReturnBadArgument;
SampleAudioDevice *audioDevice;
audioDevice = (SampleAudioDevice *)target;
if (audioDevice) {
result = audioDevice->outputMuteChanged(muteControl, oldValue, newValue);
}
return result;
}
IOReturn SampleAudioDevice::outputMuteChanged(IOAudioControl *muteControl, SInt32 oldValue, SInt32 newValue)
{
IOLog("SampleAudioDevice[%p]::outputMuteChanged(%p, %ld, %ld)\n", this, muteControl, oldValue, newValue);
return kIOReturnSuccess;
}
IOReturn SampleAudioDevice::gainChangeHandler(IOService *target, IOAudioControl *gainControl, SInt32 oldValue, SInt32 newValue)
{
IOReturn result = kIOReturnBadArgument;
SampleAudioDevice *audioDevice;
audioDevice = (SampleAudioDevice *)target;
if (audioDevice) {
result = audioDevice->gainChanged(gainControl, oldValue, newValue);
}
return result;
}
IOReturn SampleAudioDevice::gainChanged(IOAudioControl *gainControl, SInt32 oldValue, SInt32 newValue)
{
IOLog("SampleAudioDevice[%p]::gainChanged(%p, %ld, %ld)\n", this, gainControl, oldValue, newValue);
if (gainControl) {
IOLog("\t-> Channel %ld\n", gainControl->getChannelID());
}
return kIOReturnSuccess;
}
IOReturn SampleAudioDevice::inputMuteChangeHandler(IOService *target, IOAudioControl *muteControl, SInt32 oldValue, SInt32 newValue)
{
IOReturn result = kIOReturnBadArgument;
SampleAudioDevice *audioDevice;
audioDevice = (SampleAudioDevice *)target;
if (audioDevice) {
result = audioDevice->inputMuteChanged(muteControl, oldValue, newValue);
}
return result;
}
IOReturn SampleAudioDevice::inputMuteChanged(IOAudioControl *muteControl, SInt32 oldValue, SInt32 newValue)
{
IOLog("SampleAudioDevice[%p]::inputMuteChanged(%p, %ld, %ld)\n", this, muteControl, oldValue, newValue);
return kIOReturnSuccess;
}