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