source: roaraudio/roard/emul_rsound.c @ 3824:32199d63f4d4

Last change on this file since 3824:32199d63f4d4 was 3824:32199d63f4d4, checked in by phi, 14 years ago

added latency messurement, need some work

File size: 5.7 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 if ( emul_rsound_lastcon == -1 ) {
43  emul_rsound_lastcon = fh;
44  return -2;
45 } else {
46  emul_rsound_lastcon = -1;
47
48  client = clients_new();
49
50  if ( client == -1 )
51   return -1;
52
53  if ( clients_set_fh(client, fh) == -1 ) {
54   ROAR_ERR("net_get_new_client(void): Can not set client's fh");
55
56   clients_delete(client);
57   close(oldfh);
58   close(fh);
59
60   ROAR_DBG("net_get_new_client(void) = -1");
61   return -1;
62  }
63
64  if ( emul_rsound_new_client(client, oldfh) == -1 ) {
65   clients_delete(client);
66   return -1;
67  }
68
69  buf.i[0] = ROAR_HOST2NET32(0);
70  buf.i[1] = ROAR_HOST2NET32(512);
71  buf.i[2] = ROAR_HOST2NET32(0);
72  buf.i[3] = ROAR_HOST2NET32(0);
73
74  ROAR_NETWORK_WRITE(oldfh, buf.c, sizeof(buf.c));
75
76  return client;
77 }
78
79 return -1;
80}
81
82int emul_rsound_new_client  (int client, int data) {
83 struct roar_stream_server * ss;
84 struct roar_stream        *  s;
85 struct roar_client        *  c;
86 int stream;
87
88 if ( clients_get(client, &c) == -1 ) {
89  return -1;
90 }
91
92 if ((stream = streams_new()) == -1 ) {
93  clients_delete(client);
94  return -1;
95 }
96
97 if ( streams_get(stream, &ss) == -1 ) {
98  streams_delete(stream);
99  clients_delete(client);
100  return -1;
101 }
102
103 s = ROAR_STREAM(ss);
104
105 if ( client_stream_add(client, stream) == -1 ) {
106  streams_delete(stream);
107  clients_delete(client);
108  return -1;
109 }
110
111 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
112
113 ss->codec_orgi = s->info.codec = ROAR_CODEC_RIFF_WAVE;
114
115 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
116  clients_delete(client);
117  return -1;
118 }
119
120/*
121 if ( client_stream_exec(client, stream) == -1 ) {
122  clients_delete(client);
123  return -1;
124 }
125*/
126
127 if ( client_stream_set_fh(client, stream, data) == -1 ) {
128  streams_delete(stream);
129  clients_delete(client);
130  return -1;
131 }
132
133 return 0;
134}
135
136int emul_rsound_vrecv_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) {
137 ssize_t ret;
138 int     num;
139
140 if ( msg == NULL || vio == NULL )
141  return -1;
142
143 ret = roar_vio_read(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN);
144
145 if ( ret != EMUL_RSOUND_MSG_HEADER_LEN )
146  return -1;
147
148 msg->header[ret] = 0;
149
150 if ( msg->header[0] != 'R' || msg->header[1] != 'S' || msg->header[2] != 'D' )
151  return -1;
152
153 if ( sscanf(&(msg->header[3]), "%5d", &num) != 1 )
154  return -1;
155
156 if ( num > EMUL_RSOUND_MSG_DATA_LEN )
157  return -1;
158
159 msg->datalen = num;
160
161 ret = roar_vio_read(vio, msg->data, num);
162
163 if ( ret != (ssize_t)num )
164  return -1;
165
166 msg->data[num] = 0;
167
168 msg->datasp   = msg->data;
169 msg->dataslen = msg->datalen;
170
171 for (num = 0; msg->data[num] == ' '; num++) {
172  msg->datasp++;
173  msg->dataslen--;
174 }
175
176 return 0;
177}
178
179int emul_rsound_vsend_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) {
180 ssize_t ret;
181
182 if ( msg == NULL || vio == NULL )
183  return -1;
184
185 snprintf(msg->header, EMUL_RSOUND_MSG_HEADER_LEN+1, "RSD%5d", msg->datalen);
186
187 ret = roar_vio_write(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN);
188
189 if ( ret != EMUL_RSOUND_MSG_HEADER_LEN )
190  return -1;
191
192 ret = roar_vio_write(vio, msg->data, msg->datalen);
193
194 if ( ret != (ssize_t)msg->datalen )
195  return -1;
196
197 return 0;
198}
199
200int emul_rsound_check_client(int client, struct roar_vio_calls * vio) {
201 struct roar_vio_calls     rvio;
202 struct emul_rsound_msg     msg;
203 struct roar_client        *  c;
204 struct roar_stream_server * ss;
205 int                   streamid;
206 int                          i;
207 ssize_t                    ptr;
208
209 if ( vio == NULL ) {
210  vio = &rvio;
211  roar_vio_open_fh_socket(vio, clients_get_fh(client));
212 }
213
214 if ( emul_rsound_vrecv_msg(&msg, vio) == -1 )
215  return clients_delete(client);
216
217 if ( !strncmp(msg.datasp, "INFO", 4) ) {
218  // TODO: add support for INFO
219  if ( clients_get(client, &c) == -1 )
220   return clients_delete(client);
221
222  streamid = -1;
223  for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++)
224   if ( c->streams[i] > streamid )
225    streamid = c->streams[i];
226
227  if ( streamid == -1 )
228   return clients_delete(client);
229
230  if ( streams_get(streamid, &ss) == -1 )
231   return clients_delete(client);
232
233  ptr = roar_info2samplesize(&(ROAR_STREAM(ss)->info));
234
235  if ( ptr == -1 )
236   return clients_delete(client);
237
238  ptr *= ROAR_STREAM(ss)->pos;
239  ptr /= 8; // bits -> bytes
240
241  i = snprintf(msg.data+msg.datalen, EMUL_RSOUND_MSG_DATA_LEN - msg.datalen, " %lld", (long long int)ptr);
242
243  msg.datalen += i;
244
245  return emul_rsound_vsend_msg(&msg, vio);
246 } else if ( !strncmp(msg.datasp, "NULL", 4) ) {
247  // NULL is simular to NOOP
248  return 0;
249 } else if ( !strncmp(msg.datasp, "STOP", 4) ) {
250  // This is quit.
251  return clients_delete(client);
252 } else {
253  return clients_delete(client);
254 }
255}
256#endif
257
258//ll
Note: See TracBrowser for help on using the repository browser.