source: roaraudio/roard/sources.c @ 556:24413e9e6310

Last change on this file since 556:24413e9e6310 was 556:24413e9e6310, checked in by phi, 16 years ago

added codec auto detection code for source cf and added roar_file_codecdetect() proto type

File size: 3.0 KB
Line 
1//sources.c:
2
3#include "roard.h"
4
5int sources_init (void) {
6 g_source_client = -1;
7 return 0;
8}
9
10int sources_set_client (int client) {
11 if ( client >= 0 ) {
12  g_source_client = client;
13  return 0;
14 } else {
15  return -1;
16 }
17}
18
19int sources_free (void) {
20 return 0;
21}
22
23int sources_add (char * driver, char * device, char * container, char * options, int primary) {
24 if ( strcmp(driver, "raw") == 0 ) {
25  return sources_add_raw(driver, device, container, options, primary);
26 } else if ( strcmp(driver, "wav") == 0 ) {
27  return sources_add_wav(driver, device, container, options, primary);
28 } else if ( strcmp(driver, "cf") == 0 ) {
29  return sources_add_cf(driver, device, container, options, primary);
30 }
31
32 return -1;
33}
34
35int sources_add_raw (char * driver, char * device, char * container, char * options, int primary) {
36 int stream;
37 int fh;
38 struct roar_stream * s;
39
40 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
41  return -1;
42 }
43
44 if ( (stream = streams_new()) == -1 ) {
45  close(fh);
46  return -1;
47 }
48
49 streams_get(stream, (struct roar_stream_server **)&s);
50
51 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
52
53 s->dir        = ROAR_DIR_PLAY;
54 s->pos_rel_id = -1;
55
56 streams_set_fh(stream, fh);
57
58 client_stream_add(g_source_client, stream);
59
60 return 0;
61}
62
63int sources_add_wav (char * driver, char * device, char * container, char * options, int primary) {
64 int stream;
65 int fh;
66 char buf[44];
67 struct roar_stream * s;
68
69 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
70  return -1;
71 }
72
73 if (read(fh, buf, 44) != 44) {
74  close(fh);
75  return -1;
76 }
77
78 if ( (stream = streams_new()) == -1 ) {
79  close(fh);
80  return -1;
81 }
82
83 streams_get(stream, (struct roar_stream_server **)&s);
84
85 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
86
87  memcpy(&(s->info.rate    ), buf+24, 4);
88  memcpy(&(s->info.channels), buf+22, 2);
89  memcpy(&(s->info.bits    ), buf+34, 2);
90
91 s->dir        = ROAR_DIR_PLAY;
92 s->pos_rel_id = -1;
93
94 streams_set_fh(stream, fh);
95
96 client_stream_add(g_source_client, stream);
97
98 return 0;
99}
100
101int sources_add_cf (char * driver, char * device, char * container, char * options, int primary) {
102 int  stream;
103 int  fh;
104 int  codec;
105 int  len;
106 char buf[64];
107 struct roar_stream * s;
108
109 if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) {
110  return -1;
111 }
112
113 // TODO: finy out a better way of doing auto detetion without need for seek!
114 if ( !options ) {
115  if ( (len = read(fh, buf, 64)) < 1 ) {
116   close(fh);
117   return -1;
118  }
119
120  if ( lseek(fh, -len, SEEK_CUR) == (off_t)-1 ) {
121   close(fh);
122   return -1;
123  }
124
125  if ( (codec = roar_file_codecdetect(buf, len)) == -1 ) {
126   close(fh);
127   return -1;
128  }
129 } else {
130  if ( (codec = roar_str2codec(options)) == -1 ) {
131   close(fh);
132   return -1;
133  }
134 }
135
136 if ( (stream = streams_new()) == -1 ) {
137  close(fh);
138  return -1;
139 }
140
141 streams_get(stream, (struct roar_stream_server **)&s);
142
143 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
144
145 s->dir        = ROAR_DIR_PLAY;
146 s->pos_rel_id = -1;
147 s->info.codec = codec;
148
149 streams_set_fh(stream, fh);
150
151 client_stream_add(g_source_client, stream);
152
153 return 0;
154}
155
156//ll
Note: See TracBrowser for help on using the repository browser.