From e8f52fd8b516252d979161e449bc0b533a94f7c1 Mon Sep 17 00:00:00 2001 From: Kamil Jarosz Date: Sat, 7 Sep 2024 12:50:37 +0200 Subject: [PATCH] video: Use cache dir for storing downloaded OpenH264 library Using the executable's parent directory is a bad idea in most cases, as the place where Ruffle is installed should not be modified. This patch changes the directory where the OpenH264 library is saved to the cache directory, which should always be writable. --- desktop/src/player.rs | 4 +++- tests/framework/src/options.rs | 5 ++++- video/external/src/backend.rs | 12 +++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/desktop/src/player.rs b/desktop/src/player.rs index 620045a63..0e5d9ae8d 100644 --- a/desktop/src/player.rs +++ b/desktop/src/player.rs @@ -227,7 +227,9 @@ impl ActivePlayer { #[cfg(feature = "external_video")] { use ruffle_video_external::backend::ExternalVideoBackend; - let openh264 = tokio::task::block_in_place(ExternalVideoBackend::load_openh264); + let openh264 = tokio::task::block_in_place(|| { + ExternalVideoBackend::load_openh264(&opt.cache_directory.join("video")) + }); let openh264 = match openh264 { Ok(codec) => Some(codec), Err(e) => { diff --git a/tests/framework/src/options.rs b/tests/framework/src/options.rs index b837c9f66..7258a75f7 100644 --- a/tests/framework/src/options.rs +++ b/tests/framework/src/options.rs @@ -177,8 +177,11 @@ impl PlayerOptions { if self.with_video { #[cfg(feature = "ruffle_video_external")] { + let current_exe = std::env::current_exe()?; + let directory = current_exe.parent().expect("Executable parent dir"); + use ruffle_video_external::backend::ExternalVideoBackend; - let openh264 = ExternalVideoBackend::load_openh264() + let openh264 = ExternalVideoBackend::load_openh264(directory) .map_err(|e| anyhow!("Couldn't load OpenH264: {}", e))?; player_builder = diff --git a/video/external/src/backend.rs b/video/external/src/backend.rs index d684294be..7751ebe1a 100644 --- a/video/external/src/backend.rs +++ b/video/external/src/backend.rs @@ -12,7 +12,7 @@ use sha2::{Digest, Sha256}; use slotmap::SlotMap; use std::fs::File; use std::io::copy; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use swf::{VideoCodec, VideoDeblocking}; enum ProxyOrStream { @@ -102,6 +102,7 @@ impl ExternalVideoBackend { fn download_openh264( openh264_data: &OpenH264Data, + directory: &Path, ) -> Result> { // See the license at: https://www.openh264.org/BINARY_LICENSE.txt const URL_BASE: &str = "http://ciscobinary.openh264.org/"; @@ -112,10 +113,7 @@ impl ExternalVideoBackend { openh264_data.download_sha256, ); - let current_exe = std::env::current_exe()?; - let directory = current_exe - .parent() - .ok_or("Could not determine Ruffle location.")?; + std::fs::create_dir_all(directory)?; let filepath = directory.join(filename); // If the binary doesn't exist in the expected location, download it. @@ -149,7 +147,7 @@ impl ExternalVideoBackend { Ok(filepath) } - pub fn load_openh264() -> Result> { + pub fn load_openh264(directory: &Path) -> Result> { let openh264_data = Self::get_openh264_data()?; for filename in &openh264_data.local_filenames { @@ -166,7 +164,7 @@ impl ExternalVideoBackend { } tracing::info!("Downloading OpenH264 library"); - let filename = Self::download_openh264(&openh264_data)?; + let filename = Self::download_openh264(&openh264_data, directory)?; tracing::info!("Using OpenH264 at {:?}", filename); Ok(OpenH264Codec::new(&filename)?) }