[800] | 1 | //roarradio.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
[800] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[800] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include <roaraudio.h> |
---|
| 27 | |
---|
[2114] | 28 | #define P_UNKNOWN 0 |
---|
| 29 | #define P_HTTP 1 |
---|
| 30 | #define P_GOPHER 2 |
---|
| 31 | |
---|
[800] | 32 | void usage (void) { |
---|
| 33 | printf("roarradio [OPTIONS]... [FILE|URL]\n"); |
---|
| 34 | |
---|
| 35 | printf("\nOptions:\n\n"); |
---|
| 36 | |
---|
| 37 | printf(" --server SERVER - Set server hostname\n" |
---|
| 38 | " --rate RATE - Set sample rate\n" |
---|
| 39 | " --bits BITS - Set bits per sample\n" |
---|
| 40 | " --chans CHANNELS - Set number of channels\n" |
---|
| 41 | " --codec CODEC - Set the codec\n" |
---|
| 42 | " --help - Show this help\n" |
---|
| 43 | ); |
---|
| 44 | |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | int main (int argc, char * argv[]) { |
---|
| 48 | int rate = 44100; |
---|
| 49 | int bits = 16; |
---|
| 50 | int channels = 2; |
---|
[801] | 51 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
[800] | 52 | char * server = NULL; |
---|
| 53 | char * k; |
---|
| 54 | int i; |
---|
| 55 | int in = -1; |
---|
| 56 | char * file = NULL; |
---|
| 57 | char * host; |
---|
[2114] | 58 | int port; |
---|
[800] | 59 | FILE * http; |
---|
| 60 | struct roar_connection con[1]; |
---|
| 61 | struct roar_stream stream[1]; |
---|
| 62 | char buf0[80], buf1[80]; |
---|
[2114] | 63 | int proto = P_UNKNOWN; |
---|
[800] | 64 | |
---|
| 65 | for (i = 1; i < argc; i++) { |
---|
| 66 | k = argv[i]; |
---|
| 67 | |
---|
| 68 | if ( strcmp(k, "--server") == 0 ) { |
---|
| 69 | server = argv[++i]; |
---|
| 70 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
| 71 | rate = atoi(argv[++i]); |
---|
| 72 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
| 73 | bits = atoi(argv[++i]); |
---|
| 74 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 75 | channels = atoi(argv[++i]); |
---|
| 76 | } else if ( strcmp(k, "--codec") == 0 ) { |
---|
| 77 | codec = roar_str2codec(argv[++i]); |
---|
| 78 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
| 79 | usage(); |
---|
| 80 | return 0; |
---|
| 81 | } else if ( file == NULL ) { |
---|
| 82 | file = argv[i]; |
---|
| 83 | } else { |
---|
| 84 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 85 | usage(); |
---|
| 86 | return 1; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if ( file == NULL ) { |
---|
| 91 | file = "/dev/stdin"; |
---|
| 92 | in = ROAR_STDIN; |
---|
| 93 | } else { |
---|
[2114] | 94 | if ( strncmp(file, "http://", 7) == 0 ) { |
---|
| 95 | proto = P_HTTP; |
---|
| 96 | host = file+7; |
---|
| 97 | port = 80; |
---|
| 98 | } else if ( strncmp(file, "gopher://", 9) == 0 ) { |
---|
| 99 | proto = P_GOPHER; |
---|
| 100 | host = file+9; |
---|
| 101 | port = 70; |
---|
[2222] | 102 | } else { |
---|
| 103 | fprintf(stderr, "Error: unknown protocol: %s\n", file); |
---|
| 104 | return 20; |
---|
[2114] | 105 | } |
---|
[800] | 106 | |
---|
[2114] | 107 | if ( proto == P_HTTP || proto == P_GOPHER ) { |
---|
[800] | 108 | for (i = 0; host[i] != 0; i++) { |
---|
| 109 | if ( host[i] == ':' ) { |
---|
| 110 | port = atoi(host+i+1); // atoi() ignores the rest after '/' |
---|
| 111 | host[i] = 0; |
---|
| 112 | } else if ( host[i] == '/' ) { |
---|
| 113 | file = host+i; |
---|
| 114 | break; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[1511] | 118 | if ( !*file ) { |
---|
| 119 | file = "/"; |
---|
| 120 | |
---|
| 121 | if ( (in = roar_socket_connect(host, port)) == -1 ) { |
---|
| 122 | ROAR_ERR("can not connect to remote server %s (port %i): %s", host, port, strerror(errno)); |
---|
| 123 | return 0; |
---|
| 124 | } |
---|
| 125 | } else { |
---|
| 126 | *file = 0; |
---|
| 127 | |
---|
| 128 | if ( (in = roar_socket_connect(host, port)) == -1 ) { |
---|
| 129 | ROAR_ERR("can not connect to remote server %s (port %i): %s", host, port, strerror(errno)); |
---|
| 130 | return 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | *file = '/'; |
---|
[800] | 134 | } |
---|
| 135 | |
---|
[2114] | 136 | switch (proto) { |
---|
| 137 | case P_HTTP: |
---|
| 138 | if ( (http = fdopen(in, "r+")) == NULL ) { |
---|
| 139 | ROAR_ERR("can not create FILE* object: %s", strerror(errno)); |
---|
| 140 | return 0; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | fprintf(http, "GET %s HTTP/1.1\r\n", file); |
---|
| 144 | fprintf(http, "Host: %s\r\n", host); |
---|
| 145 | fprintf(http, "User-Agent: roarradio $Revision$\r\n"); |
---|
| 146 | fprintf(http, "Connection: close\r\n"); |
---|
| 147 | fprintf(http, "\r\n"); |
---|
| 148 | fflush(http); |
---|
| 149 | |
---|
| 150 | if ( fscanf(http, "%79s %i %79s\n", buf0, &port, buf1) != 3 ) { |
---|
[4012] | 151 | ROAR_ERR("HTTP protocol error!, no initial HTTP/1.x-line!"); |
---|
[2114] | 152 | return 1; |
---|
| 153 | } |
---|
| 154 | if ( port != 200 ) { // 200 = HTTP OK |
---|
| 155 | ROAR_ERR("HTTP Error: %i - %s", port, buf1); |
---|
| 156 | return 1; |
---|
| 157 | } |
---|
[800] | 158 | |
---|
[2114] | 159 | *buf0 = 0; |
---|
| 160 | |
---|
| 161 | while (*buf0 != '\r' && *buf0 != '\n') { |
---|
| 162 | fgets(buf0, 80, http); |
---|
| 163 | } |
---|
[800] | 164 | |
---|
[2114] | 165 | fflush(http); |
---|
| 166 | break; |
---|
| 167 | case P_GOPHER: |
---|
| 168 | if ( file[0] == 0 ) { |
---|
| 169 | file[0] = '/'; |
---|
| 170 | file[1] = 0; |
---|
| 171 | } else if ( file[0] == '/' && file[1] != 0 && file[2] == '/' ) { // strip the type prefix |
---|
| 172 | file += 2; |
---|
| 173 | } |
---|
| 174 | // TODO: do some error checks here |
---|
| 175 | write(in, file, strlen(file)); |
---|
| 176 | write(in, "\r\n", 2); |
---|
| 177 | ROAR_SHUTDOWN(in, SHUT_WR); |
---|
| 178 | break; |
---|
| 179 | } |
---|
| 180 | } else { |
---|
| 181 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
| 182 | ROAR_ERR("can not open file: %s: %s", file, strerror(errno)); |
---|
[800] | 183 | return 1; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | if ( in == -1 ) { |
---|
| 189 | ROAR_ERR("No open file, bad. This should not happen"); |
---|
| 190 | return 1; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | if ( roar_simple_connect(con, server, "roarradio") == -1 ) { |
---|
| 194 | ROAR_ERR("Can not connect to server"); |
---|
[2225] | 195 | return 1; |
---|
[800] | 196 | } |
---|
| 197 | |
---|
| 198 | if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) { |
---|
| 199 | roar_disconnect(con); |
---|
[2225] | 200 | return 1; |
---|
[800] | 201 | } |
---|
| 202 | |
---|
| 203 | if ( roar_stream_connect(con, stream, ROAR_DIR_PLAY) == -1 ) { |
---|
| 204 | roar_disconnect(con); |
---|
[2225] | 205 | return 1; |
---|
[800] | 206 | } |
---|
| 207 | |
---|
| 208 | if ( roar_stream_passfh(con, stream, in) == -1 ) { |
---|
| 209 | roar_disconnect(con); |
---|
[2225] | 210 | return 1; |
---|
[800] | 211 | } |
---|
| 212 | |
---|
| 213 | close(in); |
---|
| 214 | |
---|
| 215 | if ( roar_stream_attach_simple(con, stream, 0) == -1 ) { |
---|
| 216 | ROAR_ERR("Can not attach stream to server"); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | roar_disconnect(con); |
---|
| 220 | |
---|
| 221 | return 0; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | //ll |
---|