webrtc_m130/test/peer_scenario/scenario_connection.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

243 lines
9.7 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2019 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 "test/peer_scenario/scenario_connection.h"
#include "absl/memory/memory.h"
#include "media/base/rtp_utils.h"
#include "modules/rtp_rtcp/source/rtp_packet_received.h"
#include "p2p/client/basic_port_allocator.h"
#include "pc/channel.h"
#include "pc/jsep_transport_controller.h"
#include "pc/rtp_transport_internal.h"
#include "pc/session_description.h"
#include "rtc_base/task_queue_for_test.h"
namespace webrtc {
class ScenarioIceConnectionImpl : public ScenarioIceConnection,
public sigslot::has_slots<>,
private JsepTransportController::Observer,
private RtpPacketSinkInterface {
public:
ScenarioIceConnectionImpl(test::NetworkEmulationManagerImpl* net,
IceConnectionObserver* observer);
~ScenarioIceConnectionImpl() override;
void SendRtpPacket(rtc::ArrayView<const uint8_t> packet_view) override;
void SendRtcpPacket(rtc::ArrayView<const uint8_t> packet_view) override;
void SetRemoteSdp(SdpType type, const std::string& remote_sdp) override;
void SetLocalSdp(SdpType type, const std::string& local_sdp) override;
EmulatedEndpoint* endpoint() override { return endpoint_; }
const cricket::TransportDescription& transport_description() const override {
return transport_description_;
}
private:
JsepTransportController::Config CreateJsepConfig();
bool OnTransportChanged(
const std::string& mid,
RtpTransportInternal* rtp_transport,
rtc::scoped_refptr<DtlsTransport> dtls_transport,
Reland "Reland "Refactor SCTP data channels to use DataChannelTransportInterface."" This is a reland of 487f9a17e426fd14bb06b13e861071b3f15d119b Original change's description: > Reland "Refactor SCTP data channels to use DataChannelTransportInterface." > > Also clears SctpTransport before deleting JsepTransport. > > SctpTransport is ref-counted, but the underlying transport is deleted when > JsepTransport clears the rtp_dtls_transport. This results in crashes when > usrsctp attempts to send outgoing packets through a dangling pointer to the > underlying transport. > > Clearing SctpTransport before DtlsTransport removes the pointer to the > underlying transport before it becomes invalid. > > This fixes a crash in chromium's web platform tests (see > https://chromium-review.googlesource.com/c/chromium/src/+/1776711). > > Original change's description: > > Refactor SCTP data channels to use DataChannelTransportInterface. > > > > This change moves SctpTransport to be owned by JsepTransport, which now > > holds a DataChannelTransport implementation for SCTP when it is used for > > data channels. > > > > This simplifies negotiation and fallback to SCTP. Negotiation can now > > use a composite DataChannelTransport, just as negotiation for RTP uses a > > composite RTP transport. > > > > PeerConnection also has one fewer way it needs to manage data channels. > > It now handles SCTP and datagram- or media-transport-based data channels > > the same way. > > > > There are a few leaky abstractions left. For example, PeerConnection > > calls Start() on the SctpTransport at a particular point in negotiation, > > but does not need to call this for other transports. Similarly, PC > > exposes an interface to the SCTP transport directly to the user; there > > is no equivalent for other transports. > > Bug: webrtc:9719 > Change-Id: I64e94b88afb119fdbf5f22750f88c8a084d53937 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/151981 > Reviewed-by: Benjamin Wright <benwright@webrtc.org> > Reviewed-by: Steve Anton <steveanton@webrtc.org> > Commit-Queue: Benjamin Wright <benwright@webrtc.org> > Commit-Queue: Bjorn Mellem <mellem@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#29120} Bug: webrtc:9719 Change-Id: I28481a3de64a3506bc57748106383eeba4ef205c Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/152740 Reviewed-by: Artem Titov <titovartem@webrtc.org> Reviewed-by: Benjamin Wright <benwright@webrtc.org> Reviewed-by: Seth Hampson <shampson@webrtc.org> Commit-Queue: Bjorn Mellem <mellem@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29290}
2019-09-23 14:53:54 -07:00
DataChannelTransportInterface* data_channel_transport) override;
void OnRtpPacket(const RtpPacketReceived& packet) override;
void OnCandidates(const std::string& mid,
const std::vector<cricket::Candidate>& candidates);
IceConnectionObserver* const observer_;
EmulatedEndpoint* const endpoint_;
EmulatedNetworkManagerInterface* const manager_;
rtc::Thread* const signaling_thread_;
rtc::Thread* const network_thread_;
rtc::scoped_refptr<rtc::RTCCertificate> const certificate_
RTC_GUARDED_BY(network_thread_);
cricket::TransportDescription const transport_description_
RTC_GUARDED_BY(signaling_thread_);
std::unique_ptr<cricket::BasicPortAllocator> port_allocator_
RTC_GUARDED_BY(network_thread_);
std::unique_ptr<JsepTransportController> jsep_controller_;
RtpTransportInternal* rtp_transport_ RTC_GUARDED_BY(network_thread_) =
nullptr;
std::unique_ptr<SessionDescriptionInterface> remote_description_
RTC_GUARDED_BY(signaling_thread_);
std::unique_ptr<SessionDescriptionInterface> local_description_
RTC_GUARDED_BY(signaling_thread_);
};
std::unique_ptr<ScenarioIceConnection> ScenarioIceConnection::Create(
webrtc::test::NetworkEmulationManagerImpl* net,
IceConnectionObserver* observer) {
Use std::make_unique instead of absl::make_unique. WebRTC is now using C++14 so there is no need to use the Abseil version of std::make_unique. This CL has been created with the following steps: git grep -l absl::make_unique | sort | uniq > /tmp/make_unique.txt git grep -l absl::WrapUnique | sort | uniq > /tmp/wrap_unique.txt git grep -l "#include <memory>" | sort | uniq > /tmp/memory.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/make_unique.txt /tmp/wrap_unique.txt | sort | \ uniq > /tmp/only_make_unique.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/only_make_unique.txt /tmp/memory.txt | \ xargs grep -l "absl/memory" > /tmp/add-memory.txt git grep -l "\babsl::make_unique\b" | \ xargs sed -i "s/\babsl::make_unique\b/std::make_unique/g" git checkout PRESUBMIT.py abseil-in-webrtc.md cat /tmp/add-memory.txt | \ xargs sed -i \ 's/#include "absl\/memory\/memory.h"/#include <memory>/g' git cl format # Manual fix order of the new inserted #include <memory> cat /tmp/only_make_unique | xargs grep -l "#include <memory>" | \ xargs sed -i '/#include "absl\/memory\/memory.h"/d' git ls-files | grep BUILD.gn | \ xargs sed -i '/\/\/third_party\/abseil-cpp\/absl\/memory/d' python tools_webrtc/gn_check_autofix.py \ -m tryserver.webrtc -b linux_rel # Repead the gn_check_autofix step for other platforms git ls-files | grep BUILD.gn | \ xargs sed -i 's/absl\/memory:memory/absl\/memory/g' git cl format Bug: webrtc:10945 Change-Id: I3fe28ea80f4dd3ba3cf28effd151d5e1f19aff89 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153221 Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29209}
2019-09-17 17:06:18 +02:00
return std::make_unique<ScenarioIceConnectionImpl>(net, observer);
}
ScenarioIceConnectionImpl::ScenarioIceConnectionImpl(
test::NetworkEmulationManagerImpl* net,
IceConnectionObserver* observer)
: observer_(observer),
endpoint_(net->CreateEndpoint(EmulatedEndpointConfig())),
manager_(net->CreateEmulatedNetworkManagerInterface({endpoint_})),
signaling_thread_(rtc::Thread::Current()),
network_thread_(manager_->network_thread()),
certificate_(rtc::RTCCertificate::Create(
rtc::SSLIdentity::Create("", ::rtc::KT_DEFAULT))),
transport_description_(
/*transport_options*/ {},
rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH),
rtc::CreateRandomString(cricket::ICE_PWD_LENGTH),
cricket::IceMode::ICEMODE_FULL,
cricket::ConnectionRole::CONNECTIONROLE_PASSIVE,
rtc::SSLFingerprint::CreateFromCertificate(*certificate_.get())
.get()),
port_allocator_(
new cricket::BasicPortAllocator(manager_->network_manager(),
manager_->packet_socket_factory())),
jsep_controller_(
Reland "Remove thread hops from events provided by JsepTransportController." This reverts commit 6e4fcac31312f2dda5b60d33874ff0cd62f94321. Reason for revert: Parent CL issue has been resolved. Original change's description: > Revert "Remove thread hops from events provided by JsepTransportController." > > This reverts commit f554b3c577f69fa9ffad5c07155898c2d985ac76. > > Reason for revert: Parent CL breaks FYI bots. > See https://webrtc-review.googlesource.com/c/src/+/206466 > > Original change's description: > > Remove thread hops from events provided by JsepTransportController. > > > > Events associated with Subscribe* methods in JTC had trampolines that > > would use an async invoker to fire the events on the signaling thread. > > This was being done for the purposes of PeerConnection but the concept > > of a signaling thread is otherwise not applicable to JTC and use of > > JTC from PC is inconsistent across threads (as has been flagged in > > webrtc:9987). > > > > This change makes all CallbackList members only accessible from the > > network thread and moves the signaling thread related work over to > > PeerConnection, which makes hops there more visible as well as making > > that class easier to refactor for thread efficiency. > > > > This CL removes the AsyncInvoker from JTC (webrtc:12339) > > > > The signaling_thread_ variable is also removed from JTC and more thread > > checks added to catch errors. > > > > Bug: webrtc:12427, webrtc:11988, webrtc:12339 > > Change-Id: Id232aedd00dfd5403b2ba0ca147d3eca7c12c7c5 > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/206062 > > Commit-Queue: Tommi <tommi@webrtc.org> > > Reviewed-by: Niels Moller <nisse@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#33195} > > TBR=nisse@webrtc.org,tommi@webrtc.org > > Change-Id: I6134b71b74a9408854b79d44506d513519e9cf4d > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:12427 > Bug: webrtc:11988 > Bug: webrtc:12339 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/206467 > Reviewed-by: Guido Urdaneta <guidou@webrtc.org> > Commit-Queue: Guido Urdaneta <guidou@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#33203} TBR=nisse@webrtc.org,tommi@webrtc.org,guidou@webrtc.org # Not skipping CQ checks because original CL landed > 1 day ago. Bug: webrtc:12427 Bug: webrtc:11988 Bug: webrtc:12339 Change-Id: I4e2e1490e1f9a87ed6ac4d722fd3c442e3059ae0 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/206809 Reviewed-by: Tommi <tommi@webrtc.org> Commit-Queue: Tommi <tommi@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33225}
2021-02-10 17:40:08 +00:00
new JsepTransportController(network_thread_,
port_allocator_.get(),
/*async_resolver_factory*/ nullptr,
CreateJsepConfig())) {
SendTask(network_thread_, [this] {
RTC_DCHECK_RUN_ON(network_thread_);
uint32_t flags = cricket::PORTALLOCATOR_DISABLE_TCP;
port_allocator_->set_flags(port_allocator_->flags() | flags);
port_allocator_->Initialize();
RTC_CHECK(port_allocator_->SetConfiguration(/*stun_servers*/ {},
/*turn_servers*/ {}, 0,
webrtc::NO_PRUNE));
jsep_controller_->SetLocalCertificate(certificate_);
});
}
ScenarioIceConnectionImpl::~ScenarioIceConnectionImpl() {
SendTask(network_thread_, [this] {
RTC_DCHECK_RUN_ON(network_thread_);
jsep_controller_.reset();
port_allocator_.reset();
rtp_transport_ = nullptr;
});
}
JsepTransportController::Config ScenarioIceConnectionImpl::CreateJsepConfig() {
JsepTransportController::Config config;
config.transport_observer = this;
config.bundle_policy =
PeerConnectionInterface::BundlePolicy::kBundlePolicyMaxBundle;
config.rtcp_handler = [this](const rtc::CopyOnWriteBuffer& packet,
int64_t packet_time_us) {
RTC_DCHECK_RUN_ON(network_thread_);
observer_->OnPacketReceived(packet);
};
config.field_trials = &field_trials;
return config;
}
void ScenarioIceConnectionImpl::SendRtpPacket(
rtc::ArrayView<const uint8_t> packet_view) {
rtc::CopyOnWriteBuffer packet(packet_view.data(), packet_view.size(),
::cricket::kMaxRtpPacketLen);
network_thread_->PostTask([this, packet = std::move(packet)]() mutable {
RTC_DCHECK_RUN_ON(network_thread_);
if (rtp_transport_ != nullptr)
rtp_transport_->SendRtpPacket(&packet, rtc::PacketOptions(),
cricket::PF_SRTP_BYPASS);
});
}
void ScenarioIceConnectionImpl::SendRtcpPacket(
rtc::ArrayView<const uint8_t> packet_view) {
rtc::CopyOnWriteBuffer packet(packet_view.data(), packet_view.size(),
::cricket::kMaxRtpPacketLen);
network_thread_->PostTask([this, packet = std::move(packet)]() mutable {
RTC_DCHECK_RUN_ON(network_thread_);
if (rtp_transport_ != nullptr)
rtp_transport_->SendRtcpPacket(&packet, rtc::PacketOptions(),
cricket::PF_SRTP_BYPASS);
});
}
void ScenarioIceConnectionImpl::SetRemoteSdp(SdpType type,
const std::string& remote_sdp) {
RTC_DCHECK_RUN_ON(signaling_thread_);
remote_description_ = webrtc::CreateSessionDescription(type, remote_sdp);
jsep_controller_->SubscribeIceCandidateGathered(
[this](const std::string& transport,
const std::vector<cricket::Candidate>& candidate) {
ScenarioIceConnectionImpl::OnCandidates(transport, candidate);
});
auto res = jsep_controller_->SetRemoteDescription(
remote_description_->GetType(), remote_description_->description());
RTC_CHECK(res.ok()) << res.message();
RtpDemuxerCriteria criteria;
for (const auto& content : remote_description_->description()->contents()) {
if (content.media_description()->as_audio()) {
for (const auto& codec :
content.media_description()->as_audio()->codecs()) {
criteria.payload_types().insert(codec.id);
}
}
if (content.media_description()->as_video()) {
for (const auto& codec :
content.media_description()->as_video()->codecs()) {
criteria.payload_types().insert(codec.id);
}
}
}
network_thread_->PostTask([this, criteria]() {
RTC_DCHECK_RUN_ON(network_thread_);
RTC_DCHECK(rtp_transport_);
rtp_transport_->RegisterRtpDemuxerSink(criteria, this);
});
}
void ScenarioIceConnectionImpl::SetLocalSdp(SdpType type,
const std::string& local_sdp) {
RTC_DCHECK_RUN_ON(signaling_thread_);
local_description_ = webrtc::CreateSessionDescription(type, local_sdp);
auto res = jsep_controller_->SetLocalDescription(
local_description_->GetType(), local_description_->description());
RTC_CHECK(res.ok()) << res.message();
jsep_controller_->MaybeStartGathering();
}
bool ScenarioIceConnectionImpl::OnTransportChanged(
const std::string& mid,
RtpTransportInternal* rtp_transport,
rtc::scoped_refptr<DtlsTransport> dtls_transport,
Reland "Reland "Refactor SCTP data channels to use DataChannelTransportInterface."" This is a reland of 487f9a17e426fd14bb06b13e861071b3f15d119b Original change's description: > Reland "Refactor SCTP data channels to use DataChannelTransportInterface." > > Also clears SctpTransport before deleting JsepTransport. > > SctpTransport is ref-counted, but the underlying transport is deleted when > JsepTransport clears the rtp_dtls_transport. This results in crashes when > usrsctp attempts to send outgoing packets through a dangling pointer to the > underlying transport. > > Clearing SctpTransport before DtlsTransport removes the pointer to the > underlying transport before it becomes invalid. > > This fixes a crash in chromium's web platform tests (see > https://chromium-review.googlesource.com/c/chromium/src/+/1776711). > > Original change's description: > > Refactor SCTP data channels to use DataChannelTransportInterface. > > > > This change moves SctpTransport to be owned by JsepTransport, which now > > holds a DataChannelTransport implementation for SCTP when it is used for > > data channels. > > > > This simplifies negotiation and fallback to SCTP. Negotiation can now > > use a composite DataChannelTransport, just as negotiation for RTP uses a > > composite RTP transport. > > > > PeerConnection also has one fewer way it needs to manage data channels. > > It now handles SCTP and datagram- or media-transport-based data channels > > the same way. > > > > There are a few leaky abstractions left. For example, PeerConnection > > calls Start() on the SctpTransport at a particular point in negotiation, > > but does not need to call this for other transports. Similarly, PC > > exposes an interface to the SCTP transport directly to the user; there > > is no equivalent for other transports. > > Bug: webrtc:9719 > Change-Id: I64e94b88afb119fdbf5f22750f88c8a084d53937 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/151981 > Reviewed-by: Benjamin Wright <benwright@webrtc.org> > Reviewed-by: Steve Anton <steveanton@webrtc.org> > Commit-Queue: Benjamin Wright <benwright@webrtc.org> > Commit-Queue: Bjorn Mellem <mellem@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#29120} Bug: webrtc:9719 Change-Id: I28481a3de64a3506bc57748106383eeba4ef205c Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/152740 Reviewed-by: Artem Titov <titovartem@webrtc.org> Reviewed-by: Benjamin Wright <benwright@webrtc.org> Reviewed-by: Seth Hampson <shampson@webrtc.org> Commit-Queue: Bjorn Mellem <mellem@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29290}
2019-09-23 14:53:54 -07:00
DataChannelTransportInterface* data_channel_transport) {
RTC_DCHECK_RUN_ON(network_thread_);
if (rtp_transport == nullptr) {
rtp_transport_->UnregisterRtpDemuxerSink(this);
} else {
RTC_DCHECK(rtp_transport_ == nullptr || rtp_transport_ == rtp_transport);
if (rtp_transport_ != rtp_transport) {
rtp_transport_ = rtp_transport;
}
RtpDemuxerCriteria criteria(mid);
rtp_transport_->RegisterRtpDemuxerSink(criteria, this);
}
return true;
}
void ScenarioIceConnectionImpl::OnRtpPacket(const RtpPacketReceived& packet) {
RTC_DCHECK_RUN_ON(network_thread_);
observer_->OnPacketReceived(packet.Buffer());
}
void ScenarioIceConnectionImpl::OnCandidates(
const std::string& mid,
const std::vector<cricket::Candidate>& candidates) {
RTC_DCHECK_RUN_ON(signaling_thread_);
observer_->OnIceCandidates(mid, candidates);
}
} // namespace webrtc