Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendonovich committed Nov 7, 2024
1 parent 9454f99 commit 88d0e14
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 87 deletions.
2 changes: 2 additions & 0 deletions src/capturer/engine/mac/apple_sys.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(non_upper_case_globals)]

pub use screencapturekit_sys::os_types::base::*;

#[repr(C)]
Expand Down
103 changes: 42 additions & 61 deletions src/capturer/engine/mac/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::cmp;
use std::sync::mpsc;

use core_video_sys::{CVPixelBufferGetPixelFormatType, CVPixelBufferRef};
use pixelformat::get_pts_in_nanoseconds;
use screencapturekit::{
cm_sample_buffer::CMSampleBuffer,
Expand All @@ -13,20 +12,13 @@ use screencapturekit::{
sc_stream_configuration::{PixelFormat, SCStreamConfiguration},
sc_types::SCFrameStatus,
};
use screencapturekit_sys::os_types::base::{CMTime, CMTimeScale};
use screencapturekit_sys::os_types::geometry::{CGPoint, CGRect, CGSize};
use screencapturekit_sys::{
cm_sample_buffer_ref::CMSampleBufferGetImageBuffer,
os_types::base::{CMTime, CMTimeScale},
};

use crate::frame::{Frame, FrameType};
use crate::targets::Target;
use crate::{
capturer::RawCapturer,
frame::{Frame, FrameType},
};
use crate::{
capturer::{Area, Options, Point, RawCapturer, Resolution, Size},
capturer::{Area, Options, Point, Resolution, Size},
frame::BGRAFrame,
targets,
};
Expand All @@ -46,12 +38,11 @@ impl StreamErrorHandler for ErrorHandler {

pub struct Capturer {
pub tx: mpsc::Sender<ChannelItem>,
pub output_type: FrameType,
}

impl Capturer {
pub fn new(tx: mpsc::Sender<ChannelItem>, output_type: FrameType) -> Self {
Capturer { tx, output_type }
pub fn new(tx: mpsc::Sender<ChannelItem>) -> Self {
Capturer { tx }
}
}

Expand Down Expand Up @@ -99,14 +90,13 @@ pub fn create_capturer(options: &Options, tx: mpsc::Sender<ChannelItem>) -> SCSt
.into_iter()
.filter(|window| {
excluded_targets
.into_iter()
.find(|excluded_target| match excluded_target {
.iter()
.any(|excluded_target| match excluded_target {
Target::Window(excluded_window) => {
excluded_window.id == window.window_id
}
_ => false,
})
.is_some()
})
.collect();

Expand Down Expand Up @@ -156,10 +146,7 @@ pub fn create_capturer(options: &Options, tx: mpsc::Sender<ChannelItem>) -> SCSt
};

let mut stream = SCStream::new(filter, stream_config, ErrorHandler);
stream.add_output(
Capturer::new(tx, options.output_type),
SCStreamOutputType::Screen,
);
stream.add_output(Capturer::new(tx), SCStreamOutputType::Screen);

stream
}
Expand Down Expand Up @@ -217,8 +204,8 @@ pub fn get_crop_area(options: &Options) -> Area {
y: val.origin.y,
},
size: Size {
width: input_width as f64,
height: input_height as f64,
width: input_width,
height: input_height,
},
}
})
Expand All @@ -236,49 +223,43 @@ pub fn process_sample_buffer(
of_type: SCStreamOutputType,
output_type: FrameType,
) -> Option<Frame> {
match of_type {
SCStreamOutputType::Screen => {
let frame_status = &sample.frame_status;

match frame_status {
SCFrameStatus::Complete | SCFrameStatus::Started => unsafe {
return Some(match output_type {
FrameType::YUVFrame => {
let yuvframe = pixelformat::create_yuv_frame(sample).unwrap();
Frame::YUVFrame(yuvframe)
}
FrameType::RGB => {
let rgbframe = pixelformat::create_rgb_frame(sample).unwrap();
Frame::RGB(rgbframe)
}
FrameType::BGR0 => {
let bgrframe = pixelformat::create_bgr_frame(sample).unwrap();
Frame::BGR0(bgrframe)
}
FrameType::BGRAFrame => {
let bgraframe = pixelformat::create_bgra_frame(sample).unwrap();
Frame::BGRA(bgraframe)
}
});
},
SCFrameStatus::Idle => {
// Quick hack - just send an empty frame, and the caller can figure out how to handle it
match output_type {
FrameType::BGRAFrame => {
return Some(Frame::BGRA(BGRAFrame {
display_time: get_pts_in_nanoseconds(&sample),
width: 0,
height: 0,
data: vec![],
}));
}
_ => {}
if let SCStreamOutputType::Screen = of_type {
let frame_status = &sample.frame_status;

match frame_status {
SCFrameStatus::Complete | SCFrameStatus::Started => unsafe {
return Some(match output_type {
FrameType::YUVFrame => {
let yuvframe = pixelformat::create_yuv_frame(sample).unwrap();
Frame::YUVFrame(yuvframe)
}
FrameType::RGB => {
let rgbframe = pixelformat::create_rgb_frame(sample).unwrap();
Frame::RGB(rgbframe)
}
FrameType::BGR0 => {
let bgrframe = pixelformat::create_bgr_frame(sample).unwrap();
Frame::BGR0(bgrframe)
}
FrameType::BGRAFrame => {
let bgraframe = pixelformat::create_bgra_frame(sample).unwrap();
Frame::BGRA(bgraframe)
}
});
},
SCFrameStatus::Idle => {
// Quick hack - just send an empty frame, and the caller can figure out how to handle it
if let FrameType::BGRAFrame = output_type {
return Some(Frame::BGRA(BGRAFrame {
display_time: get_pts_in_nanoseconds(&sample),
width: 0,
height: 0,
data: vec![],
}));
}
_ => {}
}
_ => {}
}
_ => {}
}

None
Expand Down
4 changes: 2 additions & 2 deletions src/capturer/engine/mac/pixel_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ impl RawCapturer<'_> {

pub unsafe fn sample_buffer_to_pixel_buffer(sample_buffer: &CMSampleBuffer) -> CVPixelBufferRef {
let buffer_ref = &(*sample_buffer.sys_ref);
let pixel_buffer = CMSampleBufferGetImageBuffer(buffer_ref) as CVPixelBufferRef;


pixel_buffer
CMSampleBufferGetImageBuffer(buffer_ref) as CVPixelBufferRef
}

pub unsafe fn pixel_buffer_bounds(pixel_buffer: CVPixelBufferRef) -> (usize, usize) {
Expand Down
8 changes: 3 additions & 5 deletions src/capturer/engine/mac/pixelformat.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::{mem, slice};

use screencapturekit::{cm_sample_buffer::CMSampleBuffer, cv_pixel_buffer::CVPixelBuffer};
use screencapturekit_sys::cm_sample_buffer_ref::{
CMSampleBufferGetImageBuffer, CMSampleBufferGetSampleAttachmentsArray,
};
use screencapturekit::cm_sample_buffer::CMSampleBuffer;
use screencapturekit_sys::cm_sample_buffer_ref::CMSampleBufferGetSampleAttachmentsArray;

use super::{
apple_sys::*,
Expand Down Expand Up @@ -61,7 +59,7 @@ pub unsafe fn create_yuv_frame(sample_buffer: CMSampleBuffer) -> Option<YUVFrame
}

let display_time = get_pts_in_nanoseconds(&sample_buffer);
let pixel_buffer = CMSampleBufferGetImageBuffer(buffer_ref) as CVPixelBufferRef;
let pixel_buffer = sample_buffer_to_pixel_buffer(&sample_buffer);

CVPixelBufferLockBaseAddress(pixel_buffer, 0);

Expand Down
8 changes: 4 additions & 4 deletions src/capturer/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::mpsc;

use super::Options;
use crate::frame::{Frame, FrameType};
use crate::frame::Frame;

#[cfg(target_os = "macos")]
mod mac;
Expand Down Expand Up @@ -54,11 +54,11 @@ impl Engine {
pub fn new(options: &Options, tx: mpsc::Sender<ChannelItem>) -> Engine {
#[cfg(target_os = "macos")]
{
let mac = mac::create_capturer(&options, tx);
return Engine {
let mac = mac::create_capturer(options, tx);
Engine {
mac,
options: (*options).clone(),
};
}
}

#[cfg(target_os = "windows")]
Expand Down
28 changes: 21 additions & 7 deletions src/capturer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod engine;

use std::sync::mpsc;
use std::{error::Error, sync::mpsc};

use engine::ChannelItem;

Expand Down Expand Up @@ -28,12 +28,12 @@ pub enum Resolution {
impl Resolution {
fn value(&self, aspect_ratio: f32) -> [u32; 2] {
match *self {
Resolution::_480p => [640, ((640 as f32) / aspect_ratio).floor() as u32],
Resolution::_720p => [1280, ((1280 as f32) / aspect_ratio).floor() as u32],
Resolution::_1080p => [1920, ((1920 as f32) / aspect_ratio).floor() as u32],
Resolution::_1440p => [2560, ((2560 as f32) / aspect_ratio).floor() as u32],
Resolution::_2160p => [3840, ((3840 as f32) / aspect_ratio).floor() as u32],
Resolution::_4320p => [7680, ((7680 as f32) / aspect_ratio).floor() as u32],
Resolution::_480p => [640, (640_f32 / aspect_ratio).floor() as u32],
Resolution::_720p => [1280, (1280_f32 / aspect_ratio).floor() as u32],
Resolution::_1080p => [1920, (1920_f32 / aspect_ratio).floor() as u32],
Resolution::_1440p => [2560, (2560_f32 / aspect_ratio).floor() as u32],
Resolution::_2160p => [3840, (3840_f32 / aspect_ratio).floor() as u32],
Resolution::_4320p => [7680, (7680_f32 / aspect_ratio).floor() as u32],
Resolution::Captured => {
panic!(".value should not be called when Resolution type is Captured")
}
Expand Down Expand Up @@ -78,11 +78,25 @@ pub struct Capturer {
rx: mpsc::Receiver<ChannelItem>,
}

#[derive(Debug)]
pub enum CapturerBuildError {
NotSupported,
PermissionNotGranted,
}

impl std::fmt::Display for CapturerBuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CapturerBuildError::NotSupported => write!(f, "Screen capturing is not supported"),
CapturerBuildError::PermissionNotGranted => {
write!(f, "Permission to capture the screen is not granted")
}
}
}
}

impl Error for CapturerBuildError {}

impl Capturer {
/// Create a new capturer instance with the provided options
#[deprecated(
Expand Down
12 changes: 6 additions & 6 deletions src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn remove_alpha_channel(frame_data: Vec<u8>) -> Vec<u8> {
dst[2] = src[2];
}

return data;
data
}

pub fn convert_bgra_to_rgb(frame_data: Vec<u8>) -> Vec<u8> {
Expand All @@ -116,24 +116,24 @@ pub fn convert_bgra_to_rgb(frame_data: Vec<u8>) -> Vec<u8> {
dst[2] = src[0];
}

return data;
data
}

pub fn get_cropped_data(data: Vec<u8>, cur_width: i32, height: i32, width: i32) -> Vec<u8> {
if data.len() as i32 != height * cur_width * 4 {
return data;
data
} else {
let mut cropped_data: Vec<u8> = vec![0; (4 * height * width).try_into().unwrap()];
let mut cropped_data_index = 0;

for i in 0..data.len() {
for (i, item) in data.iter().enumerate() {
let x = i as i32 % (cur_width * 4);
if x < (width * 4) {
cropped_data[cropped_data_index] = data[i];
cropped_data[cropped_data_index] = *item;
cropped_data_index += 1;
}
}
return cropped_data;
cropped_data
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/mac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use core_graphics_helmer_fork::access::ScreenCaptureAccess;
use sysinfo::System;

pub fn has_permission() -> bool {
ScreenCaptureAccess::default().preflight()
ScreenCaptureAccess.preflight()
}

pub fn request_permission() -> bool {
ScreenCaptureAccess::default().request()
ScreenCaptureAccess.request()
}

pub fn is_supported() -> bool {
Expand Down

0 comments on commit 88d0e14

Please sign in to comment.