2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Manuel Bachmann"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "audio-api.h"
23 #include "audio-alsa.h"
25 #include "audio-pulse.h"
28 #include "afb-plugin.h"
29 #include "afb-req-itf.h"
31 /* ------ BACKEND FUNCTIONS ------- */
33 void _backend_init (const char *name, audioCtxHandleT *ctx) {
35 char *backend_env = getenv ("AFB_AUDIO_OUTPUT");
36 unsigned char res = 0;
39 if (!backend_env || (strcasecmp (backend_env, "Pulse") == 0))
40 res = _pulse_init (name, ctx);
43 res = _alsa_init (name, ctx);
46 fprintf (stderr, "Could not initialize Audio backend\n");
49 void _backend_free (audioCtxHandleT *ctx) {
52 if (ctx->audio_dev) _pulse_free (ctx); else
54 _alsa_free (ctx->name);
57 void _backend_play (audioCtxHandleT *ctx) {
60 if (ctx->audio_dev) _pulse_play (ctx); else
62 _alsa_play (ctx->idx);
65 void _backend_stop (audioCtxHandleT *ctx) {
68 if (ctx->audio_dev) _pulse_stop (ctx); else
70 _alsa_stop (ctx->idx);
73 unsigned int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
76 if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else
78 return _alsa_get_volume (ctx->idx, channel);
81 void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) {
84 if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else
86 _alsa_set_volume (ctx->idx, channel, vol);
89 void _backend_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) {
92 if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else
94 _alsa_set_volume_all (ctx->idx, vol);
97 unsigned char _backend_get_mute (audioCtxHandleT *ctx) {
100 if (ctx->audio_dev) return _pulse_get_mute (ctx); else
102 return _alsa_get_mute (ctx->idx);
105 void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
108 if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else
110 _alsa_set_mute (ctx->idx, mute);
113 void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) {
116 if (ctx->audio_dev) return; else
118 _alsa_set_channels (ctx->idx, channels);
121 /* ------ LOCAL HELPER FUNCTIONS --------- */
123 /* private client context creation ; default values */
124 STATIC audioCtxHandleT* initAudioCtx () {
126 audioCtxHandleT *ctx;
129 ctx = malloc (sizeof(audioCtxHandleT));
130 ctx->audio_dev = NULL;
132 for (i = 0; i < 8; i++)
141 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
146 /* clean client context */
152 /* called when client session dies [e.g. client quits for more than 15mns] */
153 STATIC void freeAudio (void *context) {
158 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
160 STATIC void init (struct afb_req request) { /* AFB_SESSION_CHECK */
164 /* create a private client context */
165 if (!request.context)
166 request.context = initAudioCtx();
168 _backend_init("default", request.context);
170 jresp = json_object_new_object();
171 json_object_object_add (jresp, "info", json_object_new_string ("Audio initialized"));
173 afb_req_success (request, jresp, "Audio initiliazed");
176 STATIC void volume (struct afb_req request) { /* AFB_SESSION_CHECK */
178 audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
179 const char *value = afb_req_argument (request, "value");
181 unsigned int volume[8], i;
183 char volume_str[256];
186 /* no "?value=" parameter : return current state */
188 for (i = 0; i < 8; i++) {
189 ctx->volume[i] = _backend_get_volume (ctx, i);
190 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
191 len_str = strlen(volume_str);
193 jresp = json_object_new_object();
194 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
197 /* "?value=" parameter, set volume */
199 volume_i = strdup (value);
200 volume_i = strtok (volume_i, ",");
201 volume[0] = (unsigned int) atoi (volume_i);
203 if (100 < volume[0]) {
205 //request.errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
206 afb_req_fail (request, "Failed", "Volume must be between 0 and 100");
209 ctx->volume[0] = volume[0];
210 _backend_set_volume (ctx, 0, ctx->volume[0]);
211 snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
213 for (i = 1; i < 8; i++) {
214 volume_i = strtok (NULL, ",");
215 /* if there is only one value, set all channels to this one */
216 if (!volume_i && i == 1)
217 _backend_set_volume_all (ctx, ctx->volume[0]);
218 if (!volume_i || 100 < atoi(volume_i) || atoi(volume_i) < 0) {
219 ctx->volume[i] = _backend_get_volume (ctx, i);
221 ctx->volume[i] = (unsigned int) atoi(volume_i);
222 _backend_set_volume (ctx, i, ctx->volume[i]);
224 len_str = strlen(volume_str);
225 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
227 jresp = json_object_new_object();
228 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
231 afb_req_success (request, jresp, "Audio - Volume changed");
234 STATIC void channels (struct afb_req request) { /* AFB_SESSION_CHECK */
236 audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
237 const char *value = afb_req_argument (request, "value");
238 json_object *jresp = json_object_new_object();
239 char channels_str[256];
241 /* no "?value=" parameter : return current state */
243 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
244 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
247 /* "?value=" parameter, set channels */
249 ctx->channels = (unsigned int) atoi (value);
250 _backend_set_channels (ctx, ctx->channels);
252 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
253 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
256 afb_req_success (request, jresp, "Audio - Channels set");
259 STATIC void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
261 audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
262 const char *value = afb_req_argument (request, "value");
263 json_object *jresp = json_object_new_object();
265 /* no "?value=" parameter : return current state */
267 ctx->mute = _backend_get_mute (ctx);
269 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
270 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
273 /* "?value=" parameter is "1" or "true" */
274 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
276 _backend_set_mute (ctx, ctx->mute);
278 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
281 /* "?value=" parameter is "0" or "false" */
282 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
284 _backend_set_mute (ctx, ctx->mute);
286 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
289 afb_req_success (request, jresp, "Audio - Mute set");
292 STATIC void play (struct afb_req request) { /* AFB_SESSION_CHECK */
294 audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
295 const char *value = afb_req_argument (request, "value");
296 json_object *jresp = json_object_new_object();
298 /* no "?value=" parameter : return current state */
301 json_object_object_add (jresp, "play", json_object_new_string ("on"))
302 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
305 /* "?value=" parameter is "1" or "true" */
306 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
310 json_object_object_add (jresp, "play", json_object_new_string ("on"));
313 /* "?value=" parameter is "0" or "false" */
314 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
318 json_object_object_add (jresp, "play", json_object_new_string ("off"));
321 afb_req_success (request, jresp, "Audio - Play");
324 STATIC void ping (struct afb_req request) { /* AFB_SESSION_NONE */
325 afb_req_success (request, NULL, "Audio - Ping success");
328 STATIC const struct AFB_restapi pluginApis[]= {
329 {"init" , AFB_SESSION_CHECK, init , "Audio API - init"},
330 {"volume" , AFB_SESSION_CHECK, volume , "Audio API - volume"},
331 {"channels", AFB_SESSION_CHECK, channels , "Audio API - channels"},
332 {"mute" , AFB_SESSION_CHECK, mute , "Audio API - mute"},
333 {"play" , AFB_SESSION_CHECK, play , "Audio API - play"},
334 {"ping" , AFB_SESSION_NONE, ping , "Audio API - ping"},
338 STATIC const struct AFB_plugin plug_desc = {
339 .type = AFB_PLUGIN_JSON,
340 .info = "Application Framework Binder - Audio plugin",