Skip to content

Commit

Permalink
downmerg
Browse files Browse the repository at this point in the history
  • Loading branch information
tijzwa committed Aug 1, 2022
1 parent 37b9cc2 commit 9dc4746
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 37 deletions.
15 changes: 12 additions & 3 deletions html5/js/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,7 @@ class XpraClient {
"sound.server_driven": true,
"server-window-resize": true,
"screen-resize-bigger": false,
"window.initiate-moveresize": true,
"metadata.supported": [
"fullscreen",
"maximized",
Expand Down Expand Up @@ -1614,10 +1615,14 @@ class XpraClient {
"window.pre-map": true,
//partial support:
keyboard: true,
//older servers (v4.3 and older):
xkbmap_layout: this.key_layout,
xkbmap_keycodes: this._get_keycodes(),
xkbmap_print: "",
xkbmap_query: "",
//newer servers (v4.4 and later):
keymap: {
layout: this.key_layout,
keycodes: this._get_keycodes(),
},
desktop_size: [this.desktop_width, this.desktop_height],
desktop_mode_size: [this.desktop_width, this.desktop_height],
screen_sizes: this._get_screen_sizes(),
Expand Down Expand Up @@ -1970,7 +1975,11 @@ class XpraClient {
if (!clipboardData) {
clipboardData = window.clipboardData;
}
if (clipboardData && clipboardData.files && clipboardData.files.length > 0) {
if (
clipboardData &&
clipboardData.files &&
clipboardData.files.length > 0
) {
const files = clipboardData.files;
this.clog("paste got", files.length, "files");
for (let index = 0; index < files.length; index++) {
Expand Down
9 changes: 5 additions & 4 deletions html5/js/ImageDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

class XpraImageDecoder {

async convertToBitmap(packet) {
const width = packet[4];
const height = packet[5];
Expand All @@ -30,14 +29,16 @@ class XpraImageDecoder {
premultiplyAlpha: "none",
resizeWidth: width,
resizeHeight: height,
resizeQuality: "medium"
resizeQuality: "high",
};

const blob = new Blob([packet[7].buffer], { type: `image/${paint_coding}` });
const blob = new Blob([packet[7].buffer], {
type: `image/${paint_coding}`,
});
const bitmap = await createImageBitmap(blob, bitmap_options);
packet[6] = `bitmap:${coding}`;
packet[7] = bitmap;
}
return packet;
};
}
}
66 changes: 46 additions & 20 deletions html5/js/OffscreenDecodeWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ if (XpraVideoDecoderLoader.hasNativeDecoder) {
video_coding.push("vp8");
video_coding.push("vp9");
} else {
console.warn("Offscreen decoding is available for images only. Please consider using Google Chrome 94+ in a secure (SSL or localhost) context h264 offscreen decoding support.");
console.warn(
"Offscreen decoding is available for images only. Please consider using Google Chrome 94+ in a secure (SSL or localhost) context h264 offscreen decoding support."
);
}



const all_encodings = new Set([
"void",
"scroll",
Expand All @@ -56,11 +56,11 @@ function send_decode_error(packet, error) {
self.postMessage({ error: `${error}`, packet });
}


class WindowDecoder {
constructor(canvas, debug) {
this.canvas = canvas;
this.debug = debug;
this.still = new OffscreenCanvas(this.canvas.width, this.canvas.height);
this.init();
}
init() {
Expand Down Expand Up @@ -100,7 +100,7 @@ class WindowDecoder {
} else {
this.decode_queue_draining = false;
}
})
});
}

async proccess_packet(packet) {
Expand All @@ -109,8 +109,9 @@ class WindowDecoder {
if (coding == "eos" && this.video_decoder) {
this.video_decoder._close();
return;
}
else if (image_coding.includes(coding) && !(coding == "scroll" || coding == "void")) {
} else if (coding == "scroll" || coding == "void") {
// Nothing to do
} else if (image_coding.includes(coding)) {
await this.image_decoder.convertToBitmap(packet);
} else if (video_coding.includes(coding)) {
if (coding == "vp9") {
Expand All @@ -126,9 +127,22 @@ class WindowDecoder {
} else {
this.decode_error(packet, `unsupported encoding: '${coding}'`);
}
// Paint the packet on screen refresh (if we can use requestAnimationFrame in the worker)
if (typeof requestAnimationFrame == "function") {
requestAnimationFrame(() => {
this.paint_packet(packet, start);
});
} else {
console.warn(
"No function requestAnimationFrame in webworkers supported by this browser."
);
this.paint_packet(packet, start);
}
}

paint_packet(packet, start) {
// Update the coding propery
coding = packet[6];
const coding = packet[6];
const x = packet[2];
const y = packet[3];
const width = packet[4];
Expand Down Expand Up @@ -166,17 +180,9 @@ class WindowDecoder {
this.paint_box(coding, context, sx, sy, sw, sh);
}
} else if (coding.startsWith("frame")) {
let enc_width = width;
let enc_height = height;
const options = packet[10] || {};
const scaled_size = options["scaled_size"];
if (scaled_size && (enc_width > width || enc_height > height)) {
enc_width = scaled_size[0];
enc_height = scaled_size[1];
}
context.drawImage(data, x, y, enc_width, enc_height);
context.drawImage(data, x, y, width, height);
data.close();
this.paint_box(coding, context, x, y, enc_width, enc_height);
this.paint_box(coding, context, x, y, width, height);
}

// Decode ok.
Expand All @@ -187,6 +193,9 @@ class WindowDecoder {
packet[7] = null;
packet[10] = options;
self.postMessage({ draw: packet, start });

// Call update_still in callback
setTimeout(() => this.update_still(), 0);
}

paint_box(coding, context, px, py, pw, ph) {
Expand All @@ -202,13 +211,22 @@ class WindowDecoder {
}
}

update_still() {
this.still.getContext("2d").drawImage(this.canvas, 0, 0);
}

eos() {
// Add eos packet to queue to prevent closing the decoder before all packets are proceeded
const packet = [];
packet[6] = "eos";
this.decode_queue.push(packet);
}

redraw() {
// Redraw the last know state (saved on still)
this.canvas.getContext("2d").drawImage(this.still, 0, 0);
}

update_geometry(w, h) {
if (this.closed) {
return;
Expand All @@ -219,6 +237,9 @@ class WindowDecoder {
}
this.canvas.width = w;
this.canvas.height = h;
// Also update the still
this.still.width = w;
this.still.height = h;
}

close() {
Expand Down Expand Up @@ -260,14 +281,19 @@ onmessage = function (e) {
if (wd) {
wd.queue_draw_packet(packet);
} else {
send_decode_error(packet, `no window decoder found for wid ${wid}, only:${[...offscreen_canvas.keys(),].join(",")}`);
send_decode_error(
packet,
`no window decoder found for wid ${wid}, only:${[
...offscreen_canvas.keys(),
].join(",")}`
);
}
break;
}
case "redraw":
wd = offscreen_canvas.get(data.wid);
if (wd) {
//wd.redraw();
wd.redraw();
}
break;
case "canvas":
Expand Down
4 changes: 1 addition & 3 deletions html5/js/OffscreenDecodeWorkerHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

const XpraOffscreenWorker = {
isAvailable() {
if (
typeof OffscreenCanvas !== "undefined"
) {
if (typeof OffscreenCanvas !== "undefined") {
//we also need the direct constructor:
try {
new OffscreenCanvas(256, 256);
Expand Down
17 changes: 10 additions & 7 deletions html5/js/VideoDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class XpraVideoDecoder {

// Latest possible check for draining
if (this.draining) {

videoFrame.close();
return;
}
Expand All @@ -148,6 +147,7 @@ class XpraVideoDecoder {
}

queue_frame(packet) {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
const options = packet[10] || {};
const data = packet[7];
Expand All @@ -162,11 +162,11 @@ class XpraVideoDecoder {
}

if (this.videoDecoder.state == "closed") {
reject("video decoder is closed");
reject(new Error("video decoder is closed"));
return;
}
if (this.draining) {
reject("video decoder is draining");
reject(new Error("video decoder is draining"));
return;
}

Expand All @@ -180,8 +180,10 @@ class XpraVideoDecoder {
const chunk = new EncodedVideoChunk(init);
this.videoDecoder.decode(chunk);

let frame_out = this.decoded_frames.filter(p => p[8] == packet_sequence);
while (frame_out.length == 0) {
let frame_out = this.decoded_frames.filter(
(p) => p[8] == packet_sequence
);
while (frame_out.length === 0) {
// Await our frame
await new Promise(r => setTimeout(r, this.frameWaitTimeout));
if (this.erroneous_frame) {
Expand All @@ -198,8 +200,9 @@ class XpraVideoDecoder {
}

// Remove the frame from decoded frames list
this.decoded_frames = this.decoded_frames.filter(p => p[8] != packet_sequence);

this.decoded_frames = this.decoded_frames.filter(
(p) => p[8] != packet_sequence
);
resolve(frame_out[0]);
});
}
Expand Down

0 comments on commit 9dc4746

Please sign in to comment.