-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathCoreAudioDevice.SessionController.cs
57 lines (47 loc) · 1.99 KB
/
CoreAudioDevice.SessionController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Runtime.InteropServices;
using AudioSwitcher.AudioApi.CoreAudio.Threading;
using AudioSwitcher.AudioApi.CoreAudio.Interfaces;
namespace AudioSwitcher.AudioApi.CoreAudio
{
public partial class CoreAudioDevice
{
private Lazy<CoreAudioSessionController> _sessionController;
private void ClearAudioSession()
{
if (_sessionController?.IsValueCreated == true)
_sessionController?.Value?.Dispose();
_sessionController = null;
}
private void LoadAudioSessionController()
{
if (_sessionController?.IsValueCreated == true)
return;
_sessionController = new Lazy<CoreAudioSessionController>(() =>
{
return ComThread.Invoke(() =>
{
//Need to catch here, as there is a chance that unauthorized is thrown.
//It's not an HR exception, but bubbles up through the .net call stack
try
{
var clsGuid = new Guid(ComInterfaceIds.AUDIO_SESSION_MANAGER2_IID);
object result;
Marshal.GetExceptionForHR(Device.Activate(ref clsGuid, ClassContext.Inproc, IntPtr.Zero, out result));
//This is scoped into the managed object, so disposal is taken care of there.
var audioSessionManager = result as IAudioSessionManager2;
if (audioSessionManager != null)
return new CoreAudioSessionController(this, audioSessionManager);
}
catch (Exception)
{
if (_sessionController?.IsValueCreated == true)
_sessionController?.Value?.Dispose();
_sessionController = null;
}
return null;
});
});
}
}
}