1 /*
2  * Copyright (C) 2021 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_PROXY_H
17 #define TELEPHONY_AUDIO_PROXY_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 
23 #include "audio_manager_proxy.h"
24 #include "audio_system_manager.h"
25 #include "call_manager_errors.h"
26 #include "call_manager_inner_type.h"
27 #include "singleton.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 constexpr uint16_t VOLUME_AUDIBLE_DIVISOR = 2;
32 
33 enum AudioInterruptState {
34     INTERRUPT_STATE_UNKNOWN = 0,
35     INTERRUPT_STATE_DEACTIVATED,
36     INTERRUPT_STATE_ACTIVATED,
37     INTERRUPT_STATE_RINGING,
38 };
39 
40 enum class VibrationType {
41     VIBRATION_RINGTONE = 0,
42 };
43 
44 class AudioDeviceChangeCallback : public AudioStandard::AudioManagerDeviceChangeCallback {
45     void OnDeviceChange(const AudioStandard::DeviceChangeAction &deviceChangeAction) override;
46 };
47 
48 class AudioPreferDeviceChangeCallback : public AudioStandard::AudioPreferredOutputDeviceChangeCallback {
49 public:
50     void OnPreferredOutputDeviceUpdated(const std::vector<sptr<AudioStandard::AudioDeviceDescriptor>> &desc) override;
51 };
52 
53 class AudioMicStateChangeCallback : public AudioStandard::AudioManagerMicStateChangeCallback {
54 public:
55     void OnMicStateUpdated(const AudioStandard::MicStateChangeEvent &micStateChangeEvent) override;
56 };
57 
58 class AudioProxy : public std::enable_shared_from_this<AudioProxy> {
59     DECLARE_DELAYED_SINGLETON(AudioProxy)
60 public:
61     bool SetAudioScene(AudioStandard::AudioScene audioScene);
62     int32_t ActivateAudioInterrupt(const AudioStandard::AudioInterrupt &audioInterrupt);
63     int32_t DeactivateAudioInterrupt(const AudioStandard::AudioInterrupt &audioInterrupt);
64     int32_t DeactivateAudioInterrupt();
65     void SetVolumeAudible();
66     bool IsMicrophoneMute();
67     int32_t StartVibrator();
68     int32_t StopVibrator();
69     bool SetMicrophoneMute(bool mute);
70     bool SetEarpieceDevActive();
71     bool SetSpeakerDevActive();
72     bool SetBluetoothDevActive();
73     bool SetWiredHeadsetDevActive();
74     AudioStandard::AudioRingerMode GetRingerMode() const;
75     int32_t GetVolume(AudioStandard::AudioVolumeType audioVolumeType);
76     int32_t SetVolume(AudioStandard::AudioVolumeType audioVolumeType, int32_t volume);
77     int32_t SetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType);
78     bool IsStreamActive(AudioStandard::AudioVolumeType audioVolumeType);
79     bool IsStreamMute(AudioStandard::AudioVolumeType audioVolumeType);
80     int32_t GetMaxVolume(AudioStandard::AudioVolumeType audioVolumeType);
81     int32_t GetMinVolume(AudioStandard::AudioVolumeType audioVolumeType);
82     int32_t SetAudioDeviceChangeCallback();
83     std::string GetDefaultTonePath() const;
84     std::string GetDefaultDtmfPath() const;
85     int32_t UnsetDeviceChangeCallback();
86     void SetWiredHeadsetState(bool isConnected);
87     int32_t GetPreferredOutputAudioDevice(AudioDevice &device);
88     int32_t SetAudioPreferDeviceChangeCallback();
89     int32_t UnsetAudioPreferDeviceChangeCallback();
90     int32_t SetAudioMicStateChangeCallback();
91     int32_t UnsetAudioMicStateChangeCallback();
92 
93 private:
94     const std::string defaultTonePath_ = "/system/etc/telephony/tones/tone.wav";
95     const std::string defaultDtmfPath_ = "/system/etc/telephony/dtmfs/dtmf.wav";
96     std::shared_ptr<AudioStandard::AudioManagerDeviceChangeCallback> deviceCallback_;
97     std::shared_ptr<AudioStandard::AudioPreferredOutputDeviceChangeCallback> preferredDeviceCallback_;
98     std::shared_ptr<AudioStandard::AudioManagerMicStateChangeCallback> audioMicStateChangeCallback_;
99     bool isWiredHeadsetConnected_ = false;
100 };
101 } // namespace Telephony
102 } // namespace OHOS
103 #endif // TELEPHONY_AUDIO_PROXY_H
104