Objects created by the SDK are released with tsvb::IRelease::release(), never with delete. A deleter that calls it lets std::unique_ptr and std::shared_ptr own them, so they are released on every path out of a scope, early returns and exceptions included.
The deleter
#include <memory>
struct SdkReleaser
{
{
object->release();
}
};
template <typename T>
using SdkPtr = std::unique_ptr<T, SdkReleaser>;
Base interface of all objects created by the SDK.
Definition irelease.h:17
SdkPtr takes any object the caller is responsible for releasing: tsvb::ISDKFactory, tsvb::IAuthResult, tsvb::IError, tsvb::IFrameFactory, tsvb::IFrame, tsvb::ILockedFrameData, tsvb::IPipeline, tsvb::IPipelineConfiguration and tsvb::IReplacementController.
Don't put into it what the SDK keeps for itself:
unique_ptr
struct Effects
{
SdkPtr<::tsvb::IFrameFactory> frameFactory;
SdkPtr<::tsvb::IPipeline> pipeline;
SdkPtr<::tsvb::IReplacementController> replacementController;
};
std::optional<Effects> createEffects(const char* customerID, const char* backgroundPath)
{
SdkPtr<::tsvb::ISDKFactory> factory(createFactory());
SdkPtr<::tsvb::IAuthResult> authResult(factory->auth(customerID, nullptr, nullptr));
return std::nullopt;
}
Effects effects;
effects.frameFactory.reset(factory->createFrameFactory());
effects.pipeline.reset(factory->createPipeline());
effects.pipeline->enableNoiseSuppression();
effects.pipeline->enableReplaceBackground(&controller);
effects.replacementController.reset(controller);
SdkPtr<::tsvb::IFrame> background(effects.frameFactory->loadImage(backgroundPath));
if (nullptr != background) {
effects.replacementController->setBackgroundImage(background.get());
}
return effects;
}
Controls the custom background of background replacement.
Definition pipeline.h:92
@ active
Authorization succeeded and the license is active. The SDK can be used.
Definition sdk_factory.h:32
The SDK factory is released when createEffects() returns: the frame factory and the pipeline it created don't need it. Frames, too, may outlive the frame factory that created them.
An out-parameter, such as the controller of tsvb::IPipeline::enableReplaceBackground(), goes through a plain pointer first and into the smart pointer after the call.
Processing a frame and reading the result:
SdkPtr<::tsvb::IFrame> input(
effects.frameFactory->createBGRA(bgra, bytesPerLine, width, height, false)
);
SdkPtr<::tsvb::IFrame> output(effects.pipeline->process(input.get(), &error));
if (nullptr == output) {
return;
}
{
const void* pixels = data->dataPointer(0);
unsigned int stride = data->bytesPerLine(0);
}
int PipelineError
Error code of IPipeline, one of PipelineErrorCode values.
Definition pipeline.h:24
@ read
Read only. Don't change the data.
Definition frame.h:20
@ ok
Success.
Definition pipeline.h:13
shared_ptr
A tsvb::IPipeline is thread-safe, so it can be shared, for example between the thread that processes the frames and the one that turns the effects on and off. Pass the deleter to the constructor of std::shared_ptr:
std::shared_ptr<::tsvb::IPipeline> pipeline(factory->createPipeline(), SdkReleaser());
A SdkPtr converts into a std::shared_ptr with its deleter:
SdkPtr<::tsvb::IPipeline> owned(factory->createPipeline());
std::shared_ptr<::tsvb::IPipeline> shared = std::move(owned);
Unloading the library
A smart pointer calls code of the SDK library when it releases its object. If the application loads the library at run time, release every object before it frees the library: see Library lifetime.