Skip to content

RTMP Demo

This demo shows how to stream video from a selected camera device to an RTMP server using the AVT SDK. It covers device and encoder selection, RTMP configuration, pipeline graph construction, and real-time video streaming.

Code Example

#include "AvtCore.h"
#include "AvtString.h"
#include "Graph/AvtGraph.h"
#include "Source/AvtVideoSourceProperty.h"
#include "Sink/AvtRTMPStreamParam.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,
                                              true)) {
            cout << "Failed to get the encoder parameters" << endl;
            break;
        }

        AvtRTMPStreamParam rtmpParam;
        if (streamHelper.selectRTMP(rtmpParam) != AvtResult::AVT_RESULT_OK)
        {
            cout << "Failed to init RTMP parameters" << endl;
            break;
        }
        cout << rtmpParam.mServer.getString() << rtmpParam.mStreamKey.getString() <<endl;

        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(rtmpParam, 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 video input device via DeviceHelper::selectVideoDevice(). Then, based on the selected resolution and frame rate, the encoder parameters are determined using EncoderHelper::selectVideoEncoder().

  6. RTMP Stream Setup

    The RTMP stream parameters are configured through StreamHelper::selectRTMP(), which prompts the user for the server URL and stream key.

  7. 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 RTMP stream node is added using addStream().

  8. Graph Execution

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

  9. 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.