@@ -85,8 +85,10 @@ void RecorderManager::CreateVideoRecorder(std::shared_ptr<VideoCapturer> capture
8585 fps = capturer->fps ();
8686 width = capturer->width ();
8787 height = capturer->height ();
88- video_recorder = ([capturer]() -> std::unique_ptr<VideoRecorder> {
89- if (capturer->format () == V4L2_PIX_FMT_H264) {
88+ video_recorder = ([this , capturer]() -> std::unique_ptr<VideoRecorder> {
89+ if (config.record_mode == RecordMode::Snapshot) {
90+ return nullptr ;
91+ } else if (capturer->format () == V4L2_PIX_FMT_H264) {
9092 return RawH264Recorder::Create (capturer->config ());
9193 } else {
9294 return H264Recorder::Create (capturer->config ());
@@ -95,8 +97,12 @@ void RecorderManager::CreateVideoRecorder(std::shared_ptr<VideoCapturer> capture
9597}
9698
9799void RecorderManager::CreateAudioRecorder (std::shared_ptr<PaCapturer> capturer) {
98- audio_recorder = ([capturer]() -> std::unique_ptr<AudioRecorder> {
99- return AudioRecorder::Create (capturer->config ());
100+ audio_recorder = ([this , capturer]() -> std::unique_ptr<AudioRecorder> {
101+ if (config.record_mode == RecordMode::Snapshot) {
102+ return nullptr ;
103+ } else {
104+ return AudioRecorder::Create (capturer->config ());
105+ }
100106 })();
101107}
102108
@@ -126,20 +132,27 @@ void RecorderManager::SubscribeVideoSource(std::shared_ptr<VideoCapturer> video_
126132
127133 if (has_first_keyframe && video_recorder) {
128134 video_recorder->OnBuffer (buffer);
129- elapsed_time_ = (buffer->timestamp ().tv_sec - last_created_time_.tv_sec ) +
130- (buffer->timestamp ().tv_usec - last_created_time_.tv_usec ) / 1000000.0 ;
131135 }
132- });
133136
134- video_recorder-> OnPacketed ([ this ](AVPacket *pkt) {
135- this -> WriteIntoFile (pkt) ;
137+ elapsed_time_ = (buffer-> timestamp (). tv_sec - last_created_time_. tv_sec ) +
138+ (buffer-> timestamp (). tv_usec - last_created_time_. tv_usec ) / 1000000.0 ;
136139 });
140+
141+ if (video_recorder) {
142+ video_recorder->OnPacketed ([this ](AVPacket *pkt) {
143+ this ->WriteIntoFile (pkt);
144+ });
145+ }
137146}
138147
139148void RecorderManager::SubscribeAudioSource (std::shared_ptr<PaCapturer> audio_src) {
149+ if (!audio_recorder) {
150+ return ;
151+ }
152+
140153 audio_observer = audio_src->AsObservable ();
141154 audio_observer->Subscribe ([this ](PaBuffer buffer) {
142- if (has_first_keyframe && audio_recorder ) {
155+ if (has_first_keyframe) {
143156 audio_recorder->OnBuffer (buffer);
144157 }
145158 });
@@ -171,7 +184,7 @@ void RecorderManager::Start() {
171184 auto folder = new_file.GetFolderPath ();
172185 Utils::CreateFolder (folder);
173186
174- {
187+ if (config. record_mode != RecordMode::Snapshot) {
175188 std::lock_guard<std::mutex> lock (ctx_mux);
176189 fmt_ctx = RecUtil::CreateContainer (new_file.GetFullPath ());
177190 if (fmt_ctx == nullptr ) {
@@ -198,7 +211,10 @@ void RecorderManager::Start() {
198211 audio_recorder->Start ();
199212 }
200213
201- MakePreviewImage ();
214+ if (config.record_mode != RecordMode::Video) {
215+ auto image_path = ReplaceExtension (new_file.GetFullPath (), PREVIEW_IMAGE_EXTENSION);
216+ MakePreviewImage (image_path);
217+ }
202218
203219 has_first_keyframe = true ;
204220}
@@ -228,15 +244,14 @@ RecorderManager::~RecorderManager() {
228244 audio_observer.reset ();
229245}
230246
231- void RecorderManager::MakePreviewImage () {
232- std::thread ([this ]() {
247+ void RecorderManager::MakePreviewImage (std::string path ) {
248+ std::thread ([this , path ]() {
233249 std::this_thread::sleep_for (std::chrono::seconds (3 ));
234250 if (video_src_ == nullptr ) {
235251 return ;
236252 }
237253 auto i420buff = video_src_->GetI420Frame ();
238- Utils::CreateJpegImage (i420buff->DataY (), i420buff->width (), i420buff->height (),
239- ReplaceExtension (fmt_ctx->url , PREVIEW_IMAGE_EXTENSION),
254+ Utils::CreateJpegImage (i420buff->DataY (), i420buff->width (), i420buff->height (), path,
240255 config.jpeg_quality );
241256 }).detach ();
242257}
0 commit comments