utils.cpp
12.1 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* akvirtualcamera, virtual camera for Mac and Windows.
* Copyright (C) 2020 Gonzalo Exequiel Pedone
*
* akvirtualcamera 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.
*
* akvirtualcamera 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 akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
*
* Web-Site: http://webcamoid.github.io/
*/
#include <map>
#include <sstream>
#include <algorithm>
#include <unistd.h>
#include <dlfcn.h>
#include <CoreGraphics/CGImage.h>
#include <CoreGraphics/CGDataProvider.h>
#include <libproc.h>
#include "utils.h"
#include "preferences.h"
#include "VCamUtils/src/logger.h"
#include "VCamUtils/src/messageclient.h"
#include "VCamUtils/src/utils.h"
#include "VCamUtils/src/videoformat.h"
#include "VCamUtils/src/videoframe.h"
namespace AkVCam {
namespace Utils {
struct RGB24
{
uint8_t b;
uint8_t g;
uint8_t r;
};
struct RGB32
{
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t x;
};
inline const std::map<AkVCam::PixelFormat, FourCharCode> *formatsTable()
{
static const std::map<AkVCam::PixelFormat, FourCharCode> formatsTable {
{AkVCam::PixelFormatRGB32, kCMPixelFormat_32ARGB },
{AkVCam::PixelFormatRGB24, kCMPixelFormat_24RGB },
{AkVCam::PixelFormatRGB16, kCMPixelFormat_16LE565 },
{AkVCam::PixelFormatRGB15, kCMPixelFormat_16LE555 },
{AkVCam::PixelFormatUYVY , kCMPixelFormat_422YpCbCr8 },
{AkVCam::PixelFormatYUY2 , kCMPixelFormat_422YpCbCr8_yuvs}
};
return &formatsTable;
}
}
std::string pluginInstallPath();
}
std::string AkVCam::locateManagerPath()
{
auto file = pluginInstallPath() + "/" + DATAROOTDIR "/" AKVCAM_MANAGER_NAME;
return fileExists(file)? file: std::string();
}
std::string AkVCam::locateServicePath()
{
auto file = pluginInstallPath() + "/" + DATAROOTDIR "/" AKVCAM_SERVICE_NAME;
return fileExists(file)? file: std::string();
}
std::string AkVCam::locatePluginPath()
{
auto file = pluginInstallPath() + "/" + BINDIR "/" AKVCAM_PLUGIN_NAME;
return fileExists(file)? file: std::string();
}
std::string AkVCam::tempPath()
{
return {"/tmp"};
}
bool AkVCam::uuidEqual(const REFIID &uuid1, const CFUUIDRef uuid2)
{
auto iid2 = CFUUIDGetUUIDBytes(uuid2);
auto puuid1 = reinterpret_cast<const UInt8 *>(&uuid1);
auto puuid2 = reinterpret_cast<const UInt8 *>(&iid2);
for (int i = 0; i < 16; i++)
if (puuid1[i] != puuid2[i])
return false;
return true;
}
std::string AkVCam::enumToString(uint32_t value)
{
auto valueChr = reinterpret_cast<char *>(&value);
std::stringstream ss;
for (int i = 3; i >= 0; i--)
if (valueChr[i] < 0)
ss << std::hex << valueChr[i];
else if (valueChr[i] < 32)
ss << int(valueChr[i]);
else
ss << valueChr[i];
return "'" + ss.str() + "'";
}
FourCharCode AkVCam::formatToCM(PixelFormat format)
{
for (auto &fmt: *Utils::formatsTable())
if (fmt.first == format)
return fmt.second;
return FourCharCode(0);
}
AkVCam::PixelFormat AkVCam::formatFromCM(FourCharCode format)
{
for (auto &fmt: *Utils::formatsTable())
if (fmt.second == format)
return fmt.first;
return PixelFormat(0);
}
std::string AkVCam::dirname(const std::string &path)
{
return path.substr(0, path.rfind("/"));
}
std::string AkVCam::realPath(const std::string &path)
{
char resolvedPath[4096];
memset(resolvedPath, 0, 4096);
::realpath(path.c_str(), resolvedPath);
char realPath[4096];
memset(realPath, 0, 4096);
readlink(resolvedPath, realPath, 4096);
if (strnlen(realPath, 4096) < 1)
return {resolvedPath};
return {realPath};
}
AkVCam::VideoFrame AkVCam::loadPicture(const std::string &fileName)
{
AkLogFunction();
VideoFrame frame;
if (frame.load(fileName)) {
AkLogInfo() << "Picture loaded as BMP" << std::endl;
return frame;
}
#ifndef FAKE_APPLE
auto fileDataProvider = CGDataProviderCreateWithFilename(fileName.c_str());
if (!fileDataProvider) {
AkLogError() << "Can't create a data provider for '"
<< fileName
<< "'"
<< std::endl;
return {};
}
// Check if the file is a PNG and open it.
auto cgImage = CGImageCreateWithPNGDataProvider(fileDataProvider,
nullptr,
true,
kCGRenderingIntentDefault);
// If the file is not a PNG, try opening as JPEG.
if (!cgImage) {
AkLogWarning() << "Can't read '"
<< fileName
<< "' as a PNG image."
<< std::endl;
cgImage = CGImageCreateWithJPEGDataProvider(fileDataProvider,
nullptr,
true,
kCGRenderingIntentDefault);
}
CGDataProviderRelease(fileDataProvider);
// The file format is not supported, fail.
if (!cgImage) {
AkLogError() << "Can't read '"
<< fileName
<< "' as a JPEG image."
<< std::endl;
return {};
}
FourCC format = 0;
if (CGImageGetBitsPerComponent(cgImage) == 8) {
if (CGImageGetBitsPerPixel(cgImage) == 24)
format = PixelFormatRGB24;
else if (CGImageGetBitsPerPixel(cgImage) == 32) {
format = PixelFormatRGB32;
}
}
auto width = CGImageGetWidth(cgImage);
auto height = CGImageGetHeight(cgImage);
if (format == 0 || width < 1 || height < 1) {
AkLogError() << "Invalid picture format: "
<< "BPC="
<< CGImageGetBitsPerComponent(cgImage)
<< "BPP="
<< CGImageGetBitsPerPixel(cgImage)
<< " "
<< width
<< "x"
<< height
<< std::endl;
CGImageRelease(cgImage);
return {};
}
VideoFormat videoFormat(PixelFormatRGB24, width, height);
frame = VideoFrame(videoFormat);
auto imageDataProvider = CGImageGetDataProvider(cgImage);
if (!imageDataProvider) {
AkLogError() << "Can't get data provider for picture." << std::endl;
CGImageRelease(cgImage);
return {};
}
auto data = CGDataProviderCopyData(imageDataProvider);
if (!data) {
AkLogError() << "Can't copy data from image provider." << std::endl;
CGImageRelease(cgImage);
return {};
}
auto lineSize = CGImageGetBytesPerRow(cgImage);
if (CGImageGetBitsPerPixel(cgImage) == 24) {
for (int y = 0; y < videoFormat.height(); y++) {
auto srcLine = reinterpret_cast<const Utils::RGB24 *>(CFDataGetBytePtr(data) + y * lineSize);
auto dstLine = reinterpret_cast<Utils::RGB24 *>(frame.line(0, y));
for (int x = 0; x < videoFormat.height(); x++) {
dstLine[x].r = srcLine[x].r;
dstLine[x].g = srcLine[x].g;
dstLine[x].b = srcLine[x].b;
}
}
} else if (CGImageGetBitsPerPixel(cgImage) == 32) {
if (CGImageGetAlphaInfo(cgImage) == kCGImageAlphaNone) {
for (int y = 0; y < videoFormat.height(); y++) {
auto srcLine = reinterpret_cast<const Utils::RGB32 *>(CFDataGetBytePtr(data) + y * lineSize);
auto dstLine = reinterpret_cast<Utils::RGB24 *>(frame.line(0, y));
for (int x = 0; x < videoFormat.height(); x++) {
dstLine[x].r = srcLine[x].r;
dstLine[x].g = srcLine[x].g;
dstLine[x].b = srcLine[x].b;
}
}
} else {
for (int y = 0; y < videoFormat.height(); y++) {
auto srcLine = reinterpret_cast<const Utils::RGB32 *>(CFDataGetBytePtr(data) + y * lineSize);
auto dstLine = reinterpret_cast<Utils::RGB24 *>(frame.line(0, y));
for (int x = 0; x < videoFormat.width(); x++) {
dstLine[x].r = srcLine[x].r * srcLine[x].x / 255;
dstLine[x].g = srcLine[x].g * srcLine[x].x / 255;
dstLine[x].b = srcLine[x].b * srcLine[x].x / 255;
}
}
}
}
CFRelease(data);
CGImageRelease(cgImage);
AkLogDebug() << "Picture loaded as: "
<< VideoFormat::stringFromFourcc(frame.format().fourcc())
<< " "
<< frame.format().width()
<< "x"
<< frame.format().height()
<< std::endl;
return frame;
#else
return {};
#endif
}
bool AkVCam::fileExists(const std::string &path)
{
struct stat stats;
memset(&stats, 0, sizeof(struct stat));
return stat(path.c_str(), &stats) == 0;
}
bool AkVCam::readEntitlements(const std::string &app,
const std::string &output)
{
bool writen = false;
std::string cmd = "codesign -d --entitlements - \"" + app + "\"";
auto proc = popen(cmd.c_str(), "r");
if (proc) {
auto entitlements = fopen(output.c_str(), "w");
if (entitlements) {
for (size_t i = 0; !feof(proc); i++) {
char data[1024];
auto len = fread(data, 1, 1024, proc);
if (len < 1)
break;
size_t offset = 0;
if (i == 0)
offset = std::string(data, len).find("<?xml");
fwrite(data + offset, 1, len - offset, entitlements);
writen = true;
}
fclose(entitlements);
}
pclose(proc);
}
return writen;
}
std::vector<uint64_t> AkVCam::systemProcesses()
{
auto npids = proc_listallpids(nullptr, 0);
pid_t pidsvec[npids];
memset(pidsvec, 0, npids * sizeof(pid_t));
npids = std::min(proc_listallpids(pidsvec, npids * sizeof(pid_t)), npids);
std::vector<uint64_t> pids;
for (int i = 0; i < npids; i++) {
auto it = std::find(pids.begin(), pids.end(), pidsvec[i]);
if (pidsvec[i] > 0 && it == pids.end())
pids.push_back(pidsvec[i]);
}
return pids;
}
uint64_t AkVCam::currentPid()
{
return getpid();
}
std::string AkVCam::exePath(uint64_t pid)
{
char path[4096];
memset(path, 0, 4096);
proc_pidpath(pid, path, 4096);
return {path};
}
std::string AkVCam::currentBinaryPath()
{
Dl_info info;
memset(&info, 0, sizeof(Dl_info));
if (dladdr(reinterpret_cast<void *>(currentBinaryPath), &info) == 0)
return exePath(currentPid());
return {info.dli_fname};
}
bool AkVCam::isServiceRunning()
{
AkLogFunction();
auto service = locateServicePath();
AkLogDebug() << "Service path: " << service << std::endl;
AkLogDebug() << "System processes:" << std::endl;
for (auto &pid: systemProcesses()) {
auto path = exePath(pid);
if (path.empty())
continue;
AkLogDebug() << " " << path << std::endl;
if (path == service)
return true;
}
return false;
}
bool AkVCam::isServicePortUp()
{
return MessageClient::isUp(Preferences::servicePort());
}
std::string AkVCam::pluginInstallPath()
{
return realPath(dirname(currentBinaryPath()) + "/../../..");
}