• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2025 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 "call_control_manager.h"
22 #include "cs_call_state.h"
23 #include "holding_state.h"
24 #include "ims_call_state.h"
25 #include "inactive_state.h"
26 #include "audio_control_manager.h"
27 #include "call_state_processor.h"
28 #include "ffrt.h"
29 #include "ffrt_inner.h"
30 #include "telephony_log_wrapper.h"
31 #include "call_voice_assistant_manager.h"
32 
33 namespace OHOS {
34 namespace Telephony {
35 namespace {
36     ffrt::queue reportAudioStateChangeQueue { "report_audio_state_change",
37         ffrt::queue_attr().qos(ffrt_qos_user_interactive)};
38 }
AudioSceneProcessor()39 AudioSceneProcessor::AudioSceneProcessor()
40     : currentState_(nullptr)
41 {}
42 
~AudioSceneProcessor()43 AudioSceneProcessor::~AudioSceneProcessor() {}
44 
Init()45 int32_t AudioSceneProcessor::Init()
46 {
47     memberFuncMap_[AudioEvent::SWITCH_DIALING_STATE] = [this]() { return SwitchDialing(); };
48     memberFuncMap_[AudioEvent::SWITCH_ALERTING_STATE] = [this]() { return SwitchAlerting(); };
49     memberFuncMap_[AudioEvent::SWITCH_INCOMING_STATE] = [this]() { return SwitchIncoming(); };
50     memberFuncMap_[AudioEvent::SWITCH_CS_CALL_STATE] = [this]() { return SwitchCS(); };
51     memberFuncMap_[AudioEvent::SWITCH_IMS_CALL_STATE] = [this]() { return SwitchIMS(); };
52     memberFuncMap_[AudioEvent::SWITCH_HOLDING_STATE] = [this]() { return SwitchHolding(); };
53     memberFuncMap_[AudioEvent::SWITCH_AUDIO_INACTIVE_STATE] = [this]() { return SwitchInactive(); };
54     currentState_ = std::make_unique<InActiveState>();
55     if (currentState_ == nullptr) {
56         TELEPHONY_LOGE("current call state nullptr");
57         return TELEPHONY_ERR_LOCAL_PTR_NULL;
58     }
59     return TELEPHONY_SUCCESS;
60 }
61 
ProcessEventInner(AudioEvent event)62 void AudioSceneProcessor::ProcessEventInner(AudioEvent event)
63 {
64     if (currentState_ == nullptr) {
65         TELEPHONY_LOGE("current call state nullptr");
66         return;
67     }
68     TELEPHONY_LOGI("process event inner, event: %{public}d.", event);
69     switch (event) {
70         case AudioEvent::SWITCH_DIALING_STATE:
71         case AudioEvent::SWITCH_ALERTING_STATE:
72         case AudioEvent::SWITCH_INCOMING_STATE:
73         case AudioEvent::SWITCH_CS_CALL_STATE:
74         case AudioEvent::SWITCH_IMS_CALL_STATE:
75         case AudioEvent::SWITCH_HOLDING_STATE:
76         case AudioEvent::SWITCH_AUDIO_INACTIVE_STATE:
77             if (DelayedSingleton<CallStateProcessor>::GetInstance()->ShouldStopSoundtone()) {
78                 DelayedSingleton<AudioControlManager>::GetInstance()->StopSoundtone();
79                 // Acquire disconnected lock in DisconnectedHandle when the remote hangup
80                 DelayedSingleton<CallControlManager>::GetInstance()->ReleaseDisconnectedLock();
81             }
82             SwitchState(event);
83             break;
84         case AudioEvent::NO_MORE_INCOMING_CALL:
85             DelayedSingleton<AudioControlManager>::GetInstance()->StopRingtone();
86             DelayedSingleton<AudioControlManager>::GetInstance()->StopWaitingTone();
87             currentState_->ProcessEvent(event);
88             break;
89         case AudioEvent::NO_MORE_ACTIVE_CALL:
90         case AudioEvent::NO_MORE_ALERTING_CALL:
91             DelayedSingleton<AudioControlManager>::GetInstance()->StopRingback();
92             if (DelayedSingleton<CallStateProcessor>::GetInstance()->ShouldStopSoundtone()) {
93                 DelayedSingleton<AudioControlManager>::GetInstance()->
94                     PlayCallEndedTone(CallEndedType::CALL_ENDED_NORMALLY);
95             }
96             currentState_->ProcessEvent(event);
97             break;
98         case AudioEvent::NO_MORE_DIALING_CALL:
99         case AudioEvent::NO_MORE_HOLDING_CALL:
100             if (DelayedSingleton<CallStateProcessor>::GetInstance()->ShouldStopSoundtone()) {
101                 DelayedSingleton<AudioControlManager>::GetInstance()->
102                     PlayCallEndedTone(CallEndedType::CALL_ENDED_NORMALLY);
103             }
104             currentState_->ProcessEvent(event);
105             break;
106         case AudioEvent::NEW_ACTIVE_CS_CALL:
107         case AudioEvent::NEW_ACTIVE_IMS_CALL:
108         case AudioEvent::NEW_DIALING_CALL:
109         case AudioEvent::NEW_ALERTING_CALL:
110         case AudioEvent::NEW_INCOMING_CALL:
111             currentState_->ProcessEvent(event);
112             break;
113         default:
114             break;
115     }
116 }
117 
ProcessEvent(AudioEvent event)118 bool AudioSceneProcessor::ProcessEvent(AudioEvent event)
119 {
120     reportAudioStateChangeQueue.submit([=]() { ProcessEventInner(event); });
121     return true;
122 }
123 
SwitchState(AudioEvent event)124 bool AudioSceneProcessor::SwitchState(AudioEvent event)
125 {
126     auto itFunc = memberFuncMap_.find(event);
127     if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
128         auto memberFunc = itFunc->second;
129         return memberFunc();
130     }
131     return false;
132 }
133 
SwitchState(CallStateType stateType)134 bool AudioSceneProcessor::SwitchState(CallStateType stateType)
135 {
136     TELEPHONY_LOGI("switch state, stateType: %{public}d.", stateType);
137     bool result = false;
138     std::lock_guard<std::mutex> lock(mutex_);
139     switch (stateType) {
140         case CallStateType::DIALING_STATE:
141             result = SwitchDialing();
142             break;
143         case CallStateType::ALERTING_STATE:
144             result = SwitchAlerting();
145             break;
146         case CallStateType::INCOMING_STATE:
147             result = SwitchIncoming();
148             break;
149         case CallStateType::CS_CALL_STATE:
150             result = SwitchCS();
151             break;
152         case CallStateType::IMS_CALL_STATE:
153             result = SwitchIMS();
154             break;
155         case CallStateType::HOLDING_STATE:
156             result = SwitchHolding();
157             break;
158         case CallStateType::INACTIVE_STATE:
159             result = SwitchInactive();
160             break;
161         default:
162             break;
163     }
164     TELEPHONY_LOGI("switch call state lock release");
165     return result;
166 }
167 
SwitchDialing()168 bool AudioSceneProcessor::SwitchDialing()
169 {
170     currentState_ = std::make_unique<DialingState>();
171     if (currentState_ == nullptr) {
172         TELEPHONY_LOGE("make_unique DialingState failed");
173         return false;
174     }
175     sptr<CallBase> call = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_DIALING);
176     if (call != nullptr && call->GetCallType() == CallType::TYPE_BLUETOOTH &&
177         !DelayedSingleton<AudioControlManager>::GetInstance()->IsScoTemporarilyDisabled()) {
178             DelayedSingleton<AudioControlManager>::GetInstance()->ExcludeBluetoothSco();
179     }
180     if (!DelayedSingleton<AudioControlManager>::GetInstance()->PlaySoundtone()) {
181         TELEPHONY_LOGE("PlaySoundtone fail");
182     }
183     DelayedSingleton<AudioControlManager>::GetInstance()->UpdateDeviceTypeForVideoDialing();
184     if (!DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_ACTIVATED)) {
185         TELEPHONY_LOGE("ProcessEvent AUDIO_ACTIVATED failed");
186     }
187     TELEPHONY_LOGI("current call state : dialing state");
188     return true;
189 }
190 
SwitchAlerting()191 bool AudioSceneProcessor::SwitchAlerting()
192 {
193     currentState_ = std::make_unique<AlertingState>();
194     if (currentState_ == nullptr) {
195         TELEPHONY_LOGE("make_unique AlertingState failed");
196         return false;
197     }
198     // play ringback tone while alerting state
199     DelayedSingleton<AudioControlManager>::GetInstance()->PlayRingback();
200     TELEPHONY_LOGI("current call state : alerting state");
201     return true;
202 }
203 
SwitchIncoming()204 bool AudioSceneProcessor::SwitchIncoming()
205 {
206     currentState_ = std::make_unique<IncomingState>();
207     if (currentState_ == nullptr) {
208         TELEPHONY_LOGE("make_unique IncomingState failed");
209         return false;
210     }
211     sptr<CallBase> call = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_RINGING);
212     if (call != nullptr && call->GetCallType() == CallType::TYPE_BLUETOOTH &&
213         !DelayedSingleton<AudioControlManager>::GetInstance()->IsScoTemporarilyDisabled()) {
214         DelayedSingleton<AudioControlManager>::GetInstance()->ExcludeBluetoothSco();
215     }
216     int32_t state;
217     DelayedSingleton<CallControlManager>::GetInstance()->GetVoIPCallState(state);
218     int endCallCount = CallObjectManager::GetCallNumByRunningState(CallRunningState::CALL_RUNNING_STATE_ENDED);
219     if (state == (int32_t) CallStateToApp::CALL_STATE_OFFHOOK ||
220         (CallObjectManager::GetCurrentCallNum() - endCallCount > ONE_CALL_EXIST &&
221             DelayedSingleton<AudioControlManager>::GetInstance()->IsSoundPlaying() &&
222             CallObjectManager::HasIncomingCallCrsType())) {
223         DelayedSingleton<AudioControlManager>::GetInstance()->PlayWaitingTone();
224     } else {
225         bool isStartBroadcast = CallVoiceAssistantManager::GetInstance()->IsStartVoiceBroadcast();
226         bool isNeedSilent = CallObjectManager::IsNeedSilentInDoNotDisturbMode();
227         bool isNotWearWatch = DelayedSingleton<CallControlManager>::GetInstance()->IsNotWearOnWrist();
228         if (!isStartBroadcast && !isNeedSilent && !isNotWearWatch) {
229             TELEPHONY_LOGI("broadcast switch and doNotDisturbMode close, start play system ring");
230             DelayedSingleton<AudioControlManager>::GetInstance()->StopRingtone();
231             // play ringtone while incoming state
232             DelayedSingleton<AudioControlManager>::GetInstance()->PlayRingtone();
233         } else {
234             TELEPHONY_LOGI("isStartBroadcast: %{public}d, isNeedSilent: %{public}d, isNotWearWatch: %{public}d",
235                 isStartBroadcast, isNeedSilent, isNotWearWatch);
236             if (system::GetParameter("const.product.devicetype", "") == "wearable") {
237                 DelayedSingleton<AudioControlManager>::GetInstance()->StopRingtone();
238                 DelayedSingleton<AudioControlManager>::GetInstance()->PlayForNoRing();
239             }
240         }
241         DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_RINGING);
242     }
243     TELEPHONY_LOGI("current call state : incoming state");
244     return true;
245 }
246 
SwitchCS()247 bool AudioSceneProcessor::SwitchCS()
248 {
249     currentState_ = std::make_unique<CSCallState>();
250     if (currentState_ == nullptr) {
251         TELEPHONY_LOGE("make_unique CSCallState failed");
252         return false;
253     }
254     TELEPHONY_LOGI("current call state : cs call state");
255     return true;
256 }
257 
SwitchIMS()258 bool AudioSceneProcessor::SwitchIMS()
259 {
260     currentState_ = std::make_unique<IMSCallState>();
261     if (currentState_ == nullptr) {
262         TELEPHONY_LOGE("make_unique IMSCallState failed");
263         return false;
264     }
265     TELEPHONY_LOGI("current call state : ims call state");
266     return true;
267 }
268 
SwitchHolding()269 bool AudioSceneProcessor::SwitchHolding()
270 {
271     currentState_ = std::make_unique<HoldingState>();
272     if (currentState_ == nullptr) {
273         TELEPHONY_LOGE("make_unique HoldingState failed");
274         return false;
275     }
276     TELEPHONY_LOGI("current call state : holding state");
277     return true;
278 }
279 
SwitchInactive()280 bool AudioSceneProcessor::SwitchInactive()
281 {
282     DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_DEACTIVATED);
283     currentState_ = std::make_unique<InActiveState>();
284     if (currentState_ == nullptr) {
285         TELEPHONY_LOGE("make_unique InActiveState failed");
286         return false;
287     }
288     if (DelayedSingleton<AudioControlManager>::GetInstance()->IsScoTemporarilyDisabled()) {
289         TELEPHONY_LOGI("handle UnexcludeBluetoothSco start");
290         DelayedSingleton<AudioControlManager>::GetInstance()->UnexcludeBluetoothSco();
291     }
292     TELEPHONY_LOGI("current call state : inactive state");
293     return true;
294 }
295 
SwitchOTT()296 bool AudioSceneProcessor::SwitchOTT()
297 {
298     return true;
299 }
300 } // namespace Telephony
301 } // namespace OHOS