Share encoder retries Vulkan (5x100ms, logging the reason) before falling back to VA-API, so a transient Vk open failure no longer drops DMA-BUF capture to shared memory
Browse files at this commitdbc5912bd059af487c65dad41722da03d023b33aParent: 02267bd- fix
- share encoder retries Vulkan (5x100ms, logging the reason) before falling back to VA-API, so a transient Vk open failure no longer drops DMA-BUF capture to shared memory
2 changed files +43 -3
crates/client/src/share/encode.rsmodified+35 -1
| @@ -199,6 +199,9 @@ fn report<T, E: std::fmt::Debug>(result: Result<T, E>) -> Option<T> { | |||
| 199 | } | 199 | } |
| 200 | 200 | ||
| 201 | mod hardware { | 201 | mod hardware { |
| 202 | + const VULKAN_ATTEMPTS: u32 = 5; | ||
| 203 | + const VULKAN_RETRY: std::time::Duration = std::time::Duration::from_millis(100); | ||
| 204 | + | ||
| 202 | use common::share::Codec; | 205 | use common::share::Codec; |
| 203 | 206 | ||
| 204 | use super::{Request, Settings, report}; | 207 | use super::{Request, Settings, report}; |
| @@ -314,6 +317,36 @@ mod hardware { | |||
| 314 | found | 317 | found |
| 315 | } | 318 | } |
| 316 | 319 | ||
| 320 | + fn vulkan_encodes(wanted: Codec) -> bool { | ||
| 321 | + static VULKAN: std::sync::OnceLock<Vec<Codec>> = std::sync::OnceLock::new(); | ||
| 322 | + VULKAN | ||
| 323 | + .get_or_init(|| { | ||
| 324 | + ec_hw::capabilities() | ||
| 325 | + .into_iter() | ||
| 326 | + .filter(|capability| { | ||
| 327 | + capability.direction == ec_hw::Direction::Encode | ||
| 328 | + && capability.backend == ec_hw::Backend::Vk | ||
| 329 | + }) | ||
| 330 | + .filter_map(|capability| codec(capability.codec)) | ||
| 331 | + .collect() | ||
| 332 | + }) | ||
| 333 | + .contains(&wanted) | ||
| 334 | + } | ||
| 335 | + | ||
| 336 | + fn open_vulkan(config: ec_hw::EncoderConfig, wanted: Codec) -> Option<ec_hw::HwEncoder> { | ||
| 337 | + if !vulkan_encodes(wanted) { | ||
| 338 | + return None; | ||
| 339 | + } | ||
| 340 | + for attempt in 1..=VULKAN_ATTEMPTS { | ||
| 341 | + match ec_hw::HwEncoder::open(config, Some(ec_hw::Backend::Vk)) { | ||
| 342 | + Ok(encoder) => return Some(encoder), | ||
| 343 | + Err(error) => tracing::warn!(attempt, %error, "share Vulkan encoder open failed"), | ||
| 344 | + } | ||
| 345 | + std::thread::sleep(VULKAN_RETRY); | ||
| 346 | + } | ||
| 347 | + None | ||
| 348 | + } | ||
| 349 | + | ||
| 317 | pub fn open(settings: &Settings) -> Option<Encoder> { | 350 | pub fn open(settings: &Settings) -> Option<Encoder> { |
| 318 | let codec = match settings.codec { | 351 | let codec = match settings.codec { |
| 319 | Codec::H264 => ec_hw::EncCodec::H264, | 352 | Codec::H264 => ec_hw::EncCodec::H264, |
| @@ -326,7 +359,8 @@ mod hardware { | |||
| 326 | config.bitrate = average(settings.bitrate); | 359 | config.bitrate = average(settings.bitrate); |
| 327 | config.gop_size = settings.gop; | 360 | config.gop_size = settings.gop; |
| 328 | config.rate_control = ec_hw::RateControlMode::VariableBitrate; | 361 | config.rate_control = ec_hw::RateControlMode::VariableBitrate; |
| 329 | - let encoder = report(ec_hw::HwEncoder::open(config, None))?; | 362 | + let encoder = open_vulkan(config, settings.codec) |
| 363 | + .or_else(|| report(ec_hw::HwEncoder::open(config, None)))?; | ||
| 330 | tracing::info!(backend = ?encoder.backend(), ?codec, "share hardware encoder opened"); | 364 | tracing::info!(backend = ?encoder.backend(), ?codec, "share hardware encoder opened"); |
| 331 | Some(Encoder(encoder)) | 365 | Some(Encoder(encoder)) |
| 332 | } | 366 | } |
crates/client/src/share/session.rsmodified+8 -2
| @@ -550,7 +550,8 @@ impl Playout { | |||
| 550 | self.lateness.pop_front(); | 550 | self.lateness.pop_front(); |
| 551 | } | 551 | } |
| 552 | let now = clock.now(); | 552 | let now = clock.now(); |
| 553 | - self.lateness.push_back((now, now - clock.origin(sharer_ms))); | 553 | + self.lateness |
| 554 | + .push_back((now, now - clock.origin(sharer_ms))); | ||
| 554 | let target = self | 555 | let target = self |
| 555 | .lateness | 556 | .lateness |
| 556 | .iter() | 557 | .iter() |
| @@ -573,7 +574,12 @@ impl Playout { | |||
| 573 | let Some(&(newest, _)) = self.lateness.back() else { | 574 | let Some(&(newest, _)) = self.lateness.back() else { |
| 574 | return 0; | 575 | return 0; |
| 575 | }; | 576 | }; |
| 576 | - let floor = self.lateness.iter().map(|(_, late)| *late).min().unwrap_or(0); | 577 | + let floor = self |
| 578 | + .lateness | ||
| 579 | + .iter() | ||
| 580 | + .map(|(_, late)| *late) | ||
| 581 | + .min() | ||
| 582 | + .unwrap_or(0); | ||
| 577 | self.lateness | 583 | self.lateness |
| 578 | .iter() | 584 | .iter() |
| 579 | .rev() | 585 | .rev() |