Skip to content

Commit

Permalink
Fix #963 PC 暂停时按键盘调进度不会渲染视频帧
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Sep 20, 2024
1 parent 6fe7d9c commit 0bd47d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.videoplayer.ui

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asComposeImageBitmap
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.flow.MutableStateFlow
import org.jetbrains.skia.Bitmap
import org.jetbrains.skia.ColorAlphaType
Expand All @@ -29,6 +39,11 @@ class SkiaBitmapVideoSurface : VideoSurface(VideoSurfaceAdapters.getVideoSurface
private val composeBitmap = mutableStateOf<ImageBitmap?>(null)

val enableRendering = MutableStateFlow(false)

/**
* 即使 [enableRendering]
*/
val allowedDrawFrames = atomic(0)
val bitmap by composeBitmap

fun clearBitmap() {
Expand Down Expand Up @@ -66,7 +81,9 @@ class SkiaBitmapVideoSurface : VideoSurface(VideoSurfaceAdapters.getVideoSurface
nativeBuffers: Array<ByteBuffer>,
bufferFormat: BufferFormat,
) {
if (!enableRendering.value) return
val allowedDrawFramesValue = allowedDrawFrames.value
if (!enableRendering.value && allowedDrawFramesValue <= 0) return
if (allowedDrawFrames.decrementAndGet() < 0) return
SwingUtilities.invokeLater {
nativeBuffers[0].rewind()
nativeBuffers[0].get(frameBytes)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.videoplayer.ui

import androidx.compose.foundation.Canvas
Expand Down Expand Up @@ -478,6 +487,7 @@ class VlcjVideoPlayerState(parentCoroutineContext: CoroutineContext) : PlayerSta
override fun seekTo(positionMillis: Long) {
currentPositionMillis.value = positionMillis
player.controls().setTime(positionMillis)
surface.allowedDrawFrames.value = 2 // 多渲染一帧, 防止 race 问题π
}

}
Expand Down

0 comments on commit 0bd47d3

Please sign in to comment.