diff --git a/AudioSwitcher.AudioApi.CoreAudio.Tests/SessionTests.cs b/AudioSwitcher.AudioApi.CoreAudio.Tests/SessionTests.cs index 09196e8..cc1a339 100644 --- a/AudioSwitcher.AudioApi.CoreAudio.Tests/SessionTests.cs +++ b/AudioSwitcher.AudioApi.CoreAudio.Tests/SessionTests.cs @@ -1,4 +1,5 @@ using System.Linq; +using AudioSwitcher.AudioApi.Session; using Xunit; namespace AudioSwitcher.AudioApi.CoreAudio.Tests @@ -7,13 +8,23 @@ namespace AudioSwitcher.AudioApi.CoreAudio.Tests public class SessionTests { + [Fact] + public void CoreAudioSessionController_Exists_As_Capability() + { + using (var controller = new CoreAudioController()) + { + var device = controller.DefaultPlaybackDevice; + Assert.NotNull(device.GetCapability()); + } + } + [Fact] public void CoreAudioSession_IsMuted_When_Device_Is_Muted() { using (var controller = new CoreAudioController()) { var device = controller.DefaultPlaybackDevice; - var session = device.SessionController.First(); + var session = device.GetCapability().First(); var oldDMute = device.IsMuted; var oldSMute = session.IsMuted; diff --git a/AudioSwitcher.AudioApi.CoreAudio/AudioSwitcher.AudioApi.CoreAudio.csproj b/AudioSwitcher.AudioApi.CoreAudio/AudioSwitcher.AudioApi.CoreAudio.csproj index 4a8fd64..fe06617 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/AudioSwitcher.AudioApi.CoreAudio.csproj +++ b/AudioSwitcher.AudioApi.CoreAudio/AudioSwitcher.AudioApi.CoreAudio.csproj @@ -128,7 +128,7 @@ - + diff --git a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioController.cs b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioController.cs index 0d251f3..b9a89dc 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioController.cs +++ b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioController.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading; -using System.Threading.Tasks; using AudioSwitcher.AudioApi.CoreAudio.Interfaces; using AudioSwitcher.AudioApi.CoreAudio.Threading; using AudioSwitcher.AudioApi.Observables; diff --git a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.AudioSession.cs b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.SessionController.cs similarity index 82% rename from AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.AudioSession.cs rename to AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.SessionController.cs index a184da5..a1019e7 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.AudioSession.cs +++ b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.SessionController.cs @@ -1,16 +1,14 @@ using System; using System.Runtime.InteropServices; -using AudioSwitcher.AudioApi.CoreAudio.Interfaces; using AudioSwitcher.AudioApi.CoreAudio.Threading; -using AudioSwitcher.AudioApi.Session; +using AudioSwitcher.AudioApi.CoreAudio.Interfaces; namespace AudioSwitcher.AudioApi.CoreAudio { - public partial class CoreAudioDevice : IAudioSessionEndpoint + public partial class CoreAudioDevice { - private Lazy _sessionController; - public IAudioSessionController SessionController => _sessionController?.Value; + private Lazy _sessionController; private void ClearAudioSession() { @@ -22,13 +20,9 @@ private void ClearAudioSession() private void LoadAudioSessionController(IMultimediaDevice device) { - if (SessionController != null) + if (_sessionController?.IsValueCreated == true) return; - //This should be all on the COM thread to avoid any - //weird lookups on the result COM object not on an STA Thread - //ComThread.Assert(); - _sessionController = new Lazy(() => { return ComThread.Invoke(() => diff --git a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.cs b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.cs index cce63b7..f637c39 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.cs +++ b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.cs @@ -7,6 +7,7 @@ using AudioSwitcher.AudioApi.CoreAudio.Interfaces; using AudioSwitcher.AudioApi.CoreAudio.Threading; using AudioSwitcher.AudioApi.Observables; +using AudioSwitcher.AudioApi.Session; using Nito.AsyncEx; namespace AudioSwitcher.AudioApi.CoreAudio @@ -23,19 +24,18 @@ public sealed partial class CoreAudioDevice : Device {PropertyKeys.PKEY_DEVICE_ICON, new HashSet{ nameof(Icon), nameof(IconPath) }}, }; + private readonly IDisposable _peakValueTimerSubscription; private readonly SemaphoreSlim _setDefaultSemaphore = new SemaphoreSlim(1); private readonly SemaphoreSlim _setDefaultCommSemaphore = new SemaphoreSlim(1); - private readonly AsyncAutoResetEvent _muteChangedResetEvent = new AsyncAutoResetEvent(false); private readonly AsyncAutoResetEvent _volumeResetEvent = new AsyncAutoResetEvent(false); private readonly AsyncManualResetEvent _defaultResetEvent = new AsyncManualResetEvent(false); private readonly AsyncManualResetEvent _defaultCommResetEvent = new AsyncManualResetEvent(false); - private readonly IDisposable _peakValueTimerSubscription; + private EDataFlow _dataFlow; private IMultimediaDevice _device; private readonly CoreAudioController _controller; private Guid? _id; - private double _volume; private float _peakValue = -1; private CachedPropertyDictionary _properties; @@ -53,7 +53,7 @@ private IMultimediaDevice Device get { if (_isDisposed) - throw new ObjectDisposedException(""); + throw new ObjectDisposedException("COM Device Disposed"); return _device; } @@ -230,12 +230,31 @@ protected override void Dispose(bool disposing) _device = null; }); - _isDisposed = true; } base.Dispose(disposing); } + public override bool HasCapability() + { + if (typeof(TCapability) == typeof(IAudioSessionController)) + return true; + + return false; + } + + public override TCapability GetCapability() + { + if (_sessionController.Value is TCapability) + return (TCapability)(_sessionController.Value as IDeviceCapability); + + return default(TCapability); + } + + public override IEnumerable GetAllCapabilities() + { + yield return _sessionController?.Value; + } public override bool Mute(bool mute) { @@ -279,21 +298,6 @@ public override async Task SetVolumeAsync(double volume) return _volume; } - public override bool HasCapability() - { - return false; - } - - public override TCapability GetCapability() - { - return default(TCapability); - } - - public override IEnumerable GetAllCapabilities() - { - yield return null; - } - private static float NormailizeVolume(double volume) { if (volume <= 0) diff --git a/AudioSwitcher.AudioApi.Tests/DeviceTests.cs b/AudioSwitcher.AudioApi.Tests/DeviceTests.cs index 966d1cf..fe97d0d 100644 --- a/AudioSwitcher.AudioApi.Tests/DeviceTests.cs +++ b/AudioSwitcher.AudioApi.Tests/DeviceTests.cs @@ -3,7 +3,6 @@ using AudioSwitcher.AudioApi.Tests.Stubs; using AudioSwitcher.Tests.Common; using Moq; -using Moq.Protected; using Xunit; namespace AudioSwitcher.AudioApi.Tests diff --git a/AudioSwitcher.AudioApi.Tests/Stubs/DeviceStub.cs b/AudioSwitcher.AudioApi.Tests/Stubs/DeviceStub.cs index b6b1fa3..b7cfb2b 100644 --- a/AudioSwitcher.AudioApi.Tests/Stubs/DeviceStub.cs +++ b/AudioSwitcher.AudioApi.Tests/Stubs/DeviceStub.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AudioSwitcher.AudioApi.Tests.Stubs +namespace AudioSwitcher.AudioApi.Tests.Stubs { public abstract class DeviceStub : Device { diff --git a/AudioSwitcher.AudioApi/AudioController.Generic.cs b/AudioSwitcher.AudioApi/AudioController.Generic.cs index 662f811..09c2bc0 100644 --- a/AudioSwitcher.AudioApi/AudioController.Generic.cs +++ b/AudioSwitcher.AudioApi/AudioController.Generic.cs @@ -9,8 +9,7 @@ namespace AudioSwitcher.AudioApi public abstract class AudioController : IAudioController where T : class, IDevice { - protected const DeviceState DEFAULT_DEVICE_STATE_FILTER = - DeviceState.Active | DeviceState.Unplugged | DeviceState.Disabled; + private const DeviceState DEFAULT_DEVICE_STATE_FILTER = DeviceState.Active | DeviceState.Unplugged | DeviceState.Disabled; private readonly Broadcaster _audioDeviceChanged; @@ -19,37 +18,21 @@ protected AudioController() _audioDeviceChanged = new Broadcaster(); } - public virtual T DefaultPlaybackDevice - { - get - { - return GetDefaultDevice(DeviceType.Playback, Role.Console | Role.Multimedia); - } - } + public virtual T DefaultPlaybackDevice => GetDefaultDevice(DeviceType.Playback, Role.Console | Role.Multimedia); - public virtual T DefaultPlaybackCommunicationsDevice - { - get - { - return GetDefaultDevice(DeviceType.Playback, Role.Communications); - } - } + public virtual T DefaultPlaybackCommunicationsDevice => GetDefaultDevice(DeviceType.Playback, Role.Communications); - public virtual T DefaultCaptureDevice - { - get - { - return GetDefaultDevice(DeviceType.Capture, Role.Console | Role.Multimedia); - } - } + public virtual T DefaultCaptureDevice => GetDefaultDevice(DeviceType.Capture, Role.Console | Role.Multimedia); - public virtual T DefaultCaptureCommunicationsDevice - { - get - { - return GetDefaultDevice(DeviceType.Capture, Role.Communications); - } - } + public virtual T DefaultCaptureCommunicationsDevice => GetDefaultDevice(DeviceType.Capture, Role.Communications); + + IDevice IAudioController.DefaultPlaybackDevice => DefaultPlaybackDevice; + + IDevice IAudioController.DefaultPlaybackCommunicationsDevice => DefaultPlaybackCommunicationsDevice; + + IDevice IAudioController.DefaultCaptureDevice => DefaultCaptureDevice; + + IDevice IAudioController.DefaultCaptureCommunicationsDevice => DefaultCaptureCommunicationsDevice; public IObservable AudioDeviceChanged => _audioDeviceChanged.AsObservable(); @@ -169,15 +152,6 @@ Task IAudioController.GetDeviceAsync(Guid id) return Task.FromResult(GetDevice(id) as IDevice); } - IDevice IAudioController.DefaultPlaybackDevice => DefaultPlaybackDevice; - - IDevice IAudioController.DefaultPlaybackCommunicationsDevice => DefaultPlaybackCommunicationsDevice; - - IDevice IAudioController.DefaultCaptureDevice => DefaultCaptureDevice; - - IDevice IAudioController.DefaultCaptureCommunicationsDevice => DefaultCaptureCommunicationsDevice; - - IDevice IAudioController.GetDevice(Guid id) { return GetDevice(id); diff --git a/AudioSwitcher.AudioApi/AudioSwitcher.AudioApi.csproj b/AudioSwitcher.AudioApi/AudioSwitcher.AudioApi.csproj index be35ff6..3561a7a 100644 --- a/AudioSwitcher.AudioApi/AudioSwitcher.AudioApi.csproj +++ b/AudioSwitcher.AudioApi/AudioSwitcher.AudioApi.csproj @@ -93,7 +93,6 @@ - diff --git a/AudioSwitcher.AudioApi/Capabilities/IDefaultDeviceFormat.cs b/AudioSwitcher.AudioApi/Capabilities/IDefaultDeviceFormat.cs index afb1bd6..45dde2a 100644 --- a/AudioSwitcher.AudioApi/Capabilities/IDefaultDeviceFormat.cs +++ b/AudioSwitcher.AudioApi/Capabilities/IDefaultDeviceFormat.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AudioSwitcher.AudioApi.Capabilities +namespace AudioSwitcher.AudioApi.Capabilities { public interface IDefaultDeviceFormat : IDeviceCapability { diff --git a/AudioSwitcher.AudioApi/Capabilities/ISpeakerConfiguration.cs b/AudioSwitcher.AudioApi/Capabilities/ISpeakerConfiguration.cs index 992de5c..910a48c 100644 --- a/AudioSwitcher.AudioApi/Capabilities/ISpeakerConfiguration.cs +++ b/AudioSwitcher.AudioApi/Capabilities/ISpeakerConfiguration.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AudioSwitcher.AudioApi.Capabilities +namespace AudioSwitcher.AudioApi.Capabilities { public interface ISpeakerConfiguration : IDeviceCapability { diff --git a/AudioSwitcher.AudioApi/Sandbox/SandboxAudioController.cs b/AudioSwitcher.AudioApi/Sandbox/SandboxAudioController.cs index 1afd2d2..4e5d0dc 100644 --- a/AudioSwitcher.AudioApi/Sandbox/SandboxAudioController.cs +++ b/AudioSwitcher.AudioApi/Sandbox/SandboxAudioController.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; namespace AudioSwitcher.AudioApi.Sandbox { @@ -20,21 +19,12 @@ public SandboxAudioController(IAudioController source) //Get a copy of the current system audio devices //then create a copy of the current state of the system //this allows us to "debug" macros against a "test" system - _defaultPlaybackDeviceId = source.DefaultPlaybackDevice == null - ? Guid.Empty - : source.DefaultPlaybackDevice.Id; - _defaultPlaybackCommDeviceId = source.DefaultPlaybackCommunicationsDevice == null - ? Guid.Empty - : source.DefaultPlaybackCommunicationsDevice.Id; - _defaultCaptureDeviceId = source.DefaultCaptureDevice == null ? Guid.Empty : source.DefaultCaptureDevice.Id; - _defaultCaptureCommDeviceId = source.DefaultCaptureCommunicationsDevice == null - ? Guid.Empty - : source.DefaultCaptureCommunicationsDevice.Id; - - foreach ( - var sourceDev in - source.GetDevices(DeviceType.All, - DeviceState.Active | DeviceState.Unplugged | DeviceState.Disabled)) + _defaultPlaybackDeviceId = source.DefaultPlaybackDevice?.Id ?? Guid.Empty; + _defaultPlaybackCommDeviceId = source.DefaultPlaybackCommunicationsDevice?.Id ?? Guid.Empty; + _defaultCaptureDeviceId = source.DefaultCaptureDevice?.Id ?? Guid.Empty; + _defaultCaptureCommDeviceId = source.DefaultCaptureCommunicationsDevice?.Id ?? Guid.Empty; + + foreach (var sourceDev in source.GetDevices(DeviceType.All, DeviceState.All)) { var dev = new SandboxDevice(this) { diff --git a/AudioSwitcher.AudioApi/Session/IAudioSessionController.cs b/AudioSwitcher.AudioApi/Session/IAudioSessionController.cs index c134134..6334a88 100644 --- a/AudioSwitcher.AudioApi/Session/IAudioSessionController.cs +++ b/AudioSwitcher.AudioApi/Session/IAudioSessionController.cs @@ -4,7 +4,7 @@ namespace AudioSwitcher.AudioApi.Session { - public interface IAudioSessionController : IEnumerable + public interface IAudioSessionController : IDeviceCapability, IEnumerable { IObservable SessionCreated { get; } diff --git a/AudioSwitcher.AudioApi/Session/IAudioSessionEndpoint.cs b/AudioSwitcher.AudioApi/Session/IAudioSessionEndpoint.cs deleted file mode 100644 index d161259..0000000 --- a/AudioSwitcher.AudioApi/Session/IAudioSessionEndpoint.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace AudioSwitcher.AudioApi.Session -{ - public interface IAudioSessionEndpoint - { - /// - /// Returns null if sessions are not available - /// - IAudioSessionController SessionController { get; } - } -} \ No newline at end of file diff --git a/AudioSwitcher.Scripting/DefaultExecutionContext.cs b/AudioSwitcher.Scripting/DefaultExecutionContext.cs index 6b75389..30883d3 100644 --- a/AudioSwitcher.Scripting/DefaultExecutionContext.cs +++ b/AudioSwitcher.Scripting/DefaultExecutionContext.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Threading; using System.Threading.Tasks; namespace AudioSwitcher.Scripting diff --git a/Samples/HookingSample/MainWindow.xaml.cs b/Samples/HookingSample/MainWindow.xaml.cs index f6586ed..3d87a4f 100644 --- a/Samples/HookingSample/MainWindow.xaml.cs +++ b/Samples/HookingSample/MainWindow.xaml.cs @@ -8,6 +8,7 @@ using AudioSwitcher.AudioApi; using AudioSwitcher.AudioApi.CoreAudio; using AudioSwitcher.AudioApi.Hooking; +using AudioSwitcher.AudioApi.Session; using Role = AudioSwitcher.AudioApi.Hooking.ComObjects.Role; namespace HookingSample @@ -90,7 +91,7 @@ public MainWindow() Controller.DefaultPlaybackDevice.SetAsDefault(); - Controller.DefaultPlaybackDevice.SessionController.All(); + Controller.DefaultPlaybackDevice.GetCapability(); Controller.DefaultCaptureDevice.PeakValueChanged.Subscribe(x => { @@ -104,7 +105,7 @@ public MainWindow() Thread.Sleep(100); - foreach (var audioSession in Controller.DefaultPlaybackDevice.SessionController.All()) + foreach (var audioSession in Controller.DefaultPlaybackDevice.GetCapability()) { Console.WriteLine(audioSession.Id); } @@ -127,7 +128,7 @@ public MainWindow() // }); //} - Controller.DefaultPlaybackDevice.SessionController.SessionCreated.Subscribe(x => + Controller.DefaultPlaybackDevice.GetCapability()?.SessionCreated.Subscribe(x => { Console.WriteLine("{0} - {1}", x.DisplayName, x.Volume); //x.VolumeChanged.Subscribe(v => @@ -135,11 +136,11 @@ public MainWindow() //}); }); - Controller.DefaultPlaybackDevice.SessionController.SessionDisconnected.Subscribe(x => + Controller.DefaultPlaybackDevice.GetCapability()?.SessionDisconnected.Subscribe(x => { Console.WriteLine(x); - foreach (var session in Controller.DefaultPlaybackDevice.SessionController) + foreach (var session in Controller.DefaultPlaybackDevice.GetCapability()) { Console.WriteLine("{0} - {1}", session.DisplayName, session.Volume); } diff --git a/SharedAssemblyInfo.cs b/SharedAssemblyInfo.cs index 4146399..ae5fd1d 100644 --- a/SharedAssemblyInfo.cs +++ b/SharedAssemblyInfo.cs @@ -18,6 +18,6 @@ // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("3.1.0.294")] -[assembly: AssemblyFileVersion("3.1.0.298")] -[assembly: AssemblyInformationalVersion("3.1.0.298")] \ No newline at end of file +[assembly: AssemblyVersion("4.0.0.308")] +[assembly: AssemblyFileVersion("4.0.0.308")] +[assembly: AssemblyInformationalVersion("4.0.0.308")] \ No newline at end of file