2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
20 namespace Tizen.Multimedia
22 internal static class AudioManagerLog
24 internal const string Tag = "Tizen.Multimedia.AudioManager";
28 /// The Audio Manager class provides functions to get and set sound parameters like volume and devices.
30 public static class AudioManager
32 private static int _deviceConnectionChangedCallbackId = -1;
33 private static int _deviceStateChangedCallbackId = -1;
35 private static Interop.SoundDeviceConnectionChangedCallback _audioDeviceConnectionChangedCallback;
36 private static Interop.SoundDeviceStateChangedCallback _audioDeviceStateChangedCallback;
38 private static EventHandler<AudioDeviceConnectionChangedEventArgs> _audioDeviceConnectionChanged;
39 private static EventHandler<AudioDeviceStateChangedEventArgs> _audioDeviceStateChanged;
42 /// Constructor for AudioManager. Initializes the VolumeController property etc.
46 VolumeController = new AudioVolume();
50 /// Registers/Unregisters a function to be invoked when the state of connection of an Audio device was changed.
52 public static event EventHandler<AudioDeviceConnectionChangedEventArgs> DeviceConnectionChanged
56 if (_audioDeviceConnectionChanged == null)
58 RegisterAudioDeviceEvent();
59 Tizen.Log.Info(AudioManagerLog.Tag, "DeviceConnectionChanged event registered");
61 _audioDeviceConnectionChanged += value;
62 Tizen.Log.Info(AudioManagerLog.Tag, "DeviceConnectionChanged event added");
66 if (_audioDeviceConnectionChanged?.GetInvocationList()?.GetLength(0) == 1)
68 UnregisterDeviceConnectionChangedEvent();
70 _audioDeviceConnectionChanged -= value;
71 Tizen.Log.Info(AudioManagerLog.Tag, "DeviceConnectionChanged event removed");
76 /// Registers/Unregisters a callback function to be invoked when the state of an Audio sound device was changed.
78 public static event EventHandler<AudioDeviceStateChangedEventArgs> DeviceStateChanged
82 if (_audioDeviceStateChanged == null)
84 RegisterDeviceStateChangedEvent();
86 _audioDeviceStateChanged += value;
87 Tizen.Log.Info(AudioManagerLog.Tag, "DeviceStateChanged event added");
91 if (_audioDeviceStateChanged?.GetInvocationList()?.GetLength(0) == 1)
93 UnregisterDeviceStateChangedEvent();
95 _audioDeviceStateChanged -= value;
96 Tizen.Log.Info(AudioManagerLog.Tag, "DeviceStateChanged event removed");
101 /// The VolumeController object (singleton) is-a part of SoundManager and its properties and methods are used via AudioManager
103 public static AudioVolume VolumeController { get; }
106 /// Gets the list consisting of all devices currently connected.
108 /// <param name="options">The audio device options</param>
109 /// <returns>The list of connected devices: IEnumerable of Device objects</returns>
110 public static IEnumerable<AudioDevice> GetCurrentDevices(AudioDeviceOptions options)
112 List<AudioDevice> audioDeviceList = new List<AudioDevice>();
113 IntPtr deviceListHandle;
114 IntPtr handlePosition;
115 AudioDeviceIoDirection ioDirection;
117 int ret = Interop.AudioDevice.GetCurrentDeviceList(options, out deviceListHandle);
118 if (ret != (int)AudioManagerError.NoData)
120 AudioManagerErrorFactory.CheckAndThrowException(ret, "Unable to get next device");
122 while (ret == (int)AudioManagerError.None)
124 ret = Interop.AudioDevice.GetNextDevice(deviceListHandle, out handlePosition);
125 if (ret == (int)AudioManagerError.NoData)
129 else if (ret != (int)AudioManagerError.None)
131 AudioManagerErrorFactory.CheckAndThrowException(ret, "Unable to get next device");
134 if (options == AudioDeviceOptions.Input || (options == AudioDeviceOptions.Output))
136 ret = Interop.AudioDevice.GetDeviceIoDirection(handlePosition, out ioDirection);
139 Tizen.Log.Error(AudioManagerLog.Tag, "Unable to get device IoDirection" + (AudioManagerError)ret);
140 AudioManagerErrorFactory.CheckAndThrowException(ret, handlePosition, "Unable to get device IO Direction");
142 else if (ioDirection == AudioDeviceIoDirection.InputAndOutput)
147 audioDeviceList.Add(new AudioDevice(handlePosition));
149 return audioDeviceList;
152 private static void RegisterAudioDeviceEvent()
154 _audioDeviceConnectionChangedCallback = (IntPtr device, bool isConnected, IntPtr userData) =>
156 AudioDeviceConnectionChangedEventArgs eventArgs = new AudioDeviceConnectionChangedEventArgs(new AudioDevice(device), isConnected);
157 _audioDeviceConnectionChanged?.Invoke(null, eventArgs);
159 int ret = Interop.AudioDevice.AddDeviceConnectionChangedCallback(AudioDeviceOptions.All, _audioDeviceConnectionChangedCallback, IntPtr.Zero, out _deviceConnectionChangedCallbackId);
160 AudioManagerErrorFactory.CheckAndThrowException(ret, "Unable to add device connection changed callback");
161 Tizen.Log.Info(AudioManagerLog.Tag, "AudioDeviceConnectionChanged Event registered");
164 private static void RegisterDeviceStateChangedEvent()
166 _audioDeviceStateChangedCallback = (IntPtr device, AudioDeviceState changedState, IntPtr userData) =>
168 AudioDeviceStateChangedEventArgs eventArgs = new AudioDeviceStateChangedEventArgs(new AudioDevice(device), changedState);
169 _audioDeviceStateChanged?.Invoke(null, eventArgs);
171 int ret = Interop.AudioDevice.AddDeviceStateChangedCallback(AudioDeviceOptions.All, _audioDeviceStateChangedCallback, IntPtr.Zero, out _deviceStateChangedCallbackId);
172 AudioManagerErrorFactory.CheckAndThrowException(ret, "Unable to add device state changed callback");
173 Tizen.Log.Info(AudioManagerLog.Tag, "AudioDeviceStateChangedEvent callback registered");
176 private static void UnregisterDeviceConnectionChangedEvent()
178 if (_deviceConnectionChangedCallbackId > 0)
180 int ret = Interop.AudioDevice.RemoveDeviceConnectionChangedCallback(_deviceConnectionChangedCallbackId);
181 AudioManagerErrorFactory.CheckAndThrowException(ret, "Unable to remove device connection changed callback");
182 Tizen.Log.Info(AudioManagerLog.Tag, "AudioDeviceConnectionChangedEvent callback unregistered");
183 _deviceConnectionChangedCallbackId = -1;
187 private static void UnregisterDeviceStateChangedEvent()
189 if (_deviceStateChangedCallbackId > 0)
191 int ret = Interop.AudioDevice.RemoveDeviceStateChangedCallback(_deviceStateChangedCallbackId);
192 AudioManagerErrorFactory.CheckAndThrowException(ret, "Unable to remove device state changed callback");
193 Tizen.Log.Info(AudioManagerLog.Tag, "AudioDeviceStateChanged callback unregistered");
194 _deviceStateChangedCallbackId = -1;