source: roaraudio/roard/emul_rsound.c @ 5012:b263759832f1

Last change on this file since 5012:b263759832f1 was 4739:605b086a1801, checked in by phi, 13 years ago

some work to avoid compiler warnings

File size: 7.6 KB
Line 
1//emul_rsound.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
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
30static int emul_rsound_lastcon = -1;
31
32int emul_rsound_new_client  (int client, int data);
33
34int 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  // LATENCY:
73  // we currently don't know what we need to set here.
74  buf.i[0] = ROAR_HOST2NET32(0);
75
76  // block size used by roard.
77  buf.i[1] = ROAR_HOST2NET32(ROAR_OUTPUT_BUFFER_SAMPLES * roar_info2framesize(g_sa) / 8);
78
79  // Magic zeros to enable protocol handling.
80  // (RSD_CONN_PROTO)
81  buf.i[2] = ROAR_HOST2NET32(0);
82  buf.i[3] = ROAR_HOST2NET32(0);
83
84  if ( ROAR_NETWORK_WRITE(oldfh, buf.c, sizeof(buf.c)) != sizeof(buf.c) ) {
85   clients_delete(client);
86   return -1;
87  }
88
89  return client;
90 }
91
92 return -1;
93}
94
95int emul_rsound_new_client  (int client, int data) {
96 struct roar_stream_server * ss;
97 struct roar_stream        *  s;
98 struct roar_client        *  c;
99 int stream;
100
101 if ( clients_get(client, &c) == -1 ) {
102  return -1;
103 }
104
105 if ((stream = streams_new()) == -1 ) {
106  clients_delete(client);
107  return -1;
108 }
109
110 if ( streams_get(stream, &ss) == -1 ) {
111  streams_delete(stream);
112  clients_delete(client);
113  return -1;
114 }
115
116 s = ROAR_STREAM(ss);
117
118 if ( client_stream_add(client, stream) == -1 ) {
119  streams_delete(stream);
120  clients_delete(client);
121  return -1;
122 }
123
124 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
125
126 ss->codec_orgi = s->info.codec = ROAR_CODEC_RIFF_WAVE;
127
128 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
129  clients_delete(client);
130  return -1;
131 }
132
133/*
134 if ( client_stream_exec(client, stream) == -1 ) {
135  clients_delete(client);
136  return -1;
137 }
138*/
139
140 if ( client_stream_set_fh(client, stream, data) == -1 ) {
141  streams_delete(stream);
142  clients_delete(client);
143  return -1;
144 }
145
146 return 0;
147}
148
149int emul_rsound_vrecv_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) {
150 struct roar_vio_select select;
151 struct roar_vio_selecttv tv = {
152  .sec  = 0,
153  .nsec = 0
154 };
155 ssize_t ret;
156 int     num;
157
158 if ( msg == NULL || vio == NULL )
159  return -1;
160
161 ROAR_VIO_SELECT_SETVIO(&select, vio, ROAR_VIO_SELECT_READ);
162 if ( roar_vio_select(&select, 1, &tv, NULL) == -1 )
163  return -1;
164
165 if ( !(select.eventsa & ROAR_VIO_SELECT_READ) ) {
166  errno = EAGAIN;
167  return -1;
168 }
169
170 ret = roar_vio_read(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN);
171
172 if ( ret != EMUL_RSOUND_MSG_HEADER_LEN )
173  return -1;
174
175 msg->header[ret] = 0;
176
177 if ( msg->header[0] != 'R' || msg->header[1] != 'S' || msg->header[2] != 'D' )
178  return -1;
179
180 if ( sscanf(&(msg->header[3]), "%5d", &num) != 1 )
181  return -1;
182
183 if ( num > EMUL_RSOUND_MSG_DATA_LEN )
184  return -1;
185
186 msg->datalen = num;
187
188 ret = roar_vio_read(vio, msg->data, num);
189
190 if ( ret != (ssize_t)num )
191  return -1;
192
193 msg->data[num] = 0;
194
195 msg->datasp   = msg->data;
196 msg->dataslen = msg->datalen;
197
198 for (num = 0; msg->data[num] == ' '; num++) {
199  msg->datasp++;
200  msg->dataslen--;
201 }
202
203 return 0;
204}
205
206int emul_rsound_vsend_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) {
207 struct roar_vio_select select;
208 struct roar_vio_selecttv tv = {
209  .sec = 0,
210  .nsec = 0
211 };
212 ssize_t ret;
213
214 if ( msg == NULL || vio == NULL )
215  return -1;
216
217 ROAR_VIO_SELECT_SETVIO(&select, vio, ROAR_VIO_SELECT_WRITE);
218 if ( roar_vio_select(&select, 1, &tv, NULL) == -1 )
219  return -1;
220
221 if ( !(select.eventsa & ROAR_VIO_SELECT_WRITE) ) {
222  errno = EAGAIN;
223  return -1;
224 }
225
226 snprintf(msg->header, EMUL_RSOUND_MSG_HEADER_LEN+1, "RSD%5d", (int)msg->datalen);
227
228 ret = roar_vio_write(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN);
229
230 if ( ret != EMUL_RSOUND_MSG_HEADER_LEN )
231  return -1;
232
233 ret = roar_vio_write(vio, msg->data, msg->datalen);
234
235 if ( ret != (ssize_t)msg->datalen )
236  return -1;
237
238 return 0;
239}
240
241int emul_rsound_check_client(int client, struct roar_vio_calls * vio) {
242 struct roar_vio_calls     rvio;
243 struct emul_rsound_msg     msg;
244 struct roar_client        *  c;
245 struct roar_stream_server * ss;
246 int                   streamid;
247 int                          i;
248 ssize_t                    ptr;
249 size_t                 max_len;
250
251 if ( vio == NULL ) {
252  vio = &rvio;
253  roar_vio_open_fh_socket(vio, clients_get_fh(client));
254 }
255
256 // we get called in a loop, in case this fails no problem, just
257 // return -1, caller will delete us in case of real error.
258 if ( emul_rsound_vrecv_msg(&msg, vio) == -1 )
259  return -1;
260
261 if ( !strncmp(msg.datasp, "INFO", 4) ) {
262  if ( clients_get(client, &c) == -1 )
263   return clients_delete(client);
264
265  streamid = -1;
266  for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++)
267   if ( c->streams[i] > streamid )
268    streamid = c->streams[i];
269
270  if ( streamid == -1 )
271   return clients_delete(client);
272
273  if ( streams_get(streamid, &ss) == -1 )
274   return clients_delete(client);
275
276  ptr = roar_info2samplesize(&(ROAR_STREAM(ss)->info));
277
278  if ( ptr == -1 )
279   return clients_delete(client);
280
281  ptr *= ROAR_STREAM(ss)->pos;
282  ptr /= 8; // bits -> bytes
283
284  i = snprintf(msg.data+msg.datalen, EMUL_RSOUND_MSG_DATA_LEN - msg.datalen, " %lld", (long long int)ptr);
285
286  msg.datalen += i;
287
288  return emul_rsound_vsend_msg(&msg, vio);
289 } else if ( !strncmp(msg.datasp, "NULL", 4) ) {
290  // NULL is simular to NOOP
291 } else if ( !strncmp(msg.datasp, "STOP", 4) ) {
292  // This is quit.
293  return clients_delete(client);
294 } else if ( !strncmp(msg.datasp, "IDENTITY ", 9) ) {
295  if ( msg.dataslen < (8+1+1) )
296   return clients_delete(client);
297
298  msg.datasp   += 9;
299  msg.dataslen -= 9;
300
301  if ( clients_get(client, &c) == -1 )
302   return clients_delete(client);
303
304  max_len = msg.dataslen < (ROAR_BUFFER_NAME-1) ? msg.dataslen : (ROAR_BUFFER_NAME-1);
305
306  strncpy(c->name, msg.datasp, max_len);
307  c->name[max_len] = 0;
308 } else if ( !strncmp(msg.datasp, "CLOSECTL", 9) ) {
309  if ( clients_get(client, &c) == -1 )
310   return clients_delete(client);
311
312  strncpy(msg.data, " CLOSECTL OK", EMUL_RSOUND_MSG_DATA_LEN);
313  msg.datalen = 12; //strlen(" CLOSECTL OK");
314
315  if ( emul_rsound_vsend_msg(&msg, vio) == -1 )
316   return clients_delete(client);
317
318  streamid = c->streams[0];
319
320  if ( client_stream_exec(client, streamid) == -1 )
321   return clients_delete(client);
322
323  return 0;
324 } else {
325  // Unknown command, kill the client.
326  return clients_delete(client);
327 }
328
329 return 0;
330}
331#endif
332
333//ll
Note: See TracBrowser for help on using the repository browser.