mp4recorder.h
2.0 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
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef MP4RECORDER_H
#define MP4RECORDER_H
#include <string>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <opencv2/opencv.hpp>
#include "av_writer.h"
#include "audio_recorder.h"
class MP4Recorder {
public:
MP4Recorder();
~MP4Recorder();
bool startRecording(const std::string& outputPath, int width, int height, int fps = 30, bool enableAudio = true);
bool stopRecording();
bool isRecording() const { return recording_; }
bool writeFrame(const cv::Mat& frame);
void setFrameSize(int width, int height);
// Audio recording control
bool isAudioEnabled() const { return audio_enabled_; }
// Frame rate monitoring
float getCurrentFPS() const { return current_fps_; }
private:
void writerThread();
void audioThread();
void cleanup();
void updateFrameRate();
void writeFrameWithDuplication(const cv::Mat& frame);
std::string output_path_;
bool recording_;
bool should_stop_;
bool audio_enabled_;
int frame_width_;
int frame_height_;
int target_fps_;
float current_fps_;
// Frame rate monitoring
std::chrono::steady_clock::time_point last_frame_time_;
std::chrono::steady_clock::time_point recording_start_time_;
int frame_count_;
float fps_smoothing_factor_;
// Fast FPS detection for initial frames
std::vector<std::chrono::steady_clock::time_point> recent_frame_times_;
int max_recent_frames_;
bool use_fast_detection_;
// Frame duplication for low FPS
cv::Mat last_frame_;
bool has_last_frame_;
int min_fps_threshold_;
std::queue<cv::Mat> frame_queue_;
std::mutex queue_mutex_;
std::condition_variable queue_cv_;
std::thread writer_thread_;
// Audio recording
AudioRecorder audio_recorder_;
std::thread audio_thread_;
std::mutex audio_mutex_;
AVWriter av_writer_;
std::mutex writer_mutex_;
};
#endif // MP4RECORDER_H