3f80ef02898faba33c50be42d34c5eb80dc97da3
[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 #define _GNU_SOURCE
20 #include <stdlib.h>
21
22 #include "audio-api.h"
23 #include "audio-alsa.h"
24 #ifdef HAVE_PULSE
25 #include "audio-pulse.h"
26 #endif
27
28 #include "afb-plugin.h"
29 #include "afb-req-itf.h"
30
31 /* ------ BACKEND FUNCTIONS ------- */
32
33 void _backend_init (const char *name, audioCtxHandleT *ctx) {
34
35     char *backend_env = getenv ("AFB_AUDIO_OUTPUT");
36     unsigned char res = 0;
37
38 # ifdef HAVE_PULSE
39     if (!backend_env || (strcasecmp (backend_env, "Pulse") == 0))
40         res = _pulse_init (name, ctx);
41     if (!res)
42 #endif
43     res = _alsa_init (name, ctx);
44
45     if (!res && verbose)
46         fprintf (stderr, "Could not initialize Audio backend\n");
47 }
48
49 void _backend_free (audioCtxHandleT *ctx) {
50
51 # ifdef HAVE_PULSE
52     if (ctx->audio_dev) _pulse_free (ctx); else
53 # endif
54     _alsa_free (ctx->name);
55 }
56
57 void _backend_play (audioCtxHandleT *ctx) {
58
59 # ifdef HAVE_PULSE
60     if (ctx->audio_dev) _pulse_play (ctx); else
61 # endif
62     _alsa_play (ctx->idx);
63 }
64
65 void _backend_stop (audioCtxHandleT *ctx) {
66
67 # ifdef HAVE_PULSE
68     if (ctx->audio_dev) _pulse_stop (ctx); else
69 # endif
70     _alsa_stop (ctx->idx);
71 }
72
73 unsigned int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
74
75 # ifdef HAVE_PULSE
76     if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else
77 # endif
78     return _alsa_get_volume (ctx->idx, channel);
79 }
80
81 void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) {
82
83 # ifdef HAVE_PULSE
84     if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else
85 # endif
86     _alsa_set_volume (ctx->idx, channel, vol);
87 }
88
89 void _backend_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) {
90
91 # ifdef HAVE_PULSE
92     if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else
93 # endif
94     _alsa_set_volume_all (ctx->idx, vol);
95 }
96
97 unsigned char _backend_get_mute (audioCtxHandleT *ctx) {
98
99 # ifdef HAVE_PULSE
100     if (ctx->audio_dev) return _pulse_get_mute (ctx); else
101 # endif
102     return _alsa_get_mute (ctx->idx);
103 }
104
105 void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
106
107 # ifdef HAVE_PULSE
108     if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else
109 # endif
110     _alsa_set_mute (ctx->idx, mute);
111 }
112
113 void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) {
114
115 # ifdef HAVE_PULSE
116     if (ctx->audio_dev) return; else
117 # endif
118     _alsa_set_channels (ctx->idx, channels);
119 }
120
121 /* ------ LOCAL HELPER FUNCTIONS --------- */
122
123 /* private client context creation ; default values */
124 STATIC audioCtxHandleT* initAudioCtx () {
125
126     audioCtxHandleT *ctx;
127     int i;
128
129     ctx = malloc (sizeof(audioCtxHandleT));
130     ctx->audio_dev = NULL;
131     ctx->idx = -1;
132     for (i = 0; i < 8; i++)
133         ctx->volume[i] = 25;
134     ctx->channels = 2;
135     ctx->mute = 0;
136     ctx->is_playing = 0;
137
138     return ctx;
139 }
140
141 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
142
143     /* power it off */
144     _backend_free (ctx);
145
146     /* clean client context */
147     ctx->idx = -1;
148
149     return AFB_SUCCESS;
150 }
151
152 /* called when client session dies [e.g. client quits for more than 15mns] */
153 STATIC void freeAudio (void *context) {
154     free (context);    
155 }
156
157
158 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
159
160 STATIC void init (struct afb_req request) {        /* AFB_SESSION_CHECK */
161
162     json_object *jresp;
163
164     /* create a private client context */
165     if (!request.context)
166         request.context = initAudioCtx();
167
168     _backend_init("default", request.context);
169
170     jresp = json_object_new_object();
171     json_object_object_add (jresp, "info", json_object_new_string ("Audio initialized"));
172
173     afb_req_success (request, jresp, "Audio initiliazed");
174 }
175
176 STATIC void volume (struct afb_req request) {      /* AFB_SESSION_CHECK */
177
178     audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
179     const char *value = afb_req_argument (request, "value");
180     json_object *jresp;
181     unsigned int volume[8], i;
182     char *volume_i;
183     char volume_str[256];
184     size_t len_str = 0;
185
186     /* no "?value=" parameter : return current state */
187     if (!value) {
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);
192         }
193         jresp = json_object_new_object();
194         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
195     }
196
197     /* "?value=" parameter, set volume */
198     else {
199         volume_i = strdup (value);
200         volume_i = strtok (volume_i, ",");
201         volume[0] = (unsigned int) atoi (volume_i);
202
203         if (100 < volume[0]) {
204             free (volume_i);
205             //request.errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
206             afb_req_fail (request, "Failed", "Volume must be between 0 and 100");
207             return;
208         }
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]);
212
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);
220             } else {
221                ctx->volume[i] = (unsigned int) atoi(volume_i);
222                _backend_set_volume (ctx, i, ctx->volume[i]);
223             }
224             len_str = strlen(volume_str);
225             snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
226         }
227         jresp = json_object_new_object();
228         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
229     }
230
231     afb_req_success (request, jresp, "Audio - Volume changed");
232 }
233
234 STATIC void channels (struct afb_req request) {    /* AFB_SESSION_CHECK */
235
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];
240
241     /* no "?value=" parameter : return current state */
242     if (!value) {
243         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
244         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
245     }
246
247     /* "?value=" parameter, set channels */
248     else {
249         ctx->channels = (unsigned int) atoi (value);
250         _backend_set_channels (ctx, ctx->channels);
251
252         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
253         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
254     }
255
256     afb_req_success (request, jresp, "Audio - Channels set");
257 }
258
259 STATIC void mute (struct afb_req request) {        /* AFB_SESSION_CHECK */
260
261     audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
262     const char *value = afb_req_argument (request, "value");
263     json_object *jresp = json_object_new_object();
264
265     /* no "?value=" parameter : return current state */
266     if (!value) {
267         ctx->mute = _backend_get_mute (ctx);
268         ctx->mute ?
269             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
270           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
271     }
272
273     /* "?value=" parameter is "1" or "true" */
274     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
275         ctx->mute = 1;
276         _backend_set_mute (ctx, ctx->mute);
277
278         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
279     }
280
281     /* "?value=" parameter is "0" or "false" */
282     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
283         ctx->mute = 0;
284         _backend_set_mute (ctx, ctx->mute);
285
286         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
287     }
288
289     afb_req_success (request, jresp, "Audio - Mute set");
290 }
291
292 STATIC void play (struct afb_req request) {        /* AFB_SESSION_CHECK */
293
294     audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
295     const char *value = afb_req_argument (request, "value");
296     json_object *jresp = json_object_new_object();
297
298     /* no "?value=" parameter : return current state */
299     if (!value) {
300         ctx->is_playing ?
301             json_object_object_add (jresp, "play", json_object_new_string ("on"))
302           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
303     }
304
305     /* "?value=" parameter is "1" or "true" */
306     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
307         ctx->is_playing = 1;
308         _backend_play (ctx);
309
310         json_object_object_add (jresp, "play", json_object_new_string ("on"));
311     }
312
313     /* "?value=" parameter is "0" or "false" */
314     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
315         ctx->is_playing = 0;
316         _backend_stop (ctx);
317
318         json_object_object_add (jresp, "play", json_object_new_string ("off"));
319     }
320
321     afb_req_success (request, jresp, "Audio - Play");
322 }
323
324 STATIC void ping (struct afb_req request) {         /* AFB_SESSION_NONE */
325     afb_req_success (request, NULL, "Audio - Ping success");
326 }
327
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"},
335   {NULL}
336 };
337
338 STATIC const struct AFB_plugin plug_desc = {
339     .type   = AFB_PLUGIN_JSON,
340     .info   = "Application Framework Binder - Audio plugin",
341     .prefix = "audio",
342     .apis   = pluginApis
343 };