arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
AudioServerT< Client, Server > Class Template Reference

A minimal, single-client HTTP server that streams audio (or any other data) to a browser or media player: in -copy> client. The data is provided either from an Arduino Stream or via an AudioServerDataCallback, and is copied to the connected client on each doLoop()/copy() call, so no threads or blocking loops are used. More...

#include <AudioServerT.h>

Inheritance diagram for AudioServerT< Client, Server >:
AudioEncoderServerT< Client, Server > AudioWAVServerT< Client, Server >

Public Member Functions

 AudioServerT (const char *network, const char *password, int port=80)
 Construct a new Audio WAV Server object.
 
 AudioServerT (int port=80)
 Construct a new Audio Server object We assume that the WiFi is already connected.
 
bool begin ()
 Just starts the server.
 
bool begin (AudioServerDataCallback cb, const char *contentType)
 Start the server. The data must be provided by a callback method.
 
bool begin (Stream &in, const char *contentType)
 Start the server. You need to be connected to WiFI before calling this method.
 
bool copy ()
 Add this method to your loop Returns true while the client is connected. (The same functionality like doLoop())
 
bool doLoop ()
 Add this method to your loop Returns true while the client is connected.
 
void end ()
 Ends the connection and resets the state.
 
bool isChunked ()
 Determines if chunked transfer encoding is active.
 
bool isClientConnected ()
 Checks if any client has connected.
 
size_t maxOutputSize ()
 Provides the defined total number of bytes that will be sent.
 
 operator bool ()
 Checks if any client has connected.
 
Streamout ()
 Provides the output stream.
 
Clientout_ptr ()
 Provides a pointer to the WiFiClient.
 
void setChunked (bool flag)
 
void setContentType (const char *contentType)
 Defines the content type (MIME type) of the data that will be sent to the client. Needs to be set before calling begin().
 
void setConverter (BaseConverter *c)
 defines a converter that will be used when the audio is rendered
 
void setCopyBufferSize (int size)
 Changes the copy buffer size.
 
void setMaxOutputSize (size_t size)
 

Protected Member Functions

void connectWiFi ()
 
void endChunked ()
 Sends the terminating 0-length chunk when chunked transfer is active.
 
void processClient ()
 Handle an new client connection and return the data.
 
virtual void sendReplyContent ()
 
virtual void sendReplyHeader ()
 
void setupServer (int port)
 
bool startServer ()
 

Protected Attributes

AudioServerDataCallback callback = nullptr
 
ChunkedPrint chunked_print
 
Client client_obj
 
const char * content_type = nullptr
 
BaseConverterconverter_ptr = nullptr
 
StreamCopy copier
 
Streamin = nullptr
 
bool is_chunked = true
 
size_t max_output_size = 0
 
std::string network
 
std::string password
 
size_t sent = 0
 
Server server
 

Detailed Description

template<class Client, class Server>
class audio_tools::AudioServerT< Client, Server >

A minimal, single-client HTTP server that streams audio (or any other data) to a browser or media player: in -copy> client. The data is provided either from an Arduino Stream or via an AudioServerDataCallback, and is copied to the connected client on each doLoop()/copy() call, so no threads or blocking loops are used.

The class is templated on the Client/Server implementation (e.g. WiFiClient/WiFiServer, EthernetClient/EthernetServer) so it can run on top of any Arduino networking stack that follows that API.

By default the response is sent with Transfer-Encoding: chunked since the total size is usually not known upfront; call setMaxOutputSize() if the size is known to send a Content-Length header instead.

This class only serves the data - it does not do any audio encoding. Use AudioEncoderServerT if you need the input to be encoded (e.g. to WAV or MP3) before it is sent to the client.

Template Parameters
Clientthe client class e.g. WiFiClient
Serverthe server class e.g. WiFiServer
Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ AudioServerT() [1/2]

template<class Client , class Server >
AudioServerT ( int  port = 80)
inline

Construct a new Audio Server object We assume that the WiFi is already connected.

◆ AudioServerT() [2/2]

template<class Client , class Server >
AudioServerT ( const char *  network,
const char *  password,
int  port = 80 
)
inline

Construct a new Audio WAV Server object.

Parameters
network
password

Member Function Documentation

◆ begin() [1/3]

template<class Client , class Server >
bool begin ( )
inline

Just starts the server.

◆ begin() [2/3]

template<class Client , class Server >
bool begin ( AudioServerDataCallback  cb,
const char *  contentType 
)
inline

Start the server. The data must be provided by a callback method.

Parameters
cb
contentTypeMime Type of result

◆ begin() [3/3]

template<class Client , class Server >
bool begin ( Stream in,
const char *  contentType 
)
inline

Start the server. You need to be connected to WiFI before calling this method.

Parameters
in
contentTypeMime Type of result

◆ connectWiFi()

template<class Client , class Server >
void connectWiFi ( )
inlineprotected

◆ copy()

template<class Client , class Server >
bool copy ( )
inline

Add this method to your loop Returns true while the client is connected. (The same functionality like doLoop())

Returns
true
false

◆ doLoop()

template<class Client , class Server >
bool doLoop ( )
inline

Add this method to your loop Returns true while the client is connected.

◆ end()

template<class Client , class Server >
void end ( )
inline

Ends the connection and resets the state.

◆ endChunked()

template<class Client , class Server >
void endChunked ( )
inlineprotected

Sends the terminating 0-length chunk when chunked transfer is active.

◆ isChunked()

template<class Client , class Server >
bool isChunked ( )
inline

Determines if chunked transfer encoding is active.

◆ isClientConnected()

template<class Client , class Server >
bool isClientConnected ( )
inline

Checks if any client has connected.

◆ maxOutputSize()

template<class Client , class Server >
size_t maxOutputSize ( )
inline

Provides the defined total number of bytes that will be sent.

◆ operator bool()

template<class Client , class Server >
operator bool ( )
inline

Checks if any client has connected.

◆ out()

template<class Client , class Server >
Stream & out ( )
inline

Provides the output stream.

◆ out_ptr()

template<class Client , class Server >
Client * out_ptr ( )
inline

Provides a pointer to the WiFiClient.

◆ processClient()

template<class Client , class Server >
void processClient ( )
inlineprotected

Handle an new client connection and return the data.

◆ sendReplyContent()

template<class Client , class Server >
virtual void sendReplyContent ( )
inlineprotectedvirtual

◆ sendReplyHeader()

template<class Client , class Server >
virtual void sendReplyHeader ( )
inlineprotectedvirtual

◆ setChunked()

template<class Client , class Server >
void setChunked ( bool  flag)
inline

Activates/deactivates HTTP chunked transfer encoding (Transfer-Encoding: chunked) instead of relying on the connection close to signal the end of the data. Needs to be set before calling begin().

◆ setContentType()

template<class Client , class Server >
void setContentType ( const char *  contentType)
inline

Defines the content type (MIME type) of the data that will be sent to the client. Needs to be set before calling begin().

◆ setConverter()

template<class Client , class Server >
void setConverter ( BaseConverter c)
inline

defines a converter that will be used when the audio is rendered

◆ setCopyBufferSize()

template<class Client , class Server >
void setCopyBufferSize ( int  size)
inline

Changes the copy buffer size.

◆ setMaxOutputSize()

template<class Client , class Server >
void setMaxOutputSize ( size_t  size)
inline

Defines the total number of bytes that will be sent so that a Content-Length header can be provided. If no size is defined (default), the size is unknown up front, so we fall back to chunked transfer encoding instead. Needs to be set before calling begin().

◆ setupServer()

template<class Client , class Server >
void setupServer ( int  port)
inlineprotected

◆ startServer()

template<class Client , class Server >
bool startServer ( )
inlineprotected

Connects to WiFi (if enabled) and starts the server: shared tail of all begin() overloads

Member Data Documentation

◆ callback

template<class Client , class Server >
AudioServerDataCallback callback = nullptr
protected

◆ chunked_print

template<class Client , class Server >
ChunkedPrint chunked_print
protected

◆ client_obj

template<class Client , class Server >
Client client_obj
protected

◆ content_type

template<class Client , class Server >
const char* content_type = nullptr
protected

◆ converter_ptr

template<class Client , class Server >
BaseConverter* converter_ptr = nullptr
protected

◆ copier

template<class Client , class Server >
StreamCopy copier
protected

◆ in

template<class Client , class Server >
Stream* in = nullptr
protected

◆ is_chunked

template<class Client , class Server >
bool is_chunked = true
protected

◆ max_output_size

template<class Client , class Server >
size_t max_output_size = 0
protected

◆ network

template<class Client , class Server >
std::string network
protected

◆ password

template<class Client , class Server >
std::string password
protected

◆ sent

template<class Client , class Server >
size_t sent = 0
protected

◆ server

template<class Client , class Server >
Server server
protected

The documentation for this class was generated from the following file: