source: roaraudio/roarclients/roarbidir.c @ 4997:0a1398101281

Last change on this file since 4997:0a1398101281 was 4997:0a1398101281, checked in by phi, 13 years ago

fix small theoretical resource leak

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