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 "AudioTools/AudioCodecs/CodecWAV.h"
35 template<
class Client,
class Server>
54 AudioServerT(
const char *network,
const char *password,
int port = 80) {
55 this->network = (
char *)network;
56 this->password = (
char *)password;
72 this->content_type = contentType;
92 this->content_type = contentType;
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");
177 char *password =
nullptr;
178 char *network =
nullptr;
181 const char *content_type =
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>;
306 this->encoder = encoder;
316 const char *password,
int port = 80)
318 this->encoder = encoder;
335 int bits_per_sample = 16,
BaseConverter *converter =
nullptr) {
345 encoded_stream.setEncoder(encoder);
346 encoded_stream.
begin(audio_info);
361 this->audio_info = info;
365 encoded_stream.setEncoder(encoder);
366 if (!encoded_stream.
begin(audio_info)){
367 LOGE(
"encoder begin failed");
388 encoded_stream.setEncoder(encoder);
389 encoded_stream.
begin(audio_info);
402 int bits_per_sample = 16) {
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();
490 if (encoder !=
nullptr) {
void stop()
Public generic methods.
Definition: AudioRuntime.h:28