[AudioIO] Add new sample type and change maximum sample rate (#412)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / AudioManager / AudioDevice.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.Runtime.InteropServices;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Provides the ability to query the information of sound devices.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class AudioDevice
27     {
28         private readonly int _id;
29         private readonly AudioDeviceType _type;
30         private readonly AudioDeviceIoDirection _ioDirection;
31
32         internal AudioDevice(IntPtr deviceHandle)
33         {
34             int ret = Interop.AudioDevice.GetDeviceId(deviceHandle, out _id);
35             MultimediaDebug.AssertNoError(ret);
36
37             ret = Interop.AudioDevice.GetDeviceName(deviceHandle, out var name);
38             MultimediaDebug.AssertNoError(ret);
39
40             Name = Marshal.PtrToStringAnsi(name);
41
42             ret = Interop.AudioDevice.GetDeviceType(deviceHandle, out _type);
43             MultimediaDebug.AssertNoError(ret);
44
45             ret = Interop.AudioDevice.GetDeviceIoDirection(deviceHandle, out _ioDirection);
46             MultimediaDebug.AssertNoError(ret);
47         }
48
49         /// <summary>
50         /// Gets the ID of the device.
51         /// </summary>
52         /// <value>The id of the device.</value>
53         /// <since_tizen> 3 </since_tizen>
54         public int Id => _id;
55
56         /// <summary>
57         /// Gets the name of the device.
58         /// </summary>
59         /// <value>The name of the device.</value>
60         /// <since_tizen> 3 </since_tizen>
61         public string Name { get; }
62
63         /// <summary>
64         /// Gets the type of the device.
65         /// </summary>
66         /// <value>The <see cref="AudioDeviceType"/> of the device.</value>
67         /// <since_tizen> 3 </since_tizen>
68         public AudioDeviceType Type => _type;
69
70         /// <summary>
71         /// Gets the IO direction of the device.
72         /// </summary>
73         /// <value>The IO direction of the device.</value>
74         /// <since_tizen> 3 </since_tizen>
75         public AudioDeviceIoDirection IoDirection => _ioDirection;
76
77         /// <summary>
78         /// Gets the state of the device.
79         /// </summary>
80         /// <value>The <see cref="AudioDeviceState"/> of the device.</value>
81         /// <since_tizen> 3 </since_tizen>
82         [Obsolete("Deprecated since API level 5. Please use the IsRunning property instead.")]
83         public AudioDeviceState State
84         {
85             get
86             {
87                 Interop.AudioDevice.GetDeviceState(Id, out var state).
88                     ThrowIfError("Failed to get the state of the device");
89
90                 return state;
91             }
92         }
93
94         /// <summary>
95         /// Gets the running state of the device.
96         /// </summary>
97         /// <value>true if the audio stream of device is running actually; otherwise, false.</value>
98         /// <since_tizen> 5 </since_tizen>
99         public bool IsRunning
100         {
101             get
102             {
103                 Interop.AudioDevice.IsDeviceRunning(_id, out bool isRunning).
104                     ThrowIfError("Failed to get the running state of the device");
105
106                 return isRunning;
107             }
108         }
109
110         /// <summary>
111         /// Returns a string that represents the current object.
112         /// </summary>
113         /// <returns>A string that represents the current object.</returns>
114         /// <since_tizen> 4 </since_tizen>
115         public override string ToString() =>
116             $"Id={Id}, Name={Name}, Type={Type}, IoDirection={IoDirection}, IsRunning={IsRunning}";
117
118         /// <summary>
119         /// Compares an object to an instance of <see cref="AudioDevice"/> for equality.
120         /// </summary>
121         /// <param name="obj">A <see cref="Object"/> to compare.</param>
122         /// <returns>true if the two devices are equal; otherwise, false.</returns>
123         /// <since_tizen> 4 </since_tizen>
124         public override bool Equals(object obj)
125         {
126             var rhs = obj as AudioDevice;
127             if (rhs == null)
128             {
129                 return false;
130             }
131
132             return Id == rhs.Id;
133         }
134
135
136         /// <summary>
137         /// Gets the hash code for this instance of <see cref="AudioDevice"/>.
138         /// </summary>
139         /// <returns>The hash code for this instance of <see cref="AudioDevice"/>.</returns>
140         /// <since_tizen> 4 </since_tizen>
141         public override int GetHashCode()
142         {
143             return Id.GetHashCode();
144         }
145     }
146 }