sourcey / libsourcey

C++14 evented IO libraries for high performance networking and media based applications
https://sourcey.com/libsourcey
GNU Lesser General Public License v2.1
1.31k stars 344 forks source link

Trying to convert Opencv Mat to webrtc::I420Buffer #282

Closed ardiyu07 closed 4 years ago

ardiyu07 commented 4 years ago
void VideoPacketSource::onVideoCaptured(av::MatrixPacket& packet)
.
.
.
    // Convert the packet from BGR to I420 for WebRTC
    cv::Mat yuv(packet.height*3/2, packet.width, CV_8UC4);
    cv::cvtColor(*packet.mat, yuv, CV_BGRA2YUV_I420);

    rtc::scoped_refptr<webrtc::I420Buffer> buffer = webrtc::I420Buffer::Copy(
            packet.width, packet.height,
            (uint8_t*)yuv.data, yuv.cols,
            (uint8_t*)yuv.data + yuv.rows*yuv.cols, yuv.cols/2,
            (uint8_t*)yuv.data + yuv.rows*yuv.cols + ((yuv.rows/2)*(yuv.cols/2)), yuv.cols/2);

    OnFrame(webrtc::VideoFrame(
    buffer, _rotation,
    translated_camera_time_us), // timestamp
    packet.width, packet.height);
.
.

Struggling with this code because it decodes with weird colors on the client side. Image provided below. https://i.imgur.com/f72fZeE.jpg

Do you have any idea how to fix this?

ardiyu07 commented 4 years ago

Nvm stupid mistake. Inserted wrong number of rows.