Skip to content

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.

  1. Initialization

    The program begins by initializing the AVT core with a 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.

    An AvtRecordParam is also prepared, specifying the output format as MP4 and the file path as record.mp4.

  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 EncoderHelper::selectVideoEncoder(). The selected configuration is stored in AvtVideoSourceProperty and AvtVideoEncoderProperty.

  6. Source and Recording Node Addition

    The selected video source is added to the pipeline graph using addSource(). An encoder node is created using createEncoder(), and a recording sink(1) is added via addRecord() to save the video stream as a file.

    1. A sink node is a node that consumes data from the filter graph (pipeline).
  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 recording node is removed, the graph is deleted, and the AVT core is uninitialized.