[3684] | 1 | //emul_rsound.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 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 | |
---|
| 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 | |
---|
[3825] | 42 | // TODO: add error handling |
---|
| 43 | roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK); |
---|
| 44 | |
---|
[3684] | 45 | if ( emul_rsound_lastcon == -1 ) { |
---|
| 46 | emul_rsound_lastcon = fh; |
---|
| 47 | return -2; |
---|
| 48 | } else { |
---|
| 49 | emul_rsound_lastcon = -1; |
---|
| 50 | |
---|
| 51 | client = clients_new(); |
---|
| 52 | |
---|
| 53 | if ( client == -1 ) |
---|
| 54 | return -1; |
---|
| 55 | |
---|
| 56 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
| 57 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
| 58 | |
---|
| 59 | clients_delete(client); |
---|
| 60 | close(oldfh); |
---|
| 61 | close(fh); |
---|
| 62 | |
---|
| 63 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
| 64 | return -1; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | if ( emul_rsound_new_client(client, oldfh) == -1 ) { |
---|
| 68 | clients_delete(client); |
---|
| 69 | return -1; |
---|
| 70 | } |
---|
| 71 | |
---|
[3841] | 72 | // LATENCY: |
---|
| 73 | // we currently don't know what we need to set here. |
---|
[3685] | 74 | buf.i[0] = ROAR_HOST2NET32(0); |
---|
[3841] | 75 | |
---|
| 76 | // block size used by roard. |
---|
| 77 | buf.i[1] = ROAR_HOST2NET32(ROAR_OUTPUT_BUFFER_SAMPLES * roar_info2framesize(g_sa) / 8); |
---|
| 78 | |
---|
| 79 | // Magic zeros to enable protocol handling. |
---|
| 80 | // (RSD_CONN_PROTO) |
---|
[3824] | 81 | buf.i[2] = ROAR_HOST2NET32(0); |
---|
| 82 | buf.i[3] = ROAR_HOST2NET32(0); |
---|
[3685] | 83 | |
---|
[3824] | 84 | ROAR_NETWORK_WRITE(oldfh, buf.c, sizeof(buf.c)); |
---|
[3684] | 85 | |
---|
| 86 | return client; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | return -1; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | int emul_rsound_new_client (int client, int data) { |
---|
| 93 | struct roar_stream_server * ss; |
---|
| 94 | struct roar_stream * s; |
---|
| 95 | struct roar_client * c; |
---|
| 96 | int stream; |
---|
| 97 | |
---|
| 98 | if ( clients_get(client, &c) == -1 ) { |
---|
| 99 | return -1; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if ((stream = streams_new()) == -1 ) { |
---|
| 103 | clients_delete(client); |
---|
| 104 | return -1; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if ( streams_get(stream, &ss) == -1 ) { |
---|
| 108 | streams_delete(stream); |
---|
| 109 | clients_delete(client); |
---|
| 110 | return -1; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | s = ROAR_STREAM(ss); |
---|
| 114 | |
---|
| 115 | if ( client_stream_add(client, stream) == -1 ) { |
---|
| 116 | streams_delete(stream); |
---|
| 117 | clients_delete(client); |
---|
| 118 | return -1; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 122 | |
---|
[3686] | 123 | ss->codec_orgi = s->info.codec = ROAR_CODEC_RIFF_WAVE; |
---|
[3684] | 124 | |
---|
| 125 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 126 | clients_delete(client); |
---|
| 127 | return -1; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* |
---|
| 131 | if ( client_stream_exec(client, stream) == -1 ) { |
---|
| 132 | clients_delete(client); |
---|
| 133 | return -1; |
---|
| 134 | } |
---|
| 135 | */ |
---|
| 136 | |
---|
| 137 | if ( client_stream_set_fh(client, stream, data) == -1 ) { |
---|
| 138 | streams_delete(stream); |
---|
| 139 | clients_delete(client); |
---|
| 140 | return -1; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | return 0; |
---|
| 144 | } |
---|
| 145 | |
---|
[3820] | 146 | int emul_rsound_vrecv_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) { |
---|
| 147 | ssize_t ret; |
---|
| 148 | int num; |
---|
| 149 | |
---|
| 150 | if ( msg == NULL || vio == NULL ) |
---|
| 151 | return -1; |
---|
| 152 | |
---|
| 153 | ret = roar_vio_read(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN); |
---|
| 154 | |
---|
| 155 | if ( ret != EMUL_RSOUND_MSG_HEADER_LEN ) |
---|
| 156 | return -1; |
---|
| 157 | |
---|
| 158 | msg->header[ret] = 0; |
---|
| 159 | |
---|
| 160 | if ( msg->header[0] != 'R' || msg->header[1] != 'S' || msg->header[2] != 'D' ) |
---|
| 161 | return -1; |
---|
| 162 | |
---|
| 163 | if ( sscanf(&(msg->header[3]), "%5d", &num) != 1 ) |
---|
| 164 | return -1; |
---|
| 165 | |
---|
| 166 | if ( num > EMUL_RSOUND_MSG_DATA_LEN ) |
---|
| 167 | return -1; |
---|
| 168 | |
---|
| 169 | msg->datalen = num; |
---|
| 170 | |
---|
| 171 | ret = roar_vio_read(vio, msg->data, num); |
---|
| 172 | |
---|
| 173 | if ( ret != (ssize_t)num ) |
---|
| 174 | return -1; |
---|
| 175 | |
---|
| 176 | msg->data[num] = 0; |
---|
| 177 | |
---|
| 178 | msg->datasp = msg->data; |
---|
| 179 | msg->dataslen = msg->datalen; |
---|
| 180 | |
---|
| 181 | for (num = 0; msg->data[num] == ' '; num++) { |
---|
| 182 | msg->datasp++; |
---|
| 183 | msg->dataslen--; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | return 0; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | int emul_rsound_vsend_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) { |
---|
| 190 | ssize_t ret; |
---|
| 191 | |
---|
| 192 | if ( msg == NULL || vio == NULL ) |
---|
| 193 | return -1; |
---|
| 194 | |
---|
| 195 | snprintf(msg->header, EMUL_RSOUND_MSG_HEADER_LEN+1, "RSD%5d", msg->datalen); |
---|
| 196 | |
---|
| 197 | ret = roar_vio_write(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN); |
---|
| 198 | |
---|
| 199 | if ( ret != EMUL_RSOUND_MSG_HEADER_LEN ) |
---|
| 200 | return -1; |
---|
| 201 | |
---|
| 202 | ret = roar_vio_write(vio, msg->data, msg->datalen); |
---|
| 203 | |
---|
| 204 | if ( ret != (ssize_t)msg->datalen ) |
---|
| 205 | return -1; |
---|
| 206 | |
---|
| 207 | return 0; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | int emul_rsound_check_client(int client, struct roar_vio_calls * vio) { |
---|
[3824] | 211 | struct roar_vio_calls rvio; |
---|
| 212 | struct emul_rsound_msg msg; |
---|
| 213 | struct roar_client * c; |
---|
| 214 | struct roar_stream_server * ss; |
---|
| 215 | int streamid; |
---|
| 216 | int i; |
---|
| 217 | ssize_t ptr; |
---|
[3820] | 218 | |
---|
| 219 | if ( vio == NULL ) { |
---|
| 220 | vio = &rvio; |
---|
| 221 | roar_vio_open_fh_socket(vio, clients_get_fh(client)); |
---|
| 222 | } |
---|
| 223 | |
---|
[3825] | 224 | // we get called in a loop, in case this fails no problem, just |
---|
| 225 | // return -1, caller will delete us in case of real error. |
---|
[3820] | 226 | if ( emul_rsound_vrecv_msg(&msg, vio) == -1 ) |
---|
[3825] | 227 | return -1; |
---|
[3820] | 228 | |
---|
| 229 | if ( !strncmp(msg.datasp, "INFO", 4) ) { |
---|
| 230 | // TODO: add support for INFO |
---|
[3824] | 231 | if ( clients_get(client, &c) == -1 ) |
---|
| 232 | return clients_delete(client); |
---|
| 233 | |
---|
| 234 | streamid = -1; |
---|
| 235 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) |
---|
| 236 | if ( c->streams[i] > streamid ) |
---|
| 237 | streamid = c->streams[i]; |
---|
| 238 | |
---|
| 239 | if ( streamid == -1 ) |
---|
| 240 | return clients_delete(client); |
---|
| 241 | |
---|
| 242 | if ( streams_get(streamid, &ss) == -1 ) |
---|
| 243 | return clients_delete(client); |
---|
| 244 | |
---|
| 245 | ptr = roar_info2samplesize(&(ROAR_STREAM(ss)->info)); |
---|
| 246 | |
---|
| 247 | if ( ptr == -1 ) |
---|
| 248 | return clients_delete(client); |
---|
| 249 | |
---|
| 250 | ptr *= ROAR_STREAM(ss)->pos; |
---|
| 251 | ptr /= 8; // bits -> bytes |
---|
| 252 | |
---|
| 253 | i = snprintf(msg.data+msg.datalen, EMUL_RSOUND_MSG_DATA_LEN - msg.datalen, " %lld", (long long int)ptr); |
---|
| 254 | |
---|
| 255 | msg.datalen += i; |
---|
| 256 | |
---|
| 257 | return emul_rsound_vsend_msg(&msg, vio); |
---|
[3820] | 258 | } else if ( !strncmp(msg.datasp, "NULL", 4) ) { |
---|
| 259 | // NULL is simular to NOOP |
---|
| 260 | } else if ( !strncmp(msg.datasp, "STOP", 4) ) { |
---|
| 261 | // This is quit. |
---|
| 262 | return clients_delete(client); |
---|
| 263 | } else { |
---|
[3841] | 264 | // Unknown command, kill the client. |
---|
[3820] | 265 | return clients_delete(client); |
---|
| 266 | } |
---|
[3825] | 267 | |
---|
| 268 | return 0; |
---|
[3820] | 269 | } |
---|
[3684] | 270 | #endif |
---|
| 271 | |
---|
| 272 | //ll |
---|