-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
262 lines (249 loc) · 6.89 KB
/
main.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
/*
* Audio Coffin - A simple audio recorder/logger on top of Jack,
* libsndfile and libsoxr. Main Programm
*
* Copyright (C) 2016 Nick Kossifidis <[email protected]>
*
* 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
* 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/>.
*/
#include "acoffin.h"
#include <string.h> /* For memset() */
#include <limits.h> /* For PATH_MAX */
#include <stdio.h> /* For printf/fprintf/perror */
#include <stdlib.h> /* For exit() */
#include <errno.h> /* For EINVAL */
#include <sys/stat.h> /* For stat() etc */
#include <pwd.h> /* For getpuid() etc */
void
usage(char *name)
{
printf("Audio Coffin a simple audio recorder and logger for Jack\n");
printf("\nUsage: %s -h or [<parameter> <value>] pairs\n", name);
printf("\nParameters:\n"
"\t-h\t\tShow this list\n"
"\t-p <string>\tSet output directory for storing files (default: ~/Recordings and ~/AudioLogs)\n"
"\t-m <int>\tSet operation mode, valid values are 1 for recorder (default) and 2 for logger'\n"
"\t-t <int>\tSet time interval in mins for log rotation (default is 1 hour, max is 24h), only valid for logger'\n"
"\t-s <boolean>\tEnable / disable stereo operation, valid values are 0 and 1 (default)\n"
"\t-g <boolean>\tEnable / disable GUI, valid values are 0 and 1 (default)\n"
"\t-r <int>\tSet output sample rate, default value is 48000\n"
"\t-f <int>\tSet output format, valid values are 1 for FLAC (default) and 2 for Ogg/Vorbis\n"
"\t-q <double>\tSet encoding quality for the vorbis/FLAC encoder, valid values are 0.0 - 1.0 (default: 0.5)\n"
"\t-c <double>\tSet compression level for the vorbis/FLAC encoder, valid values are 0.0 - 1.0 (default: 0.75)\n");
}
int
main(int argc, char *argv[])
{
int ret = 0;
int opt = 0;
double tmp = 0;
struct recorder rcd = { 0 };
char filepath[PATH_MAX] = { 0 };
char *resolved_path = NULL;
struct stat st = {0};
struct passwd *pw = NULL;
const char *homedir = NULL;
/* Set default values */
rcd.storage_path = ".";
rcd.opmode = RECORDER_LIVE;
rcd.logrotate_interval_secs = 60 * 60;
rcd.stereo = 1;
rcd.headless = 0;
rcd.sample_rate = 48000;
rcd.format = RECORDER_FORMAT_FLAC;
rcd.quality = 0.5;
rcd.comp_level = 0.75;
/* Grab user arguments */
while ((opt = getopt(argc, argv, "p:m:t:s:g:r:f:q:c:")) != -1)
switch (opt) {
case 'h':
usage(argv[0]);
exit(0);
break;
case 'p':
snprintf(filepath, PATH_MAX, "%s", optarg);
resolved_path = realpath(filepath, resolved_path);
if (!resolved_path) {
fprintf(stderr,
"Invalid or inaccessible path: %s\n",
optarg);
perror("realpath()");
ret = -errno;
goto cleanup;
} else
rcd.storage_path = resolved_path;
break;
case 'm':
ret = atoi(optarg);
if (!(ret & 0x3)) {
fprintf(stderr, "Invalid operation mode: %s\n",
optarg);
ret = -EINVAL;
goto cleanup;
} else
rcd.opmode =
(ret ==
1) ? RECORDER_LIVE : RECORDER_LOGGER;
break;
case 't':
ret = atoi(optarg);
if (ret > (24 * 60) || ret < 0) {
fprintf(stderr, "Invalid time interval: %s\n",
optarg);
ret = -EINVAL;
goto cleanup;
} else
rcd.logrotate_interval_secs = ret * 60;
break;
case 's':
ret = atoi(optarg);
if (ret > 1 || ret < 0) {
fprintf(stderr,
"Invalid value for stereo setting: %s\n",
optarg);
ret = -EINVAL;
goto cleanup;
} else
rcd.stereo = ret;
break;
case 'g':
ret = atoi(optarg);
if (ret > 1 || ret < 0) {
fprintf(stderr,
"Invalid value for GUI setting: %s\n",
optarg);
ret = -EINVAL;
goto cleanup;
} else
rcd.headless = ret == 0 ? 1 : 0;
break;
case 'r':
ret = atoi(optarg);
/* XXX: More checks ? */
if (ret < 0) {
fprintf(stderr, "Invalid sample rate: %s\n",
optarg);
ret = -EINVAL;
goto cleanup;
} else
rcd.sample_rate = ret;
break;
case 'f':
ret = atoi(optarg);
if (!(ret & 0x3)) {
fprintf(stderr, "Invalid format: %s\n", optarg);
ret = -EINVAL;
goto cleanup;
} else
rcd.format =
(ret ==
1) ? RECORDER_FORMAT_FLAC :
RECORDER_FORMAT_OGG_VORBIS;
break;
case 'q':
tmp = atof(optarg);
if (tmp > 1.0 || tmp < 0.0) {
fprintf(stderr,
"Invalid encoding quality: %s\n",
optarg);
ret = -EINVAL;
goto cleanup;
} else
rcd.quality = tmp;
break;
case 'c':
tmp = atof(optarg);
if (tmp > 1.0 || tmp < 0.0) {
fprintf(stderr,
"Invalid compression level: %s\n",
optarg);
ret = -EINVAL;
goto cleanup;
} else
rcd.comp_level = tmp;
break;
default: /* '?' */
usage(argv[0]);
ret = -EINVAL;
goto cleanup;
}
/* Case user inputs non-option args only */
if (argc > 1 && optind == 1) {
usage(argv[0]);
exit(-EINVAL);
}
/* Create default output directory if it doesn't exist
* and if output directory is not set.
* Use ~/Recordings for recordings and ~/AudioLogs
* for audio logs */
if(!resolved_path) {
pw = getpwuid(getuid());
if(!pw) {
fprintf(stderr, "Unable to get home directory\n");
perror("getpuid()");
ret = -errno;
goto cleanup;
}
homedir = pw->pw_dir;
if (rcd.opmode == RECORDER_LOGGER)
snprintf(filepath, PATH_MAX, "%s/%s", homedir, "AudioLogs");
else
snprintf(filepath, PATH_MAX, "%s/%s", homedir, "Recordings");
/* Cleanup pwd structure asap as it contains user private infos */
memset(pw, 0, sizeof(pw));
if (stat(filepath, &st) == -1) {
ret = mkdir(filepath, 0700);
if (ret < 0) {
fprintf(stderr, "Unable to create output directory %s\n",
filepath);
perror("mkdir()");
ret = -errno;
goto cleanup;
}
}
resolved_path = realpath(filepath, resolved_path);
if (!resolved_path) {
fprintf(stderr,
"Invalid or inaccessible path: %s\n",
filepath);
perror("realpath()");
ret = -errno;
goto cleanup;
} else
rcd.storage_path = resolved_path;
}
/* Initialize the recorder */
ret = recorder_initialize(&rcd);
if (ret < 0)
goto cleanup;
if (!rcd.headless)
ret = gui_initialize(argc, argv, &rcd);
else {
/* Logger starts on recorder_initialize
* with or without gui */
if(rcd.opmode != RECORDER_LOGGER) {
ret = recorder_start(&rcd);
if (ret < 0)
goto cleanup;
}
while (recorder_state)
sleep(1);
}
cleanup:
if (recorder_state)
recorder_cleanup(&rcd);
if(resolved_path)
free(resolved_path);
return ret;
}