Search code examples
c++jsonjson-rpcxbmc

JSON RPC with Kodi (XBMC) to restart first item in playlist


I am using libjson (https://github.com/cinemast/libjson-rpc-cpp) and the api of kodi (previously called XBMC to play/pause and stop my Kodi machine. However, after I stopped it, I cannot restart it with play/pause again. The method I use: make a json file that jsonrpcstub can process, for play/pause like this:

[
{
    "name" : "Player.PlayPause",
    "description": "Pauses or unpause playback and returns the new state",
    "params": [
        {
            "$ref": "Player.Id",
            "name": "playerid",
            "required": true
        },
        {
            "$ref": "Global.Toggle",
            "default": "toggle",
            "name": "play"
        }
    ],
    "returns": {
        "$ref": "Player.Speed"
    },
    "type": "method"
}
]

And then I use:

#include "xbmcremoteclient.h"
#include <jsonrpccpp/client/connectors/httpclient.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef WIN32
    #include <termios.h>
#else
    #include <conio.h>
#endif
#include <unistd.h>
#include <time.h>

#include <iostream>

using namespace jsonrpc;
using namespace std;

int main(int argc, char** argv) {

    try {
        HttpClient httpclient("http://user:[email protected]:8080/jsonrpc");
        XbmcRemoteClient stub(httpclient);
        stub.Player_PlayPause(0);
    } 
    catch(JsonRpcException& e) {
        cerr << e.what() << endl;
    }
    return 0;
}

to make a simple executable to run this command. That works, however, when I try to implement the goTo function: http://kodi.wiki/view/JSON-RPC_API/v6#Player.GoTo

I cannot manage it. I have been changing the json file up to this:

[
{"jsonrpc": "2.0", "method": "Player.PlayPause", "params": { "playerid": 0 }, "id": 1},
{
    "name": "Player.GoTo":,
    "params": [
        {
            "name": "playerid",
            "required": true
        },
        {
            "name": "to",
            "required": true,
            "type": [
                {
                    "enums": [
                        "previous",
                        "next"
                    ],
                    "type": "string"
                },
                {
                    "description": "position in playlist"
                }
            ]
        }
    ],
    "returns": {
        "type": "string"
    }
}
]

but I keep getting syntax error when trying to run it through jsonrpcstub:

Exception -32700 : JSON_PARSE_ERROR: The JSON-Object is not JSON-Valid:  specification file contains syntax errors

Preferrably I would just run the json file I get when browsing to http://myKodiMachine.xy:8080/jsonrpc through jsonrpcstub, but this creates syntaxerrors already. It seems that his file starting like this:

{
    "description": "JSON-RPC API of XBMC",
    "id": "http://xbmc.org/jsonrpc/ServiceDescription.json",
    "methods": {
        "Addons.ExecuteAddon": {
            "description": "Executes the given addon with the given parameters (if possible)",
            "params": [
                {
                    "name": "addonid",
                    "required": true,
                    "type": "string"
                },
                {
                    "default": "",
                    "name": "params",
                    "type": [
                        {
                            "additionalProperties": {
                                "default": "",
                                "type": "string"
                            },
                            "type": "object"
                        },
                        {
                            "items": {
                                "type": "string"
                            },
                            "type": "array"
                        },
                        {
                            "description": "URL path (must start with / or ?",
                            "type": "string"
                        }
                    ]
                },
                {
                    "default": false,
                    "name": "wait",
                    "type": "boolean"
                }
            ],
            "returns": {
                "type": "string"
            },
            "type": "method"
        },
        "Addons.GetAddonDetails": {
            "description": "Gets the details of a specific addon",
            "params": [
                {
                    "name": "addonid",
                    "required": true,
                    "type": "string"
                },
                {
                    "$ref": "Addon.Fields",

is not valid JSON for some strange reason. Any pointers in the direction of being able to stop and restart the playlist in kodi from linux (like this or any other way) will be considered helpfull. I need this especially since I play a lot of streams, and if I pause those they will get buffered (which is not very handy), so I prefer to stop/restart them.


Solution

  • Still not very happy with the fact that the json code provided by kodi is rather useless for the jsonrpc c++ library, but I managed to get it to work, the json file will need this line:

    {"jsonrpc": "2.0", "method": "Input.ExecuteAction", "params": { "action": "play"}, "id": 1}
    

    and then do:

    stub.Input_ExecuteAction("play");
    

    in the cpp file, and it will work :).