This repository was archived by the owner on Jul 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
210 lines (161 loc) · 6.29 KB
/
Copy pathserver.cpp
File metadata and controls
210 lines (161 loc) · 6.29 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
// version 1.5.2
// #define _DISABLE_HALF_RECV_LIMIT
#include <iostream>
#include <string>
#include <thread>
#include <map>
#include <vector>
#include <chrono>
#include <csignal>
#include "alsaLib.hpp"
#include "cpplibs/ssocket.hpp"
#include "cpplibs/argparse.hpp"
using namespace std;
struct cl_data {
pcmmode_t mode;
WAVHeader header;
};
long double doubleTime() { return std::chrono::duration_cast<std::chrono::duration<long double>>(std::chrono::system_clock::now().time_since_epoch()).count(); }
long double math_map(long double x, long double in_min, long double in_max, long double out_min, long double out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }
map<string, string> play_status;
map<string, cl_data> play_setup;
map<string, PCM*> pcms;
string globalDevice;
void sighandler(int e) {
for (auto i : pcms) i.second->pcm_exit();
exit(0);
}
void player(SSocket client) {
string id = client.srecv(1024).string;
while (play_setup.find(id) == play_setup.end()) continue;
pcmmode_t mode = play_setup[id].mode;
cout << "Player: connected user: " << id << endl;
PCM pcm;
try { pcm.setup(globalDevice, play_setup[id].header, mode); } catch (int e) { cerr << snd_strerror(e) << endl; goto close_connection; }
try { pcm.setBufferSize(65536); } catch (int e) {};
pcm.paramsApply();
pcms[id] = &pcm;
{
int period = pcm.getPeriod();
size_t buff_size = period * play_setup[id].header.numChannels * (play_setup[id].header.bitsPerSample / 8);
client.ssend(to_string(buff_size));
client.setblocking(false);
if (mode == PLAY) {
while (true) {
if (play_status[id] == "PAUSED") {
pcm.pause();
while (play_status[id] == "PAUSED") { this_thread::sleep_for(5ms); }
}
if (play_status[id] == "STOPPED") {
pcm.close();
break;
}
sockrecv_t wavdata = client.srecv(buff_size);
if (wavdata.length > 0) {
client.ssend("ok");
if (pcm.writei(wavdata.buffer, period) == -EPIPE) {
pcm.recover(-EPIPE, 1);
}
}
}
}
else if (mode == CAPTURE) {
char* buff = new char[buff_size];
while (true) {
if (play_status[id] == "PAUSED") {
pcm.pause();
while (play_status[id] == "PAUSED") { this_thread::sleep_for(5ms); }
}
if (play_status[id] == "STOPPED") {
pcm.close();
break;
}
if (pcm.readi(buff, period) == -EPIPE) {
pcm.recover(-EPIPE, 1);
}
client.ssend(buff, buff_size);
}
delete[] buff;
}
}
close_connection:
client.sclose();
pcms.erase(id);
cout << "Player: disconnected user: " << id << endl;
}
void manager(SSocket sock) {
// Json json;
string id = uuid4();
play_status[id] = "NONE";
cl_data cldata;
cldata.header = *((WAVHeader*)sock.srecv(44).buffer);
cldata.mode = (pcmmode_t)stoi(sock.srecv(1).string);
// JsonNode params = json.parse(sock.srecv(1024).string);
play_setup[id] = cldata;
sock.ssend(id);
long double start = 0;
long double duration = (long double)(cldata.header.subchunk2Size / (cldata.header.numChannels * (cldata.header.bitsPerSample / 8))) / (long double)cldata.header.sampleRate;
long double pause_time_start = 0;
long double pause_time = 0;
cout << "Manager: connected user: " << id << endl;
while (true) {
string data = sock.srecv(1024).string;
if (data == "start") {
play_status[id] = "RUNNING";
start = doubleTime();
}
else if (data == "pause") {
// cout << "paused" << endl;
play_status[id] = "PAUSED";
pause_time_start = doubleTime();
}
else if (data == "resume") {
// cout << "resume" << endl;
play_status[id] = "RUNNING";
pause_time += doubleTime() - pause_time_start;
sock.ssend("0");
}
else if (data == "stop" || data.length() == 0) {
// cout << pcms[id]->bufferAvailable() << endl;
play_status[id] = "STOPPED";
break;
}
// else if (data == "is_running") {
// int preverrno = errno;
// if (params["check_is_running"].str == "use_buffer_experimental") {
// int frames = pcms[id]->bufferAvailable();
// sock.ssend((frames >= pcms[id]->getBufferSize() - 100 || frames < 0) ? "0" : "1");
// } else if (params["check_is_running"].str == "use_timer") sock.ssend((doubleTime() - start >= duration + pause_time) ? "0" : "1");
// errno = preverrno;
// }
else if (data == "get_sound_progress") {
if (cldata.mode == PLAY) sock.ssend(to_string((int)math_map(doubleTime(), start + pause_time, start + duration + pause_time, 0, 100)));
else sock.ssend("0");
}
}
sock.sclose();
cout << "Manager: disconnected user: " << id << endl;
}
void listener() {
SSocket sock(AF_INET, SOCK_STREAM);
sock.ssetsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
sock.sbind("", 53765);
sock.slisten(0);
while (true) thread(player, sock.saccept().first).detach();
}
int main(int argc, char** argv) {
ArgumentParser parser(argc, argv);
parser.add_argument({.flag1 = "-D", .flag2 = "--device"});
parser.add_argument({.flag1 = "-a", .flag2 = "--use-alsa", .without_value = true});
auto args = parser.parse();
globalDevice = (args["--device"].type != ANYNONE) ? args["--device"].str : (args["--use-alsa"].boolean) ? "plughw:0,0" : "default";
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
signal(SIGKILL, sighandler);
SSocket sock(AF_INET, SOCK_STREAM);
sock.ssetsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
sock.sbind("", 53764);
sock.slisten(0);
thread(listener).detach();
while (true) thread(manager, sock.saccept().first).detach();
}