diff --git a/talk/app/webrtc/webrtcsession_unittest.cc b/talk/app/webrtc/webrtcsession_unittest.cc index 6bfa17e4fc..ab711edfb8 100644 --- a/talk/app/webrtc/webrtcsession_unittest.cc +++ b/talk/app/webrtc/webrtcsession_unittest.cc @@ -43,6 +43,7 @@ #include "webrtc/p2p/base/stunserver.h" #include "webrtc/p2p/base/teststunserver.h" #include "webrtc/p2p/base/testturnserver.h" +#include "webrtc/p2p/base/transportchannel.h" #include "webrtc/p2p/client/basicportallocator.h" #include "talk/session/media/channelmanager.h" #include "talk/session/media/mediasession.h" diff --git a/talk/session/media/channel.cc b/talk/session/media/channel.cc index 1ea70e49f3..025a6ad458 100644 --- a/talk/session/media/channel.cc +++ b/talk/session/media/channel.cc @@ -278,21 +278,28 @@ bool BaseChannel::SetRemoteContent(const MediaContentDescription* content, } void BaseChannel::StartConnectionMonitor(int cms) { - socket_monitor_.reset(new SocketMonitor(transport_channel_, - worker_thread(), - rtc::Thread::Current())); - socket_monitor_->SignalUpdate.connect( + // We pass in the BaseChannel instead of the transport_channel_ + // because if the transport_channel_ changes, the ConnectionMonitor + // would be pointing to the wrong TransportChannel. + connection_monitor_.reset(new ConnectionMonitor( + this, worker_thread(), rtc::Thread::Current())); + connection_monitor_->SignalUpdate.connect( this, &BaseChannel::OnConnectionMonitorUpdate); - socket_monitor_->Start(cms); + connection_monitor_->Start(cms); } void BaseChannel::StopConnectionMonitor() { - if (socket_monitor_) { - socket_monitor_->Stop(); - socket_monitor_.reset(); + if (connection_monitor_) { + connection_monitor_->Stop(); + connection_monitor_.reset(); } } +bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) { + ASSERT(worker_thread_ == rtc::Thread::Current()); + return transport_channel_->GetStats(infos); +} + void BaseChannel::set_rtcp_transport_channel(TransportChannel* channel) { if (rtcp_transport_channel_ != channel) { if (rtcp_transport_channel_) { @@ -1597,7 +1604,7 @@ void VoiceChannel::OnMessage(rtc::Message *pmsg) { } void VoiceChannel::OnConnectionMonitorUpdate( - SocketMonitor* monitor, const std::vector& infos) { + ConnectionMonitor* monitor, const std::vector& infos) { SignalConnectionMonitor(this, infos); } @@ -2003,7 +2010,7 @@ void VideoChannel::OnMessage(rtc::Message *pmsg) { } void VideoChannel::OnConnectionMonitorUpdate( - SocketMonitor *monitor, const std::vector &infos) { + ConnectionMonitor* monitor, const std::vector &infos) { SignalConnectionMonitor(this, infos); } @@ -2372,7 +2379,7 @@ void DataChannel::OnMessage(rtc::Message *pmsg) { } void DataChannel::OnConnectionMonitorUpdate( - SocketMonitor* monitor, const std::vector& infos) { + ConnectionMonitor* monitor, const std::vector& infos) { SignalConnectionMonitor(this, infos); } diff --git a/talk/session/media/channel.h b/talk/session/media/channel.h index 6256b35700..bb88c2ff7a 100644 --- a/talk/session/media/channel.h +++ b/talk/session/media/channel.h @@ -73,7 +73,8 @@ enum SinkType { class BaseChannel : public rtc::MessageHandler, public sigslot::has_slots<>, - public MediaChannel::NetworkInterface { + public MediaChannel::NetworkInterface, + public ConnectionStatsGetter { public: BaseChannel(rtc::Thread* thread, MediaEngineInterface* media_engine, MediaChannel* channel, BaseSession* session, @@ -130,6 +131,8 @@ class BaseChannel // Monitoring void StartConnectionMonitor(int cms); void StopConnectionMonitor(); + // For ConnectionStatsGetter, used by ConnectionMonitor + virtual bool GetConnectionStats(ConnectionInfos* infos) override; void set_srtp_signal_silent_time(uint32 silent_time) { srtp_filter_.set_signal_silent_time(silent_time); @@ -339,7 +342,7 @@ class BaseChannel // Handled in derived classes // Get the SRTP ciphers to use for RTP media virtual void GetSrtpCiphers(std::vector* ciphers) const = 0; - virtual void OnConnectionMonitorUpdate(SocketMonitor* monitor, + virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor, const std::vector& infos) = 0; // Helper function for invoking bool-returning methods on the worker thread. @@ -370,7 +373,7 @@ class BaseChannel SrtpFilter srtp_filter_; RtcpMuxFilter rtcp_mux_filter_; BundleFilter bundle_filter_; - rtc::scoped_ptr socket_monitor_; + rtc::scoped_ptr connection_monitor_; bool enabled_; bool writable_; bool rtp_ready_to_send_; @@ -480,7 +483,7 @@ class VoiceChannel : public BaseChannel { virtual void OnMessage(rtc::Message* pmsg); virtual void GetSrtpCiphers(std::vector* ciphers) const; virtual void OnConnectionMonitorUpdate( - SocketMonitor* monitor, const std::vector& infos); + ConnectionMonitor* monitor, const std::vector& infos); virtual void OnMediaMonitorUpdate( VoiceMediaChannel* media_channel, const VoiceMediaInfo& info); void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info); @@ -569,7 +572,7 @@ class VideoChannel : public BaseChannel { virtual void OnMessage(rtc::Message* pmsg); virtual void GetSrtpCiphers(std::vector* ciphers) const; virtual void OnConnectionMonitorUpdate( - SocketMonitor* monitor, const std::vector& infos); + ConnectionMonitor* monitor, const std::vector& infos); virtual void OnMediaMonitorUpdate( VideoMediaChannel* media_channel, const VideoMediaInfo& info); virtual void OnScreencastWindowEvent(uint32 ssrc, @@ -688,7 +691,7 @@ class DataChannel : public BaseChannel { virtual void OnMessage(rtc::Message* pmsg); virtual void GetSrtpCiphers(std::vector* ciphers) const; virtual void OnConnectionMonitorUpdate( - SocketMonitor* monitor, const std::vector& infos); + ConnectionMonitor* monitor, const std::vector& infos); virtual void OnMediaMonitorUpdate( DataMediaChannel* media_channel, const DataMediaInfo& info); virtual bool ShouldSetupDtlsSrtp() const; diff --git a/webrtc/p2p/client/socketmonitor.cc b/webrtc/p2p/client/socketmonitor.cc index 5245535bc0..cc48c015a0 100644 --- a/webrtc/p2p/client/socketmonitor.cc +++ b/webrtc/p2p/client/socketmonitor.cc @@ -21,53 +21,53 @@ enum { MSG_MONITOR_SIGNAL }; -SocketMonitor::SocketMonitor(TransportChannel* channel, - rtc::Thread* worker_thread, - rtc::Thread* monitor_thread) { - channel_ = channel; - channel_thread_ = worker_thread; - monitoring_thread_ = monitor_thread; +ConnectionMonitor::ConnectionMonitor(ConnectionStatsGetter* stats_getter, + rtc::Thread* worker_thread, + rtc::Thread* monitoring_thread) { + stats_getter_ = stats_getter; + worker_thread_ = worker_thread; + monitoring_thread_ = monitoring_thread; monitoring_ = false; } -SocketMonitor::~SocketMonitor() { - channel_thread_->Clear(this); +ConnectionMonitor::~ConnectionMonitor() { + worker_thread_->Clear(this); monitoring_thread_->Clear(this); } -void SocketMonitor::Start(int milliseconds) { +void ConnectionMonitor::Start(int milliseconds) { rate_ = milliseconds; if (rate_ < 250) rate_ = 250; - channel_thread_->Post(this, MSG_MONITOR_START); + worker_thread_->Post(this, MSG_MONITOR_START); } -void SocketMonitor::Stop() { - channel_thread_->Post(this, MSG_MONITOR_STOP); +void ConnectionMonitor::Stop() { + worker_thread_->Post(this, MSG_MONITOR_STOP); } -void SocketMonitor::OnMessage(rtc::Message *message) { +void ConnectionMonitor::OnMessage(rtc::Message *message) { rtc::CritScope cs(&crit_); switch (message->message_id) { case MSG_MONITOR_START: - ASSERT(rtc::Thread::Current() == channel_thread_); + ASSERT(rtc::Thread::Current() == worker_thread_); if (!monitoring_) { monitoring_ = true; - PollSocket(true); + PollConnectionStats_w(); } break; case MSG_MONITOR_STOP: - ASSERT(rtc::Thread::Current() == channel_thread_); + ASSERT(rtc::Thread::Current() == worker_thread_); if (monitoring_) { monitoring_ = false; - channel_thread_->Clear(this); + worker_thread_->Clear(this); } break; case MSG_MONITOR_POLL: - ASSERT(rtc::Thread::Current() == channel_thread_); - PollSocket(true); + ASSERT(rtc::Thread::Current() == worker_thread_); + PollConnectionStats_w(); break; case MSG_MONITOR_SIGNAL: { @@ -81,17 +81,16 @@ void SocketMonitor::OnMessage(rtc::Message *message) { } } -void SocketMonitor::PollSocket(bool poll) { - ASSERT(rtc::Thread::Current() == channel_thread_); +void ConnectionMonitor::PollConnectionStats_w() { + ASSERT(rtc::Thread::Current() == worker_thread_); rtc::CritScope cs(&crit_); // Gather connection infos - channel_->GetStats(&connection_infos_); + stats_getter_->GetConnectionStats(&connection_infos_); // Signal the monitoring thread, start another poll timer monitoring_thread_->Post(this, MSG_MONITOR_SIGNAL); - if (poll) - channel_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL); + worker_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL); } } // namespace cricket diff --git a/webrtc/p2p/client/socketmonitor.h b/webrtc/p2p/client/socketmonitor.h index 5c10a4ec03..e0dd81ef6c 100644 --- a/webrtc/p2p/client/socketmonitor.h +++ b/webrtc/p2p/client/socketmonitor.h @@ -13,36 +13,44 @@ #include -#include "webrtc/p2p/base/transportchannel.h" #include "webrtc/base/criticalsection.h" #include "webrtc/base/sigslot.h" #include "webrtc/base/thread.h" +#include "webrtc/p2p/base/transport.h" // for ConnectionInfos + +// TODO(pthatcher): Move these to connectionmonitor.h and +// connectionmonitor.cc, or just move them into channel.cc namespace cricket { -class SocketMonitor : public rtc::MessageHandler, - public sigslot::has_slots<> { +class ConnectionStatsGetter { public: - SocketMonitor(TransportChannel* channel, - rtc::Thread* worker_thread, - rtc::Thread* monitor_thread); - ~SocketMonitor(); + virtual ~ConnectionStatsGetter() {} + virtual bool GetConnectionStats(ConnectionInfos* infos) = 0; +}; + +class ConnectionMonitor : public rtc::MessageHandler, + public sigslot::has_slots<> { +public: + ConnectionMonitor(ConnectionStatsGetter* stats_getter, + rtc::Thread* worker_thread, + rtc::Thread* monitoring_thread); + ~ConnectionMonitor(); void Start(int cms); void Stop(); - rtc::Thread* monitor_thread() { return monitoring_thread_; } - - sigslot::signal2&> SignalUpdate; protected: void OnMessage(rtc::Message* message); - void PollSocket(bool poll); + private: + void PollConnectionStats_w(); std::vector connection_infos_; - TransportChannel* channel_; - rtc::Thread* channel_thread_; + ConnectionStatsGetter* stats_getter_; + rtc::Thread* worker_thread_; rtc::Thread* monitoring_thread_; rtc::CriticalSection crit_; uint32 rate_;