• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
17 #include <hdf_log.h>
18 #include <atomic>
19 #include "audio_internal.h"
20 #include "i_bluetooth_a2dp_src.h"
21 #include "i_bluetooth_host.h"
22 #include "bluetooth_a2dp_src_observer.h"
23 #include "bluetooth_def.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 #include "audio_bluetooth_manager.h"
27 
28 #ifdef A2DP_HDI_SERVICE
29 #include "bluetooth_audio_device.h"
30 #endif
31 
32 #define HDF_LOG_TAG BTAudioBluetoothManager
33 
34 namespace OHOS {
35 namespace Bluetooth {
36 using namespace OHOS::bluetooth;
37 
38 #ifdef A2DP_HDI_SERVICE
39 using namespace OHOS::bluetooth::audio;
40 static const char *g_bluetoothAudioDeviceSoPath = HDF_LIBRARY_FULL_PATH("libbluetooth_audio_session");
41 static void *g_ptrAudioDeviceHandle = NULL;
42 std::atomic_bool g_allowAudioStart = true;
43 
44 SetUpFunc setUpFunc;
45 TearDownFunc tearDownFunc;
46 GetStateFunc getStateFunc;
47 StartPlayingFunc startPlayingFunc;
48 SuspendPlayingFunc suspendPlayingFunc;
49 StopPlayingFunc stopPlayingFunc;
50 WriteFrameFunc writeFrameFunc;
51 GetLatencyFunc getLatencyFunc;
52 
53 SetUpFunc fastSetUpFunc;
54 TearDownFunc fastTearDownFunc;
55 GetStateFunc fastGetStateFunc;
56 StartPlayingFunc fastStartPlayingFunc;
57 SuspendPlayingFunc fastSuspendPlayingFunc;
58 StopPlayingFunc fastStopPlayingFunc;
59 ReqMmapBufferFunc fastReqMmapBufferFunc;
60 ReadMmapPositionFunc fastReadMmapPositionFunc;
61 GetLatencyFunc fastGetLatencyFunc;
62 GetRealStateFunc getRealStateFunc;
63 GetRenderMixerStateFunc getRenderMixerStateFunc;
64 
65 SetUpFunc setUpCaptureFunc;
66 TearDownFunc tearDownCaptureFunc;
67 GetStateFunc getCaptureStateFunc;
68 StartCaptureFunc startCaptureFunc;
69 SuspendPlayingFunc suspendCaptureFunc;
70 StopPlayingFunc stopCaptureFunc;
71 ReadFrameFunc readFrameFunc;
72 
73 SetUpFunc setUpHearingAidFunc;
74 TearDownFunc tearDownHearingAidFunc;
75 GetStateFunc getHearingAidStateFunc;
76 StartHearingAidFunc startHearingAidFunc;
77 SuspendPlayingFunc suspendHearingAidFunc;
78 StopPlayingFunc stopHearingAidFunc;
79 WriteFrameFunc writeFrameHearingAidFunc;
80 #endif
81 
82 sptr<IBluetoothA2dpSrc> g_proxy_ = nullptr;
83 static sptr<BluetoothA2dpSrcObserver> g_btA2dpSrcObserverCallbacks = nullptr;
84 int g_playState = A2DP_NOT_PLAYING;
85 std::map<int, std::string> g_playdevices {};
86 std::mutex g_playStateMutex;
87 
AudioOnConnectionStateChanged(const RawAddress & device,int state,int cause)88 static void AudioOnConnectionStateChanged(const RawAddress &device, int state, int cause)
89 {
90     HDF_LOGI("%{public}s, state:%{public}d", __func__, state);
91     (void) state;
92     (void) cause;
93 }
94 
AudioOnPlayingStatusChanged(const RawAddress & device,int playingState,int error)95 static void AudioOnPlayingStatusChanged(const RawAddress &device, int playingState, int error)
96 {
97     HDF_LOGI("%{public}s, playingState:%{public}d", __func__, playingState);
98     std::lock_guard<std::mutex> lock(g_playStateMutex);
99     std::string addr = device.GetAddress();
100     if (playingState) {
101         for (const auto &it : g_playdevices) {
102             if (strcmp(it.second.c_str(), device.GetAddress().c_str()) == 0) {
103                 return;
104             }
105         }
106         g_playdevices.insert(std::make_pair(playingState, addr));
107         g_playState = playingState;
108     } else {
109         std::map<int, std::string>::iterator it;
110         for (it = g_playdevices.begin(); it != g_playdevices.end(); it++) {
111             if (strcmp(it->second.c_str(), device.GetAddress().c_str()) == 0) {
112                 g_playdevices.erase(it);
113                 break;
114             }
115         }
116         if (g_playdevices.empty()) {
117             g_playState = playingState;
118         }
119     }
120     (void) error;
121 }
122 
AudioOnConfigurationChanged(const RawAddress & device,const BluetoothA2dpCodecInfo & info,int error)123 static void AudioOnConfigurationChanged(const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error)
124 {
125     (void) device;
126     (void) info;
127     (void) error;
128 }
129 
AudioOnMediaStackChanged(const RawAddress & device,int action)130 static void AudioOnMediaStackChanged(const RawAddress &device, int action)
131 {
132     (void) device;
133     (void) action;
134 }
135 
136 
137 static BtA2dpAudioCallback g_hdiCallbacks = {
138     .OnConnectionStateChanged = AudioOnConnectionStateChanged,
139     .OnPlayingStatusChanged = AudioOnPlayingStatusChanged,
140     .OnConfigurationChanged =  AudioOnConfigurationChanged,
141     .OnMediaStackChanged = AudioOnMediaStackChanged,
142 };
143 
GetPlayingState()144 int GetPlayingState()
145 {
146     HDF_LOGI("%{public}s: state:%{public}d", __func__, g_playState);
147     return g_playState;
148 }
149 
GetProxy()150 void GetProxy()
151 {
152     HDF_LOGI("%{public}s start", __func__);
153     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
154     if (!samgr) {
155         HDF_LOGE("%{public}s: error: no samgr", __func__);
156         return;
157     }
158 
159     sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
160     if (!hostRemote) {
161         HDF_LOGE("%{public}s: failed: no hostRemote", __func__);
162         return;
163     }
164 
165     sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
166     if (!hostProxy) {
167         HDF_LOGE("%{public}s: error: host no proxy", __func__);
168         return;
169     }
170 
171     sptr<IRemoteObject> remote = hostProxy->GetProfile("A2dpSrcServer");
172     if (!remote) {
173         HDF_LOGE("%{public}s: error: no remote", __func__);
174         return;
175     }
176 
177     g_proxy_ = iface_cast<IBluetoothA2dpSrc>(remote);
178     if (!g_proxy_) {
179         HDF_LOGE("%{public}s: error: no proxy", __func__);
180         return;
181     }
182 }
183 
RegisterObserver()184 void RegisterObserver()
185 {
186     HDF_LOGI("%{public}s", __func__);
187     g_btA2dpSrcObserverCallbacks = new (std::nothrow) BluetoothA2dpSrcObserver(&g_hdiCallbacks);
188     if (!g_btA2dpSrcObserverCallbacks) {
189         HDF_LOGE("%{public}s: g_btA2dpSrcObserverCallbacks is null", __func__);
190         return;
191     }
192     if (!g_proxy_) {
193         HDF_LOGE("%{public}s: g_proxy_ is null", __func__);
194         return;
195     }
196     g_proxy_->RegisterObserver(g_btA2dpSrcObserverCallbacks);
197 }
198 
DeRegisterObserver()199 void DeRegisterObserver()
200 {
201     HDF_LOGI("%{public}s", __func__);
202     if (!g_proxy_) {
203         HDF_LOGE("%{public}s: g_proxy_ is null", __func__);
204         return;
205     }
206     g_proxy_->DeregisterObserver(g_btA2dpSrcObserverCallbacks);
207 }
208 
209 #ifdef A2DP_HDI_SERVICE
210 #define GET_SYM_ERRPR_RET(handle, funcType, funcPtr, funcStr)       \
211     do {                                                            \
212         funcPtr = (funcType)dlsym(handle, funcStr);                 \
213         if (funcPtr == nullptr) {                                   \
214             HDF_LOGE("%{public}s: lib so func not found", funcStr); \
215             return false;                                           \
216         }                                                           \
217     } while (0)
218 
InitHearingAidDevice()219 static bool InitHearingAidDevice()
220 {
221     GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, SetUpFunc, setUpHearingAidFunc, "SetUpHearingAid");
222     GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, TearDownFunc, tearDownHearingAidFunc, "TearDownHearingAid");
223     GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, GetStateFunc, getHearingAidStateFunc, "GetHearingAidState");
224     GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, StartHearingAidFunc, startHearingAidFunc, "StartHearingAid");
225     GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, SuspendPlayingFunc, suspendHearingAidFunc, "SuspendHearingAid");
226     GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, StopPlayingFunc, stopHearingAidFunc, "StopHearingAid");
227     GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, WriteFrameFunc, writeFrameHearingAidFunc, "WriteFrameHearingAid");
228     return true;
229 }
230 
InitAudioDeviceSoHandle(const char * path)231 static bool InitAudioDeviceSoHandle(const char *path)
232 {
233     if (path == NULL) {
234         HDF_LOGE("%{public}s: path is NULL", __func__);
235         return false;
236     }
237     char pathBuf[PATH_MAX] = {'\0'};
238     if (realpath(path, pathBuf) == NULL) {
239         return false;
240     }
241     if (g_ptrAudioDeviceHandle == NULL) {
242         g_ptrAudioDeviceHandle = dlopen(pathBuf, RTLD_LAZY);
243         if (g_ptrAudioDeviceHandle == NULL) {
244             HDF_LOGE("%{public}s: open lib so fail, reason:%{public}s ", __func__, dlerror());
245             return false;
246         }
247         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, SetUpFunc, setUpFunc, "SetUp");
248         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, TearDownFunc, tearDownFunc, "TearDown");
249         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, GetStateFunc, getStateFunc, "GetState");
250         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, StartPlayingFunc, startPlayingFunc, "StartPlaying");
251         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, SuspendPlayingFunc, suspendPlayingFunc, "SuspendPlaying");
252         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, StopPlayingFunc, stopPlayingFunc, "StopPlaying");
253         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, WriteFrameFunc, writeFrameFunc, "WriteFrame");
254         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, GetLatencyFunc, getLatencyFunc, "GetLatency");
255 
256         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, SetUpFunc, fastSetUpFunc, "FastSetUp");
257         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, TearDownFunc, fastTearDownFunc, "FastTearDown");
258         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, GetStateFunc, fastGetStateFunc, "FastGetState");
259         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, StartPlayingFunc, fastStartPlayingFunc, "FastStartPlaying");
260         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, SuspendPlayingFunc, fastSuspendPlayingFunc, "FastSuspendPlaying");
261         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, StopPlayingFunc, fastStopPlayingFunc, "FastStopPlaying");
262         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, ReqMmapBufferFunc, fastReqMmapBufferFunc, "FastReqMmapBuffer");
263         GET_SYM_ERRPR_RET(
264             g_ptrAudioDeviceHandle, ReadMmapPositionFunc, fastReadMmapPositionFunc, "FastReadMmapPosition");
265         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, GetLatencyFunc, fastGetLatencyFunc, "FastGetLatency");
266         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, GetRealStateFunc, getRealStateFunc, "GetRealState");
267         GET_SYM_ERRPR_RET(
268             g_ptrAudioDeviceHandle, GetRenderMixerStateFunc, getRenderMixerStateFunc, "GetRenderMixerState");
269 
270         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, SetUpFunc, setUpCaptureFunc, "SetUpCapture");
271         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, TearDownFunc, tearDownCaptureFunc, "TearDownCapture");
272         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, GetStateFunc, getCaptureStateFunc, "GetCaptureState");
273         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, StartCaptureFunc, startCaptureFunc, "StartCapture");
274         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, SuspendPlayingFunc, suspendCaptureFunc, "SuspendCapture");
275         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, StopPlayingFunc, stopCaptureFunc, "StopCapture");
276         GET_SYM_ERRPR_RET(g_ptrAudioDeviceHandle, ReadFrameFunc, readFrameFunc, "ReadFrame");
277 
278         if (!InitHearingAidDevice()) {
279             return false;
280         }
281     }
282     return true;
283 }
284 
SetUp()285 bool SetUp()
286 {
287     bool ret = false;
288     ret = InitAudioDeviceSoHandle(g_bluetoothAudioDeviceSoPath);
289     if (ret == true) {
290         ret = setUpFunc();
291     }
292     if (ret == false) {
293         HDF_LOGE("%{public}s failed!", __func__);
294     }
295     return ret;
296 }
297 
SetUpCapture()298 bool SetUpCapture()
299 {
300     bool ret = InitAudioDeviceSoHandle(g_bluetoothAudioDeviceSoPath);
301     if (ret) {
302         ret = setUpCaptureFunc();
303     }
304     if (!ret) {
305         HDF_LOGE("%{public}s failed!", __func__);
306     }
307     return ret;
308 }
309 
SetUpHearingAid()310 bool SetUpHearingAid()
311 {
312     bool ret = InitAudioDeviceSoHandle(g_bluetoothAudioDeviceSoPath);
313     if (ret) {
314         ret = setUpHearingAidFunc();
315     }
316     if (!ret) {
317         HDF_LOGE("%{public}s failed!", __func__);
318     }
319     return ret;
320 }
321 
TearDown()322 void TearDown()
323 {
324     tearDownFunc();
325 }
326 
TearDownCapture()327 void TearDownCapture()
328 {
329     tearDownCaptureFunc();
330 }
331 
TearDownHearingAid()332 void TearDownHearingAid()
333 {
334     tearDownHearingAidFunc();
335 }
336 
FastSetUp()337 bool FastSetUp()
338 {
339     bool ret = InitAudioDeviceSoHandle(g_bluetoothAudioDeviceSoPath);
340     if (ret) {
341         ret = fastSetUpFunc();
342     }
343     if (!ret) {
344         HDF_LOGE("%{public}s failed", __func__);
345     }
346     return ret;
347 }
348 
FastTearDown()349 void FastTearDown()
350 {
351     fastTearDownFunc();
352 }
353 
FastStartPlaying(uint32_t sampleRate,uint32_t channelCount,uint32_t format)354 int FastStartPlaying(uint32_t sampleRate, uint32_t channelCount, uint32_t format)
355 {
356     BTAudioStreamState state = fastGetStateFunc();
357     if (!g_allowAudioStart.load()) {
358         HDF_LOGE("not allow to start fast render, state=%{public}hhu", state);
359         return HDF_FAILURE;
360     } else if (state != BTAudioStreamState::STARTED) {
361         HDF_LOGI("%{public}s, state=%{public}hhu", __func__, state);
362         if (!fastStartPlayingFunc(sampleRate, channelCount, format)) {
363             HDF_LOGE("%{public}s, fail to startPlaying", __func__);
364             return HDF_FAILURE;
365         }
366     }
367     return HDF_SUCCESS;
368 }
369 
FastSuspendPlayingFromParam()370 int FastSuspendPlayingFromParam()
371 {
372     int ret = 0;
373     RenderMixerState renderState = getRenderMixerStateFunc();
374     if (!g_allowAudioStart.load()) {
375         if (renderState == RenderMixerState::INITED || renderState == RenderMixerState::NORMAL_ON_MIX_STOP) {
376             HDF_LOGE("fast render is already stopping or stopped");
377             return ret;
378         }
379     }
380 
381     BTAudioStreamState state = fastGetStateFunc();
382     BTAudioStreamState realState = getRealStateFunc();
383     g_allowAudioStart = false;
384     if (state == BTAudioStreamState::STARTED) {
385         HDF_LOGI("%{public}s", __func__);
386         ret = (fastSuspendPlayingFunc() ? HDF_SUCCESS : HDF_FAILURE);
387     } else if (realState == BTAudioStreamState::STARTING && renderState == RenderMixerState::FAST_STARTED) {
388         HDF_LOGI("%{public}s fast render starting, so stopPlaying", __func__);
389         ret = (fastStopPlayingFunc() ? HDF_SUCCESS : HDF_FAILURE);
390     } else {
391         HDF_LOGE("%{public}s, state=%{public}hhu is bad state, realState=%{public}hhu, renderState=%{public}hhu",
392             __func__, state, realState, renderState);
393     }
394     return ret;
395 }
396 
FastSuspendPlaying()397 int FastSuspendPlaying()
398 {
399     int ret = 0;
400     BTAudioStreamState state = fastGetStateFunc();
401     if (state == BTAudioStreamState::STARTED) {
402         ret = (fastSuspendPlayingFunc() ? HDF_SUCCESS : HDF_FAILURE);
403     } else {
404         HDF_LOGE("%{public}s, state=%{public}hhu is bad state", __func__, state);
405     }
406     return ret;
407 }
408 
FastStopPlaying()409 int FastStopPlaying()
410 {
411     BTAudioStreamState state = fastGetStateFunc();
412     HDF_LOGI("%{public}s, state=%{public}hhu", __func__, state);
413     if (state != BTAudioStreamState::INVALID) {
414         fastStopPlayingFunc();
415     }
416     return HDF_SUCCESS;
417 }
418 
FastReqMmapBuffer(int32_t ashmemLength)419 int FastReqMmapBuffer(int32_t ashmemLength)
420 {
421     return fastReqMmapBufferFunc(ashmemLength);
422 }
423 
FastReadMmapPosition(int64_t & sec,int64_t & nSec,uint64_t & frames)424 void FastReadMmapPosition(int64_t &sec, int64_t &nSec, uint64_t &frames)
425 {
426     fastReadMmapPositionFunc(sec, nSec, frames);
427 }
428 
FastGetLatency(uint32_t & latency)429 int FastGetLatency(uint32_t &latency)
430 {
431     return (fastGetLatencyFunc(latency) ? HDF_SUCCESS : HDF_FAILURE);
432 }
433 
SuspendPlayingFromParam()434 int SuspendPlayingFromParam()
435 {
436     int retVal = 0;
437     RenderMixerState renderState = getRenderMixerStateFunc();
438     if (!g_allowAudioStart.load()) {
439         if (renderState == RenderMixerState::INITED || renderState == RenderMixerState::FAST_ON_MIX_STOP) {
440             HDF_LOGE("normal render is already stopping or stopped");
441             return retVal;
442         }
443     }
444 
445     BTAudioStreamState state = getStateFunc();
446     BTAudioStreamState realState = getRealStateFunc();
447     g_allowAudioStart = false;
448     if (state == BTAudioStreamState::STARTED) {
449         HDF_LOGI("%{public}s", __func__);
450         retVal = (suspendPlayingFunc() ? HDF_SUCCESS : HDF_FAILURE);
451     } else if (realState == BTAudioStreamState::STARTING && renderState == RenderMixerState::INITED) {
452         HDF_LOGI("%{public}s normal render starting, so stopPlaying", __func__);
453         retVal = (stopPlayingFunc() ? HDF_SUCCESS : HDF_FAILURE);
454     } else {
455         HDF_LOGE("%{public}s, state=%{public}hhu is bad state, realState=%{public}hhu, renderState=%{public}hhu",
456             __func__, state, realState, renderState);
457     }
458     return retVal;
459 }
460 
UnBlockStart()461 void UnBlockStart()
462 {
463     g_allowAudioStart = true;
464 }
465 
StartCapture()466 int StartCapture()
467 {
468     BTAudioStreamState state = getCaptureStateFunc();
469     if (state != BTAudioStreamState::STARTED) {
470         HDF_LOGE("%{public}s: state=%{public}hhu", __func__, state);
471         if (!startCaptureFunc()) {
472             HDF_LOGE("%{public}s: fail to startPlaying", __func__);
473             return HDF_FAILURE;
474         }
475     }
476     return HDF_SUCCESS;
477 }
478 
SuspendCapture()479 int SuspendCapture()
480 {
481     int ret = 0;
482     BTAudioStreamState state = getCaptureStateFunc();
483     if (state == BTAudioStreamState::STARTED) {
484         ret = suspendCaptureFunc() ? HDF_SUCCESS : HDF_FAILURE;
485     } else {
486         HDF_LOGE("%{public}s: state=%{public}hhu is bad state", __func__, state);
487     }
488     return ret;
489 }
490 
StopCapture()491 int StopCapture()
492 {
493     BTAudioStreamState state = getCaptureStateFunc();
494     HDF_LOGE("%{public}s: state=%{public}hhu", __func__, state);
495     if (state != BTAudioStreamState::INVALID) {
496         stopCaptureFunc();
497     }
498     return HDF_SUCCESS;
499 }
500 
ReadFrame(uint8_t * data,uint64_t size)501 int ReadFrame(uint8_t *data, uint64_t size)
502 {
503     HDF_LOGD("%{public}s", __func__);
504     return readFrameFunc(data, size);
505 }
506 
StartHearingAid()507 int StartHearingAid()
508 {
509     BTAudioStreamState state = getHearingAidStateFunc();
510     if (state != BTAudioStreamState::STARTED) {
511         HDF_LOGE("%{public}s: state=%{public}hhu", __func__, state);
512         if (!startHearingAidFunc()) {
513             HDF_LOGE("%{public}s: fail to startPlaying", __func__);
514             return HDF_FAILURE;
515         }
516     }
517     return HDF_SUCCESS;
518 }
519 
SuspendHearingAid()520 int SuspendHearingAid()
521 {
522     int ret = 0;
523     BTAudioStreamState state = getHearingAidStateFunc();
524     if (state == BTAudioStreamState::STARTED) {
525         ret = suspendHearingAidFunc() ? HDF_SUCCESS : HDF_FAILURE;
526     } else {
527         HDF_LOGE("%{public}s: state=%{public}hhu is bad state", __func__, state);
528     }
529     return ret;
530 }
531 
StopHearingAid()532 int StopHearingAid()
533 {
534     BTAudioStreamState state = getHearingAidStateFunc();
535     HDF_LOGE("%{public}s: state=%{public}hhu", __func__, state);
536     if (state != BTAudioStreamState::INVALID) {
537         stopHearingAidFunc();
538     }
539     return HDF_SUCCESS;
540 }
541 
542 #endif
543 
WriteFrameHearingAid(const uint8_t * data,uint32_t size,const HDI::Audio_Bluetooth::AudioSampleAttributes * attrs)544 int WriteFrameHearingAid(const uint8_t *data, uint32_t size, const HDI::Audio_Bluetooth::AudioSampleAttributes *attrs)
545 {
546     HDF_LOGD("%{public}s", __func__);
547 #ifdef A2DP_HDI_SERVICE
548     BTAudioStreamState state = getHearingAidStateFunc();
549     if (state != BTAudioStreamState::STARTED) {
550         HDF_LOGE("%{public}s: state=%{public}hhu", __func__, state);
551         if (!startHearingAidFunc()) {
552             HDF_LOGE("%{public}s: fail to startHearingAid", __func__);
553             return HDF_FAILURE;
554         }
555     }
556     return writeFrameHearingAidFunc(data, size);
557 #else
558     return HDF_ERR_NOT_SUPPORT;
559 #endif
560 }
561 
WriteFrame(const uint8_t * data,uint32_t size,const HDI::Audio_Bluetooth::AudioSampleAttributes * attrs)562 int WriteFrame(const uint8_t *data, uint32_t size, const HDI::Audio_Bluetooth::AudioSampleAttributes *attrs)
563 {
564     HDF_LOGD("%{public}s", __func__);
565 #ifdef A2DP_HDI_SERVICE
566     BTAudioStreamState state = getStateFunc();
567     if (!g_allowAudioStart.load()) {
568         HDF_LOGE("not allow to start normal render, state=%{public}hhu", state);
569         return HDF_FAILURE;
570     } else if (state != BTAudioStreamState::STARTED) {
571         HDF_LOGE("%{public}s: state=%{public}hhu", __func__, state);
572         if (!startPlayingFunc(attrs->sampleRate, attrs->channelCount, static_cast<uint32_t>(attrs->format))) {
573             HDF_LOGE("%{public}s: fail to startPlaying", __func__);
574             return HDF_FAILURE;
575         }
576     }
577     return writeFrameFunc(data, size);
578 #else
579     if (!g_proxy_) {
580         HDF_LOGE("%{public}s: g_proxy_ is null", __func__);
581         return RET_BAD_STATUS;
582     }
583     if (g_playState == A2DP_NOT_PLAYING) {
584         HDF_LOGE("%{public}s: playState is not Streaming", __func__);
585         return RET_BAD_STATUS;
586     }
587     return g_proxy_->WriteFrame(data, size);
588 #endif
589 }
590 
StartPlaying()591 int StartPlaying()
592 {
593     HDF_LOGI("%{public}s", __func__);
594 #ifdef A2DP_HDI_SERVICE
595     return HDF_SUCCESS;
596 #else
597     if (!g_proxy_) {
598         HDF_LOGE("%{public}s: g_proxy_ is null", __func__);
599         return RET_BAD_STATUS;
600     }
601     return g_proxy_->StartPlaying(g_proxy_->GetActiveSinkDevice());
602 #endif
603 }
604 
SuspendPlaying()605 int SuspendPlaying()
606 {
607 #ifdef A2DP_HDI_SERVICE
608     int retval = 0;
609     BTAudioStreamState state = getStateFunc();
610     HDF_LOGE("%{public}s: state=%{public}hhu", __func__, state);
611     if (state == BTAudioStreamState::STARTED) {
612         retval = (suspendPlayingFunc() ? HDF_SUCCESS : HDF_FAILURE);
613     } else {
614         HDF_LOGE("%{public}s: state=%{public}hhu is bad state", __func__, state);
615     }
616     return retval;
617 #else
618     HDF_LOGI("%{public}s", __func__);
619     if (!g_proxy_) {
620         HDF_LOGE("%{public}s: g_proxy_ is null", __func__);
621         return RET_BAD_STATUS;
622     }
623     return g_proxy_->SuspendPlaying(g_proxy_->GetActiveSinkDevice());
624 #endif
625 }
626 
StopPlaying()627 int StopPlaying()
628 {
629     HDF_LOGI("%{public}s", __func__);
630 #ifdef A2DP_HDI_SERVICE
631     BTAudioStreamState state = getStateFunc();
632     HDF_LOGE("%{public}s: state=%{public}hhu", __func__, state);
633     if (state != BTAudioStreamState::INVALID) {
634         stopPlayingFunc();
635     }
636     return HDF_SUCCESS;
637 #else
638     if (!g_proxy_) {
639         HDF_LOGE("%{public}s: g_proxy_ is null", __func__);
640         return RET_BAD_STATUS;
641     }
642     return g_proxy_->StopPlaying(g_proxy_->GetActiveSinkDevice());
643 #endif
644 }
645 
GetLatency(uint32_t & latency)646 int GetLatency(uint32_t &latency)
647 {
648     HDF_LOGD("%{public}s", __func__);
649 #ifdef A2DP_HDI_SERVICE
650     return (getLatencyFunc(latency) ? HDF_SUCCESS : HDF_FAILURE);
651 #else
652     return HDF_ERR_NOT_SUPPORT;
653 #endif
654 }
655 }
656 }
657