Skip to content

Commit 8a86331

Browse files
committed
Sink: added set_max_write_size() and set_max_write_delay_ms()
1 parent f19abd2 commit 8a86331

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/BluetoothA2DPSink.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void BluetoothA2DPSink::start(const char *name) {
116116
ESP_LOGD(BT_AV_TAG, "%s", __func__);
117117
log_free_heap();
118118

119-
is_autoreconnect_allowed = (reconnect_status == AutoReconnect);
119+
is_autoreconnect_allowed = (reconnect_status == AutoReconnect);
120120

121121
if (is_start_disabled) {
122122
ESP_LOGE(BT_AV_TAG, "re-start not supported after end(true)");
@@ -129,7 +129,6 @@ void BluetoothA2DPSink::start(const char *name) {
129129
}
130130
ESP_LOGI(BT_AV_TAG, "Device name will be set to '%s'", this->bt_name);
131131

132-
133132
if (is_autoreconnect_allowed) {
134133
// Initialize NVS
135134
init_nvs();
@@ -274,7 +273,6 @@ bool BluetoothA2DPSink::app_work_dispatch(app_callback_t p_cback,
274273
return false;
275274
}
276275

277-
278276
void BluetoothA2DPSink::app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param) {
279277
ESP_LOGD(BT_AV_TAG, "%s", __func__);
280278
esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(param);
@@ -992,7 +990,6 @@ void BluetoothA2DPSink::audio_data_callback(const uint8_t *data, uint32_t len) {
992990
}
993991
}
994992

995-
996993
bool BluetoothA2DPSink::is_avrc_connected() { return avrc_connection_state; }
997994

998995
void BluetoothA2DPSink::execute_avrc_command(int cmd) {
@@ -1094,7 +1091,6 @@ void BluetoothA2DPSink::confirm_pin_code(int code) {
10941091
}
10951092
}
10961093

1097-
10981094
size_t BluetoothA2DPSink::i2s_write_data(const uint8_t *data,
10991095
size_t item_size) {
11001096
if (!is_output) return item_size;
@@ -1109,9 +1105,11 @@ size_t BluetoothA2DPSink::i2s_write_data(const uint8_t *data,
11091105
int processed = 0;
11101106
while (open > 0) {
11111107
int written =
1112-
out->write(data + processed, std::min(open, A2DP_I2S_MAX_WRITE_SIZE));
1108+
out->write(data + processed, std::min(open, max_write_size));
11131109
open -= written;
11141110
processed += written;
1111+
// add some delay between the writes
1112+
delay_ms(max_write_delay_ms);
11151113
}
11161114
return processed;
11171115
}

src/BluetoothA2DPSink.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ class BluetoothA2DPSink : public BluetoothA2DPCommon {
335335
/// Checks if output is active
336336
bool is_output_active() { return is_i2s_active; }
337337

338+
/// defines the max write size: default is A2DP_I2S_MAX_WRITE_SIZE
339+
void set_max_write_size(int size) { max_write_size = size; }
340+
341+
/// defines a small delay after each write: default is 0 ms
342+
void set_max_write_delay_ms(int delay) { max_write_delay_ms = delay; }
343+
338344
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
339345
/// Provides the result of the last result for the
340346
/// esp_avrc_tg_get_rn_evt_cap() callback (Available from ESP_IDF_4)
@@ -402,6 +408,8 @@ class BluetoothA2DPSink : public BluetoothA2DPCommon {
402408
void (*rssi_callbak)(esp_bt_gap_cb_param_t::read_rssi_delta_param &rssi) =
403409
nullptr;
404410
int reconnect_delay = 1000;
411+
int max_write_size = A2DP_I2S_MAX_WRITE_SIZE;
412+
int max_write_delay_ms = A2DP_I2S_MAX_WRITE_DELAY_MS;
405413

406414
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
407415
esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap = {0};

src/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
# define A2DP_I2S_MAX_WRITE_SIZE 1024 * 5
2626
#endif
2727

28+
#ifndef A2DP_I2S_MAX_WRITE_DELAY_MS
29+
# define A2DP_I2S_MAX_WRITE_DELAY_MS 0
30+
#endif
31+
2832
// Maximum wait time for status change in 100 ms when calling end()
2933
#ifndef A2DP_DISCONNECT_LIMIT
3034
# define A2DP_DISCONNECT_LIMIT 20

0 commit comments

Comments
 (0)