[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.AudioIO / WavPlayer / WavPlayer.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.IO;
19 using System.Threading;
20 using System.Threading.Tasks;
21 using Native = Interop.WavPlayer;
22
23 namespace Tizen.Multimedia
24 {
25     /// <summary>
26     /// Provides the ability to play a wav file.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public static class WavPlayer
30     {
31         /// <summary>
32         /// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/>.
33         /// </summary>
34         /// <returns>A task that represents the asynchronous operation.</returns>
35         /// <param name="path">A file path to play.</param>
36         /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
37         /// <exception cref="ArgumentNullException">
38         ///     <paramref name="path"/> is null.
39         ///     <para>-or-</para>
40         ///     <paramref name="streamPolicy"/> is null.
41         /// </exception>
42         /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
43         /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
44         /// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception>
45         /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
46         /// <since_tizen> 3 </since_tizen>
47         public static Task StartAsync(string path, AudioStreamPolicy streamPolicy)
48         {
49             return StartAsync(path, streamPolicy, CancellationToken.None);
50         }
51
52         /// <summary>
53         /// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/>.
54         /// </summary>
55         /// <returns>A task that represents the asynchronous operation.</returns>
56         /// <param name="path">A file path to play.</param>
57         /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
58         /// <param name="cancellationToken">A cancellation token which can be used to stop.</param>
59         /// <exception cref="ArgumentNullException">
60         ///     <paramref name="path"/> is null.
61         ///     <para>-or-</para>
62         ///     <paramref name="streamPolicy"/> is null.
63         /// </exception>
64         /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
65         /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
66         /// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception>
67         /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
68         /// <since_tizen> 3 </since_tizen>
69         public static Task StartAsync(string path, AudioStreamPolicy streamPolicy,
70             CancellationToken cancellationToken)
71         {
72             if (path == null)
73             {
74                 throw new ArgumentNullException(nameof(path));
75             }
76
77             if (streamPolicy == null)
78             {
79                 throw new ArgumentNullException(nameof(streamPolicy));
80             }
81
82             if (File.Exists(path) == false)
83             {
84                 throw new FileNotFoundException("File does not exists.", path);
85             }
86
87             return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
88                 StartAsyncCore(path, streamPolicy, cancellationToken);
89         }
90
91         private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy,
92             CancellationToken cancellationToken)
93         {
94             var tcs = new TaskCompletionSource<bool>();
95
96             Native.WavPlayerCompletedCallback cb = (id_, _) => tcs.TrySetResult(true);
97
98             using (var cbKeeper = ObjectKeeper.Get(cb))
99             {
100                 Native.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out var id).
101                     Validate("Failed to play.");
102
103                 using (RegisterCancellationAction(tcs, cancellationToken, id))
104                 {
105                     await tcs.Task.ConfigureAwait(false);
106                 }
107             }
108         }
109
110         private static IDisposable RegisterCancellationAction(TaskCompletionSource<bool> tcs,
111             CancellationToken cancellationToken, int id)
112         {
113             if (cancellationToken.CanBeCanceled == false)
114             {
115                 return null;
116             }
117
118             return cancellationToken.Register(() =>
119             {
120                 Native.Stop(id).Validate("Failed to cancel");
121                 tcs.TrySetCanceled();
122             });
123         }
124     }
125 }
126