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