[MM-WavPlayer] Add base code for WavPlayer.
authorVivek Ellur <vivek.ellur@samsung.com>
Fri, 27 Jan 2017 10:12:50 +0000 (15:42 +0530)
committerVivek Ellur <vivek.ellur@samsung.com>
Wed, 22 Feb 2017 11:32:28 +0000 (17:02 +0530)
Change-Id: I8d4bfafb22d493d5427b15e2ed1465e8782be8f3
Signed-off-by: Vivek Ellur <vivek.ellur@samsung.com>
src/Tizen.Multimedia/Interop/Interop.WavPlayer.cs [new file with mode: 0755]
src/Tizen.Multimedia/WavPlayer/WavPlayer.cs [new file with mode: 0755]
src/Tizen.Multimedia/WavPlayer/WavPlayerErrorFactory.cs [new file with mode: 0755]

diff --git a/src/Tizen.Multimedia/Interop/Interop.WavPlayer.cs b/src/Tizen.Multimedia/Interop/Interop.WavPlayer.cs
new file mode 100755 (executable)
index 0000000..36aac3c
--- /dev/null
@@ -0,0 +1,18 @@
+using System;
+using System.Runtime.InteropServices;
+
+internal static partial class Interop
+{
+    internal static partial class WavPlayer
+    {
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        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);
+
+        [DllImport(Libraries.WavPlayer, EntryPoint = "wav_player_stop")]
+        internal static extern int WavPlayerStop(int PlayerId);
+    }
+}
diff --git a/src/Tizen.Multimedia/WavPlayer/WavPlayer.cs b/src/Tizen.Multimedia/WavPlayer/WavPlayer.cs
new file mode 100755 (executable)
index 0000000..524bc7f
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * 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 System.Runtime.InteropServices;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Tizen.Multimedia
+{
+    static internal class WavPlayerLog
+    {
+        internal const string LogTag = "Tizen.Multimedia.WavPlayer";
+    }
+
+    /// <summary>
+    /// The WavPlayer class allows you to simply play and stop a wav file. To play a certain
+    /// wav file, call <see cref="Tizen.Multimedia.WavPlayer.StartAsync"/> with
+    /// a path to the .wav file.
+    /// </summary>
+    public static class WavPlayer
+    {
+        /// <summary>
+        /// Plays a WAV file with the stream information of AudioManager, asynchronously.
+        /// </summary>
+        /// <param name="inputFilePath">The file path to play.</param>
+        /// <param name="streamPolicy">The Audiostream policy object.</param>
+        /// <param name="cancellationToken">The cancellation token which can be used to stop the Wav Player.</param>
+        /// <returns>The WAV player ID.</returns>
+        /// <exception cref="ArgumentException">In case of invalid parameters</exception>
+        /// <exception cref="ArgumentNullException">In case of null parameters</exception>
+        /// <exception cref="InvalidOperationException">In case of any invalid operations</exception>
+        /// <exception cref="NotSupportedException">In case of format not supported.</exception>
+        public static async Task StartAsync(string inputFilePath, AudioStreamPolicy streamPolicy, CancellationToken cancellationToken = default(CancellationToken))
+        {
+            int id;
+            var task = new TaskCompletionSource<int>();
+
+            if (String.IsNullOrEmpty(inputFilePath))
+            {
+                throw new ArgumentNullException(nameof(inputFilePath));
+            }
+
+            if (streamPolicy == null)
+            {
+                throw new ArgumentNullException(nameof(streamPolicy));
+            }
+
+            Interop.WavPlayer.WavPlayerCompletedCallback _playerCompletedCallback = (int playerId, IntPtr userData) =>
+            {
+                task.TrySetResult(playerId);
+            };
+            GCHandle callbackHandle = GCHandle.Alloc(_playerCompletedCallback, GCHandleType.Pinned);
+
+            int ret = Interop.WavPlayer.WavPlayerStart(inputFilePath, streamPolicy.Handle, _playerCompletedCallback, IntPtr.Zero, out id);
+            if (ret != (int)WavPlayerError.None)
+            {
+                Log.Error(WavPlayerLog.LogTag, "Error Occured with error code: " + (WavPlayerError)ret);
+                task.TrySetException(WavPlayerErrorFactory.CreateException(ret, "Failed to play Wav file."));
+            }
+
+            if (cancellationToken != CancellationToken.None)
+            {
+                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);
+            }
+
+            await task.Task;
+            callbackHandle.Free();
+        }
+    }
+}
+
diff --git a/src/Tizen.Multimedia/WavPlayer/WavPlayerErrorFactory.cs b/src/Tizen.Multimedia/WavPlayer/WavPlayerErrorFactory.cs
new file mode 100755 (executable)
index 0000000..435a127
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * 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;
+        }
+    }
+}