[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) { |
---|
| 56 | char cmd[1024]; |
---|
| 57 | |
---|
| 58 | snprintf(cmd, 1023, "wget -qO - '%s'", file); |
---|
| 59 | |
---|
| 60 | return popen(cmd, "r"); |
---|
| 61 | } |
---|
| 62 | |
---|
[122] | 63 | int update_stream (struct roar_connection * con, struct roar_stream * s, int * out, OggVorbis_File * vf, char * file, struct roar_audio_info * info) { |
---|
[121] | 64 | vorbis_info *vi = ov_info(vf, -1); |
---|
[111] | 65 | int bits = 16; |
---|
| 66 | int codec = ROAR_CODEC_DEFAULT; |
---|
[121] | 67 | char **ptr = ov_comment(vf, -1)->user_comments; |
---|
[132] | 68 | char key[80], value[80] = {0}; |
---|
[121] | 69 | int j, h = 0; |
---|
| 70 | struct roar_meta meta; |
---|
[122] | 71 | int need_new_stream = 0; |
---|
[121] | 72 | |
---|
| 73 | fprintf(stderr, "\n"); |
---|
| 74 | |
---|
[122] | 75 | if ( *out == -1 ) { |
---|
| 76 | need_new_stream = 1; |
---|
| 77 | } else if ( info->rate != vi->rate || info->channels != vi->channels ) { |
---|
| 78 | need_new_stream = 1; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | if ( need_new_stream ) { |
---|
| 82 | if ( *out != -1 ) |
---|
[121] | 83 | close(*out); |
---|
| 84 | |
---|
[122] | 85 | fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate); |
---|
| 86 | |
---|
| 87 | info->rate = vi->rate; |
---|
| 88 | info->channels = vi->channels; |
---|
[121] | 89 | |
---|
[122] | 90 | if ( (*out = roar_simple_new_stream_obj(con, s, vi->rate, vi->channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 91 | roar_disconnect(con); |
---|
| 92 | return -1; |
---|
| 93 | } |
---|
[121] | 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | meta.value = value; |
---|
| 98 | meta.key[0] = 0; |
---|
[132] | 99 | meta.type = ROAR_META_TYPE_NONE; |
---|
[121] | 100 | |
---|
| 101 | roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta); |
---|
| 102 | |
---|
[124] | 103 | if ( strncmp(file, "http:", 5) == 0 ) |
---|
| 104 | meta.type = ROAR_META_TYPE_FILEURL; |
---|
| 105 | else |
---|
| 106 | meta.type = ROAR_META_TYPE_FILENAME; |
---|
| 107 | |
---|
[121] | 108 | strncpy(value, file, 79); |
---|
| 109 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
| 110 | |
---|
| 111 | while(*ptr){ |
---|
| 112 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
| 113 | key[j] = (*ptr)[j]; |
---|
| 114 | key[j] = 0; |
---|
| 115 | |
---|
| 116 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
| 117 | value[h++] = (*ptr)[j]; |
---|
| 118 | value[h] = 0; |
---|
| 119 | |
---|
| 120 | meta.type = roar_meta_inttype(key); |
---|
| 121 | if ( meta.type != -1 ) |
---|
| 122 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
| 123 | |
---|
| 124 | fprintf(stderr, "Meta %-16s: %s\n", key, value); |
---|
| 125 | ++ptr; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | return 0; |
---|
| 129 | } |
---|
| 130 | |
---|
[478] | 131 | #endif |
---|
| 132 | |
---|
[121] | 133 | int main (int argc, char * argv[]) { |
---|
[478] | 134 | #ifndef ROAR_HAVE_LIBVORBISFILE |
---|
[479] | 135 | fprintf(stderr, "Error: no Vorbis support!\n"); |
---|
[478] | 136 | return 1; |
---|
| 137 | #else |
---|
[111] | 138 | char * server = NULL; |
---|
| 139 | char * file = NULL; |
---|
| 140 | char * k; |
---|
| 141 | int i; |
---|
| 142 | FILE * in; |
---|
[120] | 143 | int out = -1; |
---|
[111] | 144 | struct roar_connection con; |
---|
| 145 | struct roar_stream s; |
---|
| 146 | OggVorbis_File vf; |
---|
| 147 | int eof=0; |
---|
[132] | 148 | int current_section = -1; |
---|
[120] | 149 | int last_section = -1; |
---|
[122] | 150 | struct roar_audio_info info; |
---|
[111] | 151 | char pcmout[4096]; |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | for (i = 1; i < argc; i++) { |
---|
| 155 | k = argv[i]; |
---|
| 156 | |
---|
| 157 | if ( strcmp(k, "--server") == 0 ) { |
---|
| 158 | server = argv[++i]; |
---|
| 159 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
| 160 | usage(); |
---|
| 161 | return 0; |
---|
| 162 | } else if ( file == NULL ) { |
---|
| 163 | file = k; |
---|
| 164 | } else { |
---|
| 165 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 166 | usage(); |
---|
| 167 | return 1; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
[115] | 171 | if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) { |
---|
[111] | 172 | ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!"); |
---|
| 173 | return -1; |
---|
| 174 | } |
---|
| 175 | |
---|
[124] | 176 | if ( strncmp(file, "http:", 5) == 0 ) { |
---|
| 177 | in = open_http(file); |
---|
| 178 | } else { |
---|
| 179 | in = fopen(file, "rb"); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if ( in == NULL ) { |
---|
[111] | 183 | roar_disconnect(&con); |
---|
| 184 | return -1; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | #ifdef _WIN32 |
---|
| 188 | _setmode(_fileno(in), _O_BINARY); |
---|
| 189 | #endif |
---|
| 190 | |
---|
| 191 | if(ov_open(in, &vf, NULL, 0) < 0) { |
---|
| 192 | fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n"); |
---|
| 193 | roar_disconnect(&con); |
---|
| 194 | return -1; |
---|
| 195 | } |
---|
| 196 | |
---|
[121] | 197 | // if ( update_stream(&con, &s, &out, &vf, file) == -1 ) |
---|
| 198 | // return -1; |
---|
[111] | 199 | |
---|
[117] | 200 | while (!eof) { |
---|
| 201 | long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, ¤t_section); |
---|
[120] | 202 | |
---|
[121] | 203 | if ( last_section != current_section ) |
---|
[122] | 204 | if ( update_stream(&con, &s, &out, &vf, file, &info) == -1 ) |
---|
[121] | 205 | return -1; |
---|
| 206 | |
---|
[120] | 207 | last_section = current_section; |
---|
| 208 | |
---|
[111] | 209 | if (ret == 0) { |
---|
| 210 | /* EOF */ |
---|
| 211 | eof=1; |
---|
| 212 | } else if (ret < 0) { |
---|
| 213 | /* error in the stream. Not a problem, just reporting it in |
---|
| 214 | case we (the app) cares. In this case, we don't. */ |
---|
| 215 | } else { |
---|
| 216 | /* we don't bother dealing with sample rate changes, etc, but |
---|
| 217 | you'll have to */ |
---|
[507] | 218 | // write(out, pcmout, ret); |
---|
| 219 | roar_stream_send_data(&con, &s, pcmout, ret); |
---|
[111] | 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | ov_clear(&vf); |
---|
| 224 | |
---|
| 225 | // fclose(in); |
---|
[120] | 226 | close(out); |
---|
| 227 | roar_disconnect(&con); |
---|
[111] | 228 | |
---|
| 229 | return 0; |
---|
[478] | 230 | #endif |
---|
[111] | 231 | } |
---|
| 232 | |
---|
| 233 | //ll |
---|