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