You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.3 KiB

#pragma once
#include "config.h"
#include "frame_queue.h"
#include <string>
#include <thread>
#include <atomic>
#include <vector>
class CameraCapture {
public:
CameraCapture(int cam_id, const std::string& dev_path, const SystemConfig& config);
~CameraCapture();
bool start();
void stop();
bool getFrame(CapturedFrame& out_frame, int timeout_ms = 100);
bool isRunning() const { return running_.load(); }
bool isOpened() const { return v4l2_fd_ >= 0; }
int getCameraId() const { return cam_id_; }
uint64_t getFrameCount() const { return frame_count_.load(); }
private:
void captureLoop();
// V4L2 初始化步骤(拆分为小函数便于调试)
bool openDevice();
bool setFormat();
bool requestBuffers();
bool mmapBuffers();
bool queueAllBuffers();
bool streamOn();
void streamOff();
void cleanup();
int cam_id_;
std::string dev_path_;
SystemConfig config_;
int v4l2_fd_ = -1;
std::thread capture_thread_;
std::atomic<bool> running_{false};
FrameQueue<CapturedFrame> frame_queue_;
struct V4L2Buffer {
void* start = nullptr;
size_t length = 0;
};
std::vector<V4L2Buffer> v4l2_buffers_;
std::atomic<uint64_t> frame_count_{0};
};