source: roaraudio/libroar/stream.c @ 2588:1b7ef61d7dd2

Last change on this file since 2588:1b7ef61d7dd2 was 2588:1b7ef61d7dd2, checked in by phi, 15 years ago

added roar_stream_[gs]et_dir()

File size: 15.1 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 if ( s == NULL )
67  return -1;
68
69 s->fh         = -1;
70 s->id         = -1;
71 s->pos        =  0;
72 s->pos_rel_id = -1;
73
74 s->dir        = ROAR_DIR_DEFAULT;
75
76/*
77 s->datalen    = 0;
78 s->offset     = 0;
79
80 s->database   = NULL;
81 s->dataoff    = NULL;
82*/
83
84 s->info.rate     = rate;
85 s->info.channels = channels;
86 s->info.bits     = bits;
87 s->info.codec    = codec;
88
89 if ( bits > ROAR_BITS_MAX )
90  return -1;
91
92 return 0;
93}
94
95int roar_stream_set_rel_id(struct roar_stream * s, int id) {
96 if ( s == NULL )
97  return -1;
98
99 s->pos_rel_id = id;
100
101 return 0;
102}
103
104int roar_stream_get_rel_id(struct roar_stream * s) {
105 if ( s == NULL )
106  return -1;
107
108 return s->pos_rel_id;
109}
110
111int roar_stream_new_by_id(struct roar_stream * s, int id) {
112 if ( s == NULL )
113  return -1;
114
115 if ( roar_stream_new_empty(s) == -1 )
116  return -1;
117
118 return roar_stream_set_id(s, id);
119}
120
121int roar_stream_new_empty(struct roar_stream * s) {
122 if ( s == NULL )
123  return -1;
124
125 return roar_stream_new(s, 0, 0, 0, 0);
126}
127
128int roar_stream_set_id (struct roar_stream * s, int id) {
129 if ( s == NULL )
130  return -1;
131
132 s->id = id;
133
134 return 0;
135}
136
137int roar_stream_get_id (struct roar_stream * s) {
138 if ( s == NULL )
139  return -1;
140
141 return s->id;
142}
143
144int roar_stream_set_fh (struct roar_stream * s, int fh) {
145 if ( s == NULL )
146  return -1;
147
148 s->fh = fh;
149
150 return 0;
151}
152
153int roar_stream_get_fh (struct roar_stream * s) {
154 if ( s == NULL )
155  return -1;
156
157 return s->fh;
158}
159
160int roar_stream_set_dir (struct roar_stream * s, int dir) {
161 if ( s == NULL )
162  return -1;
163
164 s->dir = dir;
165
166 return 0;
167}
168
169int roar_stream_get_dir (struct roar_stream * s) {
170 if ( s == NULL )
171  return -1;
172
173 return s->dir;
174}
175
176
177int roar_stream_exec    (struct roar_connection * con, struct roar_stream * s) {
178 struct roar_message m;
179
180 m.cmd     = ROAR_CMD_EXEC_STREAM;
181 m.stream  = s->id;
182 m.datalen = 0;
183 m.pos     = 0;
184
185 if ( roar_req(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_connect_to (struct roar_connection * con, struct roar_stream * s, int type, char * host, int port) {
194 struct roar_message m;
195
196 if ( roar_stream_connect_to_ask(con, s, type, host, port) == -1 )
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_connect_to_ask (struct roar_connection * con, struct roar_stream * s, int type, char * host, int port) {
208 struct roar_message m;
209 int len = 0;
210
211 if ( host == NULL )
212  return -1;
213
214 ROAR_DBG("roar_stream_connect_to_ask(*): Ask the server to connect to: %s:%i", host, port);
215
216 m.cmd     = ROAR_CMD_CON_STREAM;
217 m.stream  = s->id;
218 m.pos     = 0;
219
220 m.data[0] = 0;
221 m.data[1] = type;
222 ((uint16_t*)&(m.data))[1] = ROAR_HOST2NET16(port);
223
224 len = strlen(host);
225
226 if ( len > 76 )
227  return -1;
228
229 strncpy(&(m.data[4]), host, len);
230
231 m.datalen = len + 4;
232
233 if ( roar_send_message(con, &m, NULL) == -1 )
234  return -1;
235
236 return 0;
237}
238
239int roar_stream_passfh  (struct roar_connection * con, struct roar_stream * s, int fh) {
240 struct roar_message m;
241 int confh;
242
243 m.cmd     = ROAR_CMD_PASSFH;
244 m.stream  = s->id;
245 m.pos     = 0;
246 m.datalen = 0;
247
248 ROAR_DBG("roar_stream_passfh(con=%p{...}, s={.id=%i,...}, fh=%i) = ?", con, s->id, fh);
249
250 if ( (confh = roar_get_connection_fh(con)) == -1 )
251  return -1;
252
253 if ( roar_send_message(con, &m, NULL) == -1 ) {
254  ROAR_DBG("roar_stream_passfh(con=%p{...}, s={.id=%i,...}, fh=%i) = -1 // can not send message", con, s->id, fh);
255  return -1;
256 }
257
258 ROAR_DBG("roar_stream_passfh(*): msg send");
259
260 if ( roar_socket_send_fh(confh, fh, NULL, 0) == -1 )
261  return -1;
262
263 ROAR_DBG("roar_stream_passfh(*): fh send");
264
265 if ( roar_recv_message(con, &m, NULL) == -1 )
266  return -1;
267
268 ROAR_DBG("roar_stream_passfh(*): mes recved");
269
270 if ( m.cmd == ROAR_CMD_OK )
271  return 0;
272
273 return -1;
274}
275
276int roar_stream_attach_simple (struct roar_connection * con, struct roar_stream * s, int client) {
277 struct roar_message m;
278 uint16_t * info = (uint16_t *) m.data;
279 int i;
280
281 m.cmd     = ROAR_CMD_ATTACH;
282 m.stream  = s->id;
283 m.pos     = 0;
284 m.datalen = 6;
285
286 info[0] = 0;
287 info[1] = ROAR_ATTACH_SIMPLE;
288 info[2] = client;
289
290 for (i = 0; i < m.datalen/2; i++) {
291  info[i] = ROAR_HOST2NET16(info[i]);
292 }
293
294 if ( roar_req(con, &m, NULL) == -1 )
295  return -1;
296
297 if ( m.cmd != ROAR_CMD_OK )
298  return -1;
299
300 return 0;
301}
302
303int roar_stream_add_data (struct roar_connection * con, struct roar_stream * s, char * data, size_t len) {
304 struct roar_message m;
305
306 m.cmd     = ROAR_CMD_ADD_DATA;
307 m.stream  = s->id;
308 m.pos     = 0;
309 m.datalen = len;
310
311// if ( roar_req(con, &m, (void**)&data) == -1 )
312//  return -1;
313 if ( roar_send_message(con, &m, data) != 0 )
314  return -1;
315
316 if ( roar_recv_message(con, &m, NULL) == -1 )
317  return -1;
318
319 if ( m.cmd == ROAR_CMD_OK )
320  return 0;
321 return -1;
322}
323
324int roar_stream_send_data (struct roar_connection * con, struct roar_stream * s, char * data, size_t len) {
325 if ( ! s )
326  return -1;
327
328 if ( s->fh == -1 ) {
329  if ( !con )
330   return -1;
331
332  if ( roar_stream_add_data(con, s, data, len) == -1 )
333   return -1;
334
335  return len;
336 }
337
338#ifdef ROAR_HAVE_IO_POSIX
339 return write(s->fh, data, len);
340#endif
341
342 return -1;
343}
344
345int roar_stream_get_info (struct roar_connection * con, struct roar_stream * s, struct roar_stream_info * info) {
346 struct roar_message m;
347 uint16_t * data = (uint16_t *) m.data;
348 int i;
349
350 m.cmd     = ROAR_CMD_GET_STREAM_PARA;
351 m.stream  = s->id;
352 m.datalen = 4;
353 m.pos     = 0;
354
355 data[0] = 0; // Version and reserved
356 data[1] = ROAR_STREAM_PARA_INFO; // stream
357
358 for (i = 0; i < m.datalen/2; i++) {
359  data[i] = ROAR_HOST2NET16(data[i]);
360 }
361
362 if ( roar_req(con, &m, NULL) == -1 )
363  return -1;
364
365 if ( m.cmd != ROAR_CMD_OK )
366  return -1;
367
368 for (i = 0; i < m.datalen/2; i++) {
369  data[i] = ROAR_NET2HOST16(data[i]);
370 }
371
372 if ( m.datalen < 7*2 )
373  return -1;
374
375 if ( data[0] != 0 || data[1] != 1 )
376  return -1;
377
378 memset(info, 0, sizeof(struct roar_stream_info));
379
380 info->block_size     = data[2];
381 info->pre_underruns  = data[3];
382 info->post_underruns = data[4];
383 info->codec          = data[5];
384 info->flags          = data[6];
385 info->delay          = data[7]*1000;
386
387 return 0;
388}
389
390int roar_stream_get_name (struct roar_connection * con, struct roar_stream * s, char * name, size_t len) {
391 struct roar_message m;
392 uint16_t * data = (uint16_t *) m.data;
393
394 if ( con == NULL || s == NULL || name == NULL || len == 0 )
395  return -1;
396
397 name[0] = 0; // just in case...
398
399 m.cmd     = ROAR_CMD_GET_STREAM_PARA;
400 m.stream  = s->id;
401 m.datalen = 4;
402 m.pos     = 0;
403
404 data[0] = 0; // Version and reserved
405 data[1] = ROAR_STREAM_PARA_NAME; // stream
406
407 data[0] = ROAR_HOST2NET16(data[0]);
408 data[1] = ROAR_HOST2NET16(data[1]);
409
410 ROAR_DBG("roar_stream_get_name(*) = ?");
411
412 if ( roar_req(con, &m, NULL) == -1 )
413  return -1;
414
415 ROAR_DBG("roar_stream_get_name(*) = ?");
416
417 if ( m.cmd != ROAR_CMD_OK )
418  return -1;
419
420 ROAR_DBG("roar_stream_get_name(*) = ?");
421
422 if ( m.datalen < 4 )
423  return -1;
424
425 data[0] = ROAR_NET2HOST16(data[0]);
426 data[1] = ROAR_NET2HOST16(data[1]);
427
428 ROAR_DBG("roar_stream_get_name(*) = ?");
429
430 if ( data[0] != 0 || data[1] != ROAR_STREAM_PARA_NAME )
431  return -1;
432
433 m.datalen -= 4;
434
435 len--;
436
437 if ( len > m.datalen )
438  len = m.datalen;
439
440 strncpy(name, ((char*)m.data)+4, len);
441 name[len] = 0;
442
443 ROAR_DBG("roar_stream_get_name(*) = 0");
444
445 return 0;
446}
447
448int roar_stream_set_flags (struct roar_connection * con, struct roar_stream * s, int flags, int reset) {
449 struct roar_message m;
450 uint16_t * data = (uint16_t *) m.data;
451 int i;
452
453 m.cmd     = ROAR_CMD_SET_STREAM_PARA;
454 m.stream  = s->id;
455 m.datalen = 8;
456 m.pos     = 0;
457
458 data[0] = 0; // Version and reserved
459 data[1] = 2; // flags
460 data[2] = reset == ROAR_RESET_FLAG ? ROAR_RESET_FLAG : ROAR_SET_FLAG;
461 data[3] = flags;
462
463 for (i = 0; i < m.datalen/2; i++) {
464  data[i] = ROAR_HOST2NET16(data[i]);
465 }
466
467 if ( roar_req(con, &m, NULL) == -1 )
468  return -1;
469
470 if ( m.cmd != ROAR_CMD_OK )
471  return -1;
472
473 return 0;
474}
475
476#define _ROAR_STREAM_MESSAGE_LEN ((5+1)*4)
477
478int roar_stream_s2m     (struct roar_stream * s, struct roar_message * m) {
479 uint32_t * data;
480 int i;
481
482 if ( !(s && m) )
483  return -1;
484
485 m->datalen = _ROAR_STREAM_MESSAGE_LEN;
486 data = (uint32_t*) m->data;
487
488 data[0] = s->dir;
489 data[1] = s->pos_rel_id;
490 data[2] = s->info.rate;
491 data[3] = s->info.bits;
492 data[4] = s->info.channels;
493 data[5] = s->info.codec;
494
495 for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++)
496  data[i] = ROAR_HOST2NET32(data[i]);
497
498 ROAR_DBG("roar_stream_s2m(*): s->info:");
499 roar_debug_audio_info_print(&s->info);
500
501 m->pos = s->pos;
502
503 return 0;
504}
505int roar_stream_m2s     (struct roar_stream * s, struct roar_message * m) {
506 uint32_t * data;
507 int i;
508
509 if ( !(s && m) )
510  return -1;
511
512 if ( m->datalen != _ROAR_STREAM_MESSAGE_LEN )
513  return -1;
514
515 s->pos = m->pos;
516
517 data = (uint32_t*) m->data;
518
519 for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++)
520  data[i] = ROAR_NET2HOST32(data[i]);
521
522 s->id            = m->stream;
523 s->dir           = data[0];
524 s->pos_rel_id    = data[1];
525 s->info.rate     = data[2];
526 s->info.bits     = data[3];
527 s->info.channels = data[4];
528 s->info.codec    = data[5];
529
530 ROAR_DBG("roar_stream_m2s(*): s->info:");
531 roar_debug_audio_info_print(&s->info);
532
533 return 0;
534}
535
536// stream direction funcs:
537/*
538#define roar_dir2str(x)   ((x) == ROAR_DIR_PLAY   ? "play"   : (x) == ROAR_DIR_MONITOR ? "monitor" : \
539                           (x) == ROAR_DIR_FILTER ? "filter" : (x) == ROAR_DIR_RECORD  ? "record"  : \
540                           (x) == ROAR_DIR_OUTPUT ? "output" : (x) == ROAR_DIR_BIDIR   ? "bidir"   : \
541                           (x) == ROAR_DIR_MIXING ? "mixing" : \
542                           "unknown")
543*/
544
545struct {
546 int    dir;
547 char * name;
548} _libroar_dir[] = {
549 {ROAR_DIR_PLAY,      "play"     },
550 {ROAR_DIR_RECORD,    "record"   },
551 {ROAR_DIR_MONITOR,   "monitor"  },
552 {ROAR_DIR_FILTER,    "filter"   },
553 {ROAR_DIR_OUTPUT,    "output"   },
554 {ROAR_DIR_MIXING,    "mixing"   },
555 {ROAR_DIR_META,      "meta"     },
556 {ROAR_DIR_BIDIR,     "bidir"    },
557 {ROAR_DIR_THRU,      "thru"     },
558 {ROAR_DIR_BRIDGE,    "bridge"   },
559 {ROAR_DIR_MIDI_IN,   "midi_in"  },
560 {ROAR_DIR_MIDI_OUT,  "midi_out" },
561 {ROAR_DIR_LIGHT_IN,  "light_in" },
562 {ROAR_DIR_LIGHT_OUT, "light_out"},
563 {ROAR_DIR_RAW_IN,    "raw_in"   },
564 {ROAR_DIR_RAW_OUT,   "raw_out"  },
565 {-1,                 "unknown"  }
566};
567
568char * roar_dir2str (int dir) {
569 int i;
570
571 for (i = 0; _libroar_dir[i].dir != -1; i++)
572  if ( _libroar_dir[i].dir == dir )
573   return _libroar_dir[i].name;
574
575 return _libroar_dir[i].name;
576}
577
578int roar_str2dir (char * name) {
579 int i;
580
581 for (i = 0; _libroar_dir[i].dir != -1; i++)
582  if ( !strcmp(_libroar_dir[i].name, name) )
583   return _libroar_dir[i].dir;
584
585 return _libroar_dir[i].dir;
586}
587
588// codec funcs:
589
590/*
591#define roar_codec2str(x) ((x) == ROAR_CODEC_PCM_S_LE  ? "pcm_s_le"  : (x) == ROAR_CODEC_PCM_S_BE  ? "pcm_s_be"  : \
592                           (x) == ROAR_CODEC_PCM_S_PDP ? "pcm_s_pdp" : (x) == ROAR_CODEC_MIDI_FILE ? "midi_file" : \
593                           "unknown" )
594*/
595
596struct {
597 int    codec;
598 char * name;
599} _libroar_codec[] = {
600 // PCM:
601 {ROAR_CODEC_PCM_S_LE,    "pcm_s_le"   },
602 {ROAR_CODEC_PCM_S_BE,    "pcm_s_be"   },
603 {ROAR_CODEC_PCM_S_PDP,   "pcm_s_pdp"  },
604 {ROAR_CODEC_PCM_U_LE,    "pcm_u_le"   },
605 {ROAR_CODEC_PCM_U_BE,    "pcm_u_be"   },
606 {ROAR_CODEC_PCM_U_PDP,   "pcm_u_pdp"  },
607 {ROAR_CODEC_DEFAULT,     "default"    }, // alias
608 {ROAR_CODEC_DEFAULT,     "pcm"        }, // alias
609 {ROAR_CODEC_DEFAULT,     "raw"        }, // alias
610
611 // MIDI:
612 {ROAR_CODEC_MIDI_FILE,   "midi_file"  },
613 {ROAR_CODEC_MIDI,        "midi"       },
614
615 // XIPH:
616 {ROAR_CODEC_OGG_VORBIS,  "ogg_vorbis" },
617 {ROAR_CODEC_OGG_VORBIS,  "vorbis"     }, // alias
618 {ROAR_CODEC_FLAC,        "flac"       },
619 {ROAR_CODEC_OGG_SPEEX,   "ogg_speex"  },
620 {ROAR_CODEC_OGG_SPEEX,   "speex"      }, // alias
621 {ROAR_CODEC_OGG_FLAC,    "ogg_flac"   },
622 {ROAR_CODEC_OGG_GENERAL, "ogg_general"},
623 {ROAR_CODEC_ROAR_CELT,   "roar_celt"  },
624 {ROAR_CODEC_ROAR_SPEEX,  "roar_speex" },
625
626 // RAUM:
627 {ROAR_CODEC_RAUM,        "raum"       },
628 {ROAR_CODEC_RAUM_VORBIS, "raum_vorbis"},
629 {ROAR_CODEC_RAUM_FLAC,   "raum_flac"  },
630
631 // RIFF/WAVE like:
632 {ROAR_CODEC_RIFF_WAVE,   "riff_wave"  },
633 {ROAR_CODEC_RIFF_WAVE,   "wave"       }, // alias
634 {ROAR_CODEC_RIFF_WAVE,   "wav"        }, // alias
635
636 //Log codecs:
637 {ROAR_CODEC_ALAW,        "alaw"       },
638 {ROAR_CODEC_MULAW,       "mulaw"      },
639 {ROAR_CODEC_MULAW,       "ulaw"       }, // alias
640
641 // Meta Codecs:
642 {ROAR_CODEC_META_VCLT,     "meta_vclt"    },
643 {ROAR_CODEC_META_RALT,     "meta_ralt"    },
644 {ROAR_CODEC_META_RALB,     "meta_ralb"    },
645 {ROAR_CODEC_META_RALB_LE,  "meta_ralb_le" },
646 {ROAR_CODEC_META_RALB_BE,  "meta_ralb_be" },
647 {ROAR_CODEC_META_RALB_PDP, "meta_ralb_pdp"},
648
649 // light control:
650 {ROAR_CODEC_DMX512,      "dmx512"     },
651 {ROAR_CODEC_ROARDMX,     "roardmx"    },
652
653 {-1, NULL}
654};
655
656int roar_str2codec(char * codec) {
657 int i;
658 int guess;
659
660 if ( codec == NULL || *codec == 0 )
661  return ROAR_CODEC_DEFAULT;
662
663 if ( (guess = atoi(codec)) > 0 )
664  return guess;
665
666 if ( codec == NULL || *codec == 0 )
667  return ROAR_CODEC_DEFAULT;
668
669 for (i = 0; _libroar_codec[i].codec != -1; i++)
670  if ( strcasecmp(_libroar_codec[i].name, codec) == 0 )
671   return _libroar_codec[i].codec;
672
673 return -1;
674}
675
676
677char * roar_codec2str (int codec) {
678 int i;
679
680 for (i = 0; _libroar_codec[i].codec != -1; i++)
681  if ( _libroar_codec[i].codec == codec )
682   return _libroar_codec[i].name;
683
684 return "unknown";
685}
686
687//ll
Note: See TracBrowser for help on using the repository browser.