How to send/receive video using VideoCapture/VideoWriter with GStreamer?

I want to send/receive video using VideoCapture/VideoWriter with gstreamer.
I tried the following code. It can be sent and received test image. But, received image is not correct. It is broken. It seems image is dark and misaligned.
How to send and receive images normally?

code

  • send app:
#include <opencv2/opencv.hpp>

int main()
{
    // open video capture test pattern 320x240
    cv::VideoCapture capture = cv::VideoCapture("videotestsrc ! videoconvert ! appsink");
    if(!capture.isOpened()) return -1;

    // open video writer send jpeg localhost
    cv::VideoWriter writer= cv::VideoWriter("appsrc ! videoconvert ! video/x-raw,format=YUY2 ! jpegenc ! rtpjpegpay ! udpsink host=127.0.0.1 port=5000", 0, 30, cv::Size( 320, 240 ));
    if(!writer.isOpened()) return -1;

    while(true)
    {
        // capture image
        cv::Mat image;
        capture.read(image);
        if(image.empty()) continue;

        // send image
        writer.write(image);

        // show image
        cv::imshow("send", image);
        int k = cv::waitKey(1);
        if(k == 'q') break;
    }

    return 0;
}
  • receive app:
#include <opencv2/opencv.hpp>

int main()
{
    cv::VideoCapture capture = cv::VideoCapture("udpsrc port=5000 ! application/x-rtp, media=video, encoding-name=JPEG, framerate=30/1, payload=26, clock-rate=90000 ! rtpjpegdepay ! jpegdec ! videoconvert ! appsink");
    if(!capture.isOpened()) return -1;

    while(true)
    {
        // capture image
        cv::Mat image;
        capture.read(image);
        if(image.empty()) continue;

        // show image
        cv::imshow("receive", image);
        int k = cv::waitKey(1);
        if(k == 'q') break;
    }

    return 0;
}

my environment

  • windows 10
  • visual studio 2019
  • opencv 4.5.1
  • send
    send
  • receive
    receive