3 #include <ma_config_board.h>
20 #if MA_INVOKE_ENABLE_RUN_HOOK
21 extern void ma_invoke_pre_hook(
void*);
22 extern void ma_invoke_post_hook(
void*);
30 class Invoke final :
public std::enable_shared_from_this<Invoke> {
33 return shared_from_this();
36 [[nodiscard]]
static std::shared_ptr<Invoke>
create(
const std::vector<std::string>& args,
Transport& transport,
Encoder& encoder,
size_t task_id) {
37 return std::shared_ptr<Invoke>{
new Invoke{args, transport, encoder, task_id}};
41 if (_algorithm !=
nullptr) {
57 _n_times = std::atoi(args[1].c_str());
58 _results_only = std::atoi(args[2].c_str());
63 _transport = &transport;
72 _preprocess_hook_injected =
false;
74 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
75 _buffer =
reinterpret_cast<void*
>(MA_SENSOR_ENCODE_STATIC_BUFFER_ADDR);
84 if (!prepareSensor()) [[unlikely]]
87 if (!prepareModel()) [[unlikely]]
90 if (!prepareAlgorithm()) [[unlikely]]
93 switch (_sensor->getType()) {
96 return static_resource->executor->submit([_this = std::move(
getptr())](
const std::atomic<bool>&) { _this->eventLoopCamera(); });
106 bool prepareSensor() {
108 auto it = std::find_if(sensors.begin(), sensors.end(), [&](
const Sensor* s) { return s->getID() == static_resource->current_sensor_id; });
109 if (it == sensors.end()) {
113 auto* camera =
static_cast<Camera*
>(_sensor);
114 _ret = camera->
startStream(Camera::StreamMode::kRefreshOnReturn);
115 return isEverythingOk();
118 bool prepareModel() {
120 auto it = std::find_if(models.begin(), models.end(), [&](
const ma_model_t& m) { return m.id == static_resource->current_model_id; });
121 if (it == models.end()) {
124 if (it->addr ==
nullptr) {
128 return isEverythingOk();
131 bool prepareAlgorithm() {
132 #if MA_USE_FILESYSTEM
133 _ret =
static_resource->engine->load(
static_cast<const char*
>(_model.addr));
137 if (!isEverythingOk()) {
141 if (_algorithm ==
nullptr) {
144 #if MA_INVOKE_ENABLE_RUN_HOOK
145 _algorithm->setRunDone([](
void*) { ma_invoke_post_hook(
nullptr); });
147 return isEverythingOk();
152 _encoder->write(_sensor, _sensor->currentPresetIdx());
153 std::vector<ma_model_t> model{_model};
154 _encoder->write(model);
155 _encoder->write(_algorithm ?
static_cast<int>(_algorithm->getType()) :
static_cast<int>(
static_resource->current_algorithm_id),
157 _sensor ?
static_cast<int>(_sensor->getType()) : 0,
161 _transport->send(
reinterpret_cast<const char*
>(_encoder->data()), _encoder->size());
165 void eventReply(
int width,
int height) {
168 _encoder->write(
"count", _times);
170 if (!_results_only) {
171 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
172 reinterpret_cast<char*
>(_buffer)[_buffer_size] =
'\0';
173 _encoder->write(
"image",
reinterpret_cast<const char*
>(_buffer), _buffer_size);
175 _encoder->write(
"image", _buffer);
181 auto perf = _algorithm->getPerf();
182 _encoder->write(perf);
184 _event_hook(*_encoder);
186 _transport->send(
reinterpret_cast<const char*
>(_encoder->data()), _encoder->size());
189 void eventLoopCamera() {
190 if ((_n_times >= 0) & (_times++ >= _n_times)) [[unlikely]]
197 auto camera =
static_cast<Camera*
>(_sensor);
203 if (!isEverythingOk()) [[unlikely]]
205 if (!_results_only) {
207 if (!isEverythingOk()) [[unlikely]]
210 buffer_size = 4 * ((frame.size + 2) / 3);
211 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
212 if (buffer_size > MA_SENSOR_ENCODE_STATIC_BUFFER_SIZE) {
213 MA_LOGE(
MA_TAG,
"buffer_size > MA_SENSOR_ENCODE_STATIC_BUFFER_SIZE");
218 if (buffer_size > _buffer.size()) {
219 _buffer.resize(buffer_size + 1);
226 #
if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
227 reinterpret_cast<char*
>(_buffer),
229 reinterpret_cast<char*
>(_buffer.data()),
232 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
233 _buffer_size = buffer_size;
234 static_cast<char*
>(_buffer)[buffer_size] =
'\0';
236 _buffer[buffer_size] =
'\0';
243 camera->returnFrame(frame);
245 if (!_preprocess_hook_injected) {
246 _preprocess_hook_injected =
true;
247 _algorithm->setPreprocessDone([camera, &raw_frame](
void*) {
248 camera->returnFrame(raw_frame);
249 #if MA_INVOKE_ENABLE_RUN_HOOK
250 ma_invoke_pre_hook(
nullptr);
256 _event_hook = [&raw_frame](
Encoder& encoder) {
257 int16_t rotation =
static_cast<int>(raw_frame.rotate) * 90;
258 encoder.write(
"rotation", rotation);
259 encoder.write(
"width", raw_frame.width);
260 encoder.write(
"height", raw_frame.height);
269 if (!isEverythingOk()) [[unlikely]]
276 for (
auto& rule : trigger_copy) {
278 (*rule.get())(_algorithm);
283 eventReply(raw_frame.width, raw_frame.height);
285 static_resource->executor->submit([_this = std::move(
getptr())](
const std::atomic<bool>&) { _this->eventLoopCamera(); });
289 eventReply(raw_frame.width, raw_frame.height);
292 inline bool isEverythingOk()
const {
293 return _ret ==
MA_OK;
312 bool _preprocess_hook_injected;
313 std::function<void(
Encoder&)> _event_hook;
315 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
316 #ifndef MA_SENSOR_ENCODE_STATIC_BUFFER_ADDR
317 #error "MA_SENSOR_ENCODE_STATIC_BUFFER_ADDR is not defined"
319 #ifndef MA_SENSOR_ENCODE_STATIC_BUFFER_SIZE
320 #error "MA_SENSOR_ENCODE_STATIC_BUFFER_SIZE is not defined"
Definition: ma_camera.h:13
virtual ma_err_t startStream(StreamMode mode) noexcept=0
Definition: ma_codec_base.h:14
static Model * create(Engine *engine, size_t algorithm_id=0)
Definition: ma_model_factory.cpp:8
static ma_err_t remove(Model *model)
Definition: ma_model_factory.cpp:71
Definition: ma_model_base.h:14
Definition: ma_sensor.h:12
Definition: ma_transport.h:12
Definition: invoke.hpp:30
Invoke(const std::vector< std::string > &args, Transport &transport, Encoder &encoder, size_t task_id)
Definition: invoke.hpp:54
std::shared_ptr< Invoke > getptr()
Definition: invoke.hpp:32
~Invoke()
Definition: invoke.hpp:40
void run()
Definition: invoke.hpp:49
static std::shared_ptr< Invoke > create(const std::vector< std::string > &args, Transport &transport, Encoder &encoder, size_t task_id)
Definition: invoke.hpp:36
#define MA_LOGD(TAG,...)
Definition: ma_debug.h:79
#define MA_LOGE(TAG,...)
Definition: ma_debug.h:30
#define MA_ASSERT(expr)
Definition: ma_debug.h:119
#define MA_TAG
Definition: ma_debug.h:9
@ MA_MSG_TYPE_RESP
Definition: ma_types.h:252
@ MA_MSG_TYPE_EVT
Definition: ma_types.h:252
ma_err_t
Definition: ma_types.h:21
@ MA_ENOENT
Definition: ma_types.h:33
@ MA_ENOTSUP
Definition: ma_types.h:31
@ MA_OK
Definition: ma_types.h:23
@ MA_MODEL_CFG_OPT_NMS
Definition: ma_types.h:231
@ MA_MODEL_CFG_OPT_THRESHOLD
Definition: ma_types.h:230
@ MA_PIXEL_FORMAT_JPEG
Definition: ma_types.h:109
@ MA_PIXEL_FORMAT_AUTO
Definition: ma_types.h:104
Definition: algorithm.hpp:11
ma_err_t base64_encode(const unsigned char *in, int in_len, char *out, int *out_len)
Definition: ma_base64.cpp:41
ma_err_t serializeAlgorithmOutput(Model *algorithm, Encoder *encoder, int width, int height)
Definition: refactor_required.hpp:69
ma_err_t setAlgorithmInput(Model *algorithm, ma_img_t &img)
Definition: refactor_required.hpp:42
std::forward_list< std::shared_ptr< TriggerRule > > trigger_rules
Definition: refactor_required.hpp:296
ma::Mutex trigger_rules_mutex
Definition: refactor_required.hpp:297
#define static_resource
Definition: resource.hpp:64
Definition: ma_types.h:124
Definition: ma_types.h:289