source: roaraudio/roard/sources.c @ 1186:b7ba4a4d62bd

Last change on this file since 1186:b7ba4a4d62bd was 1186:b7ba4a4d62bd, checked in by phi, 15 years ago

changed sources interface a bit: added support for an optional codec=-prefix for the codes given via -sO to be more like -oO

File size: 5.5 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 ( strcmp(driver, "raw") == 0 ) {
47  return sources_add_raw(driver, device, container, options, primary);
48 } else if ( strcmp(driver, "wav") == 0 ) {
49  return sources_add_wav(driver, device, container, options, primary);
50 } else if ( strcmp(driver, "cf") == 0 ) {
51  return sources_add_cf(driver, device, container, options, primary);
52 } else if ( strcmp(driver, "roar") == 0 ) {
53  return sources_add_roar(driver, device, container, options, primary);
54 }
55
56 return -1;
57}
58
59int sources_add_raw (char * driver, char * device, char * container, char * options, int primary) {
60 int stream;
61 int fh;
62 struct roar_stream * s;
63
64 ROAR_WARN("sources_add_raw(*): The raw source is obsolete, use source 'cf' (default)!");
65
66 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
67  return -1;
68 }
69
70 if ( (stream = streams_new()) == -1 ) {
71  close(fh);
72  return -1;
73 }
74
75 streams_get(stream, (struct roar_stream_server **)&s);
76
77 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
78
79 s->dir        = ROAR_DIR_PLAY;
80 s->pos_rel_id = -1;
81
82 streams_set_fh(stream, fh);
83
84 streams_set_flag(stream, ROAR_FLAG_SOURCE);
85 client_stream_add(g_source_client, stream);
86
87 return 0;
88}
89
90int sources_add_wav (char * driver, char * device, char * container, char * options, int primary) {
91 int stream;
92 int fh;
93 char buf[44];
94 struct roar_stream * s;
95
96 ROAR_WARN("sources_add_raw(*): The wav(e) source is obsolete, use source 'cf' (default)!");
97
98 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
99  return -1;
100 }
101
102 if (read(fh, buf, 44) != 44) {
103  close(fh);
104  return -1;
105 }
106
107 if ( (stream = streams_new()) == -1 ) {
108  close(fh);
109  return -1;
110 }
111
112 streams_get(stream, (struct roar_stream_server **)&s);
113
114 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
115
116  memcpy(&(s->info.rate    ), buf+24, 4);
117  memcpy(&(s->info.channels), buf+22, 2);
118  memcpy(&(s->info.bits    ), buf+34, 2);
119
120 s->dir        = ROAR_DIR_PLAY;
121 s->pos_rel_id = -1;
122
123 streams_set_fh(stream, fh);
124
125 streams_set_flag(stream, ROAR_FLAG_SOURCE);
126 client_stream_add(g_source_client, stream);
127
128 return 0;
129}
130
131int sources_add_cf (char * driver, char * device, char * container, char * options, int primary) {
132 int  stream;
133 int  fh;
134 int  codec;
135 int  len;
136 char buf[64];
137 struct roar_stream * s;
138
139 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
140  return -1;
141 }
142
143 // TODO: finy out a better way of doing auto detetion without need for seek!
144 if ( options == NULL ) {
145  if ( (len = read(fh, buf, 64)) < 1 ) {
146   close(fh);
147   return -1;
148  }
149
150  if ( lseek(fh, -len, SEEK_CUR) == (off_t)-1 ) {
151   close(fh);
152   return -1;
153  }
154
155  if ( (codec = roar_file_codecdetect(buf, len)) == -1 ) {
156   close(fh);
157   return -1;
158  }
159 } else {
160  if ( !strncmp(options, "codec=", 6) )
161   options += 6;
162
163  if ( (codec = roar_str2codec(options)) == -1 ) {
164   close(fh);
165   return -1;
166  }
167 }
168
169 if ( (stream = streams_new()) == -1 ) {
170  close(fh);
171  return -1;
172 }
173
174 streams_get(stream, (struct roar_stream_server **)&s);
175
176 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
177
178 s->dir        = ROAR_DIR_PLAY;
179 s->pos_rel_id = -1;
180 s->info.codec = codec;
181
182 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
183
184 streams_set_fh(stream, fh);
185 streams_set_socktype(stream, ROAR_SOCKET_TYPE_FILE);
186
187 if ( primary )
188  streams_mark_primary(stream);
189
190 streams_set_flag(stream, ROAR_FLAG_SOURCE);
191 client_stream_add(g_source_client, stream);
192
193 return 0;
194}
195
196int sources_add_roar (char * driver, char * device, char * container, char * options, int primary) {
197 int  stream;
198 int  fh;
199 int  codec = ROAR_CODEC_DEFAULT;
200 struct roar_stream * s;
201
202 if ( options != NULL && *options ) {
203  if ( !strncmp(options, "codec=", 6) )
204   options += 6;
205
206  if ( (codec = roar_str2codec(options)) == -1 ) {
207   return -1;
208  }
209 }
210
211 if ( (fh = roar_simple_monitor(g_sa->rate, g_sa->channels, g_sa->bits, codec, device, "roard")) == -1 ) {
212  return -1;
213 }
214
215 if ( (stream = streams_new()) == -1 ) {
216  close(fh);
217  return -1;
218 }
219
220 streams_get(stream, (struct roar_stream_server **)&s);
221
222 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
223
224 s->dir        = ROAR_DIR_PLAY;
225 s->pos_rel_id = -1;
226 s->info.codec = codec;
227
228 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
229
230 streams_set_fh(stream, fh);
231
232 if ( primary )
233  streams_mark_primary(stream);
234
235 streams_set_flag(stream, ROAR_FLAG_SOURCE);
236 client_stream_add(g_source_client, stream);
237
238 return 0;
239}
240
241//ll
Note: See TracBrowser for help on using the repository browser.