2013-01-11 23:11:33 +00:00
|
|
|
--[==========================================================================[
|
2013-01-24 23:24:47 +00:00
|
|
|
syncplay.lua: Syncplay interface module for VLC
|
2013-01-11 23:11:33 +00:00
|
|
|
--[==========================================================================[
|
|
|
|
|
|
|
|
Author: Etoh
|
2013-01-24 23:24:47 +00:00
|
|
|
Project: http://syncplay.pl/
|
2013-01-27 19:56:53 +00:00
|
|
|
Version: 0.0.7
|
2013-01-13 22:51:15 +00:00
|
|
|
|
|
|
|
--[==========================================================================[
|
|
|
|
|
2013-01-24 23:52:01 +00:00
|
|
|
=== Installation instructions ===
|
|
|
|
|
2013-01-25 21:36:22 +00:00
|
|
|
Place the syncplay.lua file in one of the VLC /lua/intf/ sub-directories. By default this should be:
|
2013-01-24 23:52:01 +00:00
|
|
|
* Windows (all users): %ProgramFiles%\VideoLAN\VLC\lua\intf\
|
|
|
|
* Windows (current user): %APPDATA%\VLC\lua\intf\
|
2013-01-27 19:56:53 +00:00
|
|
|
* Linux (all users): /usr/lib/vlc/lua/intf/
|
2013-01-24 23:52:01 +00:00
|
|
|
* Linux (current user): ~/.local/share/vlc/lua/intf/
|
|
|
|
* Mac OS X (all users): /Applications/VLC.app/Contents/MacOS/share/lua/intf/
|
|
|
|
|
|
|
|
If a directory does not exist then you may have to create it.
|
2013-01-24 23:24:47 +00:00
|
|
|
|
2013-01-27 19:56:53 +00:00
|
|
|
Note: You may have to copy the VLC 'modules' folder to make it a sub-directory of the 'intf' folder.
|
|
|
|
|
2013-01-24 23:24:47 +00:00
|
|
|
=== Commands and responses ===
|
|
|
|
= Note: ? denotes optional responses; * denotes mandatory response; uses \n terminator.
|
2013-01-13 22:51:15 +00:00
|
|
|
|
|
|
|
[On connect]
|
|
|
|
>> VLC version
|
|
|
|
|
|
|
|
.
|
|
|
|
? >> inputstate-change: [<input/no-input>]
|
2013-01-24 23:24:47 +00:00
|
|
|
? >> filepath-change-notification
|
2013-01-13 22:51:15 +00:00
|
|
|
|
|
|
|
* >> playstate: [<playing/paused/no-input>]
|
|
|
|
* >> position: [<decimal seconds/no-input>]
|
|
|
|
|
|
|
|
get-interface-version
|
2013-01-24 23:24:47 +00:00
|
|
|
* >> interface-version: [syncplay connector version]
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-24 23:24:47 +00:00
|
|
|
get-duration
|
|
|
|
* >> duration: [<duration/no-input>]
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-24 23:24:47 +00:00
|
|
|
get-filepath
|
|
|
|
* >> filepath: [<filepath/no-input>]
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-24 23:24:47 +00:00
|
|
|
get-filename
|
|
|
|
* >> filepath: [<filename/no-input>]
|
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
set-position: [decimal seconds]
|
|
|
|
? >> play-error: no-input
|
|
|
|
|
|
|
|
set-playstate: [<playing/paused>]
|
|
|
|
? >> set-playstate-error: no-input
|
|
|
|
|
|
|
|
set-rate: [decimal rate]
|
|
|
|
? >> set-rate-error: no-input
|
|
|
|
|
|
|
|
display-osd: [placement on screen <center/left/right/top/bottom/top-left/top-right/bottom-left/bottom-right>], [duration in seconds], [message]
|
|
|
|
? >> display-osd-error: no-input
|
|
|
|
|
|
|
|
close-vlc
|
|
|
|
|
|
|
|
[Unknown command]
|
|
|
|
* >> [Unknown command]-error: unknown-command
|
2013-01-11 23:11:33 +00:00
|
|
|
|
|
|
|
--]==========================================================================]
|
|
|
|
require "common"
|
|
|
|
require "host"
|
|
|
|
|
2013-01-27 19:56:53 +00:00
|
|
|
local connectorversion = "0.0.7"
|
|
|
|
|
|
|
|
local durationdelay = 500000 -- Pause for get_duration command for increased reliability
|
2013-01-11 23:11:33 +00:00
|
|
|
|
|
|
|
local port
|
|
|
|
|
|
|
|
local msgterminator = "\n"
|
|
|
|
local msgseperator = ": "
|
|
|
|
local argseperator = ", "
|
|
|
|
|
|
|
|
local responsemarker = "-response"
|
|
|
|
local errormarker = "-error"
|
2013-01-13 22:51:15 +00:00
|
|
|
local notificationmarker = "-notification"
|
2013-01-11 23:11:33 +00:00
|
|
|
|
|
|
|
local noinput = "no-input"
|
|
|
|
local notimplemented = "not-implemented"
|
|
|
|
local unknowncommand = "unknown-command"
|
2013-01-13 22:51:15 +00:00
|
|
|
|
|
|
|
local oldfilepath
|
|
|
|
local oldinputstate
|
|
|
|
local newfilepath
|
|
|
|
local newinputstate
|
|
|
|
|
2013-01-24 23:24:47 +00:00
|
|
|
-- Start hosting Syncplay interface.
|
|
|
|
|
|
|
|
port = tonumber(config["port"])
|
|
|
|
if (port == nil or port < 1) then port = 4123 end
|
|
|
|
|
|
|
|
vlc.msg.info("Hosting Syncplay interface on port: "..port)
|
|
|
|
|
|
|
|
h = host.host()
|
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
function detectchanges()
|
2013-01-24 23:24:47 +00:00
|
|
|
-- Detects changes in VLC to report to Syncplay.
|
|
|
|
-- [Used by the polll / "." command]
|
2013-01-13 22:51:15 +00:00
|
|
|
|
|
|
|
local notificationbuffer = ""
|
|
|
|
|
|
|
|
if vlc.object.input() then
|
|
|
|
newinputstate = "input"
|
|
|
|
newfilepath = get_filepath()
|
|
|
|
|
|
|
|
if newfilepath ~= oldfilepath then
|
|
|
|
oldfilepath = newfilepath
|
2013-01-17 20:43:44 +00:00
|
|
|
notificationbuffer = notificationbuffer .. "filepath-change"..notificationmarker..msgterminator
|
2013-01-13 22:51:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
notificationbuffer = notificationbuffer .. "playstate"..msgseperator..tostring(get_play_state())..msgterminator
|
|
|
|
notificationbuffer = notificationbuffer .. "position"..msgseperator..tostring(get_var("time"))..msgterminator
|
|
|
|
else
|
|
|
|
notificationbuffer = notificationbuffer .. "playstate"..msgseperator..noinput..msgterminator
|
|
|
|
notificationbuffer = notificationbuffer .. "position"..msgseperator..noinput..msgterminator
|
|
|
|
newinputstate = noinput
|
|
|
|
end
|
|
|
|
|
|
|
|
if newinputstate ~= oldinputstate then
|
|
|
|
oldinputstate = newinputstate
|
2013-01-17 20:43:44 +00:00
|
|
|
notificationbuffer = notificationbuffer.."inputstate-change"..msgseperator..tostring(newinputstate)..msgterminator
|
2013-01-13 22:51:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return notificationbuffer
|
|
|
|
end
|
2013-01-11 23:11:33 +00:00
|
|
|
|
|
|
|
function get_args (argument, argcount)
|
2013-01-24 23:24:47 +00:00
|
|
|
-- Converts comma-space-seperated values into array of a given size, with last item absorbing all remaining data if needed.
|
2013-01-27 19:56:53 +00:00
|
|
|
-- [Used by the display-osd command]
|
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
local argarray = {}
|
|
|
|
local index
|
|
|
|
local i
|
|
|
|
local argbuffer
|
|
|
|
|
|
|
|
argbuffer = argument
|
|
|
|
|
|
|
|
for i = 1, argcount,1 do
|
|
|
|
if i == argcount then
|
|
|
|
if argbuffer == nil then
|
|
|
|
argarray[i] = ""
|
|
|
|
else
|
|
|
|
argarray[i] = argbuffer
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if string.find(argbuffer, argseperator) then
|
|
|
|
index = string.find(argbuffer, argseperator)
|
|
|
|
argarray[i] = string.sub(argbuffer, 0, index - 1)
|
|
|
|
argbuffer = string.sub(argbuffer, index + string.len(argseperator))
|
|
|
|
else
|
|
|
|
argarray[i] = ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return argarray
|
|
|
|
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function get_var( vartoget )
|
2013-01-24 23:24:47 +00:00
|
|
|
-- [Used by the poll / '.' command to get time]
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
local response
|
|
|
|
local errormsg
|
|
|
|
local input = vlc.object.input()
|
|
|
|
|
|
|
|
if input then
|
|
|
|
response = vlc.var.get(input,tostring(vartoget))
|
|
|
|
else
|
|
|
|
errormsg = noinput
|
|
|
|
end
|
|
|
|
|
|
|
|
return response, errormsg
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|
|
|
|
|
2013-01-24 23:24:47 +00:00
|
|
|
|
2013-01-11 23:11:33 +00:00
|
|
|
function set_var(vartoset, varvalue)
|
2013-01-24 23:24:47 +00:00
|
|
|
-- [Used by the set-time and set-rate commands]
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
local errormsg
|
|
|
|
local input = vlc.object.input()
|
|
|
|
|
|
|
|
if input then
|
|
|
|
vlc.var.set(input,tostring(vartoset),tostring(varvalue))
|
|
|
|
else
|
|
|
|
errormsg = noinput
|
|
|
|
end
|
|
|
|
|
|
|
|
return errormsg
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|
|
|
|
|
2013-01-17 20:43:44 +00:00
|
|
|
h:listen( "localhost:"..port)
|
2013-01-24 23:24:47 +00:00
|
|
|
|
2013-01-11 23:11:33 +00:00
|
|
|
function get_play_state()
|
2013-01-24 23:24:47 +00:00
|
|
|
-- [Used by the get-playstate command]
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
local response
|
|
|
|
local errormsg
|
|
|
|
local input = vlc.object.input()
|
|
|
|
|
|
|
|
if input then
|
|
|
|
response = vlc.playlist.status()
|
|
|
|
else
|
|
|
|
errormsg = noinput
|
|
|
|
end
|
|
|
|
|
|
|
|
return response, errormsg
|
|
|
|
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function get_filepath ()
|
2013-01-24 23:24:47 +00:00
|
|
|
-- [Used by get-filepath command]
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
local response
|
|
|
|
local errormsg
|
|
|
|
local item
|
|
|
|
local input = vlc.object.input()
|
|
|
|
|
|
|
|
if input then
|
|
|
|
local item = vlc.input.item()
|
|
|
|
if item then
|
|
|
|
response = vlc.strings.decode_uri(item:uri())
|
|
|
|
else
|
|
|
|
errormsg = noinput
|
|
|
|
end
|
|
|
|
else
|
|
|
|
errormsg = noinput
|
|
|
|
end
|
|
|
|
|
|
|
|
return response, errormsg
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
function get_filename ()
|
2013-01-24 23:24:47 +00:00
|
|
|
-- [Used by get-filename command]
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-17 20:43:44 +00:00
|
|
|
local response
|
|
|
|
local index
|
|
|
|
local filename
|
|
|
|
filename = errormerge(get_filepath())
|
|
|
|
|
|
|
|
if(filename ~= nil) and (filename ~= "") and (filename ~= noinput) then
|
2013-01-25 21:32:28 +00:00
|
|
|
index = string.len(tostring(string.match(filename, ".*/")))
|
2013-01-17 20:43:44 +00:00
|
|
|
if index then
|
|
|
|
response = string.sub(tostring(filename), index+1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
response = noinput
|
|
|
|
end
|
|
|
|
|
|
|
|
return response
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_duration ()
|
2013-01-24 23:24:47 +00:00
|
|
|
-- [Used by get-duration command]
|
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
local response
|
|
|
|
local errormsg
|
2013-01-17 20:43:44 +00:00
|
|
|
local item
|
2013-01-13 22:51:15 +00:00
|
|
|
local input = vlc.object.input()
|
|
|
|
|
|
|
|
if input then
|
|
|
|
local item = vlc.input.item()
|
|
|
|
if item then
|
2013-01-27 19:56:53 +00:00
|
|
|
-- Try to get duration, which might not be available straight away
|
|
|
|
local i = 0
|
|
|
|
repeat
|
|
|
|
vlc.misc.mwait(vlc.misc.mdate() + durationdelay)
|
|
|
|
response = vlc.input.item():duration()
|
|
|
|
i = i + 1
|
|
|
|
until response > 0 or i > 5
|
2013-01-13 22:51:15 +00:00
|
|
|
else
|
|
|
|
errormsg = noinput
|
|
|
|
end
|
|
|
|
else
|
|
|
|
errormsg = noinput
|
|
|
|
end
|
|
|
|
|
|
|
|
return response, errormsg
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|
2013-01-17 20:43:44 +00:00
|
|
|
|
2013-01-11 23:11:33 +00:00
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
function display_osd ( argument )
|
2013-01-24 23:24:47 +00:00
|
|
|
-- [Used by display-osd command]
|
2013-01-13 22:51:15 +00:00
|
|
|
local errormsg
|
|
|
|
local osdarray
|
|
|
|
local input = vlc.object.input()
|
|
|
|
if input then
|
|
|
|
osdarray = get_args(argument,3)
|
|
|
|
--position, duration, message -> message, , position, duration (converted from seconds to microseconds)
|
|
|
|
local osdduration = tonumber(osdarray[2]) * 1000 * 1000
|
|
|
|
vlc.osd.message(osdarray[3],channel1,osdarray[1],osdduration)
|
|
|
|
else
|
|
|
|
errormsg = noinput
|
|
|
|
end
|
|
|
|
return errormsg
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
|
|
|
|
function do_command ( command, argument)
|
2013-01-24 23:24:47 +00:00
|
|
|
-- Processes all commands sent by Syncplay (see protocol, above).
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
if command == "." then
|
|
|
|
do return detectchanges() end
|
|
|
|
end
|
|
|
|
local command = tostring(command)
|
|
|
|
local argument = tostring(argument)
|
|
|
|
local errormsg = ""
|
2013-01-17 20:43:44 +00:00
|
|
|
local response = ""
|
2013-01-13 22:51:15 +00:00
|
|
|
|
|
|
|
if command == "get-interface-version" then response = "interface-version"..msgseperator..connectorversion..msgterminator
|
2013-01-17 20:43:44 +00:00
|
|
|
elseif command == "get-duration" then response = "duration"..msgseperator..errormerge(get_duration())..msgterminator
|
|
|
|
elseif command == "get-filepath" then response = "filepath"..msgseperator..errormerge(get_filepath())..msgterminator
|
|
|
|
elseif command == "get-filename" then response = "filename"..msgseperator..errormerge(get_filename())..msgterminator
|
2013-01-13 22:51:15 +00:00
|
|
|
elseif command == "set-position" then errormsg = set_var("time", tonumber(argument))
|
|
|
|
elseif command == "set-playstate" then errormsg = set_playstate(argument)
|
|
|
|
elseif command == "set-rate" then errormsg = set_var("rate", tonumber(argument))
|
|
|
|
elseif command == "display-osd" then errormsg = display_osd(argument)
|
2013-01-17 20:43:44 +00:00
|
|
|
elseif command == "close-vlc" then vlc.misc.quit()
|
2013-01-13 22:51:15 +00:00
|
|
|
else errormsg = unknowncommand
|
|
|
|
end
|
|
|
|
|
2013-01-17 20:43:44 +00:00
|
|
|
if (errormsg ~= nil) and (errormsg ~= "") then
|
2013-01-13 22:51:15 +00:00
|
|
|
response = command..errormarker..msgseperator..tostring(errormsg)..msgterminator
|
|
|
|
end
|
2013-01-11 23:11:33 +00:00
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
return response
|
|
|
|
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|
|
|
|
|
2013-01-24 23:24:47 +00:00
|
|
|
|
2013-01-17 20:43:44 +00:00
|
|
|
function errormerge(argument, errormsg)
|
2013-01-27 19:56:53 +00:00
|
|
|
-- Used to integrate 'no-input' error messages into command responses.
|
|
|
|
|
2013-01-17 20:43:44 +00:00
|
|
|
if (errormsg ~= nil) and (errormsg ~= "") then
|
|
|
|
do return errormsg end
|
|
|
|
end
|
|
|
|
|
|
|
|
return argument
|
|
|
|
end
|
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
function set_playstate(argument)
|
2013-01-24 23:24:47 +00:00
|
|
|
-- [Used by the set-playstate command]
|
2013-01-27 19:56:53 +00:00
|
|
|
|
2013-01-13 22:51:15 +00:00
|
|
|
local errormsg
|
|
|
|
local input = vlc.object.input()
|
|
|
|
local playstate
|
|
|
|
playstate, errormsg = get_play_state()
|
|
|
|
|
|
|
|
if playstate ~= "playing" then playstate = "paused" end
|
|
|
|
if ((errormsg ~= noinput) and (playstate ~= argument)) then
|
|
|
|
vlc.playlist.pause()
|
|
|
|
end
|
|
|
|
|
|
|
|
return errormsg
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|
|
|
|
|
2013-01-24 23:24:47 +00:00
|
|
|
-- main loop, which alternates between writing and reading
|
2013-01-11 23:11:33 +00:00
|
|
|
while not vlc.misc.should_die() do
|
|
|
|
-- accept new connections and select active clients
|
2013-01-13 22:51:15 +00:00
|
|
|
local write, read = h:accept_and_select()
|
2013-01-11 23:11:33 +00:00
|
|
|
|
|
|
|
-- handle clients in write mode
|
2013-01-13 22:51:15 +00:00
|
|
|
for _, client in pairs(write) do
|
|
|
|
client:send()
|
|
|
|
client.buffer = ""
|
|
|
|
client:switch_status( host.status.read )
|
|
|
|
end
|
2013-01-11 23:11:33 +00:00
|
|
|
|
|
|
|
-- handle clients in read mode
|
2013-01-13 22:51:15 +00:00
|
|
|
|
|
|
|
for _, client in pairs(read) do
|
|
|
|
local str = client:recv(1000)
|
|
|
|
local responsebuffer
|
|
|
|
if not str then break end
|
|
|
|
|
|
|
|
local safestr = string.gsub(tostring(str), "\r", "")
|
|
|
|
if client.inputbuffer == nil then client.inputbuffer = "" end
|
|
|
|
|
|
|
|
client.inputbuffer = client.inputbuffer .. safestr
|
|
|
|
|
|
|
|
while string.find(client.inputbuffer, msgterminator) do
|
|
|
|
local index = string.find(client.inputbuffer, msgterminator)
|
|
|
|
local request = string.sub(client.inputbuffer, 0, index - 1)
|
|
|
|
local command
|
|
|
|
local argument
|
|
|
|
client.inputbuffer = string.sub(client.inputbuffer, index + string.len(msgterminator))
|
|
|
|
|
|
|
|
if (string.find(request, msgseperator)) then
|
|
|
|
index = string.find(request, msgseperator)
|
|
|
|
command = string.sub(request, 0, index - 1)
|
|
|
|
argument = string.sub(request, index + string.len(msgseperator))
|
|
|
|
|
|
|
|
else
|
|
|
|
command = request
|
|
|
|
end
|
|
|
|
|
|
|
|
if (responsebuffer) then
|
|
|
|
responsebuffer = responsebuffer .. do_command(command,argument)
|
|
|
|
else
|
|
|
|
responsebuffer = do_command(command,argument)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
client.buffer = ""
|
|
|
|
if (responsebuffer) then
|
|
|
|
client:send(responsebuffer)
|
|
|
|
end
|
|
|
|
client.buffer = ""
|
2013-01-11 23:11:33 +00:00
|
|
|
client:switch_status( host.status.write )
|
|
|
|
end
|
2013-01-13 22:51:15 +00:00
|
|
|
|
2013-01-11 23:11:33 +00:00
|
|
|
end
|