[AudioManager] Add new APIs for device running changed event (#402)
authorhsgwon <haesu.gwon@samsung.com>
Wed, 22 Aug 2018 04:56:33 +0000 (13:56 +0900)
committerGitHub <noreply@github.com>
Wed, 22 Aug 2018 04:56:33 +0000 (13:56 +0900)
* [AudioManager] Add new APIs for device running changed event

* [AudioManager] Deprecate AudioDeviceStateChangedEventArgs class

* [AudioManager] fix typo

src/Tizen.Multimedia/AudioManager/AudioDevice.cs [changed mode: 0755->0644]
src/Tizen.Multimedia/AudioManager/AudioDeviceRunningChangedEventArgs.cs [new file with mode: 0644]
src/Tizen.Multimedia/AudioManager/AudioDeviceStateChangedEventArgs.cs
src/Tizen.Multimedia/AudioManager/AudioManager.cs [changed mode: 0755->0644]
src/Tizen.Multimedia/Interop/Interop.Device.cs

old mode 100755 (executable)
new mode 100644 (file)
index 9b9cc1e..ff26f85
@@ -1,4 +1,4 @@
-/*
+/*
  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
@@ -28,6 +28,7 @@ namespace Tizen.Multimedia
         private readonly int _id;
         private readonly AudioDeviceType _type;
         private readonly AudioDeviceIoDirection _ioDirection;
+        private readonly IntPtr _deviceHandle;
 
         internal AudioDevice(IntPtr deviceHandle)
         {
@@ -44,6 +45,8 @@ namespace Tizen.Multimedia
 
             ret = Interop.AudioDevice.GetDeviceIoDirection(deviceHandle, out _ioDirection);
             MultimediaDebug.AssertNoError(ret);
+
+            _deviceHandle = deviceHandle;
         }
 
         /// <summary>
@@ -79,6 +82,7 @@ namespace Tizen.Multimedia
         /// </summary>
         /// <value>The <see cref="AudioDeviceState"/> of the device.</value>
         /// <since_tizen> 3 </since_tizen>
+        [Obsolete("Deprecated since API level 5. Please use the IsRunning property instead.")]
         public AudioDeviceState State
         {
             get
@@ -91,6 +95,22 @@ namespace Tizen.Multimedia
         }
 
         /// <summary>
+        /// Gets the running state of the device.
+        /// </summary>
+        /// <value>true if the audio stream of device is running actually; otherwise, false.</value>
+        /// <since_tizen> 5 </since_tizen>
+        public bool IsRunning
+        {
+            get
+            {
+                Interop.AudioDevice.IsDeviceRunning(_deviceHandle, out bool isRunning).
+                    ThrowIfError("Failed to get the running state of the device");
+
+                return isRunning;
+            }
+        }
+
+        /// <summary>
         /// Returns a string that represents the current object.
         /// </summary>
         /// <returns>A string that represents the current object.</returns>
diff --git a/src/Tizen.Multimedia/AudioManager/AudioDeviceRunningChangedEventArgs.cs b/src/Tizen.Multimedia/AudioManager/AudioDeviceRunningChangedEventArgs.cs
new file mode 100644 (file)
index 0000000..795d2ee
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+
+namespace Tizen.Multimedia
+{
+    /// <summary>
+    /// Provides data for the <see cref="AudioManager.DeviceRunningChanged"/> event.
+    /// </summary>
+    /// <since_tizen> 5 </since_tizen>
+    public class AudioDeviceRunningChangedEventArgs : EventArgs
+    {
+        internal AudioDeviceRunningChangedEventArgs(AudioDevice device, bool isRunning)
+        {
+            Device = device;
+            IsRunning = isRunning;
+        }
+
+        /// <summary>
+        /// Gets the device.
+        /// </summary>
+        /// <value>The <see cref="AudioDevice"/>.</value>
+        /// <since_tizen> 5 </since_tizen>
+        public AudioDevice Device { get; }
+
+        /// <summary>
+        /// Gets the running state of the device.
+        /// </summary>
+        /// <value>true if the audio stream of device is running actually; otherwise, false.</value>
+        /// <since_tizen> 5 </since_tizen>
+        public bool IsRunning { get; }
+    }
+}
\ No newline at end of file
index 84093ce..34eb612 100644 (file)
@@ -22,6 +22,7 @@ namespace Tizen.Multimedia
     /// Provides data for the <see cref="AudioManager.DeviceStateChanged"/> event.
     /// </summary>
     /// <since_tizen> 3 </since_tizen>
+    [Obsolete("Deprecated since API level 5. Please use the AudioDeviceRunningChangedEventArgs class instead.")]
     public class AudioDeviceStateChangedEventArgs : EventArgs
     {
         internal AudioDeviceStateChangedEventArgs(AudioDevice device, AudioDeviceState changedState)
old mode 100755 (executable)
new mode 100644 (file)
index 104cae1..5b10e8b
@@ -1,4 +1,4 @@
-/*
+/*
  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
@@ -163,6 +163,7 @@ namespace Tizen.Multimedia
         /// Occurs when the state of an audio device changes.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        [Obsolete("Deprecated since API level 5. Please use the DeviceRunningStateChanged property instead.")]
         public static event EventHandler<AudioDeviceStateChangedEventArgs> DeviceStateChanged
         {
             add
@@ -218,5 +219,76 @@ namespace Tizen.Multimedia
                 ThrowIfError("Failed to remove device state changed event");
         }
         #endregion
+
+        #region DeviceRunningStateChanged event
+        private static int _deviceRunningChangedCallbackId = -1;
+        private static Interop.AudioDevice.RunningChangedCallback _audioDeviceRunningChangedCallback;
+        private static EventHandler<AudioDeviceRunningChangedEventArgs> _audioDeviceRunningChanged;
+        private static readonly object _audioDeviceRunningLock = new object();
+
+        /// <summary>
+        /// Occurs when the audio stream started actually to running on device.
+        /// </summary>
+        /// <remarks>
+        /// If this event is invoked once and audio stream is still running on device,<br/>
+        /// it will not invoked any more even if more audio stream runs again,<br/>
+        /// until all streams are stoped and another stream runs again.
+        /// </remarks>
+        /// <exception cref="InvalidOperationException">
+        /// AudioManager failed to communicate internally or allocate memory.
+        /// </exception>
+        /// <since_tizen> 5 </since_tizen>
+        public static event EventHandler<AudioDeviceRunningChangedEventArgs> DeviceRunningChanged
+        {
+            add
+            {
+                if (value == null)
+                {
+                    return;
+                }
+                lock (_audioDeviceRunningLock)
+                {
+                    if (_audioDeviceRunningChanged == null)
+                    {
+                        RegisterDeviceRunningChangedEvent();
+                    }
+                    _audioDeviceRunningChanged += value;
+                }
+            }
+            remove
+            {
+                if (value == null)
+                {
+                    return;
+                }
+                lock (_audioDeviceRunningLock)
+                {
+                    _audioDeviceRunningChanged -= value;
+                    if (_audioDeviceRunningChanged == null)
+                    {
+                        UnregisterDeviceRunningChangedEvent();
+                    }
+                }
+            }
+        }
+
+        private static void RegisterDeviceRunningChangedEvent()
+        {
+            _audioDeviceRunningChangedCallback = (device, isRunning, _) =>
+            {
+                _audioDeviceRunningChanged?.Invoke(null,
+                    new AudioDeviceRunningChangedEventArgs(new AudioDevice(device), isRunning));
+            };
+            Interop.AudioDevice.AddDeviceRunningChangedCallback(AudioDeviceOptions.All,
+               _audioDeviceRunningChangedCallback, IntPtr.Zero, out _deviceRunningChangedCallbackId).
+               ThrowIfError("Failed to add DeviceRunningChanged event");
+        }
+
+        private static void UnregisterDeviceRunningChangedEvent()
+        {
+            Interop.AudioDevice.RemoveDeviceRunningChangedCallback(_deviceRunningChangedCallbackId).
+                ThrowIfError("Failed to remove DeviceRunningChanged event");
+        }
+        #endregion
     }
 }
index 17f777f..f1045f1 100644 (file)
@@ -29,6 +29,10 @@ namespace Tizen.Multimedia
             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
             internal delegate void StateChangedCallback(IntPtr device, AudioDeviceState changedState, IntPtr userData);
 
+            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+            internal delegate void RunningChangedCallback(IntPtr device, bool isRunning, IntPtr userData);
+
+
             [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_get_device_list")]
             internal static extern AudioManagerError GetDeviceList(AudioDeviceOptions deviceMask, out IntPtr deviceList);
 
@@ -53,6 +57,9 @@ namespace Tizen.Multimedia
             [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_get_device_state_by_id")]
             internal static extern AudioManagerError GetDeviceState(int deviceId, out AudioDeviceState state);
 
+            [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_is_device_running")]
+            internal static extern AudioManagerError IsDeviceRunning(IntPtr device, out bool isRunning);
+
             [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_add_device_connection_changed_cb")]
             internal static extern AudioManagerError AddDeviceConnectionChangedCallback(
                 AudioDeviceOptions deviceMask, ConnectionChangedCallback callback, IntPtr userData, out int id);
@@ -66,6 +73,13 @@ namespace Tizen.Multimedia
 
             [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_remove_device_state_changed_cb")]
             internal static extern AudioManagerError RemoveDeviceStateChangedCallback(int id);
+
+            [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_add_device_running_changed_cb")]
+            internal static extern AudioManagerError AddDeviceRunningChangedCallback(AudioDeviceOptions deviceMask,
+                RunningChangedCallback callback, IntPtr userData, out int id);
+
+            [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_remove_device_running_changed_cb")]
+            internal static extern AudioManagerError RemoveDeviceRunningChangedCallback(int id);
         }
     }
 }