-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_encoder.hpp
More file actions
65 lines (47 loc) · 1.48 KB
/
frame_encoder.hpp
File metadata and controls
65 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// Created by peng on 2026/1/20.
//
#ifndef GB28181CONSOLE_FRAME_ENCODER_HPP
#define GB28181CONSOLE_FRAME_ENCODER_HPP
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <functional>
#include <opencv2/core/mat.hpp>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
class FrameEncoder {
public:
using H264DataCallback = std::function<void(const std::vector<uint8_t>&)>;
explicit FrameEncoder(size_t bufferSize = 3);
void setH264DataCallback(const H264DataCallback& callback);
// 生产者:快速写入
void pushFrame(const cv::Mat& frame);
void start();
void stop();
~FrameEncoder();
private:
struct FrameBuffer {
std::vector<cv::Mat> frames; // 预分配的帧缓冲区
size_t writeIndex = 0; // 写入位置
size_t readIndex = 0; // 读取位置
size_t count = 0; // 当前帧数
size_t capacity; // 容量
} _ringBuffer;
AVCodecContext* _codec_ctx_ptr = nullptr;
AVFrame* _frame_ptr = nullptr;
AVPacket* _packet_ptr = nullptr;
SwsContext* _sws_ctx_ptr = nullptr;
// 编码相关
std::thread* _encode_thread_ptr = nullptr;
std::mutex _mutex;
std::atomic<bool> _is_running{false};
std::condition_variable _encode_cv;
void encode_loop();
void encode_frame(const cv::Mat& frame) const;
H264DataCallback _h264_callback;
};
#endif //GB28181CONSOLE_FRAME_ENCODER_HPP