--- src/bin/vrec-daemon.rs +++ src/bin/vrec-daemon.rs @@ -62,6 +62,8 @@ let mut config = vrec::config::VrecConfig::load(); let mut ring = HeapRb::::new((config.replay_duration_sec * 60).max(60) as usize); let mut normal_muxer: Option = None; + let mut normal_recording = false; + let mut normal_waiting_keyframe = false; thread::spawn(move || { while let Ok(drain) = mux_rx.recv() { @@ -108,16 +110,14 @@ } }, Command::StartRecording => { - if config.record_enabled && normal_muxer.is_none() { - let codec_ctx = codec_ctx_ptr as *mut ffmpeg_next::ffi::AVCodecContext; - let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs(); - let name = format!("record_{}.mp4", ts); - normal_muxer = unsafe { Muxer::new(&name, codec_ctx).ok() }; - println!("Started normal recording to {}", name); + if config.record_enabled && !normal_recording { + normal_recording = true; + normal_waiting_keyframe = true; + println!("StartRecording requested, waiting for keyframe..."); } }, Command::StopRecording => { - if let Some(mut m) = normal_muxer.take() { + if let Some(mut m) = normal_muxer.take() { let _ = m.finalize(); println!("Stopped normal recording."); } + normal_recording = false; }, _ => {} } @@ -129,7 +129,19 @@ ring.push_overwrite(pkt.clone()); } - if let Some(muxer) = normal_muxer.as_mut() { - let _ = muxer.write_packet(&pkt); + if normal_recording { + if normal_waiting_keyframe { + if pkt.is_keyframe() { + normal_waiting_keyframe = false; + let codec_ctx = codec_ctx_ptr as *mut ffmpeg_next::ffi::AVCodecContext; + let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs(); + let name = format!("record_{}.mp4", ts); + normal_muxer = unsafe { Muxer::new(&name, codec_ctx).ok() }; + println!("Started normal recording to {}", name); + } + } + if !normal_waiting_keyframe { + if let Some(muxer) = normal_muxer.as_mut() { + let _ = muxer.write_packet(&pkt); + } + } } }