diff --git a/api/candidate.cc b/api/candidate.cc index ad65121bde..2da2468539 100644 --- a/api/candidate.cc +++ b/api/candidate.cc @@ -27,14 +27,14 @@ Candidate::Candidate() network_cost_(0) {} Candidate::Candidate(int component, - const std::string& protocol, + absl::string_view protocol, const rtc::SocketAddress& address, uint32_t priority, - const std::string& username, - const std::string& password, - const std::string& type, + absl::string_view username, + absl::string_view password, + absl::string_view type, uint32_t generation, - const std::string& foundation, + absl::string_view foundation, uint16_t network_id, uint16_t network_cost) : id_(rtc::CreateRandomString(8)), @@ -153,4 +153,11 @@ Candidate Candidate::ToSanitizedCopy(bool use_hostname_address, return copy; } +void Candidate::Assign(std::string& s, absl::string_view view) { + // Assigning via a temporary object, like s = std::string(view), results in + // binary size bloat. To avoid that, extract pointer and size from the + // string view, and use std::string::assign method. + s.assign(view.data(), view.size()); +} + } // namespace cricket diff --git a/api/candidate.h b/api/candidate.h index c9447bbf6c..ecfdee3fcb 100644 --- a/api/candidate.h +++ b/api/candidate.h @@ -17,6 +17,7 @@ #include #include +#include "absl/strings/string_view.h" #include "rtc_base/checks.h" #include "rtc_base/network_constants.h" #include "rtc_base/socket_address.h" @@ -33,32 +34,32 @@ class RTC_EXPORT Candidate { // TODO(pthatcher): Match the ordering and param list as per RFC 5245 // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1 Candidate(int component, - const std::string& protocol, + absl::string_view protocol, const rtc::SocketAddress& address, uint32_t priority, - const std::string& username, - const std::string& password, - const std::string& type, + absl::string_view username, + absl::string_view password, + absl::string_view type, uint32_t generation, - const std::string& foundation, + absl::string_view foundation, uint16_t network_id = 0, uint16_t network_cost = 0); Candidate(const Candidate&); ~Candidate(); const std::string& id() const { return id_; } - void set_id(const std::string& id) { id_ = id; } + void set_id(absl::string_view id) { Assign(id_, id); } int component() const { return component_; } void set_component(int component) { component_ = component; } const std::string& protocol() const { return protocol_; } - void set_protocol(const std::string& protocol) { protocol_ = protocol; } + void set_protocol(absl::string_view protocol) { Assign(protocol_, protocol); } // The protocol used to talk to relay. const std::string& relay_protocol() const { return relay_protocol_; } - void set_relay_protocol(const std::string& protocol) { - relay_protocol_ = protocol; + void set_relay_protocol(absl::string_view protocol) { + Assign(relay_protocol_, protocol); } const rtc::SocketAddress& address() const { return address_; } @@ -90,17 +91,17 @@ class RTC_EXPORT Candidate { // TODO(honghaiz): Change to usernameFragment or ufrag. const std::string& username() const { return username_; } - void set_username(const std::string& username) { username_ = username; } + void set_username(absl::string_view username) { Assign(username_, username); } const std::string& password() const { return password_; } - void set_password(const std::string& password) { password_ = password; } + void set_password(absl::string_view password) { Assign(password_, password); } const std::string& type() const { return type_; } - void set_type(const std::string& type) { type_ = type; } + void set_type(absl::string_view type) { Assign(type_, type); } const std::string& network_name() const { return network_name_; } - void set_network_name(const std::string& network_name) { - network_name_ = network_name; + void set_network_name(absl::string_view network_name) { + Assign(network_name_, network_name); } rtc::AdapterType network_type() const { return network_type_; } @@ -126,8 +127,8 @@ class RTC_EXPORT Candidate { void set_network_id(uint16_t network_id) { network_id_ = network_id; } const std::string& foundation() const { return foundation_; } - void set_foundation(const std::string& foundation) { - foundation_ = foundation; + void set_foundation(absl::string_view foundation) { + Assign(foundation_, foundation); } const rtc::SocketAddress& related_address() const { return related_address_; } @@ -135,18 +136,18 @@ class RTC_EXPORT Candidate { related_address_ = related_address; } const std::string& tcptype() const { return tcptype_; } - void set_tcptype(const std::string& tcptype) { tcptype_ = tcptype; } + void set_tcptype(absl::string_view tcptype) { Assign(tcptype_, tcptype); } // The name of the transport channel of this candidate. // TODO(phoglund): remove. const std::string& transport_name() const { return transport_name_; } - void set_transport_name(const std::string& transport_name) { - transport_name_ = transport_name; + void set_transport_name(absl::string_view transport_name) { + Assign(transport_name_, transport_name); } // The URL of the ICE server which this candidate is gathered from. const std::string& url() const { return url_; } - void set_url(const std::string& url) { url_ = url; } + void set_url(absl::string_view url) { Assign(url_, url); } // Determines whether this candidate is equivalent to the given one. bool IsEquivalent(const Candidate& c) const; @@ -177,6 +178,10 @@ class RTC_EXPORT Candidate { bool filter_related_address) const; private: + // TODO(bugs.webrtc.org/13220): With C++17, we get a std::string assignment + // operator accepting any object implicitly convertible to std::string_view, + // and then we don't need this workaround. + static void Assign(std::string& s, absl::string_view view); std::string ToStringInternal(bool sensitive) const; std::string id_; diff --git a/pc/BUILD.gn b/pc/BUILD.gn index 7a7b18fc06..f31109af33 100644 --- a/pc/BUILD.gn +++ b/pc/BUILD.gn @@ -226,6 +226,7 @@ rtc_library("media_protocol_names") { "media_protocol_names.cc", "media_protocol_names.h", ] + absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] } rtc_library("peerconnection") { diff --git a/pc/media_protocol_names.cc b/pc/media_protocol_names.cc index ae4fcf3391..0b091ee101 100644 --- a/pc/media_protocol_names.cc +++ b/pc/media_protocol_names.cc @@ -13,6 +13,8 @@ #include #include +#include + namespace cricket { // There are multiple variants of the RTP protocol stack, including @@ -26,17 +28,17 @@ const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP"; const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP"; const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP"; -bool IsDtlsSctp(const std::string& protocol) { +bool IsDtlsSctp(absl::string_view protocol) { return protocol == kMediaProtocolDtlsSctp || protocol == kMediaProtocolUdpDtlsSctp || protocol == kMediaProtocolTcpDtlsSctp; } -bool IsPlainSctp(const std::string& protocol) { +bool IsPlainSctp(absl::string_view protocol) { return protocol == kMediaProtocolSctp; } -bool IsRtpProtocol(const std::string& protocol) { +bool IsRtpProtocol(absl::string_view protocol) { if (protocol.empty()) { return true; } @@ -51,7 +53,7 @@ bool IsRtpProtocol(const std::string& protocol) { return false; } -bool IsSctpProtocol(const std::string& protocol) { +bool IsSctpProtocol(absl::string_view protocol) { return IsPlainSctp(protocol) || IsDtlsSctp(protocol); } diff --git a/pc/media_protocol_names.h b/pc/media_protocol_names.h index 88f1c4659d..048cafa324 100644 --- a/pc/media_protocol_names.h +++ b/pc/media_protocol_names.h @@ -11,7 +11,7 @@ #ifndef PC_MEDIA_PROTOCOL_NAMES_H_ #define PC_MEDIA_PROTOCOL_NAMES_H_ -#include +#include "absl/strings/string_view.h" namespace cricket { @@ -22,13 +22,13 @@ extern const char kMediaProtocolDtlsSctp[]; extern const char kMediaProtocolUdpDtlsSctp[]; extern const char kMediaProtocolTcpDtlsSctp[]; -bool IsDtlsSctp(const std::string& protocol); -bool IsPlainSctp(const std::string& protocol); +bool IsDtlsSctp(absl::string_view protocol); +bool IsPlainSctp(absl::string_view protocol); // Returns true if the given media section protocol indicates use of RTP. -bool IsRtpProtocol(const std::string& protocol); +bool IsRtpProtocol(absl::string_view protocol); // Returns true if the given media section protocol indicates use of SCTP. -bool IsSctpProtocol(const std::string& protocol); +bool IsSctpProtocol(absl::string_view protocol); } // namespace cricket