source: roaraudio/plugins/roard/protocol-esound.c @ 2533:1f734dd82389

Last change on this file since 2533:1f734dd82389 was 2533:1f734dd82389, checked in by phi, 15 years ago

added support for LATENCY command

File size: 9.3 KB
Line 
1//emul_esd.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
27#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
28#ifdef ROAR_HAVE_ESD
29
30#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_MINIMAL)
31#define _NAME(x) (x)
32#else
33#define _NAME(x) ((char*)NULL)
34#endif
35
36#define _cmd_t   int
37#define _INTSIZE sizeof(_cmd_t)
38
39struct emul_esd_command g_emul_esd_commands[] = {
40 {ESD_PROTO_CONNECT,      ESD_KEY_LEN  +     _INTSIZE, _NAME("CONNECT"),      emul_esd_on_connect},
41 {ESD_PROTO_LOCK,         ESD_KEY_LEN  +     _INTSIZE, _NAME("LOCK"),         NULL},
42 {ESD_PROTO_UNLOCK,       ESD_KEY_LEN  +     _INTSIZE, _NAME("UNLOCK"),       NULL},
43 {ESD_PROTO_STREAM_PLAY,  ESD_NAME_MAX + 2 * _INTSIZE, _NAME("STREAM_PLAY"),  emul_esd_on_stream},
44 {ESD_PROTO_STREAM_REC,   ESD_NAME_MAX + 2 * _INTSIZE, _NAME("STREAM_REC"),   emul_esd_on_stream},
45 {ESD_PROTO_STREAM_MON,   ESD_NAME_MAX + 2 * _INTSIZE, _NAME("STREAM_MON"),   emul_esd_on_stream},
46 {ESD_PROTO_SAMPLE_CACHE, ESD_NAME_MAX + 3 * _INTSIZE, _NAME("SAMPLE_CACHE"), NULL},
47 {ESD_PROTO_SAMPLE_FREE,                     _INTSIZE, _NAME("SAMPLE_FREE"),  NULL},
48 {ESD_PROTO_SAMPLE_PLAY,                     _INTSIZE, _NAME("SAMPLE_PLAY"),  NULL},
49 {ESD_PROTO_SAMPLE_LOOP,                     _INTSIZE, _NAME("SAMPLE_LOOP"),  NULL},
50 {ESD_PROTO_SAMPLE_STOP,                     _INTSIZE, _NAME("SAMPLE_STOP"),  NULL},
51 {ESD_PROTO_SAMPLE_KILL,  0                          , _NAME("SAMPLE_KILL"),  NULL},
52 {ESD_PROTO_STANDBY,      ESD_KEY_LEN +      _INTSIZE, _NAME("STANDBY"),      NULL},
53 {ESD_PROTO_RESUME,       ESD_KEY_LEN +      _INTSIZE, _NAME("RESUME"),       NULL},
54 {ESD_PROTO_SAMPLE_GETID, ESD_NAME_MAX               , _NAME("SAMPLE_GETID"), NULL},
55 {ESD_PROTO_STREAM_FILT,  ESD_NAME_MAX + 2 * _INTSIZE, _NAME("STREAM_FILT"),  emul_esd_on_stream},
56 {ESD_PROTO_SERVER_INFO,                     _INTSIZE, _NAME("SERVER_INFO"),  NULL},
57 {ESD_PROTO_ALL_INFO,                        _INTSIZE, _NAME("ALL_INFO"),     NULL},
58 {ESD_PROTO_SUBSCRIBE,    0                          , _NAME("SUBSCRIBE"),    NULL},
59 {ESD_PROTO_UNSUBSCRIBE,  0                          , _NAME("UNSUBSCRIBE"),  NULL},
60 {ESD_PROTO_STREAM_PAN,                  3 * _INTSIZE, _NAME("STREAM_PAN"),   NULL},
61 {ESD_PROTO_SAMPLE_PAN,                  3 * _INTSIZE, _NAME("SAMPLE_PAN"),   NULL},
62 {ESD_PROTO_STANDBY_MODE,                    _INTSIZE, _NAME("STANDBY_MODE"), NULL},
63 {ESD_PROTO_LATENCY,      0                          , _NAME("LATENCY"),      emul_esd_on_latency},
64 {ESD_PROTO_MAX,          0                          , _NAME("MAX"),          NULL},
65 {-1, 0, _NAME("END OF LIST"), NULL}
66};
67
68// command handling:
69int emul_esd_exec_command(int client, int cmd, struct roar_vio_calls * vio) {
70 struct emul_esd_command * cur;
71 void * data = NULL;
72 int r;
73 int i;
74
75 ROAR_DBG("emul_esd_exec_command(*) = ?");
76
77 if ( client == -1 || cmd < ESD_PROTO_CONNECT || cmd > ESD_PROTO_MAX || vio == NULL )
78  return -1;
79
80 ROAR_DBG("emul_esd_exec_command(*) = ?");
81
82 for (i = 0; (cur = &(g_emul_esd_commands[i]))->cmd != -1; i++) {
83  if ( cur->cmd == cmd ) {
84   if ( cur->datalen > 0 ) {
85    if ( (data = malloc(cur->datalen)) == NULL ) {
86     // we will do a protocol error in case we do not drop the client
87     clients_delete(client);
88     return -1;
89    }
90
91    if ( roar_vio_read(vio, data, cur->datalen) != cur->datalen ) {
92     free(data);
93     clients_delete(client);
94     return -1;
95    }
96   }
97
98   if ( cur->handler == NULL ) {
99    ROAR_WARN("emul_esd_exec_command(client=%i, cmd=%s(%i), vio=%p): client uses unimplemted command",
100               client, cur->name, cmd, vio
101             );
102    clients_delete(client);
103    r = -1;
104   } else {
105    r = cur->handler(client, cur, data, vio);
106   }
107
108   if ( data != NULL )
109    free(data);
110
111   return r;
112  }
113 }
114
115 return -1;
116}
117
118int emul_esd_check_client(int client, struct roar_vio_calls * vio) {
119 struct roar_vio_calls calls;
120 _cmd_t cmd;
121
122 if ( client == -1 )
123  return -1;
124
125 if ( vio == NULL ) {
126  vio = &calls;
127  if ( roar_vio_open_fh(vio, clients_get_fh(client)) == -1 )
128   return -1;
129 }
130
131 if ( roar_vio_read(vio, &cmd, _INTSIZE) != _INTSIZE ) {
132  // really bad protocol error
133  clients_delete(client);
134  return -1;
135 }
136
137 return emul_esd_exec_command(client, cmd, vio);
138}
139
140// porto lib:
141int emul_esd_int_read_buf  (int client, int * data, void * buf) {
142 _cmd_t d;
143
144 if ( data == NULL || buf == NULL )
145  return -1;
146
147 d = *(_cmd_t*)buf;
148
149 *data = d;
150
151 return 0;
152}
153int emul_esd_int_read      (int client, int * data, struct roar_vio_calls * vio) {
154 _cmd_t d;
155
156 if ( data == NULL )
157  return -1;
158
159 if ( roar_vio_read(vio, &d, _INTSIZE) != _INTSIZE )
160  return -1;
161
162 *data = d;
163
164 return 0;
165}
166
167int emul_esd_int_write     (int client, int   data, struct roar_vio_calls * vio) {
168 _cmd_t d = data;
169
170 return roar_vio_write(vio, &d, _INTSIZE) == _INTSIZE ? 0 : -1;
171}
172
173int emul_esd_test_auth     (int client, void * data, struct roar_vio_calls * vio) {
174 // accept all clients for the moment.
175 return emul_esd_int_write(client, 1, vio);
176}
177
178int emul_esd_test_byteorder(int client, void * data) {
179 // TODO: do a real test
180 return 0;
181}
182
183// handler:
184int emul_esd_on_connect    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
185
186 ROAR_DBG("emul_esd_on_connect(client=%i, cmd=%p, data=%p, vio=%p) = ?", client, cmd, data, vio);
187
188 if ( client == -1 || data == NULL || vio == NULL )
189  return -1;
190
191 ROAR_DBG("emul_esd_on_connect(client=%i, cmd=%p, data=%p, vio=%p) = ?", client, cmd, data, vio);
192
193 if ( emul_esd_test_auth(client, data, vio) == -1 )
194  return -1;
195
196 ROAR_DBG("emul_esd_on_connect(client=%i, cmd=%p, data=%p, vio=%p) = ?", client, cmd, data, vio);
197
198 if ( emul_esd_test_byteorder(client, data+ESD_KEY_LEN) == -1 )
199  return -1;
200
201 ROAR_DBG("emul_esd_on_connect(client=%i, cmd=%p, data=%p, vio=%p) = ?", client, cmd, data, vio);
202
203 return 0;
204}
205
206int emul_esd_on_stream     (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
207 struct roar_stream_server * ss;
208 struct roar_stream        *  s;
209 struct roar_client        *  c;
210 int stream;
211 int dir = -1;
212 int esdformat;
213 int rate;
214
215 if ( client == -1 || cmd == NULL || data == NULL || vio == NULL )
216  return -1;
217
218 switch (cmd->cmd) {
219  case ESD_PROTO_STREAM_PLAY: dir = ROAR_DIR_PLAY;    break;
220  case ESD_PROTO_STREAM_REC:  dir = ROAR_DIR_RECORD;  break;
221  case ESD_PROTO_STREAM_MON:  dir = ROAR_DIR_MONITOR; break;
222  case ESD_PROTO_STREAM_FILT: dir = ROAR_DIR_FILTER;  break;
223  default:
224    clients_delete(client);
225    return -1;
226 }
227
228 if ( clients_get(client, &c) == -1 ) {
229  return -1;
230 }
231
232 ROAR_DBG("emul_esd_on_stream(client=%i, ...): creating stream...", client);
233 if ((stream = streams_new()) == -1 ) {
234  clients_delete(client);
235  return -1;
236 }
237
238 ROAR_DBG("emul_esd_on_stream(client=%i, ...): getting stream...", client);
239 if ( streams_get(stream, &ss) == -1 ) {
240  streams_delete(stream);
241  clients_delete(client);
242  return -1;
243 }
244
245 s = ROAR_STREAM(ss);
246
247 ROAR_DBG("emul_esd_on_stream(client=%i, ...): set client of stream...", client);
248 if ( client_stream_add(client, stream) == -1 ) {
249  streams_delete(stream);
250  clients_delete(client);
251  return -1;
252 }
253
254 emul_esd_int_read_buf(client, &esdformat, data);
255 emul_esd_int_read_buf(client, &rate,      data+_INTSIZE);
256
257 strncpy(c->name, data + 2*_INTSIZE, ROAR_BUFFER_NAME > ESD_NAME_MAX ? ESD_NAME_MAX : ROAR_BUFFER_NAME);
258 c->name[ROAR_BUFFER_NAME-1] = 0;
259
260 ROAR_DBG("emul_esd_on_stream(*): esdformat=0x%.8X, rate=%i", esdformat, rate);
261
262 s->info.rate = rate;
263
264 switch (esdformat & ESD_MASK_BITS) {
265  case ESD_BITS8:  s->info.bits =  8; s->info.codec = ROAR_CODEC_PCM_U_LE; break;
266  case ESD_BITS16: s->info.bits = 16; s->info.codec = ROAR_CODEC_DEFAULT;  break;
267  default:
268    streams_delete(stream);
269    clients_delete(client);
270    return -1;
271 }
272
273 switch (esdformat & ESD_MASK_CHAN) {
274  case ESD_MONO:   s->info.channels = 1; break;
275  case ESD_STEREO: s->info.channels = 2; break;
276  default:
277    streams_delete(stream);
278    clients_delete(client);
279    return -1;
280 }
281
282 ss->codec_orgi = s->info.codec;
283
284 ROAR_DBG("emul_esd_on_stream(*): s->info = {.rate=%i, .bits=%i, .channels=%i, .codec=%i}", s->info.rate, s->info.bits, s->info.channels, s->info.codec);
285
286 if ( streams_set_dir(stream, dir, 1) == -1 ) {
287  clients_delete(client);
288  return -1;
289 }
290
291 if ( client_stream_exec(client, stream) == -1 ) {
292  clients_delete(client);
293  return -1;
294 }
295
296 return 0;
297}
298
299int emul_esd_on_latency    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
300 int lag = ROAR_OUTPUT_CFREQ;
301
302 lag *= 2.0 * 44100.0 / (float)g_sa->rate;
303 
304 return emul_esd_int_write(client, lag, vio);
305}
306
307#endif
308#endif
309
310//ll
Note: See TracBrowser for help on using the repository browser.