Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qmacosaudiodatautils_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QMACOSAUDIODATAUTILS_P_H
5#define QMACOSAUDIODATAUTILS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <CoreAudio/AudioHardware.h>
19
20#include "qaudiodevice.h"
21#include "qdebug.h"
22
23#include <optional>
24#include <vector>
25#include <algorithm>
26
28
29template<typename... Args>
30void printUnableToReadWarning(const char *logName, AudioObjectID objectID, const AudioObjectPropertyAddress &address, Args &&...args)
31{
32 if (!logName)
33 return;
34
35 char scope[5] = {0};
36 memcpy(&scope, &address.mScope, 4);
37 std::reverse(scope, scope + 4);
38
39 auto warn = qWarning();
40 warn << "Unable to read property" << logName << "for object" << objectID << ", scope" << scope << ";";
41 (warn << ... << args);
42 warn << "\n If the warning is unexpected use test_audio_config to get comprehensive audio info and report a bug";
43}
44
45inline static AudioObjectPropertyAddress
47 AudioObjectPropertyElement element = kAudioObjectPropertyElementMain)
48{
49 return { selector,
50 mode == QAudioDevice::Input ? kAudioDevicePropertyScopeInput
51 : kAudioDevicePropertyScopeOutput,
52 element };
53}
54
55inline static bool getAudioData(AudioObjectID objectID, const AudioObjectPropertyAddress &address,
56 void *dst, UInt32 dstSize, const char *logName)
57{
58 UInt32 readBytes = dstSize;
59 const auto res = AudioObjectGetPropertyData(objectID, &address, 0, nullptr, &readBytes, dst);
60
61 if (res != noErr)
62 printUnableToReadWarning(logName, objectID, address, "Err:", res);
63 else if (readBytes != dstSize)
64 printUnableToReadWarning(logName, objectID, address, "Data size", readBytes, "VS", dstSize,
65 "expected");
66 else
67 return true;
68
69 return false;
70}
71
72template<typename T>
73std::optional<std::vector<T>> getAudioData(AudioObjectID objectID,
74 const AudioObjectPropertyAddress &address,
75 const char *logName, size_t minDataSize = 0)
76{
77 static_assert(std::is_trivial_v<T>, "A trivial type is expected");
78
79 UInt32 size = 0;
80 const auto res = AudioObjectGetPropertyDataSize(objectID, &address, 0, nullptr, &size);
81
82 if (res != noErr) {
83 printUnableToReadWarning(logName, objectID, address,
84 "AudioObjectGetPropertyDataSize failed, Err:", res);
85 } else if (size / sizeof(T) < minDataSize) {
86 printUnableToReadWarning(logName, objectID, address, "Data size is too small:", size, "VS",
87 minDataSize * sizeof(T), "bytes");
88 } else {
89 std::vector<T> data(size / sizeof(T));
90 if (getAudioData(objectID, address, data.data(), data.size() * sizeof(T), logName))
91 return { std::move(data) };
92 }
93
94 return {};
95}
96
97template<typename T>
98std::optional<T> getAudioObject(AudioObjectID objectID, const AudioObjectPropertyAddress &address,
99 const char *logName)
100{
101 static_assert(std::is_trivial_v<T>, "A trivial type is expected");
102
103 T object{};
104 if (getAudioData(objectID, address, &object, sizeof(T), logName))
105 return { object };
106
107 return {};
108}
109
111
112#endif // QMACOSAUDIODATAUTILS_P_H
Mode
Describes the mode of this device.
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:166
std::optional< T > getAudioObject(AudioObjectID objectID, const AudioObjectPropertyAddress &address, const char *logName)
static bool getAudioData(AudioObjectID objectID, const AudioObjectPropertyAddress &address, void *dst, UInt32 dstSize, const char *logName)
QT_BEGIN_NAMESPACE void printUnableToReadWarning(const char *logName, AudioObjectID objectID, const AudioObjectPropertyAddress &address, Args &&...args)
static AudioObjectPropertyAddress makePropertyAddress(AudioObjectPropertySelector selector, QAudioDevice::Mode mode, AudioObjectPropertyElement element=kAudioObjectPropertyElementMain)
GLenum mode
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum dst
GLuint res
GLuint GLuint64EXT address
QFileSelector selector
[1]
QJSValueList args