opus is slow

This commit is contained in:
rustdesk 2022-02-06 03:13:41 +08:00
parent 03e433789e
commit 38a468a5cc
7 changed files with 5580 additions and 53 deletions

View File

@ -1,15 +1 @@
<svg width="410" height="404" viewBox="0 0 410 404" fill="none" xmlns="http://www.w3.org/2000/svg"> <svg viewBox="0 0 375 375" style="width:32px;height:32px;margin:0 4px 4px 0" xmlns="http://www.w3.org/2000/svg"><rect transform="matrix(.91553 0 0 .91553 -152.92 116.76)" x="167.03" y="-127.54" width="409.6" height="409.6" rx="64" ry="64" fill="#0071ff"></rect><path d="M150.428 322.264c-29.063-6.202-53.897-22.439-73.115-47.804-19.507-25.746-27.838-55.355-25.723-91.414 6.655-62.013 47.667-106.753 99.687-120.411 4.509-.989 8.353-3.462 12.55-1.322 3.22 1.64 6.028 4.467 7.206 7.251 1.25 2.955 1.877 21.54.99 29.331-1.076 9.46-3.877 12.418-14.566 15.388-29.723 10.195-48.105 34.07-53.697 61.017-4.8 29.668 2.951 59.729 21.528 78.727 8.966 8.993 17.92 14.24 30.869 18.086 8.646 2.57 13.393 5.758 15.036 10.102 1.085 2.867 1.63 22.984.779 28.772-1.33 9.046-1.702 9.796-5.792 11.667-5.029 2.3-7.404 2.392-15.752.61zm50.708.29c-3.092-1.402-5.673-4.83-6.73-8.94-.134-9.408-2.366-25.754 1.02-33.373 1.88-4.128 4.65-5.999 12.433-8.396 21.267-6.551 37.593-19.88 46.806-38.213 11.11-22.108 11.877-55.183 1.808-77.975-9.154-20.723-25.7-35.217-48.555-42.534-8.872-2.84-12.004-5.065-12.968-9.21-1.002-4.31-1.435-19.87-.785-28.218.682-8.766 1.249-9.99 6.162-13.318 3.701-2.505 5.482-2.446 17.223.575 36.718 10.077 65.97 33.597 83.026 66.68 18.495 37.034 19.191 86.11 1.742 122.655-17.233 36.09-50.591 62.511-88.622 70.194-8.172 1.65-9.07 1.656-12.56.073z" fill="#fff"></path></svg>
<path d="M399.641 59.5246L215.643 388.545C211.844 395.338 202.084 395.378 198.228 388.618L10.5817 59.5563C6.38087 52.1896 12.6802 43.2665 21.0281 44.7586L205.223 77.6824C206.398 77.8924 207.601 77.8904 208.776 77.6763L389.119 44.8058C397.439 43.2894 403.768 52.1434 399.641 59.5246Z" fill="url(#paint0_linear)"/>
<path d="M292.965 1.5744L156.801 28.2552C154.563 28.6937 152.906 30.5903 152.771 32.8664L144.395 174.33C144.198 177.662 147.258 180.248 150.51 179.498L188.42 170.749C191.967 169.931 195.172 173.055 194.443 176.622L183.18 231.775C182.422 235.487 185.907 238.661 189.532 237.56L212.947 230.446C216.577 229.344 220.065 232.527 219.297 236.242L201.398 322.875C200.278 328.294 207.486 331.249 210.492 326.603L212.5 323.5L323.454 102.072C325.312 98.3645 322.108 94.137 318.036 94.9228L279.014 102.454C275.347 103.161 272.227 99.746 273.262 96.1583L298.731 7.86689C299.767 4.27314 296.636 0.855181 292.965 1.5744Z" fill="url(#paint1_linear)"/>
<defs>
<linearGradient id="paint0_linear" x1="6.00017" y1="32.9999" x2="235" y2="344" gradientUnits="userSpaceOnUse">
<stop stop-color="#41D1FF"/>
<stop offset="1" stop-color="#BD34FE"/>
</linearGradient>
<linearGradient id="paint1_linear" x1="194.651" y1="8.81818" x2="236.076" y2="292.989" gradientUnits="userSpaceOnUse">
<stop stop-color="#FFEA83"/>
<stop offset="0.0833333" stop-color="#FFDD35"/>
<stop offset="1" stop-color="#FFA800"/>
</linearGradient>
</defs>
</svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -2,9 +2,10 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="favicon.svg" /> <link rel="icon" type="image/svg+xml" href="favicon.svg?v2" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="ogvjs-1.8.6/ogv.js"></script> <script src="ogvjs-1.8.6/ogv.js"></script>
<script src="libopus.js"></script>
<script src="./yuv-canvas-1.2.6.js"></script> <script src="./yuv-canvas-1.2.6.js"></script>
<title>Vite App</title> <title>Vite App</title>
</head> </head>

5555
libopus.js Normal file

File diff suppressed because it is too large Load Diff

BIN
libopus.wasm Executable file

Binary file not shown.

View File

@ -40,18 +40,4 @@ export async function loadVp9(callback) {
}, },
{ worker: true, threading: true } { worker: true, threading: true }
); );
}
export function loadOpus(callback, channels, rate) {
window.OGVLoader.loadClass(
"OGVDecoderAudioOpusW",
(audioCodecClass) => {
audioCodecClass({ audioFormat: { channels, rate } }).then((decoder) => {
decoder.init(() => {
callback(decoder);
})
})
},
{ worker: true }
);
} }

View File

@ -1,7 +1,7 @@
import Websock from "./websock"; import Websock from "./websock";
import * as message from "./message.js"; import * as message from "./message.js";
import * as rendezvous from "./rendezvous.js"; import * as rendezvous from "./rendezvous.js";
import { loadVp9, loadOpus } from "./codec"; import { loadVp9 } from "./codec";
import * as sha256 from "fast-sha256"; import * as sha256 from "fast-sha256";
import * as globals from "./globals"; import * as globals from "./globals";
import { decompress, mapKey, sleep } from "./common"; import { decompress, mapKey, sleep } from "./common";
@ -24,8 +24,6 @@ export default class Connection {
_peerInfo: message.PeerInfo | undefined; _peerInfo: message.PeerInfo | undefined;
_firstFrame: Boolean | undefined; _firstFrame: Boolean | undefined;
_videoDecoder: any; _videoDecoder: any;
_audioDecoder: any;
_audioPlayer: any;
_password: Uint8Array | undefined; _password: Uint8Array | undefined;
_options: any; _options: any;
_videoTestSpeed: number[]; _videoTestSpeed: number[];
@ -242,13 +240,7 @@ export default class Connection {
} else if (msg?.misc) { } else if (msg?.misc) {
this.handleMisc(msg?.misc); this.handleMisc(msg?.misc);
} else if (msg?.audio_frame) { } else if (msg?.audio_frame) {
const dec = this._audioDecoder; globals.playAudio(msg?.audio_frame.data);
dec.processAudio(msg?.audio_frame.data.slice(0).buffer, (res: any) => {
// ogv.js bug here, audioBuffer always empty
// and ogv.js vpx version is very old (2015), to-do: discard ogv
// let samples = dec.audioBuffer;
// console.log(res, dec);
});
} }
} }
} }
@ -428,9 +420,7 @@ export default class Connection {
handleMisc(misc: message.Misc) { handleMisc(misc: message.Misc) {
if (misc.audio_format) { if (misc.audio_format) {
this._audioDecoder?.destroy(); globals.initAudio(misc.audio_format.channels, misc.audio_format.sample_rate);
// this._audioPlayer = globals.newAudioPlayer(misc.audio_format.channels, misc.audio_format.sample_rate);
this.loadAudioDecoder(misc.audio_format.channels, misc.audio_format.sample_rate);
} else if (misc.chat_message) { } else if (misc.chat_message) {
globals.pushEvent("chat", misc.chat_message.text); globals.pushEvent("chat", misc.chat_message.text);
} else if (misc.permission_info) { } else if (misc.permission_info) {
@ -656,11 +646,6 @@ export default class Connection {
} }
loadAudioDecoder(channels: number, sample_rate: number) { loadAudioDecoder(channels: number, sample_rate: number) {
this._audioDecoder?.close();
loadOpus((decoder: any) => {
this._audioDecoder = decoder;
console.log("opus loaded");
}, channels, sample_rate);
} }
} }

View File

@ -1,7 +1,7 @@
import Connection from "./connection"; import Connection from "./connection";
import _sodium from "libsodium-wrappers"; import _sodium from "libsodium-wrappers";
import { CursorData } from "./message"; import { CursorData } from "./message";
import { loadOpus, loadVp9 } from "./codec"; import { loadVp9 } from "./codec";
import { checkIfRetry, version } from "./gen_js_from_hbb"; import { checkIfRetry, version } from "./gen_js_from_hbb";
import { initZstd, translate } from "./common"; import { initZstd, translate } from "./common";
import PCMPlayer from "pcm-player"; import PCMPlayer from "pcm-player";
@ -285,12 +285,27 @@ function _getByName(name, arg) {
return ''; return '';
} }
let opusWorker;
let pcmPlayer;
export function initAudio(channels, sampleRate) {
pcmPlayer = newAudioPlayer(channels, sampleRate);
opusWorker.postMessage({ channels, sampleRate });
}
export function playAudio(packet) {
opusWorker.postMessage(packet);
}
window.init = async () => { window.init = async () => {
yuvWorker = new Worker("./yuv.js"); yuvWorker = new Worker("./yuv.js");
opusWorker = new Worker("./libopus.js");
yuvWorker.onmessage = (e) => { yuvWorker.onmessage = (e) => {
currentFrame = e.data; currentFrame = e.data;
} }
loadOpus(() => { }); opusWorker.onmessage = (e) => {
pcmPlayer.feed(e.data);
}
loadVp9(() => { }); loadVp9(() => { });
await initZstd(); await initZstd();
} }
@ -303,11 +318,10 @@ export function getPeers() {
} }
} }
export function newAudioPlayer(channels, sampleRate) { function newAudioPlayer(channels, sampleRate) {
return new PCMPlayer({ return new PCMPlayer({
encoding: '16bitInt',
channels, channels,
sampleRate, sampleRate,
flushingTime: 2000 flushingTime: 2000
}); });
} }