[202] | 1 | //file.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[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 |
---|
[4159] | 76 | if ( strncmp(buf, ROAR_CELT_MAGIC_0, ROAR_CELT_MAGIC_LEN) == 0 || |
---|
| 77 | strncmp(buf, ROAR_CELT_MAGIC_1, ROAR_CELT_MAGIC_LEN) == 0 ) |
---|
[636] | 78 | codec = ROAR_CODEC_ROAR_CELT; |
---|
[623] | 79 | } |
---|
[1196] | 80 | } else if ( strncmp(buf, "fLaC", 4) == 0 ) { |
---|
| 81 | codec = ROAR_CODEC_FLAC; |
---|
[4159] | 82 | } else if ( strncmp(buf, ".snd", 4) == 0 ) { |
---|
| 83 | codec = ROAR_CODEC_AU; |
---|
[1196] | 84 | } else if ( len > 7 && strncmp(buf, "RAUM-CF0", 8) == 0 ) { |
---|
[889] | 85 | codec = ROAR_CODEC_RAUM; |
---|
[215] | 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | return codec; |
---|
| 90 | } |
---|
| 91 | |
---|
[208] | 92 | ssize_t roar_file_send_raw (int out, int in) { |
---|
[1474] | 93 | #ifdef _CAN_OPERATE |
---|
[208] | 94 | ssize_t r = 0; |
---|
[701] | 95 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
[208] | 96 | ssize_t ret; |
---|
[701] | 97 | #endif |
---|
[208] | 98 | int len; |
---|
| 99 | char buf[BUFSIZE]; |
---|
[1378] | 100 | #if defined(__linux__) && defined(ROAR_HAVE_IPV4) |
---|
[217] | 101 | int cork_new = 1, cork_old; |
---|
| 102 | socklen_t cork_len = sizeof(int); |
---|
| 103 | |
---|
[219] | 104 | if ( getsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, &cork_len) == -1 ) { |
---|
| 105 | cork_old = -1; |
---|
| 106 | } else { |
---|
| 107 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_new, sizeof(int)); |
---|
| 108 | } |
---|
[217] | 109 | #endif |
---|
[208] | 110 | |
---|
[3857] | 111 | roar_debug_warn_obsolete("roar_file_send_raw", "roar_vio_copy_data", NULL); |
---|
| 112 | |
---|
[208] | 113 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
| 114 | while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0) |
---|
| 115 | r += ret; |
---|
| 116 | #endif |
---|
| 117 | |
---|
| 118 | // TODO: try mmap here! |
---|
| 119 | |
---|
| 120 | while ((len = read(in, buf, BUFSIZE)) > 0) |
---|
| 121 | r += write(out, buf, len); |
---|
| 122 | |
---|
[1379] | 123 | #if defined(__linux__) && defined(ROAR_HAVE_IPV4) |
---|
[219] | 124 | if ( cork_old != -1 ) |
---|
| 125 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len); |
---|
[217] | 126 | #endif |
---|
[208] | 127 | return r; |
---|
[1474] | 128 | #else |
---|
| 129 | return -1; |
---|
| 130 | #endif |
---|
[208] | 131 | } |
---|
| 132 | |
---|
[649] | 133 | ssize_t roar_file_map (char * filename, int flags, mode_t mode, size_t len, void ** mem) { |
---|
[1442] | 134 | #ifdef ROAR_HAVE_MMAP |
---|
[649] | 135 | int fh; |
---|
| 136 | int mmap_flags = 0; |
---|
| 137 | struct stat stat; |
---|
| 138 | |
---|
| 139 | if ( mem == NULL || filename == NULL ) |
---|
| 140 | return -1; |
---|
| 141 | |
---|
| 142 | *mem = NULL; |
---|
| 143 | |
---|
| 144 | if ( flags & O_RDWR ) { |
---|
| 145 | mmap_flags = PROT_READ|PROT_WRITE; |
---|
| 146 | } else if ( flags & O_WRONLY ) { |
---|
| 147 | mmap_flags = PROT_WRITE; |
---|
| 148 | } else { |
---|
| 149 | mmap_flags = PROT_READ; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | if ( (fh = open(filename, flags, mode)) == -1 ) { |
---|
| 153 | return -1; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | if ( fstat(fh, &stat) == -1 ) { |
---|
| 157 | close(fh); |
---|
| 158 | return -1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | if ( stat.st_size < len ) { |
---|
| 162 | if ( ftruncate(fh, len) == -1 ) { |
---|
| 163 | close(fh); |
---|
| 164 | return -1; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | if ( (*mem = mmap(NULL, len, mmap_flags, MAP_SHARED, fh, 0)) == NULL ) { |
---|
| 169 | close(fh); |
---|
| 170 | return -1; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | close(fh); |
---|
| 174 | |
---|
| 175 | return len; |
---|
[1081] | 176 | #else |
---|
[1442] | 177 | #ifdef ROAR_TARGET_WIN32 |
---|
[1081] | 178 | ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS..."); |
---|
[1442] | 179 | #endif |
---|
[1081] | 180 | return -1; |
---|
| 181 | #endif |
---|
[649] | 182 | } |
---|
| 183 | |
---|
| 184 | int roar_file_unmap (size_t len, void * mem) { |
---|
[1442] | 185 | #ifdef ROAR_HAVE_MMAP |
---|
[649] | 186 | return munmap(mem, len); |
---|
[1081] | 187 | #else |
---|
[1442] | 188 | #ifdef ROAR_TARGET_WIN32 |
---|
[1081] | 189 | ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS..."); |
---|
[1442] | 190 | #endif |
---|
[1081] | 191 | return -1; |
---|
| 192 | #endif |
---|
[649] | 193 | } |
---|
| 194 | |
---|
| 195 | |
---|
[215] | 196 | ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) { |
---|
[764] | 197 | return roar_file_play_full(con, file, exec, 0, NULL); |
---|
[762] | 198 | } |
---|
| 199 | |
---|
| 200 | ssize_t roar_file_play_full (struct roar_connection * con, char * file, int exec, int passfh, struct roar_stream * s) { |
---|
[1474] | 201 | #ifdef _CAN_OPERATE |
---|
[215] | 202 | int codec = -1; |
---|
| 203 | int in, out = -1; |
---|
| 204 | ssize_t r = 0; |
---|
[776] | 205 | int seek; |
---|
[215] | 206 | int len; |
---|
| 207 | char buf[BUFSIZE]; |
---|
| 208 | int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT; |
---|
[764] | 209 | struct roar_stream localstream[1]; |
---|
| 210 | |
---|
[1660] | 211 | // FIXME: check error cases |
---|
| 212 | |
---|
[1767] | 213 | ROAR_DBG("roar_file_play_full(*) = ?"); |
---|
| 214 | |
---|
[764] | 215 | if ( !s ) |
---|
| 216 | s = localstream; |
---|
[215] | 217 | |
---|
| 218 | if ( !con ) |
---|
| 219 | return -1; |
---|
| 220 | |
---|
| 221 | if ( !file ) |
---|
| 222 | return -1; |
---|
| 223 | |
---|
[762] | 224 | if ( exec && passfh ) |
---|
| 225 | return -1; |
---|
| 226 | |
---|
[1765] | 227 | #ifdef ROAR_TARGET_WIN32 |
---|
| 228 | if ( (in = open(file, O_RDONLY|O_BINARY, 0644)) == -1 ) { |
---|
| 229 | #else |
---|
[215] | 230 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
[1765] | 231 | #endif |
---|
[215] | 232 | return -1; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | if ((len = read(in, buf, BUFSIZE)) < 1) { |
---|
| 236 | close(in); |
---|
| 237 | return -1; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | codec = roar_file_codecdetect(buf, len); |
---|
| 241 | |
---|
[1000] | 242 | ROAR_DBG("roar_file_play_full(*): codec=%i(%s)", codec, roar_codec2str(codec)); |
---|
| 243 | |
---|
[776] | 244 | seek = lseek(in, 0, SEEK_SET) == (off_t) -1 ? 0 : 1; |
---|
| 245 | |
---|
[215] | 246 | if ( codec == -1 ) { |
---|
[1766] | 247 | ROAR_ERR("roar_file_play_full(*): Unknown codec of file: %s", file); |
---|
[215] | 248 | close(in); |
---|
| 249 | return -1; |
---|
| 250 | } |
---|
| 251 | |
---|
[776] | 252 | if ( passfh && !seek ) { |
---|
| 253 | ROAR_WARN("roar_file_play_full(*): passfh on non seekable file: this may produce incorrect playback"); |
---|
| 254 | } |
---|
| 255 | |
---|
[215] | 256 | if ( exec ) { |
---|
[762] | 257 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
[1766] | 258 | ROAR_ERR("roar_file_play_full(*): Can not create new stream. This is realy BAD!"); |
---|
[215] | 259 | close(in); |
---|
| 260 | return -1; |
---|
| 261 | } |
---|
| 262 | |
---|
[762] | 263 | if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) { |
---|
[1766] | 264 | ROAR_ERR("roar_file_play_full(*): Can not connect new stream to server."); |
---|
[215] | 265 | close(in); |
---|
| 266 | return -1; |
---|
| 267 | } |
---|
| 268 | |
---|
[762] | 269 | if ( roar_stream_exec(con, s) == -1 ) { |
---|
[1766] | 270 | ROAR_ERR("roar_file_play_full(*): Can not exec new stream on server."); |
---|
[215] | 271 | close(in); |
---|
| 272 | return -1; |
---|
| 273 | } |
---|
| 274 | |
---|
[3857] | 275 | roar_libroar_nowarn(); |
---|
[1660] | 276 | if ( (out = roar_get_connection_fh(con)) == -1 ) { |
---|
[1766] | 277 | ROAR_ERR("roar_file_play_full(*): Can not get socket of server connection for exec data transmition."); |
---|
[1660] | 278 | close(in); |
---|
[3857] | 279 | roar_libroar_warn(); |
---|
[1660] | 280 | return -1; |
---|
| 281 | } |
---|
[3857] | 282 | roar_libroar_warn(); |
---|
[215] | 283 | |
---|
[1660] | 284 | ROAR_SHUTDOWN(out, SHUT_RD); |
---|
[215] | 285 | } else { |
---|
[776] | 286 | if ( !(passfh && seek) ) { |
---|
| 287 | if ( (out = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 288 | close(in); |
---|
| 289 | return -1; |
---|
| 290 | } |
---|
| 291 | } else { |
---|
| 292 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
| 293 | close(in); |
---|
| 294 | return -1; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) { |
---|
| 298 | close(in); |
---|
| 299 | return -1; |
---|
| 300 | } |
---|
[215] | 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
[776] | 304 | if ( !seek ) |
---|
[1765] | 305 | ROAR_NETWORK_WRITE(out, buf, len); |
---|
[215] | 306 | |
---|
[762] | 307 | if ( !passfh ) { |
---|
[1765] | 308 | #ifndef ROAR_TARGET_WIN32 |
---|
[762] | 309 | r = roar_file_send_raw(out, in); |
---|
[215] | 310 | |
---|
[762] | 311 | close(out); |
---|
[1765] | 312 | #else |
---|
| 313 | while ((len = read(in, buf, BUFSIZE)) > 0) |
---|
| 314 | if ( send(out, buf, len, 0) != len ) |
---|
| 315 | break; |
---|
| 316 | |
---|
| 317 | closesocket(out); |
---|
| 318 | #endif |
---|
[762] | 319 | |
---|
| 320 | if ( exec ) { |
---|
[1315] | 321 | // TODO: FIXME: this ma cause a memory leak in future |
---|
| 322 | // OLD: con->fh = -1; |
---|
| 323 | roar_connect_fh(con, -2); |
---|
[762] | 324 | } |
---|
[215] | 325 | |
---|
[762] | 326 | close(in); |
---|
| 327 | } else { |
---|
| 328 | if ( roar_stream_passfh(con, s, in) == -1 ) { |
---|
| 329 | return -1; |
---|
| 330 | } |
---|
| 331 | close(out); |
---|
| 332 | close(in); |
---|
| 333 | return 0; |
---|
[215] | 334 | } |
---|
| 335 | |
---|
| 336 | return r; |
---|
[1474] | 337 | #else |
---|
| 338 | return -1; |
---|
| 339 | #endif |
---|
[215] | 340 | } |
---|
| 341 | |
---|
[805] | 342 | char * roar_cdromdevice (void) { |
---|
| 343 | char * k; |
---|
| 344 | |
---|
| 345 | if ( (k = getenv("CDDA_DEVICE")) != NULL ) |
---|
| 346 | return k; |
---|
| 347 | |
---|
| 348 | #ifdef ROAR_DEFAULT_CDROM |
---|
| 349 | return ROAR_DEFAULT_CDROM; |
---|
| 350 | #endif |
---|
| 351 | |
---|
| 352 | return NULL; |
---|
| 353 | } |
---|
| 354 | |
---|
[202] | 355 | //ll |
---|