arduino-audio-tools
Loading...
Searching...
No Matches
RTSPPlatform.h
1/*
2 * Author: Phil Schatzmann
3 *
4 * Based on Micro-RTSP library:
5 * https://github.com/geeksville/Micro-RTSP
6 * https://github.com/Tomp0801/Micro-RTSP-Audio
7 *
8 */
9#pragma once
10
11#include <Arduino.h>
12#include <errno.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
17
18namespace audio_tools {
19
33template <typename TcpServer, typename TcpClient, typename UdpSocket>
35 public:
36 // Type aliases for template parameters
37 using TcpServerType = TcpServer;
38 using TcpClientType = TcpClient;
39 using UdpSocketType = UdpSocket;
43 static TcpServerType* createServer(uint16_t port){
44 TcpServerType *srv = new TcpServerType(port);
45 srv->begin();
46 return srv;
47 }
48
52 static TcpClientType getAvailableClient(TcpServerType *srv){
53 return srv->accept();
54 }
55
56 static constexpr TcpClient* NULL_TCP_SOCKET = nullptr;
57 static constexpr UdpSocket* NULL_UDP_SOCKET = nullptr;
58
63 static void closeSocket(TcpClient* s) {
64 if (s) {
65 s->stop();
66 // delete s; // TCP clients are typically not heap-allocated in Arduino
67 }
68 }
69
76 static void getSocketPeerAddr(TcpClient* s, IPAddress *addr,
77 uint16_t *port) {
78 // For WiFiClient and compatible types, assume remoteIP/remotePort are
79 // available
80 if (s) {
81 *addr = s->remoteIP();
82 *port = s->remotePort();
83 } else {
84 *addr = IPAddress();
85 *port = 0;
86 }
87 }
88
93 static void closeUdpSocket(UdpSocket* s) {
94 if (s) {
95 s->stop();
96 delete s;
97 }
98 }
99
105 static UdpSocket* createUdpSocket(unsigned short portNum) {
106 UdpSocket* s = new UdpSocket();
107
108 if (!s->begin(portNum)) {
109 printf("Can't bind port %d\n", portNum);
110 delete s;
111 return nullptr;
112 }
113
114 return s;
115 }
116
124 static ssize_t sendSocket(TcpClient* sockfd, const void *buf, size_t len) {
125 return sockfd->write((uint8_t *)buf, len);
126 }
127
137 static ssize_t sendUdpSocket(UdpSocket* sockfd, const void *buf, size_t len,
138 IPAddress destaddr, uint16_t destport) {
139 sockfd->beginPacket(destaddr, destport);
140 sockfd->write((const uint8_t *)buf, len);
141 if (!sockfd->endPacket()) printf("error sending udp packet\n");
142
143 return len;
144 }
145
154 static int readSocket(TcpClient* sock, char *buf, size_t buflen,
155 int timeoutmsec) {
156 if (!sock->connected()) {
157 printf("client has closed the socket\n");
158 return 0;
159 }
160
161 int numAvail = sock->available();
162 if (numAvail == 0 && timeoutmsec != 0) {
163 // sleep and hope for more
164 delay(timeoutmsec);
165 numAvail = sock->available();
166 }
167
168 if (numAvail == 0) {
169 // printf("timeout on read\n");
170 return -1;
171 } else {
172 // int numRead = sock->readBytesUntil('\n', buf, buflen);
173 int numRead = sock->readBytes(buf, buflen);
174 // printf("bytes avail %d, read %d: %s", numAvail, numRead, buf);
175 return numRead;
176 }
177 }
178};
179
180
181} // namespace audio_tools
Template-based platform abstraction for RTSP networking.
Definition RTSPPlatform.h:34
static TcpClientType getAvailableClient(TcpServerType *srv)
Get next available client from server.
Definition RTSPPlatform.h:52
static int readSocket(TcpClient *sock, char *buf, size_t buflen, int timeoutmsec)
Read from TCP socket with timeout.
Definition RTSPPlatform.h:154
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:137
static void closeUdpSocket(UdpSocket *s)
Close a UDP socket.
Definition RTSPPlatform.h:93
static void closeSocket(TcpClient *s)
Close a TCP socket connection.
Definition RTSPPlatform.h:63
static UdpSocket * createUdpSocket(unsigned short portNum)
Create and bind a UDP socket.
Definition RTSPPlatform.h:105
static ssize_t sendSocket(TcpClient *sockfd, const void *buf, size_t len)
Send data over TCP socket.
Definition RTSPPlatform.h:124
static TcpServerType * createServer(uint16_t port)
Create a TCP server listening on provided port.
Definition RTSPPlatform.h:43
static void getSocketPeerAddr(TcpClient *s, IPAddress *addr, uint16_t *port)
Get remote peer address and port from TCP socket.
Definition RTSPPlatform.h:76
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10