-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathfbs-connection.go
206 lines (174 loc) · 6.26 KB
/
fbs-connection.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package vnc2video
import (
"encoding/binary"
"net"
"github.com/amitbet/vnc2video/logger"
"io"
"time"
)
// Conn represents vnc conection
type FbsConn struct {
FbsReader
protocol string
//c net.IServerConn
//config *ClientConfig
colorMap ColorMap
// Encodings supported by the client. This should not be modified
// directly. Instead, SetEncodings should be used.
encodings []Encoding
// Height of the frame buffer in pixels, sent from the server.
fbHeight uint16
// Width of the frame buffer in pixels, sent from the server.
fbWidth uint16
desktopName string
// The pixel format associated with the connection. This shouldn't
// be modified. If you wish to set a new pixel format, use the
// SetPixelFormat method.
pixelFormat PixelFormat
}
// func (c *FbsConn) Close() error {
// return c.fbs.Close()
// }
// // Read reads data from conn
// func (c *FbsConn) Read(buf []byte) (int, error) {
// return c.fbs.Read(buf)
// }
//dummy, no writing to this conn...
func (c *FbsConn) Write(buf []byte) (int, error) {
return len(buf), nil
}
func (c *FbsConn) Conn() net.Conn {
return nil
}
func (c *FbsConn) Config() interface{} {
return nil
}
func (c *FbsConn) Protocol() string {
return "RFB 003.008"
}
func (c *FbsConn) PixelFormat() PixelFormat {
return c.pixelFormat
}
func (c *FbsConn) SetPixelFormat(pf PixelFormat) error {
c.pixelFormat = pf
return nil
}
func (c *FbsConn) ColorMap() ColorMap { return c.colorMap }
func (c *FbsConn) SetColorMap(cm ColorMap) { c.colorMap = cm }
func (c *FbsConn) Encodings() []Encoding { return c.encodings }
func (c *FbsConn) SetEncodings([]EncodingType) error { return nil }
func (c *FbsConn) Width() uint16 { return c.fbWidth }
func (c *FbsConn) Height() uint16 { return c.fbHeight }
func (c *FbsConn) SetWidth(w uint16) { c.fbWidth = w }
func (c *FbsConn) SetHeight(h uint16) { c.fbHeight = h }
func (c *FbsConn) DesktopName() []byte { return []byte(c.desktopName) }
func (c *FbsConn) SetDesktopName(d []byte) { c.desktopName = string(d) }
func (c *FbsConn) Flush() error { return nil }
func (c *FbsConn) Wait() {}
func (c *FbsConn) SetProtoVersion(string) {}
func (c *FbsConn) SetSecurityHandler(SecurityHandler) error { return nil }
func (c *FbsConn) SecurityHandler() SecurityHandler { return nil }
func (c *FbsConn) GetEncInstance(typ EncodingType) Encoding {
for _, enc := range c.encodings {
if enc.Type() == typ {
return enc
}
}
return nil
}
type VncStreamFileReader interface {
io.Reader
CurrentTimestamp() int
ReadStartSession() (*ServerInit, error)
CurrentPixelFormat() *PixelFormat
Encodings() []Encoding
}
type FBSPlayHelper struct {
Conn *FbsConn
//Fbs VncStreamFileReader
serverMessageMap map[uint8]ServerMessage
firstSegDone bool
startTime int
}
func NewFbsConn(filename string, encs []Encoding) (*FbsConn, error) {
fbs, err := NewFbsReader(filename)
if err != nil {
logger.Error("failed to open fbs reader:", err)
return nil, err
}
//NewFbsReader("/Users/amitbet/vncRec/recording.rbs")
initMsg, err := fbs.ReadStartSession()
if err != nil {
logger.Error("failed to open read fbs start session:", err)
return nil, err
}
fbsConn := &FbsConn{FbsReader: *fbs}
fbsConn.encodings = encs
fbsConn.SetPixelFormat(initMsg.PixelFormat)
fbsConn.SetHeight(initMsg.FBHeight)
fbsConn.SetWidth(initMsg.FBWidth)
fbsConn.SetDesktopName([]byte(initMsg.NameText))
return fbsConn, nil
}
func NewFBSPlayHelper(r *FbsConn) *FBSPlayHelper {
h := &FBSPlayHelper{Conn: r}
h.startTime = int(time.Now().UnixNano() / int64(time.Millisecond))
h.serverMessageMap = make(map[uint8]ServerMessage)
h.serverMessageMap[0] = &FramebufferUpdate{}
h.serverMessageMap[1] = &SetColorMapEntries{}
h.serverMessageMap[2] = &Bell{}
h.serverMessageMap[3] = &ServerCutText{}
return h
}
// func (handler *FBSPlayHelper) Consume(seg *RfbSegment) error {
// switch seg.SegmentType {
// case SegmentFullyParsedClientMessage:
// clientMsg := seg.Message.(ClientMessage)
// logger.Tracef("ClientUpdater.Consume:(vnc-server-bound) got ClientMessage type=%s", clientMsg.Type())
// switch clientMsg.Type() {
// case FramebufferUpdateRequestMsgType:
// if !handler.firstSegDone {
// handler.firstSegDone = true
// handler.startTime = int(time.Now().UnixNano() / int64(time.Millisecond))
// }
// handler.sendFbsMessage()
// }
// // server.MsgFramebufferUpdateRequest:
// }
// return nil
// }
func (h *FBSPlayHelper) ReadFbsMessage(SyncWithTimestamps bool, SpeedFactor float64) (ServerMessage, error) {
var messageType uint8
//messages := make(map[uint8]ServerMessage)
fbs := h.Conn
//conn := h.Conn
err := binary.Read(fbs, binary.BigEndian, &messageType)
if err != nil {
logger.Error("FBSConn.NewConnHandler: Error in reading FBS: ", err)
return nil, err
}
startTimeMsgHandling := time.Now()
//IClientConn{}
//binary.Write(h.Conn, binary.BigEndian, messageType)
msg := h.serverMessageMap[messageType]
if msg == nil {
logger.Error("FBSConn.NewConnHandler: Error unknown message type: ", messageType)
return nil, err
}
//read the actual message data
//err = binary.Read(fbs, binary.BigEndian, &msg)
parsedMsg, err := msg.Read(fbs)
if err != nil {
logger.Error("FBSConn.NewConnHandler: Error in reading FBS message: ", err)
return nil, err
}
millisSinceStart := int(startTimeMsgHandling.UnixNano()/int64(time.Millisecond)) - h.startTime
adjestedTimeStamp := float64(fbs.CurrentTimestamp()) / SpeedFactor
millisToSleep := adjestedTimeStamp - float64(millisSinceStart)
if millisToSleep > 0 && SyncWithTimestamps {
time.Sleep(time.Duration(millisToSleep) * time.Millisecond)
} else if millisToSleep < -400 {
logger.Errorf("rendering time is noticeably off, change speedup factor: videoTimeLine: %f, currentTime:%d, offset: %f", adjestedTimeStamp, millisSinceStart, millisToSleep)
}
return parsedMsg, nil
}