arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
UDPStream.h
1#pragma once
2#include <WiFi.h>
3#include <esp_wifi.h>
4#include <WiFiUdp.h>
5
6#include "AudioTools/CoreAudio/BaseStream.h"
7#include "AudioTools/CoreAudio/Buffers.h"
8
9namespace audio_tools {
10
21class UDPStream : public BaseStream {
22public:
24 UDPStream() = default;
25
30 UDPStream(const char *ssid, const char *password) {
31 setSSID(ssid);
32 setPassword(password);
33 }
34
38 UDPStream(UDP &udp) { setUDP(udp); }
39
42 void setUDP(UDP &udp) { p_udp = &udp; };
43
46 int availableForWrite() { return 1492; }
47
52 int available() override {
53 int size = p_udp->available();
54 // if the curren package is used up we prvide the info for the next
55 if (size == 0) {
56 size = p_udp->parsePacket();
57 }
58 return size;
59 }
60
62 bool begin(IPAddress a, uint16_t port) {
63 connect();
64 remote_address_ext = a;
65 remote_port_ext = port;
66 return p_udp->begin(port);
67 }
68
70 bool begin(uint16_t port, uint16_t port_ext = 0) {
71 connect();
72 remote_address_ext = IPAddress((uint32_t)0);
73 remote_port_ext = port_ext != 0 ? port_ext : port;
74 return p_udp->begin(port);
75 }
76
78 bool beginMulticast(IPAddress address, uint16_t port) {
79 connect();
80 return p_udp->beginMulticast(address,port);
81 }
82
84 uint16_t remotePort() {
85 uint16_t result = p_udp->remotePort();
86 return result != 0 ? result : remote_port_ext;
87 }
88
90 IPAddress remoteIP() {
91 // Determine address if it has not been specified
92 if ((uint32_t)remote_address_ext == 0) {
93 remote_address_ext = p_udp->remoteIP();
94 }
95 // IPAddress result = p_udp->remoteIP();
96 // LOGI("ip: %u", result);
97 return remote_address_ext;
98 }
99
101 size_t write(const uint8_t *data, size_t len) override {
102 TRACED();
103 p_udp->beginPacket(remoteIP(), remotePort());
104 size_t result = p_udp->write(data, len);
105 p_udp->endPacket();
106 return result;
107 }
108
110 size_t readBytes(uint8_t *data, size_t len) override {
111 TRACED();
112 size_t avail = available();
113 size_t bytes_read = 0;
114 if (avail > 0) {
115 // get the data now
116 bytes_read = p_udp->readBytes((uint8_t *)data, len);
117 }
118 return bytes_read;
119 }
120
121 void setSSID(const char *ssid) { this->ssid = ssid; }
122
123 void setPassword(const char *pwd) { this->password = pwd; }
124
125protected:
126 WiFiUDP default_udp;
127 UDP *p_udp = &default_udp;
128 uint16_t remote_port_ext;
129 IPAddress remote_address_ext;
130 const char *ssid = nullptr;
131 const char *password = nullptr;
132
134 void connect() {
135 if (WiFi.status() != WL_CONNECTED && ssid != nullptr &&
136 password != nullptr) {
137 WiFi.begin(ssid, password);
138 while (WiFi.status() != WL_CONNECTED) {
139 delay(500);
140 }
141 }
142
143 if (WiFi.status() == WL_CONNECTED) {
144 // Performance Hack
145 // client.setNoDelay(true);
146 esp_wifi_set_ps(WIFI_PS_NONE);
147 }
148 }
149};
150
151} // namespace audio_tools
Base class for all Streams. It relies on write(const uint8_t *buffer, size_t size) and readBytes(uint...
Definition BaseStream.h:36
A UDP class which makes sure that we can use UDP as AudioSource and AudioSink. By default the WiFiUDP...
Definition UDPStream.h:21
IPAddress remoteIP()
We use the same remote ip as defined in begin for write.
Definition UDPStream.h:90
void connect()
connect to WIFI if necessary
Definition UDPStream.h:134
uint16_t remotePort()
We use the same remote port as defined in begin for write.
Definition UDPStream.h:84
size_t readBytes(uint8_t *data, size_t len) override
Reads bytes using WiFi::readBytes.
Definition UDPStream.h:110
int available() override
Definition UDPStream.h:52
size_t write(const uint8_t *data, size_t len) override
Replys will be sent to the initial remote caller.
Definition UDPStream.h:101
int availableForWrite()
Definition UDPStream.h:46
UDPStream()=default
Default Constructor.
void setUDP(UDP &udp)
Defines an alternative UDP object. By default we use WiFiUDP.
Definition UDPStream.h:42
bool beginMulticast(IPAddress address, uint16_t port)
Starts to receive data in multicast from/with the indicated address / port.
Definition UDPStream.h:78
UDPStream(UDP &udp)
Constructor which defines an alternative UDP object. By default we use WiFiUDP.
Definition UDPStream.h:38
bool begin(uint16_t port, uint16_t port_ext=0)
Starts to receive data from/with the indicated port.
Definition UDPStream.h:70
bool begin(IPAddress a, uint16_t port)
Starts to send data to the indicated address / port.
Definition UDPStream.h:62
UDPStream(const char *ssid, const char *password)
Convinience constructor which defines the optional ssid and password.
Definition UDPStream.h:30
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10