[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 | |
---|
[215] | 40 | int roar_file_codecdetect(char * buf, int len) { |
---|
| 41 | int codec = -1; |
---|
| 42 | |
---|
| 43 | if ( len > 3 ) { |
---|
| 44 | if ( strncmp(buf, "OggS", 4) == 0 ) { |
---|
| 45 | codec = ROAR_CODEC_OGG_GENERAL; |
---|
[1000] | 46 | if ( len > 32 ) { // this is 5 bytes after the end of the header |
---|
| 47 | if ( strncmp(buf+28, "\177FLAC", 5) == 0 ) { |
---|
| 48 | codec = ROAR_CODEC_OGG_FLAC; |
---|
| 49 | } else if ( strncmp(buf+28, "Speex", 5) == 0 ) { |
---|
| 50 | codec = ROAR_CODEC_OGG_SPEEX; |
---|
| 51 | } else if ( len > 34 ) { // this is 7 bytes after the end of the header |
---|
| 52 | if ( strncmp(buf+28, "\001vorbis", 7) == 0 ) |
---|
| 53 | codec = ROAR_CODEC_OGG_VORBIS; |
---|
| 54 | } |
---|
[215] | 55 | } |
---|
[558] | 56 | } else if ( strncmp(buf, "MThd", 4) == 0 ) { |
---|
| 57 | codec = ROAR_CODEC_MIDI_FILE; |
---|
[563] | 58 | } else if ( strncmp(buf, "RIFF", 4) == 0 ) { |
---|
| 59 | if ( len > 15 ) { |
---|
| 60 | if ( strncmp(buf+8, "WAVEfmt ", 8) == 0 ) |
---|
| 61 | codec = ROAR_CODEC_RIFF_WAVE; |
---|
| 62 | } |
---|
[623] | 63 | } else if ( strncmp(buf, "Roar", 4) == 0 ) { |
---|
| 64 | if ( len > ROAR_SPEEX_MAGIC_LEN ) { |
---|
| 65 | if ( strncmp(buf, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) == 0 ) |
---|
| 66 | codec = ROAR_CODEC_ROAR_SPEEX; |
---|
[636] | 67 | #if ROAR_SPEEX_MAGIC_LEN < ROAR_CELT_MAGIC_LEN |
---|
| 68 | } |
---|
| 69 | if ( len > ROAR_CELT_MAGIC_LEN ) { |
---|
| 70 | #endif |
---|
| 71 | if ( strncmp(buf, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) == 0 ) |
---|
| 72 | codec = ROAR_CODEC_ROAR_CELT; |
---|
[623] | 73 | } |
---|
[889] | 74 | } else if ( strncmp(buf, "RAUM-CF0", 8) == 0 ) { |
---|
| 75 | codec = ROAR_CODEC_RAUM; |
---|
[215] | 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | return codec; |
---|
| 80 | } |
---|
| 81 | |
---|
[208] | 82 | ssize_t roar_file_send_raw (int out, int in) { |
---|
| 83 | ssize_t r = 0; |
---|
[701] | 84 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
[208] | 85 | ssize_t ret; |
---|
[701] | 86 | #endif |
---|
[208] | 87 | int len; |
---|
| 88 | char buf[BUFSIZE]; |
---|
[217] | 89 | #ifdef __linux__ |
---|
| 90 | int cork_new = 1, cork_old; |
---|
| 91 | socklen_t cork_len = sizeof(int); |
---|
| 92 | |
---|
[219] | 93 | if ( getsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, &cork_len) == -1 ) { |
---|
| 94 | cork_old = -1; |
---|
| 95 | } else { |
---|
| 96 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_new, sizeof(int)); |
---|
| 97 | } |
---|
[217] | 98 | #endif |
---|
[208] | 99 | |
---|
| 100 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
| 101 | while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0) |
---|
| 102 | r += ret; |
---|
| 103 | #endif |
---|
| 104 | |
---|
| 105 | // TODO: try mmap here! |
---|
| 106 | |
---|
| 107 | while ((len = read(in, buf, BUFSIZE)) > 0) |
---|
| 108 | r += write(out, buf, len); |
---|
| 109 | |
---|
[217] | 110 | #ifdef __linux__ |
---|
[219] | 111 | if ( cork_old != -1 ) |
---|
| 112 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len); |
---|
[217] | 113 | #endif |
---|
[208] | 114 | return r; |
---|
| 115 | } |
---|
| 116 | |
---|
[649] | 117 | ssize_t roar_file_map (char * filename, int flags, mode_t mode, size_t len, void ** mem) { |
---|
[1081] | 118 | #ifndef ROAR_TARGET_WIN32 |
---|
[649] | 119 | int fh; |
---|
| 120 | int mmap_flags = 0; |
---|
| 121 | struct stat stat; |
---|
| 122 | |
---|
| 123 | if ( mem == NULL || filename == NULL ) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
| 126 | *mem = NULL; |
---|
| 127 | |
---|
| 128 | if ( flags & O_RDWR ) { |
---|
| 129 | mmap_flags = PROT_READ|PROT_WRITE; |
---|
| 130 | } else if ( flags & O_WRONLY ) { |
---|
| 131 | mmap_flags = PROT_WRITE; |
---|
| 132 | } else { |
---|
| 133 | mmap_flags = PROT_READ; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if ( (fh = open(filename, flags, mode)) == -1 ) { |
---|
| 137 | return -1; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | if ( fstat(fh, &stat) == -1 ) { |
---|
| 141 | close(fh); |
---|
| 142 | return -1; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | if ( stat.st_size < len ) { |
---|
| 146 | if ( ftruncate(fh, len) == -1 ) { |
---|
| 147 | close(fh); |
---|
| 148 | return -1; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | if ( (*mem = mmap(NULL, len, mmap_flags, MAP_SHARED, fh, 0)) == NULL ) { |
---|
| 153 | close(fh); |
---|
| 154 | return -1; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | close(fh); |
---|
| 158 | |
---|
| 159 | return len; |
---|
[1081] | 160 | #else |
---|
| 161 | ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS..."); |
---|
| 162 | return -1; |
---|
| 163 | #endif |
---|
[649] | 164 | } |
---|
| 165 | |
---|
| 166 | int roar_file_unmap (size_t len, void * mem) { |
---|
[1081] | 167 | #ifndef ROAR_TARGET_WIN32 |
---|
[649] | 168 | return munmap(mem, len); |
---|
[1081] | 169 | #else |
---|
| 170 | ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS..."); |
---|
| 171 | return -1; |
---|
| 172 | #endif |
---|
[649] | 173 | } |
---|
| 174 | |
---|
| 175 | |
---|
[215] | 176 | ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) { |
---|
[764] | 177 | return roar_file_play_full(con, file, exec, 0, NULL); |
---|
[762] | 178 | } |
---|
| 179 | |
---|
| 180 | ssize_t roar_file_play_full (struct roar_connection * con, char * file, int exec, int passfh, struct roar_stream * s) { |
---|
[215] | 181 | int codec = -1; |
---|
| 182 | int in, out = -1; |
---|
| 183 | ssize_t r = 0; |
---|
[776] | 184 | int seek; |
---|
[215] | 185 | int len; |
---|
| 186 | char buf[BUFSIZE]; |
---|
| 187 | int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT; |
---|
[764] | 188 | struct roar_stream localstream[1]; |
---|
| 189 | |
---|
| 190 | if ( !s ) |
---|
| 191 | s = localstream; |
---|
[215] | 192 | |
---|
| 193 | if ( !con ) |
---|
| 194 | return -1; |
---|
| 195 | |
---|
| 196 | if ( !file ) |
---|
| 197 | return -1; |
---|
| 198 | |
---|
[762] | 199 | if ( exec && passfh ) |
---|
| 200 | return -1; |
---|
| 201 | |
---|
[215] | 202 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
| 203 | return -1; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | if ((len = read(in, buf, BUFSIZE)) < 1) { |
---|
| 207 | close(in); |
---|
| 208 | return -1; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | codec = roar_file_codecdetect(buf, len); |
---|
| 212 | |
---|
[1000] | 213 | ROAR_DBG("roar_file_play_full(*): codec=%i(%s)", codec, roar_codec2str(codec)); |
---|
| 214 | |
---|
[776] | 215 | seek = lseek(in, 0, SEEK_SET) == (off_t) -1 ? 0 : 1; |
---|
| 216 | |
---|
[215] | 217 | if ( codec == -1 ) { |
---|
| 218 | close(in); |
---|
| 219 | return -1; |
---|
| 220 | } |
---|
| 221 | |
---|
[776] | 222 | if ( passfh && !seek ) { |
---|
| 223 | ROAR_WARN("roar_file_play_full(*): passfh on non seekable file: this may produce incorrect playback"); |
---|
| 224 | } |
---|
| 225 | |
---|
[215] | 226 | if ( exec ) { |
---|
[762] | 227 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
[215] | 228 | close(in); |
---|
| 229 | return -1; |
---|
| 230 | } |
---|
| 231 | |
---|
[762] | 232 | if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) { |
---|
[215] | 233 | close(in); |
---|
| 234 | return -1; |
---|
| 235 | } |
---|
| 236 | |
---|
[762] | 237 | if ( roar_stream_exec(con, s) == -1 ) { |
---|
[215] | 238 | close(in); |
---|
| 239 | return -1; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | shutdown(con->fh, SHUT_RD); |
---|
| 243 | |
---|
| 244 | out = con->fh; |
---|
| 245 | } else { |
---|
[776] | 246 | if ( !(passfh && seek) ) { |
---|
| 247 | if ( (out = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 248 | close(in); |
---|
| 249 | return -1; |
---|
| 250 | } |
---|
| 251 | } else { |
---|
| 252 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
| 253 | close(in); |
---|
| 254 | return -1; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) { |
---|
| 258 | close(in); |
---|
| 259 | return -1; |
---|
| 260 | } |
---|
[215] | 261 | } |
---|
| 262 | } |
---|
| 263 | |
---|
[776] | 264 | if ( !seek ) |
---|
| 265 | write(out, buf, len); |
---|
[215] | 266 | |
---|
[762] | 267 | if ( !passfh ) { |
---|
| 268 | r = roar_file_send_raw(out, in); |
---|
[215] | 269 | |
---|
[762] | 270 | close(out); |
---|
| 271 | |
---|
| 272 | if ( exec ) { |
---|
| 273 | con->fh = -1; |
---|
| 274 | } |
---|
[215] | 275 | |
---|
[762] | 276 | close(in); |
---|
| 277 | } else { |
---|
| 278 | if ( roar_stream_passfh(con, s, in) == -1 ) { |
---|
| 279 | return -1; |
---|
| 280 | } |
---|
| 281 | close(out); |
---|
| 282 | close(in); |
---|
| 283 | return 0; |
---|
[215] | 284 | } |
---|
| 285 | |
---|
| 286 | return r; |
---|
| 287 | } |
---|
| 288 | |
---|
[805] | 289 | char * roar_cdromdevice (void) { |
---|
| 290 | char * k; |
---|
| 291 | |
---|
| 292 | if ( (k = getenv("CDDA_DEVICE")) != NULL ) |
---|
| 293 | return k; |
---|
| 294 | |
---|
| 295 | #ifdef ROAR_DEFAULT_CDROM |
---|
| 296 | return ROAR_DEFAULT_CDROM; |
---|
| 297 | #endif |
---|
| 298 | |
---|
| 299 | return NULL; |
---|
| 300 | } |
---|
| 301 | |
---|
[202] | 302 | //ll |
---|