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