source: roaraudio/roard/emul_rsound.c @ 4192:57e756e0059f

Last change on this file since 4192:57e756e0059f was 4130:3b5e874fd9ac, checked in by phi, 14 years ago

some rsound cleanup

File size: 6.9 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 ssize_t ret;
148 int     num;
149
150 if ( msg == NULL || vio == NULL )
151  return -1;
152
153 ret = roar_vio_read(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN);
154
155 if ( ret != EMUL_RSOUND_MSG_HEADER_LEN )
156  return -1;
157
158 msg->header[ret] = 0;
159
160 if ( msg->header[0] != 'R' || msg->header[1] != 'S' || msg->header[2] != 'D' )
161  return -1;
162
163 if ( sscanf(&(msg->header[3]), "%5d", &num) != 1 )
164  return -1;
165
166 if ( num > EMUL_RSOUND_MSG_DATA_LEN )
167  return -1;
168
169 msg->datalen = num;
170
171 ret = roar_vio_read(vio, msg->data, num);
172
173 if ( ret != (ssize_t)num )
174  return -1;
175
176 msg->data[num] = 0;
177
178 msg->datasp   = msg->data;
179 msg->dataslen = msg->datalen;
180
181 for (num = 0; msg->data[num] == ' '; num++) {
182  msg->datasp++;
183  msg->dataslen--;
184 }
185
186 return 0;
187}
188
189int emul_rsound_vsend_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) {
190 ssize_t ret;
191
192 if ( msg == NULL || vio == NULL )
193  return -1;
194
195 snprintf(msg->header, EMUL_RSOUND_MSG_HEADER_LEN+1, "RSD%5d", (int)msg->datalen);
196
197 ret = roar_vio_write(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN);
198
199 if ( ret != EMUL_RSOUND_MSG_HEADER_LEN )
200  return -1;
201
202 ret = roar_vio_write(vio, msg->data, msg->datalen);
203
204 if ( ret != (ssize_t)msg->datalen )
205  return -1;
206
207 return 0;
208}
209
210int emul_rsound_check_client(int client, struct roar_vio_calls * vio) {
211 struct roar_vio_calls     rvio;
212 struct emul_rsound_msg     msg;
213 struct roar_client        *  c;
214 struct roar_stream_server * ss;
215 int                   streamid;
216 int                          i;
217 ssize_t                    ptr;
218 size_t                 max_len;
219
220 if ( vio == NULL ) {
221  vio = &rvio;
222  roar_vio_open_fh_socket(vio, clients_get_fh(client));
223 }
224
225 // we get called in a loop, in case this fails no problem, just
226 // return -1, caller will delete us in case of real error.
227 if ( emul_rsound_vrecv_msg(&msg, vio) == -1 )
228  return -1;
229
230 if ( !strncmp(msg.datasp, "INFO", 4) ) {
231  if ( clients_get(client, &c) == -1 )
232   return clients_delete(client);
233
234  streamid = -1;
235  for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++)
236   if ( c->streams[i] > streamid )
237    streamid = c->streams[i];
238
239  if ( streamid == -1 )
240   return clients_delete(client);
241
242  if ( streams_get(streamid, &ss) == -1 )
243   return clients_delete(client);
244
245  ptr = roar_info2samplesize(&(ROAR_STREAM(ss)->info));
246
247  if ( ptr == -1 )
248   return clients_delete(client);
249
250  ptr *= ROAR_STREAM(ss)->pos;
251  ptr /= 8; // bits -> bytes
252
253  i = snprintf(msg.data+msg.datalen, EMUL_RSOUND_MSG_DATA_LEN - msg.datalen, " %lld", (long long int)ptr);
254
255  msg.datalen += i;
256
257  return emul_rsound_vsend_msg(&msg, vio);
258 } else if ( !strncmp(msg.datasp, "NULL", 4) ) {
259  // NULL is simular to NOOP
260 } else if ( !strncmp(msg.datasp, "STOP", 4) ) {
261  // This is quit.
262  return clients_delete(client);
263 } else if ( !strncmp(msg.datasp, "IDENTITY ", 9) ) {
264  if ( msg.dataslen < (8+1+1) )
265   return clients_delete(client);
266
267  msg.datasp   += 9;
268  msg.dataslen -= 9;
269
270  if ( clients_get(client, &c) == -1 )
271   return clients_delete(client);
272
273  max_len = msg.dataslen < (ROAR_BUFFER_NAME-1) ? msg.dataslen : (ROAR_BUFFER_NAME-1);
274
275  strncpy(c->name, msg.datasp, max_len);
276  c->name[max_len] = 0;
277 } else if ( !strncmp(msg.datasp, "CLOSECTL", 9) ) {
278  if ( clients_get(client, &c) == -1 )
279   return clients_delete(client);
280
281  strncpy(msg.data, " CLOSECTL OK", EMUL_RSOUND_MSG_DATA_LEN);
282  msg.datalen = 12; //strlen(" CLOSECTL OK");
283
284  if ( emul_rsound_vsend_msg(&msg, vio) == -1 )
285   return clients_delete(client);
286
287  streamid = c->streams[0];
288
289  if ( client_stream_exec(client, streamid) == -1 )
290   return clients_delete(client);
291
292  return 0;
293 } else {
294  // Unknown command, kill the client.
295  return clients_delete(client);
296 }
297
298 return 0;
299}
300#endif
301
302//ll
Note: See TracBrowser for help on using the repository browser.