source: roaraudio/libroar/file.c @ 3857:8ed6d81a32d6

Last change on this file since 3857:8ed6d81a32d6 was 3857:8ed6d81a32d6, checked in by phi, 14 years ago

use roar_debug_warn_obsolete() and roar_libroar_*warn()

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