-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[bug] Asset protocol crashes app with large video files #6375
Comments
Thanks for the detailed bug report, I will see what I can do |
I think I figured out the problem, could you apply this patch to your repro and see if it fixes the issue for your? diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index 6561691..2682f1f 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -17,6 +17,10 @@ tauri-build = { version = "1.2", features = [] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2", features = ["devtools", "dialog-open", "protocol-asset", "shell-open"] }
+percent-encoding = "2.2.0"
+url = "2.3.1"
+http-range-header = "0.3.0"
+rand = "0.8.5"
[features]
# by default Tauri runs in production mode
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 3cd46c4..2ca41eb 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -3,9 +3,157 @@
windows_subsystem = "windows"
)]
+use std::io::{Read, Seek, SeekFrom, Write};
+use tauri::http::{
+ header::{ACCEPT_RANGES, CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, RANGE},
+ status::StatusCode,
+ MimeType, ResponseBuilder,
+};
+use url::{Position, Url};
+
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![])
+ .register_uri_scheme_protocol("stream", move |_app, request| {
+ let parsed_path = Url::parse(request.uri())?;
+ let filtered_path = &parsed_path[..Position::AfterPath];
+ let path = filtered_path
+ .strip_prefix("stream://localhost/")
+ // the `strip_prefix` only returns None when a request is made to `https://tauri.$P` on Windows
+ // where `$P` is not `localhost/*`
+ .unwrap_or("");
+ let path = percent_encoding::percent_decode(path.as_bytes())
+ .decode_utf8_lossy()
+ .to_string();
+
+ let mut file = std::fs::File::open(&path)?;
+
+ // get current position
+ let old_pos = file.seek(SeekFrom::Current(0))?;
+ // get file len
+ let len = file.seek(SeekFrom::End(0))?;
+ // reset position
+ file.seek(SeekFrom::Start(old_pos))?;
+
+ // get the mime type
+ let mut prelude: [u8; 256] = [0; 256];
+ file.read(&mut prelude)?;
+ let mime_type = MimeType::parse(&prelude, &path);
+
+ // reset position
+ file.seek(SeekFrom::Start(0))?;
+
+ let mut resp = ResponseBuilder::new().header(CONTENT_TYPE, &mime_type);
+
+ let response = if let Some(x) = request.headers().get(RANGE) {
+ let not_satisfiable = || {
+ ResponseBuilder::new()
+ .status(StatusCode::RANGE_NOT_SATISFIABLE)
+ .header(CONTENT_RANGE, format!("bytes */{len}"))
+ .body(vec![])
+ };
+
+ resp = resp.header(ACCEPT_RANGES, "bytes");
+
+ let ranges = http_range_header::parse_range_header(x.to_str()?)?;
+ let ranges = ranges.validate(len);
+ let ranges: Vec<_> = if let Ok(x) = ranges {
+ x.iter().map(|r| (*r.start(), *r.end())).collect()
+ } else {
+ return not_satisfiable();
+ };
+
+ const MAX_LEN: u64 = 1000 * 1024;
+
+ if ranges.len() == 1 {
+ let &(start, mut end) = ranges.first().unwrap();
+
+ if start >= len || end >= len || end < start {
+ return not_satisfiable();
+ }
+
+ // adjust end byte for MAX_LEN
+ end = start + (end - start).min(len - start).min(MAX_LEN - 1);
+
+ // calculate number of bytes needed to be read
+ let bytes_to_read = end + 1 - start;
+
+ // allocate a buf with a suitable capacity
+ let mut buf = Vec::with_capacity(bytes_to_read as usize);
+ // seek the file to the starting byte
+ file.seek(SeekFrom::Start(start))?;
+ // read the needed bytes
+ file.take(bytes_to_read).read_to_end(&mut buf)?;
+
+ resp = resp.header(CONTENT_RANGE, format!("bytes {start}-{end}/{len}"));
+ resp = resp.header(CONTENT_LENGTH, end + 1 - start);
+ resp = resp.status(StatusCode::PARTIAL_CONTENT);
+ resp.body(buf)
+ } else {
+ let mut buf = Vec::new();
+ let ranges = ranges
+ .iter()
+ .filter_map(|&(start, mut end)| {
+ if start >= len || end >= len || end < start {
+ None
+ } else {
+ end = start + (end - start).min(len - start).min(MAX_LEN - 1);
+ Some((start, end))
+ }
+ })
+ .collect::<Vec<_>>();
+
+ let boundary = random_boundary();
+ let boundary_sep = format!("\r\n--{boundary}\r\n");
+ let boundary_closer = format!("\r\n--{boundary}\r\n");
+
+ resp = resp.header(
+ CONTENT_TYPE,
+ format!("multipart/byteranges; boundary={boundary}"),
+ );
+
+ for (end, start) in ranges {
+ // a new range is being written, write the range boundary
+ buf.write_all(boundary_sep.as_bytes())?;
+
+ // write the needed headers `Content-Type` and `Content-Range`
+ buf.write_all(format!("{CONTENT_TYPE}: {mime_type}\r\n").as_bytes())?;
+ buf.write_all(
+ format!("{CONTENT_RANGE}: bytes {start}-{end}/{len}\r\n").as_bytes(),
+ )?;
+
+ // write the separator to indicate the start of the range body
+ buf.write_all("\r\n".as_bytes())?;
+
+ // calculate number of bytes needed to be read
+ let bytes_to_read = end + 1 - start;
+
+ let mut local_buf = vec![0_u8; bytes_to_read as usize];
+ file.seek(SeekFrom::Start(start))?;
+ file.read_exact(&mut local_buf)?;
+ buf.extend_from_slice(&local_buf);
+ }
+ // all ranges have been written, write the closing boundary
+ buf.write_all(boundary_closer.as_bytes())?;
+
+ resp.body(buf)
+ }
+ } else {
+ let mut buf = Vec::with_capacity(len as usize);
+ file.read_to_end(&mut buf)?;
+ resp = resp.header(CONTENT_LENGTH, len);
+ resp.body(buf)
+ };
+
+ response
+ })
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
+
+fn random_boundary() -> String {
+ use rand::RngCore;
+
+ let mut x = [0 as u8; 30];
+ rand::thread_rng().fill_bytes(&mut x);
+ (&x[..])
+ .iter()
+ .map(|&x| format!("{:x}", x))
+ .fold(String::new(), |mut a, x| {
+ a.push_str(x.as_str());
+ a
+ })
+}
diff --git a/src/main.js b/src/main.js
index 4064b3a..fc841fc 100644
--- a/src/main.js
+++ b/src/main.js
@@ -25,6 +25,7 @@ window.addEventListener("DOMContentLoaded", () => {
const assetPath = convertFileSrc(selected);
let videoElement = document.createElement("video");
+ +videoElement.setAttribute("autoplay", "");
videoElement.setAttribute("controls", "");
videoElement.src = assetPath;
|
That works perfectly @amrbashir, thank you. The initial load of the video is still a bit slower compared to using Rocket, but the seek times are now instant and long/large videos no longer hang and crash the app. One concern I do have have though is the lack of an Other than the small header concern, what would be the timeline for getting this fix added to Tauri? |
I will see what I can do about that but no promises on this one.
In tauri's
Hard to say tbh, tauri 1.3 is already in internal auditing so It may wait for 1.4 or 1.3.1. I will make the PR today but it is not up to me to include it in 1.3 or not, cc @chippers |
The initial load time isn't that extreme that it's a big issue, so if it cannot be improved, no worries. For context initial load time for a 7 hours long video file (18.4GB) is ~4 seconds using the provided solution, compared to ~2 seconds using Rocket with
Ahh alright, great 🙂
Okay, well at least I have a temporary solution that allows me to finally ditch Rocket, so thank you again ❤️ |
PR is up now #6390
I don't know but I have a 6gb- 3hours long file and the load is almost instant to me, I don't have a bigger file to test with so I will just ignore this issue for now until I have something to test with or others report the same.
You're welcome, I also updated the diff above to include the multipart implementation, should be fine to use until the fix is released to crates.io |
Are the subtitle files embedded in the video, or separate? could you give me the request details? |
They are stored as
No quite sure what all the details you want are. But here are the request headers: For context the old version's request headers look the same, the only thing missing from the old version is |
I tried reproducing the issue but got CORS issue. I am heading out for today, if you can update the repro to include a subtitle file usage, I will check it out tomorrow. |
Alright I'll create a branch in the repo for the subtitle issue. |
Here is the new repo branch: tauri-asset-bug > subtitle-issue |
1.3 will not be having any more changes except for small hotfixes. That doesn't mean that 1.3.1 can't be far behind the 1.3 release. |
@mauritzn sorry for the delay, I updated the implementation above to fix the subtitle issue. The problem I was pre-allocating the buffer and filling it with |
@amrbashir thank you very much that seems to have fixed it ❤️
|
I have tried out the solution implemented by @amrbashir and on my side, the video at first loads quickly. But jumping on different points does still make the app crash with a exit code of 1. (Guess I have to figure out, how to debug this?) Also, after a certain number of requests, the time to handle a request for a new range of the video stream takes up over 30s. I simply wanted to add that the solution works, but only for a while in my case. |
@yannkost were you testing using the above snippet, or the PR linked to this issue? Could you make a minimal repro so I can test it? |
@amrbashir I took the snipped posted above. (Also tested the snipped from the streaming example). I can try to make a minimal repo to test. I do not know how to update to my version to a PR version? Also, for some better error analyses, I wanted to debut the reason why Tauri crashed, but "set RUST_BACKTRACE=1" does not change the output of the terminal when running the project in development, maybe someone can point me into the right direction to get a full log? (I only managed to find a Windows event journal entry which gives not much info...) |
try adding the following in [patch.crates-io]
tauri = { git = "https://github.com/tauri-apps/tauri", branch = "fix/asset-protocol-streaming" } then run |
Co-authored-by: Lucas Nogueira <[email protected]>
Sadly the "fix/asset-protocol-streaming" branch has been merged into "dev" already. But when I try to do it with the "dev" branch, I get a direct crash with following error message:
Does anybody know if there have been other changes in dev that could have provoked this?
|
fixes regression introduced in 45330e3 ref: #6375 (comment)
fixes regression introduced in 45330e3 ref: #6375 (comment)
From my limited testing the current dev branch (#2317913b) works perfectly for video streaming, captions work as expected as well and seeking is instant. I will be doing a bit more extensive testing over the coming days and if I detect any issues I'll be sure to report them. Will the current dev branch be v1.4? If so, when could we be expecting a release (roughly)? |
Works like a charm now! |
Co-authored-by: wusyong <[email protected]> Co-authored-by: Fabian-Lars <[email protected]> Co-authored-by: Lucas Nogueira <[email protected]> Co-authored-by: Simon Hyll <[email protected]> Co-authored-by: Lucas Fernandes Nogueira <[email protected]> Co-authored-by: Lucas Nogueira <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Lucas Fernandes Nogueira <[email protected]> Co-authored-by: Amr Bashir <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: chip <[email protected]> Co-authored-by: Raphii <[email protected]> Co-authored-by: Ronie Martinez <[email protected]> Co-authored-by: hanaTsuk1 <[email protected]> Co-authored-by: nathan-fall <[email protected]> Co-authored-by: Akshay <[email protected]> Co-authored-by: KurikoMoe <[email protected]> Co-authored-by: Guilherme Oenning <[email protected]> Co-authored-by: Pierre Cashon <[email protected]> Co-authored-by: Jack Wills <[email protected]> Co-authored-by: Amirhossein Akhlaghpour <[email protected]> Co-authored-by: Risto Stevcev <[email protected]> Co-authored-by: Soumt <[email protected]> Co-authored-by: yutotnh <[email protected]> Co-authored-by: Gökçe Merdun <[email protected]> Co-authored-by: Nathanael Rea <[email protected]> Co-authored-by: Usman Rajab <[email protected]> Co-authored-by: Francis The Basilisk <[email protected]> Co-authored-by: Lej77 <[email protected]> Co-authored-by: Tomáš Diblík <[email protected]> Co-authored-by: Jonas Kruckenberg <[email protected]> Co-authored-by: Pascal Sommer <[email protected]> Co-authored-by: Bo <[email protected]> Co-authored-by: Kevin Yue <[email protected]> fixed grammar and typos (#6937) Fix api.js docs pipeline with updated typedoc dependencies (#6945) closes #6887 (#6922) fix(core): Fix `WindowBuilder::on_navigation` handler never registerd, closes #6865 (#6921) fix(core): Fix `WindowBuilder::on_navigation` handler never registerd, closes #6865 fix broken symlinks in license files (#6336) fix(cli): fix cli connection timeout to dev server (fix #6045) (#6046) fix(bundler): ensure that there are no duplicate extension arguments when bundling on Windows, fixes #6103 (#6917) fix(bundler): ensure that there are no duplicate extension arguments during bundling on Windows (fix #6103) closes #5491 (#6408) fix(nsis): prefill $INSTDIR with previous install path and respect `/D` flag, closes #6928 (#6935) fix(nsis): prefill $INSTDIR with previous install path and respect `/D` flag, closes #6928 fix(updater): emit `UPTODATE` when server responds with 204, closes #6934 (#6970) fix(core): unpin all dependencies, closes #6944 (#6966) fix(bundler): Add new lang_file option in persian variant. (#6972) fix(core/ipc): access url through webview native object, closes #6889 (#6976) fix(core): remove trailing slash in http scope url, closes #5208 (#6974) fix(core): remove trailing slash in http scope url, closes #5208 fix(cli): find correct binary when `--profile` is used, closes #6954 (#6979) fix(cli): find correct binary when `--profile` is used, closes #6954 closes #6955 (#6987) closes #6955 closes #6158 (#6969) closes #6158 fix(cli): improve vs build tools detection (#6982) fix: updated appimage script to follow symlinks for /usr/lib* (fix: #6992) (#6996) fix(cli): correctly remove Cargo features (#7013) Fix typo (#7012) fix(cli): revert metadata.json field rename from #6795 (#7029) closes #6732 (#6736) fix: add missing file properties on Windows, closes #6676 (#6693) fix(cli.js): detect node-20 binary (#6667) fix version-or-publish workflow (#7031) fix(cli/devserver): inject autoreload into HTML only, closes #6997 (#7032) fix(bundler/nsis): write installer templates UTF16LE encoded, closes #7036 (#7040) fix(bundler/nsis): write installer templates UTF16LE encoded, closes #7036 fix(core): rewrite `asset` protocol streaming, closes #6375 (#6390) closes #5939 (#5960) fix(core): use `safe_block_on` (#7047) closes #6859 (#6933) closes #6955 (#6998) fix(core): populate webview_attrs from config, closes #6794 (#6797) closes #5176 (#5180) fix: sound for notifications on windows (fix #6652) (#6680) close native window's buttons, closes #2353 (#6665) fix(bundler/nsis): calculate accurate app size, closes #7056 (#7057) fix(tests): only download update when it is available (#7061) closes #6706 (#6712) fix(doc): correct the doc of `content_protected()` (#7065) closes #6472 (#6530) fix(macros): use full path to Result to avoid issues with type aliases (#7071)
Describe the bug
When seeking into longer/larger videos using Tauri's asset protocol, the video will hang and eventually crash the app entirely.
After quite a bit of testing it seems that video size is what makes Tauri's asset protocol crash the app. My original thinking was that it crashed due to the length of the video, but after generating a really long but small sized countdown video it worked fine. Then through more testing I found that videos larger than
~3.5 GiB
crashed the asset protocol when seeking roughly80%
into the video, so when about~3 GiB
needed to be loaded.Also from my testing it seems that Tauri's asset protocol is seeking the entire video from
start->seek point
, which means that for longer/larger videos it takes a significant amount of time to seek into videos (from an SSD). The reason I think it's seeking the entire video is due to how long the seeking takes on longer videos compared to when using a HTTP server to stream the file (instant seeking, compared to multiple seconds sometimes with Tauri's asset protocol).I doubt I'm running out of memory since my computer has 32GB of RAM to use. When looking at the task manger it crashes way before all my RAM is utilized. Tauri is using around 4GB of RAM before crashing.
I have tried using the streaming example, it gives the same result as the normal asset protocol.
When the app crashes nothing shows up in the Web DevTools or in the terminal running
tauri dev
. When running the app usingRUST_BACKTRACE
the app no longer crashes, instead the video tries to load endlessly but doesn't cause a crash anymore.Found temporary solution
When I found this issue with the asset protocol I decided to embed Rocket and use it to stream the videos, sadly it had the same issue, forcing me to use a Rocket responder that handled video streaming correctly. After a while I found rocket_seek_stream made by rydz. This fixed my issue and videos could now stream properly, seeking the video now doesn't load from
start->seek point
, instead the video is only loaded from the seek point onwards. Even though I was able to workaround the issue, I would like for Tauri's asset protocol to get fixed since I'd much rather not include Rocket in my project.When using
rocket_seek_stream
with Rocket the app no longer crashes and seeking inside large videos is instant.Crash demo
crash_demo.mp4
Reproduction
No response
Expected behavior
No response
Platform and versions
Environment
Packages
App
App directory structure
├─ .git
├─ .vscode
├─ node_modules
├─ src
└─ src-tauri
Stack trace
No response
Additional context
I created a small repo that allows you to pick a local video to play (which crashes when seeking close to the end of larger files). I was not able to include a video file since the video file needs to be quite large (multiple GiBs), as such the project allows you to pick the video file to play. The video file should be
10GiB+
to reliably crash the app, in my testing~3.5GiB
is usually enough to crash it, but some larger video files still work (sometimes), as such10GiB+
has always been a guaranteed crash for me.The video used should be in a format supported by browsers (e.g. H264, VP9). H265 is not support since browsers do not have codec support for it.
In general the videos I've been trying to play that crash are Twitch VODs, since they are long and large (e.g. 8+ hours, 10GiB+).
The text was updated successfully, but these errors were encountered: