source: roaraudio/libroar/file.c @ 889:ac4b3785caa0

Last change on this file since 889:ac4b3785caa0 was 889:ac4b3785caa0, checked in by phi, 15 years ago

test for RAUM magic

File size: 6.2 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, 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
35#include "libroar.h"
36
37#define BUFSIZE 8192
38#define BUFMAX  65536
39
40int 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   }
50  } else if ( strncmp(buf, "MThd", 4) == 0 ) {
51   codec = ROAR_CODEC_MIDI_FILE;
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   }
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;
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;
67   }
68  } else if ( strncmp(buf, "RAUM-CF0", 8) == 0 ) {
69   codec = ROAR_CODEC_RAUM;
70  }
71 }
72
73 return codec;
74}
75
76ssize_t roar_file_send_raw (int out, int in) {
77 ssize_t r = 0;
78#ifdef ROAR_HAVE_LINUX_SENDFILE
79 ssize_t ret;
80#endif
81 int len;
82 char buf[BUFSIZE];
83#ifdef __linux__
84 int cork_new = 1, cork_old;
85 socklen_t cork_len = sizeof(int);
86
87 if ( getsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, &cork_len) == -1 ) {
88  cork_old = -1;
89 } else {
90  setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_new, sizeof(int));
91 }
92#endif
93
94#ifdef ROAR_HAVE_LINUX_SENDFILE
95 while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0)
96  r += ret;
97#endif
98
99 // TODO: try mmap here!
100
101 while ((len = read(in, buf, BUFSIZE)) > 0)
102  r += write(out, buf, len);
103
104#ifdef __linux__
105 if ( cork_old != -1 )
106  setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len);
107#endif
108 return r;
109}
110
111ssize_t     roar_file_map        (char * filename, int flags, mode_t mode, size_t len, void ** mem) {
112 int fh;
113 int mmap_flags = 0;
114 struct stat stat;
115
116 if ( mem == NULL || filename == NULL )
117  return -1;
118
119 *mem = NULL;
120
121 if ( flags & O_RDWR ) {
122  mmap_flags = PROT_READ|PROT_WRITE;
123 } else if ( flags & O_WRONLY ) {
124  mmap_flags = PROT_WRITE;
125 } else {
126  mmap_flags = PROT_READ;
127 }
128
129 if ( (fh = open(filename, flags, mode)) == -1 ) {
130  return -1;
131 }
132
133 if ( fstat(fh, &stat) == -1 ) {
134  close(fh);
135  return -1;
136 }
137
138 if ( stat.st_size < len ) {
139  if ( ftruncate(fh, len) == -1 ) {
140   close(fh);
141   return -1;
142  }
143 }
144
145 if ( (*mem = mmap(NULL, len, mmap_flags, MAP_SHARED, fh, 0)) == NULL ) {
146  close(fh);
147  return -1;
148 }
149
150 close(fh);
151
152 return len;
153}
154
155int     roar_file_unmap      (size_t len, void * mem) {
156 return munmap(mem, len);
157}
158
159
160ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) {
161 return roar_file_play_full(con, file, exec, 0, NULL);
162}
163
164ssize_t roar_file_play_full  (struct roar_connection * con, char * file, int exec, int passfh, struct roar_stream * s) {
165 int codec = -1;
166 int in, out = -1;
167 ssize_t r = 0;
168 int seek;
169 int len;
170 char buf[BUFSIZE];
171 int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT;
172 struct roar_stream localstream[1];
173
174 if ( !s )
175  s = localstream;
176
177 if ( !con )
178  return -1;
179
180 if ( !file )
181  return -1;
182
183 if ( exec && passfh )
184  return -1;
185
186 if ( (in = open(file, O_RDONLY, 0644)) == -1 ) {
187  return -1;
188 }
189
190 if ((len = read(in, buf, BUFSIZE)) < 1) {
191  close(in);
192  return -1;
193 }
194
195 codec = roar_file_codecdetect(buf, len);
196
197 seek = lseek(in, 0, SEEK_SET) == (off_t) -1 ? 0 : 1;
198
199 if ( codec == -1 ) {
200  close(in);
201  return -1;
202 }
203
204 if ( passfh && !seek ) {
205  ROAR_WARN("roar_file_play_full(*): passfh on non seekable file: this may produce incorrect playback");
206 }
207
208 if ( exec ) {
209  if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
210   close(in);
211   return -1;
212  }
213
214  if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) {
215   close(in);
216   return -1;
217  }
218
219  if ( roar_stream_exec(con, s) == -1 ) {
220   close(in);
221   return -1;
222  }
223
224  shutdown(con->fh, SHUT_RD);
225
226  out = con->fh;
227 } else {
228  if ( !(passfh && seek) ) {
229   if ( (out = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) {
230    close(in);
231    return -1;
232   }
233  } else {
234   if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
235    close(in);
236    return -1;
237   }
238
239   if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) {
240    close(in);
241    return -1;
242   }
243  }
244 }
245
246 if ( !seek )
247  write(out, buf, len);
248
249 if ( !passfh ) {
250  r = roar_file_send_raw(out, in);
251
252  close(out);
253
254  if ( exec ) {
255   con->fh = -1;
256  }
257
258  close(in);
259 } else {
260  if ( roar_stream_passfh(con, s, in) == -1 ) {
261   return -1;
262  }
263  close(out);
264  close(in);
265  return 0;
266 }
267
268 return r;
269}
270
271char  * roar_cdromdevice     (void) {
272 char * k;
273
274 if ( (k = getenv("CDDA_DEVICE")) != NULL )
275  return k;
276
277#ifdef ROAR_DEFAULT_CDROM
278 return ROAR_DEFAULT_CDROM;
279#endif
280
281 return NULL;
282}
283
284//ll
Note: See TracBrowser for help on using the repository browser.