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 "ring.h"
28#include "singleton.h"
29#include "sound.h"
30#include "tone.h"
31
32namespace OHOS {
33namespace Telephony {
34class AudioControlManager : public CallStateListenerBase, public std::enable_shared_from_this<AudioControlManager> {
35    DECLARE_DELAYED_SINGLETON(AudioControlManager)
36
37public:
38    void Init();
39    int32_t SetAudioDevice(const AudioDevice &device);
40    int32_t SetAudioDevice(const AudioDevice &device, bool isByUser);
41    bool PlayRingtone(); // plays the default ringtone
42    bool StopRingtone();
43    int32_t PlayRingback();
44    int32_t StopRingback();
45    int32_t PlayWaitingTone();
46    int32_t StopWaitingTone();
47    int32_t PlayDtmfTone(char str);
48    int32_t StopDtmfTone();
49    int32_t OnPostDialNextChar(char str);
50    int32_t PlayCallTone(ToneDescriptor type);
51    int32_t StopCallTone();
52    int32_t MuteRinger();
53    int32_t SetMute(bool on);
54    void SetVolumeAudible();
55    bool IsTonePlaying();
56    bool IsAudioActivated() const;
57    bool IsCurrentRinging() const;
58    bool IsActiveCallExists() const;
59    bool ShouldSwitchActive() const;
60    bool ShouldSwitchDialing() const;
61    bool ShouldSwitchAlerting() const;
62    bool ShouldSwitchIncoming() const;
63    AudioDeviceType GetInitAudioDeviceType() const;
64    std::set<sptr<CallBase>> GetCallList();
65    sptr<CallBase> GetCurrentActiveCall();
66    AudioInterruptState GetAudioInterruptState();
67    bool UpdateCurrentCallState();
68    void SetRingState(RingState state);
69    void SetSoundState(SoundState state);
70    void SetToneState(ToneState state);
71    void SetLocalRingbackNeeded(bool isNeeded);
72    void NewCallCreated(sptr<CallBase> &callObjectPtr) override;
73    void CallDestroyed(const DisconnectedDetails &details) override;
74    void IncomingCallActivated(sptr<CallBase> &callObjectPtr) override;
75    void IncomingCallHungUp(sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content) override;
76    void CallStateUpdated(sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState) override;
77    void VideoStateUpdated(
78        sptr<CallBase> &callObjectPtr, VideoStateType priorVideoState, VideoStateType nextVideoState);
79    void CheckTypeAndSetAudioDevice(sptr<CallBase> &callObjectPtr, VideoStateType priorVideoState,
80        VideoStateType nextVideoState, AudioDeviceType &initDeviceType, AudioDevice &device);
81    void UpdateDeviceTypeForVideoOrSatelliteCall();
82    void UpdateDeviceTypeForCrs();
83    void UpdateDeviceTypeForVideoDialing();
84    void MuteNetWorkRingTone();
85    bool IsVideoCall(VideoStateType videoState);
86    bool IsSoundPlaying();
87    bool StopSoundtone();
88    bool PlaySoundtone();
89    void PlayCallEndedTone(CallEndedType type);
90
91private:
92    RingState ringState_ = RingState::STOPPED;
93    void HandleNextState(sptr<CallBase> &callObjectPtr, TelCallState nextState);
94    void HandlePriorState(sptr<CallBase> &callObjectPtr, TelCallState priorState);
95    void HandleCallStateUpdatedForVoip(sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState);
96    void HandleCallStateUpdated(sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState);
97    void HandleNewActiveCall(sptr<CallBase> &callObjectPtr);
98    bool IsNumberAllowed(const std::string &phoneNum);
99    sptr<CallBase> GetCallBase(int32_t callId);
100    AudioInterruptState audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_DEACTIVATED;
101    bool ShouldPlayRingtone() const;
102    bool IsEmergencyCallExists();
103    void UpdateForegroundLiveCall();
104    bool IsBtOrWireHeadPlugin();
105    void ProcessAudioWhenCallActive(sptr<CallBase> &callObjectPtr);
106    int32_t HandleDistributeAudioDevice(const AudioDevice &device);
107    void SendMuteRingEvent();
108    bool IsDistributeCallSinkStatus();
109    bool IsRingingVibrateModeOn();
110    int32_t SwitchAudioDevice(AudioDeviceType audioDeviceType);
111    ToneState toneState_ = ToneState::STOPPED;
112    SoundState soundState_ = SoundState::STOPPED;
113    bool isLocalRingbackNeeded_ = false;
114    bool isCrsVibrating_ = false;
115    std::set<sptr<CallBase>> totalCalls_;
116    std::unique_ptr<Ring> ring_;
117    std::unique_ptr<Tone> tone_;
118    std::unique_ptr<Sound> sound_;
119    std::mutex mutex_;
120    std::recursive_mutex toneStateLock_;
121    sptr<CallBase> frontCall_ = nullptr;
122    bool isSetAudioDeviceByUser_ = false;
123};
124} // namespace Telephony
125} // namespace OHOS
126#endif // TELEPHONY_AUDIO_MANAGER_H
127