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 "../include/a2dp_decoder_sbc.h"
17 #include "log.h"
18 #include "memory.h"
19 #include "securec.h"
20 
21 namespace OHOS {
22 namespace bluetooth {
23 const int CODEC_BUFFER_SIZE4K = 4096;
24 
25 sbc::CodecParam g_sbcDecode = {};
26 
A2dpSbcDecoder(A2dpDecoderObserver * observer)27 A2dpSbcDecoder::A2dpSbcDecoder(A2dpDecoderObserver *observer) : A2dpDecoder(observer)
28 {
29     LOG_INFO("[SbcDecoder] %{public}s\n", __func__);
30     g_sbcDecode.endian = sbc::SBC_ENDIANESS_LE;
31     codecLib_ = std::make_unique<A2dpSBCDynamicLibCtrl>(false);
32     codecSbcDecoderLib_ = codecLib_->LoadCodecSbcLib();
33     if (codecSbcDecoderLib_ == nullptr) {
34         LOG_ERROR("[SbcDecoder] %{public}s load sbc lib failed\n", __func__);
35     } else {
36         sbcDecoder_ = codecSbcDecoderLib_->sbcDecoder.createSbcDecode();
37     }
38 }
39 
~A2dpSbcDecoder()40 A2dpSbcDecoder::~A2dpSbcDecoder()
41 {
42     LOG_INFO("[SbcDecoder] %{public}s\n", __func__);
43     if (codecSbcDecoderLib_ != nullptr) {
44         codecSbcDecoderLib_->sbcDecoder.destroySbcDecode(sbcDecoder_);
45         codecLib_->UnloadCodecSbcLib(codecSbcDecoderLib_);
46     }
47 }
48 
DecodePacket(uint8_t * data,uint16_t size)49 bool A2dpSbcDecoder::DecodePacket(uint8_t *data, uint16_t size)
50 {
51     uint8_t pcmData[CODEC_BUFFER_SIZE4K];
52     size_t len = 0;
53     int pos = 0x01;
54     int frameLen = 0;
55     uint16_t packetSize = size;
56     uint8_t buf[CODEC_BUFFER_SIZE4K] = {0};
57     (void)memcpy_s(buf, CODEC_BUFFER_SIZE4K, data, packetSize);
58     (void)memset_s(pcmData, CODEC_BUFFER_SIZE4K, 0, CODEC_BUFFER_SIZE4K);
59     int count = len;
60     while (true) {
61         if (count + len > CODEC_BUFFER_SIZE4K) {
62             LOG_INFO(" count > buf");
63             break;
64         }
65 
66         if (pos >= packetSize) {
67             LOG_INFO("pos > packetSize");
68             break;
69         }
70 
71         frameLen = sbcDecoder_->SBCDecode(g_sbcDecode, &buf[pos], packetSize - pos,
72             &pcmData[count], sizeof(pcmData) - count, &len);
73         pos += frameLen;
74         if (frameLen < 0) {
75             LOG_ERROR(" frame  length is err");
76             break;
77         }
78         count += len;
79     }
80     LOG_INFO("[DataAvailable][packetSize:%hu][count:%{public}d][len:%{public}zu][frameLen:%{public}d]",
81         packetSize, count, len, frameLen);
82 
83     if (count > 0) {
84         observer_->DataAvailable(pcmData, count);
85     }
86     return true;
87 }
88 }  // namespace bluetooth
89 }  // namespace OHOS