source: roaraudio/libroar/file.c @ 762:b0fafbce8cc5

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

added a more complete roar_file_play_full()

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