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