64 lines
1.6 KiB
C++
64 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include "action_base.h"
|
|
#include "../uploaders/uploader_base.h"
|
|
#include "../frame_ring_buffer.h"
|
|
|
|
#include <atomic>
|
|
#include <condition_variable>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <queue>
|
|
#include <thread>
|
|
|
|
namespace rk3588 {
|
|
|
|
struct ClipTask {
|
|
AlarmEvent event;
|
|
std::vector<std::shared_ptr<Frame>> pre_frames;
|
|
std::shared_ptr<Frame> trigger_frame;
|
|
};
|
|
|
|
class ClipAction : public IAlarmAction {
|
|
public:
|
|
~ClipAction();
|
|
bool Init(const SimpleJson& config) override;
|
|
void Execute(AlarmEvent& event, std::shared_ptr<Frame> frame) override;
|
|
void Drain() override;
|
|
std::string Name() const override { return "clip"; }
|
|
|
|
void SetFrameBuffer(std::shared_ptr<FrameRingBuffer> buffer) {
|
|
frame_buffer_ = buffer;
|
|
}
|
|
|
|
void PushPostEventFrame(std::shared_ptr<Frame> frame);
|
|
|
|
private:
|
|
void WorkerLoop();
|
|
std::string ProcessClip(ClipTask& task);
|
|
std::string GenerateKey(const AlarmEvent& event);
|
|
|
|
int pre_sec_ = 5;
|
|
int post_sec_ = 10;
|
|
std::string format_ = "mp4";
|
|
int fps_ = 25;
|
|
std::unique_ptr<IUploader> uploader_;
|
|
std::shared_ptr<FrameRingBuffer> frame_buffer_;
|
|
|
|
std::atomic<bool> running_{false};
|
|
std::thread worker_;
|
|
std::mutex queue_mutex_;
|
|
std::condition_variable queue_cv_;
|
|
std::queue<ClipTask> task_queue_;
|
|
size_t in_flight_ = 0;
|
|
|
|
// Post-event collection state
|
|
std::mutex post_mutex_;
|
|
bool collecting_post_ = false;
|
|
std::vector<std::shared_ptr<Frame>> post_frames_;
|
|
int post_frames_needed_ = 0;
|
|
ClipTask* current_task_ = nullptr;
|
|
};
|
|
|
|
} // namespace rk3588
|