Newer
Older

Vincent Lucas
committed
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
#include "device.h"
#include <initguid.h>
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <propkeydef.h> // Must be defined after windows.h
#include <commctrl.h> // Must be defined after mmdeviceapi.h

Lyubomir Marinov
committed
#include <endpointvolume.h> // Must be defined after mmdeviceapi.h

Vincent Lucas
committed
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
/**
* Functions to list, access and modifies audio devices via coreaudio.
*
* @author Vincent Lucas
*/
/**
* Private definition of functions,
*/
IAudioEndpointVolume * getEndpointVolume(
const char * deviceUID);
void freeEndpointVolume(
IAudioEndpointVolume * endpointVolume);
int setDeviceVolume(
const char * deviceUID,
float volume);
float getDeviceVolume(
const char * deviceUID);
DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14); // DEVPROP_TYPE_STRING
/**
* Initializes the COM component. This function must be called first in order to
* able each following function to work correctly. Once finished, the caller of
* this function must call the "freeDevices" function.
*
* @return 0 if everything is OK. -1 if an error has occured.
*/
int initDevices(void)
{
if(CoInitializeEx(NULL, COINIT_MULTITHREADED) != S_OK)
{
fprintf(stderr,
"initDevices (coreaudio/device.c): \
\n\tCoInitialize\n");
fflush(stderr);
return -1;
}
return 0;
}
/**
* Frees the resources used by the COM component. This function must be called
* last.
*/
void freeDevices(void)
{
CoUninitialize();
}
/**
* Returns the audio device corresponding to the UID given in parameter. Or
* NULL if the device is nonexistant or if anything as failed. The device must
* be freed by the caller of this function.
*
* @param deviceUID The device UID.
*
* @return The audio device corresponding to the UID given in parameter. Or
* NULL if the device is nonexistant or if anything as failed.
*/
IMMDevice * getDevice(
const char * deviceUID)
{
// Gets the enumerator of the system devices.

Lyubomir Marinov
committed
HRESULT err;

Vincent Lucas
committed
IMMDeviceEnumerator * enumerator = NULL;
if((err = CoCreateInstance(

Lyubomir Marinov
committed
CLSID_MMDeviceEnumerator,

Vincent Lucas
committed
NULL,
CLSCTX_ALL,

Lyubomir Marinov
committed
IID_IMMDeviceEnumerator,

Vincent Lucas
committed
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
(void**) &enumerator))
!= S_OK)
{
fprintf(stderr,
"getDevice (coreaudio/device.c): \
\n\tCoCreateInstance\n");
fflush(stderr);
if(err == REGDB_E_CLASSNOTREG)
{
fprintf(stderr,
"getDevice (coreaudio/device.c): \
\n\tCoCreateInstance: REGDB_E_CLASSNOTREG\n");
fflush(stderr);
}
else if(err == CLASS_E_NOAGGREGATION)
{
fprintf(stderr,
"getDevice (coreaudio/device.c): \
\n\tCoCreateInstance: CLASS_E_NOAGGREGATION\n");
fflush(stderr);
}
else if(err == E_NOINTERFACE)
{
fprintf(stderr,
"getDevice (coreaudio/device.c): \
\n\tCoCreateInstance: E_NOINTERFACE\n");
fflush(stderr);
}
else if(err == E_POINTER)
{
fprintf(stderr,
"getDevice (coreaudio/device.c): \
\n\tCoCreateInstance: E_POINTER\n");
fflush(stderr);
}
return NULL;
}
// Gets the requested device selected by its UID.
IMMDevice *device = NULL;
size_t deviceUIDLength = strlen(deviceUID);
wchar_t wCharDeviceUID[deviceUIDLength + 1];
if(mbstowcs(wCharDeviceUID, deviceUID, deviceUIDLength + 1)
!= deviceUIDLength)
{
fprintf(stderr,
"getDevice (coreaudio/device.c): \
\n\tmbstowcs\n");
fflush(stderr);
return NULL;
}
if(enumerator->GetDevice(wCharDeviceUID, &device) != S_OK)
{
fprintf(stderr,
"getDevice (coreaudio/device.c): \
\n\tIMMDeviceEnumerator.GetDevice\n");
fflush(stderr);
return NULL;
}
return device;
}
/**
* Frees an audio device returned by the function getDevice.
*
* @param device The audio device
*/
void freeDevice(
IMMDevice * device)
{
device->Release();
}
/**
* Returns the audio device volume endpoint corresponding to the UID given in
* parameter. Or NULL if the endpoint is nonexistant or if anything as failed.
* The endpoint must be freed by the caller of this function.
*
* @param deviceUID The device UID.
*
* @return the audio device volume endpoint corresponding to the UID given in
* parameter. Or NULL if the endpoint is nonexistant or if anything as failed.
*/
IAudioEndpointVolume * getEndpointVolume(
const char * deviceUID)
{
// Gets the device corresponding to its UID.
IMMDevice * device = getDevice(deviceUID);
if(device == NULL)
{
fprintf(stderr,
"getEndpointVolume (coreaudio/device.c): \
\n\tgetDevice\n");
fflush(stderr);
return NULL;
}
// retrives the volume endpoint.
IAudioEndpointVolume *endpointVolume = NULL;
if(device->Activate(
IID_IAudioEndpointVolume,
CLSCTX_ALL,
NULL,
(void**) &endpointVolume) != S_OK)
{
fprintf(stderr,
"getEndpointVolume (coreaudio/device.c): \
\n\tIMMDevice.Activate\n");
fflush(stderr);
return NULL;
}
// Frees the device.
freeDevice(device);
return endpointVolume;
}
/**
* Frees an audio device volume endpoint returned by the function
* getEndpointVolume.
*
* @param endpointVolume The audio device volume endpoint.
*/
void freeEndpointVolume(
IAudioEndpointVolume * endpointVolume)
{
endpointVolume->Release();
}
/**
* Returns the device name for the given device. Or NULL, if not available. The
* returned string must be freed by the caller.
*
* @param device The device to get the name from.
*
* @return The device name for the given device. Or NULL, if not available. The
* returned string must be freed by the caller.
*/
char* getDeviceName(
const char * deviceUID)
{
size_t deviceNameLength;
char * deviceName = NULL;
PROPVARIANT propertyDeviceName;
PropVariantInit(&propertyDeviceName);
IPropertyStore * properties = NULL;
// Gets the audio device.
IMMDevice * device = getDevice(deviceUID);
if(device == NULL)
{
fprintf(stderr,
"getDeviceName (coreaudio/device.c): \
\n\tgetDevice\n");
fflush(stderr);
return NULL;
}
// Read the properties from the audio device.
if(device->OpenPropertyStore(STGM_READ, &properties) != S_OK)
{
fprintf(stderr,
"getDeviceName (coreaudio/device.c): \
\n\tIMMDevice.OpenPropertyStore\n");
fflush(stderr);
return NULL;
}
if(properties->GetValue(PKEY_Device_FriendlyName, &propertyDeviceName)
!= S_OK)
{
fprintf(stderr,
"getDeviceName (coreaudio/device.c): \
\n\tIPropertyStore.GetValue\n");
fflush(stderr);
return NULL;
}
deviceNameLength = wcslen(propertyDeviceName.pwszVal);
if((deviceName = (char *) malloc((deviceNameLength + 1) * sizeof(char)))
== NULL)
{
fprintf(stderr,
"getDeviceName (coreaudio/device.c): \
\n\tmalloc\n");
fflush(stderr);
return NULL;
}
if(wcstombs(deviceName, propertyDeviceName.pwszVal, deviceNameLength + 1)
!= deviceNameLength)
{
fprintf(stderr,
"getDeviceName (coreaudio/device.c): \
\n\twcstombs\n");
fflush(stderr);
return NULL;
}
// Frees.
freeDevice(device);
PropVariantClear(&propertyDeviceName);
return deviceName;
}
/**
* Sets the input volume for a given device.
*
* @param deviceUID The device UID which volume must be changed.
* @param volume The new volume of the device. This is a scalar value between
* 0.0 and 1.0
*
* @return 0 if everything works well. -1 if an error has occured.
*/
int setInputDeviceVolume(
const char * deviceUID,
float volume)
{
return setDeviceVolume(deviceUID, volume);
}
/**
* Sets the output volume for a given device.
*
* @param deviceUID The device UID which volume must be changed.
* @param volume The new volume of the device. This is a scalar value between
* 0.0 and 1.0
*
* @return 0 if everything works well. -1 if an error has occured.
*/
int setOutputDeviceVolume(
const char * deviceUID,
float volume)
{
return setDeviceVolume(deviceUID, volume);
}
/**
* Sets the input or output volume for a given device. This is an internal
* (private) function and must only be called by setInputDeviceVolume or
* setOutputDeviceVolume.
*
* @param deviceUID The device UID which volume must be changed.
* @param volume The new volume of the device. This is a scalar value between
* 0.0 and 1.0
* @param inputOutputScope The scope to tell if this is an output or an input
* device.
*
* @return 0 if everything works well. -1 if an error has occured.
*/
int setDeviceVolume(
const char * deviceUID,
float volume)
{
IAudioEndpointVolume *endpointVolume = getEndpointVolume(deviceUID);
if(endpointVolume == NULL)
{
fprintf(stderr,
"setDeviceVolume (coreaudio/device.c): \
\n\tgetEndpointVolume\n");
fflush(stderr);
return -1;
}
if(endpointVolume->SetMasterVolumeLevelScalar(volume, NULL) != S_OK)
{
fprintf(stderr,
"setDeviceVolume (coreaudio/device.c): \
\n\tSetMasterVolumeLevelScalar\n");
fflush(stderr);
return -1;
}
freeEndpointVolume(endpointVolume);
return 0;
}
/**
* Gets the input volume for a given device.
*
* @param deviceUID The device UID to get volume from.
*
* @return The device volume as a scalar value between 0.0 and 1.0. Returns -1.0
* if an error occurs.
*/
float getInputDeviceVolume(
const char * deviceUID)
{
return getDeviceVolume(deviceUID);
}
/**
* Gets the output volume for a given device.
*
* @param deviceUID The device UID to get volume from.
*
* @return The device volume as a scalar value between 0.0 and 1.0. Returns -1.0
* if an error occurs.
*/
float getOutputDeviceVolume(
const char * deviceUID)
{
return getDeviceVolume(deviceUID);
}
/**
* Gets the input or output volume for a given device. This is an internal
* (private) function and must only be called by getInputDeviceVolume or
* getOutputDeviceVolume.
*
* @param deviceUID The device UID to get volume from.
*
* @return The device volume as a scalar value between 0.0 and 1.0. Returns -1.0
* if an error occurs.
*/
float getDeviceVolume(
const char * deviceUID)
{
float volume;
IAudioEndpointVolume *endpointVolume = getEndpointVolume(deviceUID);
if(endpointVolume == NULL)
{
fprintf(stderr,
"getDeviceVolume (coreaudio/device.c): \
\n\tgetEndpointVolume\n");
fflush(stderr);
return -1;
}
if(endpointVolume->GetMasterVolumeLevelScalar(&volume) != S_OK)
{
fprintf(stderr,
"getDeviceVolume (coreaudio/device.c): \
\n\tGetMasterVolumeLevelScalar\n");
fflush(stderr);
return -1;
}
freeEndpointVolume(endpointVolume);
return volume;
}