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/functional/bind.h>
19 #include <hardware/bluetooth.h>
20 #include <hardware/bt_le_audio.h>
21 
22 #include "bta_le_audio_api.h"
23 #include "bta_le_audio_broadcaster_api.h"
24 #include "btif_common.h"
25 #include "stack/include/main_thread.h"
26 
27 using base::Bind;
28 using base::Unretained;
29 using bluetooth::le_audio::BroadcastId;
30 using bluetooth::le_audio::BroadcastState;
31 using bluetooth::le_audio::LeAudioBroadcasterCallbacks;
32 using bluetooth::le_audio::LeAudioBroadcasterInterface;
33 
34 namespace {
35 class LeAudioBroadcasterInterfaceImpl;
36 std::unique_ptr<LeAudioBroadcasterInterface> leAudioBroadcasterInstance;
37 
38 class LeAudioBroadcasterInterfaceImpl : public LeAudioBroadcasterInterface,
39                                         public LeAudioBroadcasterCallbacks {
40   ~LeAudioBroadcasterInterfaceImpl() override = default;
41 
Initialize(LeAudioBroadcasterCallbacks * callbacks)42   void Initialize(LeAudioBroadcasterCallbacks* callbacks) override {
43     this->callbacks_ = callbacks;
44     do_in_main_thread(
45         FROM_HERE,
46         Bind(&LeAudioBroadcaster::Initialize, this, base::Bind([]() -> bool {
47           return LeAudioHalVerifier::SupportsLeAudioBroadcast();
48         })));
49   }
50 
CreateBroadcast(bool is_public,std::string broadcast_name,std::optional<bluetooth::le_audio::BroadcastCode> broadcast_code,std::vector<uint8_t> public_metadata,std::vector<uint8_t> subgroup_quality,std::vector<std::vector<uint8_t>> subgroup_metadata)51   void CreateBroadcast(
52       bool is_public, std::string broadcast_name,
53       std::optional<bluetooth::le_audio::BroadcastCode> broadcast_code,
54       std::vector<uint8_t> public_metadata,
55       std::vector<uint8_t> subgroup_quality,
56       std::vector<std::vector<uint8_t>> subgroup_metadata) override {
57     do_in_main_thread(FROM_HERE, Bind(&LeAudioBroadcaster::CreateAudioBroadcast,
58                                       Unretained(LeAudioBroadcaster::Get()),
59                                       is_public, broadcast_name, broadcast_code,
60                                       std::move(public_metadata),
61                                       std::move(subgroup_quality),
62                                       std::move(subgroup_metadata)));
63   }
64 
UpdateMetadata(uint32_t broadcast_id,std::string broadcast_name,std::vector<uint8_t> public_metadata,std::vector<std::vector<uint8_t>> subgroup_metadata)65   void UpdateMetadata(
66       uint32_t broadcast_id, std::string broadcast_name,
67       std::vector<uint8_t> public_metadata,
68       std::vector<std::vector<uint8_t>> subgroup_metadata) override {
69     do_in_main_thread(FROM_HERE,
70                       Bind(&LeAudioBroadcaster::UpdateMetadata,
71                            Unretained(LeAudioBroadcaster::Get()), broadcast_id,
72                            broadcast_name, std::move(public_metadata),
73                            std::move(subgroup_metadata)));
74   }
75 
StartBroadcast(uint32_t broadcast_id)76   void StartBroadcast(uint32_t broadcast_id) override {
77     do_in_main_thread(
78         FROM_HERE, Bind(&LeAudioBroadcaster::StartAudioBroadcast,
79                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
80   }
81 
StopBroadcast(uint32_t broadcast_id)82   void StopBroadcast(uint32_t broadcast_id) override {
83     do_in_main_thread(
84         FROM_HERE, Bind(&LeAudioBroadcaster::StopAudioBroadcast,
85                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
86   }
87 
PauseBroadcast(uint32_t broadcast_id)88   void PauseBroadcast(uint32_t broadcast_id) override {
89     do_in_main_thread(
90         FROM_HERE, Bind(&LeAudioBroadcaster::SuspendAudioBroadcast,
91                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
92   }
93 
DestroyBroadcast(uint32_t broadcast_id)94   void DestroyBroadcast(uint32_t broadcast_id) override {
95     do_in_main_thread(
96         FROM_HERE, Bind(&LeAudioBroadcaster::DestroyAudioBroadcast,
97                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
98   }
99 
GetBroadcastMetadata(uint32_t broadcast_id)100   void GetBroadcastMetadata(uint32_t broadcast_id) override {
101     do_in_main_thread(
102         FROM_HERE, Bind(&LeAudioBroadcaster::GetBroadcastMetadata,
103                         Unretained(LeAudioBroadcaster::Get()), broadcast_id));
104   }
105 
OnBroadcastCreated(uint32_t broadcast_id,bool success)106   void OnBroadcastCreated(uint32_t broadcast_id, bool success) override {
107     do_in_jni_thread(Bind(&LeAudioBroadcasterCallbacks::OnBroadcastCreated,
108                           Unretained(callbacks_), broadcast_id, success));
109   }
110 
OnBroadcastDestroyed(uint32_t broadcast_id)111   void OnBroadcastDestroyed(uint32_t broadcast_id) override {
112     do_in_jni_thread(Bind(&LeAudioBroadcasterCallbacks::OnBroadcastDestroyed,
113                           Unretained(callbacks_), broadcast_id));
114   }
115 
OnBroadcastStateChanged(uint32_t broadcast_id,BroadcastState state)116   void OnBroadcastStateChanged(uint32_t broadcast_id,
117                                BroadcastState state) override {
118     do_in_jni_thread(Bind(&LeAudioBroadcasterCallbacks::OnBroadcastStateChanged,
119                           Unretained(callbacks_), broadcast_id, state));
120   }
121 
OnBroadcastMetadataChanged(uint32_t broadcast_id,const bluetooth::le_audio::BroadcastMetadata & broadcast_metadata)122   void OnBroadcastMetadataChanged(uint32_t broadcast_id,
123                                   const bluetooth::le_audio::BroadcastMetadata&
124                                       broadcast_metadata) override {
125     do_in_jni_thread(
126         Bind(&LeAudioBroadcasterCallbacks::OnBroadcastMetadataChanged,
127              Unretained(callbacks_), broadcast_id, broadcast_metadata));
128   }
129 
Stop(void)130   void Stop(void) override {
131     do_in_main_thread(FROM_HERE, Bind(&LeAudioBroadcaster::Stop));
132   }
133 
Cleanup(void)134   void Cleanup(void) override {
135     do_in_main_thread(FROM_HERE, Bind(&LeAudioBroadcaster::Cleanup));
136   }
137 
138  private:
139   LeAudioBroadcasterCallbacks* callbacks_;
140 };
141 
142 } /* namespace */
143 
btif_le_audio_broadcaster_get_interface()144 LeAudioBroadcasterInterface* btif_le_audio_broadcaster_get_interface() {
145   if (!leAudioBroadcasterInstance) {
146     leAudioBroadcasterInstance.reset(new LeAudioBroadcasterInterfaceImpl());
147   }
148 
149   return leAudioBroadcasterInstance.get();
150 }
151