source: roaraudio/libroar/stream.c @ 2632:5614f0d236ad

Last change on this file since 2632:5614f0d236ad was 2632:5614f0d236ad, checked in by phi, 15 years ago

set stream stage in case server provides it

File size: 15.2 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 if ( m.datalen < 8*2 ) {
388  info->state         = ROAR_STREAMSTATE_UNKNOWN;
389  return 0;
390 } else {
391  info->state         = data[8];
392 }
393
394 return 0;
395}
396
397int roar_stream_get_name (struct roar_connection * con, struct roar_stream * s, char * name, size_t len) {
398 struct roar_message m;
399 uint16_t * data = (uint16_t *) m.data;
400
401 if ( con == NULL || s == NULL || name == NULL || len == 0 )
402  return -1;
403
404 name[0] = 0; // just in case...
405
406 m.cmd     = ROAR_CMD_GET_STREAM_PARA;
407 m.stream  = s->id;
408 m.datalen = 4;
409 m.pos     = 0;
410
411 data[0] = 0; // Version and reserved
412 data[1] = ROAR_STREAM_PARA_NAME; // stream
413
414 data[0] = ROAR_HOST2NET16(data[0]);
415 data[1] = ROAR_HOST2NET16(data[1]);
416
417 ROAR_DBG("roar_stream_get_name(*) = ?");
418
419 if ( roar_req(con, &m, NULL) == -1 )
420  return -1;
421
422 ROAR_DBG("roar_stream_get_name(*) = ?");
423
424 if ( m.cmd != ROAR_CMD_OK )
425  return -1;
426
427 ROAR_DBG("roar_stream_get_name(*) = ?");
428
429 if ( m.datalen < 4 )
430  return -1;
431
432 data[0] = ROAR_NET2HOST16(data[0]);
433 data[1] = ROAR_NET2HOST16(data[1]);
434
435 ROAR_DBG("roar_stream_get_name(*) = ?");
436
437 if ( data[0] != 0 || data[1] != ROAR_STREAM_PARA_NAME )
438  return -1;
439
440 m.datalen -= 4;
441
442 len--;
443
444 if ( len > m.datalen )
445  len = m.datalen;
446
447 strncpy(name, ((char*)m.data)+4, len);
448 name[len] = 0;
449
450 ROAR_DBG("roar_stream_get_name(*) = 0");
451
452 return 0;
453}
454
455int roar_stream_set_flags (struct roar_connection * con, struct roar_stream * s, int flags, int reset) {
456 struct roar_message m;
457 uint16_t * data = (uint16_t *) m.data;
458 int i;
459
460 m.cmd     = ROAR_CMD_SET_STREAM_PARA;
461 m.stream  = s->id;
462 m.datalen = 8;
463 m.pos     = 0;
464
465 data[0] = 0; // Version and reserved
466 data[1] = 2; // flags
467 data[2] = reset == ROAR_RESET_FLAG ? ROAR_RESET_FLAG : ROAR_SET_FLAG;
468 data[3] = flags;
469
470 for (i = 0; i < m.datalen/2; i++) {
471  data[i] = ROAR_HOST2NET16(data[i]);
472 }
473
474 if ( roar_req(con, &m, NULL) == -1 )
475  return -1;
476
477 if ( m.cmd != ROAR_CMD_OK )
478  return -1;
479
480 return 0;
481}
482
483#define _ROAR_STREAM_MESSAGE_LEN ((5+1)*4)
484
485int roar_stream_s2m     (struct roar_stream * s, struct roar_message * m) {
486 uint32_t * data;
487 int i;
488
489 if ( !(s && m) )
490  return -1;
491
492 m->datalen = _ROAR_STREAM_MESSAGE_LEN;
493 data = (uint32_t*) m->data;
494
495 data[0] = s->dir;
496 data[1] = s->pos_rel_id;
497 data[2] = s->info.rate;
498 data[3] = s->info.bits;
499 data[4] = s->info.channels;
500 data[5] = s->info.codec;
501
502 for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++)
503  data[i] = ROAR_HOST2NET32(data[i]);
504
505 ROAR_DBG("roar_stream_s2m(*): s->info:");
506 roar_debug_audio_info_print(&s->info);
507
508 m->pos = s->pos;
509
510 return 0;
511}
512int roar_stream_m2s     (struct roar_stream * s, struct roar_message * m) {
513 uint32_t * data;
514 int i;
515
516 if ( !(s && m) )
517  return -1;
518
519 if ( m->datalen != _ROAR_STREAM_MESSAGE_LEN )
520  return -1;
521
522 s->pos = m->pos;
523
524 data = (uint32_t*) m->data;
525
526 for (i = 0; i < _ROAR_STREAM_MESSAGE_LEN/4; i++)
527  data[i] = ROAR_NET2HOST32(data[i]);
528
529 s->id            = m->stream;
530 s->dir           = data[0];
531 s->pos_rel_id    = data[1];
532 s->info.rate     = data[2];
533 s->info.bits     = data[3];
534 s->info.channels = data[4];
535 s->info.codec    = data[5];
536
537 ROAR_DBG("roar_stream_m2s(*): s->info:");
538 roar_debug_audio_info_print(&s->info);
539
540 return 0;
541}
542
543// stream direction funcs:
544/*
545#define roar_dir2str(x)   ((x) == ROAR_DIR_PLAY   ? "play"   : (x) == ROAR_DIR_MONITOR ? "monitor" : \
546                           (x) == ROAR_DIR_FILTER ? "filter" : (x) == ROAR_DIR_RECORD  ? "record"  : \
547                           (x) == ROAR_DIR_OUTPUT ? "output" : (x) == ROAR_DIR_BIDIR   ? "bidir"   : \
548                           (x) == ROAR_DIR_MIXING ? "mixing" : \
549                           "unknown")
550*/
551
552struct {
553 int    dir;
554 char * name;
555} _libroar_dir[] = {
556 {ROAR_DIR_PLAY,      "play"     },
557 {ROAR_DIR_RECORD,    "record"   },
558 {ROAR_DIR_MONITOR,   "monitor"  },
559 {ROAR_DIR_FILTER,    "filter"   },
560 {ROAR_DIR_OUTPUT,    "output"   },
561 {ROAR_DIR_MIXING,    "mixing"   },
562 {ROAR_DIR_META,      "meta"     },
563 {ROAR_DIR_BIDIR,     "bidir"    },
564 {ROAR_DIR_THRU,      "thru"     },
565 {ROAR_DIR_BRIDGE,    "bridge"   },
566 {ROAR_DIR_MIDI_IN,   "midi_in"  },
567 {ROAR_DIR_MIDI_OUT,  "midi_out" },
568 {ROAR_DIR_LIGHT_IN,  "light_in" },
569 {ROAR_DIR_LIGHT_OUT, "light_out"},
570 {ROAR_DIR_RAW_IN,    "raw_in"   },
571 {ROAR_DIR_RAW_OUT,   "raw_out"  },
572 {-1,                 "unknown"  }
573};
574
575char * roar_dir2str (int dir) {
576 int i;
577
578 for (i = 0; _libroar_dir[i].dir != -1; i++)
579  if ( _libroar_dir[i].dir == dir )
580   return _libroar_dir[i].name;
581
582 return _libroar_dir[i].name;
583}
584
585int roar_str2dir (char * name) {
586 int i;
587
588 for (i = 0; _libroar_dir[i].dir != -1; i++)
589  if ( !strcmp(_libroar_dir[i].name, name) )
590   return _libroar_dir[i].dir;
591
592 return _libroar_dir[i].dir;
593}
594
595// codec funcs:
596
597/*
598#define roar_codec2str(x) ((x) == ROAR_CODEC_PCM_S_LE  ? "pcm_s_le"  : (x) == ROAR_CODEC_PCM_S_BE  ? "pcm_s_be"  : \
599                           (x) == ROAR_CODEC_PCM_S_PDP ? "pcm_s_pdp" : (x) == ROAR_CODEC_MIDI_FILE ? "midi_file" : \
600                           "unknown" )
601*/
602
603struct {
604 int    codec;
605 char * name;
606} _libroar_codec[] = {
607 // PCM:
608 {ROAR_CODEC_PCM_S_LE,    "pcm_s_le"   },
609 {ROAR_CODEC_PCM_S_BE,    "pcm_s_be"   },
610 {ROAR_CODEC_PCM_S_PDP,   "pcm_s_pdp"  },
611 {ROAR_CODEC_PCM_U_LE,    "pcm_u_le"   },
612 {ROAR_CODEC_PCM_U_BE,    "pcm_u_be"   },
613 {ROAR_CODEC_PCM_U_PDP,   "pcm_u_pdp"  },
614 {ROAR_CODEC_DEFAULT,     "default"    }, // alias
615 {ROAR_CODEC_DEFAULT,     "pcm"        }, // alias
616 {ROAR_CODEC_DEFAULT,     "raw"        }, // alias
617
618 // MIDI:
619 {ROAR_CODEC_MIDI_FILE,   "midi_file"  },
620 {ROAR_CODEC_MIDI,        "midi"       },
621
622 // XIPH:
623 {ROAR_CODEC_OGG_VORBIS,  "ogg_vorbis" },
624 {ROAR_CODEC_OGG_VORBIS,  "vorbis"     }, // alias
625 {ROAR_CODEC_FLAC,        "flac"       },
626 {ROAR_CODEC_OGG_SPEEX,   "ogg_speex"  },
627 {ROAR_CODEC_OGG_SPEEX,   "speex"      }, // alias
628 {ROAR_CODEC_OGG_FLAC,    "ogg_flac"   },
629 {ROAR_CODEC_OGG_GENERAL, "ogg_general"},
630 {ROAR_CODEC_ROAR_CELT,   "roar_celt"  },
631 {ROAR_CODEC_ROAR_SPEEX,  "roar_speex" },
632
633 // RAUM:
634 {ROAR_CODEC_RAUM,        "raum"       },
635 {ROAR_CODEC_RAUM_VORBIS, "raum_vorbis"},
636 {ROAR_CODEC_RAUM_FLAC,   "raum_flac"  },
637
638 // RIFF/WAVE like:
639 {ROAR_CODEC_RIFF_WAVE,   "riff_wave"  },
640 {ROAR_CODEC_RIFF_WAVE,   "wave"       }, // alias
641 {ROAR_CODEC_RIFF_WAVE,   "wav"        }, // alias
642
643 //Log codecs:
644 {ROAR_CODEC_ALAW,        "alaw"       },
645 {ROAR_CODEC_MULAW,       "mulaw"      },
646 {ROAR_CODEC_MULAW,       "ulaw"       }, // alias
647
648 // Meta Codecs:
649 {ROAR_CODEC_META_VCLT,     "meta_vclt"    },
650 {ROAR_CODEC_META_RALT,     "meta_ralt"    },
651 {ROAR_CODEC_META_RALB,     "meta_ralb"    },
652 {ROAR_CODEC_META_RALB_LE,  "meta_ralb_le" },
653 {ROAR_CODEC_META_RALB_BE,  "meta_ralb_be" },
654 {ROAR_CODEC_META_RALB_PDP, "meta_ralb_pdp"},
655
656 // light control:
657 {ROAR_CODEC_DMX512,      "dmx512"     },
658 {ROAR_CODEC_ROARDMX,     "roardmx"    },
659
660 {-1, NULL}
661};
662
663int roar_str2codec(char * codec) {
664 int i;
665 int guess;
666
667 if ( codec == NULL || *codec == 0 )
668  return ROAR_CODEC_DEFAULT;
669
670 if ( (guess = atoi(codec)) > 0 )
671  return guess;
672
673 if ( codec == NULL || *codec == 0 )
674  return ROAR_CODEC_DEFAULT;
675
676 for (i = 0; _libroar_codec[i].codec != -1; i++)
677  if ( strcasecmp(_libroar_codec[i].name, codec) == 0 )
678   return _libroar_codec[i].codec;
679
680 return -1;
681}
682
683
684char * roar_codec2str (int codec) {
685 int i;
686
687 for (i = 0; _libroar_codec[i].codec != -1; i++)
688  if ( _libroar_codec[i].codec == codec )
689   return _libroar_codec[i].name;
690
691 return "unknown";
692}
693
694//ll
Note: See TracBrowser for help on using the repository browser.