private CoreAudioSession CacheSessionWrapper(IAudioSessionControl session)
        {
            var managedSession = new CoreAudioSession(_device, session);

            //There's some dumb crap in the Api that causes the sessions to still appear
            //even after the process has been terminated
            if (Process.GetProcesses().All(x => x.Id != managedSession.ProcessId))
            {
                return(null);
            }

            var acquiredLock = _lock.AcquireReadLockNonReEntrant();

            try
            {
                var existing = _sessionCache.FirstOrDefault(x => x.ProcessId == managedSession.ProcessId && String.Equals(x.Id, managedSession.Id));
                if (existing != null)
                {
                    managedSession.Dispose();
                    return(existing);
                }
            }
            finally
            {
                if (acquiredLock)
                {
                    _lock.ExitReadLock();
                }
            }

            managedSession.StateChanged.Subscribe(ManagedSessionOnStateChanged);
            managedSession.Disconnected.Subscribe(ManagedSessionOnDisconnected);

            acquiredLock = _lock.AcquireWriteLockNonReEntrant();

            try
            {
                _sessionCache.Add(managedSession);
            }
            finally
            {
                if (acquiredLock)
                {
                    _lock.ExitWriteLock();
                }
            }

            return(managedSession);
        }
        private void RemoveDeviceFromRealId(string deviceId)
        {
            var lockAcquired = _lock.AcquireWriteLockNonReEntrant();

            try
            {
                _deviceCache.RemoveWhere(
                    x => String.Equals(x.RealId, deviceId, StringComparison.InvariantCultureIgnoreCase));
            }
            finally
            {
                if (lockAcquired)
                {
                    _lock.ExitWriteLock();
                }
            }
        }
Example #3
0
        private IEnumerable <CoreAudioDevice> RemoveFromRealId(string deviceId)
        {
            var lockAcquired = _lock.AcquireWriteLockNonReEntrant();

            try
            {
                var devicesToRemove =
                    _deviceCache.Where(x => String.Equals(x.RealId, deviceId, StringComparison.InvariantCultureIgnoreCase)).ToList();

                _deviceCache.RemoveWhere(x => String.Equals(x.RealId, deviceId, StringComparison.InvariantCultureIgnoreCase));

                return(devicesToRemove);
            }
            finally
            {
                if (lockAcquired)
                {
                    _lock.ExitWriteLock();
                }
            }
        }