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