Skip to content

Instantly share code, notes, and snippets.

@drabaioli
Created December 7, 2021 19:27
Show Gist options
  • Save drabaioli/7abfa0dfc34dd9ed3344357e8752c876 to your computer and use it in GitHub Desktop.
Save drabaioli/7abfa0dfc34dd9ed3344357e8752c876 to your computer and use it in GitHub Desktop.
Capture an RTP stream in OpenCV with GStreamer backend
#include <opencv2/opencv.hpp>
int main( int argc, char ** argv )
{
cv::VideoCapture cap;
cv::Mat frame;
const std::string windowName{ "RTP Capture" };
cap.open( "udpsrc port=5000 caps = \"application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96\" ! rtph264depay ! decodebin ! videoconvert ! appsink", cv::CAP_GSTREAMER );
std::cout << "Opened " << cap.isOpened() << std::endl;
for( cap >> frame; !frame.empty(); cap >> frame )
{
cv::imshow( windowName, frame );
char key = cv::waitKey( 1 );
if( key == 'q' || key == 27 )
break;
}
cv::destroyWindow( windowName );
}
// Create RTP from camera with:
// gst-launch-1.0 -v v4l2src ! videoscale ! 'video/x-raw,width=640,height=480' ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment