Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions examples/listen.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use windows::Win32::Media::Audio::{Endpoints::{IAudioEndpointVolumeCallback, IAudioEndpointVolumeCallback_Impl}, AUDIO_VOLUME_NOTIFICATION_DATA};
use windows::Win32::Media::Audio::{
Endpoints::{IAudioEndpointVolumeCallback, IAudioEndpointVolumeCallback_Impl},
AUDIO_VOLUME_NOTIFICATION_DATA,
};
use windows_volume_control::AudioController;

#[windows::core::implement(IAudioEndpointVolumeCallback)]
struct VolumeChangeCallback;

impl IAudioEndpointVolumeCallback_Impl for VolumeChangeCallback {
fn OnNotify(&self, pnotify: *mut AUDIO_VOLUME_NOTIFICATION_DATA) -> ::windows::core::Result<()> {
fn OnNotify(
&self,
pnotify: *mut AUDIO_VOLUME_NOTIFICATION_DATA,
) -> ::windows::core::Result<()> {
unsafe {
println!("volume changed: {}", (*pnotify).fMasterVolume);
}
Expand All @@ -20,13 +26,17 @@ fn main() {
controller.GetDefaultAudioEnpointVolumeControl();
controller.GetAllProcessSessions();

let session = controller.get_session_by_name("master".to_string()).unwrap();
let session = controller
.get_session_by_name("master".to_string())
.unwrap();

if let Some(session_endpoint_volume) = session.getAudioEndpointVolume() {
let volume_callback: IAudioEndpointVolumeCallback = VolumeChangeCallback {}.into();
session_endpoint_volume.RegisterControlChangeNotify(&volume_callback).unwrap();
session_endpoint_volume
.RegisterControlChangeNotify(&volume_callback)
.unwrap();
println!("Initialised audio event listener for session 'master'");
loop {};
loop {}
}
}
}
}
7 changes: 3 additions & 4 deletions examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::Duration;

use windows_volume_control::AudioController;

fn main() {
Expand All @@ -8,8 +6,9 @@ fn main() {
controller.GetSessions();
controller.GetDefaultAudioEnpointVolumeControl();
controller.GetAllProcessSessions();
let test = controller.get_all_session_names();
let sesstion_names = controller.get_all_session_names();
println!("{:?}", sesstion_names);
let master_session = controller.get_session_by_name("master".to_string());
println!("{:?}",master_session.unwrap().getVolume());
println!("{:?}", master_session.unwrap().getVolume());
}
}
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#![allow(non_snake_case)]

use session::{ApplicationSession, EndPointSession, Session};
use std::process::exit;
use windows::{
core::Interface,
Win32::{
Expand All @@ -8,13 +11,15 @@ use windows::{
IMMDeviceEnumerator, ISimpleAudioVolume, MMDeviceEnumerator,
},
System::{
Com::{CoCreateInstance, CoInitializeEx, CLSCTX_INPROC_SERVER, COINIT_MULTITHREADED, CLSCTX_ALL, COINIT_APARTMENTTHREADED},
Com::{
CoCreateInstance, CoInitializeEx, CLSCTX_ALL, CLSCTX_INPROC_SERVER,
COINIT_APARTMENTTHREADED, COINIT_MULTITHREADED,
},
ProcessStatus::K32GetProcessImageFileNameA,
Threading::{OpenProcess, PROCESS_QUERY_INFORMATION, PROCESS_VM_READ},
},
},
};
use std::process::exit;

mod session;

Expand All @@ -26,16 +31,16 @@ pub struct AudioController {

pub enum CoinitMode {
MultiTreaded,
ApartmentThreaded
ApartmentThreaded,
}

impl AudioController {
pub unsafe fn init(coinit_mode: Option<CoinitMode>) -> Self {
let mut coinit: windows::Win32::System::Com::COINIT = COINIT_MULTITHREADED;
if let Some(x) = coinit_mode {
match x {
CoinitMode::ApartmentThreaded => {coinit = COINIT_APARTMENTTHREADED},
CoinitMode::MultiTreaded => {coinit = COINIT_MULTITHREADED}
CoinitMode::ApartmentThreaded => coinit = COINIT_APARTMENTTHREADED,
CoinitMode::MultiTreaded => coinit = COINIT_MULTITHREADED,
}
}
CoInitializeEx(None, coinit).unwrap_or_else(|err| {
Expand Down
12 changes: 6 additions & 6 deletions src/session.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use windows::Win32::Foundation::BOOL;
use windows::core::GUID;
use windows::Win32::Foundation::BOOL;
use windows::Win32::Media::Audio::Endpoints::IAudioEndpointVolume;
use windows::Win32::Media::Audio::ISimpleAudioVolume;

Expand All @@ -16,7 +16,7 @@ pub trait Session {
pub struct EndPointSession {
simple_audio_volume: IAudioEndpointVolume,
name: String,
guid: GUID
guid: GUID,
}
impl EndPointSession {
pub fn new(simple_audio_volume: IAudioEndpointVolume, name: String) -> Self {
Expand All @@ -28,7 +28,7 @@ impl EndPointSession {
Self {
simple_audio_volume: simple_audio_volume,
name: name,
guid: guid
guid: guid,
}
}
}
Expand Down Expand Up @@ -61,7 +61,7 @@ impl Session for EndPointSession {
.SetMute(mute, &self.guid)
.unwrap_or_else(|err| {
eprintln!("ERROR: Couldn't set mute: {err}");
});
});
}
unsafe fn getMute(&self) -> bool {
self.simple_audio_volume
Expand All @@ -86,7 +86,7 @@ impl ApplicationSession {
eprintln!("ERROR: Couldn't generate GUID {err}");
exit(1);
});

Self {
simple_audio_volume: simple_audio_volume,
name: name,
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Session for ApplicationSession {
.SetMute(mute, &self.guid)
.unwrap_or_else(|err| {
eprintln!("ERROR: Couldn't set mute: {err}");
});
});
}
unsafe fn getMute(&self) -> bool {
self.simple_audio_volume
Expand Down