You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter/Android:Twilio可编程视频多次调用后致应用及系统性能下降

解决Twilio Flutter可编程视频断开房间后EGLRenderer残留导致的性能问题

问题重现

使用Twilio Flutter可编程视频包集成视频功能后,断开并销毁房间时EGLRenderer未正常停止,引发应用及系统性能下降:

  • Logcat会无限打印EglRenderer相关日志(间隔约1秒),且打印频率随断开房间的次数增加而倍增:
    EglRenderer: Duration: 4006 ms. Frames received: 0. Dropped: 0. Rendered: 0. Render fps: .0. Average render time: NA. Average swapBuffer time: NA.
    
  • 尝试修改Android原生ParticipantView的dispose方法(移除视频轨道监听并释放视图)后,日志变为BufferQueueProducer报错,但性能问题依旧:
    override fun dispose() {
       debug("dispose => Disposing ParticipantView")
       videoTrack.removeSink(videoView)
       videoView.release()
    }
    
    报错日志:
    E/BufferQueueProducer(16848): [SurfaceTexture-0-16848-1](id:41d000000005,api:1,p:1097,c:16848) dequeueBuffer: BufferQueue has been abandoned
    

可行修复方案

1. 完整释放渲染与轨道资源

在ParticipantView的dispose方法中,补充停止渲染线程、释放SurfaceTexture并清空引用,确保资源被彻底回收:

override fun dispose() {
    debug("dispose => Disposing ParticipantView")
    // 移除视频轨道的sink
    videoTrack?.removeSink(videoView)
    // 停止渲染流程并释放视图关联资源
    videoView?.let {
        it.stopRendering()
        it.surfaceTexture?.release()
        it.release()
    }
    // 清空引用,帮助GC回收
    videoTrack = null
    videoView = null
}

2. Flutter侧同步清理资源

在断开房间时,主动触发所有ParticipantView控制器的销毁逻辑,确保原生侧dispose方法被正确调用:

// Flutter侧断开房间并清理参与者视图
await room.disconnect();
// 遍历所有参与者视图控制器,执行销毁
for (var controller in participantControllers) {
  await controller.dispose();
}
participantControllers.clear();

3. 参与者离开时释放轨道

在原生侧监听参与者离开事件,及时释放其关联的视频轨道资源,避免轨道残留引发无效渲染:

fun onParticipantLeft(participant: RemoteParticipant) {
    participant.videoTracks.forEach { trackPublication ->
        trackPublication.track?.removeAllSinks()
        trackPublication.track?.release()
    }
}

问题根源说明

性能损耗的核心原因是EGLRenderer线程未被正确终止,即便释放了videoView,渲染线程仍会持续尝试向已废弃的BufferQueue提交帧,导致日志循环打印和系统资源占用。只有覆盖视频轨道、渲染视图、SurfaceTexture三个层级的资源释放,并清空所有引用,才能彻底终止残留线程。

内容的提问来源于stack exchange,提问作者Riajul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 21:35:17