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 "afb-plugin.h"
26 #include "afb-req-itf.h"
28 /* ------ BACKEND FUNCTIONS ------- */
30 void _backend_init (const char *name, audioCtxHandleT *ctx) {
32 char *backend_env = getenv ("AFB_AUDIO_OUTPUT");
33 unsigned char res = 0;
36 if (!backend_env || (strcasecmp (backend_env, "Pulse") == 0))
37 res = _pulse_init (name, ctx);
40 res = _alsa_init (name, ctx);
43 fprintf (stderr, "Could not initialize Audio backend\n");
46 void _backend_free (audioCtxHandleT *ctx) {
49 if (ctx->audio_dev) _pulse_free (ctx); else
51 _alsa_free (ctx->name);
54 void _backend_play (audioCtxHandleT *ctx) {
57 if (ctx->audio_dev) _pulse_play (ctx); else
59 _alsa_play (ctx->idx);
62 void _backend_stop (audioCtxHandleT *ctx) {
65 if (ctx->audio_dev) _pulse_stop (ctx); else
67 _alsa_stop (ctx->idx);
70 unsigned int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
73 if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else
75 return _alsa_get_volume (ctx->idx, channel);
78 void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) {
81 if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else
83 _alsa_set_volume (ctx->idx, channel, vol);
86 void _backend_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) {
89 if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else
91 _alsa_set_volume_all (ctx->idx, vol);
94 unsigned char _backend_get_mute (audioCtxHandleT *ctx) {
97 if (ctx->audio_dev) return _pulse_get_mute (ctx); else
99 return _alsa_get_mute (ctx->idx);
102 void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
105 if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else
107 _alsa_set_mute (ctx->idx, mute);
110 void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) {
113 if (ctx->audio_dev) return; else
115 _alsa_set_channels (ctx->idx, channels);
118 /* ------ LOCAL HELPER FUNCTIONS --------- */
120 /* private client context creation ; default values */
121 STATIC audioCtxHandleT* initAudioCtx () {
123 audioCtxHandleT *ctx;
126 ctx = malloc (sizeof(audioCtxHandleT));
127 ctx->audio_dev = NULL;
129 for (i = 0; i < 8; i++)
138 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
143 /* clean client context */
149 /* called when client session dies [e.g. client quits for more than 15mns] */
150 STATIC void freeAudio (void *context) {
155 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
157 STATIC void init (struct afb_req request) { /* AFB_SESSION_CHECK */
161 /* create a private client context */
162 if (!request.context)
163 request.context = initAudioCtx();
165 _backend_init("default", request.context);
167 jresp = json_object_new_object();
168 json_object_object_add (jresp, "info", json_object_new_string ("Audio initialized"));
170 afb_req_success (request, jresp, "Audio initiliazed");
173 STATIC void volume (struct afb_req request) { /* AFB_SESSION_CHECK */
175 audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
176 const char *value = afb_req_argument (request, "value");
178 unsigned int volume[8], i;
180 char volume_str[256];
183 /* no "?value=" parameter : return current state */
185 for (i = 0; i < 8; i++) {
186 ctx->volume[i] = _backend_get_volume (ctx, i);
187 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
188 len_str = strlen(volume_str);
190 jresp = json_object_new_object();
191 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
194 /* "?value=" parameter, set volume */
196 volume_i = strdup (value);
197 volume_i = strtok (volume_i, ",");
198 volume[0] = (unsigned int) atoi (volume_i);
200 if (100 < volume[0]) {
202 //request.errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
203 afb_req_fail (request, "Failed", "Volume must be between 0 and 100");
206 ctx->volume[0] = volume[0];
207 _backend_set_volume (ctx, 0, ctx->volume[0]);
208 snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
210 for (i = 1; i < 8; i++) {
211 volume_i = strtok (NULL, ",");
212 /* if there is only one value, set all channels to this one */
213 if (!volume_i && i == 1)
214 _backend_set_volume_all (ctx, ctx->volume[0]);
215 if (!volume_i || 100 < atoi(volume_i) || atoi(volume_i) < 0) {
216 ctx->volume[i] = _backend_get_volume (ctx, i);
218 ctx->volume[i] = (unsigned int) atoi(volume_i);
219 _backend_set_volume (ctx, i, ctx->volume[i]);
221 len_str = strlen(volume_str);
222 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
224 jresp = json_object_new_object();
225 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
228 afb_req_success (request, jresp, "Audio - Volume changed");
231 STATIC void channels (struct afb_req request) { /* AFB_SESSION_CHECK */
233 audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
234 const char *value = afb_req_argument (request, "value");
235 json_object *jresp = json_object_new_object();
236 char channels_str[256];
238 /* no "?value=" parameter : return current state */
240 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
241 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
244 /* "?value=" parameter, set channels */
246 ctx->channels = (unsigned int) atoi (value);
247 _backend_set_channels (ctx, ctx->channels);
249 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
250 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
253 afb_req_success (request, jresp, "Audio - Channels set");
256 STATIC void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
258 audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
259 const char *value = afb_req_argument (request, "value");
260 json_object *jresp = json_object_new_object();
262 /* no "?value=" parameter : return current state */
264 ctx->mute = _backend_get_mute (ctx);
266 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
267 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
270 /* "?value=" parameter is "1" or "true" */
271 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
273 _backend_set_mute (ctx, ctx->mute);
275 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
278 /* "?value=" parameter is "0" or "false" */
279 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
281 _backend_set_mute (ctx, ctx->mute);
283 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
286 afb_req_success (request, jresp, "Audio - Mute set");
289 STATIC void play (struct afb_req request) { /* AFB_SESSION_CHECK */
291 audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
292 const char *value = afb_req_argument (request, "value");
293 json_object *jresp = json_object_new_object();
295 /* no "?value=" parameter : return current state */
298 json_object_object_add (jresp, "play", json_object_new_string ("on"))
299 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
302 /* "?value=" parameter is "1" or "true" */
303 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
307 json_object_object_add (jresp, "play", json_object_new_string ("on"));
310 /* "?value=" parameter is "0" or "false" */
311 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
315 json_object_object_add (jresp, "play", json_object_new_string ("off"));
318 afb_req_success (request, jresp, "Audio - Play");
321 STATIC void ping (struct afb_req request) { /* AFB_SESSION_NONE */
322 afb_req_success (request, NULL, "Audio - Ping success");
325 STATIC const struct AFB_restapi pluginApis[]= {
326 {"init" , AFB_SESSION_CHECK, init , "Audio API - init"},
327 {"volume" , AFB_SESSION_CHECK, volume , "Audio API - volume"},
328 {"channels", AFB_SESSION_CHECK, channels , "Audio API - channels"},
329 {"mute" , AFB_SESSION_CHECK, mute , "Audio API - mute"},
330 {"play" , AFB_SESSION_CHECK, play , "Audio API - play"},
331 {"ping" , AFB_SESSION_NONE, ping , "Audio API - ping"},
335 STATIC const struct AFB_plugin plug_desc = {
336 .type = AFB_PLUGIN_JSON,
337 .info = "Application Framework Binder - Audio plugin",