Release 4.0.0-preview1-00201
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / AudioManager / AudioManager.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.Collections.Generic;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Provides the ability to control volume levels and monitor audio devices.
24     /// </summary>
25     public static class AudioManager
26     {
27         static AudioManager()
28         {
29             VolumeController = new AudioVolume();
30         }
31
32         /// <summary>
33         /// Gets the volume controller.
34         /// </summary>
35         /// <value>The <see cref="AudioVolume"/>.</value>
36         public static AudioVolume VolumeController { get; }
37
38         /// <summary>
39         /// Gets the all devices currently connected.
40         /// </summary>
41         /// <returns>An IEnumerable&lt;AudioDevice&gt; that contains connected devices.</returns>
42         public static IEnumerable<AudioDevice> GetConnectedDevices()
43         {
44             IntPtr deviceListHandle = IntPtr.Zero;
45
46             try
47             {
48                 var ret = Interop.AudioDevice.GetDeviceList(AudioDeviceOptions.All, out deviceListHandle);
49
50                 List<AudioDevice> result = new List<AudioDevice>();
51
52                 if (ret == AudioManagerError.NoData)
53                 {
54                     return result;
55                 }
56
57                 ret.Validate("Failed to get connected devices");
58
59                 while (ret == AudioManagerError.None)
60                 {
61                     ret = Interop.AudioDevice.GetNextDevice(deviceListHandle, out var deviceHandle);
62
63                     if (ret == AudioManagerError.NoData)
64                     {
65                         break;
66                     }
67
68                     ret.Validate("Failed to get connected devices");
69
70                     result.Add(new AudioDevice(deviceHandle));
71                 }
72                 return result;
73             }
74             finally
75             {
76                 Interop.AudioDevice.FreeDeviceList(deviceListHandle);
77             }
78         }
79
80         #region DeviceConnectionChanged event
81         private static int _deviceConnectionChangedCallbackId = -1;
82
83         private static Interop.AudioDevice.ConnectionChangedCallback _audioDeviceConnectionChangedCallback;
84         private static EventHandler<AudioDeviceConnectionChangedEventArgs> _audioDeviceConnectionChanged;
85         private static object _audioDeviceConnectionLock = new object();
86
87         /// <summary>
88         /// Occurs when the state of a connection of an audio device changes.
89         /// </summary>
90         public static event EventHandler<AudioDeviceConnectionChangedEventArgs> DeviceConnectionChanged
91         {
92             add
93             {
94                 lock (_audioDeviceConnectionLock)
95                 {
96                     if (_audioDeviceConnectionChanged == null)
97                     {
98                         RegisterAudioDeviceEvent();
99                     }
100                     _audioDeviceConnectionChanged += value;
101                 }
102             }
103             remove
104             {
105                 if (value == null)
106                 {
107                     return;
108                 }
109
110                 lock (_audioDeviceConnectionLock)
111                 {
112                     if (_audioDeviceConnectionChanged == value)
113                     {
114                         UnregisterDeviceConnectionChangedEvent();
115                     }
116                     _audioDeviceConnectionChanged -= value;
117                 }
118             }
119         }
120
121         private static void RegisterAudioDeviceEvent()
122         {
123             _audioDeviceConnectionChangedCallback = (IntPtr device, bool isConnected, IntPtr userData) =>
124             {
125                 _audioDeviceConnectionChanged?.Invoke(null,
126                     new AudioDeviceConnectionChangedEventArgs(new AudioDevice(device), isConnected));
127             };
128
129             Interop.AudioDevice.AddDeviceConnectionChangedCallback(AudioDeviceOptions.All,
130                 _audioDeviceConnectionChangedCallback, IntPtr.Zero, out _deviceConnectionChangedCallbackId).
131                 Validate("Unable to add device connection changed callback");
132         }
133
134         private static void UnregisterDeviceConnectionChangedEvent()
135         {
136             Interop.AudioDevice.RemoveDeviceConnectionChangedCallback(_deviceConnectionChangedCallbackId).
137                 Validate("Unable to remove device connection changed callback");
138         }
139         #endregion
140
141         #region DeviceStateChanged event
142         private static int _deviceStateChangedCallbackId = -1;
143
144         private static Interop.AudioDevice.StateChangedCallback _audioDeviceStateChangedCallback;
145         private static EventHandler<AudioDeviceStateChangedEventArgs> _audioDeviceStateChanged;
146         private static object _audioDeviceStateLock = new object();
147
148         /// <summary>
149         /// Occurs when the state of an audio device changes.
150         /// </summary>
151         public static event EventHandler<AudioDeviceStateChangedEventArgs> DeviceStateChanged
152         {
153             add
154             {
155                 lock (_audioDeviceStateLock)
156                 {
157                     if (_audioDeviceStateChanged == null)
158                     {
159                         RegisterDeviceStateChangedEvent();
160                     }
161                     _audioDeviceStateChanged += value;
162                 }
163             }
164             remove
165             {
166                 if (value == null)
167                 {
168                     return;
169                 }
170
171                 lock (_audioDeviceStateLock)
172                 {
173                     if (_audioDeviceStateChanged == value)
174                     {
175                         UnregisterDeviceStateChangedEvent();
176                     }
177                     _audioDeviceStateChanged -= value;
178                 }
179             }
180         }
181
182         private static void RegisterDeviceStateChangedEvent()
183         {
184             _audioDeviceStateChangedCallback = (IntPtr device, AudioDeviceState changedState, IntPtr userData) =>
185             {
186                 _audioDeviceStateChanged?.Invoke(null,
187                     new AudioDeviceStateChangedEventArgs(new AudioDevice(device), changedState));
188             };
189
190             Interop.AudioDevice.AddDeviceStateChangedCallback(AudioDeviceOptions.All,
191                 _audioDeviceStateChangedCallback, IntPtr.Zero, out _deviceStateChangedCallbackId).
192                 Validate("Failed to add device state changed event");
193         }
194
195         private static void UnregisterDeviceStateChangedEvent()
196         {
197             Interop.AudioDevice.RemoveDeviceStateChangedCallback(_deviceStateChangedCallbackId).
198                 Validate("Failed to remove device state changed event");
199         }
200         #endregion
201     }
202 }