-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcapture_system_audio.cpp
47 lines (44 loc) · 1.31 KB
/
capture_system_audio.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
// C++ Native Messaging host
// https://browserext.github.io/native-messaging/
// https://developer.chrome.com/docs/apps/nativeMessaging/
// https://discourse.mozilla.org/t/webextension-with-native-messaging-c-app-side/30821
#include <iostream>
using namespace std;
string getMessage() {
uint32_t length;
cin.read(reinterpret_cast<char *>(&length), 4);
string content;
content.resize(length);
char* buffer = &(content[0]);
cin.read(buffer, length);
return content;
}
void sendMessage(string message) {
uint32_t size = message.size();
char *length = reinterpret_cast<char *>(&size);
cout.write(length, 4);
cout.write(message.c_str(), message.size());
cout.flush();
}
int main() {
string message = getMessage();
size_t length = 1764; // 441 * 4
uint8_t buffer[length];
string output;
output.reserve((length * 4) + 2);
// Exclude double quotation marks from beginning and end of string
FILE *pipe = popen(message.substr(1, message.length() - 2).c_str(), "r");
while (true) {
size_t count = fread(buffer, 1, sizeof(buffer), pipe);
output += "[";
for (size_t i = 0; i < count; i++) {
output += to_string(buffer[i]);
if (i < count - 1) {
output += ",";
}
}
output += "]";
sendMessage(output);
output.erase(output.begin(), output.end());
}
}