19#include "freertos/ringbuf.h"
25#define APP_SIG_WORK_DISPATCH (0x01)
28#define BT_AV_TAG "BT_AV"
33 BT_APP_EVT_STACK_UP = 0,
36extern "C" void ccall_app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
37extern "C" void ccall_app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
38extern "C" void ccall_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
39extern "C" void ccall_app_task_handler(
void *arg);
40extern "C" void ccall_i2s_task_handler(
void *arg);
41extern "C" void ccall_audio_data_callback(
const uint8_t *data, uint32_t len);
42extern "C" void ccall_av_hdl_stack_evt(uint16_t event,
void *p_param);
43extern "C" void ccall_av_hdl_a2d_evt(uint16_t event,
void *p_param);
44extern "C" void ccall_av_hdl_avrc_evt(uint16_t event,
void *p_param);
47extern "C" void ccall_app_rc_tg_callback(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
48extern "C" void ccall_av_hdl_avrc_tg_evt(uint16_t event,
void *p_param);
52enum PinCodeRequest {Undefined, Confirm, Reply};
87 friend void ccall_app_rc_tg_callback(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
89 friend void ccall_av_hdl_avrc_tg_evt(uint16_t event,
void *p_param);
108 virtual void start(
const char* name,
bool auto_reconect);
111 virtual void start(
const char* name);
114 virtual void end(
bool release_memory=
false);
124 this->avrc_metadata_callback = callback;
129 this->sample_rate_callback = callback;
133 virtual void set_stream_reader(
void (*callBack)(
const uint8_t*, uint32_t),
bool i2s_output=
true);
143 address_validator = callBack;
158 virtual void pause();
203 avrc_metadata_flags = flags;
208 swap_left_right = swap;
213 reconnect_status =
reconnect ? AutoReconnect : NoReconnect;
214 try_reconnect_max_count = count;
219 return &peer_bd_addr;
225 rssi_active = active;
230 return last_rssi_delta;
235 rssi_callbak = callback;
240 reconnect_delay = delay;
245 virtual const char* get_connected_source_name();
247 bool is_avrc_peer_rn_cap(esp_avrc_rn_event_ids_t cmd) {
248 return esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap, cmd);
251 bool is_avrc_peer_rn_cap_available() {
252 return s_avrc_peer_rn_cap.bits != 0;
258 xQueueHandle app_task_queue =
nullptr;
259 xTaskHandle app_task_handle =
nullptr;
261 i2s_config_t i2s_config;
262 i2s_pin_config_t pin_config;
263 const char * bt_name =
nullptr;
264 uint32_t m_pkt_cnt = 0;
266 esp_a2d_mct_t audio_type;
267 char pin_code_str[20] = {0};
268 bool is_i2s_output =
true;
269 bool player_init =
false;
270 i2s_channel_t i2s_channels = I2S_CHANNEL_STEREO;
271 i2s_port_t i2s_port = I2S_NUM_0;
272 int connection_rety_count = 0;
273 static const esp_spp_mode_t esp_spp_mode = ESP_SPP_MODE_CB;
274 _lock_t s_volume_lock;
275 uint8_t s_volume = 0;
276 bool s_volume_notify;
277 int pin_code_int = 0;
278 PinCodeRequest pin_code_request = Undefined;
279 bool is_pin_code_active =
false;
280 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;
281 void (*bt_volumechange)(int) =
nullptr;
282 void (*bt_dis_connected)() =
nullptr;
283 void (*bt_connected)() =
nullptr;
284 void (*data_received)() =
nullptr;
285 void (*stream_reader)(
const uint8_t*, uint32_t) =
nullptr;
286 void (*raw_stream_reader)(
const uint8_t*, uint32_t) =
nullptr;
287 void (*avrc_metadata_callback)(uint8_t,
const uint8_t*) =
nullptr;
288 bool (*address_validator)(esp_bd_addr_t remote_bda) =
nullptr;
289 void (*sample_rate_callback)(uint16_t rate)=
nullptr;
290 bool swap_left_right =
false;
291 bool is_i2s_active =
false;
292 int try_reconnect_max_count = AUTOCONNECT_TRY_NUM;
295 esp_bt_gap_cb_param_t::read_rssi_delta_param last_rssi_delta;
296 bool rssi_active =
false;
297 void (*rssi_callbak)(esp_bt_gap_cb_param_t::read_rssi_delta_param &rssi) =
nullptr;
298 int reconnect_delay = 1000;
301 esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
302 char remote_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
306 virtual int init_bluetooth();
307 virtual void init_i2s();
308 virtual void app_task_start_up(
void);
309 virtual void app_task_shut_down(
void);
310 virtual bool app_send_msg(
app_msg_t *msg);
311 virtual bool app_work_dispatch(
app_callback_t p_cback, uint16_t event,
void *p_params,
int param_len);
312 virtual void app_work_dispatched(
app_msg_t *msg);
313 virtual void app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param);
314 virtual void av_new_track();
315 virtual void init_nvs();
317 virtual void execute_avrc_command(
int cmd);
319 virtual const char* last_bda_nvs_name() {
323 virtual bool is_reconnect(esp_a2d_disc_rsn_t type) {
324 bool result = is_autoreconnect_allowed && (reconnect_status==AutoReconnect || reconnect_status==IsReconnecting) && has_last_connection();
325 ESP_LOGI(BT_AV_TAG,
"is_reconnect: %s", result ?
"true":
"false");
335 virtual void app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
337 virtual void app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
339 virtual void app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
341 virtual void audio_data_callback(
const uint8_t *data, uint32_t len);
343 virtual void av_hdl_stack_evt(uint16_t event,
void *p_param);
345 virtual void av_hdl_a2d_evt(uint16_t event,
void *p_param);
347 virtual void av_hdl_avrc_evt(uint16_t event,
void *p_param);
350 virtual void handle_connection_state(uint16_t event,
void *p_param);
351 virtual void handle_audio_state(uint16_t event,
void *p_param);
352 virtual void handle_audio_cfg(uint16_t event,
void *p_param);
356 virtual void volume_set_by_local_host(uint8_t volume);
357 virtual void volume_set_by_controller(uint8_t volume);
358 virtual void av_notify_evt_handler(uint8_t& event_id, esp_avrc_rn_param_t& event_parameter);
359 virtual void app_rc_tg_callback(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
360 virtual void av_hdl_avrc_tg_evt(uint16_t event,
void *p_param);
362 virtual void av_notify_evt_handler(uint8_t event_id, uint32_t event_parameter);
372 virtual void bt_i2s_task_start_up(
void) {}
373 virtual void bt_i2s_task_shut_down(
void) {}
378 virtual esp_err_t esp_a2d_connect(esp_bd_addr_t peer) {
379 return esp_a2d_sink_connect(peer);
void(* app_callback_t)(uint16_t event, void *param)
handler for the dispatched work
Definition: BluetoothA2DPCommon.h:80
Common Bluetooth A2DP functions.
Definition: BluetoothA2DPCommon.h:104
virtual A2DPVolumeControl * volume_control()
provides access to the VolumeControl object
Definition: BluetoothA2DPCommon.h:254
virtual bool reconnect()
Reconnects to the last device.
Definition: BluetoothA2DPCommon.cpp:33
A2DP Bluethooth Sink - We initialize and start the Bluetooth A2DP Sink. The example https://github....
Definition: BluetoothA2DPSink.h:63
virtual void set_raw_stream_reader(void(*callBack)(const uint8_t *, uint32_t))
Define a callback that is called before the volume changes: this callback provides access to the data...
Definition: BluetoothA2DPSink.cpp:113
virtual uint16_t sample_rate()
Provides the actually set data rate (in samples per second)
Definition: BluetoothA2DPSink.cpp:830
virtual ~BluetoothA2DPSink()
Destructor - stops the playback and releases all resources.
Definition: BluetoothA2DPSink.cpp:68
virtual void set_swap_lr_channels(bool swap)
swaps the left and right channel
Definition: BluetoothA2DPSink.h:207
virtual bool is_connected()
Returns true if the state is connected.
Definition: BluetoothA2DPSink.cpp:266
virtual void activate_pin_code(bool active)
We need to confirm a new seesion by calling confirm_pin_code()
Definition: BluetoothA2DPSink.cpp:1153
virtual void next()
AVRC next.
Definition: BluetoothA2DPSink.cpp:1118
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:1234
virtual void previous()
AVRC previous.
Definition: BluetoothA2DPSink.cpp:1121
virtual void app_task_handler(void *arg)
Definition: BluetoothA2DPSink.cpp:378
virtual int pin_code()
provides the requested pin code (0 = undefined)
Definition: BluetoothA2DPSink.h:197
virtual void stop()
AVRC stop.
Definition: BluetoothA2DPSink.cpp:1114
friend void ccall_av_hdl_stack_evt(uint16_t event, void *p_param)
av event handler
Definition: BluetoothA2DPSink.cpp:1227
virtual void set_i2s_config(i2s_config_t i2s_config)
Define the i2s configuration.
Definition: BluetoothA2DPSink.cpp:104
virtual void fast_forward()
AVRC fast_forward.
Definition: BluetoothA2DPSink.cpp:1124
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:1209
void set_rssi_active(bool active)
Activates the rssi reporting.
Definition: BluetoothA2DPSink.h:224
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:108
virtual void set_volume(uint8_t volume)
Changes the volume.
Definition: BluetoothA2DPSink.cpp:1131
esp_bt_gap_cb_param_t::read_rssi_delta_param get_last_rssi()
provides the last rssi parameters
Definition: BluetoothA2DPSink.h:229
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:76
virtual void confirm_pin_code()
confirms the connection request by returning the receivedn pin code
Definition: BluetoothA2DPSink.cpp:1157
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:270
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:202
virtual esp_bd_addr_t * get_current_peer_address()
Provides the address of the connected device.
Definition: BluetoothA2DPSink.h:218
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:1215
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:128
virtual esp_err_t i2s_mclk_pin_select(const uint8_t pin)
Defines the pin for the master clock.
Definition: BluetoothA2DPSink.cpp:241
void set_reconnect_delay(int delay)
Defines the delay that is added to delay the startup when we automatically reconnect.
Definition: BluetoothA2DPSink.h:239
void set_rssi_callback(void(*callback)(esp_bt_gap_cb_param_t::read_rssi_delta_param &rssi))
Defines the callback that is called when we get an new rssi value.
Definition: BluetoothA2DPSink.h:234
friend void ccall_av_hdl_a2d_evt(uint16_t event, void *p_param)
a2dp event handler
Definition: BluetoothA2DPSink.cpp:1241
virtual void set_bits_per_sample(int bps)
Defines the bits per sample for output (if > 16 output will be expanded)
Definition: BluetoothA2DPSink.h:179
virtual void rewind()
AVRC rewind.
Definition: BluetoothA2DPSink.cpp:1127
virtual size_t write_audio(const uint8_t *data, size_t size)
output audio data e.g. to i2s or to queue
Definition: BluetoothA2DPSink.h:366
virtual void pause()
AVRC pause.
Definition: BluetoothA2DPSink.cpp:1110
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:123
virtual void play()
Starts to play music using AVRC.
Definition: BluetoothA2DPSink.cpp:1106
virtual void set_i2s_port(i2s_port_t i2s_num)
Define an alternative i2s port other then 0.
Definition: BluetoothA2DPSink.cpp:100
size_t i2s_write_data(const uint8_t *data, size_t item_size)
writes the data to i2s
Definition: BluetoothA2DPSink.cpp:1249
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:1203
friend void ccall_i2s_task_handler(void *arg)
task hander for i2s
Definition: BluetoothA2DPSink.cpp:1191
virtual void set_on_volumechange(void(*callBack)(int))
Set the callback that is called when they change the volume.
Definition: BluetoothA2DPSink.cpp:129
virtual int get_volume()
Determines the volume.
Definition: BluetoothA2DPSink.cpp:1147
friend void ccall_app_task_handler(void *arg)
task handler
Definition: BluetoothA2DPSink.cpp:1197
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:1221
virtual void set_address_validator(bool(*callBack)(esp_bd_addr_t remote_bda))
Allows you to reject unauthorized addresses.
Definition: BluetoothA2DPSink.h:142
virtual void set_on_data_received(void(*callBack)())
Define callback which is called when we receive data.
Definition: BluetoothA2DPSink.cpp:117
virtual void set_pin_config(i2s_pin_config_t pin_config)
Define the pins.
Definition: BluetoothA2DPSink.cpp:96
virtual void set_auto_reconnect(bool reconnect, int count=AUTOCONNECT_TRY_NUM)
Defines the number of times that the system tries to automatically reconnect to the last system.
Definition: BluetoothA2DPSink.h:212
virtual void start(const char *name, bool auto_reconect)
starts the I2S bluetooth sink with the inidicated name
Definition: BluetoothA2DPSink.cpp:133
virtual void i2s_task_handler(void *arg)
dummy functions needed for BluetoothA2DPSinkQueued
Definition: BluetoothA2DPSink.h:371
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition: BluetoothA2DPCommon.h:83