huangcm
2025-09-01 53d8e046ac1bf2ebe94f671983e3d3be059df91a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
//  Copyright (C) 2017 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
 
#include "service/a2dp_source.h"
 
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "service/logging_helpers.h"
 
#define PARSE_ADDR(str)                                        \
  ({                                                           \
    RawAddress tmp;                                            \
    if (!RawAddress::FromString((str), tmp)) {                 \
      LOG(ERROR) << "Invalid device address given: " << (str); \
      return false;                                            \
    }                                                          \
    tmp;                                                       \
  })
 
#define TRY_RET(expr, err_msg) \
  do {                         \
    if (!(expr)) {             \
      LOG(ERROR) << err_msg;   \
      return false;            \
    }                          \
    return true;               \
  } while (0)
 
#define TRY_RET_FUNC(expr) TRY_RET(expr, __func__ << " failed")
 
using bluetooth::hal::BluetoothAvInterface;
using LockGuard = std::lock_guard<std::mutex>;
 
namespace bluetooth {
 
namespace {
 
btav_a2dp_codec_config_t CodecConfigToFluoride(const A2dpCodecConfig& config) {
  btav_a2dp_codec_config_t ret = {
      .codec_type = static_cast<btav_a2dp_codec_index_t>(config.codec_type()),
      .codec_priority =
          static_cast<btav_a2dp_codec_priority_t>(config.codec_priority()),
      .sample_rate =
          static_cast<btav_a2dp_codec_sample_rate_t>(config.sample_rate()),
      .bits_per_sample = static_cast<btav_a2dp_codec_bits_per_sample_t>(
          config.bits_per_sample()),
      .channel_mode =
          static_cast<btav_a2dp_codec_channel_mode_t>(config.channel_mode()),
      .codec_specific_1 = config.codec_specific_1(),
      .codec_specific_2 = config.codec_specific_2(),
      .codec_specific_3 = config.codec_specific_3(),
      .codec_specific_4 = config.codec_specific_4(),
  };
 
  return ret;
}
 
std::vector<btav_a2dp_codec_config_t> CodecConfigsToFluoride(
    const std::vector<A2dpCodecConfig>& configs) {
  std::vector<btav_a2dp_codec_config_t> ret;
  ret.reserve(configs.size());
  for (const auto& config : configs) {
    ret.push_back(CodecConfigToFluoride(config));
  }
 
  return ret;
}
A2dpCodecConfig FluorideCodecToCodec(const btav_a2dp_codec_config_t& config) {
  A2dpCodecConfig ret(config.codec_type, config.codec_priority,
                      config.sample_rate, config.bits_per_sample,
                      config.channel_mode, config.codec_specific_1,
                      config.codec_specific_2, config.codec_specific_3,
                      config.codec_specific_4);
 
  return ret;
}
 
std::vector<A2dpCodecConfig> FluorideCodecsToCodec(
    const std::vector<btav_a2dp_codec_config_t>& configs) {
  std::vector<A2dpCodecConfig> ret;
  ret.reserve(configs.size());
  for (const auto& config : configs) {
    ret.push_back(FluorideCodecToCodec(config));
  }
 
  return ret;
}
 
}  // namespace
 
// static
const int A2dpSource::kSingletonInstanceId = 0;
 
A2dpSource::A2dpSource(const Uuid& uuid) : app_identifier_(uuid) {
  hal::BluetoothAvInterface::Get()->AddA2dpSourceObserver(this);
}
 
A2dpSource::~A2dpSource() {
  hal::BluetoothAvInterface::Get()->RemoveA2dpSourceObserver(this);
}
 
const Uuid& A2dpSource::GetAppIdentifier() const { return app_identifier_; }
 
int A2dpSource::GetInstanceId() const { return kSingletonInstanceId; }
 
void A2dpSource::SetDelegate(Delegate* delegate) {
  LockGuard lock(delegate_mutex_);
  delegate_ = delegate;
}
 
bool A2dpSource::Enable(const std::vector<A2dpCodecConfig>& codec_priorities) {
  auto fluoride_priorities = CodecConfigsToFluoride(codec_priorities);
  LockGuard lock(mutex_);
  return hal::BluetoothAvInterface::Get()->A2dpSourceEnable(
      fluoride_priorities);
}
 
void A2dpSource::Disable() {
  LockGuard lock(mutex_);
  hal::BluetoothAvInterface::Get()->A2dpSourceDisable();
}
 
bool A2dpSource::Connect(const std::string& device_address) {
  RawAddress addr = PARSE_ADDR(device_address);
  LockGuard lock(mutex_);
  TRY_RET_FUNC(
      hal::BluetoothAvInterface::Get()->GetA2dpSourceHALInterface()->connect(
          addr) == BT_STATUS_SUCCESS);
}
 
bool A2dpSource::Disconnect(const std::string& device_address) {
  RawAddress addr = PARSE_ADDR(device_address);
  LockGuard lock(mutex_);
  TRY_RET_FUNC(
      hal::BluetoothAvInterface::Get()->GetA2dpSourceHALInterface()->disconnect(
          addr) == BT_STATUS_SUCCESS);
}
 
bool A2dpSource::ConfigCodec(
    const std::string& device_address,
    const std::vector<A2dpCodecConfig>& codec_preferences) {
  RawAddress addr = PARSE_ADDR(device_address);
  auto fluoride_preferences = CodecConfigsToFluoride(codec_preferences);
  LockGuard lock(mutex_);
  TRY_RET_FUNC(hal::BluetoothAvInterface::Get()
                   ->GetA2dpSourceHALInterface()
                   ->config_codec(addr, fluoride_preferences) ==
               BT_STATUS_SUCCESS);
}
 
void A2dpSource::ConnectionStateCallback(BluetoothAvInterface* iface,
                                         const RawAddress& bd_addr,
                                         btav_connection_state_t state) {
  auto device_address = BtAddrString(&bd_addr);
  LockGuard lock(delegate_mutex_);
  if (delegate_)
    delegate_->OnConnectionState(device_address, static_cast<int>(state));
}
 
void A2dpSource::AudioStateCallback(BluetoothAvInterface* iface,
                                    const RawAddress& bd_addr,
                                    btav_audio_state_t state) {
  auto device_address = BtAddrString(&bd_addr);
  LockGuard lock(delegate_mutex_);
  if (delegate_)
    delegate_->OnAudioState(device_address, static_cast<int>(state));
}
 
void A2dpSource::AudioConfigCallback(
    BluetoothAvInterface* iface, const RawAddress& bd_addr,
    const btav_a2dp_codec_config_t& codec_config_fluoride,
    const std::vector<btav_a2dp_codec_config_t>
        codecs_local_capabilities_fluoride,
    const std::vector<btav_a2dp_codec_config_t>
        codecs_selectable_capabilities_fluoride) {
  auto device_address = BtAddrString(&bd_addr);
  auto codec_config = FluorideCodecToCodec(codec_config_fluoride);
  auto codecs_local_capabilities =
      FluorideCodecsToCodec(codecs_local_capabilities_fluoride);
  auto codecs_selectable_capabilities =
      FluorideCodecsToCodec(codecs_selectable_capabilities_fluoride);
  LockGuard lock(delegate_mutex_);
  if (delegate_)
    delegate_->OnAudioConfig(device_address, codec_config,
                             codecs_local_capabilities,
                             codecs_selectable_capabilities);
}
 
// A2dpSourceFactory implementation
// ========================================================
A2dpSourceFactory::A2dpSourceFactory() = default;
A2dpSourceFactory::~A2dpSourceFactory() = default;
 
bool A2dpSourceFactory::RegisterInstance(const Uuid& uuid,
                                         const RegisterCallback& callback) {
  VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
 
  auto a2dp_source = base::WrapUnique(new A2dpSource(uuid));
  callback(BLE_STATUS_SUCCESS, uuid, std::move(a2dp_source));
  return true;
}
 
}  // namespace bluetooth