Skip to content

Commit

Permalink
move bin fns to utils and make restream use them
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredolx committed Jan 22, 2025
1 parent 4d9bca3 commit 3e640f3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
53 changes: 5 additions & 48 deletions src-tauri/src/mpv.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
use crate::settings::get_default_record_path;
use crate::sql;
use crate::types::ChannelHttpHeaders;
use crate::{log, sql};
use crate::utils::{find_macos_bin, get_bin};
use crate::{media_type, settings::get_settings, types::Channel};
use anyhow::{bail, Context, Result};
use chrono::Local;
use std::sync::LazyLock;
use std::{
env::{consts::OS, current_exe},
path::Path,
process::Stdio,
};
use std::{env::consts::OS, path::Path, process::Stdio};
use tokio::{
io::{AsyncBufReadExt, BufReader},
process::Command,
};
use which::which;

const ARG_SAVE_POSITION_ON_QUIT: &str = "--save-position-on-quit";
const ARG_CACHE: &str = "--cache=";
Expand All @@ -33,14 +29,8 @@ const MPV_BIN_NAME: &str = "mpv";
const YTDLP_BIN_NAME: &str = "yt-dlp";
const HTTP_ORIGIN: &str = "origin:";
const HTTP_REFERRER: &str = "referer:";
const MACOS_POTENTIAL_PATHS: [&str; 3] = [
"/opt/local/bin", // MacPorts
"/opt/homebrew/bin", // Homebrew on AARCH64 Mac
"/usr/local/bin", // Homebrew on AMD64 Mac
];

static MPV_PATH: LazyLock<String> = LazyLock::new(|| get_mpv_path());
static YTDLP_PATH: LazyLock<String> = LazyLock::new(|| find_macos_bin(YTDLP_BIN_NAME.to_string()));
static MPV_PATH: LazyLock<String> = LazyLock::new(|| get_bin(MPV_BIN_NAME));
static YTDLP_PATH: LazyLock<String> = LazyLock::new(|| find_macos_bin(YTDLP_BIN_NAME));

pub async fn play(channel: Channel, record: bool) -> Result<()> {
println!("{} playing", channel.url.as_ref().unwrap());
Expand Down Expand Up @@ -76,39 +66,6 @@ pub async fn play(channel: Channel, record: bool) -> Result<()> {
Ok(())
}

fn get_mpv_path() -> String {
if OS == "linux" || which("mpv").is_ok() {
return MPV_BIN_NAME.to_string();
} else if OS == "macos" {
return find_macos_bin(MPV_BIN_NAME.to_string());
}
return get_mpv_path_win();
}

fn get_mpv_path_win() -> String {
let mut path = current_exe().unwrap();
path.pop();
path.push("deps");
path.push("mpv.exe");
return path.to_string_lossy().to_string();
}

fn find_macos_bin(bin: String) -> String {
return MACOS_POTENTIAL_PATHS
.iter()
.map(|path| {
let mut path = Path::new(path).to_path_buf();
path.push(&bin);
return path;
})
.find(|path| path.exists())
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_else(|| {
log::log(format!("Could not find {} on MacOS host", bin));
return bin;
});
}

fn get_play_args(channel: Channel, record: bool) -> Result<Vec<String>> {
let mut args = Vec::new();
let settings = get_settings()?;
Expand Down
4 changes: 3 additions & 1 deletion src-tauri/src/restream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ use crate::{
settings::get_settings,
sql,
types::{AppState, Channel, NetworkInfo},
utils::get_bin,
};

const WAN_IP_API: &str = "https://api.ipify.org";
const FFMPEG_BIN_NAME: &str = "ffmpeg";

fn start_ffmpeg_listening(channel: Channel, restream_dir: PathBuf) -> Result<Child> {
let headers = sql::get_channel_headers_by_id(channel.id.context("no channel id")?)?;
let playlist_dir = get_playlist_dir(restream_dir);
let mut command = Command::new("ffmpeg");
let mut command = Command::new(get_bin(FFMPEG_BIN_NAME));
command
.arg("-i")
.arg(channel.url.context("no channel url")?);
Expand Down
48 changes: 47 additions & 1 deletion src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
log::log,
m3u,
settings::{get_default_record_path, get_settings},
source_type, sql,
Expand All @@ -9,8 +10,20 @@ use anyhow::{anyhow, bail, Context, Result};
use chrono::{DateTime, Local, Utc};
use regex::Regex;
use reqwest::Client;
use std::{io::Write, path::Path, sync::LazyLock};
use std::{
env::{consts::OS, current_exe},
io::Write,
path::Path,
sync::LazyLock,
};
use tauri::{AppHandle, Emitter};
use which::which;

const MACOS_POTENTIAL_PATHS: [&str; 3] = [
"/opt/local/bin", // MacPorts
"/opt/homebrew/bin", // Homebrew on AARCH64 Mac
"/usr/local/bin", // Homebrew on AMD64 Mac
];

static ILLEGAL_CHARS_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"[<>:"/\\|?*\x00-\x1F]"#).unwrap());
Expand Down Expand Up @@ -96,6 +109,39 @@ fn get_download_path(file_name: String) -> Result<String> {
Ok(path.to_string_lossy().to_string())
}

pub fn get_bin(bin: &str) -> String {
if OS == "linux" || which(bin).is_ok() {
return bin.to_string();
} else if OS == "macos" {
return find_macos_bin(bin);
}
return get_bin_from_deps(bin);
}

fn get_bin_from_deps(bin: &str) -> String {
let mut path = current_exe().unwrap();
path.pop();
path.push("deps");
path.push(bin);
return path.to_string_lossy().to_string();
}

pub fn find_macos_bin(bin: &str) -> String {
return MACOS_POTENTIAL_PATHS
.iter()
.map(|path| {
let mut path = Path::new(path).to_path_buf();
path.push(bin);
return path;
})
.find(|path| path.exists())
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_else(|| {
log(format!("Could not find {} on MacOS host", bin));
return bin.to_string();
});
}

#[cfg(test)]
mod test_utils {
use super::sanitize;
Expand Down

0 comments on commit 3e640f3

Please sign in to comment.