source: roaraudio/roard/sources.c @ 1609:3c2a3f0fb127

Last change on this file since 1609:3c2a3f0fb127 was 1609:3c2a3f0fb127, checked in by phi, 15 years ago

started support for server config including default flags and mixer for individuel stream types/dirs

File size: 6.0 KB
Line 
1//sources.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard 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 *  RoarAudio 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 */
24
25#include "roard.h"
26
27int sources_init (void) {
28 g_source_client = -1;
29 return 0;
30}
31
32int sources_set_client (int client) {
33 if ( client >= 0 ) {
34  g_source_client = client;
35  return 0;
36 } else {
37  return -1;
38 }
39}
40
41int sources_free (void) {
42 return 0;
43}
44
45int sources_add (char * driver, char * device, char * container, char * options, int primary) {
46 if (0) {
47#ifdef ROAR_HAVE_IO_POSIX
48 } else if ( strcmp(driver, "raw") == 0 ) {
49  return sources_add_raw(driver, device, container, options, primary);
50 } else if ( strcmp(driver, "wav") == 0 ) {
51  return sources_add_wav(driver, device, container, options, primary);
52#endif
53 } else if ( strcmp(driver, "cf") == 0 ) {
54  return sources_add_cf(driver, device, container, options, primary);
55 } else if ( strcmp(driver, "roar") == 0 ) {
56  return sources_add_roar(driver, device, container, options, primary);
57 }
58
59 return -1;
60}
61
62#ifdef ROAR_HAVE_IO_POSIX
63int sources_add_raw (char * driver, char * device, char * container, char * options, int primary) {
64 int stream;
65 int fh;
66 struct roar_stream * s;
67
68 ROAR_WARN("sources_add_raw(*): The raw source is obsolete, use source 'cf' (default)!");
69
70 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
71  return -1;
72 }
73
74 if ( (stream = streams_new()) == -1 ) {
75  close(fh);
76  return -1;
77 }
78
79 streams_get(stream, (struct roar_stream_server **)&s);
80
81 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
82
83 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
84  streams_delete(stream);
85  close(fh);
86  return -1;
87 }
88
89 s->pos_rel_id = -1;
90
91 streams_set_fh(stream, fh);
92
93 streams_set_flag(stream, ROAR_FLAG_SOURCE);
94 client_stream_add(g_source_client, stream);
95
96 return 0;
97}
98#endif
99
100#ifdef ROAR_HAVE_IO_POSIX
101int sources_add_wav (char * driver, char * device, char * container, char * options, int primary) {
102 int stream;
103 int fh;
104 char buf[44];
105 struct roar_stream * s;
106
107 ROAR_WARN("sources_add_raw(*): The wav(e) source is obsolete, use source 'cf' (default)!");
108
109 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
110  return -1;
111 }
112
113 if (read(fh, buf, 44) != 44) {
114  close(fh);
115  return -1;
116 }
117
118 if ( (stream = streams_new()) == -1 ) {
119  close(fh);
120  return -1;
121 }
122
123 streams_get(stream, (struct roar_stream_server **)&s);
124
125 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
126
127 memcpy(&(s->info.rate    ), buf+24, 4);
128 memcpy(&(s->info.channels), buf+22, 2);
129 memcpy(&(s->info.bits    ), buf+34, 2);
130
131 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
132  streams_delete(stream);
133  close(fh);
134  return -1;
135 }
136 s->pos_rel_id = -1;
137
138 streams_set_fh(stream, fh);
139
140 streams_set_flag(stream, ROAR_FLAG_SOURCE);
141 client_stream_add(g_source_client, stream);
142
143 return 0;
144}
145#endif
146
147#define _ret(x) roar_vio_close(vio); streams_delete(stream); return (x)
148
149int sources_add_cf (char * driver, char * device, char * container, char * options, int primary) {
150 int  stream;
151 int  codec;
152 int  len;
153 char buf[64];
154 struct roar_stream    * s;
155 struct roar_vio_calls * vio;
156
157 if ( (stream = streams_new()) == -1 ) {
158  return -1;
159 }
160
161 streams_get(stream, (struct roar_stream_server **)&s);
162
163 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
164
165 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
166  streams_delete(stream);
167  return -1;
168 }
169
170 s->pos_rel_id = -1;
171
172/*
173 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
174  return -1;
175 }
176*/
177
178 vio = &(ROAR_STREAM_SERVER(s)->vio);
179
180 if ( roar_vio_open_file(vio, device, O_RDONLY, 0644) == -1 ) {
181  _ret(-1);
182 }
183
184 // TODO: finy out a better way of doing auto detetion without need for seek!
185 if ( options == NULL ) {
186  if ( (len = roar_vio_read(vio, buf, 64)) < 1 ) {
187   _ret(-1);
188  }
189
190  if ( roar_vio_lseek(vio, -len, SEEK_CUR) == (off_t)-1 ) {
191   _ret(-1);
192  }
193
194  if ( (codec = roar_file_codecdetect(buf, len)) == -1 ) {
195   _ret(-1);
196  }
197 } else {
198  if ( !strncmp(options, "codec=", 6) )
199   options += 6;
200
201  if ( (codec = roar_str2codec(options)) == -1 ) {
202   _ret(-1);
203  }
204 }
205
206 s->info.codec = codec;
207
208 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
209
210 streams_set_fh(stream, -2);
211 streams_set_socktype(stream, ROAR_SOCKET_TYPE_FILE);
212
213 if ( primary )
214  streams_mark_primary(stream);
215
216 streams_set_flag(stream, ROAR_FLAG_SOURCE);
217 client_stream_add(g_source_client, stream);
218
219 return 0;
220}
221
222#undef _ret
223
224
225int sources_add_roar (char * driver, char * device, char * container, char * options, int primary) {
226 int  stream;
227 int  fh;
228 int  codec = ROAR_CODEC_DEFAULT;
229 struct roar_stream * s;
230
231 if ( options != NULL && *options ) {
232  if ( !strncmp(options, "codec=", 6) )
233   options += 6;
234
235  if ( (codec = roar_str2codec(options)) == -1 ) {
236   return -1;
237  }
238 }
239
240 if ( (fh = roar_simple_monitor(g_sa->rate, g_sa->channels, g_sa->bits, codec, device, "roard")) == -1 ) {
241  return -1;
242 }
243
244 if ( (stream = streams_new()) == -1 ) {
245  close(fh);
246  return -1;
247 }
248
249 streams_get(stream, (struct roar_stream_server **)&s);
250
251 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
252
253 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
254  streams_delete(stream);
255  close(fh);
256  return -1;
257 }
258
259 s->pos_rel_id = -1;
260 s->info.codec = codec;
261
262 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
263
264 streams_set_fh(stream, fh);
265
266 if ( primary )
267  streams_mark_primary(stream);
268
269 streams_set_flag(stream, ROAR_FLAG_SOURCE);
270 client_stream_add(g_source_client, stream);
271
272 return 0;
273}
274
275//ll
Note: See TracBrowser for help on using the repository browser.