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