9c9da2514960cdf13ff55ef2fc5009229c6fcb02
[src/app-framework-binder.git] / plugins / audio / audio-api.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Manuel Bachmann"
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "audio-api.h"
20
21 /* ------ BACKEND FUNCTIONS ------- */
22
23 void _backend_init (const char *name, audioCtxHandleT *ctx) {
24
25     char *backend_env = getenv ("AFB_AUDIO_OUTPUT");
26     unsigned char res = -1;
27
28 # ifdef HAVE_PULSE
29     if (!backend_env || (strcasecmp (backend_env, "Alsa") != 0))
30         res = _pulse_init (name, ctx);
31     if (res < 0)
32 #endif
33     res = _alsa_init (name, ctx);
34
35     if (res < 0 && verbose)
36         fprintf (stderr, "Could not initialize Audio backend\n");
37 }
38
39 void _backend_free (audioCtxHandleT *ctx) {
40
41 # ifdef HAVE_PULSE
42     if (ctx->audio_dev) _pulse_free (ctx); else
43 # endif
44     _alsa_free (ctx->idx);
45 }
46
47 void _backend_play (audioCtxHandleT *ctx) {
48
49 # ifdef HAVE_PULSE
50     if (ctx->audio_dev) _pulse_play (ctx); else
51 # endif
52     _alsa_play (ctx->idx);
53 }
54
55 void _backend_stop (audioCtxHandleT *ctx) {
56
57 # ifdef HAVE_PULSE
58     if (ctx->audio_dev) _pulse_stop (ctx); else
59 # endif
60     _alsa_stop (ctx->idx);
61 }
62
63 int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
64
65 # ifdef HAVE_PULSE
66     if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else
67 # endif
68     return _alsa_get_volume (ctx->idx, channel);
69 }
70
71 void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, int vol) {
72
73 # ifdef HAVE_PULSE
74     if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else
75 # endif
76     _alsa_set_volume (ctx->idx, channel, vol);
77 }
78
79 void _backend_set_volume_all (audioCtxHandleT *ctx, int vol) {
80
81 # ifdef HAVE_PULSE
82     if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else
83 # endif
84     _alsa_set_volume_all (ctx->idx, vol);
85 }
86
87 unsigned char _backend_get_mute (audioCtxHandleT *ctx) {
88
89 # ifdef HAVE_PULSE
90     if (ctx->audio_dev) return _pulse_get_mute (ctx); else
91 # endif
92     return _alsa_get_mute (ctx->idx);
93 }
94
95 void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
96
97 # ifdef HAVE_PULSE
98     if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else
99 # endif
100     _alsa_set_mute (ctx->idx, mute);
101 }
102
103 void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) {
104
105 # ifdef HAVE_PULSE
106     if (ctx->audio_dev) return; else
107 # endif
108     _alsa_set_channels (ctx->idx, channels);
109 }
110
111 /* ------ LOCAL HELPER FUNCTIONS --------- */
112
113 /* private client context creation ; default values */
114 STATIC audioCtxHandleT* initAudioCtx () {
115
116     audioCtxHandleT *ctx;
117     int i;
118
119     ctx = malloc (sizeof(audioCtxHandleT));
120     ctx->audio_dev = NULL;
121     ctx->idx = -1;
122     for (i = 0; i < 8; i++)
123         ctx->volume[i] = 25;
124     ctx->channels = 2;
125     ctx->mute = 0;
126     ctx->is_playing = 0;
127
128     return ctx;
129 }
130
131 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
132
133     /* power it off */
134     _backend_free (ctx);
135
136     /* clean client context */
137     ctx->idx = -1;
138
139     return AFB_SUCCESS;
140 }
141
142 /* called when client session dies [e.g. client quits for more than 15mns] */
143 STATIC void freeAudio (void *context) {
144     free (context);    
145 }
146
147
148 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
149
150 STATIC json_object* init (AFB_request *request) {        /* AFB_SESSION_CHECK */
151
152     json_object *jresp;
153     int idx;
154
155     /* create a private client context */
156     if (!request->context)
157         request->context = initAudioCtx();
158
159     _backend_init("default", request->context);
160
161     jresp = json_object_new_object();
162     json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));
163     return (jresp);
164 }
165
166 STATIC json_object* volume (AFB_request *request) {      /* AFB_SESSION_CHECK */
167
168     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
169     const char *value = getQueryValue (request, "value");
170     json_object *jresp;
171     int volume[8], i;
172     char *volume_i;
173     char volume_str[256];
174     int len_str = 0;
175
176     /* no "?value=" parameter : return current state */
177     if (!value) {
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);
182         }
183         jresp = json_object_new_object();
184         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
185     }
186
187     /* "?value=" parameter, set volume */
188     else {
189         volume_i = strdup (value);
190         volume_i = strtok (volume_i, ",");
191         volume[0] = atoi (volume_i);
192
193         if ((100 < volume[0])||(volume[0] < 0)) {
194             free (volume_i);
195             request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
196             return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
197         }
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]);
201
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);
209             } else {
210                ctx->volume[i] = atoi(volume_i);
211                _backend_set_volume (ctx, i, ctx->volume[i]);
212             }
213             len_str = strlen(volume_str);
214             snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
215         }
216         jresp = json_object_new_object();
217         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
218     }
219
220     return jresp;
221 }
222
223 STATIC json_object* channels (AFB_request *request) {    /* AFB_SESSION_CHECK */
224
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];
229
230     /* no "?value=" parameter : return current state */
231     if (!value) {
232         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
233         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
234     }
235
236     /* "?value=" parameter, set channels */
237     else {
238         ctx->channels = atoi (value);
239         _backend_set_channels (ctx, ctx->channels);
240
241         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
242         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
243     }
244
245     return jresp;
246 }
247
248 STATIC json_object* mute (AFB_request *request) {        /* AFB_SESSION_CHECK */
249
250     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
251     const char *value = getQueryValue (request, "value");
252     json_object *jresp = json_object_new_object();
253
254     /* no "?value=" parameter : return current state */
255     if (!value) {
256         ctx->mute = _backend_get_mute (ctx);
257         ctx->mute ?
258             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
259           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
260     }
261
262     /* "?value=" parameter is "1" or "true" */
263     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
264         ctx->mute = 1;
265         _backend_set_mute (ctx, ctx->mute);
266
267         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
268     }
269
270     /* "?value=" parameter is "0" or "false" */
271     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
272         ctx->mute = 0;
273         _backend_set_mute (ctx, ctx->mute);
274
275         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
276     }
277
278     return jresp;
279 }
280
281 STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
282
283     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
284     const char *value = getQueryValue (request, "value");
285     json_object *jresp = json_object_new_object();
286
287     /* no "?value=" parameter : return current state */
288     if (!value) {
289         ctx->is_playing ?
290             json_object_object_add (jresp, "play", json_object_new_string ("on"))
291           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
292     }
293
294     /* "?value=" parameter is "1" or "true" */
295     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
296         ctx->is_playing = 1;
297         _backend_play (ctx);
298
299         json_object_object_add (jresp, "play", json_object_new_string ("on"));
300     }
301
302     /* "?value=" parameter is "0" or "false" */
303     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
304         ctx->is_playing = 0;
305         _backend_stop (ctx);
306
307         json_object_object_add (jresp, "play", json_object_new_string ("off"));
308     }
309
310     return jresp;
311 }
312
313 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
314     return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
315 }
316
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"},
324   {NULL}
325 };
326
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;
333
334     plugin->freeCtxCB = (AFB_freeCtxCB)freeAudio;
335
336     return (plugin);
337 };