How to get I420 frames using LibVLCSharp

75 views Asked by At

I am trying to recieve media in I420 frames for forwarding over a WebRTC connection. Right now, the following code works for getting frames in the RV32 format.

IntPtr frameData = IntPtr.Zero;
uint width = 640;
uint height = 360;
uint pitch = 640 * 4;

LibVLC vlc = new LibVLC();
MediaPlayer player = new MediaPlayer(vlc);

player.SetVideoFormat("RV32", width, height, pitch); //  see https://wiki.videolan.org/Chroma/ for formats
player.SetVideoCallbacks(VideoLockCallback, null, VideoDisplayCallback);
Media media = new Media(vlc, new Uri("http://your_video_url"));
await media.Parse(MediaParseOptions.ParseNetwork);

private IntPtr VideoLockCallback(IntPtr opaque, IntPtr planes)
{
    Marshal.WriteIntPtr(planes, frameData);
    return IntPtr.Zero;
}

private void VideoDisplayCallback(IntPtr opaque, IntPtr picture)
{
    int imageDataLength = (int)(width * height * 4);
    byte[] imageData = new byte[imageDataLength];

    unsafe
    {
        byte* pMem = (byte*)frameData.ToPointer();
        Marshal.Copy((IntPtr)pMem, imageData, 0, imageDataLength);
    }

    // make a Bitmap from imageData or whatever
}

However, If I switch "RV32" to "I420", suddenly the application crashes with a memory access violation after calling VideoLockCallback. What am I missing in order to get I420 frame data, or, is there a better way to get VP8/H264 data for a WebRTC connection?

0

There are 0 answers