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