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