Skip to content

RTSP Demo

This demo demonstrates how to create a simple RTSP streaming pipeline using the AVT SDK. It initializes a video source (camera), encodes the video stream, and serves it via an RTSP server. The code is intended as a practical example of setting up a minimal, yet functional, real-time streaming solution.

Code Example

#include "AvtCore.h"
#include "AvtString.h"
#include "Graph/AvtGraph.h"
#include "Source/AvtVideoSourceProperty.h"
#include "Sink/AvtRTSPStreamParam.h"
#include "DeviceHelper.h"
#include "EncoderHelper.h"
#include "StreamHelper.h"

#include <iostream>
#include <csignal>
#include <unistd.h>

using namespace AVTSDK;
using namespace AVTSDK::Log;
using namespace AVTSDK::Graph;
using namespace AVTSDK::Encoder;
using namespace AVTSDK::Source;
using namespace AVTSDK::Sink;

using namespace std;

bool running = true;

void signalHandler(int signum)
{
    running = false;
}

int main()
{
    AvtCore::init(AvtLogLevel::LOG_LEVEL_WARNING, AvtLogType::LOG_TYPE_DELAYED);

    AvtGraph *graph = nullptr;
    do {
        EncoderHelper encoderHelper;
        auto vEncoderCount = encoderHelper.getVideoEncoderCount();
        if (vEncoderCount == 0) {
            cout << "No Video Encoder" << endl;
            break;
        }

        DeviceHelper devHelper;
        auto vDevCount = devHelper.getVideoDeviceCount();
        if (vDevCount == 0) {
            cout << "No Device" << endl;
            break;
        }

        StreamHelper streamHelper;

        AvtGraphFeature ft;
        ft.mVideo.mEnable = true;
        auto &vFt = ft.mVideo.mProperty;
#if defined(__QCOM__)
        vFt.mGraphicsAPI = AvtGraphicsAPI::GRAPHICS_API_QCOM;
#else
        vFt.mGraphicsAPI = AvtGraphicsAPI::GRAPHICS_API_NV;
#endif

        graph = new AvtGraph(ft);

        AvtVideoSourceProperty prop;
        prop.mLockDevice = true;

        if (!devHelper.selectVideoDevice(prop)) {
            cout << "Failed to select the correct device or device property" << endl;
            break;
        }

        AvtEncoderParam encodeParam;
        if (!encoderHelper.selectVideoEncoder(prop.mResolution, prop.mFrameRate, encodeParam,
                                              false)) {
            cout << "Failed to get the encoder parameters" << endl;
            break;
        }

        AvtRTSPStreamParam rtspParam;
        if (streamHelper.createRTSPServer(rtspParam) != AvtResult::AVT_RESULT_OK)
        {
            cout << "Failed to create RTSP server" << endl;
            break;
        }

        AvtResult result;
        graph->createGraph();
        int sourceID;
        result = graph->addSource(prop, sourceID);
        if (result != AvtResult::AVT_RESULT_OK) {
            cout << "Failed to add video source" << endl;
            break;
        }

        int encoderID;
        result = graph->createEncoder(encodeParam, encoderID);
        if (result != AvtResult::AVT_RESULT_OK) {
            cout << "Failed to create encoder" << endl;
            break;
        }

        int streamID;
        result = graph->addStream(rtspParam, encoderID ,streamID);
        if (result != AvtResult::AVT_RESULT_OK) {
            cout << "Failed to add rtsp stream" << endl;
            break;
        }

        result = graph->runGraph();
        if (result != AvtResult::AVT_RESULT_OK) {
            cout << "Failed to run graph";
            break;
        }

        signal(SIGINT, signalHandler);
        while (running)
            sleep(1);

        graph->removeStream(streamID);

    } while (false);


    if (graph)
        delete graph;

    AvtCore::uninit();
    return 0;
}

Explanation

Helper Classes

EncoderHelper, DeviceHelper, and StreamHelper are helper classes written for the demos and are not part of the library. For details about how to handle encoders, devices, and streams, please check the source code in EncoderHelper.cpp, DeviceHelper.cpp, and StreamHelper.cpp.

  1. Initialization

    The program begins by initializing the AVT core with warning-level log configuration.

  2. Encoder and Device Availability Check

    It checks whether both video encoders and input devices are available. If either is missing, the program exits early.

  3. Graph and Feature Setup

    AvtGraphFeature is the configuration structure for AvtGraph. The video feature is enabled, and the appropriate graphics API (QCOM or NV) is selected based on the platform.

  4. Graph Creation

    A new AvtGraph instance is created using the configured AvtGraphFeature.

    Note

    If you have no idea what AvtGraph is, please refer to AVT SDK Multimedia Framework.

  5. Device and Encoder Selection

    The user is prompted to select a camera device and encoder settings using DeviceHelper::selectVideoDevice() and DeviceHelper::selectEncoder().
    The selected configuration is stored in AvtVideoSourceProperty and AvtVideoEncoderProperty.

  6. Source and Stream Node Addition

    The selected video source is added to the pipeline graph using addSource(). An encoder node is created with createEncoder(), and an RTSP output node is added using addStream(). An RTSP server is also initialized through StreamHelper.

  7. Graph Execution

    The graph is executed using runGraph(), which starts the video processing pipeline.

  8. Runtime Loop and Termination

    A signal handler is registered to listen for SIGINT (Ctrl+C). The program enters a loop to keep the pipeline running until interrupted. Upon termination, the graph is deleted and the AVT core is uninitialized.