Merge remote-tracking branch 'origin/master' into tizen
[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 = (int)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         public AudioDeviceState State
83         {
84             get
85             {
86                 Interop.AudioDevice.GetDeviceState(Id, out var state).
87                     Validate("Failed to get the state of the device");
88
89                 return state;
90             }
91         }
92
93         /// <summary>
94         /// Returns a string that represents the current object.
95         /// </summary>
96         /// <returns>A string that represents the current object.</returns>
97         /// <since_tizen> 4 </since_tizen>
98         public override string ToString() =>
99             $"Id={Id}, Name={Name}, Type={Type}, IoDirection={IoDirection}, State={State}";
100
101         /// <summary>
102         /// Compares an object to an instance of <see cref="AudioDevice"/> for equality.
103         /// </summary>
104         /// <param name="obj">A <see cref="Object"/> to compare.</param>
105         /// <returns>true if the two devices are equal; otherwise, false.</returns>
106         /// <since_tizen> 4 </since_tizen>
107         public override bool Equals(object obj)
108         {
109             var rhs = obj as AudioDevice;
110             if (rhs == null)
111             {
112                 return false;
113             }
114
115             return Id == rhs.Id;
116         }
117
118
119         /// <summary>
120         /// Gets the hash code for this instance of <see cref="AudioDevice"/>.
121         /// </summary>
122         /// <returns>The hash code for this instance of <see cref="AudioDevice"/>.</returns>
123         /// <since_tizen> 4 </since_tizen>
124         public override int GetHashCode()
125         {
126             return Id.GetHashCode();
127         }
128     }
129 }