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