diff --git a/rtsp-server/net/Logger.cpp b/rtsp-server/net/Logger.cpp index 43630f7..8fc0f93 100644 --- a/rtsp-server/net/Logger.cpp +++ b/rtsp-server/net/Logger.cpp @@ -59,12 +59,14 @@ void Logger::Log(const Priority priority, const char *__file, { std::unique_lock lock(mutex_); - char buf[2048] = {0}; - sprintf(buf, "[%s][%s:%s:%d] ", Priority_To_String[priority], __file, + char buf[2048]; + auto buf_ptr = buf; + auto buf_end = buf + sizeof(buf); + buf_ptr += snprintf(buf_ptr, buf_end - buf_ptr, "[%s][%s:%s:%d] ", Priority_To_String[priority], __file, __func, __line); va_list args; va_start(args, fmt); - vsprintf(buf + strlen(buf), fmt, args); + vsnprintf(buf_ptr, buf_end - buf_ptr, fmt, args); va_end(args); this->Write(std::string(buf)); @@ -75,11 +77,13 @@ void Logger::Log2(const Priority priority, const char *fmt, ...) { std::unique_lock lock(mutex_); - char buf[4096] = {0}; - sprintf(buf, "[%s] ", Priority_To_String[priority]); + char buf[4096]; + auto buf_ptr = buf; + auto buf_end = buf + sizeof(buf); + buf_ptr += snprintf(buf_ptr, buf_end - buf_ptr, "[%s] ", Priority_To_String[priority]); va_list args; va_start(args, fmt); - vsprintf(buf + strlen(buf), fmt, args); + vsnprintf(buf_ptr, buf_end - buf_ptr, fmt, args); va_end(args); this->Write(std::string(buf)); diff --git a/rtsp-server/xop/AACSource.cpp b/rtsp-server/xop/AACSource.cpp index 8dbe48b..c557d20 100644 --- a/rtsp-server/xop/AACSource.cpp +++ b/rtsp-server/xop/AACSource.cpp @@ -42,8 +42,8 @@ AACSource::~AACSource() = default; string AACSource::GetMediaDescription(const uint16_t port) { - char buf[100] = {0}; - sprintf(buf, "m=audio %hu RTP/AVP 97", port); // \r\nb=AS:64 + char buf[100]; + snprintf(buf, sizeof(buf), "m=audio %hu RTP/AVP 97", port); // \r\nb=AS:64 return buf; } @@ -76,14 +76,14 @@ string AACSource::GetAttribute() // RFC 3640 strlen(fmtp_fmt); auto buf = vector(buf_size); const size_t rtpmap_format_size = - sprintf(buf.data(), rtpmap_fmt, samplerate_, channels_); + snprintf(buf.data(), buf_size, rtpmap_fmt, samplerate_, channels_); const array audioSpecificConfig = { static_cast((profile + 1) << 3 | samplingFrequencyIndex >> 1), static_cast(samplingFrequencyIndex << 7 | channels_ << 3)}; - sprintf(buf.data() + rtpmap_format_size, fmtp_fmt, + snprintf(buf.data() + rtpmap_format_size, buf_size - rtpmap_format_size, fmtp_fmt, audioSpecificConfig[0], audioSpecificConfig[1]); return buf.data(); diff --git a/rtsp-server/xop/G711ASource.cpp b/rtsp-server/xop/G711ASource.cpp index 2b420e4..7fd3123 100644 --- a/rtsp-server/xop/G711ASource.cpp +++ b/rtsp-server/xop/G711ASource.cpp @@ -35,8 +35,8 @@ G711ASource::~G711ASource() = default; string G711ASource::GetMediaDescription(const uint16_t port) { - char buf[100] = {0}; - sprintf(buf, "m=audio %hu RTP/AVP 8", port); + char buf[100]; + snprintf(buf, sizeof(buf), "m=audio %hu RTP/AVP 8", port); return buf; } diff --git a/rtsp-server/xop/H264Source.cpp b/rtsp-server/xop/H264Source.cpp index b7ddfe5..9f15dcb 100644 --- a/rtsp-server/xop/H264Source.cpp +++ b/rtsp-server/xop/H264Source.cpp @@ -23,14 +23,13 @@ #include "Base64Encode.h" #include "Nal.h" #include "H264NalUnit.h" -#include "H265Source.h" using namespace xop; using namespace std; H264Source::H264Source(vector sps, vector pps, const uint32_t framerate) - : framerate_(framerate), sps_(move(sps)), pps_(move(pps)) + : framerate_(framerate), sps_(std::move(sps)), pps_(std::move(pps)) { payload_ = 96; media_type_ = MediaType::H264; @@ -57,15 +56,15 @@ H264Source *H264Source::CreateNew(vector extraData, H264Source *H264Source::CreateNew(vector sps, vector pps, const uint32_t framerate) { - return new H264Source(move(sps), move(pps), framerate); + return new H264Source(std::move(sps), std::move(pps), framerate); } H264Source::~H264Source() = default; string H264Source::GetMediaDescription(const uint16_t port) { - char buf[100] = {0}; - sprintf(buf, "m=video %hu RTP/AVP 96", port); // \r\nb=AS:2000 + char buf[100]; + snprintf(buf, sizeof(buf), "m=video %hu RTP/AVP 96", port); // \r\nb=AS:2000 return buf; } @@ -89,7 +88,7 @@ string H264Source::GetAttribute() pps_base64.length(); auto buf = vector(buf_size); - sprintf(buf.data(), fmtp, profile_level_id, sps_base64.c_str(), + snprintf(buf.data(), buf_size, fmtp, profile_level_id, sps_base64.c_str(), pps_base64.c_str()); sdp.append(buf.data()); @@ -124,24 +123,24 @@ bool H264Source::HandleFrame(const MediaChannelId channelId, if (size_count > MAX_RTP_PAYLOAD_SIZE && end_index > nal_index) size_count -= nal[end_index--]->GetSize() + 2; if (end_index > nal_index) { - //Single-Time Aggregation Packet (STAP-A) - /* 0 1 2 3 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | RTP Header | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * |STAP-A NAL HDR | NALU 1 Size | NALU 1 HDR | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | NALU 1 Data | - * : : - * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | | NALU 2 Size | NALU 2 HDR | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | NALU 2 Data | - * : : - * | | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - */ + //Single-Time Aggregation Packet (STAP-A) + /* 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | RTP Header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |STAP-A NAL HDR | NALU 1 Size | NALU 1 HDR | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | NALU 1 Data | + * : : + * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | | NALU 2 Size | NALU 2 HDR | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | NALU 2 Data | + * : : + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ rtp_packet.size = RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + static_cast(size_count); rtp_packet.last = 1; @@ -177,18 +176,18 @@ bool H264Source::HandleFrame(const MediaChannelId channelId, if (!send_frame_callback_(channelId, rtp_packet)) return false; } else { - //Single NAL Unit Packets - /* 0 1 2 3 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * |F|NRI| type | | - * +-+-+-+-+-+-+-+-+ | - * | | - * | Bytes 2..n of a Single NAL unit | - * | | - * | | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - */ + //Single NAL Unit Packets + /* 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |F|NRI| type | | + * +-+-+-+-+-+-+-+-+ | + * | | + * | Bytes 2..n of a Single NAL unit | + * | | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ const auto nal_unit = nal[nal_index++]; if (nal_unit->GetSize() <= MAX_RTP_PAYLOAD_SIZE) { const auto size = nal_unit->CopyData( @@ -202,8 +201,8 @@ bool H264Source::HandleFrame(const MediaChannelId channelId, rtp_packet)) return false; } else { - //Fragmentation Units (FU-A) - /* 0 1 2 3 + //Fragmentation Units (FU-A) + /* 0 1 2 3 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | FU indicator | FU header | | diff --git a/rtsp-server/xop/H265Source.cpp b/rtsp-server/xop/H265Source.cpp index 0cca74d..7a928e7 100644 --- a/rtsp-server/xop/H265Source.cpp +++ b/rtsp-server/xop/H265Source.cpp @@ -30,10 +30,10 @@ H265Source::H265Source(vector vps, vector sps, vector pps, vector sei, const uint32_t framerate) : framerate_(framerate), - vps_(move(vps)), - sps_(move(sps)), - pps_(move(pps)), - sei_(move(sei)) + vps_(std::move(vps)), + sps_(std::move(sps)), + pps_(std::move(pps)), + sei_(std::move(sei)) { payload_ = 96; media_type_ = MediaType::H265; @@ -58,7 +58,7 @@ H265Source *H265Source::CreateNew(vector extraData, if (pps_nal_unit != nullptr) pps = pps_nal_unit->GetData(); - return new H265Source(vps, sps, pps, move(sei), framerate); + return new H265Source(vps, sps, pps, std::move(sei), framerate); } H265Source::~H265Source() = default; @@ -67,14 +67,14 @@ H265Source *H265Source::CreateNew(vector vps, vector sps, vector pps, vector sei, const uint32_t framerate) { - return new H265Source(move(vps), move(sps), move(pps), move(sei), + return new H265Source(std::move(vps), std::move(sps), std::move(pps), std::move(sei), framerate); } string H265Source::GetMediaDescription(const uint16_t port) { - char buf[100] = {0}; - sprintf(buf, "m=video %hu RTP/AVP 96", port); + char buf[100]; + snprintf(buf, sizeof(buf), "m=video %hu RTP/AVP 96", port); return buf; } @@ -86,7 +86,7 @@ string H265Source::GetAttribute() if (!vps_.empty() && !sps_.empty() && !pps_.empty()) { const auto fmtp = "a=fmtp:96 profile-space=%u;tier-flag=%u;" - "profile-id=%u;level-id=%u;interop-constraints=%012I64X;" + "profile-id=%u;level-id=%u;interop-constraints=%012llX;" "sprop-vps=%s;sprop-pps=%s;sprop-sps=%s;%s"; string vps_base64, pps_base64, sps_base64, sei; @@ -117,7 +117,7 @@ string H265Source::GetAttribute() sps_base64.length() + sei.length(); auto buf = vector(buf_size); - sprintf(buf.data(), fmtp, profile_space, tier_flag, profile_id, + snprintf(buf.data(), buf_size, fmtp, profile_space, tier_flag, profile_id, level_id, interop_constraints, vps_base64.c_str(), pps_base64.c_str(), sps_base64.c_str(), sei.c_str()); buf[strlen(buf.data()) - 1] = '\0'; @@ -154,28 +154,28 @@ bool H265Source::HandleFrame(const MediaChannelId channelId, if (size_count > MAX_RTP_PAYLOAD_SIZE && end_index > nal_index) size_count -= nal[end_index--]->GetSize() + 2; if (end_index > nal_index) { - //Aggregation Packets - /* 0 1 2 3 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | RTP Header | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | PayloadHdr (Type=48) | NALU 1 Size | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | NALU 1 HDR | | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ NALU 1 Data | - * | . . . | - * | | - * + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | . . . | NALU 2 Size | NALU 2 HDR | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | NALU 2 HDR | | - * +-+-+-+-+-+-+-+-+ NALU 2 Data | - * | . . . | - * | | - * | | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - */ + //Aggregation Packets + /* 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | RTP Header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | PayloadHdr (Type=48) | NALU 1 Size | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | NALU 1 HDR | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ NALU 1 Data | + * | . . . | + * | | + * + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | . . . | NALU 2 Size | NALU 2 HDR | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | NALU 2 HDR | | + * +-+-+-+-+-+-+-+-+ NALU 2 Data | + * | . . . | + * | | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ rtp_packet.size = RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + static_cast(size_count); rtp_packet.last = 1; @@ -219,19 +219,19 @@ bool H265Source::HandleFrame(const MediaChannelId channelId, if (!send_frame_callback_(channelId, rtp_packet)) return false; } else { - //Single NAL Unit Packets - /* 0 1 2 3 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | PayloadHdr | | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | - * | | - * | | - * | NAL unit payload data | - * | | - * | | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - */ + //Single NAL Unit Packets + /* 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | PayloadHdr | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | + * | | + * | | + * | NAL unit payload data | + * | | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ const auto nal_unit = nal[nal_index++]; if (nal_unit->GetSize() <= MAX_RTP_PAYLOAD_SIZE) { const auto size = nal_unit->CopyData( @@ -245,8 +245,8 @@ bool H265Source::HandleFrame(const MediaChannelId channelId, rtp_packet)) return false; } else { - //Fragmentation Units - /* 0 1 2 3 + //Fragmentation Units + /* 0 1 2 3 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | PayloadHdr (Type=49) | FU header | | @@ -335,7 +335,6 @@ uint32_t H265Source::GetTimestamp() return ts; #else */ //auto time_point = chrono::time_point_cast(chrono::system_clock::now()); - //auto time_point = chrono::time_point_cast(chrono::steady_clock::now()); const auto time_point = chrono::time_point_cast( chrono::steady_clock::now()); return static_cast( diff --git a/rtsp-server/xop/VP8Source.cpp b/rtsp-server/xop/VP8Source.cpp index 07d2a64..9fac799 100644 --- a/rtsp-server/xop/VP8Source.cpp +++ b/rtsp-server/xop/VP8Source.cpp @@ -35,8 +35,8 @@ VP8Source::~VP8Source() = default; string VP8Source::GetMediaDescription(const uint16_t port) { - char buf[100] = {0}; - sprintf(buf, "m=video %hu RTP/AVP 96", port); + char buf[100]; + snprintf(buf, sizeof(buf), "m=video %hu RTP/AVP 96", port); return buf; } diff --git a/rtsp_output_helper.cpp b/rtsp_output_helper.cpp index 852a598..f05449c 100644 --- a/rtsp_output_helper.cpp +++ b/rtsp_output_helper.cpp @@ -150,8 +150,9 @@ void RtspOutputHelper::CreateAudioEncoder() encoder = obs_get_encoder_by_name( "avc_aac_stream");*/ //OBS 26.0.2 Or Older /*if ((encoder = obs_get_encoder_by_name("adv_stream_audio")) == - nullptr) //OBS 30.0.0 Or Older*/ - encoder = obs_get_encoder_by_name("adv_stream_aac"); + nullptr) //OBS 30.0.0 Or Older + encoder = obs_get_encoder_by_name("adv_stream_aac");*/ + encoder = obs_get_encoder_by_name("adv_stream_audio"); } else encoder = obs_get_encoder_by_name("simple_aac");