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