X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2FTizen.Multimedia.Radio%2FRadio%2FRadio.cs;h=a21450aadec6a4a9d146018e251f923ed6ea48e2;hb=refs%2Ftags%2Fsubmit%2Ftizen%2F20171012.080815;hp=20d70fd8acb8a48f4c43bd8af18a91e64bda326d;hpb=adaf7f3ec967be836ca58ce5ec7b0111160ee393;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git diff --git a/src/Tizen.Multimedia.Radio/Radio/Radio.cs b/src/Tizen.Multimedia.Radio/Radio/Radio.cs index 20d70fd..a21450a 100755 --- a/src/Tizen.Multimedia.Radio/Radio/Radio.cs +++ b/src/Tizen.Multimedia.Radio/Radio/Radio.cs @@ -16,6 +16,7 @@ using System; using System.Linq; +using System.Runtime.InteropServices; using System.Threading.Tasks; using Tizen.System; using static Tizen.Multimedia.Interop.Radio; @@ -43,8 +44,13 @@ namespace Tizen.Multimedia try { - SetScanCompletedCb(_handle, ScanCompleteCallback).ThrowIfFailed("Failed to initialize radio"); - SetInterruptedCb(_handle, InterruptedCallback).ThrowIfFailed("Failed to initialize radio"); + _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); + + SetScanCompletedCb(_handle, _scanCompletedCallback).ThrowIfFailed("Failed to initialize radio"); + SetInterruptedCb(_handle, _interruptedCallback).ThrowIfFailed("Failed to initialize radio"); } catch (Exception) { @@ -65,6 +71,14 @@ namespace Tizen.Multimedia } } + private ScanUpdatedCallback _scanUpdatedCallback; + + private ScanStoppedCallback _scanStoppedCallback; + + private ScanCompletedCallback _scanCompletedCallback; + + private InterruptedCallback _interruptedCallback; + /// /// Occurs when the radio scanning information is updated. /// @@ -252,7 +266,7 @@ namespace Tizen.Multimedia { ValidateRadioState(RadioState.Ready, RadioState.Playing); - ScanStart(Handle, ScanUpdatedCallback); + ScanStart(Handle, _scanUpdatedCallback).ThrowIfFailed("Failed to start scanning"); } /// @@ -265,7 +279,7 @@ namespace Tizen.Multimedia { ValidateRadioState(RadioState.Scanning); - ScanStop(Handle, ScanStoppedCallback); + ScanStop(Handle, _scanStoppedCallback).ThrowIfFailed("Failed to stop scanning"); } /// @@ -277,19 +291,14 @@ namespace Tizen.Multimedia /// It can be -1 if the seeking operation has failed. /// /// The radio must be in the state. - /// The radio is not in the valid state. - public async Task SeekUpAsync() + /// + /// The radio is not in the valid state.\n + /// -or-\n + /// Seeking is in progress. + /// + public Task SeekUpAsync() { - ValidateRadioState(RadioState.Playing); - - TaskCompletionSource tcs = new TaskCompletionSource(); - SeekCompletedCallback callback = (currentFrequency, _) => - { - tcs.TrySetResult(currentFrequency); - }; - - SeekUp(Handle, callback); - return await tcs.Task; + return SeekAsync(SeekUp); } /// @@ -301,53 +310,46 @@ namespace Tizen.Multimedia /// It can be -1 if the seeking operation has failed. /// /// The radio must be in the state. - /// The radio is not in the valid state. - public async Task SeekDownAsync() + /// + /// The radio is not in the valid state.\n + /// -or-\n + /// Seeking is in progress. + /// + public Task SeekDownAsync() + { + return SeekAsync(SeekDown); + } + + private async Task SeekAsync(Func func) { ValidateRadioState(RadioState.Playing); - TaskCompletionSource tcs = new TaskCompletionSource(); - SeekCompletedCallback callback = (currentFrequency, _) => + var tcs = new TaskCompletionSource(); + SeekCompletedCallback callback = (currentFrequency, _) => tcs.TrySetResult(currentFrequency); + + GCHandle gcHandle; + try { - tcs.TrySetResult(currentFrequency); - }; + gcHandle = GCHandle.Alloc(callback); - SeekDown(Handle, callback); - return await tcs.Task; + func(Handle, callback, IntPtr.Zero).ThrowIfFailed("Failed to seek"); + return await tcs.Task; + } + finally + { + gcHandle.Free(); + } } private void ValidateFeatureSupported(string featurePath) { - bool supported = false; - Information.TryGetValue(featurePath, out supported); - - if (supported == false) + if (Information.TryGetValue(featurePath, out bool supported) == false || supported == false) { throw new NotSupportedException($"The feature({featurePath}) is not supported."); } } - private void ScanUpdatedCallback(int frequency, IntPtr data) - { - ScanUpdated?.Invoke(this, new ScanUpdatedEventArgs(frequency)); - } - - private void ScanStoppedCallback(IntPtr data) - { - ScanStopped?.Invoke(this, EventArgs.Empty); - } - - private void ScanCompleteCallback(IntPtr data) - { - ScanCompleted?.Invoke(this, EventArgs.Empty); - } - - private void InterruptedCallback(RadioInterruptedReason reason, IntPtr data) - { - Interrupted?.Invoke(this, new RadioInterruptedEventArgs(reason)); - } - private void ValidateRadioState(params RadioState[] required) { RadioState curState = State;