Skip to content

Conversation

SnirDecartAI
Copy link
Contributor

Hey, now that you added CustomVIdeoSource that can accept VideoFrames that need I420Buffers,
it might be useful to be able to use RGB frames.

VideoBufferConverter has convertFromI420 to be able to take frames from sinks and convert them to RGB,
so I added convertToI420 to convert a buffer (byte[] or ByteBuffer) to I420Buffer.
Like the existing VideoDesktopSource, this uses libyuv::ConvertToI420.

Example usage based on custom_video_source.md:

void pushRGBFrame(ByteBuffer rgbBuffer, int width, int height) throws Exception {
    NativeI420Buffer i420Buffer = NativeI420Buffer.allocate(width, height);
    VideoBufferConverter.convertToI420(rgbBuffer, i420Buffer, FourCC.RGBA);
    VideoFrame frame = new VideoFrame(buffer, System.nanoTime());
    videoSource.pushFrame(frame);
    frame.dispose();
}

I then added tests for VideoBufferConverter:

  • testing converting from/to i420 works, with either a byte[] or a direct ByteBuffer or a non-direct ByteBuffer (6 tests)
  • testing that if the buffer is too small we get an exception (6 tests)
  • testing that read-only bytebuffers work when using them as a source, but not as a destination, whether direct or non-direct (4 tests)

To test the conversion itself did something I generated I420/RGBA data that contained sequential numbers just so it wouldn't be zero or constant (see initializeI420Buffer/initializeRGBAArray/initializeRGBABuffer), then called VideoBufferConverter to perform the conversion, and then check that each channel of the destination buffer has at least one non-zero byte (see verifyI420Buffer/verifyRGBAArray/verifyRGBABuffer).

Thanks!

@devopvoid
Copy link
Owner

Thank you!

@devopvoid devopvoid merged commit 6e60403 into devopvoid:main Aug 6, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants