Release 4.0.0-preview1-00201
[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     public class AudioDevice
26     {
27         private readonly int _id;
28         private readonly AudioDeviceType _type;
29         private readonly AudioDeviceIoDirection _ioDirection;
30
31         internal AudioDevice(IntPtr deviceHandle)
32         {
33             int ret = Interop.AudioDevice.GetDeviceId(deviceHandle, out _id);
34             MultimediaDebug.AssertNoError(ret);
35
36             ret = Interop.AudioDevice.GetDeviceName(deviceHandle, out var name);
37             MultimediaDebug.AssertNoError(ret);
38
39             Name = Marshal.PtrToStringAnsi(name);
40
41             ret = Interop.AudioDevice.GetDeviceType(deviceHandle, out _type);
42             MultimediaDebug.AssertNoError(ret);
43
44             ret = (int)Interop.AudioDevice.GetDeviceIoDirection(deviceHandle, out _ioDirection);
45             MultimediaDebug.AssertNoError(ret);
46         }
47
48         /// <summary>
49         /// Gets the ID of the device.
50         /// </summary>
51         /// <value>The id of the device.</value>
52         public int Id => _id;
53
54         /// <summary>
55         /// Gets the name of the device.
56         /// </summary>
57         /// <value>The name of the device.</value>
58         public string Name { get; }
59
60         /// <summary>
61         /// Gets the type of the device.
62         /// </summary>
63         /// <value>The <see cref="AudioDeviceType"/> of the device.</value>
64         public AudioDeviceType Type => _type;
65
66         /// <summary>
67         /// Gets the IO direction of the device.
68         /// </summary>
69         /// <value>The IO direction of the device.</value>
70         public AudioDeviceIoDirection IoDirection => _ioDirection;
71
72         /// <summary>
73         /// Gets the state of the device.
74         /// </summary>
75         /// <value>The <see cref="AudioDeviceState"/> of the device.</value>
76         public AudioDeviceState State
77         {
78             get
79             {
80                 Interop.AudioDevice.GetDeviceState(Id, out var state).
81                     Validate("Failed to get the state of the device");
82
83                 return state;
84             }
85         }
86
87         /// <summary>
88         /// Returns a string that represents the current object.
89         /// </summary>
90         /// <returns>A string that represents the current object.</returns>
91         public override string ToString() =>
92             $"Id={Id}, Name={Name}, Type={Type}, IoDirection={IoDirection}, State={State}";
93
94         /// <summary>
95         /// Compares an object to an instance of <see cref="AudioDevice"/> for equality.
96         /// </summary>
97         /// <param name="obj">A <see cref="Object"/> to compare.</param>
98         /// <returns>true if the two devices are equal; otherwise, false.</returns>
99         public override bool Equals(object obj)
100         {
101             var rhs = obj as AudioDevice;
102             if (rhs == null)
103             {
104                 return false;
105             }
106
107             return Id == rhs.Id;
108         }
109
110
111         /// <summary>
112         /// Gets the hash code for this instance of <see cref="AudioDevice"/>.
113         /// </summary>
114         /// <returns>The hash code for this instance of <see cref="AudioDevice"/>.</returns>
115         public override int GetHashCode()
116         {
117             return Id.GetHashCode();
118         }
119     }
120 }