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};
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_connection_state_callback = callback;
129 this->avrc_metadata_callback = callback;
134 this->avrc_rn_playstatus_callback = callback;
139 this->sample_rate_callback = callback;
143 virtual void set_stream_reader(
void (*callBack)(
const uint8_t*, uint32_t),
bool i2s_output=
true);
153 address_validator = callBack;
177 virtual void pause();
222 avrc_metadata_flags = flags;
227 swap_left_right = swap;
232 reconnect_status =
reconnect ? AutoReconnect : NoReconnect;
233 try_reconnect_max_count = count;
238 return &peer_bd_addr;
244 rssi_active = active;
249 return last_rssi_delta;
254 rssi_callbak = callback;
259 reconnect_delay = delay;
272 return esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap, cmd);
276 return s_avrc_peer_rn_cap.bits != 0;
288 xQueueHandle app_task_queue =
nullptr;
289 xTaskHandle app_task_handle =
nullptr;
291 i2s_config_t i2s_config;
292 i2s_pin_config_t pin_config;
293 uint32_t m_pkt_cnt = 0;
295 esp_a2d_mct_t audio_type;
296 char pin_code_str[20] = {0};
297 bool is_i2s_output =
true;
298 bool player_init =
false;
299 i2s_channel_t i2s_channels = I2S_CHANNEL_STEREO;
300 i2s_port_t i2s_port = I2S_NUM_0;
301 int connection_rety_count = 0;
302 bool spp_active =
false;
303 esp_spp_mode_t esp_spp_mode = ESP_SPP_MODE_CB;
304 _lock_t s_volume_lock;
305 uint8_t s_volume = 0;
306 bool s_volume_notify;
307 int pin_code_int = 0;
308 PinCodeRequest pin_code_request = Undefined;
309 bool is_pin_code_active =
false;
310 bool avrc_connection_state =
false;
311 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;
312 void (*bt_volumechange)(int) =
nullptr;
313 void (*bt_dis_connected)() =
nullptr;
314 void (*bt_connected)() =
nullptr;
315 void (*data_received)() =
nullptr;
316 void (*stream_reader)(
const uint8_t*, uint32_t) =
nullptr;
317 void (*raw_stream_reader)(
const uint8_t*, uint32_t) =
nullptr;
318 void (*avrc_connection_state_callback)(
bool connected) =
nullptr;
319 void (*avrc_metadata_callback)(uint8_t,
const uint8_t*) =
nullptr;
320 void (*avrc_rn_playstatus_callback)(esp_avrc_playback_stat_t) =
nullptr;
321 void (*avrc_rn_volchg_complete_callback)(int) =
nullptr;
322 bool (*address_validator)(esp_bd_addr_t remote_bda) =
nullptr;
323 void (*sample_rate_callback)(uint16_t rate)=
nullptr;
324 bool swap_left_right =
false;
325 bool is_i2s_active =
false;
326 int try_reconnect_max_count = AUTOCONNECT_TRY_NUM;
329 esp_bt_gap_cb_param_t::read_rssi_delta_param last_rssi_delta;
330 bool rssi_active =
false;
331 void (*rssi_callbak)(esp_bt_gap_cb_param_t::read_rssi_delta_param &rssi) =
nullptr;
332 int reconnect_delay = 1000;
335 esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
336 char remote_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
340 virtual int init_bluetooth();
341 virtual void init_i2s();
342 virtual void app_task_start_up(
void);
343 virtual void app_task_shut_down(
void);
344 virtual bool app_send_msg(
app_msg_t *msg);
345 virtual bool app_work_dispatch(
app_callback_t p_cback, uint16_t event,
void *p_params,
int param_len);
346 virtual void app_work_dispatched(
app_msg_t *msg);
347 virtual void app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param);
348 virtual void av_new_track();
349 virtual void av_playback_changed();
350 virtual void init_nvs();
352 virtual void execute_avrc_command(
int cmd);
354 virtual const char* last_bda_nvs_name() {
358 virtual bool is_reconnect(esp_a2d_disc_rsn_t type) {
359 bool result = is_autoreconnect_allowed && (reconnect_status==AutoReconnect || reconnect_status==IsReconnecting) && has_last_connection();
360 ESP_LOGI(BT_AV_TAG,
"is_reconnect: %s", result ?
"true":
"false");
370 virtual void app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
372 virtual void app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
374 virtual void app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
376 virtual void audio_data_callback(
const uint8_t *data, uint32_t len);
378 virtual void av_hdl_stack_evt(uint16_t event,
void *p_param);
380 virtual void av_hdl_a2d_evt(uint16_t event,
void *p_param);
382 virtual void av_hdl_avrc_evt(uint16_t event,
void *p_param);
385 virtual void handle_connection_state(uint16_t event,
void *p_param);
386 virtual void handle_audio_state(uint16_t event,
void *p_param);
387 virtual void handle_audio_cfg(uint16_t event,
void *p_param);
388 virtual void handle_avrc_connection_state(
bool connected);
392 virtual void volume_set_by_local_host(uint8_t volume);
393 virtual void volume_set_by_controller(uint8_t volume);
394 virtual void av_notify_evt_handler(uint8_t event_id, esp_avrc_rn_param_t* event_parameter);
395 virtual void app_rc_tg_callback(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
396 virtual void av_hdl_avrc_tg_evt(uint16_t event,
void *p_param);
398 virtual void av_notify_evt_handler(uint8_t event_id, uint32_t event_parameter);
408 virtual void bt_i2s_task_start_up(
void) {}
409 virtual void bt_i2s_task_shut_down(
void) {}
414 virtual esp_err_t esp_a2d_connect(esp_bd_addr_t peer) {
415 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:111
virtual A2DPVolumeControl * volume_control()
provides access to the VolumeControl object
Definition: BluetoothA2DPCommon.h:281
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:851
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:226
virtual bool is_connected()
Returns true if the state is connected.
Definition: BluetoothA2DPSink.cpp:277
virtual void activate_pin_code(bool active)
We need to confirm a new seesion by calling confirm_pin_code()
Definition: BluetoothA2DPSink.cpp:1202
virtual void next()
AVRC next.
Definition: BluetoothA2DPSink.cpp:1167
BluetoothA2DPSink()
Constructor.
Definition: BluetoothA2DPSink.cpp:24
virtual const char * get_connected_source_name()
Get the name of the connected source device (obsolete)
Definition: BluetoothA2DPSink.cpp:286
friend void ccall_av_hdl_avrc_evt(uint16_t event, void *p_param)
avrc event handler
Definition: BluetoothA2DPSink.cpp:1283
virtual void previous()
AVRC previous.
Definition: BluetoothA2DPSink.cpp:1170
virtual void app_task_handler(void *arg)
Definition: BluetoothA2DPSink.cpp:391
virtual int pin_code()
provides the requested pin code (0 = undefined)
Definition: BluetoothA2DPSink.h:216
virtual void stop()
AVRC stop.
Definition: BluetoothA2DPSink.cpp:1163
friend void ccall_av_hdl_stack_evt(uint16_t event, void *p_param)
av event handler
Definition: BluetoothA2DPSink.cpp:1276
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:1173
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:1258
void set_rssi_active(bool active)
Activates the rssi reporting.
Definition: BluetoothA2DPSink.h:243
virtual bool is_avrc_connected()
returns true if the avrc service is connected
Definition: BluetoothA2DPSink.cpp:1135
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:1180
esp_bt_gap_cb_param_t::read_rssi_delta_param get_last_rssi()
provides the last rssi parameters
Definition: BluetoothA2DPSink.h:248
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:1206
virtual void set_avrc_rn_playstatus_callback(void(*callback)(esp_avrc_playback_stat_t playback))
Define a callback method which provides the avrc notifications.
Definition: BluetoothA2DPSink.h:133
virtual void set_mono_downmix(bool enabled)
mix stereo into single mono signal
Definition: BluetoothA2DPSink.h:194
virtual const char * get_peer_name()
Get the name of the connected source device.
Definition: BluetoothA2DPSink.h:280
virtual esp_a2d_mct_t get_audio_type()
Determine the actual audio type.
Definition: BluetoothA2DPSink.cpp:281
bool is_avrc_peer_rn_cap_available()
Returns true if the is_avrc_peer_rn_cap() method can be called.
Definition: BluetoothA2DPSink.h:275
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:221
virtual esp_bd_addr_t * get_current_peer_address()
Provides the address of the connected device.
Definition: BluetoothA2DPSink.h:237
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:1264
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:138
virtual esp_err_t i2s_mclk_pin_select(const uint8_t pin)
Defines the pin for the master clock.
Definition: BluetoothA2DPSink.cpp:252
void set_reconnect_delay(int delay)
Defines the delay that is added to delay the startup when we automatically reconnect.
Definition: BluetoothA2DPSink.h:258
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:253
friend void ccall_av_hdl_a2d_evt(uint16_t event, void *p_param)
a2dp event handler
Definition: BluetoothA2DPSink.cpp:1290
virtual void set_avrc_rn_volumechange_completed(void(*callBack)(int))
set the callback that the local volume change is notification is received and complete
Definition: BluetoothA2DPSink.cpp:138
virtual void set_bits_per_sample(int bps)
Defines the bits per sample for output (if > 16 output will be expanded)
Definition: BluetoothA2DPSink.h:198
virtual void rewind()
AVRC rewind.
Definition: BluetoothA2DPSink.cpp:1176
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:402
virtual void pause()
AVRC pause.
Definition: BluetoothA2DPSink.cpp:1159
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:128
virtual void play()
Starts to play music using AVRC.
Definition: BluetoothA2DPSink.cpp:1155
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:1298
virtual void set_channels(i2s_channel_t channels)
set output to I2S_CHANNEL_STEREO (default) or I2S_CHANNEL_MONO
Definition: BluetoothA2DPSink.h:190
void set_spp_active(bool flag)
Activates SSP (Serial protocol)
Definition: BluetoothA2DPSink.h:263
friend void ccall_audio_data_callback(const uint8_t *data, uint32_t len)
Callback for music stream.
Definition: BluetoothA2DPSink.cpp:1252
friend void ccall_i2s_task_handler(void *arg)
task hander for i2s
Definition: BluetoothA2DPSink.cpp:1240
virtual void set_on_volumechange(void(*callBack)(int))
Set the callback that is called when they change the volume (kept for compatibility)
Definition: BluetoothA2DPSink.cpp:130
friend void ccall_app_rc_tg_callback(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param)
handle esp_avrc_tg_cb_event_t
Definition: BluetoothA2DPSink.cpp:1395
virtual int get_volume()
Determines the volume.
Definition: BluetoothA2DPSink.cpp:1196
virtual void set_avrc_rn_volumechange(void(*callBack)(int))
Set the callback that is called when remote changes the volume.
Definition: BluetoothA2DPSink.cpp:134
friend void ccall_app_task_handler(void *arg)
task handler
Definition: BluetoothA2DPSink.cpp:1246
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:1270
virtual void set_address_validator(bool(*callBack)(esp_bd_addr_t remote_bda))
Allows you to reject unauthorized addresses.
Definition: BluetoothA2DPSink.h:152
virtual void set_avrc_connection_state_callback(void(*callback)(bool))
Define a callback method which provides connection state of AVRC service.
Definition: BluetoothA2DPSink.h:123
bool is_avrc_peer_rn_cap(esp_avrc_rn_event_ids_t cmd)
Provides the result of the last result for the esp_avrc_tg_get_rn_evt_cap() callback (Available from ...
Definition: BluetoothA2DPSink.h:271
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:231
virtual void start(const char *name, bool auto_reconect)
starts the I2S bluetooth sink with the inidicated name
Definition: BluetoothA2DPSink.cpp:142
virtual void i2s_task_handler(void *arg)
dummy functions needed for BluetoothA2DPSinkQueued
Definition: BluetoothA2DPSink.h:407
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition: BluetoothA2DPCommon.h:83