Close properly from VLC

This commit is contained in:
Et0h 2014-06-02 20:27:53 +01:00
parent 97a499a34a
commit e23609edf5
2 changed files with 82 additions and 68 deletions

View File

@ -5,14 +5,14 @@
Principal author: Etoh
Other contributors: DerGenaue, jb
Project: http://syncplay.pl/
Version: 0.2.0
Version: 0.2.1
Note:
* This interface module is intended to be used in conjunction with Syncplay.
* Syncplay provides synchronized video playback across multiple media player instances over the net.
* Syncplay allows group of people who all have the same videos to watch them together wherever they are.
* Syncplay is available to download for free from http://syncplay.pl/
--[==========================================================================[
=== Installation instructions ===
@ -30,28 +30,28 @@ You may also need to re-copy the syncplay.lua file when you update VLC.
.
? >> inputstate-change: [<input/no-input>]
? >> filepath-change-notification
* >> playstate: [<playing/paused/no-input>]
* >> position: [<decimal seconds/no-input>]
get-interface-version
* >> interface-version: [syncplay connector version]
get-duration
* >> duration: [<duration/no-input>]
get-filepath
* >> filepath: [<filepath/no-input>]
get-filename
* >> filepath: [<filename/no-input>]
get-title
* >> title: [<title/no-input>]
set-position: [decimal seconds]
? >> play-error: no-input
seek-within-title: [decimal seconds]
? >> seek-within-title-error: no-input
@ -60,7 +60,7 @@ You may also need to re-copy the syncplay.lua file when you update VLC.
set-rate: [decimal rate]
? >> set-rate-error: no-input
set-title
? >> set-title-error: no-input
@ -86,10 +86,11 @@ else
require "common"
end
local connectorversion = "0.2.0"
local connectorversion = "0.2.1"
local durationdelay = 500000 -- Pause for get_duration command etc for increased reliability (uses microseconds)
local loopsleepduration = 5000 -- Pause for every event loop (uses microseconds)
local loopsleepduration = 5000 -- Pause for every event loop (uses microseconds)
local quitcheckfrequency = 20 -- Check whether VLC has closed every X loops
local host = "localhost"
local port
@ -107,7 +108,7 @@ local noinput = "no-input"
local notimplemented = "not-implemented"
local unknowncommand = "unknown-command"
local unknownstream = "(Unknown Stream)"
local oldfilepath
local oldinputstate
local newfilepath
@ -136,7 +137,7 @@ function detectchanges()
if vlc.object.input() then
newinputstate = "input"
newfilepath = get_filepath()
if newfilepath ~= oldfilepath and get_filepath() ~= unknownstream then
oldfilepath = newfilepath
notificationbuffer = notificationbuffer .. "filepath-change"..notificationmarker..msgterminator
@ -149,30 +150,30 @@ function detectchanges()
end
oldtitle = newtitle
notificationbuffer = notificationbuffer .. "playstate"..msgseperator..tostring(get_play_state())..msgterminator
notificationbuffer = notificationbuffer .. "position"..msgseperator..tostring(get_time())..msgterminator
notificationbuffer = notificationbuffer .. "position"..msgseperator..tostring(get_time())..msgterminator
else
notificationbuffer = notificationbuffer .. "playstate"..msgseperator..noinput..msgterminator
notificationbuffer = notificationbuffer .. "position"..msgseperator..noinput..msgterminator
newinputstate = noinput
end
if newinputstate ~= oldinputstate then
oldinputstate = newinputstate
notificationbuffer = notificationbuffer.."inputstate-change"..msgseperator..tostring(newinputstate)..msgterminator
end
return notificationbuffer
end
function get_args (argument, argcount)
-- Converts comma-space-seperated values into array of a given size, with last item absorbing all remaining data if needed.
-- [Used by the display-osd command]
local argarray = {}
local index
local i
local argbuffer
argbuffer = argument
for i = 1, argcount,1 do
@ -191,38 +192,38 @@ function get_args (argument, argcount)
argarray[i] = ""
end
end
end
return argarray
end
function get_var( vartoget, fallbackvar )
-- [Used by the poll / '.' command to get time]
local response
local errormsg
local input = vlc.object.input()
if input then
response = vlc.var.get(input,tostring(vartoget))
else
response = fallbackvar
errormsg = noinput
end
return response, errormsg
end
function set_var(vartoset, varvalue)
-- [Used by the set-time and set-rate commands]
local errormsg
local input = vlc.object.input()
if input then
vlc.var.set(input,tostring(vartoset),varvalue)
else
@ -268,29 +269,29 @@ end
function get_play_state()
-- [Used by the get-playstate command]
local response
local errormsg
local input = vlc.object.input()
if input then
response = vlc.playlist.status()
else
errormsg = noinput
end
return response, errormsg
end
function get_filepath ()
-- [Used by get-filepath command]
local response
local errormsg
local item
local input = vlc.object.input()
if input then
local item = vlc.input.item()
if item then
@ -312,13 +313,13 @@ function get_filepath ()
else
errormsg = noinput
end
return response, errormsg
end
function get_filename ()
-- [Used by get-filename command]
local response
local index
local filename
@ -338,7 +339,7 @@ function get_filename ()
end
end
end
if(filename ~= nil) and (filename ~= "") and (filename ~= noinput) then
index = string.len(tostring(string.match(filename, ".*/")))
if string.sub(filename,1,3) == ":::" then
@ -349,7 +350,7 @@ function get_filename ()
else
response = noinput
end
return response
end
@ -360,12 +361,12 @@ function get_duration ()
local errormsg
local item
local input = vlc.object.input()
if input then
local item = vlc.input.item()
if (item and item:duration()) then
-- Try to get duration, which might not be available straight away
local i = 0
local i = 0
repeat
vlc.misc.mwait(vlc.misc.mdate() + durationdelay)
response = item:duration()
@ -377,14 +378,14 @@ function get_duration ()
else
errormsg = noinput
end
return response, errormsg
end
function display_osd ( argument )
-- [Used by display-osd command]
local errormsg
local osdarray
local input = vlc.object.input()
@ -401,7 +402,7 @@ end
function load_file (filepath)
-- [Used by load-file command]
local uri = vlc.strings.make_uri(filepath)
vlc.playlist.add({{path=uri}})
return "load-file-attempted\n"
@ -409,14 +410,14 @@ end
function do_command ( command, argument)
-- Processes all commands sent by Syncplay (see protocol, above).
if command == "." then
do return detectchanges() end
end
local command = tostring(command)
local argument = tostring(argument)
local errormsg = ""
local response = ""
local response = ""
if command == "get-interface-version" then response = "interface-version"..msgseperator..connectorversion..msgterminator
elseif command == "get-duration" then response = "duration"..msgseperator..errormerge(get_duration())..msgterminator
@ -428,43 +429,43 @@ function do_command ( command, argument)
elseif command == "set-playstate" then errormsg = set_playstate(argument)
elseif command == "set-rate" then errormsg = set_var("rate", tonumber(argument))
elseif command == "set-title" then errormsg = set_var("title", tonumber(argument))
elseif command == "display-osd" then errormsg = display_osd(argument)
elseif command == "display-osd" then errormsg = display_osd(argument)
elseif command == "load-file" then response = load_file(argument)
elseif command == "close-vlc" then quit_vlc()
else errormsg = unknowncommand
end
if (errormsg ~= nil) and (errormsg ~= "") then
response = command..errormarker..msgseperator..tostring(errormsg)..msgterminator
end
return response
end
function errormerge(argument, errormsg)
-- Used to integrate 'no-input' error messages into command responses.
if (errormsg ~= nil) and (errormsg ~= "") then
do return errormsg end
end
return argument
end
function set_playstate(argument)
-- [Used by the set-playstate command]
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
end
@ -477,23 +478,24 @@ else
end
-- main loop, which alternates between writing and reading
while running == true do
--accept new connections and select active clients
local fd = l:accept()
local buffer, inputbuffer, responsebuffer = "", "", ""
local quitcheckcounter = 0
local fd = l:accept()
local buffer, inputbuffer, responsebuffer = ""
while fd >= 0 and running == true do
-- handle read mode
local str = vlc.net.recv ( fd, 1000)
local responsebuffer
if str == nil then str = "" end
local safestr = string.gsub(tostring(str), "\r", "")
if inputbuffer == nil then inputbuffer = "" end
inputbuffer = inputbuffer .. safestr
while string.find(inputbuffer, msgterminator) and running == true do
@ -507,11 +509,11 @@ while running == true do
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
@ -519,19 +521,31 @@ while running == true do
end
end
if (running == false) then
net.close(fd)
end
-- handle write mode
if (responsebuffer and running == true) then
vlc.net.send( fd, responsebuffer )
responsebuffer = ""
end
vlc.misc.mwait(vlc.misc.mdate() + loopsleepduration) -- Don't waste processor time
-- check if VLC has been closed
quitcheckcounter = quitcheckcounter + 1
if quitcheckcounter > quitcheckfrequency then
if vlc.volume.get() == -256 then
running = false
end
quitcheckcounter = 0
end
end
end

View File

@ -43,7 +43,7 @@ COMMANDS_ROOM = ["r", "room"]
COMMANDS_HELP = ['help', 'h', '?', '/?', r'\?']
MPC_MIN_VER = "1.6.4"
VLC_MIN_VERSION = "2.0.0"
VLC_INTERFACE_MIN_VERSION = "0.2.0"
VLC_INTERFACE_MIN_VERSION = "0.2.1"
MPC_PATHS = [
r"C:\Program Files (x86)\MPC-HC\mpc-hc.exe",
r"C:\Program Files\MPC-HC\mpc-hc.exe",