17 #include "BluetoothA2DPCommon.h"
23 #define APP_SIG_WORK_DISPATCH (0x01)
25 #ifndef AUTOCONNECT_TRY_NUM
26 #define AUTOCONNECT_TRY_NUM 1
31 #define BT_AV_TAG "BT_AV"
36 BT_APP_EVT_STACK_UP = 0,
39 extern "C" void ccall_app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
40 extern "C" void ccall_app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
41 extern "C" void ccall_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
42 extern "C" void ccall_app_task_handler(
void *arg);
43 extern "C" void ccall_audio_data_callback(
const uint8_t *data, uint32_t len);
44 extern "C" void ccall_av_hdl_stack_evt(uint16_t event,
void *p_param);
45 extern "C" void ccall_av_hdl_a2d_evt(uint16_t event,
void *p_param);
46 extern "C" void ccall_av_hdl_avrc_evt(uint16_t event,
void *p_param);
48 #ifdef CURRENT_ESP_IDF
49 extern "C" void ccall_app_rc_tg_callback(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
50 extern "C" void ccall_av_hdl_avrc_tg_evt(uint16_t event,
void *p_param);
54 enum PinCodeRequest {Undefined, Confirm, Reply};
83 #ifdef CURRENT_ESP_IDF
86 friend void ccall_app_rc_tg_callback(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
88 friend void ccall_av_hdl_avrc_tg_evt(uint16_t event,
void *p_param);
107 virtual void start(
const char* name,
bool auto_reconect);
110 virtual void start(
const char* name);
113 virtual void end(
bool release_memory=
false);
123 this->avrc_metadata_callback = callback;
128 this->sample_rate_callback = callback;
132 virtual void set_stream_reader(
void (*callBack)(
const uint8_t*, uint32_t),
bool i2s_output=
true);
147 address_validator = callBack;
162 virtual void pause();
201 avrc_metadata_flags = flags;
206 swap_left_right = swap;
209 #ifdef CURRENT_ESP_IDF
211 virtual const char* get_connected_source_name();
216 xQueueHandle app_task_queue;
217 xTaskHandle app_task_handle;
218 i2s_config_t i2s_config;
219 i2s_pin_config_t pin_config;
220 const char * bt_name;
221 uint32_t m_pkt_cnt = 0;
223 esp_a2d_mct_t audio_type;
224 char pin_code_str[20];
225 bool is_i2s_output =
true;
226 bool player_init =
false;
227 bool mono_downmix =
false;
228 i2s_channel_t i2s_channels = I2S_CHANNEL_STEREO;
230 int connection_rety_count = 0;
231 esp_bd_addr_t peer_bd_addr = {0};
232 static const esp_spp_mode_t esp_spp_mode = ESP_SPP_MODE_CB;
233 _lock_t s_volume_lock;
234 uint8_t s_volume = 0;
235 bool is_volume_used =
false;
236 bool s_volume_notify;
237 int pin_code_int = 0;
238 PinCodeRequest pin_code_request = Undefined;
239 bool is_pin_code_active =
false;
240 int avrc_metadata_flags = ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_ATTR_ALBUM | ESP_AVRC_MD_ATTR_TRACK_NUM | ESP_AVRC_MD_ATTR_NUM_TRACKS | ESP_AVRC_MD_ATTR_GENRE;
241 void (*bt_volumechange)(int) =
nullptr;
242 void (*bt_dis_connected)() =
nullptr;
243 void (*bt_connected)() =
nullptr;
244 void (*data_received)() =
nullptr;
245 void (*stream_reader)(
const uint8_t*, uint32_t) =
nullptr;
246 void (*avrc_metadata_callback)(uint8_t,
const uint8_t*) =
nullptr;
247 bool (*address_validator)(esp_bd_addr_t remote_bda) =
nullptr;
248 void (*sample_rate_callback)(uint16_t rate)=
nullptr;
249 bool swap_left_right =
false;
251 #ifdef CURRENT_ESP_IDF
252 esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
253 char remote_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
257 virtual int init_bluetooth();
258 virtual void init_i2s();
259 virtual void app_task_start_up(
void);
260 virtual void app_task_shut_down(
void);
261 virtual bool app_send_msg(
app_msg_t *msg);
262 virtual bool app_work_dispatch(app_callback_t p_cback, uint16_t event,
void *p_params,
int param_len);
263 virtual void app_work_dispatched(
app_msg_t *msg);
264 virtual void app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param);
265 virtual void av_new_track();
266 virtual void init_nvs();
268 virtual void execute_avrc_command(
int cmd);
270 virtual const char* last_bda_nvs_name() {
279 virtual void app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
281 virtual void app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
283 virtual void app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
285 virtual void audio_data_callback(
const uint8_t *data, uint32_t len);
287 virtual void av_hdl_stack_evt(uint16_t event,
void *p_param);
289 virtual void av_hdl_a2d_evt(uint16_t event,
void *p_param);
291 virtual void av_hdl_avrc_evt(uint16_t event,
void *p_param);
293 #ifdef CURRENT_ESP_IDF
294 virtual void volume_set_by_local_host(uint8_t volume);
295 virtual void volume_set_by_controller(uint8_t volume);
296 virtual void av_notify_evt_handler(uint8_t& event_id, esp_avrc_rn_param_t& event_parameter);
297 virtual void app_rc_tg_callback(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
298 virtual void av_hdl_avrc_tg_evt(uint16_t event,
void *p_param);
300 virtual void av_notify_evt_handler(uint8_t event_id, uint32_t event_parameter);
Common Bluetooth A2DP functions.
Definition: BluetoothA2DPCommon.h:95
A2DP Bluethooth Sink - We initialize and start the Bluetooth A2DP Sink. The example https://github....
Definition: BluetoothA2DPSink.h:64
virtual uint16_t sample_rate()
Provides the actually set data rate (in samples per second)
Definition: BluetoothA2DPSink.cpp:706
virtual ~BluetoothA2DPSink()
Destructor - stops the playback and releases all resources.
Definition: BluetoothA2DPSink.cpp:54
virtual void set_swap_lr_channels(bool swap)
swaps the left and right channel
Definition: BluetoothA2DPSink.h:205
virtual bool is_connected()
Returns true if the state is connected.
Definition: BluetoothA2DPSink.cpp:242
virtual void activate_pin_code(bool active)
We need to confirm a new seesion by calling confirm_pin_code()
Definition: BluetoothA2DPSink.cpp:1048
virtual void next()
AVRC next.
Definition: BluetoothA2DPSink.cpp:1020
BluetoothA2DPSink()
Constructor.
Definition: BluetoothA2DPSink.cpp:24
friend void ccall_av_hdl_avrc_evt(uint16_t event, void *p_param)
avrc event handler
Definition: BluetoothA2DPSink.cpp:1123
virtual void previous()
AVRC previouse.
Definition: BluetoothA2DPSink.cpp:1023
virtual void app_task_handler(void *arg)
Definition: BluetoothA2DPSink.cpp:354
virtual int pin_code()
provides the requested pin code (0 = undefined)
Definition: BluetoothA2DPSink.h:195
virtual void stop()
AVRC stop.
Definition: BluetoothA2DPSink.cpp:1016
friend void ccall_av_hdl_stack_evt(uint16_t event, void *p_param)
av event handler
Definition: BluetoothA2DPSink.cpp:1116
virtual void set_i2s_config(i2s_config_t i2s_config)
Define the i2s configuration.
Definition: BluetoothA2DPSink.cpp:88
friend void ccall_app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
handle esp_a2d_cb_event_t
Definition: BluetoothA2DPSink.cpp:1098
virtual void set_stream_reader(void(*callBack)(const uint8_t *, uint32_t), bool i2s_output=true)
Define callback which is called when we receive data: This callback provides access to the data.
Definition: BluetoothA2DPSink.cpp:92
virtual void set_volume(uint8_t volume)
Changes the volume.
Definition: BluetoothA2DPSink.cpp:1027
virtual void end(bool release_memory=false)
ends the I2S bluetooth sink with the indicated name - if you release the memory a future start is not...
Definition: BluetoothA2DPSink.cpp:62
virtual void confirm_pin_code()
confirms the connection request by returning the receivedn pin code
Definition: BluetoothA2DPSink.cpp:1052
virtual void set_mono_downmix(bool enabled)
mix stereo into single mono signal
Definition: BluetoothA2DPSink.h:175
virtual esp_a2d_mct_t get_audio_type()
Determine the actual audio type.
Definition: BluetoothA2DPSink.cpp:246
virtual void set_avrc_metadata_attribute_mask(int flags)
defines the requested metadata: eg. ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_AT...
Definition: BluetoothA2DPSink.h:200
friend void ccall_app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
handle esp_avrc_ct_cb_event_t
Definition: BluetoothA2DPSink.cpp:1104
virtual void set_sample_rate_callback(void(*callback)(uint16_t rate))
Defines the method which will be called with the sample rate is updated.
Definition: BluetoothA2DPSink.h:127
virtual esp_err_t i2s_mclk_pin_select(const uint8_t pin)
Defines the pin for the master clock.
Definition: BluetoothA2DPSink.cpp:216
friend void ccall_av_hdl_a2d_evt(uint16_t event, void *p_param)
a2dp event handler
Definition: BluetoothA2DPSink.cpp:1130
virtual void set_bits_per_sample(int bps)
Defines the bits per sample for output (if > 16 output will be expanded)
Definition: BluetoothA2DPSink.h:177
virtual void pause()
AVRC pause.
Definition: BluetoothA2DPSink.cpp:1012
virtual void set_avrc_metadata_callback(void(*callback)(uint8_t, const uint8_t *))
Define a callback method which provides the meta data.
Definition: BluetoothA2DPSink.h:122
virtual void play()
Starts to play music using AVRC.
Definition: BluetoothA2DPSink.cpp:1008
virtual void set_i2s_port(i2s_port_t i2s_num)
Define an alternative i2s port other then 0.
Definition: BluetoothA2DPSink.cpp:84
virtual void set_channels(i2s_channel_t channels)
set output to I2S_CHANNEL_STEREO (default) or I2S_CHANNEL_MONO
Definition: BluetoothA2DPSink.h:171
friend void ccall_audio_data_callback(const uint8_t *data, uint32_t len)
Callback for music stream.
Definition: BluetoothA2DPSink.cpp:1092
virtual void set_on_volumechange(void(*callBack)(int))
Set the callback that is called when they change the volume.
Definition: BluetoothA2DPSink.cpp:109
virtual int get_volume()
Determines the volume.
Definition: BluetoothA2DPSink.cpp:1042
friend void ccall_app_task_handler(void *arg)
task handler
Definition: BluetoothA2DPSink.cpp:1086
friend void ccall_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
GAP callback.
Definition: BluetoothA2DPSink.cpp:1110
virtual void set_address_validator(bool(*callBack)(esp_bd_addr_t remote_bda))
Allows you to reject unauthorized addresses.
Definition: BluetoothA2DPSink.h:146
virtual void set_on_data_received(void(*callBack)())
Define callback which is called when we receive data.
Definition: BluetoothA2DPSink.cpp:97
virtual void set_pin_config(i2s_pin_config_t pin_config)
Define the pins.
Definition: BluetoothA2DPSink.cpp:80
virtual void start(const char *name, bool auto_reconect)
starts the I2S bluetooth sink with the inidicated name
Definition: BluetoothA2DPSink.cpp:113
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition: BluetoothA2DPCommon.h:75