From 2c6e960de357b06a35fc2e6f1b166947b3606aae Mon Sep 17 00:00:00 2001 From: Vikram Auradkar Date: Mon, 17 Oct 2022 14:24:42 -0700 Subject: [PATCH] win_audio: build and test win_audio BUG=b:253494168 TEST=presubmit Change-Id: Icb729671a0dcfbc4b6251c69732784de32f6318d Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3988069 Reviewed-by: Daniel Verkamp Auto-Submit: Vikram Auradkar Commit-Queue: Vikram Auradkar --- Cargo.lock | 29 +++++++ Cargo.toml | 1 + devices/Cargo.toml | 1 + win_audio/Cargo.toml | 7 +- win_audio/build.rs | 77 +++++++++++-------- win_audio/prebuilts_version | 1 + win_audio/src/lib.rs | 1 + win_audio/src/r8brain_sys/bindings.rs | 5 -- .../src/win_audio_impl/completion_handler.rs | 6 +- win_audio/src/win_audio_impl/mod.rs | 2 +- 10 files changed, 89 insertions(+), 41 deletions(-) create mode 100644 win_audio/prebuilts_version diff --git a/Cargo.lock b/Cargo.lock index e359b414d7..b53c8b76de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -540,6 +540,7 @@ dependencies = [ "vhost", "vm_control", "vm_memory", + "win_audio", "win_util", "winapi", "x86_64", @@ -712,6 +713,7 @@ dependencies = [ "vm_control", "vm_memory", "vmm_vhost", + "win_audio", "win_util", "winapi", ] @@ -2183,6 +2185,24 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17882f045410753661207383517a6f62ec3dbeb6a4ed2acce01f0728238d1983" +[[package]] +name = "win_audio" +version = "0.1.0" +dependencies = [ + "anyhow", + "audio_streams", + "base", + "libc", + "metrics", + "once_cell", + "prebuilts", + "sync", + "thiserror", + "win_util", + "winapi", + "wio", +] + [[package]] name = "win_util" version = "0.1.0" @@ -2254,6 +2274,15 @@ dependencies = [ "windows_gen", ] +[[package]] +name = "wio" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d129932f4644ac2396cb456385cbf9e63b5b30c6e8dc4820bdca4eb082037a5" +dependencies = [ + "winapi", +] + [[package]] name = "wire_format_derive" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 4966c1f233..71fa5c3b66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -398,6 +398,7 @@ sandbox = { path = "sandbox" } cros_tracing = { path = "cros_tracing" } tube_transporter = { path = "tube_transporter" } winapi = "*" +win_audio = { path = "win_audio"} win_util = { path = "win_util"} [build-dependencies] diff --git a/devices/Cargo.toml b/devices/Cargo.toml index 4e83f9871c..8ed0c5685b 100644 --- a/devices/Cargo.toml +++ b/devices/Cargo.toml @@ -96,6 +96,7 @@ vfio_sys = { path = "../vfio_sys" } broker_ipc = { path = "../broker_ipc" } metrics = { path = "../metrics" } tube_transporter = { path = "../tube_transporter" } +win_audio = { path = "../win_audio"} win_util = { path = "../win_util"} winapi = "*" diff --git a/win_audio/Cargo.toml b/win_audio/Cargo.toml index 4f0a5e0549..316054205b 100644 --- a/win_audio/Cargo.toml +++ b/win_audio/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["The Chromium OS Authors"] edition = "2021" -[dependencies] +[target.'cfg(windows)'.dependencies] audio_streams = { path = "../common/audio_streams"} base = { path = "../base" } libc = "*" @@ -15,3 +15,8 @@ sync = { path = "../common/sync" } thiserror = "*" metrics = { path = "../metrics"} once_cell = "1.7.2" + +[build-dependencies] +anyhow = "*" +prebuilts = { path = "../prebuilts" } + diff --git a/win_audio/build.rs b/win_audio/build.rs index b3ef432f53..b84df08d16 100644 --- a/win_audio/build.rs +++ b/win_audio/build.rs @@ -2,37 +2,52 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +use std::env; + +static PREBUILTS_VERSION_FILENAME: &str = "prebuilts_version"; +static R8BRAIN_LIB: &str = "r8Brain.lib"; +static R8BRAIN_DLL: &str = "r8Brain.dll"; +#[cfg(windows)] +static UCRTBASE_DLL: &str = "ucrtbased.dll"; +#[cfg(windows)] +static VCRUNTIME_DLL: &str = "vcruntime140d.dll"; + fn main() { - #[cfg(windows)] - { - use std::env; - let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - println!("cargo:rustc-link-lib=static=r8brain"); - #[cfg(debug_assertions)] - { - let dll_dir = format!( - r#"{}\..\..\..\third_party\r8brain\r8brain\x64\Debug\"#, - manifest_dir - ); - println!(r#"cargo:rustc-link-search={}"#, dll_dir); - println!( - r#"cargo:rustc-env=PATH={};{}"#, - env::var("PATH").unwrap(), - dll_dir - ); - } - #[cfg(not(debug_assertions))] - { - let dll_dir = format!( - r#"{}\..\..\..\third_party\r8brain\r8brain\x64\Release\"#, - manifest_dir - ); - println!(r#"cargo:rustc-link-search={}"#, dll_dir); - println!( - r#"cargo:rustc-env=PATH={};{}"#, - env::var("PATH").unwrap(), - dll_dir - ); - } + if std::env::var("CARGO_CFG_WINDOWS").is_ok() { + let version = std::fs::read_to_string(PREBUILTS_VERSION_FILENAME) + .unwrap() + .trim() + .parse::() + .unwrap(); + + // TODO(b:253039132) build prebuilts locally on windows from build.rs. + let files = prebuilts::download_prebuilts( + "r8brain", + version, + &[ + R8BRAIN_DLL, + R8BRAIN_LIB, + #[cfg(windows)] + UCRTBASE_DLL, + #[cfg(windows)] + VCRUNTIME_DLL, + ], + ) + .unwrap(); + let lib_dir = files + .get(0) + .unwrap() + .parent() + .unwrap() + .as_os_str() + .to_str() + .unwrap(); + println!("cargo:rustc-link-lib=r8Brain"); + println!(r#"cargo:rustc-link-search={}"#, lib_dir); + println!( + r#"cargo:rustc-env=PATH={};{}"#, + lib_dir, + env::var("PATH").unwrap(), + ); } } diff --git a/win_audio/prebuilts_version b/win_audio/prebuilts_version new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/win_audio/prebuilts_version @@ -0,0 +1 @@ +1 diff --git a/win_audio/src/lib.rs b/win_audio/src/lib.rs index ba911f5e28..9f679df577 100644 --- a/win_audio/src/lib.rs +++ b/win_audio/src/lib.rs @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#![cfg(windows)] #![allow(non_upper_case_globals)] include!(concat!( env!("CARGO_MANIFEST_DIR"), diff --git a/win_audio/src/r8brain_sys/bindings.rs b/win_audio/src/r8brain_sys/bindings.rs index b4c82326b6..f3d9442044 100644 --- a/win_audio/src/r8brain_sys/bindings.rs +++ b/win_audio/src/r8brain_sys/bindings.rs @@ -3,11 +3,6 @@ See instructions from: https://rust-lang.github.io/rust-bindgen/print.html Original library: https://github.com/avaneev/r8brain-free-src -This library should be included in the third_party folder when running -`repo sync`. Otherwise the library can be cloned with: - - git clone "sso://play-internal/battlestar/kiwivm/thirdparty/r8brain"" - To generate bindings: 1. go to the r8brain-free-src location. In my case it'll be: diff --git a/win_audio/src/win_audio_impl/completion_handler.rs b/win_audio/src/win_audio_impl/completion_handler.rs index 89f311da6b..b8b3c59ce8 100644 --- a/win_audio/src/win_audio_impl/completion_handler.rs +++ b/win_audio/src/win_audio_impl/completion_handler.rs @@ -79,7 +79,7 @@ impl WinAudioActivateAudioInterfaceCompletionHandler { info!("Activate Completed handler called from ActiviateAudioInterfaceAsync."); match Event::open(ACTIVATE_AUDIO_INTERFACE_COMPLETION_EVENT) { Ok(event) => { - event.write(1).expect("Failed to notify audioclientevent"); + event.signal().expect("Failed to notify audioclientevent"); } Err(e) => { error!("Activate audio event cannot be opened: {}", e); @@ -173,9 +173,9 @@ const IWIN_AUDIO_COMPLETION_HANDLER_VTBL: &IActivateAudioInterfaceCompletionHand let ref_count = (*win_audio_completion_handler).decrement_counter(); if ref_count == 0 { // Delete the pointer - Box::from_raw( + drop(Box::from_raw( this as *mut WinAudioActivateAudioInterfaceCompletionHandler, - ); + )); } ref_count } diff --git a/win_audio/src/win_audio_impl/mod.rs b/win_audio/src/win_audio_impl/mod.rs index 7540fc47c9..5fdc17a31d 100644 --- a/win_audio/src/win_audio_impl/mod.rs +++ b/win_audio/src/win_audio_impl/mod.rs @@ -626,7 +626,7 @@ impl DeviceRenderer { // never hang, but added a long timeout just incase. match activate_audio_interface_complete_event .unwrap() - .read_timeout(ACTIVATE_AUDIO_EVENT_TIMEOUT) + .wait_timeout(ACTIVATE_AUDIO_EVENT_TIMEOUT) { Ok(event_result) => match event_result { EventWaitResult::Signaled => {}