source: roaraudio/libroar/file.c @ 1078:fa6ec67f156f

Last change on this file since 1078:fa6ec67f156f was 1000:43afe5f9b605, checked in by phi, 15 years ago

added Ogg FLAC and Ogg Speex magic

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