Release 4.0.0-preview1-00051
[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     internal static class AudioDeviceLog
23     {
24         internal const string Tag = "Tizen.Multimedia.AudioDevice";
25     }
26
27     /// <summary>
28     /// The Device API provides functions to query the information of sound devices.
29     /// </summary>
30     public class AudioDevice
31     {
32         private readonly int _id;
33         private readonly string _name;
34         private readonly AudioDeviceType _type;
35         private readonly AudioDeviceIoDirection _ioDirection;
36         private readonly AudioDeviceState _state;
37         private readonly IntPtr _handle;
38
39         internal AudioDevice(IntPtr deviceHandle)
40         {
41             _handle = deviceHandle;
42             int ret;
43
44             ret = Interop.AudioDevice.GetDeviceId(_handle, out _id);
45             if (ret != 0)
46             {
47                 Tizen.Log.Error(AudioDeviceLog.Tag, "Unable to get device Id: " + (AudioManagerError)ret);
48             }
49             AudioManagerErrorFactory.CheckAndThrowException(ret, _handle, "Unable to get device Id");
50
51             IntPtr name;
52             ret = Interop.AudioDevice.GetDeviceName(_handle, out name);
53             if (ret != 0)
54             {
55                 Tizen.Log.Error(AudioDeviceLog.Tag, "Unable to get device name" + (AudioManagerError)ret);
56             }
57             AudioManagerErrorFactory.CheckAndThrowException(ret, _handle, "Unable to get device name");
58
59             _name = Marshal.PtrToStringAnsi(name);
60
61             ret = Interop.AudioDevice.GetDeviceType(_handle, out _type);
62             if (ret != 0)
63             {
64                 Tizen.Log.Error(AudioDeviceLog.Tag, "Unable to get device type" + (AudioManagerError)ret);
65             }
66             AudioManagerErrorFactory.CheckAndThrowException(ret, _handle, "Unable to get device type");
67
68             ret = Interop.AudioDevice.GetDeviceIoDirection(_handle, out _ioDirection);
69             if (ret != 0)
70             {
71                 Tizen.Log.Error(AudioDeviceLog.Tag, "Unable to get device IoDirection" + (AudioManagerError)ret);
72             }
73             AudioManagerErrorFactory.CheckAndThrowException(ret, _handle, "Unable to get device IO Direction");
74
75             ret = Interop.AudioDevice.GetDeviceState(_handle, out _state);
76             if (ret != 0)
77             {
78                 Tizen.Log.Error(AudioDeviceLog.Tag, "Unable to get device state" + (AudioManagerError)ret);
79             }
80             AudioManagerErrorFactory.CheckAndThrowException(ret, _handle, "Unable to get device state");
81         }
82
83         /// <summary>
84         /// The id of the device.
85         /// </summary>
86         public int Id => _id;
87
88         /// <summary>
89         /// The name of the device.
90         /// </summary>
91         public string Name => _name;
92
93         /// <summary>
94         /// The type of the device.
95         /// </summary>
96         public AudioDeviceType Type => _type;
97
98         /// <summary>
99         /// The io direction of the device.
100         /// </summary>
101         public AudioDeviceIoDirection IoDirection => _ioDirection;
102
103         /// <summary>
104         /// The state of the device.
105         /// </summary>
106         public AudioDeviceState State => _state;
107
108         internal IntPtr Handle => _handle;
109     }
110 }