xuning

尝试接入图像处理

/*
* Copyright 2024 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.livekit.android.room.track.video
import livekit.org.webrtc.VideoFrame
import java.nio.ByteBuffer
// Native OpenCV implementation via JNI
class OpenCVVideoProcessor : ChainVideoProcessor() {
companion object {
init {
System.loadLibrary("rvmncnn")
}
}
private external fun processI420(y: ByteArray, u: ByteArray, v: ByteArray, width: Int, height: Int, rotation: Int, timestamp: Long): Array<ByteArray>
override fun onFrameCaptured(frame: VideoFrame) {
val buffer = frame.buffer
if (buffer !is VideoFrame.I420Buffer) {
continueChain(frame)
return
}
val yArray = ByteArray(buffer.strideY * buffer.height)
buffer.dataY.get(yArray)
val uArray = ByteArray(buffer.strideU * (buffer.height + 1) / 2)
buffer.dataU.get(uArray)
val vArray = ByteArray(buffer.strideV * (buffer.height + 1) / 2)
buffer.dataV.get(vArray)
val result = processI420(yArray, uArray, vArray, buffer.width, buffer.height, frame.rotation, frame.timestampNs)
val newY = ByteBuffer.wrap(result[0])
val newU = ByteBuffer.wrap(result[1])
val newV = ByteBuffer.wrap(result[2])
val newBuffer = livekit.org.webrtc.JavaI420Buffer.wrap(buffer.width, buffer.height, newY, buffer.width, newU, buffer.width / 2, newV, buffer.width / 2, null)
val newFrame = VideoFrame(newBuffer, frame.rotation, frame.timestampNs)
continueChain(newFrame)
}
}
\ No newline at end of file
... ...