401 lines
11 KiB
Rust
401 lines
11 KiB
Rust
use crate::error::AppError;
|
||
use crate::services::{HlsVariant, MpvState};
|
||
use crate::state::AppState;
|
||
use serde::{Deserialize, Serialize};
|
||
use std::collections::HashMap;
|
||
use std::sync::Arc;
|
||
use tauri::{Emitter, State};
|
||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct InstalledPlayerInfo {
|
||
pub name: String,
|
||
pub path: Option<String>,
|
||
pub available: bool,
|
||
}
|
||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct StreamPlaybackInfo {
|
||
pub channel_name: String,
|
||
pub is_live: bool,
|
||
pub playback_url: Option<String>,
|
||
pub embed_url: String,
|
||
pub player_mode: String,
|
||
pub player_type: String,
|
||
pub title: String,
|
||
pub viewers: i64,
|
||
pub started_at: Option<String>,
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn detect_installed_players(
|
||
state: State<'_, AppState>,
|
||
) -> Result<Vec<InstalledPlayerInfo>, AppError> {
|
||
let cfg = state.config.read().await;
|
||
|
||
let vlc_path = state.player.find_vlc(Some(&cfg.vlc_path));
|
||
let mpv_path = state.player.find_mpv(Some(&cfg.mpv_path));
|
||
let streamlink_path = state.player.find_streamlink(Some(&cfg.streamlink_path));
|
||
|
||
Ok(vec![
|
||
InstalledPlayerInfo {
|
||
name: "Internal Native Player (libmpv D3D11VA)".to_string(),
|
||
path: None,
|
||
available: true,
|
||
},
|
||
InstalledPlayerInfo {
|
||
name: "VLC Media Player".to_string(),
|
||
available: vlc_path.is_some(),
|
||
path: vlc_path,
|
||
},
|
||
InstalledPlayerInfo {
|
||
name: "MPV Player".to_string(),
|
||
available: mpv_path.is_some(),
|
||
path: mpv_path,
|
||
},
|
||
InstalledPlayerInfo {
|
||
name: "Streamlink".to_string(),
|
||
available: streamlink_path.is_some(),
|
||
path: streamlink_path,
|
||
},
|
||
])
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn play_stream(
|
||
channel_name: String,
|
||
quality: Option<String>,
|
||
force_external: Option<bool>,
|
||
state: State<'_, AppState>,
|
||
) -> Result<StreamPlaybackInfo, AppError> {
|
||
let clean = channel_name
|
||
.trim()
|
||
.trim_start_matches('@')
|
||
.trim_start_matches("https://kick.com/")
|
||
.trim_start_matches("http://kick.com/")
|
||
.trim_start_matches("kick.com/")
|
||
.trim_end_matches('/')
|
||
.to_lowercase();
|
||
|
||
if clean.is_empty() {
|
||
return Err(AppError::InvalidInput("Kanal adı belirtilmedi".to_string()));
|
||
}
|
||
|
||
let channel_data = state.api.get_channel(&clean).await?;
|
||
let is_live = channel_data.livestream.is_some();
|
||
let playback_url = channel_data.playback_url.clone();
|
||
let started_at = channel_data
|
||
.livestream
|
||
.as_ref()
|
||
.and_then(|l| l.normalized_started_at());
|
||
let title = channel_data
|
||
.livestream
|
||
.as_ref()
|
||
.and_then(|l| l.session_title.clone())
|
||
.unwrap_or_default();
|
||
let viewers = channel_data
|
||
.livestream
|
||
.as_ref()
|
||
.and_then(|l| l.viewer_count)
|
||
.unwrap_or(0);
|
||
|
||
let (player_type, vlc_path, mpv_path, streamlink_path, preferred_quality, extra_args) = {
|
||
let cfg = state.config.read().await;
|
||
(
|
||
cfg.player_type.clone(),
|
||
cfg.vlc_path.clone(),
|
||
cfg.mpv_path.clone(),
|
||
cfg.streamlink_path.clone(),
|
||
cfg.default_quality.clone(),
|
||
cfg.custom_player_args.clone(),
|
||
)
|
||
};
|
||
|
||
let target_quality = quality.unwrap_or(preferred_quality);
|
||
let use_external = force_external.unwrap_or(player_type.to_lowercase() != "internal");
|
||
|
||
let embed_url = format!("https://player.kick.com/{}", clean);
|
||
|
||
if use_external {
|
||
let raw_url = playback_url.as_deref().unwrap_or(&embed_url);
|
||
let resolved_url = state
|
||
.api
|
||
.resolve_quality_url(raw_url, &target_quality)
|
||
.await;
|
||
|
||
let player_path = match player_type.to_lowercase().as_str() {
|
||
"mpv" => Some(mpv_path.as_str()),
|
||
_ => Some(vlc_path.as_str()),
|
||
};
|
||
|
||
state
|
||
.player
|
||
.play_external(crate::services::ExternalPlayerOptions {
|
||
channel_name: &clean,
|
||
player_type: &player_type,
|
||
stream_url: &resolved_url,
|
||
player_path,
|
||
streamlink_path: Some(&streamlink_path),
|
||
quality: &target_quality,
|
||
extra_args: Some(&extra_args),
|
||
})?;
|
||
}
|
||
|
||
{
|
||
let mut active = state.active_stream.write().await;
|
||
*active = Some(clean.clone());
|
||
}
|
||
|
||
let info = StreamPlaybackInfo {
|
||
channel_name: clean.clone(),
|
||
is_live,
|
||
playback_url,
|
||
embed_url,
|
||
player_mode: if use_external {
|
||
"external".to_string()
|
||
} else {
|
||
"internal".to_string()
|
||
},
|
||
player_type,
|
||
title,
|
||
viewers,
|
||
started_at,
|
||
};
|
||
|
||
let _ = state.app_handle.emit("stream-playback-changed", &info);
|
||
|
||
Ok(info)
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn get_hls_variants(
|
||
master_url: String,
|
||
state: State<'_, AppState>,
|
||
) -> Result<Vec<HlsVariant>, AppError> {
|
||
state.api.get_hls_variants(&master_url).await
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_load_stream(
|
||
url: String,
|
||
headers: Option<HashMap<String, String>>,
|
||
slot_id: Option<String>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
let url_clone = url;
|
||
let headers_clone = headers;
|
||
let slot_clone = slot_id;
|
||
let mpv_inner = Arc::clone(mpv.inner());
|
||
tokio::task::spawn_blocking(move || {
|
||
mpv_inner.load_stream(&url_clone, headers_clone.as_ref(), slot_clone.as_deref())
|
||
})
|
||
.await
|
||
.map_err(|e| AppError::General(format!("mpv load_stream görev hatası: {}", e)))?
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_set_bounds(
|
||
x: f64,
|
||
y: f64,
|
||
width: f64,
|
||
height: f64,
|
||
scale_factor: f64,
|
||
slot_id: Option<String>,
|
||
above_parent: Option<bool>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.set_bounds(
|
||
x,
|
||
y,
|
||
width,
|
||
height,
|
||
scale_factor,
|
||
slot_id.as_deref(),
|
||
above_parent,
|
||
)
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_set_visible(
|
||
visible: bool,
|
||
slot_id: Option<String>,
|
||
above_parent: Option<bool>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.set_visible(visible, slot_id.as_deref(), above_parent)
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_set_pause(
|
||
paused: bool,
|
||
slot_id: Option<String>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.set_pause(paused, slot_id.as_deref())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_set_volume(
|
||
volume: f64,
|
||
slot_id: Option<String>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.set_volume(volume, slot_id.as_deref())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_set_mute(
|
||
muted: bool,
|
||
slot_id: Option<String>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.set_mute(muted, slot_id.as_deref())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_seek_live(
|
||
slot_id: Option<String>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.seek_live(slot_id.as_deref())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_seek_relative(
|
||
delta_secs: f64,
|
||
slot_id: Option<String>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.seek_relative(delta_secs, slot_id.as_deref())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_seek_behind_live(
|
||
behind_secs: f64,
|
||
slot_id: Option<String>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.seek_behind_live(behind_secs, slot_id.as_deref())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_load_vod_dvr(
|
||
channel_name: String,
|
||
target_secs: f64,
|
||
preferred_height: Option<u32>,
|
||
slot_id: Option<String>,
|
||
state: State<'_, AppState>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
let dvr = state
|
||
.api
|
||
.get_live_dvr_source(&channel_name, preferred_height)
|
||
.await?;
|
||
let mpv_inner = Arc::clone(mpv.inner());
|
||
let slot_clone = slot_id;
|
||
tokio::task::spawn_blocking(move || {
|
||
mpv_inner.load_vod_dvr(
|
||
&dvr.playlist_url,
|
||
target_secs,
|
||
dvr.total_secs_hint,
|
||
slot_clone.as_deref(),
|
||
)
|
||
})
|
||
.await
|
||
.map_err(|e| AppError::General(format!("mpv load_vod_dvr görev hatası: {}", e)))?
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_update_live_url(
|
||
url: String,
|
||
slot_id: Option<String>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.update_live_url(&url, slot_id.as_deref())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_stop(
|
||
slot_id: Option<String>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
mpv.stop(slot_id.as_deref())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn mpv_stop_all(mpv: State<'_, Arc<MpvState>>) -> Result<(), AppError> {
|
||
mpv.stop_all();
|
||
Ok(())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn stop_stream(
|
||
channel_name: Option<String>,
|
||
state: State<'_, AppState>,
|
||
mpv: State<'_, Arc<MpvState>>,
|
||
) -> Result<(), AppError> {
|
||
if let Some(name) = channel_name {
|
||
let clean = name.trim().to_lowercase();
|
||
state.player.stop(&clean);
|
||
let _ = mpv.stop(Some("main"));
|
||
let _ = mpv.set_visible(false, Some("main"), Some(false));
|
||
|
||
let mut active = state.active_stream.write().await;
|
||
if active.as_deref() == Some(&clean) {
|
||
*active = None;
|
||
}
|
||
} else {
|
||
state.player.stop_all();
|
||
mpv.stop_all();
|
||
let mut active = state.active_stream.write().await;
|
||
*active = None;
|
||
}
|
||
|
||
let _ = state.app_handle.emit("stream-stopped", ());
|
||
Ok(())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub async fn get_stream_info(
|
||
channel_name: String,
|
||
state: State<'_, AppState>,
|
||
) -> Result<StreamPlaybackInfo, AppError> {
|
||
let clean = channel_name.trim().to_lowercase();
|
||
let channel_data = state.api.get_channel(&clean).await?;
|
||
let is_live = channel_data.livestream.is_some();
|
||
let playback_url = channel_data.playback_url.clone();
|
||
let started_at = channel_data
|
||
.livestream
|
||
.as_ref()
|
||
.and_then(|l| l.normalized_started_at());
|
||
let title = channel_data
|
||
.livestream
|
||
.as_ref()
|
||
.and_then(|l| l.session_title.clone())
|
||
.unwrap_or_default();
|
||
let viewers = channel_data
|
||
.livestream
|
||
.as_ref()
|
||
.and_then(|l| l.viewer_count)
|
||
.unwrap_or(0);
|
||
|
||
let player_type = {
|
||
let cfg = state.config.read().await;
|
||
cfg.player_type.clone()
|
||
};
|
||
|
||
Ok(StreamPlaybackInfo {
|
||
channel_name: clean.clone(),
|
||
is_live,
|
||
playback_url,
|
||
embed_url: format!("https://player.kick.com/{}", clean),
|
||
player_mode: if player_type.to_lowercase() == "internal" {
|
||
"internal".to_string()
|
||
} else {
|
||
"external".to_string()
|
||
},
|
||
player_type,
|
||
title,
|
||
viewers,
|
||
started_at,
|
||
})
|
||
}
|