-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathXrdpUlalacaPrivate.cpp
215 lines (163 loc) · 5.5 KB
/
XrdpUlalacaPrivate.cpp
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
207
208
209
210
211
212
213
//
// Created by Gyuhwan Park on 2023/01/28.
//
#include <cmath>
#include "XrdpUlalacaPrivate.hpp"
#include "ProjectorClient.hpp"
#include "ULSurface.hpp"
const std::string XrdpUlalacaPrivate::XRDP_ULALACA_VERSION = "0.1.1-git";
XrdpUlalacaPrivate::XrdpUlalacaPrivate(XrdpUlalaca *mod):
_mod(mod),
_error(0),
_sessionSize(),
_screenLayouts(),
_bpp(0),
_username(),
_password(),
_ip(),
_port(),
_keyLayout(0),
_delayMs(0),
_guid(),
_encodingsMask(0),
_clientInfo(),
_socket(),
_projectorClient(),
_dirtyRects(),
_surface(std::make_unique<ulalaca::ULSurface>(mod)),
_updateWaitObj(g_create_wait_obj("ulalaca_screen_update"))
{
}
XrdpUlalacaPrivate::~XrdpUlalacaPrivate() {
_isUpdateThreadRunning = false;
if (_updateThread != nullptr && _updateThread->joinable()) {
_updateThread->join();
}
}
void XrdpUlalacaPrivate::serverMessage(const char *message, int code) {
_mod->server_msg(_mod, message, code);
LOG(LOG_LEVEL_INFO, "server_msg: %s", message);
}
void XrdpUlalacaPrivate::attachToSession(std::string sessionPath) {
if (_projectorClient != nullptr) {
LOG(LOG_LEVEL_ERROR, "already attached to session");
// TODO: throw runtime_error
return;
}
_projectorClient = std::make_unique<ProjectorClient>(
*this, sessionPath
);
_projectorClient->start();
_projectorClient->sendHello(XRDP_ULALACA_VERSION, _clientInfo);
_isUpdateThreadRunning = true;
_updateThread = std::make_unique<std::thread>(
&XrdpUlalacaPrivate::updateThreadLoop, this
);
_projectorClient->setViewport(_sessionSize);
_projectorClient->setOutputSuppression(false);
_surface->setCRectSize(decideCopyRectSize());
}
int XrdpUlalacaPrivate::decideCopyRectSize() const {
if (isRFXCodec()) {
return 64;
}
if (isNSCodec() || isH264Codec() || isGFXH264Codec()) {
// ??? FIXME
return ulalaca::ULSurface::RECT_SIZE_BYPASS_CREATE;
}
return ulalaca::ULSurface::RECT_SIZE_BYPASS_CREATE;
}
void XrdpUlalacaPrivate::addDirtyRect(ULIPCRect &rect) {
_dirtyRects.push_back(rect);
}
void XrdpUlalacaPrivate::commitUpdate(const uint8_t *image, size_t size, int32_t width, int32_t height) {
auto transaction = std::make_unique<ulalaca::ULSurfaceTransaction>();
// FIXME: TODO: timestamp should be retrieved from the sessionprojector.app
auto now = std::chrono::steady_clock::now();
double timestamp = std::chrono::duration<double>(now.time_since_epoch()).count();
transaction->addRects(_dirtyRects);
transaction->setTimestamp(timestamp);
transaction->setBitmap(
std::shared_ptr<uint8_t>((uint8_t *) g_malloc(size, 0), g_free),
size, width, height
);
memmove(transaction->bitmap().get(), image, size);
_dirtyRects.clear();
{
std::lock_guard<std::mutex> lockGuard(_updateQueueMutex);
_updateQueue.emplace(std::move(transaction));
}
_updateQueueCondvar.notify_one();
}
void XrdpUlalacaPrivate::ipcDisconnected() {
LOG(LOG_LEVEL_WARNING, "ipc disconnected");
_error = 1;
}
void XrdpUlalacaPrivate::updateThreadLoop() {
std::vector<ULIPCRect> skippedRects;
std::unique_ptr<ulalaca::ULSurfaceTransaction> transaction;
g_reset_wait_obj(_updateWaitObj);
while (_isUpdateThreadRunning) {
{
using namespace std::chrono_literals;
std::unique_lock<std::mutex> lock(_updateQueueMutex);
if (!_updateQueueCondvar.wait_for(lock, 1s, [this] { return !_updateQueue.empty(); })) {
continue;
}
transaction = std::move(_updateQueue.front());
_updateQueue.pop();
if (transaction == nullptr) {
continue;
}
}
try {
_surface->beginUpdate();
if (!_surface->submitUpdate(*transaction)) {
// log("frame dropped");
}
_surface->endUpdate();
} catch (ulalaca::ULSurfaceException &e) {
LOG(LOG_LEVEL_ERROR, "failed to commit transaction: %s", e.what());
}
g_set_wait_obj(_updateWaitObj);
}
}
void XrdpUlalacaPrivate::setSessionSize(int width, int height) {
_screenLayouts.clear();
_screenLayouts.emplace_back(ULIPCRect {
0, 0, (short) width, (short) height
});
calculateSessionSize();
_surface->setSize(_sessionSize.width, _sessionSize.height);
if (_projectorClient != nullptr) {
_projectorClient->setViewport(_sessionSize);
}
}
void XrdpUlalacaPrivate::calculateSessionSize() {
// TODO: calculate session size to support multiple display environments
if (_screenLayouts.empty()) {
_sessionSize = ULIPCRect {
0, 0, 640, 480
};
return;
}
_sessionSize = _screenLayouts.front();
}
bool XrdpUlalacaPrivate::isNSCodec() const {
return _clientInfo.ns_codec_id != 0;
}
bool XrdpUlalacaPrivate::isRFXCodec() const {
return _clientInfo.rfx_codec_id != 0;
}
bool XrdpUlalacaPrivate::isJPEGCodec() const {
return _clientInfo.jpeg_codec_id != 0;
}
bool XrdpUlalacaPrivate::isH264Codec() const {
return _clientInfo.h264_codec_id != 0;
}
bool XrdpUlalacaPrivate::isGFXH264Codec() const {
return _clientInfo.capture_code & 3;
}
bool XrdpUlalacaPrivate::isRawBitmap() const {
return !(isNSCodec() || isRFXCodec() || isJPEGCodec() || isH264Codec() || isGFXH264Codec());
}