Skip to content

Commit

Permalink
Converted the audio session controller into a capability.
Browse files Browse the repository at this point in the history
Rewrote applicable tests

version bump for 4.0.0.0 alpha
  • Loading branch information
xenolightning committed Mar 2, 2016
1 parent ecede9a commit 8f92c13
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 131 deletions.
13 changes: 12 additions & 1 deletion AudioSwitcher.AudioApi.CoreAudio.Tests/SessionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using AudioSwitcher.AudioApi.Session;
using Xunit;

namespace AudioSwitcher.AudioApi.CoreAudio.Tests
Expand All @@ -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<IAudioSessionController>());
}
}

[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<IAudioSessionController>().First();

var oldDMute = device.IsMuted;
var oldSMute = session.IsMuted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
<ItemGroup>
<None Include="api.pfx" />
<Compile Include="CoreAudioDevice.Internal.cs" />
<Compile Include="CoreAudioDevice.AudioSession.cs" />
<Compile Include="CoreAudioDevice.SessionController.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AudioSwitcher.AudioApi\AudioSwitcher.AudioApi.csproj">
Expand Down
1 change: 0 additions & 1 deletion AudioSwitcher.AudioApi.CoreAudio/CoreAudioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CoreAudioSessionController> _sessionController;

public IAudioSessionController SessionController => _sessionController?.Value;
private Lazy<CoreAudioSessionController> _sessionController;

private void ClearAudioSession()
{
Expand All @@ -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<CoreAudioSessionController>(() =>
{
return ComThread.Invoke(() =>
Expand Down
44 changes: 24 additions & 20 deletions AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,19 +24,18 @@ public sealed partial class CoreAudioDevice : Device
{PropertyKeys.PKEY_DEVICE_ICON, new HashSet<string>{ 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;
Expand All @@ -53,7 +53,7 @@ private IMultimediaDevice Device
get
{
if (_isDisposed)
throw new ObjectDisposedException("");
throw new ObjectDisposedException("COM Device Disposed");

return _device;
}
Expand Down Expand Up @@ -230,12 +230,31 @@ protected override void Dispose(bool disposing)
_device = null;
});


_isDisposed = true;
}

base.Dispose(disposing);
}
public override bool HasCapability<TCapability>()
{
if (typeof(TCapability) == typeof(IAudioSessionController))
return true;

return false;
}

public override TCapability GetCapability<TCapability>()
{
if (_sessionController.Value is TCapability)
return (TCapability)(_sessionController.Value as IDeviceCapability);

return default(TCapability);
}

public override IEnumerable<IDeviceCapability> GetAllCapabilities()
{
yield return _sessionController?.Value;
}

public override bool Mute(bool mute)
{
Expand Down Expand Up @@ -279,21 +298,6 @@ public override async Task<double> SetVolumeAsync(double volume)
return _volume;
}

public override bool HasCapability<TCapability>()
{
return false;
}

public override TCapability GetCapability<TCapability>()
{
return default(TCapability);
}

public override IEnumerable<IDeviceCapability> GetAllCapabilities()
{
yield return null;
}

private static float NormailizeVolume(double volume)
{
if (volume <= 0)
Expand Down
1 change: 0 additions & 1 deletion AudioSwitcher.AudioApi.Tests/DeviceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using AudioSwitcher.AudioApi.Tests.Stubs;
using AudioSwitcher.Tests.Common;
using Moq;
using Moq.Protected;
using Xunit;

namespace AudioSwitcher.AudioApi.Tests
Expand Down
8 changes: 1 addition & 7 deletions AudioSwitcher.AudioApi.Tests/Stubs/DeviceStub.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
52 changes: 13 additions & 39 deletions AudioSwitcher.AudioApi/AudioController.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ namespace AudioSwitcher.AudioApi
public abstract class AudioController<T> : IAudioController<T>
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<DeviceChangedArgs> _audioDeviceChanged;

Expand All @@ -19,37 +18,21 @@ protected AudioController()
_audioDeviceChanged = new Broadcaster<DeviceChangedArgs>();
}

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<DeviceChangedArgs> AudioDeviceChanged => _audioDeviceChanged.AsObservable();

Expand Down Expand Up @@ -169,15 +152,6 @@ Task<IDevice> 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);
Expand Down
1 change: 0 additions & 1 deletion AudioSwitcher.AudioApi/AudioSwitcher.AudioApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
<Compile Include="Session\SessionStateChangedArgs.cs" />
<Compile Include="Session\IAudioSession.cs" />
<Compile Include="Session\IAudioSessionController.cs" />
<Compile Include="Session\IAudioSessionEndpoint.cs" />
<Compile Include="Session\SessionPeakValueChangedArgs.cs" />
<Compile Include="Session\SessionVolumeChangedArgs.cs" />
<Compile Include="SpeakerConfiguration.cs" />
Expand Down
8 changes: 1 addition & 7 deletions AudioSwitcher.AudioApi/Capabilities/IDefaultDeviceFormat.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
8 changes: 1 addition & 7 deletions AudioSwitcher.AudioApi/Capabilities/ISpeakerConfiguration.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
22 changes: 6 additions & 16 deletions AudioSwitcher.AudioApi/Sandbox/SandboxAudioController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AudioSwitcher.AudioApi.Sandbox
{
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion AudioSwitcher.AudioApi/Session/IAudioSessionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AudioSwitcher.AudioApi.Session
{
public interface IAudioSessionController : IEnumerable<IAudioSession>
public interface IAudioSessionController : IDeviceCapability, IEnumerable<IAudioSession>
{
IObservable<IAudioSession> SessionCreated { get; }

Expand Down
10 changes: 0 additions & 10 deletions AudioSwitcher.AudioApi/Session/IAudioSessionEndpoint.cs

This file was deleted.

1 change: 0 additions & 1 deletion AudioSwitcher.Scripting/DefaultExecutionContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace AudioSwitcher.Scripting
Expand Down
Loading

0 comments on commit 8f92c13

Please sign in to comment.