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 #include <hdf_log.h>
17 #include "audio_internal.h"
18 #include "i_bluetooth_a2dp_src.h"
19 #include "i_bluetooth_host.h"
20 #include "bluetooth_a2dp_src_observer.h"
21 #include "bluetooth_def.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "audio_bluetooth_manager.h"
25
26 namespace OHOS {
27 namespace Bluetooth {
28 using namespace bluetooth;
29
30 sptr<IBluetoothA2dpSrc> g_proxy_ = nullptr;
31 static sptr<BluetoothA2dpSrcObserver> g_btA2dpSrcObserverCallbacks = nullptr;
32 int g_playState = false;
33 RawAddress g_device;
34
AudioOnConnectionStateChanged(const RawAddress & device,int state)35 static void AudioOnConnectionStateChanged(const RawAddress &device, int state)
36 {
37 HDF_LOGI("AudioOnConnectionStateChanged");
38 g_device = RawAddress(device);
39 }
40
AudioOnPlayingStatusChanged(const RawAddress & device,int playingState,int error)41 static void AudioOnPlayingStatusChanged(const RawAddress &device, int playingState, int error)
42 {
43 HDF_LOGI("AudioOnPlayingStatusChanged");
44 g_playState = playingState;
45 }
46
AudioOnConfigurationChanged(const RawAddress & device,const BluetoothA2dpCodecInfo & info,int error)47 static void AudioOnConfigurationChanged(const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error)
48 {
49 HDF_LOGI("AudioOnConfigurationChanged");
50 }
51
52
53 static BtA2dpAudioCallback g_hdiCallbacks = {
54 .OnConnectionStateChanged = AudioOnConnectionStateChanged,
55 .OnPlayingStatusChanged = AudioOnPlayingStatusChanged,
56 .OnConfigurationChanged = AudioOnConfigurationChanged,
57 };
58
GetPlayingState()59 int GetPlayingState()
60 {
61 return g_playState;
62 }
63
GetDevice()64 RawAddress& GetDevice()
65 {
66 return g_device;
67 }
68
GetProxy()69 void GetProxy()
70 {
71 HDF_LOGI("audio_bluetooth_manager GetProxy start");
72 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
73 if (!samgr) {
74 HDF_LOGE("GetProxy error: no samgr");
75 return;
76 }
77
78 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
79 if (!hostRemote) {
80 HDF_LOGE("GetProxy failed: no hostRemote");
81 return;
82 }
83
84 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
85 if (!hostProxy) {
86 HDF_LOGE("GetProxy error: host no proxy");
87 return;
88 }
89
90 sptr<IRemoteObject> remote = hostProxy->GetProfile("A2dpSrcServer");
91 if (!remote) {
92 HDF_LOGE("GetProxy error: no remote");
93 return;
94 }
95
96 g_proxy_ = iface_cast<IBluetoothA2dpSrc>(remote);
97 if (!g_proxy_) {
98 HDF_LOGE("GetProxy error: no proxy");
99 return;
100 }
101 }
102
RegisterObserver()103 void RegisterObserver()
104 {
105 HDF_LOGI("audio_bluetooth_manager RegisterObserver");
106 g_btA2dpSrcObserverCallbacks = new BluetoothA2dpSrcObserver(&g_hdiCallbacks);
107 g_proxy_->RegisterObserver(g_btA2dpSrcObserverCallbacks);
108 }
109
GetCodecStatus()110 BluetoothA2dpCodecStatus GetCodecStatus()
111 {
112 HDF_LOGI("audio_bluetooth_manager GetCodecStatus");
113 BluetoothA2dpCodecStatus codecStatus;
114 if (!g_proxy_) {
115 HDF_LOGE("no proxy");
116 return codecStatus;
117 }
118 codecStatus = g_proxy_->GetCodecStatus(g_proxy_->GetActiveSinkDevice());
119 return codecStatus;
120 }
121
WriteFrame(const uint8_t * data,uint32_t size)122 int WriteFrame(const uint8_t *data, uint32_t size)
123 {
124 HDF_LOGI("audio_bluetooth_manager WriteFrame");
125 if (!g_proxy_) {
126 HDF_LOGE("no proxy");
127 return RET_BAD_STATUS;
128 }
129 return g_proxy_->WriteFrame(data, size);
130 }
131
StartPlaying()132 int StartPlaying()
133 {
134 HDF_LOGI("audio_bluetooth_manager StartPlaying");
135 if (!g_proxy_) {
136 HDF_LOGE("no proxy");
137 return RET_BAD_STATUS;
138 }
139 return g_proxy_->StartPlaying(g_proxy_->GetActiveSinkDevice());
140 }
141
SuspendPlaying()142 int SuspendPlaying()
143 {
144 HDF_LOGI("audio_bluetooth_manager SuspendPlaying");
145 if (!g_proxy_) {
146 HDF_LOGE("no proxy");
147 return RET_BAD_STATUS;
148 }
149 return g_proxy_->SuspendPlaying(g_proxy_->GetActiveSinkDevice());
150 }
151
StopPlaying()152 int StopPlaying()
153 {
154 HDF_LOGI("audio_bluetooth_manager StopPlaying");
155 if (!g_proxy_) {
156 HDF_LOGE("no proxy");
157 return RET_BAD_STATUS;
158 }
159 return g_proxy_->StopPlaying(g_proxy_->GetActiveSinkDevice());
160 }
161
GetRenderPosition(uint16_t & delayValue,uint16_t & dataSize,uint32_t & timeStamp)162 void GetRenderPosition(uint16_t &delayValue, uint16_t &dataSize, uint32_t &timeStamp)
163 {
164 HDF_LOGI("audio_bluetooth_manager GetRenderPosition");
165 if (!g_proxy_) {
166 HDF_LOGE("no proxy");
167 return;
168 }
169 return g_proxy_->GetRenderPosition(delayValue, dataSize, timeStamp);
170 }
171 }
172 }