1 | #include "Python.h"
|
---|
2 | #include <AudioToolbox/AudioServices.h>
|
---|
3 |
|
---|
4 | static PyObject
|
---|
5 | *AudioDevice_default_output_device_is_airplay(PyObject *self, PyObject *args) {
|
---|
6 | AudioObjectPropertyAddress propertyAddress;
|
---|
7 | propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
|
---|
8 | propertyAddress.mElement = kAudioObjectPropertyElementMaster;
|
---|
9 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
---|
10 |
|
---|
11 | AudioDeviceID deviceID = kAudioDeviceUnknown;
|
---|
12 | UInt32 size = sizeof(deviceID);
|
---|
13 | OSStatus err;
|
---|
14 |
|
---|
15 | err = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject,
|
---|
16 | &propertyAddress, 0, NULL,
|
---|
17 | &size, &deviceID);
|
---|
18 | if (err != noErr)
|
---|
19 | return PyErr_Format(PyExc_OSError,
|
---|
20 | "AudioHardwareServiceGetPropertyData failed (%ld)",
|
---|
21 | (long)err);
|
---|
22 |
|
---|
23 | if (deviceID == kAudioDeviceUnknown)
|
---|
24 | Py_RETURN_NONE;
|
---|
25 |
|
---|
26 | UInt32 transportType;
|
---|
27 | propertyAddress.mSelector = kAudioDevicePropertyTransportType,
|
---|
28 | err = AudioObjectGetPropertyData(deviceID,
|
---|
29 | &propertyAddress, 0, NULL,
|
---|
30 | &size, &transportType);
|
---|
31 | if (err != noErr)
|
---|
32 | return PyErr_Format(PyExc_OSError,
|
---|
33 | "AudioObjectGetPropertyData failed (%ld)",
|
---|
34 | (long)err);
|
---|
35 |
|
---|
36 | if (transportType == kAudioDeviceTransportTypeAirPlay)
|
---|
37 | Py_RETURN_TRUE;
|
---|
38 | else
|
---|
39 | Py_RETURN_FALSE;
|
---|
40 | }
|
---|
41 |
|
---|
42 | // XXX device changed notifications:
|
---|
43 | // http://stackoverflow.com/questions/9674666/
|
---|
44 |
|
---|
45 | static PyMethodDef AudioDevicemodule_methods[] = {
|
---|
46 | {"default_output_device_is_airplay",
|
---|
47 | AudioDevice_default_output_device_is_airplay, METH_NOARGS,
|
---|
48 | "default_output_device() -> bool or None\n\n"
|
---|
49 | "Return whether the default CoreAudio output device is an AirPlay device."},
|
---|
50 | {NULL, NULL, 0, NULL}
|
---|
51 | };
|
---|
52 |
|
---|
53 | PyMODINIT_FUNC
|
---|
54 | initAudioDevice(void) {
|
---|
55 | (void)Py_InitModule("AudioDevice", AudioDevicemodule_methods);
|
---|
56 | }
|
---|