diff --git a/pc/BUILD.gn b/pc/BUILD.gn index cf0179ac45..1bfa37cae1 100644 --- a/pc/BUILD.gn +++ b/pc/BUILD.gn @@ -139,6 +139,8 @@ rtc_static_library("peerconnection") { "rtpsender.h", "sctputils.cc", "sctputils.h", + "sdputils.cc", + "sdputils.h", "statscollector.cc", "statscollector.h", "streamcollection.h", diff --git a/pc/peerconnectionwrapper.cc b/pc/peerconnectionwrapper.cc index 01710d9cb2..7379aec595 100644 --- a/pc/peerconnectionwrapper.cc +++ b/pc/peerconnectionwrapper.cc @@ -16,6 +16,7 @@ #include "api/jsepsessiondescription.h" #include "media/base/fakevideocapturer.h" +#include "pc/sdputils.h" #include "rtc_base/gunit.h" #include "rtc_base/ptr_util.h" @@ -129,16 +130,6 @@ bool PeerConnectionWrapper::SetSdp( return observer->result(); } -std::unique_ptr -PeerConnectionWrapper::CloneSessionDescription( - const SessionDescriptionInterface* desc) { - std::unique_ptr clone( - new JsepSessionDescription(desc->type())); - RTC_DCHECK(clone->Initialize(desc->description()->Copy(), desc->session_id(), - desc->session_version())); - return clone; -} - void PeerConnectionWrapper::AddAudioStream(const std::string& stream_label, const std::string& track_label) { auto stream = pc_factory()->CreateLocalMediaStream(stream_label); diff --git a/pc/peerconnectionwrapper.h b/pc/peerconnectionwrapper.h index 83b81865ab..cc21c898a5 100644 --- a/pc/peerconnectionwrapper.h +++ b/pc/peerconnectionwrapper.h @@ -95,8 +95,6 @@ class PeerConnectionWrapper { std::unique_ptr CreateSdp( std::function fn); bool SetSdp(std::function fn); - std::unique_ptr CloneSessionDescription( - const SessionDescriptionInterface* desc); rtc::scoped_refptr pc_factory_; rtc::scoped_refptr pc_; diff --git a/pc/sdputils.cc b/pc/sdputils.cc new file mode 100644 index 0000000000..9339fdb874 --- /dev/null +++ b/pc/sdputils.cc @@ -0,0 +1,58 @@ +/* + * Copyright 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "pc/sdputils.h" + +#include + +#include "api/jsepsessiondescription.h" +#include "rtc_base/ptr_util.h" + +namespace webrtc { + +std::unique_ptr CloneSessionDescription( + const SessionDescriptionInterface* sdesc) { + RTC_DCHECK(sdesc); + auto clone = rtc::MakeUnique(sdesc->type()); + clone->Initialize(sdesc->description()->Copy(), sdesc->session_id(), + sdesc->session_version()); + // As of writing, our version of GCC does not allow returning a unique_ptr of + // a subclass as a unique_ptr of a base class. To get around this, we need to + // std::move the return value. + return std::move(clone); +} + +bool SdpContentsAll(SdpContentPredicate pred, + const cricket::SessionDescription* desc) { + RTC_DCHECK(desc); + for (const auto& content : desc->contents()) { + const auto* transport_info = desc->GetTransportInfoByName(content.name); + if (!pred(&content, transport_info)) { + return false; + } + } + return true; +} + +bool SdpContentsNone(SdpContentPredicate pred, + const cricket::SessionDescription* desc) { + return SdpContentsAll(std::not2(pred), desc); +} + +void SdpContentsForEach(SdpContentMutator fn, + cricket::SessionDescription* desc) { + RTC_DCHECK(desc); + for (auto& content : desc->contents()) { + auto* transport_info = desc->GetTransportInfoByName(content.name); + fn(&content, transport_info); + } +} + +} // namespace webrtc diff --git a/pc/sdputils.h b/pc/sdputils.h new file mode 100644 index 0000000000..7d67fd8d72 --- /dev/null +++ b/pc/sdputils.h @@ -0,0 +1,54 @@ +/* + * Copyright 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef PC_SDPUTILS_H_ +#define PC_SDPUTILS_H_ + +#include +#include + +#include "api/jsep.h" +#include "p2p/base/sessiondescription.h" + +namespace webrtc { + +// Returns a copy of the given session description. +std::unique_ptr CloneSessionDescription( + const SessionDescriptionInterface* sdesc); + +// Function that takes a single session description content with its +// corresponding transport and produces a boolean. +typedef std::function + SdpContentPredicate; + +// Returns true if the predicate returns true for all contents in the given +// session description. +bool SdpContentsAll(SdpContentPredicate pred, + const cricket::SessionDescription* desc); + +// Returns true if the predicate returns true for none of the contents in the +// given session description. +bool SdpContentsNone(SdpContentPredicate pred, + const cricket::SessionDescription* desc); + +// Function that takes a single session description content with its +// corresponding transport and can mutate the content and/or the transport. +typedef std::function + SdpContentMutator; + +// Applies the mutator function over all contents in the given session +// description. +void SdpContentsForEach(SdpContentMutator fn, + cricket::SessionDescription* desc); + +} // namespace webrtc + +#endif // PC_SDPUTILS_H_