source: roaraudio/plugins/roard/protocol-esound.c @ 5739:2a1671d592b9

Last change on this file since 5739:2a1671d592b9 was 5739:2a1671d592b9, checked in by phi, 12 years ago

commit 0: make protocol emulations for esd, gopher and rplay more independed so they can be moved into plugins easily (See: #311)

File size: 17.0 KB
Line 
1//emul_esd.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012
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_ESD
29#ifdef ROAR_HAVE_H_ESD
30#include <esd.h>
31
32
33struct emul_esd_command {
34 int    cmd;
35 size_t datalen;
36#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_MINIMAL)
37 char name[COMMAND_MAX_NAMELEN];
38#else
39 char * name;
40#endif
41 int (*handler)(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
42};
43
44static int emul_esd_exec_command  (int client, int cmd, struct roar_vio_calls * vio);
45static int emul_esd_check_client  (int client, struct roar_vio_calls * vio, struct roar_buffer ** obuffer, void ** userdata, const struct roar_keyval * protopara, ssize_t protoparalen, struct roar_dl_librarypara * pluginpara);
46
47static int emul_esd_int_read_buf  (int client, int * data, void * buf);
48static int emul_esd_int_read      (int client, int * data, struct roar_vio_calls * vio);
49static int emul_esd_int_write     (int client, int   data, struct roar_vio_calls * vio);
50static int emul_esd_test_auth     (int client, void * data, struct roar_vio_calls * vio);
51static int emul_esd_test_byteorder(int client, void * data);
52
53static int emul_esd_on_connect    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
54static int emul_esd_on_stream     (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
55static int emul_esd_on_latency    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
56static int emul_esd_on_standby    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
57static int emul_esd_on_standbymode(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
58
59static int emul_esd_on_stream_pan (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
60
61static int emul_esd_on_server_info(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
62
63static int emul_esd_on_all_info   (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
64
65
66
67#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_MINIMAL)
68#define _NAME(x) (x)
69#else
70#define _NAME(x) ((char*)NULL)
71#endif
72
73#define _cmd_t   int
74#define _INTSIZE sizeof(_cmd_t)
75
76#define _NOT_TO_IMPLEMENT    NULL
77#define _UNIMPLEMNTED_IN_ESD NULL
78#define _NEED_SAMPLE_SUPPORT NULL
79
80#define _ROAR2ESD(x) ((x)+1)
81#define _ESD2ROAR(x) ((x)-1)
82
83static struct emul_esd_command g_emul_esd_commands[] = {
84 {ESD_PROTO_CONNECT,      ESD_KEY_LEN  +     _INTSIZE, _NAME("CONNECT"),      emul_esd_on_connect},
85 {ESD_PROTO_LOCK,         ESD_KEY_LEN  +     _INTSIZE, _NAME("LOCK"),         NULL},
86 {ESD_PROTO_UNLOCK,       ESD_KEY_LEN  +     _INTSIZE, _NAME("UNLOCK"),       NULL},
87 {ESD_PROTO_STREAM_PLAY,  ESD_NAME_MAX + 2 * _INTSIZE, _NAME("STREAM_PLAY"),  emul_esd_on_stream},
88 {ESD_PROTO_STREAM_REC,   ESD_NAME_MAX + 2 * _INTSIZE, _NAME("STREAM_REC"),   emul_esd_on_stream},
89 {ESD_PROTO_STREAM_MON,   ESD_NAME_MAX + 2 * _INTSIZE, _NAME("STREAM_MON"),   emul_esd_on_stream},
90 {ESD_PROTO_SAMPLE_CACHE, ESD_NAME_MAX + 3 * _INTSIZE, _NAME("SAMPLE_CACHE"), _NEED_SAMPLE_SUPPORT},
91 {ESD_PROTO_SAMPLE_FREE,                     _INTSIZE, _NAME("SAMPLE_FREE"),  _NEED_SAMPLE_SUPPORT},
92 {ESD_PROTO_SAMPLE_PLAY,                     _INTSIZE, _NAME("SAMPLE_PLAY"),  _NEED_SAMPLE_SUPPORT},
93 {ESD_PROTO_SAMPLE_LOOP,                     _INTSIZE, _NAME("SAMPLE_LOOP"),  _NEED_SAMPLE_SUPPORT},
94 {ESD_PROTO_SAMPLE_STOP,                     _INTSIZE, _NAME("SAMPLE_STOP"),  _NEED_SAMPLE_SUPPORT},
95 {ESD_PROTO_SAMPLE_KILL,  0                          , _NAME("SAMPLE_KILL"),  _NEED_SAMPLE_SUPPORT},
96 {ESD_PROTO_STANDBY,      ESD_KEY_LEN +      _INTSIZE, _NAME("STANDBY"),      emul_esd_on_standby},
97 {ESD_PROTO_RESUME,       ESD_KEY_LEN +      _INTSIZE, _NAME("RESUME"),       emul_esd_on_standby},
98 {ESD_PROTO_SAMPLE_GETID, ESD_NAME_MAX               , _NAME("SAMPLE_GETID"), _NEED_SAMPLE_SUPPORT},
99 {ESD_PROTO_STREAM_FILT,  ESD_NAME_MAX + 2 * _INTSIZE, _NAME("STREAM_FILT"),  emul_esd_on_stream},
100 {ESD_PROTO_SERVER_INFO,                     _INTSIZE, _NAME("SERVER_INFO"),  emul_esd_on_server_info},
101 {ESD_PROTO_ALL_INFO,                        _INTSIZE, _NAME("ALL_INFO"),     emul_esd_on_all_info},
102 {ESD_PROTO_SUBSCRIBE,    0                          , _NAME("SUBSCRIBE"),    _UNIMPLEMNTED_IN_ESD},
103 {ESD_PROTO_UNSUBSCRIBE,  0                          , _NAME("UNSUBSCRIBE"),  _UNIMPLEMNTED_IN_ESD},
104 {ESD_PROTO_STREAM_PAN,                  3 * _INTSIZE, _NAME("STREAM_PAN"),   emul_esd_on_stream_pan},
105 {ESD_PROTO_SAMPLE_PAN,                  3 * _INTSIZE, _NAME("SAMPLE_PAN"),   _NEED_SAMPLE_SUPPORT},
106 {ESD_PROTO_STANDBY_MODE,                    _INTSIZE, _NAME("STANDBY_MODE"), emul_esd_on_standbymode},
107 {ESD_PROTO_LATENCY,      0                          , _NAME("LATENCY"),      emul_esd_on_latency},
108 {ESD_PROTO_MAX,          0                          , _NAME("MAX"),          _NOT_TO_IMPLEMENT},
109 {-1, 0, _NAME("END OF LIST"), _NOT_TO_IMPLEMENT}
110};
111
112// command handling:
113static int emul_esd_exec_command(int client, int cmd, struct roar_vio_calls * vio) {
114 struct emul_esd_command * cur;
115 void * data = NULL;
116 ssize_t ret;
117 size_t  done = 0;
118 int r;
119 int i;
120
121 ROAR_DBG("emul_esd_exec_command(*) = ?");
122
123 if ( client == -1 || cmd < ESD_PROTO_CONNECT || cmd > ESD_PROTO_MAX || vio == NULL )
124  return -1;
125
126 ROAR_DBG("emul_esd_exec_command(*) = ?");
127
128 for (i = 0; (cur = &(g_emul_esd_commands[i]))->cmd != -1; i++) {
129  if ( cur->cmd == cmd ) {
130   if ( cur->datalen > 0 ) {
131    if ( (data = roar_mm_malloc(cur->datalen)) == NULL ) {
132     // we will do a protocol error in case we do not drop the client
133     return -1;
134    }
135
136    while ( done < cur->datalen ) {
137     ret = roar_vio_read(vio, data+done, cur->datalen-done);
138
139     if ( ret < 1 ) {
140      roar_mm_free(data);
141      return -1;
142     } else {
143      done += ret;
144     }
145    }
146   }
147
148   if ( cur->handler == NULL ) {
149    ROAR_WARN("emul_esd_exec_command(client=%i, cmd=%s(%i), vio=%p): client uses unimplemted command",
150               client, cur->name, cmd, vio
151             );
152    r = -1;
153   } else {
154    r = cur->handler(client, cur, data, vio);
155   }
156
157   if ( data != NULL )
158    roar_mm_free(data);
159
160   return r;
161  }
162 }
163
164 return -1;
165}
166
167static int emul_esd_set_proto(int client, struct roar_vio_calls * vio, struct roar_buffer ** obuffer, void ** userdata, const struct roar_keyval * protopara, ssize_t protoparalen, struct roar_dl_librarypara * pluginpara) {
168 if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, vio) == -1 )
169  return -1;
170
171 return 0;
172}
173
174static int emul_esd_check_client(int client, struct roar_vio_calls * vio, struct roar_buffer ** obuffer, void ** userdata, const struct roar_keyval * protopara, ssize_t protoparalen, struct roar_dl_librarypara * pluginpara) {
175 _cmd_t cmd;
176
177 if ( client == -1 )
178  return -1;
179
180 if ( roar_vio_read(vio, &cmd, _INTSIZE) != _INTSIZE ) {
181  // really bad protocol error
182  return -1;
183 }
184
185 return emul_esd_exec_command(client, cmd, vio);
186}
187
188// porto lib:
189int emul_esd_int_read_buf  (int client, int * data, void * buf) {
190 _cmd_t d;
191
192 if ( data == NULL || buf == NULL )
193  return -1;
194
195 d = *(_cmd_t*)buf;
196
197 *data = d;
198
199 return 0;
200}
201static int emul_esd_int_read      (int client, int * data, struct roar_vio_calls * vio) {
202 _cmd_t d;
203
204 if ( data == NULL )
205  return -1;
206
207 if ( roar_vio_read(vio, &d, _INTSIZE) != _INTSIZE )
208  return -1;
209
210 *data = d;
211
212 return 0;
213}
214
215static int emul_esd_int_write     (int client, int   data, struct roar_vio_calls * vio) {
216 _cmd_t d = data;
217
218 return roar_vio_write(vio, &d, _INTSIZE) == _INTSIZE ? 0 : -1;
219}
220
221static int emul_esd_test_auth     (int client, void * data, struct roar_vio_calls * vio) {
222 // accept all clients for the moment.
223 return emul_esd_int_write(client, 1, vio);
224}
225
226static int emul_esd_test_byteorder(int client, void * data) {
227 struct roar_client * c;
228
229 if ( clients_get(client, &c) == -1 )
230  return -1;
231
232// "NDNE";
233
234 if ( !memcmp(data, "NDNE", 4) ) {
235  c->byteorder = ROAR_BYTEORDER_LE;
236 } else if ( !memcmp(data, "ENDE", 4) ) {
237  c->byteorder = ROAR_BYTEORDER_BE;
238 } else if ( !memcmp(data, "NEED", 4) ) {
239  c->byteorder = ROAR_BYTEORDER_PDP;
240 } else {
241  return -1;
242 }
243
244 return 0;
245}
246
247// handler:
248static int emul_esd_on_connect    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
249
250 ROAR_DBG("emul_esd_on_connect(client=%i, cmd=%p, data=%p, vio=%p) = ?", client, cmd, data, vio);
251
252 if ( client == -1 || data == NULL || vio == NULL )
253  return -1;
254
255 ROAR_DBG("emul_esd_on_connect(client=%i, cmd=%p, data=%p, vio=%p) = ?", client, cmd, data, vio);
256
257 if ( emul_esd_test_auth(client, data, vio) == -1 )
258  return -1;
259
260 ROAR_DBG("emul_esd_on_connect(client=%i, cmd=%p, data=%p, vio=%p) = ?", client, cmd, data, vio);
261
262 if ( emul_esd_test_byteorder(client, data+ESD_KEY_LEN) == -1 )
263  return -1;
264
265 ROAR_DBG("emul_esd_on_connect(client=%i, cmd=%p, data=%p, vio=%p) = ?", client, cmd, data, vio);
266
267 return 0;
268}
269
270static int emul_esd_on_stream     (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
271 struct roar_stream_server * ss;
272 struct roar_stream        *  s;
273 struct roar_client        *  c;
274 int stream;
275 int dir = -1;
276 int esdformat;
277 int rate = 0;
278
279 if ( client == -1 || cmd == NULL || data == NULL || vio == NULL )
280  return -1;
281
282 switch (cmd->cmd) {
283  case ESD_PROTO_STREAM_PLAY: dir = ROAR_DIR_PLAY;    break;
284  case ESD_PROTO_STREAM_REC:  dir = ROAR_DIR_RECORD;  break;
285  case ESD_PROTO_STREAM_MON:  dir = ROAR_DIR_MONITOR; break;
286  case ESD_PROTO_STREAM_FILT: dir = ROAR_DIR_FILTER;  break;
287  default:
288    return -1;
289 }
290
291 if ( clients_get(client, &c) == -1 ) {
292  return -1;
293 }
294
295 ROAR_DBG("emul_esd_on_stream(client=%i, ...): creating stream...", client);
296 if ((stream = streams_new()) == -1 ) {
297  return -1;
298 }
299
300 ROAR_DBG("emul_esd_on_stream(client=%i, ...): getting stream...", client);
301 if ( streams_get(stream, &ss) == -1 ) {
302  streams_delete(stream);
303  return -1;
304 }
305
306 s = ROAR_STREAM(ss);
307
308 ROAR_DBG("emul_esd_on_stream(client=%i, ...): set client of stream...", client);
309 if ( client_stream_add(client, stream) == -1 ) {
310  streams_delete(stream);
311  return -1;
312 }
313
314 emul_esd_int_read_buf(client, &esdformat, data);
315 emul_esd_int_read_buf(client, &rate,      data+_INTSIZE);
316
317 strncpy(c->name, data + 2*_INTSIZE, ROAR_BUFFER_NAME > ESD_NAME_MAX ? ESD_NAME_MAX : ROAR_BUFFER_NAME);
318 c->name[ROAR_BUFFER_NAME-1] = 0;
319
320 ROAR_DBG("emul_esd_on_stream(*): esdformat=0x%.8X, rate=%i", esdformat, rate);
321
322 s->info.rate = rate;
323
324 switch (esdformat & ESD_MASK_BITS) {
325  case ESD_BITS8:  s->info.bits =  8; s->info.codec = ROAR_CODEC_PCM_U_LE; break;
326  case ESD_BITS16: s->info.bits = 16; s->info.codec = ROAR_CODEC_DEFAULT;  break;
327  default:
328    streams_delete(stream);
329    return -1;
330 }
331
332 switch (esdformat & ESD_MASK_CHAN) {
333  case ESD_MONO:   s->info.channels = 1; break;
334  case ESD_STEREO: s->info.channels = 2; break;
335  default:
336    streams_delete(stream);
337    return -1;
338 }
339
340 ss->codec_orgi = s->info.codec;
341
342 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);
343
344 if ( streams_set_dir(stream, dir, 1) == -1 ) {
345  return -1;
346 }
347
348 if ( client_stream_exec(client, stream) == -1 ) {
349  return -1;
350 }
351
352 return 0;
353}
354
355static int emul_esd_on_latency    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
356 int lag = ROAR_OUTPUT_CFREQ;
357
358 lag *= 2.0 * 44100.0 / (float)g_sa->rate;
359 
360 return emul_esd_int_write(client, lag, vio);
361}
362
363static int emul_esd_on_standby    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
364 int ok = 0;
365
366 if ( emul_esd_test_auth(client, data, vio) == -1 ) {
367  return emul_esd_int_write(client, ok, vio);
368 }
369
370 ok = 1;
371
372 if (cmd->cmd == ESD_PROTO_STANDBY) {
373  g_standby = 1;
374 } else {
375  g_standby = 0;
376 }
377
378
379 return emul_esd_int_write(client, ok, vio);
380}
381
382static int emul_esd_on_standbymode(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
383 int mode = ESM_ERROR;
384
385 if ( g_standby ) {
386  if ( g_autostandby ) {
387   mode = ESM_ON_AUTOSTANDBY;
388  } else {
389   mode = ESM_ON_STANDBY;
390  }
391 } else {
392  mode = ESM_RUNNING;
393 }
394
395 return emul_esd_int_write(client, mode, vio);
396}
397
398static int emul_esd_on_stream_pan (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
399 struct roar_stream_server * ss;
400 int stream = -1;
401 int left = 256, right = 256;
402 int ok = 0;
403
404 emul_esd_int_read_buf(client, &stream, data + 0*_INTSIZE);
405 emul_esd_int_read_buf(client, &left,   data + 1*_INTSIZE);
406 emul_esd_int_read_buf(client, &right,  data + 2*_INTSIZE);
407
408 stream = _ESD2ROAR(stream);
409
410 if ( streams_get(stream, &ss) != -1 ) {
411  ss->mixer.scale    = 256;
412  ss->mixer.mixer[0] = left;
413  ss->mixer.mixer[1] = right;
414  if ( streams_set_mixer(stream) != -1 )
415   ok = 1;
416 }
417
418 return emul_esd_int_write(client, ok, vio);
419}
420
421static int emul_esd_on_server_info(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
422 int version = 0;
423 int rate    = g_sa->rate;
424 int format  = 0;
425
426 switch (g_sa->bits) {
427  case  8: format |= ESD_BITS8;  break;
428  case 16: format |= ESD_BITS16; break;
429 }
430
431 switch (g_sa->channels) {
432  case  1: format |= ESD_MONO;   break;
433  case  2: format |= ESD_STEREO; break;
434 }
435
436 if ( emul_esd_int_write(client, version, vio) == -1 )
437  return -1;
438
439 if ( emul_esd_int_write(client, rate, vio) == -1 )
440  return -1;
441
442 if ( emul_esd_int_write(client, format, vio) == -1 )
443  return -1;
444
445 return 0;
446}
447
448static int emul_esd_on_all_info   (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
449 struct roar_stream_server *   ss;
450 struct roar_audio_info    * info;
451 struct roar_client        *    c;
452 char name[ESD_NAME_MAX];
453 char * sname;
454 int id, rate, left, right, format;
455 int i;
456
457 if ( emul_esd_on_server_info(client, cmd, data, vio) == -1 )
458  return -1;
459
460 for (i = 0; i < (ROAR_STREAMS_MAX+2); i++) {
461  memset(name, 0, sizeof(name));
462
463  id   = rate  = format = 0;
464  left = right = 0;
465
466  if ( i >= ROAR_STREAMS_MAX ) {
467   id = -1;
468  } else {
469   if ( streams_get(i, &ss) == -1 )
470    continue;
471
472   switch (streams_get_dir(i)) {
473    case ROAR_DIR_PLAY:    format |= ESD_PLAY;    break;
474    case ROAR_DIR_MONITOR: format |= ESD_MONITOR; break;
475    case ROAR_DIR_RECORD:  format |= ESD_RECORD;  break;
476    default:               continue;              break;
477   }
478
479   info = &(ROAR_STREAM(ss)->info);
480
481   id = i;
482   rate = info->rate;
483
484   switch (info->bits) {
485    case  8: format |= ESD_BITS8;  break;
486    case 16: format |= ESD_BITS16; break;
487   }
488
489   switch (info->channels) {
490    case  1:
491      if ( ss->mixer.mixer[0] == ss->mixer.scale ) {
492       left = right = 256;
493      } else {
494       left = right = ss->mixer.mixer[0] * 256 / ss->mixer.scale;
495      }
496      format |= ESD_MONO;
497     break;
498    case  2:
499      if ( ss->mixer.mixer[0] == ss->mixer.scale ) {
500       left = 256;
501      } else {
502       left = ss->mixer.mixer[0] * 256 / ss->mixer.scale;
503      }
504
505      if ( ss->mixer.mixer[1] == ss->mixer.scale ) {
506       right = 256;
507      } else {
508       right = ss->mixer.mixer[1] * 256 / ss->mixer.scale;
509      }
510
511      format |= ESD_STEREO;
512     break;
513    default:
514      left = right = 0;
515   }
516
517   sname = streams_get_name(id);
518
519   if ( sname == NULL || sname[0] == 0 ) {
520    if ( clients_get(streams_get_client(id), &c) != -1 ) {
521     sname = c->name;
522    }
523   }
524
525   if ( sname == NULL || sname[0] == 0 )
526    sname = "(unknown)";
527
528   strncpy(name, sname, sizeof(name) > ROAR_BUFFER_NAME ? ROAR_BUFFER_NAME : sizeof(name));
529   name[sizeof(name)-1] = 0;
530
531  }
532
533  id = _ROAR2ESD(id);
534
535  if ( emul_esd_int_write(client, id, vio) == -1 )
536   return -1;
537  if ( roar_vio_write(vio, name, sizeof(name)) != sizeof(name) )
538   return -1;
539  if ( emul_esd_int_write(client, rate, vio) == -1 )
540   return -1;
541  if ( emul_esd_int_write(client, left, vio) == -1 )
542   return -1;
543  if ( emul_esd_int_write(client, right, vio) == -1 )
544   return -1;
545  if ( emul_esd_int_write(client, format, vio) == -1 )
546   return -1;
547
548  if ( i == ROAR_STREAMS_MAX+1) {
549   // write 'length'...
550   if ( emul_esd_int_write(client, id, vio) == -1 )
551    return -1;
552  }
553 }
554
555 return 0;
556}
557
558struct roar_dl_proto __proto_common_esd = {
559 .proto = ROAR_PROTO_ESOUND,
560 .description = "EsounD emulation",
561 .flags = ROAR_DL_PROTO_FLAGS_NONE,
562 .set_proto = emul_esd_set_proto,
563 .handle = emul_esd_check_client
564};
565
566#endif
567#endif
568
569//ll
Note: See TracBrowser for help on using the repository browser.