1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 #ifndef __APP_BT_STREAM_H__
5 #define __APP_BT_STREAM_H__
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #include "hal_aud.h"
12 #include "stdint.h"
13 #define BT_AUDIO_BUFF_AAC_SIZE (1024*2*2*2)
14 
15 #if defined(__3M_PACK__)
16 #define BT_AUDIO_BUFF_SBC_SIZE (128*7*2*2*2)
17 #else
18 #define BT_AUDIO_BUFF_SBC_SIZE (128*5*2*2*2)
19 #endif
20 
21 #if defined(A2DP_LDAC_ON)
22 #define BT_AUDIO_BUFF_SIZE_LDAC (256*4*2*2*2)
23 #endif
24 
25 #define BT_AUDIO_CACHE_2_UNCACHE(addr) \
26     ((unsigned char *)((unsigned int)addr & ~(0x04000000)))
27 
28 // #if BT_AUDIO_BUFF_SIZE < BT_AUDIO_SCO_BUFF_SIZE * 4
29 // #error BT_AUDIO_BUFF_SIZE must be at least BT_AUDIO_SCO_BUFF_SIZE * 4
30 // #endif
31 
32 // low 11 bits are output streams index while high 5 bits are input streams index
33 // one output stream and one input stream can exit at the same time,
34 // while two streams with the same direction are mutex
35 #define APP_BT_STREAM_BORDER_BIT_OFFSET     12
36 // the index < the border is the output stream
37 // the index >= the border is the input stream
38 #define APP_BT_STREAM_BORDER_INDEX          (1 << APP_BT_STREAM_BORDER_BIT_OFFSET)
39 
40 // output streams
41 #define APP_BT_STREAM_HFP_PCM               (1 << 0)
42 #define APP_BT_STREAM_HFP_CVSD              (1 << 1)
43 #define APP_BT_STREAM_HFP_VENDOR            (1 << 2)
44 #define APP_BT_STREAM_A2DP_SBC              (1 << 3)
45 #define APP_BT_STREAM_A2DP_AAC              (1 << 4)
46 #define APP_BT_STREAM_A2DP_VENDOR           (1 << 5)
47 #ifdef __FACTORY_MODE_SUPPORT__
48 #define APP_FACTORYMODE_AUDIO_LOOP          (1 << 6)
49 #endif
50 #define APP_PLAY_BACK_AUDIO                 (1 << 7)
51 #ifdef RB_CODEC
52 #define APP_BT_STREAM_RBCODEC               (1 << 8)
53 #endif
54 #ifdef AUDIO_LINEIN
55 #define APP_PLAY_LINEIN_AUDIO               (1 << 9)
56 #endif
57 #if defined(APP_LINEIN_A2DP_SOURCE)||defined(__APP_A2DP_SOURCE__)||(APP_I2S_A2DP_SOURCE)
58 #define APP_A2DP_SOURCE_LINEIN_AUDIO        (1 << 10)
59 #define APP_A2DP_SOURCE_I2S_AUDIO           (1 << 11)
60 #endif
61 
62 
63 // input streams
64 #ifdef VOICE_DATAPATH
65 #define APP_BT_STREAM_VOICEPATH             (1 << APP_BT_STREAM_BORDER_BIT_OFFSET)
66 #endif
67 
68 #ifdef __AI_VOICE__
69 #define APP_BT_STREAM_AI_VOICE             (1 << (APP_BT_STREAM_BORDER_BIT_OFFSET+1))
70 #endif
71 
72 #define APP_BT_STREAM_INVALID               0
73 
74 #define APP_BT_OUTPUT_STREAM_INDEX(player)  ((player) & ((1 << APP_BT_STREAM_BORDER_BIT_OFFSET) - 1))
75 #define APP_BT_INPUT_STREAM_INDEX(player)   ((player) & (~((1 << APP_BT_STREAM_BORDER_BIT_OFFSET) - 1)))
76 
77 #include <stdint.h>
78 
79 enum APP_BT_SETTING_T {
80     APP_BT_SETTING_OPEN = 0,
81     APP_BT_SETTING_CLOSE,
82     APP_BT_SETTING_SETUP,
83     APP_BT_SETTING_RESTART,
84     APP_BT_SETTING_CLOSEALL,
85     APP_BT_SETTING_CLOSEMEDIA,
86     APP_BT_SETTING_NUM,
87 };
88 
89 enum APP_BT_AUDIO_Q_POS {
90     APP_BT_SETTING_Q_POS_HEAD = 0,
91     APP_BT_SETTING_Q_POS_TAIL = 1,
92 };
93 
94 typedef struct {
95     uint16_t id;
96     uint16_t status;
97 
98     uint16_t aud_type;
99     uint16_t aud_id;
100 
101     uint8_t freq;
102 }APP_AUDIO_STATUS;
103 
104 const char *player2str(uint16_t player_type);
105 
106 int app_bt_stream_open(APP_AUDIO_STATUS* status);
107 
108 int app_bt_stream_close(uint16_t player);
109 
110 int app_bt_stream_setup(uint16_t player, uint8_t status);
111 
112 int app_bt_stream_restart(APP_AUDIO_STATUS* status);
113 
114 int app_bt_stream_closeall();
115 
116 bool app_bt_stream_isrun(uint16_t player);
117 
118 void app_bt_set_volume(uint16_t type,uint8_t level);
119 
120 void app_bt_stream_volumeup(void);
121 
122 void app_bt_stream_volumedown(void);
123 
124 void app_bt_stream_volume_ptr_update(uint8_t *bdAddr);
125 
126 struct btdevice_volume * app_bt_stream_volume_get_ptr(void);
127 
128 int app_bt_stream_volumeset(int8_t vol);
129 
130 uint8_t app_bt_stream_a2dpvolume_get(void);
131 
132 uint8_t app_bt_stream_hfpvolume_get(void);
133 
134 void app_bt_stream_a2dpvolume_reset(void);
135 
136 void app_bt_stream_hfpvolume_reset(void);
137 
138 void app_bt_stream_copy_track_one_to_two_24bits(int32_t *dst_buf, int32_t *src_buf, uint32_t src_len);
139 
140 void app_bt_stream_copy_track_one_to_two_16bits(int16_t *dst_buf, int16_t *src_buf, uint32_t src_len);
141 
142 void app_bt_stream_copy_track_two_to_one_16bits(int16_t *dst_buf, int16_t *src_buf, uint32_t dst_len);
143 
144 enum AUD_SAMPRATE_T bt_get_sbc_sample_rate(void);
145 
146 void bt_store_sbc_sample_rate(enum AUD_SAMPRATE_T sample_rate);
147 
148 enum AUD_SAMPRATE_T bt_parse_store_sbc_sample_rate(uint8_t sbc_samp_rate);
149 
150 bool app_bt_stream_is_mic_mix_during_music_enabled(void);
151 void app_bt_sream_set_mix_mic_flag(bool isEnable);
152 void bt_sbc_mix_mic_init(void);
153 void bt_sbc_mix_mic_deinit(void);
154 uint32_t bt_sbc_codec_capture_data(uint8_t *buf, uint32_t len);
155 
156 void app_audio_buffer_check(void);
157 
158 // =======================================================
159 // APP RESAMPLE
160 // =======================================================
161 
162 #include "hal_aud.h"
163 
164 typedef int (*APP_RESAMPLE_BUF_ALLOC_CALLBACK)(uint8_t **buff, uint32_t size);
165 
166 typedef int (*APP_RESAMPLE_ITER_CALLBACK)(uint8_t *buf, uint32_t len);
167 
168 struct APP_RESAMPLE_T {
169     enum AUD_STREAM_T stream;
170     void *id;
171     APP_RESAMPLE_ITER_CALLBACK cb;
172     uint8_t *iter_buf;
173     uint32_t iter_len;
174     uint32_t offset;
175     float ratio_step;
176 };
177 
178 struct APP_RESAMPLE_T *app_playback_resample_open(enum AUD_SAMPRATE_T sample_rate, enum AUD_CHANNEL_NUM_T chans,
179                                                   APP_RESAMPLE_ITER_CALLBACK cb, uint32_t iter_len);
180 struct APP_RESAMPLE_T *app_playback_resample_any_open(enum AUD_CHANNEL_NUM_T chans,
181                                                       APP_RESAMPLE_ITER_CALLBACK cb, uint32_t iter_len,
182                                                       float ratio_step);
183 struct APP_RESAMPLE_T *app_playback_resample_any_open_with_pre_allocated_buffer(enum AUD_CHANNEL_NUM_T chans,
184         APP_RESAMPLE_ITER_CALLBACK cb, uint32_t iter_len,
185         float ratio_step, uint8_t* ptrBuf, uint32_t bufSize);
186 int app_playback_resample_close(struct APP_RESAMPLE_T *resamp);
187 int app_playback_resample_run(struct APP_RESAMPLE_T *resamp, uint8_t *buf, uint32_t len);
188 
189 struct APP_RESAMPLE_T *app_capture_resample_open(enum AUD_SAMPRATE_T sample_rate, enum AUD_CHANNEL_NUM_T chans,
190                                                  APP_RESAMPLE_ITER_CALLBACK cb, uint32_t iter_len);
191 struct APP_RESAMPLE_T *app_capture_resample_any_open(enum AUD_CHANNEL_NUM_T chans,
192                                                      APP_RESAMPLE_ITER_CALLBACK cb, uint32_t iter_len,
193                                                      float ratio_step);
194 int app_capture_resample_close(struct APP_RESAMPLE_T *resamp);
195 int app_capture_resample_run(struct APP_RESAMPLE_T *resamp, uint8_t *buf, uint32_t len);
196 
197 void app_resample_reset(struct APP_RESAMPLE_T *resamp);
198 void app_resample_tune(struct APP_RESAMPLE_T *resamp, float ratio);
199 APP_RESAMPLE_BUF_ALLOC_CALLBACK app_resample_set_buf_alloc_callback(APP_RESAMPLE_BUF_ALLOC_CALLBACK cb);
200 uint16_t app_bt_stream_ibrt_trigger_seq_diff_calc(int32_t dma_samples, int32_t frame_samples, int32_t sub_seq, int32_t interval);
201 void app_bt_stream_volumeset_handler(int8_t vol);
202 
203 #ifdef TX_RX_PCM_MASK
204 void store_encode_frame2buff();
205 #endif
206 
207 #ifdef BONE_SENSOR_TDM
208 void bt_sco_get_tdm_buffer(uint8_t **buf, uint32_t *len);
209 #endif
210 int app_bt_stream_local_volume_get(void);
211 
212 bool bt_is_playback_triggered(void);
213 
214 struct APP_RESAMPLE_T *app_force48k_resample_any_open(enum AUD_CHANNEL_NUM_T chans,
215         APP_RESAMPLE_ITER_CALLBACK cb, uint32_t iter_len,
216         float ratio_step);
217 
218 #ifdef __cplusplus
219 }
220 #endif
221 
222 #endif
223