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