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