[202] | 1 | //file.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of libroar 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 | * libroar 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 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
[202] | 35 | #include "libroar.h" |
---|
| 36 | |
---|
[208] | 37 | #define BUFSIZE 8192 |
---|
| 38 | #define BUFMAX 65536 |
---|
| 39 | |
---|
[1474] | 40 | #ifdef ROAR_HAVE_IO_POSIX |
---|
| 41 | #define _CAN_OPERATE |
---|
| 42 | #endif |
---|
| 43 | |
---|
[215] | 44 | int roar_file_codecdetect(char * buf, int len) { |
---|
| 45 | int codec = -1; |
---|
| 46 | |
---|
| 47 | if ( len > 3 ) { |
---|
| 48 | if ( strncmp(buf, "OggS", 4) == 0 ) { |
---|
| 49 | codec = ROAR_CODEC_OGG_GENERAL; |
---|
[1000] | 50 | if ( len > 32 ) { // this is 5 bytes after the end of the header |
---|
| 51 | if ( strncmp(buf+28, "\177FLAC", 5) == 0 ) { |
---|
| 52 | codec = ROAR_CODEC_OGG_FLAC; |
---|
| 53 | } else if ( strncmp(buf+28, "Speex", 5) == 0 ) { |
---|
| 54 | codec = ROAR_CODEC_OGG_SPEEX; |
---|
| 55 | } else if ( len > 34 ) { // this is 7 bytes after the end of the header |
---|
| 56 | if ( strncmp(buf+28, "\001vorbis", 7) == 0 ) |
---|
| 57 | codec = ROAR_CODEC_OGG_VORBIS; |
---|
| 58 | } |
---|
[215] | 59 | } |
---|
[558] | 60 | } else if ( strncmp(buf, "MThd", 4) == 0 ) { |
---|
| 61 | codec = ROAR_CODEC_MIDI_FILE; |
---|
[563] | 62 | } else if ( strncmp(buf, "RIFF", 4) == 0 ) { |
---|
| 63 | if ( len > 15 ) { |
---|
| 64 | if ( strncmp(buf+8, "WAVEfmt ", 8) == 0 ) |
---|
| 65 | codec = ROAR_CODEC_RIFF_WAVE; |
---|
| 66 | } |
---|
[623] | 67 | } else if ( strncmp(buf, "Roar", 4) == 0 ) { |
---|
| 68 | if ( len > ROAR_SPEEX_MAGIC_LEN ) { |
---|
| 69 | if ( strncmp(buf, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) == 0 ) |
---|
| 70 | codec = ROAR_CODEC_ROAR_SPEEX; |
---|
[636] | 71 | #if ROAR_SPEEX_MAGIC_LEN < ROAR_CELT_MAGIC_LEN |
---|
| 72 | } |
---|
| 73 | if ( len > ROAR_CELT_MAGIC_LEN ) { |
---|
| 74 | #endif |
---|
| 75 | if ( strncmp(buf, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) == 0 ) |
---|
| 76 | codec = ROAR_CODEC_ROAR_CELT; |
---|
[623] | 77 | } |
---|
[1196] | 78 | } else if ( strncmp(buf, "fLaC", 4) == 0 ) { |
---|
| 79 | codec = ROAR_CODEC_FLAC; |
---|
| 80 | } else if ( len > 7 && strncmp(buf, "RAUM-CF0", 8) == 0 ) { |
---|
[889] | 81 | codec = ROAR_CODEC_RAUM; |
---|
[215] | 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | return codec; |
---|
| 86 | } |
---|
| 87 | |
---|
[208] | 88 | ssize_t roar_file_send_raw (int out, int in) { |
---|
[1474] | 89 | #ifdef _CAN_OPERATE |
---|
[208] | 90 | ssize_t r = 0; |
---|
[701] | 91 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
[208] | 92 | ssize_t ret; |
---|
[701] | 93 | #endif |
---|
[208] | 94 | int len; |
---|
| 95 | char buf[BUFSIZE]; |
---|
[1378] | 96 | #if defined(__linux__) && defined(ROAR_HAVE_IPV4) |
---|
[217] | 97 | int cork_new = 1, cork_old; |
---|
| 98 | socklen_t cork_len = sizeof(int); |
---|
| 99 | |
---|
[219] | 100 | if ( getsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, &cork_len) == -1 ) { |
---|
| 101 | cork_old = -1; |
---|
| 102 | } else { |
---|
| 103 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_new, sizeof(int)); |
---|
| 104 | } |
---|
[217] | 105 | #endif |
---|
[208] | 106 | |
---|
| 107 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
| 108 | while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0) |
---|
| 109 | r += ret; |
---|
| 110 | #endif |
---|
| 111 | |
---|
| 112 | // TODO: try mmap here! |
---|
| 113 | |
---|
| 114 | while ((len = read(in, buf, BUFSIZE)) > 0) |
---|
| 115 | r += write(out, buf, len); |
---|
| 116 | |
---|
[1379] | 117 | #if defined(__linux__) && defined(ROAR_HAVE_IPV4) |
---|
[219] | 118 | if ( cork_old != -1 ) |
---|
| 119 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len); |
---|
[217] | 120 | #endif |
---|
[208] | 121 | return r; |
---|
[1474] | 122 | #else |
---|
| 123 | return -1; |
---|
| 124 | #endif |
---|
[208] | 125 | } |
---|
| 126 | |
---|
[649] | 127 | ssize_t roar_file_map (char * filename, int flags, mode_t mode, size_t len, void ** mem) { |
---|
[1442] | 128 | #ifdef ROAR_HAVE_MMAP |
---|
[649] | 129 | int fh; |
---|
| 130 | int mmap_flags = 0; |
---|
| 131 | struct stat stat; |
---|
| 132 | |
---|
| 133 | if ( mem == NULL || filename == NULL ) |
---|
| 134 | return -1; |
---|
| 135 | |
---|
| 136 | *mem = NULL; |
---|
| 137 | |
---|
| 138 | if ( flags & O_RDWR ) { |
---|
| 139 | mmap_flags = PROT_READ|PROT_WRITE; |
---|
| 140 | } else if ( flags & O_WRONLY ) { |
---|
| 141 | mmap_flags = PROT_WRITE; |
---|
| 142 | } else { |
---|
| 143 | mmap_flags = PROT_READ; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | if ( (fh = open(filename, flags, mode)) == -1 ) { |
---|
| 147 | return -1; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | if ( fstat(fh, &stat) == -1 ) { |
---|
| 151 | close(fh); |
---|
| 152 | return -1; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | if ( stat.st_size < len ) { |
---|
| 156 | if ( ftruncate(fh, len) == -1 ) { |
---|
| 157 | close(fh); |
---|
| 158 | return -1; |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | if ( (*mem = mmap(NULL, len, mmap_flags, MAP_SHARED, fh, 0)) == NULL ) { |
---|
| 163 | close(fh); |
---|
| 164 | return -1; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | close(fh); |
---|
| 168 | |
---|
| 169 | return len; |
---|
[1081] | 170 | #else |
---|
[1442] | 171 | #ifdef ROAR_TARGET_WIN32 |
---|
[1081] | 172 | ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS..."); |
---|
[1442] | 173 | #endif |
---|
[1081] | 174 | return -1; |
---|
| 175 | #endif |
---|
[649] | 176 | } |
---|
| 177 | |
---|
| 178 | int roar_file_unmap (size_t len, void * mem) { |
---|
[1442] | 179 | #ifdef ROAR_HAVE_MMAP |
---|
[649] | 180 | return munmap(mem, len); |
---|
[1081] | 181 | #else |
---|
[1442] | 182 | #ifdef ROAR_TARGET_WIN32 |
---|
[1081] | 183 | ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS..."); |
---|
[1442] | 184 | #endif |
---|
[1081] | 185 | return -1; |
---|
| 186 | #endif |
---|
[649] | 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
[215] | 190 | ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) { |
---|
[764] | 191 | return roar_file_play_full(con, file, exec, 0, NULL); |
---|
[762] | 192 | } |
---|
| 193 | |
---|
| 194 | ssize_t roar_file_play_full (struct roar_connection * con, char * file, int exec, int passfh, struct roar_stream * s) { |
---|
[1474] | 195 | #ifdef _CAN_OPERATE |
---|
[215] | 196 | int codec = -1; |
---|
| 197 | int in, out = -1; |
---|
| 198 | ssize_t r = 0; |
---|
[776] | 199 | int seek; |
---|
[215] | 200 | int len; |
---|
| 201 | char buf[BUFSIZE]; |
---|
| 202 | int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT; |
---|
[764] | 203 | struct roar_stream localstream[1]; |
---|
| 204 | |
---|
[1660] | 205 | // FIXME: check error cases |
---|
| 206 | |
---|
[1767] | 207 | ROAR_DBG("roar_file_play_full(*) = ?"); |
---|
| 208 | |
---|
[764] | 209 | if ( !s ) |
---|
| 210 | s = localstream; |
---|
[215] | 211 | |
---|
| 212 | if ( !con ) |
---|
| 213 | return -1; |
---|
| 214 | |
---|
| 215 | if ( !file ) |
---|
| 216 | return -1; |
---|
| 217 | |
---|
[762] | 218 | if ( exec && passfh ) |
---|
| 219 | return -1; |
---|
| 220 | |
---|
[1765] | 221 | #ifdef ROAR_TARGET_WIN32 |
---|
| 222 | if ( (in = open(file, O_RDONLY|O_BINARY, 0644)) == -1 ) { |
---|
| 223 | #else |
---|
[215] | 224 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
[1765] | 225 | #endif |
---|
[215] | 226 | return -1; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | if ((len = read(in, buf, BUFSIZE)) < 1) { |
---|
| 230 | close(in); |
---|
| 231 | return -1; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | codec = roar_file_codecdetect(buf, len); |
---|
| 235 | |
---|
[1000] | 236 | ROAR_DBG("roar_file_play_full(*): codec=%i(%s)", codec, roar_codec2str(codec)); |
---|
| 237 | |
---|
[776] | 238 | seek = lseek(in, 0, SEEK_SET) == (off_t) -1 ? 0 : 1; |
---|
| 239 | |
---|
[215] | 240 | if ( codec == -1 ) { |
---|
[1766] | 241 | ROAR_ERR("roar_file_play_full(*): Unknown codec of file: %s", file); |
---|
[215] | 242 | close(in); |
---|
| 243 | return -1; |
---|
| 244 | } |
---|
| 245 | |
---|
[776] | 246 | if ( passfh && !seek ) { |
---|
| 247 | ROAR_WARN("roar_file_play_full(*): passfh on non seekable file: this may produce incorrect playback"); |
---|
| 248 | } |
---|
| 249 | |
---|
[215] | 250 | if ( exec ) { |
---|
[762] | 251 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
[1766] | 252 | ROAR_ERR("roar_file_play_full(*): Can not create new stream. This is realy BAD!"); |
---|
[215] | 253 | close(in); |
---|
| 254 | return -1; |
---|
| 255 | } |
---|
| 256 | |
---|
[762] | 257 | if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) { |
---|
[1766] | 258 | ROAR_ERR("roar_file_play_full(*): Can not connect new stream to server."); |
---|
[215] | 259 | close(in); |
---|
| 260 | return -1; |
---|
| 261 | } |
---|
| 262 | |
---|
[762] | 263 | if ( roar_stream_exec(con, s) == -1 ) { |
---|
[1766] | 264 | ROAR_ERR("roar_file_play_full(*): Can not exec new stream on server."); |
---|
[215] | 265 | close(in); |
---|
| 266 | return -1; |
---|
| 267 | } |
---|
| 268 | |
---|
[1660] | 269 | if ( (out = roar_get_connection_fh(con)) == -1 ) { |
---|
[1766] | 270 | ROAR_ERR("roar_file_play_full(*): Can not get socket of server connection for exec data transmition."); |
---|
[1660] | 271 | close(in); |
---|
| 272 | return -1; |
---|
| 273 | } |
---|
[215] | 274 | |
---|
[1660] | 275 | ROAR_SHUTDOWN(out, SHUT_RD); |
---|
[215] | 276 | } else { |
---|
[776] | 277 | if ( !(passfh && seek) ) { |
---|
| 278 | if ( (out = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 279 | close(in); |
---|
| 280 | return -1; |
---|
| 281 | } |
---|
| 282 | } else { |
---|
| 283 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
| 284 | close(in); |
---|
| 285 | return -1; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) { |
---|
| 289 | close(in); |
---|
| 290 | return -1; |
---|
| 291 | } |
---|
[215] | 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
[776] | 295 | if ( !seek ) |
---|
[1765] | 296 | ROAR_NETWORK_WRITE(out, buf, len); |
---|
[215] | 297 | |
---|
[762] | 298 | if ( !passfh ) { |
---|
[1765] | 299 | #ifndef ROAR_TARGET_WIN32 |
---|
[762] | 300 | r = roar_file_send_raw(out, in); |
---|
[215] | 301 | |
---|
[762] | 302 | close(out); |
---|
[1765] | 303 | #else |
---|
| 304 | while ((len = read(in, buf, BUFSIZE)) > 0) |
---|
| 305 | if ( send(out, buf, len, 0) != len ) |
---|
| 306 | break; |
---|
| 307 | |
---|
| 308 | closesocket(out); |
---|
| 309 | #endif |
---|
[762] | 310 | |
---|
| 311 | if ( exec ) { |
---|
[1315] | 312 | // TODO: FIXME: this ma cause a memory leak in future |
---|
| 313 | // OLD: con->fh = -1; |
---|
| 314 | roar_connect_fh(con, -2); |
---|
[762] | 315 | } |
---|
[215] | 316 | |
---|
[762] | 317 | close(in); |
---|
| 318 | } else { |
---|
| 319 | if ( roar_stream_passfh(con, s, in) == -1 ) { |
---|
| 320 | return -1; |
---|
| 321 | } |
---|
| 322 | close(out); |
---|
| 323 | close(in); |
---|
| 324 | return 0; |
---|
[215] | 325 | } |
---|
| 326 | |
---|
| 327 | return r; |
---|
[1474] | 328 | #else |
---|
| 329 | return -1; |
---|
| 330 | #endif |
---|
[215] | 331 | } |
---|
| 332 | |
---|
[805] | 333 | char * roar_cdromdevice (void) { |
---|
| 334 | char * k; |
---|
| 335 | |
---|
| 336 | if ( (k = getenv("CDDA_DEVICE")) != NULL ) |
---|
| 337 | return k; |
---|
| 338 | |
---|
| 339 | #ifdef ROAR_DEFAULT_CDROM |
---|
| 340 | return ROAR_DEFAULT_CDROM; |
---|
| 341 | #endif |
---|
| 342 | |
---|
| 343 | return NULL; |
---|
| 344 | } |
---|
| 345 | |
---|
[202] | 346 | //ll |
---|