refactor: 简化运行时与服务协调

提取运行时生命周期管理,集中远程访问与 USB 协调,并缩小 Web 路由状态依赖。
This commit is contained in:
mofeng-git
2026-08-26 10:53:08 +08:00
parent 827d24fde4
commit 37af369b43
24 changed files with 2016 additions and 1771 deletions

View File

@@ -1,20 +1,22 @@
use axum::{extract::State, Json};
use std::sync::Arc;
use crate::error::Result;
use crate::state::AppState;
use crate::web::state::RemoteAccessApiState;
use super::apply::{apply_rtsp_config, try_apply_lock, ConfigApplyOptions};
use super::types::{RtspConfigResponse, RtspConfigUpdate, RtspStatusResponse};
use crate::runtime::{try_apply_lock, ConfigApplyOptions};
fn validate_candidate(state: &Arc<AppState>, config: &crate::config::RtspConfig) -> Result<()> {
fn validate_candidate(
state: &RemoteAccessApiState,
config: &crate::config::RtspConfig,
) -> Result<()> {
let mut candidate = state.config.get().as_ref().clone();
candidate.rtsp = config.clone();
crate::video::codec_constraints::validate_third_party_codec_compatibility(&candidate)
}
async fn persist_and_apply(
state: &Arc<AppState>,
state: &RemoteAccessApiState,
old_config: crate::config::RtspConfig,
new_config: crate::config::RtspConfig,
) -> Result<crate::config::RtspConfig> {
@@ -26,31 +28,31 @@ async fn persist_and_apply(
})
.await?;
let stored_config = state.config.get().rtsp.clone();
apply_rtsp_config(
state,
&old_config,
&stored_config,
ConfigApplyOptions::preserving_service_state(),
)
.await?;
state
.coordinator
.apply_rtsp(
&old_config,
&stored_config,
ConfigApplyOptions::preserving_service_state(),
)
.await?;
Ok(stored_config)
}
async fn current_status(state: &Arc<AppState>) -> crate::rtsp::RtspServiceStatus {
let guard = state.rtsp.read().await;
if let Some(ref service) = *guard {
service.status().await
} else {
crate::rtsp::RtspServiceStatus::Stopped
}
async fn current_status(state: &RemoteAccessApiState) -> crate::rtsp::RtspServiceStatus {
state.coordinator.rtsp_status().await
}
pub async fn get_rtsp_config(State(state): State<Arc<AppState>>) -> Json<RtspConfigResponse> {
pub async fn get_rtsp_config(
State(state): State<RemoteAccessApiState>,
) -> Json<RtspConfigResponse> {
let config = state.config.get();
Json(RtspConfigResponse::from(&config.rtsp))
}
pub async fn get_rtsp_status(State(state): State<Arc<AppState>>) -> Json<RtspStatusResponse> {
pub async fn get_rtsp_status(
State(state): State<RemoteAccessApiState>,
) -> Json<RtspStatusResponse> {
let config = state.config.get().rtsp.clone();
let status = current_status(&state).await;
@@ -58,12 +60,12 @@ pub async fn get_rtsp_status(State(state): State<Arc<AppState>>) -> Json<RtspSta
}
pub async fn update_rtsp_config(
State(state): State<Arc<AppState>>,
State(state): State<RemoteAccessApiState>,
Json(req): Json<RtspConfigUpdate>,
) -> Result<Json<RtspConfigResponse>> {
req.validate()?;
let _apply_guard = try_apply_lock(&state.config_apply_locks.rtsp, "rtsp")?;
let _apply_guard = try_apply_lock(&state.rtsp_apply_lock, "rtsp")?;
let old_config = state.config.get().rtsp.clone();
let mut merged_config = old_config.clone();
req.apply_to(&mut merged_config);
@@ -73,40 +75,42 @@ pub async fn update_rtsp_config(
}
pub async fn start_rtsp_service(
State(state): State<Arc<AppState>>,
State(state): State<RemoteAccessApiState>,
) -> Result<Json<RtspStatusResponse>> {
let _apply_guard = try_apply_lock(&state.config_apply_locks.rtsp, "rtsp")?;
let _apply_guard = try_apply_lock(&state.rtsp_apply_lock, "rtsp")?;
let stored_config = state.config.get().rtsp.clone();
let runtime_config = state.runtime_third_party_config().await.rtsp;
let runtime_config = state.coordinator.runtime_config().await.rtsp;
let mut start_config = stored_config.clone();
start_config.enabled = true;
apply_rtsp_config(
&state,
&runtime_config,
&start_config,
ConfigApplyOptions::runtime_only(),
)
.await?;
state
.coordinator
.apply_rtsp(
&runtime_config,
&start_config,
ConfigApplyOptions::runtime_only(),
)
.await?;
let status = current_status(&state).await;
Ok(Json(RtspStatusResponse::new(&stored_config, status)))
}
pub async fn stop_rtsp_service(
State(state): State<Arc<AppState>>,
State(state): State<RemoteAccessApiState>,
) -> Result<Json<RtspStatusResponse>> {
let _apply_guard = try_apply_lock(&state.config_apply_locks.rtsp, "rtsp")?;
let _apply_guard = try_apply_lock(&state.rtsp_apply_lock, "rtsp")?;
let stored_config = state.config.get().rtsp.clone();
let runtime_config = state.runtime_third_party_config().await.rtsp;
let runtime_config = state.coordinator.runtime_config().await.rtsp;
let mut stop_config = stored_config.clone();
stop_config.enabled = false;
apply_rtsp_config(
&state,
&runtime_config,
&stop_config,
ConfigApplyOptions::runtime_only(),
)
.await?;
state
.coordinator
.apply_rtsp(
&runtime_config,
&stop_config,
ConfigApplyOptions::runtime_only(),
)
.await?;
let status = current_status(&state).await;
Ok(Json(RtspStatusResponse::new(&stored_config, status)))