[826] | 1 | //roarbidir.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of roarclients 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 | * RoarAudio 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 | */ |
---|
| 24 | |
---|
| 25 | #include <roaraudio.h> |
---|
| 26 | |
---|
| 27 | #define BUFSIZE 1024 |
---|
| 28 | |
---|
| 29 | void usage (void) { |
---|
| 30 | printf("roarcat [OPTIONS]... [FILE]\n"); |
---|
| 31 | |
---|
| 32 | printf("\nOptions:\n\n"); |
---|
| 33 | |
---|
| 34 | printf(" --server SERVER - Set server hostname\n" |
---|
| 35 | " --rate RATE - Set sample rate\n" |
---|
| 36 | " --bits BITS - Set bits per sample\n" |
---|
| 37 | " --chans CHANNELS - Set number of channels\n" |
---|
| 38 | " --codec CODEC - Set the codec\n" |
---|
| 39 | " --help - Show this help\n" |
---|
| 40 | ); |
---|
| 41 | |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | int main (int argc, char * argv[]) { |
---|
| 45 | int rate = 44100; |
---|
| 46 | int bits = 16; |
---|
| 47 | int channels = 2; |
---|
| 48 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 49 | char * server = NULL; |
---|
| 50 | char * k; |
---|
| 51 | int fh; |
---|
| 52 | int i; |
---|
| 53 | int in = -1; |
---|
| 54 | int out = -1; |
---|
| 55 | char buf[BUFSIZE]; |
---|
| 56 | fd_set sl; |
---|
| 57 | struct timeval tv; |
---|
| 58 | int max_fh; |
---|
| 59 | |
---|
| 60 | for (i = 1; i < argc; i++) { |
---|
| 61 | k = argv[i]; |
---|
| 62 | |
---|
| 63 | if ( strcmp(k, "--server") == 0 ) { |
---|
| 64 | server = argv[++i]; |
---|
| 65 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
| 66 | rate = atoi(argv[++i]); |
---|
| 67 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
| 68 | bits = atoi(argv[++i]); |
---|
| 69 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 70 | channels = atoi(argv[++i]); |
---|
| 71 | } else if ( strcmp(k, "--codec") == 0 ) { |
---|
| 72 | codec = roar_str2codec(argv[++i]); |
---|
| 73 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
| 74 | usage(); |
---|
| 75 | return 0; |
---|
| 76 | } else if ( in == -1 ) { |
---|
| 77 | if ( (in = open(k, O_RDONLY, 0644)) == -1 ) { |
---|
| 78 | fprintf(stderr, "Error: can not open file: %s: %s\n", k, strerror(errno)); |
---|
| 79 | return 1; |
---|
| 80 | } |
---|
| 81 | } else { |
---|
| 82 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 83 | usage(); |
---|
| 84 | return 1; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_BIDIR, "roarbidir")) == -1 ) { |
---|
| 89 | fprintf(stderr, "Error: can not start playback\n"); |
---|
| 90 | return 1; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | if ( in == -1 ) |
---|
| 94 | in = ROAR_STDIN; |
---|
| 95 | if ( out == -1 ) |
---|
| 96 | out = ROAR_STDOUT; |
---|
| 97 | |
---|
| 98 | max_fh = (in > fh ? in : fh) + 1; |
---|
| 99 | i = 1; |
---|
| 100 | |
---|
| 101 | while (i > 0) { |
---|
| 102 | FD_ZERO(&sl); |
---|
| 103 | FD_SET(in, &sl); |
---|
| 104 | FD_SET(fh, &sl); |
---|
| 105 | |
---|
| 106 | tv.tv_sec = 0; |
---|
| 107 | tv.tv_usec = 50000; |
---|
| 108 | |
---|
| 109 | if (select(max_fh, &sl, NULL, NULL, &tv) > 0) { |
---|
| 110 | if ( FD_ISSET(fh, &sl) ) { |
---|
| 111 | if ( (i = read(fh, buf, BUFSIZE)) == -1 ) |
---|
| 112 | return -1; |
---|
[1112] | 113 | if ( write(out, buf, i) != i ) |
---|
[826] | 114 | return -1; |
---|
[1112] | 115 | } |
---|
| 116 | if ( FD_ISSET(in, &sl) ) { |
---|
[826] | 117 | if ( (i = read(in, buf, BUFSIZE)) == -1 ) |
---|
| 118 | return -1; |
---|
| 119 | if ( write(fh, buf, i) != i ) |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | roar_simple_close(fh); |
---|
| 126 | |
---|
| 127 | close(in); |
---|
| 128 | |
---|
| 129 | return 0; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | //ll |
---|