Use absl::string_view in SDP-related utilities

A step towards reduced copying in SDP parsing.

Bug: None
Change-Id: I3a5d89f483c1809929b7160b563c67b040c9df41
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/233080
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35122}
This commit is contained in:
Niels Möller 2021-09-29 13:23:01 +02:00 committed by WebRTC LUCI CQ
parent 8afd22f286
commit d4aa3a3196
5 changed files with 49 additions and 34 deletions

View File

@ -27,14 +27,14 @@ Candidate::Candidate()
network_cost_(0) {} network_cost_(0) {}
Candidate::Candidate(int component, Candidate::Candidate(int component,
const std::string& protocol, absl::string_view protocol,
const rtc::SocketAddress& address, const rtc::SocketAddress& address,
uint32_t priority, uint32_t priority,
const std::string& username, absl::string_view username,
const std::string& password, absl::string_view password,
const std::string& type, absl::string_view type,
uint32_t generation, uint32_t generation,
const std::string& foundation, absl::string_view foundation,
uint16_t network_id, uint16_t network_id,
uint16_t network_cost) uint16_t network_cost)
: id_(rtc::CreateRandomString(8)), : id_(rtc::CreateRandomString(8)),
@ -153,4 +153,11 @@ Candidate Candidate::ToSanitizedCopy(bool use_hostname_address,
return copy; 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 } // namespace cricket

View File

@ -17,6 +17,7 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include "absl/strings/string_view.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/network_constants.h" #include "rtc_base/network_constants.h"
#include "rtc_base/socket_address.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 // 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-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
Candidate(int component, Candidate(int component,
const std::string& protocol, absl::string_view protocol,
const rtc::SocketAddress& address, const rtc::SocketAddress& address,
uint32_t priority, uint32_t priority,
const std::string& username, absl::string_view username,
const std::string& password, absl::string_view password,
const std::string& type, absl::string_view type,
uint32_t generation, uint32_t generation,
const std::string& foundation, absl::string_view foundation,
uint16_t network_id = 0, uint16_t network_id = 0,
uint16_t network_cost = 0); uint16_t network_cost = 0);
Candidate(const Candidate&); Candidate(const Candidate&);
~Candidate(); ~Candidate();
const std::string& id() const { return id_; } 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_; } int component() const { return component_; }
void set_component(int component) { component_ = component; } void set_component(int component) { component_ = component; }
const std::string& protocol() const { return protocol_; } 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. // The protocol used to talk to relay.
const std::string& relay_protocol() const { return relay_protocol_; } const std::string& relay_protocol() const { return relay_protocol_; }
void set_relay_protocol(const std::string& protocol) { void set_relay_protocol(absl::string_view protocol) {
relay_protocol_ = protocol; Assign(relay_protocol_, protocol);
} }
const rtc::SocketAddress& address() const { return address_; } const rtc::SocketAddress& address() const { return address_; }
@ -90,17 +91,17 @@ class RTC_EXPORT Candidate {
// TODO(honghaiz): Change to usernameFragment or ufrag. // TODO(honghaiz): Change to usernameFragment or ufrag.
const std::string& username() const { return username_; } 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_; } 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_; } 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_; } const std::string& network_name() const { return network_name_; }
void set_network_name(const std::string& network_name) { void set_network_name(absl::string_view network_name) {
network_name_ = network_name; Assign(network_name_, network_name);
} }
rtc::AdapterType network_type() const { return network_type_; } 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; } void set_network_id(uint16_t network_id) { network_id_ = network_id; }
const std::string& foundation() const { return foundation_; } const std::string& foundation() const { return foundation_; }
void set_foundation(const std::string& foundation) { void set_foundation(absl::string_view foundation) {
foundation_ = foundation; Assign(foundation_, foundation);
} }
const rtc::SocketAddress& related_address() const { return related_address_; } const rtc::SocketAddress& related_address() const { return related_address_; }
@ -135,18 +136,18 @@ class RTC_EXPORT Candidate {
related_address_ = related_address; related_address_ = related_address;
} }
const std::string& tcptype() const { return tcptype_; } 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. // The name of the transport channel of this candidate.
// TODO(phoglund): remove. // TODO(phoglund): remove.
const std::string& transport_name() const { return transport_name_; } const std::string& transport_name() const { return transport_name_; }
void set_transport_name(const std::string& transport_name) { void set_transport_name(absl::string_view transport_name) {
transport_name_ = transport_name; Assign(transport_name_, transport_name);
} }
// The URL of the ICE server which this candidate is gathered from. // The URL of the ICE server which this candidate is gathered from.
const std::string& url() const { return url_; } 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. // Determines whether this candidate is equivalent to the given one.
bool IsEquivalent(const Candidate& c) const; bool IsEquivalent(const Candidate& c) const;
@ -177,6 +178,10 @@ class RTC_EXPORT Candidate {
bool filter_related_address) const; bool filter_related_address) const;
private: 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 ToStringInternal(bool sensitive) const;
std::string id_; std::string id_;

View File

@ -226,6 +226,7 @@ rtc_library("media_protocol_names") {
"media_protocol_names.cc", "media_protocol_names.cc",
"media_protocol_names.h", "media_protocol_names.h",
] ]
absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
} }
rtc_library("peerconnection") { rtc_library("peerconnection") {

View File

@ -13,6 +13,8 @@
#include <ctype.h> #include <ctype.h>
#include <stddef.h> #include <stddef.h>
#include <string>
namespace cricket { namespace cricket {
// There are multiple variants of the RTP protocol stack, including // 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 kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP"; const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
bool IsDtlsSctp(const std::string& protocol) { bool IsDtlsSctp(absl::string_view protocol) {
return protocol == kMediaProtocolDtlsSctp || return protocol == kMediaProtocolDtlsSctp ||
protocol == kMediaProtocolUdpDtlsSctp || protocol == kMediaProtocolUdpDtlsSctp ||
protocol == kMediaProtocolTcpDtlsSctp; protocol == kMediaProtocolTcpDtlsSctp;
} }
bool IsPlainSctp(const std::string& protocol) { bool IsPlainSctp(absl::string_view protocol) {
return protocol == kMediaProtocolSctp; return protocol == kMediaProtocolSctp;
} }
bool IsRtpProtocol(const std::string& protocol) { bool IsRtpProtocol(absl::string_view protocol) {
if (protocol.empty()) { if (protocol.empty()) {
return true; return true;
} }
@ -51,7 +53,7 @@ bool IsRtpProtocol(const std::string& protocol) {
return false; return false;
} }
bool IsSctpProtocol(const std::string& protocol) { bool IsSctpProtocol(absl::string_view protocol) {
return IsPlainSctp(protocol) || IsDtlsSctp(protocol); return IsPlainSctp(protocol) || IsDtlsSctp(protocol);
} }

View File

@ -11,7 +11,7 @@
#ifndef PC_MEDIA_PROTOCOL_NAMES_H_ #ifndef PC_MEDIA_PROTOCOL_NAMES_H_
#define PC_MEDIA_PROTOCOL_NAMES_H_ #define PC_MEDIA_PROTOCOL_NAMES_H_
#include <string> #include "absl/strings/string_view.h"
namespace cricket { namespace cricket {
@ -22,13 +22,13 @@ extern const char kMediaProtocolDtlsSctp[];
extern const char kMediaProtocolUdpDtlsSctp[]; extern const char kMediaProtocolUdpDtlsSctp[];
extern const char kMediaProtocolTcpDtlsSctp[]; extern const char kMediaProtocolTcpDtlsSctp[];
bool IsDtlsSctp(const std::string& protocol); bool IsDtlsSctp(absl::string_view protocol);
bool IsPlainSctp(const std::string& protocol); bool IsPlainSctp(absl::string_view protocol);
// Returns true if the given media section protocol indicates use of RTP. // 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. // 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 } // namespace cricket