Initial Audio plugin
[src/app-framework-binder.git] / plugins / audio / audio-api.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
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
30     ctx = malloc (sizeof(audioCtxHandleT));
31     ctx->volume = 25;
32     ctx->rate = 22050;
33     ctx->channels = 2;
34
35     return ctx;
36 }
37
38 /* called when client session dies [e.g. client quits for more than 15mns] */
39 STATIC json_object* freeAudio (AFB_clientCtx *client) {
40
41     //releaseAudio (client->plugin->handle, client->ctx);
42     free (client->ctx);
43     
44     return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
45 }
46
47
48 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
49
50 STATIC json_object* init (AFB_request *request) {       /* AFB_SESSION_CREATE */
51
52     audioCtxHandleT *ctx;
53     json_object *jresp;
54
55     /* create a private client context */
56     ctx = initAudioCtx();
57     request->client->ctx = (audioCtxHandleT*)ctx;
58     
59     _alsa_init("default", ctx);
60     
61     jresp = json_object_new_object();
62     json_object_object_add (jresp, "token", json_object_new_string (request->client->token));
63 }
64
65
66 STATIC AFB_restapi pluginApis[]= {
67   {"init"   , AFB_SESSION_CREATE, (AFB_apiCB)init       , "Audio API - init"},
68 //  {"error"  , AFB_SESSION_CHECK,   (AFB_apiCB)wrongApi   , "Ping Application Framework"},
69
70   {NULL}
71 };
72
73 PUBLIC AFB_plugin *audioRegister () {
74     AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
75     plugin->type   = AFB_PLUGIN_JSON;
76     plugin->info   = "Application Framework Binder - Audio plugin";
77     plugin->prefix = "audio";        
78     plugin->apis   = pluginApis;
79
80     plugin->freeCtxCB = freeAudio;
81
82     return (plugin);
83 };