source: roaraudio/roard/emul_rsound.c @ 4193:024769a1636b

Last change on this file since 4193:024769a1636b was 4193:024769a1636b, checked in by phi, 14 years ago

fixed rsd bug with long running streams

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