forked from ozsun88/SynchronousAudioRouter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.cpp
More file actions
384 lines (297 loc) · 10.6 KB
/
config.cpp
File metadata and controls
384 lines (297 loc) · 10.6 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// SynchronousAudioRouter
// Copyright (C) 2015 Mackenzie Straight
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SynchronousAudioRouter. If not, see <http://www.gnu.org/licenses/>.
#include "stdafx.h"
#include "config.h"
#include "utility.h"
#include <fstream>
namespace Sar {
bool EndpointConfig::load(picojson::object& obj)
{
auto poId = obj.find("id");
auto poDescription = obj.find("description");
auto poType = obj.find("type");
auto poChannelCount = obj.find("channelCount");
auto poAttachPhysical = obj.find("attachPhysical");
auto poPhysicalChannelBase = obj.find("physicalChannelBase");
if (poId == obj.end() || poDescription == obj.end() ||
poType == obj.end() || poChannelCount == obj.end()) {
return false;
}
if (!poId->second.is<std::string>() ||
!poDescription->second.is<std::string>() ||
!poType->second.is<std::string>() ||
!poChannelCount->second.is<double>()) {
return false;
}
auto strType = poType->second.get<std::string>();
if (strType == "recording") {
type = EndpointType::Recording;
} else {
type = EndpointType::Playback;
}
id = poId->second.get<std::string>();
description = UTF8ToWide(poDescription->second.get<std::string>());
channelCount = (int)poChannelCount->second.get<double>();
if (poAttachPhysical != obj.end() && poAttachPhysical->second.is<bool>()) {
attachPhysical = poAttachPhysical->second.get<bool>();
}
if (poPhysicalChannelBase != obj.end() &&
poPhysicalChannelBase->second.is<double>()) {
physicalChannelBase = (int)poPhysicalChannelBase->second.get<double>();
}
return true;
}
picojson::object EndpointConfig::save()
{
picojson::object result;
result.insert(std::make_pair("id", picojson::value(id)));
result.insert(std::make_pair("description", picojson::value(TCHARToUTF8(description.c_str()))));
result.insert(std::make_pair("type", picojson::value(
type == EndpointType::Recording ? "recording" : "playback")));
result.insert(std::make_pair("channelCount",
picojson::value(double(channelCount))));
if (attachPhysical) {
result.insert(std::make_pair("attachPhysical",
picojson::value(attachPhysical)));
result.insert(std::make_pair("physicalChannelBase",
picojson::value(double(physicalChannelBase))));
}
return result;
}
bool DefaultEndpointConfig::load(picojson::object& obj)
{
auto poRole = obj.find("role");
auto poType = obj.find("type");
auto poId = obj.find("id");
if (poRole == obj.end() || poType == obj.end() || poId == obj.end()) {
return false;
}
if (!poRole->second.is<std::string>() ||
!poType->second.is<std::string>() ||
!poId->second.is<std::string>()) {
return false;
}
auto roleStr = poRole->second.get<std::string>();
auto typeStr = poType->second.get<std::string>();
id = poId->second.get<std::string>();
if (roleStr == "console") {
role = ERole::eConsole;
} else if (roleStr == "communications") {
role = ERole::eCommunications;
} else if (roleStr == "multimedia") {
role = ERole::eMultimedia;
} else {
return false;
}
if (typeStr == "playback") {
type = EDataFlow::eRender;
} else if (typeStr == "recording") {
type = EDataFlow::eCapture;
} else {
return false;
}
return true;
}
picojson::object DefaultEndpointConfig::save()
{
picojson::object result;
std::string roleStr;
std::string typeStr;
result.insert(std::make_pair("id", picojson::value(id)));
switch (role) {
case ERole::eConsole: roleStr = "console"; break;
case ERole::eCommunications: roleStr = "communications"; break;
case ERole::eMultimedia: roleStr = "multimedia"; break;
}
switch (type) {
case EDataFlow::eRender: typeStr = "playback"; break;
case EDataFlow::eCapture: typeStr = "recording"; break;
}
result.insert(std::make_pair("role", picojson::value(roleStr)));
result.insert(std::make_pair("type", picojson::value(typeStr)));
return result;
}
static std::wstring constantRegex(const std::wstring& path)
{
static const std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
std::wostringstream os;
os << "^"
<< std::regex_replace(path, escape, L"\\\\&",
std::regex_constants::match_default |
std::regex_constants::format_sed)
<< "$";
return os.str();
}
bool ApplicationConfig::load(picojson::object& obj)
{
auto poDescription = obj.find("description");
auto poPath = obj.find("path");
auto poRegexMatch = obj.find("regexMatch");
auto poDefaults = obj.find("defaults");
if (poDescription == obj.end() || poPath == obj.end()) {
return false;
}
if (!poDescription->second.is<std::string>() ||
!poPath->second.is<std::string>()) {
return false;
}
if (poDefaults != obj.end() && poDefaults->second.is<picojson::array>()) {
for (auto& item : poDefaults->second.get<picojson::array>()) {
if (!item.is<picojson::object>()) {
continue;
}
DefaultEndpointConfig defaultEndpoint;
if (defaultEndpoint.load(item.get<picojson::object>())) {
defaults.push_back(defaultEndpoint);
}
}
}
description = UTF8ToWide(poDescription->second.get<std::string>());
if (poRegexMatch != obj.end() &&
poRegexMatch->second.is<bool>()) {
regexMatch = poRegexMatch->second.get<bool>();
}
try {
path = UTF8ToWide(poPath->second.get<std::string>());
// TODO: UTF-8 support on Windows is very sad. This might need ICU or
// PCRE.
pattern = std::wregex(regexMatch ? path : constantRegex(path),
std::regex_constants::ECMAScript | std::regex_constants::icase);
} catch (std::exception&) {
// nom nom
}
return true;
}
picojson::object ApplicationConfig::save()
{
picojson::object result;
result.insert(std::make_pair("description", picojson::value(TCHARToUTF8(description.c_str()))));
result.insert(std::make_pair("path", picojson::value(TCHARToUTF8(path.c_str()))));
if (regexMatch) {
result.insert(std::make_pair("regexMatch", picojson::value(true)));
}
if (defaults.size()) {
picojson::array arr;
for (auto& defaultEndpoint : defaults) {
arr.push_back(picojson::value(defaultEndpoint.save()));
}
result.insert(std::make_pair("defaults", picojson::value(arr)));
}
return result;
}
void DriverConfig::load(picojson::object& obj)
{
auto poDriverClsid = obj.find("driverClsid");
auto poEndpoints = obj.find("endpoints");
auto poApplications = obj.find("applications");
auto poWaveRtMinimumFrames = obj.find("waveRtMinimumFrames");
auto poEnableApplicationRouting = obj.find("enableApplicationRouting");
if (poDriverClsid != obj.end() &&
poDriverClsid->second.is<std::string>()) {
driverClsid = poDriverClsid->second.get<std::string>();
}
if (poEndpoints != obj.end() && poEndpoints->second.is<picojson::array>()) {
for (auto& item : poEndpoints->second.get<picojson::array>()) {
if (!item.is<picojson::object>()) {
continue;
}
EndpointConfig endpoint;
if (endpoint.load(item.get<picojson::object>())) {
endpoints.emplace_back(endpoint);
}
}
}
if (poApplications != obj.end() &&
poApplications->second.is<picojson::array>()) {
for (auto& item : poApplications->second.get<picojson::array>()) {
if (!item.is<picojson::object>()) {
continue;
}
ApplicationConfig application;
if (application.load(item.get<picojson::object>())) {
applications.emplace_back(application);
}
}
}
if (poWaveRtMinimumFrames != obj.end() &&
poWaveRtMinimumFrames->second.is<double>()) {
waveRtMinimumFrames = (int)poWaveRtMinimumFrames->second.get<double>();
}
if (poEnableApplicationRouting != obj.end() &&
poEnableApplicationRouting->second.is<bool>()) {
enableApplicationRouting =
poEnableApplicationRouting->second.get<bool>();
}
}
picojson::object DriverConfig::save()
{
picojson::object result;
result.insert(std::make_pair("driverClsid", picojson::value(driverClsid)));
result.insert(std::make_pair("enableApplicationRouting",
picojson::value(enableApplicationRouting)));
if (waveRtMinimumFrames > 2) {
result.insert(std::make_pair("waveRtMinimumFrames",
picojson::value((double)waveRtMinimumFrames)));
}
if (endpoints.size()) {
picojson::array arr;
for (auto& endpoint : endpoints) {
arr.emplace_back(endpoint.save());
}
result.insert(std::make_pair("endpoints", picojson::value(arr)));
}
if (applications.size()) {
picojson::array arr;
for (auto& application : applications) {
arr.emplace_back(application.save());
}
result.insert(std::make_pair("applications", picojson::value(arr)));
}
return result;
}
bool DriverConfig::writeFile(const std::wstring& path)
{
std::ofstream fp(path);
if (fp.bad()) {
return false;
}
picojson::value(save()).serialize(std::ostream_iterator<char>(fp), true);
return true;
}
DriverConfig DriverConfig::fromFile(const std::wstring& path)
{
std::ifstream fp(path);
picojson::value json;
DriverConfig result;
if (fp.bad()) {
return result;
}
fp >> json;
if (json.is<picojson::object>()) {
result.load(json.get<picojson::object>());
}
return result;
}
EndpointConfig *DriverConfig::findEndpoint(const std::string& id)
{
for (auto& ep : endpoints) {
if (ep.id == id) {
return &ep;
}
}
return nullptr;
}
} // namespace Sar