[3684] | 1 | //emul_rsound.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012 |
---|
[3684] | 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 | |
---|
| 28 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
| 29 | |
---|
| 30 | static int emul_rsound_lastcon = -1; |
---|
| 31 | |
---|
| 32 | int emul_rsound_new_client (int client, int data); |
---|
| 33 | |
---|
| 34 | int emul_rsound_on_connect (int fh, struct roard_listen * lsock) { |
---|
| 35 | int oldfh = emul_rsound_lastcon; |
---|
| 36 | int client; |
---|
[3685] | 37 | union { |
---|
[3824] | 38 | int32_t i[4]; |
---|
| 39 | char c[16]; |
---|
[3685] | 40 | } buf; |
---|
[3684] | 41 | |
---|
[5270] | 42 | (void)lsock; |
---|
| 43 | |
---|
[3825] | 44 | // TODO: add error handling |
---|
| 45 | roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK); |
---|
| 46 | |
---|
[3684] | 47 | if ( emul_rsound_lastcon == -1 ) { |
---|
| 48 | emul_rsound_lastcon = fh; |
---|
| 49 | return -2; |
---|
| 50 | } else { |
---|
| 51 | emul_rsound_lastcon = -1; |
---|
| 52 | |
---|
| 53 | client = clients_new(); |
---|
| 54 | |
---|
| 55 | if ( client == -1 ) |
---|
| 56 | return -1; |
---|
| 57 | |
---|
| 58 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
| 59 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
| 60 | |
---|
| 61 | clients_delete(client); |
---|
| 62 | close(oldfh); |
---|
| 63 | close(fh); |
---|
| 64 | |
---|
| 65 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
| 66 | return -1; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | if ( emul_rsound_new_client(client, oldfh) == -1 ) { |
---|
| 70 | clients_delete(client); |
---|
| 71 | return -1; |
---|
| 72 | } |
---|
| 73 | |
---|
[3841] | 74 | // LATENCY: |
---|
| 75 | // we currently don't know what we need to set here. |
---|
[3685] | 76 | buf.i[0] = ROAR_HOST2NET32(0); |
---|
[3841] | 77 | |
---|
| 78 | // block size used by roard. |
---|
| 79 | buf.i[1] = ROAR_HOST2NET32(ROAR_OUTPUT_BUFFER_SAMPLES * roar_info2framesize(g_sa) / 8); |
---|
| 80 | |
---|
| 81 | // Magic zeros to enable protocol handling. |
---|
| 82 | // (RSD_CONN_PROTO) |
---|
[3824] | 83 | buf.i[2] = ROAR_HOST2NET32(0); |
---|
| 84 | buf.i[3] = ROAR_HOST2NET32(0); |
---|
[3685] | 85 | |
---|
[4739] | 86 | if ( ROAR_NETWORK_WRITE(oldfh, buf.c, sizeof(buf.c)) != sizeof(buf.c) ) { |
---|
| 87 | clients_delete(client); |
---|
| 88 | return -1; |
---|
| 89 | } |
---|
[3684] | 90 | |
---|
| 91 | return client; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return -1; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | int emul_rsound_new_client (int client, int data) { |
---|
| 98 | struct roar_stream_server * ss; |
---|
| 99 | struct roar_stream * s; |
---|
| 100 | struct roar_client * c; |
---|
| 101 | int stream; |
---|
| 102 | |
---|
| 103 | if ( clients_get(client, &c) == -1 ) { |
---|
| 104 | return -1; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if ((stream = streams_new()) == -1 ) { |
---|
| 108 | clients_delete(client); |
---|
| 109 | return -1; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | if ( streams_get(stream, &ss) == -1 ) { |
---|
| 113 | streams_delete(stream); |
---|
| 114 | clients_delete(client); |
---|
| 115 | return -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | s = ROAR_STREAM(ss); |
---|
| 119 | |
---|
| 120 | if ( client_stream_add(client, stream) == -1 ) { |
---|
| 121 | streams_delete(stream); |
---|
| 122 | clients_delete(client); |
---|
| 123 | return -1; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 127 | |
---|
[3686] | 128 | ss->codec_orgi = s->info.codec = ROAR_CODEC_RIFF_WAVE; |
---|
[3684] | 129 | |
---|
| 130 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 131 | clients_delete(client); |
---|
| 132 | return -1; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /* |
---|
| 136 | if ( client_stream_exec(client, stream) == -1 ) { |
---|
| 137 | clients_delete(client); |
---|
| 138 | return -1; |
---|
| 139 | } |
---|
| 140 | */ |
---|
| 141 | |
---|
| 142 | if ( client_stream_set_fh(client, stream, data) == -1 ) { |
---|
| 143 | streams_delete(stream); |
---|
| 144 | clients_delete(client); |
---|
| 145 | return -1; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | return 0; |
---|
| 149 | } |
---|
| 150 | |
---|
[3820] | 151 | int emul_rsound_vrecv_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) { |
---|
[4193] | 152 | struct roar_vio_select select; |
---|
| 153 | struct roar_vio_selecttv tv = { |
---|
| 154 | .sec = 0, |
---|
| 155 | .nsec = 0 |
---|
| 156 | }; |
---|
[3820] | 157 | ssize_t ret; |
---|
| 158 | int num; |
---|
| 159 | |
---|
| 160 | if ( msg == NULL || vio == NULL ) |
---|
| 161 | return -1; |
---|
| 162 | |
---|
[4193] | 163 | ROAR_VIO_SELECT_SETVIO(&select, vio, ROAR_VIO_SELECT_READ); |
---|
| 164 | if ( roar_vio_select(&select, 1, &tv, NULL) == -1 ) |
---|
| 165 | return -1; |
---|
| 166 | |
---|
| 167 | if ( !(select.eventsa & ROAR_VIO_SELECT_READ) ) { |
---|
| 168 | errno = EAGAIN; |
---|
| 169 | return -1; |
---|
| 170 | } |
---|
| 171 | |
---|
[3820] | 172 | ret = roar_vio_read(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN); |
---|
| 173 | |
---|
| 174 | if ( ret != EMUL_RSOUND_MSG_HEADER_LEN ) |
---|
| 175 | return -1; |
---|
| 176 | |
---|
| 177 | msg->header[ret] = 0; |
---|
| 178 | |
---|
| 179 | if ( msg->header[0] != 'R' || msg->header[1] != 'S' || msg->header[2] != 'D' ) |
---|
| 180 | return -1; |
---|
| 181 | |
---|
| 182 | if ( sscanf(&(msg->header[3]), "%5d", &num) != 1 ) |
---|
| 183 | return -1; |
---|
| 184 | |
---|
| 185 | if ( num > EMUL_RSOUND_MSG_DATA_LEN ) |
---|
| 186 | return -1; |
---|
| 187 | |
---|
| 188 | msg->datalen = num; |
---|
| 189 | |
---|
| 190 | ret = roar_vio_read(vio, msg->data, num); |
---|
| 191 | |
---|
| 192 | if ( ret != (ssize_t)num ) |
---|
| 193 | return -1; |
---|
| 194 | |
---|
| 195 | msg->data[num] = 0; |
---|
| 196 | |
---|
| 197 | msg->datasp = msg->data; |
---|
| 198 | msg->dataslen = msg->datalen; |
---|
| 199 | |
---|
| 200 | for (num = 0; msg->data[num] == ' '; num++) { |
---|
| 201 | msg->datasp++; |
---|
| 202 | msg->dataslen--; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | return 0; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | int emul_rsound_vsend_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) { |
---|
[4193] | 209 | struct roar_vio_select select; |
---|
| 210 | struct roar_vio_selecttv tv = { |
---|
| 211 | .sec = 0, |
---|
| 212 | .nsec = 0 |
---|
| 213 | }; |
---|
[3820] | 214 | ssize_t ret; |
---|
| 215 | |
---|
| 216 | if ( msg == NULL || vio == NULL ) |
---|
| 217 | return -1; |
---|
| 218 | |
---|
[4193] | 219 | ROAR_VIO_SELECT_SETVIO(&select, vio, ROAR_VIO_SELECT_WRITE); |
---|
| 220 | if ( roar_vio_select(&select, 1, &tv, NULL) == -1 ) |
---|
| 221 | return -1; |
---|
| 222 | |
---|
| 223 | if ( !(select.eventsa & ROAR_VIO_SELECT_WRITE) ) { |
---|
| 224 | errno = EAGAIN; |
---|
| 225 | return -1; |
---|
| 226 | } |
---|
| 227 | |
---|
[4130] | 228 | snprintf(msg->header, EMUL_RSOUND_MSG_HEADER_LEN+1, "RSD%5d", (int)msg->datalen); |
---|
[3820] | 229 | |
---|
| 230 | ret = roar_vio_write(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN); |
---|
| 231 | |
---|
| 232 | if ( ret != EMUL_RSOUND_MSG_HEADER_LEN ) |
---|
| 233 | return -1; |
---|
| 234 | |
---|
| 235 | ret = roar_vio_write(vio, msg->data, msg->datalen); |
---|
| 236 | |
---|
| 237 | if ( ret != (ssize_t)msg->datalen ) |
---|
| 238 | return -1; |
---|
| 239 | |
---|
| 240 | return 0; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | int emul_rsound_check_client(int client, struct roar_vio_calls * vio) { |
---|
[3824] | 244 | struct roar_vio_calls rvio; |
---|
| 245 | struct emul_rsound_msg msg; |
---|
| 246 | struct roar_client * c; |
---|
| 247 | struct roar_stream_server * ss; |
---|
| 248 | int streamid; |
---|
| 249 | int i; |
---|
| 250 | ssize_t ptr; |
---|
[3918] | 251 | size_t max_len; |
---|
[3820] | 252 | |
---|
| 253 | if ( vio == NULL ) { |
---|
| 254 | vio = &rvio; |
---|
| 255 | roar_vio_open_fh_socket(vio, clients_get_fh(client)); |
---|
| 256 | } |
---|
| 257 | |
---|
[3825] | 258 | // we get called in a loop, in case this fails no problem, just |
---|
| 259 | // return -1, caller will delete us in case of real error. |
---|
[3820] | 260 | if ( emul_rsound_vrecv_msg(&msg, vio) == -1 ) |
---|
[3825] | 261 | return -1; |
---|
[3820] | 262 | |
---|
| 263 | if ( !strncmp(msg.datasp, "INFO", 4) ) { |
---|
[3824] | 264 | if ( clients_get(client, &c) == -1 ) |
---|
| 265 | return clients_delete(client); |
---|
| 266 | |
---|
| 267 | streamid = -1; |
---|
| 268 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) |
---|
| 269 | if ( c->streams[i] > streamid ) |
---|
| 270 | streamid = c->streams[i]; |
---|
| 271 | |
---|
| 272 | if ( streamid == -1 ) |
---|
| 273 | return clients_delete(client); |
---|
| 274 | |
---|
| 275 | if ( streams_get(streamid, &ss) == -1 ) |
---|
| 276 | return clients_delete(client); |
---|
| 277 | |
---|
| 278 | ptr = roar_info2samplesize(&(ROAR_STREAM(ss)->info)); |
---|
| 279 | |
---|
| 280 | if ( ptr == -1 ) |
---|
| 281 | return clients_delete(client); |
---|
| 282 | |
---|
| 283 | ptr *= ROAR_STREAM(ss)->pos; |
---|
| 284 | ptr /= 8; // bits -> bytes |
---|
| 285 | |
---|
| 286 | i = snprintf(msg.data+msg.datalen, EMUL_RSOUND_MSG_DATA_LEN - msg.datalen, " %lld", (long long int)ptr); |
---|
| 287 | |
---|
| 288 | msg.datalen += i; |
---|
| 289 | |
---|
| 290 | return emul_rsound_vsend_msg(&msg, vio); |
---|
[3820] | 291 | } else if ( !strncmp(msg.datasp, "NULL", 4) ) { |
---|
| 292 | // NULL is simular to NOOP |
---|
| 293 | } else if ( !strncmp(msg.datasp, "STOP", 4) ) { |
---|
| 294 | // This is quit. |
---|
| 295 | return clients_delete(client); |
---|
[3918] | 296 | } else if ( !strncmp(msg.datasp, "IDENTITY ", 9) ) { |
---|
| 297 | if ( msg.dataslen < (8+1+1) ) |
---|
| 298 | return clients_delete(client); |
---|
| 299 | |
---|
| 300 | msg.datasp += 9; |
---|
| 301 | msg.dataslen -= 9; |
---|
| 302 | |
---|
| 303 | if ( clients_get(client, &c) == -1 ) |
---|
| 304 | return clients_delete(client); |
---|
| 305 | |
---|
| 306 | max_len = msg.dataslen < (ROAR_BUFFER_NAME-1) ? msg.dataslen : (ROAR_BUFFER_NAME-1); |
---|
| 307 | |
---|
| 308 | strncpy(c->name, msg.datasp, max_len); |
---|
| 309 | c->name[max_len] = 0; |
---|
[3921] | 310 | } else if ( !strncmp(msg.datasp, "CLOSECTL", 9) ) { |
---|
| 311 | if ( clients_get(client, &c) == -1 ) |
---|
| 312 | return clients_delete(client); |
---|
| 313 | |
---|
| 314 | strncpy(msg.data, " CLOSECTL OK", EMUL_RSOUND_MSG_DATA_LEN); |
---|
| 315 | msg.datalen = 12; //strlen(" CLOSECTL OK"); |
---|
| 316 | |
---|
[4130] | 317 | if ( emul_rsound_vsend_msg(&msg, vio) == -1 ) |
---|
[3921] | 318 | return clients_delete(client); |
---|
| 319 | |
---|
| 320 | streamid = c->streams[0]; |
---|
[4130] | 321 | |
---|
| 322 | if ( client_stream_exec(client, streamid) == -1 ) |
---|
[3921] | 323 | return clients_delete(client); |
---|
| 324 | |
---|
| 325 | return 0; |
---|
[3820] | 326 | } else { |
---|
[3841] | 327 | // Unknown command, kill the client. |
---|
[3820] | 328 | return clients_delete(client); |
---|
| 329 | } |
---|
[3825] | 330 | |
---|
| 331 | return 0; |
---|
[3820] | 332 | } |
---|
[3684] | 333 | #endif |
---|
| 334 | |
---|
| 335 | //ll |
---|