[0] | 1 | //stream.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of libroar 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 | * libroar 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 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
[0] | 35 | #include "libroar.h" |
---|
| 36 | |
---|
| 37 | int roar_stream_connect (struct roar_connection * con, struct roar_stream * s, int dir) { |
---|
| 38 | struct roar_message m; |
---|
| 39 | |
---|
| 40 | s->dir = dir; |
---|
| 41 | |
---|
| 42 | m.cmd = ROAR_CMD_NEW_STREAM; |
---|
| 43 | m.stream = -1; |
---|
| 44 | m.pos = 0; |
---|
| 45 | |
---|
| 46 | roar_stream_s2m(s, &m); |
---|
| 47 | |
---|
| 48 | if ( roar_req(con, &m, NULL) != 0 ) |
---|
| 49 | return -1; |
---|
| 50 | |
---|
| 51 | if ( m.cmd == ROAR_CMD_OK ) { |
---|
| 52 | s->id = m.stream; |
---|
| 53 | |
---|
| 54 | ROAR_DBG("roar_stream_connect(*) = 0"); |
---|
| 55 | return 0; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | ROAR_ERR("roar_stream_connect(*): Connecting new stream faild!"); |
---|
| 59 | ROAR_DBG("roar_stream_connect(*) = -1"); |
---|
| 60 | return -1; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | int roar_stream_new (struct roar_stream * s, unsigned int rate, |
---|
| 64 | unsigned int channels, unsigned int bits, unsigned int codec) { |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | s->fh = -1; |
---|
| 68 | s->id = -1; |
---|
| 69 | s->pos = 0; |
---|
| 70 | s->pos_rel_id = -1; |
---|
| 71 | |
---|
| 72 | s->dir = ROAR_DIR_DEFAULT; |
---|
| 73 | |
---|
| 74 | s->datalen = 0; |
---|
| 75 | s->offset = 0; |
---|
| 76 | |
---|
| 77 | s->database = NULL; |
---|
| 78 | s->dataoff = NULL; |
---|
| 79 | |
---|
| 80 | s->info.rate = rate; |
---|
| 81 | s->info.channels = channels; |
---|
| 82 | s->info.bits = bits; |
---|
| 83 | s->info.codec = codec; |
---|
| 84 | |
---|
| 85 | if ( bits > ROAR_BITS_MAX ) |
---|
| 86 | return -1; |
---|
| 87 | |
---|
| 88 | return 0; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | int roar_stream_exec (struct roar_connection * con, struct roar_stream * s) { |
---|
| 92 | struct roar_message m; |
---|
| 93 | |
---|
| 94 | m.cmd = ROAR_CMD_EXEC_STREAM; |
---|
| 95 | m.stream = s->id; |
---|
| 96 | m.datalen = 0; |
---|
| 97 | m.pos = 0; |
---|
| 98 | |
---|
| 99 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 100 | return -1; |
---|
| 101 | |
---|
| 102 | if ( m.cmd == ROAR_CMD_OK ) |
---|
| 103 | return 0; |
---|
| 104 | return -1; |
---|
| 105 | } |
---|
| 106 | |
---|
[81] | 107 | int roar_stream_connect_to (struct roar_connection * con, struct roar_stream * s, int type, char * host, int port) { |
---|
| 108 | struct roar_message m; |
---|
[82] | 109 | |
---|
| 110 | if ( roar_stream_connect_to_ask(con, s, type, host, port) == -1 ) |
---|
| 111 | return -1; |
---|
| 112 | |
---|
| 113 | if ( roar_recv_message(con, &m, NULL) == -1 ) |
---|
| 114 | return -1; |
---|
| 115 | |
---|
| 116 | if ( m.cmd == ROAR_CMD_OK ) |
---|
| 117 | return 0; |
---|
| 118 | return -1; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | int roar_stream_connect_to_ask (struct roar_connection * con, struct roar_stream * s, int type, char * host, int port) { |
---|
| 122 | struct roar_message m; |
---|
[81] | 123 | int len = 0; |
---|
| 124 | |
---|
| 125 | if ( host == NULL ) |
---|
| 126 | return -1; |
---|
| 127 | |
---|
[487] | 128 | ROAR_DBG("roar_stream_connect_to_ask(*): Ask the server to connect to: %s:%i", host, port); |
---|
| 129 | |
---|
[81] | 130 | m.cmd = ROAR_CMD_CON_STREAM; |
---|
| 131 | m.stream = s->id; |
---|
| 132 | m.pos = 0; |
---|
| 133 | |
---|
| 134 | m.data[0] = 0; |
---|
| 135 | m.data[1] = type; |
---|
| 136 | ((uint16_t*)&(m.data))[1] = ROAR_HOST2NET16(port); |
---|
| 137 | |
---|
| 138 | len = strlen(host); |
---|
| 139 | |
---|
| 140 | if ( len > 76 ) |
---|
| 141 | return -1; |
---|
| 142 | |
---|
| 143 | strncpy(&(m.data[4]), host, len); |
---|
| 144 | |
---|
| 145 | m.datalen = len + 4; |
---|
| 146 | |
---|
[82] | 147 | if ( roar_send_message(con, &m, NULL) == -1 ) |
---|
[81] | 148 | return -1; |
---|
| 149 | |
---|
[82] | 150 | return 0; |
---|
[81] | 151 | } |
---|
[0] | 152 | |
---|
[758] | 153 | int roar_stream_passfh (struct roar_connection * con, struct roar_stream * s, int fh) { |
---|
| 154 | struct roar_message m; |
---|
| 155 | |
---|
| 156 | m.cmd = ROAR_CMD_PASSFH; |
---|
| 157 | m.stream = s->id; |
---|
| 158 | m.pos = 0; |
---|
[759] | 159 | m.datalen = 0; |
---|
[758] | 160 | |
---|
[759] | 161 | ROAR_DBG("roar_stream_passfh(con={.fh=%i,...}, s={.id=%i,...}, fh=%i) = ?", con->fh, s->id, fh); |
---|
| 162 | |
---|
| 163 | if ( roar_send_message(con, &m, NULL) == -1 ) { |
---|
| 164 | ROAR_DBG("roar_stream_passfh(con={.fh=%i,...}, s={.id=%i,...}, fh=%i) = -1 // can not send message", con->fh, s->id, fh); |
---|
[758] | 165 | return -1; |
---|
[759] | 166 | } |
---|
| 167 | |
---|
| 168 | ROAR_DBG("roar_stream_passfh(*): msg send"); |
---|
[758] | 169 | |
---|
| 170 | if ( roar_socket_send_fh(con->fh, fh, NULL, 0) == -1 ) |
---|
| 171 | return -1; |
---|
| 172 | |
---|
[759] | 173 | ROAR_DBG("roar_stream_passfh(*): fh send"); |
---|
| 174 | |
---|
[758] | 175 | if ( roar_recv_message(con, &m, NULL) == -1 ) |
---|
| 176 | return -1; |
---|
| 177 | |
---|
[759] | 178 | ROAR_DBG("roar_stream_passfh(*): mes recved"); |
---|
| 179 | |
---|
[761] | 180 | if ( m.cmd == ROAR_CMD_OK ) |
---|
| 181 | return 0; |
---|
| 182 | |
---|
| 183 | return -1; |
---|
[758] | 184 | } |
---|
| 185 | |
---|
[773] | 186 | int roar_stream_attach_simple (struct roar_connection * con, struct roar_stream * s, int client) { |
---|
| 187 | struct roar_message m; |
---|
| 188 | uint16_t * info = (uint16_t *) m.data; |
---|
| 189 | int i; |
---|
| 190 | |
---|
| 191 | m.cmd = ROAR_CMD_ATTACH; |
---|
| 192 | m.stream = s->id; |
---|
| 193 | m.pos = 0; |
---|
| 194 | m.datalen = 6; |
---|
| 195 | |
---|
| 196 | info[0] = 0; |
---|
| 197 | info[1] = ROAR_ATTACH_SIMPLE; |
---|
| 198 | info[2] = client; |
---|
| 199 | |
---|
| 200 | for (i = 0; i < m.datalen/2; i++) { |
---|
| 201 | info[i] = ROAR_HOST2NET16(info[i]); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 205 | return -1; |
---|
| 206 | |
---|
| 207 | if ( m.cmd != ROAR_CMD_OK ) |
---|
| 208 | return -1; |
---|
| 209 | |
---|
| 210 | return 0; |
---|
| 211 | } |
---|
| 212 | |
---|
[0] | 213 | int roar_stream_add_data (struct roar_connection * con, struct roar_stream * s, char * data, size_t len) { |
---|
| 214 | struct roar_message m; |
---|
| 215 | |
---|
| 216 | m.cmd = ROAR_CMD_ADD_DATA; |
---|
| 217 | m.stream = s->id; |
---|
[134] | 218 | m.pos = 0; |
---|
[0] | 219 | m.datalen = len; |
---|
| 220 | |
---|
| 221 | // if ( roar_req(con, &m, (void**)&data) == -1 ) |
---|
| 222 | // return -1; |
---|
| 223 | if ( roar_send_message(con, &m, data) != 0 ) |
---|
| 224 | return -1; |
---|
| 225 | |
---|
| 226 | if ( roar_recv_message(con, &m, NULL) == -1 ) |
---|
| 227 | return -1; |
---|
| 228 | |
---|
| 229 | if ( m.cmd == ROAR_CMD_OK ) |
---|
| 230 | return 0; |
---|
| 231 | return -1; |
---|
| 232 | } |
---|
| 233 | |
---|
[506] | 234 | int roar_stream_send_data (struct roar_connection * con, struct roar_stream * s, char * data, size_t len) { |
---|
| 235 | if ( ! s ) |
---|
| 236 | return -1; |
---|
| 237 | |
---|
| 238 | if ( s->fh == -1 ) { |
---|
| 239 | if ( !con ) |
---|
| 240 | return -1; |
---|
| 241 | |
---|
| 242 | if ( roar_stream_add_data(con, s, data, len) == -1 ) |
---|
| 243 | return -1; |
---|
| 244 | |
---|
| 245 | return len; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | return write(s->fh, data, len); |
---|
| 249 | } |
---|
| 250 | |
---|
[465] | 251 | int roar_stream_get_info (struct roar_connection * con, struct roar_stream * s, struct roar_stream_info * info) { |
---|
| 252 | struct roar_message m; |
---|
| 253 | uint16_t * data = (uint16_t *) m.data; |
---|
| 254 | int i; |
---|
| 255 | |
---|
| 256 | m.cmd = ROAR_CMD_GET_STREAM_PARA; |
---|
| 257 | m.stream = s->id; |
---|
| 258 | m.datalen = 4; |
---|
| 259 | m.pos = 0; |
---|
| 260 | |
---|
| 261 | data[0] = 0; // Version and reserved |
---|
| 262 | data[1] = 1; // stream |
---|
| 263 | |
---|
| 264 | for (i = 0; i < m.datalen/2; i++) { |
---|
| 265 | data[i] = ROAR_HOST2NET16(data[i]); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 269 | return -1; |
---|
| 270 | |
---|
| 271 | if ( m.cmd != ROAR_CMD_OK ) |
---|
| 272 | return -1; |
---|
| 273 | |
---|
| 274 | for (i = 0; i < m.datalen/2; i++) { |
---|
| 275 | data[i] = ROAR_NET2HOST16(data[i]); |
---|
| 276 | } |
---|
| 277 | |
---|
[540] | 278 | if ( m.datalen < 6*2 ) |
---|
[465] | 279 | return -1; |
---|
| 280 | |
---|
| 281 | if ( data[0] != 0 || data[1] != 1 ) |
---|
| 282 | return -1; |
---|
| 283 | |
---|
[963] | 284 | memset(info, 0, sizeof(struct roar_stream_info)); |
---|
| 285 | |
---|
[496] | 286 | info->block_size = data[2]; |
---|
| 287 | info->pre_underruns = data[3]; |
---|
| 288 | info->post_underruns = data[4]; |
---|
[540] | 289 | info->codec = data[5]; |
---|
[963] | 290 | info->flags = data[6]; |
---|
[465] | 291 | |
---|
| 292 | return 0; |
---|
| 293 | } |
---|
[0] | 294 | |
---|
| 295 | #define _ROAR_STREAM_MESSAGE_LEN ((5+1)*4) |
---|
| 296 | |
---|
| 297 | int roar_stream_s2m (struct roar_stream * s, struct roar_message * m) { |
---|
| 298 | uint32_t * data; |
---|
| 299 | int i; |
---|
| 300 | |
---|
| 301 | if ( !(s && m) ) |
---|
| 302 | return -1; |
---|
| 303 | |
---|
| 304 | m->datalen = _ROAR_STREAM_MESSAGE_LEN; |
---|
| 305 | data = (uint32_t*) m->data; |
---|
| 306 | |
---|
| 307 | data[0] = s->dir; |
---|
| 308 | data[1] = s->pos_rel_id; |
---|
| 309 | data[2] = s->info.rate; |
---|
| 310 | data[3] = s->info.bits; |
---|
| 311 | data[4] = s->info.channels; |
---|
| 312 | data[5] = s->info.codec; |
---|
| 313 | |
---|
| 314 | for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++) |
---|
| 315 | data[i] = ROAR_HOST2NET32(data[i]); |
---|
| 316 | |
---|
| 317 | ROAR_DBG("roar_stream_s2m(*): s->info:"); |
---|
| 318 | roar_debug_audio_info_print(&s->info); |
---|
| 319 | |
---|
[964] | 320 | m->pos = s->pos; |
---|
| 321 | |
---|
[0] | 322 | return 0; |
---|
| 323 | } |
---|
| 324 | int roar_stream_m2s (struct roar_stream * s, struct roar_message * m) { |
---|
| 325 | uint32_t * data; |
---|
| 326 | int i; |
---|
| 327 | |
---|
| 328 | if ( !(s && m) ) |
---|
| 329 | return -1; |
---|
| 330 | |
---|
| 331 | if ( m->datalen != _ROAR_STREAM_MESSAGE_LEN ) |
---|
| 332 | return -1; |
---|
| 333 | |
---|
[964] | 334 | s->pos = m->pos; |
---|
| 335 | |
---|
[0] | 336 | data = (uint32_t*) m->data; |
---|
| 337 | |
---|
| 338 | for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++) |
---|
| 339 | data[i] = ROAR_NET2HOST32(data[i]); |
---|
| 340 | |
---|
[465] | 341 | s->id = m->stream; |
---|
[0] | 342 | s->dir = data[0]; |
---|
| 343 | s->pos_rel_id = data[1]; |
---|
| 344 | s->info.rate = data[2]; |
---|
| 345 | s->info.bits = data[3]; |
---|
| 346 | s->info.channels = data[4]; |
---|
| 347 | s->info.codec = data[5]; |
---|
| 348 | |
---|
| 349 | ROAR_DBG("roar_stream_m2s(*): s->info:"); |
---|
| 350 | roar_debug_audio_info_print(&s->info); |
---|
| 351 | |
---|
| 352 | return 0; |
---|
| 353 | } |
---|
| 354 | |
---|
[537] | 355 | |
---|
| 356 | // codec funcs: |
---|
| 357 | |
---|
| 358 | /* |
---|
| 359 | #define roar_codec2str(x) ((x) == ROAR_CODEC_PCM_S_LE ? "pcm_s_le" : (x) == ROAR_CODEC_PCM_S_BE ? "pcm_s_be" : \ |
---|
| 360 | (x) == ROAR_CODEC_PCM_S_PDP ? "pcm_s_pdp" : (x) == ROAR_CODEC_MIDI_FILE ? "midi_file" : \ |
---|
| 361 | "unknown" ) |
---|
| 362 | */ |
---|
| 363 | |
---|
| 364 | struct { |
---|
| 365 | int codec; |
---|
| 366 | char * name; |
---|
| 367 | } _libroar_codec[] = { |
---|
| 368 | // PCM: |
---|
| 369 | {ROAR_CODEC_PCM_S_LE, "pcm_s_le" }, |
---|
| 370 | {ROAR_CODEC_PCM_S_BE, "pcm_s_be" }, |
---|
| 371 | {ROAR_CODEC_PCM_S_PDP, "pcm_s_pdp" }, |
---|
| 372 | {ROAR_CODEC_PCM_U_LE, "pcm_u_le" }, |
---|
| 373 | {ROAR_CODEC_PCM_U_BE, "pcm_u_be" }, |
---|
| 374 | {ROAR_CODEC_PCM_U_PDP, "pcm_u_pdp" }, |
---|
| 375 | {ROAR_CODEC_DEFAULT, "default" }, // alias |
---|
| 376 | {ROAR_CODEC_DEFAULT, "pcm" }, // alias |
---|
[557] | 377 | {ROAR_CODEC_DEFAULT, "raw" }, // alias |
---|
[537] | 378 | |
---|
| 379 | // MIDI: |
---|
| 380 | {ROAR_CODEC_MIDI_FILE, "midi_file" }, |
---|
| 381 | |
---|
| 382 | // XIPH: |
---|
| 383 | {ROAR_CODEC_OGG_VORBIS, "ogg_vorbis" }, |
---|
| 384 | {ROAR_CODEC_OGG_VORBIS, "vorbis" }, // alias |
---|
| 385 | {ROAR_CODEC_FLAC, "flac" }, |
---|
| 386 | {ROAR_CODEC_OGG_SPEEX, "ogg_speex" }, |
---|
| 387 | {ROAR_CODEC_OGG_SPEEX, "speex" }, // alias |
---|
| 388 | {ROAR_CODEC_OGG_FLAC, "ogg_flac" }, |
---|
| 389 | {ROAR_CODEC_OGG_GENERAL, "ogg_general"}, |
---|
| 390 | {ROAR_CODEC_ROAR_CELT, "roar_celt" }, |
---|
| 391 | {ROAR_CODEC_ROAR_SPEEX, "roar_speex" }, |
---|
[560] | 392 | |
---|
[881] | 393 | // RAUM: |
---|
| 394 | {ROAR_CODEC_RAUM, "raum" }, |
---|
| 395 | {ROAR_CODEC_RAUM_VORBIS, "raum_vorbis"}, |
---|
| 396 | |
---|
[560] | 397 | // RIFF/WAVE like: |
---|
| 398 | {ROAR_CODEC_RIFF_WAVE, "riff_wave" }, |
---|
| 399 | {ROAR_CODEC_RIFF_WAVE, "wave" }, // alias |
---|
| 400 | {ROAR_CODEC_RIFF_WAVE, "wav" }, // alias |
---|
| 401 | |
---|
[733] | 402 | //Log codecs: |
---|
| 403 | {ROAR_CODEC_ALAW, "alaw" }, |
---|
| 404 | {ROAR_CODEC_MULAW, "mulaw" }, |
---|
| 405 | {ROAR_CODEC_MULAW, "ulaw" }, // alias |
---|
| 406 | |
---|
[537] | 407 | {-1, NULL} |
---|
| 408 | }; |
---|
| 409 | |
---|
| 410 | int roar_str2codec(char * codec) { |
---|
| 411 | int i; |
---|
[549] | 412 | int guess; |
---|
[537] | 413 | |
---|
[549] | 414 | if ( codec == NULL || *codec == 0 ) |
---|
| 415 | return ROAR_CODEC_DEFAULT; |
---|
| 416 | |
---|
| 417 | if ( (guess = atoi(codec)) > 0 ) |
---|
[537] | 418 | return guess; |
---|
| 419 | |
---|
| 420 | if ( codec == NULL || *codec == 0 ) |
---|
| 421 | return ROAR_CODEC_DEFAULT; |
---|
| 422 | |
---|
| 423 | for (i = 0; _libroar_codec[i].codec != -1; i++) |
---|
| 424 | if ( strcasecmp(_libroar_codec[i].name, codec) == 0 ) |
---|
| 425 | return _libroar_codec[i].codec; |
---|
| 426 | |
---|
| 427 | return -1; |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | char * roar_codec2str (int codec) { |
---|
| 432 | int i; |
---|
| 433 | |
---|
| 434 | for (i = 0; _libroar_codec[i].codec != -1; i++) |
---|
| 435 | if ( _libroar_codec[i].codec == codec ) |
---|
| 436 | return _libroar_codec[i].name; |
---|
| 437 | |
---|
| 438 | return "unknown"; |
---|
| 439 | } |
---|
| 440 | |
---|
[0] | 441 | //ll |
---|