Refactor registerable callbacks for FrameCountObserver from rtp_rtcp module into vie_channel.

R=stefan@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/16839004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@6649 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h b/webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h
index 235ca84..34f4d3f 100644
--- a/webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h
+++ b/webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h
@@ -68,6 +68,7 @@
     RemoteBitrateEstimator* remote_bitrate_estimator;
     PacedSender* paced_sender;
     BitrateStatisticsObserver* send_bitrate_observer;
+    FrameCountObserver* send_frame_count_observer;
   };
 
   /*
@@ -340,10 +341,6 @@
 
     virtual int TimeToSendPadding(int bytes) = 0;
 
-    virtual void RegisterSendFrameCountObserver(
-        FrameCountObserver* observer) = 0;
-    virtual FrameCountObserver* GetSendFrameCountObserver() const = 0;
-
     virtual bool GetSendSideDelay(int* avg_send_delay_ms,
                                   int* max_send_delay_ms) const = 0;
 
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.cc b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
index 855d51b..a7f81c6 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
@@ -38,7 +38,8 @@
       audio_messages(NullObjectRtpAudioFeedback()),
       remote_bitrate_estimator(NULL),
       paced_sender(NULL),
-      send_bitrate_observer(NULL) {
+      send_bitrate_observer(NULL),
+      send_frame_count_observer(NULL) {
 }
 
 RtpRtcp* RtpRtcp::CreateRtpRtcp(const RtpRtcp::Configuration& configuration) {
@@ -62,7 +63,8 @@
                   configuration.outgoing_transport,
                   configuration.audio_messages,
                   configuration.paced_sender,
-                  configuration.send_bitrate_observer),
+                  configuration.send_bitrate_observer,
+                  configuration.send_frame_count_observer),
       rtcp_sender_(configuration.id,
                    configuration.audio,
                    configuration.clock,
@@ -1350,15 +1352,6 @@
   return rtp_sender_.GetRtpStatisticsCallback();
 }
 
-void ModuleRtpRtcpImpl::RegisterSendFrameCountObserver(
-    FrameCountObserver* observer) {
-  rtp_sender_.RegisterFrameCountObserver(observer);
-}
-
-FrameCountObserver* ModuleRtpRtcpImpl::GetSendFrameCountObserver() const {
-  return rtp_sender_.GetFrameCountObserver();
-}
-
 bool ModuleRtpRtcpImpl::IsDefaultModule() const {
   CriticalSectionScoped cs(critical_section_module_ptrs_.get());
   return !child_modules_.empty();
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h
index b65131f..37a0fa4 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h
+++ b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h
@@ -373,10 +373,6 @@
 
   void OnRequestSendReport();
 
-  virtual void RegisterSendFrameCountObserver(
-      FrameCountObserver* observer) OVERRIDE;
-  virtual FrameCountObserver* GetSendFrameCountObserver() const OVERRIDE;
-
  protected:
   void RegisterChildModule(RtpRtcp* module);
 
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_sender.cc b/webrtc/modules/rtp_rtcp/source/rtp_sender.cc
index 858fc42..c98d498 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_sender.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtp_sender.cc
@@ -46,7 +46,8 @@
                      Transport* transport,
                      RtpAudioFeedback* audio_feedback,
                      PacedSender* paced_sender,
-                     BitrateStatisticsObserver* bitrate_callback)
+                     BitrateStatisticsObserver* bitrate_callback,
+                     FrameCountObserver* frame_count_observer)
     : clock_(clock),
       bitrate_sent_(clock, this),
       id_(id),
@@ -71,9 +72,9 @@
       packet_history_(clock),
       // Statistics
       statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
-      frame_count_observer_(NULL),
       rtp_stats_callback_(NULL),
       bitrate_callback_(bitrate_callback),
+      frame_count_observer_(frame_count_observer),
       // RTP variables
       start_timestamp_forced_(false),
       start_timestamp_(0),
@@ -1664,16 +1665,6 @@
   *length += 2;
 }
 
-void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
-  CriticalSectionScoped cs(statistics_crit_.get());
-  frame_count_observer_ = observer;
-}
-
-FrameCountObserver* RTPSender::GetFrameCountObserver() const {
-  CriticalSectionScoped cs(statistics_crit_.get());
-  return frame_count_observer_;
-}
-
 void RTPSender::RegisterRtpStatisticsCallback(
     StreamDataCountersCallback* callback) {
   CriticalSectionScoped cs(statistics_crit_.get());
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_sender.h b/webrtc/modules/rtp_rtcp/source/rtp_sender.h
index 0cc35cf..265e69c 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_sender.h
+++ b/webrtc/modules/rtp_rtcp/source/rtp_sender.h
@@ -70,7 +70,8 @@
   RTPSender(const int32_t id, const bool audio, Clock *clock,
             Transport *transport, RtpAudioFeedback *audio_feedback,
             PacedSender *paced_sender,
-            BitrateStatisticsObserver* bitrate_callback);
+            BitrateStatisticsObserver* bitrate_callback,
+            FrameCountObserver* frame_count_observer);
   virtual ~RTPSender();
 
   void ProcessBitrate();
@@ -265,9 +266,6 @@
   int32_t SetFecParameters(const FecProtectionParams *delta_params,
                            const FecProtectionParams *key_params);
 
-  virtual void RegisterFrameCountObserver(FrameCountObserver* observer);
-  virtual FrameCountObserver* GetFrameCountObserver() const;
-
   int SendPadData(int payload_type, uint32_t timestamp, int64_t capture_time_ms,
                   int32_t bytes, StorageType store,
                   bool force_full_size_packets, bool only_pad_after_markerbit);
@@ -373,11 +371,11 @@
   scoped_ptr<CriticalSectionWrapper> statistics_crit_;
   SendDelayMap send_delays_ GUARDED_BY(statistics_crit_);
   std::map<FrameType, uint32_t> frame_counts_ GUARDED_BY(statistics_crit_);
-  FrameCountObserver* frame_count_observer_ GUARDED_BY(statistics_crit_);
   StreamDataCounters rtp_stats_ GUARDED_BY(statistics_crit_);
   StreamDataCounters rtx_rtp_stats_ GUARDED_BY(statistics_crit_);
   StreamDataCountersCallback* rtp_stats_callback_ GUARDED_BY(statistics_crit_);
   BitrateStatisticsObserver* const bitrate_callback_;
+  FrameCountObserver* const frame_count_observer_;
 
   // RTP variables
   bool start_timestamp_forced_ GUARDED_BY(send_critsect_);
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_sender_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtp_sender_unittest.cc
index e08aa20..42ee023 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_sender_unittest.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtp_sender_unittest.cc
@@ -94,7 +94,7 @@
 
   virtual void SetUp() {
     rtp_sender_.reset(new RTPSender(0, false, &fake_clock_, &transport_, NULL,
-                                    &mock_paced_sender_, NULL));
+                                    &mock_paced_sender_, NULL, NULL));
     rtp_sender_->SetSequenceNumber(kSeqNum);
   }
 
@@ -672,7 +672,7 @@
 TEST_F(RtpSenderTest, SendRedundantPayloads) {
   MockTransport transport;
   rtp_sender_.reset(new RTPSender(0, false, &fake_clock_, &transport, NULL,
-                                  &mock_paced_sender_, NULL));
+                                  &mock_paced_sender_, NULL, NULL));
   rtp_sender_->SetSequenceNumber(kSeqNum);
   // Make all packets go through the pacer.
   EXPECT_CALL(mock_paced_sender_,
@@ -817,6 +817,9 @@
     uint32_t delta_frames_;
   } callback;
 
+  rtp_sender_.reset(new RTPSender(0, false, &fake_clock_, &transport_, NULL,
+                                  &mock_paced_sender_, NULL, &callback));
+
   char payload_name[RTP_PAYLOAD_NAME_SIZE] = "GENERIC";
   const uint8_t payload_type = 127;
   ASSERT_EQ(0, rtp_sender_->RegisterPayload(payload_name, payload_type, 90000,
@@ -825,8 +828,6 @@
   rtp_sender_->SetStorePacketsStatus(true, 1);
   uint32_t ssrc = rtp_sender_->SSRC();
 
-  rtp_sender_->RegisterFrameCountObserver(&callback);
-
   ASSERT_EQ(0, rtp_sender_->SendOutgoingData(kVideoFrameKey, payload_type, 1234,
                                              4321, payload, sizeof(payload),
                                              NULL));
@@ -845,7 +846,7 @@
   EXPECT_EQ(1U, callback.key_frames_);
   EXPECT_EQ(1U, callback.delta_frames_);
 
-  rtp_sender_->RegisterFrameCountObserver(NULL);
+  rtp_sender_.reset();
 }
 
 TEST_F(RtpSenderTest, BitrateCallbacks) {
@@ -866,7 +867,7 @@
     BitrateStatistics bitrate_;
   } callback;
   rtp_sender_.reset(new RTPSender(0, false, &fake_clock_, &transport_, NULL,
-                                  &mock_paced_sender_, &callback));
+                                  &mock_paced_sender_, &callback, NULL));
 
   // Simulate kNumPackets sent with kPacketInterval ms intervals.
   const uint32_t kNumPackets = 15;
@@ -922,7 +923,7 @@
   virtual void SetUp() {
     payload_ = kAudioPayload;
     rtp_sender_.reset(new RTPSender(0, true, &fake_clock_, &transport_, NULL,
-                                    &mock_paced_sender_, NULL));
+                                    &mock_paced_sender_, NULL, NULL));
     rtp_sender_->SetSequenceNumber(kSeqNum);
   }
 };
diff --git a/webrtc/video_engine/vie_channel.cc b/webrtc/video_engine/vie_channel.cc
index 4662251..64c2692 100644
--- a/webrtc/video_engine/vie_channel.cc
+++ b/webrtc/video_engine/vie_channel.cc
@@ -117,6 +117,7 @@
   configuration.paced_sender = paced_sender;
   configuration.receive_statistics = vie_receiver_.GetReceiveStatistics();
   configuration.send_bitrate_observer = &send_bitrate_observer_;
+  configuration.send_frame_count_observer = &send_frame_count_observer_;
 
   rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(configuration));
   vie_receiver_.SetRtpRtcpModule(rtp_rtcp_.get());
@@ -298,7 +299,6 @@
       module_process_thread_.DeRegisterModule(rtp_rtcp);
       rtp_rtcp->SetSendingStatus(false);
       rtp_rtcp->SetSendingMediaStatus(false);
-      rtp_rtcp->RegisterSendFrameCountObserver(NULL);
       rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(NULL);
       rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(NULL);
       simulcast_rtp_rtcp_.pop_back();
@@ -346,8 +346,6 @@
         rtp_rtcp->DeregisterSendRtpHeaderExtension(
             kRtpExtensionAbsoluteSendTime);
       }
-      rtp_rtcp->RegisterSendFrameCountObserver(
-          rtp_rtcp_->GetSendFrameCountObserver());
       rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(
           rtp_rtcp_->GetSendChannelRtcpStatisticsCallback());
       rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(
@@ -362,7 +360,6 @@
       module_process_thread_.DeRegisterModule(rtp_rtcp);
       rtp_rtcp->SetSendingStatus(false);
       rtp_rtcp->SetSendingMediaStatus(false);
-      rtp_rtcp->RegisterSendFrameCountObserver(NULL);
       rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(NULL);
       rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(NULL);
       simulcast_rtp_rtcp_.pop_back();
@@ -1524,7 +1521,6 @@
     RtpRtcp* rtp_rtcp = CreateRtpRtcpModule();
     rtp_rtcp->SetSendingStatus(false);
     rtp_rtcp->SetSendingMediaStatus(false);
-    rtp_rtcp->RegisterSendFrameCountObserver(NULL);
     rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(NULL);
     rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(NULL);
     removed_rtp_rtcp_.push_back(rtp_rtcp);
@@ -1714,13 +1710,7 @@
 
 void ViEChannel::RegisterSendFrameCountObserver(
     FrameCountObserver* observer) {
-  rtp_rtcp_->RegisterSendFrameCountObserver(observer);
-  CriticalSectionScoped cs(rtp_rtcp_cs_.get());
-  for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
-       it != simulcast_rtp_rtcp_.end();
-       it++) {
-    (*it)->RegisterSendFrameCountObserver(observer);
-  }
+  send_frame_count_observer_.Set(observer);
 }
 
 void ViEChannel::ReceivedBWEPacket(int64_t arrival_time_ms,
diff --git a/webrtc/video_engine/vie_channel.h b/webrtc/video_engine/vie_channel.h
index 39f9b75..50ec8ea 100644
--- a/webrtc/video_engine/vie_channel.h
+++ b/webrtc/video_engine/vie_channel.h
@@ -411,7 +411,8 @@
     DISALLOW_COPY_AND_ASSIGN(RegisterableCallback);
   };
 
-  class : public RegisterableCallback<BitrateStatisticsObserver> {
+  class RegisterableBitrateStatisticsObserver:
+    public RegisterableCallback<BitrateStatisticsObserver> {
     virtual void Notify(const BitrateStatistics& stats, uint32_t ssrc) {
       CriticalSectionScoped cs(critsect_.get());
       if (callback_)
@@ -420,6 +421,17 @@
   }
   send_bitrate_observer_;
 
+  class RegisterableFrameCountObserver
+      : public RegisterableCallback<FrameCountObserver> {
+    virtual void FrameCountUpdated(FrameType frame_type,
+                                   uint32_t frame_count,
+                                   const unsigned int ssrc) {
+      CriticalSectionScoped cs(critsect_.get());
+      if (callback_)
+        callback_->FrameCountUpdated(frame_type, frame_count, ssrc);
+    }
+  } send_frame_count_observer_;
+
   int32_t channel_id_;
   int32_t engine_id_;
   uint32_t number_of_cores_;