source: roaraudio/roard/sources.c @ 1608:41f671214d02

Last change on this file since 1608:41f671214d02 was 1504:d00454bcfd80, checked in by phi, 15 years ago

use fh=-2 as specal *read me* info

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