Links

Categories

Tags


« | Main | »

Remote-controlling VLC via TCP socket

By Jewe | December 21, 2014

This simple script opens VLC without GUI and initializes the media player’s built-in remote-control interface. Command input entered by the user is sent via TCP socket to VLC. This script could be the basis of your own VLC remote program. Run it with the JILRun runtime environment.

/*
 * vlc-remote.jc
 *
 * Start VLC with remote control interface and offer simple command prompt.
 */

import stdlib;
import System::Network::Socket;
import System::Text::WideString;
import System::Time::Delay;
import Windows::API;

using stdlib;
using System::Network::Socket;
using System::Text::WideString;
using System::Time::Delay;

const string VLC = /"C:\Program Files (x86)\VideoLAN\vlc\vlc.exe"/;
const string Command = "%s --rc-host %s:%d --extraintf oldrc --intf dummy";

const string Host_IP = "localhost"; // set to the computer you want to connect to, or use default
const int Host_Port = 8080; // set to the port you wish to use, or use default

//------------------------------------------------------------------------------
// function main
//------------------------------------------------------------------------------

function string main(const string[] args)
{
    string cmd = string::format(Command, {VLC, Host_IP, Host_Port});
    Windows::API::Execute(cmd, 0); // launch VLC
    DoClient(Host_IP, Host_Port); // connect to it
    return null;
}

//------------------------------------------------------------------------------
// function DoClient
//------------------------------------------------------------------------------

function DoClient(const string addr, const int port)
{
    clause (int e)
    {
        Delay delay;
        Socket socket;
        println("Trying to connect to " + addr + ":" + port);
        // connect to specified host and port (it can take a while until VLC is fully loaded,
        // so we'll keep trying for 30 seconds)
        delay.Start(30000, false);
        for (;;)
        {
            e = socket.Connect(addr, port); // try to connect
            if (e == 0)
                break; // success!
            Windows::API::Sleep(200); // connection failed, sleep 200 ms, then retry
            if (delay.Elapsed())
                break; // time-out!
        }
        if (e)
            goto Error(e);
        println("Connection established, enter query. Type 'help' for commands.");
        while (socket.Connected)
        {
            // check if there is a message from the server
            while (socket.InputPending)
            {
                WideString resp = socket.Receiveln(); // receive answer in Unicode
                println(resp.toString(resp.oemCodePage)); // convert to OEM code page
            }
            // have user enter something
            print("VLC> ");
            string query = getString();
            // send it to server
            socket.Sendln(query);
            // process input
            if (query == "logout" || query == "quit")
                break;
            // wait for response
            delay.Start(1000, false);
            while (socket.InputPending == 0)
            {
                Windows::API::Sleep(20);
                if (delay.Elapsed())
                    break;
            }
        }
        socket.Close();
    }
    clause Error
    {
        printf("ERROR %d\n", e);
    }
}

Topics: code examples, news | Comments Off on Remote-controlling VLC via TCP socket

Comments are closed.