Release 4.0.0-preview1-00201
[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     public static class WavPlayer
29     {
30         /// <summary>
31         /// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/>.
32         /// </summary>
33         /// <returns>A task that represents the asynchronous operation.</returns>
34         /// <param name="path">A file path to play.</param>
35         /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
36         /// <exception cref="ArgumentNullException">
37         ///     <paramref name="path"/> is null.
38         ///     <para>-or-</para>
39         ///     <paramref name="streamPolicy"/> is null.
40         /// </exception>
41         /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
42         /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
43         /// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception>
44         /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
45         public static Task StartAsync(string path, AudioStreamPolicy streamPolicy)
46         {
47             return StartAsync(path, streamPolicy, CancellationToken.None);
48         }
49
50         /// <summary>
51         /// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/>.
52         /// </summary>
53         /// <returns>A task that represents the asynchronous operation.</returns>
54         /// <param name="path">A file path to play.</param>
55         /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
56         /// <param name="cancellationToken">A cancellation token which can be used to stop.</param>
57         /// <exception cref="ArgumentNullException">
58         ///     <paramref name="path"/> is null.
59         ///     <para>-or-</para>
60         ///     <paramref name="streamPolicy"/> is null.
61         /// </exception>
62         /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
63         /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
64         /// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception>
65         /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
66         public static Task StartAsync(string path, AudioStreamPolicy streamPolicy,
67             CancellationToken cancellationToken)
68         {
69             if (path == null)
70             {
71                 throw new ArgumentNullException(nameof(path));
72             }
73
74             if (streamPolicy == null)
75             {
76                 throw new ArgumentNullException(nameof(streamPolicy));
77             }
78
79             if (File.Exists(path) == false)
80             {
81                 throw new FileNotFoundException("File does not exists.", path);
82             }
83
84             return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
85                 StartAsyncCore(path, streamPolicy, cancellationToken);
86         }
87
88         private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy,
89             CancellationToken cancellationToken)
90         {
91             var tcs = new TaskCompletionSource<bool>();
92
93             Native.WavPlayerCompletedCallback cb = (id_, _) => tcs.TrySetResult(true);
94
95             using (ObjectKeeper.Get(cb))
96             {
97                 Native.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out var id).
98                     Validate("Failed to play.");
99
100                 using (RegisterCancellationAction(tcs, cancellationToken, id))
101                 {
102                     await tcs.Task;
103                 }
104             }
105         }
106
107         private static IDisposable RegisterCancellationAction(TaskCompletionSource<bool> tcs,
108             CancellationToken cancellationToken, int id)
109         {
110             if (cancellationToken.CanBeCanceled == false)
111             {
112                 return null;
113             }
114
115             return cancellationToken.Register(() =>
116             {
117                 Native.Stop(id).Validate("Failed to cancel");
118                 tcs.TrySetCanceled();
119             });
120         }
121     }
122 }
123