[111] | 1 | //roarvorbis.c: |
---|
| 2 | |
---|
[669] | 3 | /* |
---|
[3790] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
[669] | 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. |
---|
[669] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[111] | 26 | #include <roaraudio.h> |
---|
| 27 | |
---|
| 28 | #include <stdio.h> |
---|
| 29 | #include <stdlib.h> |
---|
| 30 | #include <math.h> |
---|
[478] | 31 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
[111] | 32 | #include <vorbis/codec.h> |
---|
| 33 | #include <vorbis/vorbisfile.h> |
---|
[478] | 34 | #endif |
---|
[111] | 35 | |
---|
| 36 | #ifdef _WIN32 |
---|
| 37 | #include <io.h> |
---|
| 38 | #include <fcntl.h> |
---|
| 39 | #endif |
---|
| 40 | |
---|
| 41 | #define BUFSIZE 1024 |
---|
| 42 | |
---|
| 43 | void usage (void) { |
---|
| 44 | printf("roarvorbis [OPTIONS]... FILE\n"); |
---|
| 45 | |
---|
| 46 | printf("\nOptions:\n\n"); |
---|
| 47 | |
---|
| 48 | printf(" --server SERVER - Set server hostname\n" |
---|
| 49 | " --help - Show this help\n" |
---|
[3043] | 50 | " --vclt-out FILE - Writes VCLT file\n" |
---|
[111] | 51 | ); |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | |
---|
[124] | 55 | |
---|
[478] | 56 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
[124] | 57 | FILE * open_http (char * file) { |
---|
[861] | 58 | #ifdef ROAR_HAVE_BIN_WGET |
---|
[124] | 59 | char cmd[1024]; |
---|
| 60 | |
---|
[861] | 61 | snprintf(cmd, 1023, ROAR_HAVE_BIN_WGET " -qO - '%s'", file); |
---|
[124] | 62 | |
---|
| 63 | return popen(cmd, "r"); |
---|
[861] | 64 | #else |
---|
| 65 | return NULL; |
---|
| 66 | #endif |
---|
[124] | 67 | } |
---|
| 68 | |
---|
[3043] | 69 | int update_stream (struct roar_connection * con, struct roar_stream * s, int * out, OggVorbis_File * vf, char * file, struct roar_audio_info * info, struct roar_vio_calls * vclt) { |
---|
[121] | 70 | vorbis_info *vi = ov_info(vf, -1); |
---|
[111] | 71 | int bits = 16; |
---|
| 72 | int codec = ROAR_CODEC_DEFAULT; |
---|
[121] | 73 | char **ptr = ov_comment(vf, -1)->user_comments; |
---|
[718] | 74 | char key[ROAR_META_MAX_NAMELEN], value[LIBROAR_BUFFER_MSGDATA] = {0}; |
---|
[121] | 75 | int j, h = 0; |
---|
| 76 | struct roar_meta meta; |
---|
[122] | 77 | int need_new_stream = 0; |
---|
[715] | 78 | int meta_ok; |
---|
[121] | 79 | |
---|
| 80 | fprintf(stderr, "\n"); |
---|
| 81 | |
---|
[3045] | 82 | if ( vclt != NULL ) { |
---|
| 83 | roar_vio_printf(vclt, "AUDIOINFO=rate:%iHz, channels:%i\n", vi->rate, vi->channels); |
---|
| 84 | } |
---|
| 85 | |
---|
[122] | 86 | if ( *out == -1 ) { |
---|
| 87 | need_new_stream = 1; |
---|
[3641] | 88 | } else if ( info->rate != (uint16_t)vi->rate || info->channels != (uint16_t)vi->channels ) { |
---|
[122] | 89 | need_new_stream = 1; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | if ( need_new_stream ) { |
---|
| 93 | if ( *out != -1 ) |
---|
[121] | 94 | close(*out); |
---|
| 95 | |
---|
[122] | 96 | fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate); |
---|
| 97 | |
---|
| 98 | info->rate = vi->rate; |
---|
| 99 | info->channels = vi->channels; |
---|
[121] | 100 | |
---|
[122] | 101 | if ( (*out = roar_simple_new_stream_obj(con, s, vi->rate, vi->channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 102 | roar_disconnect(con); |
---|
| 103 | return -1; |
---|
| 104 | } |
---|
[121] | 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | meta.value = value; |
---|
| 109 | meta.key[0] = 0; |
---|
[132] | 110 | meta.type = ROAR_META_TYPE_NONE; |
---|
[121] | 111 | |
---|
| 112 | roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta); |
---|
| 113 | |
---|
[124] | 114 | if ( strncmp(file, "http:", 5) == 0 ) |
---|
| 115 | meta.type = ROAR_META_TYPE_FILEURL; |
---|
| 116 | else |
---|
| 117 | meta.type = ROAR_META_TYPE_FILENAME; |
---|
| 118 | |
---|
[715] | 119 | |
---|
[719] | 120 | strncpy(value, file, LIBROAR_BUFFER_MSGDATA-1); |
---|
| 121 | value[LIBROAR_BUFFER_MSGDATA-1] = 0; |
---|
[121] | 122 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
| 123 | |
---|
| 124 | while(*ptr){ |
---|
[715] | 125 | meta_ok = 1; |
---|
| 126 | |
---|
| 127 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
| 128 | if ( j == ROAR_META_MAX_NAMELEN ) { |
---|
| 129 | ROAR_ERR("update_stream(*): invalid meta data: meta data key too long"); |
---|
| 130 | meta_ok = 0; |
---|
| 131 | j = 0; |
---|
| 132 | break; |
---|
| 133 | } |
---|
[121] | 134 | key[j] = (*ptr)[j]; |
---|
[715] | 135 | } |
---|
| 136 | key[j] = 0; |
---|
[121] | 137 | |
---|
[715] | 138 | if ( meta_ok ) { |
---|
| 139 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
[718] | 140 | if ( h == LIBROAR_BUFFER_MSGDATA ) { |
---|
[715] | 141 | ROAR_ERR("update_stream(*): invalid meta data: meta data value for key '%s' too long", key); |
---|
| 142 | meta_ok = 0; |
---|
| 143 | h = 0; |
---|
| 144 | break; |
---|
| 145 | } |
---|
| 146 | value[h++] = (*ptr)[j]; |
---|
| 147 | } |
---|
[121] | 148 | value[h] = 0; |
---|
[715] | 149 | } |
---|
[121] | 150 | |
---|
[715] | 151 | if ( meta_ok ) { |
---|
| 152 | fprintf(stderr, "Meta %-16s: %s\n", key, value); |
---|
[121] | 153 | |
---|
[3043] | 154 | if ( vclt != NULL ) { |
---|
| 155 | roar_vio_printf(vclt, "%s=%s\n", key, value); |
---|
| 156 | } |
---|
| 157 | |
---|
[715] | 158 | meta.type = roar_meta_inttype(key); |
---|
| 159 | if ( meta.type != -1 ) |
---|
| 160 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | ptr++; |
---|
[121] | 164 | } |
---|
| 165 | |
---|
[1238] | 166 | *value = 0; |
---|
| 167 | meta.key[0] = 0; |
---|
| 168 | meta.type = ROAR_META_TYPE_NONE; |
---|
| 169 | roar_stream_meta_set(con, s, ROAR_META_MODE_FINALIZE, &meta); |
---|
| 170 | |
---|
[3043] | 171 | if ( vclt != NULL ) { |
---|
| 172 | roar_vio_printf(vclt, "==\n"); |
---|
| 173 | } |
---|
| 174 | |
---|
[121] | 175 | return 0; |
---|
| 176 | } |
---|
| 177 | |
---|
[478] | 178 | #endif |
---|
| 179 | |
---|
[121] | 180 | int main (int argc, char * argv[]) { |
---|
[478] | 181 | #ifndef ROAR_HAVE_LIBVORBISFILE |
---|
[3787] | 182 | (void)argc, (void)argv; |
---|
[479] | 183 | fprintf(stderr, "Error: no Vorbis support!\n"); |
---|
[478] | 184 | return 1; |
---|
| 185 | #else |
---|
[3787] | 186 | struct roar_vio_calls vclt; |
---|
| 187 | struct roar_vio_defaults def; |
---|
[111] | 188 | char * server = NULL; |
---|
| 189 | char * file = NULL; |
---|
[3043] | 190 | char * vcltfile = NULL; |
---|
[111] | 191 | char * k; |
---|
| 192 | int i; |
---|
| 193 | FILE * in; |
---|
[120] | 194 | int out = -1; |
---|
[111] | 195 | struct roar_connection con; |
---|
| 196 | struct roar_stream s; |
---|
| 197 | OggVorbis_File vf; |
---|
| 198 | int eof=0; |
---|
[132] | 199 | int current_section = -1; |
---|
[120] | 200 | int last_section = -1; |
---|
[122] | 201 | struct roar_audio_info info; |
---|
[111] | 202 | char pcmout[4096]; |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | for (i = 1; i < argc; i++) { |
---|
| 206 | k = argv[i]; |
---|
| 207 | |
---|
| 208 | if ( strcmp(k, "--server") == 0 ) { |
---|
| 209 | server = argv[++i]; |
---|
[3043] | 210 | } else if ( strcmp(k, "--vclt-out") == 0 ) { |
---|
| 211 | vcltfile = argv[++i]; |
---|
[111] | 212 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
| 213 | usage(); |
---|
| 214 | return 0; |
---|
| 215 | } else if ( file == NULL ) { |
---|
| 216 | file = k; |
---|
| 217 | } else { |
---|
| 218 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 219 | usage(); |
---|
| 220 | return 1; |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
[2568] | 224 | roar_libroar_set_server(server); |
---|
| 225 | |
---|
[1510] | 226 | if ( file == NULL ) { |
---|
| 227 | ROAR_ERR("No filename given."); |
---|
| 228 | return 1; |
---|
| 229 | } |
---|
| 230 | |
---|
[115] | 231 | if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) { |
---|
[111] | 232 | ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!"); |
---|
[1406] | 233 | return 1; |
---|
[111] | 234 | } |
---|
| 235 | |
---|
[124] | 236 | if ( strncmp(file, "http:", 5) == 0 ) { |
---|
| 237 | in = open_http(file); |
---|
| 238 | } else { |
---|
| 239 | in = fopen(file, "rb"); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | if ( in == NULL ) { |
---|
[111] | 243 | roar_disconnect(&con); |
---|
[1406] | 244 | return 1; |
---|
[111] | 245 | } |
---|
| 246 | |
---|
| 247 | #ifdef _WIN32 |
---|
| 248 | _setmode(_fileno(in), _O_BINARY); |
---|
| 249 | #endif |
---|
| 250 | |
---|
| 251 | if(ov_open(in, &vf, NULL, 0) < 0) { |
---|
| 252 | fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n"); |
---|
| 253 | roar_disconnect(&con); |
---|
[1406] | 254 | return 1; |
---|
[111] | 255 | } |
---|
| 256 | |
---|
[3043] | 257 | if ( vcltfile != NULL ) { |
---|
| 258 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_WRONLY|O_CREAT|O_APPEND, 0644) == -1 ) |
---|
| 259 | return 1; |
---|
| 260 | if ( roar_vio_open_dstr(&vclt, vcltfile, &def, 1) == -1 ) { |
---|
| 261 | fprintf(stderr, "Error: can not open file: %s: %s\n", k, strerror(errno)); |
---|
| 262 | return 1; |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
[121] | 266 | // if ( update_stream(&con, &s, &out, &vf, file) == -1 ) |
---|
[1406] | 267 | // return 1; |
---|
[111] | 268 | |
---|
[117] | 269 | while (!eof) { |
---|
| 270 | long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, ¤t_section); |
---|
[120] | 271 | |
---|
[121] | 272 | if ( last_section != current_section ) |
---|
[3043] | 273 | if ( update_stream(&con, &s, &out, &vf, file, &info, vcltfile == NULL ? NULL : &vclt) == -1 ) |
---|
[715] | 274 | return 1; |
---|
[121] | 275 | |
---|
[120] | 276 | last_section = current_section; |
---|
| 277 | |
---|
[111] | 278 | if (ret == 0) { |
---|
| 279 | /* EOF */ |
---|
| 280 | eof=1; |
---|
| 281 | } else if (ret < 0) { |
---|
| 282 | /* error in the stream. Not a problem, just reporting it in |
---|
| 283 | case we (the app) cares. In this case, we don't. */ |
---|
| 284 | } else { |
---|
| 285 | /* we don't bother dealing with sample rate changes, etc, but |
---|
| 286 | you'll have to */ |
---|
[507] | 287 | // write(out, pcmout, ret); |
---|
| 288 | roar_stream_send_data(&con, &s, pcmout, ret); |
---|
[111] | 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | ov_clear(&vf); |
---|
| 293 | |
---|
| 294 | // fclose(in); |
---|
[120] | 295 | close(out); |
---|
| 296 | roar_disconnect(&con); |
---|
[111] | 297 | |
---|
[3043] | 298 | if ( vcltfile != NULL ) { |
---|
| 299 | roar_vio_close(&vclt); |
---|
| 300 | } |
---|
| 301 | |
---|
[111] | 302 | return 0; |
---|
[478] | 303 | #endif |
---|
[111] | 304 | } |
---|
| 305 | |
---|
| 306 | //ll |
---|