source: roaraudio/libroar/stream.c @ 761:63700d19fee7

Last change on this file since 761:63700d19fee7 was 761:63700d19fee7, checked in by phi, 16 years ago

return error on error ;)

File size: 9.3 KB
Line 
1//stream.c:
2
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
35#include "libroar.h"
36
37int 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
63int 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
91int 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
107int roar_stream_connect_to (struct roar_connection * con, struct roar_stream * s, int type, char * host, int port) {
108 struct roar_message m;
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
121int roar_stream_connect_to_ask (struct roar_connection * con, struct roar_stream * s, int type, char * host, int port) {
122 struct roar_message m;
123 int len = 0;
124
125 if ( host == NULL )
126  return -1;
127
128 ROAR_DBG("roar_stream_connect_to_ask(*): Ask the server to connect to: %s:%i", host, port);
129
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
147 if ( roar_send_message(con, &m, NULL) == -1 )
148  return -1;
149
150 return 0;
151}
152
153int 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;
159 m.datalen = 0;
160
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);
165  return -1;
166 }
167
168 ROAR_DBG("roar_stream_passfh(*): msg send");
169
170 if ( roar_socket_send_fh(con->fh, fh, NULL, 0) == -1 )
171  return -1;
172
173 ROAR_DBG("roar_stream_passfh(*): fh send");
174
175 if ( roar_recv_message(con, &m, NULL) == -1 )
176  return -1;
177
178 ROAR_DBG("roar_stream_passfh(*): mes recved");
179
180 if ( m.cmd == ROAR_CMD_OK )
181  return 0;
182
183 return -1;
184}
185
186int roar_stream_add_data (struct roar_connection * con, struct roar_stream * s, char * data, size_t len) {
187 struct roar_message m;
188
189 m.cmd     = ROAR_CMD_ADD_DATA;
190 m.stream  = s->id;
191 m.pos     = 0;
192 m.datalen = len;
193
194// if ( roar_req(con, &m, (void**)&data) == -1 )
195//  return -1;
196 if ( roar_send_message(con, &m, data) != 0 )
197  return -1;
198
199 if ( roar_recv_message(con, &m, NULL) == -1 )
200  return -1;
201
202 if ( m.cmd == ROAR_CMD_OK )
203  return 0;
204 return -1;
205}
206
207int roar_stream_send_data (struct roar_connection * con, struct roar_stream * s, char * data, size_t len) {
208 if ( ! s )
209  return -1;
210
211 if ( s->fh == -1 ) {
212  if ( !con )
213   return -1;
214
215  if ( roar_stream_add_data(con, s, data, len) == -1 )
216   return -1;
217
218  return len;
219 }
220
221 return write(s->fh, data, len);
222}
223
224int roar_stream_get_info (struct roar_connection * con, struct roar_stream * s, struct roar_stream_info * info) {
225 struct roar_message m;
226 uint16_t * data = (uint16_t *) m.data;
227 int i;
228
229 m.cmd     = ROAR_CMD_GET_STREAM_PARA;
230 m.stream  = s->id;
231 m.datalen = 4;
232 m.pos     = 0;
233
234 data[0] = 0; // Version and reserved
235 data[1] = 1; // stream
236
237 for (i = 0; i < m.datalen/2; i++) {
238  data[i] = ROAR_HOST2NET16(data[i]);
239 }
240
241 if ( roar_req(con, &m, NULL) == -1 )
242  return -1;
243
244 if ( m.cmd != ROAR_CMD_OK )
245  return -1;
246
247 for (i = 0; i < m.datalen/2; i++) {
248  data[i] = ROAR_NET2HOST16(data[i]);
249 }
250
251 if ( m.datalen < 6*2 )
252  return -1;
253
254 if ( data[0] != 0 || data[1] != 1 )
255  return -1;
256
257 info->block_size     = data[2];
258 info->pre_underruns  = data[3];
259 info->post_underruns = data[4];
260 info->codec          = data[5];
261
262 return 0;
263}
264
265#define _ROAR_STREAM_MESSAGE_LEN ((5+1)*4)
266
267int roar_stream_s2m     (struct roar_stream * s, struct roar_message * m) {
268 uint32_t * data;
269 int i;
270
271 if ( !(s && m) )
272  return -1;
273
274 m->datalen = _ROAR_STREAM_MESSAGE_LEN;
275 data = (uint32_t*) m->data;
276
277 data[0] = s->dir;
278 data[1] = s->pos_rel_id;
279 data[2] = s->info.rate;
280 data[3] = s->info.bits;
281 data[4] = s->info.channels;
282 data[5] = s->info.codec;
283
284 for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++)
285  data[i] = ROAR_HOST2NET32(data[i]);
286
287 ROAR_DBG("roar_stream_s2m(*): s->info:");
288 roar_debug_audio_info_print(&s->info);
289
290 return 0;
291}
292int roar_stream_m2s     (struct roar_stream * s, struct roar_message * m) {
293 uint32_t * data;
294 int i;
295
296 if ( !(s && m) )
297  return -1;
298
299 if ( m->datalen != _ROAR_STREAM_MESSAGE_LEN )
300  return -1;
301
302 data = (uint32_t*) m->data;
303
304 for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++)
305  data[i] = ROAR_NET2HOST32(data[i]);
306
307 s->id            = m->stream;
308 s->dir           = data[0];
309 s->pos_rel_id    = data[1];
310 s->info.rate     = data[2];
311 s->info.bits     = data[3];
312 s->info.channels = data[4];
313 s->info.codec    = data[5];
314
315 ROAR_DBG("roar_stream_m2s(*): s->info:");
316 roar_debug_audio_info_print(&s->info);
317
318 return 0;
319}
320
321
322// codec funcs:
323
324/*
325#define roar_codec2str(x) ((x) == ROAR_CODEC_PCM_S_LE  ? "pcm_s_le"  : (x) == ROAR_CODEC_PCM_S_BE  ? "pcm_s_be"  : \
326                           (x) == ROAR_CODEC_PCM_S_PDP ? "pcm_s_pdp" : (x) == ROAR_CODEC_MIDI_FILE ? "midi_file" : \
327                           "unknown" )
328*/
329
330struct {
331 int    codec;
332 char * name;
333} _libroar_codec[] = {
334 // PCM:
335 {ROAR_CODEC_PCM_S_LE,    "pcm_s_le"   },
336 {ROAR_CODEC_PCM_S_BE,    "pcm_s_be"   },
337 {ROAR_CODEC_PCM_S_PDP,   "pcm_s_pdp"  },
338 {ROAR_CODEC_PCM_U_LE,    "pcm_u_le"   },
339 {ROAR_CODEC_PCM_U_BE,    "pcm_u_be"   },
340 {ROAR_CODEC_PCM_U_PDP,   "pcm_u_pdp"  },
341 {ROAR_CODEC_DEFAULT,     "default"    }, // alias
342 {ROAR_CODEC_DEFAULT,     "pcm"        }, // alias
343 {ROAR_CODEC_DEFAULT,     "raw"        }, // alias
344
345 // MIDI:
346 {ROAR_CODEC_MIDI_FILE,   "midi_file"  },
347
348 // XIPH:
349 {ROAR_CODEC_OGG_VORBIS,  "ogg_vorbis" },
350 {ROAR_CODEC_OGG_VORBIS,  "vorbis"     }, // alias
351 {ROAR_CODEC_FLAC,        "flac"       },
352 {ROAR_CODEC_OGG_SPEEX,   "ogg_speex"  },
353 {ROAR_CODEC_OGG_SPEEX,   "speex"      }, // alias
354 {ROAR_CODEC_OGG_FLAC,    "ogg_flac"   },
355 {ROAR_CODEC_OGG_GENERAL, "ogg_general"},
356 {ROAR_CODEC_ROAR_CELT,   "roar_celt"  },
357 {ROAR_CODEC_ROAR_SPEEX,  "roar_speex" },
358
359 // RIFF/WAVE like:
360 {ROAR_CODEC_RIFF_WAVE,   "riff_wave"  },
361 {ROAR_CODEC_RIFF_WAVE,   "wave"       }, // alias
362 {ROAR_CODEC_RIFF_WAVE,   "wav"        }, // alias
363
364 //Log codecs:
365 {ROAR_CODEC_ALAW,        "alaw"       },
366 {ROAR_CODEC_MULAW,       "mulaw"      },
367 {ROAR_CODEC_MULAW,       "ulaw"       }, // alias
368
369 {-1, NULL}
370};
371
372int roar_str2codec(char * codec) {
373 int i;
374 int guess;
375
376 if ( codec == NULL || *codec == 0 )
377  return ROAR_CODEC_DEFAULT;
378
379 if ( (guess = atoi(codec)) > 0 )
380  return guess;
381
382 if ( codec == NULL || *codec == 0 )
383  return ROAR_CODEC_DEFAULT;
384
385 for (i = 0; _libroar_codec[i].codec != -1; i++)
386  if ( strcasecmp(_libroar_codec[i].name, codec) == 0 )
387   return _libroar_codec[i].codec;
388
389 return -1;
390}
391
392
393char * roar_codec2str (int codec) {
394 int i;
395
396 for (i = 0; _libroar_codec[i].codec != -1; i++)
397  if ( _libroar_codec[i].codec == codec )
398   return _libroar_codec[i].name;
399
400 return "unknown";
401}
402
403//ll
Note: See TracBrowser for help on using the repository browser.