1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "audio_proxy.h"
17
18 #include "telephony_log_wrapper.h"
19 #include "bluetooth_call_manager.h"
20 #include "audio_control_manager.h"
21
22 namespace OHOS {
23 namespace Telephony {
AudioProxy()24 AudioProxy::AudioProxy()
25 : context_(nullptr), audioSoundManager_(std::make_unique<AudioStandard::RingtoneSoundManager>()),
26 deviceCallback_(std::make_shared<AudioDeviceChangeCallback>())
27 {}
28
~AudioProxy()29 AudioProxy::~AudioProxy() {}
30
SetAudioScene(AudioStandard::AudioScene audioScene)31 bool AudioProxy::SetAudioScene(AudioStandard::AudioScene audioScene)
32 {
33 return (AudioStandard::AudioSystemManager::GetInstance()->SetAudioScene(audioScene) == TELEPHONY_SUCCESS);
34 }
35
SetAudioDeviceChangeCallback()36 int32_t AudioProxy::SetAudioDeviceChangeCallback()
37 {
38 if (deviceCallback_ == nullptr) {
39 TELEPHONY_LOGE("device callback nullptr");
40 return TELEPHONY_ERR_LOCAL_PTR_NULL;
41 }
42 return AudioStandard::AudioSystemManager::GetInstance()->SetDeviceChangeCallback(
43 AudioStandard::DeviceFlag::ALL_DEVICES_FLAG, deviceCallback_);
44 }
45
SetBluetoothDevActive()46 bool AudioProxy::SetBluetoothDevActive()
47 {
48 if (AudioStandard::AudioSystemManager::GetInstance()->IsDeviceActive(
49 AudioStandard::ActiveDeviceType::BLUETOOTH_SCO)) {
50 TELEPHONY_LOGI("bluetooth device is already active");
51 return true;
52 }
53 bool ret = AudioStandard::AudioSystemManager::GetInstance()->SetDeviceActive(
54 AudioStandard::ActiveDeviceType::BLUETOOTH_SCO, true);
55 if (ret == ERR_NONE) {
56 return true;
57 }
58 return false;
59 }
60
SetSpeakerDevActive()61 bool AudioProxy::SetSpeakerDevActive()
62 {
63 if (AudioStandard::AudioSystemManager::GetInstance()->IsDeviceActive(AudioStandard::ActiveDeviceType::SPEAKER)) {
64 TELEPHONY_LOGI("speaker device is already active");
65 return true;
66 }
67 bool ret = AudioStandard::AudioSystemManager::GetInstance()->SetDeviceActive(
68 AudioStandard::ActiveDeviceType::SPEAKER, true);
69 if (ret == ERR_NONE) {
70 return true;
71 }
72 return false;
73 }
74
SetWiredHeadsetDevActive()75 bool AudioProxy::SetWiredHeadsetDevActive()
76 {
77 return false;
78 }
79
SetEarpieceDevActive()80 bool AudioProxy::SetEarpieceDevActive()
81 {
82 return false;
83 }
84
GetVolume(AudioStandard::AudioVolumeType audioVolumeType)85 int32_t AudioProxy::GetVolume(AudioStandard::AudioVolumeType audioVolumeType)
86 {
87 return AudioStandard::AudioSystemManager::GetInstance()->GetVolume(audioVolumeType);
88 }
89
SetVolume(AudioStandard::AudioVolumeType audioVolumeType,int32_t volume)90 int32_t AudioProxy::SetVolume(AudioStandard::AudioVolumeType audioVolumeType, int32_t volume)
91 {
92 return AudioStandard::AudioSystemManager::GetInstance()->SetVolume(audioVolumeType, volume);
93 }
94
SetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType)95 int32_t AudioProxy::SetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType)
96 {
97 int32_t maxVolume = GetMaxVolume(audioVolumeType);
98 return AudioStandard::AudioSystemManager::GetInstance()->SetVolume(audioVolumeType, maxVolume);
99 }
100
SetVolumeAudible()101 void AudioProxy::SetVolumeAudible()
102 {
103 int32_t volume = GetMaxVolume(AudioStandard::AudioVolumeType::STREAM_VOICE_CALL);
104 SetVolume(AudioStandard::AudioVolumeType::STREAM_VOICE_CALL,
105 (int32_t)(volume / VOLUME_AUDIBLE_DIVISOR));
106 }
107
IsStreamActive(AudioStandard::AudioVolumeType audioVolumeType)108 bool AudioProxy::IsStreamActive(AudioStandard::AudioVolumeType audioVolumeType)
109 {
110 return AudioStandard::AudioSystemManager::GetInstance()->IsStreamActive(audioVolumeType);
111 }
112
IsStreamMute(AudioStandard::AudioVolumeType audioVolumeType)113 bool AudioProxy::IsStreamMute(AudioStandard::AudioVolumeType audioVolumeType)
114 {
115 return AudioStandard::AudioSystemManager::GetInstance()->IsStreamMute(audioVolumeType);
116 }
117
GetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType)118 int32_t AudioProxy::GetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType)
119 {
120 return AudioStandard::AudioSystemManager::GetInstance()->GetMaxVolume(audioVolumeType);
121 }
122
GetMinVolume(AudioStandard::AudioVolumeType audioVolumeType)123 int32_t AudioProxy::GetMinVolume(AudioStandard::AudioVolumeType audioVolumeType)
124 {
125 return AudioStandard::AudioSystemManager::GetInstance()->GetMinVolume(audioVolumeType);
126 }
127
IsMicrophoneMute()128 bool AudioProxy::IsMicrophoneMute()
129 {
130 return AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute();
131 }
132
SetMicrophoneMute(bool mute)133 bool AudioProxy::SetMicrophoneMute(bool mute)
134 {
135 if (mute == IsMicrophoneMute()) {
136 return true;
137 }
138 int32_t muteResult = AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(mute);
139 TELEPHONY_LOGI("set microphone mute result : %{public}d", muteResult);
140 return (muteResult == TELEPHONY_SUCCESS);
141 }
142
GetRingerMode() const143 AudioStandard::AudioRingerMode AudioProxy::GetRingerMode() const
144 {
145 return AudioStandard::AudioSystemManager::GetInstance()->GetRingerMode();
146 }
147
IsVibrateMode() const148 bool AudioProxy::IsVibrateMode() const
149 {
150 return (AudioStandard::AudioRingerMode::RINGER_MODE_VIBRATE == GetRingerMode());
151 }
152
OnDeviceChange(const AudioStandard::DeviceChangeAction & deviceChangeAction)153 void AudioDeviceChangeCallback::OnDeviceChange(const AudioStandard::DeviceChangeAction &deviceChangeAction)
154 {
155 AudioStandard::DeviceChangeType changeType = deviceChangeAction.type;
156 switch (changeType) {
157 case AudioStandard::DeviceChangeType::CONNECT:
158 #ifdef ABILITY_AUDIO_SUPPORT
159 auto devices = deviceChangeAction.deviceDescriptors;
160 for (auto device : devices) {
161 if (device->getType() == AudioStandard::DeviceType::DEVICE_TYPE_BLUETOOTH_SCO) {
162 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(
163 AudioEvent::BLUETOOTH_SCO_CONNECTED);
164 } else if (device->getType() == AudioStandard::DeviceType::DEVICE_TYPE_WIRED_HEADSET) {
165 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(
166 AudioEvent::WIRED_HEADSET_CONNECTED);
167 }
168 }
169 #endif
170 break;
171 case AudioStandard::DeviceChangeType::DISCONNECT:
172 #ifdef ABILITY_AUDIO_SUPPORT
173 auto devices = deviceChangeAction.deviceDescriptors;
174 for (auto device : devices) {
175 if (device->getType() == AudioStandard::DeviceType::DEVICE_TYPE_BLUETOOTH_SCO) {
176 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(
177 AudioEvent::BLUETOOTH_SCO_DISCONNECTED);
178 } else if (device->getType() == AudioStandard::DeviceType::DEVICE_TYPE_WIRED_HEADSET) {
179 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(
180 AudioEvent::WIRED_HEADSET_DISCONNECTED);
181 }
182 }
183 #endif
184 break;
185 default:
186 break;
187 }
188 }
189
GetSystemRingtoneUri() const190 std::string AudioProxy::GetSystemRingtoneUri() const
191 {
192 if (audioSoundManager_ == nullptr) {
193 TELEPHONY_LOGE("audio sound manager nullptr");
194 return "";
195 }
196 if (context_ == nullptr) {
197 TELEPHONY_LOGE("context nullptr");
198 return "";
199 }
200 AudioStandard::RingtoneType rinigtoneType = AudioStandard::RingtoneType::RINGTONE_TYPE_DEFAULT;
201 return audioSoundManager_->GetSystemRingtoneUri(context_, rinigtoneType);
202 }
203
StartVibrate()204 int32_t AudioProxy::StartVibrate()
205 {
206 #ifdef ABILITY_SENSOR_SUPPORT
207 return VibratorManager::GetInstance()->StartVibrate();
208 #endif
209 return TELEPHONY_SUCCESS;
210 }
211
CancelVibrate()212 int32_t AudioProxy::CancelVibrate()
213 {
214 #ifdef ABILITY_SENSOR_SUPPORT
215 return VibratorManager::GetInstance()->CancelVibrate();
216 #endif
217 return TELEPHONY_SUCCESS;
218 }
219
GetDefaultRingPath() const220 std::string AudioProxy::GetDefaultRingPath() const
221 {
222 return defaultRingPath_;
223 }
224
GetDefaultTonePath() const225 std::string AudioProxy::GetDefaultTonePath() const
226 {
227 return defaultTonePath_;
228 }
229
GetDefaultDtmfPath() const230 std::string AudioProxy::GetDefaultDtmfPath() const
231 {
232 return defaultDtmfPath_;
233 }
234 } // namespace Telephony
235 } // namespace OHOS
236