| 22 | |
| 23 | impl Capturer { |
| 24 | pub fn new<F: Fn(Frame) + 'static>( |
| 25 | display: Display, |
| 26 | width: usize, |
| 27 | height: usize, |
| 28 | format: PixelFormat, |
| 29 | config: Config, |
| 30 | handler: F, |
| 31 | ) -> Result<Capturer, CGError> { |
| 32 | let stopped = Arc::new(Mutex::new(false)); |
| 33 | let cloned_stopped = stopped.clone(); |
| 34 | let handler: FrameAvailableHandler = ConcreteBlock::new(move |status, _, surface, _| { |
| 35 | use self::CGDisplayStreamFrameStatus::*; |
| 36 | if status == Stopped { |
| 37 | let mut lock = cloned_stopped.lock().unwrap(); |
| 38 | *lock = true; |
| 39 | return; |
| 40 | } |
| 41 | if status == FrameComplete { |
| 42 | handler(unsafe { Frame::new(surface) }); |
| 43 | } |
| 44 | }) |
| 45 | .copy(); |
| 46 | |
| 47 | let queue = unsafe { |
| 48 | dispatch_queue_create( |
| 49 | b"quadrupleslap.scrap\0".as_ptr() as *const i8, |
| 50 | ptr::null_mut(), |
| 51 | ) |
| 52 | }; |
| 53 | |
| 54 | let stream = unsafe { |
| 55 | let config = config.build(); |
| 56 | let stream = CGDisplayStreamCreateWithDispatchQueue( |
| 57 | display.id(), |
| 58 | width, |
| 59 | height, |
| 60 | format, |
| 61 | config, |
| 62 | queue, |
| 63 | &*handler as *const Block<_, _> as *const c_void, |
| 64 | ); |
| 65 | CFRelease(config); |
| 66 | stream |
| 67 | }; |
| 68 | |
| 69 | match unsafe { CGDisplayStreamStart(stream) } { |
| 70 | CGError::Success => Ok(Capturer { |
| 71 | stream, |
| 72 | queue, |
| 73 | width, |
| 74 | height, |
| 75 | format, |
| 76 | display, |
| 77 | stopped, |
| 78 | }), |
| 79 | x => Err(x), |
| 80 | } |
| 81 | } |