Compare commits

...

5 Commits

Author SHA1 Message Date
mofeng-git
eeb41159b7 build: 增加硬件编码所需驱动依赖 2026-03-22 20:19:30 +08:00
mofeng-git
24a10aa222 feat: 支持硬件编码能力测试,otg 自检修改为需要手动执行 2026-03-22 20:19:30 +08:00
mofeng-git
c119db4908 perf: 编码器探测测试分辨率由 1080p 调整为 720p 2026-03-22 20:19:30 +08:00
mofeng-git
0db287bf55 refactor: 重构 ffmpeg 编码器探测模块 2026-03-22 20:19:30 +08:00
mofeng-git
e229f35777 fix(web): 修复控制台全屏视频时鼠标定位偏移问题 2026-03-22 20:19:30 +08:00
24 changed files with 1347 additions and 586 deletions

View File

@@ -12,7 +12,8 @@ ARG TARGETPLATFORM
# Install runtime dependencies in a single layer # Install runtime dependencies in a single layer
# All codec libraries (libx264, libx265, libopus) are now statically linked # All codec libraries (libx264, libx265, libopus) are now statically linked
# Only hardware acceleration drivers and core system libraries remain dynamic # Only hardware acceleration drivers and core system libraries remain dynamic
RUN apt-get update && \ RUN sed -i 's/ main$/ main contrib non-free/' /etc/apt/sources.list && \
apt-get update && \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
# Core runtime (all platforms) - no codec libs needed # Core runtime (all platforms) - no codec libs needed
ca-certificates \ ca-certificates \
@@ -24,7 +25,8 @@ RUN apt-get update && \
# Platform-specific hardware acceleration # Platform-specific hardware acceleration
if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \ if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
libva2 libva-drm2 libva-x11-2 libx11-6 libxcb1 libxau6 libxdmcp6 libmfx1; \ libva2 libva-drm2 libva-x11-2 libx11-6 libxcb1 libxau6 libxdmcp6 libmfx1 \
i965-va-driver-shaders intel-media-va-driver-non-free; \
elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
libdrm2 libva2; \ libdrm2 libva2; \

View File

@@ -12,7 +12,8 @@ ARG TARGETPLATFORM
# Install runtime dependencies in a single layer # Install runtime dependencies in a single layer
# All codec libraries (libx264, libx265, libopus) are now statically linked # All codec libraries (libx264, libx265, libopus) are now statically linked
# Only hardware acceleration drivers and core system libraries remain dynamic # Only hardware acceleration drivers and core system libraries remain dynamic
RUN apt-get update && \ RUN sed -i 's/ main$/ main contrib non-free/' /etc/apt/sources.list && \
apt-get update && \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
# Core runtime (all platforms) - no codec libs needed # Core runtime (all platforms) - no codec libs needed
ca-certificates \ ca-certificates \
@@ -24,7 +25,8 @@ RUN apt-get update && \
# Platform-specific hardware acceleration # Platform-specific hardware acceleration
if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \ if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
libva2 libva-drm2 libva-x11-2 libx11-6 libxcb1 libxau6 libxdmcp6 libmfx1; \ libva2 libva-drm2 libva-x11-2 libx11-6 libxcb1 libxau6 libxdmcp6 libmfx1 \
i965-va-driver-shaders intel-media-va-driver-non-free; \
elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
libdrm2 libva2; \ libdrm2 libva2; \

View File

@@ -7,6 +7,8 @@ Wants=network-online.target
[Service] [Service]
Type=simple Type=simple
User=root User=root
# Example for older Intel GPUs:
# Environment=LIBVA_DRIVER_NAME=i965
ExecStart=/usr/bin/one-kvm ExecStart=/usr/bin/one-kvm
Restart=on-failure Restart=on-failure
RestartSec=5 RestartSec=5

View File

@@ -126,7 +126,7 @@ EOF
# Create control file # Create control file
BASE_DEPS="libc6 (>= 2.31), libgcc-s1, libstdc++6, libasound2 (>= 1.1), libdrm2 (>= 2.4)" BASE_DEPS="libc6 (>= 2.31), libgcc-s1, libstdc++6, libasound2 (>= 1.1), libdrm2 (>= 2.4)"
AMD64_DEPS="libva2 (>= 2.0), libva-drm2 (>= 2.10), libva-x11-2 (>= 2.10), libmfx1 (>= 21.1), libx11-6 (>= 1.6), libxcb1 (>= 1.14)" AMD64_DEPS="libva2 (>= 2.0), libva-drm2 (>= 2.10), libva-x11-2 (>= 2.10), libmfx1 (>= 21.1), libx11-6 (>= 1.6), libxcb1 (>= 1.14), i965-va-driver-shaders (>= 2.4), intel-media-va-driver-non-free (>= 21.1)"
DEPS="$BASE_DEPS" DEPS="$BASE_DEPS"
if [ "$DEB_ARCH" = "amd64" ]; then if [ "$DEB_ARCH" = "amd64" ]; then
DEPS="$DEPS, $AMD64_DEPS" DEPS="$DEPS, $AMD64_DEPS"

View File

@@ -15,12 +15,412 @@ use std::{
slice, slice,
}; };
use super::Priority;
#[cfg(any(windows, target_os = "linux"))] #[cfg(any(windows, target_os = "linux"))]
use crate::common::Driver; use crate::common::Driver;
/// Timeout for encoder test in milliseconds /// Timeout for encoder test in milliseconds
const TEST_TIMEOUT_MS: u64 = 3000; const TEST_TIMEOUT_MS: u64 = 3000;
const PRIORITY_NVENC: i32 = 0;
const PRIORITY_QSV: i32 = 1;
const PRIORITY_AMF: i32 = 2;
const PRIORITY_RKMPP: i32 = 3;
const PRIORITY_VAAPI: i32 = 4;
const PRIORITY_V4L2M2M: i32 = 5;
#[derive(Clone, Copy)]
struct CandidateCodecSpec {
name: &'static str,
format: DataFormat,
priority: i32,
}
fn push_candidate(codecs: &mut Vec<CodecInfo>, candidate: CandidateCodecSpec) {
codecs.push(CodecInfo {
name: candidate.name.to_owned(),
format: candidate.format,
priority: candidate.priority,
..Default::default()
});
}
#[cfg(target_os = "linux")]
fn linux_support_vaapi() -> bool {
let entries = match std::fs::read_dir("/dev/dri") {
Ok(entries) => entries,
Err(_) => return false,
};
entries.flatten().any(|entry| {
entry
.file_name()
.to_str()
.map(|name| name.starts_with("renderD"))
.unwrap_or(false)
})
}
#[cfg(not(target_os = "linux"))]
fn linux_support_vaapi() -> bool {
false
}
#[cfg(target_os = "linux")]
fn linux_support_rkmpp() -> bool {
extern "C" {
fn linux_support_rkmpp() -> c_int;
}
unsafe { linux_support_rkmpp() == 0 }
}
#[cfg(not(target_os = "linux"))]
fn linux_support_rkmpp() -> bool {
false
}
#[cfg(target_os = "linux")]
fn linux_support_v4l2m2m() -> bool {
extern "C" {
fn linux_support_v4l2m2m() -> c_int;
}
unsafe { linux_support_v4l2m2m() == 0 }
}
#[cfg(not(target_os = "linux"))]
fn linux_support_v4l2m2m() -> bool {
false
}
#[cfg(any(windows, target_os = "linux"))]
fn enumerate_candidate_codecs(ctx: &EncodeContext) -> Vec<CodecInfo> {
use log::debug;
let mut codecs = Vec::new();
let contains = |_vendor: Driver, _format: DataFormat| {
// Without VRAM feature, we can't check SDK availability.
// Keep the prefilter coarse and let FFmpeg validation do the real check.
true
};
let (nv, amf, intel) = crate::common::supported_gpu(true);
debug!(
"GPU support detected - NV: {}, AMF: {}, Intel: {}",
nv, amf, intel
);
if nv && contains(Driver::NV, H264) {
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "h264_nvenc",
format: H264,
priority: PRIORITY_NVENC,
},
);
}
if nv && contains(Driver::NV, H265) {
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "hevc_nvenc",
format: H265,
priority: PRIORITY_NVENC,
},
);
}
if intel && contains(Driver::MFX, H264) {
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "h264_qsv",
format: H264,
priority: PRIORITY_QSV,
},
);
}
if intel && contains(Driver::MFX, H265) {
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "hevc_qsv",
format: H265,
priority: PRIORITY_QSV,
},
);
}
if amf && contains(Driver::AMF, H264) {
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "h264_amf",
format: H264,
priority: PRIORITY_AMF,
},
);
}
if amf && contains(Driver::AMF, H265) {
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "hevc_amf",
format: H265,
priority: PRIORITY_AMF,
},
);
}
if linux_support_rkmpp() {
debug!("RKMPP hardware detected, adding Rockchip encoders");
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "h264_rkmpp",
format: H264,
priority: PRIORITY_RKMPP,
},
);
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "hevc_rkmpp",
format: H265,
priority: PRIORITY_RKMPP,
},
);
}
if cfg!(target_os = "linux") && linux_support_vaapi() {
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "h264_vaapi",
format: H264,
priority: PRIORITY_VAAPI,
},
);
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "hevc_vaapi",
format: H265,
priority: PRIORITY_VAAPI,
},
);
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "vp8_vaapi",
format: VP8,
priority: PRIORITY_VAAPI,
},
);
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "vp9_vaapi",
format: VP9,
priority: PRIORITY_VAAPI,
},
);
}
if linux_support_v4l2m2m() {
debug!("V4L2 M2M hardware detected, adding V4L2 encoders");
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "h264_v4l2m2m",
format: H264,
priority: PRIORITY_V4L2M2M,
},
);
push_candidate(
&mut codecs,
CandidateCodecSpec {
name: "hevc_v4l2m2m",
format: H265,
priority: PRIORITY_V4L2M2M,
},
);
}
codecs.retain(|codec| {
!(ctx.pixfmt == AVPixelFormat::AV_PIX_FMT_YUV420P && codec.name.contains("qsv"))
});
codecs
}
#[derive(Clone, Copy)]
struct ProbePolicy {
max_attempts: usize,
request_keyframe: bool,
accept_any_output: bool,
}
impl ProbePolicy {
fn for_codec(codec_name: &str) -> Self {
if codec_name.contains("v4l2m2m") {
Self {
max_attempts: 5,
request_keyframe: true,
accept_any_output: true,
}
} else {
Self {
max_attempts: 1,
request_keyframe: false,
accept_any_output: false,
}
}
}
fn prepare_attempt(&self, encoder: &mut Encoder) {
if self.request_keyframe {
encoder.request_keyframe();
}
}
fn passed(&self, frames: &[EncodeFrame], elapsed_ms: u128) -> bool {
if elapsed_ms >= TEST_TIMEOUT_MS as u128 {
return false;
}
if self.accept_any_output {
!frames.is_empty()
} else {
frames.len() == 1 && frames[0].key == 1
}
}
}
fn log_failed_probe_attempt(
codec_name: &str,
policy: ProbePolicy,
attempt: usize,
frames: &[EncodeFrame],
elapsed_ms: u128,
) {
use log::debug;
if policy.accept_any_output {
if frames.is_empty() {
debug!(
"Encoder {} test produced no output on attempt {}",
codec_name, attempt
);
} else {
debug!(
"Encoder {} test failed on attempt {} - frames: {}, timeout: {}ms",
codec_name,
attempt,
frames.len(),
elapsed_ms
);
}
} else if frames.len() == 1 {
debug!(
"Encoder {} test failed on attempt {} - key: {}, timeout: {}ms",
codec_name, attempt, frames[0].key, elapsed_ms
);
} else {
debug!(
"Encoder {} test failed on attempt {} - wrong frame count: {}",
codec_name,
attempt,
frames.len()
);
}
}
fn validate_candidate(codec: &CodecInfo, ctx: &EncodeContext, yuv: &[u8]) -> bool {
use log::debug;
debug!("Testing encoder: {}", codec.name);
let test_ctx = EncodeContext {
name: codec.name.clone(),
mc_name: codec.mc_name.clone(),
..ctx.clone()
};
match Encoder::new(test_ctx) {
Ok(mut encoder) => {
debug!("Encoder {} created successfully", codec.name);
let policy = ProbePolicy::for_codec(&codec.name);
let mut last_err: Option<i32> = None;
for attempt in 0..policy.max_attempts {
let attempt_no = attempt + 1;
policy.prepare_attempt(&mut encoder);
let pts = (attempt as i64) * 33;
let start = std::time::Instant::now();
match encoder.encode(yuv, pts) {
Ok(frames) => {
let elapsed = start.elapsed().as_millis();
if policy.passed(frames, elapsed) {
if policy.accept_any_output {
debug!(
"Encoder {} test passed on attempt {} (frames: {})",
codec.name,
attempt_no,
frames.len()
);
} else {
debug!(
"Encoder {} test passed on attempt {}",
codec.name, attempt_no
);
}
return true;
} else {
log_failed_probe_attempt(
&codec.name,
policy,
attempt_no,
frames,
elapsed,
);
}
}
Err(err) => {
last_err = Some(err);
debug!(
"Encoder {} test attempt {} returned error: {}",
codec.name, attempt_no, err
);
}
}
}
debug!(
"Encoder {} test failed after retries{}",
codec.name,
last_err
.map(|e| format!(" (last err: {})", e))
.unwrap_or_default()
);
false
}
Err(_) => {
debug!("Failed to create encoder {}", codec.name);
false
}
}
}
fn add_software_fallback(codecs: &mut Vec<CodecInfo>) {
use log::debug;
for fallback in CodecInfo::soft().into_vec() {
if !codecs.iter().any(|codec| codec.format == fallback.format) {
debug!(
"Adding software {:?} encoder: {}",
fallback.format, fallback.name
);
codecs.push(fallback);
}
}
}
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct EncodeContext { pub struct EncodeContext {
@@ -185,305 +585,21 @@ impl Encoder {
if !(cfg!(windows) || cfg!(target_os = "linux")) { if !(cfg!(windows) || cfg!(target_os = "linux")) {
return vec![]; return vec![];
} }
let mut codecs: Vec<CodecInfo> = vec![];
#[cfg(any(windows, target_os = "linux"))]
{
let contains = |_vendor: Driver, _format: DataFormat| {
// Without VRAM feature, we can't check SDK availability
// Just return true and let FFmpeg handle the actual detection
true
};
let (_nv, amf, _intel) = crate::common::supported_gpu(true);
debug!(
"GPU support detected - NV: {}, AMF: {}, Intel: {}",
_nv, amf, _intel
);
#[cfg(windows)]
if _intel && contains(Driver::MFX, H264) {
codecs.push(CodecInfo {
name: "h264_qsv".to_owned(),
format: H264,
priority: Priority::Best as _,
..Default::default()
});
}
#[cfg(windows)]
if _intel && contains(Driver::MFX, H265) {
codecs.push(CodecInfo {
name: "hevc_qsv".to_owned(),
format: H265,
priority: Priority::Best as _,
..Default::default()
});
}
if _nv && contains(Driver::NV, H264) {
codecs.push(CodecInfo {
name: "h264_nvenc".to_owned(),
format: H264,
priority: Priority::Best as _,
..Default::default()
});
}
if _nv && contains(Driver::NV, H265) {
codecs.push(CodecInfo {
name: "hevc_nvenc".to_owned(),
format: H265,
priority: Priority::Best as _,
..Default::default()
});
}
if amf && contains(Driver::AMF, H264) {
codecs.push(CodecInfo {
name: "h264_amf".to_owned(),
format: H264,
priority: Priority::Best as _,
..Default::default()
});
}
if amf {
codecs.push(CodecInfo {
name: "hevc_amf".to_owned(),
format: H265,
priority: Priority::Best as _,
..Default::default()
});
}
#[cfg(target_os = "linux")]
{
codecs.push(CodecInfo {
name: "h264_vaapi".to_owned(),
format: H264,
priority: Priority::Good as _,
..Default::default()
});
codecs.push(CodecInfo {
name: "hevc_vaapi".to_owned(),
format: H265,
priority: Priority::Good as _,
..Default::default()
});
codecs.push(CodecInfo {
name: "vp8_vaapi".to_owned(),
format: VP8,
priority: Priority::Good as _,
..Default::default()
});
codecs.push(CodecInfo {
name: "vp9_vaapi".to_owned(),
format: VP9,
priority: Priority::Good as _,
..Default::default()
});
// Rockchip MPP hardware encoder support
use std::ffi::c_int;
extern "C" {
fn linux_support_rkmpp() -> c_int;
fn linux_support_v4l2m2m() -> c_int;
}
if unsafe { linux_support_rkmpp() } == 0 {
debug!("RKMPP hardware detected, adding Rockchip encoders");
codecs.push(CodecInfo {
name: "h264_rkmpp".to_owned(),
format: H264,
priority: Priority::Best as _,
..Default::default()
});
codecs.push(CodecInfo {
name: "hevc_rkmpp".to_owned(),
format: H265,
priority: Priority::Best as _,
..Default::default()
});
}
// V4L2 Memory-to-Memory hardware encoder support (generic ARM)
if unsafe { linux_support_v4l2m2m() } == 0 {
debug!("V4L2 M2M hardware detected, adding V4L2 encoders");
codecs.push(CodecInfo {
name: "h264_v4l2m2m".to_owned(),
format: H264,
priority: Priority::Good as _,
..Default::default()
});
codecs.push(CodecInfo {
name: "hevc_v4l2m2m".to_owned(),
format: H265,
priority: Priority::Good as _,
..Default::default()
});
}
}
}
// qsv doesn't support yuv420p
codecs.retain(|c| {
let ctx = ctx.clone();
if ctx.pixfmt == AVPixelFormat::AV_PIX_FMT_YUV420P && c.name.contains("qsv") {
return false;
}
return true;
});
let mut res = vec![]; let mut res = vec![];
#[cfg(any(windows, target_os = "linux"))]
let codecs = enumerate_candidate_codecs(&ctx);
if let Ok(yuv) = Encoder::dummy_yuv(ctx.clone()) { if let Ok(yuv) = Encoder::dummy_yuv(ctx.clone()) {
for codec in codecs { for codec in codecs {
// Skip if this format already exists in results if validate_candidate(&codec, &ctx, &yuv) {
if res res.push(codec);
.iter()
.any(|existing: &CodecInfo| existing.format == codec.format)
{
continue;
}
debug!("Testing encoder: {}", codec.name);
let c = EncodeContext {
name: codec.name.clone(),
mc_name: codec.mc_name.clone(),
..ctx
};
match Encoder::new(c) {
Ok(mut encoder) => {
debug!("Encoder {} created successfully", codec.name);
let mut passed = false;
let mut last_err: Option<i32> = None;
let is_v4l2m2m = codec.name.contains("v4l2m2m");
let max_attempts = if is_v4l2m2m { 5 } else { 1 };
for attempt in 0..max_attempts {
if is_v4l2m2m {
encoder.request_keyframe();
}
let pts = (attempt as i64) * 33; // 33ms is an approximation for 30 FPS (1000 / 30)
let start = std::time::Instant::now();
match encoder.encode(&yuv, pts) {
Ok(frames) => {
let elapsed = start.elapsed().as_millis();
if is_v4l2m2m {
if !frames.is_empty() && elapsed < TEST_TIMEOUT_MS as _ {
debug!(
"Encoder {} test passed on attempt {} (frames: {})",
codec.name,
attempt + 1,
frames.len()
);
res.push(codec.clone());
passed = true;
break;
} else if frames.is_empty() {
debug!(
"Encoder {} test produced no output on attempt {}",
codec.name,
attempt + 1
);
} else {
debug!(
"Encoder {} test failed on attempt {} - frames: {}, timeout: {}ms",
codec.name,
attempt + 1,
frames.len(),
elapsed
);
}
} else if frames.len() == 1 {
if frames[0].key == 1 && elapsed < TEST_TIMEOUT_MS as _ {
debug!(
"Encoder {} test passed on attempt {}",
codec.name,
attempt + 1
);
res.push(codec.clone());
passed = true;
break;
} else {
debug!(
"Encoder {} test failed on attempt {} - key: {}, timeout: {}ms",
codec.name,
attempt + 1,
frames[0].key,
elapsed
);
}
} else {
debug!(
"Encoder {} test failed on attempt {} - wrong frame count: {}",
codec.name,
attempt + 1,
frames.len()
);
}
}
Err(err) => {
last_err = Some(err);
debug!(
"Encoder {} test attempt {} returned error: {}",
codec.name,
attempt + 1,
err
);
}
}
}
if !passed {
debug!(
"Encoder {} test failed after retries{}",
codec.name,
last_err
.map(|e| format!(" (last err: {})", e))
.unwrap_or_default()
);
}
}
Err(_) => {
debug!("Failed to create encoder {}", codec.name);
}
} }
} }
} else { } else {
debug!("Failed to generate dummy YUV data"); debug!("Failed to generate dummy YUV data");
} }
// Add software encoders as fallback add_software_fallback(&mut res);
let soft_codecs = CodecInfo::soft();
// Add H264 software encoder if not already present
if !res.iter().any(|c| c.format == H264) {
if let Some(h264_soft) = soft_codecs.h264 {
debug!("Adding software H264 encoder: {}", h264_soft.name);
res.push(h264_soft);
}
}
// Add H265 software encoder if not already present
if !res.iter().any(|c| c.format == H265) {
if let Some(h265_soft) = soft_codecs.h265 {
debug!("Adding software H265 encoder: {}", h265_soft.name);
res.push(h265_soft);
}
}
// Add VP8 software encoder if not already present
if !res.iter().any(|c| c.format == VP8) {
if let Some(vp8_soft) = soft_codecs.vp8 {
debug!("Adding software VP8 encoder: {}", vp8_soft.name);
res.push(vp8_soft);
}
}
// Add VP9 software encoder if not already present
if !res.iter().any(|c| c.format == VP9) {
if let Some(vp9_soft) = soft_codecs.vp9 {
debug!("Adding software VP9 encoder: {}", vp9_soft.name);
res.push(vp9_soft);
}
}
res res
} }

View File

@@ -86,6 +86,40 @@ impl Default for CodecInfo {
} }
impl CodecInfo { impl CodecInfo {
pub fn software(format: DataFormat) -> Option<Self> {
match format {
H264 => Some(CodecInfo {
name: "libx264".to_owned(),
mc_name: Default::default(),
format: H264,
hwdevice: AV_HWDEVICE_TYPE_NONE,
priority: Priority::Soft as _,
}),
H265 => Some(CodecInfo {
name: "libx265".to_owned(),
mc_name: Default::default(),
format: H265,
hwdevice: AV_HWDEVICE_TYPE_NONE,
priority: Priority::Soft as _,
}),
VP8 => Some(CodecInfo {
name: "libvpx".to_owned(),
mc_name: Default::default(),
format: VP8,
hwdevice: AV_HWDEVICE_TYPE_NONE,
priority: Priority::Soft as _,
}),
VP9 => Some(CodecInfo {
name: "libvpx-vp9".to_owned(),
mc_name: Default::default(),
format: VP9,
hwdevice: AV_HWDEVICE_TYPE_NONE,
priority: Priority::Soft as _,
}),
AV1 => None,
}
}
pub fn prioritized(coders: Vec<CodecInfo>) -> CodecInfos { pub fn prioritized(coders: Vec<CodecInfo>) -> CodecInfos {
let mut h264: Option<CodecInfo> = None; let mut h264: Option<CodecInfo> = None;
let mut h265: Option<CodecInfo> = None; let mut h265: Option<CodecInfo> = None;
@@ -148,34 +182,10 @@ impl CodecInfo {
pub fn soft() -> CodecInfos { pub fn soft() -> CodecInfos {
CodecInfos { CodecInfos {
h264: Some(CodecInfo { h264: CodecInfo::software(H264),
name: "libx264".to_owned(), h265: CodecInfo::software(H265),
mc_name: Default::default(), vp8: CodecInfo::software(VP8),
format: H264, vp9: CodecInfo::software(VP9),
hwdevice: AV_HWDEVICE_TYPE_NONE,
priority: Priority::Soft as _,
}),
h265: Some(CodecInfo {
name: "libx265".to_owned(),
mc_name: Default::default(),
format: H265,
hwdevice: AV_HWDEVICE_TYPE_NONE,
priority: Priority::Soft as _,
}),
vp8: Some(CodecInfo {
name: "libvpx".to_owned(),
mc_name: Default::default(),
format: VP8,
hwdevice: AV_HWDEVICE_TYPE_NONE,
priority: Priority::Soft as _,
}),
vp9: Some(CodecInfo {
name: "libvpx-vp9".to_owned(),
mc_name: Default::default(),
format: VP9,
hwdevice: AV_HWDEVICE_TYPE_NONE,
priority: Priority::Soft as _,
}),
av1: None, av1: None,
} }
} }
@@ -191,6 +201,23 @@ pub struct CodecInfos {
} }
impl CodecInfos { impl CodecInfos {
pub fn into_vec(self) -> Vec<CodecInfo> {
let mut codecs = Vec::new();
if let Some(codec) = self.h264 {
codecs.push(codec);
}
if let Some(codec) = self.h265 {
codecs.push(codec);
}
if let Some(codec) = self.vp8 {
codecs.push(codec);
}
if let Some(codec) = self.vp9 {
codecs.push(codec);
}
codecs
}
pub fn serialize(&self) -> Result<String, ()> { pub fn serialize(&self) -> Result<String, ()> {
match serde_json::to_string_pretty(self) { match serde_json::to_string_pretty(self) {
Ok(s) => Ok(s), Ok(s) => Ok(s),

View File

@@ -652,22 +652,22 @@ impl Connection {
// H264 is preferred because it has the best hardware encoder support (RKMPP, VAAPI, etc.) // H264 is preferred because it has the best hardware encoder support (RKMPP, VAAPI, etc.)
// and most RustDesk clients support H264 hardware decoding // and most RustDesk clients support H264 hardware decoding
if constraints.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::H264) if constraints.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::H264)
&& registry.is_format_available(VideoEncoderType::H264, false) && registry.is_codec_available(VideoEncoderType::H264)
{ {
return VideoEncoderType::H264; return VideoEncoderType::H264;
} }
if constraints.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::H265) if constraints.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::H265)
&& registry.is_format_available(VideoEncoderType::H265, false) && registry.is_codec_available(VideoEncoderType::H265)
{ {
return VideoEncoderType::H265; return VideoEncoderType::H265;
} }
if constraints.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::VP8) if constraints.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::VP8)
&& registry.is_format_available(VideoEncoderType::VP8, false) && registry.is_codec_available(VideoEncoderType::VP8)
{ {
return VideoEncoderType::VP8; return VideoEncoderType::VP8;
} }
if constraints.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::VP9) if constraints.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::VP9)
&& registry.is_format_available(VideoEncoderType::VP9, false) && registry.is_codec_available(VideoEncoderType::VP9)
{ {
return VideoEncoderType::VP9; return VideoEncoderType::VP9;
} }
@@ -784,7 +784,7 @@ impl Connection {
} }
let registry = EncoderRegistry::global(); let registry = EncoderRegistry::global();
if registry.is_format_available(new_codec, false) { if registry.is_codec_available(new_codec) {
info!( info!(
"Client requested codec switch: {:?} -> {:?}", "Client requested codec switch: {:?} -> {:?}",
self.negotiated_codec, new_codec self.negotiated_codec, new_codec
@@ -1121,16 +1121,16 @@ impl Connection {
// Check which encoders are available (include software fallback) // Check which encoders are available (include software fallback)
let h264_available = constraints let h264_available = constraints
.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::H264) .is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::H264)
&& registry.is_format_available(VideoEncoderType::H264, false); && registry.is_codec_available(VideoEncoderType::H264);
let h265_available = constraints let h265_available = constraints
.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::H265) .is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::H265)
&& registry.is_format_available(VideoEncoderType::H265, false); && registry.is_codec_available(VideoEncoderType::H265);
let vp8_available = constraints let vp8_available = constraints
.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::VP8) .is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::VP8)
&& registry.is_format_available(VideoEncoderType::VP8, false); && registry.is_codec_available(VideoEncoderType::VP8);
let vp9_available = constraints let vp9_available = constraints
.is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::VP9) .is_webrtc_codec_allowed(crate::video::encoder::VideoCodecType::VP9)
&& registry.is_format_available(VideoEncoderType::VP9, false); && registry.is_codec_available(VideoEncoderType::VP9);
info!( info!(
"Server encoding capabilities: H264={}, H265={}, VP8={}, VP9={}", "Server encoding capabilities: H264={}, H265={}, VP8={}, VP9={}",

View File

@@ -17,6 +17,8 @@ use hwcodec::ffmpeg::AVPixelFormat;
use hwcodec::ffmpeg_ram::encode::{EncodeContext, Encoder as HwEncoder}; use hwcodec::ffmpeg_ram::encode::{EncodeContext, Encoder as HwEncoder};
use hwcodec::ffmpeg_ram::CodecInfo; use hwcodec::ffmpeg_ram::CodecInfo;
use super::detect_best_codec_for_format;
use super::registry::EncoderBackend;
use super::traits::{EncodedFormat, EncodedFrame, Encoder, EncoderConfig}; use super::traits::{EncodedFormat, EncodedFrame, Encoder, EncoderConfig};
use crate::error::{AppError, Result}; use crate::error::{AppError, Result};
use crate::video::format::{PixelFormat, Resolution}; use crate::video::format::{PixelFormat, Resolution};
@@ -69,21 +71,17 @@ impl std::fmt::Display for H264EncoderType {
} }
/// Map codec name to encoder type /// Map codec name to encoder type
fn codec_name_to_type(name: &str) -> H264EncoderType { impl From<EncoderBackend> for H264EncoderType {
if name.contains("nvenc") { fn from(backend: EncoderBackend) -> Self {
H264EncoderType::Nvenc match backend {
} else if name.contains("qsv") { EncoderBackend::Nvenc => H264EncoderType::Nvenc,
H264EncoderType::Qsv EncoderBackend::Qsv => H264EncoderType::Qsv,
} else if name.contains("amf") { EncoderBackend::Amf => H264EncoderType::Amf,
H264EncoderType::Amf EncoderBackend::Vaapi => H264EncoderType::Vaapi,
} else if name.contains("vaapi") { EncoderBackend::Rkmpp => H264EncoderType::Rkmpp,
H264EncoderType::Vaapi EncoderBackend::V4l2m2m => H264EncoderType::V4l2M2m,
} else if name.contains("rkmpp") { EncoderBackend::Software => H264EncoderType::Software,
H264EncoderType::Rkmpp }
} else if name.contains("v4l2m2m") {
H264EncoderType::V4l2M2m
} else {
H264EncoderType::Software
} }
} }
@@ -215,21 +213,15 @@ pub fn get_available_encoders(width: u32, height: u32) -> Vec<CodecInfo> {
pub fn detect_best_encoder(width: u32, height: u32) -> (H264EncoderType, Option<String>) { pub fn detect_best_encoder(width: u32, height: u32) -> (H264EncoderType, Option<String>) {
let encoders = get_available_encoders(width, height); let encoders = get_available_encoders(width, height);
if encoders.is_empty() { if let Some((encoder_type, codec_name)) =
detect_best_codec_for_format(&encoders, hwcodec::common::DataFormat::H264, |_| true)
{
info!("Best H.264 encoder: {} ({})", codec_name, encoder_type);
(encoder_type, Some(codec_name))
} else {
warn!("No H.264 encoders available from hwcodec"); warn!("No H.264 encoders available from hwcodec");
return (H264EncoderType::None, None); (H264EncoderType::None, None)
} }
// Find H264 encoder (not H265)
for codec in &encoders {
if codec.format == hwcodec::common::DataFormat::H264 {
let encoder_type = codec_name_to_type(&codec.name);
info!("Best H.264 encoder: {} ({})", codec.name, encoder_type);
return (encoder_type, Some(codec.name.clone()));
}
}
(H264EncoderType::None, None)
} }
/// Encoded frame from hwcodec (cloned for ownership) /// Encoded frame from hwcodec (cloned for ownership)
@@ -321,7 +313,7 @@ impl H264Encoder {
})?; })?;
let yuv_length = inner.length; let yuv_length = inner.length;
let encoder_type = codec_name_to_type(codec_name); let encoder_type = H264EncoderType::from(EncoderBackend::from_codec_name(codec_name));
info!( info!(
"H.264 encoder created: {} (type: {}, buffer_length: {}, input_format: {:?})", "H.264 encoder created: {} (type: {}, buffer_length: {}, input_format: {:?})",

View File

@@ -15,6 +15,7 @@ use hwcodec::ffmpeg::AVPixelFormat;
use hwcodec::ffmpeg_ram::encode::{EncodeContext, Encoder as HwEncoder}; use hwcodec::ffmpeg_ram::encode::{EncodeContext, Encoder as HwEncoder};
use hwcodec::ffmpeg_ram::CodecInfo; use hwcodec::ffmpeg_ram::CodecInfo;
use super::detect_best_codec_for_format;
use super::registry::{EncoderBackend, EncoderRegistry, VideoEncoderType}; use super::registry::{EncoderBackend, EncoderRegistry, VideoEncoderType};
use super::traits::{EncodedFormat, EncodedFrame, Encoder, EncoderConfig}; use super::traits::{EncodedFormat, EncodedFrame, Encoder, EncoderConfig};
use crate::error::{AppError, Result}; use crate::error::{AppError, Result};
@@ -221,43 +222,25 @@ pub fn get_available_h265_encoders(width: u32, height: u32) -> Vec<CodecInfo> {
pub fn detect_best_h265_encoder(width: u32, height: u32) -> (H265EncoderType, Option<String>) { pub fn detect_best_h265_encoder(width: u32, height: u32) -> (H265EncoderType, Option<String>) {
let encoders = get_available_h265_encoders(width, height); let encoders = get_available_h265_encoders(width, height);
if encoders.is_empty() {
warn!("No H.265 encoders available");
return (H265EncoderType::None, None);
}
// Prefer hardware encoders over software (libx265) // Prefer hardware encoders over software (libx265)
// Hardware priority: NVENC > QSV > AMF > VAAPI > RKMPP > V4L2 M2M > Software // Hardware priority: NVENC > QSV > AMF > VAAPI > RKMPP > V4L2 M2M > Software
let codec = encoders if let Some((encoder_type, codec_name)) =
.iter() detect_best_codec_for_format(&encoders, DataFormat::H265, |codec| {
.find(|e| !e.name.contains("libx265")) !codec.name.contains("libx265")
.or_else(|| encoders.first()) })
.unwrap(); {
info!("Selected H.265 encoder: {} ({})", codec_name, encoder_type);
let encoder_type = if codec.name.contains("nvenc") { (encoder_type, Some(codec_name))
H265EncoderType::Nvenc
} else if codec.name.contains("qsv") {
H265EncoderType::Qsv
} else if codec.name.contains("amf") {
H265EncoderType::Amf
} else if codec.name.contains("vaapi") {
H265EncoderType::Vaapi
} else if codec.name.contains("rkmpp") {
H265EncoderType::Rkmpp
} else if codec.name.contains("v4l2m2m") {
H265EncoderType::V4l2M2m
} else { } else {
H265EncoderType::Software // Default to software for unknown warn!("No H.265 encoders available");
}; (H265EncoderType::None, None)
}
info!("Selected H.265 encoder: {} ({})", codec.name, encoder_type);
(encoder_type, Some(codec.name.clone()))
} }
/// Check if H265 hardware encoding is available /// Check if H265 hardware encoding is available
pub fn is_h265_available() -> bool { pub fn is_h265_available() -> bool {
let registry = EncoderRegistry::global(); let registry = EncoderRegistry::global();
registry.is_format_available(VideoEncoderType::H265, true) registry.is_codec_available(VideoEncoderType::H265)
} }
/// Encoded frame from hwcodec (cloned for ownership) /// Encoded frame from hwcodec (cloned for ownership)
@@ -268,7 +251,7 @@ pub struct HwEncodeFrame {
pub key: i32, pub key: i32,
} }
/// H.265 encoder using hwcodec (hardware only) /// H.265 encoder using hwcodec
pub struct H265Encoder { pub struct H265Encoder {
/// hwcodec encoder instance /// hwcodec encoder instance
inner: HwEncoder, inner: HwEncoder,

View File

@@ -3,17 +3,21 @@
//! This module provides video encoding capabilities including: //! This module provides video encoding capabilities including:
//! - JPEG encoding for raw frames (YUYV, NV12, etc.) //! - JPEG encoding for raw frames (YUYV, NV12, etc.)
//! - H264 encoding (hardware + software) //! - H264 encoding (hardware + software)
//! - H265 encoding (hardware only) //! - H265 encoding (hardware + software)
//! - VP8 encoding (hardware only - VAAPI) //! - VP8 encoding (hardware + software)
//! - VP9 encoding (hardware only - VAAPI) //! - VP9 encoding (hardware + software)
//! - WebRTC video codec abstraction //! - WebRTC video codec abstraction
//! - Encoder registry for automatic detection //! - Encoder registry for automatic detection
use hwcodec::common::DataFormat;
use hwcodec::ffmpeg_ram::CodecInfo;
pub mod codec; pub mod codec;
pub mod h264; pub mod h264;
pub mod h265; pub mod h265;
pub mod jpeg; pub mod jpeg;
pub mod registry; pub mod registry;
pub mod self_check;
pub mod traits; pub mod traits;
pub mod vp8; pub mod vp8;
pub mod vp9; pub mod vp9;
@@ -28,18 +32,53 @@ pub use codec::{CodecFrame, VideoCodec, VideoCodecConfig, VideoCodecFactory, Vid
// Encoder registry // Encoder registry
pub use registry::{AvailableEncoder, EncoderBackend, EncoderRegistry, VideoEncoderType}; pub use registry::{AvailableEncoder, EncoderBackend, EncoderRegistry, VideoEncoderType};
pub use self_check::{
build_hardware_self_check_runtime_error, run_hardware_self_check, VideoEncoderSelfCheckCell,
VideoEncoderSelfCheckCodec, VideoEncoderSelfCheckResponse, VideoEncoderSelfCheckRow,
};
// H264 encoder // H264 encoder
pub use h264::{H264Config, H264Encoder, H264EncoderType, H264InputFormat}; pub use h264::{H264Config, H264Encoder, H264EncoderType, H264InputFormat};
// H265 encoder (hardware only) // H265 encoder
pub use h265::{H265Config, H265Encoder, H265EncoderType, H265InputFormat}; pub use h265::{H265Config, H265Encoder, H265EncoderType, H265InputFormat};
// VP8 encoder (hardware only) // VP8 encoder
pub use vp8::{VP8Config, VP8Encoder, VP8EncoderType, VP8InputFormat}; pub use vp8::{VP8Config, VP8Encoder, VP8EncoderType, VP8InputFormat};
// VP9 encoder (hardware only) // VP9 encoder
pub use vp9::{VP9Config, VP9Encoder, VP9EncoderType, VP9InputFormat}; pub use vp9::{VP9Config, VP9Encoder, VP9EncoderType, VP9InputFormat};
// JPEG encoder // JPEG encoder
pub use jpeg::JpegEncoder; pub use jpeg::JpegEncoder;
pub(crate) fn select_codec_for_format<F>(
encoders: &[CodecInfo],
format: DataFormat,
preferred: F,
) -> Option<&CodecInfo>
where
F: Fn(&CodecInfo) -> bool,
{
encoders
.iter()
.find(|codec| codec.format == format && preferred(codec))
.or_else(|| encoders.iter().find(|codec| codec.format == format))
}
pub(crate) fn detect_best_codec_for_format<T, F>(
encoders: &[CodecInfo],
format: DataFormat,
preferred: F,
) -> Option<(T, String)>
where
T: From<EncoderBackend>,
F: Fn(&CodecInfo) -> bool,
{
select_codec_for_format(encoders, format, preferred).map(|codec| {
(
T::from(EncoderBackend::from_codec_name(&codec.name)),
codec.name.clone(),
)
})
}

View File

@@ -7,6 +7,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::OnceLock; use std::sync::OnceLock;
use std::time::Duration;
use tracing::{debug, info, warn}; use tracing::{debug, info, warn};
use hwcodec::common::{DataFormat, Quality, RateControl}; use hwcodec::common::{DataFormat, Quality, RateControl};
@@ -28,6 +29,10 @@ pub enum VideoEncoderType {
} }
impl VideoEncoderType { impl VideoEncoderType {
pub const fn ordered() -> [Self; 4] {
[Self::H264, Self::H265, Self::VP8, Self::VP9]
}
/// Convert to hwcodec DataFormat /// Convert to hwcodec DataFormat
pub fn to_data_format(&self) -> DataFormat { pub fn to_data_format(&self) -> DataFormat {
match self { match self {
@@ -68,17 +73,6 @@ impl VideoEncoderType {
VideoEncoderType::VP9 => "VP9", VideoEncoderType::VP9 => "VP9",
} }
} }
/// Check if this format requires hardware-only encoding
/// H264 supports software fallback, others require hardware
pub fn hardware_only(&self) -> bool {
match self {
VideoEncoderType::H264 => false,
VideoEncoderType::H265 => true,
VideoEncoderType::VP8 => true,
VideoEncoderType::VP9 => true,
}
}
} }
impl std::fmt::Display for VideoEncoderType { impl std::fmt::Display for VideoEncoderType {
@@ -210,14 +204,84 @@ pub struct EncoderRegistry {
} }
impl EncoderRegistry { impl EncoderRegistry {
fn detect_encoders_with_timeout(ctx: EncodeContext, timeout: Duration) -> Vec<CodecInfo> {
use std::sync::mpsc;
let (tx, rx) = mpsc::channel();
let handle = std::thread::Builder::new()
.name("ffmpeg-encoder-detect".to_string())
.spawn(move || {
let result = HwEncoder::available_encoders(ctx, None);
let _ = tx.send(result);
});
let Ok(handle) = handle else {
warn!("Failed to spawn encoder detection thread");
return Vec::new();
};
match rx.recv_timeout(timeout) {
Ok(encoders) => {
let _ = handle.join();
encoders
}
Err(mpsc::RecvTimeoutError::Timeout) => {
warn!(
"Encoder detection timed out after {}ms, skipping hardware detection",
timeout.as_millis()
);
std::thread::spawn(move || {
let _ = handle.join();
});
Vec::new()
}
Err(mpsc::RecvTimeoutError::Disconnected) => {
let _ = handle.join();
warn!("Encoder detection thread exited unexpectedly");
Vec::new()
}
}
}
fn register_software_fallbacks(&mut self) {
info!("Registering software encoders...");
for format in VideoEncoderType::ordered() {
let encoders = self.encoders.entry(format).or_default();
if encoders.iter().any(|encoder| !encoder.is_hardware) {
continue;
}
let codec_name = match format {
VideoEncoderType::H264 => "libx264",
VideoEncoderType::H265 => "libx265",
VideoEncoderType::VP8 => "libvpx",
VideoEncoderType::VP9 => "libvpx-vp9",
};
encoders.push(AvailableEncoder {
format,
codec_name: codec_name.to_string(),
backend: EncoderBackend::Software,
priority: 100,
is_hardware: false,
});
debug!(
"Registered software encoder: {} for {} (priority: {})",
codec_name, format, 100
);
}
}
/// Get the global registry instance /// Get the global registry instance
/// ///
/// The registry is initialized lazily on first access with 1920x1080 detection. /// The registry is initialized lazily on first access with 1280x720 detection.
pub fn global() -> &'static Self { pub fn global() -> &'static Self {
static INSTANCE: OnceLock<EncoderRegistry> = OnceLock::new(); static INSTANCE: OnceLock<EncoderRegistry> = OnceLock::new();
INSTANCE.get_or_init(|| { INSTANCE.get_or_init(|| {
let mut registry = EncoderRegistry::new(); let mut registry = EncoderRegistry::new();
registry.detect_encoders(1920, 1080); registry.detect_encoders(1280, 720);
registry registry
}) })
} }
@@ -257,32 +321,11 @@ impl EncoderRegistry {
}; };
const DETECT_TIMEOUT_MS: u64 = 5000; const DETECT_TIMEOUT_MS: u64 = 5000;
info!("Encoder detection timeout: {}ms", DETECT_TIMEOUT_MS);
// Get all available encoders from hwcodec with a hard timeout let all_encoders = Self::detect_encoders_with_timeout(
let all_encoders = { ctx.clone(),
use std::sync::mpsc; Duration::from_millis(DETECT_TIMEOUT_MS),
use std::time::Duration; );
info!("Encoder detection timeout: {}ms", DETECT_TIMEOUT_MS);
let (tx, rx) = mpsc::channel();
let ctx_clone = ctx.clone();
std::thread::spawn(move || {
let result = HwEncoder::available_encoders(ctx_clone, None);
let _ = tx.send(result);
});
match rx.recv_timeout(Duration::from_millis(DETECT_TIMEOUT_MS)) {
Ok(encoders) => encoders,
Err(_) => {
warn!(
"Encoder detection timed out after {}ms, skipping hardware detection",
DETECT_TIMEOUT_MS
);
Vec::new()
}
}
};
info!("Found {} encoders from hwcodec", all_encoders.len()); info!("Found {} encoders from hwcodec", all_encoders.len());
@@ -305,32 +348,7 @@ impl EncoderRegistry {
encoders.sort_by_key(|e| e.priority); encoders.sort_by_key(|e| e.priority);
} }
// Register software encoders as fallback self.register_software_fallbacks();
info!("Registering software encoders...");
let software_encoders = [
(VideoEncoderType::H264, "libx264", 100),
(VideoEncoderType::H265, "libx265", 100),
(VideoEncoderType::VP8, "libvpx", 100),
(VideoEncoderType::VP9, "libvpx-vp9", 100),
];
for (format, codec_name, priority) in software_encoders {
self.encoders
.entry(format)
.or_default()
.push(AvailableEncoder {
format,
codec_name: codec_name.to_string(),
backend: EncoderBackend::Software,
priority,
is_hardware: false,
});
debug!(
"Registered software encoder: {} for {} (priority: {})",
codec_name, format, priority
);
}
// Log summary // Log summary
for (format, encoders) in &self.encoders { for (format, encoders) in &self.encoders {
@@ -370,6 +388,10 @@ impl EncoderRegistry {
) )
} }
pub fn best_available_encoder(&self, format: VideoEncoderType) -> Option<&AvailableEncoder> {
self.best_encoder(format, false)
}
/// Get all encoders for a format /// Get all encoders for a format
pub fn encoders_for_format(&self, format: VideoEncoderType) -> &[AvailableEncoder] { pub fn encoders_for_format(&self, format: VideoEncoderType) -> &[AvailableEncoder] {
self.encoders self.encoders
@@ -405,31 +427,17 @@ impl EncoderRegistry {
self.best_encoder(format, hardware_only).is_some() self.best_encoder(format, hardware_only).is_some()
} }
pub fn is_codec_available(&self, format: VideoEncoderType) -> bool {
self.best_available_encoder(format).is_some()
}
/// Get available formats for user selection /// Get available formats for user selection
/// ///
/// Returns formats that are actually usable based on their requirements:
/// - H264: Available if any encoder exists (hardware or software)
/// - H265/VP8/VP9: Available only if hardware encoder exists
pub fn selectable_formats(&self) -> Vec<VideoEncoderType> { pub fn selectable_formats(&self) -> Vec<VideoEncoderType> {
let mut formats = Vec::new(); VideoEncoderType::ordered()
.into_iter()
// H264 - supports software fallback .filter(|format| self.is_codec_available(*format))
if self.is_format_available(VideoEncoderType::H264, false) { .collect()
formats.push(VideoEncoderType::H264);
}
// H265/VP8/VP9 - hardware only
for format in [
VideoEncoderType::H265,
VideoEncoderType::VP8,
VideoEncoderType::VP9,
] {
if self.is_format_available(format, true) {
formats.push(format);
}
}
formats
} }
/// Get detection resolution /// Get detection resolution
@@ -534,11 +542,16 @@ mod tests {
} }
#[test] #[test]
fn test_hardware_only_requirement() { fn test_codec_ordering() {
assert!(!VideoEncoderType::H264.hardware_only()); assert_eq!(
assert!(VideoEncoderType::H265.hardware_only()); VideoEncoderType::ordered(),
assert!(VideoEncoderType::VP8.hardware_only()); [
assert!(VideoEncoderType::VP9.hardware_only()); VideoEncoderType::H264,
VideoEncoderType::H265,
VideoEncoderType::VP8,
VideoEncoderType::VP9,
]
);
} }
#[test] #[test]

View File

@@ -0,0 +1,335 @@
use serde::Serialize;
use std::sync::mpsc;
use std::time::{Duration, Instant};
use super::{
EncoderRegistry, H264Config, H264Encoder, H265Config, H265Encoder, VP8Config, VP8Encoder,
VP9Config, VP9Encoder, VideoEncoderType,
};
use crate::error::{AppError, Result};
use crate::video::format::{PixelFormat, Resolution};
const SELF_CHECK_TIMEOUT: Duration = Duration::from_secs(5);
const SELF_CHECK_FRAME_ATTEMPTS: u64 = 3;
#[derive(Serialize)]
pub struct VideoEncoderSelfCheckCodec {
pub id: &'static str,
pub name: &'static str,
}
#[derive(Serialize)]
pub struct VideoEncoderSelfCheckCell {
pub codec_id: &'static str,
pub ok: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub elapsed_ms: Option<u64>,
}
#[derive(Serialize)]
pub struct VideoEncoderSelfCheckRow {
pub resolution_id: &'static str,
pub resolution_label: &'static str,
pub width: u32,
pub height: u32,
pub cells: Vec<VideoEncoderSelfCheckCell>,
}
#[derive(Serialize)]
pub struct VideoEncoderSelfCheckResponse {
pub current_hardware_encoder: String,
pub codecs: Vec<VideoEncoderSelfCheckCodec>,
pub rows: Vec<VideoEncoderSelfCheckRow>,
}
pub fn run_hardware_self_check() -> VideoEncoderSelfCheckResponse {
let registry = EncoderRegistry::global();
let codecs = codec_columns();
let mut rows = Vec::new();
for (resolution_id, resolution_label, resolution) in test_resolutions() {
let mut cells = Vec::new();
for codec in test_codecs() {
let cell = match registry.best_encoder(codec, true) {
Some(encoder) => run_single_check(codec, resolution, encoder.codec_name.clone()),
None => unsupported_cell(codec),
};
cells.push(cell);
}
rows.push(VideoEncoderSelfCheckRow {
resolution_id,
resolution_label,
width: resolution.width,
height: resolution.height,
cells,
});
}
VideoEncoderSelfCheckResponse {
current_hardware_encoder: current_hardware_encoder(registry),
codecs,
rows,
}
}
pub fn build_hardware_self_check_runtime_error() -> VideoEncoderSelfCheckResponse {
let codecs = codec_columns();
let mut rows = Vec::new();
for (resolution_id, resolution_label, resolution) in test_resolutions() {
let cells = test_codecs()
.into_iter()
.map(|codec| VideoEncoderSelfCheckCell {
codec_id: codec_id(codec),
ok: false,
elapsed_ms: None,
})
.collect();
rows.push(VideoEncoderSelfCheckRow {
resolution_id,
resolution_label,
width: resolution.width,
height: resolution.height,
cells,
});
}
VideoEncoderSelfCheckResponse {
current_hardware_encoder: "None".to_string(),
codecs,
rows,
}
}
fn codec_columns() -> Vec<VideoEncoderSelfCheckCodec> {
test_codecs()
.into_iter()
.map(|codec| VideoEncoderSelfCheckCodec {
id: codec_id(codec),
name: match codec {
VideoEncoderType::H265 => "H.265",
_ => codec.display_name(),
},
})
.collect()
}
fn test_codecs() -> [VideoEncoderType; 4] {
[
VideoEncoderType::H264,
VideoEncoderType::H265,
VideoEncoderType::VP8,
VideoEncoderType::VP9,
]
}
fn test_resolutions() -> [(&'static str, &'static str, Resolution); 4] {
[
("720p", "720p", Resolution::HD720),
("1080p", "1080p", Resolution::HD1080),
("2k", "2K", Resolution::new(2560, 1440)),
("4k", "4K", Resolution::UHD4K),
]
}
fn codec_id(codec: VideoEncoderType) -> &'static str {
match codec {
VideoEncoderType::H264 => "h264",
VideoEncoderType::H265 => "h265",
VideoEncoderType::VP8 => "vp8",
VideoEncoderType::VP9 => "vp9",
}
}
fn unsupported_cell(codec: VideoEncoderType) -> VideoEncoderSelfCheckCell {
VideoEncoderSelfCheckCell {
codec_id: codec_id(codec),
ok: false,
elapsed_ms: None,
}
}
fn run_single_check(
codec: VideoEncoderType,
resolution: Resolution,
codec_name_ffmpeg: String,
) -> VideoEncoderSelfCheckCell {
let started = Instant::now();
let (tx, rx) = mpsc::channel();
let thread_codec_name = codec_name_ffmpeg.clone();
let spawn_result = std::thread::Builder::new()
.name(format!(
"encoder-self-check-{}-{}x{}",
codec_id(codec),
resolution.width,
resolution.height
))
.spawn(move || {
let _ = tx.send(run_smoke_test(codec, resolution, &thread_codec_name));
});
if let Err(e) = spawn_result {
let _ = e;
return VideoEncoderSelfCheckCell {
codec_id: codec_id(codec),
ok: false,
elapsed_ms: Some(started.elapsed().as_millis() as u64),
};
}
match rx.recv_timeout(SELF_CHECK_TIMEOUT) {
Ok(Ok(())) => VideoEncoderSelfCheckCell {
codec_id: codec_id(codec),
ok: true,
elapsed_ms: Some(started.elapsed().as_millis() as u64),
},
Ok(Err(_)) => VideoEncoderSelfCheckCell {
codec_id: codec_id(codec),
ok: false,
elapsed_ms: Some(started.elapsed().as_millis() as u64),
},
Err(mpsc::RecvTimeoutError::Timeout) => VideoEncoderSelfCheckCell {
codec_id: codec_id(codec),
ok: false,
elapsed_ms: Some(started.elapsed().as_millis() as u64),
},
Err(mpsc::RecvTimeoutError::Disconnected) => VideoEncoderSelfCheckCell {
codec_id: codec_id(codec),
ok: false,
elapsed_ms: Some(started.elapsed().as_millis() as u64),
},
}
}
fn current_hardware_encoder(registry: &EncoderRegistry) -> String {
let backends = registry
.available_backends()
.into_iter()
.filter(|backend| backend.is_hardware())
.map(|backend| backend.display_name().to_string())
.collect::<Vec<_>>();
if backends.is_empty() {
"None".to_string()
} else {
backends.join("/")
}
}
fn run_smoke_test(
codec: VideoEncoderType,
resolution: Resolution,
codec_name_ffmpeg: &str,
) -> Result<()> {
match codec {
VideoEncoderType::H264 => run_h264_smoke_test(resolution, codec_name_ffmpeg),
VideoEncoderType::H265 => run_h265_smoke_test(resolution, codec_name_ffmpeg),
VideoEncoderType::VP8 => run_vp8_smoke_test(resolution, codec_name_ffmpeg),
VideoEncoderType::VP9 => run_vp9_smoke_test(resolution, codec_name_ffmpeg),
}
}
fn run_h264_smoke_test(resolution: Resolution, codec_name_ffmpeg: &str) -> Result<()> {
let mut encoder = H264Encoder::with_codec(
H264Config::low_latency(resolution, bitrate_kbps_for_resolution(resolution)),
codec_name_ffmpeg,
)?;
encoder.request_keyframe();
let frame = build_nv12_test_frame(resolution, encoder.yuv_info().2 as usize);
for sequence in 0..SELF_CHECK_FRAME_ATTEMPTS {
let frames = encoder.encode_raw(&frame, pts_ms(sequence))?;
if frames.iter().any(|frame| !frame.data.is_empty()) {
return Ok(());
}
}
Err(AppError::VideoError(
"Encoder produced no output after multiple frames".to_string(),
))
}
fn run_h265_smoke_test(resolution: Resolution, codec_name_ffmpeg: &str) -> Result<()> {
let mut encoder = H265Encoder::with_codec(
H265Config::low_latency(resolution, bitrate_kbps_for_resolution(resolution)),
codec_name_ffmpeg,
)?;
encoder.request_keyframe();
let frame = build_nv12_test_frame(resolution, encoder.buffer_info().2 as usize);
for sequence in 0..SELF_CHECK_FRAME_ATTEMPTS {
let frames = encoder.encode_raw(&frame, pts_ms(sequence))?;
if frames.iter().any(|frame| !frame.data.is_empty()) {
return Ok(());
}
}
Err(AppError::VideoError(
"Encoder produced no output after multiple frames".to_string(),
))
}
fn run_vp8_smoke_test(resolution: Resolution, codec_name_ffmpeg: &str) -> Result<()> {
let mut encoder = VP8Encoder::with_codec(
VP8Config::low_latency(resolution, bitrate_kbps_for_resolution(resolution)),
codec_name_ffmpeg,
)?;
let frame = build_nv12_test_frame(resolution, encoder.buffer_info().2 as usize);
for sequence in 0..SELF_CHECK_FRAME_ATTEMPTS {
let frames = encoder.encode_raw(&frame, pts_ms(sequence))?;
if frames.iter().any(|frame| !frame.data.is_empty()) {
return Ok(());
}
}
Err(AppError::VideoError(
"Encoder produced no output after multiple frames".to_string(),
))
}
fn run_vp9_smoke_test(resolution: Resolution, codec_name_ffmpeg: &str) -> Result<()> {
let mut encoder = VP9Encoder::with_codec(
VP9Config::low_latency(resolution, bitrate_kbps_for_resolution(resolution)),
codec_name_ffmpeg,
)?;
let frame = build_nv12_test_frame(resolution, encoder.buffer_info().2 as usize);
for sequence in 0..SELF_CHECK_FRAME_ATTEMPTS {
let frames = encoder.encode_raw(&frame, pts_ms(sequence))?;
if frames.iter().any(|frame| !frame.data.is_empty()) {
return Ok(());
}
}
Err(AppError::VideoError(
"Encoder produced no output after multiple frames".to_string(),
))
}
fn build_nv12_test_frame(resolution: Resolution, buffer_length: usize) -> Vec<u8> {
let minimum_length = PixelFormat::Nv12.frame_size(resolution).unwrap_or(0);
let mut frame = vec![0x80; buffer_length.max(minimum_length)];
let y_plane_len = (resolution.width * resolution.height) as usize;
let fill_len = y_plane_len.min(frame.len());
frame[..fill_len].fill(0x10);
frame
}
fn bitrate_kbps_for_resolution(resolution: Resolution) -> u32 {
match resolution.width {
0..=1280 => 4_000,
1281..=1920 => 8_000,
1921..=2560 => 12_000,
_ => 20_000,
}
}
fn pts_ms(sequence: u64) -> i64 {
((sequence * 1000) / 30) as i64
}

View File

@@ -15,6 +15,7 @@ use hwcodec::ffmpeg::AVPixelFormat;
use hwcodec::ffmpeg_ram::encode::{EncodeContext, Encoder as HwEncoder}; use hwcodec::ffmpeg_ram::encode::{EncodeContext, Encoder as HwEncoder};
use hwcodec::ffmpeg_ram::CodecInfo; use hwcodec::ffmpeg_ram::CodecInfo;
use super::detect_best_codec_for_format;
use super::registry::{EncoderBackend, EncoderRegistry, VideoEncoderType}; use super::registry::{EncoderBackend, EncoderRegistry, VideoEncoderType};
use super::traits::{EncodedFormat, EncodedFrame, Encoder, EncoderConfig}; use super::traits::{EncodedFormat, EncodedFrame, Encoder, EncoderConfig};
use crate::error::{AppError, Result}; use crate::error::{AppError, Result};
@@ -156,32 +157,24 @@ pub fn get_available_vp8_encoders(width: u32, height: u32) -> Vec<CodecInfo> {
pub fn detect_best_vp8_encoder(width: u32, height: u32) -> (VP8EncoderType, Option<String>) { pub fn detect_best_vp8_encoder(width: u32, height: u32) -> (VP8EncoderType, Option<String>) {
let encoders = get_available_vp8_encoders(width, height); let encoders = get_available_vp8_encoders(width, height);
if encoders.is_empty() {
warn!("No VP8 encoders available");
return (VP8EncoderType::None, None);
}
// Prefer hardware encoders (VAAPI) over software (libvpx) // Prefer hardware encoders (VAAPI) over software (libvpx)
let codec = encoders if let Some((encoder_type, codec_name)) =
.iter() detect_best_codec_for_format(&encoders, DataFormat::VP8, |codec| {
.find(|e| e.name.contains("vaapi")) codec.name.contains("vaapi")
.or_else(|| encoders.first()) })
.unwrap(); {
info!("Selected VP8 encoder: {} ({})", codec_name, encoder_type);
let encoder_type = if codec.name.contains("vaapi") { (encoder_type, Some(codec_name))
VP8EncoderType::Vaapi
} else { } else {
VP8EncoderType::Software // Default to software for unknown warn!("No VP8 encoders available");
}; (VP8EncoderType::None, None)
}
info!("Selected VP8 encoder: {} ({})", codec.name, encoder_type);
(encoder_type, Some(codec.name.clone()))
} }
/// Check if VP8 hardware encoding is available /// Check if VP8 hardware encoding is available
pub fn is_vp8_available() -> bool { pub fn is_vp8_available() -> bool {
let registry = EncoderRegistry::global(); let registry = EncoderRegistry::global();
registry.is_format_available(VideoEncoderType::VP8, true) registry.is_codec_available(VideoEncoderType::VP8)
} }
/// Encoded frame from hwcodec (cloned for ownership) /// Encoded frame from hwcodec (cloned for ownership)
@@ -192,7 +185,7 @@ pub struct HwEncodeFrame {
pub key: i32, pub key: i32,
} }
/// VP8 encoder using hwcodec (hardware only - VAAPI) /// VP8 encoder using hwcodec
pub struct VP8Encoder { pub struct VP8Encoder {
/// hwcodec encoder instance /// hwcodec encoder instance
inner: HwEncoder, inner: HwEncoder,

View File

@@ -15,6 +15,7 @@ use hwcodec::ffmpeg::AVPixelFormat;
use hwcodec::ffmpeg_ram::encode::{EncodeContext, Encoder as HwEncoder}; use hwcodec::ffmpeg_ram::encode::{EncodeContext, Encoder as HwEncoder};
use hwcodec::ffmpeg_ram::CodecInfo; use hwcodec::ffmpeg_ram::CodecInfo;
use super::detect_best_codec_for_format;
use super::registry::{EncoderBackend, EncoderRegistry, VideoEncoderType}; use super::registry::{EncoderBackend, EncoderRegistry, VideoEncoderType};
use super::traits::{EncodedFormat, EncodedFrame, Encoder, EncoderConfig}; use super::traits::{EncodedFormat, EncodedFrame, Encoder, EncoderConfig};
use crate::error::{AppError, Result}; use crate::error::{AppError, Result};
@@ -156,32 +157,24 @@ pub fn get_available_vp9_encoders(width: u32, height: u32) -> Vec<CodecInfo> {
pub fn detect_best_vp9_encoder(width: u32, height: u32) -> (VP9EncoderType, Option<String>) { pub fn detect_best_vp9_encoder(width: u32, height: u32) -> (VP9EncoderType, Option<String>) {
let encoders = get_available_vp9_encoders(width, height); let encoders = get_available_vp9_encoders(width, height);
if encoders.is_empty() {
warn!("No VP9 encoders available");
return (VP9EncoderType::None, None);
}
// Prefer hardware encoders (VAAPI) over software (libvpx-vp9) // Prefer hardware encoders (VAAPI) over software (libvpx-vp9)
let codec = encoders if let Some((encoder_type, codec_name)) =
.iter() detect_best_codec_for_format(&encoders, DataFormat::VP9, |codec| {
.find(|e| e.name.contains("vaapi")) codec.name.contains("vaapi")
.or_else(|| encoders.first()) })
.unwrap(); {
info!("Selected VP9 encoder: {} ({})", codec_name, encoder_type);
let encoder_type = if codec.name.contains("vaapi") { (encoder_type, Some(codec_name))
VP9EncoderType::Vaapi
} else { } else {
VP9EncoderType::Software // Default to software for unknown warn!("No VP9 encoders available");
}; (VP9EncoderType::None, None)
}
info!("Selected VP9 encoder: {} ({})", codec.name, encoder_type);
(encoder_type, Some(codec.name.clone()))
} }
/// Check if VP9 hardware encoding is available /// Check if VP9 hardware encoding is available
pub fn is_vp9_available() -> bool { pub fn is_vp9_available() -> bool {
let registry = EncoderRegistry::global(); let registry = EncoderRegistry::global();
registry.is_format_available(VideoEncoderType::VP9, true) registry.is_codec_available(VideoEncoderType::VP9)
} }
/// Encoded frame from hwcodec (cloned for ownership) /// Encoded frame from hwcodec (cloned for ownership)
@@ -192,7 +185,7 @@ pub struct HwEncodeFrame {
pub key: i32, pub key: i32,
} }
/// VP9 encoder using hwcodec (hardware only - VAAPI) /// VP9 encoder using hwcodec
pub struct VP9Encoder { pub struct VP9Encoder {
/// hwcodec encoder instance /// hwcodec encoder instance
inner: HwEncoder, inner: HwEncoder,

View File

@@ -38,14 +38,12 @@ use crate::error::{AppError, Result};
use crate::utils::LogThrottler; use crate::utils::LogThrottler;
use crate::video::convert::{Nv12Converter, PixelConverter}; use crate::video::convert::{Nv12Converter, PixelConverter};
use crate::video::decoder::MjpegTurboDecoder; use crate::video::decoder::MjpegTurboDecoder;
use crate::video::encoder::h264::{detect_best_encoder, H264Config, H264Encoder, H264InputFormat}; use crate::video::encoder::h264::{H264Config, H264Encoder, H264InputFormat};
use crate::video::encoder::h265::{ use crate::video::encoder::h265::{H265Config, H265Encoder, H265InputFormat};
detect_best_h265_encoder, H265Config, H265Encoder, H265InputFormat,
};
use crate::video::encoder::registry::{EncoderBackend, EncoderRegistry, VideoEncoderType}; use crate::video::encoder::registry::{EncoderBackend, EncoderRegistry, VideoEncoderType};
use crate::video::encoder::traits::EncoderConfig; use crate::video::encoder::traits::EncoderConfig;
use crate::video::encoder::vp8::{detect_best_vp8_encoder, VP8Config, VP8Encoder}; use crate::video::encoder::vp8::{VP8Config, VP8Encoder};
use crate::video::encoder::vp9::{detect_best_vp9_encoder, VP9Config, VP9Encoder}; use crate::video::encoder::vp9::{VP9Config, VP9Encoder};
use crate::video::format::{PixelFormat, Resolution}; use crate::video::format::{PixelFormat, Resolution};
use crate::video::frame::{FrameBuffer, FrameBufferPool, VideoFrame}; use crate::video::frame::{FrameBuffer, FrameBufferPool, VideoFrame};
use crate::video::v4l2r_capture::V4l2rCaptureStream; use crate::video::v4l2r_capture::V4l2rCaptureStream;
@@ -389,7 +387,7 @@ impl SharedVideoPipeline {
.encoder_with_backend(format, b) .encoder_with_backend(format, b)
.map(|e| e.codec_name.clone()), .map(|e| e.codec_name.clone()),
None => registry None => registry
.best_encoder(format, false) .best_available_encoder(format)
.map(|e| e.codec_name.clone()), .map(|e| e.codec_name.clone()),
} }
}; };
@@ -447,10 +445,7 @@ impl SharedVideoPipeline {
)) ))
})? })?
} else { } else {
// Auto select best available encoder get_codec_name(VideoEncoderType::H264, None).ok_or_else(|| {
let (_encoder_type, detected) =
detect_best_encoder(config.resolution.width, config.resolution.height);
detected.ok_or_else(|| {
AppError::VideoError("No H.264 encoder available".to_string()) AppError::VideoError("No H.264 encoder available".to_string())
})? })?
} }
@@ -472,9 +467,7 @@ impl SharedVideoPipeline {
)) ))
})? })?
} else { } else {
let (_encoder_type, detected) = get_codec_name(VideoEncoderType::H265, None).ok_or_else(|| {
detect_best_h265_encoder(config.resolution.width, config.resolution.height);
detected.ok_or_else(|| {
AppError::VideoError("No H.265 encoder available".to_string()) AppError::VideoError("No H.265 encoder available".to_string())
})? })?
} }
@@ -485,9 +478,7 @@ impl SharedVideoPipeline {
AppError::VideoError(format!("Backend {:?} does not support VP8", backend)) AppError::VideoError(format!("Backend {:?} does not support VP8", backend))
})? })?
} else { } else {
let (_encoder_type, detected) = get_codec_name(VideoEncoderType::VP8, None).ok_or_else(|| {
detect_best_vp8_encoder(config.resolution.width, config.resolution.height);
detected.ok_or_else(|| {
AppError::VideoError("No VP8 encoder available".to_string()) AppError::VideoError("No VP8 encoder available".to_string())
})? })?
} }
@@ -498,9 +489,7 @@ impl SharedVideoPipeline {
AppError::VideoError(format!("Backend {:?} does not support VP9", backend)) AppError::VideoError(format!("Backend {:?} does not support VP9", backend))
})? })?
} else { } else {
let (_encoder_type, detected) = get_codec_name(VideoEncoderType::VP9, None).ok_or_else(|| {
detect_best_vp9_encoder(config.resolution.width, config.resolution.height);
detected.ok_or_else(|| {
AppError::VideoError("No VP9 encoder available".to_string()) AppError::VideoError("No VP9 encoder available".to_string())
})? })?
} }

View File

@@ -191,15 +191,14 @@ impl VideoSessionManager {
*self.frame_source.write().await = Some(rx); *self.frame_source.write().await = Some(rx);
} }
/// Get available codecs based on hardware capabilities /// Get available codecs based on encoder availability
pub fn available_codecs(&self) -> Vec<VideoEncoderType> { pub fn available_codecs(&self) -> Vec<VideoEncoderType> {
EncoderRegistry::global().selectable_formats() EncoderRegistry::global().selectable_formats()
} }
/// Check if a codec is available /// Check if a codec is available
pub fn is_codec_available(&self, codec: VideoEncoderType) -> bool { pub fn is_codec_available(&self, codec: VideoEncoderType) -> bool {
let hardware_only = codec.hardware_only(); EncoderRegistry::global().is_codec_available(codec)
EncoderRegistry::global().is_format_available(codec, hardware_only)
} }
/// Create a new video session /// Create a new video session
@@ -520,7 +519,7 @@ impl VideoSessionManager {
/// Get codec info /// Get codec info
pub fn get_codec_info(&self, codec: VideoEncoderType) -> Option<CodecInfo> { pub fn get_codec_info(&self, codec: VideoEncoderType) -> Option<CodecInfo> {
let registry = EncoderRegistry::global(); let registry = EncoderRegistry::global();
let encoder = registry.best_encoder(codec, codec.hardware_only())?; let encoder = registry.best_available_encoder(codec)?;
Some(CodecInfo { Some(CodecInfo {
codec_type: codec, codec_type: codec,

View File

@@ -16,7 +16,10 @@ use crate::events::SystemEvent;
use crate::state::AppState; use crate::state::AppState;
use crate::update::{UpdateChannel, UpdateOverviewResponse, UpdateStatusResponse, UpgradeRequest}; use crate::update::{UpdateChannel, UpdateOverviewResponse, UpdateStatusResponse, UpgradeRequest};
use crate::video::codec_constraints::codec_to_id; use crate::video::codec_constraints::codec_to_id;
use crate::video::encoder::BitratePreset; use crate::video::encoder::{
build_hardware_self_check_runtime_error, run_hardware_self_check, BitratePreset,
VideoEncoderSelfCheckResponse,
};
// ============================================================================ // ============================================================================
// Health & Info // Health & Info
@@ -1798,7 +1801,7 @@ pub async fn stream_codecs_list() -> Json<AvailableCodecsResponse> {
}); });
// Check H264 availability (supports software fallback) // Check H264 availability (supports software fallback)
let h264_encoder = registry.best_encoder(VideoEncoderType::H264, false); let h264_encoder = registry.best_available_encoder(VideoEncoderType::H264);
codecs.push(VideoCodecInfo { codecs.push(VideoCodecInfo {
id: "h264".to_string(), id: "h264".to_string(),
name: "H.264 / WebRTC".to_string(), name: "H.264 / WebRTC".to_string(),
@@ -1809,7 +1812,7 @@ pub async fn stream_codecs_list() -> Json<AvailableCodecsResponse> {
}); });
// Check H265 availability (now supports software too) // Check H265 availability (now supports software too)
let h265_encoder = registry.best_encoder(VideoEncoderType::H265, false); let h265_encoder = registry.best_available_encoder(VideoEncoderType::H265);
codecs.push(VideoCodecInfo { codecs.push(VideoCodecInfo {
id: "h265".to_string(), id: "h265".to_string(),
name: "H.265 / WebRTC".to_string(), name: "H.265 / WebRTC".to_string(),
@@ -1820,7 +1823,7 @@ pub async fn stream_codecs_list() -> Json<AvailableCodecsResponse> {
}); });
// Check VP8 availability (now supports software too) // Check VP8 availability (now supports software too)
let vp8_encoder = registry.best_encoder(VideoEncoderType::VP8, false); let vp8_encoder = registry.best_available_encoder(VideoEncoderType::VP8);
codecs.push(VideoCodecInfo { codecs.push(VideoCodecInfo {
id: "vp8".to_string(), id: "vp8".to_string(),
name: "VP8 / WebRTC".to_string(), name: "VP8 / WebRTC".to_string(),
@@ -1831,7 +1834,7 @@ pub async fn stream_codecs_list() -> Json<AvailableCodecsResponse> {
}); });
// Check VP9 availability (now supports software too) // Check VP9 availability (now supports software too)
let vp9_encoder = registry.best_encoder(VideoEncoderType::VP9, false); let vp9_encoder = registry.best_available_encoder(VideoEncoderType::VP9);
codecs.push(VideoCodecInfo { codecs.push(VideoCodecInfo {
id: "vp9".to_string(), id: "vp9".to_string(),
name: "VP9 / WebRTC".to_string(), name: "VP9 / WebRTC".to_string(),
@@ -1848,6 +1851,15 @@ pub async fn stream_codecs_list() -> Json<AvailableCodecsResponse> {
}) })
} }
/// Run hardware encoder smoke tests across common resolutions/codecs.
pub async fn video_encoder_self_check() -> Json<VideoEncoderSelfCheckResponse> {
let response = tokio::task::spawn_blocking(run_hardware_self_check)
.await
.unwrap_or_else(|_| build_hardware_self_check_runtime_error());
Json(response)
}
/// Query parameters for MJPEG stream /// Query parameters for MJPEG stream
#[derive(Deserialize, Default)] #[derive(Deserialize, Default)]
pub struct MjpegStreamQuery { pub struct MjpegStreamQuery {

View File

@@ -51,6 +51,10 @@ pub fn create_router(state: Arc<AppState>) -> Router {
.route("/stream/bitrate", post(handlers::stream_set_bitrate)) .route("/stream/bitrate", post(handlers::stream_set_bitrate))
.route("/stream/codecs", get(handlers::stream_codecs_list)) .route("/stream/codecs", get(handlers::stream_codecs_list))
.route("/stream/constraints", get(handlers::stream_constraints_get)) .route("/stream/constraints", get(handlers::stream_constraints_get))
.route(
"/video/encoder/self-check",
get(handlers::video_encoder_self_check),
)
// WebRTC endpoints // WebRTC endpoints
.route("/webrtc/session", post(handlers::webrtc_create_session)) .route("/webrtc/session", post(handlers::webrtc_create_session))
.route("/webrtc/offer", post(handlers::webrtc_offer)) .route("/webrtc/offer", post(handlers::webrtc_offer))

View File

@@ -221,23 +221,11 @@ impl WebRtcStreamer {
use crate::video::encoder::registry::EncoderRegistry; use crate::video::encoder::registry::EncoderRegistry;
let registry = EncoderRegistry::global(); let registry = EncoderRegistry::global();
let mut codecs = vec![]; VideoEncoderType::ordered()
.into_iter()
// H264 always available (has software fallback) .filter(|codec| registry.is_codec_available(*codec))
codecs.push(VideoCodecType::H264); .map(Self::encoder_type_to_codec_type)
.collect()
// Check hardware codecs
if registry.is_format_available(VideoEncoderType::H265, true) {
codecs.push(VideoCodecType::H265);
}
if registry.is_format_available(VideoEncoderType::VP8, true) {
codecs.push(VideoCodecType::VP8);
}
if registry.is_format_available(VideoEncoderType::VP9, true) {
codecs.push(VideoCodecType::VP9);
}
codecs
} }
/// Convert VideoCodecType to VideoEncoderType /// Convert VideoCodecType to VideoEncoderType
@@ -250,6 +238,15 @@ impl WebRtcStreamer {
} }
} }
fn encoder_type_to_codec_type(codec: VideoEncoderType) -> VideoCodecType {
match codec {
VideoEncoderType::H264 => VideoCodecType::H264,
VideoEncoderType::H265 => VideoCodecType::H265,
VideoEncoderType::VP8 => VideoCodecType::VP8,
VideoEncoderType::VP9 => VideoCodecType::VP9,
}
}
fn should_stop_pipeline(session_count: usize, subscriber_count: usize) -> bool { fn should_stop_pipeline(session_count: usize, subscriber_count: usize) -> bool {
session_count == 0 && subscriber_count == 0 session_count == 0 && subscriber_count == 0
} }
@@ -577,7 +574,7 @@ impl WebRtcStreamer {
VideoCodecType::VP9 => VideoEncoderType::VP9, VideoCodecType::VP9 => VideoEncoderType::VP9,
}; };
EncoderRegistry::global() EncoderRegistry::global()
.best_encoder(codec_type, false) .best_available_encoder(codec_type)
.map(|e| e.is_hardware) .map(|e| e.is_hardware)
.unwrap_or(false) .unwrap_or(false)
} }

View File

@@ -177,6 +177,31 @@ export interface StreamConstraintsResponse {
current_mode: string current_mode: string
} }
export interface VideoEncoderSelfCheckCodec {
id: string
name: string
}
export interface VideoEncoderSelfCheckCell {
codec_id: string
ok: boolean
elapsed_ms?: number | null
}
export interface VideoEncoderSelfCheckRow {
resolution_id: string
resolution_label: string
width: number
height: number
cells: VideoEncoderSelfCheckCell[]
}
export interface VideoEncoderSelfCheckResponse {
current_hardware_encoder: string
codecs: VideoEncoderSelfCheckCodec[]
rows: VideoEncoderSelfCheckRow[]
}
export const streamApi = { export const streamApi = {
status: () => status: () =>
request<{ request<{
@@ -217,6 +242,9 @@ export const streamApi = {
getConstraints: () => getConstraints: () =>
request<StreamConstraintsResponse>('/stream/constraints'), request<StreamConstraintsResponse>('/stream/constraints'),
encoderSelfCheck: () =>
request<VideoEncoderSelfCheckResponse>('/video/encoder/self-check'),
setBitratePreset: (bitrate_preset: import('@/types/generated').BitratePreset) => setBitratePreset: (bitrate_preset: import('@/types/generated').BitratePreset) =>
request<{ success: boolean; message?: string }>('/stream/bitrate', { request<{ success: boolean; message?: string }>('/stream/bitrate', {
method: 'POST', method: 'POST',

View File

@@ -757,6 +757,15 @@ export default {
udc_speed: 'Device may not be fully enumerated; try reconnecting USB', udc_speed: 'Device may not be fully enumerated; try reconnecting USB',
}, },
}, },
encoderSelfCheck: {
title: 'Hardware Encoding Capability Test',
desc: 'Test hardware encoding capability across 720p, 1080p, 2K, and 4K',
run: 'Start Test',
failed: 'Failed to run hardware encoding capability test',
resolution: 'Resolution',
currentHardwareEncoder: 'Current Hardware Encoder',
none: 'None',
},
// WebRTC / ICE // WebRTC / ICE
webrtcSettings: 'WebRTC Settings', webrtcSettings: 'WebRTC Settings',
webrtcSettingsDesc: 'Configure STUN/TURN servers for NAT traversal', webrtcSettingsDesc: 'Configure STUN/TURN servers for NAT traversal',

View File

@@ -757,6 +757,15 @@ export default {
udc_speed: '设备可能未完成枚举,可尝试重插 USB', udc_speed: '设备可能未完成枚举,可尝试重插 USB',
}, },
}, },
encoderSelfCheck: {
title: '硬件编码能力测试',
desc: '按 720p、1080p、2K、4K 测试硬件编码能力',
run: '开始测试',
failed: '执行硬件编码能力测试失败',
resolution: '分辨率',
currentHardwareEncoder: '当前硬件编码器',
none: '无',
},
// WebRTC / ICE // WebRTC / ICE
webrtcSettings: 'WebRTC 设置', webrtcSettings: 'WebRTC 设置',
webrtcSettingsDesc: '配置 STUN/TURN 服务器以实现 NAT 穿透', webrtcSettingsDesc: '配置 STUN/TURN 服务器以实现 NAT 穿透',

View File

@@ -1624,16 +1624,86 @@ function handleKeyUp(e: KeyboardEvent) {
sendKeyboardEvent('up', hidKey, modifierMask) sendKeyboardEvent('up', hidKey, modifierMask)
} }
function getActiveVideoElement(): HTMLImageElement | HTMLVideoElement | null {
return videoMode.value !== 'mjpeg' ? webrtcVideoRef.value : videoRef.value
}
function getActiveVideoAspectRatio(): number | null {
if (videoMode.value !== 'mjpeg') {
const video = webrtcVideoRef.value
if (video?.videoWidth && video.videoHeight) {
return video.videoWidth / video.videoHeight
}
} else {
const image = videoRef.value
if (image?.naturalWidth && image.naturalHeight) {
return image.naturalWidth / image.naturalHeight
}
}
if (!videoAspectRatio.value) return null
const [width, height] = videoAspectRatio.value.split('/').map(Number)
if (!width || !height) return null
return width / height
}
function getRenderedVideoRect() {
const videoElement = getActiveVideoElement()
if (!videoElement) return null
const rect = videoElement.getBoundingClientRect()
if (rect.width <= 0 || rect.height <= 0) return null
const contentAspectRatio = getActiveVideoAspectRatio()
if (!contentAspectRatio) {
return rect
}
const boxAspectRatio = rect.width / rect.height
if (!Number.isFinite(boxAspectRatio) || boxAspectRatio <= 0) {
return rect
}
if (boxAspectRatio > contentAspectRatio) {
const width = rect.height * contentAspectRatio
return {
left: rect.left + (rect.width - width) / 2,
top: rect.top,
width,
height: rect.height,
}
}
const height = rect.width / contentAspectRatio
return {
left: rect.left,
top: rect.top + (rect.height - height) / 2,
width: rect.width,
height,
}
}
function getAbsoluteMousePosition(e: MouseEvent) {
const rect = getRenderedVideoRect()
if (!rect) return null
const normalizedX = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width))
const normalizedY = Math.max(0, Math.min(1, (e.clientY - rect.top) / rect.height))
return {
x: Math.round(normalizedX * 32767),
y: Math.round(normalizedY * 32767),
}
}
function handleMouseMove(e: MouseEvent) { function handleMouseMove(e: MouseEvent) {
// Use the appropriate video element based on current mode (WebRTC for h264/h265/vp8/vp9, MJPEG for mjpeg) const videoElement = getActiveVideoElement()
const videoElement = videoMode.value !== 'mjpeg' ? webrtcVideoRef.value : videoRef.value
if (!videoElement) return if (!videoElement) return
if (mouseMode.value === 'absolute') { if (mouseMode.value === 'absolute') {
// Absolute mode: send absolute coordinates (0-32767 range) const absolutePosition = getAbsoluteMousePosition(e)
const rect = videoElement.getBoundingClientRect() if (!absolutePosition) return
const x = Math.round((e.clientX - rect.left) / rect.width * 32767) const { x, y } = absolutePosition
const y = Math.round((e.clientY - rect.top) / rect.height * 32767)
mousePosition.value = { x, y } mousePosition.value = { x, y }
// Queue for throttled sending (absolute mode: just update pending position) // Queue for throttled sending (absolute mode: just update pending position)
@@ -1758,6 +1828,15 @@ function handleMouseDown(e: MouseEvent) {
return return
} }
if (mouseMode.value === 'absolute') {
const absolutePosition = getAbsoluteMousePosition(e)
if (absolutePosition) {
mousePosition.value = absolutePosition
sendMouseEvent({ type: 'move_abs', ...absolutePosition })
pendingMouseMove = null
}
}
const button = e.button === 0 ? 'left' : e.button === 2 ? 'right' : 'middle' const button = e.button === 0 ? 'left' : e.button === 2 ? 'right' : 'middle'
pressedMouseButton.value = button pressedMouseButton.value = button
sendMouseEvent({ type: 'down', button }) sendMouseEvent({ type: 'down', button })

View File

@@ -24,6 +24,7 @@ import {
type UpdateOverviewResponse, type UpdateOverviewResponse,
type UpdateStatusResponse, type UpdateStatusResponse,
type UpdateChannel, type UpdateChannel,
type VideoEncoderSelfCheckResponse,
} from '@/api' } from '@/api'
import type { import type {
ExtensionsStatus, ExtensionsStatus,
@@ -539,6 +540,64 @@ async function onRunOtgSelfCheckClick() {
await runOtgSelfCheck() await runOtgSelfCheck()
} }
type VideoEncoderSelfCheckCell = VideoEncoderSelfCheckResponse['rows'][number]['cells'][number]
type VideoEncoderSelfCheckRow = VideoEncoderSelfCheckResponse['rows'][number]
const videoEncoderSelfCheckLoading = ref(false)
const videoEncoderSelfCheckResult = ref<VideoEncoderSelfCheckResponse | null>(null)
const videoEncoderSelfCheckError = ref('')
const videoEncoderRunButtonPressed = ref(false)
function videoEncoderCell(row: VideoEncoderSelfCheckRow, codecId: string): VideoEncoderSelfCheckCell | undefined {
return row.cells.find(cell => cell.codec_id === codecId)
}
const currentHardwareEncoderText = computed(() =>
videoEncoderSelfCheckResult.value?.current_hardware_encoder === 'None'
? t('settings.encoderSelfCheck.none')
: (videoEncoderSelfCheckResult.value?.current_hardware_encoder || t('settings.encoderSelfCheck.none'))
)
function videoEncoderCodecLabel(codecId: string, codecName: string): string {
return codecId === 'h265' ? 'H.265' : codecName
}
function videoEncoderCellClass(ok: boolean | undefined): string {
return ok ? 'text-emerald-600 dark:text-emerald-400' : 'text-red-600 dark:text-red-400'
}
function videoEncoderCellSymbol(ok: boolean | undefined): string {
return ok ? '✓' : '✗'
}
function videoEncoderCellTime(cell: VideoEncoderSelfCheckCell | undefined): string {
if (!cell || typeof cell.elapsed_ms !== 'number') return '-'
return `${cell.elapsed_ms}ms`
}
async function runVideoEncoderSelfCheck() {
videoEncoderSelfCheckLoading.value = true
videoEncoderSelfCheckError.value = ''
try {
videoEncoderSelfCheckResult.value = await streamApi.encoderSelfCheck()
} catch (e) {
console.error('Failed to run encoder self-check:', e)
videoEncoderSelfCheckError.value = t('settings.encoderSelfCheck.failed')
} finally {
videoEncoderSelfCheckLoading.value = false
}
}
async function onRunVideoEncoderSelfCheckClick() {
if (!videoEncoderSelfCheckLoading.value) {
videoEncoderRunButtonPressed.value = true
window.setTimeout(() => {
videoEncoderRunButtonPressed.value = false
}, 160)
}
await runVideoEncoderSelfCheck()
}
function alignHidProfileForLowEndpoint() { function alignHidProfileForLowEndpoint() {
if (hidProfileAligned.value) return if (hidProfileAligned.value) return
if (!configLoaded.value || !devicesLoaded.value) return if (!configLoaded.value || !devicesLoaded.value) return
@@ -1781,16 +1840,15 @@ onMounted(async () => {
if (updateRunning.value) { if (updateRunning.value) {
startUpdatePolling() startUpdatePolling()
} }
await runOtgSelfCheck()
}) })
watch(updateChannel, async () => { watch(updateChannel, async () => {
await loadUpdateOverview() await loadUpdateOverview()
}) })
watch(() => config.value.hid_backend, async () => { watch(() => config.value.hid_backend, () => {
await runOtgSelfCheck() otgSelfCheckResult.value = null
otgSelfCheckError.value = ''
}) })
</script> </script>
@@ -2364,6 +2422,86 @@ watch(() => config.value.hid_backend, async () => {
</p> </p>
</CardContent> </CardContent>
</Card> </Card>
<Card>
<CardHeader class="flex flex-row items-start justify-between space-y-0">
<div class="space-y-1.5">
<CardTitle>{{ t('settings.encoderSelfCheck.title') }}</CardTitle>
<CardDescription>{{ t('settings.encoderSelfCheck.desc') }}</CardDescription>
</div>
<Button
variant="outline"
size="sm"
:disabled="videoEncoderSelfCheckLoading"
:class="[
'transition-all duration-150 active:scale-95 active:brightness-95',
videoEncoderRunButtonPressed ? 'scale-95 brightness-95' : ''
]"
@click="onRunVideoEncoderSelfCheckClick"
>
<RefreshCw class="h-4 w-4 mr-2" :class="{ 'animate-spin': videoEncoderSelfCheckLoading }" />
{{ t('settings.encoderSelfCheck.run') }}
</Button>
</CardHeader>
<CardContent class="space-y-3">
<p v-if="videoEncoderSelfCheckError" class="text-xs text-red-600 dark:text-red-400">
{{ videoEncoderSelfCheckError }}
</p>
<template v-if="videoEncoderSelfCheckResult">
<div class="text-sm">
{{ t('settings.encoderSelfCheck.currentHardwareEncoder') }}{{ currentHardwareEncoderText }}
</div>
<div class="rounded-md border bg-card">
<table class="w-full table-fixed text-sm">
<thead>
<tr>
<th class="px-2 py-3 text-left font-medium w-[18%]">{{ t('settings.encoderSelfCheck.resolution') }}</th>
<th
v-for="codec in videoEncoderSelfCheckResult.codecs"
:key="codec.id"
class="px-2 py-3 text-center font-medium w-[20.5%]"
>
{{ videoEncoderCodecLabel(codec.id, codec.name) }}
</th>
</tr>
</thead>
<tbody>
<tr
v-for="row in videoEncoderSelfCheckResult.rows"
:key="row.resolution_id"
>
<td class="px-2 py-3 align-middle">
<div class="font-medium">{{ row.resolution_label }}</div>
</td>
<td
v-for="codec in videoEncoderSelfCheckResult.codecs"
:key="`${row.resolution_id}-${codec.id}`"
class="px-2 py-3 align-middle"
>
<div
class="flex flex-col items-center justify-center gap-1"
:class="videoEncoderCellClass(videoEncoderCell(row, codec.id)?.ok)"
>
<div class="text-lg leading-none font-semibold">
{{ videoEncoderCellSymbol(videoEncoderCell(row, codec.id)?.ok) }}
</div>
<div class="text-[11px] leading-4 text-foreground/70">
{{ videoEncoderCellTime(videoEncoderCell(row, codec.id)) }}
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<p v-else-if="videoEncoderSelfCheckLoading" class="text-xs text-muted-foreground">
{{ t('common.loading') }}
</p>
</CardContent>
</Card>
</div> </div>
<!-- Access Section --> <!-- Access Section -->