Fix taking screenshot not working when limiting FPS

This commit is contained in:
redphx 2024-10-19 18:04:05 +07:00
parent 602c31dc7f
commit 8664c1a60f
4 changed files with 32 additions and 26 deletions

View File

@ -5036,12 +5036,14 @@ class WebGL2Player {
let gl = this.gl, program = this.program; let gl = this.gl, program = this.program;
gl.uniform2f(gl.getUniformLocation(program, "iResolution"), this.$canvas.width, this.$canvas.height), gl.uniform1i(gl.getUniformLocation(program, "filterId"), this.options.filterId), gl.uniform1f(gl.getUniformLocation(program, "sharpenFactor"), this.options.sharpenFactor), gl.uniform1f(gl.getUniformLocation(program, "brightness"), this.options.brightness), gl.uniform1f(gl.getUniformLocation(program, "contrast"), this.options.contrast), gl.uniform1f(gl.getUniformLocation(program, "saturation"), this.options.saturation); gl.uniform2f(gl.getUniformLocation(program, "iResolution"), this.$canvas.width, this.$canvas.height), gl.uniform1i(gl.getUniformLocation(program, "filterId"), this.options.filterId), gl.uniform1f(gl.getUniformLocation(program, "sharpenFactor"), this.options.sharpenFactor), gl.uniform1f(gl.getUniformLocation(program, "brightness"), this.options.brightness), gl.uniform1f(gl.getUniformLocation(program, "contrast"), this.options.contrast), gl.uniform1f(gl.getUniformLocation(program, "saturation"), this.options.saturation);
} }
drawFrame() { drawFrame(force = !1) {
if (this.targetFps === 0) return; if (!force) {
if (this.targetFps < 60) { if (this.targetFps === 0) return;
let currentTime = performance.now(); if (this.targetFps < 60) {
if (currentTime - this.lastFrameTime < this.frameInterval) return; let currentTime = performance.now();
this.lastFrameTime = currentTime; if (currentTime - this.lastFrameTime < this.frameInterval) return;
this.lastFrameTime = currentTime;
}
} }
let gl = this.gl; let gl = this.gl;
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, this.$video), gl.drawArrays(gl.TRIANGLES, 0, 6); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, this.$video), gl.drawArrays(gl.TRIANGLES, 0, 6);

View File

@ -1814,7 +1814,7 @@ class Screenshot {
if (!$player || !$player.isConnected) return; if (!$player || !$player.isConnected) return;
$player.parentElement.addEventListener("animationend", this.#onAnimationEnd, { once: !0 }), $player.parentElement.classList.add("bx-taking-screenshot"); $player.parentElement.addEventListener("animationend", this.#onAnimationEnd, { once: !0 }), $player.parentElement.classList.add("bx-taking-screenshot");
let canvasContext = Screenshot.#canvasContext; let canvasContext = Screenshot.#canvasContext;
if ($player instanceof HTMLCanvasElement) streamPlayer.getWebGL2Player().drawFrame(); if ($player instanceof HTMLCanvasElement) streamPlayer.getWebGL2Player().drawFrame(!0);
if (canvasContext.drawImage($player, 0, 0, $canvas.width, $canvas.height), AppInterface) { if (canvasContext.drawImage($player, 0, 0, $canvas.width, $canvas.height), AppInterface) {
let data = $canvas.toDataURL("image/png").split(";base64,")[1]; let data = $canvas.toDataURL("image/png").split(";base64,")[1];
AppInterface.saveScreenshot(currentStream.titleSlug, data), canvasContext.clearRect(0, 0, $canvas.width, $canvas.height), callback && callback(); AppInterface.saveScreenshot(currentStream.titleSlug, data), canvasContext.clearRect(0, 0, $canvas.width, $canvas.height), callback && callback();
@ -6915,12 +6915,14 @@ class WebGL2Player {
let gl = this.gl, program = this.program; let gl = this.gl, program = this.program;
gl.uniform2f(gl.getUniformLocation(program, "iResolution"), this.$canvas.width, this.$canvas.height), gl.uniform1i(gl.getUniformLocation(program, "filterId"), this.options.filterId), gl.uniform1f(gl.getUniformLocation(program, "sharpenFactor"), this.options.sharpenFactor), gl.uniform1f(gl.getUniformLocation(program, "brightness"), this.options.brightness), gl.uniform1f(gl.getUniformLocation(program, "contrast"), this.options.contrast), gl.uniform1f(gl.getUniformLocation(program, "saturation"), this.options.saturation); gl.uniform2f(gl.getUniformLocation(program, "iResolution"), this.$canvas.width, this.$canvas.height), gl.uniform1i(gl.getUniformLocation(program, "filterId"), this.options.filterId), gl.uniform1f(gl.getUniformLocation(program, "sharpenFactor"), this.options.sharpenFactor), gl.uniform1f(gl.getUniformLocation(program, "brightness"), this.options.brightness), gl.uniform1f(gl.getUniformLocation(program, "contrast"), this.options.contrast), gl.uniform1f(gl.getUniformLocation(program, "saturation"), this.options.saturation);
} }
drawFrame() { drawFrame(force = !1) {
if (this.targetFps === 0) return; if (!force) {
if (this.targetFps < 60) { if (this.targetFps === 0) return;
let currentTime = performance.now(); if (this.targetFps < 60) {
if (currentTime - this.lastFrameTime < this.frameInterval) return; let currentTime = performance.now();
this.lastFrameTime = currentTime; if (currentTime - this.lastFrameTime < this.frameInterval) return;
this.lastFrameTime = currentTime;
}
} }
let gl = this.gl; let gl = this.gl;
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, this.$video), gl.drawArrays(gl.TRIANGLES, 0, 6); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, this.$video), gl.drawArrays(gl.TRIANGLES, 0, 6);

View File

@ -94,20 +94,22 @@ export class WebGL2Player {
gl.uniform1f(gl.getUniformLocation(program, 'saturation'), this.options.saturation); gl.uniform1f(gl.getUniformLocation(program, 'saturation'), this.options.saturation);
} }
drawFrame() { drawFrame(force=false) {
// Don't draw when FPS is 0 if (!force) {
if (this.targetFps === 0) { // Don't draw when FPS is 0
return; if (this.targetFps === 0) {
}
// Limit FPS
if (this.targetFps < 60) {
const currentTime = performance.now();
const timeSinceLastFrame = currentTime - this.lastFrameTime;
if (timeSinceLastFrame < this.frameInterval) {
return; return;
} }
this.lastFrameTime = currentTime;
// Limit FPS
if (this.targetFps < 60) {
const currentTime = performance.now();
const timeSinceLastFrame = currentTime - this.lastFrameTime;
if (timeSinceLastFrame < this.frameInterval) {
return;
}
this.lastFrameTime = currentTime;
}
} }
const gl = this.gl!; const gl = this.gl!;

View File

@ -64,7 +64,7 @@ export class Screenshot {
const canvasContext = Screenshot.#canvasContext; const canvasContext = Screenshot.#canvasContext;
if ($player instanceof HTMLCanvasElement) { if ($player instanceof HTMLCanvasElement) {
streamPlayer.getWebGL2Player().drawFrame(); streamPlayer.getWebGL2Player().drawFrame(true);
} }
canvasContext.drawImage($player, 0, 0, $canvas.width, $canvas.height); canvasContext.drawImage($player, 0, 0, $canvas.width, $canvas.height);