forked from marcoevang/camilladsp-setrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetrate_main.c
214 lines (181 loc) · 5.99 KB
/
setrate_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
////////////////////////////////////////////////////
//
// camilladsp-setrate
// Automatic sample rate switcher for CamillaDSP
//
// setrate_main.c
// Main program
//
////////////////////////////////////////////////////
#include <libwebsockets.h>
#include <getopt.h>
#include "setrate.h"
extern char device_name[]; // Name of the capture device
extern struct lws_context *context; // Websocket context
extern int server_port; // Server IP port
extern char server_address[]; // Server IP address
extern states state; // Global state
int upsampling_factor; // Upsampling factor
int rate; // Sample rate
char level[10];
int logmask;
// Command line options
static int timestamp = FALSE;
static int syslog = FALSE;
static int device = FALSE;
static int address = FALSE;
static int port = FALSE;
static int loglevel = FALSE;
int upsampling = FALSE;
int capture = FALSE;
static struct option long_options[] =
{
{"timestamp", no_argument, ×tamp, TRUE},
{"syslog", no_argument, &syslog, TRUE},
{"capture", no_argument, &capture, TRUE},
{"device", required_argument, &device, TRUE},
{"address", required_argument, &address, TRUE},
{"port", required_argument, &port, TRUE},
{"loglevel", required_argument, &loglevel, TRUE},
{"help", no_argument, NULL, 0},
{"version", no_argument, NULL, 0},
{"upsampling",required_argument, &upsampling,TRUE},
{0, 0, 0, 0}
};
/////////////////////////////////////////////
// MAIN PROGRAM
/////////////////////////////////////////////
int main(int argc, char* argv[])
{
pid_t child_pid; // pid of child process
int err;
int ret;
int option;
// Evaluate options
opterr = 0;
while (TRUE)
{
int option_index = 0;
option = getopt_long_only(argc, argv, "", long_options, &option_index);
if (option == -1)
break;
if (option == '?')
{
printf("\nError: invalid option %s\n", argv[optind-1]);
print_usage(argv[0]);
exit(FAIL);
}
if (!strcmp(long_options[option_index].name, "help"))
{
print_usage(argv[0]);
exit(SUCCESS);
}
else if (!strcmp(long_options[option_index].name, "version"))
{
printf("\n%s v%s\n\n", argv[0], VERSION);
exit(SUCCESS);
}
else if (!strcmp(long_options[option_index].name, "device"))
strcpy(device_name, optarg);
else if (!strcmp(long_options[option_index].name, "address"))
strcpy(server_address, optarg);
else if (!strcmp(long_options[option_index].name, "port"))
server_port = atoi(optarg);
else if (!strcmp(long_options[option_index].name, "loglevel"))
strcpy(level, optarg);
else if (!strcmp(long_options[option_index].name, "upsampling"))
{
upsampling_factor = atoi(optarg);
if (upsampling_factor <= 0)
{
printf("\nError: invalid option %s\n", argv[optind-1]);
printf(" upsampling factor must be a positive integer\n\n");
print_usage(argv[0]);
exit(FAIL);
}
}
}
// --capture and --upsampling cannot be used at the same time
if (capture && upsampling)
{
printf("\nError: --capture and --upsampling cannot be used at the same time\n");
print_usage(argv[0]);
exit(FAIL);
}
// Device name defaults to hw:UAC2Gadget
if (!device)
strcpy(device_name, "hw:UAC2Gadget");
// Server address defaults to localhost
if (!address)
strcpy(server_address, "localhost");
// Server port defaults to 1234
if (!port)
server_port = 1234;
// Set the log level mask
// ERR is the default
if (loglevel)
{
if (!strcmp(level, "off"))
logmask = 0;
if (!strcmp(level, "err"))
logmask = ERR;
else if (!strcmp(level, "warn"))
logmask = ERR | WARN;
else if (!strcmp(level, "user"))
logmask = ERR | WARN | USER;
else if (!strcmp(level, "notice"))
logmask = ERR | WARN | USER | NOTICE;
else
{
printf("Invalid log level: %s\n", level);
print_usage(argv[0]);
exit(FAIL);
}
}
else
logmask = ERR;
// Set libwebsockets log level
if (syslog)
lws_set_log_level(logmask, lwsl_emit_syslog);
else
lws_set_log_level(logmask, timestamp ? NULL : lwsl_emit_stderr_notimestamp);
writelog(NOTICE, "%s %s\n", argv[0], VERSION);
writelog(NOTICE, "An automatic sample rate switcher for CamillaDSP\n");
// Initialise the finite-state machine
fsm_init();
// Initialise websocket environment
websocket_init();
// Enable signal catching
signal_control(ENABLE);
// Initialise alsa control
alsa_init();
err = 0;
// Request websocket servicing
while (err >= 0)
err = lws_service(context, 0);
// Destroy websocket context
lws_context_destroy(context);
exit(SUCCESS);
}
/////////////////////////////////////////////
// Print command usage
/////////////////////////////////////////////
void print_usage(char *cmd)
{
printf("%s v%s\n", cmd, VERSION);
printf("An automatic sample rate switcher for CamillaDSP\n");
printf("\nUSAGE:\n %s [FLAGS] [OPTIONS]\n", cmd);
printf("\nFLAGS:\n");
printf(" -c, --capture Update capture_samplerate instead of samplerate\n");
printf(" -t, --timestamp Prepend timestamp to log messages\n");
printf(" -s, --syslog Redirect logging to syslog [if omitted, logging emits to stderr]\n");
printf(" -v, --version Print software version\n");
printf(" -h, --help Print this help\n");
printf("\nOPTIONS:\n");
printf(" -d, --device=<capture device> Set alsa capture device name [default: hw:UAC2Gadget]\n");
printf(" -a, --address=<address> Set server IP address [default: localhost]\n");
printf(" -p, --port=<port> Set server IP port [default: 1234]\n");
printf(" -u, --upsampling=<factor> Set upsampling factor [default: 1]\n");
printf(" -l, --loglevel=<log level> Set log level [values: err, warn, user, notice, off. Default: err]\n");
printf("\n");
}