source: roaraudio/libroar/stream.c @ 758:2bb998ad89ce

Last change on this file since 758:2bb998ad89ce was 758:2bb998ad89ce, checked in by phi, 16 years ago

added roar_stream_passfh()

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