• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
24 const int32_t NO_DEVICE_VALID = 0;
25 const int32_t RENDERER_FLAG = 0;
26 
AudioProxy()27 AudioProxy::AudioProxy()
28     : deviceCallback_(std::make_shared<AudioDeviceChangeCallback>()),
29       preferredDeviceCallback_(std::make_shared<AudioPreferDeviceChangeCallback>())
30 {}
31 
~AudioProxy()32 AudioProxy::~AudioProxy() {}
33 
SetAudioScene(AudioStandard::AudioScene audioScene)34 bool AudioProxy::SetAudioScene(AudioStandard::AudioScene audioScene)
35 {
36     return (AudioStandard::AudioSystemManager::GetInstance()->SetAudioScene(audioScene) == TELEPHONY_SUCCESS);
37 }
38 
SetAudioDeviceChangeCallback()39 int32_t AudioProxy::SetAudioDeviceChangeCallback()
40 {
41     if (deviceCallback_ == nullptr) {
42         TELEPHONY_LOGE("device callback nullptr");
43         return TELEPHONY_ERR_LOCAL_PTR_NULL;
44     }
45     return AudioStandard::AudioSystemManager::GetInstance()->SetDeviceChangeCallback(
46         AudioStandard::DeviceFlag::ALL_DEVICES_FLAG, deviceCallback_);
47 }
48 
UnsetDeviceChangeCallback()49 int32_t AudioProxy::UnsetDeviceChangeCallback()
50 {
51     if (deviceCallback_ == nullptr) {
52         TELEPHONY_LOGI("device callback nullptr");
53         return TELEPHONY_SUCCESS;
54     }
55     return AudioStandard::AudioSystemManager::GetInstance()->UnsetDeviceChangeCallback();
56 }
57 
SetBluetoothDevActive()58 bool AudioProxy::SetBluetoothDevActive()
59 {
60     if (AudioStandard::AudioSystemManager::GetInstance()->IsDeviceActive(
61         AudioStandard::ActiveDeviceType::BLUETOOTH_SCO)) {
62         TELEPHONY_LOGI("bluetooth device is already active");
63         return true;
64     }
65     bool ret = AudioStandard::AudioSystemManager::GetInstance()->SetDeviceActive(
66         AudioStandard::ActiveDeviceType::BLUETOOTH_SCO, true);
67     if (ret == ERR_NONE) {
68         return true;
69     }
70     return false;
71 }
72 
SetSpeakerDevActive()73 bool AudioProxy::SetSpeakerDevActive()
74 {
75     if (AudioStandard::AudioSystemManager::GetInstance()->IsDeviceActive(AudioStandard::ActiveDeviceType::SPEAKER)) {
76         TELEPHONY_LOGI("speaker device is already active");
77         return true;
78     }
79     bool ret = AudioStandard::AudioSystemManager::GetInstance()->SetDeviceActive(
80         AudioStandard::ActiveDeviceType::SPEAKER, true);
81     if (ret == ERR_NONE) {
82         return true;
83     }
84     return false;
85 }
86 
SetWiredHeadsetDevActive()87 bool AudioProxy::SetWiredHeadsetDevActive()
88 {
89     if (!isWiredHeadsetConnected_) {
90         TELEPHONY_LOGE("SetWiredHeadsetDevActive wiredheadset is not connected");
91         return false;
92     }
93     if (AudioStandard::AudioSystemManager::GetInstance()->IsDeviceActive(AudioStandard::ActiveDeviceType::SPEAKER)) {
94         int32_t ret = AudioStandard::AudioSystemManager::GetInstance()->SetDeviceActive(
95             AudioStandard::ActiveDeviceType::SPEAKER, false);
96         if (ret != ERR_NONE) {
97             TELEPHONY_LOGE("SetWiredHeadsetDevActive speaker close fail");
98             return false;
99         }
100     }
101     if (AudioStandard::AudioSystemManager::GetInstance()->IsDeviceActive(AudioStandard::ActiveDeviceType::EARPIECE)) {
102         int32_t ret = AudioStandard::AudioSystemManager::GetInstance()->SetDeviceActive(
103             AudioStandard::ActiveDeviceType::EARPIECE, false);
104         if (ret != ERR_NONE) {
105             TELEPHONY_LOGE("SetWiredHeadsetDevActive bluetooth sco close fail");
106             return false;
107         }
108     }
109     if (AudioStandard::AudioSystemManager::GetInstance()->IsDeviceActive(
110         AudioStandard::ActiveDeviceType::BLUETOOTH_SCO)) {
111         int32_t ret = AudioStandard::AudioSystemManager::GetInstance()->SetDeviceActive(
112             AudioStandard::ActiveDeviceType::BLUETOOTH_SCO, false);
113         if (ret != ERR_NONE) {
114             TELEPHONY_LOGE("SetWiredHeadsetDevActive bluetooth sco close fail");
115             return false;
116         }
117     }
118     return true;
119 }
120 
SetEarpieceDevActive()121 bool AudioProxy::SetEarpieceDevActive()
122 {
123     if (isWiredHeadsetConnected_) {
124         TELEPHONY_LOGE("SetEarpieceDevActive wiredheadset is connected, no need set earpiece dev active");
125         return false;
126     }
127     if (AudioStandard::AudioSystemManager::GetInstance()->IsDeviceActive(AudioStandard::ActiveDeviceType::EARPIECE)) {
128         TELEPHONY_LOGI("earpiece device is already active");
129         return true;
130     }
131     if (AudioStandard::AudioSystemManager::GetInstance()->SetDeviceActive(
132         AudioStandard::ActiveDeviceType::EARPIECE, true) != ERR_NONE) {
133         TELEPHONY_LOGE("SetEarpieceDevActive earpiece active fail");
134         return false;
135     }
136     return true;
137 }
138 
GetVolume(AudioStandard::AudioVolumeType audioVolumeType)139 int32_t AudioProxy::GetVolume(AudioStandard::AudioVolumeType audioVolumeType)
140 {
141     return AudioStandard::AudioSystemManager::GetInstance()->GetVolume(audioVolumeType);
142 }
143 
SetVolume(AudioStandard::AudioVolumeType audioVolumeType,int32_t volume)144 int32_t AudioProxy::SetVolume(AudioStandard::AudioVolumeType audioVolumeType, int32_t volume)
145 {
146     return AudioStandard::AudioSystemManager::GetInstance()->SetVolume(audioVolumeType, volume);
147 }
148 
SetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType)149 int32_t AudioProxy::SetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType)
150 {
151     int32_t maxVolume = GetMaxVolume(audioVolumeType);
152     return AudioStandard::AudioSystemManager::GetInstance()->SetVolume(audioVolumeType, maxVolume);
153 }
154 
SetVolumeAudible()155 void AudioProxy::SetVolumeAudible()
156 {
157     int32_t volume = GetMaxVolume(AudioStandard::AudioVolumeType::STREAM_VOICE_CALL);
158     SetVolume(AudioStandard::AudioVolumeType::STREAM_VOICE_CALL,
159         (int32_t)(volume / VOLUME_AUDIBLE_DIVISOR));
160 }
161 
IsStreamActive(AudioStandard::AudioVolumeType audioVolumeType)162 bool AudioProxy::IsStreamActive(AudioStandard::AudioVolumeType audioVolumeType)
163 {
164     return AudioStandard::AudioSystemManager::GetInstance()->IsStreamActive(audioVolumeType);
165 }
166 
IsStreamMute(AudioStandard::AudioVolumeType audioVolumeType)167 bool AudioProxy::IsStreamMute(AudioStandard::AudioVolumeType audioVolumeType)
168 {
169     return AudioStandard::AudioSystemManager::GetInstance()->IsStreamMute(audioVolumeType);
170 }
171 
GetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType)172 int32_t AudioProxy::GetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType)
173 {
174     return AudioStandard::AudioSystemManager::GetInstance()->GetMaxVolume(audioVolumeType);
175 }
176 
GetMinVolume(AudioStandard::AudioVolumeType audioVolumeType)177 int32_t AudioProxy::GetMinVolume(AudioStandard::AudioVolumeType audioVolumeType)
178 {
179     return AudioStandard::AudioSystemManager::GetInstance()->GetMinVolume(audioVolumeType);
180 }
181 
IsMicrophoneMute()182 bool AudioProxy::IsMicrophoneMute()
183 {
184     return AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute();
185 }
186 
SetMicrophoneMute(bool mute)187 bool AudioProxy::SetMicrophoneMute(bool mute)
188 {
189     if (mute == IsMicrophoneMute()) {
190         return true;
191     }
192     int32_t muteResult = AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(mute);
193     TELEPHONY_LOGI("set microphone mute result : %{public}d", muteResult);
194     return (muteResult == TELEPHONY_SUCCESS);
195 }
196 
GetRingerMode() const197 AudioStandard::AudioRingerMode AudioProxy::GetRingerMode() const
198 {
199     return AudioStandard::AudioSystemManager::GetInstance()->GetRingerMode();
200 }
201 
IsVibrateMode() const202 bool AudioProxy::IsVibrateMode() const
203 {
204     return (AudioStandard::AudioRingerMode::RINGER_MODE_VIBRATE == GetRingerMode());
205 }
206 
OnDeviceChange(const AudioStandard::DeviceChangeAction & deviceChangeAction)207 void AudioDeviceChangeCallback::OnDeviceChange(const AudioStandard::DeviceChangeAction &deviceChangeAction)
208 {
209     TELEPHONY_LOGI("AudioDeviceChangeCallback::OnDeviceChange enter");
210     for (auto &audioDeviceDescriptor : deviceChangeAction.deviceDescriptors) {
211         if (audioDeviceDescriptor->deviceType_ == AudioStandard::DEVICE_TYPE_WIRED_HEADSET ||
212             audioDeviceDescriptor->deviceType_ == AudioStandard::DEVICE_TYPE_WIRED_HEADPHONES ||
213             audioDeviceDescriptor->deviceType_ == AudioStandard::DEVICE_TYPE_USB_HEADSET) {
214             if (deviceChangeAction.type == AudioStandard::CONNECT) {
215                 TELEPHONY_LOGI("WiredHeadset connected");
216                 DelayedSingleton<AudioProxy>::GetInstance()->SetWiredHeadsetState(true);
217                 DelayedSingleton<AudioDeviceManager>::GetInstance()->AddAudioDeviceList(
218                     "", AudioDeviceType::DEVICE_WIRED_HEADSET);
219             } else {
220                 TELEPHONY_LOGI("WiredHeadset disConnected");
221                 DelayedSingleton<AudioProxy>::GetInstance()->SetWiredHeadsetState(false);
222                 DelayedSingleton<AudioDeviceManager>::GetInstance()->RemoveAudioDeviceList(
223                     "", AudioDeviceType::DEVICE_WIRED_HEADSET);
224                 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(
225                     AudioEvent::WIRED_HEADSET_DISCONNECTED);
226             }
227         }
228     }
229 }
230 
StartVibrate()231 int32_t AudioProxy::StartVibrate()
232 {
233 #ifdef ABILITY_SENSOR_SUPPORT
234     return VibratorManager::GetInstance()->StartVibrate();
235 #endif
236     return TELEPHONY_SUCCESS;
237 }
238 
CancelVibrate()239 int32_t AudioProxy::CancelVibrate()
240 {
241 #ifdef ABILITY_SENSOR_SUPPORT
242     return VibratorManager::GetInstance()->CancelVibrate();
243 #endif
244     return TELEPHONY_SUCCESS;
245 }
246 
GetDefaultRingPath() const247 std::string AudioProxy::GetDefaultRingPath() const
248 {
249     return defaultRingPath_;
250 }
251 
GetDefaultTonePath() const252 std::string AudioProxy::GetDefaultTonePath() const
253 {
254     return defaultTonePath_;
255 }
256 
GetDefaultDtmfPath() const257 std::string AudioProxy::GetDefaultDtmfPath() const
258 {
259     return defaultDtmfPath_;
260 }
261 
SetWiredHeadsetState(bool isConnected)262 void AudioProxy::SetWiredHeadsetState(bool isConnected)
263 {
264     isWiredHeadsetConnected_ = isConnected;
265 }
266 
GetPreferredOutputAudioDevice(AudioDevice & device)267 int32_t AudioProxy::GetPreferredOutputAudioDevice(AudioDevice &device)
268 {
269     AudioStandard::AudioRendererInfo rendererInfo;
270     rendererInfo.contentType = AudioStandard::ContentType::CONTENT_TYPE_SPEECH;
271     rendererInfo.streamUsage = AudioStandard::StreamUsage::STREAM_USAGE_VOICE_MODEM_COMMUNICATION;
272     rendererInfo.rendererFlags = RENDERER_FLAG;
273     std::vector<sptr<AudioStandard::AudioDeviceDescriptor>> desc;
274     int32_t ret =
275         AudioStandard::AudioRoutingManager::GetInstance()->GetPreferredOutputDeviceForRendererInfo(rendererInfo, desc);
276     if (ret != TELEPHONY_SUCCESS) {
277         TELEPHONY_LOGE("GetPreferredOutputDeviceForRendererInfo fail");
278         return CALL_ERR_AUDIO_OPERATE_FAILED;
279     }
280     if (desc.size() == NO_DEVICE_VALID) {
281         TELEPHONY_LOGE("desc size is zero");
282         return CALL_ERR_AUDIO_OPERATE_FAILED;
283     }
284     switch (desc[0]->deviceType_) {
285         case AudioStandard::DEVICE_TYPE_BLUETOOTH_SCO:
286             device.deviceType = AudioDeviceType::DEVICE_BLUETOOTH_SCO;
287             if (memset_s(&device.address, kMaxAddressLen + 1, 0, kMaxAddressLen + 1) != EOK) {
288                 TELEPHONY_LOGE("memset_s address fail");
289                 return TELEPHONY_ERR_MEMSET_FAIL;
290             }
291             if (memcpy_s(device.address, kMaxAddressLen, desc[0]->macAddress_.c_str(),
292                 desc[0]->macAddress_.length()) != EOK) {
293                 TELEPHONY_LOGE("memcpy_s address fail");
294                 return TELEPHONY_ERR_MEMCPY_FAIL;
295             }
296             break;
297         case AudioStandard::DEVICE_TYPE_EARPIECE:
298             device.deviceType = AudioDeviceType::DEVICE_EARPIECE;
299             break;
300         case AudioStandard::DEVICE_TYPE_SPEAKER:
301             device.deviceType = AudioDeviceType::DEVICE_SPEAKER;
302             break;
303         case AudioStandard::DEVICE_TYPE_WIRED_HEADSET:
304         case AudioStandard::DEVICE_TYPE_WIRED_HEADPHONES:
305         case AudioStandard::DEVICE_TYPE_USB_HEADSET:
306             device.deviceType = AudioDeviceType::DEVICE_WIRED_HEADSET;
307             break;
308         default:
309             break;
310     }
311     return TELEPHONY_SUCCESS;
312 }
313 
SetAudioPreferDeviceChangeCallback()314 int32_t AudioProxy::SetAudioPreferDeviceChangeCallback()
315 {
316     if (preferredDeviceCallback_ == nullptr) {
317         TELEPHONY_LOGE("preferredDeviceCallback_ is nullptr");
318         return TELEPHONY_ERR_LOCAL_PTR_NULL;
319     }
320     AudioStandard::AudioRendererInfo rendererInfo;
321     rendererInfo.contentType = AudioStandard::ContentType::CONTENT_TYPE_SPEECH;
322     rendererInfo.streamUsage = AudioStandard::StreamUsage::STREAM_USAGE_VOICE_MODEM_COMMUNICATION;
323     rendererInfo.rendererFlags = RENDERER_FLAG;
324     int32_t ret = AudioStandard::AudioRoutingManager::GetInstance()->SetPreferredOutputDeviceChangeCallback(
325         rendererInfo, preferredDeviceCallback_);
326     if (ret != TELEPHONY_SUCCESS) {
327         TELEPHONY_LOGE("SetPreferredOutputDeviceChangeCallback fail");
328         return CALL_ERR_AUDIO_OPERATE_FAILED;
329     }
330     return TELEPHONY_SUCCESS;
331 }
332 
UnsetAudioPreferDeviceChangeCallback()333 int32_t AudioProxy::UnsetAudioPreferDeviceChangeCallback()
334 {
335     if (preferredDeviceCallback_ == nullptr) {
336         TELEPHONY_LOGE("preferredDeviceCallback_ is nullptr");
337         return TELEPHONY_ERR_LOCAL_PTR_NULL;
338     }
339     int32_t ret = AudioStandard::AudioRoutingManager::GetInstance()->UnsetPreferredOutputDeviceChangeCallback();
340     if (ret != TELEPHONY_SUCCESS) {
341         TELEPHONY_LOGE("UnsetPreferredOutputDeviceChangeCallback fail");
342         return CALL_ERR_AUDIO_OPERATE_FAILED;
343     }
344     return TELEPHONY_SUCCESS;
345 }
346 
OnPreferredOutputDeviceUpdated(const std::vector<sptr<AudioStandard::AudioDeviceDescriptor>> & desc)347 void AudioPreferDeviceChangeCallback::OnPreferredOutputDeviceUpdated(
348     const std::vector<sptr<AudioStandard::AudioDeviceDescriptor>> &desc)
349 {
350     AudioDevice device;
351     if (desc.size() == NO_DEVICE_VALID) {
352         TELEPHONY_LOGE("desc size is zero");
353         return;
354     }
355     switch (desc[0]->deviceType_) {
356         case AudioStandard::DEVICE_TYPE_BLUETOOTH_SCO:
357             device.deviceType = AudioDeviceType::DEVICE_BLUETOOTH_SCO;
358             if (memset_s(&device.address, kMaxAddressLen + 1, 0, kMaxAddressLen + 1) != EOK) {
359                 TELEPHONY_LOGE("memset_s address fail");
360                 return;
361             }
362             if (memcpy_s(device.address, kMaxAddressLen, desc[0]->macAddress_.c_str(),
363                 desc[0]->macAddress_.length()) != EOK) {
364                 TELEPHONY_LOGE("memcpy_s address fail");
365                 return;
366             }
367             DelayedSingleton<AudioDeviceManager>::GetInstance()->SetCurrentAudioDevice(device.deviceType);
368             break;
369         case AudioStandard::DEVICE_TYPE_EARPIECE:
370             device.deviceType = AudioDeviceType::DEVICE_EARPIECE;
371             DelayedSingleton<AudioDeviceManager>::GetInstance()->SetCurrentAudioDevice(device.deviceType);
372             break;
373         case AudioStandard::DEVICE_TYPE_SPEAKER:
374             device.deviceType = AudioDeviceType::DEVICE_SPEAKER;
375             DelayedSingleton<AudioDeviceManager>::GetInstance()->SetCurrentAudioDevice(device.deviceType);
376             break;
377         case AudioStandard::DEVICE_TYPE_WIRED_HEADSET:
378         case AudioStandard::DEVICE_TYPE_WIRED_HEADPHONES:
379         case AudioStandard::DEVICE_TYPE_USB_HEADSET:
380             device.deviceType = AudioDeviceType::DEVICE_WIRED_HEADSET;
381             DelayedSingleton<AudioDeviceManager>::GetInstance()->AddAudioDeviceList(
382                 "", AudioDeviceType::DEVICE_WIRED_HEADSET);
383             DelayedSingleton<AudioDeviceManager>::GetInstance()->SetCurrentAudioDevice(device.deviceType);
384             break;
385         default:
386             break;
387     }
388 }
389 } // namespace Telephony
390 } // namespace OHOS
391