-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudiosessionmanager.cpp
More file actions
213 lines (174 loc) · 6.79 KB
/
audiosessionmanager.cpp
File metadata and controls
213 lines (174 loc) · 6.79 KB
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
#include "audiosessionmanager.h"
#include <QCoreApplication>
#include <combaseapi.h>
#include <QDebug>
AudioSessionManager::AudioSessionManager(QObject *parent)
: QObject(parent), m_sessionManager(nullptr), m_sessionControl(nullptr)
{
// CoInitialize(nullptr);
// Initialize COM for this thread with apartment threading
HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (FAILED(hr) && hr != RPC_E_CHANGED_MODE) {
qWarning() << "Failed to initialize COM:" << hr;
}
}
AudioSessionManager::~AudioSessionManager()
{
cleanup();
CoUninitialize();
}
void AudioSessionManager::cleanup()
{
if (m_sessionControl) {
m_sessionControl->Release();
m_sessionControl = nullptr;
}
if (m_sessionManager) {
m_sessionManager->Release();
m_sessionManager = nullptr;
}
}
bool AudioSessionManager::findPhoneAudioSession()
{
HRESULT hr;
IMMDeviceEnumerator *deviceEnumerator = nullptr;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr,
CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator),
(void**)&deviceEnumerator);
if (FAILED(hr)) {
qWarning() << "Failed to create device enumerator:" << hr;
return false;
}
DWORD currentProcessId = GetCurrentProcessId();
qDebug() << "Current process ID:" << currentProcessId;
// Try both capture (recording) and render (playback) devices
// A2DP Sink appears as capture since audio comes FROM the phone
EDataFlow dataFlows[] = { eCapture, eRender };
for (int flowIdx = 0; flowIdx < 2; flowIdx++) {
EDataFlow dataFlow = dataFlows[flowIdx];
qDebug() << "Searching" << (dataFlow == eCapture ? "CAPTURE" : "RENDER") << "devices...";
IMMDevice *device = nullptr;
hr = deviceEnumerator->GetDefaultAudioEndpoint(dataFlow, eConsole, &device);
if (FAILED(hr)) {
qWarning() << "Failed to get default audio endpoint for flow" << dataFlow << ":" << hr;
continue;
}
// Get the session manager
IAudioSessionManager2 *sessionManager = nullptr;
hr = device->Activate(__uuidof(IAudioSessionManager2), CLSCTX_INPROC_SERVER,
nullptr, (void**)&sessionManager);
device->Release();
if (FAILED(hr)) {
qWarning() << "Failed to activate session manager:" << hr;
continue;
}
// Enumerate sessions
IAudioSessionEnumerator *sessionEnumerator = nullptr;
hr = sessionManager->GetSessionEnumerator(&sessionEnumerator);
if (FAILED(hr)) {
qWarning() << "Failed to get session enumerator:" << hr;
sessionManager->Release();
continue;
}
int sessionCount = 0;
sessionEnumerator->GetCount(&sessionCount);
qDebug() << "Found" << sessionCount << "sessions";
for (int i = 0; i < sessionCount; i++) {
IAudioSessionControl *sessionControl = nullptr;
hr = sessionEnumerator->GetSession(i, &sessionControl);
if (SUCCEEDED(hr)) {
IAudioSessionControl2 *sessionControl2 = nullptr;
hr = sessionControl->QueryInterface(__uuidof(IAudioSessionControl2),
(void**)&sessionControl2);
sessionControl->Release();
if (SUCCEEDED(hr)) {
DWORD processId = 0;
sessionControl2->GetProcessId(&processId);
LPWSTR displayName = nullptr;
sessionControl2->GetDisplayName(&displayName);
QString name = displayName ? QString::fromWCharArray(displayName) : QString("(no name)");
CoTaskMemFree(displayName);
qDebug() << " Session" << i << "- PID:" << processId << "Name:" << name;
// Match by process ID OR by name containing "Microphone" or "A2DP"
if (processId == currentProcessId ||
name.contains("Microphone", Qt::CaseInsensitive) ||
name.contains("A2DP", Qt::CaseInsensitive) ||
name.contains("Phone Audio Link", Qt::CaseInsensitive)) {
qDebug() << "*** MATCHED session:" << name;
m_sessionControl = sessionControl2;
m_sessionManager = sessionManager;
sessionEnumerator->Release();
deviceEnumerator->Release();
return true;
}
sessionControl2->Release();
}
}
}
sessionEnumerator->Release();
sessionManager->Release();
}
deviceEnumerator->Release();
qWarning() << "Could not find audio session";
return false;
}
bool AudioSessionManager::setSessionProperties(const QString &displayName, const QString &iconPath)
{
// if (!m_sessionControl) {
// if (!findPhoneAudioSession()) {
// return false;
// }
// }
// if (!m_sessionControl) {
// return false;
// }
// // Set display name
// HRESULT hr = m_sessionControl->SetDisplayName(
// reinterpret_cast<LPCWSTR>(displayName.utf16()), nullptr);
// if (FAILED(hr)) {
// qWarning() << "Failed to set display name:" << hr;
// return false;
// }
// // Set icon path
// hr = m_sessionControl->SetIconPath(
// reinterpret_cast<LPCWSTR>(iconPath.utf16()), nullptr);
// if (FAILED(hr)) {
// qWarning() << "Failed to set icon path:" << hr;
// return false;
// }
// qDebug() << "Successfully set audio session properties";
// return true;
// Always try to find the session first
if (m_sessionControl) {
m_sessionControl->Release();
m_sessionControl = nullptr;
}
if (m_sessionManager) {
m_sessionManager->Release();
m_sessionManager = nullptr;
}
if (!findPhoneAudioSession()) {
return false;
}
if (!m_sessionControl) {
return false;
}
qDebug() << "Setting display name to:" << displayName;
qDebug() << "Setting icon path to:" << iconPath;
// Set display name
HRESULT hr = m_sessionControl->SetDisplayName(
reinterpret_cast<LPCWSTR>(displayName.utf16()), nullptr);
if (FAILED(hr)) {
qWarning() << "Failed to set display name:" << QString::number(hr, 16);
return false;
}
// Set icon path
hr = m_sessionControl->SetIconPath(
reinterpret_cast<LPCWSTR>(iconPath.utf16()), nullptr);
if (FAILED(hr)) {
qWarning() << "Failed to set icon path:" << QString::number(hr, 16);
return false;
}
qDebug() << "Successfully set audio session properties!";
return true;
}