• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "audio_bluetooth_manager.h"
17 
18 #include "audio_info.h"
19 #include "bluetooth_a2dp_src_observer.h"
20 #include "bluetooth_def.h"
21 #include "bluetooth_types.h"
22 #include "bt_def.h"
23 #include "i_bluetooth_a2dp_src.h"
24 #include "i_bluetooth_host.h"
25 #include "iservice_registry.h"
26 #include "media_log.h"
27 #include "system_ability_definition.h"
28 
29 namespace OHOS {
30 namespace Bluetooth {
31 using namespace bluetooth;
32 using namespace AudioStandard;
33 
34 sptr<IBluetoothA2dpSrc> g_proxy = nullptr;
35 static sptr<BluetoothA2dpSrcObserver> g_btA2dpSrcObserverCallbacks = nullptr;
36 int g_playState = false;
37 RawAddress g_device;
38 IDeviceStatusObserver *g_deviceObserver = nullptr;
39 
AudioOnPlayingStatusChanged(const RawAddress & device,int playingState,int error)40 static void AudioOnPlayingStatusChanged(const RawAddress &device, int playingState, int error)
41 {
42     MEDIA_INFO_LOG("AudioOnPlayingStatusChanged");
43     g_playState = playingState;
44 }
45 
AudioOnConfigurationChanged(const RawAddress & device,const BluetoothA2dpCodecInfo & info,int error)46 static void AudioOnConfigurationChanged(const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error)
47 {
48     MEDIA_INFO_LOG("AudioOnConfigurationChanged");
49 }
50 
AudioOnConnectionChanged(const RawAddress & device,int state)51 static void AudioOnConnectionChanged(const RawAddress &device, int state)
52 {
53     MEDIA_INFO_LOG("AudioOnConnectionChanged: device: %{public}s, state: %{public}d",
54         device.GetAddress().c_str(), state);
55     g_device = RawAddress(device);
56 
57     if (g_deviceObserver == nullptr) {
58         MEDIA_INFO_LOG("observer is null");
59         return;
60     }
61 
62     if (state == STATE_TURN_ON) {
63         g_deviceObserver->OnDeviceStatusUpdated(DEVICE_TYPE_BLUETOOTH_A2DP, true, nullptr);
64     } else if (state == STATE_TURN_OFF) {
65         g_deviceObserver->OnDeviceStatusUpdated(DEVICE_TYPE_BLUETOOTH_A2DP, false, nullptr);
66     }
67 }
68 
69 static BtA2dpAudioCallback g_hdiCallacks = {
70     .OnPlayingStatusChanged = AudioOnPlayingStatusChanged,
71     .OnConfigurationChanged = AudioOnConfigurationChanged,
72     .OnConnectionStateChanged = AudioOnConnectionChanged,
73 };
74 
GetPlayingState()75 int GetPlayingState()
76 {
77     return g_playState;
78 }
79 
GetDevice()80 RawAddress& GetDevice()
81 {
82     return g_device;
83 }
84 
GetProxy()85 void GetProxy()
86 {
87     MEDIA_INFO_LOG("GetProxy start");
88     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
89     if (!samgr) {
90         MEDIA_INFO_LOG("GetProxy error: no samgr");
91         return;
92     }
93 
94     sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
95     if (!hostRemote) {
96         MEDIA_INFO_LOG("A2dpSource::impl:GetProxy failed: no hostRemote");
97         return;
98     }
99 
100     sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
101     if (!hostProxy) {
102         MEDIA_INFO_LOG("GetProxy error: host no proxy");
103         return;
104     }
105 
106     sptr<IRemoteObject> remote = hostProxy->GetProfile("A2dpSrcServer");
107     if (!remote) {
108         MEDIA_INFO_LOG("GetProxy error: no remote");
109         return;
110     }
111 
112     g_proxy = iface_cast<IBluetoothA2dpSrc>(remote);
113     if (!g_proxy) {
114         MEDIA_INFO_LOG("GetProxy error: no proxy");
115         return;
116     }
117 }
118 
RegisterObserver(IDeviceStatusObserver & observer)119 void RegisterObserver(IDeviceStatusObserver &observer)
120 {
121     MEDIA_INFO_LOG("RegisterObserver start");
122     if (g_proxy == nullptr) {
123         MEDIA_ERR_LOG("proxy is null");
124         return;
125     }
126 
127     g_deviceObserver = &observer;
128     g_btA2dpSrcObserverCallbacks = new BluetoothA2dpSrcObserver(&g_hdiCallacks);
129     g_proxy->RegisterObserver(g_btA2dpSrcObserverCallbacks);
130 }
131 }
132 }
133