3 #include "AudioConfig.h"
4 #if defined(USE_AUDIO_SERVER) && (defined(USE_ETHERNET) || defined(USE_WIFI))
8 # include <ESP8266WiFi.h>
15 # include <Ethernet.h>
18 #include "AudioCodecs/CodecWAV.h"
24 typedef void (*AudioServerDataCallback)(Print *out);
35 template<
class Client,
class Server>
42 AudioServerT(
int port = 80) {
44 copier.setCheckAvailableForWrite(
false);
54 AudioServerT(
const char *network,
const char *password,
int port = 80) {
55 this->network = (
char *)network;
56 this->password = (
char *)password;
58 copier.setCheckAvailableForWrite(
false);
69 bool begin(Stream &in,
const char *contentType) {
72 this->content_type = contentType;
88 bool begin(AudioServerDataCallback cb,
const char *contentType) {
92 this->content_type = contentType;
111 bool copy() {
return doLoop(); }
120 if (!client_obj.connected()) {
121 #if USE_SERVER_ACCEPT
122 client_obj = server.accept();
124 client_obj = server.available();
130 if (callback ==
nullptr) {
131 LOGD(
"copy data...");
132 if (converter_ptr ==
nullptr) {
135 copier.copy(*converter_ptr);
140 LOGI(
"stop client...");
146 LOGI(
"client was not connected");
153 void setConverter(BaseConverter *c) { converter_ptr = c; }
156 Stream &out() {
return client_obj; }
159 Client *out_ptr() {
return &client_obj; }
162 bool isClientConnected() {
return client_obj.connected(); }
165 void setCopyBufferSize(
int size){
177 char *password =
nullptr;
178 char *network =
nullptr;
181 const char *content_type =
nullptr;
182 AudioServerDataCallback callback =
nullptr;
183 Stream *in =
nullptr;
185 BaseConverter *converter_ptr =
nullptr;
187 void setupServer(
int port) {
195 if (WiFi.status() != WL_CONNECTED && network !=
nullptr &&
196 password !=
nullptr) {
197 WiFi.begin(network, password);
198 while (WiFi.status() != WL_CONNECTED) {
203 WiFi.setSleep(
false);
207 Serial.print(
"IP address: ");
208 Serial.println(WiFi.localIP());
212 virtual void sendReplyHeader() {
216 client_obj.println(
"HTTP/1.1 200 OK");
217 LOGI(
"Reply: HTTP/1.1 200 OK");
218 if (content_type !=
nullptr) {
219 client_obj.print(
"Content-type:");
220 client_obj.println(content_type);
221 LOGI(
"Content-type: %s", content_type);
223 client_obj.println();
224 if (!client_obj.connected()){
225 LOGE(
"connection was closed");
229 virtual void sendReplyContent() {
231 if (callback !=
nullptr) {
233 LOGI(
"sendReply - calling callback");
234 callback(&client_obj);
236 }
else if (in !=
nullptr) {
238 LOGI(
"sendReply - Returning audio stream...");
239 copier.begin(client_obj, *in);
240 if (!client_obj.connected()){
241 LOGE(
"connection was closed");
247 void processClient() {
253 while (client_obj.connected()) {
256 char c = client_obj.read();
258 LOGI(
"Request: %s", currentLine.c_str());
262 if (currentLine.length() == 0) {
270 }
else if (c !=
'\r') {
281 using AudioServer = AudioServerT<WiFiClient, WiFiServer>;
282 using AudioServerWiFi = AudioServerT<WiFiClient, WiFiServer>;
286 using AudioServer = AudioServerT<EthernetClient, EthernetServer>;
287 using AudioServerEthernet = AudioServerT<EthernetClient, EthernetServer>;
299 class AudioEncoderServer :
public AudioServer {
305 AudioEncoderServer(AudioEncoder *encoder,
int port = 80) : AudioServer(port) {
306 this->encoder = encoder;
315 AudioEncoderServer(AudioEncoder *encoder,
const char *network,
316 const char *password,
int port = 80)
317 : AudioServer(network, password, port) {
318 this->encoder = encoder;
324 ~AudioEncoderServer() {}
334 bool begin(Stream &in,
int sample_rate,
int channels,
335 int bits_per_sample = 16, BaseConverter *converter =
nullptr) {
338 setConverter(converter);
339 audio_info.sample_rate = sample_rate;
340 audio_info.channels = channels;
341 audio_info.bits_per_sample = bits_per_sample;
342 encoder->setAudioInfo(audio_info);
344 encoded_stream.setOutput(&client_obj);
345 encoded_stream.setEncoder(encoder);
346 encoded_stream.begin(audio_info);
347 return AudioServer::begin(in, encoder->mime());
358 bool begin(Stream &in, AudioInfo info, BaseConverter *converter =
nullptr) {
361 this->audio_info = info;
362 setConverter(converter);
363 encoder->setAudioInfo(audio_info);
364 encoded_stream.setOutput(&client_obj);
365 encoded_stream.setEncoder(encoder);
366 if (!encoded_stream.begin(audio_info)){
367 LOGE(
"encoder begin failed");
371 return AudioServer::begin(in, encoder->mime());
381 bool begin(AudioStream &in, BaseConverter *converter =
nullptr) {
384 this->audio_info = in.audioInfo();
385 setConverter(converter);
386 encoder->setAudioInfo(audio_info);
387 encoded_stream.setOutput(&client_obj);
388 encoded_stream.setEncoder(encoder);
389 encoded_stream.begin(audio_info);
391 return AudioServer::begin(in, encoder->mime());
401 bool begin(AudioServerDataCallback cb,
int sample_rate,
int channels,
402 int bits_per_sample = 16) {
404 audio_info.sample_rate = sample_rate;
405 audio_info.channels = channels;
406 audio_info.bits_per_sample = bits_per_sample;
407 encoder->setAudioInfo(audio_info);
409 return AudioServer::begin(cb, encoder->mime());
413 AudioEncoder *audioEncoder() {
return encoder; }
417 EncodedAudioOutput encoded_stream;
418 AudioInfo audio_info;
419 AudioEncoder *encoder =
nullptr;
422 void sendReplyHeader()
override {}
424 void sendReplyContent()
override {
432 if (callback !=
nullptr) {
434 encoded_stream.setOutput(out_ptr());
435 encoded_stream.setEncoder(encoder);
436 encoded_stream.begin();
439 LOGI(
"sendReply - calling callback");
441 AudioServer::sendReplyHeader();
442 callback(&encoded_stream);
444 }
else if (in !=
nullptr) {
446 LOGI(
"sendReply - Returning encoded stream...");
448 encoded_stream.setOutput(out_ptr());
449 encoded_stream.setEncoder(encoder);
450 encoded_stream.begin();
452 copier.begin(encoded_stream, *in);
453 if (!client_obj.connected()){
454 LOGE(
"connection was closed");
457 AudioServer::sendReplyHeader();
470 class AudioWAVServer :
public AudioEncoderServer {
476 AudioWAVServer(
int port = 80) : AudioEncoderServer(new WAVEncoder(), port) {}
484 AudioWAVServer(
const char *network,
const char *password,
int port = 80)
485 : AudioEncoderServer(new WAVEncoder(), network, password, port) {}
489 AudioEncoder *encoder = audioEncoder();
490 if (encoder !=
nullptr) {
496 WAVEncoder &wavEncoder() {
return *
static_cast<WAVEncoder *
>(encoder); }
void stop()
Public generic methods.
Definition: AudioRuntime.h:27