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/>.
19 #include "audio-api.h"
21 /* ------ BACKEND FUNCTIONS ------- */
23 void _backend_init (const char *name, audioCtxHandleT *ctx) {
25 char *backend_env = getenv ("AFB_AUDIO_OUTPUT");
26 unsigned char res = -1;
29 if (!backend_env || (strcasecmp (backend_env, "Alsa") != 0))
30 res = _pulse_init (name, ctx);
33 res = _alsa_init (name, ctx);
35 if (res < 0 && verbose)
36 fprintf (stderr, "Could not initialize Audio backend\n");
39 void _backend_free (audioCtxHandleT *ctx) {
42 if (ctx->audio_dev) _pulse_free (ctx); else
44 _alsa_free (ctx->idx);
47 void _backend_play (audioCtxHandleT *ctx) {
50 if (ctx->audio_dev) _pulse_play (ctx); else
52 _alsa_play (ctx->idx);
55 void _backend_stop (audioCtxHandleT *ctx) {
58 if (ctx->audio_dev) _pulse_stop (ctx); else
60 _alsa_stop (ctx->idx);
63 int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
66 if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else
68 return _alsa_get_volume (ctx->idx, channel);
71 void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, int vol) {
74 if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else
76 _alsa_set_volume (ctx->idx, channel, vol);
79 void _backend_set_volume_all (audioCtxHandleT *ctx, int vol) {
82 if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else
84 _alsa_set_volume_all (ctx->idx, vol);
87 unsigned char _backend_get_mute (audioCtxHandleT *ctx) {
90 if (ctx->audio_dev) return _pulse_get_mute (ctx); else
92 return _alsa_get_mute (ctx->idx);
95 void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
98 if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else
100 _alsa_set_mute (ctx->idx, mute);
103 void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) {
106 if (ctx->audio_dev) return; else
108 _alsa_set_channels (ctx->idx, channels);
111 /* ------ LOCAL HELPER FUNCTIONS --------- */
113 /* private client context creation ; default values */
114 STATIC audioCtxHandleT* initAudioCtx () {
116 audioCtxHandleT *ctx;
119 ctx = malloc (sizeof(audioCtxHandleT));
120 ctx->audio_dev = NULL;
122 for (i = 0; i < 8; i++)
131 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
136 /* clean client context */
142 /* called when client session dies [e.g. client quits for more than 15mns] */
143 STATIC void freeAudio (void *context) {
148 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
150 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
155 /* create a private client context */
156 if (!request->context)
157 request->context = initAudioCtx();
159 _backend_init("default", request->context);
161 jresp = json_object_new_object();
162 json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));
166 STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
168 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
169 const char *value = getQueryValue (request, "value");
173 char volume_str[256];
176 /* no "?value=" parameter : return current state */
178 for (i = 0; i < 8; i++) {
179 ctx->volume[i] = _backend_get_volume (ctx, i);
180 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
181 len_str = strlen(volume_str);
183 jresp = json_object_new_object();
184 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
187 /* "?value=" parameter, set volume */
189 volume_i = strdup (value);
190 volume_i = strtok (volume_i, ",");
191 volume[0] = atoi (volume_i);
193 if ((100 < volume[0])||(volume[0] < 0)) {
195 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
196 return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
198 ctx->volume[0] = volume[0];
199 _backend_set_volume (ctx, 0, ctx->volume[0]);
200 snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
202 for (i = 1; i < 8; i++) {
203 volume_i = strtok (NULL, ",");
204 /* if there is only one value, set all channels to this one */
205 if (!volume_i && i == 1)
206 _backend_set_volume_all (ctx, ctx->volume[0]);
207 if (!volume_i || 100 < atoi(volume_i) < 0) {
208 ctx->volume[i] = _backend_get_volume (ctx, i);
210 ctx->volume[i] = atoi(volume_i);
211 _backend_set_volume (ctx, i, ctx->volume[i]);
213 len_str = strlen(volume_str);
214 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
216 jresp = json_object_new_object();
217 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
223 STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
225 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
226 const char *value = getQueryValue (request, "value");
227 json_object *jresp = json_object_new_object();
228 char channels_str[256];
230 /* no "?value=" parameter : return current state */
232 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
233 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
236 /* "?value=" parameter, set channels */
238 ctx->channels = atoi (value);
239 _backend_set_channels (ctx, ctx->channels);
241 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
242 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
248 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
250 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
251 const char *value = getQueryValue (request, "value");
252 json_object *jresp = json_object_new_object();
254 /* no "?value=" parameter : return current state */
256 ctx->mute = _backend_get_mute (ctx);
258 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
259 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
262 /* "?value=" parameter is "1" or "true" */
263 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
265 _backend_set_mute (ctx, ctx->mute);
267 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
270 /* "?value=" parameter is "0" or "false" */
271 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
273 _backend_set_mute (ctx, ctx->mute);
275 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
281 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
283 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
284 const char *value = getQueryValue (request, "value");
285 json_object *jresp = json_object_new_object();
287 /* no "?value=" parameter : return current state */
290 json_object_object_add (jresp, "play", json_object_new_string ("on"))
291 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
294 /* "?value=" parameter is "1" or "true" */
295 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
299 json_object_object_add (jresp, "play", json_object_new_string ("on"));
302 /* "?value=" parameter is "0" or "false" */
303 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
307 json_object_object_add (jresp, "play", json_object_new_string ("off"));
313 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
314 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
317 STATIC AFB_restapi pluginApis[]= {
318 {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Audio API - init"},
319 {"volume" , AFB_SESSION_CHECK, (AFB_apiCB)volume , "Audio API - volume"},
320 {"channels", AFB_SESSION_CHECK, (AFB_apiCB)channels , "Audio API - channels"},
321 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Audio API - mute"},
322 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Audio API - play"},
323 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Audio API - ping"},
327 PUBLIC AFB_plugin *pluginRegister () {
328 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
329 plugin->type = AFB_PLUGIN_JSON;
330 plugin->info = "Application Framework Binder - Audio plugin";
331 plugin->prefix = "audio";
332 plugin->apis = pluginApis;
334 plugin->freeCtxCB = (AFB_freeCtxCB)freeAudio;