source: roaraudio/libroar/file.c @ 776:1d9ce44598b2

Last change on this file since 776:1d9ce44598b2 was 776:1d9ce44598b2, checked in by phi, 16 years ago

better support passfh on seekable streams

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