2 * Copyright (C) 2016 "IoT.bzh"
3 * Author "Manuel Bachmann"
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.
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.
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/>.
19 #include "media-api.h"
21 /* ------ LOCAL HELPER FUNCTIONS --------- */
23 /* private client context creation ; default values */
24 STATIC mediaCtxHandleT* initMediaCtx () {
28 ctx = malloc (sizeof(mediaCtxHandleT));
29 ctx->media_server = NULL;
35 /* called when client session dies [e.g. client quits for more than 15mns] */
36 STATIC void freeMedia (void *context) {
41 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
43 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
48 /* create a private client context */
49 if (!request->context)
50 request->context = initMediaCtx();
52 ctx = (mediaCtxHandleT*)request->context;
54 /* initialize server connection */
55 if (!ctx->media_server)
56 _rygel_init (request->context);
58 jresp = json_object_new_object();
59 json_object_object_add(jresp, "info", json_object_new_string ("Media initialized"));
63 STATIC json_object* list (AFB_request *request) { /* AFB_SESSION_CHECK */
65 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
68 jresp = _rygel_list (ctx);
71 return jsonNewMessage(AFB_FAIL, "No content found in media server");
76 STATIC json_object* selecting (AFB_request *request) { /* AFB_SESSION_CHECK */
78 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
79 const char *value = getQueryValue (request, "value");
84 /* no "?value=" parameter : return current index */
86 snprintf (index_str, sizeof(index_str), "%d", ctx->index);
87 jresp = json_object_new_object();
88 json_object_object_add (jresp, "index", json_object_new_string (index_str));
91 /* "?value=" parameter is negative */
92 else if (atoi(value) < 0)
93 return jsonNewMessage(AFB_FAIL, "Chosen index cannot be negative");
95 /* "?value=" parameter is positive */
96 else if (atoi(value) >= 0) {
97 index = (unsigned int) atoi(value);
99 if (!_rygel_select (ctx, index))
100 return jsonNewMessage(AFB_FAIL, "Chosen index superior to current media count");
103 jresp = json_object_new_object();
104 json_object_object_add (jresp, "index", json_object_new_string (value));
112 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
114 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
116 if (!_rygel_do (ctx, PLAY, NULL))
117 return jsonNewMessage(AFB_FAIL, "Could not play chosen media");
119 return jsonNewMessage(AFB_SUCCESS, "PLaying media");
122 STATIC json_object* stop (AFB_request *request) { /* AFB_SESSION_CHECK */
124 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
126 if (!_rygel_do (ctx, STOP, NULL))
127 return jsonNewMessage(AFB_FAIL, "Could not stop chosen media");
129 return jsonNewMessage(AFB_SUCCESS, "Stopped media");
132 STATIC json_object* pausing (AFB_request *request) { /* AFB_SESSION_CHECK */
134 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
136 if (!_rygel_do (ctx, PAUSE, NULL))
137 return jsonNewMessage(AFB_FAIL, "Could not pause chosen media");
139 return jsonNewMessage(AFB_SUCCESS, "Paused media");
142 STATIC json_object* seek (AFB_request *request) { /* AFB_SESSION_CHECK */
144 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
145 const char *value = getQueryValue (request, "value");
147 /* no "?value=" parameter : return error */
149 return jsonNewMessage(AFB_FAIL, "You must provide a time");
151 if (!_rygel_do (ctx, SEEK, value))
152 return jsonNewMessage(AFB_FAIL, "Could not seek chosen media");
154 return jsonNewMessage(AFB_SUCCESS, "Seeked media");
157 STATIC json_object* upload (AFB_request *request, AFB_PostItem *item) { /* AFB_SESSION_CHECK */
159 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
160 AFB_PostCtx *postFileCtx;
164 /* item is !NULL until transfer is complete */
166 return getPostFile (request, item, "media");
168 /* target intermediary file path */
169 path = getPostPath (request);
172 fprintf (stderr, "Error encoutered during intermediary file transfer\n");
174 else if (!_rygel_upload (ctx, path)) {
175 request->errcode = MHD_HTTP_EXPECTATION_FAILED;
176 request->jresp = jsonNewMessage (AFB_FAIL, "Error when uploading file to media server... could not complete");
180 request->errcode = MHD_HTTP_OK;
181 request->jresp = jsonNewMessage (AFB_SUCCESS, "upload=%s done", path);
184 /* finalizes file transfer */
185 return getPostFile (request, item, NULL);
188 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
189 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Media API");
193 STATIC AFB_restapi pluginApis[]= {
194 {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Media API - init" },
195 {"list" , AFB_SESSION_CHECK, (AFB_apiCB)list , "Media API - list" },
196 {"select" , AFB_SESSION_CHECK, (AFB_apiCB)selecting , "Media API - select" },
197 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Media API - play" },
198 {"stop" , AFB_SESSION_CHECK, (AFB_apiCB)stop , "Media API - stop" },
199 {"pause" , AFB_SESSION_CHECK, (AFB_apiCB)pausing , "Media API - pause" },
200 {"seek" , AFB_SESSION_CHECK, (AFB_apiCB)seek , "Media API - seek" },
201 {"upload" , AFB_SESSION_CHECK, (AFB_apiCB)upload , "Media API - upload" },
202 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Media API - ping" },
206 PUBLIC AFB_plugin* pluginRegister () {
207 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
208 plugin->type = AFB_PLUGIN_JSON;
209 plugin->info = "Application Framework Binder - Media plugin";
210 plugin->prefix = "media";
211 plugin->apis = pluginApis;
213 /*plugin->handle = initRadioPlugin();*/
214 plugin->freeCtxCB = (AFB_freeCtxCB)freeMedia;