• 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 
27 #include "telephony_log_wrapper.h"
28 
29 namespace OHOS {
30 namespace Telephony {
AudioSceneProcessor()31 AudioSceneProcessor::AudioSceneProcessor()
32     : currentState_(nullptr)
33 {}
34 
~AudioSceneProcessor()35 AudioSceneProcessor::~AudioSceneProcessor() {}
36 
Init()37 int32_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)54 bool 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)88 bool 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)98 bool 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()131 bool AudioSceneProcessor::SwitchDialing()
132 {
133     if (!DelayedSingleton<AudioControlManager>::GetInstance()->PlaySoundtone()) {
134         TELEPHONY_LOGE("PlaySoundtone fail");
135         return false;
136     }
137     currentState_ = std::make_unique<DialingState>();
138     if (currentState_ == nullptr) {
139         TELEPHONY_LOGE("make_unique DialingState failed");
140         return false;
141     }
142     DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_ACTIVATED);
143     TELEPHONY_LOGI("current call state : dialing state");
144     return true;
145 }
146 
SwitchAlerting()147 bool AudioSceneProcessor::SwitchAlerting()
148 {
149     currentState_ = std::make_unique<AlertingState>();
150     if (currentState_ == nullptr) {
151         TELEPHONY_LOGE("make_unique AlertingState failed");
152         return false;
153     }
154     // play ringback tone while alerting state
155     DelayedSingleton<AudioControlManager>::GetInstance()->PlayRingback();
156     TELEPHONY_LOGI("current call state : alerting state");
157     return true;
158 }
159 
SwitchIncoming()160 bool AudioSceneProcessor::SwitchIncoming()
161 {
162     currentState_ = std::make_unique<IncomingState>();
163     if (currentState_ == nullptr) {
164         TELEPHONY_LOGE("make_unique IncomingState failed");
165         return false;
166     }
167     // play ringtone while incoming state
168     DelayedSingleton<AudioControlManager>::GetInstance()->PlayRingtone();
169     DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_RINGING);
170     TELEPHONY_LOGI("current call state : incoming state");
171     return true;
172 }
173 
SwitchCS()174 bool AudioSceneProcessor::SwitchCS()
175 {
176     currentState_ = std::make_unique<CSCallState>();
177     if (currentState_ == nullptr) {
178         TELEPHONY_LOGE("make_unique CSCallState failed");
179         return false;
180     }
181     TELEPHONY_LOGI("current call state : cs call state");
182     return true;
183 }
184 
SwitchIMS()185 bool AudioSceneProcessor::SwitchIMS()
186 {
187     currentState_ = std::make_unique<IMSCallState>();
188     if (currentState_ == nullptr) {
189         TELEPHONY_LOGE("make_unique IMSCallState failed");
190         return false;
191     }
192     TELEPHONY_LOGI("current call state : ims call state");
193     return true;
194 }
195 
SwitchHolding()196 bool AudioSceneProcessor::SwitchHolding()
197 {
198     currentState_ = std::make_unique<HoldingState>();
199     if (currentState_ == nullptr) {
200         TELEPHONY_LOGE("make_unique HoldingState failed");
201         return false;
202     }
203     TELEPHONY_LOGI("current call state : holding state");
204     return true;
205 }
206 
SwitchInactive()207 bool AudioSceneProcessor::SwitchInactive()
208 {
209     DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(AudioEvent::AUDIO_DEACTIVATED);
210     DelayedSingleton<AudioControlManager>::GetInstance()->StopSoundtone();
211     currentState_ = std::make_unique<InActiveState>();
212     if (currentState_ == nullptr) {
213         TELEPHONY_LOGE("make_unique InActiveState failed");
214         return false;
215     }
216     TELEPHONY_LOGI("current call state : inactive state");
217     return true;
218 }
219 
SwitchOTT()220 bool AudioSceneProcessor::SwitchOTT()
221 {
222     return true;
223 }
224 } // namespace Telephony
225 } // namespace OHOS