first commit

This commit is contained in:
Rene Zeldenthuis
2022-07-03 23:42:14 +02:00
commit b0522218f6
12 changed files with 457 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
{
"name": "RTSPServer",
"version": "1.0.0",
"description": "",
"dependencies": {
"contrem/arduino-timer": "^2.3.1"
}
}

View File

@@ -0,0 +1,51 @@
#include "rtsp_server.h"
#include <esp32-hal-log.h>
#include <ESPmDNS.h>
#include <OV2640Streamer.h>
// URI: e.g. rtsp://192.168.178.27:554/mjpeg/1
rtsp_server::rtsp_server(OV2640 &cam, unsigned long interval, int port /*= 554*/)
: WiFiServer(port), cam_(cam)
{
log_i("Starting RTSP server");
WiFiServer::begin();
// Add service to mDNS - rtsp
MDNS.addService("rtsp", "tcp", 554);
timer_.every(interval, client_handler, this);
}
void rtsp_server::doLoop()
{
timer_.tick();
}
rtsp_server::rtsp_client::rtsp_client(const WiFiClient &client, OV2640 &cam)
{
wifi_client = client;
streamer = std::shared_ptr<OV2640Streamer>(new OV2640Streamer(&wifi_client, cam));
session = std::shared_ptr<CRtspSession>(new CRtspSession(&wifi_client, streamer.get()));
}
bool rtsp_server::client_handler(void *arg)
{
auto self = static_cast<rtsp_server *>(arg);
// Check if a client wants to connect
auto new_client = self->accept();
if (new_client)
self->clients_.push_back(std::unique_ptr<rtsp_client>(new rtsp_client(new_client, self->cam_)));
auto now = millis();
for (const auto &client : self->clients_)
{
// Handle requests
client->session->handleRequests(0);
// Send the frame. For now return the uptime as time marker, currMs
client->session->broadcastCurrentFrame(now);
}
self->clients_.remove_if([](std::unique_ptr<rtsp_client> const &c)
{ return c->session->m_stopped; });
return true;
}

View File

@@ -0,0 +1,36 @@
#pragma once
#include <list>
#include <WiFiServer.h>
#include <ESPmDNS.h>
#include <OV2640.h>
#include <CRtspSession.h>
#include <arduino-timer.h>
class rtsp_server : public WiFiServer
{
public:
rtsp_server(OV2640 &cam, unsigned long interval, int port = 554);
void doLoop();
private:
struct rtsp_client
{
public:
rtsp_client(const WiFiClient &client, OV2640 &cam);
WiFiClient wifi_client;
// Streamer for UDP/TCP based RTP transport
std::shared_ptr<CStreamer> streamer;
// RTSP session and state
std::shared_ptr<CRtspSession> session;
};
OV2640 cam_;
std::list<std::unique_ptr<rtsp_client>> clients_;
uintptr_t task_;
Timer<> timer_;
static bool client_handler(void *);
};