1 | //req.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | /* ckport options: |
---|
27 | * massage <-> object converters: |
---|
28 | * ckport: ignore-symbol: roar_stream_s2m of target libroar0 |
---|
29 | * ckport: ignore-symbol: roar_stream_m2s of target libroar0 |
---|
30 | * ckport: ignore-symbol: roar_ctl_c2m of target libroar0 |
---|
31 | * ckport: ignore-symbol: roar_ctl_ia2m of target libroar0 |
---|
32 | * ckport: ignore-symbol: roar_ctl_m2f of target libroar0 |
---|
33 | * Caps support: |
---|
34 | * ckport: ignore-symbol: roar_caps_from_msg of target libroar0 |
---|
35 | * ckport: ignore-symbol: roar_caps_to_msg of target libroar0 |
---|
36 | * Server info: |
---|
37 | * ckport: ignore-symbol: roar_server_info_to_mes of target libroar0 |
---|
38 | * PASSFH: |
---|
39 | * ckport: ignore-symbol: roar_socket_recv_fh of target libroar0 |
---|
40 | * AUTH: |
---|
41 | * ckport: ignore-symbol: roar_auth_from_mes of target libroar0 |
---|
42 | * ckport: ignore-symbol: roar_auth_to_mes of target libroar0 |
---|
43 | * Events: |
---|
44 | * ckport: ignore-symbol: roar_event_from_blob of target libroar0 |
---|
45 | * Filter: |
---|
46 | * ckport: ignore-symbol: roar_filter_match of target libroar0 |
---|
47 | */ |
---|
48 | |
---|
49 | #include "roard.h" |
---|
50 | |
---|
51 | // include for uname() used by req_on_server_info() |
---|
52 | #ifdef ROAR_HAVE_UNAME |
---|
53 | #include <sys/utsname.h> |
---|
54 | #endif |
---|
55 | |
---|
56 | static void * _dataspace(struct roar_message * mes, char ** data, uint32_t flags[2], size_t len) { |
---|
57 | if ( len <= LIBROAR_BUFFER_MSGDATA ) |
---|
58 | return mes->data; |
---|
59 | |
---|
60 | if ( *data != NULL ) |
---|
61 | free(*data); |
---|
62 | |
---|
63 | *data = malloc(len); |
---|
64 | |
---|
65 | ROAR_DBG("_dataspace(mes=%p, data=%p, flags=%p, len=%llu): *data=%p", mes, data, flags, (long long unsigned int)len, *data); |
---|
66 | |
---|
67 | if ( *data == NULL ) |
---|
68 | return NULL; |
---|
69 | |
---|
70 | flags[1] |= COMMAND_FLAG_OUT_LONGDATA; |
---|
71 | |
---|
72 | return *data; |
---|
73 | } |
---|
74 | |
---|
75 | int req_on_noop (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
76 | mes->cmd = ROAR_CMD_OK; |
---|
77 | mes->pos = g_pos; |
---|
78 | mes->datalen = 0; |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | int req_on_identify (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
83 | struct roar_client_server * cs; |
---|
84 | struct roar_client * c; |
---|
85 | int max_len; |
---|
86 | |
---|
87 | if ( mes->datalen < 1 ) |
---|
88 | return -1; |
---|
89 | |
---|
90 | clients_get_server(client, &cs); |
---|
91 | |
---|
92 | c = ROAR_CLIENT(cs); |
---|
93 | |
---|
94 | if ( mes->data[0] == 1 ) { |
---|
95 | if ( c->pid == -1 ) { |
---|
96 | c->pid = ROAR_NET2HOST32(*(uint32_t*)((mes->data)+1)); |
---|
97 | ROAR_DBG("req_on_identify(): new PID: c->pid = %i", c->pid); |
---|
98 | } |
---|
99 | |
---|
100 | ROAR_DBG("req_on_identify(): final PID: c->pid = %i", c->pid); |
---|
101 | |
---|
102 | max_len = (mes->datalen - 5) < (ROAR_BUFFER_NAME-1) ? (mes->datalen - 5) : (ROAR_BUFFER_NAME-1); |
---|
103 | |
---|
104 | strncpy(c->name, mes->data + 5, max_len); |
---|
105 | c->name[max_len] = 0; |
---|
106 | |
---|
107 | // we set the acclevel to IDENTED here. |
---|
108 | // if it is alreay > IDENTED we reset it anyway |
---|
109 | // as potential auth relevent data changed. |
---|
110 | cs->acclev = ACCLEV_IDENTED; |
---|
111 | |
---|
112 | mes->cmd = ROAR_CMD_OK; |
---|
113 | mes->pos = g_pos; |
---|
114 | mes->datalen = 0; |
---|
115 | |
---|
116 | ROAR_DBG("req_on_identify(*): client=%i, pid=%i", client, c->pid); |
---|
117 | ROAR_DBG("req_on_identify(*) = 0"); |
---|
118 | return 0; |
---|
119 | } |
---|
120 | |
---|
121 | return -1; |
---|
122 | } |
---|
123 | |
---|
124 | int req_on_auth (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
125 | struct roar_error_frame error_frame; |
---|
126 | void * error_data; |
---|
127 | struct roar_message error_mes; |
---|
128 | struct roar_client_server * cs; |
---|
129 | struct roar_auth_message authmes; |
---|
130 | int next = -1; |
---|
131 | int ret; |
---|
132 | |
---|
133 | clients_get_server(client, &cs); |
---|
134 | |
---|
135 | // TODO: add code to support some auth. |
---|
136 | |
---|
137 | if ( roar_auth_from_mes(&authmes, mes, *data) == -1 ) |
---|
138 | return -1; |
---|
139 | |
---|
140 | ROAR_INFO("req_on_auth(client=%i,...): authtype=%s(%i), len=%llu bytes", ROAR_DBG_INFO_VERBOSE, |
---|
141 | client, roar_autht2str(authmes.type), authmes.type, (long long unsigned int)authmes.len); |
---|
142 | |
---|
143 | ret = auth_client_ckeck(cs, &authmes, &next); |
---|
144 | |
---|
145 | if ( ret != 1 ) { |
---|
146 | if ( next != -1 ) { |
---|
147 | memset(&authmes, 0, sizeof(authmes)); |
---|
148 | |
---|
149 | authmes.type = next; |
---|
150 | |
---|
151 | if ( roar_auth_to_mes(&error_mes, NULL, &authmes) == -1 ) |
---|
152 | return -1; |
---|
153 | |
---|
154 | if ( roar_err_int(&error_frame) == -1 ) |
---|
155 | return -1; |
---|
156 | |
---|
157 | error_frame.ra_errno = ROAR_ERROR_PERM; |
---|
158 | error_frame.datalen = error_mes.datalen; |
---|
159 | |
---|
160 | error_data = roar_err_buildmsg(mes, &error_frame); |
---|
161 | |
---|
162 | memcpy(error_data, error_mes.data, error_mes.datalen); |
---|
163 | |
---|
164 | mes->cmd = ROAR_CMD_ERROR; |
---|
165 | mes->pos = g_pos; |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | return -1; |
---|
170 | } |
---|
171 | |
---|
172 | mes->cmd = ROAR_CMD_OK; |
---|
173 | mes->pos = g_pos; |
---|
174 | mes->datalen = 0; |
---|
175 | return 0; |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | int req_on_whoami (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
180 | mes->cmd = ROAR_CMD_OK; |
---|
181 | mes->pos = g_pos; |
---|
182 | mes->datalen = 1; |
---|
183 | mes->data[0] = client; |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | int req_on_new_stream (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
189 | int stream; |
---|
190 | struct roar_stream_server * ss; |
---|
191 | struct roar_stream * s; |
---|
192 | struct roar_stream * source_stream; |
---|
193 | struct roar_audio_info * info; |
---|
194 | struct roar_audio_info * source_info; |
---|
195 | |
---|
196 | ROAR_DBG("req_on_new_stream(client=%i, ...): creating stream...", client); |
---|
197 | if ((stream = streams_new()) == -1 ) |
---|
198 | return -1; |
---|
199 | |
---|
200 | ROAR_DBG("req_on_new_stream(client=%i, ...): stream=%i", client, stream); |
---|
201 | |
---|
202 | ROAR_DBG("req_on_new_stream(client=%i, ...): getting stream...", client); |
---|
203 | if ( streams_get(stream, &ss) == -1 ) { |
---|
204 | streams_delete(stream); |
---|
205 | return -1; |
---|
206 | } |
---|
207 | |
---|
208 | s = ROAR_STREAM(ss); |
---|
209 | |
---|
210 | ROAR_DBG("req_on_new_stream(client=%i, ...): set client of stream...", client); |
---|
211 | if ( client_stream_add(client, stream) == -1 ) { |
---|
212 | streams_delete(stream); |
---|
213 | return -1; |
---|
214 | } |
---|
215 | |
---|
216 | ROAR_DBG("req_on_new_stream(client=%i, ...): loading stream from message...", client); |
---|
217 | if ( roar_stream_m2s(s, mes) == -1 ) { |
---|
218 | streams_delete(stream); |
---|
219 | return -1; |
---|
220 | } |
---|
221 | |
---|
222 | ROAR_DBG("req_on_new_stream(client=%i, ...): setting id and codec of stream...", client); |
---|
223 | ROAR_STREAM(s)->id = stream; // roar_stream_m2s() resets this |
---|
224 | ROAR_STREAM_SERVER(s)->codec_orgi = ROAR_STREAM(s)->info.codec; |
---|
225 | |
---|
226 | ROAR_DBG("req_on_new_stream(client=%i, ...): setting direction stream...", client); |
---|
227 | // int streams_set_dir (int id, int dir, int defaults) |
---|
228 | if ( streams_set_dir(stream, ROAR_STREAM(s)->dir, 1) == -1 ) { |
---|
229 | streams_delete(stream); |
---|
230 | return -1; |
---|
231 | } |
---|
232 | |
---|
233 | ROAR_DBG("req_on_new_stream(client=%i, ...): checking mixer settings...", client); |
---|
234 | if ( mes->stream != -1 && mes->stream != ss->mixer_stream ) { |
---|
235 | streams_delete(stream); |
---|
236 | return -1; |
---|
237 | } |
---|
238 | |
---|
239 | ROAR_DBG("req_on_new_stream(client=%i, ...): setting up direction specific stream settings...", client); |
---|
240 | switch (ROAR_STREAM(s)->dir) { |
---|
241 | case ROAR_DIR_LIGHT_IN: |
---|
242 | case ROAR_DIR_LIGHT_OUT: |
---|
243 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
244 | info = &(ROAR_STREAM(s)->info); |
---|
245 | |
---|
246 | info->channels = 0; |
---|
247 | info->bits = 0; |
---|
248 | info->rate = 0; |
---|
249 | #else |
---|
250 | streams_delete(stream); |
---|
251 | return -1; |
---|
252 | #endif |
---|
253 | |
---|
254 | break; |
---|
255 | case ROAR_DIR_MIDI_IN: |
---|
256 | case ROAR_DIR_MIDI_OUT: |
---|
257 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
258 | info = &(ROAR_STREAM(s)->info); |
---|
259 | |
---|
260 | info->channels = ROAR_MIDI_CHANNELS_DEFAULT; |
---|
261 | info->bits = ROAR_MIDI_BITS; |
---|
262 | info->rate = 0; |
---|
263 | #else |
---|
264 | streams_delete(stream); |
---|
265 | return -1; |
---|
266 | #endif |
---|
267 | |
---|
268 | break; |
---|
269 | |
---|
270 | case ROAR_DIR_RAW_IN: |
---|
271 | #ifndef ROAR_WITHOUT_DCOMP_RAW |
---|
272 | if ( ROAR_STREAM(s)->pos_rel_id == -1 || |
---|
273 | ROAR_STREAM(s)->pos_rel_id == stream || |
---|
274 | streams_get_dir(ROAR_STREAM(s)->pos_rel_id) != ROAR_DIR_RAW_OUT |
---|
275 | ) { |
---|
276 | ROAR_STREAM(s)->pos_rel_id = -1; // force this here as it will try to delete itself while deleting |
---|
277 | // in case rel_id == stream |
---|
278 | streams_delete(stream); |
---|
279 | return -1; |
---|
280 | } |
---|
281 | #else |
---|
282 | case ROAR_DIR_RAW_OUT: |
---|
283 | streams_delete(stream); |
---|
284 | return -1; |
---|
285 | #endif |
---|
286 | |
---|
287 | break; |
---|
288 | case ROAR_DIR_THRU: |
---|
289 | |
---|
290 | if ( ROAR_STREAM(s)->pos_rel_id == -1 || ROAR_STREAM(s)->pos_rel_id == stream ) { |
---|
291 | ROAR_STREAM(s)->pos_rel_id = -1; // force this here as it will try to delete itself while deleting |
---|
292 | // in case rel_id == stream |
---|
293 | streams_delete(stream); |
---|
294 | return -1; |
---|
295 | } |
---|
296 | |
---|
297 | if ( streams_get_clientobj(ROAR_STREAM(s)->pos_rel_id, &source_stream) == -1 ) { |
---|
298 | streams_delete(stream); |
---|
299 | return -1; |
---|
300 | } |
---|
301 | |
---|
302 | info = &(ROAR_STREAM(s)->info); |
---|
303 | source_info = &(ROAR_STREAM(source_stream)->info); |
---|
304 | |
---|
305 | info->channels = source_info->channels; |
---|
306 | info->bits = source_info->bits; |
---|
307 | info->rate = source_info->rate; |
---|
308 | info->codec = source_info->codec; |
---|
309 | ROAR_STREAM_SERVER(s)->codec_orgi = ROAR_STREAM_SERVER(source_stream)->codec_orgi; |
---|
310 | |
---|
311 | break; |
---|
312 | case ROAR_DIR_FILTER: |
---|
313 | info = &(ROAR_STREAM(s)->info); |
---|
314 | |
---|
315 | if ( ROAR_STREAM(s)->pos_rel_id == -1 ) { |
---|
316 | source_info = g_sa; |
---|
317 | } else { |
---|
318 | if ( streams_get_clientobj(ROAR_STREAM(s)->pos_rel_id, &source_stream) == -1 ) { |
---|
319 | streams_delete(stream); |
---|
320 | return -1; |
---|
321 | } |
---|
322 | source_info = &(ROAR_STREAM(source_stream)->info); |
---|
323 | } |
---|
324 | |
---|
325 | if ( info->channels != source_info->channels || info->bits != source_info->bits || |
---|
326 | info->codec != source_info->codec || info->rate != source_info->rate ) { |
---|
327 | // the stream parameters don't match the one of the stream being filtered. |
---|
328 | // -> delete and reject the stream. |
---|
329 | streams_delete(stream); |
---|
330 | return -1; |
---|
331 | } |
---|
332 | break; |
---|
333 | } |
---|
334 | |
---|
335 | ROAR_DBG("req_on_new_stream(client=%i, ...): returning (OK, stream=%i)...", client, stream); |
---|
336 | |
---|
337 | mes->cmd = ROAR_CMD_OK; |
---|
338 | mes->stream = stream; |
---|
339 | mes->datalen = 0; |
---|
340 | |
---|
341 | return 0; |
---|
342 | } |
---|
343 | |
---|
344 | int req_on_exec_stream (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
345 | struct roar_message m; |
---|
346 | struct roar_client * c; |
---|
347 | struct roar_connection con; |
---|
348 | int16_t * d = (int16_t*)mes->data; |
---|
349 | int sock; |
---|
350 | int i; |
---|
351 | int r; |
---|
352 | |
---|
353 | ROAR_DBG("req_on_exec_stream(client=%i, mes={stream=%i,...},...): execing stream", client, mes->stream); |
---|
354 | |
---|
355 | |
---|
356 | if ( mes->stream != -1 ) { |
---|
357 | if ( streams_is_ready(mes->stream) ) { |
---|
358 | flags[1] |= COMMAND_FLAG_OUT_CLOSECON; |
---|
359 | } else { |
---|
360 | if ( (r = client_stream_exec(client, mes->stream)) == -1 ) |
---|
361 | return -1; |
---|
362 | } |
---|
363 | |
---|
364 | ROAR_DBG("req_on_exec_stream(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream); |
---|
365 | mes->cmd = ROAR_CMD_OK; |
---|
366 | mes->datalen = 0; |
---|
367 | return 0; |
---|
368 | } |
---|
369 | |
---|
370 | ROAR_DBG("req_on_exec_stream(client=%i,...): non stream exec", client); |
---|
371 | |
---|
372 | if ( mes->datalen < 4*2 ) |
---|
373 | return -1; |
---|
374 | |
---|
375 | ROAR_DBG("req_on_exec_stream(client=%i,...) = ?", client); |
---|
376 | |
---|
377 | for (i = 0; i < 4; i++) { |
---|
378 | d[i] = ROAR_NET2HOST16(d[i]); |
---|
379 | } |
---|
380 | |
---|
381 | if ( d[0] != 0 ) // version |
---|
382 | return -1; |
---|
383 | |
---|
384 | if ( d[1] != 0 ) // flags |
---|
385 | return -1; |
---|
386 | |
---|
387 | ROAR_DBG("req_on_exec_stream(client=%i,...) = ?", client); |
---|
388 | |
---|
389 | memset(&m, 0, sizeof(m)); |
---|
390 | |
---|
391 | m.cmd = ROAR_CMD_OK; |
---|
392 | |
---|
393 | flags[1] |= COMMAND_FLAG_OUT_NOSEND; |
---|
394 | |
---|
395 | c = ROAR_CLIENT(g_clients[client]); |
---|
396 | |
---|
397 | sock = c->fh; |
---|
398 | |
---|
399 | if ( sock == -1 ) |
---|
400 | return -1; |
---|
401 | |
---|
402 | ROAR_DBG("req_on_exec_stream(client=%i,...) = ?", client); |
---|
403 | |
---|
404 | roar_connect_fh(&con, sock); |
---|
405 | |
---|
406 | roar_send_message(&con, &m, NULL); |
---|
407 | |
---|
408 | clients_set_fh(client, -1); |
---|
409 | clients_delete(client); |
---|
410 | |
---|
411 | ROAR_DBG("req_on_exec_stream(client=%i,...): calling clients_new_from_fh(): sock=%i, d={?, ?, %i, %i}", client, sock, (int)d[2], (int)d[3]); |
---|
412 | clients_new_from_fh(sock, d[2], d[3], 1); |
---|
413 | |
---|
414 | ROAR_DBG("req_on_exec_stream(client=%i,...) = 0", client); |
---|
415 | |
---|
416 | return 0; |
---|
417 | } |
---|
418 | |
---|
419 | int req_on_con_stream (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
420 | char host[80] = {0}; |
---|
421 | int port = 0; |
---|
422 | int type; |
---|
423 | int fh; |
---|
424 | int len; |
---|
425 | |
---|
426 | if ( mes->datalen < 4 ) |
---|
427 | return -1; |
---|
428 | |
---|
429 | if ( *(mes->data) != 0 ) |
---|
430 | return -1; |
---|
431 | |
---|
432 | if ( mes->datalen > 80 ) // we do not support long messages here |
---|
433 | return -1; |
---|
434 | |
---|
435 | type = (unsigned)mes->data[1]; |
---|
436 | port = ROAR_NET2HOST16(((uint16_t*)mes->data)[1]); |
---|
437 | |
---|
438 | len = mes->datalen - 4; |
---|
439 | |
---|
440 | strncpy(host, &(mes->data[4]), len); |
---|
441 | host[len] = 0; |
---|
442 | |
---|
443 | if ( type > ROAR_SOCKET_TYPE_MAX ) |
---|
444 | return -1; |
---|
445 | |
---|
446 | if ( type == ROAR_SOCKET_TYPE_FILE ) // disabled because of security resons |
---|
447 | return -1; |
---|
448 | |
---|
449 | if ( type == ROAR_SOCKET_TYPE_FORK ) // why should we connect to ourself? |
---|
450 | return -1; |
---|
451 | |
---|
452 | ROAR_DBG("req_on_con_stream(*): CONNECT(type=%i, host='%s', port=%i)", type, host, port); |
---|
453 | |
---|
454 | if ( (fh = roar_socket_open(ROAR_SOCKET_MODE_CONNECT, type, host, port)) == -1 ) |
---|
455 | return -1; |
---|
456 | |
---|
457 | if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) { |
---|
458 | close(fh); |
---|
459 | return 1; |
---|
460 | } |
---|
461 | |
---|
462 | mes->datalen = 0; |
---|
463 | mes->cmd = ROAR_CMD_OK; |
---|
464 | |
---|
465 | return 0; |
---|
466 | } |
---|
467 | |
---|
468 | int req_on_passfh (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
469 | int sock = clients_get_fh(client); |
---|
470 | int16_t * d = (int16_t*)mes->data; |
---|
471 | struct roard_listen * lsock; |
---|
472 | int listening; |
---|
473 | int fh; |
---|
474 | int i; |
---|
475 | |
---|
476 | ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...) = ?", client, mes->stream); |
---|
477 | |
---|
478 | if ( (fh = roar_socket_recv_fh(sock, NULL, NULL)) == -1 ) { |
---|
479 | ROAR_WARN("req_on_passfh(client=%i, mes={stream=%i,...},...): was unabled to get filehandle from remote end. bad.", client, mes->stream); |
---|
480 | ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (ERROR)...", client, mes->stream); |
---|
481 | return -1; |
---|
482 | } |
---|
483 | |
---|
484 | ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): fh=%i", client, mes->stream, fh); |
---|
485 | |
---|
486 | if ( mes->stream != -1 ) { // stream pass: |
---|
487 | ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): This is a stream passfh", client, mes->stream); |
---|
488 | |
---|
489 | if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) { |
---|
490 | close(fh); |
---|
491 | ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (ERROR)...", client, mes->stream); |
---|
492 | return -1; |
---|
493 | } |
---|
494 | |
---|
495 | ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream); |
---|
496 | |
---|
497 | mes->datalen = 0; |
---|
498 | mes->cmd = ROAR_CMD_OK; |
---|
499 | |
---|
500 | return 0; |
---|
501 | } |
---|
502 | |
---|
503 | // non-stream pass: |
---|
504 | |
---|
505 | ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): This is a client passfh", client, mes->stream); |
---|
506 | |
---|
507 | /* |
---|
508 | 0: Version, 16 |
---|
509 | 1: Flags, 16 |
---|
510 | 2: Protocol, 16 |
---|
511 | 3: Byteorder, 16 |
---|
512 | Options... |
---|
513 | */ |
---|
514 | |
---|
515 | if ( mes->datalen < 4*2 ) |
---|
516 | return -1; |
---|
517 | |
---|
518 | for (i = 0; i < 4; i++) { |
---|
519 | d[i] = ROAR_NET2HOST16(d[i]); |
---|
520 | } |
---|
521 | |
---|
522 | if ( d[0] != 0 ) // version |
---|
523 | return -1; |
---|
524 | |
---|
525 | listening = d[1] & ROAR_CLIENTPASS_FLAG_LISTEN; |
---|
526 | |
---|
527 | if ( listening ) |
---|
528 | d[1] -= ROAR_CLIENTPASS_FLAG_LISTEN; |
---|
529 | |
---|
530 | if ( d[1] != 0 ) // flags |
---|
531 | return -1; |
---|
532 | |
---|
533 | if ( listening ) { |
---|
534 | if ( get_listen(&lsock, NULL) == -1 ) { |
---|
535 | close(fh); |
---|
536 | return -1; |
---|
537 | } |
---|
538 | |
---|
539 | roar_vio_open_fh_socket(&(lsock->sock), fh); |
---|
540 | lsock->used = 1; |
---|
541 | lsock->proto = d[2]; |
---|
542 | } else { |
---|
543 | if ( clients_new_from_fh(fh, d[2], d[3], 1) == -1 ) |
---|
544 | return -1; |
---|
545 | } |
---|
546 | |
---|
547 | ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream); |
---|
548 | |
---|
549 | mes->datalen = 0; |
---|
550 | mes->cmd = ROAR_CMD_OK; |
---|
551 | |
---|
552 | return 0; |
---|
553 | } |
---|
554 | |
---|
555 | #ifdef ROAR_SUPPORT_META |
---|
556 | int req_on_set_meta (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
557 | int type; |
---|
558 | int mode; |
---|
559 | int namelen, vallen; |
---|
560 | char val[255+1]; |
---|
561 | char name[ROAR_META_MAX_NAMELEN+1]; |
---|
562 | |
---|
563 | if ( mes->datalen < 3 ) |
---|
564 | return -1; |
---|
565 | |
---|
566 | if ( mes->data[0] != 0 ) // version |
---|
567 | return -1; |
---|
568 | |
---|
569 | mode = (unsigned) mes->data[1]; |
---|
570 | type = (unsigned) mes->data[2]; |
---|
571 | |
---|
572 | ROAR_DBG("req_on_set_meta(*): mode=%i, type=%i", mode, type); |
---|
573 | |
---|
574 | if ( mode == ROAR_META_MODE_CLEAR ) { |
---|
575 | stream_meta_clear(mes->stream); |
---|
576 | mes->datalen = 0; |
---|
577 | mes->cmd = ROAR_CMD_OK; |
---|
578 | return 0; |
---|
579 | } else if ( mode == ROAR_META_MODE_DELETE ) { // unsuppoerted at the moment |
---|
580 | return -1; |
---|
581 | } else if ( mode == ROAR_META_MODE_FINALIZE ) { |
---|
582 | stream_meta_finalize(mes->stream); |
---|
583 | mes->datalen = 0; |
---|
584 | mes->cmd = ROAR_CMD_OK; |
---|
585 | return 0; |
---|
586 | } else if ( mode == ROAR_META_MODE_SET || mode == ROAR_META_MODE_ADD ) { |
---|
587 | if ( mes->datalen < 5 ) |
---|
588 | return -1; |
---|
589 | |
---|
590 | namelen = (unsigned) mes->data[3]; |
---|
591 | vallen = (unsigned) mes->data[4]; |
---|
592 | |
---|
593 | ROAR_DBG("req_on_set_meta(*): namelen=%i, vallen=%i", namelen, vallen); |
---|
594 | |
---|
595 | if ( mes->datalen < (5 + namelen + vallen) ) |
---|
596 | return -1; |
---|
597 | |
---|
598 | if ( namelen > ROAR_META_MAX_NAMELEN ) |
---|
599 | return -1; |
---|
600 | |
---|
601 | strncpy(name, &(mes->data[5]), namelen); |
---|
602 | name[namelen] = 0; |
---|
603 | |
---|
604 | if ( vallen > 255 ) |
---|
605 | return -1; |
---|
606 | |
---|
607 | strncpy(val, &(mes->data[5+namelen]), vallen); |
---|
608 | val[vallen] = 0; |
---|
609 | |
---|
610 | if ( mode == ROAR_META_MODE_SET ) { |
---|
611 | if ( stream_meta_set(mes->stream, type, name, val) == -1 ) |
---|
612 | return -1; |
---|
613 | } else { |
---|
614 | if ( stream_meta_add(mes->stream, type, name, val) == -1 ) |
---|
615 | return -1; |
---|
616 | } |
---|
617 | |
---|
618 | mes->datalen = 0; |
---|
619 | mes->cmd = ROAR_CMD_OK; |
---|
620 | return 0; |
---|
621 | } else { // unknown mode! |
---|
622 | return -1; |
---|
623 | } |
---|
624 | |
---|
625 | return -1; |
---|
626 | } |
---|
627 | |
---|
628 | int req_on_get_meta (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
629 | int vallen; |
---|
630 | int type; |
---|
631 | char val[LIBROAR_BUFFER_MSGDATA-1]; |
---|
632 | |
---|
633 | if ( mes->datalen != 2 ) |
---|
634 | return -1; |
---|
635 | |
---|
636 | if ( mes->data[0] != 0 ) // version |
---|
637 | return -1; |
---|
638 | |
---|
639 | type = (unsigned) mes->data[1]; |
---|
640 | |
---|
641 | if ( stream_meta_get(mes->stream, type, NULL, val, LIBROAR_BUFFER_MSGDATA-2) == -1 ) |
---|
642 | return -1; |
---|
643 | |
---|
644 | vallen = strlen(val); |
---|
645 | |
---|
646 | mes->cmd = ROAR_CMD_OK; |
---|
647 | mes->datalen = 2 + vallen; |
---|
648 | |
---|
649 | mes->data[0] = 0; |
---|
650 | mes->data[1] = (unsigned char) vallen; |
---|
651 | |
---|
652 | val[vallen] = 0; |
---|
653 | |
---|
654 | strncpy(&(mes->data[2]), val, vallen+1); |
---|
655 | |
---|
656 | return 0; |
---|
657 | } |
---|
658 | |
---|
659 | int req_on_list_meta (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
660 | int i; |
---|
661 | int len = 0; |
---|
662 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
663 | |
---|
664 | if ( mes->datalen != 1 ) |
---|
665 | return -1; |
---|
666 | |
---|
667 | if ( mes->data[0] != 0 ) // version |
---|
668 | return -1; |
---|
669 | |
---|
670 | if ( (len = stream_meta_list(mes->stream, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
671 | return -1; |
---|
672 | |
---|
673 | mes->cmd = ROAR_CMD_OK; |
---|
674 | mes->datalen = 1 + len; |
---|
675 | mes->data[0] = 0; |
---|
676 | |
---|
677 | for (i = 0; i < len; i++) |
---|
678 | mes->data[i+1] = types[i]; |
---|
679 | |
---|
680 | return 0; |
---|
681 | } |
---|
682 | #endif |
---|
683 | |
---|
684 | int req_on_server_info (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
685 | #ifdef ROAR_HAVE_UNAME |
---|
686 | struct utsname utsname; |
---|
687 | #endif |
---|
688 | #ifdef ROAR_HAVE_GETVERSIONEX |
---|
689 | OSVERSIONINFO osinfo; |
---|
690 | char buf_release[80]; |
---|
691 | #endif |
---|
692 | struct roar_server_info info; |
---|
693 | uint16_t * d16; |
---|
694 | |
---|
695 | if ( mes->datalen != 4 ) |
---|
696 | return -1; |
---|
697 | |
---|
698 | d16 = (uint16_t*)mes->data; |
---|
699 | |
---|
700 | // check version. |
---|
701 | if ( ROAR_NET2HOST16(d16[0]) != 0 ) |
---|
702 | return -1; |
---|
703 | |
---|
704 | switch (ROAR_NET2HOST16(d16[1])) { |
---|
705 | case ROAR_IT_SERVER: |
---|
706 | memset(&info, 0, sizeof(info)); |
---|
707 | |
---|
708 | // the following if is optimized out by compiler. |
---|
709 | if ( DISTRIBUTION_VERSION_STRING[0] == 0 ) { |
---|
710 | info.version = "roard/" PACKAGE_VERSION " <" DEVICE_VENDOR_STRING ">"; |
---|
711 | } else { |
---|
712 | info.version = "roard/" PACKAGE_VERSION " <" DEVICE_VENDOR_STRING "> (" DISTRIBUTION_VERSION_STRING ")"; |
---|
713 | } |
---|
714 | |
---|
715 | if ( !!strcmp(g_config->location, CONF_DEF_STRING) ) |
---|
716 | info.location = g_config->location; |
---|
717 | |
---|
718 | if ( !!strcmp(g_config->description, CONF_DEF_STRING) ) |
---|
719 | info.description = g_config->description; |
---|
720 | |
---|
721 | #ifdef ROAR_HAVE_UNAME |
---|
722 | if ( uname(&utsname) == 0 ) { |
---|
723 | info.un.sysname = utsname.sysname; |
---|
724 | info.un.release = utsname.release; |
---|
725 | info.un.nodename = utsname.nodename; |
---|
726 | info.un.machine = utsname.machine; |
---|
727 | } |
---|
728 | #endif |
---|
729 | #ifdef ROAR_HAVE_GETVERSIONEX |
---|
730 | osinfo.dwOSVersionInfoSize = sizeof(osinfo); |
---|
731 | if ( GetVersionEx(&osinfo) ) { |
---|
732 | info.un.sysname = "Windows"; |
---|
733 | snprintf(buf_release, sizeof(buf_release), "%i.%i.%i", |
---|
734 | (int)osinfo.dwMajorVersion, (int)osinfo.dwMinorVersion, (int)osinfo.dwBuildNumber); |
---|
735 | buf_release[sizeof(buf_release)-1] = 0; |
---|
736 | info.un.release = buf_release; |
---|
737 | } |
---|
738 | #endif |
---|
739 | |
---|
740 | *data = NULL; |
---|
741 | |
---|
742 | if ( roar_server_info_to_mes(mes, &info, (void**)data) == -1 ) |
---|
743 | return -1; |
---|
744 | |
---|
745 | if ( *data != NULL ) |
---|
746 | flags[1] |= COMMAND_FLAG_OUT_LONGDATA; |
---|
747 | break; |
---|
748 | default: /* unknown request */ |
---|
749 | return -1; |
---|
750 | break; |
---|
751 | } |
---|
752 | |
---|
753 | mes->cmd = ROAR_CMD_OK; |
---|
754 | |
---|
755 | return 0; |
---|
756 | } |
---|
757 | |
---|
758 | int req_on_server_oinfo (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
759 | struct roar_stream s; |
---|
760 | int dir = ROAR_DIR_PLAY; |
---|
761 | int subsys; |
---|
762 | //ROAR_DIR_OUTPUT |
---|
763 | |
---|
764 | if ( mes->datalen != 0 ) { |
---|
765 | if ( mes->datalen != 2 ) |
---|
766 | return -1; |
---|
767 | |
---|
768 | if ( mes->data[0] != 0 ) |
---|
769 | return -1; |
---|
770 | |
---|
771 | dir = mes->data[1]; |
---|
772 | } |
---|
773 | |
---|
774 | subsys = streams_dir2subsys(dir); |
---|
775 | |
---|
776 | memset(&s, 0, sizeof(struct roar_stream)); |
---|
777 | |
---|
778 | s.dir = ROAR_DIR_MIXING; |
---|
779 | s.pos_rel_id = -1; |
---|
780 | |
---|
781 | switch (subsys) { |
---|
782 | case ROAR_SUBSYS_WAVEFORM: |
---|
783 | s.info.rate = g_sa->rate; |
---|
784 | s.info.bits = g_sa->bits; |
---|
785 | s.info.channels = g_sa->channels; |
---|
786 | s.info.codec = g_sa->codec; |
---|
787 | s.pos = g_pos; |
---|
788 | break; |
---|
789 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
790 | case ROAR_SUBSYS_MIDI: |
---|
791 | break; |
---|
792 | #endif |
---|
793 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
794 | case ROAR_SUBSYS_LIGHT: |
---|
795 | s.info.rate = ROAR_OUTPUT_CFREQ; |
---|
796 | s.info.bits = ROAR_LIGHT_BITS; |
---|
797 | s.info.channels = g_light_state.channels; |
---|
798 | break; |
---|
799 | #endif |
---|
800 | #ifndef ROAR_WITHOUT_DCOMP_RAW |
---|
801 | case ROAR_SUBSYS_RAW: |
---|
802 | // no need to set anything here. |
---|
803 | break; |
---|
804 | #endif |
---|
805 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
806 | case ROAR_SUBSYS_RDTCS: |
---|
807 | s.info.rate = ROAR_OUTPUT_CFREQ; |
---|
808 | s.info.bits = ROAR_RDTCS_BITS; |
---|
809 | s.info.channels = ROAR_RDTCS_CHANNELS; |
---|
810 | s.info.codec = ROAR_RDTCS_CODEC; |
---|
811 | break; |
---|
812 | #endif |
---|
813 | default: |
---|
814 | return -1; |
---|
815 | break; |
---|
816 | } |
---|
817 | |
---|
818 | if ( roar_stream_s2m(&s, mes) == -1 ) |
---|
819 | return -1; |
---|
820 | |
---|
821 | mes->cmd = ROAR_CMD_OK; |
---|
822 | |
---|
823 | return 0; |
---|
824 | } |
---|
825 | |
---|
826 | int req_on_caps (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
827 | struct roar_caps caps; |
---|
828 | struct roar_stds * stds; |
---|
829 | size_t i; |
---|
830 | |
---|
831 | if ( roar_caps_from_msg(&caps, mes, *data) == -1 ) |
---|
832 | return -1; |
---|
833 | |
---|
834 | // handle the data from the caps command here... |
---|
835 | |
---|
836 | if ( !(caps.flags & ROAR_CF_REQUEST) ) { |
---|
837 | mes->cmd = ROAR_CMD_OK; |
---|
838 | mes->datalen = 0; |
---|
839 | return 0; |
---|
840 | } |
---|
841 | |
---|
842 | mes->datalen = 0; |
---|
843 | |
---|
844 | switch (caps.type) { |
---|
845 | case ROAR_CT_STANDARDS: |
---|
846 | if ( (stds = roar_stds_new(g_caps_stds.stds_len)) == NULL ) |
---|
847 | return -1; |
---|
848 | |
---|
849 | for ( i = 0; i < stds->stds_len; i++) { |
---|
850 | stds->stds[i] = ROAR_HOST2NET32(g_caps_stds.stds[i]); |
---|
851 | } |
---|
852 | |
---|
853 | caps.data = stds->stds; |
---|
854 | caps.len = stds->stds_len * 4; |
---|
855 | // TODO: add support for **data. |
---|
856 | if ( roar_caps_to_msg(mes, &caps, NULL) == -1 ) { |
---|
857 | roar_stds_free(stds); |
---|
858 | return -1; |
---|
859 | } |
---|
860 | roar_stds_free(stds); |
---|
861 | break; |
---|
862 | default: |
---|
863 | return -1; |
---|
864 | break; |
---|
865 | } |
---|
866 | |
---|
867 | mes->cmd = ROAR_CMD_OK; |
---|
868 | |
---|
869 | return 0; |
---|
870 | } |
---|
871 | |
---|
872 | int req_on_get_standby (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
873 | mes->cmd = ROAR_CMD_OK; |
---|
874 | mes->pos = g_pos; |
---|
875 | mes->datalen = 2; |
---|
876 | |
---|
877 | *((uint16_t*)mes->data) = ROAR_HOST2NET16((unsigned) g_standby); |
---|
878 | |
---|
879 | return 0; |
---|
880 | } |
---|
881 | |
---|
882 | int req_on_set_standby (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
883 | if ( mes->datalen != 2 ) |
---|
884 | return -1; |
---|
885 | |
---|
886 | g_standby = ROAR_NET2HOST16(*((uint16_t*)mes->data)); |
---|
887 | |
---|
888 | mes->cmd = ROAR_CMD_OK; |
---|
889 | mes->pos = g_pos; |
---|
890 | mes->datalen = 0; |
---|
891 | |
---|
892 | return 0; |
---|
893 | } |
---|
894 | |
---|
895 | int req_on_exit (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
896 | int term = 0; |
---|
897 | |
---|
898 | if ( mes->datalen == 1 ) |
---|
899 | term = mes->data[0]; |
---|
900 | |
---|
901 | mes->cmd = ROAR_CMD_OK; |
---|
902 | mes->pos = g_pos; |
---|
903 | mes->datalen = 0; |
---|
904 | |
---|
905 | ROAR_DBG("req_on_exit(*): term=%i", term); |
---|
906 | |
---|
907 | if ( term ) { |
---|
908 | cleanup_listen_socket(1); |
---|
909 | } else { |
---|
910 | alive = 0; |
---|
911 | } |
---|
912 | |
---|
913 | return 0; |
---|
914 | } |
---|
915 | |
---|
916 | int req_on_list_clients(int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
917 | unsigned char filter, cmp; |
---|
918 | uint32_t id; |
---|
919 | int clients[ROAR_CLIENTS_MAX]; |
---|
920 | int i, c = 0; |
---|
921 | |
---|
922 | if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 ) |
---|
923 | return -1; |
---|
924 | |
---|
925 | // TODO: add code to support filter |
---|
926 | if ( filter != ROAR_CTL_FILTER_ANY ) |
---|
927 | return -1; |
---|
928 | |
---|
929 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
930 | if ( g_clients[i] != NULL ) { |
---|
931 | clients[c++] = i; |
---|
932 | } |
---|
933 | } |
---|
934 | |
---|
935 | roar_ctl_ia2m(mes, clients, c); |
---|
936 | |
---|
937 | mes->cmd = ROAR_CMD_OK; |
---|
938 | |
---|
939 | return 0; |
---|
940 | } |
---|
941 | int req_on_list_streams(int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
942 | unsigned char filter, cmp; |
---|
943 | uint32_t id; |
---|
944 | int streams[ROAR_STREAMS_MAX]; |
---|
945 | int i, c = 0; |
---|
946 | int match; |
---|
947 | |
---|
948 | if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 ) |
---|
949 | return -1; |
---|
950 | |
---|
951 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
952 | if ( g_streams[i] == NULL ) |
---|
953 | continue; |
---|
954 | |
---|
955 | match = 0; |
---|
956 | |
---|
957 | switch (filter) { |
---|
958 | case ROAR_CTL_FILTER_ANY: |
---|
959 | match = 1; |
---|
960 | break; |
---|
961 | case ROAR_CTL_FILTER_DIR: |
---|
962 | match = roar_filter_match(cmp, id, ROAR_STREAM(g_streams[i])->dir); |
---|
963 | break; |
---|
964 | default: // unsupported filter... |
---|
965 | return -1; |
---|
966 | break; |
---|
967 | } |
---|
968 | |
---|
969 | if ( match ) |
---|
970 | streams[c++] = i; |
---|
971 | } |
---|
972 | |
---|
973 | roar_ctl_ia2m(mes, streams, c); |
---|
974 | |
---|
975 | mes->cmd = ROAR_CMD_OK; |
---|
976 | |
---|
977 | return 0; |
---|
978 | } |
---|
979 | |
---|
980 | int req_on_get_client (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
981 | struct roar_client * c; |
---|
982 | |
---|
983 | if ( mes->datalen != 1 ) |
---|
984 | return -1; |
---|
985 | |
---|
986 | if ( clients_get(mes->data[0], &c) == -1 ) |
---|
987 | return -1; |
---|
988 | |
---|
989 | mes->cmd = ROAR_CMD_OK; |
---|
990 | |
---|
991 | return roar_ctl_c2m(mes, c); |
---|
992 | } |
---|
993 | |
---|
994 | int req_on_get_stream (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
995 | struct roar_stream_server * s; |
---|
996 | |
---|
997 | if ( mes->datalen != 1 ) |
---|
998 | return -1; |
---|
999 | |
---|
1000 | if ( streams_get(mes->data[0], &s) == -1 ) |
---|
1001 | return -1; |
---|
1002 | |
---|
1003 | mes->cmd = ROAR_CMD_OK; |
---|
1004 | mes->stream = mes->data[0]; |
---|
1005 | |
---|
1006 | return roar_stream_s2m(ROAR_STREAM(s), mes); |
---|
1007 | } |
---|
1008 | |
---|
1009 | int req_on_get_stream_para (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
1010 | struct roar_stream * s; |
---|
1011 | struct roar_stream_server * ss; |
---|
1012 | struct roar_audio_info * audio_info; |
---|
1013 | struct roar_stream_ltm * ltm; |
---|
1014 | uint16_t * d = (uint16_t *) mes->data; |
---|
1015 | int64_t * d64 = ( int64_t *) mes->data; |
---|
1016 | int64_t * d64ptr; |
---|
1017 | int i, h, k; |
---|
1018 | char * str; |
---|
1019 | size_t needed; |
---|
1020 | int test, bits; |
---|
1021 | |
---|
1022 | ROAR_DBG("req_on_get_stream_para(client=%i, mes=%p{.stream=%i, .datalen=%i,...}, data=%p, flags=%p) = ?", client, mes, (int)mes->stream, (int)mes->datalen, data, flags); |
---|
1023 | |
---|
1024 | if ( mes->datalen < 4 ) |
---|
1025 | return -1; |
---|
1026 | |
---|
1027 | for (i = 0; i < 2; i++) { |
---|
1028 | d[i] = ROAR_NET2HOST16(d[i]); |
---|
1029 | } |
---|
1030 | |
---|
1031 | if ( d[0] != 0 ) { |
---|
1032 | ROAR_WARN("req_on_get_stream_para(*): unsupported command version: %i, %i", (int)d[0], (int)d[1]); |
---|
1033 | return -1; |
---|
1034 | } |
---|
1035 | |
---|
1036 | switch (d[1]) { |
---|
1037 | case ROAR_STREAM_PARA_INFO: |
---|
1038 | if ( streams_get(mes->stream, &ss) == -1 ) { |
---|
1039 | ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream); |
---|
1040 | return -1; |
---|
1041 | } |
---|
1042 | |
---|
1043 | if ( streams_calc_delay(mes->stream) == -1 ) { |
---|
1044 | ROAR_WARN("req_on_get_stream_para(*): can not calc delay for stream %i", mes->stream); |
---|
1045 | } |
---|
1046 | |
---|
1047 | s = ROAR_STREAM(ss); |
---|
1048 | |
---|
1049 | audio_info = &(s->info); |
---|
1050 | |
---|
1051 | mes->datalen = 2*12; |
---|
1052 | |
---|
1053 | d[ 2] = ROAR_OUTPUT_CALC_OUTBUFSIZE(audio_info); |
---|
1054 | d[ 3] = ss->pre_underruns; |
---|
1055 | d[ 4] = ss->post_underruns; |
---|
1056 | d[ 5] = ss->codec_orgi; |
---|
1057 | d[ 6] = (ss->flags & 0xFFFF) | (ss->primary ? ROAR_FLAG_PRIMARY : 0) | (ss->driver_id != -1 ? ROAR_FLAG_OUTPUT : 0); |
---|
1058 | d[ 7] = ss->delay/1000; |
---|
1059 | d[ 8] = ss->state; |
---|
1060 | d[ 9] = (ss->flags & 0xFFFF0000) >> 16; |
---|
1061 | d[10] = ss->mixer_stream; |
---|
1062 | d[11] = ss->role; |
---|
1063 | |
---|
1064 | ROAR_DBG("req_on_get_stream_para(*): ss->driver_id=%i", ss->driver_id); |
---|
1065 | |
---|
1066 | ROAR_DBG("req_on_get_stream_para(*): delay=%i, send delay=%i", ss->delay, d[7]); |
---|
1067 | |
---|
1068 | for (i = 0; i < mes->datalen/2; i++) { |
---|
1069 | d[i] = ROAR_HOST2NET16(d[i]); |
---|
1070 | } |
---|
1071 | |
---|
1072 | mes->pos = s->pos; |
---|
1073 | break; |
---|
1074 | |
---|
1075 | case ROAR_STREAM_PARA_NAME: |
---|
1076 | str = streams_get_name(mes->stream); |
---|
1077 | |
---|
1078 | if ( str == NULL ) |
---|
1079 | return -1; |
---|
1080 | |
---|
1081 | mes->datalen = 4 + strlen(str); |
---|
1082 | |
---|
1083 | if ( mes->datalen > LIBROAR_BUFFER_MSGDATA ) |
---|
1084 | return -1; |
---|
1085 | |
---|
1086 | strncpy(((char*)&(mes->data))+4, str, mes->datalen); |
---|
1087 | |
---|
1088 | d[0] = ROAR_HOST2NET16(d[0]); |
---|
1089 | d[1] = ROAR_HOST2NET16(d[1]); |
---|
1090 | break; |
---|
1091 | |
---|
1092 | case ROAR_STREAM_PARA_CHANMAP: |
---|
1093 | if ( streams_get(mes->stream, &ss) == -1 ) { |
---|
1094 | ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream); |
---|
1095 | return -1; |
---|
1096 | } |
---|
1097 | |
---|
1098 | s = ROAR_STREAM(ss); |
---|
1099 | |
---|
1100 | memcpy(&(mes->data[4]), ss->chanmap.in, s->info.channels); |
---|
1101 | mes->datalen = 2*2 + s->info.channels; |
---|
1102 | |
---|
1103 | d[0] = ROAR_HOST2NET16(d[0]); |
---|
1104 | d[1] = ROAR_HOST2NET16(d[1]); |
---|
1105 | break; |
---|
1106 | |
---|
1107 | case ROAR_STREAM_PARA_LTM: |
---|
1108 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM request...", client); |
---|
1109 | |
---|
1110 | if ( mes->datalen < (6 * 2) ) { |
---|
1111 | ROAR_ERR("req_on_get_stream_para(client=%i, ...): LTM request of client is corruped: message too short", client); |
---|
1112 | return -1; |
---|
1113 | } |
---|
1114 | |
---|
1115 | for (i = 2; i < mes->datalen/2; i++) { |
---|
1116 | d[i] = ROAR_NET2HOST16(d[i]); |
---|
1117 | } |
---|
1118 | |
---|
1119 | if ( d[2] != ROAR_LTM_SST_GET_RAW ) { |
---|
1120 | ROAR_ERR("req_on_get_stream_para(client=%i, ...): LTM request of client is corruped: unknown LTM subtype: %i", client, (int)d[2]); |
---|
1121 | return -1; |
---|
1122 | } |
---|
1123 | |
---|
1124 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM request of type GET_RAW", client); |
---|
1125 | |
---|
1126 | test = d[5]; |
---|
1127 | bits = 0; |
---|
1128 | while (test) { |
---|
1129 | if ( test & 0x1 ) |
---|
1130 | bits++; |
---|
1131 | |
---|
1132 | test >>= 1; |
---|
1133 | } |
---|
1134 | |
---|
1135 | needed = 0; |
---|
1136 | |
---|
1137 | if ( mes->stream == -1 ) { |
---|
1138 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM multi-stream request...", client); |
---|
1139 | |
---|
1140 | for (i = 6; i < mes->datalen/2; i++) { |
---|
1141 | if ( (ltm = streams_ltm_get(d[i], d[5], d[3])) == NULL ) |
---|
1142 | return -1; |
---|
1143 | |
---|
1144 | needed += ltm->channels; |
---|
1145 | } |
---|
1146 | } else { |
---|
1147 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM single-stream request for stream %i...", client, mes->stream); |
---|
1148 | if ( (ltm = streams_ltm_get(mes->stream, d[5], d[3])) == NULL ) |
---|
1149 | return -1; |
---|
1150 | |
---|
1151 | needed = ltm->channels; |
---|
1152 | } |
---|
1153 | |
---|
1154 | needed *= bits; |
---|
1155 | |
---|
1156 | needed += mes->stream == -1 ? (mes->datalen/2) - 6 : 1; |
---|
1157 | |
---|
1158 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): data size for answer is %i 64 bit sub-packets", client, (int)needed); |
---|
1159 | |
---|
1160 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data); |
---|
1161 | d64 = _dataspace(mes, data, flags, needed * 8); |
---|
1162 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data); |
---|
1163 | |
---|
1164 | if ( (d = roar_mm_malloc(mes->datalen)) == NULL ) |
---|
1165 | return -1; |
---|
1166 | |
---|
1167 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): d=%p, data=%p{%p}", client, d, data, *data); |
---|
1168 | |
---|
1169 | |
---|
1170 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data); |
---|
1171 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data); |
---|
1172 | memcpy(d, mes->data, mes->datalen); |
---|
1173 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data); |
---|
1174 | |
---|
1175 | d64ptr = d64; |
---|
1176 | |
---|
1177 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data); |
---|
1178 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data); |
---|
1179 | |
---|
1180 | if ( mes->stream == -1 ) { |
---|
1181 | for (i = 6; i < mes->datalen/2; i++) { |
---|
1182 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data); |
---|
1183 | |
---|
1184 | if ( (ltm = streams_ltm_get(d[i], d[5], d[3])) == NULL ) |
---|
1185 | return -1; |
---|
1186 | |
---|
1187 | *d64ptr = ltm->channels & 0xFFFF; |
---|
1188 | d64ptr++; |
---|
1189 | |
---|
1190 | for (h = 0; h < ltm->channels; h++) { |
---|
1191 | for (k = 0; k < ROAR_LTM_MTBITS; k++) { |
---|
1192 | if ( d[5] & (1<<k) ) { |
---|
1193 | switch (1<<k) { |
---|
1194 | case ROAR_LTM_MT_RMS: |
---|
1195 | *d64ptr = ltm->cur[h].rms; |
---|
1196 | break; |
---|
1197 | default: |
---|
1198 | ROAR_ERR("req_on_get_stream_para(client=%i, ...): client requets unknown MT for LTM: bit %i", client, k); |
---|
1199 | } |
---|
1200 | d64ptr++; |
---|
1201 | } |
---|
1202 | } |
---|
1203 | } |
---|
1204 | } |
---|
1205 | } else { |
---|
1206 | if ( (ltm = streams_ltm_get(mes->stream, d[5], d[3])) == NULL ) |
---|
1207 | return -1; |
---|
1208 | |
---|
1209 | *d64ptr = ltm->channels & 0xFFFF; |
---|
1210 | d64ptr++; |
---|
1211 | |
---|
1212 | for (h = 0; h < ltm->channels; h++) { |
---|
1213 | for (k = 0; k < ROAR_LTM_MTBITS; k++) { |
---|
1214 | if ( d[5] & (1<<k) ) { |
---|
1215 | switch (1<<k) { |
---|
1216 | case ROAR_LTM_MT_RMS: |
---|
1217 | *d64ptr = ltm->cur[h].rms; |
---|
1218 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): rms=%lli to %p", client, (long long int)*d64ptr, d64ptr); |
---|
1219 | break; |
---|
1220 | default: |
---|
1221 | ROAR_ERR("req_on_get_stream_para(client=%i, ...): client requets unknown MT for LTM: bit %i", client, k); |
---|
1222 | } |
---|
1223 | d64ptr++; |
---|
1224 | } |
---|
1225 | } |
---|
1226 | } |
---|
1227 | } |
---|
1228 | |
---|
1229 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data); |
---|
1230 | |
---|
1231 | roar_mm_free(d); |
---|
1232 | |
---|
1233 | for (i = 0; i < needed; i++) { |
---|
1234 | d64[i] = ROAR_HOST2NET64(d64[i]); |
---|
1235 | } |
---|
1236 | |
---|
1237 | mes->datalen = needed * 8; |
---|
1238 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM d64=%p, d64ptr=%p", client, d64, d64ptr); |
---|
1239 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM final message has %i byte of data", client, (int)mes->datalen); |
---|
1240 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data); |
---|
1241 | ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM GET_RAW request: OK. returning...", client); |
---|
1242 | break; |
---|
1243 | |
---|
1244 | default: |
---|
1245 | ROAR_WARN("req_on_get_stream_para(*): unsupported command: %i", d[1]); |
---|
1246 | return -1; |
---|
1247 | } |
---|
1248 | |
---|
1249 | ROAR_DBG("req_on_get_stream_para(client=%i, mes=%p{.stream=%i, .datalen=%i,...}, data=%p, flags=%p) = 0 // returning OK", client, mes, (int)mes->stream, (int)mes->datalen, data, flags); |
---|
1250 | mes->cmd = ROAR_CMD_OK; |
---|
1251 | return 0; |
---|
1252 | } |
---|
1253 | |
---|
1254 | int req_on_set_stream_para (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
1255 | struct roar_stream_server * ss; |
---|
1256 | uint16_t * d = (uint16_t *) mes->data; |
---|
1257 | uint32_t tmp, tmp2, flagstore; |
---|
1258 | int protect = 0; |
---|
1259 | int i; |
---|
1260 | |
---|
1261 | if ( mes->datalen < 2*2 ) |
---|
1262 | return -1; |
---|
1263 | |
---|
1264 | for (i = 0; i < 2; i++) { |
---|
1265 | d[i] = ROAR_NET2HOST16(d[i]); |
---|
1266 | } |
---|
1267 | |
---|
1268 | if ( d[0] != 0 ) |
---|
1269 | return -1; |
---|
1270 | |
---|
1271 | switch (d[1]) { |
---|
1272 | case ROAR_STREAM_PARA_FLAGS: |
---|
1273 | if ( mes->datalen != 2*4 && mes->datalen != 2*5 ) |
---|
1274 | return -1; |
---|
1275 | |
---|
1276 | d[2] = ROAR_NET2HOST16(d[2]); |
---|
1277 | d[3] = ROAR_NET2HOST16(d[3]); |
---|
1278 | |
---|
1279 | ROAR_DBG("req_on_set_stream_para(*): request seems to be valid"); |
---|
1280 | |
---|
1281 | tmp = 0; |
---|
1282 | |
---|
1283 | if ( mes->datalen == 2*5 ) { |
---|
1284 | d[4] = ROAR_NET2HOST16(d[4]); |
---|
1285 | tmp = d[4]; |
---|
1286 | tmp <<= 16; |
---|
1287 | } |
---|
1288 | |
---|
1289 | tmp |= d[3]; |
---|
1290 | |
---|
1291 | if ( d[2] & ROAR_PROTECT_FLAG ) { |
---|
1292 | protect = 1; |
---|
1293 | d[2] -= ROAR_PROTECT_FLAG; |
---|
1294 | } |
---|
1295 | |
---|
1296 | switch (d[2]) { |
---|
1297 | case ROAR_SET_FLAG: |
---|
1298 | if ( streams_set_flag(mes->stream, tmp) == -1 ) |
---|
1299 | return -1; |
---|
1300 | break; |
---|
1301 | case ROAR_RESET_FLAG: |
---|
1302 | if ( streams_reset_flag(mes->stream, tmp) == -1 ) |
---|
1303 | return -1; |
---|
1304 | break; |
---|
1305 | case ROAR_NOOP_FLAG: |
---|
1306 | break; |
---|
1307 | case ROAR_TOGGLE_FLAG: |
---|
1308 | if ( streams_get(mes->stream, &ss) == -1 ) |
---|
1309 | return -1; |
---|
1310 | |
---|
1311 | flagstore = ss->flags; |
---|
1312 | |
---|
1313 | tmp2 = flagstore & tmp; // those are the flags we need to reset. |
---|
1314 | ROAR_DBG("req_on_set_stream_para(*): tmp2=0x%.8lx", (long int)tmp2); |
---|
1315 | if ( tmp2 ) |
---|
1316 | if ( streams_reset_flag(mes->stream, tmp2) == -1 ) |
---|
1317 | return -1; |
---|
1318 | |
---|
1319 | tmp2 = (flagstore ^ tmp) & tmp; // those are the flags we need to set. |
---|
1320 | ROAR_DBG("req_on_set_stream_para(*): tmp2=0x%.8lx", (long int)tmp2); |
---|
1321 | if ( tmp2 ) |
---|
1322 | if ( streams_set_flag(mes->stream, tmp2) == -1 ) |
---|
1323 | return -1; |
---|
1324 | break; |
---|
1325 | default: |
---|
1326 | return -1; |
---|
1327 | break; |
---|
1328 | } |
---|
1329 | |
---|
1330 | ROAR_DBG("req_on_set_stream_para(*): protect=%i", protect); |
---|
1331 | if ( protect ) |
---|
1332 | if ( streams_protect_flag(mes->stream, tmp) == -1 ) |
---|
1333 | return -1; |
---|
1334 | |
---|
1335 | break; |
---|
1336 | case ROAR_STREAM_PARA_CHANMAP: |
---|
1337 | if ( streams_set_map(mes->stream, &(mes->data[4]), mes->datalen - 4) == -1 ) |
---|
1338 | return -1; |
---|
1339 | break; |
---|
1340 | case ROAR_STREAM_PARA_ROLE: |
---|
1341 | if ( mes->datalen != 2*3 ) |
---|
1342 | return -1; |
---|
1343 | |
---|
1344 | d[2] = ROAR_NET2HOST16(d[2]); |
---|
1345 | |
---|
1346 | if ( streams_set_role(mes->stream, d[2]) == -1 ) |
---|
1347 | return -1; |
---|
1348 | break; |
---|
1349 | case ROAR_STREAM_PARA_LTM: |
---|
1350 | if ( mes->datalen < (6 * 2) ) |
---|
1351 | return -1; |
---|
1352 | |
---|
1353 | for (i = 2; i < mes->datalen/2; i++) { |
---|
1354 | d[i] = ROAR_NET2HOST16(d[i]); |
---|
1355 | } |
---|
1356 | |
---|
1357 | if ( mes->stream == -1 ) { |
---|
1358 | for (i = 6; i < mes->datalen/2; i++) |
---|
1359 | if ( streams_ltm_ctl(d[i], d[5], d[3], d[2]) == -1 ) |
---|
1360 | return -1; |
---|
1361 | } else { |
---|
1362 | if ( streams_ltm_ctl(mes->stream, d[5], d[3], d[2]) == -1 ) |
---|
1363 | return -1; |
---|
1364 | } |
---|
1365 | break; |
---|
1366 | default: |
---|
1367 | ROAR_WARN("req_on_set_stream_para(*): unsupported command version: %i, %i", d[0], d[1]); |
---|
1368 | return -1; |
---|
1369 | break; |
---|
1370 | } |
---|
1371 | |
---|
1372 | mes->cmd = ROAR_CMD_OK; |
---|
1373 | mes->datalen = 0; |
---|
1374 | |
---|
1375 | return 0; |
---|
1376 | } |
---|
1377 | |
---|
1378 | int req_on_kick (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
1379 | struct roar_stream_server * ss; |
---|
1380 | uint16_t * info = (uint16_t *) mes->data; |
---|
1381 | int is_stream = 0; |
---|
1382 | |
---|
1383 | if ( mes->datalen != 4 ) |
---|
1384 | return -1; |
---|
1385 | |
---|
1386 | info[0] = ROAR_NET2HOST16(info[0]); |
---|
1387 | info[1] = ROAR_NET2HOST16(info[1]); |
---|
1388 | |
---|
1389 | switch (info[0]) { |
---|
1390 | case ROAR_OT_CLIENT: |
---|
1391 | clients_delete(info[1]); |
---|
1392 | break; |
---|
1393 | case ROAR_OT_STREAM: |
---|
1394 | is_stream = 1; |
---|
1395 | break; |
---|
1396 | case ROAR_OT_SOURCE: |
---|
1397 | if ( streams_get_flag(info[1], ROAR_FLAG_SOURCE) != 1 ) |
---|
1398 | return -1; |
---|
1399 | is_stream = 1; |
---|
1400 | break; |
---|
1401 | case ROAR_OT_OUTPUT: |
---|
1402 | if ( streams_get(info[1], &ss) == -1 ) |
---|
1403 | return -1; |
---|
1404 | |
---|
1405 | if ( ss->driver_id == -1 ) |
---|
1406 | return -1; |
---|
1407 | |
---|
1408 | is_stream = 1; |
---|
1409 | break; |
---|
1410 | case ROAR_OT_MIXER: |
---|
1411 | if ( streams_get(info[1], &ss) == -1 ) |
---|
1412 | return -1; |
---|
1413 | |
---|
1414 | if ( ROAR_STREAM(ss)->dir != ROAR_DIR_MIXING ) |
---|
1415 | return -1; |
---|
1416 | |
---|
1417 | is_stream = 1; |
---|
1418 | break; |
---|
1419 | case ROAR_OT_BRIDGE: |
---|
1420 | if ( streams_get(info[1], &ss) == -1 ) |
---|
1421 | return -1; |
---|
1422 | |
---|
1423 | if ( ROAR_STREAM(ss)->dir != ROAR_DIR_BRIDGE ) |
---|
1424 | return -1; |
---|
1425 | |
---|
1426 | is_stream = 1; |
---|
1427 | break; |
---|
1428 | default: |
---|
1429 | /* TODO: those types should be handled, too: |
---|
1430 | #define ROAR_OT_SAMPLE 4 |
---|
1431 | #define ROAR_OT_LISTEN 8 |
---|
1432 | #define ROAR_OT_ACTION 9 |
---|
1433 | #define ROAR_OT_MSGQUEUE 10 |
---|
1434 | #define ROAR_OT_MSGBUS 11 |
---|
1435 | */ |
---|
1436 | return -1; |
---|
1437 | break; |
---|
1438 | } |
---|
1439 | |
---|
1440 | if ( is_stream ) { |
---|
1441 | if ( streams_get_flag(info[1], ROAR_FLAG_IMMUTABLE) == 1 ) |
---|
1442 | return -1; |
---|
1443 | |
---|
1444 | streams_delete(info[1]); |
---|
1445 | } |
---|
1446 | |
---|
1447 | mes->cmd = ROAR_CMD_OK; |
---|
1448 | mes->datalen = 0; |
---|
1449 | |
---|
1450 | return 0; |
---|
1451 | } |
---|
1452 | |
---|
1453 | int req_on_attach (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
1454 | uint16_t * info = (uint16_t *) mes->data; |
---|
1455 | |
---|
1456 | if ( mes->datalen < 6 ) |
---|
1457 | return -1; |
---|
1458 | |
---|
1459 | info[0] = ROAR_NET2HOST16(info[0]); |
---|
1460 | info[1] = ROAR_NET2HOST16(info[1]); |
---|
1461 | info[2] = ROAR_NET2HOST16(info[2]); |
---|
1462 | |
---|
1463 | if ( info[0] != 0 ) |
---|
1464 | return -1; |
---|
1465 | |
---|
1466 | if ( info[1] == ROAR_ATTACH_SIMPLE ) { |
---|
1467 | if ( client_stream_move(info[2], mes->stream) == -1 ) |
---|
1468 | return -1; |
---|
1469 | } else { |
---|
1470 | return -1; |
---|
1471 | } |
---|
1472 | |
---|
1473 | mes->cmd = ROAR_CMD_OK; |
---|
1474 | mes->datalen = 0; |
---|
1475 | |
---|
1476 | return 0; |
---|
1477 | } |
---|
1478 | |
---|
1479 | int req_on_set_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
1480 | struct roar_mixer_settings tmpmixer; |
---|
1481 | struct roar_stream_server * s; |
---|
1482 | uint16_t * info = (uint16_t *) mes->data; |
---|
1483 | uint16_t version; |
---|
1484 | uint16_t scale = 65535; |
---|
1485 | int stream; |
---|
1486 | int i; |
---|
1487 | int chans; |
---|
1488 | |
---|
1489 | ROAR_DBG("req_on_set_vol(*) = ?"); |
---|
1490 | ROAR_DBG("req_on_set_vol(*): mes->datalen=%i", mes->datalen); |
---|
1491 | |
---|
1492 | if ( mes->datalen < (4*2) ) |
---|
1493 | return -1; |
---|
1494 | |
---|
1495 | version = ROAR_NET2HOST16(info[0]); |
---|
1496 | ROAR_DBG("req_on_set_vol(*): version=%i", (int)version); |
---|
1497 | |
---|
1498 | switch (version) { |
---|
1499 | case 0: |
---|
1500 | stream = ROAR_NET2HOST16(info[1]); |
---|
1501 | break; |
---|
1502 | case 1: |
---|
1503 | stream = mes->stream; |
---|
1504 | scale = ROAR_NET2HOST16(info[1]); |
---|
1505 | break; |
---|
1506 | default: |
---|
1507 | return -1; |
---|
1508 | break; |
---|
1509 | } |
---|
1510 | ROAR_DBG("req_on_set_vol(*): stream=%i", stream); |
---|
1511 | |
---|
1512 | if ( scale == 0 ) |
---|
1513 | return -1; |
---|
1514 | |
---|
1515 | // TODO: change this code. |
---|
1516 | // we should not directly change the stream object but use some stream_*()-func |
---|
1517 | // for that job. |
---|
1518 | |
---|
1519 | if ( stream < 0 || stream >= ROAR_STREAMS_MAX ) |
---|
1520 | return -1; |
---|
1521 | |
---|
1522 | s = g_streams[stream]; |
---|
1523 | |
---|
1524 | if ( s == NULL ) |
---|
1525 | return -1; |
---|
1526 | |
---|
1527 | ROAR_DBG("req_on_set_vol(*): s=%p", s); |
---|
1528 | |
---|
1529 | info[2] = ROAR_NET2HOST16(info[2]); |
---|
1530 | |
---|
1531 | switch (info[2]) { |
---|
1532 | case ROAR_SET_VOL_ALL: |
---|
1533 | chans = (mes->datalen/2) - 3; |
---|
1534 | ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ALL, channes=%i", chans); |
---|
1535 | |
---|
1536 | if ( chans >= ROAR_MAX_CHANNELS ) |
---|
1537 | return -1; |
---|
1538 | |
---|
1539 | ROAR_DBG("req_on_set_vol(*): mixer at %p", s->mixer.mixer); |
---|
1540 | |
---|
1541 | for (i = 0; i < chans; i++) { |
---|
1542 | s->mixer.mixer[i] = ROAR_NET2HOST16(info[i+3]); |
---|
1543 | ROAR_DBG("req_on_set_vol(*): channel %i: %i", i, ROAR_NET2HOST16(info[i+3])); |
---|
1544 | } |
---|
1545 | |
---|
1546 | s->mixer.scale = scale; |
---|
1547 | |
---|
1548 | ROAR_DBG("req_on_set_vol(*): mixer changed!"); |
---|
1549 | |
---|
1550 | break; |
---|
1551 | case ROAR_SET_VOL_ONE: |
---|
1552 | ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ONE"); |
---|
1553 | if ( ROAR_NET2HOST16(info[3]) >= ROAR_MAX_CHANNELS ) |
---|
1554 | return -1; |
---|
1555 | |
---|
1556 | s->mixer.mixer[ROAR_NET2HOST16(info[3])] = ROAR_NET2HOST16(info[4]); |
---|
1557 | |
---|
1558 | s->mixer.scale = scale; |
---|
1559 | break; |
---|
1560 | case ROAR_SET_VOL_UNMAPPED: |
---|
1561 | chans = (mes->datalen/2) - 3; |
---|
1562 | |
---|
1563 | if ( chans >= ROAR_MAX_CHANNELS ) |
---|
1564 | return -1; |
---|
1565 | |
---|
1566 | memcpy(&tmpmixer, &(s->mixer), sizeof(tmpmixer)); |
---|
1567 | |
---|
1568 | for (i = 0; i < chans; i++) { |
---|
1569 | tmpmixer.mixer[i] = ROAR_NET2HOST16(info[i+3]); |
---|
1570 | } |
---|
1571 | |
---|
1572 | tmpmixer.scale = scale; |
---|
1573 | |
---|
1574 | if ( roar_conv_volume(&(s->mixer), &tmpmixer, ROAR_STREAM(s)->info.channels, chans) == -1 ) |
---|
1575 | return -1; |
---|
1576 | |
---|
1577 | break; |
---|
1578 | case ROAR_SET_VOL_MS: |
---|
1579 | default: |
---|
1580 | return -1; |
---|
1581 | } |
---|
1582 | |
---|
1583 | if ( streams_set_mixer(stream) == -1 ) |
---|
1584 | return -1; |
---|
1585 | |
---|
1586 | mes->cmd = ROAR_CMD_OK; |
---|
1587 | mes->datalen = 0; |
---|
1588 | |
---|
1589 | return 0; |
---|
1590 | } |
---|
1591 | |
---|
1592 | int req_on_get_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
1593 | uint16_t * info = (uint16_t *) mes->data; |
---|
1594 | uint16_t version = -1; |
---|
1595 | int stream; |
---|
1596 | struct roar_stream_server * s; |
---|
1597 | int i; |
---|
1598 | int chans; |
---|
1599 | |
---|
1600 | ROAR_DBG("req_on_get_vol(*) = ?"); |
---|
1601 | ROAR_DBG("req_on_get_vol(*): mes->datalen=%i", mes->datalen); |
---|
1602 | |
---|
1603 | if ( mes->datalen < 2 ) { |
---|
1604 | return -1; |
---|
1605 | } |
---|
1606 | |
---|
1607 | version = ROAR_NET2HOST16(info[0]); |
---|
1608 | |
---|
1609 | switch (version) { |
---|
1610 | case 0: |
---|
1611 | if ( mes->datalen < (2*2) ) |
---|
1612 | return -1; |
---|
1613 | |
---|
1614 | stream = ROAR_NET2HOST16(info[1]); |
---|
1615 | break; |
---|
1616 | case 1: |
---|
1617 | stream = mes->stream; |
---|
1618 | break; |
---|
1619 | default: |
---|
1620 | return -1; |
---|
1621 | break; |
---|
1622 | } |
---|
1623 | |
---|
1624 | ROAR_DBG("req_on_get_vol(*): stream=%i", stream); |
---|
1625 | |
---|
1626 | // TODO: change this code. |
---|
1627 | // we should not directly change the stream object but use some stream_*()-func |
---|
1628 | // for that job. |
---|
1629 | |
---|
1630 | if ( stream < 0 || stream >= ROAR_STREAMS_MAX ) |
---|
1631 | return -1; |
---|
1632 | |
---|
1633 | s = g_streams[stream]; |
---|
1634 | |
---|
1635 | if ( s == NULL ) |
---|
1636 | return -1; |
---|
1637 | |
---|
1638 | ROAR_DBG("req_on_get_vol(*): s=%p", s); |
---|
1639 | |
---|
1640 | // ok, we have everything |
---|
1641 | |
---|
1642 | info[0] = ROAR_HOST2NET16(version); |
---|
1643 | |
---|
1644 | switch (version) { |
---|
1645 | case 0: |
---|
1646 | info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels); |
---|
1647 | |
---|
1648 | for (i = 0; i < chans; i++) |
---|
1649 | info[2+i] = ROAR_HOST2NET16(s->mixer.mixer[i]); |
---|
1650 | |
---|
1651 | mes->datalen = (2 + chans)*2; |
---|
1652 | break; |
---|
1653 | case 1: |
---|
1654 | info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels); |
---|
1655 | info[2] = ROAR_HOST2NET16(s->mixer.scale); |
---|
1656 | info[3] = ROAR_HOST2NET16(s->mixer.rpg_mul); |
---|
1657 | info[4] = ROAR_HOST2NET16(s->mixer.rpg_div); |
---|
1658 | |
---|
1659 | for (i = 0; i < chans; i++) |
---|
1660 | info[5+i] = ROAR_HOST2NET16(s->mixer.mixer[i]); |
---|
1661 | |
---|
1662 | mes->datalen = (5 + chans)*2; |
---|
1663 | break; |
---|
1664 | default: |
---|
1665 | return -1; |
---|
1666 | break; |
---|
1667 | } |
---|
1668 | |
---|
1669 | mes->cmd = ROAR_CMD_OK; |
---|
1670 | |
---|
1671 | return 0; |
---|
1672 | } |
---|
1673 | |
---|
1674 | int req_on_add_data (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
1675 | struct roar_buffer * b; |
---|
1676 | void * buf; |
---|
1677 | |
---|
1678 | if ( roar_buffer_new_data(&b, mes->datalen, &buf) == -1 ) { |
---|
1679 | ROAR_ERR("req_on_add_data(*): Can not alloc buffer space!"); |
---|
1680 | ROAR_DBG("req_on_add_data(*) = -1"); |
---|
1681 | return -1; |
---|
1682 | } |
---|
1683 | |
---|
1684 | if ( data == NULL ) { |
---|
1685 | memcpy(buf, mes->data, mes->datalen); |
---|
1686 | } else { |
---|
1687 | memcpy(buf, *data, mes->datalen); |
---|
1688 | } |
---|
1689 | |
---|
1690 | if ( stream_add_buffer(mes->stream, b) == -1 ) { |
---|
1691 | roar_buffer_free(b); |
---|
1692 | return -1; |
---|
1693 | } |
---|
1694 | |
---|
1695 | mes->cmd = ROAR_CMD_OK_STOP; |
---|
1696 | mes->datalen = 0; |
---|
1697 | |
---|
1698 | return 0; |
---|
1699 | } |
---|
1700 | |
---|
1701 | int req_on_beep (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
1702 | struct roar_beep bs; |
---|
1703 | int16_t * info = (int16_t*)mes->data; |
---|
1704 | int stream; |
---|
1705 | |
---|
1706 | memset(&bs, 0, sizeof(bs)); |
---|
1707 | |
---|
1708 | if ( mes->datalen > 0 ) { |
---|
1709 | if ( mes->datalen < 2 ) |
---|
1710 | return -1; |
---|
1711 | |
---|
1712 | if ( ROAR_NET2HOST16(info[0]) != 0 ) /* version */ |
---|
1713 | return -1; |
---|
1714 | |
---|
1715 | if ( mes->datalen != 8*2 ) |
---|
1716 | return -1; |
---|
1717 | |
---|
1718 | bs.vol = ROAR_NET2HOST16(info[1]); |
---|
1719 | bs.time = ROAR_NET2HOST16(info[2]); |
---|
1720 | bs.freq = ROAR_NET2HOST16(info[3]); |
---|
1721 | bs.type = ROAR_NET2HOST16(info[4]); |
---|
1722 | bs.x = ROAR_NET2HOST16(info[5]); |
---|
1723 | bs.y = ROAR_NET2HOST16(info[6]); |
---|
1724 | bs.z = ROAR_NET2HOST16(info[7]); |
---|
1725 | } |
---|
1726 | |
---|
1727 | if ( (stream = beep_start(client, &bs)) == -1 ) |
---|
1728 | return -1; |
---|
1729 | |
---|
1730 | mes->stream = stream; |
---|
1731 | mes->cmd = ROAR_CMD_OK_STOP; |
---|
1732 | mes->datalen = 0; |
---|
1733 | |
---|
1734 | return 0; |
---|
1735 | } |
---|
1736 | |
---|
1737 | int req_on_wait (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) { |
---|
1738 | uint16_t * u16 = (uint16_t*)mes->data; |
---|
1739 | struct roar_event events[4]; |
---|
1740 | size_t left, tmp; |
---|
1741 | size_t num = 0; |
---|
1742 | void * vp = mes->data; |
---|
1743 | |
---|
1744 | vp += 4; |
---|
1745 | |
---|
1746 | // check for complet header... |
---|
1747 | if ( mes->datalen < 4 ) |
---|
1748 | return -1; |
---|
1749 | |
---|
1750 | u16[0] = ROAR_NET2HOST16(u16[0]); |
---|
1751 | u16[1] = ROAR_NET2HOST16(u16[1]); |
---|
1752 | |
---|
1753 | // do we support version and flags? |
---|
1754 | if ( u16[0] != 0 || u16[1] != 0 ) |
---|
1755 | return -1; |
---|
1756 | |
---|
1757 | memset(events, 0, sizeof(events)); |
---|
1758 | |
---|
1759 | left = mes->datalen - 4; |
---|
1760 | |
---|
1761 | while (left) { |
---|
1762 | tmp = left; |
---|
1763 | if ( roar_event_from_blob(&(events[num]), vp, &tmp) == -1 ) |
---|
1764 | return -1; |
---|
1765 | |
---|
1766 | vp += tmp; |
---|
1767 | left -= tmp; |
---|
1768 | num++; |
---|
1769 | } |
---|
1770 | |
---|
1771 | if ( clients_wait(client, events, num) == -1 ) |
---|
1772 | return -1; |
---|
1773 | |
---|
1774 | flags[1] |= COMMAND_FLAG_OUT_NOSEND; |
---|
1775 | |
---|
1776 | return 0; |
---|
1777 | } |
---|
1778 | |
---|
1779 | //ll |
---|