diff --git a/pc/BUILD.gn b/pc/BUILD.gn index 58e27c0b05..005a848f2b 100644 --- a/pc/BUILD.gn +++ b/pc/BUILD.gn @@ -139,6 +139,7 @@ rtc_static_library("peerconnection") { "peerconnection.h", "peerconnectionfactory.cc", "peerconnectionfactory.h", + "peerconnectioninternal.h", "remoteaudiosource.cc", "remoteaudiosource.h", "rtcstatscollector.cc", diff --git a/pc/peerconnection.cc b/pc/peerconnection.cc index d24dcbf8f4..fefa321b37 100644 --- a/pc/peerconnection.cc +++ b/pc/peerconnection.cc @@ -3887,7 +3887,7 @@ rtc::scoped_refptr PeerConnection::InternalCreateDataChannel( &PeerConnection::OnSctpDataChannelClosed); } - SignalDataChannelCreated(channel.get()); + SignalDataChannelCreated_(channel.get()); return channel; } diff --git a/pc/peerconnection.h b/pc/peerconnection.h index a8a2bd8196..c73f1ecd27 100644 --- a/pc/peerconnection.h +++ b/pc/peerconnection.h @@ -21,6 +21,7 @@ #include "api/turncustomizer.h" #include "pc/iceserverparsing.h" #include "pc/peerconnectionfactory.h" +#include "pc/peerconnectioninternal.h" #include "pc/rtcstatscollector.h" #include "pc/rtptransceiver.h" #include "pc/statscollector.h" @@ -33,27 +34,6 @@ class MediaStreamObserver; class VideoRtpReceiver; class RtcEventLog; -// Statistics for all the transports of the session. -// TODO(pthatcher): Think of a better name for this. We already have -// a TransportStats in transport.h. Perhaps TransportsStats? -struct SessionStats { - std::map transport_stats; -}; - -struct ChannelNamePair { - ChannelNamePair(const std::string& content_name, - const std::string& transport_name) - : content_name(content_name), transport_name(transport_name) {} - std::string content_name; - std::string transport_name; -}; - -struct ChannelNamePairs { - rtc::Optional voice; - rtc::Optional video; - rtc::Optional data; -}; - // PeerConnection is the implementation of the PeerConnection object as defined // by the PeerConnectionInterface API surface. // The class currently is solely responsible for the following: @@ -68,7 +48,7 @@ struct ChannelNamePairs { // - Generating offers and answers based on the current state. // - The ICE state machine. // - Generating stats. -class PeerConnection : public PeerConnectionInterface, +class PeerConnection : public PeerConnectionInternal, public DataChannelProviderInterface, public rtc::MessageHandler, public sigslot::has_slots<> { @@ -203,98 +183,86 @@ class PeerConnection : public PeerConnectionInterface, void Close() override; - sigslot::signal1 SignalDataChannelCreated; - - // Virtual for unit tests. - virtual const std::vector>& - sctp_data_channels() const { - return sctp_data_channels_; + // PeerConnectionInternal implementation. + rtc::Thread* network_thread() const override { + return factory_->network_thread(); + } + rtc::Thread* worker_thread() const override { + return factory_->worker_thread(); + } + rtc::Thread* signaling_thread() const override { + return factory_->signaling_thread(); } - rtc::Thread* network_thread() const { return factory_->network_thread(); } - rtc::Thread* worker_thread() const { return factory_->worker_thread(); } - rtc::Thread* signaling_thread() const { return factory_->signaling_thread(); } + const std::string& session_id() const override { return session_id_; } - // The SDP session ID as defined by RFC 3264. - virtual const std::string& session_id() const { return session_id_; } + bool initial_offerer() const override { + return initial_offerer_ && *initial_offerer_; + } - // Returns true if we were the initial offerer. - bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; } - - // Returns stats for all channels of all transports. - // This avoids exposing the internal structures used to track them. - // The parameterless version creates |ChannelNamePairs| from |voice_channel|, - // |video_channel| and |voice_channel| if available - this requires it to be - // called on the signaling thread - and invokes the other |GetStats|. The - // other |GetStats| can be invoked on any thread; if not invoked on the - // network thread a thread hop will happen. - std::unique_ptr GetSessionStats_s(); - virtual std::unique_ptr GetSessionStats( - const ChannelNamePairs& channel_name_pairs); - - // virtual so it can be mocked in unit tests - virtual bool GetLocalCertificate( - const std::string& transport_name, - rtc::scoped_refptr* certificate); - virtual std::unique_ptr GetRemoteSSLCertificate( - const std::string& transport_name); - - virtual Call::Stats GetCallStats(); - - // Exposed for stats collecting. - // TODO(steveanton): Switch callers to use the plural form and remove these. - virtual cricket::VoiceChannel* voice_channel() const { + cricket::VoiceChannel* voice_channel() const override { if (IsUnifiedPlan()) { - // TODO(steveanton): Change stats collection to work with transceivers. + // TODO(bugs.webrtc.org/8764): Change stats collection to work with + // transceivers. return nullptr; } return static_cast( GetAudioTransceiver()->internal()->channel()); } - virtual cricket::VideoChannel* video_channel() const { + + cricket::VideoChannel* video_channel() const override { if (IsUnifiedPlan()) { - // TODO(steveanton): Change stats collection to work with transceivers. + // TODO(bugs.webrtc.org/8764): Change stats collection to work with + // transceivers. return nullptr; } return static_cast( GetVideoTransceiver()->internal()->channel()); } - // Only valid when using deprecated RTP data channels. - virtual cricket::RtpDataChannel* rtp_data_channel() { + std::vector< + rtc::scoped_refptr>> + GetTransceiversForTesting() const override { + return transceivers_; + } + + bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override; + bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override; + + sigslot::signal1& SignalDataChannelCreated() override { + return SignalDataChannelCreated_; + } + + cricket::RtpDataChannel* rtp_data_channel() const override { return rtp_data_channel_; } - virtual rtc::Optional sctp_content_name() const { + + const std::vector>& sctp_data_channels() + const override { + return sctp_data_channels_; + } + + rtc::Optional sctp_content_name() const override { return sctp_content_name_; } - virtual rtc::Optional sctp_transport_name() const { + + rtc::Optional sctp_transport_name() const override { return sctp_transport_name_; } - // Get the id used as a media stream track's "id" field from ssrc. - virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id); - virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id); + std::unique_ptr GetSessionStats_s() override; + std::unique_ptr GetSessionStats( + const ChannelNamePairs& channel_name_pairs) override; + Call::Stats GetCallStats() override; - // Returns true if there was an ICE restart initiated by the remote offer. - bool IceRestartPending(const std::string& content_name) const; - - // Returns true if the ICE restart flag above was set, and no ICE restart has - // occurred yet for this transport (by applying a local description with - // changed ufrag/password). If the transport has been deleted as a result of - // bundling, returns false. - bool NeedsIceRestart(const std::string& content_name) const; - - // Get SSL role for an arbitrary m= section (handles bundling correctly). - // TODO(deadbeef): This is only used internally by the session description - // factory, it shouldn't really be public). - bool GetSslRole(const std::string& content_name, rtc::SSLRole* role); - - // Exposed for tests. - std::vector< - rtc::scoped_refptr>> - GetTransceiversForTesting() const { - return transceivers_; - } + bool GetLocalCertificate( + const std::string& transport_name, + rtc::scoped_refptr* certificate) override; + std::unique_ptr GetRemoteSSLCertificate( + const std::string& transport_name) override; + bool IceRestartPending(const std::string& content_name) const override; + bool NeedsIceRestart(const std::string& content_name) const override; + bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override; protected: ~PeerConnection() override; @@ -921,6 +889,8 @@ class PeerConnection : public PeerConnectionInterface, // method is called. void DestroyBaseChannel(cricket::BaseChannel* channel); + sigslot::signal1 SignalDataChannelCreated_; + // Storing the factory as a scoped reference pointer ensures that the memory // in the PeerConnectionFactoryImpl remains available as long as the // PeerConnection is running. It is passed to PeerConnection as a raw pointer. diff --git a/pc/peerconnectioninternal.h b/pc/peerconnectioninternal.h new file mode 100644 index 0000000000..4e179f5d15 --- /dev/null +++ b/pc/peerconnectioninternal.h @@ -0,0 +1,118 @@ +/* + * Copyright 2018 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_PEERCONNECTIONINTERNAL_H_ +#define PC_PEERCONNECTIONINTERNAL_H_ + +#include +#include +#include +#include + +#include "api/peerconnectioninterface.h" +#include "pc/datachannel.h" +#include "pc/rtptransceiver.h" + +namespace webrtc { + +// Statistics for all the transports of the session. +// TODO(pthatcher): Think of a better name for this. We already have +// a TransportStats in transport.h. Perhaps TransportsStats? +struct SessionStats { + std::map transport_stats; +}; + +struct ChannelNamePair { + ChannelNamePair(const std::string& content_name, + const std::string& transport_name) + : content_name(content_name), transport_name(transport_name) {} + std::string content_name; + std::string transport_name; +}; + +struct ChannelNamePairs { + rtc::Optional voice; + rtc::Optional video; + rtc::Optional data; +}; + +// Internal interface for extra PeerConnection methods. +class PeerConnectionInternal : public PeerConnectionInterface { + public: + virtual rtc::Thread* network_thread() const = 0; + virtual rtc::Thread* worker_thread() const = 0; + virtual rtc::Thread* signaling_thread() const = 0; + + // The SDP session ID as defined by RFC 3264. + virtual const std::string& session_id() const = 0; + + // Returns true if we were the initial offerer. + virtual bool initial_offerer() const = 0; + + // TODO(steveanton): Remove these. + virtual cricket::VoiceChannel* voice_channel() const = 0; + virtual cricket::VideoChannel* video_channel() const = 0; + + // Exposed for tests. + virtual std::vector< + rtc::scoped_refptr>> + GetTransceiversForTesting() const = 0; + + // Get the id used as a media stream track's "id" field from ssrc. + virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id) = 0; + virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id) = 0; + + virtual sigslot::signal1& SignalDataChannelCreated() = 0; + + // Only valid when using deprecated RTP data channels. + virtual cricket::RtpDataChannel* rtp_data_channel() const = 0; + + virtual const std::vector>& + sctp_data_channels() const = 0; + + virtual rtc::Optional sctp_content_name() const = 0; + virtual rtc::Optional sctp_transport_name() const = 0; + + // Returns stats for all channels of all transports. + // This avoids exposing the internal structures used to track them. + // The parameterless version creates |ChannelNamePairs| from |voice_channel|, + // |video_channel| and |voice_channel| if available - this requires it to be + // called on the signaling thread - and invokes the other |GetStats|. The + // other |GetStats| can be invoked on any thread; if not invoked on the + // network thread a thread hop will happen. + virtual std::unique_ptr GetSessionStats_s() = 0; + virtual std::unique_ptr GetSessionStats( + const ChannelNamePairs& channel_name_pairs) = 0; + + virtual Call::Stats GetCallStats() = 0; + + virtual bool GetLocalCertificate( + const std::string& transport_name, + rtc::scoped_refptr* certificate) = 0; + virtual std::unique_ptr GetRemoteSSLCertificate( + const std::string& transport_name) = 0; + + // Returns true if there was an ICE restart initiated by the remote offer. + virtual bool IceRestartPending(const std::string& content_name) const = 0; + + // Returns true if the ICE restart flag above was set, and no ICE restart has + // occurred yet for this transport (by applying a local description with + // changed ufrag/password). If the transport has been deleted as a result of + // bundling, returns false. + virtual bool NeedsIceRestart(const std::string& content_name) const = 0; + + // Get SSL role for an arbitrary m= section (handles bundling correctly). + virtual bool GetSslRole(const std::string& content_name, + rtc::SSLRole* role) = 0; +}; + +} // namespace webrtc + +#endif // PC_PEERCONNECTIONINTERNAL_H_ diff --git a/pc/rtcstatscollector.cc b/pc/rtcstatscollector.cc index ff68f37aae..bc0c3cf1e6 100644 --- a/pc/rtcstatscollector.cc +++ b/pc/rtcstatscollector.cc @@ -629,12 +629,13 @@ void ProduceMediaStreamStats( } // namespace rtc::scoped_refptr RTCStatsCollector::Create( - PeerConnection* pc, int64_t cache_lifetime_us) { + PeerConnectionInternal* pc, + int64_t cache_lifetime_us) { return rtc::scoped_refptr( new rtc::RefCountedObject(pc, cache_lifetime_us)); } -RTCStatsCollector::RTCStatsCollector(PeerConnection* pc, +RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc, int64_t cache_lifetime_us) : pc_(pc), signaling_thread_(pc->signaling_thread()), @@ -649,7 +650,7 @@ RTCStatsCollector::RTCStatsCollector(PeerConnection* pc, RTC_DCHECK(worker_thread_); RTC_DCHECK(network_thread_); RTC_DCHECK_GE(cache_lifetime_us_, 0); - pc_->SignalDataChannelCreated.connect( + pc_->SignalDataChannelCreated().connect( this, &RTCStatsCollector::OnDataChannelCreated); } diff --git a/pc/rtcstatscollector.h b/pc/rtcstatscollector.h index d73a76443f..0549966a93 100644 --- a/pc/rtcstatscollector.h +++ b/pc/rtcstatscollector.h @@ -24,6 +24,7 @@ #include "call/call.h" #include "media/base/mediachannel.h" #include "pc/datachannel.h" +#include "pc/peerconnectioninternal.h" #include "pc/trackmediainfomap.h" #include "rtc_base/asyncinvoker.h" #include "rtc_base/refcount.h" @@ -32,20 +33,8 @@ #include "rtc_base/sslidentity.h" #include "rtc_base/timeutils.h" -namespace cricket { -class Candidate; -} // namespace cricket - -namespace rtc { -class SSLCertificate; -} // namespace rtc - namespace webrtc { -class PeerConnection; -struct SessionStats; -struct ChannelNamePairs; - // All public methods of the collector are to be called on the signaling thread. // Stats are gathered on the signaling, worker and network threads // asynchronously. The callback is invoked on the signaling thread. Resulting @@ -54,7 +43,7 @@ class RTCStatsCollector : public virtual rtc::RefCountInterface, public sigslot::has_slots<> { public: static rtc::scoped_refptr Create( - PeerConnection* pc, + PeerConnectionInternal* pc, int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec); // Gets a recent stats report. If there is a report cached that is still fresh @@ -71,7 +60,7 @@ class RTCStatsCollector : public virtual rtc::RefCountInterface, void WaitForPendingRequest(); protected: - RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us); + RTCStatsCollector(PeerConnectionInternal* pc, int64_t cache_lifetime_us); ~RTCStatsCollector(); // Stats gathering on a particular thread. Calls |AddPartialResults| before @@ -141,7 +130,7 @@ class RTCStatsCollector : public virtual rtc::RefCountInterface, void OnDataChannelOpened(DataChannel* channel); void OnDataChannelClosed(DataChannel* channel); - PeerConnection* const pc_; + PeerConnectionInternal* const pc_; rtc::Thread* const signaling_thread_; rtc::Thread* const worker_thread_; rtc::Thread* const network_thread_; diff --git a/pc/rtcstatscollector_unittest.cc b/pc/rtcstatscollector_unittest.cc index 20cfc59de6..8ff35efd6a 100644 --- a/pc/rtcstatscollector_unittest.cc +++ b/pc/rtcstatscollector_unittest.cc @@ -570,7 +570,7 @@ class FakeRTCStatsCollector : public RTCStatsCollector, public RTCStatsCollectorCallback { public: static rtc::scoped_refptr Create( - PeerConnection* pc, + PeerConnectionInternal* pc, int64_t cache_lifetime_us) { return rtc::scoped_refptr( new rtc::RefCountedObject( @@ -608,7 +608,7 @@ class FakeRTCStatsCollector : public RTCStatsCollector, } protected: - FakeRTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime) + FakeRTCStatsCollector(PeerConnectionInternal* pc, int64_t cache_lifetime) : RTCStatsCollector(pc, cache_lifetime), signaling_thread_(pc->signaling_thread()), worker_thread_(pc->worker_thread()), @@ -1461,10 +1461,10 @@ TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { rtc::scoped_refptr dummy_channel_a = DataChannel::Create( nullptr, cricket::DCT_NONE, "DummyChannelA", InternalDataChannelInit()); - test_->pc().SignalDataChannelCreated(dummy_channel_a.get()); + test_->pc().SignalDataChannelCreated()(dummy_channel_a.get()); rtc::scoped_refptr dummy_channel_b = DataChannel::Create( nullptr, cricket::DCT_NONE, "DummyChannelB", InternalDataChannelInit()); - test_->pc().SignalDataChannelCreated(dummy_channel_b.get()); + test_->pc().SignalDataChannelCreated()(dummy_channel_b.get()); dummy_channel_a->SignalOpened(dummy_channel_a.get()); // Closing a channel that is not opened should not affect the counts. diff --git a/pc/rtpsender.cc b/pc/rtpsender.cc index 7c85764279..ef8c650acc 100644 --- a/pc/rtpsender.cc +++ b/pc/rtpsender.cc @@ -14,6 +14,7 @@ #include "api/mediastreaminterface.h" #include "pc/localaudiosource.h" +#include "pc/statscollector.h" #include "rtc_base/checks.h" #include "rtc_base/helpers.h" #include "rtc_base/trace_event.h" diff --git a/pc/rtpsender.h b/pc/rtpsender.h index 1fd7b37344..092efe7111 100644 --- a/pc/rtpsender.h +++ b/pc/rtpsender.h @@ -26,10 +26,11 @@ #include "media/base/audiosource.h" #include "media/base/mediachannel.h" #include "pc/dtmfsender.h" -#include "pc/statscollector.h" namespace webrtc { +class StatsCollector; + // Internal interface used by PeerConnection. class RtpSenderInternal : public RtpSenderInterface { public: diff --git a/pc/statscollector.cc b/pc/statscollector.cc index a8d187ee16..80c988dca4 100644 --- a/pc/statscollector.cc +++ b/pc/statscollector.cc @@ -434,7 +434,7 @@ const char* AdapterTypeToStatsType(rtc::AdapterType type) { } } -StatsCollector::StatsCollector(PeerConnection* pc) +StatsCollector::StatsCollector(PeerConnectionInternal* pc) : pc_(pc), stats_gathering_started_(0) { RTC_DCHECK(pc_); } diff --git a/pc/statscollector.h b/pc/statscollector.h index 1a3abfc9aa..c171dcae02 100644 --- a/pc/statscollector.h +++ b/pc/statscollector.h @@ -22,11 +22,10 @@ #include "api/mediastreaminterface.h" #include "api/peerconnectioninterface.h" #include "api/statstypes.h" +#include "pc/peerconnectioninternal.h" namespace webrtc { -class PeerConnection; - // Conversion function to convert candidate type string to the corresponding one // from enum RTCStatsIceCandidateType. const char* IceCandidateTypeToStatsType(const std::string& candidate_type); @@ -43,7 +42,7 @@ class StatsCollector { public: // The caller is responsible for ensuring that the pc outlives the // StatsCollector instance. - explicit StatsCollector(PeerConnection* pc); + explicit StatsCollector(PeerConnectionInternal* pc); virtual ~StatsCollector(); // Adds a MediaStream with tracks that can be used as a |selector| in a call @@ -139,7 +138,7 @@ class StatsCollector { StatsCollection reports_; TrackIdMap track_ids_; // Raw pointer to the peer connection the statistics are gathered from. - PeerConnection* const pc_; + PeerConnectionInternal* const pc_; double stats_gathering_started_; // TODO(tommi): We appear to be holding on to raw pointers to reference diff --git a/pc/webrtcsessiondescriptionfactory.cc b/pc/webrtcsessiondescriptionfactory.cc index feec1fdd41..36927bba0c 100644 --- a/pc/webrtcsessiondescriptionfactory.cc +++ b/pc/webrtcsessiondescriptionfactory.cc @@ -18,7 +18,6 @@ #include "api/jsep.h" #include "api/jsepsessiondescription.h" #include "api/mediaconstraintsinterface.h" -#include "pc/peerconnection.h" #include "rtc_base/checks.h" #include "rtc_base/ptr_util.h" #include "rtc_base/sslidentity.h" @@ -122,7 +121,7 @@ void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( rtc::Thread* signaling_thread, cricket::ChannelManager* channel_manager, - PeerConnection* pc, + PeerConnectionInternal* pc, const std::string& session_id, std::unique_ptr cert_generator, const rtc::scoped_refptr& certificate) diff --git a/pc/webrtcsessiondescriptionfactory.h b/pc/webrtcsessiondescriptionfactory.h index 12d1cd215d..88d5727fe3 100644 --- a/pc/webrtcsessiondescriptionfactory.h +++ b/pc/webrtcsessiondescriptionfactory.h @@ -15,25 +15,15 @@ #include #include -#include "api/peerconnectioninterface.h" #include "p2p/base/transportdescriptionfactory.h" #include "pc/mediasession.h" +#include "pc/peerconnectioninternal.h" #include "rtc_base/constructormagic.h" #include "rtc_base/messagehandler.h" #include "rtc_base/rtccertificate.h" #include "rtc_base/rtccertificategenerator.h" -namespace cricket { -class ChannelManager; -class TransportDescriptionFactory; -} // namespace cricket - namespace webrtc { -class CreateSessionDescriptionObserver; -class MediaConstraintsInterface; -class PeerConnection; -class SessionDescriptionInterface; -class WebRtcSession; // DTLS certificate request callback class. class WebRtcCertificateGeneratorCallback @@ -84,7 +74,7 @@ class WebRtcSessionDescriptionFactory : public rtc::MessageHandler, WebRtcSessionDescriptionFactory( rtc::Thread* signaling_thread, cricket::ChannelManager* channel_manager, - PeerConnection* pc, + PeerConnectionInternal* pc, const std::string& session_id, std::unique_ptr cert_generator, const rtc::scoped_refptr& certificate); @@ -152,7 +142,7 @@ class WebRtcSessionDescriptionFactory : public rtc::MessageHandler, const std::unique_ptr cert_generator_; // TODO(jiayl): remove the dependency on peer connection once bug 2264 is // fixed. - PeerConnection* const pc_; + PeerConnectionInternal* const pc_; const std::string session_id_; CertificateRequestState certificate_request_state_;