2 * Copyright (C) 2016 "IoT.bzh"
3 * Author "Manuel Bachmann"
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "media-api.h"
20 /* ------ LOCAL HELPER FUNCTIONS --------- */
22 /* private client context creation ; default values */
23 STATIC mediaCtxHandleT* initMediaCtx () {
27 ctx = malloc (sizeof(mediaCtxHandleT));
28 ctx->media_server = NULL;
34 /* called when client session dies [e.g. client quits for more than 15mns] */
35 STATIC void freeMedia (void *context) {
40 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
42 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
47 /* create a private client context */
48 if (!request->context)
49 request->context = initMediaCtx();
51 ctx = (mediaCtxHandleT*)request->context;
53 /* initialize server connection */
54 if (!ctx->media_server)
55 _rygel_init (request->context);
57 jresp = json_object_new_object();
58 json_object_object_add(jresp, "info", json_object_new_string ("Media initialized"));
62 STATIC json_object* list (AFB_request *request) { /* AFB_SESSION_CHECK */
64 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
67 jresp = _rygel_list (ctx);
70 return jsonNewMessage(AFB_FAIL, "No content found in media server");
75 STATIC json_object* selecting (AFB_request *request) { /* AFB_SESSION_CHECK */
77 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
78 const char *value = getQueryValue (request, "value");
83 /* no "?value=" parameter : return current index */
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));
90 /* "?value=" parameter is negative */
91 else if (atoi(value) < 0)
92 return jsonNewMessage(AFB_FAIL, "Chosen index cannot be negative");
94 /* "?value=" parameter is positive */
95 else if (atoi(value) >= 0) {
96 index = (unsigned int) atoi(value);
98 if (!_rygel_select (ctx, index))
99 return jsonNewMessage(AFB_FAIL, "Chosen index superior to current media count");
102 jresp = json_object_new_object();
103 json_object_object_add (jresp, "index", json_object_new_string (value));
111 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
113 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
115 if (!_rygel_do (ctx, PLAY, NULL))
116 return jsonNewMessage(AFB_FAIL, "Could not play chosen media");
118 return jsonNewMessage(AFB_SUCCESS, "PLaying media");
121 STATIC json_object* stop (AFB_request *request) { /* AFB_SESSION_CHECK */
123 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
125 if (!_rygel_do (ctx, STOP, NULL))
126 return jsonNewMessage(AFB_FAIL, "Could not stop chosen media");
128 return jsonNewMessage(AFB_SUCCESS, "Stopped media");
131 STATIC json_object* pausing (AFB_request *request) { /* AFB_SESSION_CHECK */
133 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
135 if (!_rygel_do (ctx, PAUSE, NULL))
136 return jsonNewMessage(AFB_FAIL, "Could not pause chosen media");
138 return jsonNewMessage(AFB_SUCCESS, "Paused media");
141 STATIC json_object* seek (AFB_request *request) { /* AFB_SESSION_CHECK */
143 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
144 const char *value = getQueryValue (request, "value");
146 /* no "?value=" parameter : return error */
148 return jsonNewMessage(AFB_FAIL, "You must provide a time");
150 if (!_rygel_do (ctx, SEEK, value))
151 return jsonNewMessage(AFB_FAIL, "Could not seek chosen media");
153 return jsonNewMessage(AFB_SUCCESS, "Seeked media");
156 STATIC json_object* upload (AFB_request *request, AFB_PostItem *item) { /* AFB_SESSION_CHECK */
158 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
159 AFB_PostCtx *postFileCtx;
163 /* item is !NULL until transfer is complete */
165 return getPostFile (request, item, "media");
167 /* target intermediary file path */
168 path = getPostPath (request);
171 fprintf (stderr, "Error encoutered during intermediary file transfer\n");
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");
179 request->errcode = MHD_HTTP_OK;
180 request->jresp = jsonNewMessage (AFB_SUCCESS, "upload=%s done", path);
183 /* finalizes file transfer */
184 return getPostFile (request, item, NULL);
187 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
188 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Media API");
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" },
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;
212 /*plugin->handle = initRadioPlugin();*/
213 plugin->freeCtxCB = (AFB_freeCtxCB)freeMedia;