1
votes

I use OpenCV with ffmpeg (api-preference CAP_FFMPEG) to receive a RTP-Stream and show the video.

When I send a short video, the video is played as expected. But when I try to send a second video, the second one is not shown at all.

My code:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdlib.h>

using namespace std;
using namespace cv;
int main(int argc, char** argv) {

    cout << "--- OpenCV Verson: " << CV_VERSION << " ---" << endl;

    char env[] = "OPENCV_FFMPEG_CAPTURE_OPTIONS=protocol_whitelist;file,rtp,udp";
    int rc = putenv(env);
    if (rc != 0){
        cout << "Could not set environment variables\n" << endl;
    }

    VideoCapture cap;
    char input[] = "path/to/saved_sdp_file.sdp";
    cap.open(input, CAP_FFMPEG);
    if(!cap.isOpened()) {
        cout << "Could not open Stream" << endl;
        return 1;
    }

    string windowName = "Test";
    namedWindow(windowName, cv::WindowFlags::WINDOW_NORMAL);
    Mat frame;

    while(1){

        cap >> frame;
        if (frame.empty()){
            cout << "Application closed due to empty frame" << endl;
            return 1;
        }

        imshow(windowName, frame);
        int c = waitKey(1);
        if (c == 27){
            break;
        }
    }

    cap.release();
    destroyAllWindows();
    return 0;
}

My .sdp file:

SDP:
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
m=video 6005 RTP/AVP 96
b=AS:132
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z2QADazZQUH7ARAAAAMAEAAAAwPA8UKZYA==,aOvjyyLA; profile-level-id=64000D

The command to start the stream:

ffmpeg -re -thread_queue_size 4 -i video_file.mp4 -strict -2 -vcodec copy -an -f rtp rtp://192.168.20.98:6005

I tested streaming with ffplay. I use the following command:

ffplay -protocol_whitelist "file,rtp,udp" -i saved_sdp_test.sdp -reorder_queue_size 0

Everything seems to work and I can send and receive multiple videos. But I do not know how I could pass the -reorder_queue_size option in OpenCV.

I know this would also be possible with gstreamer instead of ffmpeg. But there is a memory leak issue with the gstreamer-api in OpenCV (https://github.com/opencv/opencv/issues/5715) which I could not resolve.

Question

How can I use OpenCV with ffmpeg to receive multiple videos (consecutively) via rtp?

1

1 Answers

0
votes

I can add the -reorder_queue_size option the same way I added the protocol_whitelist option. I just have to use | between the options. Than everything works as expected.

char env[] = "OPENCV_FFMPEG_CAPTURE_OPTIONS=protocol_whitelist;file,rtp,udp | reorder_queue_size;0";
putenv(env);