[653] | 1 | #include "Python.h"
|
---|
| 2 | #include <AudioToolbox/AudioServices.h>
|
---|
| 3 |
|
---|
[657] | 4 | #define FourCC2Str(code) (char[5]){(code >> 24) & 0xFF, (code >> 16) & 0xFF, (code >> 8) & 0xFF, code & 0xFF, 0}
|
---|
| 5 |
|
---|
[654] | 6 | static PyObject *
|
---|
[657] | 7 | OSError_from_HALError(const char *failed_operation, OSStatus err) {
|
---|
| 8 | // these error codes are actually mnemonic, so display them
|
---|
| 9 | return PyErr_Format(PyExc_OSError,
|
---|
| 10 | "%s failed (%ld - %s)",
|
---|
| 11 | failed_operation, (long)err, FourCC2Str(err));
|
---|
| 12 | }
|
---|
| 13 |
|
---|
[662] | 14 | #ifndef kAudioDeviceTransportTypeAirPlay
|
---|
| 15 | enum { kAudioDeviceTransportTypeAirPlay = 'airp' }; /* 10.8+ */
|
---|
| 16 | #endif
|
---|
| 17 |
|
---|
[657] | 18 | static PyObject *
|
---|
[654] | 19 | AudioDevice_default_output_device_is_airplay(PyObject *self, PyObject *args) {
|
---|
[653] | 20 | AudioObjectPropertyAddress propertyAddress;
|
---|
| 21 | propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
|
---|
| 22 | propertyAddress.mElement = kAudioObjectPropertyElementMaster;
|
---|
| 23 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
---|
| 24 |
|
---|
| 25 | AudioDeviceID deviceID = kAudioDeviceUnknown;
|
---|
| 26 | UInt32 size = sizeof(deviceID);
|
---|
| 27 | OSStatus err;
|
---|
| 28 |
|
---|
| 29 | err = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject,
|
---|
| 30 | &propertyAddress, 0, NULL,
|
---|
| 31 | &size, &deviceID);
|
---|
| 32 | if (err != noErr)
|
---|
[657] | 33 | return OSError_from_HALError("AudioHardwareServiceGetPropertyData", err);
|
---|
[653] | 34 |
|
---|
| 35 | if (deviceID == kAudioDeviceUnknown)
|
---|
| 36 | Py_RETURN_NONE;
|
---|
| 37 |
|
---|
| 38 | UInt32 transportType;
|
---|
| 39 | propertyAddress.mSelector = kAudioDevicePropertyTransportType,
|
---|
| 40 | err = AudioObjectGetPropertyData(deviceID,
|
---|
| 41 | &propertyAddress, 0, NULL,
|
---|
| 42 | &size, &transportType);
|
---|
[657] | 43 | if (err == kAudioHardwareBadObjectError)
|
---|
| 44 | Py_RETURN_NONE;
|
---|
[653] | 45 | if (err != noErr)
|
---|
[657] | 46 | return OSError_from_HALError("AudioObjectGetPropertyData", err);
|
---|
[653] | 47 |
|
---|
| 48 | if (transportType == kAudioDeviceTransportTypeAirPlay)
|
---|
[657] | 49 | Py_RETURN_TRUE;
|
---|
[653] | 50 | else
|
---|
[657] | 51 | Py_RETURN_FALSE;
|
---|
[653] | 52 | }
|
---|
| 53 |
|
---|
[654] | 54 | static PyObject *default_output_device_changed_callback = NULL;
|
---|
[653] | 55 |
|
---|
[654] | 56 | OSStatus
|
---|
| 57 | output_device_changed_listener(AudioObjectID inObjectID,
|
---|
| 58 | UInt32 inNumberAddresses,
|
---|
| 59 | const AudioObjectPropertyAddress inAddresses[],
|
---|
| 60 | void *inClientData) {
|
---|
| 61 | PyGILState_STATE gstate = PyGILState_Ensure();
|
---|
| 62 | PyObject_CallObject(default_output_device_changed_callback, NULL);
|
---|
| 63 | PyGILState_Release(gstate);
|
---|
| 64 |
|
---|
| 65 | return noErr;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | static PyObject *
|
---|
| 69 | AudioDevice_set_default_output_device_changed_callback(PyObject *self,
|
---|
| 70 | PyObject *args) {
|
---|
| 71 | PyObject *new_callback;
|
---|
| 72 | if (!PyArg_ParseTuple(args, "O", &new_callback))
|
---|
| 73 | return NULL;
|
---|
| 74 |
|
---|
| 75 | if (!PyCallable_Check(new_callback)) {
|
---|
| 76 | PyErr_SetString(PyExc_TypeError, "parameter must be callable");
|
---|
| 77 | return NULL;
|
---|
| 78 | }
|
---|
| 79 | Py_INCREF(new_callback);
|
---|
| 80 | if (default_output_device_changed_callback == NULL) {
|
---|
| 81 | AudioObjectPropertyAddress propertyAddress;
|
---|
| 82 | propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
|
---|
| 83 | propertyAddress.mElement = kAudioObjectPropertyElementMaster;
|
---|
| 84 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
---|
| 85 |
|
---|
| 86 | OSStatus err;
|
---|
| 87 | err = AudioObjectAddPropertyListener(kAudioObjectSystemObject,
|
---|
| 88 | &propertyAddress,
|
---|
| 89 | &output_device_changed_listener,
|
---|
| 90 | NULL);
|
---|
| 91 | if (err != noErr)
|
---|
[657] | 92 | return OSError_from_HALError("AudioObjectAddPropertyListener", err);
|
---|
[654] | 93 | } else {
|
---|
| 94 | Py_DECREF(default_output_device_changed_callback);
|
---|
| 95 | }
|
---|
| 96 | default_output_device_changed_callback = new_callback;
|
---|
| 97 |
|
---|
| 98 | Py_RETURN_NONE;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[653] | 101 | static PyMethodDef AudioDevicemodule_methods[] = {
|
---|
| 102 | {"default_output_device_is_airplay",
|
---|
| 103 | AudioDevice_default_output_device_is_airplay, METH_NOARGS,
|
---|
| 104 | "default_output_device() -> bool or None\n\n"
|
---|
| 105 | "Return whether the default CoreAudio output device is an AirPlay device."},
|
---|
[654] | 106 | {"set_default_output_device_changed_callback",
|
---|
| 107 | AudioDevice_set_default_output_device_changed_callback, METH_VARARGS,
|
---|
| 108 | "set_default_output_device_changed_callback(callable)\n\n"
|
---|
| 109 | "Set a callback invoked when the default CoreAudio output device changes."},
|
---|
[653] | 110 | {NULL, NULL, 0, NULL}
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | PyMODINIT_FUNC
|
---|
| 114 | initAudioDevice(void) {
|
---|
| 115 | (void)Py_InitModule("AudioDevice", AudioDevicemodule_methods);
|
---|
| 116 | }
|
---|