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, void *handle) {
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;
69 result = _rygel_list (ctx);
72 return jsonNewMessage(AFB_FAIL, "No content found in media server");
74 jresp = json_object_new_object();
75 json_object_object_add(jresp, "list", json_object_new_string (result));
79 STATIC json_object* selecting (AFB_request *request) { /* AFB_SESSION_CHECK */
81 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
82 const char *value = getQueryValue (request, "value");
87 /* no "?value=" parameter : return current index */
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));
94 /* "?value=" parameter is negative */
95 else if (atoi(value) < 0)
96 return jsonNewMessage(AFB_FAIL, "Chosen index cannot be negative");
98 /* "?value=" parameter is positive */
99 else if (atoi(value) >= 0) {
100 index = (unsigned int) atoi(value);
102 if (!_rygel_select (ctx, index))
103 return jsonNewMessage(AFB_FAIL, "Chosen index superior to current media count");
106 jresp = json_object_new_object();
107 json_object_object_add (jresp, "index", json_object_new_string (value));
113 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
115 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
117 if (!_rygel_do (ctx, PLAY, NULL))
118 return jsonNewMessage(AFB_FAIL, "Could not play chosen media");
120 return jsonNewMessage(AFB_SUCCESS, "PLaying media");
123 STATIC json_object* stop (AFB_request *request) { /* AFB_SESSION_CHECK */
125 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
127 if (!_rygel_do (ctx, STOP, NULL))
128 return jsonNewMessage(AFB_FAIL, "Could not stop chosen media");
130 return jsonNewMessage(AFB_SUCCESS, "Stopped media");
133 STATIC json_object* pausing (AFB_request *request) { /* AFB_SESSION_CHECK */
135 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
137 if (!_rygel_do (ctx, PAUSE, NULL))
138 return jsonNewMessage(AFB_FAIL, "Could not pause chosen media");
140 return jsonNewMessage(AFB_SUCCESS, "Paused media");
143 STATIC json_object* seek (AFB_request *request) { /* AFB_SESSION_CHECK */
145 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
146 const char *value = getQueryValue (request, "value");
148 /* no "?value=" parameter : return error */
150 return jsonNewMessage(AFB_FAIL, "You must provide a time");
152 if (!_rygel_do (ctx, SEEK, value))
153 return jsonNewMessage(AFB_FAIL, "Could not seek chosen media");
155 return jsonNewMessage(AFB_SUCCESS, "Seeked media");
158 STATIC json_object* upload (AFB_request *request) { /* AFB_SESSION_CHECK */
160 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
161 const char *value = getQueryValue (request, "value");
165 /* no "?value=" parameter : return error */
167 return jsonNewMessage(AFB_FAIL, "You must provide a file name");
169 snprintf (path, sizeof(path), "/tmp/%s", value);
170 if (access (path, R_OK) == -1)
171 return jsonNewMessage(AFB_FAIL, "File not found");
173 if (!_rygel_upload (ctx, path))
174 return jsonNewMessage(AFB_FAIL, "Error when uploading file... could not complete");
176 return jsonNewMessage(AFB_SUCCESS, "File successfully uploaded");
179 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
180 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Media API");
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" },
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;
204 /*plugin->handle = initRadioPlugin();*/
205 plugin->freeCtxCB = (AFB_freeCtxCB)freeMedia;