Record Demo
This demo demonstrates how to retrieve video from a selected camera device and save it as an .mp4 file to your current working directory.
Similar to Preview Demo, you need to select the desired camera and configure its settings. In addition, you are required to select a suitable video encoder before starting the recording process.
Code Example
#include "AvtCore.h"
#include "Graph/AvtGraph.h"
#include "EncoderHelper.h"
#include "DeviceHelper.h"
#include "Source/AvtVideoSourceProperty.h"
#include "Sink/AvtRecordParam.h"
#include <iostream>
#include <csignal>
#include <unistd.h>
using namespace AVTSDK;
using namespace AVTSDK::Log;
using namespace AVTSDK::Graph;
using namespace AVTSDK::Source;
using namespace AVTSDK::Encoder;
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;
}
AvtRecordParam recordParam;
recordParam.mFormat = AvtRecordFormat::RECORD_FORMAT_MP4;
recordParam.mOutputPath.setString(L"record.mp4");
AvtGraphFeature ft;
ft.mVideo.mEnable = true;
auto &vFt = ft.mVideo.mProperty;
ft.mAudio.mEnable = false;
vFt.mGraphicsAPI = AvtGraphicsAPI::GRAPHICS_API_QCOM;
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;
}
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 recordID;
result = graph->addRecord(recordParam, encoderID, recordID);
if (result != AvtResult::AVT_RESULT_OK) {
cout << "Failed to add record" << 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->removeRecord(recordID);
} while (false);
if (graph)
delete graph;
AvtCore::uninit();
return 0;
}
Explanation
Helper Classes
EncoderHelper and DeviceHelper are helper classes written for the demos and are not part of the library. For details about how to handle encoders and devices, please check the source code in EncoderHelper.cpp and DeviceHelper.cpp.
-
Initialization
The program begins by initializing the AVT core with a 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.An
AvtRecordParamis also prepared, specifying the output format as MP4 and the file path asrecord.mp4. -
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 camera device and encoder settings using
DeviceHelper::selectVideoDevice()andEncoderHelper::selectVideoEncoder(). The selected configuration is stored inAvtVideoSourcePropertyandAvtVideoEncoderProperty. -
Source and Recording Node Addition
The selected video source is added to the pipeline graph using
addSource(). An encoder node is created usingcreateEncoder(), and a recording sink(1) is added viaaddRecord()to save the video stream as a file.- A sink node is a node that consumes data from the filter graph (pipeline).
-
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 recording node is removed, the graph is deleted, and the AVT core is uninitialized.