forked from saily/vnc2video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbs-reader.go
190 lines (159 loc) · 5.01 KB
/
fbs-reader.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
package vnc2video
import (
"bytes"
"encoding/binary"
"io"
"os"
//"vncproxy/common"
//"vncproxy/encodings"
"vnc2video/logger"
//"vncproxy/encodings"
//"vncproxy/encodings"
)
type FbsReader struct {
reader io.ReadCloser
buffer bytes.Buffer
currentTimestamp int
//pixelFormat *PixelFormat
//encodings []IEncoding
}
func (fbs *FbsReader) Close() error {
return fbs.reader.Close()
}
func (fbs *FbsReader) CurrentTimestamp() int {
return fbs.currentTimestamp
}
func (fbs *FbsReader) Read(p []byte) (n int, err error) {
if fbs.buffer.Len() < len(p) {
seg, err := fbs.ReadSegment()
if err != nil {
logger.Error("FBSReader.Read: error reading FBSsegment: ", err)
return 0, err
}
fbs.buffer.Write(seg.bytes)
fbs.currentTimestamp = int(seg.timestamp)
}
return fbs.buffer.Read(p)
}
//func (fbs *FbsReader) CurrentPixelFormat() *PixelFormat { return fbs.pixelFormat }
//func (fbs *FbsReader) CurrentColorMap() *common.ColorMap { return &common.ColorMap{} }
//func (fbs *FbsReader) Encodings() []IEncoding { return fbs.encodings }
func NewFbsReader(fbsFile string) (*FbsReader, error) {
reader, err := os.OpenFile(fbsFile, os.O_RDONLY, 0644)
if err != nil {
logger.Error("NewFbsReader: can't open fbs file: ", fbsFile)
return nil, err
}
return &FbsReader{reader: reader}, //encodings: []IEncoding{
// //&encodings.CopyRectEncoding{},
// //&encodings.ZLibEncoding{},
// //&encodings.ZRLEEncoding{},
// //&encodings.CoRREEncoding{},
// //&encodings.HextileEncoding{},
// &TightEncoding{},
// //&TightPngEncoding{},
// //&EncCursorPseudo{},
// &RawEncoding{},
// //&encodings.RREEncoding{},
//},
nil
}
func (fbs *FbsReader) ReadStartSession() (*ServerInit, error) {
initMsg := ServerInit{}
reader := fbs.reader
var framebufferWidth uint16
var framebufferHeight uint16
var SecTypeNone uint32
//read rfb header information (the only part done without the [size|data|timestamp] block wrapper)
//.("FBS 001.000\n")
bytes := make([]byte, 12)
_, err := reader.Read(bytes)
if err != nil {
logger.Error("FbsReader.ReadStartSession: error reading rbs init message - FBS file Version:", err)
return nil, err
}
//read the version message into the buffer, it is written in the first fbs block
//RFB 003.008\n
bytes = make([]byte, 12)
_, err = fbs.Read(bytes)
if err != nil {
logger.Error("FbsReader.ReadStartSession: error reading rbs init - RFB Version: ", err)
return nil, err
}
//push sec type and fb dimensions
binary.Read(fbs, binary.BigEndian, &SecTypeNone)
if err != nil {
logger.Error("FbsReader.ReadStartSession: error reading rbs init - SecType: ", err)
}
//read frame buffer width, height
binary.Read(fbs, binary.BigEndian, &framebufferWidth)
if err != nil {
logger.Error("FbsReader.ReadStartSession: error reading rbs init - FBWidth: ", err)
return nil, err
}
initMsg.FBWidth = framebufferWidth
binary.Read(fbs, binary.BigEndian, &framebufferHeight)
if err != nil {
logger.Error("FbsReader.ReadStartSession: error reading rbs init - FBHeight: ", err)
return nil, err
}
initMsg.FBHeight = framebufferHeight
//read pixel format
pixelFormat := &PixelFormat{}
binary.Read(fbs, binary.BigEndian, pixelFormat)
if err != nil {
logger.Error("FbsReader.ReadStartSession: error reading rbs init - Pixelformat: ", err)
return nil, err
}
initMsg.PixelFormat = *pixelFormat
//read desktop name
var desknameLen uint32
binary.Read(fbs, binary.BigEndian, &desknameLen)
if err != nil {
logger.Error("FbsReader.ReadStartSession: error reading rbs init - deskname Len: ", err)
return nil, err
}
initMsg.NameLength = desknameLen
bytes = make([]byte, desknameLen)
fbs.Read(bytes)
if err != nil {
logger.Error("FbsReader.ReadStartSession: error reading rbs init - desktopName: ", err)
return nil, err
}
initMsg.NameText = bytes
return &initMsg, nil
}
func (fbs *FbsReader) ReadSegment() (*FbsSegment, error) {
reader := fbs.reader
var bytesLen uint32
//read length
err := binary.Read(reader, binary.BigEndian, &bytesLen)
if err != nil {
logger.Error("FbsReader.ReadSegment: reading len, error reading rbs file: ", err)
return nil, err
}
paddedSize := (bytesLen + 3) & 0x7FFFFFFC
//read bytes
bytes := make([]byte, paddedSize)
_, err = reader.Read(bytes)
if err != nil {
logger.Error("FbsReader.ReadSegment: reading bytes, error reading rbs file: ", err)
return nil, err
}
//remove padding
actualBytes := bytes[:bytesLen]
//read timestamp
var timeSinceStart uint32
binary.Read(reader, binary.BigEndian, &timeSinceStart)
if err != nil {
logger.Error("FbsReader.ReadSegment: read timestamp, error reading rbs file: ", err)
return nil, err
}
//timeStamp := time.Unix(timeSinceStart, 0)
seg := &FbsSegment{bytes: actualBytes, timestamp: timeSinceStart}
return seg, nil
}
type FbsSegment struct {
bytes []byte
timestamp uint32
}