3#include "AudioTools/AudioCodecs/CodecWAV.h"
4#include "AudioTools/CoreAudio/AudioStreams.h"
5#include "AudioTools/CoreAudio/StreamCopy.h"
6#include "AudioToolsConfig.h"
36template <
class Client,
class Server>
55 AudioServerT(
const char *network,
const char *password,
int port = 80) {
56 this->network = (
char *)network;
57 this->password = (
char *)password;
73 this->content_type = contentType;
93 this->content_type = contentType;
121 if (!client_obj.connected()) {
123 client_obj = server.accept();
125 client_obj = server.available();
131 if (callback ==
nullptr) {
132 LOGD(
"copy data...");
133 if (converter_ptr ==
nullptr) {
134 sent += copier.
copy();
136 sent += copier.
copy(*converter_ptr);
139 if (max_bytes > 0 && sent >= max_bytes) {
140 LOGI(
"range exhausted...");
148 LOGI(
"stop client...");
154 LOGI(
"client was not connected");
183 char *password =
nullptr;
184 char *network =
nullptr;
185 size_t max_bytes = 0;
189 const char *content_type =
nullptr;
191 Stream *in =
nullptr;
193 BaseConverter *converter_ptr =
nullptr;
195 void setupServer(
int port) {
203 if (WiFi.status() != WL_CONNECTED && network !=
nullptr &&
204 password !=
nullptr) {
205 WiFi.begin(network, password);
206 while (WiFi.status() != WL_CONNECTED) {
211 WiFi.setSleep(
false);
215 Serial.print(
"IP address: ");
216 Serial.println(WiFi.localIP());
220 virtual void sendReplyHeader() {
225 const char *response;
227 response =
"HTTP/1.1 206 OK";
229 response =
"HTTP/1.1 200 OK";
231 client_obj.println(response);
232 LOGI(
"%s", response);
233 if (content_type !=
nullptr) {
234 client_obj.print(
"Content-type:");
235 client_obj.println(content_type);
236 LOGI(
"Content-type: %s", content_type);
238 client_obj.println();
239 if (!client_obj.connected()) {
240 LOGE(
"connection was closed");
244 virtual void sendReplyContent() {
246 if (callback !=
nullptr) {
248 LOGI(
"sendReply - calling callback");
249 callback(&client_obj);
251 }
else if (in !=
nullptr) {
253 LOGI(
"sendReply - Returning audio stream...");
254 copier.begin(client_obj, *in);
255 if (!client_obj.connected()) {
256 LOGE(
"connection was closed");
262 void processClient() {
270 while (client_obj.connected()) {
273 char c = client_obj.read();
275 LOGI(
"Request: %s", currentLine.c_str());
276 if (currentLine.startsWith(String(
"Range: bytes="))) {
277 int minuspos = currentLine.indexOf(
'-', 13);
280 firstbyte = currentLine.substring(13, minuspos).toInt();
281 lastbyte = currentLine.substring(minuspos + 1).toInt();
286 if (currentLine.length() == 0) {
289 max_bytes = lastbyte - firstbyte;
296 }
else if (c !=
'\r') {
void(* AudioServerDataCallback)(Print *out)
Calback which writes the sound data to the stream.
Definition AudioServerT.h:11