18#include <peak/dll_interface/peak_dll_interface_util.hpp>
20#include <unordered_map>
183 ,
public std::enable_shared_from_this<DataStream>
199 std::string
Key()
const;
205 std::string
ID()
const;
208 std::string
TLType()
const;
470 void QueueBuffer(
const std::shared_ptr<Buffer>& buffer);
490 void RevokeBuffer(
const std::shared_ptr<Buffer>& buffer);
652 static void PEAK_CALL_CONV BufferRevocationCallbackCWrapper(
void* buffer,
void* userPtr,
void* context);
658 void AddAnnouncedBuffer(
const std::shared_ptr<Buffer>& buffer);
659 void RemoveAnnouncedBuffer(
const std::shared_ptr<Buffer>& buffer);
660 void RevokeAnnouncedBuffers();
664 friend ClassCreator<DataStream>;
668 std::weak_ptr<Device> m_parentDevice;
670 std::vector<std::shared_ptr<Buffer>> m_announcedBuffers;
671 std::unordered_map<PEAK_BUFFER_HANDLE, std::shared_ptr<Buffer>> m_announcedBuffersByHandle;
672 std::unordered_map<PEAK_BUFFER_HANDLE, std::unique_ptr<BufferRevocationCallback>>
673 m_bufferRevocationCallbacksByBufferHandle;
675 mutable std::mutex m_announcedBuffersMutex;
676 mutable std::mutex m_bufferRevocationCallbacksByBufferHandleMutex;
692 std::string entryString;
696 entryString =
"InputPoolToOutputQueue";
700 entryString =
"DiscardOutputQueue";
704 entryString =
"AllToInputPool";
708 entryString =
"UnqueuedToInputPool";
712 entryString =
"DiscardAll";
716 entryString =
"Custom";
724 std::string entryString;
728 entryString =
"Default";
732 entryString =
"Custom";
740 std::string entryString;
744 entryString =
"Default";
748 entryString =
"Kill";
752 entryString =
"Custom";
759 : m_backendHandle(dataStreamHandle)
760 , m_parentDevice(parentDevice)
761 , m_announcedBuffers()
762 , m_announcedBuffersByHandle()
763 , m_announcedBuffersMutex()
764 , m_key(QueryStringFromCInterfaceFunction([&](char* key, size_t* keySize) {
788 return QueryRawInformationFromCInterfaceFunction([&](int32_t* dataType, uint8_t* buffer,
size_t* bufferSize) {
790 m_backendHandle, infoCommand, dataType, buffer, bufferSize);
796 return QueryStringFromCInterfaceFunction([&](
char*
id,
size_t* idSize) {
803 return QueryStringFromCInterfaceFunction([&](
char* tlType,
size_t* tlTypeSize) {
810 return QueryNumericFromCInterfaceFunction<size_t>([&](
size_t* numBuffersAnnouncedMinRequired) {
812 m_backendHandle, numBuffersAnnouncedMinRequired);
818 return QueryNumericFromCInterfaceFunction<size_t>([&](
size_t* numBuffersAnnounced) {
825 return QueryNumericFromCInterfaceFunction<size_t>([&](
size_t* numBuffersQueued) {
832 return QueryNumericFromCInterfaceFunction<size_t>([&](
size_t* numBuffersAwaitDelivery) {
834 m_backendHandle, numBuffersAwaitDelivery);
840 return QueryNumericFromCInterfaceFunction<uint64_t>([&](uint64_t* numBuffersDelivered) {
847 return QueryNumericFromCInterfaceFunction<uint64_t>([&](uint64_t* numBuffersStarted) {
854 return QueryNumericFromCInterfaceFunction<uint64_t>([&](uint64_t* numUnderruns) {
861 return QueryNumericFromCInterfaceFunction<size_t>([&](
size_t* numChunksPerBufferMax) {
868 return QueryNumericFromCInterfaceFunction<size_t>([&](
size_t* bufferAlignment) {
875 return QueryNumericFromCInterfaceFunction<size_t>([&](
size_t* payloadSize) {
882 return QueryNumericFromCInterfaceFunction<PEAK_BOOL8>([&](
PEAK_BOOL8* definesPayloadSize) {
889 return QueryNumericFromCInterfaceFunction<PEAK_BOOL8>([&](
PEAK_BOOL8* isGrabbing) {
897 auto _callback = std::make_unique<BufferRevocationCallback>(revocationCallback);
899 auto callbackContext = _callback.get();
901 auto bufferHandle = QueryNumericFromCInterfaceFunction<PEAK_BUFFER_HANDLE>(
904 BufferRevocationCallbackCWrapper, callbackContext, _bufferHandle);
907 auto _buffer = std::make_shared<ClassCreator<Buffer>>(bufferHandle, shared_from_this());
908 AddAnnouncedBuffer(_buffer);
911 std::lock_guard<std::mutex> lock(m_bufferRevocationCallbacksByBufferHandleMutex);
913 m_bufferRevocationCallbacksByBufferHandle.emplace(bufferHandle, std::move(_callback));
921 auto bufferHandle = QueryNumericFromCInterfaceFunction<PEAK_BUFFER_HANDLE>(
924 m_backendHandle, size, userPtr, _bufferHandle);
927 auto buffer = std::make_shared<ClassCreator<Buffer>>(bufferHandle, shared_from_this());
928 AddAnnouncedBuffer(buffer);
935 if (buffer ==
nullptr)
940 auto bufferHandle = buffer->m_backendHandle;
942 CallAndCheckCInterfaceFunction(
948 if (buffer ==
nullptr)
953 auto bufferHandle = buffer->m_backendHandle;
955 CallAndCheckCInterfaceFunction(
958 RemoveAnnouncedBuffer(buffer);
961 std::lock_guard<std::mutex> lock(m_bufferRevocationCallbacksByBufferHandleMutex);
963 m_bufferRevocationCallbacksByBufferHandle.erase(bufferHandle);
971 auto bufferHandle = QueryNumericFromCInterfaceFunction<PEAK_BUFFER_HANDLE>(
974 m_backendHandle, timeout_ms, _bufferHandle);
977 return GetAnnouncedBuffer(bufferHandle);
992 CallAndCheckCInterfaceFunction([&] {
1000 std::lock_guard<std::mutex> lock(m_announcedBuffersMutex);
1002 return m_announcedBuffers;
1007 CallAndCheckCInterfaceFunction([&] {
1015 CallAndCheckCInterfaceFunction([&] {
1023 return LockOrThrow(m_parentDevice);
1026inline void PEAK_CALL_CONV DataStream::BufferRevocationCallbackCWrapper(
void* buffer,
void* userPtr,
void* context)
1031 callback->operator()(buffer, userPtr);
1037 auto moduleHandle = QueryNumericFromCInterfaceFunction<PEAK_MODULE_HANDLE>(
1042 return moduleHandle;
1047 auto eventSupportingModuleHandle = QueryNumericFromCInterfaceFunction<PEAK_EVENT_SUPPORTING_MODULE_HANDLE>(
1050 m_backendHandle, _eventSupportingModuleHandle);
1053 return eventSupportingModuleHandle;
1056inline std::shared_ptr<Buffer> DataStream::GetAnnouncedBuffer(
PEAK_BUFFER_HANDLE bufferHandle)
const
1058 std::lock_guard<std::mutex> lock(m_announcedBuffersMutex);
1060 return m_announcedBuffersByHandle.at(bufferHandle);
1063inline void DataStream::AddAnnouncedBuffer(
const std::shared_ptr<Buffer>& buffer)
1065 std::lock_guard<std::mutex> lock(m_announcedBuffersMutex);
1067 m_announcedBuffers.emplace_back(buffer);
1068 m_announcedBuffersByHandle.emplace(buffer->m_backendHandle, buffer);
1071inline void DataStream::RemoveAnnouncedBuffer(
const std::shared_ptr<Buffer>& buffer)
1073 std::lock_guard<std::mutex> lock(m_announcedBuffersMutex);
1075 m_announcedBuffers.erase(std::remove(std::begin(m_announcedBuffers), std::end(m_announcedBuffers), buffer));
1076 m_announcedBuffersByHandle.erase(buffer->m_backendHandle);
1079inline void DataStream::RevokeAnnouncedBuffers()
1081 for (
const auto& buffer : AnnouncedBuffers())
1083 RevokeBuffer(buffer);
1087inline void DataStream::Shutdown()
1091 StopAcquisition(AcquisitionStopMode::Default);
1093 Flush(DataStreamFlushMode::DiscardAll);
1094 RevokeAnnouncedBuffers();
static const uint64_t INFINITE_NUMBER
Definition peak_data_stream.hpp:189
std::shared_ptr< Buffer > AnnounceBuffer(void *buffer, size_t size, void *userPtr, const BufferRevocationCallback &revocationCallback)
Announces a given client allocated buffer to the data stream.
Definition peak_data_stream.hpp:894
DataStream(DataStream &&other)=delete
RawInformation Info(int32_t infoCommand) const
Delivers information based on the given GenTL info command.
Definition peak_data_stream.hpp:786
void RevokeBuffer(const std::shared_ptr< Buffer > &buffer)
Revokes a specified buffer.
Definition peak_data_stream.hpp:946
bool DefinesPayloadSize() const
Checks whether the data stream defines the payload size.
Definition peak_data_stream.hpp:880
void StartAcquisition(AcquisitionStartMode mode=AcquisitionStartMode::Default, uint64_t numToAcquire=INFINITE_NUMBER)
Starts the acquisition.
Definition peak_data_stream.hpp:1005
DataStream & operator=(DataStream &&other)=delete
size_t BufferAlignment() const
Returns the buffer alignment.
Definition peak_data_stream.hpp:866
void QueueBuffer(const std::shared_ptr< Buffer > &buffer)
Queues a given buffer.
Definition peak_data_stream.hpp:933
std::shared_ptr< Device > ParentDevice() const
Returns the parent device.
Definition peak_data_stream.hpp:1021
std::shared_ptr< Buffer > AllocAndAnnounceBuffer(size_t size, void *userPtr)
Allocates and announces a producer allocated buffer on the data stream.
Definition peak_data_stream.hpp:919
size_t NumBuffersAnnounced() const
Retrieves the count of buffers announced via AnnounceBuffer or AllocAndAnnounceBuffer.
Definition peak_data_stream.hpp:816
~DataStream() override
Definition peak_data_stream.hpp:769
uint64_t NumBuffersDelivered() const
Retrieves the count of buffers delivered since the last acquisition start.
Definition peak_data_stream.hpp:838
std::string Key() const
Returns the unique key.
Definition peak_data_stream.hpp:781
std::string TLType() const
Returns the TL (transport layer) type.
Definition peak_data_stream.hpp:801
uint64_t NumUnderruns() const
Retrieves the count of underruns.
Definition peak_data_stream.hpp:852
uint64_t NumBuffersStarted() const
Returns the count of buffers that have been initiated.
Definition peak_data_stream.hpp:845
std::shared_ptr< Buffer > WaitForFinishedBuffer(Timeout timeout_ms)
Blocking wait for a finished Buffer.
Definition peak_data_stream.hpp:969
size_t NumBuffersAnnouncedMinRequired() const
Returns the minimum number of buffers that must be announced before starting the acquisition with Sta...
Definition peak_data_stream.hpp:808
void KillWait()
Tells the data stream to kill one call to WaitForFinishedBuffer.
Definition peak_data_stream.hpp:980
void Flush(DataStreamFlushMode mode)
Move buffers between the Input Buffer Pool, the Output Buffer Pool and the Announced Buffer Pool,...
Definition peak_data_stream.hpp:990
size_t NumBuffersAwaitDelivery() const
Retrieves the count of buffers that are currently waiting to be delivered, i.e., the number of buffer...
Definition peak_data_stream.hpp:830
bool IsGrabbing() const
Checks whether the data stream is grabbing.
Definition peak_data_stream.hpp:887
size_t NumChunksPerBufferMax() const
Returns the maximum number of chunks per buffer.
Definition peak_data_stream.hpp:859
size_t PayloadSize() const
Returns the payload size in bytes, i.e. the size of the buffers.
Definition peak_data_stream.hpp:873
std::function< void(void *buffer, void *userPtr)> BufferRevocationCallback
Definition peak_data_stream.hpp:187
DataStream(const DataStream &other)=delete
std::vector< std::shared_ptr< Buffer > > AnnouncedBuffers() const
Retrieves the list of the currently announced buffers. The buffers' states are irrelevant,...
Definition peak_data_stream.hpp:998
void StopAcquisition(AcquisitionStopMode mode=AcquisitionStopMode::Default)
Stops the acquisition.
Definition peak_data_stream.hpp:1013
std::string ID() const
Returns the ID.
Definition peak_data_stream.hpp:794
DataStream & operator=(const DataStream &other)=delete
size_t NumBuffersQueued() const
Retrieves the count of buffers that are currently queued, i.e., the number of buffers present in the ...
Definition peak_data_stream.hpp:823
void FlushPendingKillWaits()
Flushes the KillWait queue.
Definition peak_data_stream.hpp:985
EventSupportingModule()=default
The base class for all exceptions thrown by the library.
Definition peak_exception.hpp:26
The exception thrown for passing an invalid argument to a function.
Definition peak_exception.hpp:74
Represents a timeout value in milliseconds.
Definition peak_timeout.hpp:31
The "core" namespace contains the direct GenAPI and GenTL wrapper.
Definition peak_buffer.hpp:31
DataStreamFlushMode
Operation modes for DataStream::Flush() method.
Definition peak_data_stream.hpp:43
@ UnqueuedToInputPool
Definition peak_data_stream.hpp:70
@ DiscardOutputQueue
Definition peak_data_stream.hpp:57
@ Custom
Definition peak_data_stream.hpp:83
@ InputPoolToOutputQueue
Definition peak_data_stream.hpp:51
@ AllToInputPool
Definition peak_data_stream.hpp:64
@ DiscardAll
Definition peak_data_stream.hpp:78
std::string ToString(BufferPayloadType entry)
Definition peak_buffer.hpp:519
AcquisitionStartMode
The enum holding the possible acquisition start modes, for DataStream::StartAcquisition().
Definition peak_data_stream.hpp:92
@ Default
Definition peak_data_stream.hpp:96
@ Custom
Definition peak_data_stream.hpp:101
@ Custom
Definition peak_buffer.hpp:52
AcquisitionStopMode
The enum holding the possible acquisition stop modes, for DataStream::StopAcquisition().
Definition peak_data_stream.hpp:110
@ Kill
Definition peak_data_stream.hpp:125
@ Default
Definition peak_data_stream.hpp:116
@ Custom
Definition peak_data_stream.hpp:130
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_dynamic_loader.h:33
struct PEAK_DATA_STREAM * PEAK_DATA_STREAM_HANDLE
Definition peak_backend.h:401
PEAK_C_API PEAK_DataStream_KillWait(PEAK_DATA_STREAM_HANDLE dataStreamHandle)
Tells the data stream to kill one call to PEAK_DataStream_WaitForFinishedBuffer.
PEAK_C_API PEAK_DataStream_GetNumBuffersAnnouncedMinRequired(PEAK_DATA_STREAM_HANDLE dataStreamHandle, size_t *numBuffersAnnouncedMinRequired)
Asks the given data stream for its minimum required number of announced buffers.
struct PEAK_MODULE * PEAK_MODULE_HANDLE
Definition peak_backend.h:421
PEAK_C_API PEAK_DataStream_ToModule(PEAK_DATA_STREAM_HANDLE dataStreamHandle, PEAK_MODULE_HANDLE *moduleHandle)
Casts the given data stream to a module.
PEAK_C_API PEAK_DataStream_Flush(PEAK_DATA_STREAM_HANDLE dataStreamHandle, PEAK_DATA_STREAM_FLUSH_MODE flushMode)
Tells the given data stream to flush its queues using the given mode.
PEAK_C_API PEAK_DataStream_Destruct(PEAK_DATA_STREAM_HANDLE dataStreamHandle)
Destroys the given data stream.
PEAK_C_API PEAK_DataStream_GetNumUnderruns(PEAK_DATA_STREAM_HANDLE dataStreamHandle, uint64_t *numUnderruns)
Asks the given data stream for its number of underruns.
PEAK_C_API PEAK_DataStream_GetBufferAlignment(PEAK_DATA_STREAM_HANDLE dataStreamHandle, size_t *bufferAlignment)
Asks the given data stream for its buffer alignment.
PEAK_C_API PEAK_DataStream_GetNumChunksPerBufferMax(PEAK_DATA_STREAM_HANDLE dataStreamHandle, size_t *numChunksPerBufferMax)
Asks the given data stream for its maximum number of chunks per buffer.
PEAK_C_API PEAK_DataStream_GetDefinesPayloadSize(PEAK_DATA_STREAM_HANDLE dataStreamHandle, PEAK_BOOL8 *definesPayloadSize)
Asks the given data stream whether it defines the payload size.
struct PEAK_BUFFER * PEAK_BUFFER_HANDLE
Definition peak_backend.h:405
struct PEAK_EVENT_SUPPORTING_MODULE * PEAK_EVENT_SUPPORTING_MODULE_HANDLE
Definition peak_backend.h:477
PEAK_C_API PEAK_DataStream_GetNumBuffersStarted(PEAK_DATA_STREAM_HANDLE dataStreamHandle, uint64_t *numBuffersStarted)
Asks the given data stream for its number of started buffers.
PEAK_C_API PEAK_DataStream_GetPayloadSize(PEAK_DATA_STREAM_HANDLE dataStreamHandle, size_t *payloadSize)
Asks the given data stream for its payload size.
PEAK_C_API PEAK_DataStream_GetNumBuffersAnnounced(PEAK_DATA_STREAM_HANDLE dataStreamHandle, size_t *numBuffersAnnounced)
Asks the given data stream for its number of announced buffers.
PEAK_C_API PEAK_DataStream_GetKey(PEAK_DATA_STREAM_HANDLE dataStreamHandle, char *key, size_t *keySize)
Asks the given data stream for its key.
PEAK_C_API PEAK_DataStream_AnnounceBuffer(PEAK_DATA_STREAM_HANDLE dataStreamHandle, void *buffer, size_t bufferSize, void *userPtr, PEAK_BUFFER_REVOCATION_CALLBACK revocationCallback, void *callbackContext, PEAK_BUFFER_HANDLE *bufferHandle)
Announces the given buffer at the given data stream.
PEAK_C_API PEAK_DataStream_GetNumBuffersQueued(PEAK_DATA_STREAM_HANDLE dataStreamHandle, size_t *numBuffersQueued)
Asks the given data stream for its number of queued buffers.
PEAK_C_API PEAK_DataStream_FlushPendingKillWaits(PEAK_DATA_STREAM_HANDLE dataStreamHandle)
Flushes the KillWait queue.
PEAK_C_API PEAK_DataStream_WaitForFinishedBuffer(PEAK_DATA_STREAM_HANDLE dataStreamHandle, uint64_t timeout_ms, PEAK_BUFFER_HANDLE *bufferHandle)
Blocking wait for a finished Buffer.
PEAK_C_API PEAK_DataStream_GetIsGrabbing(PEAK_DATA_STREAM_HANDLE dataStreamHandle, PEAK_BOOL8 *isGrabbing)
Asks the given data stream whether it is grabbing.
PEAK_C_API PEAK_DataStream_RevokeBuffer(PEAK_DATA_STREAM_HANDLE dataStreamHandle, PEAK_BUFFER_HANDLE bufferHandle)
Tells the given data stream to revoke the given buffer.
int32_t PEAK_ACQUISITION_START_MODE
Definition peak_backend.h:52
int8_t PEAK_BOOL8
Definition peak_backend.h:43
PEAK_C_API PEAK_DataStream_GetNumBuffersDelivered(PEAK_DATA_STREAM_HANDLE dataStreamHandle, uint64_t *numBuffersDelivered)
Asks the given data stream for its number of delivered buffers.
const uint64_t PEAK_INFINITE_NUMBER
The constant defining an infinite number of buffers.
Definition peak_backend.h:358
int32_t PEAK_ACQUISITION_STOP_MODE
Definition peak_backend.h:62
int32_t PEAK_DATA_STREAM_FLUSH_MODE
Definition peak_backend.h:120
PEAK_C_API PEAK_DataStream_GetID(PEAK_DATA_STREAM_HANDLE dataStreamHandle, char *id, size_t *idSize)
Asks the given data stream for its ID.
PEAK_C_API PEAK_DataStream_StopAcquisition(PEAK_DATA_STREAM_HANDLE dataStreamHandle, PEAK_ACQUISITION_STOP_MODE stopMode)
Tells the given data stream to stop the acquisition using the given mode.
PEAK_C_API PEAK_DataStream_QueueBuffer(PEAK_DATA_STREAM_HANDLE dataStreamHandle, PEAK_BUFFER_HANDLE bufferHandle)
Tells the given data stream to queue the given buffer.
PEAK_C_API PEAK_DataStream_GetNumBuffersAwaitDelivery(PEAK_DATA_STREAM_HANDLE dataStreamHandle, size_t *numBuffersAwaitDelivery)
Asks the given data stream for its number of buffers awaiting delivery.
PEAK_C_API PEAK_DataStream_AllocAndAnnounceBuffer(PEAK_DATA_STREAM_HANDLE dataStreamHandle, size_t bufferSize, void *userPtr, PEAK_BUFFER_HANDLE *bufferHandle)
Tells the given data stream to allocate and announce a buffer with the given size.
PEAK_C_API PEAK_DataStream_GetTLType(PEAK_DATA_STREAM_HANDLE dataStreamHandle, char *tlType, size_t *tlTypeSize)
Asks the given data stream for its TL type.
PEAK_C_API PEAK_DataStream_GetInfo(PEAK_DATA_STREAM_HANDLE dataStreamHandle, int32_t infoCommand, int32_t *infoDataType, uint8_t *info, size_t *infoSize)
Asks the given data stream for an information based on a specific command.
PEAK_C_API PEAK_DataStream_ToEventSupportingModule(PEAK_DATA_STREAM_HANDLE dataStreamHandle, PEAK_EVENT_SUPPORTING_MODULE_HANDLE *eventSupportingModuleHandle)
Casts the given data stream to an event-supporting module.
PEAK_C_API PEAK_DataStream_StartAcquisition(PEAK_DATA_STREAM_HANDLE dataStreamHandle, PEAK_ACQUISITION_START_MODE startMode, uint64_t numToAcquire)
Tells the given data stream to start the acquisition using the given mode and to acquire the given nu...
#define PEAK_C_ABI_PREFIX
Definition peak_dll_defines.h:16
#define PEAK_CALL_CONV
Definition peak_dynamic_loader.h:30