1 /* 2 * Copyright (c) 2021-2023 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 AUDIO_BLUETOOTH_MANAGERI_H 17 #define AUDIO_BLUETOOTH_MANAGERI_H 18 19 #include "bluetooth_a2dp_src.h" 20 #include "bluetooth_a2dp_codec.h" 21 #include "bluetooth_avrcp_tg.h" 22 #include "bluetooth_hfp_ag.h" 23 #include "audio_info.h" 24 #include "bluetooth_device_utils.h" 25 #include "bluetooth_sco_manager.h" 26 27 namespace OHOS { 28 namespace Bluetooth { 29 class AudioA2dpPlayingStateChangedListener { 30 public: 31 virtual void OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState) = 0; 32 }; 33 34 // Audio bluetooth a2dp feature support 35 class AudioA2dpListener : public A2dpSourceObserver { 36 public: 37 AudioA2dpListener() = default; 38 virtual ~AudioA2dpListener() = default; 39 40 virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause); 41 virtual void OnConfigurationChanged(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info, int error); 42 virtual void OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error); 43 virtual void OnMediaStackChanged(const BluetoothRemoteDevice &device, int action); 44 virtual void OnVirtualDeviceChanged(int32_t action, std::string macAddress); 45 virtual void OnCaptureConnectionStateChanged(const BluetoothRemoteDevice &device, int state, 46 const A2dpCodecInfo &codecInfo); 47 48 private: 49 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AudioA2dpListener); 50 }; 51 52 class AudioA2dpManager { 53 public: 54 AudioA2dpManager() = default; 55 virtual ~AudioA2dpManager() = default; 56 static void RegisterBluetoothA2dpListener(); 57 static void UnregisterBluetoothA2dpListener(); 58 static void DisconnectBluetoothA2dpSink(); 59 static void DisconnectBluetoothA2dpSource(); 60 static int32_t SetActiveA2dpDevice(const std::string& macAddress); 61 static std::string GetActiveA2dpDevice(); 62 static int32_t SetDeviceAbsVolume(const std::string& macAddress, int32_t volume); 63 static int32_t GetA2dpDeviceStreamInfo(const std::string& macAddress, 64 AudioStandard::AudioStreamInfo &streamInfo); 65 static int32_t GetA2dpInDeviceStreamInfo(const std::string &macAddress, 66 AudioStandard::AudioStreamInfo &streamInfo); 67 static bool HasA2dpDeviceConnected(); 68 static void CheckA2dpDeviceReconnect(); 69 static int32_t A2dpOffloadSessionRequest(const std::vector<A2dpStreamInfo> &info); 70 static int32_t OffloadStartPlaying(const std::vector<int32_t> &sessionsID); 71 static int32_t OffloadStopPlaying(const std::vector<int32_t> &sessionsID); 72 static int32_t GetRenderPosition(uint32_t &delayValue, uint64_t &sendDataSize, uint32_t &timeStamp); 73 static int32_t RegisterA2dpPlayingStateChangedListener( 74 std::shared_ptr<AudioA2dpPlayingStateChangedListener> listener); 75 static void OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState); 76 static int32_t Connect(const std::string &macAddress); 77 SetConnectionState(int state)78 static void SetConnectionState(int state) 79 { 80 connectionState_ = state; 81 } GetConnectionState()82 static int GetConnectionState() 83 { 84 return connectionState_; 85 } SetCaptureConnectionState(int32_t state)86 static void SetCaptureConnectionState(int32_t state) 87 { 88 captureConnectionState_ = state; 89 } GetCaptureConnectionState()90 static int32_t GetCaptureConnectionState() 91 { 92 return captureConnectionState_; 93 } GetCurrentActiveA2dpDevice()94 static BluetoothRemoteDevice GetCurrentActiveA2dpDevice() 95 { 96 return activeA2dpDevice_; 97 } 98 99 private: 100 static A2dpSource *a2dpInstance_; 101 static std::shared_ptr<AudioA2dpListener> a2dpListener_; 102 static int connectionState_; 103 static int32_t captureConnectionState_; 104 static BluetoothRemoteDevice activeA2dpDevice_; 105 static std::vector<std::shared_ptr<AudioA2dpPlayingStateChangedListener>> a2dpPlayingStateChangedListeners_; 106 }; 107 108 // Audio bluetooth sco feature support 109 class AudioHfpListener : public HandsFreeAudioGatewayObserver { 110 public: 111 AudioHfpListener() = default; 112 virtual ~AudioHfpListener() = default; 113 114 void OnScoStateChanged(const BluetoothRemoteDevice &device, int state, int reason); 115 void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause); OnActiveDeviceChanged(const BluetoothRemoteDevice & device)116 void OnActiveDeviceChanged(const BluetoothRemoteDevice &device) {} OnHfEnhancedDriverSafetyChanged(const BluetoothRemoteDevice & device,int indValue)117 void OnHfEnhancedDriverSafetyChanged(const BluetoothRemoteDevice &device, int indValue) {} 118 void OnVirtualDeviceChanged(int32_t action, std::string macAddress); 119 virtual void OnHfpStackChanged(const BluetoothRemoteDevice &device, int action); 120 121 private: 122 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AudioHfpListener); 123 }; 124 125 class AudioHfpManager { 126 public: 127 AudioHfpManager() = default; 128 virtual ~AudioHfpManager() = default; 129 static void RegisterBluetoothScoListener(); 130 static void UnregisterBluetoothScoListener(); 131 static int32_t SetActiveHfpDevice(const std::string &macAddress); 132 static std::string GetActiveHfpDevice(); 133 static int32_t ConnectScoWithAudioScene(AudioStandard::AudioScene scene); 134 static int32_t DisconnectSco(); 135 static int8_t GetScoCategoryFromScene(AudioStandard::AudioScene scene); 136 static void DisconnectBluetoothHfpSink(); 137 static void UpdateCurrentActiveHfpDevice(const BluetoothRemoteDevice &device); 138 static std::string GetCurrentActiveHfpDevice(); 139 static void UpdateAudioScene(AudioStandard::AudioScene scene); 140 static void CheckHfpDeviceReconnect(); 141 static AudioStandard::AudioScene GetCurrentAudioScene(); 142 static AudioStandard::AudioScene GetPolicyAudioScene(); 143 static void SetAudioSceneFromPolicy(AudioStandard::AudioScene scene); 144 static int32_t HandleScoWithRecongnition(bool handleFlag, BluetoothRemoteDevice &device); 145 static void ClearRecongnitionStatus(); 146 static ScoCategory GetScoCategory(); 147 static RecognitionStatus GetRecognitionStatus(); 148 static int32_t Connect(const std::string &macAddress); 149 static int32_t SetVirtualCall(const bool isVirtual); 150 static bool IsVirtualCall(); 151 static bool IsAudioScoStateConnect(); 152 153 private: 154 static int32_t ConnectScoUponDefaultScene(int8_t category); 155 156 private: 157 static HandsFreeAudioGateway *hfpInstance_; 158 static std::shared_ptr<AudioHfpListener> hfpListener_; 159 static AudioStandard::AudioScene scene_; 160 static AudioStandard::AudioScene sceneFromPolicy_; 161 static BluetoothRemoteDevice activeHfpDevice_; 162 static ScoCategory scoCategory; 163 static RecognitionStatus recognitionStatus; 164 static bool isVirtualCall; 165 }; 166 } 167 } 168 #endif // AUDIO_BLUETOOTH_MANAGERI_H 169