1 /*
2 * Copyright (C) 2021-2022 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 "a2dp_profile.h"
17 #include <cstring>
18 #include "a2dp_avdtp.h"
19 #include "a2dp_codec_thread.h"
20 #include "a2dp_encoder_sbc.h"
21 #include "a2dp_service.h"
22 #include "a2dp_sink.h"
23 #include "a2dp_source.h"
24 #include "adapter_config.h"
25 #include "log.h"
26 #include "log_util.h"
27 #include "profile_service_manager.h"
28 #include "raw_address.h"
29 #include "securec.h"
30
31 namespace OHOS {
32 namespace bluetooth {
33 const int CODEC_SBC_TYPE_INDEX = 2;
34 const int SAMPLE_SBC_INDEX = 3;
35 const int CODEC_LENGTH = 8;
36 const int BIT_MOVE8 = 8;
37 uint8_t A2dpProfile::g_linkNum = 0;
38 bool A2dpProfile::g_registGap = false;
39 std::mutex g_profileMutex;
40 const std::string A2DP_IDLE_ADDRESS = "00:00:00:00:00:00";
41
RegisterServiceSecurity(GAP_ServiceConnectDirection direction,GAP_Service serviceId,GAP_SecMultiplexingProtocol protocolId,GapSecChannel channelId,uint16_t securityMode)42 int A2dpProfile::RegisterServiceSecurity(GAP_ServiceConnectDirection direction, GAP_Service serviceId,
43 GAP_SecMultiplexingProtocol protocolId, GapSecChannel channelId, uint16_t securityMode)
44 {
45 LOG_INFO("[A2dpGap]%{public}s\n", __func__);
46
47 GapServiceSecurityInfo securityInfo = {direction, serviceId, protocolId, channelId};
48 int ret = GAPIF_RegisterServiceSecurity(nullptr, &securityInfo, securityMode);
49
50 return ret;
51 }
52
DeregisterServiceSecurity(GAP_ServiceConnectDirection direction,GAP_Service serviceId)53 int A2dpProfile::DeregisterServiceSecurity(GAP_ServiceConnectDirection direction, GAP_Service serviceId)
54 {
55 LOG_INFO("[A2dpGap]%{public}s\n", __func__);
56
57 GapServiceSecurityInfo securityInfo = {direction, serviceId, SEC_PROTOCOL_L2CAP, {AVDT_PSM}};
58 int ret = GAPIF_DeregisterServiceSecurity(nullptr, &securityInfo);
59
60 return ret;
61 }
62
A2dpProfile(const uint8_t role)63 A2dpProfile::A2dpProfile(const uint8_t role)
64 {
65 LOG_INFO("[A2dpProfile]%{public}s role(%u)\n", __func__, role);
66
67 role_ = role;
68 sdpInstance_.SetProfileRole(role_);
69
70 packetQueue_ = QueueCreate(MAX_PCM_FRAME_NUM_PER_TICK * FRAME_THREE);
71 buffer_ = new A2dpSharedBuffer();
72 }
73
~A2dpProfile()74 A2dpProfile::~A2dpProfile()
75 {
76 LOG_INFO("[A2dpProfile]%{public}s\n", __func__);
77
78 for (const auto &it : peers_) {
79 delete it.second;
80 }
81 if (packetQueue_ != nullptr) {
82 QueueDelete(packetQueue_, CleanPacketData);
83 packetQueue_ = nullptr;
84 }
85 if (buffer_ != nullptr) {
86 delete buffer_;
87 buffer_ = nullptr;
88 }
89 peers_.clear();
90 }
91
GetRole() const92 uint8_t A2dpProfile::GetRole() const
93 {
94 std::lock_guard<std::mutex> lock(g_profileMutex);
95
96 LOG_INFO("[A2dpProfile]%{public}s role(%u)\n", __func__, role_);
97
98 return role_;
99 }
100
GetProfileEnable() const101 bool A2dpProfile::GetProfileEnable() const
102 {
103 std::lock_guard<std::mutex> lock(g_profileMutex);
104
105 LOG_INFO("[A2dpProfile]%{public}s enable(%{public}d)\n", __func__, enable_);
106
107 return enable_;
108 }
109
SetProfileEnable(bool value)110 void A2dpProfile::SetProfileEnable(bool value)
111 {
112 std::lock_guard<std::mutex> lock(g_profileMutex);
113
114 LOG_INFO("[A2dpProfile]%{public}s enableValue(%{public}d)\n", __func__, value);
115
116 enable_ = value;
117 }
118
SetActivePeer(const BtAddr & peer)119 void A2dpProfile::SetActivePeer(const BtAddr &peer)
120 {
121 LOG_INFO("[A2dpProfile]%{public}s\n", __func__);
122 std::lock_guard<std::mutex> lock(g_profileMutex);
123 activePeer_ = peer;
124 }
125
GetActivePeer() const126 const BtAddr &A2dpProfile::GetActivePeer() const
127 {
128 LOG_INFO("[A2dpProfile]%{public}s\n", __func__);
129 std::lock_guard<std::mutex> lock(g_profileMutex);
130 return activePeer_;
131 }
132
Enable()133 void A2dpProfile::Enable()
134 {
135 AvdtRegisterParam avdtRcb = {};
136 uint8_t role = GetRole();
137 A2dpAvdtp avdtpInstance(role);
138 CodecInfo codecInfo = {};
139
140 (void)memset_s(&codecInfo, sizeof(CodecInfo), 0, sizeof(CodecInfo));
141 avdtRcb.mtu = A2DP_MAX_AVDTP_MTU_SIZE;
142 avdtRcb.role = AVDT_ROLE_SRC;
143 avdtpInstance.RegisterAvdtp(avdtRcb);
144
145 if (GetProfileEnable()) {
146 return;
147 }
148
149 if (!GetGapRegisterInfo()) {
150 UpdateGapRegisterInfo(true);
151 RegisterServiceSecurity(INCOMING, GAVDP_ACP,
152 SEC_PROTOCOL_L2CAP, { .l2capPsm = AVDT_PSM },
153 GAP_SEC_IN_AUTHENTICATION | GAP_SEC_OUT_AUTHENTICATION);
154 RegisterServiceSecurity(OUTGOING, GAVDP_INT,
155 SEC_PROTOCOL_L2CAP, { .l2capPsm = AVDT_PSM },
156 GAP_SEC_IN_AUTHENTICATION | GAP_SEC_OUT_AUTHENTICATION);
157 }
158
159 // register local stream endpoint
160 if (role == A2DP_ROLE_SOURCE) {
161 CreateSEPConfigureInfo(A2DP_ROLE_SOURCE);
162 } else {
163 CreateSEPConfigureInfo(A2DP_ROLE_SINK);
164 }
165
166 GetSDPInstance().RegisterService();
167 SetProfileEnable(true);
168 }
169
ProcessAvdtpCallback(const BtAddr & addr,const utility::Message & message)170 void A2dpProfile::ProcessAvdtpCallback(const BtAddr &addr, const utility::Message &message)
171 {
172 A2dpAvdtMsg *msg = static_cast<A2dpAvdtMsg*>(message.arg2_);
173 uint8_t role = GetRole();
174 uint8_t event = msg->event;
175 A2dpProfilePeer *peer = FindOrCreatePeer(addr, role);
176 if (peer == nullptr) {
177 LOG_ERROR("[A2dpService] %{public}s Failed to get peer instance\n", __func__);
178 return;
179 }
180 A2dpAvdtp avdtpInstance(role);
181 A2dpAvdtMsg *msgParsed =
182 avdtpInstance.ParseAvdtpCallbackContent(msg->handle, addr, msg->event, msg->a2dpMsg.msg, role);
183
184 if (msgParsed != nullptr) {
185 msgParsed->role = role;
186 utility::Message data(msgParsed->event, 0, msgParsed);
187 LOG_INFO("[A2dpProfile] %{public}s cmd(%u)\n", __func__, msgParsed->event);
188 peer->GetStateMachine()->ProcessMessage(data);
189 delete msgParsed;
190 msgParsed = nullptr;
191 } else {
192 if (event == AVDT_CONNECT_IND_EVT) {
193 DeletePeer(addr);
194 }
195 }
196 }
197
ConnectStateChangedNotify(const BtAddr & addr,const int state,void * context)198 void A2dpProfile::ConnectStateChangedNotify(const BtAddr &addr, const int state, void *context)
199 {
200 LOG_INFO("[A2dpProfile] %{public}s state(%{public}d)\n", __func__, state);
201 switch (state) {
202 case STREAM_CONNECT_FAILED:
203 case STREAM_DISCONNECT:
204 ResetDelayValue(addr);
205 DeletePeer(addr);
206 if (IsActiveDevice(addr)) {
207 ClearActiveDevice();
208 }
209 QueueFlush(packetQueue_, CleanPacketData);
210 buffer_->Reset();
211 break;
212 case STREAM_CONNECT:
213 SetActivePeer(addr);
214 break;
215 default:
216 break;
217 }
218 if (a2dpSvcCBack_ != nullptr) {
219 a2dpSvcCBack_->OnConnectStateChanged(addr, state, context);
220 } else {
221 LOG_ERROR("[A2dpProfile] %{public}s Can't find observer objest \n", __func__);
222 }
223 }
224
AudioStateChangedNotify(const BtAddr & addr,const int state,void * context) const225 void A2dpProfile::AudioStateChangedNotify(const BtAddr &addr, const int state, void *context) const
226 {
227 LOG_INFO("[A2dpProfile] %{public}s state(%{public}d)\n", __func__, state);
228 if (state == A2DP_IS_PLAYING) {
229 buffer_->SetValid(true);
230 } else {
231 buffer_->SetValid(false);
232 }
233 if (a2dpSvcCBack_ != nullptr) {
234 a2dpSvcCBack_->OnAudioStateChanged(addr, state, context);
235 } else {
236 LOG_ERROR("[A2dpProfile] %{public}s Can't find observer objest \n", __func__);
237 }
238 }
239
CodecChangedNotify(const BtAddr & addr,void * context) const240 void A2dpProfile::CodecChangedNotify(const BtAddr &addr, void *context) const
241 {
242 LOG_INFO("[A2dpProfile] %{public}s", __func__);
243 A2dpProfilePeer *peer = FindPeerByAddress(addr);
244
245 if (a2dpSvcCBack_ != nullptr && peer != nullptr) {
246 a2dpSvcCBack_->OnCodecStateChanged(addr, peer->GetCodecStatus(), context);
247 } else {
248 LOG_ERROR("[A2dpProfile] %{public}s Can't find observer objest \n", __func__);
249 }
250 }
251
SetUserCodecConfigure(const BtAddr & addr,const A2dpSrcCodecInfo & info)252 int A2dpProfile::SetUserCodecConfigure(const BtAddr &addr, const A2dpSrcCodecInfo &info)
253 {
254 LOG_INFO("[A2dpProfile] %{public}s", __func__);
255 int ret;
256 uint8_t role = GetRole();
257 A2dpProfile *profile = GetProfileInstance(role);
258 if (profile == nullptr) {
259 LOG_ERROR("[A2dpProfile] %{public}s Can't find the instance of profile\n", __func__);
260 ret = RET_NO_SUPPORT;
261 return ret;
262 }
263
264 A2dpProfilePeer *peer = profile->FindPeerByAddress(addr);
265 if (peer == nullptr) {
266 LOG_ERROR("[A2dpProfile] %{public}s Can't find the instance of peer\n", __func__);
267 ret = RET_BAD_PARAM;
268 return ret;
269 }
270
271 ret = peer->SetUserCodecConfigure(info);
272
273 return ret;
274 }
275
IsActiveDevice(const BtAddr & addr) const276 bool A2dpProfile::IsActiveDevice(const BtAddr &addr) const
277 {
278 bool ret = false;
279 RawAddress rawAddress = RawAddress::ConvertToString(addr.addr);
280 RawAddress activeAddr = RawAddress::ConvertToString(GetActivePeer().addr);
281 std::lock_guard<std::mutex> lock(g_profileMutex);
282
283 HILOGI("[A2dpProfile] current active address(%{public}s)", GetEncryptAddr(activeAddr.GetAddress()).c_str());
284 if (strcmp(activeAddr.GetAddress().c_str(), rawAddress.GetAddress().c_str()) == 0) {
285 HILOGI("[A2dpProfile] connect address(%{public}s)", GetEncryptAddr(rawAddress.GetAddress()).c_str());
286 ret = true;
287 }
288 return ret;
289 }
290
ClearActiveDevice()291 void A2dpProfile::ClearActiveDevice()
292 {
293 std::lock_guard<std::mutex> lock(g_profileMutex);
294
295 (void)memset_s(&activePeer_, sizeof(BtAddr), 0, sizeof(BtAddr));
296 }
297
EnableOptionalCodec(const BtAddr & addr,bool value)298 bool A2dpProfile::EnableOptionalCodec(const BtAddr &addr, bool value)
299 {
300 LOG_INFO("[A2dpProfile] %{public}s\n", __func__);
301 bool ret = true;
302 A2dpProfilePeer *peer = FindPeerByAddress(addr);
303 if (peer != nullptr) {
304 ret = peer->EnableOptionalCodec(value);
305 } else {
306 LOG_ERROR("[A2dpProfile] %{public}s Can't get the device of peer\n", __func__);
307 }
308
309 return ret;
310 }
311
312
NotifyEncoder(const BtAddr & addr) const313 void A2dpProfile::NotifyEncoder(const BtAddr &addr) const
314 {
315 LOG_INFO("[A2dpProfile] %{public}s\n", __func__);
316 A2dpProfilePeer *peer = FindPeerByAddress(addr);
317 if (peer != nullptr) {
318 peer->NotifyEncoder();
319 } else {
320 LOG_ERROR("[A2dpProfile] %{public}s Can't get the device of peer\n", __func__);
321 }
322 }
323
NotifyDecoder(const BtAddr & addr) const324 void A2dpProfile::NotifyDecoder(const BtAddr &addr) const
325 {
326 LOG_INFO("[A2dpProfile] %{public}s\n", __func__);
327
328 A2dpProfilePeer *peer = FindPeerByAddress(addr);
329 if (peer != nullptr) {
330 peer->NotifyDecoder();
331 } else {
332 LOG_ERROR("[A2dpProfile] %{public}s Can't get the device of peer\n", __func__);
333 }
334 }
335
SetPcmData(const uint8_t * buf,uint32_t size)336 uint32_t A2dpProfile::SetPcmData(const uint8_t *buf, uint32_t size)
337 {
338 LOG_INFO("[A2dpProfile] %{public}s %{public}u\n", __func__, size);
339
340 uint32_t actualWrittenBytes = buffer_->Write(buf, size);
341 LOG_INFO("[A2dpProfile] %{public}s expectedWrittenBytes(%{public}u) actualWrittenBytes(%{public}u)\n",
342 __func__, size, actualWrittenBytes);
343 if (actualWrittenBytes == 0) {
344 LOG_ERROR("[A2dpProfile] %{public}s failed\n", __func__);
345 }
346 return actualWrittenBytes;
347 }
348
GetPcmData(uint8_t * buf,uint32_t size)349 uint32_t A2dpProfile::GetPcmData(uint8_t *buf, uint32_t size)
350 {
351 LOG_INFO("[A2dpProfile] %{public}s\n", __func__);
352
353 uint32_t actualReadBytes = buffer_->Read(buf, size);
354 LOG_INFO("[A2dpProfile] %{public}s expectedReadBytes(%{public}u) actualReadBytes(%{public}u)\n",
355 __func__, size, actualReadBytes);
356 if (actualReadBytes == 0) {
357 LOG_ERROR("[A2dpProfile] %{public}s failed\n", __func__);
358 return false;
359 }
360 return actualReadBytes;
361 }
362
GetRenderPosition(uint32_t & delayValue,uint64_t & sendDataSize,uint32_t & timeStamp)363 void A2dpProfile::GetRenderPosition(uint32_t &delayValue, uint64_t &sendDataSize, uint32_t &timeStamp)
364 {
365 LOG_INFO("[A2dpProfile] %{public}s\n", __func__);
366
367 A2dpCodecThread *codecThread = A2dpCodecThread::GetInstance();
368 delayValue = delayValue_;
369 codecThread->GetRenderPosition(sendDataSize, timeStamp);
370 }
371
DequeuePacket()372 void A2dpProfile::DequeuePacket()
373 {
374 LOG_INFO("[A2dpProfile] %{public}s \n", __func__);
375 A2dpProfilePeer *peer = FindOrCreatePeer(activePeer_, A2DP_ROLE_SOURCE);
376 if (peer != nullptr) {
377 int32_t count = QueueGetSize(packetQueue_);
378 LOG_INFO("[A2dpProfile] %{public}s QueueTryDequeue starts, count %{public}d \n", __func__, count);
379 PacketData *packetData = (PacketData *)QueueTryDequeue(packetQueue_);
380 if (packetData == nullptr) {
381 LOG_ERROR("[A2dpProfile] %{public}s no data\n", __func__);
382 return;
383 }
384 count = QueueGetSize(packetQueue_);
385 LOG_INFO("[A2dpProfile] %{public}s packetData is not null, QueueTryDequeue ends, count %{public}d\n",
386 __func__, count);
387 peer->SendPacket(packetData->packet, packetData->frames, packetData->bytes, packetData->pktTimeStamp);
388 PacketFree(packetData->packet);
389 free(packetData);
390 packetData = nullptr;
391 } else {
392 LOG_ERROR("[A2dpProfile] %{public}s no peer\n", __func__);
393 }
394 }
395
EnqueuePacket(const Packet * packet,size_t frames,uint32_t bytes,uint32_t pktTimeStamp)396 void A2dpProfile::EnqueuePacket(const Packet *packet, size_t frames, uint32_t bytes, uint32_t pktTimeStamp)
397 {
398 Packet *refpkt = nullptr;
399 PacketData *packetData = nullptr;
400 int32_t count = QueueGetSize(packetQueue_);
401 LOG_INFO("[A2dpProfile] %{public}s, frame %{public}u, count %{public}d \n", __func__, bytes, count);
402 if (count >= MAX_PCM_FRAME_NUM_PER_TICK * FRAME_THREE) {
403 QueueFlush(packetQueue_, CleanPacketData);
404 }
405 count = QueueGetSize(packetQueue_);
406 LOG_INFO("[A2dpProfile] %{public}s, after flush count %{public}d \n", __func__, count);
407 refpkt = PacketRefMalloc((Packet *)packet);
408 packetData = (PacketData *)malloc(sizeof(PacketData));
409 if (packetData == nullptr) {
410 LOG_ERROR("[A2dpProfile] %{public}s, packetData is NULL\n", __func__);
411 return;
412 }
413 packetData->packet = refpkt;
414 packetData->frames = frames;
415 packetData->bytes = bytes;
416 packetData->pktTimeStamp = pktTimeStamp;
417
418 QueueEnqueue(packetQueue_, (void *)packetData);
419 count = QueueGetSize(packetQueue_);
420 LOG_INFO("[A2dpProfile] %{public}s ends count %{public}d \n", __func__, count);
421 }
422
CreateSEPConfigureInfo(uint8_t role)423 void A2dpProfile::CreateSEPConfigureInfo(uint8_t role)
424 {
425 AvdtStreamConfig cfg[AVDT_NUM_SEPS] = {};
426 A2dpAvdtp avdtp(role);
427 uint8_t number = 0;
428
429 for (int i = A2DP_SOURCE_CODEC_INDEX_SBC; i < A2DP_CODEC_INDEX_MAX; i++) {
430 int value = 0x01;
431 LOG_INFO("[A2dpProfile]%{public}s index (%{public}d)\n", __func__, i);
432 if (i < A2DP_SINK_CODEC_INDEX_MIN) {
433 cfg[number].sepType = AVDT_ROLE_SRC;
434 cfg[number].cfg.mediaType = A2DP_MEDIA_TYPE_AUDIO;
435 if (A2DP_SOURCE_CODEC_INDEX_SBC == i) {
436 AdapterConfig::GetInstance()->GetValue(SECTION_A2DP_SRC_SERVICE, PROPERTY_CODEC_SBC_SUPPORT, value);
437 LOG_INFO("[A2dpProfile] %{public}s SRC SBC value[%{public}d] \n", __func__, value);
438 if (!value) {
439 continue;
440 }
441 BuildCodecInfo(A2DP_SOURCE_CODEC_INDEX_SBC, cfg[number].cfg.codecInfo);
442 cfg[number].cfg.numCodec = A2DP_CODEC_SBC_INFO_LEN;
443 } else if (A2DP_SOURCE_CODEC_INDEX_AAC == i) {
444 AdapterConfig::GetInstance()->GetValue(SECTION_A2DP_SRC_SERVICE, PROPERTY_CODEC_AAC_SUPPORT, value);
445 LOG_INFO("[A2dpProfile] %{public}s SRC AAC value[%{public}d] \n", __func__, value);
446 if (!value) {
447 continue;
448 }
449 BuildCodecInfo(A2DP_SOURCE_CODEC_INDEX_AAC, cfg[number].cfg.codecInfo);
450 cfg[number].cfg.numCodec = A2DP_CODEC_AAC_INFO_LEN;
451 }
452 cfg[number].codecIndex = i;
453 cfg[number].sinkDataCback = ProcessSinkStream;
454 cfg[number].cfg.pscMask = (AVDT_PSC_MSK_TRANS | AVDT_PSC_MSK_DELAY_RPT | AVDT_PSC_MSK_CODEC);
455 } else {
456 cfg[number].sepType = AVDT_ROLE_SNK;
457 cfg[number].cfg.mediaType = A2DP_MEDIA_TYPE_AUDIO;
458 if (A2DP_SINK_CODEC_INDEX_SBC == i) {
459 AdapterConfig::GetInstance()->GetValue(SECTION_A2DP_SNK_SERVICE, PROPERTY_CODEC_SBC_SUPPORT, value);
460 LOG_INFO("[A2dpProfile] %{public}s SNK SBC value[%{public}d] \n", __func__, value);
461 if (!value) {
462 continue;
463 }
464 BuildCodecInfo(A2DP_SINK_CODEC_INDEX_SBC, cfg[number].cfg.codecInfo);
465 cfg[number].cfg.numCodec = A2DP_CODEC_SBC_INFO_LEN;
466 } else if (A2DP_SINK_CODEC_INDEX_AAC == i) {
467 AdapterConfig::GetInstance()->GetValue(SECTION_A2DP_SNK_SERVICE, PROPERTY_CODEC_AAC_SUPPORT, value);
468 LOG_INFO("[A2dpProfile] %{public}s SNK AAC value[%{public}d] \n", __func__, value);
469 if (!value) {
470 continue;
471 }
472 BuildCodecInfo(A2DP_SINK_CODEC_INDEX_AAC, cfg[number].cfg.codecInfo);
473 cfg[number].cfg.numCodec = A2DP_CODEC_AAC_INFO_LEN;
474 }
475 cfg[number].codecIndex = i;
476 cfg[number].sinkDataCback = ProcessSinkStream;
477 cfg[number].cfg.pscMask = (AVDT_PSC_MSK_TRANS | AVDT_PSC_MSK_DELAY_RPT | AVDT_PSC_MSK_CODEC);
478 }
479 number++;
480 }
481 avdtp.RegisterLocalSEP(cfg, number);
482 }
483
BuildCodecInfo(A2dpCodecIndex index,uint8_t * data)484 void A2dpProfile::BuildCodecInfo(A2dpCodecIndex index, uint8_t *data)
485 {
486 LOG_INFO("[A2dpProfile]%{public}s index(%u)\n", __func__, index);
487 switch (index) {
488 case A2DP_SINK_CODEC_INDEX_SBC:
489 *data++ = A2DP_CODEC_SBC_INFO_LEN;
490 *data++ = A2DP_MEDIA_TYPE_AUDIO;
491 *data++ = A2DP_CODEC_TYPE_SBC;
492 *data++ = A2DP_SBC_SINK_CAPS.sampleFreq | A2DP_SBC_SINK_CAPS.channelMode;
493 *data++ = A2DP_SBC_SINK_CAPS.blockLen | A2DP_SBC_SINK_CAPS.numSubbands |
494 A2DP_SBC_SINK_CAPS.allocatedMethod;
495 *data++ = A2DP_SBC_SINK_CAPS.minBitpool;
496 *data = A2DP_SBC_SINK_CAPS.maxBitpool;
497 break;
498 case A2DP_SOURCE_CODEC_INDEX_SBC:
499 *data++ = A2DP_CODEC_SBC_INFO_LEN;
500 *data++ = A2DP_MEDIA_TYPE_AUDIO;
501 *data++ = A2DP_CODEC_TYPE_SBC;
502 *data++ = A2DP_SBC_SOURCE_CAPS.sampleFreq | A2DP_SBC_SOURCE_CAPS.channelMode;
503 *data++ = A2DP_SBC_SOURCE_CAPS.blockLen | A2DP_SBC_SOURCE_CAPS.numSubbands |
504 A2DP_SBC_SOURCE_CAPS.allocatedMethod;
505 *data++ = A2DP_SBC_SOURCE_CAPS.minBitpool;
506 *data = A2DP_SBC_SOURCE_CAPS.maxBitpool;
507 break;
508 case A2DP_SINK_CODEC_INDEX_AAC:
509 case A2DP_SOURCE_CODEC_INDEX_AAC:
510 *data++ = A2DP_CODEC_AAC_INFO_LEN;
511 *data++ = A2DP_MEDIA_TYPE_AUDIO;
512 *data++ = A2DP_CODEC_TYPE_AAC;
513 *data++ = A2DP_AAC_MPEG2_OBJECT_TYPE; // object type
514 *data++ = A2DP_AAC_SAMPLE_RATE_OCTET1_44100; // octet1
515 *data++ = (A2DP_AAC_SAMPLE_RATE_OCTET2_48000 >> BIT_MOVE8) | A2DP_AAC_CHANNEL_MODE_OCTET2_DOUBLE |
516 A2DP_AAC_CHANNEL_MODE_OCTET2_SINGLE;
517 // octet2: b4~7:sampleFrequency b2: dual channels b3: single channel
518 *data++ = A2DP_AAC_VARIABLE_BIT_RATE_DISABLED; // octect3 b7: vbr b6 ~b0: bitrate22 ~ 16
519 *data++ = 0x00; // octet 4 bit rate 8 ~ 15
520 *data = A2DP_SAMPLE_BITS_16; // octet 5 bitrate 0 ~7
521 break;
522 default:
523 break;
524 }
525 }
526
Disable()527 void A2dpProfile::Disable()
528 {
529 LOG_INFO("[A2dpProfile]%{public}s enable(%{public}d) role(%u)\n", __func__, GetProfileEnable(), GetRole());
530
531 A2dpAvdtMsg data = {};
532 utility::Message msg(EVT_DISCONNECT_REQ, 0, &data);
533 A2dpProfilePeer *peer = nullptr;
534 uint8_t role = GetRole();
535
536 if (!GetProfileEnable()) {
537 return;
538 }
539 SetProfileEnable(false);
540
541 for (const auto &it : peers_) {
542 peer = it.second;
543 HILOGI("[A2dpProfile] matched peers_addr(%{public}s) [role%u]\n", GetEncryptAddr(it.first).c_str(), role);
544 if (peer != nullptr && it.first.c_str() != nullptr) {
545 data.a2dpMsg.connectInfo.addr = peer->GetPeerAddress();
546 data.role = role;
547 msg.arg2_ = &data;
548 peer->GetStateMachine()->ProcessMessage(msg);
549 peer->SetCurrentCmd(EVT_DISCONNECT_REQ);
550 break;
551 }
552 }
553 if (GetGapRegisterInfo()) {
554 UpdateGapRegisterInfo(false);
555 DeregisterServiceSecurity(INCOMING, GAVDP_ACP);
556 DeregisterServiceSecurity(OUTGOING, GAVDP_INT);
557 }
558
559 GetSDPInstance().UnregisterService();
560 ClearNumberPeerDevice();
561
562 QueueFlush(packetQueue_, CleanPacketData);
563 buffer_->Reset();
564 }
565
GetSDPInstance(void) const566 A2dpSdpManager A2dpProfile::GetSDPInstance(void) const
567 {
568 std::lock_guard<std::mutex> lock(g_profileMutex);
569
570 return sdpInstance_;
571 }
GetConfigure() const572 const ConfigureStream &A2dpProfile::GetConfigure() const
573 {
574 LOG_INFO("[A2dpProfile]%{public}s\n", __func__);
575 std::lock_guard<std::mutex> lock(g_profileMutex);
576 return configureStream_;
577 }
578
SetConfigure(const BtAddr & addr)579 void A2dpProfile::SetConfigure(const BtAddr &addr)
580 {
581 std::lock_guard<std::mutex> lock(g_profileMutex);
582
583 A2dpProfilePeer *peer = FindPeerByAddress(addr);
584 if (peer != nullptr) {
585 (void)memset_s(&configureStream_, sizeof(ConfigureStream), 0, sizeof(ConfigureStream));
586 configureStream_.cfg = peer->GetPeerCapabilityInfo();
587 configureStream_.intSeid = peer->GetIntSeid();
588 configureStream_.acpSeid = peer->GetAcpSeid();
589 configureStream_.addr = addr;
590 }
591
592 LOG_INFO("[A2dpProfile]%{public}s AcpSeid(%u) intseid(%u)\n",
593 __func__, configureStream_.acpSeid, configureStream_.intSeid);
594 }
595
Connect(const BtAddr & device)596 int A2dpProfile::Connect(const BtAddr &device)
597 {
598 LOG_INFO("[A2dpProfile]%{public}s\n", __func__);
599
600 A2dpProfile *a2dpInstance = nullptr;
601 uint8_t role = GetRole();
602 if (role == A2DP_ROLE_SOURCE) {
603 a2dpInstance = A2dpSrcProfile::GetInstance();
604 } else {
605 a2dpInstance = A2dpSnkProfile::GetInstance();
606 }
607 FindOrCreatePeer(device, role);
608 if (GetSDPInstance().FindSnkService(device, a2dpInstance, A2dpProfilePeer::SDPServiceCallback) != BT_SUCCESS) {
609 LOG_WARN("[A2dpProfile]%{public}s SDP_ServiceSearch Error\n", __func__);
610 }
611 return A2DP_SUCCESS;
612 }
613
ProcessSDPCallback(const BtAddr & addr,uint8_t result)614 void A2dpProfile::ProcessSDPCallback(const BtAddr &addr, uint8_t result)
615 {
616 if (result != A2DP_SUCCESS) {
617 CallbackParameter param = {A2DP_ROLE_INT, true, 0};
618 ConnectStateChangedNotify(addr, STREAM_DISCONNECT, (void *)¶m);
619 LOG_INFO("[A2dpProfile]%{public}s Failed to get SDPresult(%hhu)\n", __func__, result);
620 return;
621 }
622
623 /// Check sdp information
624 uint8_t role = GetRole();
625 A2dpAvdtMsg data = {};
626 A2dpProfilePeer *peer = FindOrCreatePeer(addr, role);
627 utility::Message msg(EVT_CONNECT_REQ, 0, &data);
628
629 data.a2dpMsg.connectInfo.addr = addr;
630 data.role = role;
631 msg.arg2_ = &data;
632 if (peer == nullptr) {
633 LOG_ERROR("[A2dpProfile]%{public}s Not resources", __func__);
634 } else {
635 if (!peer->GetSDPServiceCapability()) {
636 msg.arg1_ = EVT_SDP_CFM;
637 LOG_ERROR("[A2dpProfile]%{public}s Can't find peer service matched. role(%u)", __func__, data.role);
638 }
639 peer->GetStateMachine()->ProcessMessage(msg);
640 peer->SetCurrentCmd(EVT_CONNECT_REQ);
641 peer->SetInitSide(true);
642 }
643 }
644
Disconnect(const BtAddr & device)645 int A2dpProfile::Disconnect(const BtAddr &device)
646 {
647 int ret = 0;
648 A2dpProfilePeer *peer = nullptr;
649 A2dpAvdtMsg data = {};
650 utility::Message msg(EVT_DISCONNECT_REQ, 0, &data);
651
652 data.a2dpMsg.connectInfo.addr = device;
653 data.role = GetRole();
654 msg.arg2_ = &data;
655 peer = FindPeerByAddress(device);
656 if (peer == nullptr) {
657 ret = AVDT_NO_RESOURCES;
658 LOG_ERROR("[A2dpProfile]%{public}s Not resources", __func__);
659 } else {
660 if (peer->GetCurrentCmd() != EVT_SDP_DISC && peer->GetCurrentCmd() != EVT_CONNECT_REQ) {
661 ret = peer->GetStateMachine()->ProcessMessage(msg);
662 peer->SetCurrentCmd(EVT_DISCONNECT_REQ);
663 } else {
664 peer->SetDisconnectIndication(true);
665 }
666 }
667
668 return ret;
669 }
670
Stop(const uint16_t handle,const bool suspend)671 int A2dpProfile::Stop(const uint16_t handle, const bool suspend)
672 {
673 LOG_INFO("[A2dpProfile]%{public}s handle(%u) suspend(%{public}d)\n", __func__, handle, suspend);
674 int ret = BT_SUCCESS;
675 A2dpProfilePeer *peer = nullptr;
676 A2dpAvdtMsg data = {};
677 utility::Message msg(EVT_SUSPEND_REQ, 0, &data);
678
679 data.a2dpMsg.stream.handle = handle;
680 data.role = GetRole();
681 msg.arg2_ = &data;
682
683 peer = FindPeerByHandle(handle);
684 if (peer == nullptr) {
685 ret = AVDT_BAD_PARAMS;
686 LOG_ERROR("[A2dpProfile]%{public}s invalid handle(%u) suspend(%{public}d)\n", __func__, handle, suspend);
687 return ret;
688 }
689
690 if (strcmp(A2DP_PROFILE_STREAMING.c_str(), peer->GetStateMachine()->GetStateName().c_str()) == 0) {
691 LOG_INFO("[A2dpProfile]%{public}s streaming exited now\n", __func__);
692 peer->GetStateMachine()->ProcessMessage(msg);
693 } else {
694 LOG_ERROR("[A2dpProfile]%{public}s Not streaming status now\n", __func__);
695 ret = AVDT_ERR_UNSUPPORTED_COMMAND;
696 }
697
698 QueueFlush(packetQueue_, CleanPacketData);
699 buffer_->Reset();
700 return ret;
701 }
702
Start(const uint16_t handle)703 int A2dpProfile::Start(const uint16_t handle)
704 {
705 LOG_INFO("[A2dpProfile]%{public}s handle(%hu)\n", __func__, handle);
706
707 int ret = BT_SUCCESS;
708 uint8_t role = GetRole();
709 A2dpProfilePeer *peer = nullptr;
710 A2dpAvdtMsg data = {};
711 utility::Message msg(EVT_START_REQ, 0, &data);
712
713 data.a2dpMsg.stream.handle = handle;
714 data.role = role;
715 msg.arg2_ = &data;
716 peer = FindPeerByHandle(handle);
717 if (peer == nullptr) {
718 ret = AVDT_BAD_PARAMS;
719 return ret;
720 }
721 if (strcmp(A2DP_PROFILE_STREAMING.c_str(), peer->GetStateMachine()->GetStateName().c_str()) == 0) {
722 LOG_INFO("[A2dpProfile]%{public}s The state of stream is streaming now\n", __func__);
723 if (role == A2DP_ROLE_SOURCE) {
724 peer->NotifyEncoder();
725 } else if (role == A2DP_ROLE_SINK) {
726 peer->NotifyDecoder();
727 } else {
728 LOG_ERROR("[A2dpProfile]%{public}s Audio data is not ready\n", __func__);
729 }
730 return BT_SUCCESS;
731 }
732 if (!JudgeAllowedStreaming()) {
733 return AVDT_NO_RESOURCES;
734 }
735 peer->GetStateMachine()->ProcessMessage(msg);
736
737 return ret;
738 }
739
JudgeAllowedStreaming() const740 bool A2dpProfile::JudgeAllowedStreaming() const
741 {
742 bool ret = true;
743 A2dpProfilePeer *peerProfile = nullptr;
744
745 for (const auto &it : peers_) {
746 peerProfile = it.second;
747 if (peerProfile != nullptr) {
748 LOG_INFO("[A2dpProfile]%{public}s state(%{public}s)\n",
749 __func__, peerProfile->GetStateMachine()->GetStateName().c_str());
750 if (strcmp(A2DP_PROFILE_STREAMING.c_str(), peerProfile->GetStateMachine()->GetStateName().c_str()) == 0) {
751 LOG_ERROR("[A2dpProfile]%{public}s there is one streaming exited now\n", __func__);
752 ret = false;
753 break;
754 }
755 }
756 }
757 return ret;
758 }
759
Close(const uint16_t handle) const760 int A2dpProfile::Close(const uint16_t handle) const
761 {
762 int ret = BT_SUCCESS;
763 A2dpProfilePeer *peer = nullptr;
764 A2dpAvdtMsg data = {};
765 utility::Message msg(EVT_CLOSE_REQ, 0, &data);
766
767 data.a2dpMsg.stream.handle = handle;
768 data.role = GetRole();
769 msg.arg2_ = &data;
770 peer = FindPeerByHandle(handle);
771 if (peer == nullptr) {
772 ret = AVDT_BAD_PARAMS;
773 return ret;
774 }
775
776 if (strcmp(A2DP_PROFILE_OPEN.c_str(), peer->GetStateMachine()->GetStateName().c_str()) == 0 ||
777 strcmp(A2DP_PROFILE_STREAMING.c_str(), peer->GetStateMachine()->GetStateName().c_str()) == 0) {
778 LOG_INFO("[A2dpProfile]%{public}s open/streaming exited now\n", __func__);
779 peer->GetStateMachine()->ProcessMessage(msg);
780 } else {
781 LOG_ERROR("[A2dpProfile]%{public}s Not open/streaming status now\n", __func__);
782 ret = AVDT_ERR_UNSUPPORTED_COMMAND;
783 }
784 return ret;
785 }
786
787
HasStreaming()788 bool A2dpProfile::HasStreaming()
789 {
790 A2dpProfilePeer *peer = nullptr;
791 for (const auto &it : peers_) {
792 LOG_INFO("[A2dpProfile]%{public}s handle(%u)\n", __func__, it.second->GetStreamHandle());
793 peer = it.second;
794 if (peer != nullptr) {
795 if (strcmp(A2DP_PROFILE_STREAMING.c_str(), peer->GetStateMachine()->GetStateName().c_str()) == 0) {
796 LOG_INFO("[A2dpProfile]%{public}s streaming exited now\n", __func__);
797 return true;
798 }
799 }
800 }
801 return false;
802 }
803
HasOpen()804 bool A2dpProfile::HasOpen()
805 {
806 A2dpProfilePeer *peer = nullptr;
807 for (const auto &it : peers_) {
808 LOG_INFO("[A2dpProfile]%{public}s \n", __func__);
809 peer = it.second;
810 if (peer != nullptr) {
811 if (strcmp(A2DP_PROFILE_OPEN.c_str(), peer->GetStateMachine()->GetStateName().c_str()) == 0) {
812 LOG_INFO("[A2dpProfile]%{public}s open exited now\n", __func__);
813 return true;
814 }
815 }
816 }
817 return false;
818 }
819
SetDisalbeTag(bool tag)820 void A2dpProfile::SetDisalbeTag(bool tag)
821 {
822 LOG_INFO("[A2dpProfile]%{public}s isDoDisable(%u)\n", __func__, tag);
823 isDoDisable_ = tag;
824 }
825
GetDisalbeTag()826 bool A2dpProfile::GetDisalbeTag()
827 {
828 LOG_INFO("[A2dpProfile]%{public}s isDoDisable(%u)\n", __func__, isDoDisable_);
829 return isDoDisable_;
830 }
831
CloseAll()832 void A2dpProfile::CloseAll()
833 {
834 for (const auto &it : peers_) {
835 LOG_INFO("[A2dpProfile]%{public}s handle(%u)\n", __func__, it.second->GetStreamHandle());
836 if (it.second != nullptr) {
837 Close(it.second->GetStreamHandle());
838 }
839 }
840 }
841
SendDelay(const uint16_t handle,const uint16_t delayValue)842 int A2dpProfile::SendDelay(const uint16_t handle, const uint16_t delayValue)
843 {
844 LOG_INFO("[A2dpProfile]%{public}s handle(%u)\n", __func__, handle);
845 A2dpAvdtp avdtp(A2DP_ROLE_SINK);
846 uint8_t label = 0;
847
848 int ret = avdtp.DelayReq(handle, label, delayValue);
849 return ret;
850 }
851
CloseRsp(const uint16_t handle,const uint8_t errorCode) const852 int A2dpProfile::CloseRsp(const uint16_t handle, const uint8_t errorCode) const
853 {
854 LOG_INFO("[A2dpProfile]%{public}s handle(%u) role(%u)\n", __func__, handle, GetRole());
855 A2dpAvdtp avdtp(GetRole());
856 uint8_t label = 0;
857
858 int ret = avdtp.CloseRsp(handle, label, errorCode);
859 return ret;
860 }
861
WriteStream(const uint16_t handle,const uint8_t config,const uint8_t bitpool)862 int A2dpProfile::WriteStream(const uint16_t handle, const uint8_t config, const uint8_t bitpool)
863 {
864 LOG_INFO("[A2dpProfile]%{public}s handle(%u)\n", __func__, handle);
865
866 A2dpAvdtp avdtp(A2DP_ROLE_SOURCE);
867 uint32_t timeStamp = 0x8FF;
868 Packet *pkt = PacketMalloc(0, 0, 0);
869 uint8_t pktType = 0;
870 uint16_t marker = 0x0B;
871 uint8_t codecInfo[CODEC_LENGTH] = {0x01, 0x9c, 0x00, 0x35, 0x00, 0x88, 0x88, 0x88};
872
873 codecInfo[CODEC_SBC_TYPE_INDEX] = config;
874 codecInfo[SAMPLE_SBC_INDEX] = bitpool;
875 Buffer *encBuf = BufferMalloc(CODEC_LENGTH);
876 (void)memcpy_s(BufferPtr(encBuf), CODEC_LENGTH, codecInfo, CODEC_LENGTH);
877 PacketPayloadAddLast(pkt, encBuf);
878
879 int ret = avdtp.WriteStream(handle, pkt, timeStamp, pktType, marker);
880 return ret;
881 }
882
Reconfigure(const uint16_t handle,uint8_t * codecInfo) const883 int A2dpProfile::Reconfigure(const uint16_t handle, uint8_t *codecInfo) const
884 {
885 LOG_INFO("[A2dpProfile]%{public}s handle(%u)\n", __func__, handle);
886
887 int ret = BT_SUCCESS;
888 A2dpProfilePeer *peer = nullptr;
889 A2dpAvdtMsg data = {};
890 utility::Message msg(EVT_RECONFIG_REQ, 0, &data);
891
892 peer = FindPeerByHandle(handle);
893 if (peer == nullptr) {
894 ret = AVDT_BAD_PARAMS;
895 LOG_ERROR("[A2dpProfile]%{public}s invalid handle(%u) \n", __func__, handle);
896 return ret;
897 }
898
899 (void)memcpy_s(data.a2dpMsg.configStream.cfg.codecInfo, A2DP_CODEC_SIZE, codecInfo,
900 sizeof(data.a2dpMsg.configStream.cfg.codecInfo));
901 data.a2dpMsg.configStream.cfg.mediaType = A2DP_MEDIA_TYPE_AUDIO;
902 data.a2dpMsg.configStream.cfg.numCodec = A2DP_CODEC_SBC_INFO_LEN;
903 data.a2dpMsg.configStream.cfg.pscMask = AVDT_PSC_MSK_CODEC;
904 data.a2dpMsg.configStream.intSeid = handle;
905 data.role = GetRole();
906 msg.arg2_ = &data;
907
908 if (strcmp(A2DP_PROFILE_OPEN.c_str(), peer->GetStateMachine()->GetStateName().c_str()) == 0) {
909 LOG_INFO("[A2dpProfile]%{public}s streaming exited now\n", __func__);
910 peer->GetStateMachine()->ProcessMessage(msg);
911 } else {
912 LOG_ERROR("[A2dpProfile]%{public}s Not open status now\n", __func__);
913 ret = AVDT_ERR_UNSUPPORTED_COMMAND;
914 }
915
916 return ret;
917 }
918
DelayReportNotify(const BtAddr & device,const uint16_t delayValue)919 void A2dpProfile::DelayReportNotify(const BtAddr &device, const uint16_t delayValue)
920 {
921 LOG_INFO("[A2dpProfile]%{public}s delayValue(%{public}u)\n", __func__, delayValue);
922
923 A2dpProfilePeer *peer = nullptr;
924 peer = FindPeerByAddress(device);
925 if (peer == nullptr) {
926 LOG_ERROR("[A2dpProfile]%{public}s Not resources", __func__);
927 return;
928 }
929
930 delayValue_ = delayValue;
931 }
932
ResetDelayValue(const BtAddr & device)933 void A2dpProfile::ResetDelayValue(const BtAddr &device)
934 {
935 LOG_INFO("[A2dpProfile]%{public}s", __func__);
936
937 A2dpProfilePeer *peer = nullptr;
938 peer = FindPeerByAddress(device);
939 if (peer == nullptr) {
940 LOG_ERROR("[A2dpProfile]%{public}s Not resources", __func__);
941 return;
942 }
943 delayValue_ = 0;
944 }
945
RegisterObserver(A2dpProfileObserver * observer)946 void A2dpProfile::RegisterObserver(A2dpProfileObserver *observer)
947 {
948 LOG_INFO("[A2dpProfile]%{public}s\n", __func__);
949
950 a2dpSvcCBack_ = observer;
951 return;
952 }
953
DeregisterObserver(A2dpProfileObserver * observer)954 void A2dpProfile::DeregisterObserver(A2dpProfileObserver *observer)
955 {
956 LOG_INFO("[A2dpProfile]%{public}s\n", __func__);
957 return;
958 }
959
FindPeerByAddress(const BtAddr & peerAddress) const960 A2dpProfilePeer *A2dpProfile::FindPeerByAddress(const BtAddr &peerAddress) const
961 {
962 HILOGI("[A2dpProfile] addr(%{public}s)",
963 GetEncryptAddr(RawAddress::ConvertToString(peerAddress.addr).GetAddress()).c_str());
964 A2dpProfilePeer *peer = nullptr;
965
966 for (const auto &it : peers_) {
967 if (strcmp(it.first.c_str(), RawAddress::ConvertToString(peerAddress.addr).GetAddress().c_str()) == 0) {
968 peer = it.second;
969 HILOGI("[A2dpProfile] matched peers_addr(%{public}s)\n", GetEncryptAddr(it.first).c_str());
970 return peer;
971 }
972 HILOGI("[A2dpProfile] peers_addr(%{public}s)\n", GetEncryptAddr(it.first).c_str());
973 }
974 return peer;
975 }
976
FindPeerByHandle(uint16_t handle) const977 A2dpProfilePeer *A2dpProfile::FindPeerByHandle(uint16_t handle) const
978 {
979 LOG_INFO("[A2dpProfile]%{public}s handle(%u)\n", __func__, handle);
980 A2dpProfilePeer *peer = nullptr;
981
982 for (const auto &it : peers_) {
983 LOG_INFO("[A2dpProfile]%{public}s handle(%u)\n", __func__, it.second->GetStreamHandle());
984 if (handle == it.second->GetStreamHandle()) {
985 peer = it.second;
986 break;
987 }
988 }
989 return peer;
990 }
991
FindOrCreatePeer(const BtAddr & peerAddress,uint8_t localRole)992 A2dpProfilePeer *A2dpProfile::FindOrCreatePeer(const BtAddr &peerAddress, uint8_t localRole)
993 {
994 A2dpProfilePeer *peer = nullptr;
995 std::string addr = RawAddress::ConvertToString(peerAddress.addr).GetAddress();
996 peer = FindPeerByAddress(peerAddress);
997 if (peer != nullptr) {
998 LOG_DEBUG("[A2dpProfile]%{public}s Peer is exited", __func__);
999 return peer;
1000 }
1001 if (UpdateNumberPeerDevice(true)) {
1002 auto peerInstance = std::make_unique<A2dpProfilePeer>(peerAddress, localRole);
1003 peer = peerInstance.release();
1004 peers_.insert(std::make_pair(addr, peer));
1005 HILOGI("addr: %{public}s", GetEncryptAddr(addr).c_str());
1006 } else {
1007 HILOGE("Connected device is max: %{public}u", GetConnectedPeerDevice());
1008 }
1009 return peer;
1010 }
1011
DeletePeer(const BtAddr & peerAddress)1012 bool A2dpProfile::DeletePeer(const BtAddr &peerAddress)
1013 {
1014 bool ret = false;
1015 std::map<std::string, A2dpProfilePeer *>::iterator it;
1016
1017 if (IsActiveDevice(peerAddress)) {
1018 A2dpCodecThread *codecThread = A2dpCodecThread::GetInstance();
1019 if (codecThread != nullptr) {
1020 codecThread->StopA2dpCodecThread();
1021 delete codecThread;
1022 }
1023 }
1024
1025 for (it = peers_.begin(); it != peers_.end(); it++) {
1026 if (strcmp(it->first.c_str(), RawAddress::ConvertToString(peerAddress.addr).GetAddress().c_str()) == 0) {
1027 delete it->second;
1028 peers_.erase(it);
1029 UpdateNumberPeerDevice(false);
1030 ret = true;
1031 HILOGI("[A2dpProfile] matched peers_addr(%{public}s)",
1032 GetEncryptAddr(RawAddress::ConvertToString(peerAddress.addr).GetAddress()).c_str());
1033 break;
1034 }
1035 HILOGI("[A2dpProfile] peers_addr(%{public}s)\n", GetEncryptAddr(it->first).c_str());
1036 }
1037
1038 return ret;
1039 }
1040
UpdateNumberPeerDevice(bool plus)1041 bool A2dpProfile::UpdateNumberPeerDevice(bool plus)
1042 {
1043 std::lock_guard<std::mutex> lock(g_profileMutex);
1044 LOG_INFO("[A2dpProfile]%{public}s plus(%{public}d)\n", __func__, plus);
1045
1046 if (plus) {
1047 g_linkNum++;
1048 } else {
1049 if (g_linkNum > 0) {
1050 g_linkNum--;
1051 }
1052 }
1053 if (g_linkNum > A2DP_CONNECT_NUM_MAX) {
1054 LOG_ERROR("[A2dpProfile]%{public}s linkNum(%u)\n", __func__, g_linkNum);
1055 g_linkNum = A2DP_CONNECT_NUM_MAX;
1056 return false;
1057 }
1058 LOG_INFO("[A2dpProfile]%{public}s linkNum(%u)\n", __func__, g_linkNum);
1059 return true;
1060 }
1061
GetConnectedPeerDevice()1062 uint8_t A2dpProfile::GetConnectedPeerDevice()
1063 {
1064 std::lock_guard<std::mutex> lock(g_profileMutex);
1065 LOG_INFO("[A2dpProfile]%{public}s \n", __func__);
1066
1067 return g_linkNum;
1068 }
1069
ClearNumberPeerDevice()1070 void A2dpProfile::ClearNumberPeerDevice()
1071 {
1072 std::lock_guard<std::mutex> lock(g_profileMutex);
1073 LOG_INFO("[A2dpProfile]%{public}s \n", __func__);
1074
1075 g_linkNum = 0;
1076 }
1077
UpdateGapRegisterInfo(bool value)1078 void A2dpProfile::UpdateGapRegisterInfo(bool value)
1079 {
1080 std::lock_guard<std::mutex> lock(g_profileMutex);
1081 LOG_INFO("[A2dpProfile]%{public}s register(%{public}d) \n", __func__, value);
1082
1083 g_registGap = value;
1084 }
1085
GetGapRegisterInfo()1086 bool A2dpProfile::GetGapRegisterInfo()
1087 {
1088 std::lock_guard<std::mutex> lock(g_profileMutex);
1089 LOG_INFO("[A2dpProfile]%{public}s register(%{public}d) \n", __func__, g_registGap);
1090
1091 return g_registGap;
1092 }
1093
ProcessSignalingTimeoutCallback(const BtAddr & addr) const1094 void A2dpProfile::ProcessSignalingTimeoutCallback(const BtAddr &addr) const
1095 {
1096 LOG_INFO("[A2dpProfile]%{public}s \n", __func__);
1097
1098 A2dpProfilePeer *peer = FindPeerByAddress(addr);
1099 if (peer != nullptr) {
1100 peer->ProcessSignalingTimeoutCallback();
1101 }
1102 }
1103
ProcessSinkStream(uint16_t handle,Packet * pkt,uint32_t timeStamp,uint8_t pt,uint16_t streamHandle)1104 void ProcessSinkStream(uint16_t handle, Packet *pkt, uint32_t timeStamp, uint8_t pt, uint16_t streamHandle)
1105 {
1106 LOG_INFO("[A2dpProfile]%{public}s \n", __func__);
1107
1108 A2dpProfile *profile = GetProfileInstance(A2DP_ROLE_SINK);
1109 if (profile == nullptr) {
1110 LOG_ERROR("[A2dpProfile]%{public}s Can't find the instance of profile \n", __func__);
1111 return;
1112 }
1113 A2dpProfilePeer *peer = profile->FindPeerByHandle(handle);
1114 if (peer == nullptr) {
1115 LOG_ERROR("[A2dpProfile]%{public}s Can't find the instance of peer device \n", __func__);
1116 return;
1117 }
1118 if (strcmp(A2DP_PROFILE_STREAMING.c_str(), peer->GetStateMachine()->GetStateName().c_str()) != 0) {
1119 LOG_ERROR("[A2dpProfile]%{public}s Peer statemachine is not Streaming \n", __func__);
1120 return;
1121 }
1122
1123 A2dpCodecThread *codecThread = A2dpCodecThread::GetInstance();
1124 if (codecThread == nullptr) {
1125 LOG_ERROR("[A2dpProfile]%{public}s Can't find the instance of codec \n", __func__);
1126 return;
1127 }
1128
1129 uint16_t packetSize = PacketSize(pkt);
1130 if (packetSize == 0) {
1131 return;
1132 }
1133 auto frameData = (uint8_t *)malloc(packetSize);
1134 if (frameData != nullptr) {
1135 (void)memset_s(frameData, packetSize, 0, packetSize);
1136 PacketRead(pkt, frameData, 0, packetSize);
1137 A2dpEncoderInitPeerParams peerParams = {};
1138 utility::Message msg(A2DP_FRAME_READY, packetSize, frameData);
1139 codecThread->PostMessage(msg, peerParams, nullptr, nullptr);
1140 }
1141 }
1142
CleanPacketData(void * data)1143 void CleanPacketData(void *data)
1144 {
1145 PacketData *packetData = (PacketData *)data;
1146 PacketFree(packetData->packet);
1147 free(packetData);
1148 }
1149 } // namespace bluetooth
1150 } // namespace OHOS
1151