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