Skip to content

Commit 81f3a42

Browse files
unpapardoMiguel Pardo
andauthored
Non blocking A2DPStream begin() (#1909)
* 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]>
1 parent f86334f commit 81f3a42

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/AudioTools/AudioLibs/A2DPStream.h

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ class A2DPStream : public AudioStream, public VolumeSupport {
123123
}
124124

125125
/// Starts the processing
126-
bool begin(RxTxMode mode, const char* name){
126+
bool begin(RxTxMode mode, const char* name, bool wait_for_connection=true){
127127
A2DPConfig cfg;
128128
cfg.mode = mode;
129129
cfg.name = name;
130-
return begin(cfg);
130+
return begin(cfg, wait_for_connection);
131131
}
132132

133133
/// Starts the processing
134-
bool begin(A2DPConfig cfg){
134+
bool begin(A2DPConfig cfg, bool wait_for_connection=true){
135135
this->config = cfg;
136136
bool result = false;
137137
LOGI("Connecting to %s",cfg.name);
@@ -158,14 +158,19 @@ class A2DPStream : public AudioStream, public VolumeSupport {
158158
a2dp_source->set_ssid_callback(detected_device);
159159
}
160160
a2dp_source->set_on_connection_state_changed(a2dp_state_callback, this);
161-
a2dp_source->start_raw((char*)cfg.name, a2dp_stream_source_sound_data);
162-
while(!a2dp_source->is_connected()){
163-
LOGD("waiting for connection");
164-
delay(1000);
161+
a2dp_source->start_raw((char*)cfg.name, a2dp_stream_source_sound_data);
162+
if (wait_for_connection){
163+
while(!a2dp_source->is_connected()){
164+
LOGD("waiting for connection");
165+
delay(1000);
166+
}
167+
LOGI("a2dp_source is connected...");
168+
notify_base_Info(44100);
169+
//is_a2dp_active = true;
170+
}
171+
else{
172+
LOGI("a2dp_source started without connecting");
165173
}
166-
LOGI("a2dp_source is connected...");
167-
notify_base_Info(44100);
168-
//is_a2dp_active = true;
169174
result = true;
170175
break;
171176

@@ -178,11 +183,16 @@ class A2DPStream : public AudioStream, public VolumeSupport {
178183
a2dp_sink->set_on_connection_state_changed(a2dp_state_callback, this);
179184
a2dp_sink->set_sample_rate_callback(sample_rate_callback);
180185
a2dp_sink->start((char*)cfg.name);
181-
while(!a2dp_sink->is_connected()){
182-
LOGD("waiting for connection");
183-
delay(1000);
186+
if (wait_for_connection){
187+
while(!a2dp_sink->is_connected()){
188+
LOGD("waiting for connection");
189+
delay(1000);
190+
}
191+
LOGI("a2dp_sink is connected...");
192+
}
193+
else{
194+
LOGI("a2dp_sink started without connection");
184195
}
185-
LOGI("a2dp_sink is connected...");
186196
is_a2dp_active = true;
187197
result = true;
188198
break;

0 commit comments

Comments
 (0)