-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessioncreationsource.cpp
142 lines (109 loc) · 4.44 KB
/
sessioncreationsource.cpp
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
#include "sessioncreationsource.h"
#include <QThread>
#include <QDebug>
#include <objbase.h>
#include "util.h"
REGISTER_METATYPE(IAudioSessionControl2Ptr)
COM_SMARTPTR(IAudioSessionEnumerator);
COM_SMARTPTR(IAudioSessionControl);
class SessionCreationSource::SessionNotificationCallback : IAudioSessionNotification {
ULONG refcount;
SessionCreationSource *manager;
public:
SessionNotificationCallback(SessionCreationSource *src);
virtual ~SessionNotificationCallback();
// IUnknown
virtual ULONG AddRef();
virtual ULONG Release();
virtual HRESULT QueryInterface(REFIID riid, void **ppvObject);
// IAudioSessionNotification
virtual HRESULT OnSessionCreated(IAudioSessionControl *NewSession);
};
SessionCreationSource::SessionNotificationCallback::SessionNotificationCallback(SessionCreationSource *src) :
refcount(1),
manager(src)
{ }
SessionCreationSource::SessionNotificationCallback::~SessionNotificationCallback() { }
COM_IMPL_REFCOUNT(SessionCreationSource::SessionNotificationCallback)
COM_IMPL_QUERYINTERFACE(SessionCreationSource::SessionNotificationCallback,
COM_IMPL_QICASE(IAudioSessionNotification))
HRESULT SessionCreationSource::SessionNotificationCallback::OnSessionCreated(IAudioSessionControl *NewSession) {
IAudioSessionControl2 *s2;
HRESULT hr = NewSession->QueryInterface(__uuidof(IAudioSessionControl2), (void**)&s2);
if(FAILED(hr)) {
qDebug() << "Couldn't cast session (" << hr << ")";
}
emit manager->notificationTrampoline(s2);
return S_OK;
}
class SessionNotificationFactoryThread : QThread {
protected:
void run() {
auto hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if(FAILED(hr)) {
QString failmsg = QString("CoInitializeEx failed in session factory thread (%0)").arg(hr);
wchar_t *msgdata = new wchar_t[failmsg.length()];
failmsg.toWCharArray(msgdata);
MessageBox(NULL, msgdata, L"Volume Control", MB_OK);
delete[] msgdata;
return;
}
this->exec();
CoUninitialize();
}
private:
static SessionNotificationFactoryThread *factorythread;
public:
static SessionNotificationFactoryThread *runningThread() {
if(factorythread == nullptr) {
factorythread = new SessionNotificationFactoryThread();
factorythread->start();
}
return factorythread;
}
};
SessionNotificationFactoryThread *SessionNotificationFactoryThread::factorythread = nullptr;
SessionCreationSource::SessionCreationSource(IAudioSessionManager2Ptr sessionManager, QObject *parent) :
QObject(parent),
manager(sessionManager)
{
SessionNotificationFactoryThread::runningThread();
connect(this, &SessionCreationSource::notificationTrampoline, this, &SessionCreationSource::notificationTrampolineLanding, Qt::QueuedConnection);
this->callback = new SessionNotificationCallback(this);
HRESULT hr;
hr = this->callback->QueryInterface(__uuidof(IAudioSessionNotification), (void**)&callback_as_callback);
assertHR(hr, "Somehow failed to cast the session notification callback (%0)");
this->manager->RegisterSessionNotification(this->callback_as_callback);
}
SessionCreationSource::~SessionCreationSource() {
this->manager->UnregisterSessionNotification(this->callback_as_callback);
this->callback->Release();
}
void SessionCreationSource::notificationTrampolineLanding(IAudioSessionControl2 *session) {
IAudioSessionControl2Ptr s(session);
emit sessionExists(s);
}
void SessionCreationSource::triggerEnumeration() {
IAudioSessionEnumeratorPtr enumer;
int sessionCount;
HRESULT hr;
hr = this->manager->GetSessionEnumerator(&enumer);
assertHR(hr, "Failed to get session enumerator (%0)");
hr = enumer->GetCount(&sessionCount);
assertHR(hr, "Failed to get session count (%0)");
for(int i = 0; i < sessionCount; i++) {
IAudioSessionControlPtr session;
hr = enumer->GetSession(i, &session);
if(FAILED(hr)) {
qDebug() << "Couldn't get session " << i;
continue;
}
IAudioSessionControl2Ptr s2;
hr = session.QueryInterface(__uuidof(IAudioSessionControl2), &s2);
if(FAILED(hr)) {
qDebug() << "Couldn't cast session " << i;
continue;
}
emit sessionExists(s2.GetInterfacePtr());
}
}