Skip to content

Commit 54ef34c

Browse files
committed
SnapClient: WiFiClient - using delegating constructors
1 parent 7d6fd0b commit 54ef34c

File tree

1 file changed

+23
-50
lines changed

1 file changed

+23
-50
lines changed

src/SnapClient.h

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
*/
66

77
#include "AudioTools.h"
8+
#include "Client.h"
89
#include "SnapConfig.h"
910
#include "api/SnapCommon.h"
1011
#include "api/SnapLogger.h"
11-
#include "Client.h"
12-
1312

1413
#if CONFIG_NVS_FLASH
1514
#include "nvs_flash.h"
@@ -35,8 +34,7 @@ namespace snap_arduino {
3534
* @copyright Copyright (c) 2023
3635
*/
3736
class SnapClient {
38-
39-
public:
37+
public:
4038
SnapClient(Client &client, AudioStream &stream, AudioDecoder &decoder) {
4139
static AdapterAudioStreamToAudioOutput output_adapter;
4240
output_adapter.setStream(stream);
@@ -62,7 +60,7 @@ class SnapClient {
6260
p_output = &output;
6361
p_client = &client;
6462
server_ip.fromString(CONFIG_SNAPCAST_SERVER_HOST);
65-
}
63+
}
6664

6765
SnapClient(Client &client, AudioOutput &output, StreamingDecoder &decoder,
6866
int bufferSize = CONFIG_STREAMIN_DECODER_BUFFER) {
@@ -71,62 +69,41 @@ class SnapClient {
7169
p_client = &client;
7270
}
7371

74-
//
75-
SnapClient(WiFiClient &client, AudioStream &stream, AudioDecoder &decoder) {
72+
//
73+
SnapClient(WiFiClient &client, AudioStream &stream, AudioDecoder &decoder)
74+
: SnapClient((Client &)client, stream, decoder) {
7675
is_wifi = true;
77-
static AdapterAudioStreamToAudioOutput output_adapter;
78-
output_adapter.setStream(stream);
79-
p_decoder = &decoder;
80-
p_output = &output_adapter;
81-
p_client = &client;
82-
server_ip.fromString(CONFIG_SNAPCAST_SERVER_HOST);
8376
}
8477

8578
SnapClient(WiFiClient &client, AudioStream &stream, StreamingDecoder &decoder,
86-
int bufferSize = CONFIG_STREAMIN_DECODER_BUFFER) {
79+
int bufferSize = CONFIG_STREAMIN_DECODER_BUFFER)
80+
: SnapClient((Client &)client, stream, decoder, bufferSize) {
8781
is_wifi = true;
88-
static DecoderFromStreaming decoder_adapter(decoder, bufferSize);
89-
static AdapterAudioStreamToAudioOutput output_adapter;
90-
p_decoder = &decoder_adapter;
91-
output_adapter.setStream(stream);
92-
p_output = &output_adapter;
93-
p_client = &client;
94-
server_ip.fromString(CONFIG_SNAPCAST_SERVER_HOST);
9582
}
9683

97-
SnapClient(WiFiClient &client, AudioOutput &output, AudioDecoder &decoder) {
84+
SnapClient(WiFiClient &client, AudioOutput &output, AudioDecoder &decoder)
85+
: SnapClient((Client &)client, output, decoder) {
9886
is_wifi = true;
99-
p_decoder = &decoder;
100-
p_output = &output;
101-
p_client = &client;
102-
server_ip.fromString(CONFIG_SNAPCAST_SERVER_HOST);
103-
}
87+
}
10488

10589
SnapClient(WiFiClient &client, AudioOutput &output, StreamingDecoder &decoder,
106-
int bufferSize = CONFIG_STREAMIN_DECODER_BUFFER) {
90+
int bufferSize = CONFIG_STREAMIN_DECODER_BUFFER)
91+
: SnapClient((Client &)client, output, decoder, bufferSize) {
10792
is_wifi = true;
108-
p_decoder = new DecoderFromStreaming(decoder, bufferSize);
109-
p_output = &output;
110-
p_client = &client;
11193
}
11294

113-
11495
/// Destructor
115-
~SnapClient() {
116-
end();
117-
}
96+
~SnapClient() { end(); }
11897

11998
/// Defines an alternative commnuication client (default is WiFiClient)
12099
void setClient(Client &client) { p_client = &client; }
121100

122101
/// @brief Defines the Snapcast Server IP address
123-
/// @param address
102+
/// @param address
124103
void setServerIP(IPAddress ipAddress) { this->server_ip = ipAddress; }
125104

126105
/// Defines the time synchronization logic
127-
void setSnapTimeSync(SnapTimeSync &timeSync){
128-
p_time_sync = &timeSync;
129-
}
106+
void setSnapTimeSync(SnapTimeSync &timeSync) { p_time_sync = &timeSync; }
130107

131108
/// Starts the processing
132109
bool begin(SnapTimeSync &timeSync) {
@@ -137,7 +114,7 @@ class SnapClient {
137114
/// Starts the processing
138115
bool begin() {
139116
#if defined(ESP32)
140-
if (is_wifi){
117+
if (is_wifi) {
141118
if (WiFi.status() != WL_CONNECTED) {
142119
ESP_LOGE(TAG, "WiFi not connected");
143120
return false;
@@ -191,19 +168,15 @@ class SnapClient {
191168
}
192169

193170
/// Provides the actual processor
194-
SnapProcessor& snapProcessor(){
195-
return *p_snapprocessor;
196-
}
171+
SnapProcessor &snapProcessor() { return *p_snapprocessor; }
197172

198173
/// Defines the Snap output implementation to be used
199-
void setSnapOutput(SnapOutput &out){
200-
p_snapprocessor->setSnapOutput(out);
201-
}
174+
void setSnapOutput(SnapOutput &out) { p_snapprocessor->setSnapOutput(out); }
202175

203176
/// Call from Arduino Loop - to receive and process the audio data
204177
bool doLoop() { return p_snapprocessor->doLoop(); }
205178

206-
protected:
179+
protected:
207180
const char *TAG = "SnapClient";
208181
SnapTime &snap_time = SnapTime::instance();
209182
SnapProcessor default_processor;
@@ -238,7 +211,8 @@ class SnapClient {
238211
server_ip[2], server_ip[3]);
239212
server_port = MDNS.port(0);
240213

241-
ESP_LOGI(TAG, "MDNS: SNAPCAST ip: %s, port: %d", str_address, server_port);
214+
ESP_LOGI(TAG, "MDNS: SNAPCAST ip: %s, port: %d", str_address,
215+
server_port);
242216

243217
} else {
244218
ESP_LOGE(TAG, "SNAPCAST server not found");
@@ -262,7 +236,6 @@ class SnapClient {
262236
#endif
263237
}
264238

265-
266239
void setupMACAddress() {
267240
#ifdef ESP32
268241
const char *adr = strdup(WiFi.macAddress().c_str());
@@ -273,4 +246,4 @@ class SnapClient {
273246
}
274247
};
275248

276-
}
249+
} // namespace snap_arduino

0 commit comments

Comments
 (0)