Search code examples
c++opencvstreaminggstreamerrtsp

Video Streaming to Android from PC after processing from Opencv using RTSP


I am trying to stream a combined video stream taken from two webcams to android app after processing in opencv(combining two frames).

Here I am trying to use RTSP to send video stream from opencv to Android(using a gstreamer pipeline).

But i am stuck in how to send .sdp file configurations to the client(file name is live.sdp),and here's the code I used so far.

//basic
#include <iostream>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>


//opencv libraries
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"




using namespace cv;
using namespace std;






int main(int argc,char **argv){

Mat im1;
Mat im2;




VideoCapture cap1(1);
VideoCapture cap2(2);


VideoWriter video;
video.open("appsrc ! videoconvert ! x264enc noise-reduction=10000 tune=zerolatency byte-stream=true threads=4 ! mpegtsmux ! rtpmp2tpay send-config=true config-interval=10 pt=96 ! udpsink host=localhost port=5000 -v"
            , 0, (double)20,Size(1280, 480), true);



if(video.isOpened()){
    cout << "Video Writer is opened!"<<endl;
}else{
    cout << "Video Writer is Closed!"<<endl;
 return -1;
}




while(1){

    cap1.grab();
    cap2.grab();

    bool bSuccess1 = cap1.read(im2);
    bool bSuccess2 = cap2.read(im1);

        Size sz1 = im1.size();
        Size sz2 = im2.size();
        Mat im3(sz1.height, sz1.width+sz2.width, CV_8UC3);
        Mat left(im3, Rect(0, 0, sz1.width, sz1.height));
        im1.copyTo(left);
        Mat right(im3, Rect(sz1.width, 0, sz2.width, sz2.height));
        im2.copyTo(right);
            video << im3;

        //imshow("im3", im3);


            if(waitKey(10) == 27){

                break;
            }
        }
        cap1.release();
        cap2.release();
        video.release();

    return 0;

}

And the .sdp file configuration,

v=0
m=video 5000 RTP/AVP 96
c=IN IP4 localhost
a=rtpmap:96 MP2T/90000

I can play the stream from vlc inside the local folder, using

vlc live.sdp

but not inside the network,by using,

vlc rtsp://localhost:5000/live.sdp

Solution

  • Used Gstreamer with opencv solved the problem,but still have small lag.