Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XrdpUlalacaPrivate.cpp - github.com/neutrinolabs/ulalaca-xrdp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec8213e918c6ef6bb5d546271df8e0298620093b (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//
// Created by Gyuhwan Park on 2023/01/28.
//

#include <cmath>

#include "XrdpUlalacaPrivate.hpp"

#include "ProjectorClient.hpp"

XrdpUlalacaPrivate::XrdpUlalacaPrivate(XrdpUlalaca *mod):
        _mod(mod),
        _error(0),

        _sessionSize(),
        _screenLayouts(),

        _bpp(0),

        _frameId(0),
        _ackFrameId(0),

        _username(),
        _password(),
        _ip(),
        _port(),

        _keyLayout(0),
        _delayMs(0),
        _guid(),
        _encodingsMask(0),
        _clientInfo(),

        _socket(),
        _projectorClient(),

        _fullInvalidate(true),
        _commitUpdateLock(),

        _dirtyRects(std::make_shared<std::vector<ULIPCRect>>())
{
}

XrdpUlalacaPrivate::~XrdpUlalacaPrivate() {
    _isUpdateThreadRunning = false;
    if (_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();
    _isUpdateThreadRunning = true;
    _updateThread = std::make_unique<std::thread>(
        &XrdpUlalacaPrivate::updateThreadLoop, this
    );

    LOG(LOG_LEVEL_TRACE, "sessionSize: %d, %d", _sessionSize.width, _sessionSize.height);
    _projectorClient->setViewport(_sessionSize);
    _projectorClient->setOutputSuppression(false);
}

int XrdpUlalacaPrivate::decideCopyRectSize() const {
    if (isRFXCodec()) {
        return 64;
    }

    if (isNSCodec() || isH264Codec() || isGFXH264Codec()) {
        // ??? FIXME
        return RECT_SIZE_BYPASS_CREATE;
    }

    return RECT_SIZE_BYPASS_CREATE;
}

std::unique_ptr<std::vector<ULIPCRect>> XrdpUlalacaPrivate::createCopyRects(
    std::vector<ULIPCRect> &dirtyRects,
    int rectSize
) const {
    auto blocks = std::make_unique<std::vector<ULIPCRect>>();
    blocks->reserve((_sessionSize.width * _sessionSize.height) / (rectSize * rectSize));

    if (rectSize == RECT_SIZE_BYPASS_CREATE) {
        std::copy(dirtyRects.begin(), dirtyRects.end(), std::back_insert_iterator(*blocks));
        return std::move(blocks);
    }

    int mapWidth  = ceil((float) _sessionSize.width / (float) rectSize);
    int mapHeight = ceil((float) _sessionSize.height / (float) rectSize);
    int mapSize = mapWidth * mapHeight;
    std::unique_ptr<uint8_t> rectMap(new uint8_t[mapSize]);
    memset(rectMap.get(), 0x00, mapSize);

    for (auto &dirtyRect : dirtyRects) {
        if (_sessionSize.width <= dirtyRect.x ||
            _sessionSize.height <= dirtyRect.y) {
            continue;
        }

        int mapX1 = dirtyRect.x / rectSize;
        int mapY1 = dirtyRect.y / rectSize;
        int mapX2 = std::min(
                (dirtyRect.x + dirtyRect.width) / rectSize,
                mapWidth - 1
        );
        int mapY2 = std::min(
                (dirtyRect.y + dirtyRect.height) / rectSize,
                mapHeight - 1
        );

        for (int y = mapY1; y <= mapY2; y++) {
            for (int x = mapX1; x <= mapX2; x++) {
                rectMap.get()[(y * mapWidth) + x] = 0x01;
            }
        }
    }

    for (int y = 0; y < mapHeight; y++) {
        for (int x = 0; x < mapWidth; x++) {
            if (rectMap.get()[(y * mapWidth) + x] == 0x01) {
                int rectX = x * rectSize;
                int rectY = y * rectSize;

                blocks->emplace_back(ULIPCRect{(short) rectX, (short) rectY, (short) rectSize, (short) rectSize});
            }
        }
    }

    return std::move(blocks);
}

bool XrdpUlalacaPrivate::isRectOverlaps(const ULIPCRect &a, const ULIPCRect &b) {
    int16_t a_x1 = a.x;
    int16_t a_x2 = a.x + a.width;
    int16_t a_y1 = a.y;
    int16_t a_y2 = a.y + a.height;
    int16_t b_x1 = b.x;
    int16_t b_x2 = b.x + b.width;
    int16_t b_y1 = b.y;
    int16_t b_y2 = b.y + b.height;

    return (
            (a_x1 >= b_x1 && a_x1 <= b_x2 && a_y1 >= b_y1 && a_y1 <= b_y2) ||
            (a_x2 >= b_x1 && a_x2 <= b_x2 && a_y1 >= b_y1 && a_y1 <= b_y2) ||
            (a_x1 >= b_x1 && a_x1 <= b_x2 && a_y2 >= b_y1 && a_y2 <= b_y2) ||
            (a_x2 >= b_x1 && a_x2 <= b_x2 && a_y2 >= b_y1 && a_y2 <= b_y2)
    );
}

void XrdpUlalacaPrivate::mergeRect(ULIPCRect &a, const ULIPCRect &b) {
    int16_t a_x1 = a.x;
    int16_t a_x2 = a.x + a.width;
    int16_t a_y1 = a.y;
    int16_t a_y2 = a.y + a.height;
    int16_t b_x1 = b.x;
    int16_t b_x2 = b.x + b.width;
    int16_t b_y1 = b.y;
    int16_t b_y2 = b.y + b.height;

    a.x = std::min(a_x1, b_x1);
    a.y = std::min(a_y1, b_y1);
    a.width  = std::max(a_x2, b_x2) - a.x;
    a.height = std::max(a_y2, b_y2) - a.y;
}

std::vector<ULIPCRect> XrdpUlalacaPrivate::removeRectOverlap(const ULIPCRect &a, const ULIPCRect &b) {

}

void XrdpUlalacaPrivate::addDirtyRect(ULIPCRect &rect) {
    for (auto &x: *_dirtyRects) {
        if (isRectOverlaps(x, rect)) {
            mergeRect(x, rect);
            return;
        }
    }


    _dirtyRects->push_back(rect);
}

void XrdpUlalacaPrivate::commitUpdate(const uint8_t *image, size_t size, int32_t width, int32_t height) {
    std::shared_ptr<uint8_t> tmp((uint8_t *) g_malloc(size, 0), g_free);
    memmove(tmp.get(), image, size);

    auto dirtyRects = _dirtyRects;
    _dirtyRects = std::make_shared<std::vector<ULIPCRect>>();

    auto now = std::chrono::steady_clock::now();
    _updateQueue.emplace(ScreenUpdate {
            std::chrono::duration<double>(now.time_since_epoch()).count(),
            tmp, size, width, height, dirtyRects
    });
}

void XrdpUlalacaPrivate::ipcDisconnected() {
    LOG(LOG_LEVEL_WARNING, "ipc disconnected");
    _error = 1;
}

void XrdpUlalacaPrivate::updateThreadLoop() {
    std::vector<ULIPCRect> skippedRects;

    while (_isUpdateThreadRunning) {
        while (_updateQueue.empty()) {
            using namespace std::chrono_literals;
            std::this_thread::sleep_for(4ms);
        }

        if (!_isUpdateThreadRunning) {
            break;
        }

        _commitUpdateLock.lock();
        auto update = std::move(_updateQueue.front());
        _updateQueue.pop();
        _commitUpdateLock.unlock();


        auto now = std::chrono::steady_clock::now();
        auto tdelta = std::chrono::duration<double>(now.time_since_epoch()).count() - update.timestamp;

        auto width = update.width;
        auto height = update.height;
        auto dirtyRects = update.dirtyRects;
        auto image = update.image;

        if (tdelta > 1.0 / 30.0 || _updateQueue.size() > 2 || std::abs(_frameId - _ackFrameId) > 1) {
            LOG(LOG_LEVEL_INFO, "skipping frame (tdelta = %.4f)", tdelta);
            skippedRects.insert(skippedRects.end(), dirtyRects->begin(), dirtyRects->end());
            continue;
        } else {
            dirtyRects->insert(dirtyRects->end(), skippedRects.begin(), skippedRects.end());
            skippedRects.clear();
        }

        // LOG(LOG_LEVEL_TRACE, "updating screen: [%.4f] %d, %d", update.timestamp, update.width, update.height);

        if (_sessionSize.width != update.width || _sessionSize.height != update.height) {
            // server_reset(this, width, height, _bpp);
        }

        if (_frameId > 0 && dirtyRects->empty()) {
            continue;
        }

        _mod->server_begin_update(_mod);

        ULIPCRect screenRect {0, 0, (short) width, (short) height};
        auto copyRectSize = decideCopyRectSize();

#ifdef XRDP_TUMOD_ENCODER_HINTS_AVAILABLE
        int paintFlags = XRDP_ENCODER_HINT_QUALITY_LOW;
#else
        int paintFlags = 0;
#endif

        if (!_fullInvalidate) {
            auto copyRects = createCopyRects(*dirtyRects, copyRectSize);

            _mod->server_paint_rects(
                    _mod,
                    dirtyRects->size(), reinterpret_cast<short *>(dirtyRects->data()),
                    copyRects->size(), reinterpret_cast<short *>(copyRects->data()),
                    (char *) image.get(),
                    width, height,
                    paintFlags, (_frameId++ % INT32_MAX)
            );
        } else {
            // paint entire screen
            auto dirtyRects = std::vector<ULIPCRect> { screenRect } ;
            auto copyRects = createCopyRects(dirtyRects, copyRectSize);

            if (isRawBitmap()) {
                _mod->server_paint_rect(
                        _mod,
                        screenRect.x, screenRect.y,
                        screenRect.width, screenRect.height,
                        (char *) image.get(),
                        screenRect.width, screenRect.height,
                        0, 0
                );
            } else {
                _mod->server_paint_rects(
                        _mod,
                        dirtyRects.size(), reinterpret_cast<short *>(dirtyRects.data()),
                        copyRects->size(), reinterpret_cast<short *>(copyRects->data()),
                        (char *) image.get(),
                        width, height,
                        paintFlags, (_frameId++ % INT32_MAX)
                );
            }

            _fullInvalidate = false;
        }

        _mod->server_end_update(_mod);
    }

}

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());
}