main.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![cfg_attr(
  5. all(not(debug_assertions), target_os = "windows"),
  6. windows_subsystem = "windows"
  7. )]
  8. fn main() {
  9. use std::{
  10. cmp::min,
  11. io::{Read, Seek, SeekFrom},
  12. path::PathBuf,
  13. process::{Command, Stdio},
  14. };
  15. use tauri::http::{HttpRange, ResponseBuilder};
  16. let video_file = PathBuf::from("test_video.mp4");
  17. let video_url =
  18. "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
  19. if !video_file.exists() {
  20. // Downloading with curl this saves us from adding
  21. // a Rust HTTP client dependency.
  22. println!("Downloading {}", video_url);
  23. let status = Command::new("curl")
  24. .arg("-L")
  25. .arg("-o")
  26. .arg(&video_file)
  27. .arg(video_url)
  28. .stdout(Stdio::inherit())
  29. .stderr(Stdio::inherit())
  30. .output()
  31. .unwrap();
  32. assert!(status.status.success());
  33. assert!(video_file.exists());
  34. }
  35. tauri::Builder::default()
  36. .register_uri_scheme_protocol("stream", move |_app, request| {
  37. // prepare our response
  38. let mut response = ResponseBuilder::new();
  39. // get the wanted path
  40. #[cfg(target_os = "windows")]
  41. let path = request.uri().replace("stream://localhost/", "");
  42. #[cfg(not(target_os = "windows"))]
  43. let path = request.uri().replace("stream://", "");
  44. if path != "example/test_video.mp4" {
  45. // return error 404 if it's not out video
  46. return response.mimetype("text/plain").status(404).body(Vec::new());
  47. }
  48. // read our file
  49. let mut content = std::fs::File::open(&video_file)?;
  50. let mut buf = Vec::new();
  51. // default status code
  52. let mut status_code = 200;
  53. // if the webview sent a range header, we need to send a 206 in return
  54. // Actually only macOS and Windows are supported. Linux will ALWAYS return empty headers.
  55. if let Some(range) = request.headers().get("range") {
  56. // Get the file size
  57. let file_size = content.metadata().unwrap().len();
  58. // we parse the range header with tauri helper
  59. let range = HttpRange::parse(range.to_str().unwrap(), file_size).unwrap();
  60. // let support only 1 range for now
  61. let first_range = range.first();
  62. if let Some(range) = first_range {
  63. let mut real_length = range.length;
  64. // prevent max_length;
  65. // specially on webview2
  66. if range.length > file_size / 3 {
  67. // max size sent (400ko / request)
  68. // as it's local file system we can afford to read more often
  69. real_length = min(file_size - range.start, 1024 * 400);
  70. }
  71. // last byte we are reading, the length of the range include the last byte
  72. // who should be skipped on the header
  73. let last_byte = range.start + real_length - 1;
  74. // partial content
  75. status_code = 206;
  76. // Only macOS and Windows are supported, if you set headers in linux they are ignored
  77. response = response
  78. .header("Connection", "Keep-Alive")
  79. .header("Accept-Ranges", "bytes")
  80. .header("Content-Length", real_length)
  81. .header(
  82. "Content-Range",
  83. format!("bytes {}-{}/{}", range.start, last_byte, file_size),
  84. );
  85. // FIXME: Add ETag support (caching on the webview)
  86. // seek our file bytes
  87. content.seek(SeekFrom::Start(range.start))?;
  88. content.take(real_length).read_to_end(&mut buf)?;
  89. } else {
  90. content.read_to_end(&mut buf)?;
  91. }
  92. }
  93. response.mimetype("video/mp4").status(status_code).body(buf)
  94. })
  95. .run(tauri::generate_context!(
  96. "../../examples/streaming/tauri.conf.json"
  97. ))
  98. .expect("error while running tauri application");
  99. }