1. Adding null event handler - it should be prevented because if no handler is added before, then no need to register the native callback.
2. Renames error validation method to ThrowIfError for readability.
And some minor code improvements
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
ret = Interop.AudioDevice.GetDeviceType(deviceHandle, out _type);
MultimediaDebug.AssertNoError(ret);
- ret = (int)Interop.AudioDevice.GetDeviceIoDirection(deviceHandle, out _ioDirection);
+ ret = Interop.AudioDevice.GetDeviceIoDirection(deviceHandle, out _ioDirection);
MultimediaDebug.AssertNoError(ret);
}
get
{
Interop.AudioDevice.GetDeviceState(Id, out var state).
- Validate("Failed to get the state of the device");
+ ThrowIfError("Failed to get the state of the device");
return state;
}
{
var ret = Interop.AudioDevice.GetDeviceList(AudioDeviceOptions.All, out deviceListHandle);
- List<AudioDevice> result = new List<AudioDevice>();
-
if (ret == AudioManagerError.NoData)
{
- return result;
+ return new List<AudioDevice>();
}
- ret.Validate("Failed to get connected devices");
+ ret.ThrowIfError("Failed to get connected devices");
+
+ return RetrieveDevices();
+ }
+ finally
+ {
+ Interop.AudioDevice.FreeDeviceList(deviceListHandle);
+ }
+
+ IEnumerable<AudioDevice> RetrieveDevices()
+ {
+ var result = new List<AudioDevice>();
- while (ret == AudioManagerError.None)
+ while (true)
{
- ret = Interop.AudioDevice.GetNextDevice(deviceListHandle, out var deviceHandle);
+ var ret = Interop.AudioDevice.GetNextDevice(deviceListHandle, out var deviceHandle);
if (ret == AudioManagerError.NoData)
{
break;
}
- ret.Validate("Failed to get connected devices");
+ ret.ThrowIfError("Failed to get connected devices");
result.Add(new AudioDevice(deviceHandle));
}
return result;
}
- finally
- {
- Interop.AudioDevice.FreeDeviceList(deviceListHandle);
- }
}
#region DeviceConnectionChanged event
private static Interop.AudioDevice.ConnectionChangedCallback _audioDeviceConnectionChangedCallback;
private static EventHandler<AudioDeviceConnectionChangedEventArgs> _audioDeviceConnectionChanged;
- private static object _audioDeviceConnectionLock = new object();
+ private static readonly object _audioDeviceConnectionLock = new object();
/// <summary>
/// Occurs when the state of a connection of an audio device changes.
{
add
{
+ if (value == null)
+ {
+ return;
+ }
+
lock (_audioDeviceConnectionLock)
{
if (_audioDeviceConnectionChanged == null)
private static void RegisterAudioDeviceEvent()
{
- _audioDeviceConnectionChangedCallback = (IntPtr device, bool isConnected, IntPtr userData) =>
+ _audioDeviceConnectionChangedCallback = (device, isConnected, _) =>
{
_audioDeviceConnectionChanged?.Invoke(null,
new AudioDeviceConnectionChangedEventArgs(new AudioDevice(device), isConnected));
Interop.AudioDevice.AddDeviceConnectionChangedCallback(AudioDeviceOptions.All,
_audioDeviceConnectionChangedCallback, IntPtr.Zero, out _deviceConnectionChangedCallbackId).
- Validate("Unable to add device connection changed callback");
+ ThrowIfError("Unable to add device connection changed callback");
}
private static void UnregisterDeviceConnectionChangedEvent()
{
Interop.AudioDevice.RemoveDeviceConnectionChangedCallback(_deviceConnectionChangedCallbackId).
- Validate("Unable to remove device connection changed callback");
+ ThrowIfError("Unable to remove device connection changed callback");
}
#endregion
private static Interop.AudioDevice.StateChangedCallback _audioDeviceStateChangedCallback;
private static EventHandler<AudioDeviceStateChangedEventArgs> _audioDeviceStateChanged;
- private static object _audioDeviceStateLock = new object();
+ private static readonly object _audioDeviceStateLock = new object();
/// <summary>
/// Occurs when the state of an audio device changes.
{
add
{
+ if (value == null)
+ {
+ return;
+ }
+
lock (_audioDeviceStateLock)
{
if (_audioDeviceStateChanged == null)
private static void RegisterDeviceStateChangedEvent()
{
- _audioDeviceStateChangedCallback = (IntPtr device, AudioDeviceState changedState, IntPtr userData) =>
+ _audioDeviceStateChangedCallback = (device, changedState, _) =>
{
_audioDeviceStateChanged?.Invoke(null,
new AudioDeviceStateChangedEventArgs(new AudioDevice(device), changedState));
Interop.AudioDevice.AddDeviceStateChangedCallback(AudioDeviceOptions.All,
_audioDeviceStateChangedCallback, IntPtr.Zero, out _deviceStateChangedCallbackId).
- Validate("Failed to add device state changed event");
+ ThrowIfError("Failed to add device state changed event");
}
private static void UnregisterDeviceStateChangedEvent()
{
Interop.AudioDevice.RemoveDeviceStateChangedCallback(_deviceStateChangedCallbackId).
- Validate("Failed to remove device state changed event");
+ ThrowIfError("Failed to remove device state changed event");
}
#endregion
}
internal static class AudioManagerErrorExtensions
{
- internal static void Validate(this AudioManagerError err, string msg)
+ internal static void ThrowIfError(this AudioManagerError err, string msg)
{
if (err == AudioManagerError.None)
{
return;
}
- msg = msg ?? "";
- msg += $" : {err}.";
+ msg = $"{msg ?? ""} : {err}.";
switch (err)
{
throw new AudioPolicyException(msg);
case AudioManagerError.NoData:
- // TODO check when it is thrown
throw new InvalidOperationException(msg);
case AudioManagerError.Internal:
_focusStateChangedCallback = (IntPtr streamInfo, AudioStreamFocusOptions focusMask,
AudioStreamFocusState state, AudioStreamFocusChangedReason reason, AudioStreamBehaviors behaviors,
- string extraInfo, IntPtr userData) =>
+ string extraInfo, IntPtr _) =>
{
FocusStateChanged?.Invoke(this,
new AudioStreamPolicyFocusStateChangedEventArgs(focusMask, state, reason, behaviors, extraInfo));
};
Interop.AudioStreamPolicy.Create(streamType, _focusStateChangedCallback,
- IntPtr.Zero, out _handle).Validate("Unable to create stream information");
+ IntPtr.Zero, out _handle).ThrowIfError("Unable to create stream information");
Debug.Assert(_handle != null);
}
{
get
{
- AudioVolumeType type;
- var ret = Interop.AudioStreamPolicy.GetSoundType(Handle, out type);
+ var ret = Interop.AudioStreamPolicy.GetSoundType(Handle, out var type);
if (ret == AudioManagerError.NoData)
{
return AudioVolumeType.None;
}
- ret.Validate("Failed to get volume type");
+ ret.ThrowIfError("Failed to get volume type");
return type;
}
get
{
Interop.AudioStreamPolicy.GetFocusReacquisition(Handle, out var enabled).
- Validate("Failed to get focus reacquisition state");
+ ThrowIfError("Failed to get focus reacquisition state");
return enabled;
}
set
{
Interop.AudioStreamPolicy.SetFocusReacquisition(Handle, value).
- Validate("Failed to set focus reacquisition");
+ ThrowIfError("Failed to set focus reacquisition");
}
}
}
Interop.AudioStreamPolicy.AcquireFocus(Handle, options, behaviors, extraInfo).
- Validate("Failed to acquire focus");
+ ThrowIfError("Failed to acquire focus");
}
/// <summary>
}
Interop.AudioStreamPolicy.ReleaseFocus(Handle, options, behaviors, extraInfo).
- Validate("Failed to release focus");
+ ThrowIfError("Failed to release focus");
}
/// <summary>
/// <since_tizen> 3 </since_tizen>
public void ApplyStreamRouting()
{
- Interop.AudioStreamPolicy.ApplyStreamRouting(Handle).Validate("Failed to apply stream routing");
+ Interop.AudioStreamPolicy.ApplyStreamRouting(Handle).ThrowIfError("Failed to apply stream routing");
}
/// <summary>
throw new InvalidOperationException("The device seems not connected.");
}
- ret.Validate("Failed to add device for stream routing");
+ ret.ThrowIfError("Failed to add device for stream routing");
}
/// <summary>
}
Interop.AudioStreamPolicy.RemoveDeviceForStreamRouting(Handle, device.Id).
- Validate("Failed to remove device for stream routing");
+ ThrowIfError("Failed to remove device for stream routing");
}
/// <summary>
private static bool _isWatchCallbackRegistered;
private static EventHandler<StreamFocusStateChangedEventArgs> _streamFocusStateChanged;
private static Interop.AudioStreamPolicy.FocusStateWatchCallback _focusStateWatchCallback;
- private static object _streamFocusEventLock = new object();
+ private static readonly object _streamFocusEventLock = new object();
/// <summary>
/// Occurs when the focus state for stream types is changed regardless of the process.
private static void RegisterFocusStateWatch()
{
- _focusStateWatchCallback = (int id, AudioStreamFocusOptions options, AudioStreamFocusState focusState,
- AudioStreamFocusChangedReason reason, string extraInfo, IntPtr userData) =>
+ _focusStateWatchCallback = (id, options, focusState, reason, extraInfo, _) =>
{
_streamFocusStateChanged?.Invoke(null,
new StreamFocusStateChangedEventArgs(options, focusState, reason, extraInfo));
Interop.AudioStreamPolicy.AddFocusStateWatchCallback(
AudioStreamFocusOptions.Playback | AudioStreamFocusOptions.Recording,
_focusStateWatchCallback, IntPtr.Zero, out var cbId).
- Validate("Failed to initialize focus state event");
+ ThrowIfError("Failed to initialize focus state event");
}
#endregion
}
private EventHandler<VolumeChangedEventArgs> _volumeChanged;
private Interop.AudioVolume.VolumeChangedCallback _volumeChangedCallback;
- private object _eventLock = new object();
+ private readonly object _eventLock = new object();
internal AudioVolume()
{
{
add
{
+ if (value == null)
+ {
+ return;
+ }
+
lock (_eventLock)
{
if (_volumeChanged == null)
{
return AudioVolumeType.None;
}
- ret.Validate("Failed to get current volume type");
+ ret.ThrowIfError("Failed to get current volume type");
return currentType;
}
private void RegisterVolumeChangedEvent()
{
- _volumeChangedCallback = (AudioVolumeType type, uint volume, IntPtr userData) =>
+ _volumeChangedCallback = (type, volume, _) =>
{
_volumeChanged?.Invoke(this, new VolumeChangedEventArgs(type, volume));
};
+
var error = Interop.AudioVolume.AddVolumeChangedCallback(_volumeChangedCallback, IntPtr.Zero,
out _volumeChangedCallbackId);
Log.Info(Tag, $"VolumeController callback id:{_volumeChangedCallbackId}");
- error.Validate("Failed to add volume changed event");
+ error.ThrowIfError("Failed to add volume changed event");
}
private void UnregisterVolumeChangedEvent()
{
Interop.AudioVolume.RemoveVolumeChangedCallback(_volumeChangedCallbackId).
- Validate("Failed to remove volume changed event");
+ ThrowIfError("Failed to remove volume changed event");
}
}
}
}
Interop.AudioVolume.GetMaxVolume(type, out var maxVolume).
- Validate("Failed to get the max volume level");
+ ThrowIfError("Failed to get the max volume level");
return maxVolume;
}
"Cannot get volume level for AudioVolumeType.None");
}
- Interop.AudioVolume.GetVolume(type, out var volume).Validate("Failed to get the volume level");
+ Interop.AudioVolume.GetVolume(type, out var volume).ThrowIfError("Failed to get the volume level");
return volume;
}
$"valid volume level range is 0 <= x <= {nameof(MaxVolumeLevel)}[{nameof(AudioVolumeType)}]");
}
- ret.Validate("Failed to set the volume level");
+ ret.ThrowIfError("Failed to set the volume level");
}
}
}
internal static extern int GetDeviceType(IntPtr device, out AudioDeviceType type);
[DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_get_device_io_direction")]
- internal static extern AudioManagerError GetDeviceIoDirection(IntPtr device, out AudioDeviceIoDirection ioDirection);
+ internal static extern int GetDeviceIoDirection(IntPtr device, out AudioDeviceIoDirection ioDirection);
[DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_get_device_id")]
internal static extern int GetDeviceId(IntPtr device, out int id);
internal static extern AudioManagerError RemoveDeviceStateChangedCallback(int id);
}
}
-}
\ No newline at end of file
+}