3#include "AudioToolsConfig.h"
4#if defined(USE_AUDIO_SERVER) && (defined(USE_ETHERNET) || defined(USE_WIFI))
8#include <ESP8266WiFi.h>
19#include "AudioTools/AudioCodecs/CodecWAV.h"
35template <
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()) {
122 client_obj = server.accept();
124 client_obj = server.available();
130 if (callback ==
nullptr) {
131 LOGD(
"copy data...");
132 if (converter_ptr ==
nullptr) {
133 sent += copier.
copy();
135 sent += copier.
copy(*converter_ptr);
138 if (max_bytes > 0 && sent >= max_bytes) {
139 LOGI(
"range exhausted...");
147 LOGI(
"stop client...");
153 LOGI(
"client was not connected");
182 char *password =
nullptr;
183 char *network =
nullptr;
184 size_t max_bytes = 0;
188 const char *content_type =
nullptr;
190 Stream *in =
nullptr;
192 BaseConverter *converter_ptr =
nullptr;
194 void setupServer(
int port) {
202 if (WiFi.status() != WL_CONNECTED && network !=
nullptr &&
203 password !=
nullptr) {
204 WiFi.begin(network, password);
205 while (WiFi.status() != WL_CONNECTED) {
210 WiFi.setSleep(
false);
214 Serial.print(
"IP address: ");
215 Serial.println(WiFi.localIP());
219 virtual void sendReplyHeader() {
224 const char *response;
226 response =
"HTTP/1.1 206 OK";
228 response =
"HTTP/1.1 200 OK";
230 client_obj.println(response);
231 LOGI(
"%s", response);
232 if (content_type !=
nullptr) {
233 client_obj.print(
"Content-type:");
234 client_obj.println(content_type);
235 LOGI(
"Content-type: %s", content_type);
237 client_obj.println();
238 if (!client_obj.connected()) {
239 LOGE(
"connection was closed");
243 virtual void sendReplyContent() {
245 if (callback !=
nullptr) {
247 LOGI(
"sendReply - calling callback");
248 callback(&client_obj);
250 }
else if (in !=
nullptr) {
252 LOGI(
"sendReply - Returning audio stream...");
253 copier.begin(client_obj, *in);
254 if (!client_obj.connected()) {
255 LOGE(
"connection was closed");
261 void processClient() {
269 while (client_obj.connected()) {
272 char c = client_obj.read();
274 LOGI(
"Request: %s", currentLine.c_str());
275 if (currentLine.startsWith(String(
"Range: bytes="))) {
276 int minuspos = currentLine.indexOf(
'-', 13);
279 firstbyte = currentLine.substring(13, minuspos).toInt();
280 lastbyte = currentLine.substring(minuspos + 1).toInt();
285 if (currentLine.length() == 0) {
288 max_bytes = lastbyte - firstbyte;
295 }
else if (c !=
'\r') {
306using AudioServer = AudioServerT<WiFiClient, WiFiServer>;
307using AudioServerWiFi = AudioServerT<WiFiClient, WiFiServer>;
311using AudioServer = AudioServerT<EthernetClient, EthernetServer>;
312using AudioServerEthernet = AudioServerT<EthernetClient, EthernetServer>;
331 this->encoder = encoder;
341 const char *password,
int port = 80)
343 this->encoder = encoder;
360 int bits_per_sample = 16,
BaseConverter *converter =
nullptr) {
370 encoded_stream.setEncoder(encoder);
371 encoded_stream.
begin(audio_info);
386 this->audio_info = info;
390 encoded_stream.setEncoder(encoder);
391 if (!encoded_stream.
begin(audio_info)) {
392 LOGE(
"encoder begin failed");
413 encoded_stream.setEncoder(encoder);
414 encoded_stream.
begin(audio_info);
427 int bits_per_sample = 16) {
443 EncodedAudioOutput encoded_stream;
444 AudioInfo audio_info;
445 AudioEncoder *encoder =
nullptr;
448 void sendReplyHeader()
override {}
450 void sendReplyContent()
override {
458 if (callback !=
nullptr) {
460 encoded_stream.setOutput(
out_ptr());
461 encoded_stream.setEncoder(encoder);
462 encoded_stream.begin();
465 LOGI(
"sendReply - calling callback");
467 AudioServer::sendReplyHeader();
468 callback(&encoded_stream);
470 }
else if (in !=
nullptr) {
472 LOGI(
"sendReply - Returning encoded stream...");
474 encoded_stream.setOutput(
out_ptr());
475 encoded_stream.setEncoder(encoder);
476 encoded_stream.begin();
478 copier.begin(encoded_stream, *in);
479 if (!client_obj.connected()) {
480 LOGE(
"connection was closed");
483 AudioServer::sendReplyHeader();
516 if (encoder !=
nullptr) {
void stop()
Public generic methods.
Definition AudioRuntime.h:18