[800] | 1 | //roarradio.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 | void usage (void) { |
---|
| 28 | printf("roarradio [OPTIONS]... [FILE|URL]\n"); |
---|
| 29 | |
---|
| 30 | printf("\nOptions:\n\n"); |
---|
| 31 | |
---|
| 32 | printf(" --server SERVER - Set server hostname\n" |
---|
| 33 | " --rate RATE - Set sample rate\n" |
---|
| 34 | " --bits BITS - Set bits per sample\n" |
---|
| 35 | " --chans CHANNELS - Set number of channels\n" |
---|
| 36 | " --codec CODEC - Set the codec\n" |
---|
| 37 | " --help - Show this help\n" |
---|
| 38 | ); |
---|
| 39 | |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | int main (int argc, char * argv[]) { |
---|
| 43 | int rate = 44100; |
---|
| 44 | int bits = 16; |
---|
| 45 | int channels = 2; |
---|
[801] | 46 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
[800] | 47 | char * server = NULL; |
---|
| 48 | char * k; |
---|
| 49 | int i; |
---|
| 50 | int in = -1; |
---|
| 51 | char * file = NULL; |
---|
| 52 | char * host; |
---|
| 53 | int port = 80; |
---|
| 54 | FILE * http; |
---|
| 55 | struct roar_connection con[1]; |
---|
| 56 | struct roar_stream stream[1]; |
---|
| 57 | char buf0[80], buf1[80]; |
---|
| 58 | |
---|
| 59 | for (i = 1; i < argc; i++) { |
---|
| 60 | k = argv[i]; |
---|
| 61 | |
---|
| 62 | if ( strcmp(k, "--server") == 0 ) { |
---|
| 63 | server = argv[++i]; |
---|
| 64 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
| 65 | rate = atoi(argv[++i]); |
---|
| 66 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
| 67 | bits = atoi(argv[++i]); |
---|
| 68 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 69 | channels = atoi(argv[++i]); |
---|
| 70 | } else if ( strcmp(k, "--codec") == 0 ) { |
---|
| 71 | codec = roar_str2codec(argv[++i]); |
---|
| 72 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
| 73 | usage(); |
---|
| 74 | return 0; |
---|
| 75 | } else if ( file == NULL ) { |
---|
| 76 | file = argv[i]; |
---|
| 77 | } else { |
---|
| 78 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 79 | usage(); |
---|
| 80 | return 1; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if ( file == NULL ) { |
---|
| 85 | file = "/dev/stdin"; |
---|
| 86 | in = ROAR_STDIN; |
---|
| 87 | } else { |
---|
| 88 | if ( strncmp(file, "http://", 7) != 0 ) { |
---|
| 89 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
| 90 | ROAR_ERR("can not open file: %s: %s", file, strerror(errno)); |
---|
| 91 | return 1; |
---|
| 92 | } |
---|
| 93 | } else { |
---|
| 94 | host = file+7; |
---|
| 95 | |
---|
| 96 | for (i = 0; host[i] != 0; i++) { |
---|
| 97 | if ( host[i] == ':' ) { |
---|
| 98 | port = atoi(host+i+1); // atoi() ignores the rest after '/' |
---|
| 99 | host[i] = 0; |
---|
| 100 | } else if ( host[i] == '/' ) { |
---|
| 101 | file = host+i; |
---|
| 102 | break; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
[1511] | 106 | if ( !*file ) { |
---|
| 107 | file = "/"; |
---|
| 108 | |
---|
| 109 | if ( (in = roar_socket_connect(host, port)) == -1 ) { |
---|
| 110 | ROAR_ERR("can not connect to remote server %s (port %i): %s", host, port, strerror(errno)); |
---|
| 111 | return 0; |
---|
| 112 | } |
---|
| 113 | } else { |
---|
| 114 | *file = 0; |
---|
| 115 | |
---|
| 116 | if ( (in = roar_socket_connect(host, port)) == -1 ) { |
---|
| 117 | ROAR_ERR("can not connect to remote server %s (port %i): %s", host, port, strerror(errno)); |
---|
| 118 | return 0; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | *file = '/'; |
---|
[800] | 122 | } |
---|
| 123 | |
---|
| 124 | if ( (http = fdopen(in, "r+")) == NULL ) { |
---|
| 125 | ROAR_ERR("can not create FILE* object: %s", strerror(errno)); |
---|
| 126 | return 0; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | fprintf(http, "GET %s HTTP/1.1\r\n", file); |
---|
| 130 | fprintf(http, "Host: %s\r\n", host); |
---|
| 131 | fprintf(http, "User-Agent: roarradio $Revision$\r\n"); |
---|
| 132 | fprintf(http, "Connection: close\r\n"); |
---|
| 133 | fprintf(http, "\r\n"); |
---|
| 134 | fflush(http); |
---|
| 135 | |
---|
| 136 | if ( fscanf(http, "%79s %i %79s\n", buf0, &port, buf1) != 3 ) { |
---|
| 137 | ROAR_ERR("HTTP protocoll error!, no initial HTTP/1.x-line!"); |
---|
| 138 | return 1; |
---|
| 139 | } |
---|
| 140 | if ( port != 200 ) { // 200 = HTTP OK |
---|
| 141 | ROAR_ERR("HTTP Error: %i - %s", port, buf1); |
---|
| 142 | return 1; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | *buf0 = 0; |
---|
| 146 | |
---|
| 147 | while (*buf0 != '\r' && *buf0 != '\n') { |
---|
| 148 | fgets(buf0, 80, http); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | fflush(http); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | if ( in == -1 ) { |
---|
| 156 | ROAR_ERR("No open file, bad. This should not happen"); |
---|
| 157 | return 1; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | if ( roar_simple_connect(con, server, "roarradio") == -1 ) { |
---|
| 161 | ROAR_ERR("Can not connect to server"); |
---|
| 162 | return 0; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) { |
---|
| 166 | roar_disconnect(con); |
---|
| 167 | return -1; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | if ( roar_stream_connect(con, stream, ROAR_DIR_PLAY) == -1 ) { |
---|
| 171 | roar_disconnect(con); |
---|
| 172 | return -1; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | if ( roar_stream_passfh(con, stream, in) == -1 ) { |
---|
| 176 | roar_disconnect(con); |
---|
| 177 | return -1; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | close(in); |
---|
| 181 | |
---|
| 182 | if ( roar_stream_attach_simple(con, stream, 0) == -1 ) { |
---|
| 183 | ROAR_ERR("Can not attach stream to server"); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | roar_disconnect(con); |
---|
| 187 | |
---|
| 188 | return 0; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | //ll |
---|