Pier8283
Published © GPL3+

RTP Video stream with C++ and Raspberry Pi

A compact, low-cost IP camera system built using a Raspberry Pi and USB Camera module, powered by custom RTP software written in C++.

BeginnerProtip2 hours5
RTP Video stream with C++ and Raspberry Pi

Things used in this project

Hardware components

Raspberry Pi 5
Raspberry Pi 5
×1
USB Camera
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Code::Blocks
OpenCV
OpenCV

Story

Read more

Code

WebcamStream source code

C/C++
#include <opencv2/opencv.hpp>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <map>

using namespace cv;



std::string clean(const std::string& s) {
    std::string result = s;
    result.erase(std::remove(result.begin(), result.end(), '\r'), result.end());
    return result;
}

std::map<std::string, std::string> loadConfig(const std::string& filename) {
    std::map<std::string, std::string> config;
    std::ifstream file(filename);
    std::string line;

    while (std::getline(file, line)) {
        std::istringstream iss(line);
        std::string key, value;
        if (std::getline(iss, key, '=') && std::getline(iss, value)) {
            config[clean(key)] = clean(value);
        }
    }

    return config;
}




int main() {
    VideoCapture cap(0);
    if (!cap.isOpened()) {
        printf("Error: impossible to open webcam.\n");
        return -1;
    }

	auto config = loadConfig("stream_config.txt");

	int width = std::stoi(config["width"]);
	int height = std::stoi(config["height"]);
	int fps = std::stoi(config["fps"]);
	std::string ip = config["ip"];
	std::string port = config["port"];
	std::string codec = config["codec"];
	std::string bitrate = config["bitrate"];


    // Set resolution
    cap.set(CAP_PROP_FRAME_WIDTH, width);
    cap.set(CAP_PROP_FRAME_HEIGHT, height);

    // Command FFmpeg for RTP
	std::ostringstream cmd;
	cmd << "ffmpeg -f rawvideo -pix_fmt bgr24 -s "
    << width << "x" << height << " -r " << fps << " -i - "
    << "-c:v " << codec <<  " -preset ultrafast -tune zerolatency -x264-params bframes=0"
    << " -b:v " << bitrate << " -g 15 "
    << "-fflags nobuffer -flags low_delay -flush_packets 1 "
    << "-f rtp rtp://" << ip << ":" << port << " -sdp_file stream.sdp";

	const std::string ffmpegCmdStr = cmd.str();
	const char* ffmpegCmd = ffmpegCmdStr.c_str();


    // Open pipe FFmpeg
    FILE* pipe = popen(ffmpegCmd, "w");
    if (!pipe) {
        printf("Error: impossible to start FFmpeg.\n");
        return -1;
    }

    Mat frame;
    while (true) {
        cap >> frame;
        if (frame.empty()) break;

        // Scrivi i dati raw del frame nella pipe
        fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe);

        // Mostra anteprima
        imshow("Streaming RTP", frame);
        if (waitKey(1) == 'q') break;
    }

    // Chiudi risorse
    pclose(pipe);
    cap.release();
    destroyAllWindows();
    return 0;
}

WebcamStream Code:Blocks project

C/C++
No preview (download only).

Credits

Pier8283
11 projects • 5 followers

Comments