Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitshaoxiang committed Sep 19, 2024
1 parent 69ac54e commit a83fca7
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 217 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
Expand Down Expand Up @@ -164,4 +164,4 @@ StatementMacros:
TabWidth: 4
UseCRLF: false
UseTab: Never
...
...
9 changes: 5 additions & 4 deletions examples/capture/capture.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ void setup() {
}
Serial.println("Camera Init Success");

TimerCAM.Camera.sensor->set_pixformat(TimerCAM.Camera.sensor,
PIXFORMAT_JPEG);
TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor,
FRAMESIZE_QVGA);
TimerCAM.Camera.sensor->set_pixformat(TimerCAM.Camera.sensor, PIXFORMAT_JPEG);
// 2MP Sensor
TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor, FRAMESIZE_UXGA);
// 3MP Sensor
// TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor, FRAMESIZE_QXGA);

TimerCAM.Camera.sensor->set_vflip(TimerCAM.Camera.sensor, 1);
TimerCAM.Camera.sensor->set_hmirror(TimerCAM.Camera.sensor, 0);
Expand Down
9 changes: 5 additions & 4 deletions examples/ext_wakeup/ext_wakeup.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ void setup() {
return;
}
Serial.println("Camera Init Success");
TimerCAM.Camera.sensor->set_pixformat(TimerCAM.Camera.sensor,
PIXFORMAT_JPEG);
TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor,
FRAMESIZE_QVGA);
TimerCAM.Camera.sensor->set_pixformat(TimerCAM.Camera.sensor, PIXFORMAT_JPEG);
// 2MP Sensor
TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor, FRAMESIZE_UXGA);
// 3MP Sensor
// TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor, FRAMESIZE_QXGA);

TimerCAM.Camera.sensor->set_vflip(TimerCAM.Camera.sensor, 1);
TimerCAM.Camera.sensor->set_hmirror(TimerCAM.Camera.sensor, 0);
Expand Down
12 changes: 6 additions & 6 deletions examples/http_post/http_post.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ void setup() {
}
Serial.println("Camera Init Success");

TimerCAM.Camera.sensor->set_pixformat(TimerCAM.Camera.sensor,
PIXFORMAT_JPEG);
TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor,
FRAMESIZE_QVGA);
TimerCAM.Camera.sensor->set_pixformat(TimerCAM.Camera.sensor, PIXFORMAT_JPEG);
// 2MP Sensor
TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor, FRAMESIZE_UXGA);
// 3MP Sensor
// TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor, FRAMESIZE_QXGA);

TimerCAM.Camera.sensor->set_vflip(TimerCAM.Camera.sensor, 1);
TimerCAM.Camera.sensor->set_hmirror(TimerCAM.Camera.sensor, 0);
Expand Down Expand Up @@ -69,8 +70,7 @@ void loop() {
String contentType = "image/jpeg";

// client.post("/post", contentType, postData);
client.post("/post", contentType.c_str(), TimerCAM.Camera.fb->len,
TimerCAM.Camera.fb->buf);
client.post("/post", contentType.c_str(), TimerCAM.Camera.fb->len, TimerCAM.Camera.fb->buf);

// read the status code and body of the response
int statusCode = client.responseStatusCode();
Expand Down
102 changes: 37 additions & 65 deletions examples/rtsp_stream/CRtspSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#include <time.h>

CRtspSession::CRtspSession(WiFiClient& aClient, CStreamer* aStreamer)
: LinkedListElement(aStreamer->getClientsListHead()),
m_Client(aClient),
m_Streamer(aStreamer) {
: LinkedListElement(aStreamer->getClientsListHead()), m_Client(aClient), m_Streamer(aStreamer) {
printf("Creating RTSP session\n");
Init();

Expand Down Expand Up @@ -37,8 +35,7 @@ void CRtspSession::Init() {
m_ContentLength = 0;
};

bool CRtspSession::ParseRtspRequest(char const* aRequest,
unsigned aRequestSize) {
bool CRtspSession::ParseRtspRequest(char const* aRequest, unsigned aRequestSize) {
char CmdName[RTSP_PARAM_STRING_MAX];
static char CurRequest[RTSP_BUFFER_SIZE]; // Note: we assume single
// threaded, this large buf we
Expand Down Expand Up @@ -118,26 +115,20 @@ bool CRtspSession::ParseRtspRequest(char const* aRequest,

// Skip over the prefix of any "rtsp://" or "rtsp:/" URL that follows:
unsigned j = i + 1;
while (j < CurRequestSize &&
(CurRequest[j] == ' ' || CurRequest[j] == '\t'))
while (j < CurRequestSize && (CurRequest[j] == ' ' || CurRequest[j] == '\t'))
++j; // skip over any additional white space
for (; (int)j < (int)(CurRequestSize - 8); ++j) {
if ((CurRequest[j] == 'r' || CurRequest[j] == 'R') &&
(CurRequest[j + 1] == 't' || CurRequest[j + 1] == 'T') &&
if ((CurRequest[j] == 'r' || CurRequest[j] == 'R') && (CurRequest[j + 1] == 't' || CurRequest[j + 1] == 'T') &&
(CurRequest[j + 2] == 's' || CurRequest[j + 2] == 'S') &&
(CurRequest[j + 3] == 'p' || CurRequest[j + 3] == 'P') &&
CurRequest[j + 4] == ':' && CurRequest[j + 5] == '/') {
(CurRequest[j + 3] == 'p' || CurRequest[j + 3] == 'P') && CurRequest[j + 4] == ':' &&
CurRequest[j + 5] == '/') {
j += 6;
if (CurRequest[j] == '/') { // This is a "rtsp://" URL; skip over
// the host:port part that follows:
++j;
unsigned uidx = 0;
while (
j < CurRequestSize && CurRequest[j] != '/' &&
CurRequest[j] != ' ' &&
uidx <
sizeof(m_URLHostPort) -
1) { // extract the host:port part of the URL here
while (j < CurRequestSize && CurRequest[j] != '/' && CurRequest[j] != ' ' &&
uidx < sizeof(m_URLHostPort) - 1) { // extract the host:port part of the URL here
m_URLHostPort[uidx] = CurRequest[j];
uidx++;
++j;
Expand All @@ -152,8 +143,7 @@ bool CRtspSession::ParseRtspRequest(char const* aRequest,
// Look for the URL suffix (before the following "RTSP/"):
parseSucceeded = false;
for (unsigned k = i + 1; (int)k < (int)(CurRequestSize - 5); ++k) {
if (CurRequest[k] == 'R' && CurRequest[k + 1] == 'T' &&
CurRequest[k + 2] == 'S' && CurRequest[k + 3] == 'P' &&
if (CurRequest[k] == 'R' && CurRequest[k + 1] == 'T' && CurRequest[k + 2] == 'S' && CurRequest[k + 3] == 'P' &&
CurRequest[k + 4] == '/') {
while (--k >= i && CurRequest[k] == ' ') {
}
Expand Down Expand Up @@ -181,16 +171,12 @@ bool CRtspSession::ParseRtspRequest(char const* aRequest,
// or \n as 'CSeq':
parseSucceeded = false;
for (j = i; (int)j < (int)(CurRequestSize - 5); ++j) {
if (CurRequest[j] == 'C' && CurRequest[j + 1] == 'S' &&
CurRequest[j + 2] == 'e' && CurRequest[j + 3] == 'q' &&
if (CurRequest[j] == 'C' && CurRequest[j + 1] == 'S' && CurRequest[j + 2] == 'e' && CurRequest[j + 3] == 'q' &&
CurRequest[j + 4] == ':') {
j += 5;
while (j < CurRequestSize &&
(CurRequest[j] == ' ' || CurRequest[j] == '\t'))
++j;
while (j < CurRequestSize && (CurRequest[j] == ' ' || CurRequest[j] == '\t')) ++j;
unsigned n;
for (n = 0; n < sizeof(m_CSeq) - 1 && j < CurRequestSize;
++n, ++j) {
for (n = 0; n < sizeof(m_CSeq) - 1 && j < CurRequestSize; ++n, ++j) {
char c = CurRequest[j];
if (c == '\r' || c == '\n') {
parseSucceeded = true;
Expand All @@ -206,27 +192,21 @@ bool CRtspSession::ParseRtspRequest(char const* aRequest,

// Also: Look for "Content-Length:" (optional)
for (j = i; (int)j < (int)(CurRequestSize - 15); ++j) {
if (CurRequest[j] == 'C' && CurRequest[j + 1] == 'o' &&
CurRequest[j + 2] == 'n' && CurRequest[j + 3] == 't' &&
CurRequest[j + 4] == 'e' && CurRequest[j + 5] == 'n' &&
CurRequest[j + 6] == 't' && CurRequest[j + 7] == '-' &&
(CurRequest[j + 8] == 'L' || CurRequest[j + 8] == 'l') &&
CurRequest[j + 9] == 'e' && CurRequest[j + 10] == 'n' &&
CurRequest[j + 11] == 'g' && CurRequest[j + 12] == 't' &&
CurRequest[j + 13] == 'h' && CurRequest[j + 14] == ':') {
if (CurRequest[j] == 'C' && CurRequest[j + 1] == 'o' && CurRequest[j + 2] == 'n' && CurRequest[j + 3] == 't' &&
CurRequest[j + 4] == 'e' && CurRequest[j + 5] == 'n' && CurRequest[j + 6] == 't' &&
CurRequest[j + 7] == '-' && (CurRequest[j + 8] == 'L' || CurRequest[j + 8] == 'l') &&
CurRequest[j + 9] == 'e' && CurRequest[j + 10] == 'n' && CurRequest[j + 11] == 'g' &&
CurRequest[j + 12] == 't' && CurRequest[j + 13] == 'h' && CurRequest[j + 14] == ':') {
j += 15;
while (j < CurRequestSize &&
(CurRequest[j] == ' ' || CurRequest[j] == '\t'))
++j;
while (j < CurRequestSize && (CurRequest[j] == ' ' || CurRequest[j] == '\t')) ++j;
unsigned num;
if (sscanf(&CurRequest[j], "%u", &num) == 1) m_ContentLength = num;
}
}
return true;
};

RTSP_CMD_TYPES CRtspSession::Handle_RtspRequest(char const* aRequest,
unsigned aRequestSize) {
RTSP_CMD_TYPES CRtspSession::Handle_RtspRequest(char const* aRequest, unsigned aRequestSize) {
if (ParseRtspRequest(aRequest, aRequestSize)) {
switch (m_RtspCmdType) {
case RTSP_OPTIONS: {
Expand Down Expand Up @@ -272,15 +252,12 @@ void CRtspSession::Handle_RtspDESCRIBE() {

// check whether we know a stream with the URL which is requested
m_StreamID = -1; // invalid URL
if ((strcmp(m_URLPreSuffix, "mjpeg") == 0) &&
(strcmp(m_URLSuffix, "1") == 0))
if ((strcmp(m_URLPreSuffix, "mjpeg") == 0) && (strcmp(m_URLSuffix, "1") == 0))
m_StreamID = 0;
else if ((strcmp(m_URLPreSuffix, "mjpeg") == 0) &&
(strcmp(m_URLSuffix, "2") == 0))
else if ((strcmp(m_URLPreSuffix, "mjpeg") == 0) && (strcmp(m_URLSuffix, "2") == 0))
m_StreamID = 1;
if (m_StreamID == -1) { // Stream not available
snprintf(Response, sizeof(Response),
"RTSP/1.0 404 Stream Not Found\r\nCSeq: %s\r\n%s\r\n", m_CSeq,
snprintf(Response, sizeof(Response), "RTSP/1.0 404 Stream Not Found\r\nCSeq: %s\r\n%s\r\n", m_CSeq,
DateHeader());

socketsend(m_RtspClient, Response, strlen(Response));
Expand All @@ -294,16 +271,15 @@ void CRtspSession::Handle_RtspDESCRIBE() {
ColonPtr = strstr(OBuf, ":");
if (ColonPtr != nullptr) ColonPtr[0] = 0x00;

snprintf(
SDPBuf, sizeof(SDPBuf),
"v=0\r\n"
"o=- %d 1 IN IP4 %s\r\n"
"s=\r\n"
"t=0 0\r\n" // start / stop - 0 -> unbounded and permanent session
"m=video 0 RTP/AVP 26\r\n" // currently we just handle UDP sessions
// "a=x-dimensions: 640,480\r\n"
"c=IN IP4 0.0.0.0\r\n",
rand(), OBuf);
snprintf(SDPBuf, sizeof(SDPBuf),
"v=0\r\n"
"o=- %d 1 IN IP4 %s\r\n"
"s=\r\n"
"t=0 0\r\n" // start / stop - 0 -> unbounded and permanent session
"m=video 0 RTP/AVP 26\r\n" // currently we just handle UDP sessions
// "a=x-dimensions: 640,480\r\n"
"c=IN IP4 0.0.0.0\r\n",
rand(), OBuf);
char StreamName[64];
switch (m_StreamID) {
case 0:
Expand Down Expand Up @@ -345,16 +321,13 @@ void CRtspSession::Handle_RtspSETUP() {

// simulate SETUP server response
if (m_TcpTransport)
snprintf(Transport, sizeof(Transport),
"RTP/AVP/TCP;unicast;interleaved=0-1");
snprintf(Transport, sizeof(Transport), "RTP/AVP/TCP;unicast;interleaved=0-1");
else
snprintf(Transport, sizeof(Transport),
"RTP/"
"AVP;unicast;destination=127.0.0.1;source=127.0.0.1;client_"
"port=%i-%i;server_port=%i-%i",
m_ClientRTPPort, m_ClientRTCPPort,
m_Streamer->GetRtpServerPort(),
m_Streamer->GetRtcpServerPort());
m_ClientRTPPort, m_ClientRTCPPort, m_Streamer->GetRtpServerPort(), m_Streamer->GetRtcpServerPort());
snprintf(Response, sizeof(Response),
"RTSP/1.0 200 OK\r\nCSeq: %s\r\n"
"%s\r\n"
Expand Down Expand Up @@ -397,17 +370,16 @@ int CRtspSession::GetStreamID() {
bool CRtspSession::handleRequests(uint32_t readTimeoutMs) {
if (m_stopped) return false; // Already closed down

static char
RecvBuf[RTSP_BUFFER_SIZE]; // Note: we assume single threaded, this
// large buf we keep off of the tiny stack
static char RecvBuf[RTSP_BUFFER_SIZE]; // Note: we assume single threaded, this
// large buf we keep off of the tiny stack

memset(RecvBuf, 0x00, sizeof(RecvBuf));
int res = socketread(m_RtspClient, RecvBuf, sizeof(RecvBuf), readTimeoutMs);
if (res > 0) {
// we filter away everything which seems not to be an RTSP command:
// O-ption, D-escribe, S-etup, P-lay, T-eardown
if ((RecvBuf[0] == 'O') || (RecvBuf[0] == 'D') || (RecvBuf[0] == 'S') ||
(RecvBuf[0] == 'P') || (RecvBuf[0] == 'T')) {
if ((RecvBuf[0] == 'O') || (RecvBuf[0] == 'D') || (RecvBuf[0] == 'S') || (RecvBuf[0] == 'P') ||
(RecvBuf[0] == 'T')) {
RTSP_CMD_TYPES C = Handle_RtspRequest(RecvBuf, res);
if (C == RTSP_PLAY)
m_streaming = true;
Expand Down
20 changes: 5 additions & 15 deletions examples/rtsp_stream/CRtspSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
#include "platglue-esp32.h"

// supported command types
enum RTSP_CMD_TYPES {
RTSP_OPTIONS,
RTSP_DESCRIBE,
RTSP_SETUP,
RTSP_PLAY,
RTSP_TEARDOWN,
RTSP_UNKNOWN
};
enum RTSP_CMD_TYPES { RTSP_OPTIONS, RTSP_DESCRIBE, RTSP_SETUP, RTSP_PLAY, RTSP_TEARDOWN, RTSP_UNKNOWN };

#define RTSP_BUFFER_SIZE 10000 // for incoming requests, and outgoing responses
#define RTSP_PARAM_STRING_MAX 200
Expand All @@ -23,8 +16,7 @@ class CRtspSession : public LinkedListElement {
CRtspSession(WiFiClient& aRtspClient, CStreamer* aStreamer);
~CRtspSession();

RTSP_CMD_TYPES Handle_RtspRequest(char const* aRequest,
unsigned aRequestSize);
RTSP_CMD_TYPES Handle_RtspRequest(char const* aRequest, unsigned aRequestSize);
int GetStreamID();

/**
Expand Down Expand Up @@ -74,15 +66,13 @@ class CRtspSession : public LinkedListElement {
// parameters of the last received RTSP request

RTSP_CMD_TYPES
m_RtspCmdType; // command type (if any) of the current request
m_RtspCmdType; // command type (if any) of the current request
char m_URLPreSuffix[RTSP_PARAM_STRING_MAX]; // stream name pre suffix
char m_URLSuffix[RTSP_PARAM_STRING_MAX]; // stream name suffix
char m_CSeq[RTSP_PARAM_STRING_MAX]; // RTSP command sequence number
char m_URLHostPort[MAX_HOSTNAME_LEN]; // host:port part of the URL
unsigned m_ContentLength; // SDP string size

uint16_t
m_RtpClientPort; // RTP receiver port on client (in host byte order!)
uint16_t
m_RtcpClientPort; // RTCP receiver port on client (in host byte order!)
uint16_t m_RtpClientPort; // RTP receiver port on client (in host byte order!)
uint16_t m_RtcpClientPort; // RTCP receiver port on client (in host byte order!)
};
Loading

0 comments on commit a83fca7

Please sign in to comment.