31 #ifndef DW_FRAMEWORK_CHANNEL_PARAMETERS_HPP_ 32 #define DW_FRAMEWORK_CHANNEL_PARAMETERS_HPP_ 34 #include <dwcgf/Types.hpp> 36 #include <dw/core/base/Types.h> 37 #include <dw/core/container/BaseString.hpp> 38 #include <dw/core/container/VectorFixed.hpp> 39 #include <dw/core/system/NvMediaExt.h> 51 DW_CHANNEL_TYPE_SHMEM_LOCAL = 0,
52 DW_CHANNEL_TYPE_SHMEM_REMOTE = 1,
53 DW_CHANNEL_TYPE_EGLSTREAM = 2,
54 DW_CHANNEL_TYPE_SOCKET = 3,
55 DW_CHANNEL_TYPE_DDS = 4,
56 DW_CHANNEL_TYPE_NVSCI = 5,
64 case DW_CHANNEL_TYPE_SHMEM_LOCAL:
65 result =
"type=SHMEM_LOCAL";
67 case DW_CHANNEL_TYPE_SHMEM_REMOTE:
68 result =
"type=SHMEM_REMOTE";
70 case DW_CHANNEL_TYPE_EGLSTREAM:
71 result =
"type=EGLSTREAM";
73 case DW_CHANNEL_TYPE_SOCKET:
74 result =
"type=SOCKET";
76 case DW_CHANNEL_TYPE_DDS:
79 case DW_CHANNEL_TYPE_NVSCI:
80 result =
"type=NVSCI";
171 template <
typename T>
177 std::size_t start = 0;
182 end = value.find(
"|", start);
184 ChannelParamStr valueSubString = (end != dw::core::FixedString<1>::NPOS) ? value.substr(start, end - start) : value.substr(start, value.length() - start);
186 if (valueSubString ==
"CPU")
190 else if (valueSubString ==
"EGL")
194 else if (valueSubString ==
"CUDA")
198 else if (valueSubString ==
"PVA")
202 else if (valueSubString ==
"DLA")
206 else if (valueSubString ==
"NVMEDIA")
212 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: enabledComponents sub-value unrecongnized: ", valueSubString);
216 }
while (end != dw::core::FixedString<1>::NPOS);
225 if (value ==
"dynamic")
229 else if (value ==
"static")
235 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: connection phase value unrecongnized: ", value);
244 if (value ==
"thread")
248 else if (value ==
"process")
252 else if (value ==
"vm")
256 else if (value ==
"chip")
262 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: reach value unrecongnized: ", value);
271 auto translatedSize = strtol(value.c_str(),
nullptr, 10);
272 return translatedSize;
278 auto translatedSize = ParseChannelParameter<int64_t>(value);
279 if (translatedSize < 0)
281 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: size_t is negative");
283 size_t result =
static_cast<size_t>(translatedSize);
290 auto translatedSize = ParseChannelParameter<size_t>(value);
291 uint32_t result =
static_cast<uint32_t
>(translatedSize);
298 auto translatedSize = ParseChannelParameter<size_t>(value);
299 if (translatedSize > 0xFFFF)
301 throw Exception(DW_INVALID_ARGUMENT,
"ChannelSocketParams: port is larger than uint16_t allows!");
303 uint16_t result =
static_cast<uint16_t
>(translatedSize);
311 if ((value ==
"true") || (value ==
"1"))
315 else if ((value ==
"false") || (value ==
"0"))
321 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: needs to be 'true' or 'false' or 1/0");
330 if (value ==
"producer")
334 else if (value ==
"consumer")
338 else if (value ==
"composite")
344 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: role unknown!");
353 if (value ==
"SHMEM_LOCAL")
355 result = ChannelType::DW_CHANNEL_TYPE_SHMEM_LOCAL;
357 else if (value ==
"SHMEM_REMOTE")
359 result = ChannelType::DW_CHANNEL_TYPE_SHMEM_REMOTE;
361 else if (value ==
"EGLSTREAM")
363 result = ChannelType::DW_CHANNEL_TYPE_EGLSTREAM;
365 else if (value ==
"SOCKET")
367 result = ChannelType::DW_CHANNEL_TYPE_SOCKET;
369 else if (value ==
"DDS")
371 result = ChannelType::DW_CHANNEL_TYPE_DDS;
373 else if (value ==
"NVSCI")
375 result = ChannelType::DW_CHANNEL_TYPE_NVSCI;
379 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: type unknown!");
389 if (value ==
"mailbox")
393 else if (value ==
"singleton")
399 throw Exception(DW_INVALID_ARGUMENT,
"ParseChannelParameter: ChannelMode unknown!");
404 template <
typename T>
407 result = ParseChannelParameter<T>(value);
410 template <
size_t Size>
416 template <
typename T,
size_t N>
424 endpos = value.find(
":", pos);
425 done = endpos == dw::core::FixedString<1>::NPOS;
426 size_t count = done ? endpos : endpos - pos;
429 result.push_back(entry);
439 template <
typename T,
typename... Others>
442 if (key == staticKey)
450 template <
typename... Others>
454 std::size_t pos = channelParams.find(
"=");
455 std::size_t value = 0;
456 std::size_t valueEnd = 0;
460 while (pos != dw::core::FixedString<1>::NPOS && value != dw::core::FixedString<1>::NPOS)
462 keyString = channelParams.substr(key, pos - key);
463 value = channelParams.find(
",", pos);
464 valueEnd = (value == dw::core::FixedString<1>::NPOS) ? (channelParams.length() - (pos + 1)) : (value - pos - 1);
465 valueString = channelParams.substr(pos + 1, valueEnd);
469 pos = channelParams.find(
"=", key);
478 bool producerFifo =
false,
479 uint16_t numBlockingConnections = 1)
481 std::stringstream ss;
482 ss.flags(std::ios::dec);
484 if (serverIP !=
nullptr)
491 ss <<
",producer-fifo=";
492 ss << static_cast<uint32_t>(producerFifo);
493 ss <<
",num-clients=";
494 ss << numBlockingConnections;
502 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams(params);
505 "producer-fifo", m_producerFifo,
520 bool m_producerFifo =
false;
533 dw::core::FixedString<MAX_CHANNEL_ALL_PARAMS_SIZE> channelParams(params);
535 "streamName", m_streamNames,
536 "enabledComponents", m_enabledComponents,
537 "connectionType", m_connectionType,
538 "timeoutUsec", m_timeoutUsec,
561 static_assert(decltype(m_streamNames)::CAPACITY_AT_COMPILE_TIME < std::numeric_limits<uint16_t>::max(),
"ChannelNvSciStreamParams: number of outputs over limit");
562 return static_cast<uint16_t
>(m_streamNames.size());
567 return m_streamNames.size() > 1U;
572 if (index >= m_streamNames.size())
574 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: stream name index out of range");
576 return m_streamNames[index].c_str();
581 if (m_reaches.size() == 0)
585 if (index >= m_reaches.size())
587 throw Exception(DW_INVALID_ARGUMENT,
"ChannelNvSciStreamParams: reach index out of range");
589 return m_reaches[index];
593 dwTime_t m_timeoutUsec = 5000 * 1000;
597 dw::core::VectorFixed<dw::core::FixedString<64>, 8> m_streamNames{};
598 dw::core::VectorFixed<ChannelReach, 8> m_reaches{};
608 "fifo-size", m_fifoSize,
610 "singleton-id", m_singletonId,
611 "trace-name", m_traceId,
614 "reuse", m_reuseEnabled,
615 "debug-port", m_debugPort,
616 "num-clients", m_clientsCount,
617 "debug-num-clients", m_debugClientsCount,
620 "data-offset", m_dataOffset,
621 "strict", m_strictFifo,
622 "sync-enabled", m_syncEnabled);
624 adjustPoolCapacity();
626 if (m_clientsCount == 0)
631 if (!m_singletonId.empty())
638 m_mailboxMode =
true;
642 m_singletonMode =
true;
645 if (!m_id.empty() && m_singletonId.empty())
647 m_singletonId = m_id;
658 case DW_CHANNEL_TYPE_SHMEM_LOCAL:
660 case DW_CHANNEL_TYPE_SOCKET:
663 case DW_CHANNEL_TYPE_NVSCI:
666 case DW_CHANNEL_TYPE_SHMEM_REMOTE:
667 case DW_CHANNEL_TYPE_EGLSTREAM:
668 case DW_CHANNEL_TYPE_DDS:
670 throw Exception(DW_NOT_IMPLEMENTED,
"ChannelParams: no parameters for channel type");
683 const char*
getStr()
const {
return m_str.c_str(); }
695 m_strictFifo = strictFifo;
720 if (m_type != ChannelType::DW_CHANNEL_TYPE_SOCKET)
722 throw Exception(DW_CALL_NOT_ALLOWED,
"ChannelParams: getSocketParams: channel is not of type SOCKET");
724 return m_socketParams;
729 if (m_type != ChannelType::DW_CHANNEL_TYPE_NVSCI)
731 throw Exception(DW_CALL_NOT_ALLOWED,
"ChannelParams: getNvSciStreamParams: channel is not of type NVSCI");
733 return m_nvSciStreamParams;
737 void ValidateMailbox()
743 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton and mailbox modes are incompatible with a fifo setting other than 1");
746 if (!m_mailboxMode && m_reuseEnabled)
748 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: reuse=true specified when mode!=mailbox. Not valid");
750 if (m_mailboxMode && m_singletonMode)
752 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is incompatible mailbox mode");
756 void ValidateSingleton()
759 if (m_singletonMode && m_singletonId.empty())
761 m_singletonId = m_id;
763 if (!m_singletonMode && !m_singletonId.empty())
765 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode requires both the mode set AND singletonId set");
767 if (m_singletonMode && (m_type != ChannelType::DW_CHANNEL_TYPE_SHMEM_LOCAL))
769 throw Exception(DW_INVALID_ARGUMENT,
"ChannelParams: Singleton mode is only valid for SHMEM_LOCAL channels");
773 void adjustPoolCapacity()
781 if (m_fifoSize + MAX_CHANNEL_CONSUMERS_COUNT + 1 > m_poolCapacity)
783 m_poolCapacity = m_fifoSize + MAX_CHANNEL_CONSUMERS_COUNT + 1;
795 size_t m_fifoSize = 1;
802 size_t m_poolCapacity = MAX_CHANNEL_CONSUMERS_COUNT + 2;
804 uint32_t m_dataOffset = 0;
805 bool m_syncEnabled =
false;
806 bool m_mailboxMode =
false;
807 bool m_singletonMode =
false;
808 bool m_reuseEnabled =
false;
810 ChannelType m_type = ChannelType::DW_CHANNEL_TYPE_SHMEM_LOCAL;
811 bool m_strictFifo =
true;
813 uint16_t m_clientsCount = 1;
814 uint16_t m_debugClientsCount = 1;
816 uint16_t m_debugPort = 0;
825 #endif // DW_FRAMEWORK_CHANNEL_HPP_ constexpr bool IsProducer(ChannelRole role)
void setMailboxMode(bool mailboxEnabled)
static constexpr uint16_t MAX_CHANNEL_PRODUCERS_COUNT
dwTime_t getTimeoutUsec() const
ChannelSocketParams(const char *params)
constexpr ChannelNvSciStreamEnabledComponents operator|(ChannelNvSciStreamEnabledComponents a, ChannelNvSciStreamEnabledComponents b)
ChannelType getType() const
bool getDebugMode() const
const ChannelNvSciStreamParams & getNvSciStreamParams() const
bool isEnabledComponentCuda() const
ChannelParamStr getTraceId() const
static void ParseAllChannelParameters(const ChannelParamStr &channelParams, Others &&... others)
ChannelNvSciStreamEnabledComponents
bool isEnabledComponentDla() const
ChannelParams(const char *params)
bool getSyncEnabled() const
bool getMailboxMode() const
uint16_t getDebugPort() const
uint32_t getDataOffset() const
enum ChannelType :uint8_t { DW_CHANNEL_TYPE_SHMEM_LOCAL=0, DW_CHANNEL_TYPE_SHMEM_REMOTE=1, DW_CHANNEL_TYPE_EGLSTREAM=2, DW_CHANNEL_TYPE_SOCKET=3, DW_CHANNEL_TYPE_DDS=4, DW_CHANNEL_TYPE_NVSCI=5, } ChannelType
const ChannelSocketParams & getSocketParams() const
ChannelNvSciStreamParams(const char *params)
ChannelConnectionType getChannelConnectionType() const
ChannelParams(const ChannelParams &other)
size_t getPoolCapacity() const
ChannelReach getChannelReach(uint16_t index=0) const
uint16_t getExpectedDebugConnectionsCount() const
static T ParseChannelParameter(const ChannelParamStr &value)
const char * getStr() const
bool isEnabledComponentEgl() const
constexpr ChannelNvSciStreamEnabledComponents operator &(ChannelNvSciStreamEnabledComponents a, ChannelNvSciStreamEnabledComponents b)
ChannelNvSciStreamEnabledComponents getEnabledComponents() const
static constexpr uint16_t MAX_CHANNEL_CONSUMERS_COUNT
size_t getFifoSize() const
bool isConnectionTypeStatic() const
bool isConnectionTypeDynamic() const
bool hasProducerFifo() const
ChannelRole getRole() const
static constexpr uint16_t MAX_CHANNEL_ALL_PARAMS_SIZE
bool isEnabledComponentCpu() const
void setStrictFifo(bool strictFifo)
bool getReuseEnabled() const
ChannelParamStr getSingletonId() const
uint16_t getNumOutputs() const
static void ParseChannelParameters(const ChannelParamStr &, const ChannelParamStr &)
bool isEnabledComponentNvmedia() const
uint16_t getExpectedConnectionsCount() const
ChannelParamStr getServerIP() const
bool isEnabledComponentPva() const
static constexpr uint16_t MAX_CHANNEL_PARAM_SIZE
ChannelParamStr getId() const
dw::core::FixedString< MAX_CHANNEL_PARAM_SIZE > ChannelParamStr
static ChannelParamStr getParamStr(const char *serverIP, uint16_t port, bool producerFifo=false, uint16_t numBlockingConnections=1)
static const char * ToParam(ChannelType channelType)
const char * getStreamName(uint16_t index=0) const
bool getSingletonMode() const
bool isStrictFifo() const
constexpr bool IsConsumer(ChannelRole role)