From cd36745d3a85fbde4ab2cfba2cf82c8fdde971ff Mon Sep 17 00:00:00 2001 From: coderhyme Date: Tue, 11 Apr 2017 15:25:59 +0900 Subject: [PATCH] [WavPlayer, TonePlayer]minor code and comment improvement. Change-Id: Ieca945c4c12e64c302393172e08632d135f92bc0 Signed-off-by: coderhyme --- .../Interop/Interop.TonePlayer.cs | 5 +- .../Interop/Interop.WavPlayer.cs | 8 +- .../Tizen.Multimedia.AudioIO.csproj | 2 +- .../TonePlayer/TonePlayer.cs | 127 +++++++++++++-------- ...onePlayerErrorFactory.cs => TonePlayerError.cs} | 37 ++---- .../TonePlayer/{TonePlayerEnums.cs => ToneType.cs} | 4 +- .../WavPlayer/WavPlayer.cs | 112 +++++++++++------- .../WavPlayer/WavPlayerError.cs | 57 +++++++++ .../WavPlayer/WavPlayerErrorFactory.cs | 94 --------------- 9 files changed, 229 insertions(+), 217 deletions(-) rename src/Tizen.Multimedia.AudioIO/TonePlayer/{TonePlayerErrorFactory.cs => TonePlayerError.cs} (54%) rename src/Tizen.Multimedia.AudioIO/TonePlayer/{TonePlayerEnums.cs => ToneType.cs} (99%) create mode 100644 src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayerError.cs delete mode 100644 src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayerErrorFactory.cs diff --git a/src/Tizen.Multimedia.AudioIO/Interop/Interop.TonePlayer.cs b/src/Tizen.Multimedia.AudioIO/Interop/Interop.TonePlayer.cs index 17f8f0a..076a703 100644 --- a/src/Tizen.Multimedia.AudioIO/Interop/Interop.TonePlayer.cs +++ b/src/Tizen.Multimedia.AudioIO/Interop/Interop.TonePlayer.cs @@ -23,10 +23,11 @@ internal static partial class Interop internal static partial class TonePlayer { [DllImport(Libraries.TonePlayer, EntryPoint = "tone_player_start_new")] - internal static extern int Start(ToneType tone, IntPtr streamInfoHandle, int durationMs, out int playerId); + internal static extern TonePlayerError Start(ToneType tone, IntPtr streamInfoHandle, + int durationMs, out int id); [DllImport(Libraries.TonePlayer, EntryPoint = "tone_player_stop")] - internal static extern int Stop(int PlayerId); + internal static extern TonePlayerError Stop(int id); } } diff --git a/src/Tizen.Multimedia.AudioIO/Interop/Interop.WavPlayer.cs b/src/Tizen.Multimedia.AudioIO/Interop/Interop.WavPlayer.cs index 933b1de..a7f7a6e 100644 --- a/src/Tizen.Multimedia.AudioIO/Interop/Interop.WavPlayer.cs +++ b/src/Tizen.Multimedia.AudioIO/Interop/Interop.WavPlayer.cs @@ -16,6 +16,7 @@ using System; using System.Runtime.InteropServices; +using Tizen.Multimedia; internal static partial class Interop { @@ -25,10 +26,11 @@ internal static partial class Interop internal delegate void WavPlayerCompletedCallback(int playerId, IntPtr userData); [DllImport(Libraries.WavPlayer, EntryPoint = "wav_player_start_new")] - internal static extern int WavPlayerStart(string filePath, IntPtr streamInfoHandle, WavPlayerCompletedCallback completedCallback, - IntPtr userData, out int playerId); + internal static extern WavPlayerError Start(string filePath, IntPtr streamInfoHandle, + WavPlayerCompletedCallback completedCallback, IntPtr userData, out int id); [DllImport(Libraries.WavPlayer, EntryPoint = "wav_player_stop")] - internal static extern int WavPlayerStop(int PlayerId); + internal static extern WavPlayerError Stop(int id); } } + diff --git a/src/Tizen.Multimedia.AudioIO/Tizen.Multimedia.AudioIO.csproj b/src/Tizen.Multimedia.AudioIO/Tizen.Multimedia.AudioIO.csproj index 79e0607..c2a17e2 100644 --- a/src/Tizen.Multimedia.AudioIO/Tizen.Multimedia.AudioIO.csproj +++ b/src/Tizen.Multimedia.AudioIO/Tizen.Multimedia.AudioIO.csproj @@ -3,7 +3,7 @@ - 1.0.0 + 1.0.1 Provides the Multimedia AudioIO API for Tizen .NET diff --git a/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayer.cs b/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayer.cs index 091ba9c..51c92b4 100644 --- a/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayer.cs +++ b/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayer.cs @@ -15,86 +15,121 @@ */ using System; -using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; namespace Tizen.Multimedia { - static internal class TonePlayerLog - { - internal const string LogTag = "Tizen.Multimedia.TonePlayer"; - } - /// - /// The TonePlayer class allows you to play and stop playing the tone. To play a particular - /// type of tone , - /// use . + /// Provides the ability to play a tone. /// public static class TonePlayer { /// /// Plays a tone, asynchronously. /// - /// The tone type to play. - /// The Audiostream policy object. - /// The tone duration in milliseconds. -1 indicates an infinite duration. + /// A to play. + /// A . + /// The tone duration in milliseconds. -1 indicates an infinite duration. + /// A task that represents the asynchronous operation. + /// is invalid. + /// is null. + /// is less than -1. + /// Any invalid operations occurred. + /// is not a supported type. + /// has already been disposed. + public static Task StartAsync(ToneType tone, AudioStreamPolicy streamPolicy, + int durationMilliseconds) + { + return StartAsync(tone, streamPolicy, durationMilliseconds, CancellationToken.None); + } + + /// + /// Plays a tone, asynchronously. + /// + /// A to play. + /// A . + /// The tone duration in milliseconds. -1 indicates an infinite duration. /// The cancellation token which can be used to stop playing the tone. - /// In case of invalid parameters - /// In case of null parameters - /// In case of play duration less than -1. - /// In case of any invalid operations - /// In case of tone type not supported. - public static async Task StartAsync(ToneType tone, AudioStreamPolicy streamPolicy, int durationMs, CancellationToken cancellationToken = default(CancellationToken)) + /// A task that represents the asynchronous operation. + /// is invalid. + /// is null. + /// is less than -1. + /// Any invalid operations occurred. + /// is not a supported type. + /// has already been disposed. + public static Task StartAsync(ToneType tone, AudioStreamPolicy streamPolicy, + int durationMilliseconds, CancellationToken cancellationToken) { - if (durationMs < -1) + if (durationMilliseconds < -1) { - throw new ArgumentOutOfRangeException(nameof(durationMs)); + throw new ArgumentOutOfRangeException(nameof(durationMilliseconds), durationMilliseconds, + $"{nameof(durationMilliseconds)} can't be less than -1."); } if (streamPolicy == null) { throw new ArgumentNullException(nameof(streamPolicy)); + } + + ValidationUtil.ValidateEnum(typeof(ToneType), tone, nameof(tone)); + if (cancellationToken.IsCancellationRequested) + { + return Task.FromCanceled(cancellationToken); } - if (Enum.IsDefined(typeof(ToneType), tone) == false) + return StartAsyncCore(tone, streamPolicy, durationMilliseconds, cancellationToken); + } + + private static async Task StartAsyncCore(ToneType tone, AudioStreamPolicy streamPolicy, + int durationMilliseconds, CancellationToken cancellationToken) + { + + var tcs = new TaskCompletionSource(); + + Interop.TonePlayer.Start(tone, streamPolicy.Handle, durationMilliseconds, out var id). + Validate("Failed to play tone."); + + using (RegisterCancellationAction(tcs, cancellationToken, id)) { - throw new ArgumentException("Invalid ToneType provided : " + tone); + await WaitForDuration(tcs, cancellationToken, durationMilliseconds); + + await tcs.Task; } + } - int id; - var task = new TaskCompletionSource(); - int ret = Interop.TonePlayer.Start(tone, streamPolicy.Handle, durationMs, out id); - if (ret != (int)TonePlayerError.None) + private static async Task WaitForDuration(TaskCompletionSource tcs, + CancellationToken cancellationToken, int durationMilliseconds) + { + if (durationMilliseconds == -1) { - Log.Error(TonePlayerLog.LogTag, "Error Occured with error code: " + (TonePlayerError)ret); - throw TonePlayerErrorFactory.CreateException(ret, "Failed to play tone."); + return; } - if (cancellationToken != CancellationToken.None) + try { - cancellationToken.Register((playerId) => - { - int resultCancel = Interop.TonePlayer.Stop((int)playerId); - if ((TonePlayerError)resultCancel != TonePlayerError.None) - { - Log.Error(TonePlayerLog.LogTag, "Failed to stop tone Player with error code: " + (TonePlayerError)resultCancel); - } - task.TrySetCanceled(); - }, id); + await Task.Delay(durationMilliseconds, cancellationToken); + tcs.TrySetResult(true); } + catch (TaskCanceledException) + { + } + } - if (durationMs != -1) + private static IDisposable RegisterCancellationAction(TaskCompletionSource tcs, + CancellationToken cancellationToken, int id) + { + if (cancellationToken.CanBeCanceled == false) { - Task delayTask = Task.Delay(durationMs, cancellationToken); - await delayTask; - if (delayTask.IsCompleted) - { - task.TrySetResult(id); - } + return null; } - await task.Task; + + return cancellationToken.Register(() => + { + Interop.TonePlayer.Stop(id).Validate("Failed to cancel"); + tcs.TrySetCanceled(); + }); } } } diff --git a/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayerErrorFactory.cs b/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayerError.cs similarity index 54% rename from src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayerErrorFactory.cs rename to src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayerError.cs index 8386558..e4fd0fa 100644 --- a/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayerErrorFactory.cs +++ b/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayerError.cs @@ -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,43 +28,30 @@ namespace Tizen.Multimedia TypeNotSupported = TizenErrorTonePlayer | 0x01 } - internal static class TonePlayerErrorFactory + internal static class TonePlayerErrorExtensions { - internal static Exception CreateException(int errorCode, string errorMessage) + internal static void Validate(this TonePlayerError error, string message) { - TonePlayerError err = (TonePlayerError)errorCode; - Exception exp; - if (string.IsNullOrEmpty(errorMessage)) + if (error == TonePlayerError.None) { - errorMessage = err.ToString(); + return; } - switch ((TonePlayerError)errorCode) + switch (error) { case TonePlayerError.InvalidParameter: - { - exp = new ArgumentException(errorMessage + "Invalid parameters provided"); - break; - } + throw new ArgumentException(message); case TonePlayerError.TypeNotSupported: - { - exp = new NotSupportedException(errorMessage + "Not Supported"); - break; - } + throw new NotSupportedException(message); case TonePlayerError.InvalidOperation: - { - exp = new InvalidOperationException(errorMessage + "Invalid Operation"); - break; - } + throw new InvalidOperationException(message); + default: - { - exp = new InvalidOperationException(errorMessage); - break; - } + throw new InvalidOperationException(message); + } - return exp; } } } diff --git a/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayerEnums.cs b/src/Tizen.Multimedia.AudioIO/TonePlayer/ToneType.cs similarity index 99% rename from src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayerEnums.cs rename to src/Tizen.Multimedia.AudioIO/TonePlayer/ToneType.cs index cc256bf..b96354e 100644 --- a/src/Tizen.Multimedia.AudioIO/TonePlayer/TonePlayerEnums.cs +++ b/src/Tizen.Multimedia.AudioIO/TonePlayer/ToneType.cs @@ -14,8 +14,6 @@ * limitations under the License. */ -using System; - namespace Tizen.Multimedia { /// @@ -72,7 +70,7 @@ namespace Tizen.Multimedia /// DtmfS, /// - /// Predefined DTMF sharP (#). + /// Predefined DTMF sharp (#). /// DtmfP, /// diff --git a/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayer.cs b/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayer.cs index ad27fce..954d6f3 100644 --- a/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayer.cs +++ b/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayer.cs @@ -15,43 +15,59 @@ */ using System; -using System.Runtime.InteropServices; +using System.IO; using System.Threading; using System.Threading.Tasks; namespace Tizen.Multimedia { - static internal class WavPlayerLog - { - internal const string LogTag = "Tizen.Multimedia.WavPlayer"; - } - /// - /// The WavPlayer class allows you to simply play and stop a wav file. To play a certain - /// wav file, call with - /// a path to the .wav file. + /// Provides the ability to play a wav file. /// public static class WavPlayer { /// - /// Plays a WAV file with the stream information of AudioManager, asynchronously. + /// Plays a wav file based on the specified . /// - /// The file path to play. - /// The Audiostream policy object. - /// The cancellation token which can be used to stop the Wav Player. - /// The WAV player ID. - /// In case of invalid parameters - /// In case of null parameters - /// In case of any invalid operations - /// In case of format not supported. - public static async Task StartAsync(string inputFilePath, AudioStreamPolicy streamPolicy, CancellationToken cancellationToken = default(CancellationToken)) + /// A task that represents the asynchronous operation. + /// A file path to play. + /// A . + /// + /// is null. + /// -or- + /// is null. + /// + /// An internal error occurs. + /// does not exists. + /// The format of is not supported. + /// has already been disposed of. + public static Task StartAsync(string path, AudioStreamPolicy streamPolicy) { - int id; - var task = new TaskCompletionSource(); + return StartAsync(path, streamPolicy, CancellationToken.None); + } - if (String.IsNullOrEmpty(inputFilePath)) + /// + /// Plays a wav file based on the specified . + /// + /// A task that represents the asynchronous operation. + /// A file path to play. + /// A . + /// A cancellation token which can be used to stop. + /// + /// is null. + /// -or- + /// is null. + /// + /// An internal error occurs. + /// does not exists. + /// The format of is not supported. + /// has already been disposed. + public static Task StartAsync(string path, AudioStreamPolicy streamPolicy, + CancellationToken cancellationToken) + { + if (path == null) { - throw new ArgumentNullException(nameof(inputFilePath)); + throw new ArgumentNullException(nameof(path)); } if (streamPolicy == null) @@ -59,34 +75,44 @@ namespace Tizen.Multimedia throw new ArgumentNullException(nameof(streamPolicy)); } - Interop.WavPlayer.WavPlayerCompletedCallback _playerCompletedCallback = (int playerId, IntPtr userData) => + if (File.Exists(path) == false) { - task.TrySetResult(playerId); - }; - GCHandle callbackHandle = GCHandle.Alloc(_playerCompletedCallback); + throw new FileNotFoundException("File does not exists.", path); + } - int ret = Interop.WavPlayer.WavPlayerStart(inputFilePath, streamPolicy.Handle, _playerCompletedCallback, IntPtr.Zero, out id); - if (ret != (int)WavPlayerError.None) + return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) : + StartAsyncCore(path, streamPolicy, cancellationToken); + } + + private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy, + CancellationToken cancellationToken) + { + var tcs = new TaskCompletionSource(); + + Interop.WavPlayer.WavPlayerCompletedCallback cb = (id_, _) => tcs.TrySetResult(true); + + Interop.WavPlayer.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out var id). + Validate("Failed to play."); + + using (RegisterCancellationAction(tcs, cancellationToken, id)) { - Log.Error(WavPlayerLog.LogTag, "Error Occured with error code: " + (WavPlayerError)ret); - task.TrySetException(WavPlayerErrorFactory.CreateException(ret, "Failed to play Wav file.")); + await tcs.Task; } + } - if (cancellationToken != CancellationToken.None) + private static IDisposable RegisterCancellationAction(TaskCompletionSource tcs, + CancellationToken cancellationToken, int id) + { + if (cancellationToken.CanBeCanceled == false) { - cancellationToken.Register((playerId) => - { - int resultCancel = Interop.WavPlayer.WavPlayerStop((int)playerId); - if ((WavPlayerError)resultCancel != WavPlayerError.None) - { - Log.Error(WavPlayerLog.LogTag, "Failed to stop Wav Player with error code: " + (WavPlayerError)resultCancel); - } - task.TrySetCanceled(); - }, id); + return null; } - await task.Task; - callbackHandle.Free(); + return cancellationToken.Register(() => + { + Interop.WavPlayer.Stop(id).Validate("Failed to cancel"); + tcs.TrySetCanceled(); + }); } } } diff --git a/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayerError.cs b/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayerError.cs new file mode 100644 index 0000000..cde5fd3 --- /dev/null +++ b/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayerError.cs @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016 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; +using Tizen.Internals.Errors; + +namespace Tizen.Multimedia +{ + internal enum WavPlayerError + { + None = ErrorCode.None, + InvalidParameter = ErrorCode.InvalidParameter, + InvalidOperation = ErrorCode.InvalidOperation, + TizenErrorWavPlayer = -0x01990000, + FormatNotSupported = TizenErrorWavPlayer | 0x01, + NotSupportedType = TizenErrorWavPlayer | 0x02 + } + + internal static class WavPlayerErrorExtensions + { + internal static void Validate(this WavPlayerError error, string message) + { + if (error == WavPlayerError.None) + { + return; + } + + switch (error) + { + case WavPlayerError.InvalidParameter: + throw new ArgumentException(message); + + case WavPlayerError.FormatNotSupported: + + case WavPlayerError.NotSupportedType: + throw new NotSupportedException(message); + + case WavPlayerError.InvalidOperation: + throw new InvalidOperationException(message); + } + } + } + +} diff --git a/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayerErrorFactory.cs b/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayerErrorFactory.cs deleted file mode 100644 index 435a127..0000000 --- a/src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayerErrorFactory.cs +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (c) 2016 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; -using Tizen.Internals.Errors; - -namespace Tizen.Multimedia -{ - internal enum WavPlayerError - { - None = ErrorCode.None, - InvalidParameter = ErrorCode.InvalidParameter, - InvalidOperation = ErrorCode.InvalidOperation, - TizenErrorWavPlayer = -0x01990000, - FormatNotSupported = TizenErrorWavPlayer | 0x01, - NotSupportedType = TizenErrorWavPlayer | 0x02 - } - - internal static class WavPlayerErrorFactory - { - internal static void ThrowException(int errorCode, string errorMessage = null, string paramName = null) - { - WavPlayerError err = (WavPlayerError)errorCode; - if (string.IsNullOrEmpty(errorMessage)) - { - errorMessage = err.ToString(); - } - - switch ((WavPlayerError)errorCode) - { - case WavPlayerError.InvalidParameter: - throw new ArgumentException(errorMessage, paramName); - - case WavPlayerError.FormatNotSupported: - case WavPlayerError.NotSupportedType: - throw new NotSupportedException(errorMessage); - - case WavPlayerError.InvalidOperation: - throw new InvalidOperationException(errorMessage); - } - } - - internal static Exception CreateException(int errorCode, string errorMessage) - { - WavPlayerError err = (WavPlayerError)errorCode; - Exception exp; - if (string.IsNullOrEmpty(errorMessage)) - { - errorMessage = err.ToString(); - } - - switch ((WavPlayerError)errorCode) - { - case WavPlayerError.InvalidParameter: - { - exp = new ArgumentException(errorMessage + "Invalid parameters provided"); - break; - } - - case WavPlayerError.FormatNotSupported: - case WavPlayerError.NotSupportedType: - { - exp = new NotSupportedException(errorMessage + "Not Supported"); - break; - } - - case WavPlayerError.InvalidOperation: - { - exp = new InvalidOperationException(errorMessage + "Invalid Operation"); - break; - } - default: - { - exp = new InvalidOperationException(errorMessage); - break; - } - } - return exp; - } - } -} -- 2.7.4