forked from jwatzman/vlc-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-client.lua
More file actions
133 lines (113 loc) · 3.4 KB
/
sync-client.lua
File metadata and controls
133 lines (113 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
--[[
Syncronization extension for VLC -- client component
Copyright (c) 2011 Joshua Watzman (sinclair44@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
--]]
-- set to false to tell TODO
enabled = true
function dlog(msg)
vlc.msg.dbg(string.format("[sync-client] %s", msg))
end
function descriptor()
return
{
title = "Sync Client";
version = "2011-01-24";
author = "jwatzman";
shortdesc = "Sync Client";
description = "Synchronizes two viewings of the same video over the "
.. "internet. This is the client component -- we connect "
.. "to the server and it tells us how far into the video "
.. "we should be.";
capabilities = { "input-listener", "playing-listener" };
}
end
function client(fd)
while enabled do
local pollfds = {}
pollfds[fd] = vlc.net.POLLIN
local input = vlc.object.input()
if not input then
enabled = false
vlc.deactivate()
elseif vlc.net.poll(pollfds, 1) > 0 then
local str = vlc.net.recv(fd, 1000)
if str == "" or str == "\004" then
enabled = false
vlc.deactivate()
else
vlc.keep_alive()
local i, j = string.find(str, "%d+\n")
local remote_time = tonumber(string.sub(str, i, j))
local local_time = math.floor(1000 * vlc.var.get(input, "time"))
if math.abs(remote_time - local_time) > tonumber(jitter) then
dlog(string.format("updating time from %i to %i",
local_time, remote_time))
vlc.var.set(input, "time", remote_time/1000)
end
end
end
end
vlc.net.close(fd)
end
function update_params()
server = server_w:get_text()
jitter = jitter_w:get_text()
vlc.msg.info(server)
vlc.msg.info(jitter)
conf_d:delete()
local fd = vlc.net.connect_tcp(server, 1234)
if fd < 0 then
dlog("connect_tcp failed")
local dialog = vlc.dialog("Sync Client")
dialog:add_label("Connection failed.", 1, 1, 1, 1)
dialog:add_button("Close", vlc.deactivate, 2, 1, 1, 1)
dialog:show()
else
client(fd)
end
end
function set_params()
conf_d = vlc.dialog("Sync Client")
conf_d:add_label("Connect to:", 1, 1, 1, 1)
conf_d:add_label("Jitter tolerance (ms):", 1, 2, 1, 1)
server_w = conf_d:add_text_input("localhost", 2, 1, 1, 1)
jitter_w = conf_d:add_text_input("250", 2, 2, 1, 1)
conf_d:add_button("OK", update_params, 3, 1, 1, 1)
conf_d:show()
end
function activate()
dlog("activated")
enabled = true
if not vlc.input.is_playing() then
dlog("not playing")
local dialog = vlc.dialog("Sync Client")
dialog:add_label("Nothing is playing!", 1, 1, 1, 1)
dialog:add_button("Close", vlc.deactivate, 2, 1, 1, 1)
dialog:show()
else
set_params()
end
end
function deactivate()
dlog("deactivated")
enabled = false
end
function input_changed()
dlog("input changed")
vlc.deactivate()
end
function status_changed()
dlog("status changed")
vlc.deactivate()
end