ADTF
Loading...
Searching...
No Matches
Example Demo Sample Data Reference

Description

Shows how to process samples with zero-copy.

Prebuilt Binaries

Source Code

./examples/demo_adtfplugins/demo_sample_data_reference/

Streaming Source

demo_sample_data_reference_source.h

#pragma once
#include <memory>
#include <vector>
#ifndef ADTF_EXAMPLES_CID
#define ADTF_EXAMPLES_CID ".local.cid"
#endif
class cReferenceBuffer: public adtf::ucom::object<adtf::streaming::ISampleBuffer>
{
public:
cReferenceBuffer(std::shared_ptr<const std::vector<uint8_t>> pSharedBuffer):
m_pSharedBuffer(std::move(pSharedBuffer))
{
}
~cReferenceBuffer()
{
// the reference to the shared buffer will be released when this object is destroyed
}
tResult Lock() const override
{
// this is a read only buffer
RETURN_ERROR(ERR_NOT_SUPPORTED);
}
tResult Unlock() const override
{
RETURN_ERROR(ERR_NOT_SUPPORTED);
}
tResult LockShared() const override
{
// this buffer will not be changed during its lifetime, so no locking required
}
tResult UnlockShared() const override
{
}
tResult Write(const adtf::base::IRawMemory& /* oBufferWrite */) override
{
RETURN_ERROR(ERR_NOT_SUPPORTED);
}
tResult Read(adtf::base::IRawMemory&& oBufferRead) const override
{
RETURN_IF_FAILED(oBufferRead.Set(m_pSharedBuffer->data(), m_pSharedBuffer->size()));
}
void* GetPtr() override
{
return nullptr;
}
const void* GetPtr() const override
{
return m_pSharedBuffer->data();
}
size_t GetSize() const override
{
return m_pSharedBuffer->size();
}
size_t GetCapacity() const override
{
return m_pSharedBuffer->size();
}
tResult Reserve(size_t /* szSize */) override
{
RETURN_ERROR(ERR_NOT_SUPPORTED);
}
tResult Resize(size_t /* szSize */) override
{
RETURN_ERROR(ERR_NOT_SUPPORTED);
}
private:
const std::shared_ptr<const std::vector<uint8_t>> m_pSharedBuffer;
};
//*************************************************************************************************
class cDemoSampleDataReferenceSource final : public adtf::filter::cSampleStreamingSource
{
public:
ADTF_CLASS_ID_NAME(cDemoSampleDataReferenceSource,
"demo_sample_data_reference_generator.streaming_source" ADTF_EXAMPLES_CID,
"Demo Sample Data Reference Generator");
ADTF_CLASS_DEPENDENCIES(REQUIRE_INTERFACE(adtf::services::IReferenceClock),
REQUIRE_INTERFACE(adtf::services::IKernel));
private:
adtf::base::property_variable<uint32_t> m_nWidth = 640;
adtf::base::property_variable<uint32_t> m_nHeight = 480;
adtf::base::property_variable<int64_t> m_nFrameDelay = 1000000;
adtf::ucom::object_ptr<adtf::services::IReferenceClock> m_pClock;
adtf::streaming::ISampleWriter* m_pOutputWriter = nullptr;
adtf::filter::cRunnerFallback m_oTimer;
public:
cDemoSampleDataReferenceSource();
~cDemoSampleDataReferenceSource() override;
tResult Init() override;
tResult StartStreaming() override;
tResult StopStreaming() override;
private:
std::shared_ptr<const std::vector<uint8_t>> GetRandomBuffer();
adtf::ucom::object_ptr<adtf::streaming::IStreamType> GetStreamTypeFromProperties();
void TimerFunc();
};
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
Definition result.h:42
#define REQUIRE_INTERFACE(_interface)
Macro usable with ADTF_CLASS_DEPENDENCIES() to require mandatory interfaces.
Definition class_dependencies.h:36
#define ADTF_CLASS_DEPENDENCIES(...)
Add interface ids (string literals,.
Definition class_dependencies.h:61
#define ADTF_CLASS_ID_NAME(_class, _strcid, _strclabel)
Definition class_id.h:33
Definition sample_streaming_source.h:50
Definition object.h:397

demo_sample_data_reference_source.cpp

#include "demo_sample_data_reference_source.h"
using namespace adtf::base;
using namespace adtf::ucom;
using namespace adtf::streaming;
using namespace adtf::filter;
using namespace adtf::system;
// Avoid Warning C4244 in std::generate(m_oBuffer.begin(), m_oBuffer.end(), std::rand)
namespace {
unsigned char generator()
{
return static_cast<unsigned char>(std::rand());
}
}
ADTF_PLUGIN("Demo Sample Data Reference Plugin",
cDemoSampleDataReferenceSource);
cDemoSampleDataReferenceSource::cDemoSampleDataReferenceSource()
{
m_nWidth.SetDescription("Specified width for Sample dimension.");
RegisterPropertyVariable("width", m_nWidth);
m_nHeight.SetDescription("Specified height for Sample dimension.");
RegisterPropertyVariable("height", m_nHeight);
m_nFrameDelay.SetDescription("Update interval in microseconds for Sample creation. This is only used when no Timer Runner is connected to the 'generate_frames' runner.");
RegisterPropertyVariable("frame_delay", m_nFrameDelay);
m_pOutputWriter = CreateOutputPin("output", GetStreamTypeFromProperties());
SetDescription("output", "Provides the generated referenced samples");
// For session compatibility reasons we use this fallback helper for the case where no active runner is connected to the runner.
// In your own implementations use a simple CreateRunner(...) instead.
m_oTimer = adtf::filter::cRunnerFallback(this, "generate_frames", cTimerTriggerHint(*m_nFrameDelay),[this]{ TimerFunc(); });
SetDescription("generate_frames",
"Connect a Timer Runner that will trigger the frame generation. In this case the property `frame_delay` has no effect."
"If this is not connected, the source will create a timer on its own.");
// sets a short description for the component
SetDescription("Use this streaming source to to generate samples that reference external data.");
// set help link to jump to documentation from ADTF Configuration Editor
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_sample_data_reference_generator.html");
}
cDemoSampleDataReferenceSource::~cDemoSampleDataReferenceSource() = default;
tResult cDemoSampleDataReferenceSource::Init()
{
RETURN_IF_FAILED(cSampleStreamingSource::Init());
m_pOutputWriter->ChangeType(GetStreamTypeFromProperties());
}
object_ptr<IStreamType> cDemoSampleDataReferenceSource::GetStreamTypeFromProperties()
{
sFormat.m_strFormatName = ADTF_IMAGE_FORMAT(GREYSCALE_8);
sFormat.m_ui32Width = m_nWidth;
sFormat.m_ui32Height = m_nHeight;
sFormat.m_szMaxByteSize = m_nWidth * m_nHeight;
sFormat.m_ui8DataEndianess = PLATFORM_BYTEORDER;
set_stream_type_image_format(*pType, sFormat);
return pType;
}
tResult cDemoSampleDataReferenceSource::StartStreaming()
{
RETURN_IF_FAILED(_runtime->GetObject(m_pClock));
RETURN_IF_FAILED(m_oTimer.Activate(m_nFrameDelay));
}
tResult cDemoSampleDataReferenceSource::StopStreaming()
{
m_oTimer.Deactivate();
}
std::shared_ptr<const std::vector<uint8_t>> cDemoSampleDataReferenceSource::GetRandomBuffer()
{
const auto pBuffer = std::make_shared<std::vector<uint8_t>>(m_nWidth * m_nHeight);
std::generate(pBuffer->begin(), pBuffer->end(), generator);
return pBuffer;
}
void cDemoSampleDataReferenceSource::TimerFunc()
{
auto tmNow = m_pClock->GetStreamTimeNs();
try
{
{
THROW_IF_FAILED(alloc_sample(pSample, tmNow));
const auto pNewBuffer = adtf::ucom::make_object_ptr<cReferenceBuffer>(GetRandomBuffer());
}
m_pOutputWriter->Write(ucom_object_ptr_cast<ISample>(pSample));
m_pOutputWriter->ManualTrigger(tmNow);
}
catch(...)
{
m_pOutputWriter->SetStreamError(CURRENT_EXCEPTION());
}
}
#define ADTF_PLUGIN(__plugin_identifier,...)
Definition adtf_plugin.h:29
#define CURRENT_EXCEPTION()
converts the current exception to a tResult
Definition result.h:108
#define THROW_IF_FAILED(s)
throws if the expression returns a failed tResult
Definition result.h:88
Definition runner_fallback.h:25
Definition object_ptr.h:384
Namespace for the ADTF Base SDK.
Definition adtf_base_type_traits.h:13
Namespace for the ADTF Filter SDK.
Definition configurable_runner.h:18
Namespace for the ADTF Streaming SDK.
Definition bindingproxyinport.h:14
tResult alloc_sample(ucom::ant::iobject_ptr< ucom::ant::IObject > &pSample)
tResult set_stream_type_image_format(IStreamType &oType, const tStreamImageFormat &oFormat)
Namespace for the ADTF System SDK.
Definition adtf_service.h:14
Namespace for the ADTF uCOM SDK.
Definition adtf_system.h:324
object_ptr< T > ucom_object_ptr_cast(object_ptr< T > oCasted)
Alias always bringing the latest version of ant::ucom_object_ptr_cast() into scope.
Definition object_ptr_utilities.h:104
object_ptr< Implementation > make_object_ptr(Args &&... args)
Alias always bringing the latest version of ant::make_object_ptr() into scope.
Definition object_ptr_utilities.h:129
#define ADTF_IMAGE_FORMAT(_FORMAT_)
Definition streamimageformat.h:183
Definition streamimageformat.h:30
tStreamImageFormatName m_strFormatName
Formatname of the image - see adtf::streaming::ant::imageformat_definition. usually it is a pixelform...
Definition streamimageformat.h:33
uint32_t m_ui32Width
Pixel width of the image.
Definition streamimageformat.h:35
size_t m_szMaxByteSize
Maximal byte size of the image.
Definition streamimageformat.h:39
uint32_t m_ui32Height
Pixel height of the image.
Definition streamimageformat.h:37
uint8_t m_ui8DataEndianess
Endianess of the image (usually pixel endianess) - by default this is PLATFORM_BYTEORDER (PLATFORM_BI...
Definition streamimageformat.h:43
Use this Stream Meta Type for describing a video stream with single frames and there format and set t...
Definition streammetatypeimage.h:101