Skip to content

Commit

Permalink
feat(rtsp): Updated rtsp to explicit types (#99)
Browse files Browse the repository at this point in the history
* Updated rtp/jpeg classes to use strongly typed types (uint8_t instead of char, uintX_t instead of int) for better compiler support to prevent conversion warnings / errors and to ensure data does not change due to type change.
* Updated display_frame python script to not have frame in it but instead to show image from comand line
  • Loading branch information
finger563 authored Jul 31, 2023
1 parent 7dcc9df commit 0aaa986
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
10 changes: 6 additions & 4 deletions components/rtsp/include/jpeg_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class JpegFrame {
/// @param data The buffer containing the jpeg data.
/// @param size The size of the buffer.
explicit JpegFrame(const char *data, size_t size)
: data_(data, data + size), header_(std::string_view(data_.data(), size)) {}
: data_(data, data + size), header_(std::string_view((const char *)data_.data(), size)) {}

/// Get a reference to the header.
/// @return A reference to the header.
Expand Down Expand Up @@ -67,15 +67,17 @@ class JpegFrame {
/// Get the serialized data.
/// This will return the serialized data.
/// @return The serialized data.
std::string_view get_data() const { return std::string_view(data_.data(), data_.size()); }
std::string_view get_data() const {
return std::string_view((const char *)data_.data(), data_.size());
}

/// Get the scan data.
/// This will return the scan data.
/// @return The scan data.
std::string_view get_scan_data() const {
auto header_data = header_.get_data();
size_t header_size = header_data.size();
return std::string_view(data_.data() + header_size, data_.size() - header_size);
return std::string_view((const char *)data_.data() + header_size, data_.size() - header_size);
}

protected:
Expand Down Expand Up @@ -117,7 +119,7 @@ class JpegFrame {
data_.push_back(0xD9);
}

std::vector<char> data_;
std::vector<uint8_t> data_;
JpegHeader header_;
bool finalized_ = false;
};
Expand Down
16 changes: 9 additions & 7 deletions components/rtsp/include/jpeg_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class JpegHeader {

/// Get the JPEG header data.
/// @return The JPEG header data.
std::string_view get_data() const { return std::string_view(data_.data(), data_.size()); }
std::string_view get_data() const {
return std::string_view((const char *)data_.data(), data_.size());
}

/// Get the Quantization table at the index.
/// @param index The index of the quantization table.
Expand All @@ -52,7 +54,7 @@ class JpegHeader {
static constexpr int DQT_HEADER_SIZE = 5;

// JFIF APP0 Marker for version 1.2 with 72 DPI and no thumbnail
static constexpr char JFIF_APP0_DATA[] = {
static constexpr uint8_t JFIF_APP0_DATA[] = {
0xFF, 0xE0, // APP0 marker
0x00, 0x10, // Length of APP0 data (16 bytes)
0x4A, 0x46, 0x49, 0x46, 0x00, // Identifier: ASCII "JFIF\0"
Expand All @@ -63,7 +65,7 @@ class JpegHeader {
0x00, 0x00 // No thumbnail
};

static constexpr char HUFFMAN_TABLES[] = {
static constexpr uint8_t HUFFMAN_TABLES[] = {
// Huffman table DC (luminance)
0xff,
0xc4,
Expand Down Expand Up @@ -503,7 +505,7 @@ class JpegHeader {
};

// Scan header (SOS)
static constexpr char SOS[] = {
static constexpr uint8_t SOS[] = {
0xFF, 0xDA, // SOS marker
0x00, 0x0C, // length
0x03, // number of components
Expand Down Expand Up @@ -618,7 +620,7 @@ class JpegHeader {
fmt::print("Invalid DQT marker\n");
return;
}
q0_table_ = std::string_view(data_.data() + offset, 64);
q0_table_ = std::string_view((const char *)data_.data() + offset, 64);
offset += 64;
// check the DQT marker for chrominance
if (data_[offset++] != 0xFF || data_[offset++] != 0xDB) {
Expand All @@ -633,7 +635,7 @@ class JpegHeader {
fmt::print("Invalid DQT marker\n");
return;
}
q1_table_ = std::string_view(data_.data() + offset, 64);
q1_table_ = std::string_view((const char *)data_.data() + offset, 64);
offset += 64;
// check huffman tables
if (data_[offset++] != 0xFF || data_[offset++] != 0xC4) {
Expand Down Expand Up @@ -758,6 +760,6 @@ class JpegHeader {
std::string_view q0_table_;
std::string_view q1_table_;

std::vector<char> data_;
std::vector<uint8_t> data_;
};
} // namespace espp
24 changes: 12 additions & 12 deletions components/rtsp/include/rtp_jpeg_packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class RtpJpegPacket : public RtpPacket {
/// Get the mjepg header.
/// @return The mjepg header.
std::string_view get_mjpeg_header() {
return std::string_view(get_payload().data(), MJPEG_HEADER_SIZE);
return std::string_view((char *)get_payload().data(), MJPEG_HEADER_SIZE);
}

/// Get whether the packet contains quantization tables.
Expand Down Expand Up @@ -128,7 +128,7 @@ class RtpJpegPacket : public RtpPacket {
/// @return The JPEG data.
std::string_view get_jpeg_data() const {
auto payload = get_payload();
return std::string_view(payload.data() + jpeg_data_start_, jpeg_data_size_);
return std::string_view((char *)payload.data() + jpeg_data_start_, jpeg_data_size_);
}

protected:
Expand All @@ -153,13 +153,13 @@ class RtpJpegPacket : public RtpPacket {
size_t offset = MJPEG_HEADER_SIZE;

if (has_q_tables()) {
int num_quant_bytes = payload[11];
uint8_t num_quant_bytes = payload[11];
int expected_num_quant_bytes = NUM_Q_TABLES * Q_TABLE_SIZE;
if (num_quant_bytes == expected_num_quant_bytes) {
q_tables_.resize(NUM_Q_TABLES);
offset += QUANT_HEADER_SIZE;
for (int i = 0; i < NUM_Q_TABLES; i++) {
q_tables_[i] = std::string_view(payload.data() + offset, Q_TABLE_SIZE);
q_tables_[i] = std::string_view((char *)payload.data() + offset, Q_TABLE_SIZE);
offset += Q_TABLE_SIZE;
}
}
Expand Down Expand Up @@ -193,20 +193,20 @@ class RtpJpegPacket : public RtpPacket {
packet[offset++] = NUM_Q_TABLES * Q_TABLE_SIZE;

memcpy(packet.data() + offset, q0.data(), Q_TABLE_SIZE);
q_tables_[0] = std::string_view(packet.data() + offset, Q_TABLE_SIZE);
q_tables_[0] = std::string_view((char *)packet.data() + offset, Q_TABLE_SIZE);
offset += Q_TABLE_SIZE;

memcpy(packet.data() + offset, q1.data(), Q_TABLE_SIZE);
q_tables_[1] = std::string_view(packet.data() + offset, Q_TABLE_SIZE);
q_tables_[1] = std::string_view((char *)packet.data() + offset, Q_TABLE_SIZE);
offset += Q_TABLE_SIZE;
}

int type_specific_{0};
int offset_{0};
int frag_type_{0};
int q_{0};
int width_{0};
int height_{0};
uint8_t type_specific_{0};
uint32_t offset_{0};
uint8_t frag_type_{0};
uint8_t q_{0};
uint32_t width_{0};
uint32_t height_{0};
int jpeg_data_start_{0};
int jpeg_data_size_{0};
std::vector<std::string_view> q_tables_;
Expand Down
12 changes: 7 additions & 5 deletions components/rtsp/include/rtp_packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class RtpPacket {
/// @note If you manually build the packet_ vector, you should make sure that you
/// call serialize() before calling this method.
/// @return A string_view of the whole packet.
std::string_view get_data() const { return std::string_view(packet_.data(), packet_.size()); }
std::string_view get_data() const {
return std::string_view((char *)packet_.data(), packet_.size());
}

/// Get the size of the RTP header.
/// @return The size of the RTP header.
Expand All @@ -81,17 +83,17 @@ class RtpPacket {
/// Get a string_view of the RTP header.
/// @return A string_view of the RTP header.
std::string_view get_rpt_header() const {
return std::string_view(packet_.data(), RTP_HEADER_SIZE);
return std::string_view((char *)packet_.data(), RTP_HEADER_SIZE);
}

/// Get a reference to the packet_ vector.
/// @return A reference to the packet_ vector.
std::vector<char> &get_packet() { return packet_; }
std::vector<uint8_t> &get_packet() { return packet_; }

/// Get a string_view of the payload.
/// @return A string_view of the payload.
std::string_view get_payload() const {
return std::string_view(packet_.data() + RTP_HEADER_SIZE, payload_size_);
return std::string_view((char *)packet_.data() + RTP_HEADER_SIZE, payload_size_);
}

/// Set the payload.
Expand Down Expand Up @@ -132,7 +134,7 @@ class RtpPacket {
packet_[11] = ssrc_ & 0xFF;
}

std::vector<char> packet_;
std::vector<uint8_t> packet_;
int version_;
bool padding_;
bool extension_;
Expand Down
Loading

0 comments on commit 0aaa986

Please sign in to comment.