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 <WiFiUdp.h>
4#if defined(ESP32)
5# include <esp_wifi.h>
6#endif
7#include "AudioTools/CoreAudio/BaseStream.h"
8#include "AudioTools/CoreAudio/Buffers.h"
9
10namespace audio_tools {
11
22class UDPStream : public BaseStream {
23public:
25 UDPStream() = default;
26
31 UDPStream(const char *ssid, const char *password) {
32 setSSID(ssid);
33 setPassword(password);
34 }
35
39 UDPStream(UDP &udp) { setUDP(udp); }
40
43 void setUDP(UDP &udp) { p_udp = &udp; };
44
47 int availableForWrite() { return 1492; }
48
53 int available() override {
54 int size = p_udp->available();
55 // if the curren package is used up we prvide the info for the next
56 if (size == 0) {
57 size = p_udp->parsePacket();
58 }
59 return size;
60 }
61
63 bool begin(IPAddress a, uint16_t port) {
64 connect();
65 remote_address_ext = a;
66 remote_port_ext = port;
67 return p_udp->begin(port);
68 }
69
71 bool begin(uint16_t port, uint16_t port_ext = 0) {
72 connect();
73 remote_address_ext = IPAddress((uint32_t)0);
74 remote_port_ext = port_ext != 0 ? port_ext : port;
75 printIP();
76 return p_udp->begin(port);
77 }
78
80 bool beginMulticast(IPAddress address, uint16_t port) {
81 connect();
82 return p_udp->beginMulticast(address,port);
83 }
84
86 uint16_t remotePort() {
87 uint16_t result = p_udp->remotePort();
88 return result != 0 ? result : remote_port_ext;
89 }
90
92 IPAddress remoteIP() {
93 // Determine address if it has not been specified
94 if ((uint32_t)remote_address_ext == 0) {
95 remote_address_ext = p_udp->remoteIP();
96 }
97 // IPAddress result = p_udp->remoteIP();
98 // LOGI("ip: %u", result);
99 return remote_address_ext;
100 }
101
103 size_t write(const uint8_t *data, size_t len) override {
104 TRACED();
105 p_udp->beginPacket(remoteIP(), remotePort());
106 size_t result = p_udp->write(data, len);
107 p_udp->endPacket();
108 return result;
109 }
110
112 size_t readBytes(uint8_t *data, size_t len) override {
113 TRACED();
114 size_t avail = available();
115 size_t bytes_read = 0;
116 if (avail > 0) {
117 // get the data now
118 bytes_read = p_udp->readBytes((uint8_t *)data, len);
119 }
120 return bytes_read;
121 }
122
123 void setSSID(const char *ssid) { this->ssid = ssid; }
124
125 void setPassword(const char *pwd) { this->password = pwd; }
126
127protected:
128 WiFiUDP default_udp;
129 UDP *p_udp = &default_udp;
130 uint16_t remote_port_ext = 0;
131 IPAddress remote_address_ext;
132 const char *ssid = nullptr;
133 const char *password = nullptr;
134
135 void printIP(){
136 Serial.print(WiFi.localIP());
137 Serial.print(":");
138 Serial.println(remote_port_ext);
139 }
140
142 void connect() {
143 if (WiFi.status() != WL_CONNECTED && ssid != nullptr &&
144 password != nullptr) {
145 WiFi.begin(ssid, password);
146 while (WiFi.status() != WL_CONNECTED) {
147 delay(500);
148 }
149 }
150#if defined(ESP32)
151 if (WiFi.status() == WL_CONNECTED) {
152 esp_wifi_set_ps(WIFI_PS_NONE);
153 // Performance Hack
154 // client.setNoDelay(true);
155 }
156#endif
157 }
158};
159
160} // 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:22
IPAddress remoteIP()
We use the same remote ip as defined in begin for write.
Definition UDPStream.h:92
void connect()
connect to WIFI if necessary
Definition UDPStream.h:142
uint16_t remotePort()
We use the same remote port as defined in begin for write.
Definition UDPStream.h:86
size_t readBytes(uint8_t *data, size_t len) override
Reads bytes using WiFi::readBytes.
Definition UDPStream.h:112
int available() override
Definition UDPStream.h:53
size_t write(const uint8_t *data, size_t len) override
Replys will be sent to the initial remote caller.
Definition UDPStream.h:103
int availableForWrite()
Definition UDPStream.h:47
UDPStream()=default
Default Constructor.
void setUDP(UDP &udp)
Defines an alternative UDP object. By default we use WiFiUDP.
Definition UDPStream.h:43
bool beginMulticast(IPAddress address, uint16_t port)
Starts to receive data in multicast from/with the indicated address / port.
Definition UDPStream.h:80
UDPStream(UDP &udp)
Constructor which defines an alternative UDP object. By default we use WiFiUDP.
Definition UDPStream.h:39
bool begin(uint16_t port, uint16_t port_ext=0)
Starts to receive data from/with the indicated port.
Definition UDPStream.h:71
bool begin(IPAddress a, uint16_t port)
Starts to send data to the indicated address / port.
Definition UDPStream.h:63
UDPStream(const char *ssid, const char *password)
Convinience constructor which defines the optional ssid and password.
Definition UDPStream.h:31
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10