yuv wasm seems slow

This commit is contained in:
rustdesk 2022-01-27 23:32:51 +08:00
parent c8b681b84c
commit 58f2419f27
2 changed files with 40 additions and 20 deletions

View File

@ -143,14 +143,14 @@ export default class Connection {
} }
if (!pk) { if (!pk) {
// send an empty message out in case server is setting up secure and waiting for first message // send an empty message out in case server is setting up secure and waiting for first message
await this._ws?.sendMessage({}); this._ws?.sendMessage({});
return; return;
} }
const msg = this._ws?.parseMessage(await this._ws?.next()); const msg = this._ws?.parseMessage(await this._ws?.next());
let signedId: any = msg?.signedId; let signedId: any = msg?.signedId;
if (!signedId) { if (!signedId) {
console.error("Handshake failed: invalid message type"); console.error("Handshake failed: invalid message type");
await this._ws?.sendMessage({}); this._ws?.sendMessage({});
return; return;
} }
try { try {
@ -160,7 +160,7 @@ export default class Connection {
// fall back to non-secure connection in case pk mismatch // fall back to non-secure connection in case pk mismatch
console.error("pk mismatch, fall back to non-secure"); console.error("pk mismatch, fall back to non-secure");
const publicKey = message.PublicKey.fromPartial({}); const publicKey = message.PublicKey.fromPartial({});
await this._ws?.sendMessage({ publicKey }); this._ws?.sendMessage({ publicKey });
return; return;
} }
signedId = new TextDecoder().decode(signedId!); signedId = new TextDecoder().decode(signedId!);
@ -169,7 +169,7 @@ export default class Connection {
let theirPk = tmp[1]; let theirPk = tmp[1];
if (id != this._id!) { if (id != this._id!) {
console.error("Handshake failed: sign failure"); console.error("Handshake failed: sign failure");
await this._ws?.sendMessage({}); this._ws?.sendMessage({});
return; return;
} }
theirPk = globals.decodeBase64(theirPk); theirPk = globals.decodeBase64(theirPk);
@ -177,7 +177,7 @@ export default class Connection {
console.error( console.error(
"Handshake failed: invalid public box key length from peer" "Handshake failed: invalid public box key length from peer"
); );
await this._ws?.sendMessage({}); this._ws?.sendMessage({});
return; return;
} }
const [mySk, asymmetricValue] = globals.genBoxKeyPair(); const [mySk, asymmetricValue] = globals.genBoxKeyPair();
@ -187,7 +187,7 @@ export default class Connection {
asymmetricValue, asymmetricValue,
symmetricValue, symmetricValue,
}); });
await this._ws?.sendMessage({ publicKey }); this._ws?.sendMessage({ publicKey });
this._ws?.setSecretKey(secretKey); this._ws?.setSecretKey(secretKey);
return true; return true;
} }
@ -198,11 +198,11 @@ export default class Connection {
if (msg?.hash) { if (msg?.hash) {
this._hash = msg?.hash; this._hash = msg?.hash;
if (!this._password) this.msgbox("input-password", "Password Required", ""); if (!this._password) this.msgbox("input-password", "Password Required", "");
await this.login(this._password); this.login(this._password);
} else if (msg?.testDelay) { } else if (msg?.testDelay) {
const testDelay = msg?.testDelay; const testDelay = msg?.testDelay;
if (!testDelay.fromClient) { if (!testDelay.fromClient) {
await this._ws?.sendMessage({ testDelay }); this._ws?.sendMessage({ testDelay });
} }
} else if (msg?.loginResponse) { } else if (msg?.loginResponse) {
const r = msg?.loginResponse; const r = msg?.loginResponse;
@ -237,7 +237,7 @@ export default class Connection {
this._msgbox?.(type_, title, text); this._msgbox?.(type_, title, text);
} }
draw(frame: Uint8Array) { draw(frame: any) {
this._draw?.(frame); this._draw?.(frame);
} }
@ -249,11 +249,11 @@ export default class Connection {
this._audioDecoder?.close(); this._audioDecoder?.close();
} }
async refresh() { refresh() {
const misc = message.Misc.fromPartial({ const misc = message.Misc.fromPartial({
refreshVideo: true, refreshVideo: true,
}); });
await this._ws?.sendMessage({ misc }); this._ws?.sendMessage({ misc });
} }
setMsgbox(callback: MsgboxCallback) { setMsgbox(callback: MsgboxCallback) {
@ -264,7 +264,7 @@ export default class Connection {
this._draw = callback; this._draw = callback;
} }
async login(password: string | undefined, _remember: Boolean = false) { login(password: string | undefined, _remember: Boolean = false) {
this._password = password; this._password = password;
if (password) { if (password) {
const salt = this._hash?.salt; const salt = this._hash?.salt;
@ -272,9 +272,9 @@ export default class Connection {
const challenge = this._hash?.challenge; const challenge = this._hash?.challenge;
p = hash([p, challenge!]); p = hash([p, challenge!]);
this.msgbox("connecting", "Connecting...", "Logging in..."); this.msgbox("connecting", "Connecting...", "Logging in...");
await this._sendLoginMessage(p); this._sendLoginMessage(p);
} else { } else {
await this._sendLoginMessage(); this._sendLoginMessage();
} }
} }
@ -283,14 +283,14 @@ export default class Connection {
await this.start(this._id); await this.start(this._id);
} }
async _sendLoginMessage(password: Uint8Array | undefined = undefined) { _sendLoginMessage(password: Uint8Array | undefined = undefined) {
const loginRequest = message.LoginRequest.fromPartial({ const loginRequest = message.LoginRequest.fromPartial({
username: this._id!, username: this._id!,
myId: "web", // to-do myId: "web", // to-do
myName: "web", // to-do myName: "web", // to-do
password, password,
}); });
await this._ws?.sendMessage({ loginRequest }); this._ws?.sendMessage({ loginRequest });
} }
handleVideoFrame(vf: message.VideoFrame) { handleVideoFrame(vf: message.VideoFrame) {

View File

@ -11,7 +11,11 @@ var currentFrame = undefined;
var events = []; var events = [];
window.curConn = undefined; window.curConn = undefined;
window.getRgba = () => currentFrame; window.getRgba = () => {
const tmp = currentFrame;
currentFrame = undefined;
return tmp || null;
}
window.getLanguage = () => navigator.language; window.getLanguage = () => navigator.language;
export function msgbox(type, title, text) { export function msgbox(type, title, text) {
@ -37,7 +41,7 @@ export function pushEvent(name, payload) {
} }
export function draw(frame) { export function draw(frame) {
currentFrame = frame; currentFrame = I420ToABGR(frame);
} }
export function setConn(conn) { export function setConn(conn) {
@ -276,8 +280,24 @@ window.init = async () => {
await initZstd(); await initZstd();
} }
function I420ToARGB(yuvbuffer) { function I420ToABGR(yb) {
// if (!wasmDsp) return null;
const yPtr = wasmDsp._malloc(yb.y.bytes.length);
wasmDsp.HEAPU8.set(yb.y.bytes, yPtr);
const uPtr = wasmDsp._malloc(yb.u.bytes.length);
wasmDsp.HEAPU8.set(yb.u.bytes, uPtr);
const vPtr = wasmDsp._malloc(yb.v.bytes.length);
wasmDsp.HEAPU8.set(yb.v.bytes, vPtr);
const oSize = yb.format.width * yb.format.height * 4;
const outPtr = wasmDsp._malloc(oSize);
const res = wasmDsp._I420ToABGR(yPtr, yb.y.stride, uPtr, yb.u.stride, vPtr, yb.v.stride, outPtr, yb.format.width * 4,
yb.format.width, yb.format.height);
const out = wasmDsp.HEAPU8.slice(outPtr, outPtr + oSize);
wasmDsp._free(yPtr);
wasmDsp._free(uPtr);
wasmDsp._free(vPtr);
wasmDsp._free(outPtr);
return out;
} }
async function initZstd() { async function initZstd() {