1 /* 2 * Copyright (C) 2021-2022 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_scene_processor.h" 17 18 #include "dialing_state.h" 19 #include "alerting_state.h" 20 #include "incoming_state.h" 21 #include "cs_call_state.h" 22 #include "holding_state.h" 23 #include "ims_call_state.h" 24 #include "inactive_state.h" 25 #include "audio_control_manager.h" 26 27 #include "telephony_log_wrapper.h" 28 29 namespace OHOS { 30 namespace Telephony { AudioSceneProcessor()31AudioSceneProcessor::AudioSceneProcessor() 32 : currentAudioScene_(AudioStandard::AudioScene::AUDIO_SCENE_DEFAULT), currentState_(nullptr) 33 {} 34 ~AudioSceneProcessor()35AudioSceneProcessor::~AudioSceneProcessor() {} 36 Init()37int32_t AudioSceneProcessor::Init() 38 { 39 memberFuncMap_[AudioEvent::SWITCH_DIALING_STATE] = &AudioSceneProcessor::SwitchDialing; 40 memberFuncMap_[AudioEvent::SWITCH_ALERTING_STATE] = &AudioSceneProcessor::SwitchAlerting; 41 memberFuncMap_[AudioEvent::SWITCH_INCOMING_STATE] = &AudioSceneProcessor::SwitchIncoming; 42 memberFuncMap_[AudioEvent::SWITCH_CS_CALL_STATE] = &AudioSceneProcessor::SwitchCS; 43 memberFuncMap_[AudioEvent::SWITCH_IMS_CALL_STATE] = &AudioSceneProcessor::SwitchIMS; 44 memberFuncMap_[AudioEvent::SWITCH_HOLDING_STATE] = &AudioSceneProcessor::SwitchHolding; 45 memberFuncMap_[AudioEvent::SWITCH_AUDIO_INACTIVE_STATE] = &AudioSceneProcessor::SwitchInactive; 46 currentState_ = std::make_unique<InActiveState>(); 47 if (currentState_ == nullptr) { 48 TELEPHONY_LOGE("current call state nullptr"); 49 return TELEPHONY_ERR_LOCAL_PTR_NULL; 50 } 51 return TELEPHONY_SUCCESS; 52 } 53 ProcessEvent(AudioEvent event)54bool AudioSceneProcessor::ProcessEvent(AudioEvent event) 55 { 56 if (currentState_ == nullptr) { 57 TELEPHONY_LOGE("current call state nullptr"); 58 return false; 59 } 60 bool result = false; 61 switch (event) { 62 case AudioEvent::SWITCH_DIALING_STATE: 63 case AudioEvent::SWITCH_ALERTING_STATE: 64 case AudioEvent::SWITCH_INCOMING_STATE: 65 case AudioEvent::SWITCH_CS_CALL_STATE: 66 case AudioEvent::SWITCH_IMS_CALL_STATE: 67 case AudioEvent::SWITCH_HOLDING_STATE: 68 case AudioEvent::SWITCH_AUDIO_INACTIVE_STATE: 69 result = SwitchState(event); 70 break; 71 case AudioEvent::NEW_ACTIVE_CS_CALL: 72 case AudioEvent::NEW_ACTIVE_IMS_CALL: 73 case AudioEvent::NEW_DIALING_CALL: 74 case AudioEvent::NEW_ALERTING_CALL: 75 case AudioEvent::NEW_INCOMING_CALL: 76 case AudioEvent::NO_MORE_ACTIVE_CALL: 77 case AudioEvent::NO_MORE_DIALING_CALL: 78 case AudioEvent::NO_MORE_ALERTING_CALL: 79 case AudioEvent::NO_MORE_INCOMING_CALL: 80 result = currentState_->ProcessEvent(event); 81 break; 82 default: 83 break; 84 } 85 return result; 86 } 87 SwitchState(AudioEvent event)88bool AudioSceneProcessor::SwitchState(AudioEvent event) 89 { 90 auto itFunc = memberFuncMap_.find(event); 91 if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) { 92 auto memberFunc = itFunc->second; 93 return (this->*memberFunc)(); 94 } 95 return false; 96 } 97 SwitchState(CallStateType stateType)98bool AudioSceneProcessor::SwitchState(CallStateType stateType) 99 { 100 bool result = false; 101 std::lock_guard<std::mutex> lock(mutex_); 102 switch (stateType) { 103 case CallStateType::DIALING_STATE: 104 result = SwitchDialing(); 105 break; 106 case CallStateType::ALERTING_STATE: 107 result = SwitchAlerting(); 108 break; 109 case CallStateType::INCOMING_STATE: 110 result = SwitchIncoming(); 111 break; 112 case CallStateType::CS_CALL_STATE: 113 result = SwitchCS(); 114 break; 115 case CallStateType::IMS_CALL_STATE: 116 result = SwitchIMS(); 117 break; 118 case CallStateType::HOLDING_STATE: 119 result = SwitchHolding(); 120 break; 121 case CallStateType::INACTIVE_STATE: 122 result = SwitchInactive(); 123 break; 124 default: 125 break; 126 } 127 TELEPHONY_LOGI("switch call state lock release"); 128 return result; 129 } 130 SwitchDialing()131bool AudioSceneProcessor::SwitchDialing() 132 { 133 if (DelayedSingleton<AudioProxy>::GetInstance()->SetAudioScene( 134 AudioStandard::AudioScene::AUDIO_SCENE_PHONE_CALL)) { 135 currentState_ = std::make_unique<DialingState>(); 136 if (currentState_ == nullptr) { 137 TELEPHONY_LOGE("make_unique DialingState failed"); 138 return false; 139 } 140 // play ringback tone while dialing state 141 currentAudioScene_ = AudioStandard::AudioScene::AUDIO_SCENE_PHONE_CALL; 142 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_ACTIVATED); 143 TELEPHONY_LOGI("current call state : dialing state"); 144 return true; 145 } 146 return false; 147 } 148 SwitchAlerting()149bool AudioSceneProcessor::SwitchAlerting() 150 { 151 if (DelayedSingleton<AudioProxy>::GetInstance()->SetAudioScene( 152 AudioStandard::AudioScene::AUDIO_SCENE_PHONE_CALL)) { 153 currentState_ = std::make_unique<AlertingState>(); 154 if (currentState_ == nullptr) { 155 TELEPHONY_LOGE("make_unique AlertingState failed"); 156 return false; 157 } 158 // play ringback tone while alerting state 159 DelayedSingleton<AudioControlManager>::GetInstance()->PlayRingback(); 160 currentAudioScene_ = AudioStandard::AudioScene::AUDIO_SCENE_PHONE_CALL; 161 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_ACTIVATED); 162 TELEPHONY_LOGI("current call state : alerting state"); 163 return true; 164 } 165 return false; 166 } 167 SwitchIncoming()168bool AudioSceneProcessor::SwitchIncoming() 169 { 170 if (DelayedSingleton<AudioProxy>::GetInstance()->SetAudioScene(AudioStandard::AudioScene::AUDIO_SCENE_RINGING)) { 171 currentState_ = std::make_unique<IncomingState>(); 172 if (currentState_ == nullptr) { 173 TELEPHONY_LOGE("make_unique IncomingState failed"); 174 return false; 175 } 176 // play ringtone while incoming state 177 DelayedSingleton<AudioControlManager>::GetInstance()->PlayRingtone(); 178 currentAudioScene_ = AudioStandard::AudioScene::AUDIO_SCENE_RINGING; 179 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_RINGING); 180 TELEPHONY_LOGI("current call state : incoming state"); 181 return true; 182 } 183 return false; 184 } 185 SwitchCS()186bool AudioSceneProcessor::SwitchCS() 187 { 188 if (DelayedSingleton<AudioProxy>::GetInstance()->SetAudioScene( 189 AudioStandard::AudioScene::AUDIO_SCENE_PHONE_CALL)) { 190 currentState_ = std::make_unique<CSCallState>(); 191 if (currentState_ == nullptr) { 192 TELEPHONY_LOGE("make_unique CSCallState failed"); 193 return false; 194 } 195 currentAudioScene_ = AudioStandard::AudioScene::AUDIO_SCENE_PHONE_CALL; 196 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_ACTIVATED); 197 TELEPHONY_LOGI("current call state : cs call state"); 198 return true; 199 } 200 return false; 201 } 202 SwitchIMS()203bool AudioSceneProcessor::SwitchIMS() 204 { 205 if (DelayedSingleton<AudioProxy>::GetInstance()->SetAudioScene( 206 AudioStandard::AudioScene::AUDIO_SCENE_PHONE_CALL)) { 207 currentState_ = std::make_unique<IMSCallState>(); 208 if (currentState_ == nullptr) { 209 TELEPHONY_LOGE("make_unique IMSCallState failed"); 210 return false; 211 } 212 currentAudioScene_ = AudioStandard::AudioScene::AUDIO_SCENE_PHONE_CALL; 213 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_ACTIVATED); 214 TELEPHONY_LOGI("current call state : ims call state"); 215 return true; 216 } 217 return false; 218 } 219 SwitchHolding()220bool AudioSceneProcessor::SwitchHolding() 221 { 222 // stay at current audio scene while holding state 223 if (DelayedSingleton<AudioProxy>::GetInstance()->SetAudioScene(currentAudioScene_)) { 224 currentState_ = std::make_unique<HoldingState>(); 225 if (currentState_ == nullptr) { 226 TELEPHONY_LOGE("make_unique HoldingState failed"); 227 return false; 228 } 229 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_ACTIVATED); 230 TELEPHONY_LOGI("current call state : holding state"); 231 return true; 232 } 233 return false; 234 } 235 SwitchInactive()236bool AudioSceneProcessor::SwitchInactive() 237 { 238 if (DelayedSingleton<AudioProxy>::GetInstance()->SetAudioScene(AudioStandard::AudioScene::AUDIO_SCENE_DEFAULT)) { 239 currentState_ = std::make_unique<InActiveState>(); 240 if (currentState_ == nullptr) { 241 TELEPHONY_LOGE("make_unique InActiveState failed"); 242 return false; 243 } 244 currentAudioScene_ = AudioStandard::AudioScene::AUDIO_SCENE_DEFAULT; 245 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_DEACTIVATED); 246 TELEPHONY_LOGI("current call state : inactive state"); 247 return true; 248 } 249 return false; 250 } 251 SwitchOTT()252bool AudioSceneProcessor::SwitchOTT() 253 { 254 return true; 255 } 256 } // namespace Telephony 257 } // namespace OHOS