[Multimedia] Fixed minor issues and clean codes in AudioManager. (#82)
authorcoderhyme <jhyo.kim@samsung.com>
Mon, 29 Jan 2018 04:04:51 +0000 (13:04 +0900)
committerGitHub <noreply@github.com>
Mon, 29 Jan 2018 04:04:51 +0000 (13:04 +0900)
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>
src/Tizen.Multimedia/AudioManager/AudioDevice.cs
src/Tizen.Multimedia/AudioManager/AudioManager.cs
src/Tizen.Multimedia/AudioManager/AudioManagerError.cs
src/Tizen.Multimedia/AudioManager/AudioStreamPolicy.cs
src/Tizen.Multimedia/AudioManager/AudioVolume.cs
src/Tizen.Multimedia/AudioManager/MaxVolumeLevel.cs
src/Tizen.Multimedia/AudioManager/VolumeLevel.cs
src/Tizen.Multimedia/Interop/Interop.Device.cs

index cd7bcc2..9b9cc1e 100755 (executable)
@@ -42,7 +42,7 @@ namespace Tizen.Multimedia
             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);
         }
 
@@ -84,7 +84,7 @@ namespace Tizen.Multimedia
             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;
             }
index ca3745c..104cae1 100755 (executable)
@@ -50,34 +50,39 @@ namespace Tizen.Multimedia
             {
                 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
@@ -85,7 +90,7 @@ namespace Tizen.Multimedia
 
         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.
@@ -95,6 +100,11 @@ namespace Tizen.Multimedia
         {
             add
             {
+                if (value == null)
+                {
+                    return;
+                }
+
                 lock (_audioDeviceConnectionLock)
                 {
                     if (_audioDeviceConnectionChanged == null)
@@ -124,7 +134,7 @@ namespace Tizen.Multimedia
 
         private static void RegisterAudioDeviceEvent()
         {
-            _audioDeviceConnectionChangedCallback = (IntPtr device, bool isConnected, IntPtr userData) =>
+            _audioDeviceConnectionChangedCallback = (device, isConnected, _) =>
             {
                 _audioDeviceConnectionChanged?.Invoke(null,
                     new AudioDeviceConnectionChangedEventArgs(new AudioDevice(device), isConnected));
@@ -132,13 +142,13 @@ namespace Tizen.Multimedia
 
             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
 
@@ -147,7 +157,7 @@ namespace Tizen.Multimedia
 
         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.
@@ -157,6 +167,11 @@ namespace Tizen.Multimedia
         {
             add
             {
+                if (value == null)
+                {
+                    return;
+                }
+
                 lock (_audioDeviceStateLock)
                 {
                     if (_audioDeviceStateChanged == null)
@@ -186,7 +201,7 @@ namespace Tizen.Multimedia
 
         private static void RegisterDeviceStateChangedEvent()
         {
-            _audioDeviceStateChangedCallback = (IntPtr device, AudioDeviceState changedState, IntPtr userData) =>
+            _audioDeviceStateChangedCallback = (device, changedState, _) =>
             {
                 _audioDeviceStateChanged?.Invoke(null,
                     new AudioDeviceStateChangedEventArgs(new AudioDevice(device), changedState));
@@ -194,13 +209,13 @@ namespace Tizen.Multimedia
 
             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
     }
index f990248..a6bb150 100644 (file)
@@ -70,15 +70,14 @@ namespace Tizen.Multimedia
 
     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)
             {
@@ -98,7 +97,6 @@ namespace Tizen.Multimedia
                     throw new AudioPolicyException(msg);
 
                 case AudioManagerError.NoData:
-                    // TODO check when it is thrown
                     throw new InvalidOperationException(msg);
 
                 case AudioManagerError.Internal:
index fb05936..9d0365b 100755 (executable)
@@ -46,14 +46,14 @@ namespace Tizen.Multimedia
 
             _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);
         }
@@ -81,14 +81,13 @@ namespace Tizen.Multimedia
         {
             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;
             }
@@ -136,14 +135,14 @@ namespace Tizen.Multimedia
             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");
             }
         }
 
@@ -193,7 +192,7 @@ namespace Tizen.Multimedia
             }
 
             Interop.AudioStreamPolicy.AcquireFocus(Handle, options, behaviors, extraInfo).
-                Validate("Failed to acquire focus");
+                ThrowIfError("Failed to acquire focus");
         }
 
         /// <summary>
@@ -229,7 +228,7 @@ namespace Tizen.Multimedia
             }
 
             Interop.AudioStreamPolicy.ReleaseFocus(Handle, options, behaviors, extraInfo).
-                Validate("Failed to release focus");
+                ThrowIfError("Failed to release focus");
         }
 
         /// <summary>
@@ -244,7 +243,7 @@ namespace Tizen.Multimedia
         /// <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>
@@ -279,7 +278,7 @@ namespace Tizen.Multimedia
                 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>
@@ -302,7 +301,7 @@ namespace Tizen.Multimedia
             }
 
             Interop.AudioStreamPolicy.RemoveDeviceForStreamRouting(Handle, device.Id).
-                Validate("Failed to remove device for stream routing");
+                ThrowIfError("Failed to remove device for stream routing");
         }
 
         /// <summary>
@@ -336,7 +335,7 @@ namespace Tizen.Multimedia
         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.
@@ -367,8 +366,7 @@ namespace Tizen.Multimedia
 
         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));
@@ -377,7 +375,7 @@ namespace Tizen.Multimedia
             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
     }
index 7667176..eaa31e7 100755 (executable)
@@ -31,7 +31,7 @@ namespace Tizen.Multimedia
         private EventHandler<VolumeChangedEventArgs> _volumeChanged;
         private Interop.AudioVolume.VolumeChangedCallback _volumeChangedCallback;
 
-        private object _eventLock = new object();
+        private readonly object _eventLock = new object();
 
         internal AudioVolume()
         {
@@ -47,6 +47,11 @@ namespace Tizen.Multimedia
         {
             add
             {
+                if (value == null)
+                {
+                    return;
+                }
+
                 lock (_eventLock)
                 {
                     if (_volumeChanged == null)
@@ -88,7 +93,7 @@ namespace Tizen.Multimedia
                 {
                     return AudioVolumeType.None;
                 }
-                ret.Validate("Failed to get current volume type");
+                ret.ThrowIfError("Failed to get current volume type");
 
                 return currentType;
             }
@@ -110,21 +115,22 @@ namespace Tizen.Multimedia
 
         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");
         }
     }
 }
index ea15e7b..0939c75 100644 (file)
@@ -49,7 +49,7 @@ namespace Tizen.Multimedia
                 }
 
                 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;
             }
index 6ce27d3..4816e72 100644 (file)
@@ -57,7 +57,7 @@ namespace Tizen.Multimedia
                         "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;
             }
@@ -79,7 +79,7 @@ namespace Tizen.Multimedia
                         $"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");
             }
         }
     }
index 0cc2ecb..17f777f 100644 (file)
@@ -42,7 +42,7 @@ namespace Tizen.Multimedia
             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);
@@ -68,4 +68,4 @@ namespace Tizen.Multimedia
             internal static extern AudioManagerError RemoveDeviceStateChangedCallback(int id);
         }
     }
-}
\ No newline at end of file
+}