Skip to content

Commit

Permalink
check for AE5 CLSID (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonoughe authored May 8, 2022
1 parent c150ff4 commit 7566577
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Support for AE-5.

## [4.0.0] - 2020-05-14
### Changed
Expand Down
40 changes: 23 additions & 17 deletions src/media/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use winapi::shared::guiddef::GUID;
use winapi::shared::guiddef::{IsEqualIID, REFIID};
use winapi::shared::minwindef::ULONG;
use winapi::shared::ntdef::HRESULT;
use winapi::shared::winerror::NTE_NOT_FOUND;
use winapi::shared::winerror::{E_INVALIDARG, E_NOINTERFACE};
use winapi::shared::wtypes::{PROPERTYKEY, VARTYPE};
use winapi::um::combaseapi::CLSCTX_ALL;
Expand All @@ -46,7 +45,7 @@ pub use self::event::VolumeNotification;
use crate::com::{ComObject, ComScope};
use crate::hresult::{check, Win32Error};
use crate::lazy::Lazy;
use crate::soundcore::{SoundCoreError, PKEY_SOUNDCORECTL_CLSID};
use crate::soundcore::{SoundCoreError, PKEY_SOUNDCORECTL_CLSID_AE5, PKEY_SOUNDCORECTL_CLSID_Z};
use crate::winapiext::{PKEY_DeviceInterface_FriendlyName, PKEY_Device_DeviceDesc};

fn parse_guid(src: &str) -> Result<GUID, Box<dyn Error>> {
Expand Down Expand Up @@ -150,31 +149,30 @@ impl Endpoint {
///
/// This allows discovery of a SoundCore implementation for devices that support it.
pub fn clsid(&self) -> Result<GUID, SoundCoreError> {
match self
.property_store()?
.get_string_value(&PKEY_SOUNDCORECTL_CLSID)
{
Ok(str) => parse_guid(&str).or(Err(SoundCoreError::NotSupported)),
Err(GetPropertyError::UnexpectedType(_)) => Err(SoundCoreError::NotSupported),
Err(GetPropertyError::Win32(ref error)) if error.code == NTE_NOT_FOUND => {
Err(SoundCoreError::NotSupported)
}
Err(GetPropertyError::Win32(error)) => Err(SoundCoreError::Win32(error)),
}
let store = self
.property_store()?;
let value = match store.get_string_value(&PKEY_SOUNDCORECTL_CLSID_AE5)? {
Some(value) => value,
None => store.get_string_value(&PKEY_SOUNDCORECTL_CLSID_Z)?.ok_or(SoundCoreError::NotSupported)?,
};
parse_guid(&value).or(Err(SoundCoreError::NotSupported))
}

/// Gets the friendly name of the audio interface (sound adapter).
///
/// See [Core Audio Properties: Device Properties](https://docs.microsoft.com/en-us/windows/desktop/coreaudio/core-audio-properties#device-properties).
pub fn interface(&self) -> Result<String, GetPropertyError> {
self.property_store()?
.get_string_value(&PKEY_DeviceInterface_FriendlyName)
.get_string_value(&PKEY_DeviceInterface_FriendlyName)?
.ok_or(GetPropertyError::NOT_FOUND)
}
/// Gets a description of the audio endpoint (speakers, headphones, etc).
///
/// See [Core Audio Properties: Device Properties](https://docs.microsoft.com/en-us/windows/desktop/coreaudio/core-audio-properties#device-properties).
pub fn description(&self) -> Result<String, GetPropertyError> {
self.property_store()?
.get_string_value(&PKEY_Device_DeviceDesc)
.get_string_value(&PKEY_Device_DeviceDesc)?
.ok_or(GetPropertyError::NOT_FOUND)
}
fn volume(&self) -> Result<ComObject<IAudioEndpointVolume>, Win32Error> {
self.volume
Expand Down Expand Up @@ -252,6 +250,10 @@ pub enum GetPropertyError {
UnexpectedType(VARTYPE),
}

impl GetPropertyError {
pub(crate) const NOT_FOUND: GetPropertyError = GetPropertyError::UnexpectedType(0);
}

impl fmt::Display for GetPropertyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down Expand Up @@ -288,10 +290,14 @@ impl PropertyStore {
Ok(property_value.assume_init())
}
#[allow(clippy::cast_ptr_alignment)]
fn get_string_value(&self, key: &PROPERTYKEY) -> Result<String, GetPropertyError> {
fn get_string_value(&self, key: &PROPERTYKEY) -> Result<Option<String>, GetPropertyError> {
unsafe {
let mut property_value = self.get_value(key)?;
trace!(self.1, "Returned variant has type {}", property_value.vt);
// VT_EMPTY
if property_value.vt == 0 {
return Ok(None);
}
// VT_LPWSTR
if property_value.vt != 31 {
PropVariantClear(&mut property_value);
Expand All @@ -307,7 +313,7 @@ impl PropertyStore {
PropVariantClear(&mut property_value);
let str = str.unwrap();
trace!(self.1, "Returned variant has value {}", &str);
Ok(str)
Ok(Some(str))
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/soundcore/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use winapi::shared::wtypes::PROPERTYKEY;

DEFINE_PROPERTYKEY! {PKEY_SOUNDCORECTL_CLSID,
DEFINE_PROPERTYKEY! {PKEY_SOUNDCORECTL_CLSID_Z,
0xc949c6aa, 0x132b, 0x4511,0xbb, 0x1b, 0x35, 0x26, 0x1a, 0x2a, 0x63, 0x33,
0}
DEFINE_PROPERTYKEY! {PKEY_SOUNDCORECTL_CLSID_AE5,
0xd8570091, 0xaf3f, 0x4615,0x9f, 0xaa, 0xa2, 0x48, 0x45, 0xd1, 0x09, 0x36,
0}
10 changes: 10 additions & 0 deletions src/soundcore/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::error::Error;
use std::fmt;

use crate::hresult::Win32Error;
use crate::media::GetPropertyError;

/// Describes an error that occurred while acting on Creative's SoundCore API.
#[derive(Debug)]
Expand Down Expand Up @@ -35,3 +36,12 @@ impl From<Win32Error> for SoundCoreError {
SoundCoreError::Win32(err)
}
}

impl From<GetPropertyError> for SoundCoreError {
fn from(err: GetPropertyError) -> SoundCoreError {
match err {
GetPropertyError::UnexpectedType(_) => SoundCoreError::NotSupported,
GetPropertyError::Win32(inner) => inner.into(),
}
}
}
2 changes: 1 addition & 1 deletion src/soundcore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod feature_iterator;
mod parameter;
mod parameter_iterator;

pub use self::consts::PKEY_SOUNDCORECTL_CLSID;
pub use self::consts::*;
pub use self::core::SoundCore;
pub use self::error::SoundCoreError;
pub(crate) use self::event::SoundCoreEvents;
Expand Down

0 comments on commit 7566577

Please sign in to comment.