17 #ifndef ASTRA_STREAMREADER_HPP
18 #define ASTRA_STREAMREADER_HPP
20 #include "capi/astra_core.h"
21 #include <astra_core/FrameListener.hpp>
22 #include <astra_core/Frame.hpp>
46 : readerRef_(std::make_shared<ReaderRef>(reader))
50 : readerRef_(reader.readerRef_)
55 this->readerRef_ = reader.readerRef_;
62 return stream<T>(DEFAULT_SUBTYPE);
66 T stream(astra_stream_subtype_t subtype)
69 throw std::logic_error(
"StreamReader is not associated with a streamset.");
72 astra_reader_get_stream(readerRef_->get_reader(),
90 throw std::logic_error(
"StreamReader is not associated with a streamset.");
92 readerRef_.get()->add_listener(listener);
104 throw std::logic_error(
"StreamReader is not associated with a streamset.");
106 readerRef_.get()->remove_listener(listener);
109 bool is_valid() {
return readerRef_ !=
nullptr; }
114 throw std::logic_error(
"StreamReader is not associated with a streamset.");
116 bool hasNewFrame =
false;
117 astra_reader_has_new_frame(readerRef_->get_reader(), &hasNewFrame);
121 Frame get_latest_frame(
int timeoutMillis = ASTRA_TIMEOUT_FOREVER)
124 throw std::logic_error(
"StreamReader is not associated with a streamset.");
126 astra_reader_frame_t frame;
127 astra_reader_open_frame(readerRef_->get_reader(), timeoutMillis, &frame);
134 using ReaderRefPtr = std::shared_ptr<ReaderRef>;
136 StreamReader(ReaderRefPtr readerRef)
137 : readerRef_(readerRef)
141 public std::enable_shared_from_this<ReaderRef>
144 ReaderRef(astra_reader_t reader)
151 ensure_callback_removed();
152 astra_reader_destroy(&reader_);
155 static void ASTRA_CALLBACK frame_ready_thunk(
void* clientTag,
156 astra_reader_t reader,
157 astra_reader_frame_t frame)
159 ReaderRef*
self =
static_cast<ReaderRef*
>(clientTag);
160 self->notify_listeners(frame);
165 ensure_callback_added();
167 auto it = std::find(listeners_.begin(),
171 if (it != listeners_.end())
176 addedListeners_.push_back(listener);
180 listeners_.push_back(listener);
186 auto it = std::find(listeners_.begin(),
190 if (it == listeners_.end())
195 removedListeners_.push_back(listener);
199 listeners_.erase(it);
202 if (listeners_.size() == 0)
204 ensure_callback_removed();
208 void notify_listeners(astra_reader_frame_t readerFrame)
210 if (removedListeners_.size() > 0)
212 for(FrameListener& listener : removedListeners_)
214 auto it = std::find(listeners_.begin(),
218 listeners_.erase(it);
220 removedListeners_.clear();
223 std::move(addedListeners_.begin(),
224 addedListeners_.end(),
225 std::back_inserter(listeners_));
227 if (listeners_.size() == 0)
229 ensure_callback_removed();
235 const bool autoCloseFrame =
false;
239 StreamReader reader(shared_from_this());
240 for(FrameListener& listener : listeners_)
242 listener.on_frame_ready(reader, frameWrapper);
245 isNotifying_ =
false;
248 astra_reader_t get_reader() {
return reader_; }
251 void ensure_callback_added()
253 if (!callbackRegistered_)
255 astra_reader_register_frame_ready_callback(reader_,
256 &ReaderRef::frame_ready_thunk,
260 callbackRegistered_ =
true;
264 void ensure_callback_removed()
266 if (callbackRegistered_)
268 astra_reader_unregister_frame_ready_callback(&callbackId_);
269 callbackRegistered_ =
false;
273 astra_reader_t reader_;
275 bool isNotifying_{
false};
276 bool callbackRegistered_{
false};
278 using ListenerList = std::vector<std::reference_wrapper<FrameListener>>;
280 ListenerList listeners_;
281 ListenerList addedListeners_;
282 ListenerList removedListeners_;
287 ReaderRefPtr readerRef_;
289 friend bool operator==(
const StreamReader& lhs,
const StreamReader& rhs);
292 inline bool operator==(
const StreamReader& lhs,
const StreamReader& rhs)
294 return lhs.readerRef_ == rhs.readerRef_;
297 inline bool operator!=(
const StreamReader& lhs,
const StreamReader& rhs)
299 return !(lhs == rhs);
Frame class
Definition: Frame.hpp:32
Frame Listener class
Definition: FrameListener.hpp:31
Stream Reader class
Definition: StreamReader.hpp:39
void add_listener(FrameListener &listener)
add listener
Definition: StreamReader.hpp:87
void remove_listener(FrameListener &listener)
remove listener
Definition: StreamReader.hpp:101
bool operator!=(const ImageStreamMode &lhs, const ImageStreamMode &rhs)
compare is ImageStreamMode not equal
Definition: Image.hpp:247
bool operator==(const ImageStreamMode &lhs, const ImageStreamMode &rhs)
compare is ImageStreamMode equal
Definition: Image.hpp:230
Definition: astra_plugin.h:29
Definition: astra_plugin.h:24