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