From 772cfc5a4dfecd768e759fbd468b0d6b369553e1 Mon Sep 17 00:00:00 2001 From: zoff99 Date: Mon, 16 Sep 2024 21:09:41 +0200 Subject: [PATCH] change rtp_send_data to static allocation of buffer and fix a few comments for readability (again) --- amalgamation/toxcore_amalgamation.c | 28 +++++++++++------------ toxav/rtp.c | 35 ++++++++++++++++------------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/amalgamation/toxcore_amalgamation.c b/amalgamation/toxcore_amalgamation.c index dffd4dad4b..b3daba6985 100644 --- a/amalgamation/toxcore_amalgamation.c +++ b/amalgamation/toxcore_amalgamation.c @@ -75459,10 +75459,10 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, boo header.flags |= RTP_KEY_FRAME; } - uint8_t rdata[MAX_CRYPTO_DATA_SIZE]; - memset(rdata, 0, MAX_CRYPTO_DATA_SIZE); - rdata[0] = session->payload_type; // packet id == payload_type + uint8_t rdata_buf[MAX_CRYPTO_DATA_SIZE]; + memset(rdata_buf, 0, MAX_CRYPTO_DATA_SIZE); + rdata_buf[0] = session->payload_type; // packet id == payload_type LOGGER_API_DEBUG(session->tox, "check encoded video packet:length=%d MAX_CRYPTO_DATA_SIZE=%d", length, MAX_CRYPTO_DATA_SIZE); if ((length + RTP_HEADER_SIZE + 1) <= MAX_CRYPTO_DATA_SIZE) { @@ -75472,11 +75472,11 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, boo */ header.rtp_packet_number = session->rtp_packet_num; session->rtp_packet_num++; - rtp_header_pack(rdata + 1, &header); - memcpy(rdata + 1 + RTP_HEADER_SIZE, data, length); + rtp_header_pack(rdata_buf + 1, &header); + memcpy(rdata_buf + 1 + RTP_HEADER_SIZE, data, length); - if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, length) == -1) { - LOGGER_API_DEBUG(session->tox, "RTP send failed (len: %d)! std error: %s", (int)length, strerror(errno)); + if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata_buf, length + RTP_HEADER_SIZE + 1) == -1) { + LOGGER_API_DEBUG(session->tox, "RTP send failed (len: %d)! std error: %s", (length + RTP_HEADER_SIZE + 1), strerror(errno)); } } else { /** @@ -75489,11 +75489,11 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, boo while (((length - sent) + RTP_HEADER_SIZE + 1) > MAX_CRYPTO_DATA_SIZE) { header.rtp_packet_number = session->rtp_packet_num; session->rtp_packet_num++; - rtp_header_pack(rdata + 1, &header); - memcpy(rdata + 1 + RTP_HEADER_SIZE, data + sent, piece); + rtp_header_pack(rdata_buf + 1, &header); + memcpy(rdata_buf + 1 + RTP_HEADER_SIZE, data + sent, piece); if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, - rdata, piece + RTP_HEADER_SIZE + 1) == -1) { + rdata_buf, piece + RTP_HEADER_SIZE + 1) == -1) { LOGGER_API_DEBUG(session->tox, "RTP send failed (len: %d)! std error: %s", piece + RTP_HEADER_SIZE + 1, strerror(errno)); } @@ -75506,13 +75506,13 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, boo piece = length - sent; if (piece) { - memset(rdata, 0, MAX_CRYPTO_DATA_SIZE); + memset(rdata_buf, 0, MAX_CRYPTO_DATA_SIZE); header.rtp_packet_number = session->rtp_packet_num; session->rtp_packet_num++; - rtp_header_pack(rdata + 1, &header); - memcpy(rdata + 1 + RTP_HEADER_SIZE, data + sent, piece); + rtp_header_pack(rdata_buf + 1, &header); + memcpy(rdata_buf + 1 + RTP_HEADER_SIZE, data + sent, piece); - if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, + if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata_buf, piece + RTP_HEADER_SIZE + 1) == -1) { LOGGER_API_DEBUG(session->tox, "RTP send failed (len: %d)! std error: %s", piece + RTP_HEADER_SIZE + 1, strerror(errno)); diff --git a/toxav/rtp.c b/toxav/rtp.c index 70206a9a31..be0a28e04e 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c @@ -1186,22 +1186,24 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, boo header.flags |= RTP_KEY_FRAME; } - VLA(uint8_t, rdata, length + RTP_HEADER_SIZE + 1); - memset(rdata, 0, SIZEOF_VLA(rdata)); - rdata[0] = session->payload_type; // packet id == payload_type + uint8_t rdata_buf[MAX_CRYPTO_DATA_SIZE]; + memset(rdata_buf, 0, MAX_CRYPTO_DATA_SIZE); - if (MAX_CRYPTO_DATA_SIZE > (length + RTP_HEADER_SIZE + 1)) { + rdata_buf[0] = session->payload_type; // packet id == payload_type + LOGGER_API_DEBUG(session->tox, "check encoded video packet:length=%d MAX_CRYPTO_DATA_SIZE=%d", length, MAX_CRYPTO_DATA_SIZE); + + if ((length + RTP_HEADER_SIZE + 1) <= MAX_CRYPTO_DATA_SIZE) { /** - * The length is less than the maximum allowed length (including header) + * The length is less or equal than the maximum allowed length (including header) * Send the packet in single piece. */ header.rtp_packet_number = session->rtp_packet_num; session->rtp_packet_num++; - rtp_header_pack(rdata + 1, &header); - memcpy(rdata + 1 + RTP_HEADER_SIZE, data, length); + rtp_header_pack(rdata_buf + 1, &header); + memcpy(rdata_buf + 1 + RTP_HEADER_SIZE, data, length); - if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, SIZEOF_VLA(rdata)) == -1) { - LOGGER_API_DEBUG(session->tox, "RTP send failed (len: %zu)! std error: %s", SIZEOF_VLA(rdata), strerror(errno)); + if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata_buf, length + RTP_HEADER_SIZE + 1) == -1) { + LOGGER_API_DEBUG(session->tox, "RTP send failed (len: %d)! std error: %s", (length + RTP_HEADER_SIZE + 1), strerror(errno)); } } else { /** @@ -1211,14 +1213,14 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, boo uint32_t sent = 0; uint16_t piece = MAX_CRYPTO_DATA_SIZE - (RTP_HEADER_SIZE + 1); - while ((length - sent) + RTP_HEADER_SIZE + 1 > MAX_CRYPTO_DATA_SIZE) { + while (((length - sent) + RTP_HEADER_SIZE + 1) > MAX_CRYPTO_DATA_SIZE) { header.rtp_packet_number = session->rtp_packet_num; session->rtp_packet_num++; - rtp_header_pack(rdata + 1, &header); - memcpy(rdata + 1 + RTP_HEADER_SIZE, data + sent, piece); + rtp_header_pack(rdata_buf + 1, &header); + memcpy(rdata_buf + 1 + RTP_HEADER_SIZE, data + sent, piece); if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, - rdata, piece + RTP_HEADER_SIZE + 1) == -1) { + rdata_buf, piece + RTP_HEADER_SIZE + 1) == -1) { LOGGER_API_DEBUG(session->tox, "RTP send failed (len: %d)! std error: %s", piece + RTP_HEADER_SIZE + 1, strerror(errno)); } @@ -1231,12 +1233,13 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, boo piece = length - sent; if (piece) { + memset(rdata_buf, 0, MAX_CRYPTO_DATA_SIZE); header.rtp_packet_number = session->rtp_packet_num; session->rtp_packet_num++; - rtp_header_pack(rdata + 1, &header); - memcpy(rdata + 1 + RTP_HEADER_SIZE, data + sent, piece); + rtp_header_pack(rdata_buf + 1, &header); + memcpy(rdata_buf + 1 + RTP_HEADER_SIZE, data + sent, piece); - if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, + if (rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata_buf, piece + RTP_HEADER_SIZE + 1) == -1) { LOGGER_API_DEBUG(session->tox, "RTP send failed (len: %d)! std error: %s", piece + RTP_HEADER_SIZE + 1, strerror(errno));