This commit is contained in:
Rene Zeldenthuis
2024-04-01 17:57:37 +02:00
parent c599657882
commit f59fb8bb9a
10 changed files with 200 additions and 184 deletions

View File

@@ -8,18 +8,19 @@
#include "micro_rtsp_camera.h"
#include "micro_rtsp_requests.h"
#include "micro_rtsp_streamer.h"
class micro_rtsp_server : WiFiServer
{
public:
micro_rtsp_server(const micro_rtsp_camera& source, unsigned frame_interval = 100);
micro_rtsp_server(micro_rtsp_camera &source, unsigned frame_interval = 100);
~micro_rtsp_server();
void begin(unsigned short port = 554);
void end();
unsigned get_frame_interval() { return frame_interval_; }
unsigned set_frame_interval(unsigned value) { return frame_interval_ = value; }
unsigned get_frame_interval() { return frame_interval_; }
unsigned set_frame_interval(unsigned value) { return frame_interval_ = value; }
void loop();
@@ -35,12 +36,10 @@ public:
};
private:
const micro_rtsp_camera &source_;
unsigned frame_interval_;
micro_rtsp_camera &source_;
unsigned frame_interval_;
unsigned long next_frame_update_;
unsigned long next_check_client_;
micro_rtsp_streamer streamer_;
std::list<rtsp_client> clients_;
};

View File

@@ -1,15 +1,90 @@
#pragma once
#include <jpg.h>
// https://en.wikipedia.org/wiki/Maximum_transmission_unit
constexpr size_t max_wifi_mtu = 2304;
// Payload JPG - https://www.ietf.org/rfc/rfc1890.txt
constexpr uint8_t RTP_PAYLOAD_JPG = 26;
// http://www.ietf.org/rfc/rfc2345.txt Each table is an array of 64 values given in zig-zag order, identical to the format used in a JFIF DQT marker segment.
constexpr size_t jpeg_luminance_table_length = 64;
constexpr size_t jpeg_chrominance_table_length = 64;
// https://www.ietf.org/rfc/rfc2326#section-10.12
typedef struct __attribute__((packed))
{
char magic; // Magic encapsulation ASCII dollar sign (24 hexadecimal)
uint8_t channel; // Channel identifier
uint16_t length; // Network order
} rtp_over_tcp_hdr_t;
class micro_rtsp_streamer
{
public:
micro_rtsp_streamer(const uint16_t width, const uint16_t height);
size_t create_jpg_packet(const uint8_t *jpg, uint8_t **jpg_current, const uint8_t *jpg_end, const uint32_t timestamp);
rtp_over_tcp_hdr_t *create_jpg_packet(const uint8_t *jpg_scan, const uint8_t *jpg_scan_end, uint8_t **jpg_offset, const uint32_t timestamp, const uint8_t* quantization_table_luminance, const uint8_t* quantization_table_chrominance);
private:
uint16_t width_, height_;
uint32_t ssrc_;
uint16_t sequenceNumber_;
uint16_t sequence_number_;
// RTP data header - http://www.ietf.org/rfc/rfc3550.txt
typedef struct __attribute__((packed))
{
uint16_t version : 2; // protocol version
uint16_t padding : 1; // padding flag
uint16_t extension : 1; // header extension flag
uint16_t cc : 4; // CSRC count
uint16_t marker : 1; // marker bit
uint16_t pt : 7; // payload type
uint16_t seq : 16; // sequence number
uint32_t ts; // timestamp
uint32_t ssrc; // synchronization source
} rtp_hdr_t;
// https://datatracker.ietf.org/doc/html/rfc2435
typedef struct __attribute__((packed))
{
uint32_t tspec : 8; // type-specific field
uint32_t off : 24; // fragment byte offset
uint8_t type; // id of jpeg decoder params
uint8_t q; // Q values 0-127 indicate the quantization tables. JPEG types 0 and 1 (and their corresponding types 64 and 65)
uint8_t width; // frame width in 8 pixel blocks
uint8_t height; // frame height in 8 pixel blocks
} jpeg_hdr_t;
typedef struct __attribute__((packed))
{
uint16_t dri;
uint16_t f : 1;
uint16_t l : 1;
uint16_t count : 14;
} jpeg_hdr_rst_t;
typedef struct __attribute__((packed))
{
uint8_t mbz;
uint8_t precision;
uint16_t length;
} jpeg_hdr_qtable_t;
// The types below will be returned, the jpeg_packet_with_quantization_t for the first packet, then the jpeg_packet_t
typedef struct __attribute__((packed))
{
rtp_over_tcp_hdr_t rtp_over_tcp_hdr;
rtp_hdr_t rtp_hdr;
jpeg_hdr_t jpeg_hdr;
jpeg_hdr_qtable_t jpeg_hdr_qtable;
uint8_t quantization_table_luminance[jpeg_luminance_table_length];
uint8_t quantization_table_chrominance[jpeg_chrominance_table_length];
uint8_t jpeg_data[];
} jpeg_packet_with_quantization_t;
typedef struct __attribute__((packed))
{
rtp_over_tcp_hdr_t rtp_over_tcp_hdr;
rtp_hdr_t rtp_hdr;
jpeg_hdr_t jpeg_hdr;
uint8_t jpeg_data[];
} jpeg_packet_t;
};

View File

@@ -1,12 +1,13 @@
#include <micro_rtsp_server.h>
#include <jpg.h>
#include <vector>
#include <memory>
// Check client connections every 100 milliseconds
#define CHECK_CLIENT_INTERVAL 10
micro_rtsp_server::micro_rtsp_server(const micro_rtsp_camera &source, unsigned frame_interval /*= 100*/)
: source_(source)
micro_rtsp_server::micro_rtsp_server(micro_rtsp_camera &source, unsigned frame_interval /*= 100*/)
: source_(source), streamer_(source.width(), source.height())
{
log_i("starting RTSP server");
frame_interval_ = frame_interval;
@@ -51,10 +52,26 @@ void micro_rtsp_server::loop()
if (next_frame_update_ < now)
{
next_frame_update_ = now + frame_interval_;
for (auto client : clients_)
auto ts = time(nullptr);
// Get next jpg frame
source_.update_frame();
// Decode to get quantitation- and scan data
jpg jpg;
auto jpg_data = source_.data();
auto jpg_size = source_.size();
assert(jpg.decode(jpg_data, jpg_size));
auto jpg_scan_current = (uint8_t*)jpg.jpeg_data_start;
while (jpg_scan_current < jpg.jpeg_data_end)
{
;
auto packet = streamer_.create_jpg_packet(jpg.jpeg_data_start, jpg.jpeg_data_end, &jpg_scan_current, ts, jpg.quantization_table_luminance_->data, jpg.quantization_table_chrominance_->data);
for (auto client : clients_)
{
;
// client->session->broadcastCurrentFrame(now);
}
free(packet);
}
}
}

View File

@@ -1,164 +1,89 @@
#include <stddef.h>
#include <memory.h>
#include <esp32-hal-log.h>
#include "micro_rtsp_streamer.h"
#include "esp_random.h"
// https://github.com/txgcwm/Linux-C-Examples/blob/master/h264/h264dec/rtcp.h
#define RTP_PAYLOAD_JPG 26
// RTP data header (http://www.ietf.org/rfc/rfc3550.txt)
struct rtp_hdr
{
uint16_t version : 2; // protocol version
uint16_t p : 1; // padding flag
uint16_t x : 1; // header extension flag
uint16_t cc : 4; // CSRC count
uint16_t m : 1; // marker bit
uint16_t pt : 7; // payload type
uint16_t seq : 16; // sequence number
uint32_t ts; // timestamp
uint32_t ssrc; // synchronization source
uint32_t csrc[]; // optional CSRC list
} rtp_hdr;
// https://datatracker.ietf.org/doc/html/rfc2435
// The following definition is from RFC1890
#define RTP_PT_JPEG 26
struct jpeghdr
{
uint32_t tspec : 8; // type-specific field
uint32_t off : 24; // fragment byte offset
uint8_t type; // id of jpeg decoder params
uint8_t q; // quantization factor (or table id)
uint8_t width; // frame width in 8 pixel blocks
uint8_t height; // frame height in 8 pixel blocks
};
struct jpeghdr_rst
{
uint16_t dri;
uint16_t f : 1;
uint16_t l : 1;
uint16_t count : 14;
};
struct jpeghdr_qtable
{
uint8_t mbz;
uint8_t precision;
uint16_t length;
};
#define RTP_JPEG_RESTART 0x40
micro_rtsp_streamer::micro_rtsp_streamer(const uint16_t width, const uint16_t height)
{
width_ = width;
height_ = height;
// Random number
ssrc_ = esp_random();
sequenceNumber_ = 0;
sequence_number_ = 0;
}
#define MAX_ESP32_MTU 1440
size_t micro_rtsp_streamer::create_jpg_packet(const uint8_t *jpg, uint8_t **jpg_current, const uint8_t *jpg_end, const uint32_t timestamp)
rtp_over_tcp_hdr_t *micro_rtsp_streamer::create_jpg_packet(const uint8_t *jpg_scan, const uint8_t *jpg_scan_end, uint8_t **jpg_offset, const uint32_t timestamp, const uint8_t* quantization_table_luminance, const uint8_t* quantization_table_chrominance)
{
const int MAX_FRAGMENT_SIZE = 1100; // FIXME, pick more carefully
int fragmentLen = MAX_FRAGMENT_SIZE;
auto jpegLen = jpg_end - *jpg_current;
// The MTU of wireless networks is 2,312 bytes. This size includes the packet headers.
const auto isFirstFragment = jpg_scan == *jpg_offset;
const auto include_quantization_tables = isFirstFragment && quantization_table_luminance != nullptr && quantization_table_chrominance != nullptr;
// Quantization tables musty be included in the first packet
const auto headers_size = include_quantization_tables ? sizeof(jpeg_packet_with_quantization_t) : sizeof(jpeg_packet_t);
const auto payload_size = max_wifi_mtu - headers_size;
auto offset = *jpg_current - jpg;
if (fragmentLen + offset > jpegLen) // Shrink last fragment if needed
fragmentLen = jpegLen - offset;
const auto jpg_bytes_left = jpg_scan_end - *jpg_offset;
const bool isLastFragment = jpg_bytes_left <= payload_size;
const auto jpg_bytes = isLastFragment ? jpg_bytes_left : payload_size;
const uint16_t packet_size = headers_size + jpg_bytes;
// bool isLastFragment = (fragmentOffset + fragmentLen) == jpegLen;
const auto packet = (jpeg_packet_t *)calloc(1, packet_size);
struct rtp_hdr header = {
.version = 2,
.m = 1, // TODO = 1 if last fragfment
.pt = RTP_PAYLOAD_JPG,
.seq = sequenceNumber_,
.ts = timestamp,
.ssrc = ssrc_};
// 4 bytes RTP over TCP header
assert(4 == sizeof(rtp_over_tcp_hdr_t));
packet->rtp_over_tcp_hdr.magic = '$'; // encapsulation
packet->rtp_over_tcp_hdr.channel = 0; // number of multiplexed sub-channel on RTPS connection - here the RTP channel
packet->rtp_over_tcp_hdr.length = packet_size;
log_v("rtp_over_tcp_hdr_t={.magic=%c,.channel=%i,.length=%i}", packet->rtp_over_tcp_hdr.magic, packet->rtp_over_tcp_hdr.channel, packet->rtp_over_tcp_hdr.length);
struct jpeghdr jpghdr = {
.tspec = 0, // type-specific field
.off = offset, // fragment byte offset
.type = 0, // id of jpeg decoder params
.q = 0x5e, // quantization factor (or table id)
.width = width_ >> 3, // frame width in 8 pixel blocks
.height = height_ >> 3 // frame height in 8 pixel blocks
};
// 12 bytes RTP header
assert(12 == sizeof(rtp_hdr_t));
packet->rtp_hdr.version = 2;
packet->rtp_hdr.marker = isLastFragment;
packet->rtp_hdr.pt = RTP_PAYLOAD_JPG;
packet->rtp_hdr.seq = sequence_number_;
packet->rtp_hdr.ts = timestamp;
packet->rtp_hdr.ssrc = ssrc_;
log_v("rtp_hdr={.version:%i,.padding:%i,.extension:%i,.cc:%i,.marker:%i,.pt:%i,.seq:%i,.ts:%u,.ssrc:%u}", packet->rtp_hdr.version, packet->rtp_hdr.padding, packet->rtp_hdr.extension, packet->rtp_hdr.cc, packet->rtp_hdr.marker, packet->rtp_hdr.pt, packet->rtp_hdr.seq, packet->rtp_hdr.ts, packet->rtp_hdr.ssrc);
uint8_t rtp_buffer[0x800];
// memset(RtpBuf, 0x00, sizeof(RtpBuf));
// Prepare the first 4 byte of the packet. This is the Rtp over Rtsp header in case of TCP based transport
rtp_buffer[0] = (uint8_t)'$'; // magic number
rtp_buffer[1] = 0; // number of multiplexed subchannel on RTPS connection - here the RTP channel
// rtp_buffer[2] = (RtpPacketSize & 0xFF00) >> 8;
// rtp_buffer[3] = (RtpPacketSize & 0x00FF);
// Prepare the 12 byte RTP header
// RtpBuf[4] = 0x80; // RTP version
// RtpBuf[5] = 0x1a | (isLastFragment ? 0x80 : 0x00); // JPEG payload (26) and marker bit
// RtpBuf[7] = m_SequenceNumber & 0x0FF; // each packet is counted with a sequence counter
// RtpBuf[6] = m_SequenceNumber >> 8;
// RtpBuf[8] = (m_Timestamp & 0xFF000000) >> 24; // each image gets a timestamp
// RtpBuf[9] = (m_Timestamp & 0x00FF0000) >> 16;
// RtpBuf[10] = (m_Timestamp & 0x0000FF00) >> 8;
// RtpBuf[11] = (m_Timestamp & 0x000000FF);
// RtpBuf[12] = 0x13; // 4 byte SSRC (sychronization source identifier)
// RtpBuf[13] = 0xf9; // we just an arbitrary number here to keep it simple
// RtpBuf[14] = 0x7e;
// RtpBuf[15] = 0x67;
// 8 bytes JPEG payload header
assert(8 == sizeof(jpeg_hdr_t));
packet->jpeg_hdr.tspec = 0; // type-specific field
packet->jpeg_hdr.off = (uint32_t)(*jpg_offset - jpg_scan); // fragment byte offset (24 bits in jpg)
packet->jpeg_hdr.type = 0; // id of jpeg decoder params
packet->jpeg_hdr.q = (uint8_t)(include_quantization_tables ? 0x80 : 0x5e); // quantization factor (or table id) 5eh=94d
packet->jpeg_hdr.width = (uint8_t)(width_ >> 3); // frame width in 8 pixel blocks
packet->jpeg_hdr.height = (uint8_t)(height_ >> 3); // frame height in 8 pixel blocks
log_v("jpeg_hdr={.tspec:%i,.off:0x%6x,.type:0x2%x,.q:%i,.width:%i.height:%i}", packet->jpeg_hdr.tspec, packet->jpeg_hdr.off, packet->jpeg_hdr.type, packet->jpeg_hdr.q, packet->jpeg_hdr.width, packet->jpeg_hdr.height);
// Prepare the 8 byte payload JPEG header
// RtpBuf[16] = 0x00; // type specific
// RtpBuf[17] = (fragmentOffset & 0x00FF0000) >> 16; // 3 byte fragmentation offset for fragmented images
// RtpBuf[18] = (fragmentOffset & 0x0000FF00) >> 8;
// RtpBuf[19] = (fragmentOffset & 0x000000FF);
// length so far should be 24 bytes
assert(24 == sizeof(jpeg_packet_t));
// /* These sampling factors indicate that the chrominance components of
// type 0 video is downsampled horizontally by 2 (often called 4:2:2)
// while the chrominance components of type 1 video are downsampled both
// horizontally and vertically by 2 (often called 4:2:0). */
// RtpBuf[20] = 0x00; // type (fixme might be wrong for camera data) https://tools.ietf.org/html/rfc2435
// RtpBuf[21] = q; // quality scale factor was 0x5e
// RtpBuf[22] = width_ / 8; // width / 8
// RtpBuf[23] = height_ / 8; // height / 8
int headerLen = 24; // Including jpeg header but not qant table header
if (includeQuantTbl)
{ // we need a quant header - but only in first packet of the frame
// printf("inserting quanttbl\n");
RtpBuf[24] = 0; // MBZ
RtpBuf[25] = 0; // 8 bit precision
RtpBuf[26] = 0; // MSB of lentgh
int numQantBytes = 64; // Two 64 byte tables
RtpBuf[27] = 2 * numQantBytes; // LSB of length
headerLen += 4;
memcpy(RtpBuf + headerLen, quant0tbl, numQantBytes);
headerLen += numQantBytes;
memcpy(RtpBuf + headerLen, quant1tbl, numQantBytes);
headerLen += numQantBytes;
if (include_quantization_tables)
{
const auto packet_with_quantization = (jpeg_packet_with_quantization_t *)packet;
assert(4 == sizeof(jpeg_hdr_qtable_t));
// Only in first packet of the frame
packet_with_quantization->jpeg_hdr_qtable.mbz = 0;
packet_with_quantization->jpeg_hdr_qtable.precision = 0; // 8 bit precision
packet_with_quantization->jpeg_hdr_qtable.length = jpeg_luminance_table_length + jpeg_chrominance_table_length;
log_v("jpeg_hdr_qtable={.mbz:%i,.precision:%i,.length:%d}", packet_with_quantization->jpeg_hdr_qtable.mbz, packet_with_quantization->jpeg_hdr_qtable.precision, packet_with_quantization->jpeg_hdr_qtable.length);
memcpy(packet_with_quantization->quantization_table_luminance, quantization_table_luminance, jpeg_luminance_table_length);
memcpy(packet_with_quantization->quantization_table_chrominance, quantization_table_chrominance, jpeg_chrominance_table_length);
// Copy JPG data
memcpy(packet_with_quantization->jpeg_data, *jpg_offset, jpg_bytes);
}
else
{
// Copy JPG data
memcpy(packet->jpeg_data, *jpg_offset, jpg_bytes);
}
// printf("Sending timestamp %d, seq %d, fragoff %d, fraglen %d, jpegLen %d\n", m_Timestamp, m_SequenceNumber, fragmentOffset, fragmentLen, jpegLen);
// append the JPEG scan data to the RTP buffer
memcpy(RtpBuf + headerLen, jpeg + fragmentOffset, fragmentLen);
fragmentOffset += fragmentLen;
// Update JPG offset
*jpg_offset += jpg_bytes;
// Update sequence number
sequence_number_++;
m_SequenceNumber++; // prepare the packet counter for the next packet
IPADDRESS otherip;
IPPORT otherport;
socketpeeraddr(m_Client, &otherip, &otherport);
return (rtp_over_tcp_hdr_t*)packet;
}