• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 
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 }
AudioSceneProcessor()38 AudioSceneProcessor::AudioSceneProcessor()
39     : currentState_(nullptr)
40 {}
41 
~AudioSceneProcessor()42 AudioSceneProcessor::~AudioSceneProcessor() {}
43 
Init()44 int32_t AudioSceneProcessor::Init()
45 {
46     memberFuncMap_[AudioEvent::SWITCH_DIALING_STATE] = [this]() { return SwitchDialing(); };
47     memberFuncMap_[AudioEvent::SWITCH_ALERTING_STATE] = [this]() { return SwitchAlerting(); };
48     memberFuncMap_[AudioEvent::SWITCH_INCOMING_STATE] = [this]() { return SwitchIncoming(); };
49     memberFuncMap_[AudioEvent::SWITCH_CS_CALL_STATE] = [this]() { return SwitchCS(); };
50     memberFuncMap_[AudioEvent::SWITCH_IMS_CALL_STATE] = [this]() { return SwitchIMS(); };
51     memberFuncMap_[AudioEvent::SWITCH_HOLDING_STATE] = [this]() { return SwitchHolding(); };
52     memberFuncMap_[AudioEvent::SWITCH_AUDIO_INACTIVE_STATE] = [this]() { return SwitchInactive(); };
53     currentState_ = std::make_unique<InActiveState>();
54     if (currentState_ == nullptr) {
55         TELEPHONY_LOGE("current call state nullptr");
56         return TELEPHONY_ERR_LOCAL_PTR_NULL;
57     }
58     return TELEPHONY_SUCCESS;
59 }
60 
ProcessEventInner(AudioEvent event)61 void AudioSceneProcessor::ProcessEventInner(AudioEvent event)
62 {
63     if (currentState_ == nullptr) {
64         TELEPHONY_LOGE("current call state nullptr");
65         return;
66     }
67     TELEPHONY_LOGI("process event inner, event: %{public}d.", event);
68     switch (event) {
69         case AudioEvent::SWITCH_DIALING_STATE:
70         case AudioEvent::SWITCH_ALERTING_STATE:
71         case AudioEvent::SWITCH_INCOMING_STATE:
72         case AudioEvent::SWITCH_CS_CALL_STATE:
73         case AudioEvent::SWITCH_IMS_CALL_STATE:
74         case AudioEvent::SWITCH_HOLDING_STATE:
75         case AudioEvent::SWITCH_AUDIO_INACTIVE_STATE:
76             if (DelayedSingleton<CallStateProcessor>::GetInstance()->ShouldStopSoundtone()) {
77                 DelayedSingleton<AudioControlManager>::GetInstance()->StopSoundtone();
78             }
79             SwitchState(event);
80             break;
81         case AudioEvent::NO_MORE_INCOMING_CALL:
82             DelayedSingleton<AudioControlManager>::GetInstance()->StopRingtone();
83             DelayedSingleton<AudioControlManager>::GetInstance()->StopWaitingTone();
84             currentState_->ProcessEvent(event);
85             break;
86         case AudioEvent::NO_MORE_ACTIVE_CALL:
87         case AudioEvent::NO_MORE_ALERTING_CALL:
88             DelayedSingleton<AudioControlManager>::GetInstance()->StopRingback();
89             if (DelayedSingleton<CallStateProcessor>::GetInstance()->ShouldStopSoundtone()) {
90                 DelayedSingleton<AudioControlManager>::GetInstance()->
91                     PlayCallEndedTone(CallEndedType::CALL_ENDED_NORMALLY);
92             }
93             currentState_->ProcessEvent(event);
94             break;
95         case AudioEvent::NO_MORE_DIALING_CALL:
96         case AudioEvent::NO_MORE_HOLDING_CALL:
97             if (DelayedSingleton<CallStateProcessor>::GetInstance()->ShouldStopSoundtone()) {
98                 DelayedSingleton<AudioControlManager>::GetInstance()->
99                     PlayCallEndedTone(CallEndedType::CALL_ENDED_NORMALLY);
100             }
101             currentState_->ProcessEvent(event);
102             break;
103         case AudioEvent::NEW_ACTIVE_CS_CALL:
104         case AudioEvent::NEW_ACTIVE_IMS_CALL:
105         case AudioEvent::NEW_DIALING_CALL:
106         case AudioEvent::NEW_ALERTING_CALL:
107         case AudioEvent::NEW_INCOMING_CALL:
108             currentState_->ProcessEvent(event);
109             break;
110         default:
111             break;
112     }
113 }
114 
ProcessEvent(AudioEvent event)115 bool AudioSceneProcessor::ProcessEvent(AudioEvent event)
116 {
117     reportAudioStateChangeQueue.submit([=]() { ProcessEventInner(event); });
118     return true;
119 }
120 
SwitchState(AudioEvent event)121 bool AudioSceneProcessor::SwitchState(AudioEvent event)
122 {
123     auto itFunc = memberFuncMap_.find(event);
124     if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
125         auto memberFunc = itFunc->second;
126         return memberFunc();
127     }
128     return false;
129 }
130 
SwitchState(CallStateType stateType)131 bool AudioSceneProcessor::SwitchState(CallStateType stateType)
132 {
133     TELEPHONY_LOGI("switch state, stateType: %{public}d.", stateType);
134     bool result = false;
135     std::lock_guard<std::mutex> lock(mutex_);
136     switch (stateType) {
137         case CallStateType::DIALING_STATE:
138             result = SwitchDialing();
139             break;
140         case CallStateType::ALERTING_STATE:
141             result = SwitchAlerting();
142             break;
143         case CallStateType::INCOMING_STATE:
144             result = SwitchIncoming();
145             break;
146         case CallStateType::CS_CALL_STATE:
147             result = SwitchCS();
148             break;
149         case CallStateType::IMS_CALL_STATE:
150             result = SwitchIMS();
151             break;
152         case CallStateType::HOLDING_STATE:
153             result = SwitchHolding();
154             break;
155         case CallStateType::INACTIVE_STATE:
156             result = SwitchInactive();
157             break;
158         default:
159             break;
160     }
161     TELEPHONY_LOGI("switch call state lock release");
162     return result;
163 }
164 
SwitchDialing()165 bool AudioSceneProcessor::SwitchDialing()
166 {
167     currentState_ = std::make_unique<DialingState>();
168     if (currentState_ == nullptr) {
169         TELEPHONY_LOGE("make_unique DialingState failed");
170         return false;
171     }
172     if (!DelayedSingleton<AudioControlManager>::GetInstance()->PlaySoundtone()) {
173         TELEPHONY_LOGE("PlaySoundtone fail");
174     }
175     DelayedSingleton<AudioControlManager>::GetInstance()->UpdateDeviceTypeForVideoDialing();
176     if (!DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_ACTIVATED)) {
177         TELEPHONY_LOGE("ProcessEvent AUDIO_ACTIVATED failed");
178     }
179     TELEPHONY_LOGI("current call state : dialing state");
180     return true;
181 }
182 
SwitchAlerting()183 bool AudioSceneProcessor::SwitchAlerting()
184 {
185     currentState_ = std::make_unique<AlertingState>();
186     if (currentState_ == nullptr) {
187         TELEPHONY_LOGE("make_unique AlertingState failed");
188         return false;
189     }
190     // play ringback tone while alerting state
191     DelayedSingleton<AudioControlManager>::GetInstance()->PlayRingback();
192     TELEPHONY_LOGI("current call state : alerting state");
193     return true;
194 }
195 
SwitchIncoming()196 bool AudioSceneProcessor::SwitchIncoming()
197 {
198     currentState_ = std::make_unique<IncomingState>();
199     if (currentState_ == nullptr) {
200         TELEPHONY_LOGE("make_unique IncomingState failed");
201         return false;
202     }
203     int32_t state;
204     DelayedSingleton<CallControlManager>::GetInstance()->GetVoIPCallState(state);
205     if (state == (int32_t) CallStateToApp::CALL_STATE_OFFHOOK) {
206         DelayedSingleton<AudioControlManager>::GetInstance()->PlayWaitingTone();
207     } else {
208         bool isStartBroadcast = CallVoiceAssistantManager::GetInstance()->IsStartVoiceBroadcast();
209         bool isNeedSilent = CallObjectManager::IsNeedSilentInDoNotDisturbMode();
210         if (!isStartBroadcast && !isNeedSilent) {
211             TELEPHONY_LOGI("broadcast switch and doNotDisturbMode close, start play system ring");
212             DelayedSingleton<AudioControlManager>::GetInstance()->StopRingtone();
213             // play ringtone while incoming state
214             DelayedSingleton<AudioControlManager>::GetInstance()->PlayRingtone();
215         } else {
216             TELEPHONY_LOGI("isStartBroadcast: %{public}d, isNeedSilent: %{public}d", isStartBroadcast, isNeedSilent);
217         }
218         DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_RINGING);
219     }
220     TELEPHONY_LOGI("current call state : incoming state");
221     return true;
222 }
223 
SwitchCS()224 bool AudioSceneProcessor::SwitchCS()
225 {
226     currentState_ = std::make_unique<CSCallState>();
227     if (currentState_ == nullptr) {
228         TELEPHONY_LOGE("make_unique CSCallState failed");
229         return false;
230     }
231     TELEPHONY_LOGI("current call state : cs call state");
232     return true;
233 }
234 
SwitchIMS()235 bool AudioSceneProcessor::SwitchIMS()
236 {
237     currentState_ = std::make_unique<IMSCallState>();
238     if (currentState_ == nullptr) {
239         TELEPHONY_LOGE("make_unique IMSCallState failed");
240         return false;
241     }
242     TELEPHONY_LOGI("current call state : ims call state");
243     return true;
244 }
245 
SwitchHolding()246 bool AudioSceneProcessor::SwitchHolding()
247 {
248     currentState_ = std::make_unique<HoldingState>();
249     if (currentState_ == nullptr) {
250         TELEPHONY_LOGE("make_unique HoldingState failed");
251         return false;
252     }
253     TELEPHONY_LOGI("current call state : holding state");
254     return true;
255 }
256 
SwitchInactive()257 bool AudioSceneProcessor::SwitchInactive()
258 {
259     DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_DEACTIVATED);
260     currentState_ = std::make_unique<InActiveState>();
261     if (currentState_ == nullptr) {
262         TELEPHONY_LOGE("make_unique InActiveState failed");
263         return false;
264     }
265     TELEPHONY_LOGI("current call state : inactive state");
266     return true;
267 }
268 
SwitchOTT()269 bool AudioSceneProcessor::SwitchOTT()
270 {
271     return true;
272 }
273 } // namespace Telephony
274 } // namespace OHOS