1 | //roarctl.c: |
---|
2 | |
---|
3 | #include <roaraudio.h> |
---|
4 | |
---|
5 | int display_mixer (struct roar_connection * con, int stream); |
---|
6 | |
---|
7 | void usage (void) { |
---|
8 | printf("roarcat [OPTIONS]... COMMAND [OPTS] [COMMAND [OPTS] [COMMAND [OPTS] [...]]]\n"); |
---|
9 | |
---|
10 | printf("\nOptions:\n\n"); |
---|
11 | |
---|
12 | printf(" --server SERVER - Set server hostname\n" |
---|
13 | " --help - Show this help\n" |
---|
14 | ); |
---|
15 | |
---|
16 | printf("\nCommands:\n\n"); |
---|
17 | printf( |
---|
18 | " help - Show this help\n" |
---|
19 | "\n" |
---|
20 | " standby, off - Go into standby mode\n" |
---|
21 | " resume, on - Go into active mode\n" |
---|
22 | " standbymode - Show current standby mode\n" |
---|
23 | " exit - Quits the roard (must be used as last command)\n" |
---|
24 | "\n" |
---|
25 | " volume ID CHAN V0 V1... - Sets volume for stream ID\n" |
---|
26 | " CHAN is the number of channels or 'mono' or 'stereo'\n" |
---|
27 | " if mono or stereo is chosen roarctl trys to set\n" |
---|
28 | " sensfull values for all channels even if the output\n" |
---|
29 | " is has more channels.\n" |
---|
30 | " all other args are the volumes of the channels\n" |
---|
31 | " you may use integer or percent values.\n" |
---|
32 | " percent values can flooding points.\n" |
---|
33 | "\n" |
---|
34 | " kick TYPE ID - Kicks object of TYPE with id ID\n" |
---|
35 | " Types: client stream sample source\n" |
---|
36 | "\n" |
---|
37 | " serveroinfo - Gets Informations about server output\n" |
---|
38 | " listclients - Gets Informations about clients\n" |
---|
39 | " liststreams - Gets Informations about streams\n" |
---|
40 | " allinfo - Get all infos\n" |
---|
41 | ); |
---|
42 | } |
---|
43 | |
---|
44 | void server_oinfo (struct roar_connection * con) { |
---|
45 | struct roar_stream s; |
---|
46 | |
---|
47 | if ( roar_server_oinfo(con, &s) == -1 ) { |
---|
48 | fprintf(stderr, "Error: can not get server output info\n"); |
---|
49 | return; |
---|
50 | } |
---|
51 | |
---|
52 | printf("Stream direction : %s\n", roar_dir2str(s.dir)); |
---|
53 | printf("Server Output rate : %i\n", s.info.rate); |
---|
54 | printf("Server Output bits : %i\n", s.info.bits); |
---|
55 | printf("Server Output channels: %i\n", s.info.channels); |
---|
56 | printf("Server Output codec : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec), |
---|
57 | s.info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
58 | // printf("Server Output rate: %i", s.info.rate); |
---|
59 | } |
---|
60 | |
---|
61 | void list_clients (struct roar_connection * con) { |
---|
62 | int i; |
---|
63 | int num; |
---|
64 | int h; |
---|
65 | int id[ROAR_CLIENTS_MAX]; |
---|
66 | struct roar_client c; |
---|
67 | |
---|
68 | if ( (num = roar_list_clients(con, id, ROAR_CLIENTS_MAX)) == -1 ) { |
---|
69 | fprintf(stderr, "Error: can not get client list\n"); |
---|
70 | return; |
---|
71 | } |
---|
72 | |
---|
73 | for (i = 0; i < num; i++) { |
---|
74 | printf("client %i:\n", id[i]); |
---|
75 | if ( roar_get_client(con, &c, id[i]) == -1 ) { |
---|
76 | fprintf(stderr, "Error: can not get client info\n"); |
---|
77 | continue; |
---|
78 | } |
---|
79 | printf("Player name : %s\n", c.name); |
---|
80 | if ( c.execed != -1 ) |
---|
81 | printf("Execed stream : %i\n", c.execed); |
---|
82 | |
---|
83 | for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++) |
---|
84 | if ( c.streams[h] != -1 ) |
---|
85 | printf("stream : %i\n", c.streams[h]); |
---|
86 | } |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | void list_streams (struct roar_connection * con) { |
---|
91 | int i; |
---|
92 | int num; |
---|
93 | int id[ROAR_STREAMS_MAX]; |
---|
94 | struct roar_stream s; |
---|
95 | |
---|
96 | |
---|
97 | if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) { |
---|
98 | fprintf(stderr, "Error: can not get stream list\n"); |
---|
99 | return; |
---|
100 | } |
---|
101 | |
---|
102 | for (i = 0; i < num; i++) { |
---|
103 | printf("stream %i:\n", id[i]); |
---|
104 | if ( roar_get_stream(con, &s, id[i]) == -1 ) { |
---|
105 | fprintf(stderr, "Error: can not get stream info\n"); |
---|
106 | continue; |
---|
107 | } |
---|
108 | printf("Stream direction : %s\n", roar_dir2str(s.dir)); |
---|
109 | if ( s.pos_rel_id == -1 ) |
---|
110 | printf("Relativ position id : none (stream not synchronized)\n"); |
---|
111 | else |
---|
112 | printf("Relativ position id : %i\n", s.pos_rel_id); |
---|
113 | printf("Input rate : %i\n", s.info.rate); |
---|
114 | printf("Input bits : %i\n", s.info.bits); |
---|
115 | printf("Input channels : %i\n", s.info.channels); |
---|
116 | printf("Input codec : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec), |
---|
117 | s.info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
118 | display_mixer(con, id[i]); |
---|
119 | } |
---|
120 | |
---|
121 | } |
---|
122 | |
---|
123 | int display_mixer (struct roar_connection * con, int stream) { |
---|
124 | int channels; |
---|
125 | struct roar_mixer_settings mixer; |
---|
126 | int i; |
---|
127 | |
---|
128 | if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) { |
---|
129 | fprintf(stderr, "Error: can not get stream mixer info\n"); |
---|
130 | return -1; |
---|
131 | } |
---|
132 | |
---|
133 | for (i = 0; i < channels; i++) |
---|
134 | printf("Mixer volume chan %2i : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35); |
---|
135 | |
---|
136 | return 0; |
---|
137 | } |
---|
138 | |
---|
139 | int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) { |
---|
140 | int chans = 0; |
---|
141 | int id; |
---|
142 | int i; |
---|
143 | int len; |
---|
144 | int old_chans; |
---|
145 | int vol_l, vol_r; |
---|
146 | char * k; |
---|
147 | struct roar_mixer_settings mixer; |
---|
148 | struct roar_mixer_settings old_mixer; |
---|
149 | |
---|
150 | if (*cur + 2 > max) |
---|
151 | return -1; |
---|
152 | |
---|
153 | id = atoi(arg[++(*cur)]); |
---|
154 | |
---|
155 | k = arg[++(*cur)]; |
---|
156 | |
---|
157 | if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) { |
---|
158 | fprintf(stderr, "Error: can not get stream mixer info\n"); |
---|
159 | return -1; |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | if ( strcmp(k, "mono") == 0 && old_chans != 1 ) { |
---|
164 | chans = 1; |
---|
165 | |
---|
166 | if ( *cur + 1 > max ) |
---|
167 | return -1; |
---|
168 | |
---|
169 | k = arg[++(*cur)]; |
---|
170 | len = strlen(k); |
---|
171 | |
---|
172 | if ( k[len - 1] == '%' ) { |
---|
173 | k[len - 1] = 0; |
---|
174 | vol_l = (atof(k)*65535)/100; |
---|
175 | } else { |
---|
176 | vol_l = atoi(k); |
---|
177 | } |
---|
178 | |
---|
179 | for (i = 0; i < old_chans; i++) |
---|
180 | mixer.mixer[i] = vol_l; |
---|
181 | |
---|
182 | chans = old_chans; |
---|
183 | |
---|
184 | } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) { |
---|
185 | chans = 2; |
---|
186 | // printf("mode: stereo; chans=%i, old_chans=%i\n", chans, old_chans); |
---|
187 | ROAR_ERR("mode stereo not supported"); |
---|
188 | return -1; |
---|
189 | } else { |
---|
190 | if ( strcmp(k, "mono") == 0 ) { |
---|
191 | chans = 1; |
---|
192 | } else if ( strcmp(k, "stereo") == 0 ) { |
---|
193 | chans = 2; |
---|
194 | } else { |
---|
195 | chans = atoi(k); |
---|
196 | } |
---|
197 | |
---|
198 | // printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans); |
---|
199 | |
---|
200 | if ( *cur + chans > max ) |
---|
201 | return -1; |
---|
202 | |
---|
203 | for (i = 0; i < chans; i++) { |
---|
204 | k = arg[++(*cur)]; |
---|
205 | len = strlen(k); |
---|
206 | |
---|
207 | if ( k[len - 1] == '%' ) { |
---|
208 | k[len - 1] = 0; |
---|
209 | mixer.mixer[i] = (atof(k)*(int)65535)/100; |
---|
210 | } else { |
---|
211 | mixer.mixer[i] = atoi(k); |
---|
212 | } |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | mixer.scale = 65535; |
---|
217 | |
---|
218 | return roar_set_vol(con, id, &mixer, chans); |
---|
219 | } |
---|
220 | |
---|
221 | int main (int argc, char * argv[]) { |
---|
222 | struct roar_connection con; |
---|
223 | char * server = NULL; |
---|
224 | char * k = NULL; |
---|
225 | int i; |
---|
226 | int t = 0; |
---|
227 | |
---|
228 | for (i = 1; i < argc; i++) { |
---|
229 | k = argv[i]; |
---|
230 | |
---|
231 | if ( strcmp(k, "--server") == 0 ) { |
---|
232 | server = argv[++i]; |
---|
233 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
234 | usage(); |
---|
235 | return 0; |
---|
236 | } else if ( *k == '-' ) { |
---|
237 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
238 | usage(); |
---|
239 | return 1; |
---|
240 | } else { |
---|
241 | break; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | // connect |
---|
246 | |
---|
247 | if ( roar_connect(&con, server) == -1 ) { |
---|
248 | fprintf(stderr, "Error: Can not connect to server\n"); |
---|
249 | return 1; |
---|
250 | } |
---|
251 | |
---|
252 | if ( roar_identify(&con, "roarctl") == -1 ) { |
---|
253 | fprintf(stderr, "Error: Can not identify to server\n"); |
---|
254 | return 1; |
---|
255 | } |
---|
256 | |
---|
257 | if ( i == argc ) { |
---|
258 | fprintf(stderr, "Error: No Commands given\n"); |
---|
259 | return 0; // this is not a fatal error... |
---|
260 | } |
---|
261 | |
---|
262 | for (; i < argc; i++) { |
---|
263 | k = argv[i]; |
---|
264 | // cmd is in k |
---|
265 | |
---|
266 | printf("--- [ %s ] ---\n", k); |
---|
267 | |
---|
268 | if ( !strcmp(k, "help") ) { |
---|
269 | usage(); |
---|
270 | |
---|
271 | |
---|
272 | } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) { |
---|
273 | if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) { |
---|
274 | fprintf(stderr, "Error: can not set mode to standby\n"); |
---|
275 | } else { |
---|
276 | printf("going into standby\n"); |
---|
277 | } |
---|
278 | } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) { |
---|
279 | if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) { |
---|
280 | fprintf(stderr, "Error: can not set mode to active\n"); |
---|
281 | } else { |
---|
282 | printf("going into active mode\n"); |
---|
283 | } |
---|
284 | |
---|
285 | } else if ( !strcmp(k, "exit") ) { |
---|
286 | if ( roar_exit(&con) == -1 ) { |
---|
287 | fprintf(stderr, "Error: can not quit server\n"); |
---|
288 | } else { |
---|
289 | printf("Server quited\n"); |
---|
290 | break; |
---|
291 | } |
---|
292 | |
---|
293 | } else if ( !strcmp(k, "standbymode") ) { |
---|
294 | t = roar_get_standby(&con); |
---|
295 | if ( t == -1 ) { |
---|
296 | fprintf(stderr, "Error: can not get stanby mode\n"); |
---|
297 | } else if ( t == ROAR_STANDBY_ACTIVE ) { |
---|
298 | printf("Server is in standby\n"); |
---|
299 | } else if ( t == ROAR_STANDBY_INACTIVE ) { |
---|
300 | printf("Server is active\n"); |
---|
301 | } else { |
---|
302 | fprintf(stderr, "Error: unknown standby mode: %i\n", t); |
---|
303 | } |
---|
304 | |
---|
305 | } else if ( !strcmp(k, "serveroinfo") ) { |
---|
306 | server_oinfo(&con); |
---|
307 | } else if ( !strcmp(k, "listclients") ) { |
---|
308 | list_clients(&con); |
---|
309 | } else if ( !strcmp(k, "liststreams") ) { |
---|
310 | list_streams(&con); |
---|
311 | } else if ( !strcmp(k, "allinfo") ) { |
---|
312 | server_oinfo(&con); |
---|
313 | printf("\n"); |
---|
314 | list_clients(&con); |
---|
315 | printf("\n"); |
---|
316 | list_streams(&con); |
---|
317 | |
---|
318 | } else if ( !strcmp(k, "kick") ) { |
---|
319 | k = argv[++i]; |
---|
320 | if ( !strcmp(k, "client") ) { |
---|
321 | t = ROAR_OT_CLIENT; |
---|
322 | } else if ( !strcmp(k, "stream") ) { |
---|
323 | t = ROAR_OT_STREAM; |
---|
324 | } else if ( !strcmp(k, "sample") ) { |
---|
325 | t = ROAR_OT_SAMPLE; |
---|
326 | } else if ( !strcmp(k, "source") ) { |
---|
327 | t = ROAR_OT_SOURCE; |
---|
328 | } else { |
---|
329 | fprintf(stderr, "Error: unknown type: %s\n", k); |
---|
330 | continue; |
---|
331 | } |
---|
332 | //t = atoi(argv[i++]); |
---|
333 | if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) { |
---|
334 | fprintf(stderr, "Error: can not kick %s\n", k); |
---|
335 | } else { |
---|
336 | printf("%s kicked\n", k); |
---|
337 | } |
---|
338 | |
---|
339 | } else if ( !strcmp(k, "volume") ) { |
---|
340 | if ( set_mixer(&con, &i, argc, argv) == -1 ) { |
---|
341 | fprintf(stderr, "Error: can not set volume\n"); |
---|
342 | } else { |
---|
343 | printf("volume changed\n"); |
---|
344 | } |
---|
345 | |
---|
346 | } else { |
---|
347 | fprintf(stderr, "Error: invalid command: %s\n", k); |
---|
348 | } |
---|
349 | |
---|
350 | } |
---|
351 | |
---|
352 | roar_disconnect(&con); |
---|
353 | |
---|
354 | return 0; |
---|
355 | } |
---|
356 | |
---|
357 | //ll |
---|