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.
-
Initialization
The program begins by initializing the AVT core with warning-level log configuration.
-
Encoder and Device Availability Check
It checks whether both video encoders and input devices are available. If either is missing, the program exits early.
-
Graph and Feature Setup
AvtGraphFeatureis the configuration structure forAvtGraph. The video feature is enabled, and the appropriate graphics API (QCOMorNV) is selected based on the platform. -
Graph Creation
A new
AvtGraphinstance is created using the configuredAvtGraphFeature.Note
If you have no idea what
AvtGraphis, please refer to AVT SDK Multimedia Framework. -
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 usingEncoderHelper::selectVideoEncoder(). -
RTMP Stream Setup
The RTMP stream parameters are configured through
StreamHelper::selectRTMP(), which prompts the user for the server URL and stream key. -
Source and Stream Node Addition
The selected video source is added to the pipeline graph using
addSource(). An encoder node is created withcreateEncoder(), and an RTMP stream node is added usingaddStream(). -
Graph Execution
The graph is executed using
runGraph(), which starts the video processing pipeline. -
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.