• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 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 #ifndef TELEPHONY_AUDIO_MANAGER_H
17 #define TELEPHONY_AUDIO_MANAGER_H
18 
19 #include <mutex>
20 #include <set>
21 
22 #include "audio_device_manager.h"
23 #include "audio_proxy.h"
24 #include "audio_scene_processor.h"
25 #include "call_manager_inner_type.h"
26 #include "call_state_listener_base.h"
27 #include "ffrt.h"
28 #include "ring.h"
29 #include "singleton.h"
30 #include "sound.h"
31 #include "tone.h"
32 
33 namespace OHOS {
34 namespace Telephony {
35 class AudioControlManager : public CallStateListenerBase, public std::enable_shared_from_this<AudioControlManager> {
36     DECLARE_DELAYED_SINGLETON(AudioControlManager)
37 
38 public:
39     void Init();
40     void UnInit();
41     int32_t SetAudioDevice(const AudioDevice &device);
42     int32_t SetAudioDevice(const AudioDevice &device, bool isByUser);
43     bool PlayRingtone(); // plays the default ringtone
44     bool StopRingtone();
45     int32_t PlayRingback();
46     int32_t StopRingback();
47     int32_t PlayWaitingTone();
48     int32_t StopWaitingTone();
49     int32_t PlayDtmfTone(char str);
50     int32_t StopDtmfTone();
51     int32_t OnPostDialNextChar(char str);
52     int32_t PlayCallTone(ToneDescriptor type);
53     int32_t StopCallTone();
54     int32_t MuteRinger();
55     int32_t SetMute(bool on);
56     void SetVolumeAudible();
57     bool IsTonePlaying();
58     bool IsAudioActivated() const;
59     bool IsCurrentRinging() const;
60     bool IsActiveCallExists() const;
61     bool ShouldSwitchActive() const;
62     bool ShouldSwitchDialing() const;
63     bool ShouldSwitchAlerting() const;
64     bool ShouldSwitchIncoming() const;
65     AudioDeviceType GetInitAudioDeviceType() const;
66     std::set<sptr<CallBase>> GetCallList();
67     sptr<CallBase> GetCurrentActiveCall();
68     AudioInterruptState GetAudioInterruptState();
69     bool UpdateCurrentCallState();
70     void SetRingState(RingState state);
71     void SetSoundState(SoundState state);
72     void SetToneState(ToneState state);
73     void SetLocalRingbackNeeded(bool isNeeded);
74     void NewCallCreated(sptr<CallBase> &callObjectPtr) override;
75     void CallDestroyed(const DisconnectedDetails &details) override;
76     void IncomingCallActivated(sptr<CallBase> &callObjectPtr) override;
77     void IncomingCallHungUp(sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content) override;
78     void CallStateUpdated(sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState) override;
79     void VideoStateUpdated(
80         sptr<CallBase> &callObjectPtr, VideoStateType priorVideoState, VideoStateType nextVideoState);
81     void CheckTypeAndSetAudioDevice(sptr<CallBase> &callObjectPtr, VideoStateType priorVideoState,
82         VideoStateType nextVideoState, AudioDeviceType &initDeviceType, AudioDevice &device);
83     void UpdateDeviceTypeForVideoOrSatelliteCall();
84     void UpdateDeviceTypeForCrs(AudioDeviceType deviceType);
85     void UpdateDeviceTypeForVideoDialing();
86     void MuteNetWorkRingTone(bool isMute = true);
87     bool IsVideoCall(VideoStateType videoState);
88     bool IsSoundPlaying();
89     bool StopSoundtone();
90     bool PlaySoundtone();
91     void PlayCallEndedTone(CallEndedType type);
92     void HandleNotNormalRingerMode(CallEndedType type);
93     bool IsDistributeCallSinkStatus();
94     void SetRingToneVolume(float volume);
95     bool IsScoTemporarilyDisabled();
96     void ExcludeBluetoothSco();
97     void UnexcludeBluetoothSco();
98     bool PlayForNoRing();
99     bool StopForNoRing();
100     bool NeedPlayVideoRing(ContactInfo &contactInfo, sptr<CallBase> &callObjectPtr);
101 private:
102     RingState ringState_ = RingState::STOPPED;
103     void HandleNextState(sptr<CallBase> &callObjectPtr, TelCallState nextState);
104     void HandlePriorState(sptr<CallBase> &callObjectPtr, TelCallState priorState);
105     void HandleCallStateUpdatedForVoip(sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState);
106     void HandleCallStateUpdated(sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState);
107     void HandleNewActiveCall(sptr<CallBase> &callObjectPtr);
108     bool IsNumberAllowed(const std::string &phoneNum);
109     sptr<CallBase> GetCallBase(int32_t callId);
110     AudioInterruptState audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_DEACTIVATED;
111     bool ShouldPlayRingtone() const;
112     bool dealCrsScene(const AudioStandard::AudioRingerMode &ringMode);
113     bool IsEmergencyCallExists();
114     void UpdateForegroundLiveCall();
115     bool IsBtOrWireHeadPlugin();
116     void ProcessAudioWhenCallActive(sptr<CallBase> &callObjectPtr);
117     void ResumeCrsSoundTone();
118     int32_t HandleDistributeAudioDevice(const AudioDevice &device);
119     int32_t HandleWirelessAudioDevice(const AudioDevice &device);
120     void GetWirelessAudioDeviceAddress(AudioDeviceType deviceType, std::string &address);
121     void SendMuteRingEvent();
122     bool IsRingingVibrateModeOn();
123     bool IsVoIPCallActived();
124     int32_t SwitchAudioDevice(AudioDeviceType audioDeviceType);
125     bool IsBtCallDisconnected();
126     void AdjustVolumesForCrs();
127     void SaveVoiceVolume(int32_t volume);
128     int32_t GetBackupVoiceVolume();
129     void RestoreVoiceValumeIfNecessary();
130     void PostProcessRingtone();
131     bool IsExternalAudioDevice(AudioDeviceType initDeviceType);
132     bool IsInEarAudioDevice(AudioDeviceType initDeviceType);
133     void UnmuteSoundTone();
134     void ProcessSoundtone(sptr<CallBase> &callObjectPtr);
135     bool IsSystemVideoRing(sptr<CallBase> &callObjectPtr);
136     ToneState toneState_ = ToneState::STOPPED;
137     SoundState soundState_ = SoundState::STOPPED;
138     bool isLocalRingbackNeeded_ = false;
139     bool isCrsVibrating_ = false;
140     bool isCrsStartSoundTone_ = false;
141     bool isVideoRingVibrating_ = false;
142     std::set<sptr<CallBase>> totalCalls_;
143     std::unique_ptr<Ring> ring_;
144     std::unique_ptr<Tone> tone_;
145     std::unique_ptr<Sound> sound_;
146     std::mutex mutex_;
147     std::recursive_mutex toneStateLock_;
148     sptr<CallBase> frontCall_ = nullptr;
149     bool isSetAudioDeviceByUser_ = false;
150     bool isScoTemporarilyDisabled_ = false;
151     int32_t voiceVolume_ = -1;
152     AudioDeviceType initCrsDeviceType_ = AudioDeviceType::DEVICE_UNKNOWN;
153     ffrt::mutex crsMutex_{};
154     std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_ = nullptr;
155     bool isPlayForNoRing_ = false;
156 };
157 } // namespace Telephony
158 } // namespace OHOS
159 #endif // TELEPHONY_AUDIO_MANAGER_H
160