-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.c
267 lines (230 loc) · 8.48 KB
/
client.c
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
/*
Copyright (c) 2018, Christof Schulze <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "alloc.h"
#include "alsaplayer.h"
#include "error.h"
#include "pcmchunk.h"
#include "snapcast.h"
#include "syscallwrappers.h"
#include "types.h"
#include "util.h"
#include "vector.h"
#include "version.h"
#include "intercom_client.h"
#define SIGTERM_MSG "Exiting.\n"
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <netdb.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
snapctx_t snapctx = {};
void sig_term_handler(int signum, siginfo_t *info, void *ptr) {
alsaplayer_remove_task(&snapctx.alsaplayer_ctx);
alsaplayer_uninit(&snapctx.alsaplayer_ctx);
write(STDERR_FILENO, SIGTERM_MSG, sizeof(SIGTERM_MSG));
_exit(EXIT_SUCCESS);
}
int alsa_get_fd_amount() {
int fd_amount;
alsaplayer_ctx aplay_tmp_ctx = {
// just set some sane defaults such that we can determine how many FD alsa will need.
.frame_size = 2,
.channels = 2,
.rate = 48000,
.pcm.name = strdupa(snapctx.alsaplayer_ctx.pcm.name),
.card = strdupa(snapctx.alsaplayer_ctx.card),
.mixer = strdupa(snapctx.alsaplayer_ctx.mixer),
};
alsaplayer_init(&aplay_tmp_ctx);
fd_amount = aplay_tmp_ctx.pollfd_count;
alsaplayer_remove_task(&aplay_tmp_ctx);
alsaplayer_uninit(&aplay_tmp_ctx);
return fd_amount;
}
void resume_polling(void *d) {
struct pollfd *fds = (struct pollfd*)d;
fds->fd = snapctx.intercom_ctx.fd;
}
void loop() {
int fd_index = 0;
fd_index += alsa_get_fd_amount();
struct pollfd fds[fd_index + 2]; // allocate fds for alsa events
snapctx.alsaplayer_ctx.main_poll_fd = fds; // alsa fds must be the first
// Alsa will be initialized when first packets arrive.
for (int i = 0; i < fd_index; i++) {
fds[i].fd = -1;
fds[i].events = POLLIN;
}
fds[fd_index].fd = snapctx.taskqueue_ctx.fd;
fds[fd_index].events = POLLIN;
fd_index++;
fds[fd_index].fd = snapctx.intercom_ctx.fd;
fds[fd_index].events = POLLIN;
fd_index++;
log_verbose("starting loop\n");
while (1) {
log_debug("polling\n");
int n = poll(fds, fd_index, -1);
if (n == -1) {
sig_term_handler(0, 0, 0);
exit_error("poll error - Catch me if you can!\n");
} else if (n == 0)
log_error("this should never happen - timeout on poll");
else {
for (int i = 0; i < fd_index; i++) {
if (fds[i].revents) {
log_debug("handling event on fd %i.\n", fds[i].fd);
if (is_alsafd(fds[i].fd, &snapctx.alsaplayer_ctx)) {
// this is a nightmare. alsa logic is meant to be contained in alsaplayer
unsigned short revents;
snd_pcm_poll_descriptors_revents(snapctx.alsaplayer_ctx.pcm_handle, fds,
snapctx.alsaplayer_ctx.pollfd_count, &revents);
fds[i].revents = revents;
}
if ((fds[i].revents & POLLIN) && (fds[i].fd == snapctx.taskqueue_ctx.fd)) {
log_debug("taskqueue ready\n");
taskqueue_run(&snapctx.taskqueue_ctx);
} else if ((fds[i].revents & POLLIN) && (fds[i].fd == snapctx.intercom_ctx.fd)) {
log_debug("intercom ready for IO\n");
intercom_handle_in(&snapctx.intercom_ctx, fds[i].fd);
} else if (( fds[i].revents & POLLERR) && (fds[i].fd == snapctx.intercom_ctx.fd)) {
log_error("Error on network layer, could not reach server. Re-initializing.\n");
intercom_uninit(&snapctx.intercom_ctx);
alsaplayer_uninit(&snapctx.alsaplayer_ctx);
post_task(&snapctx.taskqueue_ctx, 1, 0, intercom_reinit, NULL, (void*)&snapctx.intercom_ctx);
} else if (( fds[i].revents & POLLNVAL) && (fds[i].fd == snapctx.intercom_ctx.fd)) {
fds[fd_index-1].fd = -1;
post_task(&snapctx.taskqueue_ctx, 1, 0, resume_polling, NULL, &fds[fd_index-1]);
} else if ((fds[i].revents & POLLOUT) && (is_alsafd(fds[i].fd, &snapctx.alsaplayer_ctx))) {
log_debug("alsa device ready for IO\n");
alsaplayer_handle(&snapctx.alsaplayer_ctx);
snapctx.alsaplayer_ctx.ufds[i].revents = 0; // clear events for next iteration
}
fds[i].revents = 0; // clear events for next iteration
}
}
}
}
}
void usage() {
puts("snapclient [-l | -H <servername> [-L <latency_ms>] [-p <serverport>] [-c <card>] [-m <mixer>] [-s <pcm name>] [-i <clientID>] [-v] [-d] ] ");
puts("");
puts("-l list pcm devices");
puts("-L set pcm latency");
puts("-V show version");
puts("");
puts("example: snapcast-client -H media -c hw:1 -m snapclient -s snapclient -i 2 -v");
}
void catch_sigterm() {
static struct sigaction _sigact;
memset(&_sigact, 0, sizeof(_sigact));
_sigact.sa_sigaction = sig_term_handler;
_sigact.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &_sigact, NULL);
}
int main(int argc, char *argv[]) {
snapctx.verbose = false;
snapctx.debug = false;
snapctx.alsaplayer_ctx.pollfd_count = 0;
snapctx.alsaplayer_ctx.initialized = false;
snapctx.bufferms = 1000;
snapctx.alsaplayer_ctx.card = snap_strdup("default");
snapctx.alsaplayer_ctx.mixer = snap_strdup("Master");
snapctx.alsaplayer_ctx.pcm.name = snap_strdup("default");
snapctx.intercom_ctx.port = INTERCOM_PORT;
snapctx.intercom_ctx.mtu = 1500; // Do we need to expose this to the user via cli?
obtainrandom(&snapctx.intercom_ctx.nodeid, sizeof(uint16_t), 0);
int option_index = 0;
struct option long_options[] = {{"help", 0, NULL, 'h'}, {"version", 0, NULL, 'V'}};
int c;
while ((c = getopt_long(argc, argv, "lVvdhH:p:s:i:L:c:m:", long_options, &option_index)) != -1) {
switch (c) {
case 'V':
printf("snapclient %s\n", SOURCE_VERSION);
#if defined(GIT_BRANCH) && defined(GIT_COMMIT_HASH)
printf("branch: %s\n commit: %s\n", GIT_BRANCH, GIT_COMMIT_HASH);
#endif
exit(EXIT_SUCCESS);
case 'c':
free(snapctx.alsaplayer_ctx.card);
snapctx.alsaplayer_ctx.card = snap_strdup(optarg);
break;
case 'm':
free(snapctx.alsaplayer_ctx.mixer);
snapctx.alsaplayer_ctx.mixer = snap_strdup(optarg);
break;
case 's':
free(snapctx.alsaplayer_ctx.pcm.name);
snapctx.alsaplayer_ctx.pcm.name = snap_strdup(optarg);
break;
case 'l':
alsaplayer_pcm_list();
exit(EXIT_SUCCESS);
case 'p':
snapctx.intercom_ctx.port = atoi(optarg);
break;
case 'i':
errno = 0;
snapctx.intercom_ctx.nodeid = strtoul(optarg, NULL, 10);
if (errno == ERANGE || (errno != 0 && snapctx.intercom_ctx.nodeid == 0 ) || snapctx.intercom_ctx.nodeid == 0 )
exit_errno("unable to parse parameter for -i (%s)", optarg);
break;
case 'H':
snapctx.servername = snap_strdup(optarg);
break;
case 'd':
snapctx.debug = true;
/* Falls through. */
case 'v':
snapctx.verbose = true;
break;
case 'L':
snapctx.alsaplayer_ctx.latency_ms = atol(optarg);
break;
case 'h':
default:
usage();
exit(EXIT_SUCCESS);
break;
}
}
log_verbose("Using Node-ID: %zu\n", snapctx.intercom_ctx.nodeid);
catch_sigterm();
taskqueue_init(&snapctx.taskqueue_ctx);
intercom_init(&snapctx.intercom_ctx);
// we have realtime business when feeding the alsa buffer, setting prio may help on an otherwise busy client.
if (setpriority(PRIO_PROCESS, 0, -5)) {
log_error("could not set priority. Adjusting priority helps on otherwise busy clients. Continuing.\n");
}
loop();
return 0;
}