source: roaraudio/libroar/file.c @ 623:13261b0cbc5a

Last change on this file since 623:13261b0cbc5a was 623:13261b0cbc5a, checked in by phi, 16 years ago

added RoarSpeex? support

File size: 2.7 KB
Line 
1//file.c:
2
3#include "libroar.h"
4
5#define BUFSIZE 8192
6#define BUFMAX  65536
7
8int roar_file_codecdetect(char * buf, int len) {
9 int codec = -1;
10
11 if ( len > 3 ) {
12  if ( strncmp(buf, "OggS", 4) == 0 ) {
13   codec = ROAR_CODEC_OGG_GENERAL;
14   if ( len > 34 ) { // this is 7 bytes after the end of the header
15    if ( strncmp(buf+28, "\001vorbis", 7) == 0 )
16     codec = ROAR_CODEC_OGG_VORBIS;
17   }
18  } else if ( strncmp(buf, "MThd", 4) == 0 ) {
19   codec = ROAR_CODEC_MIDI_FILE;
20  } else if ( strncmp(buf, "RIFF", 4) == 0 ) {
21   if ( len > 15 ) {
22    if ( strncmp(buf+8, "WAVEfmt ", 8) == 0 )
23     codec = ROAR_CODEC_RIFF_WAVE;
24   }
25  } else if ( strncmp(buf, "Roar", 4) == 0 ) {
26   if ( len > ROAR_SPEEX_MAGIC_LEN ) {
27    if ( strncmp(buf, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) == 0 )
28     codec = ROAR_CODEC_ROAR_SPEEX;
29   }
30  }
31 }
32
33 return codec;
34}
35
36ssize_t roar_file_send_raw (int out, int in) {
37 ssize_t r = 0;
38 ssize_t ret;
39 int len;
40 char buf[BUFSIZE];
41#ifdef __linux__
42 int cork_new = 1, cork_old;
43 socklen_t cork_len = sizeof(int);
44
45 if ( getsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, &cork_len) == -1 ) {
46  cork_old = -1;
47 } else {
48  setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_new, sizeof(int));
49 }
50#endif
51
52#ifdef ROAR_HAVE_LINUX_SENDFILE
53 while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0)
54  r += ret;
55#endif
56
57 // TODO: try mmap here!
58
59 while ((len = read(in, buf, BUFSIZE)) > 0)
60  r += write(out, buf, len);
61
62#ifdef __linux__
63 if ( cork_old != -1 )
64  setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len);
65#endif
66 return r;
67}
68
69ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) {
70 int codec = -1;
71 int in, out = -1;
72 struct roar_stream s;
73 ssize_t r = 0;
74 int len;
75 char buf[BUFSIZE];
76 int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT;
77
78 if ( !con )
79  return -1;
80
81 if ( !file )
82  return -1;
83
84 if ( (in = open(file, O_RDONLY, 0644)) == -1 ) {
85  return -1;
86 }
87
88 if ((len = read(in, buf, BUFSIZE)) < 1) {
89  close(in);
90  return -1;
91 }
92
93 codec = roar_file_codecdetect(buf, len);
94
95 if ( codec == -1 ) {
96  close(in);
97  return -1;
98 }
99
100 if ( exec ) {
101  if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) {
102   close(in);
103   return -1;
104  }
105
106  if ( roar_stream_connect(con, &s, ROAR_DIR_PLAY) == -1 ) {
107   close(in);
108   return -1;
109  }
110
111  if ( roar_stream_exec(con, &s) == -1 ) {
112   close(in);
113   return -1;
114  }
115
116  shutdown(con->fh, SHUT_RD);
117
118  out = con->fh;
119 } else {
120  if ( (out = roar_simple_new_stream(con, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) {
121   close(in);
122   return -1;
123  }
124 }
125
126 write(out, buf, len);
127
128 r = roar_file_send_raw(out, in);
129
130 close(out);
131
132 if ( exec ) {
133  con->fh = -1;
134 }
135
136 close(in);
137
138 return r;
139}
140
141//ll
Note: See TracBrowser for help on using the repository browser.