+ if (!ctx->media_server)
+ _rygel_init (ctx);
+
+ jresp = json_object_new_object ();
+ json_object_object_add (jresp, "init", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Media - Initialized");
+}
+
+static void list (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
+ json_object *jresp;
+
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
+ jresp = _rygel_list (ctx);
+
+ if (!jresp) {
+ afb_req_fail (request, "failed", "no content found in media server");
+ return;
+ }
+
+ afb_req_success (request, jresp, "Media - Listed");
+}
+
+static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
+ const char *value = afb_req_value (request, "value");
+ json_object *jresp;
+ unsigned int index;
+ char index_str[5];
+
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
+ /* no "?value=" parameter : return current index */
+ if (!value) {
+ snprintf (index_str, sizeof(index_str), "%d", ctx->index);
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "index", json_object_new_string (index_str));
+ }
+
+ /* "?value=" parameter is negative */
+ else if (atoi(value) < 0) {
+ afb_req_fail (request, "failed", "chosen index cannot be negative");
+ return;
+ }
+
+ /* "?value=" parameter is positive */
+ else if (atoi(value) >= 0) {
+ index = (unsigned int) atoi(value);
+
+ if (!_rygel_select (ctx, index)) {
+ afb_req_fail (request, "failed", "chosen index superior to current media count");
+ return;
+ }
+
+ ctx->index = index;
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "index", json_object_new_string (value));
+ }
+ else
+ jresp = NULL;
+
+ afb_req_success (request, jresp, "Media - Listed");
+}
+
+static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
+ json_object *jresp;
+
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
+ if (!_rygel_do (ctx, PLAY, NULL)) {
+ afb_req_fail (request, "failed", "could not play chosen media");
+ return;
+ }
+
+ jresp = json_object_new_object ();
+ json_object_object_add (jresp, "play", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Media - Listed");
+}
+
+static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
+ json_object *jresp;
+
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
+ if (!_rygel_do (ctx, STOP, NULL)) {
+ afb_req_fail (request, "failed", "could not stop chosen media");
+ return;
+ }
+
+ jresp = json_object_new_object ();
+ json_object_object_add (jresp, "stop", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Media - Stopped");
+}
+
+static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
+ json_object *jresp;
+
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
+ if (!_rygel_do (ctx, PAUSE, NULL)) {
+ afb_req_fail (request, "failed", "could not pause chosen media");
+ return;
+ }