Skip to content

Commit 4732fbe

Browse files
WASAPI: Separate Engine and Flux into separate files
1 parent 019dd19 commit 4732fbe

7 files changed

Lines changed: 624 additions & 549 deletions

File tree

src/backends/WASAPI/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ target_include_directories(be_wasapi
2222

2323
target_sources(be_wasapi
2424
PRIVATE
25+
"WASAPI.cpp"
26+
"WASAPI.hpp"
27+
28+
"Engine.cpp"
29+
"Engine.hpp"
30+
31+
"Flux.cpp"
32+
"Flux.hpp"
33+
2534
"Device.cpp"
2635
"Device.hpp"
36+
2737
"EventManager.cpp"
2838
"EventManager.hpp"
29-
"WASAPI.cpp"
30-
"WASAPI.hpp"
3139
)
3240

3341
target_link_libraries(be_wasapi

src/backends/WASAPI/Engine.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright The Mumble Developers. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license
3+
// that can be found in the LICENSE file at the root of the
4+
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
5+
6+
#include "Engine.hpp"
7+
8+
#include "Device.hpp"
9+
#include "EventManager.hpp"
10+
11+
#include "Node.h"
12+
13+
#include <mmdeviceapi.h>
14+
15+
using namespace wasapi;
16+
17+
ErrorCode Engine::threadInit() {
18+
switch (CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE | COINIT_SPEED_OVER_MEMORY)) {
19+
case S_OK:
20+
case S_FALSE:
21+
case RPC_E_CHANGED_MODE:
22+
return CROSSAUDIO_EC_OK;
23+
default:
24+
return CROSSAUDIO_EC_INIT;
25+
}
26+
}
27+
28+
ErrorCode Engine::threadDeinit() {
29+
CoUninitialize();
30+
31+
return CROSSAUDIO_EC_OK;
32+
}
33+
34+
Engine::Engine() : m_enumerator(nullptr) {
35+
CoCreateGuid(reinterpret_cast< GUID * >(&m_sessionID));
36+
37+
CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
38+
reinterpret_cast< void ** >(&m_enumerator));
39+
}
40+
41+
Engine::~Engine() {
42+
if (m_enumerator) {
43+
m_enumerator->Release();
44+
}
45+
}
46+
47+
ErrorCode Engine::start(const EngineFeedback &feedback) {
48+
m_feedback = feedback;
49+
50+
const EventManager::Feedback eventManagerFeedback{
51+
.nodeAdded = [this](Node *node) { m_feedback.nodeAdded(m_feedback.userData, node); },
52+
.nodeRemoved = [this](Node *node) { m_feedback.nodeRemoved(m_feedback.userData, node); }
53+
};
54+
55+
m_eventManager = std::make_unique< EventManager >(m_enumerator, eventManagerFeedback);
56+
57+
return CROSSAUDIO_EC_OK;
58+
}
59+
60+
ErrorCode Engine::stop() {
61+
m_eventManager.reset();
62+
63+
return CROSSAUDIO_EC_OK;
64+
}
65+
66+
const char *Engine::nameGet() const {
67+
return m_name.data();
68+
}
69+
70+
ErrorCode Engine::nameSet(const char *name) {
71+
m_name = name;
72+
73+
return CROSSAUDIO_EC_OK;
74+
}
75+
76+
Nodes *Engine::engineNodesGet() {
77+
IMMDeviceCollection *collection;
78+
if (m_enumerator->EnumAudioEndpoints(eAll, DEVICE_STATE_ACTIVE, &collection) != S_OK) {
79+
return nullptr;
80+
}
81+
82+
UINT count = 0;
83+
collection->GetCount(&count);
84+
85+
auto nodes = nodesNew(count);
86+
87+
for (decltype(count) i = 0; i < count; ++i) {
88+
IMMDevice *device;
89+
if (collection->Item(i, &device) != S_OK) {
90+
continue;
91+
}
92+
93+
populateNode(nodes->items[i], *device, nullptr);
94+
95+
device->Release();
96+
}
97+
98+
collection->Release();
99+
100+
return nodes;
101+
}

src/backends/WASAPI/Engine.hpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright The Mumble Developers. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license
3+
// that can be found in the LICENSE file at the root of the
4+
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
5+
6+
#ifndef CROSSAUDIO_SRC_BACKENDS_WASAPI_ENGINE_HPP
7+
#define CROSSAUDIO_SRC_BACKENDS_WASAPI_ENGINE_HPP
8+
9+
#include "crossaudio/Engine.h"
10+
#include "crossaudio/ErrorCode.h"
11+
#include "crossaudio/Node.h"
12+
13+
#include <cstdint>
14+
#include <memory>
15+
#include <string>
16+
17+
typedef CrossAudio_ErrorCode ErrorCode;
18+
19+
typedef CrossAudio_EngineFeedback EngineFeedback;
20+
typedef CrossAudio_Nodes Nodes;
21+
22+
struct IMMDeviceEnumerator;
23+
24+
namespace wasapi {
25+
class EventManager;
26+
27+
class Engine {
28+
public:
29+
struct SessionID {
30+
uint32_t part1;
31+
uint16_t part2;
32+
uint16_t part3;
33+
uint8_t part4[8];
34+
};
35+
36+
static ErrorCode threadInit();
37+
static ErrorCode threadDeinit();
38+
39+
Engine();
40+
~Engine();
41+
42+
constexpr operator bool() const { return m_enumerator; }
43+
44+
const char *nameGet() const;
45+
ErrorCode nameSet(const char *name);
46+
47+
Nodes *engineNodesGet();
48+
49+
ErrorCode start(const EngineFeedback &feedback);
50+
ErrorCode stop();
51+
52+
std::string m_name;
53+
SessionID m_sessionID;
54+
IMMDeviceEnumerator *m_enumerator;
55+
56+
private:
57+
Engine(const Engine &) = delete;
58+
Engine &operator=(const Engine &) = delete;
59+
60+
EngineFeedback m_feedback;
61+
std::unique_ptr< EventManager > m_eventManager;
62+
};
63+
} // namespace wasapi
64+
65+
#endif

0 commit comments

Comments
 (0)