正在显示
1 个修改的文件
包含
57 行增加
和
0 行删除
livekit-android-sdk/src/main/java/io/livekit/android/room/track/video/OpenCVVideoProcessor.kt
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2024 LiveKit, Inc. | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package io.livekit.android.room.track.video | ||
| 18 | +import livekit.org.webrtc.VideoFrame | ||
| 19 | +import java.nio.ByteBuffer | ||
| 20 | +// Native OpenCV implementation via JNI | ||
| 21 | + | ||
| 22 | +class OpenCVVideoProcessor : ChainVideoProcessor() { | ||
| 23 | + | ||
| 24 | + companion object { | ||
| 25 | + init { | ||
| 26 | + System.loadLibrary("rvmncnn") | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + private external fun processI420(y: ByteArray, u: ByteArray, v: ByteArray, width: Int, height: Int, rotation: Int, timestamp: Long): Array<ByteArray> | ||
| 31 | + | ||
| 32 | + override fun onFrameCaptured(frame: VideoFrame) { | ||
| 33 | + val buffer = frame.buffer | ||
| 34 | + if (buffer !is VideoFrame.I420Buffer) { | ||
| 35 | + continueChain(frame) | ||
| 36 | + return | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + val yArray = ByteArray(buffer.strideY * buffer.height) | ||
| 40 | + buffer.dataY.get(yArray) | ||
| 41 | + val uArray = ByteArray(buffer.strideU * (buffer.height + 1) / 2) | ||
| 42 | + buffer.dataU.get(uArray) | ||
| 43 | + val vArray = ByteArray(buffer.strideV * (buffer.height + 1) / 2) | ||
| 44 | + buffer.dataV.get(vArray) | ||
| 45 | + | ||
| 46 | + val result = processI420(yArray, uArray, vArray, buffer.width, buffer.height, frame.rotation, frame.timestampNs) | ||
| 47 | + | ||
| 48 | + val newY = ByteBuffer.wrap(result[0]) | ||
| 49 | + val newU = ByteBuffer.wrap(result[1]) | ||
| 50 | + val newV = ByteBuffer.wrap(result[2]) | ||
| 51 | + | ||
| 52 | + val newBuffer = livekit.org.webrtc.JavaI420Buffer.wrap(buffer.width, buffer.height, newY, buffer.width, newU, buffer.width / 2, newV, buffer.width / 2, null) | ||
| 53 | + | ||
| 54 | + val newFrame = VideoFrame(newBuffer, frame.rotation, frame.timestampNs) | ||
| 55 | + continueChain(newFrame) | ||
| 56 | + } | ||
| 57 | +} |
-
请 注册 或 登录 后发表评论