[AudioManager] Add properties for the preferred device (#987)
authorSangchul Lee <sc11.lee@samsung.com>
Tue, 3 Sep 2019 03:10:20 +0000 (12:10 +0900)
committerGitHub <noreply@github.com>
Tue, 3 Sep 2019 03:10:20 +0000 (12:10 +0900)
* [AudioManager] Add properties for the preferred device to AudioStreamPolicy class

Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
* fixup! [AudioManager] Add properties for the preferred device to AudioStreamPolicy class

* fixup! [AudioManager] Add properties for the preferred device to AudioStreamPolicy class

* [AudioManager] Use null-conditional operator and ?? operator

Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
* [AudioManager] Remove codes checking device id of preferred device handle

Native API is now changed to return error if there's a difference of preferred
deivce id between server and stream info. handle.

Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
* fixup! [AudioManager] Remove codes checking device id of preferred device handle

Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
src/Tizen.Multimedia/AudioManager/AudioStreamPolicy.cs
src/Tizen.Multimedia/Interop/Interop.StreamPolicy.cs

index 0309e92..5a50f73 100644 (file)
@@ -28,6 +28,9 @@ namespace Tizen.Multimedia
         private AudioStreamPolicyHandle _handle;
         private bool _disposed = false;
         private Interop.AudioStreamPolicy.FocusStateChangedCallback _focusStateChangedCallback;
+        private static AudioDevice _inputDevice = null;
+        private static AudioDevice _outputDevice = null;
+        private const string Tag = "Tizen.Multimedia.AudioStreamPolicy";
 
         /// <summary>
         /// Initializes a new instance of the <see cref="AudioStreamPolicy"/> class with <see cref="AudioStreamType"/>.
@@ -305,6 +308,84 @@ namespace Tizen.Multimedia
         }
 
         /// <summary>
+        /// Gets or sets the preferred input device.
+        /// </summary>
+        /// <value>
+        /// The <see cref="AudioDevice"/> instance.<br/>
+        /// The default is null which means any device is not set on this property.
+        /// </value>
+        /// <remarks>
+        /// This property is to set a specific built-in device when the system has multiple devices of the same built-in device type.
+        /// When there's only one device for a built-in device type in the system, nothing will happen even if this property is set successfully.
+        /// </remarks>
+        /// <exception cref="ArgumentException">A device is not for input.</exception>
+        /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
+        /// <exception cref="AudioPolicyException">A device is not supported by this <see cref="AudioStreamPolicy"/> instance.</exception>
+        /// <exception cref="ObjectDisposedException">The <see cref="AudioStreamPolicy"/> has already been disposed of.</exception>
+        /// <seealso cref="AudioManager.GetConnectedDevices()"/>
+        /// <since_tizen> 6 </since_tizen>
+        public AudioDevice PreferredInputDevice
+        {
+            get
+            {
+                /* This P/Invoke intends to validate if the core audio system
+                 * is normal. Otherwise, it'll throw an error here. */
+                Interop.AudioStreamPolicy.GetPreferredDevice(Handle, out var inDeviceId, out _).
+                    ThrowIfError("Failed to get preferred input device");
+
+                Log.Debug(Tag, $"preferred input device id:{inDeviceId}");
+
+                return _inputDevice;
+            }
+            set
+            {
+                Interop.AudioStreamPolicy.SetPreferredDevice(Handle, AudioDeviceIoDirection.Input, value?.Id ?? 0).
+                    ThrowIfError("Failed to set preferred input device");
+
+                _inputDevice = value;
+            }
+        }
+
+        /// <summary>
+        /// Gets or sets the preferred output device.
+        /// </summary>
+        /// <value>
+        /// The <see cref="AudioDevice"/> instance.<br/>
+        /// The default is null which means any device is not set on this property.
+        /// </value>
+        /// <remarks>
+        /// This property is to set a specific built-in device when the system has multiple devices of the same built-in device type.
+        /// When there's only one device for a built-in device type in the system, nothing will happen even if this property is set successfully.
+        /// </remarks>
+        /// <exception cref="ArgumentException">A device is not for output.</exception>
+        /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
+        /// <exception cref="AudioPolicyException">A device is not supported by this <see cref="AudioStreamPolicy"/> instance.</exception>
+        /// <exception cref="ObjectDisposedException">The <see cref="AudioStreamPolicy"/> has already been disposed of.</exception>
+        /// <seealso cref="AudioManager.GetConnectedDevices()"/>
+        /// <since_tizen> 6 </since_tizen>
+        public AudioDevice PreferredOutputDevice
+        {
+            get
+            {
+                /* This P/Invoke intends to validate if the core audio system
+                 * is normal. Otherwise, it'll throw an error here. */
+                Interop.AudioStreamPolicy.GetPreferredDevice(Handle, out _, out var outDeviceId).
+                    ThrowIfError("Failed to get preferred output device");
+
+                Log.Debug(Tag, $"preferred output device id:{outDeviceId}");
+
+                return _outputDevice;
+            }
+            set
+            {
+                Interop.AudioStreamPolicy.SetPreferredDevice(Handle, AudioDeviceIoDirection.Output, value?.Id ?? 0).
+                    ThrowIfError("Failed to set preferred output device");
+
+                _outputDevice = value;
+            }
+        }
+
+        /// <summary>
         /// Checks if any stream from the current AudioStreamPolicy is using the device.
         /// </summary>
         /// <returns>true if any audio stream from the current AudioStreamPolicy is using the device; otherwise, false.</returns>
index 5c0dc0a..ede3562 100644 (file)
@@ -51,6 +51,12 @@ namespace Tizen.Multimedia
             [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_apply_stream_routing")]
             internal static extern AudioManagerError ApplyStreamRouting(AudioStreamPolicyHandle streamInfo);
 
+            [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_set_stream_preferred_device_id")]
+            internal static extern AudioManagerError SetPreferredDevice(AudioStreamPolicyHandle streamInfo, AudioDeviceIoDirection direction, int deviceId);
+
+            [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_get_stream_preferred_device")]
+            internal static extern AudioManagerError GetPreferredDevice(AudioStreamPolicyHandle streamInfo, out int inDeviceId, out int outDeviceId);
+
             [DllImport(Libraries.SoundManager, EntryPoint = "sound_manager_acquire_focus")]
             internal static extern AudioManagerError AcquireFocus(AudioStreamPolicyHandle streamInfo,
                 AudioStreamFocusOptions focusMask, AudioStreamBehaviors audioStreamBehavior, string extraInfo);