31 #ifndef DW_FRAMEWORK_PORT_H_ 32 #define DW_FRAMEWORK_PORT_H_ 66 virtual bool isBound() = 0;
86 static constexpr
char LOG_TAG[] =
"PortOutput";
100 : m_reference(std::move(ref))
108 , m_waiterAttrs(waiterAttrs)
109 , m_signalerAttrs(signalerAttrs)
116 auto ref = make_specimen<T>(&m_reference);
117 return bindChannelWithReference(channel, ref);
127 FRWK_LOGE <<
"PortOutput: bindChannel: attempted to bind the same port twice, ignoring this bind!" << Logger::State::endl;
130 if (channel ==
nullptr)
132 throw Exception(DW_INVALID_ARGUMENT,
"PortOutput: bindChannel: expected channel != nullptr");
135 BaseSyncHelper::parseDataSynced(channel->
getParams());
137 if (BaseSyncHelper::isDataSynced())
146 if (m_channelProducer ==
nullptr)
148 throw Exception(DW_INTERNAL_ERROR,
"PortOutput bindChannel: wrong channel implementations returned.");
157 throw Exception(DW_NOT_AVAILABLE,
"PortOutput: setOnDataReady: no bound channel");
159 m_channelProducer->setOnDataReady(opaque, onDataReady);
164 return (m_channelProducer !=
nullptr);
167 dwStatus
wait(dwTime_t timeout)
171 throw Exception(DW_NOT_AVAILABLE,
"PortInput: wait: no bound channel");
174 return m_channelProducer->wait(timeout);
181 dwStatus status = DW_FAILURE;
183 if (m_channelProducer)
185 status = m_channelProducer->get(&genericData);
188 if (status != DW_SUCCESS)
193 if (BaseSyncHelper::isDataSynced())
195 return BaseSyncHelper::extractInternalPacket(genericData);
198 return genericData.template getData<T>();
202 virtual dwStatus
send(T* frame)
204 if (!m_channelProducer)
206 throw Exception(DW_NOT_AVAILABLE,
"PortOutput: channel not bound");
209 if (BaseSyncHelper::isDataSynced())
211 return m_channelProducer->send(BaseSyncHelper::getSyncPacket(frame));
214 return m_channelProducer->send(frame);
219 if (!m_channelProducer)
221 throw Exception(DW_NOT_AVAILABLE,
"PortOutput: channel not bound");
223 return m_channelProducer->getSyncSignaler();
228 if (BaseSyncHelper::isDataSynced())
230 m_channelProducer->getSyncSignaler().setSignalFences(BaseSyncHelper::getSyncPacket(frame), fences);
234 m_channelProducer->getSyncSignaler().setSignalFences(frame, fences);
240 if (!m_channelProducer)
242 throw Exception(DW_NOT_AVAILABLE,
"PortOutput: channel not bound");
244 return m_channelProducer->getSyncWaiter();
249 if (BaseSyncHelper::isDataSynced())
251 m_channelProducer->getSyncWaiter().getWaitFences(BaseSyncHelper::getSyncPacket(frame), fences);
255 m_channelProducer->getSyncWaiter().getWaitFences(frame, fences);
260 template <
typename T>
271 template <
typename T>
274 static constexpr
char LOG_TAG[] =
"PortInput";
287 : m_reference(std::move(ref))
297 : m_waiterAttrs(waiterAttrs)
298 , m_signalerAttrs(signalerAttrs)
306 , m_waiterAttrs(waiterAttrs)
307 , m_signalerAttrs(signalerAttrs)
321 FRWK_LOGE <<
"PortInput: bindChannel: attempted to bind the same port twice, ignoring this bind!" << Logger::State::endl;
324 if (channel ==
nullptr)
326 throw Exception(DW_INVALID_ARGUMENT,
"PortInput: bindChannel: expected channel != nullptr");
329 BaseSyncHelper::parseDataSynced(channel->
getParams());
330 auto ref = make_specimen<T>(
nullptr);
334 ref = make_specimen<T>(&m_reference.value());
337 if (BaseSyncHelper::isDataSynced())
339 ref.packetTypeID = BaseSyncHelper::getNewPacketID(ref.packetTypeID);
342 ref.setWaiterAttributes = m_waiterAttrs;
343 ref.setSignalerAttributes = m_signalerAttrs;
346 if (m_channelConsumer ==
nullptr)
348 throw Exception(DW_INTERNAL_ERROR,
"PortInput bindChannel: wrong channel implementations returned.");
356 return !(m_channelConsumer ==
nullptr);
363 throw Exception(DW_NOT_AVAILABLE,
"PortInput: setOnDataReady: no bound channel");
365 m_channelConsumer->setOnDataReady(opaque, onDataReady);
369 dwStatus
wait(dwTime_t timeout)
373 throw Exception(DW_NOT_AVAILABLE,
"PortInput: wait: no bound channel");
379 if (BaseSyncHelper::isValidPacketBuffered())
383 else if (BaseSyncHelper::isPacketBuffered())
385 return DW_NOT_AVAILABLE;
387 else if (BaseSyncHelper::isDataSynced())
392 dwTime_t waitTime = m_last ? 0 : timeout;
393 dwStatus status = m_channelConsumer->wait(waitTime);
394 if (m_last && (status == DW_TIME_OUT || status == DW_NOT_AVAILABLE))
403 virtual std::shared_ptr<T>
recv()
406 std::shared_ptr<T> result;
412 T* typedData =
nullptr;
413 void* releasePtr =
nullptr;
415 if (BaseSyncHelper::isValidPacketBuffered())
418 data = BaseSyncHelper::getBufferedPacket();
419 releasePtr = data.getPointer();
420 typedData = BaseSyncHelper::extractInternalPacket(data);
422 else if (BaseSyncHelper::isPacketBuffered())
429 dwStatus status = m_channelConsumer->recv(&data);
430 if (status != DW_SUCCESS)
432 if (m_last !=
nullptr)
441 releasePtr = data.getPointer();
443 if (BaseSyncHelper::isDataSynced())
445 typedData = BaseSyncHelper::extractInternalPacket(data);
453 typedData = data.template getData<T>();
458 auto* channelConsumer = m_channelConsumer;
459 result = std::shared_ptr<T>(typedData, [channelConsumer, releasePtr](T*) {
460 channelConsumer->release(releasePtr);
472 if (!m_channelConsumer)
474 throw Exception(DW_NOT_AVAILABLE,
"PortInput: channel not bound");
476 return m_channelConsumer->getSyncSignaler();
481 if (BaseSyncHelper::isDataSynced())
483 throw Exception(DW_NOT_SUPPORTED,
"PortInput: not supported");
487 m_channelConsumer->getSyncSignaler().setSignalFences(frame, fences);
493 if (!m_channelConsumer)
495 throw Exception(DW_NOT_AVAILABLE,
"PortInput: channel not bound");
497 return m_channelConsumer->getSyncWaiter();
502 if (BaseSyncHelper::isDataSynced())
504 throw Exception(DW_NOT_SUPPORTED,
"PortInput: not supported");
508 m_channelConsumer->getSyncWaiter().getWaitFences(frame, fences);
515 std::shared_ptr<T> m_last{};
516 dw::core::Optional<SpecimenT> m_reference{};
521 template <
typename T>
527 #endif // DW_FRAMEWORK_PORT_H_
dwStatus wait(dwTime_t timeout)
OnSetSyncAttrs setSignalerAttributes
PortOutput mimics an Output Block.
virtual dwStatus send(T *frame)
virtual Producer * getProducer(GenericDataReference ref)=0
Register a producer client.
void setOnDataReady(void *opaque, IChannelPacketFactory::OnDataReady onDataReady)
static dwStatus guard(TryBlock tryBlock)
Same as previous guard but with a simpler tryBlock with signature 'void tryBlock()' Always returns DW...
ChannelObject::SyncSignaler & getSyncSignaler()
Child interface to consume packets on the channel.
virtual Consumer * getConsumer(GenericDataReference ref)=0
Register a consumer client.
PortOutput(SpecimenT const &ref, OnSetSyncAttrs signalerAttrs, OnSetSyncAttrs waiterAttrs={})
dwStatus bindChannel(ChannelObject *channel) override
void setSignalFences(T *frame, dw::core::span< NvSciSyncFence > fences)
typename parameter_traits< T >::SpecimenT SpecimenT
PortOutput(SpecimenT &&ref)
dwStatus bindChannelWithReference(ChannelObject *channel, GenericDataReference &ref)
dw::core::Function< void(NvSciSyncAttrList)> OnSetSyncAttrs
Function signature to call back to the application to set the sync attributes when needed...
void getWaitFences(T *frame, dw::core::span< NvSciSyncFence > fences)
bool getReuseEnabled() const
dw::core::Function< void()> OnDataReady
ChannelPacketTypeID packetTypeID
virtual const ChannelParams & getParams() const =0
Get the parameters for this channel.
virtual ~PortBase()=default
Child interface to produce packets on the channel.
OnSetSyncAttrs setWaiterAttributes
ChannelObject::SyncWaiter & getSyncWaiter()
PortOutput(SpecimenT const &ref)
virtual dwStatus initialize()