17 class Sample final :
public std::enable_shared_from_this<Sample> {
20 return shared_from_this();
23 [[nodiscard]]
static std::shared_ptr<Sample>
create(
const std::vector<std::string>& args,
Transport& transport,
Encoder& encoder,
size_t task_id) {
24 return std::shared_ptr<Sample>{
new Sample{args, transport, encoder, task_id}};
39 _n_times = std::atoi(args[1].c_str());
44 _transport = &transport;
51 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
52 _buffer =
reinterpret_cast<void*
>(MA_SENSOR_ENCODE_STATIC_BUFFER_ADDR);
61 if (!prepareSensor()) [[unlikely]]
64 switch (_sensor->getType()) {
67 return static_resource->executor->submit([_this = std::move(
getptr())](
const std::atomic<bool>&) { _this->eventLoopCamera(); });
77 bool prepareSensor() {
79 auto it = std::find_if(sensors.begin(), sensors.end(), [&](
const Sensor* s) { return s->getID() == static_resource->current_sensor_id; });
80 if (it == sensors.end()) {
84 auto* camera =
static_cast<Camera*
>(_sensor);
85 _ret = camera->
startStream(Camera::StreamMode::kRefreshOnReturn);
86 return isEverythingOk();
91 _encoder->write(_sensor, _sensor->currentPresetIdx());
93 _transport->send(
reinterpret_cast<const char*
>(_encoder->data()), _encoder->size());
98 _encoder->write(
"count", _times);
99 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
100 reinterpret_cast<char*
>(_buffer)[_buffer_size] =
'\0';
101 _encoder->write(
"image",
reinterpret_cast<char*
>(_buffer), _buffer_size);
103 _encoder->write(
"image", _buffer);
106 _event_hook(*_encoder);
108 _transport->send(
reinterpret_cast<const char*
>(_encoder->data()), _encoder->size());
111 void eventLoopCamera() {
112 if ((_n_times >= 0) & (_times++ >= _n_times)) [[unlikely]]
117 auto camera =
static_cast<Camera*
>(_sensor);
122 if (!isEverythingOk()) [[unlikely]]
125 buffer_size = 4 * ((frame.size + 2) / 3);
126 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
127 if (buffer_size > MA_SENSOR_ENCODE_STATIC_BUFFER_SIZE) {
128 MA_LOGE(
MA_TAG,
"buffer_size > MA_SENSOR_ENCODE_STATIC_BUFFER_SIZE");
131 _buffer_size = buffer_size;
133 if (buffer_size > _buffer.size()) {
134 _buffer.resize(buffer_size + 1);
141 #
if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
142 reinterpret_cast<char*
>(_buffer),
144 reinterpret_cast<char*
>(_buffer.data()),
147 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
148 static_cast<char*
>(_buffer)[buffer_size] =
'\0';
150 _buffer[buffer_size] =
'\0';
157 camera->returnFrame(frame);
160 _event_hook = [&frame](
Encoder& encoder) {
161 int16_t rotation =
static_cast<int>(frame.rotate) * 90;
162 encoder.write(
"rotation", rotation);
163 encoder.write(
"width", frame.width);
164 encoder.write(
"height", frame.height);
170 static_resource->executor->submit([_this = std::move(
getptr())](
const std::atomic<bool>&) { _this->eventLoopCamera(); });
177 inline bool isEverythingOk()
const {
178 return _ret ==
MA_OK;
194 std::function<void(
Encoder&)> _event_hook;
196 #if MA_SENSOR_ENCODE_USE_STATIC_BUFFER
197 #ifndef MA_SENSOR_ENCODE_STATIC_BUFFER_ADDR
198 #error "MA_SENSOR_ENCODE_STATIC_BUFFER_ADDR is not defined"
200 #ifndef MA_SENSOR_ENCODE_STATIC_BUFFER_SIZE
201 #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
Definition: ma_sensor.h:12
Definition: ma_transport.h:12
Definition: sample.hpp:17
~Sample()
Definition: sample.hpp:27
Sample(const std::vector< std::string > &args, Transport &transport, Encoder &encoder, size_t task_id)
Definition: sample.hpp:36
void run()
Definition: sample.hpp:31
static std::shared_ptr< Sample > create(const std::vector< std::string > &args, Transport &transport, Encoder &encoder, size_t task_id)
Definition: sample.hpp:23
std::shared_ptr< Sample > getptr()
Definition: sample.hpp:19
#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_ENOTSUP
Definition: ma_types.h:31
@ MA_OK
Definition: ma_types.h:23
@ MA_PIXEL_FORMAT_JPEG
Definition: ma_types.h:109
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
#define static_resource
Definition: resource.hpp:64
Definition: ma_types.h:124