feat(decode): 用 WebRTC stats 替代原生探针计算解码耗时

This commit is contained in:
TongTongStudio
2026-07-25 11:18:34 +08:00
parent 4a33f3db0d
commit e5dafb65ed
3 changed files with 101 additions and 46 deletions

View File

@@ -44,3 +44,10 @@ kotlin {
flutter {
source = "../.."
}
dependencies {
// flutter_webrtc 依赖了 io.github.webrtc-sdkorg.webrtc.*),但它声明为
// implementationapp 模块默认看不到 org.webrtc.VideoTrack/VideoSink 等类型。
// 这里显式引入同版本,使解码耗时探针能直接引用 libwebrtc 的 VideoTrack/VideoSink。
implementation("io.github.webrtc-sdk:android:144.7559.09")
}

View File

@@ -16,7 +16,6 @@ import io.flutter.view.TextureRegistry
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer
import java.nio.ByteOrder
import org.webrtc.VideoFrame
import org.webrtc.VideoSink
import org.webrtc.VideoTrack
import com.cloudwebrtc.webrtc.FlutterWebRTCPlugin
@@ -90,7 +89,7 @@ private class DecodeProbeSink {
val nowMs = System.currentTimeMillis()
framesSinceFps++
if (lastFpsStampMs != 0L && nowMs - lastFpsStampMs >= 1000) {
fpsNow = framesSinceFps * 1000 / (nowMs - lastFpsStampMs)
fpsNow = framesSinceFps * 1000 / (nowMs - lastFpsStampMs).toInt()
framesSinceFps = 0
lastFpsStampMs = nowMs
} else if (lastFpsStampMs == 0L) {
@@ -254,15 +253,22 @@ class SelfCodecDecoderPlugin :
try {
val plugin = FlutterWebRTCPlugin.sharedSingleton
val track = plugin?.getRemoteTrack(trackId)
if (track !is VideoTrack) {
if (track == null) {
result.error("PROBE", "remote video track not found: $trackId", null)
return
}
// getRemoteTrack 返回 org.webrtc.MediaStreamTrack超类addSink 只在
// 子类 org.webrtc.VideoTrack 上,用安全强转拿到 VideoTrack。
val videoTrack = track as? VideoTrack
if (videoTrack == null) {
result.error("PROBE", "track is not a video track: $trackId", null)
return
}
detachWebRtcDecodeProbeInternal()
val stats = DecodeProbeSink()
val sink = VideoSink { stats.onFrame() }
track.addSink(sink)
probeHolder = ProbeHolder(track, sink, stats)
videoTrack.addSink(sink)
probeHolder = ProbeHolder(videoTrack, sink, stats)
result.success(null)
} catch (e: Exception) {
Log.e(TAG, "attachWebRtcDecodeProbe failed", e)