1 | //emul_rsound.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
29 | |
---|
30 | static int emul_rsound_lastcon = -1; |
---|
31 | |
---|
32 | int emul_rsound_new_client (int client, int data); |
---|
33 | |
---|
34 | int emul_rsound_on_connect (int fh, struct roard_listen * lsock) { |
---|
35 | int oldfh = emul_rsound_lastcon; |
---|
36 | int client; |
---|
37 | union { |
---|
38 | int32_t i[4]; |
---|
39 | char c[16]; |
---|
40 | } buf; |
---|
41 | |
---|
42 | // TODO: add error handling |
---|
43 | roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK); |
---|
44 | |
---|
45 | if ( emul_rsound_lastcon == -1 ) { |
---|
46 | emul_rsound_lastcon = fh; |
---|
47 | return -2; |
---|
48 | } else { |
---|
49 | emul_rsound_lastcon = -1; |
---|
50 | |
---|
51 | client = clients_new(); |
---|
52 | |
---|
53 | if ( client == -1 ) |
---|
54 | return -1; |
---|
55 | |
---|
56 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
57 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
58 | |
---|
59 | clients_delete(client); |
---|
60 | close(oldfh); |
---|
61 | close(fh); |
---|
62 | |
---|
63 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
64 | return -1; |
---|
65 | } |
---|
66 | |
---|
67 | if ( emul_rsound_new_client(client, oldfh) == -1 ) { |
---|
68 | clients_delete(client); |
---|
69 | return -1; |
---|
70 | } |
---|
71 | |
---|
72 | buf.i[0] = ROAR_HOST2NET32(0); |
---|
73 | buf.i[1] = ROAR_HOST2NET32(512); |
---|
74 | buf.i[2] = ROAR_HOST2NET32(0); |
---|
75 | buf.i[3] = ROAR_HOST2NET32(0); |
---|
76 | |
---|
77 | ROAR_NETWORK_WRITE(oldfh, buf.c, sizeof(buf.c)); |
---|
78 | |
---|
79 | return client; |
---|
80 | } |
---|
81 | |
---|
82 | return -1; |
---|
83 | } |
---|
84 | |
---|
85 | int emul_rsound_new_client (int client, int data) { |
---|
86 | struct roar_stream_server * ss; |
---|
87 | struct roar_stream * s; |
---|
88 | struct roar_client * c; |
---|
89 | int stream; |
---|
90 | |
---|
91 | if ( clients_get(client, &c) == -1 ) { |
---|
92 | return -1; |
---|
93 | } |
---|
94 | |
---|
95 | if ((stream = streams_new()) == -1 ) { |
---|
96 | clients_delete(client); |
---|
97 | return -1; |
---|
98 | } |
---|
99 | |
---|
100 | if ( streams_get(stream, &ss) == -1 ) { |
---|
101 | streams_delete(stream); |
---|
102 | clients_delete(client); |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | s = ROAR_STREAM(ss); |
---|
107 | |
---|
108 | if ( client_stream_add(client, stream) == -1 ) { |
---|
109 | streams_delete(stream); |
---|
110 | clients_delete(client); |
---|
111 | return -1; |
---|
112 | } |
---|
113 | |
---|
114 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
115 | |
---|
116 | ss->codec_orgi = s->info.codec = ROAR_CODEC_RIFF_WAVE; |
---|
117 | |
---|
118 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
119 | clients_delete(client); |
---|
120 | return -1; |
---|
121 | } |
---|
122 | |
---|
123 | /* |
---|
124 | if ( client_stream_exec(client, stream) == -1 ) { |
---|
125 | clients_delete(client); |
---|
126 | return -1; |
---|
127 | } |
---|
128 | */ |
---|
129 | |
---|
130 | if ( client_stream_set_fh(client, stream, data) == -1 ) { |
---|
131 | streams_delete(stream); |
---|
132 | clients_delete(client); |
---|
133 | return -1; |
---|
134 | } |
---|
135 | |
---|
136 | return 0; |
---|
137 | } |
---|
138 | |
---|
139 | int emul_rsound_vrecv_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) { |
---|
140 | ssize_t ret; |
---|
141 | int num; |
---|
142 | |
---|
143 | if ( msg == NULL || vio == NULL ) |
---|
144 | return -1; |
---|
145 | |
---|
146 | ret = roar_vio_read(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN); |
---|
147 | |
---|
148 | if ( ret != EMUL_RSOUND_MSG_HEADER_LEN ) |
---|
149 | return -1; |
---|
150 | |
---|
151 | msg->header[ret] = 0; |
---|
152 | |
---|
153 | if ( msg->header[0] != 'R' || msg->header[1] != 'S' || msg->header[2] != 'D' ) |
---|
154 | return -1; |
---|
155 | |
---|
156 | if ( sscanf(&(msg->header[3]), "%5d", &num) != 1 ) |
---|
157 | return -1; |
---|
158 | |
---|
159 | if ( num > EMUL_RSOUND_MSG_DATA_LEN ) |
---|
160 | return -1; |
---|
161 | |
---|
162 | msg->datalen = num; |
---|
163 | |
---|
164 | ret = roar_vio_read(vio, msg->data, num); |
---|
165 | |
---|
166 | if ( ret != (ssize_t)num ) |
---|
167 | return -1; |
---|
168 | |
---|
169 | msg->data[num] = 0; |
---|
170 | |
---|
171 | msg->datasp = msg->data; |
---|
172 | msg->dataslen = msg->datalen; |
---|
173 | |
---|
174 | for (num = 0; msg->data[num] == ' '; num++) { |
---|
175 | msg->datasp++; |
---|
176 | msg->dataslen--; |
---|
177 | } |
---|
178 | |
---|
179 | return 0; |
---|
180 | } |
---|
181 | |
---|
182 | int emul_rsound_vsend_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) { |
---|
183 | ssize_t ret; |
---|
184 | |
---|
185 | if ( msg == NULL || vio == NULL ) |
---|
186 | return -1; |
---|
187 | |
---|
188 | snprintf(msg->header, EMUL_RSOUND_MSG_HEADER_LEN+1, "RSD%5d", msg->datalen); |
---|
189 | |
---|
190 | ret = roar_vio_write(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN); |
---|
191 | |
---|
192 | if ( ret != EMUL_RSOUND_MSG_HEADER_LEN ) |
---|
193 | return -1; |
---|
194 | |
---|
195 | ret = roar_vio_write(vio, msg->data, msg->datalen); |
---|
196 | |
---|
197 | if ( ret != (ssize_t)msg->datalen ) |
---|
198 | return -1; |
---|
199 | |
---|
200 | return 0; |
---|
201 | } |
---|
202 | |
---|
203 | int emul_rsound_check_client(int client, struct roar_vio_calls * vio) { |
---|
204 | struct roar_vio_calls rvio; |
---|
205 | struct emul_rsound_msg msg; |
---|
206 | struct roar_client * c; |
---|
207 | struct roar_stream_server * ss; |
---|
208 | int streamid; |
---|
209 | int i; |
---|
210 | ssize_t ptr; |
---|
211 | |
---|
212 | if ( vio == NULL ) { |
---|
213 | vio = &rvio; |
---|
214 | roar_vio_open_fh_socket(vio, clients_get_fh(client)); |
---|
215 | } |
---|
216 | |
---|
217 | // we get called in a loop, in case this fails no problem, just |
---|
218 | // return -1, caller will delete us in case of real error. |
---|
219 | if ( emul_rsound_vrecv_msg(&msg, vio) == -1 ) |
---|
220 | return -1; |
---|
221 | |
---|
222 | if ( !strncmp(msg.datasp, "INFO", 4) ) { |
---|
223 | // TODO: add support for INFO |
---|
224 | if ( clients_get(client, &c) == -1 ) |
---|
225 | return clients_delete(client); |
---|
226 | |
---|
227 | streamid = -1; |
---|
228 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) |
---|
229 | if ( c->streams[i] > streamid ) |
---|
230 | streamid = c->streams[i]; |
---|
231 | |
---|
232 | if ( streamid == -1 ) |
---|
233 | return clients_delete(client); |
---|
234 | |
---|
235 | if ( streams_get(streamid, &ss) == -1 ) |
---|
236 | return clients_delete(client); |
---|
237 | |
---|
238 | ptr = roar_info2samplesize(&(ROAR_STREAM(ss)->info)); |
---|
239 | |
---|
240 | if ( ptr == -1 ) |
---|
241 | return clients_delete(client); |
---|
242 | |
---|
243 | ptr *= ROAR_STREAM(ss)->pos; |
---|
244 | ptr /= 8; // bits -> bytes |
---|
245 | |
---|
246 | i = snprintf(msg.data+msg.datalen, EMUL_RSOUND_MSG_DATA_LEN - msg.datalen, " %lld", (long long int)ptr); |
---|
247 | |
---|
248 | msg.datalen += i; |
---|
249 | |
---|
250 | return emul_rsound_vsend_msg(&msg, vio); |
---|
251 | } else if ( !strncmp(msg.datasp, "NULL", 4) ) { |
---|
252 | // NULL is simular to NOOP |
---|
253 | } else if ( !strncmp(msg.datasp, "STOP", 4) ) { |
---|
254 | // This is quit. |
---|
255 | return clients_delete(client); |
---|
256 | } else { |
---|
257 | return clients_delete(client); |
---|
258 | } |
---|
259 | |
---|
260 | return 0; |
---|
261 | } |
---|
262 | #endif |
---|
263 | |
---|
264 | //ll |
---|