source: roaraudio/libroar/file.c @ 3934:224c9cf406db

Last change on this file since 3934:224c9cf406db was 3857:8ed6d81a32d6, checked in by phi, 14 years ago

use roar_debug_warn_obsolete() and roar_libroar_*warn()

File size: 8.3 KB
RevLine 
[202]1//file.c:
2
[690]3/*
[3811]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
[690]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
[202]36#include "libroar.h"
37
[208]38#define BUFSIZE 8192
39#define BUFMAX  65536
40
[1474]41#ifdef ROAR_HAVE_IO_POSIX
42#define _CAN_OPERATE
43#endif
44
[215]45int roar_file_codecdetect(char * buf, int len) {
46 int codec = -1;
47
48 if ( len > 3 ) {
49  if ( strncmp(buf, "OggS", 4) == 0 ) {
50   codec = ROAR_CODEC_OGG_GENERAL;
[1000]51   if ( len > 32 ) { // this is 5 bytes after the end of the header
52    if ( strncmp(buf+28, "\177FLAC", 5) == 0 ) {
53     codec = ROAR_CODEC_OGG_FLAC;
54    } else if ( strncmp(buf+28, "Speex", 5) == 0 ) {
55     codec = ROAR_CODEC_OGG_SPEEX;
56    } else if ( len > 34 ) { // this is 7 bytes after the end of the header
57     if ( strncmp(buf+28, "\001vorbis", 7) == 0 )
58      codec = ROAR_CODEC_OGG_VORBIS;
59    }
[215]60   }
[558]61  } else if ( strncmp(buf, "MThd", 4) == 0 ) {
62   codec = ROAR_CODEC_MIDI_FILE;
[563]63  } else if ( strncmp(buf, "RIFF", 4) == 0 ) {
64   if ( len > 15 ) {
65    if ( strncmp(buf+8, "WAVEfmt ", 8) == 0 )
66     codec = ROAR_CODEC_RIFF_WAVE;
67   }
[623]68  } else if ( strncmp(buf, "Roar", 4) == 0 ) {
69   if ( len > ROAR_SPEEX_MAGIC_LEN ) {
70    if ( strncmp(buf, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) == 0 )
71     codec = ROAR_CODEC_ROAR_SPEEX;
[636]72#if ROAR_SPEEX_MAGIC_LEN < ROAR_CELT_MAGIC_LEN
73   }
74   if ( len > ROAR_CELT_MAGIC_LEN ) {
75#endif
76    if ( strncmp(buf, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) == 0 )
77     codec = ROAR_CODEC_ROAR_CELT;
[623]78   }
[1196]79  } else if ( strncmp(buf, "fLaC", 4) == 0 ) {
80   codec = ROAR_CODEC_FLAC;
81  } else if ( len > 7 && strncmp(buf, "RAUM-CF0", 8) == 0 ) {
[889]82   codec = ROAR_CODEC_RAUM;
[215]83  }
84 }
85
86 return codec;
87}
88
[208]89ssize_t roar_file_send_raw (int out, int in) {
[1474]90#ifdef _CAN_OPERATE
[208]91 ssize_t r = 0;
[701]92#ifdef ROAR_HAVE_LINUX_SENDFILE
[208]93 ssize_t ret;
[701]94#endif
[208]95 int len;
96 char buf[BUFSIZE];
[1378]97#if defined(__linux__) && defined(ROAR_HAVE_IPV4)
[217]98 int cork_new = 1, cork_old;
99 socklen_t cork_len = sizeof(int);
100
[219]101 if ( getsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, &cork_len) == -1 ) {
102  cork_old = -1;
103 } else {
104  setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_new, sizeof(int));
105 }
[217]106#endif
[208]107
[3857]108 roar_debug_warn_obsolete("roar_file_send_raw", "roar_vio_copy_data", NULL);
109
[208]110#ifdef ROAR_HAVE_LINUX_SENDFILE
111 while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0)
112  r += ret;
113#endif
114
115 // TODO: try mmap here!
116
117 while ((len = read(in, buf, BUFSIZE)) > 0)
118  r += write(out, buf, len);
119
[1379]120#if defined(__linux__) && defined(ROAR_HAVE_IPV4)
[219]121 if ( cork_old != -1 )
122  setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len);
[217]123#endif
[208]124 return r;
[1474]125#else
126 return -1;
127#endif
[208]128}
129
[649]130ssize_t     roar_file_map        (char * filename, int flags, mode_t mode, size_t len, void ** mem) {
[1442]131#ifdef ROAR_HAVE_MMAP
[649]132 int fh;
133 int mmap_flags = 0;
134 struct stat stat;
135
136 if ( mem == NULL || filename == NULL )
137  return -1;
138
139 *mem = NULL;
140
141 if ( flags & O_RDWR ) {
142  mmap_flags = PROT_READ|PROT_WRITE;
143 } else if ( flags & O_WRONLY ) {
144  mmap_flags = PROT_WRITE;
145 } else {
146  mmap_flags = PROT_READ;
147 }
148
149 if ( (fh = open(filename, flags, mode)) == -1 ) {
150  return -1;
151 }
152
153 if ( fstat(fh, &stat) == -1 ) {
154  close(fh);
155  return -1;
156 }
157
158 if ( stat.st_size < len ) {
159  if ( ftruncate(fh, len) == -1 ) {
160   close(fh);
161   return -1;
162  }
163 }
164
165 if ( (*mem = mmap(NULL, len, mmap_flags, MAP_SHARED, fh, 0)) == NULL ) {
166  close(fh);
167  return -1;
168 }
169
170 close(fh);
171
172 return len;
[1081]173#else
[1442]174#ifdef ROAR_TARGET_WIN32
[1081]175 ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS...");
[1442]176#endif
[1081]177 return -1;
178#endif
[649]179}
180
181int     roar_file_unmap      (size_t len, void * mem) {
[1442]182#ifdef ROAR_HAVE_MMAP
[649]183 return munmap(mem, len);
[1081]184#else
[1442]185#ifdef ROAR_TARGET_WIN32
[1081]186 ROAR_ERR("roar_file_map(*): There is no support to fast access files via mmap() within win32, download a real OS...");
[1442]187#endif
[1081]188 return -1;
189#endif
[649]190}
191
192
[215]193ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) {
[764]194 return roar_file_play_full(con, file, exec, 0, NULL);
[762]195}
196
197ssize_t roar_file_play_full  (struct roar_connection * con, char * file, int exec, int passfh, struct roar_stream * s) {
[1474]198#ifdef _CAN_OPERATE
[215]199 int codec = -1;
200 int in, out = -1;
201 ssize_t r = 0;
[776]202 int seek;
[215]203 int len;
204 char buf[BUFSIZE];
205 int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT;
[764]206 struct roar_stream localstream[1];
207
[1660]208 // FIXME: check error cases
209
[1767]210 ROAR_DBG("roar_file_play_full(*) = ?");
211
[764]212 if ( !s )
213  s = localstream;
[215]214
215 if ( !con )
216  return -1;
217
218 if ( !file )
219  return -1;
220
[762]221 if ( exec && passfh )
222  return -1;
223
[1765]224#ifdef ROAR_TARGET_WIN32
225 if ( (in = open(file, O_RDONLY|O_BINARY, 0644)) == -1 ) {
226#else
[215]227 if ( (in = open(file, O_RDONLY, 0644)) == -1 ) {
[1765]228#endif
[215]229  return -1;
230 }
231
232 if ((len = read(in, buf, BUFSIZE)) < 1) {
233  close(in);
234  return -1;
235 }
236
237 codec = roar_file_codecdetect(buf, len);
238
[1000]239 ROAR_DBG("roar_file_play_full(*): codec=%i(%s)", codec, roar_codec2str(codec));
240
[776]241 seek = lseek(in, 0, SEEK_SET) == (off_t) -1 ? 0 : 1;
242
[215]243 if ( codec == -1 ) {
[1766]244  ROAR_ERR("roar_file_play_full(*): Unknown codec of file: %s", file);
[215]245  close(in);
246  return -1;
247 }
248
[776]249 if ( passfh && !seek ) {
250  ROAR_WARN("roar_file_play_full(*): passfh on non seekable file: this may produce incorrect playback");
251 }
252
[215]253 if ( exec ) {
[762]254  if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
[1766]255   ROAR_ERR("roar_file_play_full(*): Can not create new stream. This is realy BAD!");
[215]256   close(in);
257   return -1;
258  }
259
[762]260  if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) {
[1766]261   ROAR_ERR("roar_file_play_full(*): Can not connect new stream to server.");
[215]262   close(in);
263   return -1;
264  }
265
[762]266  if ( roar_stream_exec(con, s) == -1 ) {
[1766]267   ROAR_ERR("roar_file_play_full(*): Can not exec new stream on server.");
[215]268   close(in);
269   return -1;
270  }
271
[3857]272  roar_libroar_nowarn();
[1660]273  if ( (out = roar_get_connection_fh(con)) == -1 ) {
[1766]274   ROAR_ERR("roar_file_play_full(*): Can not get socket of server connection for exec data transmition.");
[1660]275   close(in);
[3857]276   roar_libroar_warn();
[1660]277   return -1;
278  }
[3857]279  roar_libroar_warn();
[215]280
[1660]281  ROAR_SHUTDOWN(out, SHUT_RD);
[215]282 } else {
[776]283  if ( !(passfh && seek) ) {
284   if ( (out = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) {
285    close(in);
286    return -1;
287   }
288  } else {
289   if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
290    close(in);
291    return -1;
292   }
293
294   if ( roar_stream_connect(con, s, ROAR_DIR_PLAY) == -1 ) {
295    close(in);
296    return -1;
297   }
[215]298  }
299 }
300
[776]301 if ( !seek )
[1765]302  ROAR_NETWORK_WRITE(out, buf, len);
[215]303
[762]304 if ( !passfh ) {
[1765]305#ifndef ROAR_TARGET_WIN32
[762]306  r = roar_file_send_raw(out, in);
[215]307
[762]308  close(out);
[1765]309#else
310 while ((len = read(in, buf, BUFSIZE)) > 0)
311  if ( send(out, buf, len, 0) != len )
312   break;
313
314 closesocket(out);
315#endif
[762]316
317  if ( exec ) {
[1315]318   // TODO: FIXME: this ma cause a memory leak in future
319   // OLD: con->fh = -1;
320   roar_connect_fh(con, -2);
[762]321  }
[215]322
[762]323  close(in);
324 } else {
325  if ( roar_stream_passfh(con, s, in) == -1 ) {
326   return -1;
327  }
328  close(out);
329  close(in);
330  return 0;
[215]331 }
332
333 return r;
[1474]334#else
335 return -1;
336#endif
[215]337}
338
[805]339char  * roar_cdromdevice     (void) {
340 char * k;
341
342 if ( (k = getenv("CDDA_DEVICE")) != NULL )
343  return k;
344
345#ifdef ROAR_DEFAULT_CDROM
346 return ROAR_DEFAULT_CDROM;
347#endif
348
349 return NULL;
350}
351
[202]352//ll
Note: See TracBrowser for help on using the repository browser.