source: roaraudio/libroar/file.c @ 690:cee9bf5fa456

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

added copyright statements

File size: 5.0 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 ssize_t ret;
77 int len;
78 char buf[BUFSIZE];
79#ifdef __linux__
80 int cork_new = 1, cork_old;
81 socklen_t cork_len = sizeof(int);
82
83 if ( getsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, &cork_len) == -1 ) {
84  cork_old = -1;
85 } else {
86  setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_new, sizeof(int));
87 }
88#endif
89
90#ifdef ROAR_HAVE_LINUX_SENDFILE
91 while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0)
92  r += ret;
93#endif
94
95 // TODO: try mmap here!
96
97 while ((len = read(in, buf, BUFSIZE)) > 0)
98  r += write(out, buf, len);
99
100#ifdef __linux__
101 if ( cork_old != -1 )
102  setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len);
103#endif
104 return r;
105}
106
107ssize_t     roar_file_map        (char * filename, int flags, mode_t mode, size_t len, void ** mem) {
108 int fh;
109 int mmap_flags = 0;
110 struct stat stat;
111
112 if ( mem == NULL || filename == NULL )
113  return -1;
114
115 *mem = NULL;
116
117 if ( flags & O_RDWR ) {
118  mmap_flags = PROT_READ|PROT_WRITE;
119 } else if ( flags & O_WRONLY ) {
120  mmap_flags = PROT_WRITE;
121 } else {
122  mmap_flags = PROT_READ;
123 }
124
125 if ( (fh = open(filename, flags, mode)) == -1 ) {
126  return -1;
127 }
128
129 if ( fstat(fh, &stat) == -1 ) {
130  close(fh);
131  return -1;
132 }
133
134 if ( stat.st_size < len ) {
135  if ( ftruncate(fh, len) == -1 ) {
136   close(fh);
137   return -1;
138  }
139 }
140
141 if ( (*mem = mmap(NULL, len, mmap_flags, MAP_SHARED, fh, 0)) == NULL ) {
142  close(fh);
143  return -1;
144 }
145
146 close(fh);
147
148 return len;
149}
150
151int     roar_file_unmap      (size_t len, void * mem) {
152 return munmap(mem, len);
153}
154
155
156ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) {
157 int codec = -1;
158 int in, out = -1;
159 struct roar_stream s;
160 ssize_t r = 0;
161 int len;
162 char buf[BUFSIZE];
163 int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT;
164
165 if ( !con )
166  return -1;
167
168 if ( !file )
169  return -1;
170
171 if ( (in = open(file, O_RDONLY, 0644)) == -1 ) {
172  return -1;
173 }
174
175 if ((len = read(in, buf, BUFSIZE)) < 1) {
176  close(in);
177  return -1;
178 }
179
180 codec = roar_file_codecdetect(buf, len);
181
182 if ( codec == -1 ) {
183  close(in);
184  return -1;
185 }
186
187 if ( exec ) {
188  if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) {
189   close(in);
190   return -1;
191  }
192
193  if ( roar_stream_connect(con, &s, ROAR_DIR_PLAY) == -1 ) {
194   close(in);
195   return -1;
196  }
197
198  if ( roar_stream_exec(con, &s) == -1 ) {
199   close(in);
200   return -1;
201  }
202
203  shutdown(con->fh, SHUT_RD);
204
205  out = con->fh;
206 } else {
207  if ( (out = roar_simple_new_stream(con, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) {
208   close(in);
209   return -1;
210  }
211 }
212
213 write(out, buf, len);
214
215 r = roar_file_send_raw(out, in);
216
217 close(out);
218
219 if ( exec ) {
220  con->fh = -1;
221 }
222
223 close(in);
224
225 return r;
226}
227
228//ll
Note: See TracBrowser for help on using the repository browser.