forked from per-gron/reacdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREACDevice.cpp
More file actions
executable file
·268 lines (215 loc) · 8.86 KB
/
Copy pathREACDevice.cpp
File metadata and controls
executable file
·268 lines (215 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* REACDevice.cpp
* REAC
*
* Copyright 2011 Per Eckerdal. All rights reserved.
*
*
* This file is part of the OS X REAC driver.
*
* The OS X REAC driver is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* The OS X REAC driver is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OS X REAC driver. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "REACDevice.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>
#include <net/kpi_interface.h>
#include "REACAudioEngine.h"
#define super IOAudioDevice
OSDefineMetaClassAndStructors(REACDevice, super)
bool REACDevice::init(OSDictionary *properties) {
protocols = OSArray::withCapacity(5);
if (NULL == protocols) {
return false;
}
return super::init(properties);
}
bool REACDevice::initHardware(IOService *provider)
{
bool result = false;
// IOLog("REACDevice[%p]::initHardware(%p)\n", this, provider);
if (!super::initHardware(provider))
goto Done;
setDeviceName("REAC by Per Eckerdal");
setDeviceShortName("REAC by Per Eckerdal");
setManufacturerName("Per Eckerdal");
if (!createProtocolListeners())
goto Done;
result = true;
Done:
return result;
}
void REACDevice::stop(IOService *provider)
{
super::stop(provider);
protocols->flushCollection();
}
void REACDevice::free() {
if (NULL != protocols) {
protocols->release();
}
super::free();
}
bool REACDevice::createProtocolListeners() {
OSArray *interfaceArray = OSDynamicCast(OSArray, getProperty(INTERFACES_KEY));
OSCollectionIterator *interfaceIterator;
OSDictionary *interfaceDict;
if (!interfaceArray) {
IOLog("REACDevice[%p]::createProtocolListeners() - Error: no Interface array in personality.\n", this);
return false;
}
interfaceIterator = OSCollectionIterator::withCollection(interfaceArray);
if (!interfaceIterator) {
IOLog("REACDevice: no interfaces to listen to available.\n");
return true;
}
while ((interfaceDict = (OSDictionary*)interfaceIterator->getNextObject())) {
OSString *ifname = OSDynamicCast(OSString, interfaceDict->getObject(INTERFACE_NAME_KEY));
REACConnection *protocol = NULL;
ifnet_t interface;
if (NULL == ifname) {
IOLog("REACDevice: Invalid interface entry in personality (no string name).\n");
goto Next;
}
if (0 != ifnet_find_by_name(ifname->getCStringNoCopy(), &interface)) {
IOLog("REACDevice[%p]::createProtocolListeners() - Error: failed to find interface '%s'.\n",
this, ifname->getCStringNoCopy());
goto Next;
}
protocol = REACConnection::withInterface(getWorkLoop(),
interface,
REACConnection::REAC_SPLIT,
&REACDevice::connectionCallback,
&REACDevice::samplesCallback,
&REACDevice::getSamplesCallback,
this, // Cookie A (the REACAudioDevice)
NULL, // Cookie B (the REACAudioEngine)
16, // inChannels (in REAC_MASTER mode)
8); // outChannels (in REAC_MASTER mode)
ifnet_release(interface);
if (NULL == protocol) {
IOLog("REACDevice[%p]::createProtocolListeners() - Error: failed to initialize REAC listener for '%s'.\n",
this, ifname->getCStringNoCopy());
goto Next;
}
if (!protocol->start()) {
IOLog("REACDevice[%p]::createProtocolListeners() - Error: failed to listen to '%s'.\n",
this, ifname->getCStringNoCopy());
goto Next;
}
if (!protocols->setObject(protocol)) {
IOLog("REACDevice[%p]::createProtocolListeners(): Failed to insert protocol listener '%s' into array.\n",
this, ifname->getCStringNoCopy());
goto Next;
}
Next:
if (NULL != protocol) {
protocol->release();
}
}
interfaceIterator->release();
return true;
}
void REACDevice::connectionCallback(REACConnection *proto, void **cookieA, void** cookieB, REACDeviceInfo *deviceInfo) {
REACDevice *device = (REACDevice*) *cookieA;
if (NULL == *cookieB) {
*cookieB = (void*) device->createAudioEngine(proto);
}
return; // TODO Debug
REACAudioEngine *engine = (REACAudioEngine*) *cookieB;
// We need to stop the audio engine regardless of whether it's a connect or a disconnect;
// when connecting, we want to make sure to stop any old instance just in case.
if (NULL != engine) {
engine->stopAudioEngine();
*cookieB = NULL;
}
if (NULL == deviceInfo) {
IOLog("REACDevice[%p]::connectionCallback() - Disconnected.\n", device);
}
else {
// IOLog("REACDevice[%p]::connectionCallback() - Connected.\n", device);
*cookieB = (void*) device->createAudioEngine(proto);
}
}
void REACDevice::samplesCallback(REACConnection *proto, void **cookieA, void** cookieB, UInt8 **data, UInt32 *bufferSize) {
// IOLog("REACDevice[%p]::samplesCallback()\n", *cookieA);
REACAudioEngine *engine = (REACAudioEngine *)*cookieB;
if (NULL != engine) {
engine->gotSamples(data, bufferSize);
}
}
void REACDevice::getSamplesCallback(REACConnection *proto, void **cookieA, void** cookieB, UInt8 **data, UInt32 *bufferSize) {
// IOLog("REACDevice[%p]::samplesCallback()\n", *cookieA);
REACAudioEngine *engine = (REACAudioEngine *)*cookieB;
if (NULL != engine) {
engine->getSamples(data, bufferSize);
}
}
REACAudioEngine* REACDevice::createAudioEngine(REACConnection *proto) {
OSDictionary *originalAudioEngineParams = OSDynamicCast(OSDictionary, getProperty(AUDIO_ENGINE_PARAMS_KEY));
OSDictionary *audioEngineParams = NULL;
ifnet_t interface = proto->getInterface();
u_int32_t unitNumber = ifnet_unit(interface);
const char* ifname = ifnet_name(interface);
if (!originalAudioEngineParams) {
IOLog("REACDevice[%p]::createAudioEngine() - Error: no AudioEngine parameters in personality.\n", this);
return NULL;
}
audioEngineParams = OSDynamicCast(OSDictionary, originalAudioEngineParams->copyCollection());
if (NULL == audioEngineParams) {
return NULL;
}
OSString* desc = OSDynamicCast(OSString, audioEngineParams->getObject(DESCRIPTION_KEY));
if (NULL != desc) {
char buf[100];
snprintf(buf, sizeof(buf), "%s (%s%d)", desc->getCStringNoCopy(), ifname, unitNumber);
OSString* newName = OSString::withCStringNoCopy(buf);
if (newName) {
audioEngineParams->setObject(DESCRIPTION_KEY, newName);
newName->release();
}
}
REACAudioEngine* audioEngine = new REACAudioEngine;
if (!audioEngine) {
goto Done;
}
if (!audioEngine->init(proto, audioEngineParams)) {
IOLog("REACDevice[%p]::createAudioEngine() - Error: Failed to init audio engine.\n", this);
audioEngine->release();
audioEngine = NULL;
goto Done;
}
if (kIOReturnSuccess != activateAudioEngine(audioEngine)) { // increments refcount and manages the object
IOLog("REACDevice[%p]::createAudioEngine() - Error: Failed to activate audio engine.\n", this);
audioEngine->release();
audioEngine = NULL;
goto Done;
}
audioEngine->release(); // decrement refcount so object is released when the manager eventually releases it
Done:
if (NULL != audioEngineParams) {
audioEngineParams->release();
}
return audioEngine;
}
IOReturn REACDevice::performPowerStateChange(IOAudioDevicePowerState oldPowerState,
IOAudioDevicePowerState newPowerState,
UInt32 *microsecondsUntilComplete) {
// TODO
return kIOReturnSuccess;
}