Fix Media Plugin refresh, add seek API
[src/app-framework-binder.git] / plugins / media / media-api.c
1 /*
2  * Copyright (C) 2016 "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 "media-api.h"
20
21 /* ------ LOCAL HELPER FUNCTIONS --------- */
22
23 /* private client context creation ; default values */
24 STATIC mediaCtxHandleT* initMediaCtx () {
25
26     mediaCtxHandleT *ctx;
27
28     ctx = malloc (sizeof(mediaCtxHandleT));
29     ctx->media_server = NULL;
30     ctx->index = 0;
31
32     return ctx;
33 }
34
35 /* called when client session dies [e.g. client quits for more than 15mns] */
36 STATIC void freeMedia (void *context, void *handle) {
37
38     free (context);
39 }
40
41 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
42
43 STATIC json_object* init (AFB_request *request) {        /* AFB_SESSION_CHECK */
44
45     mediaCtxHandleT *ctx;
46     json_object *jresp;
47
48     /* create a private client context */
49     if (!request->context)
50         request->context = initMediaCtx();
51
52     ctx = (mediaCtxHandleT*)request->context;
53
54     /* initialize server connection */
55     if (!ctx->media_server)
56       _rygel_init (request->context);
57
58     jresp = json_object_new_object();
59     json_object_object_add(jresp, "info", json_object_new_string ("Media initialized"));
60     return jresp;
61 }
62
63 STATIC json_object* list (AFB_request *request) {        /* AFB_SESSION_CHECK */
64
65     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
66     json_object *jresp;
67     char *result;
68
69     result = _rygel_list (ctx);
70
71     if (!result)
72       return jsonNewMessage(AFB_FAIL, "No content found in media server");
73
74     jresp = json_object_new_object();
75     json_object_object_add(jresp, "list", json_object_new_string (result));
76     return jresp;
77 }
78
79 STATIC json_object* selecting (AFB_request *request) {   /* AFB_SESSION_CHECK */
80
81     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
82     const char *value = getQueryValue (request, "value");
83     json_object *jresp;
84     unsigned int index;
85     char index_str[5];
86
87     /* no "?value=" parameter : return current index */
88     if (!value) {
89         snprintf (index_str, sizeof(index_str), "%d", ctx->index);
90         jresp = json_object_new_object();
91         json_object_object_add (jresp, "index", json_object_new_string (index_str));
92     }
93
94     /* "?value=" parameter is negative */
95     else if (atoi(value) < 0)
96         return jsonNewMessage(AFB_FAIL, "Chosen index cannot be negative");
97
98     /* "?value=" parameter is positive */
99     else if (atoi(value) >= 0) {
100         index = (unsigned int) atoi(value);
101
102         if (!_rygel_select (ctx, index))
103           return jsonNewMessage(AFB_FAIL, "Chosen index superior to current media count");
104
105         ctx->index = index;
106         jresp = json_object_new_object();
107         json_object_object_add (jresp, "index", json_object_new_string (value));
108     }
109
110     return jresp;
111 }
112
113 STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
114
115     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
116
117     if (!_rygel_do (ctx, PLAY, NULL))
118       return jsonNewMessage(AFB_FAIL, "Could not play chosen media");
119
120     return jsonNewMessage(AFB_SUCCESS, "PLaying media");
121 }
122
123 STATIC json_object* stop (AFB_request *request) {        /* AFB_SESSION_CHECK */
124
125     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
126
127     if (!_rygel_do (ctx, STOP, NULL))
128       return jsonNewMessage(AFB_FAIL, "Could not stop chosen media");
129
130     return jsonNewMessage(AFB_SUCCESS, "Stopped media");
131 }
132
133 STATIC json_object* pausing (AFB_request *request) {     /* AFB_SESSION_CHECK */
134
135     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
136
137     if (!_rygel_do (ctx, PAUSE, NULL))
138       return jsonNewMessage(AFB_FAIL, "Could not pause chosen media");
139
140     return jsonNewMessage(AFB_SUCCESS, "Paused media");
141 }
142
143 STATIC json_object* seek (AFB_request *request) {        /* AFB_SESSION_CHECK */
144
145     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
146     const char *value = getQueryValue (request, "value");
147
148     /* no "?value=" parameter : return error */
149     if (!value)
150       return jsonNewMessage(AFB_FAIL, "You must provide a time");
151
152     if (!_rygel_do (ctx, SEEK, value))
153       return jsonNewMessage(AFB_FAIL, "Could not seek chosen media");
154
155     return jsonNewMessage(AFB_SUCCESS, "Seeked media");
156 }
157
158 STATIC json_object* upload (AFB_request *request) {      /* AFB_SESSION_CHECK */
159
160     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
161     const char *value = getQueryValue (request, "value");
162     json_object *jresp;
163     char path[256];
164
165     /* no "?value=" parameter : return error */
166     if (!value)
167       return jsonNewMessage(AFB_FAIL, "You must provide a file name");
168
169     snprintf (path, sizeof(path), "/tmp/%s", value);
170     if (access (path, R_OK) == -1)
171       return jsonNewMessage(AFB_FAIL, "File not found");
172
173     if (!_rygel_upload (ctx, path))
174       return jsonNewMessage(AFB_FAIL, "Error when uploading file... could not complete");
175
176     return jsonNewMessage(AFB_SUCCESS, "File successfully uploaded");
177 }
178
179 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
180     return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Media API");
181 }
182
183
184 STATIC AFB_restapi pluginApis[]= {
185   {"init"   , AFB_SESSION_CHECK,  (AFB_apiCB)init       , "Media API - init"   },
186   {"list"   , AFB_SESSION_CHECK,  (AFB_apiCB)list       , "Media API - list"   },
187   {"select" , AFB_SESSION_CHECK,  (AFB_apiCB)selecting  , "Media API - select" },
188   {"play"   , AFB_SESSION_CHECK,  (AFB_apiCB)play       , "Media API - play"   },
189   {"stop"   , AFB_SESSION_CHECK,  (AFB_apiCB)stop       , "Media API - stop"   },
190   {"pause"  , AFB_SESSION_CHECK,  (AFB_apiCB)pausing    , "Media API - pause"  },
191   {"seek"   , AFB_SESSION_CHECK,  (AFB_apiCB)seek       , "Media API - seek"   },
192   {"upload" , AFB_SESSION_CHECK,  (AFB_apiCB)upload     , "Media API - upload" },
193   {"ping"   , AFB_SESSION_NONE,   (AFB_apiCB)ping       , "Media API - ping"   },
194   {NULL}
195 };
196
197 PUBLIC AFB_plugin* pluginRegister () {
198     AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
199     plugin->type  = AFB_PLUGIN_JSON;
200     plugin->info  = "Application Framework Binder - Media plugin";
201     plugin->prefix  = "media";
202     plugin->apis  = pluginApis;
203
204     /*plugin->handle = initRadioPlugin();*/
205     plugin->freeCtxCB = (AFB_freeCtxCB)freeMedia;
206
207     return (plugin);
208 };