[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; |
---|
| 46 | if ( len > 34 ) { // this is 7 bytes after the end of the header |
---|
| 47 | if ( strncmp(buf+28, "\001vorbis", 7) == 0 ) |
---|
| 48 | codec = ROAR_CODEC_OGG_VORBIS; |
---|
| 49 | } |
---|
[558] | 50 | } else if ( strncmp(buf, "MThd", 4) == 0 ) { |
---|
| 51 | codec = ROAR_CODEC_MIDI_FILE; |
---|
[563] | 52 | } else if ( strncmp(buf, "RIFF", 4) == 0 ) { |
---|
| 53 | if ( len > 15 ) { |
---|
| 54 | if ( strncmp(buf+8, "WAVEfmt ", 8) == 0 ) |
---|
| 55 | codec = ROAR_CODEC_RIFF_WAVE; |
---|
| 56 | } |
---|
[623] | 57 | } else if ( strncmp(buf, "Roar", 4) == 0 ) { |
---|
| 58 | if ( len > ROAR_SPEEX_MAGIC_LEN ) { |
---|
| 59 | if ( strncmp(buf, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) == 0 ) |
---|
| 60 | codec = ROAR_CODEC_ROAR_SPEEX; |
---|
[636] | 61 | #if ROAR_SPEEX_MAGIC_LEN < ROAR_CELT_MAGIC_LEN |
---|
| 62 | } |
---|
| 63 | if ( len > ROAR_CELT_MAGIC_LEN ) { |
---|
| 64 | #endif |
---|
| 65 | if ( strncmp(buf, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) == 0 ) |
---|
| 66 | codec = ROAR_CODEC_ROAR_CELT; |
---|
[623] | 67 | } |
---|
[215] | 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | return codec; |
---|
| 72 | } |
---|
| 73 | |
---|
[208] | 74 | ssize_t roar_file_send_raw (int out, int in) { |
---|
| 75 | ssize_t r = 0; |
---|
[701] | 76 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
[208] | 77 | ssize_t ret; |
---|
[701] | 78 | #endif |
---|
[208] | 79 | int len; |
---|
| 80 | char buf[BUFSIZE]; |
---|
[217] | 81 | #ifdef __linux__ |
---|
| 82 | int cork_new = 1, cork_old; |
---|
| 83 | socklen_t cork_len = sizeof(int); |
---|
| 84 | |
---|
[219] | 85 | if ( getsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, &cork_len) == -1 ) { |
---|
| 86 | cork_old = -1; |
---|
| 87 | } else { |
---|
| 88 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_new, sizeof(int)); |
---|
| 89 | } |
---|
[217] | 90 | #endif |
---|
[208] | 91 | |
---|
| 92 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
| 93 | while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0) |
---|
| 94 | r += ret; |
---|
| 95 | #endif |
---|
| 96 | |
---|
| 97 | // TODO: try mmap here! |
---|
| 98 | |
---|
| 99 | while ((len = read(in, buf, BUFSIZE)) > 0) |
---|
| 100 | r += write(out, buf, len); |
---|
| 101 | |
---|
[217] | 102 | #ifdef __linux__ |
---|
[219] | 103 | if ( cork_old != -1 ) |
---|
| 104 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len); |
---|
[217] | 105 | #endif |
---|
[208] | 106 | return r; |
---|
| 107 | } |
---|
| 108 | |
---|
[649] | 109 | ssize_t roar_file_map (char * filename, int flags, mode_t mode, size_t len, void ** mem) { |
---|
| 110 | int fh; |
---|
| 111 | int mmap_flags = 0; |
---|
| 112 | struct stat stat; |
---|
| 113 | |
---|
| 114 | if ( mem == NULL || filename == NULL ) |
---|
| 115 | return -1; |
---|
| 116 | |
---|
| 117 | *mem = NULL; |
---|
| 118 | |
---|
| 119 | if ( flags & O_RDWR ) { |
---|
| 120 | mmap_flags = PROT_READ|PROT_WRITE; |
---|
| 121 | } else if ( flags & O_WRONLY ) { |
---|
| 122 | mmap_flags = PROT_WRITE; |
---|
| 123 | } else { |
---|
| 124 | mmap_flags = PROT_READ; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | if ( (fh = open(filename, flags, mode)) == -1 ) { |
---|
| 128 | return -1; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if ( fstat(fh, &stat) == -1 ) { |
---|
| 132 | close(fh); |
---|
| 133 | return -1; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if ( stat.st_size < len ) { |
---|
| 137 | if ( ftruncate(fh, len) == -1 ) { |
---|
| 138 | close(fh); |
---|
| 139 | return -1; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | if ( (*mem = mmap(NULL, len, mmap_flags, MAP_SHARED, fh, 0)) == NULL ) { |
---|
| 144 | close(fh); |
---|
| 145 | return -1; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | close(fh); |
---|
| 149 | |
---|
| 150 | return len; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | int roar_file_unmap (size_t len, void * mem) { |
---|
| 154 | return munmap(mem, len); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
[215] | 158 | ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) { |
---|
| 159 | int codec = -1; |
---|
| 160 | int in, out = -1; |
---|
| 161 | struct roar_stream s; |
---|
| 162 | ssize_t r = 0; |
---|
| 163 | int len; |
---|
| 164 | char buf[BUFSIZE]; |
---|
| 165 | int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT; |
---|
| 166 | |
---|
| 167 | if ( !con ) |
---|
| 168 | return -1; |
---|
| 169 | |
---|
| 170 | if ( !file ) |
---|
| 171 | return -1; |
---|
| 172 | |
---|
| 173 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
| 174 | return -1; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | if ((len = read(in, buf, BUFSIZE)) < 1) { |
---|
| 178 | close(in); |
---|
| 179 | return -1; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | codec = roar_file_codecdetect(buf, len); |
---|
| 183 | |
---|
| 184 | if ( codec == -1 ) { |
---|
| 185 | close(in); |
---|
| 186 | return -1; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | if ( exec ) { |
---|
| 190 | if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) { |
---|
| 191 | close(in); |
---|
| 192 | return -1; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | if ( roar_stream_connect(con, &s, ROAR_DIR_PLAY) == -1 ) { |
---|
| 196 | close(in); |
---|
| 197 | return -1; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if ( roar_stream_exec(con, &s) == -1 ) { |
---|
| 201 | close(in); |
---|
| 202 | return -1; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | shutdown(con->fh, SHUT_RD); |
---|
| 206 | |
---|
| 207 | out = con->fh; |
---|
| 208 | } else { |
---|
| 209 | if ( (out = roar_simple_new_stream(con, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 210 | close(in); |
---|
| 211 | return -1; |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | write(out, buf, len); |
---|
| 216 | |
---|
| 217 | r = roar_file_send_raw(out, in); |
---|
| 218 | |
---|
| 219 | close(out); |
---|
| 220 | |
---|
| 221 | if ( exec ) { |
---|
| 222 | con->fh = -1; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | close(in); |
---|
| 226 | |
---|
| 227 | return r; |
---|
| 228 | } |
---|
| 229 | |
---|
[202] | 230 | //ll |
---|