Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qavaudiosessionmanager.mm
Go to the documentation of this file.
1// Copyright (C) 2026 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
5
6#include <QtMultimedia/private/qmultimedia_ranges_p.h>
7
8#include <QtCore/qapplicationstatic.h>
9#include <QtCore/qcoreapplication.h>
10#include <QtCore/qloggingcategory.h>
11#include <QtCore/qthread.h>
12
13#import <AVFoundation/AVAudioSession.h>
14
16
17Q_STATIC_LOGGING_CATEGORY(qLcAudioSession, "qt.multimedia.darwin.audiosession");
18
19namespace QtMultimediaPrivate {
20
22
23namespace {
24
25struct InterruptionInfo
26{
27 bool began;
28 bool shouldResume;
29};
30
31InterruptionInfo parseInterruption(NSNotification *notification)
32{
33 NSDictionary *userInfo = notification.userInfo;
34 NSNumber *typeValue = userInfo[AVAudioSessionInterruptionTypeKey];
35 const auto type = static_cast<AVAudioSessionInterruptionType>(typeValue.unsignedIntegerValue);
36
37 if (type == AVAudioSessionInterruptionTypeBegan) {
38 qCDebug(qLcAudioSession) << "interruption notification: began";
39 return { true, false };
40 }
41
42 // AVAudioSessionInterruptionOptionKey may be absent on "ended"; absence means "do not
43 // resume", only AVAudioSessionInterruptionOptionShouldResume means resume.
44 NSNumber *optionValue = userInfo[AVAudioSessionInterruptionOptionKey];
45 const bool shouldResume = optionValue != nil
46 && (optionValue.unsignedIntegerValue & AVAudioSessionInterruptionOptionShouldResume);
47 qCDebug(qLcAudioSession) << "interruption notification: ended, shouldResume:" << shouldResume;
48 return { false, shouldResume };
49}
50
51// Merges the requirements of all currently live ActivationTokens into a single category/options
52// pair. The manager never chooses a category of its own beyond this merge: if any live client
53// needs recording, the session must support it; options requested by any client (e.g. "mix with
54// others") are unioned in.
55AVAudioSessionCategory
56mergedCategory(const std::vector<std::pair<TokenId, SessionRequirement>> &requirements)
57{
58 const bool needsRecord = ranges::any_of(requirements, [](const auto &entry) {
59 return entry.second.needsRecord;
60 });
61 return needsRecord ? AVAudioSessionCategoryPlayAndRecord : AVAudioSessionCategoryPlayback;
62}
63
64AVAudioSessionCategoryOptions
65mergedOptions(const std::vector<std::pair<TokenId, SessionRequirement>> &requirements)
66{
67 AVAudioSessionCategoryOptions options = 0;
68 for (const auto &entry : requirements)
69 options |= entry.second.options;
70 return options;
71}
72
73} // namespace
74
76
81
123
129
130// Must be called with m_activationMutex held.
132{
136
138 NSError *error = nil;
140 qCWarning(qLcAudioSession) << "failed to set audio session category:"
142 return false;
143 }
144 }
145
146 NSError *error = nil;
148 if (!success)
149 qCWarning(qLcAudioSession) << "failed to activate audio session:"
151 else
152 qCDebug(qLcAudioSession) << "audio session activated";
153
154 return success;
155}
156
166
168{
170 const auto it = ranges::find_if(m_requirements, [&](const auto &entry) {
171 return entry.first == tokenId;
172 });
173 if (it != m_requirements.end())
175
176 if (m_requirements.empty()) {
177 NSError *error = nil;
179 qCWarning(qLcAudioSession) << "failed to deactivate audio session:"
181 else
182 qCDebug(qLcAudioSession) << "audio session deactivated";
183 return;
184 }
185
187}
188
190{
192 if (m_requirements.empty()) {
193 NSError *error = nil;
195 if (!success)
196 qCWarning(qLcAudioSession) << "failed to activate audio session:"
198 return success;
199 }
200
202}
203
205 std::shared_ptr<bool> destroyed,
208{
209}
210
217
220{
221 if (this != &other) {
222 release();
223 m_manager = std::exchange(other.m_manager, nullptr);
226 }
227 return *this;
228}
229
234
236{
239 m_manager = nullptr;
240}
241
242} // namespace QtMultimediaPrivate
243
244QT_END_NAMESPACE
245
246#include "moc_qavaudiosessionmanager_p.cpp"
Combined button and popup list for selecting options.