Use C++11 for range loop in pc/mediasession.cc (and test)

Bug: webrtc:9732
Change-Id: I1fad3313c5ad627f7eca52ea907608d67af6891f
Reviewed-on: https://webrtc-review.googlesource.com/98924
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Seth Hampson <shampson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24664}
This commit is contained in:
Steve Anton
2018-09-10 12:57:37 -07:00
committed by Commit Bot
parent f2582da04e
commit 3a66edf3c3
2 changed files with 91 additions and 100 deletions

View File

@ -122,7 +122,7 @@ static bool IsMediaContentOfType(const ContentInfo* content,
static bool CreateCryptoParams(int tag, static bool CreateCryptoParams(int tag,
const std::string& cipher, const std::string& cipher,
CryptoParams* out) { CryptoParams* crypto_out) {
int key_len; int key_len;
int salt_len; int salt_len;
if (!rtc::GetSrtpKeyAndSaltLengths(rtc::SrtpCryptoSuiteFromName(cipher), if (!rtc::GetSrtpKeyAndSaltLengths(rtc::SrtpCryptoSuiteFromName(cipher),
@ -139,35 +139,33 @@ static bool CreateCryptoParams(int tag,
RTC_CHECK_EQ(master_key_len, master_key.size()); RTC_CHECK_EQ(master_key_len, master_key.size());
std::string key = rtc::Base64::Encode(master_key); std::string key = rtc::Base64::Encode(master_key);
out->tag = tag; crypto_out->tag = tag;
out->cipher_suite = cipher; crypto_out->cipher_suite = cipher;
out->key_params = kInline; crypto_out->key_params = kInline;
out->key_params += key; crypto_out->key_params += key;
return true; return true;
} }
static bool AddCryptoParams(const std::string& cipher_suite, static bool AddCryptoParams(const std::string& cipher_suite,
CryptoParamsVec* out) { CryptoParamsVec* cryptos_out) {
int size = static_cast<int>(out->size()); int size = static_cast<int>(cryptos_out->size());
out->resize(size + 1); cryptos_out->resize(size + 1);
return CreateCryptoParams(size, cipher_suite, &out->at(size)); return CreateCryptoParams(size, cipher_suite, &cryptos_out->at(size));
} }
void AddMediaCryptos(const CryptoParamsVec& cryptos, void AddMediaCryptos(const CryptoParamsVec& cryptos,
MediaContentDescription* media) { MediaContentDescription* media) {
for (CryptoParamsVec::const_iterator crypto = cryptos.begin(); for (const CryptoParams& crypto : cryptos) {
crypto != cryptos.end(); ++crypto) { media->AddCrypto(crypto);
media->AddCrypto(*crypto);
} }
} }
bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites, bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
MediaContentDescription* media) { MediaContentDescription* media) {
CryptoParamsVec cryptos; CryptoParamsVec cryptos;
for (std::vector<std::string>::const_iterator it = crypto_suites.begin(); for (const std::string& crypto_suite : crypto_suites) {
it != crypto_suites.end(); ++it) { if (!AddCryptoParams(crypto_suite, &cryptos)) {
if (!AddCryptoParams(*it, &cryptos)) {
return false; return false;
} }
} }
@ -184,15 +182,15 @@ const CryptoParamsVec* GetCryptos(const ContentInfo* content) {
bool FindMatchingCrypto(const CryptoParamsVec& cryptos, bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
const CryptoParams& crypto, const CryptoParams& crypto,
CryptoParams* out) { CryptoParams* crypto_out) {
for (CryptoParamsVec::const_iterator it = cryptos.begin(); auto it = std::find_if(
it != cryptos.end(); ++it) { cryptos.begin(), cryptos.end(),
if (crypto.Matches(*it)) { [&crypto](const CryptoParams& c) { return crypto.Matches(c); });
*out = *it; if (it == cryptos.end()) {
return true; return false;
}
} }
return false; *crypto_out = *it;
return true;
} }
// For audio, HMAC 32 (if enabled) is prefered over HMAC 80 because of the // For audio, HMAC 32 (if enabled) is prefered over HMAC 80 because of the
@ -255,18 +253,17 @@ void GetSupportedDataSdesCryptoSuiteNames(
static bool SelectCrypto(const MediaContentDescription* offer, static bool SelectCrypto(const MediaContentDescription* offer,
bool bundle, bool bundle,
const rtc::CryptoOptions& crypto_options, const rtc::CryptoOptions& crypto_options,
CryptoParams* crypto) { CryptoParams* crypto_out) {
bool audio = offer->type() == MEDIA_TYPE_AUDIO; bool audio = offer->type() == MEDIA_TYPE_AUDIO;
const CryptoParamsVec& cryptos = offer->cryptos(); const CryptoParamsVec& cryptos = offer->cryptos();
for (CryptoParamsVec::const_iterator i = cryptos.begin(); i != cryptos.end(); for (const CryptoParams& crypto : cryptos) {
++i) {
if ((crypto_options.enable_gcm_crypto_suites && if ((crypto_options.enable_gcm_crypto_suites &&
rtc::IsGcmCryptoSuiteName(i->cipher_suite)) || rtc::IsGcmCryptoSuiteName(crypto.cipher_suite)) ||
rtc::CS_AES_CM_128_HMAC_SHA1_80 == i->cipher_suite || rtc::CS_AES_CM_128_HMAC_SHA1_80 == crypto.cipher_suite ||
(rtc::CS_AES_CM_128_HMAC_SHA1_32 == i->cipher_suite && audio && (rtc::CS_AES_CM_128_HMAC_SHA1_32 == crypto.cipher_suite && audio &&
!bundle && crypto_options.enable_aes128_sha1_32_crypto_cipher)) { !bundle && crypto_options.enable_aes128_sha1_32_crypto_cipher)) {
return CreateCryptoParams(i->tag, i->cipher_suite, crypto); return CreateCryptoParams(crypto.tag, crypto.cipher_suite, crypto_out);
} }
} }
return false; return false;
@ -310,14 +307,11 @@ void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
// Filter RTP codec for SCTP and vice versa. // Filter RTP codec for SCTP and vice versa.
const char* codec_name = const char* codec_name =
sctp ? kGoogleRtpDataCodecName : kGoogleSctpDataCodecName; sctp ? kGoogleRtpDataCodecName : kGoogleSctpDataCodecName;
for (std::vector<DataCodec>::iterator iter = codecs->begin(); codecs->erase(std::remove_if(codecs->begin(), codecs->end(),
iter != codecs->end();) { [&codec_name](const DataCodec& codec) {
if (CodecNamesEq(iter->name, codec_name)) { return CodecNamesEq(codec.name, codec_name);
iter = codecs->erase(iter); }),
} else { codecs->end());
++iter;
}
}
} }
template <typename IdStruct> template <typename IdStruct>
@ -334,9 +328,8 @@ class UsedIds {
// Note that typename Id must be a type of IdStruct. // Note that typename Id must be a type of IdStruct.
template <typename Id> template <typename Id>
void FindAndSetIdUsed(std::vector<Id>* ids) { void FindAndSetIdUsed(std::vector<Id>* ids) {
for (typename std::vector<Id>::iterator it = ids->begin(); it != ids->end(); for (const Id& id : *ids) {
++it) { FindAndSetIdUsed(&id);
FindAndSetIdUsed(&*it);
} }
} }
@ -512,13 +505,12 @@ static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
selected_transport_info->description.ice_pwd; selected_transport_info->description.ice_pwd;
ConnectionRole selected_connection_role = ConnectionRole selected_connection_role =
selected_transport_info->description.connection_role; selected_transport_info->description.connection_role;
for (TransportInfos::iterator it = sdesc->transport_infos().begin(); for (TransportInfo& transport_info : sdesc->transport_infos()) {
it != sdesc->transport_infos().end(); ++it) { if (bundle_group.HasContentName(transport_info.content_name) &&
if (bundle_group.HasContentName(it->content_name) && transport_info.content_name != selected_content_name) {
it->content_name != selected_content_name) { transport_info.description.ice_ufrag = selected_ufrag;
it->description.ice_ufrag = selected_ufrag; transport_info.description.ice_pwd = selected_pwd;
it->description.ice_pwd = selected_pwd; transport_info.description.connection_role = selected_connection_role;
it->description.connection_role = selected_connection_role;
} }
} }
return true; return true;
@ -592,19 +584,20 @@ static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
// Get the common cryptos. // Get the common cryptos.
const ContentNames& content_names = bundle_group.content_names(); const ContentNames& content_names = bundle_group.content_names();
CryptoParamsVec common_cryptos; CryptoParamsVec common_cryptos;
for (ContentNames::const_iterator it = content_names.begin(); bool first = true;
it != content_names.end(); ++it) { for (const std::string& content_name : content_names) {
if (!IsRtpContent(sdesc, *it)) { if (!IsRtpContent(sdesc, content_name)) {
continue; continue;
} }
// The common cryptos are needed if any of the content does not have DTLS // The common cryptos are needed if any of the content does not have DTLS
// enabled. // enabled.
if (!sdesc->GetTransportInfoByName(*it)->description.secure()) { if (!sdesc->GetTransportInfoByName(content_name)->description.secure()) {
common_cryptos_needed = true; common_cryptos_needed = true;
} }
if (it == content_names.begin()) { if (first) {
first = false;
// Initial the common_cryptos with the first content in the bundle group. // Initial the common_cryptos with the first content in the bundle group.
if (!GetCryptosByName(sdesc, *it, &common_cryptos)) { if (!GetCryptosByName(sdesc, content_name, &common_cryptos)) {
return false; return false;
} }
if (common_cryptos.empty()) { if (common_cryptos.empty()) {
@ -613,7 +606,7 @@ static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
} }
} else { } else {
CryptoParamsVec cryptos; CryptoParamsVec cryptos;
if (!GetCryptosByName(sdesc, *it, &cryptos)) { if (!GetCryptosByName(sdesc, content_name, &cryptos)) {
return false; return false;
} }
PruneCryptos(cryptos, &common_cryptos); PruneCryptos(cryptos, &common_cryptos);
@ -625,12 +618,11 @@ static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
} }
// Update to use the common cryptos. // Update to use the common cryptos.
for (ContentNames::const_iterator it = content_names.begin(); for (const std::string& content_name : content_names) {
it != content_names.end(); ++it) { if (!IsRtpContent(sdesc, content_name)) {
if (!IsRtpContent(sdesc, *it)) {
continue; continue;
} }
ContentInfo* content = sdesc->GetContentByName(*it); ContentInfo* content = sdesc->GetContentByName(content_name);
if (IsMediaContent(content)) { if (IsMediaContent(content)) {
MediaContentDescription* media_desc = content->media_description(); MediaContentDescription* media_desc = content->media_description();
if (!media_desc) { if (!media_desc) {
@ -892,17 +884,21 @@ static void MergeCodecs(const std::vector<C>& reference_codecs,
static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions, static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions,
const webrtc::RtpExtension& ext_to_match, const webrtc::RtpExtension& ext_to_match,
webrtc::RtpExtension* found_extension) { webrtc::RtpExtension* found_extension) {
for (RtpHeaderExtensions::const_iterator it = extensions.begin(); auto it =
it != extensions.end(); ++it) { std::find_if(extensions.begin(), extensions.end(),
// We assume that all URIs are given in a canonical format. [&ext_to_match](const webrtc::RtpExtension& extension) {
if (it->uri == ext_to_match.uri && it->encrypt == ext_to_match.encrypt) { // We assume that all URIs are given in a canonical
if (found_extension) { // format.
*found_extension = *it; return extension.uri == ext_to_match.uri &&
} extension.encrypt == ext_to_match.encrypt;
return true; });
} if (it == extensions.end()) {
return false;
} }
return false; if (found_extension) {
*found_extension = *it;
}
return true;
} }
static bool FindByUri(const RtpHeaderExtensions& extensions, static bool FindByUri(const RtpHeaderExtensions& extensions,
@ -927,17 +923,16 @@ static bool FindByUriWithEncryptionPreference(
bool encryption_preference, bool encryption_preference,
webrtc::RtpExtension* found_extension) { webrtc::RtpExtension* found_extension) {
const webrtc::RtpExtension* unencrypted_extension = nullptr; const webrtc::RtpExtension* unencrypted_extension = nullptr;
for (RtpHeaderExtensions::const_iterator it = extensions.begin(); for (const webrtc::RtpExtension& extension : extensions) {
it != extensions.end(); ++it) {
// We assume that all URIs are given in a canonical format. // We assume that all URIs are given in a canonical format.
if (it->uri == ext_to_match.uri) { if (extension.uri == ext_to_match.uri) {
if (!encryption_preference || it->encrypt) { if (!encryption_preference || extension.encrypt) {
if (found_extension) { if (found_extension) {
*found_extension = *it; *found_extension = extension;
} }
return true; return true;
} }
unencrypted_extension = &(*it); unencrypted_extension = &extension;
} }
} }
if (unencrypted_extension) { if (unencrypted_extension) {
@ -1025,12 +1020,10 @@ static void NegotiateRtpHeaderExtensions(
const RtpHeaderExtensions& offered_extensions, const RtpHeaderExtensions& offered_extensions,
bool enable_encrypted_rtp_header_extensions, bool enable_encrypted_rtp_header_extensions,
RtpHeaderExtensions* negotiated_extenstions) { RtpHeaderExtensions* negotiated_extenstions) {
RtpHeaderExtensions::const_iterator ours; for (const webrtc::RtpExtension& ours : local_extensions) {
for (ours = local_extensions.begin(); ours != local_extensions.end();
++ours) {
webrtc::RtpExtension theirs; webrtc::RtpExtension theirs;
if (FindByUriWithEncryptionPreference( if (FindByUriWithEncryptionPreference(
offered_extensions, *ours, enable_encrypted_rtp_header_extensions, offered_extensions, ours, enable_encrypted_rtp_header_extensions,
&theirs)) { &theirs)) {
// We respond with their RTP header extension id. // We respond with their RTP header extension id.
negotiated_extenstions->push_back(theirs); negotiated_extenstions->push_back(theirs);
@ -1039,14 +1032,13 @@ static void NegotiateRtpHeaderExtensions(
} }
static void StripCNCodecs(AudioCodecs* audio_codecs) { static void StripCNCodecs(AudioCodecs* audio_codecs) {
AudioCodecs::iterator iter = audio_codecs->begin(); audio_codecs->erase(std::remove_if(audio_codecs->begin(), audio_codecs->end(),
while (iter != audio_codecs->end()) { [](const AudioCodec& codec) {
if (STR_CASE_CMP(iter->name.c_str(), kComfortNoiseCodecName) == 0) { return STR_CASE_CMP(
iter = audio_codecs->erase(iter); codec.name.c_str(),
} else { kComfortNoiseCodecName) == 0;
++iter; }),
} audio_codecs->end());
}
} }
// Create a media content to be answered for the given |sender_options| // Create a media content to be answered for the given |sender_options|

View File

@ -321,15 +321,15 @@ static void AttachSenderToMediaSection(
static void DetachSenderFromMediaSection(const std::string& mid, static void DetachSenderFromMediaSection(const std::string& mid,
const std::string& track_id, const std::string& track_id,
MediaSessionOptions* session_options) { MediaSessionOptions* session_options) {
auto it = FindFirstMediaDescriptionByMid(mid, session_options); std::vector<cricket::SenderOptions>& sender_options_list =
auto sender_it = it->sender_options.begin(); FindFirstMediaDescriptionByMid(mid, session_options)->sender_options;
for (; sender_it != it->sender_options.end(); ++sender_it) { auto sender_it =
if (sender_it->track_id == track_id) { std::find_if(sender_options_list.begin(), sender_options_list.end(),
it->sender_options.erase(sender_it); [track_id](const cricket::SenderOptions& sender_options) {
return; return sender_options.track_id == track_id;
} });
} RTC_DCHECK(sender_it != sender_options_list.end());
RTC_NOTREACHED(); sender_options_list.erase(sender_it);
} }
// Helper function used to create a default MediaSessionOptions for Plan B SDP. // Helper function used to create a default MediaSessionOptions for Plan B SDP.
@ -2183,10 +2183,9 @@ TEST_F(MediaSessionDescriptionFactoryTest, RtxWithoutApt) {
ASSERT_TRUE(media_desc); ASSERT_TRUE(media_desc);
VideoContentDescription* desc = media_desc->as_video(); VideoContentDescription* desc = media_desc->as_video();
std::vector<VideoCodec> codecs = desc->codecs(); std::vector<VideoCodec> codecs = desc->codecs();
for (std::vector<VideoCodec>::iterator iter = codecs.begin(); for (VideoCodec& codec : codecs) {
iter != codecs.end(); ++iter) { if (codec.name.find(cricket::kRtxCodecName) == 0) {
if (iter->name.find(cricket::kRtxCodecName) == 0) { codec.params.clear();
iter->params.clear();
} }
} }
desc->set_codecs(codecs); desc->set_codecs(codecs);