MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
CameraReader.cpp
Go to the documentation of this file.
1#include "CameraReader.hpp"
2
4
7
9
10extern "C" {
11#include <libavcodec/avcodec.h>
12#include <libavdevice/avdevice.h>
13#include <libavformat/avformat.h>
14#include <libavutil/dict.h>
15#include <libavutil/frame.h>
16#include <libavutil/imgutils.h>
17#include <libswscale/swscale.h>
18}
19
20namespace MayaFlux::IO {
21
22// std::once_flag CameraReader::s_avdevice_init;
23
25 : m_demux(std::make_shared<FFmpegDemuxContext>())
26 , m_video(std::make_shared<VideoStreamContext>())
27{
28}
29
30void CameraReader::setup_io_service(uint64_t reader_id)
31{
32 m_standalone_reader_id = reader_id;
33
35 .get_service<Registry::Service::IOService>()) {
36
37 m_standalone_service = std::make_shared<Registry::Service::IOService>();
38 m_standalone_service->request_frame = [this](uint64_t reader_id) {
39 if (reader_id == m_standalone_reader_id)
41 };
42
45 [this]() -> void* { return m_standalone_service.get(); });
46
47 m_owns_service = true;
48 }
49}
50
60
62{
63 close();
64
65 const std::string fmt_name = config.format_override.empty()
66 ? std::string(CAMERA_FORMAT)
67 : config.format_override;
68
69 AVDictionary* opts = nullptr;
70
71 std::string fps_str = std::to_string(static_cast<int>(config.target_fps));
72 av_dict_set(&opts, "framerate", fps_str.c_str(), 0);
73
74 std::string size_str = std::to_string(config.target_width)
75 + "x"
76 + std::to_string(config.target_height);
77
78 av_dict_set(&opts, "video_size", size_str.c_str(), 0);
79
80 if (!m_demux->open_device(config.device_name, fmt_name, &opts)) {
81 m_last_error = "Device open failed: " + m_demux->last_error();
83 "CameraReader::open — {}", m_last_error);
84 return false;
85 }
86
87 if (!m_video->open_device(*m_demux,
88 config.target_width,
89 config.target_height)) {
90 m_last_error = "Video stream open failed: " + m_video->last_error();
92 "CameraReader::open — {}", m_last_error);
93 m_demux->close();
94 return false;
95 }
96
97 m_sws_buf.clear();
98 m_scaler_ready = false;
99
101 "CameraReader: opened '{}' via {} — {}x{} @{:.1f}fps (scaler deferred)",
102 config.device_name, fmt_name,
103 m_video->width, m_video->height, m_video->frame_rate);
104
105 return true;
106}
107
109{
111
112 std::unique_lock lock(m_ctx_mutex);
113 m_video->close();
114 m_demux->close();
115 m_scaler_ready = false;
116 m_sws_buf.clear();
117 m_sws_buf.shrink_to_fit();
118 m_last_error.clear();
119}
120
122{
123 std::shared_lock lock(m_ctx_mutex);
124 return m_demux->is_open() && m_video->is_codec_valid();
125}
126
127std::shared_ptr<Kakshya::CameraContainer>
129{
130 std::shared_lock lock(m_ctx_mutex);
131 if (!m_video->is_codec_valid()) {
132 m_last_error = "Cannot create container: reader not open";
133 return nullptr;
134 }
135
136 return std::make_shared<Kakshya::CameraContainer>(
137 m_video->out_width,
138 m_video->out_height,
139 4,
140 m_video->frame_rate);
141}
142
144 const std::shared_ptr<Kakshya::CameraContainer>& container)
145{
146 if (!container)
147 return false;
148
149 std::shared_lock lock(m_ctx_mutex);
150 if (!m_video->is_codec_valid())
151 return false;
152
153 uint8_t* dest = container->mutable_frame_ptr();
154 if (!dest)
155 return false;
156
157 AVPacket* pkt = av_packet_alloc();
158 if (!pkt)
159 return false;
160
161 AVFrame* frame = av_frame_alloc();
162 if (!frame) {
163 av_packet_free(&pkt);
164 return false;
165 }
166
167 bool got_frame = false;
168
169 while (!got_frame) {
170 int ret = av_read_frame(m_demux->format_context, pkt);
171 if (ret < 0)
172 break;
173
174 if (pkt->stream_index != m_video->stream_index) {
175 av_packet_unref(pkt);
176 continue;
177 }
178
179 ret = avcodec_send_packet(m_video->codec_context, pkt);
180 av_packet_unref(pkt);
181
182 if (ret < 0 && ret != AVERROR(EAGAIN))
183 break;
184
185 ret = avcodec_receive_frame(m_video->codec_context, frame);
186 if (ret == AVERROR(EAGAIN))
187 continue;
188 if (ret < 0)
189 break;
190
191 if (!m_scaler_ready) {
192 if (!m_video->rebuild_scaler_from_frame(
193 frame,
194 static_cast<uint32_t>(frame->width),
195 static_cast<uint32_t>(frame->height))) {
197 "CameraReader: scaler init failed: {}",
198 m_video->last_error());
199 break;
200 }
201 const size_t buf_bytes = static_cast<size_t>(m_video->out_linesize) * m_video->out_height;
202 m_sws_buf.assign(buf_bytes, 0);
203 m_scaler_ready = true;
204 }
205
206 uint8_t* sws_dst[1] = { m_sws_buf.data() };
207 int sws_stride[1] = { m_video->out_linesize };
208
209 sws_scale(m_video->sws_context,
210 frame->data, frame->linesize,
211 0, static_cast<int>(m_video->height),
212 sws_dst, sws_stride);
213
214 const int packed_stride = static_cast<int>(m_video->out_width * m_video->out_bytes_per_pixel);
215
216 if (m_video->out_linesize == packed_stride) {
217 std::memcpy(dest, m_sws_buf.data(),
218 static_cast<size_t>(packed_stride) * m_video->out_height);
219 } else {
220 for (uint32_t row = 0; row < m_video->out_height; ++row) {
221 std::memcpy(
222 dest + static_cast<size_t>(row) * packed_stride,
223 m_sws_buf.data() + static_cast<size_t>(row) * m_video->out_linesize,
224 static_cast<size_t>(packed_stride));
225 }
226 }
227
228 container->mark_ready_for_processing(true);
229 got_frame = true;
230 }
231
232 av_frame_free(&frame);
233 av_packet_free(&pkt);
234
235 return got_frame;
236}
237
238uint32_t CameraReader::width() const
239{
240 std::shared_lock lock(m_ctx_mutex);
241 return m_video->out_width;
242}
243
244uint32_t CameraReader::height() const
245{
246 std::shared_lock lock(m_ctx_mutex);
247 return m_video->out_height;
248}
249
251{
252 std::shared_lock lock(m_ctx_mutex);
253 return m_video->frame_rate;
254}
255
257 const std::shared_ptr<Kakshya::CameraContainer>& container)
258{
259 m_container_ref = container;
261}
262
264{
265 {
266 std::lock_guard lock(m_decode_mutex);
267 m_frame_requested.store(true, std::memory_order_relaxed);
268 }
269 m_decode_cv.notify_one();
270}
271
272const std::string& CameraReader::last_error() const
273{
274 return m_last_error;
275}
276
278{
280
281 m_decode_stop.store(false);
282 m_decode_active.store(true);
284}
285
287{
288 if (!m_decode_active.load())
289 return;
290
291 m_decode_stop.store(true);
292 m_decode_cv.notify_all();
293
294 if (m_decode_thread.joinable())
295 m_decode_thread.join();
296
297 m_decode_active.store(false);
298}
299
301{
302 while (!m_decode_stop.load()) {
303 {
304 std::unique_lock lock(m_decode_mutex);
305 m_decode_cv.wait(lock, [this] {
306 return m_decode_stop.load()
307 || m_frame_requested.load(std::memory_order_relaxed);
308 });
309 }
310
311 if (m_decode_stop.load())
312 break;
313
314 m_frame_requested.store(false, std::memory_order_relaxed);
315
316 auto container = m_container_ref.lock();
317 if (container)
318 pull_frame(container);
319 }
320
321 m_decode_active.store(false);
322}
323
324} // namespace MayaFlux::IO
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
std::atomic< bool > m_decode_stop
uint32_t height() const
Negotiated output height in pixels.
std::atomic< bool > m_decode_active
void setup_io_service(uint64_t reader_id)
Setup an IOService for this reader with the given reader_id.
void set_container(const std::shared_ptr< Kakshya::CameraContainer > &container)
Store a weak reference to the container for IOService dispatch.
std::atomic< bool > m_frame_requested
const std::string & last_error() const
Last error string, empty if no error.
std::shared_ptr< Registry::Service::IOService > m_standalone_service
std::vector< uint8_t > m_sws_buf
std::shared_mutex m_ctx_mutex
void close()
Release codec, demux, and scratch buffer resources.
bool is_open() const
True if the device is open and codec is ready.
std::weak_ptr< Kakshya::CameraContainer > m_container_ref
std::shared_ptr< VideoStreamContext > m_video
bool open(const CameraConfig &config)
Open a camera device using the supplied config.
std::condition_variable m_decode_cv
std::shared_ptr< FFmpegDemuxContext > m_demux
void pull_frame_all()
Signal the background decode thread to pull one frame.
bool pull_frame(const std::shared_ptr< Kakshya::CameraContainer > &container)
Decode one frame from the device into the container's m_data[0].
double frame_rate() const
Negotiated frame rate in fps.
std::shared_ptr< Kakshya::CameraContainer > create_container() const
Create a CameraContainer sized to the negotiated device resolution.
uint32_t width() const
Negotiated output width in pixels.
RAII owner of a single AVFormatContext and associated demux state.
RAII owner of one video stream's codec and pixel-format scaler state.
void register_service(ServiceFactory factory)
Register a backend service capability.
static BackendRegistry & instance()
Get the global registry instance.
void unregister_service()
Unregister a service.
@ FileIO
Filesystem I/O operations.
@ Runtime
General runtime operations (default fallback)
@ IO
Networking, file handling, streaming.
std::string device_name
Platform device string.
std::string format_override
Leave empty to use CAMERA_FORMAT for current platform.
uint32_t target_width
Requested width in pixels.
uint32_t target_height
Requested height in pixels.
double target_fps
Hint only; device may ignore.
Platform-specific FFmpeg input format string for camera devices.
Backend IO streaming service interface.
Definition IOService.hpp:18