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