c# - 来自图像的 Android org.webrtc.VideoRenderer.I420Frame 数组

标签 c# android webrtc android-image yuv

我一直希望一些代码会出现在互联网上,但一无所获;) 我正在运行这个 github例子。 WebRTC 传入的 I420Frame 对象似乎有 3 个 yuvPlanes 数组

典型的 Android 相机应用获取 PreviewCallback.onPreviewFrame byte[] 作为单个字节数组。 我的工作是定期将图像流式传输为 I420。 有人可以帮助我如何从单字节 [] 数组(如 JPEG/PNG 文件)生成 I420Frames yuvPlanes 吗?

这很关键。所有答案表示赞赏。

最佳答案

PreviewCallback.onPreviewFrame() 永远不会返回 JPEG 或 PNG 流。你应该检查你的相机getSupportedPreviewFormats()列出 (请注意,前置和后置摄像头可能有所不同)NV21 肯定在此列表中。如果幸运的话,您可以选择 YV12,因为 API 级别 12 (请注意,某些设备,例如 Amazon Fire HD (2012),对此撒谎,实际上无法提供 YV12 流)

构建 I420Frame 很容易来自YV12字节数组:

private VideoRenderer.I420Frame mFrame;
void onPreviewFrame(byte[] yv12_data, Camera camera) {
    if (mFrame == null) {
        Camera.Parameters params = camera.getParameters(); // this is an expensive call, don't repeat it on every frame!
        assert(params.getPreviewFormat() == ImageFormat.YV12);
        int width = params.getPreviewSize().width;
        int stride_y = 16 + ((width-1)/16)*16;
        int stride_uv = 16 + ((stride_y/2-1)/16)*16;
        int height = params.getPreviewSize().height; 
        mFrame = new VideoRenderer.I420Frame(width, height, 0, new int[]{stride_y, stride_uv, stride_uv}, new ByteBuffer[3], 0);
    }

    mFrame.yuvPlanes[0] = ByteBuffer.wrap(yv12_data, 0, mFrame.yuvStrides[0]*mFrame.height) // Y
    mFrame.yuvPlanes[1] = ByteBuffer.wrap(yv12_data, mFrame.yuvStrides[0]*mFrame.height+mFrame.yuvStrides[2]*mFrame.height/2, mFrame.yuvStrides[1]*mFrame.height/2) // U
    mFrame.yuvPlanes[2] = ByteBuffer.wrap(yv12_data, mFrame.yuvStrides[0]*mFrame.height, mFrame.yuvStrides[2]*mFrame.height/4) // V

    ... do something with the frame
}

对于 NV21 ,您必须分配 U 和 V 平面:

private VideoRenderer.I420Frame mFrame;
void onPreviewFrame(byte[] nv21_data, Camera camera) {
    if (mFrame == null) {
        Camera.Parameters params = camera.getParameters(); // this is an expensive call, don't repeat it on every frame!
        assert(params.getPreviewFormat() == ImageFormat.NV21);
        int width = params.getPreviewSize().width;
        int height = params.getPreviewSize().height; 
        mFrame = new VideoRenderer.I420Frame(width, height, 0, new int[]{width, width/2, width/2}, new ByteBuffer[3], 0);
        mFrame.yuvPlanes[1] = ByteBuffer.wrap(new byte[width*height/4]);
        mFrame.yuvPlanes[2] = ByteBuffer.wrap(new byte[width*height/4]);
    }

    mFrame.yuvPlanes[0] = ByteBuffer.wrap(nv21_data, 0, mFrame.width*mFrame.height) // Y
    for (int top=0, from=mFrame.width*mFrame.height; from < mFrame.width*mFrame.height*3/2; to++, from+=2) {
        mframe.yuvPlanes[1][to] = nv21_data[from+1]; // U
        mframe.yuvPlanes[2][to] = nv21_data[from]; // V
    }

    ... do something with the frame
}

关于c# - 来自图像的 Android org.webrtc.VideoRenderer.I420Frame 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841257/

相关文章:

c# - 在 WPF 应用程序中使用远程数据库的建议方法是什么

java - 在 Mac 10.9 Mavericks 上从 Android 源构建 aapt 出现段错误,或者在 10.8 Mountain Lion 上给出 "Illegal Instruction 4"

Android,构建成功,但 apk 构建(后续运行)失败

java - 从我的应用程序类打开 Google Play 商店

javascript - 如何强制录制WebRTC音频的时间限制

html - webrtc - 视频出现 Blob ,但它仍然是黑色的

c# - 在图片框上添加额外的半透明层

c# - 使用Mono获取Unix/Linux系统架构

c# - 调整控件的宽度和高度以换行其内容

webrtc - 关于通过 REST api 进行 TURN 服务器身份验证的说明