source: roaraudio/libroar/file.c @ 764:a38fe1677502

Last change on this file since 764:a38fe1677502 was 764:a38fe1677502, checked in by phi, 16 years ago

accept NULL as stream object

File size: 5.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 > 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 len;
167 char buf[BUFSIZE];
168 int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT;
169 struct roar_stream localstream[1];
170
171 if ( !s )
172  s = localstream;
173
174 if ( !con )
175  return -1;
176
177 if ( !file )
178  return -1;
179
180 if ( exec && passfh )
181  return -1;
182
183 if ( (in = open(file, O_RDONLY, 0644)) == -1 ) {
184  return -1;
185 }
186
187 if ((len = read(in, buf, BUFSIZE)) < 1) {
188  close(in);
189  return -1;
190 }
191
192 codec = roar_file_codecdetect(buf, len);
193
194 if ( codec == -1 ) {
195  close(in);
196  return -1;
197 }
198
199 if ( exec ) {
200  if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
201   close(in);
202   return -1;
203  }
204
205  if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) {
206   close(in);
207   return -1;
208  }
209
210  if ( roar_stream_exec(con, s) == -1 ) {
211   close(in);
212   return -1;
213  }
214
215  shutdown(con->fh, SHUT_RD);
216
217  out = con->fh;
218 } else {
219  if ( (out = roar_simple_new_stream(con, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) {
220   close(in);
221   return -1;
222  }
223 }
224
225 write(out, buf, len);
226
227 if ( !passfh ) {
228  r = roar_file_send_raw(out, in);
229
230  close(out);
231
232  if ( exec ) {
233   con->fh = -1;
234  }
235
236  close(in);
237 } else {
238  if ( roar_stream_passfh(con, s, in) == -1 ) {
239   return -1;
240  }
241  close(out);
242  close(in);
243  return 0;
244 }
245
246 return r;
247}
248
249//ll
Note: See TracBrowser for help on using the repository browser.