Skip to content

Commit

Permalink
Non blocking A2DPStream begin() (#1909)
Browse files Browse the repository at this point in the history
* Added non-blocking option to A2DPStream

* Added non-blocking option to A2DPStream

* Added non-blocking option to A2DPStream

* Added non-blocking option to A2DPStream

* Added non-blocking option to A2DPStream

---------

Co-authored-by: Miguel Pardo <[email protected]>
  • Loading branch information
unpapardo and Miguel Pardo authored Feb 10, 2025
1 parent f86334f commit 81f3a42
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/AudioTools/AudioLibs/A2DPStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ class A2DPStream : public AudioStream, public VolumeSupport {
}

/// Starts the processing
bool begin(RxTxMode mode, const char* name){
bool begin(RxTxMode mode, const char* name, bool wait_for_connection=true){
A2DPConfig cfg;
cfg.mode = mode;
cfg.name = name;
return begin(cfg);
return begin(cfg, wait_for_connection);
}

/// Starts the processing
bool begin(A2DPConfig cfg){
bool begin(A2DPConfig cfg, bool wait_for_connection=true){
this->config = cfg;
bool result = false;
LOGI("Connecting to %s",cfg.name);
Expand All @@ -158,14 +158,19 @@ class A2DPStream : public AudioStream, public VolumeSupport {
a2dp_source->set_ssid_callback(detected_device);
}
a2dp_source->set_on_connection_state_changed(a2dp_state_callback, this);
a2dp_source->start_raw((char*)cfg.name, a2dp_stream_source_sound_data);
while(!a2dp_source->is_connected()){
LOGD("waiting for connection");
delay(1000);
a2dp_source->start_raw((char*)cfg.name, a2dp_stream_source_sound_data);
if (wait_for_connection){
while(!a2dp_source->is_connected()){
LOGD("waiting for connection");
delay(1000);
}
LOGI("a2dp_source is connected...");
notify_base_Info(44100);
//is_a2dp_active = true;
}
else{
LOGI("a2dp_source started without connecting");
}
LOGI("a2dp_source is connected...");
notify_base_Info(44100);
//is_a2dp_active = true;
result = true;
break;

Expand All @@ -178,11 +183,16 @@ class A2DPStream : public AudioStream, public VolumeSupport {
a2dp_sink->set_on_connection_state_changed(a2dp_state_callback, this);
a2dp_sink->set_sample_rate_callback(sample_rate_callback);
a2dp_sink->start((char*)cfg.name);
while(!a2dp_sink->is_connected()){
LOGD("waiting for connection");
delay(1000);
if (wait_for_connection){
while(!a2dp_sink->is_connected()){
LOGD("waiting for connection");
delay(1000);
}
LOGI("a2dp_sink is connected...");
}
else{
LOGI("a2dp_sink started without connection");
}
LOGI("a2dp_sink is connected...");
is_a2dp_active = true;
result = true;
break;
Expand Down

0 comments on commit 81f3a42

Please sign in to comment.