[Multimedia] Modified the Radio to disable sound session compatibility. 53/155153/1
authorcoderhyme <jhyo.kim@samsung.com>
Thu, 12 Oct 2017 03:02:12 +0000 (12:02 +0900)
committercoderhyme <jhyo.kim@samsung.com>
Thu, 12 Oct 2017 07:27:57 +0000 (16:27 +0900)
Change-Id: I73d3e4bcc42868a2bebf3ff906df8827fb9e8c9a
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
src/Tizen.Multimedia.Radio/Interop/Interop.Libraries.cs
src/Tizen.Multimedia.Radio/Interop/Interop.Radio.cs
src/Tizen.Multimedia.Radio/Radio/Radio.cs

index f237099..060623e 100644 (file)
  * limitations under the License.
  */
 
-namespace Tizen.Multimedia
+internal static partial class Interop
 {
-    internal static partial class Interop
+    internal static partial class Libraries
     {
-        internal static partial class Libraries
-        {
-            public const string Radio = "libcapi-media-radio.so.0";
-        }
+        public const string Radio = "libcapi-media-radio.so.0";
     }
 }
index 929324b..7dfc05d 100644 (file)
 
 using System;
 using System.Runtime.InteropServices;
+using Tizen.Multimedia;
 
-namespace Tizen.Multimedia
+internal static partial class Interop
 {
-    internal static partial class Interop
+    internal static class Radio
     {
-        internal static class Radio
-        {
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void SeekCompletedCallback(int frequency, IntPtr userData);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void SeekCompletedCallback(int frequency, IntPtr userData);
 
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void ScanUpdatedCallback(int frequency, IntPtr userData);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void ScanUpdatedCallback(int frequency, IntPtr userData);
 
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void ScanStoppedCallback(IntPtr userData);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void ScanStoppedCallback(IntPtr userData);
 
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void ScanCompletedCallback(IntPtr userData);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void ScanCompletedCallback(IntPtr userData);
 
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void InterruptedCallback(RadioInterruptedReason reason, IntPtr userData);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void InterruptedCallback(RadioInterruptedReason reason, IntPtr userData);
 
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_create")]
-            internal static extern RadioError Create(out RadioHandle radio);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_create")]
+        internal static extern RadioError Create(out RadioHandle radio);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_destroy")]
-            internal static extern RadioError Destroy(IntPtr radio);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_destroy")]
+        internal static extern RadioError Destroy(IntPtr radio);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_get_state")]
-            internal static extern RadioError GetState(RadioHandle radio, out RadioState state);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_get_state")]
+        internal static extern RadioError GetState(RadioHandle radio, out RadioState state);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_start")]
-            internal static extern RadioError Start(RadioHandle radio);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_start")]
+        internal static extern RadioError Start(RadioHandle radio);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_stop")]
-            internal static extern RadioError Stop(RadioHandle radio);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_stop")]
+        internal static extern RadioError Stop(RadioHandle radio);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_seek_up")]
-            internal static extern RadioError SeekUp(RadioHandle radio, SeekCompletedCallback callback,
-                IntPtr userData = default(IntPtr));
+        [DllImport(Libraries.Radio, EntryPoint = "radio_seek_up")]
+        internal static extern RadioError SeekUp(RadioHandle radio, SeekCompletedCallback callback,
+            IntPtr userData = default(IntPtr));
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_seek_down")]
-            internal static extern RadioError SeekDown(RadioHandle radio, SeekCompletedCallback callback,
-                IntPtr userData = default(IntPtr));
+        [DllImport(Libraries.Radio, EntryPoint = "radio_seek_down")]
+        internal static extern RadioError SeekDown(RadioHandle radio, SeekCompletedCallback callback,
+            IntPtr userData = default(IntPtr));
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_set_frequency")]
-            internal static extern RadioError SetFrequency(RadioHandle radio, int frequency);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_set_frequency")]
+        internal static extern RadioError SetFrequency(RadioHandle radio, int frequency);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_get_frequency")]
-            internal static extern RadioError GetFrequency(RadioHandle radio, out int frequency);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_get_frequency")]
+        internal static extern RadioError GetFrequency(RadioHandle radio, out int frequency);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_get_signal_strength")]
-            internal static extern RadioError GetSignalStrength(RadioHandle radio, out int strength);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_get_signal_strength")]
+        internal static extern RadioError GetSignalStrength(RadioHandle radio, out int strength);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_scan_start")]
-            internal static extern RadioError ScanStart(RadioHandle radio, ScanUpdatedCallback callback,
-                IntPtr userData = default(IntPtr));
+        [DllImport(Libraries.Radio, EntryPoint = "radio_scan_start")]
+        internal static extern RadioError ScanStart(RadioHandle radio, ScanUpdatedCallback callback,
+            IntPtr userData = default(IntPtr));
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_scan_stop")]
-            internal static extern RadioError ScanStop(RadioHandle radio, ScanStoppedCallback callback,
-                IntPtr userData = default(IntPtr));
+        [DllImport(Libraries.Radio, EntryPoint = "radio_scan_stop")]
+        internal static extern RadioError ScanStop(RadioHandle radio, ScanStoppedCallback callback,
+            IntPtr userData = default(IntPtr));
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_set_mute")]
-            internal static extern RadioError SetMute(RadioHandle radio, bool muted);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_set_mute")]
+        internal static extern RadioError SetMute(RadioHandle radio, bool muted);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_is_muted")]
-            internal static extern RadioError GetMuted(RadioHandle radio, out bool muted);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_is_muted")]
+        internal static extern RadioError GetMuted(RadioHandle radio, out bool muted);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_set_scan_completed_cb")]
-            internal static extern RadioError SetScanCompletedCb(RadioHandle radio,
-                ScanCompletedCallback callback, IntPtr userData = default(IntPtr));
+        [DllImport(Libraries.Radio, EntryPoint = "radio_set_scan_completed_cb")]
+        internal static extern RadioError SetScanCompletedCb(RadioHandle radio,
+            ScanCompletedCallback callback, IntPtr userData = default(IntPtr));
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_unset_scan_completed_cb")]
-            internal static extern RadioError UnsetScanCompletedCb(RadioHandle radio);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_unset_scan_completed_cb")]
+        internal static extern RadioError UnsetScanCompletedCb(RadioHandle radio);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_set_interrupted_cb")]
-            internal static extern RadioError SetInterruptedCb(RadioHandle radio,
-                InterruptedCallback callback, IntPtr userData = default(IntPtr));
+        [DllImport(Libraries.Radio, EntryPoint = "radio_set_interrupted_cb")]
+        internal static extern RadioError SetInterruptedCb(RadioHandle radio,
+            InterruptedCallback callback, IntPtr userData = default(IntPtr));
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_unset_interrupted_cb")]
-            internal static extern RadioError UnsetInterruptedCb(RadioHandle radio);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_unset_interrupted_cb")]
+        internal static extern RadioError UnsetInterruptedCb(RadioHandle radio);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_get_frequency_range")]
-            internal static extern RadioError GetFrequencyRange(RadioHandle radio, out int minFreq, out int maxFreq);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_get_frequency_range")]
+        internal static extern RadioError GetFrequencyRange(RadioHandle radio, out int minFreq, out int maxFreq);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_get_channel_spacing")]
-            internal static extern RadioError GetChannelSpacing(RadioHandle radio, out int channelSpacing);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_get_channel_spacing")]
+        internal static extern RadioError GetChannelSpacing(RadioHandle radio, out int channelSpacing);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_set_volume")]
-            internal static extern RadioError SetVolume(RadioHandle radio, float volume);
+        [DllImport(Libraries.Radio, EntryPoint = "radio_set_volume")]
+        internal static extern RadioError SetVolume(RadioHandle radio, float volume);
 
-            [DllImport(Libraries.Radio, EntryPoint = "radio_get_volume")]
-            internal static extern RadioError GetVolume(RadioHandle radio, out float volume);
-        }
+        [DllImport(Libraries.Radio, EntryPoint = "radio_get_volume")]
+        internal static extern RadioError GetVolume(RadioHandle radio, out float volume);
+    }
 
-        internal class RadioHandle : SafeHandle
+    internal class RadioHandle : SafeHandle
+    {
+        protected RadioHandle() : base(IntPtr.Zero, true)
         {
-            protected RadioHandle() : base(IntPtr.Zero, true)
-            {
-            }
+        }
 
-            public override bool IsInvalid => handle == IntPtr.Zero;
+        public override bool IsInvalid => handle == IntPtr.Zero;
 
-            protected override bool ReleaseHandle()
+        protected override bool ReleaseHandle()
+        {
+            var ret = Radio.Destroy(handle);
+            if (ret != RadioError.None)
             {
-                var ret = Radio.Destroy(handle);
-                if (ret != RadioError.None)
-                {
-                    Log.Debug(GetType().FullName, $"Failed to release native handle.");
-                    return false;
-                }
-
-                return true;
+                Tizen.Log.Debug(GetType().FullName, $"Failed to release native handle.");
+                return false;
             }
+
+            return true;
         }
     }
 }
index a21450a..b2f02f6 100755 (executable)
 
 using System;
 using System.Linq;
-using System.Runtime.InteropServices;
 using System.Threading.Tasks;
 using Tizen.System;
-using static Tizen.Multimedia.Interop.Radio;
+using Native = Interop.Radio;
+using static Interop;
 
 namespace Tizen.Multimedia
 {
@@ -28,7 +28,7 @@ namespace Tizen.Multimedia
     /// </summary>
     public class Radio : IDisposable
     {
-        private Interop.RadioHandle _handle;
+        private RadioHandle _handle;
 
         private const string FeatureFmRadio = "http://tizen.org/feature/fmradio";
 
@@ -38,19 +38,20 @@ namespace Tizen.Multimedia
         /// <exception cref="NotSupportedException">The radio feature is not supported.</exception>
         public Radio()
         {
+            CompatibilitySupport.DisableSessionBackwardCompatibility();
+
             ValidateFeatureSupported(FeatureFmRadio);
 
-            Create(out _handle);
+            Native.Create(out _handle);
 
             try
             {
-                _scanCompletedCallback = _ => ScanCompleted?.Invoke(this, EventArgs.Empty);
-                _interruptedCallback = (reason, _) => Interrupted?.Invoke(this, new RadioInterruptedEventArgs(reason));
-                _scanUpdatedCallback = (frequency, _) => ScanUpdated?.Invoke(this, new ScanUpdatedEventArgs(frequency));
-                _scanStoppedCallback = _ => ScanStopped?.Invoke(this, EventArgs.Empty);
+                InitCallbacks();
 
-                SetScanCompletedCb(_handle, _scanCompletedCallback).ThrowIfFailed("Failed to initialize radio");
-                SetInterruptedCb(_handle, _interruptedCallback).ThrowIfFailed("Failed to initialize radio");
+                Native.SetScanCompletedCb(_handle, _scanCompletedCallback).
+                    ThrowIfFailed("Failed to initialize radio");
+                Native.SetInterruptedCb(_handle, _interruptedCallback).
+                    ThrowIfFailed("Failed to initialize radio");
             }
             catch (Exception)
             {
@@ -59,7 +60,20 @@ namespace Tizen.Multimedia
             }
         }
 
-        private Interop.RadioHandle Handle
+        private void InitCallbacks()
+        {
+            _scanCompletedCallback = _ => ScanCompleted?.Invoke(this, EventArgs.Empty);
+
+            _interruptedCallback =
+                (reason, _) => Interrupted?.Invoke(this, new RadioInterruptedEventArgs(reason));
+
+            _scanUpdatedCallback =
+                (frequency, _) => ScanUpdated?.Invoke(this, new ScanUpdatedEventArgs(frequency));
+
+            _scanStoppedCallback = _ => ScanStopped?.Invoke(this, EventArgs.Empty);
+        }
+
+        private RadioHandle Handle
         {
             get
             {
@@ -71,13 +85,13 @@ namespace Tizen.Multimedia
             }
         }
 
-        private ScanUpdatedCallback _scanUpdatedCallback;
+        private Native.ScanUpdatedCallback _scanUpdatedCallback;
 
-        private ScanStoppedCallback _scanStoppedCallback;
+        private Native.ScanStoppedCallback _scanStoppedCallback;
 
-        private ScanCompletedCallback _scanCompletedCallback;
+        private Native.ScanCompletedCallback _scanCompletedCallback;
 
-        private InterruptedCallback _interruptedCallback;
+        private Native.InterruptedCallback _interruptedCallback;
 
         /// <summary>
         /// Occurs when the radio scanning information is updated.
@@ -106,8 +120,7 @@ namespace Tizen.Multimedia
         {
             get
             {
-                RadioState state;
-                GetState(Handle, out state);
+                Native.GetState(Handle, out var state).ThrowIfFailed("Failed to get state");
                 return state;
             }
         }
@@ -124,18 +137,18 @@ namespace Tizen.Multimedia
         {
             get
             {
-                int value = 0;
-                GetFrequency(Handle, out value).ThrowIfFailed("Failed to get frequency");
+                Native.GetFrequency(Handle, out var value).ThrowIfFailed("Failed to get frequency");
                 return value;
             }
             set
             {
                 if (value < FrequencyRange.Min || value > FrequencyRange.Max)
                 {
-                    throw new ArgumentOutOfRangeException(nameof(Frequency), value, "Frequency must be within FrequencyRange.");
+                    throw new ArgumentOutOfRangeException(nameof(Frequency), value,
+                        "Frequency must be within FrequencyRange.");
                 }
 
-                SetFrequency(Handle, value).ThrowIfFailed("Failed to set frequency");
+                Native.SetFrequency(Handle, value).ThrowIfFailed("Failed to set frequency");
             }
         }
 
@@ -147,7 +160,7 @@ namespace Tizen.Multimedia
             get
             {
                 int value = 0;
-                GetSignalStrength(Handle, out value).ThrowIfFailed("Failed to get signal strength");
+                Native.GetSignalStrength(Handle, out value).ThrowIfFailed("Failed to get signal strength");
                 return value;
             }
         }
@@ -163,13 +176,12 @@ namespace Tizen.Multimedia
         {
             get
             {
-                bool value;
-                GetMuted(Handle, out value).ThrowIfFailed("Failed to get the mute state");
+                Native.GetMuted(Handle, out var value).ThrowIfFailed("Failed to get the mute state");
                 return value;
             }
             set
             {
-                SetMute(Handle, value).ThrowIfFailed("Failed to set the mute state");
+                Native.SetMute(Handle, value).ThrowIfFailed("Failed to set the mute state");
             }
         }
 
@@ -180,8 +192,8 @@ namespace Tizen.Multimedia
         {
             get
             {
-                int value;
-                GetChannelSpacing(Handle, out value).ThrowIfFailed("Failed to get channel spacing");
+                Native.GetChannelSpacing(Handle, out var value).
+                    ThrowIfFailed("Failed to get channel spacing");
                 return value;
             }
         }
@@ -200,8 +212,7 @@ namespace Tizen.Multimedia
         {
             get
             {
-                float value;
-                GetVolume(Handle, out value).ThrowIfFailed("Failed to get volume level.");
+                Native.GetVolume(Handle, out var value).ThrowIfFailed("Failed to get volume level.");
                 return value;
             }
             set
@@ -212,7 +223,7 @@ namespace Tizen.Multimedia
                         $"Valid volume range is 0 <= value <= 1.0, but got { value }.");
                 }
 
-                SetVolume(Handle, value).ThrowIfFailed("Failed to set volume level");
+                Native.SetVolume(Handle, value).ThrowIfFailed("Failed to set volume level");
             }
         }
 
@@ -223,9 +234,8 @@ namespace Tizen.Multimedia
         {
             get
             {
-                int min, max;
-
-                GetFrequencyRange(Handle, out min, out max).ThrowIfFailed("Failed to get frequency range");
+                Native.GetFrequencyRange(Handle, out var min, out var max).
+                    ThrowIfFailed("Failed to get frequency range");
 
                 return new Range(min, max);
             }
@@ -240,7 +250,7 @@ namespace Tizen.Multimedia
         {
             ValidateRadioState(RadioState.Ready);
 
-            Interop.Radio.Start(Handle).ThrowIfFailed("Failed to start radio");
+            Native.Start(Handle).ThrowIfFailed("Failed to start radio");
         }
 
         /// <summary>
@@ -252,7 +262,7 @@ namespace Tizen.Multimedia
         {
             ValidateRadioState(RadioState.Playing);
 
-            Interop.Radio.Stop(Handle).ThrowIfFailed("Failed to stop radio");
+            Native.Stop(Handle).ThrowIfFailed("Failed to stop radio");
         }
 
         /// <summary>
@@ -266,7 +276,7 @@ namespace Tizen.Multimedia
         {
             ValidateRadioState(RadioState.Ready, RadioState.Playing);
 
-            ScanStart(Handle, _scanUpdatedCallback).ThrowIfFailed("Failed to start scanning");
+            Native.ScanStart(Handle, _scanUpdatedCallback).ThrowIfFailed("Failed to start scanning");
         }
 
         /// <summary>
@@ -279,7 +289,7 @@ namespace Tizen.Multimedia
         {
             ValidateRadioState(RadioState.Scanning);
 
-            ScanStop(Handle, _scanStoppedCallback).ThrowIfFailed("Failed to stop scanning");
+            Native.ScanStop(Handle, _scanStoppedCallback).ThrowIfFailed("Failed to stop scanning");
         }
 
         /// <summary>
@@ -298,7 +308,7 @@ namespace Tizen.Multimedia
         /// </exception>
         public Task<int> SeekUpAsync()
         {
-            return SeekAsync(SeekUp);
+            return SeekAsync(Native.SeekUp);
         }
 
         /// <summary>
@@ -317,28 +327,23 @@ namespace Tizen.Multimedia
         /// </exception>
         public Task<int> SeekDownAsync()
         {
-            return SeekAsync(SeekDown);
+            return SeekAsync(Native.SeekDown);
         }
 
-        private async Task<int> SeekAsync(Func<Interop.RadioHandle, SeekCompletedCallback, IntPtr, RadioError> func)
+        private async Task<int> SeekAsync(
+            Func<RadioHandle, Native.SeekCompletedCallback, IntPtr, RadioError> func)
         {
             ValidateRadioState(RadioState.Playing);
 
             var tcs = new TaskCompletionSource<int>();
-            SeekCompletedCallback callback = (currentFrequency, _) => tcs.TrySetResult(currentFrequency);
+            Native.SeekCompletedCallback callback =
+                (currentFrequency, _) => tcs.TrySetResult(currentFrequency);
 
-            GCHandle gcHandle;
-            try
+            using (ObjectKeeper.Get(callback))
             {
-                gcHandle = GCHandle.Alloc(callback);
-
                 func(Handle, callback, IntPtr.Zero).ThrowIfFailed("Failed to seek");
                 return await tcs.Task;
             }
-            finally
-            {
-                gcHandle.Free();
-            }
         }
 
         private void ValidateFeatureSupported(string featurePath)
@@ -347,7 +352,6 @@ namespace Tizen.Multimedia
             {
                 throw new NotSupportedException($"The feature({featurePath}) is not supported.");
             }
-
         }
 
         private void ValidateRadioState(params RadioState[] required)