source: roaraudio/roard/sources.c @ 1614:7d8deb4b9dfc

Last change on this file since 1614:7d8deb4b9dfc was 1614:7d8deb4b9dfc, checked in by phi, 15 years ago

do not double close a vio object

File size: 6.3 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) 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 struct roar_vio_defaults def;
157
158 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 )
159  return -1;
160
161 if ( (stream = streams_new()) == -1 ) {
162  return -1;
163 }
164
165 streams_get(stream, (struct roar_stream_server **)&s);
166
167 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
168
169 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
170  streams_delete(stream);
171  return -1;
172 }
173
174 s->pos_rel_id = -1;
175
176/*
177 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
178  return -1;
179 }
180*/
181
182 vio = &(ROAR_STREAM_SERVER(s)->vio);
183
184 //if ( roar_vio_open_file(vio, device, O_RDONLY, 0644) == -1 ) {
185 if ( roar_vio_open_dstr(vio, device, &def, 1) == -1 ) {
186  _ret(-1);
187 }
188
189 ROAR_DBG("sources_add_cf(*) = ?");
190
191 // TODO: finy out a better way of doing auto detetion without need for seek!
192 if ( options == NULL ) {
193  if ( (len = roar_vio_read(vio, buf, 64)) < 1 ) {
194   _ret(-1);
195  }
196
197  if ( roar_vio_lseek(vio, -len, SEEK_CUR) == (off_t)-1 ) {
198   _ret(-1);
199  }
200
201  if ( (codec = roar_file_codecdetect(buf, len)) == -1 ) {
202   _ret(-1);
203  }
204 } else {
205  if ( !strncmp(options, "codec=", 6) )
206   options += 6;
207
208  if ( (codec = roar_str2codec(options)) == -1 ) {
209   _ret(-1);
210  }
211 }
212
213 s->info.codec = codec;
214
215 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
216
217 ROAR_DBG("sources_add_cf(*) = ?");
218 streams_set_fh(stream, -2);
219 ROAR_DBG("sources_add_cf(*) = ?");
220 streams_set_socktype(stream, ROAR_SOCKET_TYPE_FILE);
221
222 if ( primary )
223  streams_mark_primary(stream);
224
225 streams_set_flag(stream, ROAR_FLAG_SOURCE);
226 client_stream_add(g_source_client, stream);
227
228 return 0;
229}
230
231#undef _ret
232
233
234int sources_add_roar (char * driver, char * device, char * container, char * options, int primary) {
235 int  stream;
236 int  fh;
237 int  codec = ROAR_CODEC_DEFAULT;
238 struct roar_stream * s;
239
240 if ( options != NULL && *options ) {
241  if ( !strncmp(options, "codec=", 6) )
242   options += 6;
243
244  if ( (codec = roar_str2codec(options)) == -1 ) {
245   return -1;
246  }
247 }
248
249 if ( (fh = roar_simple_monitor(g_sa->rate, g_sa->channels, g_sa->bits, codec, device, "roard")) == -1 ) {
250  return -1;
251 }
252
253 if ( (stream = streams_new()) == -1 ) {
254  close(fh);
255  return -1;
256 }
257
258 streams_get(stream, (struct roar_stream_server **)&s);
259
260 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
261
262 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
263  streams_delete(stream);
264  close(fh);
265  return -1;
266 }
267
268 s->pos_rel_id = -1;
269 s->info.codec = codec;
270
271 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
272
273 streams_set_fh(stream, fh);
274
275 if ( primary )
276  streams_mark_primary(stream);
277
278 streams_set_flag(stream, ROAR_FLAG_SOURCE);
279 client_stream_add(g_source_client, stream);
280
281 return 0;
282}
283
284//ll
Note: See TracBrowser for help on using the repository browser.