4 changed files with 143 additions and 63 deletions
+108
View File
@@ -170,6 +170,114 @@ pub async fn open_url(app: tauri::AppHandle, url: String) -> Result<(), String>
.map_err(|e| e.to_string())
}
/// 切换移动端屏幕方向(iOS 原生旋转屏幕方向:横屏 Landscape / 竖屏 Portrait)。
#[tauri::command]
pub async fn set_orientation(app: tauri::AppHandle, landscape: bool) -> Result<(), String> {
#[cfg(target_os = "ios")]
{
let _ = app.run_on_main_thread(move || {
request_ios_orientation(landscape);
});
}
#[cfg(not(target_os = "ios"))]
{
let _ = (&app, landscape);
}
Ok(())
}
#[cfg(target_os = "ios")]
fn request_ios_orientation(landscape: bool) {
use std::ffi::{c_char, c_void};
extern "C" {
fn objc_getClass(name: *const c_char) -> *mut c_void;
fn sel_registerName(name: *const c_char) -> *mut c_void;
fn objc_msgSend();
}
unsafe {
let msg0: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
std::mem::transmute(objc_msgSend as *const ());
let msg0_usize: unsafe extern "C" fn(*mut c_void, *mut c_void) -> usize =
std::mem::transmute(objc_msgSend as *const ());
let msg1_usize: unsafe extern "C" fn(*mut c_void, *mut c_void, usize) -> *mut c_void =
std::mem::transmute(objc_msgSend as *const ());
let msg2_ptr: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void, *mut c_void) =
std::mem::transmute(objc_msgSend as *const ());
let msg1_void: unsafe extern "C" fn(*mut c_void, *mut c_void) =
std::mem::transmute(objc_msgSend as *const ());
// 1. iOS 16+ UIWindowScene.requestGeometryUpdateWithPreferences:errorHandler:
// UIInterfaceOrientationMaskPortrait = 1 << 1 = 2
// UIInterfaceOrientationMaskLandscapeRight = 1 << 3 = 8
let mask: usize = if landscape { 8 } else { 2 };
let prefs_cls = objc_getClass(b"UIWindowSceneGeometryPreferencesIOS\0".as_ptr() as *const c_char);
let app_cls = objc_getClass(b"UIApplication\0".as_ptr() as *const c_char);
if !prefs_cls.is_null() && !app_cls.is_null() {
let sel_alloc = sel_registerName(b"alloc\0".as_ptr() as *const c_char);
let sel_init = sel_registerName(b"initWithInterfaceOrientations:\0".as_ptr() as *const c_char);
let raw_prefs = msg0(prefs_cls, sel_alloc);
let prefs = if !raw_prefs.is_null() {
msg1_usize(raw_prefs, sel_init, mask)
} else {
std::ptr::null_mut()
};
let sel_shared = sel_registerName(b"sharedApplication\0".as_ptr() as *const c_char);
let app = msg0(app_cls, sel_shared);
if !app.is_null() && !prefs.is_null() {
let sel_scenes = sel_registerName(b"connectedScenes\0".as_ptr() as *const c_char);
let scenes_set = msg0(app, sel_scenes);
if !scenes_set.is_null() {
let sel_all = sel_registerName(b"allObjects\0".as_ptr() as *const c_char);
let arr = msg0(scenes_set, sel_all);
if !arr.is_null() {
let sel_count = sel_registerName(b"count\0".as_ptr() as *const c_char);
let sel_at = sel_registerName(b"objectAtIndex:\0".as_ptr() as *const c_char);
let sel_req = sel_registerName(
b"requestGeometryUpdateWithPreferences:errorHandler:\0".as_ptr() as *const c_char,
);
let sel_responds = sel_registerName(b"respondsToSelector:\0".as_ptr() as *const c_char);
let msg_responds: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) -> bool =
std::mem::transmute(objc_msgSend as *const ());
let count = msg0_usize(arr, sel_count);
for i in 0..count {
let scene = msg1_usize(arr, sel_at, i);
if !scene.is_null() && msg_responds(scene, sel_responds, sel_req) {
msg2_ptr(scene, sel_req, prefs, std::ptr::null_mut());
}
}
}
}
}
}
// 2. Fallback: UIDevice setValue:forKey:@"orientation" + attemptRotationToDeviceOrientation
let dev_cls = objc_getClass(b"UIDevice\0".as_ptr() as *const c_char);
let num_cls = objc_getClass(b"NSNumber\0".as_ptr() as *const c_char);
let str_cls = objc_getClass(b"NSString\0".as_ptr() as *const c_char);
let vc_cls = objc_getClass(b"UIViewController\0".as_ptr() as *const c_char);
if !dev_cls.is_null() && !num_cls.is_null() && !str_cls.is_null() {
let sel_cur = sel_registerName(b"currentDevice\0".as_ptr() as *const c_char);
let dev = msg0(dev_cls, sel_cur);
let sel_num = sel_registerName(b"numberWithInteger:\0".as_ptr() as *const c_char);
let orient_val: usize = if landscape { 3 } else { 1 };
let num = msg1_usize(num_cls, sel_num, orient_val);
let sel_str = sel_registerName(b"stringWithUTF8String:\0".as_ptr() as *const c_char);
let msg_str: unsafe extern "C" fn(*mut c_void, *mut c_void, *const c_char) -> *mut c_void =
std::mem::transmute(objc_msgSend as *const ());
let key = msg_str(str_cls, sel_str, b"orientation\0".as_ptr() as *const c_char);
if !dev.is_null() && !num.is_null() && !key.is_null() {
let sel_set_val = sel_registerName(b"setValue:forKey:\0".as_ptr() as *const c_char);
msg2_ptr(dev, sel_set_val, num, key);
}
if !vc_cls.is_null() {
let sel_attempt = sel_registerName(b"attemptRotationToDeviceOrientation\0".as_ptr() as *const c_char);
msg1_void(vc_cls, sel_attempt);
}
}
}
}
/* ───────────────────────── 会话 ───────────────────────── */
#[tauri::command]
+2 -6
View File
@@ -29,7 +29,7 @@ fn allowed_dev_host(host: &str) -> bool {
fn allowed_frontend_origin(origin: &str) -> bool {
if matches!(
origin,
"tauri://localhost" | "http://tauri.localhost" | "https://tauri.localhost" | "null"
"tauri://localhost" | "http://tauri.localhost" | "https://tauri.localhost"
) {
return true;
}
@@ -121,11 +121,6 @@ pub fn run() {
.insert("Access-Control-Allow-Origin", origin);
resp.headers_mut()
.insert("Vary", tauri::http::HeaderValue::from_static("Origin"));
} else {
resp.headers_mut().insert(
"Access-Control-Allow-Origin",
tauri::http::HeaderValue::from_static("*"),
);
}
responder.respond(resp);
});
@@ -145,6 +140,7 @@ pub fn run() {
api::line_probe,
api::check_update,
api::open_url,
api::set_orientation,
api::api_login,
api::api_register,
api::api_logout,
+12 -53
View File
@@ -30,16 +30,9 @@ static IMG_CACHE: Lazy<Mutex<HashMap<String, Arc<Vec<u8>>>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
static PL_CACHE: Lazy<Mutex<HashMap<String, Arc<String>>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
static KEY_CACHE: Lazy<Mutex<HashMap<String, (String, Arc<Vec<u8>>)>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
static SEG_CACHE: Lazy<Mutex<HashMap<String, (String, Arc<Vec<u8>>)>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
const IMG_CACHE_MAX: usize = 240;
const PL_CACHE_MAX: usize = 60;
const KEY_CACHE_MAX: usize = 120;
const SEG_CACHE_MAX: usize = 36;
const SEG_CACHE_MAX_BYTES: usize = 4 * 1024 * 1024;
fn cache_put<V: Clone>(map: &Mutex<HashMap<String, V>>, key: String, val: V, max: usize) {
let mut m = map.lock().unwrap();
@@ -191,10 +184,9 @@ fn rewrite_uri_attr(line: &str, prefix: &str) -> String {
map_uri_attributes(line, |uri| format!("{prefix}/seg?u={}", enc(uri)))
}
/// 取 m3u8 并把 key / 子列表 / 分片地址改写到本地协议。
/// 取 m3u8 并把 key / 分片地址改写到本地协议。
pub async fn load_playlist(url: &str, prefix: &str) -> Result<Arc<String>, String> {
let cache_key = format!("{prefix}|{url}");
if let Some(hit) = PL_CACHE.lock().unwrap().get(&cache_key).cloned() {
if let Some(hit) = PL_CACHE.lock().unwrap().get(url).cloned() {
return Ok(hit);
}
let (_, bytes) = fetch_bytes(url).await?;
@@ -203,7 +195,7 @@ pub async fn load_playlist(url: &str, prefix: &str) -> Result<Arc<String>, Strin
return Err("响应不是 HLS 播放列表".into());
}
let arc = Arc::new(rewrite_playlist(url, text, prefix));
cache_put(&PL_CACHE, cache_key, arc.clone(), PL_CACHE_MAX);
cache_put(&PL_CACHE, url.to_string(), arc.clone(), PL_CACHE_MAX);
Ok(arc)
}
@@ -259,54 +251,23 @@ fn absolutize_uri_in_attr(base: &str, line: &str) -> String {
map_uri_attributes(line, |uri| absolutize(base, uri))
}
/// 加载分片或 AES Key(带内存缓存,避免拖动进度条时重复拉取同一 Key 或分片导致卡顿)。
pub async fn load_segment(url: &str) -> Result<(Option<String>, Arc<Vec<u8>>), String> {
if let Some((ctype, hit)) = KEY_CACHE.lock().unwrap().get(url).cloned() {
return Ok((Some(ctype), hit));
}
if let Some((ctype, hit)) = SEG_CACHE.lock().unwrap().get(url).cloned() {
return Ok((Some(ctype), hit));
}
let (ctype, bytes) = fetch_bytes(url).await?;
let mime = ctype.clone().unwrap_or_else(|| {
let p = url.split('?').next().unwrap_or(url).to_ascii_lowercase();
if p.ends_with(".ts") {
"video/mp2t".to_string()
} else if p.ends_with(".m4s") || p.ends_with(".mp4") {
"video/mp4".to_string()
} else {
"application/octet-stream".to_string()
}
});
let len = bytes.len();
let arc = Arc::new(bytes);
if len <= 65536 {
cache_put(&KEY_CACHE, url.to_string(), (mime.clone(), arc.clone()), KEY_CACHE_MAX);
} else if len <= SEG_CACHE_MAX_BYTES {
cache_put(&SEG_CACHE, url.to_string(), (mime.clone(), arc.clone()), SEG_CACHE_MAX);
}
Ok((Some(mime), arc))
}
/// 清空封面 / 播放列表 / 分片内存缓存
/// 清空封面 / 播放列表内存缓存
pub fn clear_caches() {
if let Ok(mut g) = IMG_CACHE.lock() { g.clear(); }
if let Ok(mut g) = PL_CACHE.lock() { g.clear(); }
if let Ok(mut g) = KEY_CACHE.lock() { g.clear(); }
if let Ok(mut g) = SEG_CACHE.lock() { g.clear(); }
if let Ok(mut g) = IMG_CACHE.lock() {
g.clear();
}
if let Ok(mut g) = PL_CACHE.lock() {
g.clear();
}
}
/* ───────────────────────── 协议响应 ───────────────────────── */
fn respond(status: u16, ctype: &str, body: Vec<u8>, cache: &str) -> Response<Vec<u8>> {
let len = body.len().to_string();
Response::builder()
.status(StatusCode::from_u16(status).unwrap_or(StatusCode::OK))
.header("Content-Type", ctype)
.header("Content-Length", len)
.header("Accept-Ranges", "bytes")
.header("X-Content-Type-Options", "nosniff")
.header("Access-Control-Expose-Headers", "Content-Length, Content-Type, Accept-Ranges")
.header("Cache-Control", cache)
.body(body)
.unwrap_or_else(|_| Response::new(Vec::new()))
@@ -389,10 +350,8 @@ pub async fn handle(path: &str, query: &str, origin: &str) -> Response<Vec<u8>>
),
Err(e) => respond(502, "text/plain", e.into_bytes(), "no-store"),
},
"/seg" => match load_segment(&target).await {
Ok((ctype, bytes)) => {
segment_response(&target, ctype.as_deref(), bytes.as_ref().clone(), origin)
}
"/seg" => match fetch_bytes(&target).await {
Ok((ctype, bytes)) => segment_response(&target, ctype.as_deref(), bytes, origin),
Err(e) => respond(502, "text/plain", e.into_bytes(), "no-store"),
},
_ => respond(404, "text/plain", b"not found".to_vec(), "no-store"),
+21 -4
View File
@@ -34,6 +34,7 @@
}
.plat-windows { --r-sm: 4px; --r-md: 8px; --r-lg: 8px; --r-ctl: 4px; --nav-h: 0px; }
.plat-macos { --r-sm: 6px; --r-md: 10px; --r-lg: 12px; --r-ctl: 6px; --nav-h: 0px; }
.plat-ios { --r-sm: 10px; --r-md: 14px; --r-lg: 18px; --r-ctl: 999px; -webkit-touch-callout: none; }
html.theme-light {
color-scheme: light;
--p-canvas: #f3f3f3;
@@ -152,7 +153,7 @@ html.theme-light .elev-3 { box-shadow: 0 10px 30px rgba(0,0,0,.1); }
.a-panel-out { animation: kf-panel-out .2s ease-in both; }
@keyframes kf-scale-in { from{opacity:0;transform:scale(.92)} to{opacity:1;transform:scale(1)} }
.a-scale-in { animation: kf-scale-in .22s cubic-bezier(.2,0,0,1) both; }
@keyframes kf-page-in { from{transform:translateX(100%)} to{transform:translateX(0)} }
@keyframes kf-page-in { from{transform:translateX(100%)} to{transform:none} }
.a-page-in { animation: kf-page-in .3s cubic-bezier(.2,0,0,1) both; }
@keyframes kf-page-out { from{transform:translateX(0)} to{transform:translateX(100%)} }
.a-page-out { animation: kf-page-out .22s cubic-bezier(.3,0,.8,.15) both; }
@@ -250,7 +251,8 @@ input[type="range"].hg-range::-webkit-slider-thumb { -webkit-appearance: none; w
原生全屏时红绿灯被系统隐藏(is-native-fs),让位收回,避免留一条空白。 */
.plat-macos .win-titlebar { padding-left: 84px; }
.plat-macos .win-titlebar.is-native-fs { padding-left: 14px; }
.plat-android .win-main { background: var(--p-bg); overflow: hidden; }
.plat-android .win-main,
.plat-ios .win-main { background: var(--p-bg); overflow: hidden; }
.win-page-in { animation: kf-page-in .26s cubic-bezier(.2,0,0,1) both; }
.win-page-out { animation: kf-page-out .2s ease-in both; }
@@ -483,13 +485,28 @@ input[type="range"].hg-range::-webkit-slider-thumb { -webkit-appearance: none; w
.player-surface .player-root,
.player-root { background: #15181d; }
/* —— 安卓沉浸全屏:横屏视频把舞台旋转 90°(不依赖系统旋转) —— */
/* —— 移动端沉浸全屏:固定铺满全屏视口,脱离外层滚动容器 scrollTop 影响 —— */
.fs-immersive {
position: fixed;
inset: 0;
width: 100vw; height: 100vh;
width: 100dvw; height: 100dvh;
z-index: 45;
background: #000;
overflow: hidden;
}
/* —— 移动端沉浸全屏(系统仍处于竖屏时的 CSS 90° 旋转兜底) —— */
.fs-landscape {
position: absolute;
position: fixed;
top: 50%; left: 50%; right: auto; bottom: auto;
width: 100vh; height: 100vw;
width: 100dvh; height: 100dvw;
transform: translate(-50%, -50%) rotate(90deg);
transform-origin: center center;
z-index: 45;
background: #000;
overflow: hidden;
}
.desktop-dock-panel {