summaryrefslogtreecommitdiffstats
path: root/audio/out/ao_coreaudio_common.c
blob: 289fa93b647773e2e5f453bf614f888454a556c6 (plain)
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
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
/*
 * This file is part of mpv.
 *
 * mpv 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 2 of the License, or
 * (at your option) any later version.
 *
 * mpv 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 mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * This file contains functions interacting with the CoreAudio framework
 * that are not specific to the AUHAL. These are split in a separate file for
 * the sake of readability. In the future the could be used by other AOs based
 * on CoreAudio but not the AUHAL (such as using AudioQueue services).
 */

#include <AudioToolbox/AudioToolbox.h>
#include <AudioUnit/AudioUnit.h>
#include <inttypes.h>
#include <stdbool.h>
#include "core/mp_msg.h"

#define ca_msg(a, b ...) mp_msg(MSGT_AO, a, "AO: [coreaudio] " b)
#define CA_CFSTR_ENCODING kCFStringEncodingASCII

static char *fourcc_repr(void *talloc_ctx, uint32_t code)
{
    // Extract FourCC letters from the uint32_t and finde out if it's a valid
    // code that is made of letters.
    char fcc[4] = {
        (code >> 24) & 0xFF,
        (code >> 16) & 0xFF,
        (code >> 8)  & 0xFF,
        code         & 0xFF,
    };

    bool valid_fourcc = true;
    for (int i = 0; i < 4; i++)
        if (!isprint(fcc[i]))
            valid_fourcc = false;

    char *repr;
    if (valid_fourcc)
        repr = talloc_asprintf(talloc_ctx, "'%c%c%c%c'",
                               fcc[0], fcc[1], fcc[2], fcc[3]);
    else
        repr = talloc_asprintf(NULL, "%d", code);

    return repr;
}

static bool check_ca_st(int level, OSStatus code, const char *message)
{
    if (code == noErr) return true;

    char *error_string = fourcc_repr(NULL, code);
    ca_msg(level, "%s (%s)\n", message, error_string);
    talloc_free(error_string);

    return false;
}

#define CHECK_CA_ERROR_L(label, message) \
    do { \
        if (!check_ca_st(MSGL_ERR, err, message)) { \
            goto label; \
        } \
    } while (0)

#define CHECK_CA_ERROR(message) CHECK_CA_ERROR_L(coreaudio_error, message)
#define CHECK_CA_WARN(message)  check_ca_st(MSGL_WARN, err, message)

static void ca_print_asbd(const char *description,
                          const AudioStreamBasicDescription *asbd)
{
    uint32_t flags  = asbd->mFormatFlags;
    char *format    = fourcc_repr(NULL, asbd->mFormatID);

    ca_msg(MSGL_V,
       "%s %7.1fHz %" PRIu32 "bit [%s]"
       "[%" PRIu32 "][%" PRIu32 "][%" PRIu32 "]"
       "[%" PRIu32 "][%" PRIu32 "] "
       "%s %s %s%s%s%s\n",
       description, asbd->mSampleRate, asbd->mBitsPerChannel, format,
       asbd->mFormatFlags, asbd->mBytesPerPacket, asbd->mFramesPerPacket,
       asbd->mBytesPerFrame, asbd->mChannelsPerFrame,
       (flags & kAudioFormatFlagIsFloat) ? "float" : "int",
       (flags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE",
       (flags & kAudioFormatFlagIsSignedInteger) ? "S" : "U",
       (flags & kAudioFormatFlagIsPacked) ? " packed" : "",
       (flags & kAudioFormatFlagIsAlignedHigh) ? " aligned" : "",
       (flags & kAudioFormatFlagIsNonInterleaved) ? " P" : "");

    talloc_free(format);
}

static OSStatus GetAudioProperty(AudioObjectID id,
                                 AudioObjectPropertySelector selector,
                                 UInt32 outSize, void *outData)
{
    AudioObjectPropertyAddress p_addr;

    p_addr.mSelector = selector;
    p_addr.mScope    = kAudioObjectPropertyScopeGlobal;
    p_addr.mElement  = kAudioObjectPropertyElementMaster;

    return AudioObjectGetPropertyData(id, &p_addr, 0, NULL, &outSize,
                                      outData);
}

static UInt32 GetAudioPropertyArray(AudioObjectID id,
                                    AudioObjectPropertySelector selector,
                                    AudioObjectPropertyScope scope,
                                    void **data)
{
    OSStatus err;
    AudioObjectPropertyAddress p_addr;
    UInt32 p_size;

    p_addr.mSelector = selector;
    p_addr.mScope    = scope;
    p_addr.mElement  = kAudioObjectPropertyElementMaster;

    err = AudioObjectGetPropertyDataSize(id, &p_addr, 0, NULL, &p_size);
    CHECK_CA_ERROR("Can't fetch property size");

    *data = malloc(p_size);

    err = AudioObjectGetPropertyData(id, &p_addr, 0, NULL, &p_size, *data);
    CHECK_CA_ERROR_L(coreaudio_error_free, "Can't fetch property data %s");

    return p_size;

coreaudio_error_free:
    free(*data);
coreaudio_error:
    return 0;
}

static UInt32 GetGlobalAudioPropertyArray(AudioObjectID id,
                                          AudioObjectPropertySelector selector,
                                          void **outData)
{
    return GetAudioPropertyArray(id, selector, kAudioObjectPropertyScopeGlobal,
                                 outData);
}

static OSStatus GetAudioPropertyString(AudioObjectID id,
                                       AudioObjectPropertySelector selector,
                                       char **data)
{
    OSStatus err;
    AudioObjectPropertyAddress p_addr;
    UInt32 p_size = sizeof(CFStringRef);
    CFStringRef string;

    p_addr.mSelector = selector;
    p_addr.mScope    = kAudioObjectPropertyScopeGlobal;
    p_addr.mElement  = kAudioObjectPropertyElementMaster;

    err = AudioObjectGetPropertyData(id, &p_addr, 0, NULL, &p_size, &string);
    CHECK_CA_ERROR("Can't fetch array property");

    CFIndex size =
        CFStringGetMaximumSizeForEncoding(
            CFStringGetLength(string), CA_CFSTR_ENCODING) + 1;

    *data = malloc(size);
    CFStringGetCString(string, *data, size, CA_CFSTR_ENCODING);
    CFRelease(string);
coreaudio_error:
    return err;
}

static OSStatus SetAudioProperty(AudioObjectID id,
                                 AudioObjectPropertySelector selector,
                                 UInt32 inDataSize, void *inData)
{
    AudioObjectPropertyAddress p_addr;

    p_addr.mSelector = selector;
    p_addr.mScope    = kAudioObjectPropertyScopeGlobal;
    p_addr.mElement  = kAudioObjectPropertyElementMaster;

    return AudioObjectSetPropertyData(id, &p_addr, 0, NULL,
                                      inDataSize, inData);
}

static Boolean IsAudioPropertySettable(AudioObjectID id,
                                       AudioObjectPropertySelector selector,
                                       Boolean *outData)
{
    AudioObjectPropertyAddress p_addr;

    p_addr.mSelector = selector;
    p_addr.mScope    = kAudioObjectPropertyScopeGlobal;
    p_addr.mElement  = kAudioObjectPropertyElementMaster;

    return AudioObjectIsPropertySettable(id, &p_addr, outData);
}

static int AudioFormatIsDigital(AudioStreamBasicDescription asbd)
{
    switch (asbd.mFormatID)
    case 'IAC3':
    case 'iac3':
    case  kAudioFormat60958AC3:
    case  kAudioFormatAC3:
        return CONTROL_OK;
    return CONTROL_FALSE;
}

static int AudioStreamSupportsDigital(AudioStreamID stream)
{
    AudioStreamRangedDescription *formats = NULL;

    /* Retrieve all the stream formats supported by each output stream. */
    uint32_t size =
        GetGlobalAudioPropertyArray(stream,
                                    kAudioStreamPropertyAvailablePhysicalFormats,
                                    (void **)&formats);

    if (!size) {
        ca_msg(MSGL_WARN, "Could not get number of stream formats.\n");
        return CONTROL_FALSE;
    }

    const int n_formats = size / sizeof(AudioStreamRangedDescription);
    for (int i = 0; i < n_formats; ++i) {
        AudioStreamBasicDescription asbd = formats[i].mFormat;
        ca_print_asbd("supported format:", &(asbd));
        if (AudioFormatIsDigital(asbd)) {
            free(formats);
            return CONTROL_TRUE;
        }
    }

    free(formats);
    return CONTROL_FALSE;
}

static int AudioDeviceSupportsDigital(AudioDeviceID device)
{
    AudioStreamID *streams = NULL;

    /* Retrieve all the output streams. */
    uint32_t size = GetAudioPropertyArray(device,
                                          kAudioDevicePropertyStreams,
                                          kAudioDevicePropertyScopeOutput,
                                          (void **)&streams);

    if (!size) {
        ca_msg(MSGL_WARN, "could not get number of streams.\n");
        return CONTROL_FALSE;
    }

    const int n_streams = size / sizeof(AudioStreamID);
    for (int i = 0; i < n_streams; ++i) {
        if (AudioStreamSupportsDigital(streams[i])) {
            free(streams);
            return CONTROL_OK;
        }
    }

    free(streams);
    return CONTROL_FALSE;
}

static OSStatus ca_property_listener(AudioObjectPropertySelector selector,
                                     AudioObjectID object, uint32_t n_addresses,
                                     const AudioObjectPropertyAddress addresses[],
                                     void *data)
{
    // TODO: ++i seems wrong in this context. Check out if it was a programmer
    // mistake
    void *talloc_ctx = talloc_new(NULL);

    for (int i = 0; i < n_addresses; ++i) {
        if (addresses[i].mSelector == selector) {
            ca_msg(MSGL_WARN, "event: property %s changed",
                              fourcc_repr(talloc_ctx, selector));
            if (data) *(volatile int *)data = 1;
            break;
        }
    }
    talloc_free(talloc_ctx);
    return noErr;
}

static OSStatus ca_stream_listener(AudioObjectID object, uint32_t n_addresses,
                                   const AudioObjectPropertyAddress addresses[],
                                   void *data)
{
    return ca_property_listener(kAudioStreamPropertyPhysicalFormat,
                                object, n_addresses, addresses, data);
}

static OSStatus ca_device_listener(AudioObjectID object, uint32_t n_addresses,
                                   const AudioObjectPropertyAddress addresses[],
                                   void *data)
{
    return ca_property_listener(kAudioDevicePropertyDeviceHasChanged,
                                object, n_addresses, addresses, data);
}

static OSStatus ca_lock_device(AudioDeviceID device, pid_t *pid) {
    *pid = getpid();
    OSStatus err = SetAudioProperty(device, kAudioDevicePropertyHogMode,
                                    sizeof(*pid), pid);
    if (err != noErr)
        *pid = -1;

    return err;
}

static OSStatus ca_unlock_device(AudioDeviceID device, pid_t *pid) {
    if (*pid == getpid()) {
        *pid = -1;
        return SetAudioProperty(device, kAudioDevicePropertyHogMode,
                                sizeof(*pid), &pid);
    }
    return noErr;
}