Move string_format.h to webrtc namespace

Bug: webrtc:42232595
Change-Id: I208257358150eeb97304946929649414af5eb2ca
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/377542
Commit-Queue: Evan Shrubsole <eshr@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43915}
This commit is contained in:
Evan Shrubsole 2025-02-18 13:10:10 +00:00 committed by WebRTC LUCI CQ
parent e9aa803359
commit 418a8c2c83
13 changed files with 54 additions and 48 deletions

View File

@ -74,7 +74,7 @@ std::string VideoSendStream::Stats::ToString(int64_t time_ms) const {
char buf[2048]; char buf[2048];
rtc::SimpleStringBuilder ss(buf); rtc::SimpleStringBuilder ss(buf);
ss << "VideoSendStream stats: " << time_ms << ", {"; ss << "VideoSendStream stats: " << time_ms << ", {";
ss << "input_fps: " << rtc::StringFormat("%.1f", input_frame_rate) << ", "; ss << "input_fps: " << StringFormat("%.1f", input_frame_rate) << ", ";
ss << "encode_fps: " << encode_frame_rate << ", "; ss << "encode_fps: " << encode_frame_rate << ", ";
ss << "encode_ms: " << avg_encode_time_ms << ", "; ss << "encode_ms: " << avg_encode_time_ms << ", ";
ss << "encode_usage_perc: " << encode_usage_percent << ", "; ss << "encode_usage_perc: " << encode_usage_percent << ", ";

View File

@ -2452,17 +2452,17 @@ void WebRtcVoiceReceiveChannel::OnDemuxerCriteriaUpdateComplete() {}
bool WebRtcVoiceReceiveChannel::SetOutputVolume(uint32_t ssrc, double volume) { bool WebRtcVoiceReceiveChannel::SetOutputVolume(uint32_t ssrc, double volume) {
RTC_DCHECK_RUN_ON(worker_thread_); RTC_DCHECK_RUN_ON(worker_thread_);
RTC_LOG(LS_INFO) << rtc::StringFormat("WRVMC::%s({ssrc=%u}, {volume=%.2f})", RTC_LOG(LS_INFO) << webrtc::StringFormat(
__func__, ssrc, volume); "WRVMC::%s({ssrc=%u}, {volume=%.2f})", __func__, ssrc, volume);
const auto it = recv_streams_.find(ssrc); const auto it = recv_streams_.find(ssrc);
if (it == recv_streams_.end()) { if (it == recv_streams_.end()) {
RTC_LOG(LS_WARNING) << rtc::StringFormat( RTC_LOG(LS_WARNING) << webrtc::StringFormat(
"WRVMC::%s => (WARNING: no receive stream for SSRC %u)", __func__, "WRVMC::%s => (WARNING: no receive stream for SSRC %u)", __func__,
ssrc); ssrc);
return false; return false;
} }
it->second->SetOutputVolume(volume); it->second->SetOutputVolume(volume);
RTC_LOG(LS_INFO) << rtc::StringFormat( RTC_LOG(LS_INFO) << webrtc::StringFormat(
"WRVMC::%s => (stream with SSRC %u now uses volume %.2f)", __func__, ssrc, "WRVMC::%s => (stream with SSRC %u now uses volume %.2f)", __func__, ssrc,
volume); volume);
return true; return true;

View File

@ -80,7 +80,7 @@ void InitAckChunk::SerializeTo(std::vector<uint8_t>& out) const {
} }
std::string InitAckChunk::ToString() const { std::string InitAckChunk::ToString() const {
return rtc::StringFormat("INIT_ACK, initiate_tag=0x%0x, initial_tsn=%u", return webrtc::StringFormat("INIT_ACK, initiate_tag=0x%0x, initial_tsn=%u",
*initiate_tag(), *initial_tsn()); *initiate_tag(), *initial_tsn());
} }
} // namespace dcsctp } // namespace dcsctp

View File

@ -81,8 +81,8 @@ void InitChunk::SerializeTo(std::vector<uint8_t>& out) const {
} }
std::string InitChunk::ToString() const { std::string InitChunk::ToString() const {
return rtc::StringFormat("INIT, initiate_tag=0x%0x, initial_tsn=%u", return webrtc::StringFormat("INIT, initiate_tag=0x%0x, initial_tsn=%u",
*initiate_tag(), *initial_tsn()); *initiate_tag(), *initial_tsn());
} }
} // namespace dcsctp } // namespace dcsctp

View File

@ -139,7 +139,7 @@ std::optional<SctpPacket> SctpPacket::Parse(rtc::ArrayView<const uint8_t> data,
BoundedByteWriter<kHeaderSize>(data_copy).Store32<8>(0); BoundedByteWriter<kHeaderSize>(data_copy).Store32<8>(0);
uint32_t calculated_checksum = GenerateCrc32C(data_copy); uint32_t calculated_checksum = GenerateCrc32C(data_copy);
if (calculated_checksum != common_header.checksum) { if (calculated_checksum != common_header.checksum) {
RTC_DLOG(LS_WARNING) << rtc::StringFormat( RTC_DLOG(LS_WARNING) << webrtc::StringFormat(
"Invalid packet checksum, packet_checksum=0x%08x, " "Invalid packet checksum, packet_checksum=0x%08x, "
"calculated_checksum=0x%08x", "calculated_checksum=0x%08x",
common_header.checksum, calculated_checksum); common_header.checksum, calculated_checksum);

View File

@ -316,7 +316,7 @@ void DcSctpSocket::Connect() {
callbacks_.GetRandomInt(kMinVerificationTag, kMaxVerificationTag)); callbacks_.GetRandomInt(kMinVerificationTag, kMaxVerificationTag));
RTC_DLOG(LS_INFO) RTC_DLOG(LS_INFO)
<< log_prefix() << log_prefix()
<< rtc::StringFormat( << webrtc::StringFormat(
"Connecting. my_verification_tag=%08x, my_initial_tsn=%u", "Connecting. my_verification_tag=%08x, my_initial_tsn=%u",
*connect_params_.verification_tag, *connect_params_.initial_tsn); *connect_params_.verification_tag, *connect_params_.initial_tsn);
SendInit(); SendInit();
@ -715,7 +715,7 @@ bool DcSctpSocket::ValidatePacket(const SctpPacket& packet) {
} }
callbacks_.OnError( callbacks_.OnError(
ErrorKind::kParseFailed, ErrorKind::kParseFailed,
rtc::StringFormat( webrtc::StringFormat(
"Packet has invalid verification tag: %08x, expected %08x", "Packet has invalid verification tag: %08x, expected %08x",
*header.verification_tag, *connect_params_.verification_tag)); *header.verification_tag, *connect_params_.verification_tag));
return false; return false;
@ -760,7 +760,7 @@ bool DcSctpSocket::ValidatePacket(const SctpPacket& packet) {
callbacks_.OnError( callbacks_.OnError(
ErrorKind::kParseFailed, ErrorKind::kParseFailed,
rtc::StringFormat( webrtc::StringFormat(
"Packet has invalid verification tag: %08x, expected %08x", "Packet has invalid verification tag: %08x, expected %08x",
*header.verification_tag, *my_verification_tag)); *header.verification_tag, *my_verification_tag));
return false; return false;
@ -1215,7 +1215,7 @@ void DcSctpSocket::HandleInit(const CommonHeader& /* header */,
RTC_DLOG(LS_VERBOSE) RTC_DLOG(LS_VERBOSE)
<< log_prefix() << log_prefix()
<< rtc::StringFormat( << webrtc::StringFormat(
"Proceeding with connection. my_verification_tag=%08x, " "Proceeding with connection. my_verification_tag=%08x, "
"my_initial_tsn=%u, peer_verification_tag=%08x, " "my_initial_tsn=%u, peer_verification_tag=%08x, "
"peer_initial_tsn=%u", "peer_initial_tsn=%u",
@ -1327,7 +1327,7 @@ void DcSctpSocket::HandleCookieEcho(
if (header.verification_tag != cookie->my_tag()) { if (header.verification_tag != cookie->my_tag()) {
callbacks_.OnError( callbacks_.OnError(
ErrorKind::kParseFailed, ErrorKind::kParseFailed,
rtc::StringFormat( webrtc::StringFormat(
"Received CookieEcho with invalid verification tag: %08x, " "Received CookieEcho with invalid verification tag: %08x, "
"expected %08x", "expected %08x",
*header.verification_tag, *cookie->my_tag())); *header.verification_tag, *cookie->my_tag()));

View File

@ -167,7 +167,8 @@ class SctpActor : public DcSctpSocketCallbacks {
double bitrate_mbps = double bitrate_mbps =
static_cast<double>(received_bytes_ * 8) / duration.ms() / 1000; static_cast<double>(received_bytes_ * 8) / duration.ms() / 1000;
RTC_LOG(LS_INFO) << log_prefix() RTC_LOG(LS_INFO) << log_prefix()
<< rtc::StringFormat("Received %0.2f Mbps", bitrate_mbps); << webrtc::StringFormat("Received %0.2f Mbps",
bitrate_mbps);
received_bitrate_mbps_.push_back(bitrate_mbps); received_bitrate_mbps_.push_back(bitrate_mbps);
received_bytes_ = 0; received_bytes_ = 0;

View File

@ -170,7 +170,7 @@ BaseChannel::~BaseChannel() {
} }
std::string BaseChannel::ToString() const { std::string BaseChannel::ToString() const {
return StringFormat( return webrtc::StringFormat(
"{mid: %s, media_type: %s}", mid().c_str(), "{mid: %s, media_type: %s}", mid().c_str(),
MediaTypeToString(media_send_channel_->media_type()).c_str()); MediaTypeToString(media_send_channel_->media_type()).c_str());
} }
@ -508,9 +508,9 @@ bool BaseChannel::MaybeUpdateDemuxerAndRtpExtensions_w(
return true; return true;
if (!rtp_transport_->RegisterRtpDemuxerSink(demuxer_criteria_, this)) { if (!rtp_transport_->RegisterRtpDemuxerSink(demuxer_criteria_, this)) {
error_desc = error_desc = webrtc::StringFormat(
StringFormat("Failed to apply demuxer criteria for '%s': '%s'.", "Failed to apply demuxer criteria for '%s': '%s'.", mid().c_str(),
mid().c_str(), demuxer_criteria_.ToString().c_str()); demuxer_criteria_.ToString().c_str());
return false; return false;
} }
return true; return true;
@ -666,7 +666,7 @@ bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
continue; continue;
} }
if (!media_send_channel()->RemoveSendStream(old_stream.first_ssrc())) { if (!media_send_channel()->RemoveSendStream(old_stream.first_ssrc())) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to remove send stream with ssrc %u from m-section with " "Failed to remove send stream with ssrc %u from m-section with "
"mid='%s'.", "mid='%s'.",
old_stream.first_ssrc(), mid().c_str()); old_stream.first_ssrc(), mid().c_str());
@ -692,7 +692,7 @@ bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
RTC_DCHECK(new_stream.has_ssrcs() || new_stream.has_rids()); RTC_DCHECK(new_stream.has_ssrcs() || new_stream.has_rids());
if (new_stream.has_ssrcs() && new_stream.has_rids()) { if (new_stream.has_ssrcs() && new_stream.has_rids()) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to add send stream: %u into m-section with mid='%s'. Stream " "Failed to add send stream: %u into m-section with mid='%s'. Stream "
"has both SSRCs and RIDs.", "has both SSRCs and RIDs.",
new_stream.first_ssrc(), mid().c_str()); new_stream.first_ssrc(), mid().c_str());
@ -712,7 +712,7 @@ bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
RTC_LOG(LS_INFO) << "Add send stream ssrc: " << new_stream.ssrcs[0] RTC_LOG(LS_INFO) << "Add send stream ssrc: " << new_stream.ssrcs[0]
<< " into " << ToString(); << " into " << ToString();
} else { } else {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to add send stream ssrc: %u into m-section with mid='%s'", "Failed to add send stream ssrc: %u into m-section with mid='%s'",
new_stream.first_ssrc(), mid().c_str()); new_stream.first_ssrc(), mid().c_str());
ret = false; ret = false;
@ -754,7 +754,7 @@ bool BaseChannel::UpdateRemoteStreams_w(const MediaContentDescription* content,
RTC_LOG(LS_INFO) << "Remove remote ssrc: " << old_stream.first_ssrc() RTC_LOG(LS_INFO) << "Remove remote ssrc: " << old_stream.first_ssrc()
<< " from " << ToString() << "."; << " from " << ToString() << ".";
} else { } else {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to remove remote stream with ssrc %u from m-section with " "Failed to remove remote stream with ssrc %u from m-section with "
"mid='%s'.", "mid='%s'.",
old_stream.first_ssrc(), mid().c_str()); old_stream.first_ssrc(), mid().c_str());
@ -778,12 +778,12 @@ bool BaseChannel::UpdateRemoteStreams_w(const MediaContentDescription* content,
: "unsignaled") : "unsignaled")
<< " to " << ToString(); << " to " << ToString();
} else { } else {
error_desc = error_desc = webrtc::StringFormat(
StringFormat("Failed to add remote stream ssrc: %s to %s", "Failed to add remote stream ssrc: %s to %s",
new_stream.has_ssrcs() new_stream.has_ssrcs()
? std::to_string(new_stream.first_ssrc()).c_str() ? std::to_string(new_stream.first_ssrc()).c_str()
: "unsignaled", : "unsignaled",
ToString().c_str()); ToString().c_str());
return false; return false;
} }
} }
@ -800,8 +800,8 @@ bool BaseChannel::UpdateRemoteStreams_w(const MediaContentDescription* content,
// Re-register the sink to update after changing the demuxer criteria. // Re-register the sink to update after changing the demuxer criteria.
if (needs_re_registration && !RegisterRtpDemuxerSink_w()) { if (needs_re_registration && !RegisterRtpDemuxerSink_w()) {
error_desc = StringFormat("Failed to set up audio demuxing for mid='%s'.", error_desc = webrtc::StringFormat(
mid().c_str()); "Failed to set up audio demuxing for mid='%s'.", mid().c_str());
return false; return false;
} }
@ -910,7 +910,7 @@ bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
recv_params.mid = mid(); recv_params.mid = mid();
if (!media_receive_channel()->SetReceiverParameters(recv_params)) { if (!media_receive_channel()->SetReceiverParameters(recv_params)) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to set local audio description recv parameters for m-section " "Failed to set local audio description recv parameters for m-section "
"with mid='%s'.", "with mid='%s'.",
mid().c_str()); mid().c_str());
@ -966,7 +966,7 @@ bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
bool parameters_applied = bool parameters_applied =
media_send_channel()->SetSenderParameters(send_params); media_send_channel()->SetSenderParameters(send_params);
if (!parameters_applied) { if (!parameters_applied) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to set remote audio description send parameters for m-section " "Failed to set remote audio description send parameters for m-section "
"with mid='%s'.", "with mid='%s'.",
mid().c_str()); mid().c_str());
@ -1097,7 +1097,7 @@ bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
send_codec.packetization = std::nullopt; send_codec.packetization = std::nullopt;
needs_send_params_update = true; needs_send_params_update = true;
} else if (!has_matching_packetization) { } else if (!has_matching_packetization) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to set local answer due to incompatible codec " "Failed to set local answer due to incompatible codec "
"packetization for pt='%d' specified in m-section with mid='%s'.", "packetization for pt='%d' specified in m-section with mid='%s'.",
send_codec.id, mid().c_str()); send_codec.id, mid().c_str());
@ -1111,7 +1111,7 @@ bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
} }
if (!media_receive_channel()->SetReceiverParameters(recv_params)) { if (!media_receive_channel()->SetReceiverParameters(recv_params)) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to set local video description recv parameters for m-section " "Failed to set local video description recv parameters for m-section "
"with mid='%s'.", "with mid='%s'.",
mid().c_str()); mid().c_str());
@ -1130,7 +1130,7 @@ bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
if (needs_send_params_update) { if (needs_send_params_update) {
if (!media_send_channel()->SetSenderParameters(send_params)) { if (!media_send_channel()->SetSenderParameters(send_params)) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to set send parameters for m-section with mid='%s'.", "Failed to set send parameters for m-section with mid='%s'.",
mid().c_str()); mid().c_str());
return false; return false;
@ -1211,7 +1211,7 @@ bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
recv_codec.packetization = std::nullopt; recv_codec.packetization = std::nullopt;
needs_recv_params_update = true; needs_recv_params_update = true;
} else if (!has_matching_packetization) { } else if (!has_matching_packetization) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to set remote answer due to incompatible codec " "Failed to set remote answer due to incompatible codec "
"packetization for pt='%d' specified in m-section with mid='%s'.", "packetization for pt='%d' specified in m-section with mid='%s'.",
recv_codec.id, mid().c_str()); recv_codec.id, mid().c_str());
@ -1225,7 +1225,7 @@ bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
} }
if (!media_send_channel()->SetSenderParameters(send_params)) { if (!media_send_channel()->SetSenderParameters(send_params)) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to set remote video description send parameters for m-section " "Failed to set remote video description send parameters for m-section "
"with mid='%s'.", "with mid='%s'.",
mid().c_str()); mid().c_str());
@ -1241,7 +1241,7 @@ bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
if (needs_recv_params_update) { if (needs_recv_params_update) {
if (!media_receive_channel()->SetReceiverParameters(recv_params)) { if (!media_receive_channel()->SetReceiverParameters(recv_params)) {
error_desc = StringFormat( error_desc = webrtc::StringFormat(
"Failed to set recv parameters for m-section with mid='%s'.", "Failed to set recv parameters for m-section with mid='%s'.",
mid().c_str()); mid().c_str());
return false; return false;

View File

@ -115,8 +115,7 @@ bool RemoteAudioSource::remote() const {
void RemoteAudioSource::SetVolume(double volume) { void RemoteAudioSource::SetVolume(double volume) {
RTC_DCHECK_GE(volume, 0); RTC_DCHECK_GE(volume, 0);
RTC_DCHECK_LE(volume, 10); RTC_DCHECK_LE(volume, 10);
RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__, RTC_LOG(LS_INFO) << StringFormat("RAS::%s({volume=%.2f})", __func__, volume);
volume);
for (auto* observer : audio_observers_) { for (auto* observer : audio_observers_) {
observer->OnSetVolume(volume); observer->OnSetVolume(volume);
} }

View File

@ -277,7 +277,7 @@ TEST(ToString, SanityCheck) {
EXPECT_EQ(ToString((unsigned long long int)123), "123"); EXPECT_EQ(ToString((unsigned long long int)123), "123");
EXPECT_EQ(ToString(0.5), "0.5"); EXPECT_EQ(ToString(0.5), "0.5");
int i = 10; int i = 10;
EXPECT_EQ(StringFormat("%p", &i), ToString(&i)); EXPECT_EQ(webrtc::StringFormat("%p", &i), ToString(&i));
} }
template <typename T> template <typename T>

View File

@ -14,7 +14,7 @@
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
namespace rtc { namespace webrtc {
namespace { namespace {
@ -38,4 +38,4 @@ std::string StringFormat(const char* fmt, ...) {
return std::string(buffer); return std::string(buffer);
} }
} // namespace rtc } // namespace webrtc

View File

@ -13,7 +13,7 @@
#include <string> #include <string>
namespace rtc { namespace webrtc {
#if defined(__GNUC__) #if defined(__GNUC__)
#define RTC_PRINTF_FORMAT(format_param, dots_param) \ #define RTC_PRINTF_FORMAT(format_param, dots_param) \
@ -26,6 +26,12 @@ namespace rtc {
// Based on base::StringPrintf() in Chrome but without its fancy dynamic memory // Based on base::StringPrintf() in Chrome but without its fancy dynamic memory
// allocation for any size of the input buffer. // allocation for any size of the input buffer.
std::string StringFormat(const char* fmt, ...) RTC_PRINTF_FORMAT(1, 2); std::string StringFormat(const char* fmt, ...) RTC_PRINTF_FORMAT(1, 2);
} // namespace webrtc
// Re-export symbols from the webrtc namespace for backwards compatibility.
// TODO(bugs.webrtc.org/4222596): Remove once all references are updated.
namespace rtc {
using ::webrtc::StringFormat;
} // namespace rtc } // namespace rtc
#endif // RTC_BASE_STRINGS_STRING_FORMAT_H_ #endif // RTC_BASE_STRINGS_STRING_FORMAT_H_

View File

@ -17,7 +17,7 @@
#include "rtc_base/string_encode.h" #include "rtc_base/string_encode.h"
#include "test/gtest.h" #include "test/gtest.h"
namespace rtc { namespace webrtc {
TEST(StringFormatTest, Empty) { TEST(StringFormatTest, Empty) {
EXPECT_EQ("", StringFormat("%s", "")); EXPECT_EQ("", StringFormat("%s", ""));
@ -49,4 +49,4 @@ TEST(StringFormatTest, FormatStringView) {
EXPECT_EQ(formatted.compare("We have a substring."), 0); EXPECT_EQ(formatted.compare("We have a substring."), 0);
} }
} // namespace rtc } // namespace webrtc