1 | //emul_rplay.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_RPLAY |
---|
29 | |
---|
30 | struct emul_rplay_command emul_rplay_commands[] = { |
---|
31 | {"access", NULL, -1, -1, NULL}, |
---|
32 | {"application", NULL, 1, -1, NULL}, |
---|
33 | {"continue", NULL, 1, -1, NULL}, |
---|
34 | {"die", NULL, 1, -1, NULL}, |
---|
35 | {"done", NULL, 1, -1, NULL}, // #ifdef DEBUG |
---|
36 | {"find", NULL, 1, 1, NULL}, |
---|
37 | {"get", NULL, 1, 1, NULL}, |
---|
38 | {"help", NULL, -1, -1, emul_rplay_on_help}, |
---|
39 | {"info", NULL, 1, 1, NULL}, |
---|
40 | {"list", NULL, 0, 1, NULL}, |
---|
41 | {"modify", NULL, 2, -1, NULL}, |
---|
42 | {"monitor", NULL, 1, -1, NULL}, |
---|
43 | {"pause", NULL, 1, -1, NULL}, |
---|
44 | {"play", NULL, 1, -1, NULL}, |
---|
45 | {"put", NULL, 2, -1, NULL}, |
---|
46 | {"quit", NULL, 0, 0, emul_rplay_on_quit}, |
---|
47 | {"reset", NULL, 0, 0, NULL}, |
---|
48 | {"set", NULL, 1, -1, NULL}, |
---|
49 | {"skip", NULL, 1, 1, NULL}, |
---|
50 | {"status", NULL, 0, 0, emul_rplay_on_status}, |
---|
51 | {"stop", NULL, 1, -1, NULL}, |
---|
52 | {"version", NULL, 0, 0, NULL}, |
---|
53 | {"volume", NULL, 0, 1, NULL}, |
---|
54 | {"wait", NULL, -1, -1, NULL}, |
---|
55 | {NULL, NULL, -1, -1, NULL} |
---|
56 | }; |
---|
57 | |
---|
58 | static inline int is_true(const char * str) { |
---|
59 | const char * ts[] = {"true", "t", "1", "yes", "y", "on"}; |
---|
60 | int i; |
---|
61 | |
---|
62 | for (i = 0; i < sizeof(ts)/sizeof(*ts); i++) |
---|
63 | if ( !strcasecmp(str, ts[i]) ) |
---|
64 | return 1; |
---|
65 | |
---|
66 | return 0; |
---|
67 | } |
---|
68 | |
---|
69 | static inline int is_false(const char * str) { |
---|
70 | return !is_true(str); |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | int emul_rplay_check_client (int client, struct roar_vio_calls * vio) { |
---|
75 | struct roar_vio_calls calls; |
---|
76 | char buf[1024]; |
---|
77 | ssize_t len; |
---|
78 | |
---|
79 | if ( client == -1 ) |
---|
80 | return -1; |
---|
81 | |
---|
82 | if ( vio == NULL ) { |
---|
83 | vio = &calls; |
---|
84 | if ( roar_vio_open_fh_socket(vio, clients_get_fh(client)) == -1 ) |
---|
85 | return -1; |
---|
86 | } |
---|
87 | |
---|
88 | if ( (len = roar_vio_read(vio, buf, sizeof(buf)-1)) <= 0 ) { |
---|
89 | // really bad protocol error |
---|
90 | clients_delete(client); |
---|
91 | return -1; |
---|
92 | } |
---|
93 | |
---|
94 | for (; buf[len-1] == '\r' || buf[len-1] == '\n'; len--); |
---|
95 | |
---|
96 | buf[len] = 0; |
---|
97 | |
---|
98 | return emul_rplay_exec_command(client, vio, buf); |
---|
99 | } |
---|
100 | |
---|
101 | int emul_rplay_exec_command (int client, struct roar_vio_calls * vio, char * command) { |
---|
102 | struct emul_rplay_command * cmd; |
---|
103 | struct roar_keyval * kv; |
---|
104 | ssize_t kvlen; |
---|
105 | char * para = NULL; |
---|
106 | char * c; |
---|
107 | int last_was_space = 0; |
---|
108 | |
---|
109 | for (c = command; *c != 0; c++) { |
---|
110 | if ( *c == ' ' || *c == '\t' ) { |
---|
111 | last_was_space = 1; |
---|
112 | *c = 0; |
---|
113 | } else { |
---|
114 | if ( last_was_space ) { |
---|
115 | para = c; |
---|
116 | break; |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | if ( para == NULL ) { |
---|
122 | kv = NULL; |
---|
123 | kvlen = 0; |
---|
124 | } else { |
---|
125 | kvlen = roar_keyval_split(&kv, para, " \t", "=", 0); |
---|
126 | if ( kvlen == -1 ) |
---|
127 | return emul_rplay_send_error(client, NULL, vio, NULL, 0, "Can not parse parameter list"); |
---|
128 | } |
---|
129 | |
---|
130 | for (cmd = emul_rplay_commands; cmd->name != NULL; cmd++) { |
---|
131 | if ( !strcasecmp(cmd->name, command) ) |
---|
132 | break; |
---|
133 | } |
---|
134 | |
---|
135 | if ( cmd->name == NULL ) |
---|
136 | return emul_rplay_send_error(client, NULL, vio, kv, kvlen, "unknown command"); |
---|
137 | |
---|
138 | if ( cmd->handler == NULL ) |
---|
139 | return emul_rplay_send_error(client, cmd, vio, kv, kvlen, "unsupported command"); |
---|
140 | |
---|
141 | return cmd->handler(client, cmd, vio, kv, kvlen); |
---|
142 | } |
---|
143 | |
---|
144 | int emul_rplay_send_error (int client, struct emul_rplay_command * cmd, struct roar_vio_calls * vio, struct roar_keyval * kv, size_t kvlen, const char * msg) { |
---|
145 | struct roar_keyval * kvr; |
---|
146 | const char * command; |
---|
147 | const char * cd = NULL; |
---|
148 | |
---|
149 | if ( cmd != NULL ) { |
---|
150 | command = cmd->name; |
---|
151 | } else { |
---|
152 | command = "(unknown)"; |
---|
153 | } |
---|
154 | |
---|
155 | if ( kv != NULL ) { |
---|
156 | kvr = roar_keyval_lookup(kv, "client-data", kvlen, 0); |
---|
157 | if ( kvr != NULL ) |
---|
158 | cd = kvr->value; |
---|
159 | } |
---|
160 | |
---|
161 | if ( cd == NULL ) |
---|
162 | cd = ""; |
---|
163 | |
---|
164 | return roar_vio_printf(vio, "-error=\"%s\" command=\"%s\" client-data=\"%s\"\n", msg, command, cd) <= 0 ? -1 : 0; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | int emul_rplay_on_status(int client, struct emul_rplay_command * cmd, struct roar_vio_calls * vio, struct roar_keyval * kv, size_t kvlen) { |
---|
169 | const char * hostname = "localhost"; |
---|
170 | const char * version = "RoarAudio"; |
---|
171 | char uptime[16]; |
---|
172 | const char * byteorder = "native"; |
---|
173 | int fragsize = ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa); |
---|
174 | int h, m, s; |
---|
175 | |
---|
176 | s = g_pos / g_sa->rate / g_sa->channels; |
---|
177 | h = s / 3600; |
---|
178 | s -= h * 3600; |
---|
179 | m = s / 60; |
---|
180 | s -= m * 60; |
---|
181 | |
---|
182 | sprintf(uptime, "%.2i:%.2i:%.2i", h, m, s); |
---|
183 | |
---|
184 | switch (ROAR_CODEC_BYTE_ORDER(g_sa->codec)) { |
---|
185 | case ROAR_CODEC_LE: |
---|
186 | byteorder = "little-endian"; |
---|
187 | break; |
---|
188 | case ROAR_CODEC_BE: |
---|
189 | byteorder = "big-endian"; |
---|
190 | break; |
---|
191 | case ROAR_CODEC_PDP: |
---|
192 | byteorder = "pdp-endian"; |
---|
193 | break; |
---|
194 | } |
---|
195 | |
---|
196 | roar_vio_printf(vio, |
---|
197 | "+host=%s version=%s uptime=%s " |
---|
198 | "audio-bits=%i audio-byte-order=%s audio-channels=%i " |
---|
199 | "audio-device=internal-mixer " |
---|
200 | "audio-format=linear-%i " |
---|
201 | "audio-fragsize=%i " |
---|
202 | "audio-port=speaker,headphone,lineout audio-rate=10 " |
---|
203 | "audio-sample-rate=%i " |
---|
204 | "volume=254 " |
---|
205 | "curr-rate=10 priority-threshold=0 audio-close=0 audio-device-status=open" |
---|
206 | "\n", |
---|
207 | hostname, version, uptime, |
---|
208 | g_sa->bits, byteorder, g_sa->channels, |
---|
209 | g_sa->bits, |
---|
210 | fragsize, |
---|
211 | g_sa->rate |
---|
212 | ); |
---|
213 | |
---|
214 | return 0; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | int emul_rplay_on_quit(int client, struct emul_rplay_command * cmd, struct roar_vio_calls * vio, struct roar_keyval * kv, size_t kvlen) { |
---|
219 | clients_delete(client); |
---|
220 | return -1; |
---|
221 | } |
---|
222 | |
---|
223 | int emul_rplay_on_help(int client, struct emul_rplay_command * cmd, struct roar_vio_calls * vio, struct roar_keyval * kv, size_t kvlen) { |
---|
224 | struct emul_rplay_command * c; |
---|
225 | |
---|
226 | roar_vio_printf(vio, "+message=\"command summary\" command=help\n"); |
---|
227 | |
---|
228 | for (c = emul_rplay_commands; c->name != NULL; c++) { |
---|
229 | roar_vio_printf(vio, "%-8s %s\n", c->name, c->usage == NULL ? "" : c->usage); |
---|
230 | } |
---|
231 | |
---|
232 | roar_vio_printf(vio, ".\n"); |
---|
233 | |
---|
234 | return -1; |
---|
235 | } |
---|
236 | |
---|
237 | #endif |
---|
238 | |
---|
239 | //ll |
---|