• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 HIMSA II K/S - www.himsa.com. Represented by EHIMA -
3  * www.ehima.com
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <base/bind.h>
19 #include <base/logging.h>
20 #include <hardware/bluetooth.h>
21 #include <hardware/bt_le_audio.h>
22 
23 #include "audio_hal_interface/hal_version_manager.h"
24 #include "bta_le_audio_api.h"
25 #include "bta_le_audio_broadcaster_api.h"
26 #include "btif_common.h"
27 #include "stack/include/btu.h"
28 
29 using base::Bind;
30 using base::Unretained;
31 using bluetooth::le_audio::BroadcastId;
32 using bluetooth::le_audio::BroadcastState;
33 using bluetooth::le_audio::LeAudioBroadcasterCallbacks;
34 using bluetooth::le_audio::LeAudioBroadcasterInterface;
35 
36 namespace {
37 class LeAudioBroadcasterInterfaceImpl;
38 std::unique_ptr<LeAudioBroadcasterInterface> leAudioBroadcasterInstance;
39 
40 class LeAudioBroadcasterInterfaceImpl : public LeAudioBroadcasterInterface,
41                                         public LeAudioBroadcasterCallbacks {
42   ~LeAudioBroadcasterInterfaceImpl() override = default;
43 
Initialize(LeAudioBroadcasterCallbacks * callbacks)44   void Initialize(LeAudioBroadcasterCallbacks* callbacks) override {
45     this->callbacks_ = callbacks;
46     do_in_main_thread(
47         FROM_HERE,
48         Bind(&LeAudioBroadcaster::Initialize, this, base::Bind([]() -> bool {
49           return LeAudioHalVerifier::SupportsLeAudioBroadcast();
50         })));
51   }
52 
CreateBroadcast(std::vector<uint8_t> metadata,std::optional<std::array<uint8_t,16>> broadcast_code)53   void CreateBroadcast(
54       std::vector<uint8_t> metadata,
55       std::optional<std::array<uint8_t, 16>> broadcast_code) override {
56     DVLOG(2) << __func__;
57     do_in_main_thread(FROM_HERE, Bind(&LeAudioBroadcaster::CreateAudioBroadcast,
58                                       Unretained(LeAudioBroadcaster::Get()),
59                                       std::move(metadata), broadcast_code));
60   }
61 
UpdateMetadata(uint32_t broadcast_id,std::vector<uint8_t> metadata)62   void UpdateMetadata(uint32_t broadcast_id,
63                       std::vector<uint8_t> metadata) override {
64     DVLOG(2) << __func__;
65     do_in_main_thread(FROM_HERE, Bind(&LeAudioBroadcaster::UpdateMetadata,
66                                       Unretained(LeAudioBroadcaster::Get()),
67                                       broadcast_id, std::move(metadata)));
68   }
69 
StartBroadcast(uint32_t broadcast_id)70   void StartBroadcast(uint32_t broadcast_id) override {
71     DVLOG(2) << __func__;
72     do_in_main_thread(
73         FROM_HERE, Bind(&LeAudioBroadcaster::StartAudioBroadcast,
74                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
75   }
76 
StopBroadcast(uint32_t broadcast_id)77   void StopBroadcast(uint32_t broadcast_id) override {
78     DVLOG(2) << __func__;
79     do_in_main_thread(
80         FROM_HERE, Bind(&LeAudioBroadcaster::StopAudioBroadcast,
81                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
82   }
83 
PauseBroadcast(uint32_t broadcast_id)84   void PauseBroadcast(uint32_t broadcast_id) override {
85     DVLOG(2) << __func__;
86     do_in_main_thread(
87         FROM_HERE, Bind(&LeAudioBroadcaster::SuspendAudioBroadcast,
88                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
89   }
90 
DestroyBroadcast(uint32_t broadcast_id)91   void DestroyBroadcast(uint32_t broadcast_id) override {
92     DVLOG(2) << __func__;
93     do_in_main_thread(
94         FROM_HERE, Bind(&LeAudioBroadcaster::DestroyAudioBroadcast,
95                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
96   }
97 
GetBroadcastMetadata(uint32_t broadcast_id)98   void GetBroadcastMetadata(uint32_t broadcast_id) override {
99     DVLOG(2) << __func__;
100     do_in_main_thread(
101         FROM_HERE, Bind(&LeAudioBroadcaster::GetBroadcastMetadata,
102                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
103   }
104 
OnBroadcastCreated(uint32_t broadcast_id,bool success)105   void OnBroadcastCreated(uint32_t broadcast_id, bool success) override {
106     DVLOG(2) << __func__;
107     do_in_jni_thread(FROM_HERE,
108                      Bind(&LeAudioBroadcasterCallbacks::OnBroadcastCreated,
109                           Unretained(callbacks_), broadcast_id, success));
110   }
111 
OnBroadcastDestroyed(uint32_t broadcast_id)112   void OnBroadcastDestroyed(uint32_t broadcast_id) override {
113     DVLOG(2) << __func__;
114     do_in_jni_thread(FROM_HERE,
115                      Bind(&LeAudioBroadcasterCallbacks::OnBroadcastDestroyed,
116                           Unretained(callbacks_), broadcast_id));
117   }
118 
OnBroadcastStateChanged(uint32_t broadcast_id,BroadcastState state)119   void OnBroadcastStateChanged(uint32_t broadcast_id,
120                                BroadcastState state) override {
121     DVLOG(2) << __func__;
122     do_in_jni_thread(FROM_HERE,
123                      Bind(&LeAudioBroadcasterCallbacks::OnBroadcastStateChanged,
124                           Unretained(callbacks_), broadcast_id, state));
125   }
126 
OnBroadcastMetadataChanged(uint32_t broadcast_id,const bluetooth::le_audio::BroadcastMetadata & broadcast_metadata)127   void OnBroadcastMetadataChanged(uint32_t broadcast_id,
128                                   const bluetooth::le_audio::BroadcastMetadata&
129                                       broadcast_metadata) override {
130     DVLOG(2) << __func__;
131     do_in_jni_thread(
132         FROM_HERE,
133         Bind(&LeAudioBroadcasterCallbacks::OnBroadcastMetadataChanged,
134              Unretained(callbacks_), broadcast_id, broadcast_metadata));
135   }
136 
Stop(void)137   void Stop(void) override {
138     do_in_main_thread(FROM_HERE, Bind(&LeAudioBroadcaster::Stop));
139   }
140 
Cleanup(void)141   void Cleanup(void) override {
142     do_in_main_thread(FROM_HERE, Bind(&LeAudioBroadcaster::Cleanup));
143   }
144 
145  private:
146   LeAudioBroadcasterCallbacks* callbacks_;
147 };
148 
149 } /* namespace */
150 
btif_le_audio_broadcaster_get_interface()151 LeAudioBroadcasterInterface* btif_le_audio_broadcaster_get_interface() {
152   if (!leAudioBroadcasterInstance) {
153     leAudioBroadcasterInstance.reset(new LeAudioBroadcasterInterfaceImpl());
154   }
155 
156   return leAudioBroadcasterInstance.get();
157 }
158