Audio API: manage multiple-channel volume
[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 #include "audio-alsa.h"
21
22
23 /* ------ LOCAL HELPER FUNCTIONS --------- */
24
25 /* private client context creation ; default values */
26 STATIC audioCtxHandleT* initAudioCtx () {
27
28     audioCtxHandleT *ctx;
29     int i;
30
31     ctx = malloc (sizeof(audioCtxHandleT));
32     ctx->idx = -1;
33     for (i = 0; i < 8; i++)
34         ctx->volume[i] = 25;
35     ctx->channels = 2;
36     ctx->mute = 0;
37     ctx->is_playing = 0;
38
39     return ctx;
40 }
41
42 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
43
44     /* power it off */
45     _alsa_free (ctx->idx);
46
47     /* clean client context */
48     ctx->idx = -1;
49
50     return AFB_SUCCESS;
51 }
52
53 /* called when client session dies [e.g. client quits for more than 15mns] */
54 STATIC json_object* freeAudio (AFB_clientCtx *client) {
55
56     releaseAudio (client->ctx);
57     free (client->ctx);
58     
59     return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
60 }
61
62
63 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
64
65 STATIC json_object* init (AFB_request *request) {       /* AFB_SESSION_CREATE */
66
67     audioCtxHandleT *ctx;
68     json_object *jresp;
69     int idx;
70
71     /* create a private client context */
72     ctx = initAudioCtx();
73     request->client->ctx = (audioCtxHandleT*)ctx;
74     
75     _alsa_init("default", ctx);
76     
77     jresp = json_object_new_object();
78     json_object_object_add (jresp, "token", json_object_new_string (request->client->token));
79     return jresp;
80 }
81
82 STATIC json_object* volume (AFB_request *request) {      /* AFB_SESSION_CHECK */
83
84     audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
85     const char *value = getQueryValue (request, "value");
86     json_object *jresp;
87     int volume[8], i;
88     char *volume_i;
89     char volume_str[256];
90     int len_str = 0;
91
92     /* no "?value=" parameter : return current state */
93     if (!value) {
94         for (i = 0; i < 8; i++) {
95             ctx->volume[i] = _alsa_get_volume (ctx->idx, i);
96             snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
97             len_str = strlen(volume_str);
98         }
99         jresp = json_object_new_object();
100         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
101     }
102
103     /* "?value=" parameter, set volume */
104     else {
105         volume_i = strdup (value);
106         volume_i = strtok (volume_i, ",");
107         volume[0] = atoi (volume_i);
108
109         if (100 < volume[0] < 0) {
110             free (volume_i);
111             request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
112             return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
113         }
114         ctx->volume[0] = volume[0];
115         _alsa_set_volume (ctx->idx, 0, ctx->volume[0]);
116         snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
117
118         for (i = 1; i < 8; i++) {
119             volume_i = strtok (NULL, ",");
120             /* if there is only one value, set all channels to this one */
121             if (!volume_i && i == 1)
122                _alsa_set_volume_all (ctx->idx, ctx->volume[0]);
123             if (!volume_i || 100 < atoi(volume_i) < 0) {
124                ctx->volume[i] = _alsa_get_volume (ctx->idx, i);
125             } else {
126                ctx->volume[i] = atoi(volume_i);
127                _alsa_set_volume (ctx->idx, i, ctx->volume[i]);
128             }
129             len_str = strlen(volume_str);
130             snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
131         }
132         jresp = json_object_new_object();
133         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
134     }
135
136     return jresp;
137 }
138
139 STATIC json_object* channels (AFB_request *request) {    /* AFB_SESSION_CHECK */
140
141     audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
142     const char *value = getQueryValue (request, "value");
143     json_object *jresp = json_object_new_object();
144     char channels_str[256];
145
146     /* no "?value=" parameter : return current state */
147     if (!value) {
148         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
149         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
150     }
151
152     /* "?value=" parameter, set channels */
153     else {
154         ctx->channels = atoi (value);
155         _alsa_set_channels (ctx->idx, ctx->channels);
156
157         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
158         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
159     }
160
161     return jresp;
162 }
163
164 STATIC json_object* mute (AFB_request *request) {        /* AFB_SESSION_CHECK */
165
166     audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
167     const char *value = getQueryValue (request, "value");
168     json_object *jresp = json_object_new_object();
169
170     /* no "?value=" parameter : return current state */
171     if (!value) {
172         ctx->mute = _alsa_get_mute (ctx->idx);
173         ctx->mute ?
174             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
175           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
176     }
177
178     /* "?value=" parameter is "1" or "true" */
179     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
180         ctx->mute = 1;
181         _alsa_set_mute (ctx->idx, ctx->mute);
182
183         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
184     }
185
186     /* "?value=" parameter is "0" or "false" */
187     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
188         ctx->mute = 0;
189         _alsa_set_mute (ctx->idx, ctx->mute);
190
191         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
192     }
193
194     return jresp;
195 }
196
197 STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
198
199     audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
200     const char *value = getQueryValue (request, "value");
201     json_object *jresp = json_object_new_object();
202
203     /* no "?value=" parameter : return current state */
204     if (!value) {
205         ctx->is_playing ?
206             json_object_object_add (jresp, "play", json_object_new_string ("on"))
207           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
208     }
209
210     /* "?value=" parameter is "1" or "true" */
211     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
212         ctx->is_playing = 1;
213         _alsa_play (ctx->idx);
214
215         json_object_object_add (jresp, "play", json_object_new_string ("on"));
216     }
217
218     /* "?value=" parameter is "0" or "false" */
219     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
220         ctx->is_playing = 0;
221         _alsa_stop (ctx->idx);
222
223         json_object_object_add (jresp, "play", json_object_new_string ("off"));
224     }
225
226     return jresp;
227 }
228
229 STATIC json_object* refresh (AFB_request *request) {     /* AFB_SESSION_RENEW */
230     json_object *jresp = json_object_new_object();
231     json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
232     return jresp;
233 }
234
235 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
236     return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
237 }
238
239
240 STATIC AFB_restapi pluginApis[]= {
241   {"init"    , AFB_SESSION_CREATE, (AFB_apiCB)init      , "Audio API - init"},
242   {"volume"  , AFB_SESSION_CHECK,  (AFB_apiCB)volume    , "Audio API - volume"},
243   {"channels", AFB_SESSION_CHECK,  (AFB_apiCB)channels  , "Audio API - channels"},
244   {"mute"    , AFB_SESSION_CHECK,  (AFB_apiCB)mute      , "Audio API - mute"},
245   {"play"    , AFB_SESSION_CHECK,  (AFB_apiCB)play      , "Audio API - play"},
246   {"refresh" , AFB_SESSION_RENEW,  (AFB_apiCB)refresh   , "Audio API - refresh"},
247   {"ping"    , AFB_SESSION_NONE,   (AFB_apiCB)ping      , "Audio API - ping"},
248   {NULL}
249 };
250
251 PUBLIC AFB_plugin *pluginRegister () {
252     AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
253     plugin->type   = AFB_PLUGIN_JSON;
254     plugin->info   = "Application Framework Binder - Audio plugin";
255     plugin->prefix = "audio";        
256     plugin->apis   = pluginApis;
257
258     plugin->freeCtxCB = freeAudio;
259
260     return (plugin);
261 };