Arduino A2DP
BluetoothA2DPSink.h
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 //
13 // Copyright 2020 Phil Schatzmann
14 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
15 
16 #pragma once
17 #include "BluetoothA2DPCommon.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #define APP_SIG_WORK_DISPATCH (0x01)
24 
25 #ifndef AUTOCONNECT_TRY_NUM
26 #define AUTOCONNECT_TRY_NUM 1
27 #endif
28 
29 
30 #ifndef BT_AV_TAG
31 #define BT_AV_TAG "BT_AV"
32 #endif
33 
34 /* @brief event for handler "bt_av_hdl_stack_up */
35 enum {
36  BT_APP_EVT_STACK_UP = 0,
37 };
38 
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);
47 
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);
51 #endif
52 
53 // defines the mechanism to confirm a pin request
54 enum PinCodeRequest {Undefined, Confirm, Reply};
55 
65 
67  friend void ccall_app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
69  friend void ccall_app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
71  friend void ccall_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
73  friend void ccall_app_task_handler(void *arg);
75  friend void ccall_audio_data_callback(const uint8_t *data, uint32_t len);
77  friend void ccall_av_hdl_stack_evt(uint16_t event, void *p_param);
79  friend void ccall_av_hdl_a2d_evt(uint16_t event, void *p_param);
81  friend void ccall_av_hdl_avrc_evt(uint16_t event, void *p_param);
82 
83 #ifdef CURRENT_ESP_IDF
84 
86  friend void ccall_app_rc_tg_callback(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
87  /* avrc TG event handler */
88  friend void ccall_av_hdl_avrc_tg_evt(uint16_t event, void *p_param);
89 
90 #endif
91 
92  public:
96  virtual ~BluetoothA2DPSink();
98  virtual void set_pin_config(i2s_pin_config_t pin_config);
99 
101  virtual void set_i2s_port(i2s_port_t i2s_num);
102 
104  virtual void set_i2s_config(i2s_config_t i2s_config);
105 
107  virtual void start(const char* name, bool auto_reconect);
108 
110  virtual void start(const char* name);
111 
113  virtual void end(bool release_memory=false);
114 
116  virtual bool is_connected();
117 
119  virtual esp_a2d_mct_t get_audio_type();
120 
122  virtual void set_avrc_metadata_callback(void (*callback)(uint8_t, const uint8_t*)) {
123  this->avrc_metadata_callback = callback;
124  }
125 
127  virtual void set_sample_rate_callback(void (*callback)(uint16_t rate)) {
128  this->sample_rate_callback = callback;
129  }
130 
132  virtual void set_stream_reader(void (*callBack)(const uint8_t*, uint32_t), bool i2s_output=true);
133 
135  virtual void set_on_data_received(void (*callBack)());
136 
137  // /// Obsolete: please use set_on_connection_state_changed - Set the callback that is called when the BT device is connected
138  // DEPRECATED
139  // virtual void set_on_connected2BT(void (*callBack)());
140 
141  // /// Obsolete: please use set_on_connection_state_changed - Set the callback that is called when the BT device is dis_connected
142  // DEPRECATED
143  // virtual void set_on_dis_connected2BT(void (*callBack)());
144 
146  virtual void set_address_validator(bool (*callBack)(esp_bd_addr_t remote_bda)){
147  address_validator = callBack;
148  }
149 
151  virtual void set_volume(uint8_t volume);
152 
154  virtual int get_volume();
155 
157  virtual void set_on_volumechange(void (*callBack)(int));
158 
160  virtual void play();
162  virtual void pause();
164  virtual void stop();
166  virtual void next();
168  virtual void previous();
169 
171  virtual void set_channels(i2s_channel_t channels) {
172  set_mono_downmix(channels==I2S_CHANNEL_MONO);
173  }
175  virtual void set_mono_downmix(bool enabled) { mono_downmix = enabled; }
177  virtual void set_bits_per_sample(int bps) { i2s_config.bits_per_sample = (i2s_bits_per_sample_t) bps; }
178 
180  virtual uint16_t sample_rate();
181 
183  virtual esp_err_t i2s_mclk_pin_select(const uint8_t pin);
184 
186  virtual void activate_pin_code(bool active);
187 
189  virtual void confirm_pin_code();
190 
192  virtual void confirm_pin_code(int code);
193 
195  virtual int pin_code() {
196  return pin_code_int;
197  }
198 
200  virtual void set_avrc_metadata_attribute_mask(int flags){
201  avrc_metadata_flags = flags;
202  }
203 
205  virtual void set_swap_lr_channels(bool swap){
206  swap_left_right = swap;
207  }
208 
209  #ifdef CURRENT_ESP_IDF
211  virtual const char* get_connected_source_name();
212  #endif
213 
214  protected:
215  // protected data
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;
222  //esp_a2d_audio_state_t m_audio_state = ESP_A2D_AUDIO_STATE_STOPPED;
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;
229  i2s_port_t i2s_port;
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;
250 
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];
254 #endif
255 
256  // protected methods
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();
267  // execute AVRC command
268  virtual void execute_avrc_command(int cmd);
269 
270  virtual const char* last_bda_nvs_name() {
271  return "last_bda";
272  }
276  // task handler
277  virtual void app_task_handler(void *arg);
278  // a2d callback
279  virtual void app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
280  // GAP callback
281  virtual void app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
282  // avrc callback
283  virtual void app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
284  // Callback for music stream
285  virtual void audio_data_callback(const uint8_t *data, uint32_t len);
286  // av event handler
287  virtual void av_hdl_stack_evt(uint16_t event, void *p_param);
288  // a2dp event handler
289  virtual void av_hdl_a2d_evt(uint16_t event, void *p_param);
290  // avrc event handler
291  virtual void av_hdl_avrc_evt(uint16_t event, void *p_param);
292 
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);
299 #else
300  virtual void av_notify_evt_handler(uint8_t event_id, uint32_t event_parameter);
301 #endif
302 
303 };
304 
305 
306 #ifdef __cplusplus
307 }
308 #endif
309 
310 
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