Lyend

Untitled

Mar 8th, 2020
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. use std::process::{Command, Stdio};
  2. use std::net::TcpListener;
  3. use std::os::unix::io::{AsRawFd, FromRawFd};
  4. use std::io::{Write, Read};
  5. use structopt::StructOpt;
  6.  
  7.  
  8. #[derive(Debug, StructOpt)]
  9. #[structopt(name = "ScreenShare-viewer", about="Recieves Screenshare Stream")]
  10. struct Opt {
  11. /// Activate debug mode
  12. // short and long flags (-d, --debug) will be deduced from the field's name
  13. #[structopt(short, long, default_value="0.0.0.0:9090")]
  14. connect: String,
  15.  
  16. #[structopt(long, default_value = "60")]
  17. fps: u32,
  18.  
  19. #[structopt(long, short)]
  20. record: Option<String>
  21. }
  22.  
  23. fn main() -> std::io::Result<()> {
  24. let opt = Opt::from_args();
  25.  
  26. let server = TcpListener::bind(&*opt.connect).unwrap();
  27.  
  28. if let Some(Ok(mut stream)) = server.incoming().next() {
  29. let (w, h) = {
  30. let mut buf = [0; 32];
  31. stream.read(&mut buf[..])?;
  32. let s = String::from_utf8_lossy(&buf);
  33. let mut slices = s.split("|");
  34. (
  35. &slices.next()
  36. .expect("Expected Width Param")
  37. .parse::<usize>()
  38. .expect("Expected Int"),
  39.  
  40. &slices.next()
  41. .expect("Expected Height Param")
  42. .parse::<usize>()
  43. .expect("Expected Int")
  44. )
  45. };
  46.  
  47.  
  48. Command::new("ffplay")
  49. .args(&[
  50. "-f", "rawvideo",
  51. "-pixel_format", "bgr0",
  52. "-video_size", &format!("{}x{}", w, h),
  53. "-framerate", &format!("{}", opt.fps),
  54. "-"
  55. ])
  56. .stdin(unsafe {
  57. Stdio::from_raw_fd(stream.as_raw_fd())
  58. })
  59. .spawn()
  60. .expect("Requires ffplay.")
  61. .wait()?;
  62.  
  63. Ok(())
  64. }
  65. else {
  66. Err(std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "Failed to accept connection connect"))
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment