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

46
lib/README Normal file
View File

@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

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 *);
};