summaryrefslogtreecommitdiffstats
path: root/src/multimedia/darwin/qmacosaudiodatautils_p.h
blob: 8cc2f8440158b0fb65a544d4dd9111fbfd0eaec7 (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QMACOSAUDIODATAUTILS_P_H
#define QMACOSAUDIODATAUTILS_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <CoreAudio/AudioHardware.h>

#include "qaudiodevice.h"
#include "qdebug.h"

#include <optional>
#include <vector>
#include <algorithm>

QT_BEGIN_NAMESPACE

template<typename... Args>
void printUnableToReadWarning(const char *logName, AudioObjectID objectID, const AudioObjectPropertyAddress &address, Args &&...args)
{
    if (!logName)
        return;

    char scope[5] = {0};
    memcpy(&scope, &address.mScope, 4);
    std::reverse(scope, scope + 4);

    auto warn = qWarning();
    warn << "Unable to read property" << logName << "for object" << objectID << ", scope" << scope << ";";
    (warn << ... << args);
    warn << "\n  If the warning is unexpected use test_audio_config to get comprehensive audio info and report a bug";
}

inline static AudioObjectPropertyAddress
makePropertyAddress(AudioObjectPropertySelector selector, QAudioDevice::Mode mode,
                    AudioObjectPropertyElement element = kAudioObjectPropertyElementMain)
{
    return { selector,
             mode == QAudioDevice::Input ? kAudioDevicePropertyScopeInput
                                         : kAudioDevicePropertyScopeOutput,
             element };
}

inline static bool getAudioData(AudioObjectID objectID, const AudioObjectPropertyAddress &address,
                                void *dst, UInt32 dstSize, const char *logName)
{
    UInt32 readBytes = dstSize;
    const auto res = AudioObjectGetPropertyData(objectID, &address, 0, nullptr, &readBytes, dst);

    if (res != noErr)
        printUnableToReadWarning(logName, objectID, address, "Err:", res);
    else if (readBytes != dstSize)
        printUnableToReadWarning(logName, objectID, address, "Data size", readBytes, "VS", dstSize,
                                 "expected");
    else
        return true;

    return false;
}

template<typename T>
std::optional<std::vector<T>> getAudioData(AudioObjectID objectID,
                                           const AudioObjectPropertyAddress &address,
                                           const char *logName, size_t minDataSize = 0)
{
    static_assert(std::is_trivial_v<T>, "A trivial type is expected");

    UInt32 size = 0;
    const auto res = AudioObjectGetPropertyDataSize(objectID, &address, 0, nullptr, &size);

    if (res != noErr) {
        printUnableToReadWarning(logName, objectID, address,
                                 "AudioObjectGetPropertyDataSize failed, Err:", res);
    } else if (size / sizeof(T) < minDataSize) {
        printUnableToReadWarning(logName, objectID, address, "Data size is too small:", size, "VS",
                                 minDataSize * sizeof(T), "bytes");
    } else {
        std::vector<T> data(size / sizeof(T));
        if (getAudioData(objectID, address, data.data(), data.size() * sizeof(T), logName))
            return { std::move(data) };
    }

    return {};
}

template<typename T>
std::optional<T> getAudioObject(AudioObjectID objectID, const AudioObjectPropertyAddress &address,
                                const char *logName)
{
    static_assert(std::is_trivial_v<T>, "A trivial type is expected");

    T object{};
    if (getAudioData(objectID, address, &object, sizeof(T), logName))
        return { object };

    return {};
}

QT_END_NAMESPACE

#endif // QMACOSAUDIODATAUTILS_P_H