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
23template <typename TcpServer, typename TcpClient, typename UdpSocket>
25 public:
26 // Type aliases for template parameters
27 using TcpServerType = TcpServer;
28 using TcpClientType = TcpClient;
29 using UdpSocketType = UdpSocket;
33 static TcpServerType* createServer(uint16_t port){
34 TcpServerType *srv = new TcpServerType(port);
35 srv->begin();
36 return srv;
37 }
38
42 static TcpClientType getAvailableClient(TcpServerType *srv){
43 return srv->accept();
44 }
45
46 static constexpr TcpClient* NULL_TCP_SOCKET = nullptr;
47 static constexpr UdpSocket* NULL_UDP_SOCKET = nullptr;
48
53 static void closeSocket(TcpClient* s) {
54 if (s) {
55 s->stop();
56 // delete s; // TCP clients are typically not heap-allocated in Arduino
57 }
58 }
59
66 static void getSocketPeerAddr(TcpClient* s, IPAddress *addr,
67 uint16_t *port) {
68 // For WiFiClient and compatible types, assume remoteIP/remotePort are
69 // available
70 if (s) {
71 *addr = s->remoteIP();
72 *port = s->remotePort();
73 } else {
74 *addr = IPAddress();
75 *port = 0;
76 }
77 }
78
83 static void closeUdpSocket(UdpSocket* s) {
84 if (s) {
85 s->stop();
86 delete s;
87 }
88 }
89
95 static UdpSocket* createUdpSocket(unsigned short portNum) {
96 UdpSocket* s = new UdpSocket();
97
98 if (!s->begin(portNum)) {
99 printf("Can't bind port %d\n", portNum);
100 delete s;
101 return nullptr;
102 }
103
104 return s;
105 }
106
114 static ssize_t sendSocket(TcpClient* sockfd, const void *buf, size_t len) {
115 return sockfd->write((uint8_t *)buf, len);
116 }
117
127 static ssize_t sendUdpSocket(UdpSocket* sockfd, const void *buf, size_t len,
128 IPAddress destaddr, uint16_t destport) {
129 sockfd->beginPacket(destaddr, destport);
130 sockfd->write((const uint8_t *)buf, len);
131 if (!sockfd->endPacket()) printf("error sending udp packet\n");
132
133 return len;
134 }
135
144 static int readSocket(TcpClient* sock, char *buf, size_t buflen,
145 int timeoutmsec) {
146 if (!sock->connected()) {
147 printf("client has closed the socket\n");
148 return 0;
149 }
150
151 int numAvail = sock->available();
152 if (numAvail == 0 && timeoutmsec != 0) {
153 // sleep and hope for more
154 delay(timeoutmsec);
155 numAvail = sock->available();
156 }
157
158 if (numAvail == 0) {
159 // printf("timeout on read\n");
160 return -1;
161 } else {
162 // int numRead = sock->readBytesUntil('\n', buf, buflen);
163 int numRead = sock->readBytes(buf, buflen);
164 // printf("bytes avail %d, read %d: %s", numAvail, numRead, buf);
165 return numRead;
166 }
167 }
168};
169
170
171} // namespace audio_tools
Template-based platform abstraction for RTSP networking.
Definition RTSPPlatform.h:24
static TcpClientType getAvailableClient(TcpServerType *srv)
Get next available client from server.
Definition RTSPPlatform.h:42
static int readSocket(TcpClient *sock, char *buf, size_t buflen, int timeoutmsec)
Read from TCP socket with timeout.
Definition RTSPPlatform.h:144
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:127
static void closeUdpSocket(UdpSocket *s)
Close a UDP socket.
Definition RTSPPlatform.h:83
static void closeSocket(TcpClient *s)
Close a TCP socket connection.
Definition RTSPPlatform.h:53
static UdpSocket * createUdpSocket(unsigned short portNum)
Create and bind a UDP socket.
Definition RTSPPlatform.h:95
static ssize_t sendSocket(TcpClient *sockfd, const void *buf, size_t len)
Send data over TCP socket.
Definition RTSPPlatform.h:114
static TcpServerType * createServer(uint16_t port)
Create a TCP server listening on provided port.
Definition RTSPPlatform.h:33
static void getSocketPeerAddr(TcpClient *s, IPAddress *addr, uint16_t *port)
Get remote peer address and port from TCP socket.
Definition RTSPPlatform.h:66
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10