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