update syncplay.lua - Major rewrite,verup to 0.0.3

This commit is contained in:
Etoh 2013-01-13 22:51:15 +00:00
parent f7489f7c89
commit 36d2485dd6

View File

@ -3,12 +3,49 @@
--[==========================================================================[ --[==========================================================================[
Author: Etoh Author: Etoh
Project: http://syncplay.pl
--[==========================================================================[
=== Commands and response ===
= Note: ? is optional response, * is mandatory response; uses \n terminator
[On connect]
>> VLC version
.
? >> inputstate-change: [<input/no-input>]
? >> filepath-change: [filepath URI]
? >> file-length: [decimal seconds]
* >> playstate: [<playing/paused/no-input>]
* >> position: [<decimal seconds/no-input>]
get-interface-version
* >> interface-version: [sncplay connector version]
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
--]==========================================================================] --]==========================================================================]
require "common" require "common"
require "host" require "host"
local connectorversion = "0.0.2" local connectorversion = "0.0.3"
local port local port
@ -18,42 +55,77 @@ local argseperator = ", "
local responsemarker = "-response" local responsemarker = "-response"
local errormarker = "-error" local errormarker = "-error"
local notificationmarker = "-notification"
local noinput = "no-input" local noinput = "no-input"
local notimplemented = "not-implemented" local notimplemented = "not-implemented"
local unknowncommand = "unknown-command" local unknowncommand = "unknown-command"
local oldfilepath
local oldinputstate
local newfilepath
local newinputstate
function get_args (argument, argcount) function detectchanges()
local argarray = {}
local index
local i
local argbuffer
argbuffer = argument
for i = 1, argcount,1 do local notificationbuffer = ""
if i == argcount then
if argbuffer == nil then if vlc.object.input() then
argarray[i] = "" newinputstate = "input"
else newfilepath = get_filepath()
argarray[i] = argbuffer
end if newfilepath ~= oldfilepath then
else oldfilepath = newfilepath
if string.find(argbuffer, argseperator) then notificationbuffer = notificationbuffer .. "filepath-change"..msgseperator..tostring(newfilepath)..msgterminator
index = string.find(argbuffer, argseperator) notificationbuffer = notificationbuffer .. "file-length-change"..msgseperator..get_var("length")..msgterminator
argarray[i] = string.sub(argbuffer, 0, index - 1) end
argbuffer = string.sub(argbuffer, index + string.len(argseperator))
else notificationbuffer = notificationbuffer .. "playstate"..msgseperator..tostring(get_play_state())..msgterminator
argarray[i] = "" notificationbuffer = notificationbuffer .. "position"..msgseperator..tostring(get_var("time"))..msgterminator
end else
end notificationbuffer = notificationbuffer .. "playstate"..msgseperator..noinput..msgterminator
notificationbuffer = notificationbuffer .. "position"..msgseperator..noinput..msgterminator
end newinputstate = noinput
end
return argarray
if newinputstate ~= oldinputstate then
oldinputstate = newinputstate
notificationbuffer = "inputstate-change"..msgseperator..tostring(newinputstate)..msgterminator..notificationbuffer
end
return notificationbuffer
end end
function get_args (argument, argcount)
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
end
port = tonumber(config["port"]) port = tonumber(config["port"])
if (port == nil or port < 1) then port = 4123 end if (port == nil or port < 1) then port = 4123 end
@ -64,238 +136,209 @@ h = host.host()
-- Bypass any authentication -- Bypass any authentication
function on_password( client ) function on_password( client )
client:switch_status( host.status.read ) client:switch_status( host.status.read )
end end
function get_var( vartoget ) function get_var( vartoget )
local response local response
local errormsg local errormsg
local input = vlc.object.input() local input = vlc.object.input()
if input then if input then
response = vlc.var.get(input,tostring(vartoget)) response = vlc.var.get(input,tostring(vartoget))
else else
errormsg = noinput errormsg = noinput
end end
vlc.msg.info("getvar `"..tostring(vartoget).."`: '"..tostring(response).."'") return response, errormsg
return response, errormsg
end end
function set_var(vartoset, varvalue) function set_var(vartoset, varvalue)
local errormsg local errormsg
local input = vlc.object.input() local input = vlc.object.input()
if input then if input then
vlc.var.set(input,tostring(vartoset),tostring(varvalue)) vlc.var.set(input,tostring(vartoset),tostring(varvalue))
else else
errormsg = noinput errormsg = noinput
end end
vlc.msg.info("setvar: '"..tostring(vartoset).."' = '"..tostring(varvalue).."'")
return errormsg return errormsg
end end
h:listen( "localhost:"..port) h:listen( "localhost:"..port)
--h:listen( "*console" ) -- h:listen( "*console" )
function get_play_state() function get_play_state()
local response local response
local errormsg local errormsg
local input = vlc.object.input() local input = vlc.object.input()
if input then if input then
response = vlc.playlist.status() response = vlc.playlist.status()
else else
errormsg = noinput errormsg = noinput
end end
vlc.msg.info("get play state: '"..tostring(response).."'") return response, errormsg
return response, errormsg
end end
function get_filepath () function get_filepath ()
local response local response
local errormsg local errormsg
local item local item
local input = vlc.object.input() local input = vlc.object.input()
if input then if input then
local item = vlc.input.item() local item = vlc.input.item()
if item then if item then
response = vlc.strings.decode_uri(item:uri()) response = vlc.strings.decode_uri(item:uri())
else else
errormsg = noinput errormsg = noinput
end end
else else
errormsg = noinput errormsg = noinput
end end
return response, errormsg return response, errormsg
end
function get_filename ()
local response
local errormsg
local input = vlc.object.input()
if input then
local item = vlc.input.item()
if item then
response = item:name()
else
errormsg = noinput
end
else
errormsg = noinput
end
return response, errormsg
end end
function display_osd ( argument ) function display_osd ( argument )
local errormsg local errormsg
local osdarray local osdarray
local input = vlc.object.input() local input = vlc.object.input()
if input then if input then
osdarray = get_args(argument,3) osdarray = get_args(argument,3)
--position, duration, message -> message, , position, duration --position, duration, message -> message, , position, duration (converted from seconds to microseconds)
vlc.osd.message(osdarray[3],channel1,osdarray[1],tonumber(osdarray[2])) local osdduration = tonumber(osdarray[2]) * 1000 * 1000
else vlc.osd.message(osdarray[3],channel1,osdarray[1],osdduration)
errormsg = noinput else
end errormsg = noinput
return errormsg end
return errormsg
end end
function do_command ( command, argument) function do_command ( command, argument)
if command == "." then
do return detectchanges() end
end
local command = tostring(command)
local argument = tostring(argument)
local errormsg = ""
local response = ""
local command = tostring(command) local input = vlc.object.input()
local argument = tostring(argument)
local errormsg = ""
local response = ""
vlc.msg.info("Command: '"..command.."'") if command == "get-interface-version" then response = "interface-version"..msgseperator..connectorversion..msgterminator
vlc.msg.info("Argument: '"..argument.."'") elseif command == "set-position" then errormsg = set_var("time", tonumber(argument))
elseif command == "set-playstate" then errormsg = set_playstate(argument)
local input = vlc.object.input() elseif command == "set-rate" then errormsg = set_var("rate", tonumber(argument))
elseif command == "display-osd" then errormsg = display_osd(argument)
elseif command == "close-vlc" then misc.quit()
else errormsg = unknowncommand
end
if (tostring(errormsg) ~= nil) and (errormsg ~= "") then
response = command..errormarker..msgseperator..tostring(errormsg)..msgterminator
end
if command == "get-playstate" then response, errormsg = get_play_state() return response
elseif command == "get-time" then response, errormsg = get_var("time")
elseif command == "get-filename" then response, errormsg = get_var("title")
elseif command == "get-file-length" then response, errormsg = get_var("length")
elseif command == "get-filepath" then response, errormsg = get_filepath()
elseif command == "get-vlc-version" then response = vlc.misc.version()
elseif command == "get-version" then response = connectorversion
elseif command == "play" then errormsg = playfile()
elseif command == "pause" then errormsg = pausefile()
elseif command == "playpause" then errormsg = playpausefile()
elseif command == "seek" then errormsg = set_var("time", tonumber(argument))
elseif command == "set-rate" then errormsg = set_var("rate", tonumber(argument))
elseif command == "display-osd" then errormsg = display_osd(argument)
else errormsg = unknowncommand
end
if (tostring(errormsg) == "" or errormsg == nil) then
if (tostring(response) == "") then
response = command..responsemarker..msgterminator
else
response = command..responsemarker..msgseperator..tostring(response)..msgterminator
end
else
response = command..errormarker..msgseperator..tostring(errormsg)..msgterminator
end
vlc.msg.info("Response: '"..tostring(response).."'")
return response
end end
function playfile() function set_playstate(argument)
local errormsg local errormsg
local input = vlc.object.input() local input = vlc.object.input()
local playstate local playstate
playstate, errormsg = get_play_state() playstate, errormsg = get_play_state()
if playstate == "paused" then if playstate ~= "playing" then playstate = "paused" end
vlc.playlist.pause() if ((errormsg ~= noinput) and (playstate ~= argument)) then
end vlc.playlist.pause()
end
return errormsg
end return errormsg
function pausefile()
local errormsg
local input = vlc.object.input()
local playstate
playstate, errormsg = get_play_state()
if playstate == "playing" then
vlc.playlist.pause()
end
return errormsg
end
function playpausefile()
local errormsg
local input = vlc.object.input()
if input then
vlc.playlist.pause()
else
errormsg = noinput
end
return errormsg
end end
-- main loop -- main loop
while not vlc.misc.should_die() do while not vlc.misc.should_die() do
-- accept new connections and select active clients -- accept new connections and select active clients
local write, read = h:accept_and_select() local write, read = h:accept_and_select()
-- handle clients in write mode -- handle clients in write mode
for _, client in pairs(write) do for _, client in pairs(write) do
client:send() client:send()
client.buffer = "" client.buffer = ""
client:switch_status( host.status.read ) client:switch_status( host.status.read )
end end
-- handle clients in read mode -- handle clients in read mode
for _, client in pairs(read) do for _, client in pairs(read) do
local str = client:recv(1000) local str = client:recv(1000)
local responsebuffer local responsebuffer
if not str then break end if not str then break end
local safestr = string.gsub(tostring(str), "\r", "")
local safestr = string.gsub(tostring(str), "\r", "")
if client.inputbuffer == nil then
client.inputbuffer = "" if client.inputbuffer == nil then client.inputbuffer = "" end
end
client.inputbuffer = client.inputbuffer .. safestr
client.inputbuffer = client.inputbuffer .. safestr
while string.find(client.inputbuffer, msgterminator) do
vlc.msg.info("Str: '" .. safestr.."'") local index = string.find(client.inputbuffer, msgterminator)
vlc.msg.info("Input buffer: '" .. client.inputbuffer.."'") local request = string.sub(client.inputbuffer, 0, index - 1)
local command
while string.find(client.inputbuffer, msgterminator) do local argument
local index = string.find(client.inputbuffer, msgterminator) client.inputbuffer = string.sub(client.inputbuffer, index + string.len(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 if (string.find(request, msgseperator)) then
index = string.find(request, msgseperator) index = string.find(request, msgseperator)
command = string.sub(request, 0, index - 1) command = string.sub(request, 0, index - 1)
argument = string.sub(request, index + string.len(msgseperator)) argument = string.sub(request, index + string.len(msgseperator))
else else
command = request command = request
end end
if (responsebuffer) then if (responsebuffer) then
responsebuffer = responsebuffer .. do_command(command,argument) responsebuffer = responsebuffer .. do_command(command,argument)
else else
responsebuffer = do_command(command,argument) responsebuffer = do_command(command,argument)
end end
end end
client.buffer = "" client.buffer = ""
if (responsebuffer) then if (responsebuffer) then
client:send(responsebuffer) client:send(responsebuffer)
end end
client.buffer = "" client.buffer = ""
client:switch_status( host.status.write ) client:switch_status( host.status.write )
end end
end end