source: roaraudio/roard/req.c @ 3729:9350342eaccb

Last change on this file since 3729:9350342eaccb was 3729:9350342eaccb, checked in by phi, 14 years ago

update nnode on client pass

File size: 24.4 KB
Line 
1//req.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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#include "roard.h"
27
28int req_on_noop        (int client, struct roar_message * mes, char * data) {
29 mes->cmd     = ROAR_CMD_OK;
30 mes->datalen = 0;
31 return 0;
32}
33
34int req_on_identify    (int client, struct roar_message * mes, char * data) {
35 struct roar_client * c;
36 int max_len;
37
38 if ( mes->datalen < 1 )
39  return -1;
40
41 clients_get(client, &c);
42
43 if ( mes->data[0] == 1 ) {
44  if ( c->pid == -1 ) {
45   c->pid       = ROAR_NET2HOST32(*(uint32_t*)((mes->data)+1));
46   ROAR_DBG("req_on_identify(): new PID: c->pid = %i", c->pid);
47  }
48
49  ROAR_DBG("req_on_identify(): final PID: c->pid = %i", c->pid);
50
51  max_len = (mes->datalen - 5) < (ROAR_BUFFER_NAME-1) ? (mes->datalen - 5) : (ROAR_BUFFER_NAME-1);
52
53  strncpy(c->name, mes->data + 5, max_len);
54  c->name[max_len] = 0;
55
56  mes->cmd     = ROAR_CMD_OK;
57  mes->datalen = 0;
58
59  ROAR_DBG("req_on_identify(*): client=%i, pid=%i", client, c->pid);
60  ROAR_DBG("req_on_identify(*) = 0");
61  return 0;
62 }
63
64 return -1;
65}
66
67int req_on_auth        (int client, struct roar_message * mes, char * data) {
68 // TODO: add code to support some auth.
69 mes->cmd     = ROAR_CMD_OK;
70 mes->datalen = 0;
71 return 0;
72}
73
74
75int req_on_whoami      (int client, struct roar_message * mes, char * data) {
76 mes->cmd     = ROAR_CMD_OK;
77 mes->datalen = 1;
78 mes->data[0] = client;
79 return 0;
80}
81
82
83int req_on_new_stream  (int client, struct roar_message * mes, char * data) {
84 int stream;
85 struct roar_stream * s;
86 struct roar_stream * source_stream;
87 struct roar_audio_info * info;
88 struct roar_audio_info * source_info;
89
90 ROAR_DBG("req_on_new_stream(client=%i, ...): creating stream...", client);
91 if ((stream = streams_new()) == -1 )
92  return -1;
93
94 ROAR_DBG("req_on_new_stream(client=%i, ...): getting stream...", client);
95 if ( streams_get(stream, (struct roar_stream_server **)&s) == -1 ) {
96  streams_delete(stream);
97  return -1;
98 }
99
100 ROAR_DBG("req_on_new_stream(client=%i, ...): set client of stream...", client);
101 if ( client_stream_add(client, stream) == -1 ) {
102  streams_delete(stream);
103  return -1;
104 }
105
106 ROAR_DBG("req_on_new_stream(client=%i, ...): loading stream from message...", client);
107 if ( roar_stream_m2s(s, mes) == -1 ) {
108  streams_delete(stream);
109  return -1;
110 }
111
112 ROAR_DBG("req_on_new_stream(client=%i, ...): setting id and codec of stream...", client);
113 ROAR_STREAM(s)->id = stream; // roar_stream_m2s() resets this
114 ROAR_STREAM_SERVER(s)->codec_orgi = ROAR_STREAM(s)->info.codec;
115
116 ROAR_DBG("req_on_new_stream(client=%i, ...): setting direction stream...", client);
117 // int streams_set_dir    (int id, int dir, int defaults)
118 if ( streams_set_dir(stream, ROAR_STREAM(s)->dir, 1) == -1 ) {
119  streams_delete(stream);
120  return -1;
121 }
122
123 ROAR_DBG("req_on_new_stream(client=%i, ...): setting up direction specific stream settings...", client);
124 switch (ROAR_STREAM(s)->dir) {
125  case ROAR_DIR_LIGHT_IN:
126  case ROAR_DIR_LIGHT_OUT:
127#ifndef ROAR_WITHOUT_DCOMP_LIGHT
128    info = &(ROAR_STREAM(s)->info);
129
130    info->channels = 0;
131    info->bits     = 0;
132    info->rate     = 0;
133#else
134    streams_delete(stream);
135    return -1;
136#endif
137
138   break;
139  case ROAR_DIR_MIDI_IN:
140  case ROAR_DIR_MIDI_OUT:
141#ifndef ROAR_WITHOUT_DCOMP_MIDI
142    info = &(ROAR_STREAM(s)->info);
143
144    info->channels = ROAR_MIDI_CHANNELS_DEFAULT;
145    info->bits     = ROAR_MIDI_BITS;
146    info->rate     = 0;
147#else
148    streams_delete(stream);
149    return -1;
150#endif
151
152   break;
153
154  case ROAR_DIR_RAW_IN:
155#ifndef ROAR_WITHOUT_DCOMP_RAW
156    if ( ROAR_STREAM(s)->pos_rel_id == -1     ||
157         ROAR_STREAM(s)->pos_rel_id == stream ||
158         streams_get_dir(ROAR_STREAM(s)->pos_rel_id) != ROAR_DIR_RAW_OUT
159       ) {
160     ROAR_STREAM(s)->pos_rel_id = -1; // force this here as it will try to delete itself while deleting
161                                      // in case rel_id == stream
162     streams_delete(stream);
163     return -1;
164    }
165#else
166  case ROAR_DIR_RAW_OUT:
167    streams_delete(stream);
168    return -1;
169#endif
170
171   break;
172  case ROAR_DIR_THRU:
173
174    if ( ROAR_STREAM(s)->pos_rel_id == -1 || ROAR_STREAM(s)->pos_rel_id == stream ) {
175     ROAR_STREAM(s)->pos_rel_id = -1; // force this here as it will try to delete itself while deleting
176                                      // in case rel_id == stream
177     streams_delete(stream);
178     return -1;
179    }
180
181    if ( streams_get(ROAR_STREAM(s)->pos_rel_id, (struct roar_stream_server **)&source_stream) == -1 ) {
182     streams_delete(stream);
183     return -1;
184    }
185
186    info        = &(ROAR_STREAM(s)->info);
187    source_info = &(ROAR_STREAM(source_stream)->info);
188
189    info->channels = source_info->channels;
190    info->bits     = source_info->bits;
191    info->rate     = source_info->rate;
192    info->codec    = source_info->codec;
193    ROAR_STREAM_SERVER(s)->codec_orgi = ROAR_STREAM_SERVER(source_stream)->codec_orgi;
194
195   break;
196  case ROAR_DIR_FILTER:
197    info        = &(ROAR_STREAM(s)->info);
198
199    if ( ROAR_STREAM(s)->pos_rel_id == -1 ) {
200     source_info = g_sa;
201    } else {
202     if ( streams_get(ROAR_STREAM(s)->pos_rel_id, (struct roar_stream_server **)&source_stream) == -1 ) {
203      streams_delete(stream);
204      return -1;
205     }
206     source_info = &(ROAR_STREAM(source_stream)->info);
207    }
208
209    if ( info->channels != source_info->channels || info->bits != source_info->bits ||
210         info->codec    != source_info->codec    || info->rate != source_info->rate ) {
211     // the stream parameters don't match the one of the stream being filtered.
212     // -> delete and reject the stream.
213     streams_delete(stream);
214     return -1;
215    }
216   break;
217 }
218
219 ROAR_DBG("req_on_new_stream(client=%i, ...): returning (OK)...", client);
220
221 mes->cmd     = ROAR_CMD_OK;
222 mes->stream  = stream;
223 mes->datalen = 0;
224
225 return 0;
226}
227
228int req_on_exec_stream (int client, struct roar_message * mes, char * data) {
229 int r;
230
231 ROAR_DBG("req_on_exec_stream(client=%i, mes={stream=%i,...},...): execing stream", client, mes->stream);
232
233 if ( (r = client_stream_exec(client, mes->stream)) == -1 )
234  return -1;
235
236 ROAR_DBG("req_on_exec_stream(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream);
237 mes->cmd     = ROAR_CMD_OK;
238 mes->datalen = 0;
239
240 return 0;
241}
242
243int req_on_con_stream  (int client, struct roar_message * mes, char * data) {
244 char   host[80] = {0};
245 int    port = 0;
246 int    type;
247 int    fh;
248 int    len;
249
250 if ( mes->datalen < 4 )
251  return -1;
252
253 if ( *(mes->data) != 0 )
254  return -1;
255
256 if ( mes->datalen > 80 ) // we do not support long messages here
257  return -1;
258
259 type = (unsigned)mes->data[1];
260 port = ROAR_NET2HOST16(((uint16_t*)mes->data)[1]);
261
262 len = mes->datalen - 4;
263
264 strncpy(host, &(mes->data[4]), len);
265 host[len] = 0;
266
267 if ( type > ROAR_SOCKET_TYPE_MAX )
268  return -1;
269
270 if ( type == ROAR_SOCKET_TYPE_FILE ) // disabled because of security resons
271  return -1;
272
273 if ( type == ROAR_SOCKET_TYPE_FORK ) // why should we connect to ourself?
274  return -1;
275
276 ROAR_DBG("req_on_con_stream(*): CONNECT(type=%i, host='%s', port=%i)", type, host, port);
277
278 if ( (fh = roar_socket_open(ROAR_SOCKET_MODE_CONNECT, type, host, port)) == -1 )
279  return -1;
280
281 if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) {
282  close(fh);
283  return 1;
284 }
285
286 mes->datalen = 0;
287 mes->cmd     = ROAR_CMD_OK;
288
289 return 0;
290}
291
292int req_on_passfh      (int client, struct roar_message * mes, char * data) {
293 struct roar_client * c;
294 int sock = clients_get_fh(client);
295 int16_t * d = (int16_t*)mes->data;
296 int fh;
297 int i;
298
299 if ( (fh = roar_socket_recv_fh(sock, NULL, NULL)) == -1 )
300  return -1;
301
302 if ( mes->stream != -1 ) { // stream pass:
303  if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) {
304   close(fh);
305   return 1;
306  }
307
308  mes->datalen = 0;
309  mes->cmd     = ROAR_CMD_OK;
310
311  return 0;
312 }
313
314// non-stream pass:
315
316/*
317 0: Version,  16
318 1: Flags,    16
319 2: Protocol, 16
320 3: Byteorder 16
321 Options...
322*/
323
324 if ( mes->datalen < 4*2 )
325  return -1;
326
327 for (i = 0; i < 4; i++) {
328  d[i] = ROAR_NET2HOST16(d[i]);
329 }
330
331 if ( d[0] != 0 ) // version
332  return -1;
333
334 if ( d[1] != 0 ) // flags
335  return -1;
336
337 if ( d[2] != ROAR_PROTO_ROARAUDIO ) // protocol
338  return -1;
339
340 if ( d[3] != ROAR_BYTEORDER_NETWORK ) // byte order
341  return -1;
342
343 if ( (client = clients_new()) == -1 )
344  return -1;
345
346 if ( clients_set_fh(client, fh) == -1 ) {
347  clients_delete(client);
348  return -1;
349 }
350
351 if ( clients_get(client, &c) != -1 ) {
352  roar_nnode_new_from_fh(&(c->nnode), fh, 1);
353 }
354
355 mes->datalen = 0;
356 mes->cmd     = ROAR_CMD_OK;
357
358 return 0;
359}
360
361#ifdef ROAR_SUPPORT_META
362int req_on_set_meta    (int client, struct roar_message * mes, char * data) {
363 int type;
364 int mode;
365 int namelen, vallen;
366 char   val[255+1];
367 char   name[ROAR_META_MAX_NAMELEN+1];
368
369 if ( mes->datalen < 3 )
370  return -1;
371
372 if ( mes->data[0] != 0 ) // version
373  return -1;
374
375 mode = (unsigned) mes->data[1];
376 type = (unsigned) mes->data[2];
377
378 ROAR_DBG("req_on_set_meta(*): mode=%i, type=%i", mode, type);
379
380 if ( mode == ROAR_META_MODE_CLEAR ) {
381  stream_meta_clear(mes->stream);
382  mes->datalen = 0;
383  mes->cmd     = ROAR_CMD_OK;
384  return 0;
385 } else if ( mode == ROAR_META_MODE_DELETE ) { // unsuppoerted at the moment
386  return -1;
387 } else if ( mode == ROAR_META_MODE_FINALIZE ) {
388  stream_meta_finalize(mes->stream);
389  mes->datalen = 0;
390  mes->cmd     = ROAR_CMD_OK;
391  return 0;
392 } else if ( mode == ROAR_META_MODE_SET || mode == ROAR_META_MODE_ADD ) {
393  if ( mes->datalen < 5 )
394   return -1;
395
396  namelen = (unsigned) mes->data[3];
397  vallen  = (unsigned) mes->data[4];
398
399  ROAR_DBG("req_on_set_meta(*): namelen=%i, vallen=%i", namelen, vallen);
400
401  if ( mes->datalen < (5 + namelen + vallen) )
402   return -1;
403
404  if ( namelen > ROAR_META_MAX_NAMELEN )
405   return -1;
406
407  strncpy(name, &(mes->data[5]), namelen);
408  name[namelen] = 0;
409
410  if ( vallen > 255 )
411   return -1;
412
413  strncpy(val, &(mes->data[5+namelen]), vallen);
414  val[vallen] = 0;
415
416  if ( mode == ROAR_META_MODE_SET ) {
417   if ( stream_meta_set(mes->stream, type, name, val) == -1 )
418    return -1;
419  } else {
420   if ( stream_meta_add(mes->stream, type, name, val) == -1 )
421    return -1;
422  }
423
424  mes->datalen = 0;
425  mes->cmd     = ROAR_CMD_OK;
426  return 0;
427 } else { // unknown mode!
428  return -1;
429 }
430
431 return -1;
432}
433
434int req_on_get_meta    (int client, struct roar_message * mes, char * data) {
435 int vallen;
436 int type;
437 char val[LIBROAR_BUFFER_MSGDATA-1];
438
439 if ( mes->datalen != 2 )
440  return -1;
441
442 if ( mes->data[0] != 0 ) // version
443  return -1;
444
445 type = (unsigned) mes->data[1];
446
447 if ( stream_meta_get(mes->stream, type, NULL, val, LIBROAR_BUFFER_MSGDATA-2) == -1 )
448  return -1;
449
450 vallen = strlen(val);
451
452 mes->cmd     = ROAR_CMD_OK;
453 mes->datalen = 2 + vallen;
454
455 mes->data[0] = 0;
456 mes->data[1] = (unsigned char) vallen;
457
458 val[vallen] = 0;
459
460 strncpy(&(mes->data[2]), val, vallen+1);
461
462 return 0;
463}
464
465int req_on_list_meta   (int client, struct roar_message * mes, char * data) {
466 int i;
467 int len = 0;
468 int types[ROAR_META_MAX_PER_STREAM];
469
470 if ( mes->datalen != 1 )
471  return -1;
472
473 if ( mes->data[0] != 0 ) // version
474  return -1;
475
476 if ( (len = stream_meta_list(mes->stream, types, ROAR_META_MAX_PER_STREAM)) == -1 )
477  return -1;
478
479 mes->cmd     = ROAR_CMD_OK;
480 mes->datalen = 1 + len;
481 mes->data[0] = 0;
482
483 for (i = 0; i < len; i++)
484  mes->data[i+1] = types[i];
485
486 return 0;
487}
488#endif
489
490int req_on_server_oinfo    (int client, struct roar_message * mes, char * data) {
491 struct roar_stream s;
492//ROAR_DIR_OUTPUT
493
494 memset(&s, 0, sizeof(struct roar_stream));
495
496 s.dir           = ROAR_DIR_MIXING;
497 s.pos_rel_id    = -1;
498 s.info.rate     = g_sa->rate;
499 s.info.bits     = g_sa->bits;
500 s.info.channels = g_sa->channels;
501 s.info.codec    = g_sa->codec;
502 s.pos           = g_pos;
503
504 if ( roar_stream_s2m(&s, mes) == -1 )
505  return -1;
506
507 mes->cmd = ROAR_CMD_OK;
508
509 return 0;
510}
511
512
513int req_on_get_standby (int client, struct roar_message * mes, char * data) {
514 mes->cmd = ROAR_CMD_OK;
515 mes->datalen = 2;
516
517 *((uint16_t*)mes->data) = ROAR_HOST2NET16((unsigned) g_standby);
518
519 return 0;
520}
521
522int req_on_set_standby (int client, struct roar_message * mes, char * data) {
523 if ( mes->datalen != 2 )
524  return -1;
525
526 g_standby = ROAR_NET2HOST16(*((uint16_t*)mes->data));
527
528 mes->cmd     = ROAR_CMD_OK;
529 mes->datalen = 0;
530
531 return 0;
532}
533
534int req_on_exit      (int client, struct roar_message * mes, char * data) {
535 int term = 0;
536
537 if ( mes->datalen == 1 )
538  term = mes->data[0];
539
540 mes->cmd     = ROAR_CMD_OK;
541 mes->datalen = 0;
542
543 ROAR_DBG("req_on_exit(*): term=%i", term);
544
545 if ( term ) {
546  cleanup_listen_socket(1);
547 } else {
548  alive = 0;
549 }
550
551 return 0;
552}
553
554int req_on_list_clients(int client, struct roar_message * mes, char * data) {
555 unsigned char filter, cmp;
556 uint32_t id;
557 int clients[ROAR_CLIENTS_MAX];
558 int i, c = 0;
559
560 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
561  return -1;
562
563 // TODO: add code to support filter
564 if ( filter != ROAR_CTL_FILTER_ANY )
565  return -1;
566
567 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
568  if ( g_clients[i] != NULL ) {
569   clients[c++] = i;
570  }
571 }
572
573 roar_ctl_ia2m(mes, clients, c);
574
575 mes->cmd = ROAR_CMD_OK;
576
577 return 0;
578}
579int req_on_list_streams(int client, struct roar_message * mes, char * data) {
580 unsigned char filter, cmp;
581 uint32_t id;
582 int streams[ROAR_STREAMS_MAX];
583 int i, c = 0;
584
585 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
586  return -1;
587
588 // TODO: add code to support filter
589 if ( filter != ROAR_CTL_FILTER_ANY )
590  return -1;
591
592 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
593  if ( g_streams[i] != NULL ) {
594   streams[c++] = i;
595  }
596 }
597
598 roar_ctl_ia2m(mes, streams, c);
599
600 mes->cmd = ROAR_CMD_OK;
601
602 return 0;
603}
604
605int req_on_get_client  (int client, struct roar_message * mes, char * data) {
606 struct roar_client * c;
607
608 if ( mes->datalen != 1 )
609  return -1;
610
611 if ( clients_get(mes->data[0], &c) == -1 )
612  return -1;
613
614 mes->cmd = ROAR_CMD_OK;
615
616 return roar_ctl_c2m(mes, c);
617}
618
619int req_on_get_stream  (int client, struct roar_message * mes, char * data) {
620 struct roar_stream_server * s;
621
622 if ( mes->datalen != 1 )
623  return -1;
624
625 if ( streams_get(mes->data[0], &s) == -1 )
626  return -1;
627
628 mes->cmd = ROAR_CMD_OK;
629 mes->stream = mes->data[0];
630
631 return roar_stream_s2m(ROAR_STREAM(s), mes);
632}
633
634int req_on_get_stream_para (int client, struct roar_message * mes, char * data) {
635 struct roar_stream * s;
636 struct roar_stream_server * ss;
637 struct roar_audio_info * audio_info;
638 uint16_t * d = (uint16_t *) mes->data;
639 int i;
640 char * str;
641
642 if ( mes->datalen != 4 )
643  return -1;
644
645 for (i = 0; i < mes->datalen/2; i++) {
646  d[i] = ROAR_NET2HOST16(d[i]);
647 }
648
649 if ( d[0] != 0 ) {
650  ROAR_WARN("req_on_get_stream_para(*): unsupported command version: %i, %i", d[0], d[1]);
651  return -1;
652 }
653
654 switch (d[1]) {
655  case ROAR_STREAM_PARA_INFO:
656    if ( streams_get(mes->stream, &ss) == -1 ) {
657     ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream);
658     return -1;
659    }
660
661    if ( streams_calc_delay(mes->stream) == -1 ) {
662     ROAR_WARN("req_on_get_stream_para(*): can not calc delay for stream %i", mes->stream);
663    }
664
665    s = ROAR_STREAM(ss);
666
667    audio_info = &(s->info);
668
669    mes->datalen = 2*12;
670
671    d[ 2] = ROAR_OUTPUT_CALC_OUTBUFSIZE(audio_info);
672    d[ 3] = ss->pre_underruns;
673    d[ 4] = ss->post_underruns;
674    d[ 5] = ss->codec_orgi;
675    d[ 6] = (ss->flags & 0xFFFF) | (ss->primary ? ROAR_FLAG_PRIMARY : 0) | (ss->driver_id != -1 ? ROAR_FLAG_OUTPUT : 0);
676    d[ 7] = ss->delay/1000;
677    d[ 8] = ss->state;
678    d[ 9] = (ss->flags & 0xFFFF0000) >> 16;
679    d[10] = ss->mixer_stream;
680    d[11] = ss->role;
681
682    ROAR_DBG("req_on_get_stream_para(*): ss->driver_id=%i", ss->driver_id);
683
684    ROAR_DBG("req_on_get_stream_para(*): delay=%i, send delay=%i", ss->delay, d[7]);
685
686    for (i = 0; i < mes->datalen/2; i++) {
687     d[i] = ROAR_HOST2NET16(d[i]);
688    }
689
690    mes->pos = s->pos;
691   break;
692
693  case ROAR_STREAM_PARA_NAME:
694   str = streams_get_name(mes->stream);
695
696   if ( str == NULL )
697    return -1;
698
699    mes->datalen = 4 + strlen(str);
700
701    if ( mes->datalen > LIBROAR_BUFFER_MSGDATA )
702     return -1;
703
704    strncpy(((char*)&(mes->data))+4, str, mes->datalen);
705
706    d[0] = ROAR_HOST2NET16(d[0]);
707    d[1] = ROAR_HOST2NET16(d[1]);
708   break;
709
710  case ROAR_STREAM_PARA_CHANMAP:
711    if ( streams_get(mes->stream, &ss) == -1 ) {
712     ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream);
713     return -1;
714    }
715
716    s = ROAR_STREAM(ss);
717
718    memcpy(&(mes->data[4]), ss->chanmap.in, s->info.channels);
719    mes->datalen = 2*2 + s->info.channels;
720
721    d[0] = ROAR_HOST2NET16(d[0]);
722    d[1] = ROAR_HOST2NET16(d[1]);
723   break;
724
725  default:
726    ROAR_WARN("req_on_get_stream_para(*): unsupported command: %i", d[1]);
727    return -1;
728 }
729
730 mes->cmd = ROAR_CMD_OK;
731 return 0;
732}
733
734int req_on_set_stream_para (int client, struct roar_message * mes, char * data) {
735 uint16_t * d = (uint16_t *) mes->data;
736 int i;
737
738 if ( mes->datalen < 2*2 )
739  return -1;
740
741 for (i = 0; i < 2; i++) {
742  d[i] = ROAR_NET2HOST16(d[i]);
743 }
744
745 if ( d[0] != 0 )
746  return -1;
747
748 switch (d[1]) {
749  case ROAR_STREAM_PARA_FLAGS:
750    if ( mes->datalen != 2*4 )
751     return -1;
752
753    d[2] = ROAR_NET2HOST16(d[2]);
754    d[3] = ROAR_NET2HOST16(d[3]);
755
756    ROAR_DBG("req_on_set_stream_para(*): request seems to be valid");
757
758    if ( d[2] == ROAR_RESET_FLAG ) {
759     if ( streams_reset_flag(mes->stream, d[3]) == -1 )
760      return -1;
761    } else {
762     if ( streams_set_flag(mes->stream, d[3]) == -1 )
763      return -1;
764    }
765   break;
766  case ROAR_STREAM_PARA_CHANMAP:
767    if ( streams_set_map(mes->stream, &(mes->data[4]), mes->datalen - 4) == -1 )
768     return -1;
769   break;
770  case ROAR_STREAM_PARA_ROLE:
771    if ( mes->datalen != 2*3 )
772     return -1;
773
774    d[2] = ROAR_NET2HOST16(d[2]);
775
776    if ( streams_set_role(mes->stream, d[2]) == -1 )
777     return -1;
778   break;
779  default:
780    ROAR_WARN("req_on_set_stream_para(*): unsupported command version: %i, %i", d[0], d[1]);
781    return -1;
782   break;
783 }
784
785 mes->cmd     = ROAR_CMD_OK;
786 mes->datalen = 0;
787
788 return 0;
789}
790
791int req_on_kick (int client, struct roar_message * mes, char * data) {
792 uint16_t * info = (uint16_t *) mes->data;
793
794 if ( mes->datalen != 4 )
795  return -1;
796
797 info[0] = ROAR_NET2HOST16(info[0]);
798 info[1] = ROAR_NET2HOST16(info[1]);
799
800 if ( info[0] == ROAR_OT_CLIENT ) {
801  clients_delete(info[1]);
802 } else if ( info[0] == ROAR_OT_STREAM ) {
803  if ( streams_get_flag(info[1], ROAR_FLAG_IMMUTABLE) == 1 )
804   return -1;
805
806  streams_delete(info[1]);
807 } else if ( info[0] == ROAR_OT_SOURCE ) {
808  if ( streams_get_flag(info[1], ROAR_FLAG_IMMUTABLE) == 1 )
809   return -1;
810
811  if ( streams_get_flag(info[1], ROAR_FLAG_SOURCE) == 1 ) {
812   streams_delete(info[1]);
813  } else {
814   return -1;
815  }
816 } else {
817  return -1;
818 }
819
820 mes->cmd     = ROAR_CMD_OK;
821 mes->datalen = 0;
822
823 return 0;
824}
825
826int req_on_attach      (int client, struct roar_message * mes, char * data) {
827 uint16_t * info = (uint16_t *) mes->data;
828
829 if ( mes->datalen < 6 )
830  return -1;
831
832 info[0] = ROAR_NET2HOST16(info[0]);
833 info[1] = ROAR_NET2HOST16(info[1]);
834 info[2] = ROAR_NET2HOST16(info[2]);
835
836 if ( info[0] != 0 )
837  return -1;
838
839 if ( info[1] == ROAR_ATTACH_SIMPLE ) {
840  if ( client_stream_move(info[2], mes->stream) == -1 )
841   return -1;
842 } else {
843  return -1;
844 }
845
846 mes->cmd     = ROAR_CMD_OK;
847 mes->datalen = 0;
848
849 return 0;
850}
851
852int req_on_set_vol (int client, struct roar_message * mes, char * data) {
853 struct roar_stream_server * s;
854 uint16_t * info = (uint16_t *) mes->data;
855 uint16_t   version;
856 uint16_t   scale = 65535;
857 int stream;
858 int i;
859 int chans;
860
861 ROAR_DBG("req_on_set_vol(*) = ?");
862 ROAR_DBG("req_on_set_vol(*): mes->datalen=%i", mes->datalen);
863
864 if ( mes->datalen < (4*2) )
865  return -1;
866
867 version = ROAR_NET2HOST16(info[0]);
868 ROAR_DBG("req_on_set_vol(*): version=%i", (int)version);
869
870 switch (version) {
871  case 0:
872    stream = ROAR_NET2HOST16(info[1]);
873   break;
874  case 1:
875    stream = mes->stream;
876    scale  = ROAR_NET2HOST16(info[1]);
877   break;
878  default:
879    return -1;
880   break;
881 }
882 ROAR_DBG("req_on_set_vol(*): stream=%i", stream);
883
884 if ( scale == 0 )
885  return -1;
886
887 // TODO: change this code.
888 //       we should not directly change the stream object but use some stream_*()-func
889 //       for that job.
890
891 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
892  return -1;
893
894 s = g_streams[stream];
895
896 if ( s == NULL )
897  return -1;
898
899 ROAR_DBG("req_on_set_vol(*): s=%p", s);
900
901 info[2] = ROAR_NET2HOST16(info[2]);
902
903 if ( info[2] == ROAR_SET_VOL_ALL ) {
904  chans = (mes->datalen/2) - 3;
905  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ALL, channes=%i", chans);
906
907  if ( chans >= ROAR_MAX_CHANNELS )
908   return -1;
909
910  ROAR_DBG("req_on_set_vol(*): mixer at %p", s->mixer.mixer);
911
912  for (i = 0; i < chans; i++) {
913   s->mixer.mixer[i] = ROAR_NET2HOST16(info[i+3]);
914   ROAR_DBG("req_on_set_vol(*): channel %i: %i", i, ROAR_NET2HOST16(info[i+3]));
915  }
916
917  s->mixer.scale = scale;
918
919  ROAR_DBG("req_on_set_vol(*): mixer changed!");
920
921 } else if ( info[2] == ROAR_SET_VOL_ONE ) {
922  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ONE");
923  if ( ROAR_NET2HOST16(info[3]) >= ROAR_MAX_CHANNELS )
924   return -1;
925
926  s->mixer.mixer[ROAR_NET2HOST16(info[3])] = ROAR_NET2HOST16(info[4]);
927
928  s->mixer.scale = scale;
929 } else {
930  return -1;
931 }
932
933 if ( streams_set_mixer(stream) == -1 )
934  return -1;
935
936 mes->cmd     = ROAR_CMD_OK;
937 mes->datalen = 0;
938
939 return 0;
940}
941
942int req_on_get_vol (int client, struct roar_message * mes, char * data) {
943 uint16_t * info = (uint16_t *) mes->data;
944 uint16_t   version = -1;
945 int stream;
946 struct roar_stream_server * s;
947 int i;
948 int chans;
949
950 ROAR_DBG("req_on_get_vol(*) = ?");
951 ROAR_DBG("req_on_get_vol(*): mes->datalen=%i", mes->datalen);
952
953 if ( mes->datalen < 2 ) {
954  return -1;
955 }
956
957 version = ROAR_NET2HOST16(info[0]);
958
959 switch (version) {
960  case 0:
961    if ( mes->datalen < (2*2) )
962     return -1;
963
964    stream = ROAR_NET2HOST16(info[1]);
965   break;
966  case 1:
967    stream = mes->stream;
968   break;
969  default:
970    return -1;
971   break;
972 }
973
974 ROAR_DBG("req_on_get_vol(*): stream=%i", stream);
975
976 // TODO: change this code.
977 //       we should not directly change the stream object but use some stream_*()-func
978 //       for that job.
979
980 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
981  return -1;
982
983 s = g_streams[stream];
984
985 if ( s == NULL )
986  return -1;
987
988 ROAR_DBG("req_on_get_vol(*): s=%p", s);
989
990 // ok, we have everything
991
992 info[0] = ROAR_HOST2NET16(version);
993
994 switch (version) {
995  case 0:
996    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
997
998    for (i = 0; i < chans; i++)
999     info[2+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1000
1001     mes->datalen = (2 + chans)*2;
1002   break;
1003  case 1:
1004    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
1005    info[2] = ROAR_HOST2NET16(s->mixer.scale);
1006    info[3] = ROAR_HOST2NET16(s->mixer.rpg_mul);
1007    info[4] = ROAR_HOST2NET16(s->mixer.rpg_div);
1008
1009    for (i = 0; i < chans; i++)
1010     info[5+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1011
1012     mes->datalen = (5 + chans)*2;
1013   break;
1014  default:
1015    return -1;
1016   break;
1017 }
1018
1019 mes->cmd = ROAR_CMD_OK;
1020
1021 return 0;
1022}
1023
1024int req_on_add_data (int client, struct roar_message * mes, char * data) {
1025 struct roar_buffer * b;
1026 char               * buf;
1027
1028 if ( roar_buffer_new(&b, mes->datalen) == -1 ) {
1029  ROAR_ERR("req_on_add_data(*): Can not alloc buffer space!");
1030  ROAR_DBG("req_on_add_data(*) = -1");
1031  return -1;
1032 }
1033
1034 roar_buffer_get_data(b, (void **)&buf);
1035
1036 if ( data == NULL ) {
1037  memcpy(buf, mes->data, mes->datalen);
1038 } else {
1039  memcpy(buf, data, mes->datalen);
1040 }
1041
1042 if ( stream_add_buffer(mes->stream, b) == -1 ) {
1043  roar_buffer_free(b);
1044  return -1;
1045 }
1046
1047 mes->cmd     = ROAR_CMD_OK_STOP;
1048 mes->datalen = 0;
1049
1050 return 0;
1051}
1052
1053int req_on_beep        (int client, struct roar_message * mes, char * data) {
1054 struct roar_beep bs;
1055 int16_t * info = (int16_t*)mes->data;
1056 int stream;
1057
1058 memset(&bs, 0, sizeof(bs));
1059
1060 if ( mes->datalen > 0 ) {
1061  if ( mes->datalen < 2 )
1062   return -1;
1063
1064  if ( ROAR_NET2HOST16(info[0]) != 0 ) /* version */
1065   return -1;
1066
1067  if ( mes->datalen != 8*2 )
1068   return -1;
1069
1070  bs.vol  = ROAR_NET2HOST16(info[1]);
1071  bs.time = ROAR_NET2HOST16(info[2]);
1072  bs.freq = ROAR_NET2HOST16(info[3]);
1073  bs.type = ROAR_NET2HOST16(info[4]);
1074  bs.x    = ROAR_NET2HOST16(info[5]);
1075  bs.y    = ROAR_NET2HOST16(info[6]);
1076  bs.z    = ROAR_NET2HOST16(info[7]);
1077 }
1078
1079 if ( (stream = beep_start(client, &bs)) == -1 )
1080  return -1;
1081
1082 mes->stream  = stream;
1083 mes->cmd     = ROAR_CMD_OK_STOP;
1084 mes->datalen = 0;
1085
1086 return 0;
1087}
1088
1089//ll
Note: See TracBrowser for help on using the repository browser.