Move internal PeerConnection methods to PeerConnectionInternal

PeerConnectionInternal is being introduced so that it can be mocked in
tests and so that a fake can be written for it to be used by stats
tests.

Bug: webrtc:8764
Change-Id: I375d12ce352523e8ac584402685a7870bc399fac
Reviewed-on: https://webrtc-review.googlesource.com/43202
Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org>
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21747}
This commit is contained in:
Steve Anton 2018-01-23 16:38:46 -08:00 committed by Commit Bot
parent 194939b8ca
commit 2d8609c77e
13 changed files with 202 additions and 133 deletions

View File

@ -139,6 +139,7 @@ rtc_static_library("peerconnection") {
"peerconnection.h",
"peerconnectionfactory.cc",
"peerconnectionfactory.h",
"peerconnectioninternal.h",
"remoteaudiosource.cc",
"remoteaudiosource.h",
"rtcstatscollector.cc",

View File

@ -3887,7 +3887,7 @@ rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel(
&PeerConnection::OnSctpDataChannelClosed);
}
SignalDataChannelCreated(channel.get());
SignalDataChannelCreated_(channel.get());
return channel;
}

View File

@ -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<std::string, cricket::TransportStats> 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<ChannelNamePair> voice;
rtc::Optional<ChannelNamePair> video;
rtc::Optional<ChannelNamePair> 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<DataChannel*> SignalDataChannelCreated;
// Virtual for unit tests.
virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
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<SessionStats> GetSessionStats_s();
virtual std::unique_ptr<SessionStats> 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<rtc::RTCCertificate>* certificate);
virtual std::unique_ptr<rtc::SSLCertificate> 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<cricket::VoiceChannel*>(
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<cricket::VideoChannel*>(
GetVideoTransceiver()->internal()->channel());
}
// Only valid when using deprecated RTP data channels.
virtual cricket::RtpDataChannel* rtp_data_channel() {
std::vector<
rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
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<DataChannel*>& SignalDataChannelCreated() override {
return SignalDataChannelCreated_;
}
cricket::RtpDataChannel* rtp_data_channel() const override {
return rtp_data_channel_;
}
virtual rtc::Optional<std::string> sctp_content_name() const {
const std::vector<rtc::scoped_refptr<DataChannel>>& sctp_data_channels()
const override {
return sctp_data_channels_;
}
rtc::Optional<std::string> sctp_content_name() const override {
return sctp_content_name_;
}
virtual rtc::Optional<std::string> sctp_transport_name() const {
rtc::Optional<std::string> 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<SessionStats> GetSessionStats_s() override;
std::unique_ptr<SessionStats> 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<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
GetTransceiversForTesting() const {
return transceivers_;
}
bool GetLocalCertificate(
const std::string& transport_name,
rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
std::unique_ptr<rtc::SSLCertificate> 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<DataChannel*> 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.

118
pc/peerconnectioninternal.h Normal file
View File

@ -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 <map>
#include <memory>
#include <string>
#include <vector>
#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<std::string, cricket::TransportStats> 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<ChannelNamePair> voice;
rtc::Optional<ChannelNamePair> video;
rtc::Optional<ChannelNamePair> 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<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
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<DataChannel*>& SignalDataChannelCreated() = 0;
// Only valid when using deprecated RTP data channels.
virtual cricket::RtpDataChannel* rtp_data_channel() const = 0;
virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
sctp_data_channels() const = 0;
virtual rtc::Optional<std::string> sctp_content_name() const = 0;
virtual rtc::Optional<std::string> 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<SessionStats> GetSessionStats_s() = 0;
virtual std::unique_ptr<SessionStats> GetSessionStats(
const ChannelNamePairs& channel_name_pairs) = 0;
virtual Call::Stats GetCallStats() = 0;
virtual bool GetLocalCertificate(
const std::string& transport_name,
rtc::scoped_refptr<rtc::RTCCertificate>* certificate) = 0;
virtual std::unique_ptr<rtc::SSLCertificate> 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_

View File

@ -629,12 +629,13 @@ void ProduceMediaStreamStats(
} // namespace
rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
PeerConnection* pc, int64_t cache_lifetime_us) {
PeerConnectionInternal* pc,
int64_t cache_lifetime_us) {
return rtc::scoped_refptr<RTCStatsCollector>(
new rtc::RefCountedObject<RTCStatsCollector>(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);
}

View File

@ -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<RTCStatsCollector> 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_;

View File

@ -570,7 +570,7 @@ class FakeRTCStatsCollector : public RTCStatsCollector,
public RTCStatsCollectorCallback {
public:
static rtc::scoped_refptr<FakeRTCStatsCollector> Create(
PeerConnection* pc,
PeerConnectionInternal* pc,
int64_t cache_lifetime_us) {
return rtc::scoped_refptr<FakeRTCStatsCollector>(
new rtc::RefCountedObject<FakeRTCStatsCollector>(
@ -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<DataChannel> 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<DataChannel> 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.

View File

@ -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"

View File

@ -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:

View File

@ -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_);
}

View File

@ -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

View File

@ -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<rtc::RTCCertificateGeneratorInterface> cert_generator,
const rtc::scoped_refptr<rtc::RTCCertificate>& certificate)

View File

@ -15,25 +15,15 @@
#include <queue>
#include <string>
#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<rtc::RTCCertificateGeneratorInterface> cert_generator,
const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
@ -152,7 +142,7 @@ class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
const std::unique_ptr<rtc::RTCCertificateGeneratorInterface> 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_;