• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "hfp_lc3_encoder"
18 
19 #include <bluetooth/log.h>
20 
21 #include "hfp_lc3_encoder.h"
22 #include "mmc/codec_client/codec_client.h"
23 #include "mmc/proto/mmc_config.pb.h"
24 #include "os/log.h"
25 
26 using namespace bluetooth;
27 
28 const int HFP_LC3_PCM_BYTES = 480;
29 const int HFP_LC3_PKT_FRAME_LEN = 58;
30 
31 static mmc::CodecClient* client = nullptr;
32 
hfp_lc3_encoder_init()33 void hfp_lc3_encoder_init() {
34   hfp_lc3_encoder_cleanup();
35   client = new mmc::CodecClient;
36 
37   const int dt_us = 7500;
38   const int sr_hz = 32000;
39   const int sr_pcm_hz = 32000;
40 
41   mmc::Lc3Param param;
42   param.set_dt_us(dt_us);
43   param.set_sr_hz(sr_hz);
44   param.set_sr_pcm_hz(sr_pcm_hz);
45   param.set_stride(1);
46   param.set_fmt(mmc::Lc3Param::kLc3PcmFormatS16);
47 
48   mmc::ConfigParam config;
49   *config.mutable_hfp_lc3_encoder_param() = param;
50 
51   int ret = client->init(config);
52   if (ret < 0) {
53     log::error("Init failed with error message, {}", strerror(-ret));
54   }
55   return;
56 }
57 
hfp_lc3_encoder_cleanup()58 void hfp_lc3_encoder_cleanup() {
59   if (client) {
60     client->cleanup();
61     delete client;
62     client = nullptr;
63   }
64 }
65 
hfp_lc3_encode_frames(int16_t * input,uint8_t * output)66 uint32_t hfp_lc3_encode_frames(int16_t* input, uint8_t* output) {
67   if (input == nullptr || output == nullptr) {
68     log::error("Buffer is null");
69     return 0;
70   }
71 
72   if (!client) {
73     log::error("CodecClient has not been initialized");
74     return 0;
75   }
76 
77   int rc = client->transcode((uint8_t*)input, HFP_LC3_PCM_BYTES, output,
78                              HFP_LC3_PKT_FRAME_LEN);
79 
80   if (rc < 0) {
81     log::warn("Encode failed with error message, {}", strerror(-rc));
82     return 0;
83   }
84 
85   return HFP_LC3_PCM_BYTES;
86 }
87