-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcpiped.c
392 lines (352 loc) · 10.9 KB
/
cpiped.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
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
/*
* Copyright (C) 2014 Brian Fitzpatrick <brian@froxen.com>
*
* 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
// Use the newer ALSA API
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <stdio.h>
#include <math.h>
#include <alsa/asoundlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <errno.h>
snd_pcm_t *handle;
int daemonize = 0;
int readfd = 0;
int writefd = 0;
void mylog(int level, const char *format, ...)
{
va_list args;
va_start (args, format);
if (daemonize) {
vsyslog(level, format, args);
} else {
vprintf(format, args);
}
va_end(args);
}
void myterm() {
mylog(LOG_INFO, "Stopping due to interrupt/term.\n");
snd_pcm_drain(handle);
snd_pcm_close(handle);
if (readfd > 0) close(readfd);
if (writefd > 0) close(writefd);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]) {
int rc;
int capsize;
snd_pcm_hw_params_t *params;
unsigned int val = 44100;
unsigned int capusec;
int dir;
snd_pcm_uframes_t frames;
char *capbuffer;
int16_t *scapbuffer;
char *capdev = "default";
char *fifonam;
struct stat status;
int readbytes = 0;
int writebytes = 0;
char *buf;
size_t bufsize = 176400;
int bufstart;
int bufend;
int bufused;
int fillbuf = 1;
int buffull = 0;
int bufendtoend;
int bufstarttoend;
int capcount = 0;
long power = 0;
int prevpower = 0;
int i;
int silent = 1;
int silentcount = 0;
int soundcount = 0;
char startcmd[1000] = "";
char endcmd[1000] = "";
int wrote = 0;
int silentt = 100;
extern char *optarg;
extern int optind;
int opt;
int err = 0;
pid_t pid, sid;
char pidstr[10];
char pidfile[50];
int pidfd;
// Ignore pipe signals
signal(SIGPIPE, SIG_IGN);
// Flush the hardware buffer on interrupt/term
signal(SIGINT, myterm);
signal(SIGTERM, myterm);
// Process command-line options and arguments
while ((opt = getopt(argc, argv, "d:b:s:e:t:D")) != -1) {
switch (opt) {
case 'd':
capdev = optarg;
break;
case 'b':
bufsize = atof(optarg) * val * 8;
if (bufsize < 33280 || bufsize > 1764000) {
mylog(LOG_ERR, "Invalid buffer. Range is 0.1-5.0 sec.\n");
goto error;
}
break;
case 's':
strcpy(startcmd, optarg);
if (stat(startcmd, &status) != 0) {
mylog(LOG_ERR, "Invalid start command: %s\n", startcmd);
goto error;
}
strcat(startcmd, " &");
break;
case 'e':
strcpy(endcmd, optarg);
if (stat(endcmd, &status) != 0) {
mylog(LOG_ERR, "Invalid end command: %s\n", endcmd);
goto error;
}
strcat(endcmd, " &");
break;
case 't':
silentt = atoi(optarg);
if ((silentt < 1) || (silentt > 32767)) {
mylog(LOG_ERR, "Invalid silence threshold. Range is 1-32767.\n");
goto error;
}
break;
case 'D':
daemonize = 1;
break;
case '?':
err = 1;
mylog(LOG_ERR, "Invalid option: %s\n", argv[optind - 1]);
break;
}
}
if ((optind + 1) != argc) err = 1;
if (err) {
if (!daemonize) {
printf("\nUsage:\n %s [-d arg] [-b arg] [-s arg] "
"[-e arg] [-t arg] [-D] FIFO\n"
" -d : ALSA capture device ['default']\n"
" -b : target buffer in seconds [.5]\n"
" -s : command to run when sound detected\n"
" -e : command to run when silence detected\n"
" -t : silence threshold (1 - 32767, [100])\n"
" -D : daemonize\n"
" FIFO : path to a named pipe\n", argv[0]);
}
goto error;
}
mylog(LOG_INFO, "Starting up.\n");
fifonam = argv[argc - 1];
// Daemonize
if (daemonize) {
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
} else if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0) {
mylog(LOG_ERR, "Error creating new session: %s\n", strerror(errno));
goto error;
}
if (chdir("/") != 0) {
mylog(LOG_ERR, "Error on chdir /: %s\n", strerror(errno));
goto error;
}
}
// Write the pidfile
sprintf(pidfile, "/var/run/cpiped.pid");
sprintf(pidstr, "%d\n", getpid());
pidfd = open(pidfile, O_RDWR|O_CREAT, 0644);
write(pidfd, pidstr, strlen(pidstr));
close(pidfd);
// Open the FIFO for read first, so the open for write succeeds.
readfd = open(fifonam, O_RDONLY | O_NONBLOCK);
if (readfd <= 0) {
mylog(LOG_ERR, "Error opening FIFO for read: %s\n", strerror(errno));
goto error;
}
if (fstat(readfd, &status) != 0) {
mylog(LOG_ERR, "fstat error on FIFO (%s).\n", fifonam);
goto error;
}
if (!S_ISFIFO(status.st_mode)) {
mylog(LOG_ERR, "%s is not a FIFO.\n", fifonam);
goto error;
}
writefd = open(fifonam, O_WRONLY);
if (writefd <= 0) {
mylog(LOG_ERR, "Error opening FIFO for write: %s\n", strerror(errno));
goto error;
}
close(readfd);
// Set the FIFO size to 8192 bytes to minimize latency
fcntl(writefd, F_SETPIPE_SZ, 8192);
// Open PCM device for recording (capture).
rc = snd_pcm_open(&handle, capdev, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
if (rc != 0) {
mylog(LOG_ERR, "Unable to open pcm device: %s\n", snd_strerror(rc));
goto error;
}
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(handle, params);
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(handle, params, 2);
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
// Set period size to 1024 frames.
frames = 1024;
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);
// Write the parameters to the driver
rc = snd_pcm_hw_params(handle, params);
if (rc != 0) {
mylog(LOG_ERR, "Unable to set hw parameters: %s\n", snd_strerror(rc));
goto error;
}
// Determine the elapsed time of one period
snd_pcm_hw_params_get_period_time(params, &capusec, &dir);
// Create a capture buffer large enough to hold one period
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
capsize = frames * 4; // 2 bytes/sample, 2 channels
capbuffer = calloc(capsize, sizeof(char));
scapbuffer = (int16_t*)capbuffer;
// Determine write size
writebytes = capsize - 32 ; // Write a bit less to make sure write controls pace
// Create a ring buffer to handle clock mismatch
bufsize++; // Add one byte to permit determination of empty/full states
buf = calloc(bufsize, sizeof(char));
bufstart = 0;
bufend = 0;
while (1) {
// Determine the amount of buffer used
bufused = bufend - bufstart + (bufend < bufstart) * (bufsize - 1);
//printf("size:%d, start:%d, end:%d, used:%d, wrote:%d\n", bufsize, bufstart, bufend, bufused, wrote);
if (bufused < capsize) {
// Buffer is almost empty, don't send to pipe
mylog(LOG_INFO, "Filling buffer\n");
fillbuf = 1;
}
if (bufused > (int)bufsize - 1 - capsize) {
// Buffer is almost full, don't store captured samples
mylog(LOG_INFO, "Buffer full\n");
buffull = 1;
}
// Resume both capture storage and write to pipe when the buffer reaches half-full
if ((fillbuf == 1) && (bufused > (int)bufsize / 2))
fillbuf = 0;
if ((buffull == 1) && (bufused < (int)bufsize / 2))
buffull = 0;
// Capture samples
if (!wrote) // When not writing, wait a bit between captures.
usleep(capusec * .95);
rc = snd_pcm_readi(handle, capbuffer, frames);
if (rc == -EPIPE) { // EPIPE means overrun
mylog(LOG_NOTICE, "Overrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc == -EAGAIN) {
rc = 0; // Ignore "Not Available" errors
} else if (rc < 0) {
mylog(LOG_ERR, "Capture error: %s\n", snd_strerror(rc));
}
// Compute RMS power level regularly
if (capcount > 10 && rc > 0) { // Approx. every quarter second
power = 0;
for (i = 0; i < rc / 2; i++) {
power += pow(scapbuffer[i], 2) + pow(scapbuffer[i + 1], 2);
}
power = sqrt(power/rc);
capcount = 0;
if (power > silentt) {
soundcount++;
silentcount = 0;
} else {
silentcount++;
soundcount = 0;
}
if (silent && soundcount > 1) {
silent = 0;
mylog(LOG_INFO, "Sound detected. Last two values: %d, %d\n", prevpower, (int)power);
if (strcmp(startcmd, "") != 0) {
mylog(LOG_INFO, "Running '%s'\n", startcmd);
if (system(startcmd) != 0) {
mylog(LOG_ERR, "Error running start command.\n");
}
}
}
if (!silent && silentcount > 55) {
silent = 1;
mylog(LOG_INFO, "Silence detected. Last two values: %d, %d\n", prevpower, (int)power);
if (strcmp(endcmd, "") != 0) {
mylog(LOG_INFO, "Running '%s'\n", endcmd);
if (system(endcmd) != 0) {
mylog(LOG_ERR, "Error running end command.\n");
}
}
}
}
prevpower = power;
capcount++;
if (!buffull && rc > 0) {
// Store samples in buffer
readbytes = rc * 4;
bufendtoend = bufsize - bufend;
if (readbytes <= bufendtoend) {
// No wrap required
memcpy(buf + bufend, capbuffer, readbytes);
} else {
// Wrap required
memcpy(buf + bufend, capbuffer, bufendtoend);
memcpy(buf, capbuffer + bufendtoend, readbytes - bufendtoend);
}
bufend = (bufend + readbytes) % bufsize;
}
wrote = 0;
if (!fillbuf) {
bufstarttoend = bufsize - bufstart;
if (writebytes <= bufstarttoend) { // No wrap required
rc = write(writefd, buf + bufstart, writebytes);
if (rc == writebytes) wrote = 1;
} else { // Wrap required
rc = write(writefd, buf + bufstart, bufstarttoend);
rc = write(writefd, buf, writebytes - bufstarttoend);
if (rc == writebytes - bufstarttoend) wrote = 1;
}
if (wrote) {
bufstart = (bufstart + writebytes) % bufsize;
} else {
bufstart = (bufstart + readbytes) % bufsize; // Keep buffer used constant
}
}
}
exit(EXIT_SUCCESS);
error:
if (readfd > 0) close(readfd);
if (writefd > 0) close(writefd);
mylog(LOG_INFO, "Stopping due to error.");
exit(EXIT_FAILURE);
}