arduino-audio-tools
Loading...
Searching...
No Matches
RTSPPlatform.h
1#pragma once
2
3#include <Arduino.h>
4#include <errno.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10namespace audio_tools {
11
25template <typename TcpServer, typename TcpClient, typename UdpSocket>
27 public:
28 // Type aliases for template parameters
29 using TcpServerType = TcpServer;
30 using TcpClientType = TcpClient;
31 using UdpSocketType = UdpSocket;
35 static TcpServerType* createServer(uint16_t port){
36 TcpServerType *srv = new TcpServerType(port);
37 srv->begin();
38 return srv;
39 }
40
44 static TcpClientType getAvailableClient(TcpServerType *srv){
45 return srv->accept();
46 }
47
48 static constexpr TcpClient* NULL_TCP_SOCKET = nullptr;
49 static constexpr UdpSocket* NULL_UDP_SOCKET = nullptr;
50
55 static void closeSocket(TcpClient* s) {
56 if (s) {
57 s->stop();
58 // delete s; // TCP clients are typically not heap-allocated in Arduino
59 }
60 }
61
68 static void getSocketPeerAddr(TcpClient* s, IPAddress *addr,
69 uint16_t *port) {
70 // For WiFiClient and compatible types, assume remoteIP/remotePort are
71 // available
72 if (s) {
73 *addr = s->remoteIP();
74 *port = s->remotePort();
75 } else {
76 *addr = IPAddress();
77 *port = 0;
78 }
79 }
80
85 static void closeUdpSocket(UdpSocket* s) {
86 if (s) {
87 s->stop();
88 delete s;
89 }
90 }
91
97 static UdpSocket* createUdpSocket(unsigned short portNum) {
98 UdpSocket* s = new UdpSocket();
99
100 if (!s->begin(portNum)) {
101 printf("Can't bind port %d\n", portNum);
102 delete s;
103 return nullptr;
104 }
105
106 return s;
107 }
108
116 static ssize_t sendSocket(TcpClient* sockfd, const void *buf, size_t len) {
117 return sockfd->write((uint8_t *)buf, len);
118 }
119
129 static ssize_t sendUdpSocket(UdpSocket* sockfd, const void *buf, size_t len,
130 IPAddress destaddr, uint16_t destport) {
131 sockfd->beginPacket(destaddr, destport);
132 sockfd->write((const uint8_t *)buf, len);
133 if (!sockfd->endPacket()) printf("error sending udp packet\n");
134
135 return len;
136 }
137
146 static int readSocket(TcpClient* sock, char *buf, size_t buflen,
147 int timeoutmsec) {
148 if (!sock->connected()) {
149 printf("client has closed the socket\n");
150 return 0;
151 }
152
153 int numAvail = sock->available();
154 if (numAvail == 0 && timeoutmsec != 0) {
155 // sleep and hope for more
156 delay(timeoutmsec);
157 numAvail = sock->available();
158 }
159
160 if (numAvail == 0) {
161 // printf("timeout on read\n");
162 return -1;
163 } else {
164 // int numRead = sock->readBytesUntil('\n', buf, buflen);
165 int numRead = sock->readBytes(buf, buflen);
166 // printf("bytes avail %d, read %d: %s", numAvail, numRead, buf);
167 return numRead;
168 }
169 }
170};
171
172
173} // namespace audio_tools
Template-based platform abstraction for RTSP networking.
Definition RTSPPlatform.h:26
static TcpClientType getAvailableClient(TcpServerType *srv)
Get next available client from server.
Definition RTSPPlatform.h:44
static int readSocket(TcpClient *sock, char *buf, size_t buflen, int timeoutmsec)
Read from TCP socket with timeout.
Definition RTSPPlatform.h:146
static ssize_t sendUdpSocket(UdpSocket *sockfd, const void *buf, size_t len, IPAddress destaddr, uint16_t destport)
Send UDP packet to specified destination.
Definition RTSPPlatform.h:129
static void closeUdpSocket(UdpSocket *s)
Close a UDP socket.
Definition RTSPPlatform.h:85
static void closeSocket(TcpClient *s)
Close a TCP socket connection.
Definition RTSPPlatform.h:55
static UdpSocket * createUdpSocket(unsigned short portNum)
Create and bind a UDP socket.
Definition RTSPPlatform.h:97
static ssize_t sendSocket(TcpClient *sockfd, const void *buf, size_t len)
Send data over TCP socket.
Definition RTSPPlatform.h:116
static TcpServerType * createServer(uint16_t port)
Create a TCP server listening on provided port.
Definition RTSPPlatform.h:35
static void getSocketPeerAddr(TcpClient *s, IPAddress *addr, uint16_t *port)
Get remote peer address and port from TCP socket.
Definition RTSPPlatform.h:68
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10