首页
社区
课程
招聘
[求助]为什么用toByteArray()这个方法在某些机型上卡顿?
2022-7-26 17:41 4681

[求助]为什么用toByteArray()这个方法在某些机型上卡顿?

2022-7-26 17:41
4681

场景:将手机录屏编码后得到的 byte[]序列化成 protobuf 类
问题:一秒编码 30 帧,随着时间推移,手机会越来越卡。
经过排查,发现是这行代码造成卡顿,
val toByteArray = screenMes.build().toByteArray()

 

完整的代码如下:

 

val byteBuffer: ByteBuffer = ByteBuffer.allocateDirect(1024 1024 3)
private lateinit var screenMes:
UDXMessage.ScreencapMes.Builder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
* 编码回调
 * bytes 编码后的数据
 * flags 是否 I 帧
 */
override fun onScreenInfo(bytes: ByteArray?, flags: Int) {
    isRecording = true
    if (bytes != null) {
        screenMes.data = ByteString.copyFrom(bytes)
        screenMes.sort = mFlowSort
 
        if (flags == MediaCodec.BUFFER_FLAG_KEY_FRAME) {
            screenMes.frameType = 0
        } else {
            screenMes.frameType = 1
        }
 
        val toByteArray = screenMes.build().toByteArray()
 
 
        byteBuffer.clear()
        byteBuffer.putInt(toByteArray.size + 4)
        byteBuffer.putInt(UdxType.Type_Send_Frame)
        byteBuffer.put(toByteArray)
        byteBuffer.flip()
        if (mUdxState == 1) {
            if (mFlowSort > 999999999999999999) {
                mFlowSort = 1
            }
            mFlowSort++
            SendUtils.getInstance().handlePacket(true, byteBuffer, screenMes.frameType)
        } else {
            mFlowSort = 1
        }
 
    }
}

[培训]二进制漏洞攻防(第3期);满10人开班;模糊测试与工具使用二次开发;网络协议漏洞挖掘;Linux内核漏洞挖掘与利用;AOSP漏洞挖掘与利用;代码审计。

收藏
点赞0
打赏
分享
最新回复 (1)
雪    币: 153
活跃值: (135)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
kylinokok 2022-7-26 17:43
2
0
为什么呢?只有在 三星S22上卡顿,其他品牌手机不会
游客
登录 | 注册 方可回帖
返回