Skip to content

Commit

Permalink
refactor: Eliminated compiler warning, replaced "sprintf" with "snpri…
Browse files Browse the repository at this point in the history
…ntf".

Signed-off-by: scottxu <[email protected]>
  • Loading branch information
iamscottxu committed Dec 24, 2023
1 parent e338223 commit 97cc270
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 102 deletions.
16 changes: 10 additions & 6 deletions rtsp-server/net/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions rtsp-server/xop/AACSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -76,14 +76,14 @@ string AACSource::GetAttribute() // RFC 3640
strlen(fmtp_fmt);
auto buf = vector<char>(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<uint8_t>((profile + 1) << 3 |
samplingFrequencyIndex >> 1),
static_cast<uint8_t>(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();
Expand Down
4 changes: 2 additions & 2 deletions rtsp-server/xop/G711ASource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
75 changes: 37 additions & 38 deletions rtsp-server/xop/H264Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> sps, vector<uint8_t> 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;
Expand All @@ -57,15 +56,15 @@ H264Source *H264Source::CreateNew(vector<uint8_t> extraData,
H264Source *H264Source::CreateNew(vector<uint8_t> sps, vector<uint8_t> 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;
}

Expand All @@ -89,7 +88,7 @@ string H264Source::GetAttribute()
pps_base64.length();
auto buf = vector<char>(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());
Expand Down Expand Up @@ -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<uint16_t>(size_count);
rtp_packet.last = 1;
Expand Down Expand Up @@ -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(
Expand All @@ -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 | |
Expand Down
95 changes: 47 additions & 48 deletions rtsp-server/xop/H265Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ H265Source::H265Source(vector<uint8_t> vps, vector<uint8_t> sps,
vector<uint8_t> pps, vector<uint8_t> 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;
Expand All @@ -58,7 +58,7 @@ H265Source *H265Source::CreateNew(vector<uint8_t> 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;
Expand All @@ -67,14 +67,14 @@ H265Source *H265Source::CreateNew(vector<uint8_t> vps, vector<uint8_t> sps,
vector<uint8_t> pps, vector<uint8_t> 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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -117,7 +117,7 @@ string H265Source::GetAttribute()
sps_base64.length() + sei.length();
auto buf = vector<char>(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';
Expand Down Expand Up @@ -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<uint16_t>(size_count);
rtp_packet.last = 1;
Expand Down Expand Up @@ -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(
Expand All @@ -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 | |
Expand Down Expand Up @@ -335,7 +335,6 @@ uint32_t H265Source::GetTimestamp()
return ts;
#else */
//auto time_point = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
//auto time_point = chrono::time_point_cast<chrono::milliseconds>(chrono::steady_clock::now());
const auto time_point = chrono::time_point_cast<chrono::microseconds>(
chrono::steady_clock::now());
return static_cast<uint32_t>(
Expand Down
4 changes: 2 additions & 2 deletions rtsp-server/xop/VP8Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions rtsp_output_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 97cc270

Please sign in to comment.