Skip to content

Commit 762ba5e

Browse files
committed
[*] update normally
1 parent d2d4747 commit 762ba5e

File tree

13 files changed

+177
-28
lines changed

13 files changed

+177
-28
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ desktop-debug-winit:
3636
SLINT_BACKEND=winit-femtovg $(desktop-build-env) $(run-env) cargo run --bin ${app-name} --no-default-features --features=desktop
3737

3838
desktop-run-release:
39-
$(desktop-build-env) $(run-env) cargo run --release --bin ${app-name} --no-default-features --features=desktop
39+
$(desktop-build-env) RUST_LOG=info cargo run --release --bin ${app-name} --no-default-features --features=desktop
4040

4141

4242
web-build:

lib/recorder/examples/fps_demo.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use fps::SimpleFpsCounter;
2+
3+
fn main() {
4+
let mut fps_counter = SimpleFpsCounter::new();
5+
6+
for _ in 0..100 {
7+
let fps = fps_counter.add_frame(Instant::now());
8+
println!("Current FPS: {:.2}", fps);
9+
thread::sleep(Duration::from_millis(20));
10+
}
11+
}

lib/recorder/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub use mp4_builtin::combine_tracks;
8888
mod mp4_ffmpeg;
8989

9090
#[cfg(feature = "mp4-ffmpeg")]
91-
pub use mp4_ffmpeg::{combine_tracks, is_installed};
91+
pub use mp4_ffmpeg::{combine_tracks, is_ffmpeg_installed};
9292

9393
pub use audio_level::*;
9494
pub use crossbeam::channel::{Receiver, Sender, bounded};
@@ -99,7 +99,7 @@ pub use record_audio::{
9999
};
100100
pub use record_speaker::{SpeakerError, SpeakerRecorder};
101101
pub use recorder::RecordingSession;
102-
pub use recorder_config::{FPS, RecorderConfig};
102+
pub use recorder_config::{FPS, RecorderConfig, SimpleFpsCounter};
103103
pub use recorder_error::RecorderError;
104104
pub use resolution::Resolution;
105105
pub use video_encoder::{EncodedFrame, VideoEncoder};
@@ -175,6 +175,7 @@ pub struct Frame {
175175

176176
#[derive(Debug, Clone)]
177177
pub struct StatsUser {
178+
pub fps: f32,
178179
pub total_frames: u64,
179180
pub loss_frames: u64,
180181
}

lib/recorder/src/mp4_ffmpeg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::sync::atomic::Ordering;
2626
/// println!("FFmpeg is not available, using built-in MP4 writer");
2727
/// }
2828
/// ```
29-
pub fn is_installed() -> bool {
29+
pub fn is_ffmpeg_installed() -> bool {
3030
ffmpeg_sidecar::command::ffmpeg_is_installed()
3131
}
3232

lib/recorder/src/recorder.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::{
22
AudioError, AudioRecorder, CombineTracksConfig, EncodedFrame, Frame, FrameUser, H264Writer,
33
MAX_H264_WRITER_QUEUE_SIZE, ProgressState, RecorderConfig, RecorderError, Resolution,
4-
SpeakerRecorder, StatsUser, StreamingAudioRecorder, VideoEncoder, combine_tracks,
4+
SimpleFpsCounter, SpeakerRecorder, StatsUser, StreamingAudioRecorder, VideoEncoder,
5+
combine_tracks,
56
};
67
use capture::{Capture, CaptureIterConfig, capture_output_iter};
78
use crossbeam::channel::{Receiver, Sender, bounded};
@@ -82,7 +83,9 @@ static CAPTURE_MEAN_TIME: Lazy<Mutex<Option<Duration>>> = Lazy::new(|| Mutex::ne
8283
pub struct RecordingSession {
8384
config: RecorderConfig,
8485

86+
// statistic
8587
start_time: Instant,
88+
fps_counter: SimpleFpsCounter,
8689
total_frame_count: u64,
8790
loss_frame_count: Arc<AtomicU64>,
8891

@@ -177,9 +180,10 @@ impl RecordingSession {
177180
Self {
178181
config,
179182

183+
start_time: std::time::Instant::now(),
184+
fps_counter: Default::default(),
180185
total_frame_count: 0,
181186
loss_frame_count: Arc::new(AtomicU64::new(0)),
182-
start_time: std::time::Instant::now(),
183187

184188
frame_sender: Some(Arc::new(frame_sender)),
185189
frame_receiver,
@@ -359,6 +363,7 @@ impl RecordingSession {
359363
if let Some(ref sender) = self.frame_sender_user {
360364
let frame_user = FrameUser {
361365
stats: StatsUser {
366+
fps: self.fps_counter.add_frame(frame.timestamp),
362367
total_frames: self.total_frame_count,
363368
loss_frames: self.loss_frame_count.load(Ordering::Relaxed),
364369
},
@@ -663,7 +668,7 @@ impl RecordingSession {
663668
Ok((total_frame_index, frame)) => {
664669
let frame_timestamp = frame.timestamp.duration_since(start_time);
665670
log::debug!(
666-
"total frame[{}] thread[{}] thread_frame[{}] capture time: {:.2?}. fps: {:.2}. timestamp: {:.2?}. chanenel remained: {}",
671+
"total frame[{}] thread[{}] thread_frame[{}] capture time: {:.2?}. thread_fps: {:.2}. timestamp: {:.2?}. chanenel remained: {}",
667672
total_frame_index,
668673
frame.thread_id,
669674
frame.cb_data.frame_index,
@@ -859,11 +864,12 @@ impl RecordingSession {
859864
}
860865

861866
log::info!(
862-
"Total frame: {}. loss frame: {} ({:.2}%)",
867+
"Total frame: {}. loss frame: {} ({:.2}%). fps: {:.2}",
863868
self.total_frame_count,
864869
self.loss_frame_count.load(Ordering::Relaxed),
865870
self.loss_frame_count.load(Ordering::Relaxed) as f64 * 100.0
866871
/ self.total_frame_count.max(1) as f64,
872+
self.fps_counter.fps,
867873
);
868874

869875
Ok(())

lib/recorder/src/recorder_config.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use capture::LogicalSize;
33
use chrono::Local;
44
use derive_setters::Setters;
55
use std::{
6+
collections::VecDeque,
67
path::{Path, PathBuf},
78
sync::{Arc, atomic::AtomicI32},
9+
time::{Duration, Instant},
810
};
911

1012
/// Supported frame rates for screen recording.
@@ -276,3 +278,42 @@ impl RecorderConfig {
276278
dir.as_ref().to_path_buf().join(filename)
277279
}
278280
}
281+
282+
#[derive(Debug, Default, Clone)]
283+
pub struct SimpleFpsCounter {
284+
pub fps: f32,
285+
frames: VecDeque<Instant>,
286+
}
287+
288+
impl SimpleFpsCounter {
289+
pub fn new() -> Self {
290+
Self {
291+
frames: VecDeque::new(),
292+
fps: 0.0,
293+
}
294+
}
295+
296+
pub fn add_frame(&mut self, timestamp: Instant) -> f32 {
297+
let three_seconds_ago = timestamp - Duration::from_secs(3);
298+
299+
while let Some(&oldest) = self.frames.front() {
300+
if oldest < three_seconds_ago {
301+
self.frames.pop_front();
302+
} else {
303+
break;
304+
}
305+
}
306+
307+
self.frames.push_back(timestamp);
308+
309+
if self.frames.len() >= 2 {
310+
let time_span = timestamp.duration_since(*self.frames.front().unwrap());
311+
if time_span.as_secs_f64() > 0.0 {
312+
self.fps = (self.frames.len() as f64 / time_span.as_secs_f64()) as f32;
313+
return self.fps;
314+
}
315+
}
316+
317+
0.0
318+
}
319+
}

wayshot/src/config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ pub struct Recorder {
123123
pub remove_temporary_files: bool,
124124

125125
#[derivative(Default(value = "true"))]
126-
pub show_cursor: bool,
126+
pub include_cursor: bool,
127+
128+
#[derivative(Default(value = "true"))]
129+
pub enable_preview: bool,
127130

128131
#[derivative(Default(value = "fps_default()"))]
129132
pub fps: UIFps,

wayshot/src/logic/recorder.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ fn inner_init(ui: &AppWindow) {
9292
store_audio_sources!(ui).set_vec(vec![]);
9393
store_video_sources!(ui).set_vec(vec![]);
9494

95+
if !recorder::is_ffmpeg_installed() {
96+
global_store!(ui).set_ffmpeg_is_installed(false);
97+
}
98+
9599
if let Err(e) = init_input_audio(&ui) {
96100
toast_warn!(ui, format!("{e}"));
97101
}
@@ -265,6 +269,12 @@ fn init_video(ui: &AppWindow) -> Result<()> {
265269
name: name.clone(),
266270
});
267271

272+
tokio::spawn(async move {
273+
if let Err(e) = RecordingSession::init(name.as_str()) {
274+
log::warn!("RecordingSession::init failed in `init_video`: {e}");
275+
}
276+
});
277+
268278
Ok(())
269279
}
270280

@@ -410,9 +420,9 @@ fn inner_start_recording(ui_weak: Weak<AppWindow>) -> Result<()> {
410420
screen_info.logical_size.clone(),
411421
RecorderConfig::make_filename(&all_config.recorder.save_dir),
412422
)
413-
.with_enable_frame_channel_user(true)
423+
.with_enable_frame_channel_user(all_config.recorder.enable_preview)
414424
.with_enable_recording_speaker(all_config.control.enable_desktop_speaker)
415-
.with_include_cursor(all_config.recorder.show_cursor)
425+
.with_include_cursor(all_config.recorder.include_cursor)
416426
.with_remove_cache_files(all_config.recorder.remove_temporary_files)
417427
.with_audio_device_name(input_audio_name)
418428
.with_audio_amplification(Arc::new(AtomicI32::new(
@@ -462,12 +472,11 @@ fn inner_start_recording(ui_weak: Weak<AppWindow>) -> Result<()> {
462472
let img = slint::Image::from_rgba8(buffer);
463473
global_store!(ui).set_preview_image(img);
464474

465-
// TODO: fps
466475
let mut sinfo = global_store!(ui).get_stats_info();
476+
sinfo.fps = frame.stats.fps;
467477
sinfo.total = frame.stats.total_frames as i32;
468-
sinfo.loss = (frame.stats.loss_frames as f64 * 100.0
469-
/ frame.stats.total_frames.max(1) as f64)
470-
as f32;
478+
sinfo.loss =
479+
frame.stats.loss_frames as f32 / frame.stats.total_frames.max(1) as f32;
471480
global_store!(ui).set_stats_info(sinfo);
472481
});
473482
}

wayshot/ui/base/icon.slint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export global Icons {
236236
out property <image> monitor-recorder-light: @image-url("../images/icons/monitor-recorder-light.svg");
237237
out property <image> clear-fill: @image-url("../images/icons/clear-fill.svg");
238238
out property <image> cursor-light: @image-url("../images/icons/cursor-light.svg");
239+
out property <image> preview-light: @image-url("../images/icons/preview-light.svg");
239240

240241
out property <image> landing-account: @image-url("../images/landing/landing-account.svg");
241242
out property <image> landing-language-switch: @image-url("../images/landing/landing-language-switch.svg");
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)