source: roaraudio/libroar/file.c @ 3517:1a3218a3fc5b

Last change on this file since 3517:1a3218a3fc5b was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

File size: 8.1 KB
Line 
1//file.c:
2
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, 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 BUFSIZE 8192
39#define BUFMAX  65536
40
41#ifdef ROAR_HAVE_IO_POSIX
42#define _CAN_OPERATE
43#endif
44
45int 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, ROAR_CELT_MAGIC_LEN) == 0 )
77     codec = ROAR_CODEC_ROAR_CELT;
78   }
79  } else if ( strncmp(buf, "fLaC", 4) == 0 ) {
80   codec = ROAR_CODEC_FLAC;
81  } else if ( len > 7 && strncmp(buf, "RAUM-CF0", 8) == 0 ) {
82   codec = ROAR_CODEC_RAUM;
83  }
84 }
85
86 return codec;
87}
88
89ssize_t roar_file_send_raw (int out, int in) {
90#ifdef _CAN_OPERATE
91 ssize_t r = 0;
92#ifdef ROAR_HAVE_LINUX_SENDFILE
93 ssize_t ret;
94#endif
95 int len;
96 char buf[BUFSIZE];
97#if defined(__linux__) && defined(ROAR_HAVE_IPV4)
98 int cork_new = 1, cork_old;
99 socklen_t cork_len = sizeof(int);
100
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 }
106#endif
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
118#if defined(__linux__) && defined(ROAR_HAVE_IPV4)
119 if ( cork_old != -1 )
120  setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len);
121#endif
122 return r;
123#else
124 return -1;
125#endif
126}
127
128ssize_t     roar_file_map        (char * filename, int flags, mode_t mode, size_t len, void ** mem) {
129#ifdef ROAR_HAVE_MMAP
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;
171#else
172#ifdef ROAR_TARGET_WIN32
173 ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS...");
174#endif
175 return -1;
176#endif
177}
178
179int     roar_file_unmap      (size_t len, void * mem) {
180#ifdef ROAR_HAVE_MMAP
181 return munmap(mem, len);
182#else
183#ifdef ROAR_TARGET_WIN32
184 ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS...");
185#endif
186 return -1;
187#endif
188}
189
190
191ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) {
192 return roar_file_play_full(con, file, exec, 0, NULL);
193}
194
195ssize_t roar_file_play_full  (struct roar_connection * con, char * file, int exec, int passfh, struct roar_stream * s) {
196#ifdef _CAN_OPERATE
197 int codec = -1;
198 int in, out = -1;
199 ssize_t r = 0;
200 int seek;
201 int len;
202 char buf[BUFSIZE];
203 int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT;
204 struct roar_stream localstream[1];
205
206 // FIXME: check error cases
207
208 ROAR_DBG("roar_file_play_full(*) = ?");
209
210 if ( !s )
211  s = localstream;
212
213 if ( !con )
214  return -1;
215
216 if ( !file )
217  return -1;
218
219 if ( exec && passfh )
220  return -1;
221
222#ifdef ROAR_TARGET_WIN32
223 if ( (in = open(file, O_RDONLY|O_BINARY, 0644)) == -1 ) {
224#else
225 if ( (in = open(file, O_RDONLY, 0644)) == -1 ) {
226#endif
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
237 ROAR_DBG("roar_file_play_full(*): codec=%i(%s)", codec, roar_codec2str(codec));
238
239 seek = lseek(in, 0, SEEK_SET) == (off_t) -1 ? 0 : 1;
240
241 if ( codec == -1 ) {
242  ROAR_ERR("roar_file_play_full(*): Unknown codec of file: %s", file);
243  close(in);
244  return -1;
245 }
246
247 if ( passfh && !seek ) {
248  ROAR_WARN("roar_file_play_full(*): passfh on non seekable file: this may produce incorrect playback");
249 }
250
251 if ( exec ) {
252  if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
253   ROAR_ERR("roar_file_play_full(*): Can not create new stream. This is realy BAD!");
254   close(in);
255   return -1;
256  }
257
258  if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) {
259   ROAR_ERR("roar_file_play_full(*): Can not connect new stream to server.");
260   close(in);
261   return -1;
262  }
263
264  if ( roar_stream_exec(con, s) == -1 ) {
265   ROAR_ERR("roar_file_play_full(*): Can not exec new stream on server.");
266   close(in);
267   return -1;
268  }
269
270  if ( (out = roar_get_connection_fh(con)) == -1 ) {
271   ROAR_ERR("roar_file_play_full(*): Can not get socket of server connection for exec data transmition.");
272   close(in);
273   return -1;
274  }
275
276  ROAR_SHUTDOWN(out, SHUT_RD);
277 } else {
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   }
293  }
294 }
295
296 if ( !seek )
297  ROAR_NETWORK_WRITE(out, buf, len);
298
299 if ( !passfh ) {
300#ifndef ROAR_TARGET_WIN32
301  r = roar_file_send_raw(out, in);
302
303  close(out);
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
311
312  if ( exec ) {
313   // TODO: FIXME: this ma cause a memory leak in future
314   // OLD: con->fh = -1;
315   roar_connect_fh(con, -2);
316  }
317
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;
326 }
327
328 return r;
329#else
330 return -1;
331#endif
332}
333
334char  * 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
347//ll
Note: See TracBrowser for help on using the repository browser.