[0] | 1 | //stream.c: |
---|
| 2 | |
---|
| 3 | #include "libroar.h" |
---|
| 4 | |
---|
| 5 | int roar_stream_connect (struct roar_connection * con, struct roar_stream * s, int dir) { |
---|
| 6 | struct roar_message m; |
---|
| 7 | |
---|
| 8 | s->dir = dir; |
---|
| 9 | |
---|
| 10 | m.cmd = ROAR_CMD_NEW_STREAM; |
---|
| 11 | m.stream = -1; |
---|
| 12 | m.pos = 0; |
---|
| 13 | |
---|
| 14 | roar_stream_s2m(s, &m); |
---|
| 15 | |
---|
| 16 | if ( roar_req(con, &m, NULL) != 0 ) |
---|
| 17 | return -1; |
---|
| 18 | |
---|
| 19 | if ( m.cmd == ROAR_CMD_OK ) { |
---|
| 20 | s->id = m.stream; |
---|
| 21 | |
---|
| 22 | ROAR_DBG("roar_stream_connect(*) = 0"); |
---|
| 23 | return 0; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | ROAR_ERR("roar_stream_connect(*): Connecting new stream faild!"); |
---|
| 27 | ROAR_DBG("roar_stream_connect(*) = -1"); |
---|
| 28 | return -1; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | int roar_stream_new (struct roar_stream * s, unsigned int rate, |
---|
| 32 | unsigned int channels, unsigned int bits, unsigned int codec) { |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | s->fh = -1; |
---|
| 36 | s->id = -1; |
---|
| 37 | s->pos = 0; |
---|
| 38 | s->pos_rel_id = -1; |
---|
| 39 | |
---|
| 40 | s->dir = ROAR_DIR_DEFAULT; |
---|
| 41 | |
---|
| 42 | s->datalen = 0; |
---|
| 43 | s->offset = 0; |
---|
| 44 | |
---|
| 45 | s->database = NULL; |
---|
| 46 | s->dataoff = NULL; |
---|
| 47 | |
---|
| 48 | s->info.rate = rate; |
---|
| 49 | s->info.channels = channels; |
---|
| 50 | s->info.bits = bits; |
---|
| 51 | s->info.codec = codec; |
---|
| 52 | |
---|
| 53 | if ( bits > ROAR_BITS_MAX ) |
---|
| 54 | return -1; |
---|
| 55 | |
---|
| 56 | return 0; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | int roar_stream_exec (struct roar_connection * con, struct roar_stream * s) { |
---|
| 60 | struct roar_message m; |
---|
| 61 | |
---|
| 62 | m.cmd = ROAR_CMD_EXEC_STREAM; |
---|
| 63 | m.stream = s->id; |
---|
| 64 | m.datalen = 0; |
---|
| 65 | m.pos = 0; |
---|
| 66 | |
---|
| 67 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 68 | return -1; |
---|
| 69 | |
---|
| 70 | if ( m.cmd == ROAR_CMD_OK ) |
---|
| 71 | return 0; |
---|
| 72 | return -1; |
---|
| 73 | } |
---|
| 74 | |
---|
[81] | 75 | int roar_stream_connect_to (struct roar_connection * con, struct roar_stream * s, int type, char * host, int port) { |
---|
| 76 | struct roar_message m; |
---|
[82] | 77 | |
---|
| 78 | if ( roar_stream_connect_to_ask(con, s, type, host, port) == -1 ) |
---|
| 79 | return -1; |
---|
| 80 | |
---|
| 81 | if ( roar_recv_message(con, &m, NULL) == -1 ) |
---|
| 82 | return -1; |
---|
| 83 | |
---|
| 84 | if ( m.cmd == ROAR_CMD_OK ) |
---|
| 85 | return 0; |
---|
| 86 | return -1; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | int roar_stream_connect_to_ask (struct roar_connection * con, struct roar_stream * s, int type, char * host, int port) { |
---|
| 90 | struct roar_message m; |
---|
[81] | 91 | int len = 0; |
---|
| 92 | |
---|
| 93 | if ( host == NULL ) |
---|
| 94 | return -1; |
---|
| 95 | |
---|
[487] | 96 | ROAR_DBG("roar_stream_connect_to_ask(*): Ask the server to connect to: %s:%i", host, port); |
---|
| 97 | |
---|
[81] | 98 | m.cmd = ROAR_CMD_CON_STREAM; |
---|
| 99 | m.stream = s->id; |
---|
| 100 | m.pos = 0; |
---|
| 101 | |
---|
| 102 | m.data[0] = 0; |
---|
| 103 | m.data[1] = type; |
---|
| 104 | ((uint16_t*)&(m.data))[1] = ROAR_HOST2NET16(port); |
---|
| 105 | |
---|
| 106 | len = strlen(host); |
---|
| 107 | |
---|
| 108 | if ( len > 76 ) |
---|
| 109 | return -1; |
---|
| 110 | |
---|
| 111 | strncpy(&(m.data[4]), host, len); |
---|
| 112 | |
---|
| 113 | m.datalen = len + 4; |
---|
| 114 | |
---|
[82] | 115 | if ( roar_send_message(con, &m, NULL) == -1 ) |
---|
[81] | 116 | return -1; |
---|
| 117 | |
---|
[82] | 118 | return 0; |
---|
[81] | 119 | } |
---|
[0] | 120 | |
---|
| 121 | int roar_stream_add_data (struct roar_connection * con, struct roar_stream * s, char * data, size_t len) { |
---|
| 122 | struct roar_message m; |
---|
| 123 | |
---|
| 124 | m.cmd = ROAR_CMD_ADD_DATA; |
---|
| 125 | m.stream = s->id; |
---|
[134] | 126 | m.pos = 0; |
---|
[0] | 127 | m.datalen = len; |
---|
| 128 | |
---|
| 129 | // if ( roar_req(con, &m, (void**)&data) == -1 ) |
---|
| 130 | // return -1; |
---|
| 131 | if ( roar_send_message(con, &m, data) != 0 ) |
---|
| 132 | return -1; |
---|
| 133 | |
---|
| 134 | if ( roar_recv_message(con, &m, NULL) == -1 ) |
---|
| 135 | return -1; |
---|
| 136 | |
---|
| 137 | if ( m.cmd == ROAR_CMD_OK ) |
---|
| 138 | return 0; |
---|
| 139 | return -1; |
---|
| 140 | } |
---|
| 141 | |
---|
[506] | 142 | int roar_stream_send_data (struct roar_connection * con, struct roar_stream * s, char * data, size_t len) { |
---|
| 143 | if ( ! s ) |
---|
| 144 | return -1; |
---|
| 145 | |
---|
| 146 | if ( s->fh == -1 ) { |
---|
| 147 | if ( !con ) |
---|
| 148 | return -1; |
---|
| 149 | |
---|
| 150 | if ( roar_stream_add_data(con, s, data, len) == -1 ) |
---|
| 151 | return -1; |
---|
| 152 | |
---|
| 153 | return len; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | return write(s->fh, data, len); |
---|
| 157 | } |
---|
| 158 | |
---|
[465] | 159 | int roar_stream_get_info (struct roar_connection * con, struct roar_stream * s, struct roar_stream_info * info) { |
---|
| 160 | struct roar_message m; |
---|
| 161 | uint16_t * data = (uint16_t *) m.data; |
---|
| 162 | int i; |
---|
| 163 | |
---|
| 164 | m.cmd = ROAR_CMD_GET_STREAM_PARA; |
---|
| 165 | m.stream = s->id; |
---|
| 166 | m.datalen = 4; |
---|
| 167 | m.pos = 0; |
---|
| 168 | |
---|
| 169 | data[0] = 0; // Version and reserved |
---|
| 170 | data[1] = 1; // stream |
---|
| 171 | |
---|
| 172 | for (i = 0; i < m.datalen/2; i++) { |
---|
| 173 | data[i] = ROAR_HOST2NET16(data[i]); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 177 | return -1; |
---|
| 178 | |
---|
| 179 | if ( m.cmd != ROAR_CMD_OK ) |
---|
| 180 | return -1; |
---|
| 181 | |
---|
| 182 | for (i = 0; i < m.datalen/2; i++) { |
---|
| 183 | data[i] = ROAR_NET2HOST16(data[i]); |
---|
| 184 | } |
---|
| 185 | |
---|
[540] | 186 | if ( m.datalen < 6*2 ) |
---|
[465] | 187 | return -1; |
---|
| 188 | |
---|
| 189 | if ( data[0] != 0 || data[1] != 1 ) |
---|
| 190 | return -1; |
---|
| 191 | |
---|
[496] | 192 | info->block_size = data[2]; |
---|
| 193 | info->pre_underruns = data[3]; |
---|
| 194 | info->post_underruns = data[4]; |
---|
[540] | 195 | info->codec = data[5]; |
---|
[465] | 196 | |
---|
| 197 | return 0; |
---|
| 198 | } |
---|
[0] | 199 | |
---|
| 200 | #define _ROAR_STREAM_MESSAGE_LEN ((5+1)*4) |
---|
| 201 | |
---|
| 202 | int roar_stream_s2m (struct roar_stream * s, struct roar_message * m) { |
---|
| 203 | uint32_t * data; |
---|
| 204 | int i; |
---|
| 205 | |
---|
| 206 | if ( !(s && m) ) |
---|
| 207 | return -1; |
---|
| 208 | |
---|
| 209 | m->datalen = _ROAR_STREAM_MESSAGE_LEN; |
---|
| 210 | data = (uint32_t*) m->data; |
---|
| 211 | |
---|
| 212 | data[0] = s->dir; |
---|
| 213 | data[1] = s->pos_rel_id; |
---|
| 214 | data[2] = s->info.rate; |
---|
| 215 | data[3] = s->info.bits; |
---|
| 216 | data[4] = s->info.channels; |
---|
| 217 | data[5] = s->info.codec; |
---|
| 218 | |
---|
| 219 | for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++) |
---|
| 220 | data[i] = ROAR_HOST2NET32(data[i]); |
---|
| 221 | |
---|
| 222 | ROAR_DBG("roar_stream_s2m(*): s->info:"); |
---|
| 223 | roar_debug_audio_info_print(&s->info); |
---|
| 224 | |
---|
| 225 | return 0; |
---|
| 226 | } |
---|
| 227 | int roar_stream_m2s (struct roar_stream * s, struct roar_message * m) { |
---|
| 228 | uint32_t * data; |
---|
| 229 | int i; |
---|
| 230 | |
---|
| 231 | if ( !(s && m) ) |
---|
| 232 | return -1; |
---|
| 233 | |
---|
| 234 | if ( m->datalen != _ROAR_STREAM_MESSAGE_LEN ) |
---|
| 235 | return -1; |
---|
| 236 | |
---|
| 237 | data = (uint32_t*) m->data; |
---|
| 238 | |
---|
| 239 | for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++) |
---|
| 240 | data[i] = ROAR_NET2HOST32(data[i]); |
---|
| 241 | |
---|
[465] | 242 | s->id = m->stream; |
---|
[0] | 243 | s->dir = data[0]; |
---|
| 244 | s->pos_rel_id = data[1]; |
---|
| 245 | s->info.rate = data[2]; |
---|
| 246 | s->info.bits = data[3]; |
---|
| 247 | s->info.channels = data[4]; |
---|
| 248 | s->info.codec = data[5]; |
---|
| 249 | |
---|
| 250 | ROAR_DBG("roar_stream_m2s(*): s->info:"); |
---|
| 251 | roar_debug_audio_info_print(&s->info); |
---|
| 252 | |
---|
| 253 | return 0; |
---|
| 254 | } |
---|
| 255 | |
---|
[537] | 256 | |
---|
| 257 | // codec funcs: |
---|
| 258 | |
---|
| 259 | /* |
---|
| 260 | #define roar_codec2str(x) ((x) == ROAR_CODEC_PCM_S_LE ? "pcm_s_le" : (x) == ROAR_CODEC_PCM_S_BE ? "pcm_s_be" : \ |
---|
| 261 | (x) == ROAR_CODEC_PCM_S_PDP ? "pcm_s_pdp" : (x) == ROAR_CODEC_MIDI_FILE ? "midi_file" : \ |
---|
| 262 | "unknown" ) |
---|
| 263 | */ |
---|
| 264 | |
---|
| 265 | struct { |
---|
| 266 | int codec; |
---|
| 267 | char * name; |
---|
| 268 | } _libroar_codec[] = { |
---|
| 269 | // PCM: |
---|
| 270 | {ROAR_CODEC_PCM_S_LE, "pcm_s_le" }, |
---|
| 271 | {ROAR_CODEC_PCM_S_BE, "pcm_s_be" }, |
---|
| 272 | {ROAR_CODEC_PCM_S_PDP, "pcm_s_pdp" }, |
---|
| 273 | {ROAR_CODEC_PCM_U_LE, "pcm_u_le" }, |
---|
| 274 | {ROAR_CODEC_PCM_U_BE, "pcm_u_be" }, |
---|
| 275 | {ROAR_CODEC_PCM_U_PDP, "pcm_u_pdp" }, |
---|
| 276 | {ROAR_CODEC_DEFAULT, "default" }, // alias |
---|
| 277 | {ROAR_CODEC_DEFAULT, "pcm" }, // alias |
---|
[557] | 278 | {ROAR_CODEC_DEFAULT, "raw" }, // alias |
---|
[537] | 279 | |
---|
| 280 | // MIDI: |
---|
| 281 | {ROAR_CODEC_MIDI_FILE, "midi_file" }, |
---|
| 282 | |
---|
| 283 | // XIPH: |
---|
| 284 | {ROAR_CODEC_OGG_VORBIS, "ogg_vorbis" }, |
---|
| 285 | {ROAR_CODEC_OGG_VORBIS, "vorbis" }, // alias |
---|
| 286 | {ROAR_CODEC_FLAC, "flac" }, |
---|
| 287 | {ROAR_CODEC_OGG_SPEEX, "ogg_speex" }, |
---|
| 288 | {ROAR_CODEC_OGG_SPEEX, "speex" }, // alias |
---|
| 289 | {ROAR_CODEC_OGG_FLAC, "ogg_flac" }, |
---|
| 290 | {ROAR_CODEC_OGG_GENERAL, "ogg_general"}, |
---|
| 291 | {ROAR_CODEC_ROAR_CELT, "roar_celt" }, |
---|
| 292 | {ROAR_CODEC_ROAR_SPEEX, "roar_speex" }, |
---|
[560] | 293 | |
---|
| 294 | // RIFF/WAVE like: |
---|
| 295 | {ROAR_CODEC_RIFF_WAVE, "riff_wave" }, |
---|
| 296 | {ROAR_CODEC_RIFF_WAVE, "wave" }, // alias |
---|
| 297 | {ROAR_CODEC_RIFF_WAVE, "wav" }, // alias |
---|
| 298 | |
---|
[537] | 299 | {-1, NULL} |
---|
| 300 | }; |
---|
| 301 | |
---|
| 302 | int roar_str2codec(char * codec) { |
---|
| 303 | int i; |
---|
[549] | 304 | int guess; |
---|
[537] | 305 | |
---|
[549] | 306 | if ( codec == NULL || *codec == 0 ) |
---|
| 307 | return ROAR_CODEC_DEFAULT; |
---|
| 308 | |
---|
| 309 | if ( (guess = atoi(codec)) > 0 ) |
---|
[537] | 310 | return guess; |
---|
| 311 | |
---|
| 312 | if ( codec == NULL || *codec == 0 ) |
---|
| 313 | return ROAR_CODEC_DEFAULT; |
---|
| 314 | |
---|
| 315 | for (i = 0; _libroar_codec[i].codec != -1; i++) |
---|
| 316 | if ( strcasecmp(_libroar_codec[i].name, codec) == 0 ) |
---|
| 317 | return _libroar_codec[i].codec; |
---|
| 318 | |
---|
| 319 | return -1; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | |
---|
| 323 | char * roar_codec2str (int codec) { |
---|
| 324 | int i; |
---|
| 325 | |
---|
| 326 | for (i = 0; _libroar_codec[i].codec != -1; i++) |
---|
| 327 | if ( _libroar_codec[i].codec == codec ) |
---|
| 328 | return _libroar_codec[i].name; |
---|
| 329 | |
---|
| 330 | return "unknown"; |
---|
| 331 | } |
---|
| 332 | |
---|
[0] | 333 | //ll |
---|