arduino-audio-tools
Loading...
Searching...
No Matches
src
AudioTools
Communication
UDPStream.h
Go to the documentation of this file.
1
#pragma once
2
#if (defined(__zephyr__))
3
# include "
AudioTools/Communication/Network/WiFiUDPZephyr.h
"
4
#elif defined(ARDUINO)
5
# include <WiFi.h>
6
# include <WiFiUdp.h>
7
# if defined(ESP32)
8
# include <esp_wifi.h>
9
# endif
10
#endif
11
12
#include "
AudioTools/CoreAudio/BaseStream.h
"
13
#include "
AudioTools/CoreAudio/Buffers.h
"
14
15
namespace
audio_tools
{
16
30
class
UDPStream
:
public
BaseStream
{
31
public
:
33
UDPStream
() =
default
;
34
39
UDPStream
(
const
char
*
ssid
,
const
char
*
password
) {
40
setSSID
(
ssid
);
41
setPassword
(
password
);
42
}
43
47
UDPStream
(
UDP
&udp) {
setUDP
(udp); }
48
51
void
setUDP
(
UDP
&udp) {
p_udp
= &udp; };
52
55
int
availableForWrite
() {
return
1492; }
56
61
int
available
()
override
{
62
int
size =
p_udp
->
available
();
63
// if the curren package is used up we prvide the info for the next
64
if
(size == 0) {
65
size =
p_udp
->
parsePacket
();
66
}
67
return
size;
68
}
69
71
bool
begin
(
IPAddress
a
,
uint16_t
port) {
72
connect
();
73
remote_address_ext
=
a
;
74
remote_port_ext
= port;
75
return
p_udp
->
begin
(port);
76
}
77
79
bool
begin
(
uint16_t
port,
uint16_t
port_ext
= 0) {
80
connect
();
81
remote_address_ext
=
IPAddress
((
uint32_t
)0);
82
remote_port_ext
=
port_ext
!= 0 ?
port_ext
: port;
83
printIP
();
84
return
p_udp
->
begin
(port);
85
}
86
88
bool
beginMulticast
(
IPAddress
address,
uint16_t
port) {
89
connect
();
90
return
p_udp
->
beginMulticast
(address,port);
91
}
92
94
uint16_t
remotePort
() {
95
uint16_t
result =
p_udp
->
remotePort
();
96
return
result != 0 ? result :
remote_port_ext
;
97
}
98
100
IPAddress
remoteIP
() {
101
// Determine address if it has not been specified
102
if
((
uint32_t
)
remote_address_ext
== 0) {
103
remote_address_ext
=
p_udp
->
remoteIP
();
104
}
105
// IPAddress result = p_udp->remoteIP();
106
// LOGI("ip: %u", result);
107
return
remote_address_ext
;
108
}
109
111
void
setTarget
(
IPAddress
ip,
uint16_t
port) {
112
remote_address_ext
= ip;
113
remote_port_ext
= port;
114
}
115
117
size_t
write
(
const
uint8_t
*data,
size_t
len)
override
{
118
TRACED
();
119
p_udp
->
beginPacket
(
remoteIP
(),
remotePort
());
120
size_t
result =
p_udp
->
write
(data, len);
121
p_udp
->
endPacket
();
122
return
result;
123
}
124
126
size_t
readBytes
(
uint8_t
*data,
size_t
len)
override
{
127
TRACED
();
128
size_t
avail
=
available
();
129
size_t
bytes_read
= 0;
130
if
(
avail
> 0) {
131
// get the data now
132
bytes_read
=
p_udp
->
readBytes
((
uint8_t
*)data, len);
133
}
134
return
bytes_read
;
135
}
136
137
void
setSSID
(
const
char
*
ssid
) { this->ssid =
ssid
; }
138
139
void
setPassword
(
const
char
*
pwd
) { this->
password
=
pwd
; }
140
141
protected
:
142
WiFiUDP
default_udp
;
143
UDP
*
p_udp
= &
default_udp
;
144
uint16_t
remote_port_ext
= 0;
145
IPAddress
remote_address_ext
;
146
const
char
*
ssid
=
nullptr
;
147
const
char
*
password
=
nullptr
;
148
149
void
printIP
(){
150
Serial
.print(
WiFi
.
localIP
());
151
Serial
.print(
":"
);
152
Serial
.println(
remote_port_ext
);
153
}
154
156
void
connect
() {
157
if
(
WiFi
.
status
() !=
WL_CONNECTED
&&
ssid
!=
nullptr
&&
158
password
!=
nullptr
) {
159
WiFi
.
begin
(
ssid
,
password
);
160
while
(
WiFi
.
status
() !=
WL_CONNECTED
) {
161
delay
(500);
162
}
163
}
164
#if defined(ESP32)
165
if
(
WiFi
.
status
() ==
WL_CONNECTED
) {
166
esp_wifi_set_ps
(
WIFI_PS_NONE
);
167
// Performance Hack
168
// client.setNoDelay(true);
169
}
170
#endif
171
}
172
};
173
174
}
// namespace audio_tools
Serial
static HardwareSerial Serial
Definition
Arduino.h:179
TRACED
#define TRACED()
Definition
AudioLoggerIDF.h:31
BaseStream.h
Buffers.h
WiFiUDPZephyr.h
audio_tools::BaseStream
Base class for all Streams. It relies on write(const uint8_t *buffer, size_t size) and readBytes(uint...
Definition
BaseStream.h:33
audio_tools::IPAddress
Arduino-compatible IPAddress class implemented in pure C++.
Definition
IPAddress.h:28
audio_tools::UDPStream
A UDP class which makes sure that we can use UDP as AudioSource and AudioSink. By default the WiFiUDP...
Definition
UDPStream.h:30
audio_tools::UDPStream::remoteIP
IPAddress remoteIP()
We use the same remote ip as defined in begin for write.
Definition
UDPStream.h:100
audio_tools::UDPStream::connect
void connect()
connect to WIFI if necessary
Definition
UDPStream.h:156
audio_tools::UDPStream::default_udp
WiFiUDP default_udp
Definition
UDPStream.h:142
audio_tools::UDPStream::setSSID
void setSSID(const char *ssid)
Definition
UDPStream.h:137
audio_tools::UDPStream::remotePort
uint16_t remotePort()
We use the same remote port as defined in begin for write.
Definition
UDPStream.h:94
audio_tools::UDPStream::readBytes
size_t readBytes(uint8_t *data, size_t len) override
Reads bytes using WiFi::readBytes.
Definition
UDPStream.h:126
audio_tools::UDPStream::ssid
const char * ssid
Definition
UDPStream.h:146
audio_tools::UDPStream::remote_port_ext
uint16_t remote_port_ext
Definition
UDPStream.h:144
audio_tools::UDPStream::available
int available() override
Definition
UDPStream.h:61
audio_tools::UDPStream::write
size_t write(const uint8_t *data, size_t len) override
Replys will be sent to the initial remote caller.
Definition
UDPStream.h:117
audio_tools::UDPStream::setTarget
void setTarget(IPAddress ip, uint16_t port)
Changes the target address and port for subsequent writes.
Definition
UDPStream.h:111
audio_tools::UDPStream::availableForWrite
int availableForWrite()
Definition
UDPStream.h:55
audio_tools::UDPStream::UDPStream
UDPStream()=default
Default Constructor.
audio_tools::UDPStream::setUDP
void setUDP(UDP &udp)
Defines an alternative UDP object. By default we use WiFiUDP.
Definition
UDPStream.h:51
audio_tools::UDPStream::beginMulticast
bool beginMulticast(IPAddress address, uint16_t port)
Starts to receive data in multicast from/with the indicated address / port.
Definition
UDPStream.h:88
audio_tools::UDPStream::password
const char * password
Definition
UDPStream.h:147
audio_tools::UDPStream::UDPStream
UDPStream(UDP &udp)
Constructor which defines an alternative UDP object. By default we use WiFiUDP.
Definition
UDPStream.h:47
audio_tools::UDPStream::begin
bool begin(uint16_t port, uint16_t port_ext=0)
Starts to receive data from/with the indicated port.
Definition
UDPStream.h:79
audio_tools::UDPStream::begin
bool begin(IPAddress a, uint16_t port)
Starts to send data to the indicated address / port.
Definition
UDPStream.h:71
audio_tools::UDPStream::UDPStream
UDPStream(const char *ssid, const char *password)
Convinience constructor which defines the optional ssid and password.
Definition
UDPStream.h:39
audio_tools::UDPStream::remote_address_ext
IPAddress remote_address_ext
Definition
UDPStream.h:145
audio_tools::UDPStream::p_udp
UDP * p_udp
Definition
UDPStream.h:143
audio_tools::UDPStream::printIP
void printIP()
Definition
UDPStream.h:149
audio_tools::UDPStream::setPassword
void setPassword(const char *pwd)
Definition
UDPStream.h:139
audio_tools::WiFiUDPZephyr
WiFiUDP (Zephyr zsock version) This class provides a UDP interface using Zephyr's socket API (zsock)....
Definition
WiFiUDPZephyr.h:21
audio_tools::WiFiUDPZephyr::beginPacket
int beginPacket(const char *host, uint16_t port)
Definition
WiFiUDPZephyr.h:114
audio_tools::WiFiUDPZephyr::remotePort
uint16_t remotePort()
Definition
WiFiUDPZephyr.h:252
audio_tools::WiFiUDPZephyr::beginMulticast
uint8_t beginMulticast(IPAddress multicast, uint16_t port)
Join a multicast group and listen on the given port.
Definition
WiFiUDPZephyr.h:55
audio_tools::WiFiUDPZephyr::endPacket
int endPacket()
Definition
WiFiUDPZephyr.h:138
audio_tools::WiFiUDPZephyr::available
int available()
Definition
WiFiUDPZephyr.h:206
audio_tools::WiFiUDPZephyr::readBytes
size_t readBytes(uint8_t *dest, size_t len)
Definition
WiFiUDPZephyr.h:224
audio_tools::WiFiUDPZephyr::remoteIP
IPAddress remoteIP() const
Returns the remote IP as an IPAddress (Arduino-compatible)
Definition
WiFiUDPZephyr.h:238
audio_tools::WiFiUDPZephyr::write
size_t write(uint8_t b)
Definition
WiFiUDPZephyr.h:159
audio_tools::WiFiUDPZephyr::begin
uint8_t begin(uint16_t port)
Definition
WiFiUDPZephyr.h:29
audio_tools::WiFiUDPZephyr::parsePacket
int parsePacket()
Definition
WiFiUDPZephyr.h:180
audio_tools::WiFiZephyr::begin
bool begin(const char *ssid, const char *password)
Definition
WiFiZephyr.h:51
audio_tools::WiFiZephyr::status
wifi_status_t status()
Definition
WiFiZephyr.h:85
audio_tools::WiFiZephyr::localIP
const char * localIP()
Definition
WiFiZephyr.h:90
audio_tools
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition
AudioCodecsBase.h:10
audio_tools::delay
void delay(uint32_t ms)
Definition
Arduino.h:255
audio_tools::WL_CONNECTED
@ WL_CONNECTED
Definition
WiFiZephyr.h:30
audio_tools::WiFi
static WiFiZephyr WiFi
Definition
WiFiZephyr.h:230
audio_tools::writeData
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition
AudioTypes.h:508
Generated by
1.9.8