1ba9126c672a1d2782e89546aef235a8e7547620
[src/app-framework-binder.git] / plugins / audio / audio-api.c
1 /*
2  * Copyright (C) 2015, 2016 "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 #include <json-c/json.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/afb-plugin.h>
29 #include <afb/afb-req-itf.h>
30
31 /* ------ BACKEND FUNCTIONS ------- */
32
33 unsigned char _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)
46         fprintf (stderr, "Could not initialize Audio backend\n");
47
48     return res;
49 }
50
51 void _backend_free (audioCtxHandleT *ctx) {
52
53 # ifdef HAVE_PULSE
54     if (ctx->audio_dev) _pulse_free (ctx); else
55 # endif
56     _alsa_free (ctx->name);
57 }
58
59 void _backend_play (audioCtxHandleT *ctx) {
60
61 # ifdef HAVE_PULSE
62     if (ctx->audio_dev) _pulse_play (ctx); else
63 # endif
64     _alsa_play (ctx->idx);
65 }
66
67 void _backend_stop (audioCtxHandleT *ctx) {
68
69 # ifdef HAVE_PULSE
70     if (ctx->audio_dev) _pulse_stop (ctx); else
71 # endif
72     _alsa_stop (ctx->idx);
73 }
74
75 unsigned int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
76
77 # ifdef HAVE_PULSE
78     if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else
79 # endif
80     return _alsa_get_volume (ctx->idx, channel);
81 }
82
83 void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) {
84
85 # ifdef HAVE_PULSE
86     if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else
87 # endif
88     _alsa_set_volume (ctx->idx, channel, vol);
89 }
90
91 void _backend_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) {
92
93 # ifdef HAVE_PULSE
94     if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else
95 # endif
96     _alsa_set_volume_all (ctx->idx, vol);
97 }
98
99 unsigned char _backend_get_mute (audioCtxHandleT *ctx) {
100
101 # ifdef HAVE_PULSE
102     if (ctx->audio_dev) return _pulse_get_mute (ctx); else
103 # endif
104     return _alsa_get_mute (ctx->idx);
105 }
106
107 void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
108
109 # ifdef HAVE_PULSE
110     if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else
111 # endif
112     _alsa_set_mute (ctx->idx, mute);
113 }
114
115 void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) {
116
117 # ifdef HAVE_PULSE
118     if (ctx->audio_dev) return; else
119 # endif
120     _alsa_set_channels (ctx->idx, channels);
121 }
122
123 /* ------ LOCAL HELPER FUNCTIONS --------- */
124
125 /* private client context constructor ; default values */
126 static audioCtxHandleT* initAudioCtx () {
127
128     audioCtxHandleT *ctx;
129     int i;
130
131     ctx = malloc (sizeof(audioCtxHandleT));
132     ctx->audio_dev = NULL;
133     ctx->name = NULL;
134     ctx->idx = -1;
135     for (i = 0; i < 8; i++)
136         ctx->volume[i] = 25;
137     ctx->channels = 2;
138     ctx->mute = 0;
139     ctx->is_playing = 0;
140
141     return ctx;
142 }
143
144 static void releaseAudioCtx (void *context) {
145
146     audioCtxHandleT *ctx = (audioCtxHandleT*) context;
147
148     /* power it off */
149     _backend_free (ctx);
150
151     /* clean client context */
152     ctx->audio_dev = NULL;
153     if (ctx->name)
154                 free (ctx->name);
155     ctx->idx = -1;
156     free (ctx);
157 }
158
159
160 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
161
162 static void init (struct afb_req request) {        /* AFB_SESSION_CHECK */
163
164     audioCtxHandleT *ctx = afb_req_context_get (request);
165     json_object *jresp;
166
167     /* create a private client context */
168         if (!ctx) {
169         ctx = initAudioCtx();
170         afb_req_context_set (request, ctx, releaseAudioCtx);
171     }
172
173     if (!_backend_init ("default", ctx))
174         afb_req_fail (request, "failed", "backend initialization failed");
175
176     jresp = json_object_new_object();
177     json_object_object_add (jresp, "init", json_object_new_string ("success"));
178     afb_req_success (request, jresp, "Audio initialized");
179 }
180
181 static void volume (struct afb_req request) {      /* AFB_SESSION_CHECK */
182
183     audioCtxHandleT *ctx = afb_req_context_get (request);
184     const char *value = afb_req_value (request, "value");
185     json_object *jresp;
186     unsigned int volume[8], i;
187     char *volume_i;
188     char volume_str[256];
189     size_t len_str = 0;
190
191         if (!ctx) {
192         afb_req_fail (request, "failed", "you must call 'init' first");
193         return;
194     }
195     jresp = json_object_new_object();
196
197     /* no "?value=" parameter : return current state */
198     if (!value) {
199         for (i = 0; i < 8; i++) {
200             ctx->volume[i] = _backend_get_volume (ctx, i);
201             snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
202             len_str = strlen (volume_str);
203         }
204         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
205         afb_req_success (request, jresp, "Audio - Volume obtained");
206         return;
207     }
208
209     /* "?value=" parameter, set volume */
210     else {
211         volume_i = strdup (value);
212         volume_i = strtok (volume_i, ",");
213         volume[0] = (unsigned int) atoi (volume_i);
214
215         if (100 < volume[0]) {
216             free (volume_i);
217             afb_req_fail (request, "failed", "volume must be between 0 and 100");
218             return;
219         }
220         ctx->volume[0] = volume[0];
221         _backend_set_volume (ctx, 0, ctx->volume[0]);
222         snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
223
224         for (i = 1; i < 8; i++) {
225             volume_i = strtok (NULL, ",");
226             /* if there is only one value, set all channels to this one */
227             if (!volume_i && i == 1)
228                _backend_set_volume_all (ctx, ctx->volume[0]);
229             if (!volume_i || 100 < atoi(volume_i) || atoi(volume_i) < 0)
230                ctx->volume[i] = _backend_get_volume (ctx, i);
231             else {
232                ctx->volume[i] = (unsigned int) atoi(volume_i);
233                _backend_set_volume (ctx, i, ctx->volume[i]);
234             }
235             len_str = strlen(volume_str);
236             snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
237         }
238         free (volume_i);
239         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
240     }
241
242     afb_req_success (request, jresp, "Audio - Volume changed");
243 }
244
245 static void channels (struct afb_req request) {    /* AFB_SESSION_CHECK */
246
247     audioCtxHandleT *ctx = afb_req_context_get (request);
248     const char *value = afb_req_value (request, "value");
249     json_object *jresp;
250     char channels_str[256];
251
252         if (!ctx) {
253         afb_req_fail (request, "failed", "you must call 'init' first");
254         return;
255     }
256     jresp = json_object_new_object();
257
258     /* no "?value=" parameter : return current state */
259     if (!value) {
260         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
261
262         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
263         afb_req_success (request, jresp, "Audio - Channels obtained");
264         return;
265     }
266
267     /* "?value=" parameter, set channels */
268     else {
269         ctx->channels = (unsigned int) atoi (value);
270         _backend_set_channels (ctx, ctx->channels);
271         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
272
273         jresp = json_object_new_object();
274         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
275     }
276
277     afb_req_success (request, jresp, "Audio - Channels set");
278 }
279
280 static void mute (struct afb_req request) {        /* AFB_SESSION_CHECK */
281
282     audioCtxHandleT *ctx = afb_req_context_get (request);
283     const char *value = afb_req_value (request, "value");
284     json_object *jresp;
285
286         if (!ctx) {
287         afb_req_fail (request, "failed", "you must call 'init' first");
288         return;
289     }
290     jresp = json_object_new_object();
291
292     /* no "?value=" parameter : return current state */
293     if (!value) {
294         ctx->mute = _backend_get_mute (ctx);
295         ctx->mute ?
296             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
297           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
298         afb_req_success (request, jresp, "Audio - Mute status obtained");
299         return;
300     }
301
302     /* "?value=" parameter is "1" or "true" */
303     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
304         ctx->mute = 1;
305         _backend_set_mute (ctx, ctx->mute);
306         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
307     }
308
309     /* "?value=" parameter is "0" or "false" */
310     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
311         ctx->mute = 0;
312         _backend_set_mute (ctx, ctx->mute);
313         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
314     }
315
316     afb_req_success (request, jresp, "Audio - Mute set");
317 }
318
319 static void play (struct afb_req request) {        /* AFB_SESSION_CHECK */
320
321     audioCtxHandleT *ctx = afb_req_context_get (request);
322     const char *value = afb_req_value (request, "value");
323     json_object *jresp;
324
325         if (!ctx) {
326         afb_req_fail (request, "failed", "you must call 'init' first");
327         return;
328     }
329     jresp = json_object_new_object();
330
331     /* no "?value=" parameter : return current state */
332     if (!value) {
333         ctx->is_playing ?
334             json_object_object_add (jresp, "play", json_object_new_string ("on"))
335           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
336         afb_req_success (request, jresp, "Audio - Playing status obtained");
337         return;
338     }
339
340     /* "?value=" parameter is "1" or "true" */
341     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
342         ctx->is_playing = 1;
343         _backend_play (ctx);
344         json_object_object_add (jresp, "play", json_object_new_string ("on"));
345     }
346
347     /* "?value=" parameter is "0" or "false" */
348     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
349         ctx->is_playing = 0;
350         _backend_stop (ctx);
351         json_object_object_add (jresp, "play", json_object_new_string ("off"));
352     }
353
354     afb_req_success (request, jresp, "Audio - Play");
355 }
356
357 static void ping (struct afb_req request) {         /* AFB_SESSION_NONE */
358     afb_req_success (request, NULL, "Audio - Ping success");
359 }
360
361 static const struct AFB_verb_desc_v1 verbs[] = {
362   {"init"    , AFB_SESSION_CHECK,  init      , "Audio API - init"},
363   {"volume"  , AFB_SESSION_CHECK,  volume    , "Audio API - volume"},
364   {"channels", AFB_SESSION_CHECK,  channels  , "Audio API - channels"},
365   {"mute"    , AFB_SESSION_CHECK,  mute      , "Audio API - mute"},
366   {"play"    , AFB_SESSION_CHECK,  play      , "Audio API - play"},
367   {"ping"    , AFB_SESSION_NONE,   ping      , "Audio API - ping"},
368   {NULL}
369 };
370
371 static const struct AFB_plugin pluginDesc = {
372     .type   = AFB_PLUGIN_VERSION_1,
373     .v1 = {
374         .info   = "Application Framework Binder - Audio plugin",
375         .prefix = "audio",
376         .verbs   = verbs
377     }
378 };
379
380 const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf)
381 {
382         return &pluginDesc;
383 }