Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / video / call_perf_tests.cc
index 0637ec3..62d2adc 100644 (file)
 #include "testing/gtest/include/gtest/gtest.h"
 
 #include "webrtc/call.h"
-#include "webrtc/modules/remote_bitrate_estimator/include/rtp_to_ntp.h"
+#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
 #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
+#include "webrtc/system_wrappers/interface/rtp_to_ntp.h"
 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
+#include "webrtc/system_wrappers/interface/thread_annotations.h"
+#include "webrtc/test/call_test.h"
 #include "webrtc/test/direct_transport.h"
+#include "webrtc/test/encoder_settings.h"
 #include "webrtc/test/fake_audio_device.h"
 #include "webrtc/test/fake_decoder.h"
 #include "webrtc/test/fake_encoder.h"
 
 namespace webrtc {
 
-static unsigned int kLongTimeoutMs = 120 * 1000;
-static const uint32_t kSendSsrc = 0x654321;
-static const uint32_t kReceiverLocalSsrc = 0x123456;
-static const uint8_t kSendPayloadType = 125;
+class CallPerfTest : public test::CallTest {
+ protected:
+  void TestMinTransmitBitrate(bool pad_to_min_bitrate);
 
-class CallPerfTest : public ::testing::Test {
+  void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
+                          int threshold_ms,
+                          int start_time_ms,
+                          int run_time_ms);
 };
 
 class SyncRtcpObserver : public test::RtpRtcpObserver {
  public:
   explicit SyncRtcpObserver(const FakeNetworkPipe::Config& config)
-      : test::RtpRtcpObserver(kLongTimeoutMs, config),
-        critical_section_(CriticalSectionWrapper::CreateCriticalSection()) {}
+      : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs, config),
+        crit_(CriticalSectionWrapper::CreateCriticalSection()) {}
 
   virtual Action OnSendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
     RTCPUtility::RTCPParserV2 parser(packet, length, true);
@@ -62,7 +68,7 @@ class SyncRtcpObserver : public test::RtpRtcpObserver {
          packet_type = parser.Iterate()) {
       if (packet_type == RTCPUtility::kRtcpSrCode) {
         const RTCPUtility::RTCPPacket& packet = parser.Packet();
-        synchronization::RtcpMeasurement ntp_rtp_pair(
+        RtcpMeasurement ntp_rtp_pair(
             packet.SR.NTPMostSignificant,
             packet.SR.NTPLeastSignificant,
             packet.SR.RTPTimestamp);
@@ -73,22 +79,22 @@ class SyncRtcpObserver : public test::RtpRtcpObserver {
   }
 
   int64_t RtpTimestampToNtp(uint32_t timestamp) const {
-    CriticalSectionScoped cs(critical_section_.get());
+    CriticalSectionScoped lock(crit_.get());
     int64_t timestamp_in_ms = -1;
     if (ntp_rtp_pairs_.size() == 2) {
       // TODO(stefan): We can't EXPECT_TRUE on this call due to a bug in the
       // RTCP sender where it sends RTCP SR before any RTP packets, which leads
       // to a bogus NTP/RTP mapping.
-      synchronization::RtpToNtpMs(timestamp, ntp_rtp_pairs_, &timestamp_in_ms);
+      RtpToNtpMs(timestamp, ntp_rtp_pairs_, &timestamp_in_ms);
       return timestamp_in_ms;
     }
     return -1;
   }
 
  private:
-  void StoreNtpRtpPair(synchronization::RtcpMeasurement ntp_rtp_pair) {
-    CriticalSectionScoped cs(critical_section_.get());
-    for (synchronization::RtcpList::iterator it = ntp_rtp_pairs_.begin();
+  void StoreNtpRtpPair(RtcpMeasurement ntp_rtp_pair) {
+    CriticalSectionScoped lock(crit_.get());
+    for (RtcpList::iterator it = ntp_rtp_pairs_.begin();
          it != ntp_rtp_pairs_.end();
          ++it) {
       if (ntp_rtp_pair.ntp_secs == it->ntp_secs &&
@@ -105,8 +111,8 @@ class SyncRtcpObserver : public test::RtpRtcpObserver {
     ntp_rtp_pairs_.push_front(ntp_rtp_pair);
   }
 
-  scoped_ptr<CriticalSectionWrapper> critical_section_;
-  synchronization::RtcpList ntp_rtp_pairs_;
+  const scoped_ptr<CriticalSectionWrapper> crit_;
+  RtcpList ntp_rtp_pairs_ GUARDED_BY(crit_);
 };
 
 class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
@@ -144,14 +150,18 @@ class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
     int64_t stream_offset = latest_audio_ntp - latest_video_ntp;
     std::stringstream ss;
     ss << stream_offset;
-    webrtc::test::PrintResult(
-        "stream_offset", "", "synchronization", ss.str(), "ms", false);
+    webrtc::test::PrintResult("stream_offset",
+                              "",
+                              "synchronization",
+                              ss.str(),
+                              "ms",
+                              false);
     int64_t time_since_creation = now_ms - creation_time_ms_;
     // During the first couple of seconds audio and video can falsely be
     // estimated as being synchronized. We don't want to trigger on those.
     if (time_since_creation < kStartupTimeMs)
       return;
-    if (abs(latest_audio_ntp - latest_video_ntp) < kInSyncThresholdMs) {
+    if (std::abs(latest_audio_ntp - latest_video_ntp) < kInSyncThresholdMs) {
       if (first_time_in_sync_ == -1) {
         first_time_in_sync_ = now_ms;
         webrtc::test::PrintResult("sync_convergence_time",
@@ -167,7 +177,7 @@ class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
   }
 
  private:
-  Clock* clock_;
+  Clock* const clock_;
   int voe_channel_;
   VoEVideoSync* voe_sync_;
   SyncRtcpObserver* audio_observer_;
@@ -176,6 +186,31 @@ class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
 };
 
 TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
+  class AudioPacketReceiver : public PacketReceiver {
+   public:
+    AudioPacketReceiver(int channel, VoENetwork* voe_network)
+        : channel_(channel),
+          voe_network_(voe_network),
+          parser_(RtpHeaderParser::Create()) {}
+    virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
+                                         size_t length) OVERRIDE {
+      int ret;
+      if (parser_->IsRtcp(packet, static_cast<int>(length))) {
+        ret = voe_network_->ReceivedRTCPPacket(
+            channel_, packet, static_cast<unsigned int>(length));
+      } else {
+        ret = voe_network_->ReceivedRTPPacket(
+            channel_, packet, static_cast<unsigned int>(length), PacketTime());
+      }
+      return ret == 0 ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
+    }
+
+   private:
+    int channel_;
+    VoENetwork* voe_network_;
+    scoped_ptr<RtpHeaderParser> parser_;
+  };
+
   VoiceEngine* voice_engine = VoiceEngine::Create();
   VoEBase* voe_base = VoEBase::GetInterface(voice_engine);
   VoECodec* voe_codec = VoECodec::GetInterface(voice_engine);
@@ -192,85 +227,41 @@ TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
   FakeNetworkPipe::Config net_config;
   net_config.queue_delay_ms = 500;
   SyncRtcpObserver audio_observer(net_config);
-  VideoRtcpAndSyncObserver observer(
-      Clock::GetRealTimeClock(), channel, voe_sync, &audio_observer);
+  VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(),
+                                    channel,
+                                    voe_sync,
+                                    &audio_observer);
 
   Call::Config receiver_config(observer.ReceiveTransport());
   receiver_config.voice_engine = voice_engine;
-  scoped_ptr<Call> sender_call(
-      Call::Create(Call::Config(observer.SendTransport())));
-  scoped_ptr<Call> receiver_call(Call::Create(receiver_config));
+  CreateCalls(Call::Config(observer.SendTransport()), receiver_config);
+
   CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000};
   EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac));
 
-  class VoicePacketReceiver : public PacketReceiver {
-   public:
-    VoicePacketReceiver(int channel, VoENetwork* voe_network)
-        : channel_(channel),
-          voe_network_(voe_network),
-          parser_(RtpHeaderParser::Create()) {}
-    virtual bool DeliverPacket(const uint8_t* packet, size_t length) {
-      int ret;
-      if (parser_->IsRtcp(packet, static_cast<int>(length))) {
-        ret = voe_network_->ReceivedRTCPPacket(
-            channel_, packet, static_cast<unsigned int>(length));
-      } else {
-        ret = voe_network_->ReceivedRTPPacket(
-            channel_, packet, static_cast<unsigned int>(length));
-      }
-      return ret == 0;
-    }
-
-   private:
-    int channel_;
-    VoENetwork* voe_network_;
-    scoped_ptr<RtpHeaderParser> parser_;
-  } voe_packet_receiver(channel, voe_network);
-
+  AudioPacketReceiver voe_packet_receiver(channel, voe_network);
   audio_observer.SetReceivers(&voe_packet_receiver, &voe_packet_receiver);
 
   internal::TransportAdapter transport_adapter(audio_observer.SendTransport());
+  transport_adapter.Enable();
   EXPECT_EQ(0,
             voe_network->RegisterExternalTransport(channel, transport_adapter));
 
-  observer.SetReceivers(receiver_call->Receiver(), sender_call->Receiver());
+  observer.SetReceivers(receiver_call_->Receiver(), sender_call_->Receiver());
 
-  test::FakeEncoder fake_encoder(Clock::GetRealTimeClock());
   test::FakeDecoder fake_decoder;
 
-  VideoSendStream::Config send_config = sender_call->GetDefaultSendConfig();
-  send_config.rtp.ssrcs.push_back(kSendSsrc);
-  send_config.encoder = &fake_encoder;
-  send_config.internal_source = false;
-  test::FakeEncoder::SetCodecSettings(&send_config.codec, 1);
-  send_config.codec.plType = kSendPayloadType;
-
-  VideoReceiveStream::Config receive_config =
-      receiver_call->GetDefaultReceiveConfig();
-  receive_config.codecs.clear();
-  receive_config.codecs.push_back(send_config.codec);
-  ExternalVideoDecoder decoder;
-  decoder.decoder = &fake_decoder;
-  decoder.payload_type = send_config.codec.plType;
-  receive_config.external_decoders.push_back(decoder);
-  receive_config.rtp.remote_ssrc = send_config.rtp.ssrcs[0];
-  receive_config.rtp.local_ssrc = kReceiverLocalSsrc;
-  receive_config.renderer = &observer;
-  receive_config.audio_channel_id = channel;
-
-  VideoSendStream* send_stream =
-      sender_call->CreateVideoSendStream(send_config);
-  VideoReceiveStream* receive_stream =
-      receiver_call->CreateVideoReceiveStream(receive_config);
-  scoped_ptr<test::FrameGeneratorCapturer> capturer(
-      test::FrameGeneratorCapturer::Create(send_stream->Input(),
-                                           send_config.codec.width,
-                                           send_config.codec.height,
-                                           30,
-                                           Clock::GetRealTimeClock()));
-  receive_stream->StartReceiving();
-  send_stream->StartSending();
-  capturer->Start();
+  CreateSendConfig(1);
+  CreateMatchingReceiveConfigs();
+
+  receive_configs_[0].renderer = &observer;
+  receive_configs_[0].audio_channel_id = channel;
+
+  CreateStreams();
+
+  CreateFrameGeneratorCapturer();
+
+  Start();
 
   fake_audio_device.Start();
   EXPECT_EQ(0, voe_base->StartPlayout(channel));
@@ -285,9 +276,7 @@ TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
   EXPECT_EQ(0, voe_base->StopPlayout(channel));
   fake_audio_device.Stop();
 
-  capturer->Stop();
-  send_stream->StopSending();
-  receive_stream->StopReceiving();
+  Stop();
   observer.StopSending();
   audio_observer.StopSending();
 
@@ -296,8 +285,275 @@ TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
   voe_codec->Release();
   voe_network->Release();
   voe_sync->Release();
-  sender_call->DestroyVideoSendStream(send_stream);
-  receiver_call->DestroyVideoReceiveStream(receive_stream);
+
+  DestroyStreams();
+
   VoiceEngine::Delete(voice_engine);
 }
+
+void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
+                                      int threshold_ms,
+                                      int start_time_ms,
+                                      int run_time_ms) {
+  class CaptureNtpTimeObserver : public test::EndToEndTest,
+                                 public VideoRenderer {
+   public:
+    CaptureNtpTimeObserver(const FakeNetworkPipe::Config& config,
+                           int threshold_ms,
+                           int start_time_ms,
+                           int run_time_ms)
+        : EndToEndTest(kLongTimeoutMs, config),
+          clock_(Clock::GetRealTimeClock()),
+          threshold_ms_(threshold_ms),
+          start_time_ms_(start_time_ms),
+          run_time_ms_(run_time_ms),
+          creation_time_ms_(clock_->TimeInMilliseconds()),
+          capturer_(NULL),
+          rtp_start_timestamp_set_(false),
+          rtp_start_timestamp_(0) {}
+
+   private:
+    virtual void RenderFrame(const I420VideoFrame& video_frame,
+                             int time_to_render_ms) OVERRIDE {
+      if (video_frame.ntp_time_ms() <= 0) {
+        // Haven't got enough RTCP SR in order to calculate the capture ntp
+        // time.
+        return;
+      }
+
+      int64_t now_ms = clock_->TimeInMilliseconds();
+      int64_t time_since_creation = now_ms - creation_time_ms_;
+      if (time_since_creation < start_time_ms_) {
+        // Wait for |start_time_ms_| before start measuring.
+        return;
+      }
+
+      if (time_since_creation > run_time_ms_) {
+        observation_complete_->Set();
+      }
+
+      FrameCaptureTimeList::iterator iter =
+          capture_time_list_.find(video_frame.timestamp());
+      EXPECT_TRUE(iter != capture_time_list_.end());
+
+      // The real capture time has been wrapped to uint32_t before converted
+      // to rtp timestamp in the sender side. So here we convert the estimated
+      // capture time to a uint32_t 90k timestamp also for comparing.
+      uint32_t estimated_capture_timestamp =
+          90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
+      uint32_t real_capture_timestamp = iter->second;
+      int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
+      time_offset_ms = time_offset_ms / 90;
+      std::stringstream ss;
+      ss << time_offset_ms;
+
+      webrtc::test::PrintResult(
+          "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true);
+      EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
+    }
+
+    virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
+      RTPHeader header;
+      EXPECT_TRUE(parser_->Parse(packet, length, &header));
+
+      if (!rtp_start_timestamp_set_) {
+        // Calculate the rtp timestamp offset in order to calculate the real
+        // capture time.
+        uint32_t first_capture_timestamp =
+            90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
+        rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
+        rtp_start_timestamp_set_ = true;
+      }
+
+      uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
+      capture_time_list_.insert(
+          capture_time_list_.end(),
+          std::make_pair(header.timestamp, capture_timestamp));
+      return SEND_PACKET;
+    }
+
+    virtual void OnFrameGeneratorCapturerCreated(
+        test::FrameGeneratorCapturer* frame_generator_capturer) OVERRIDE {
+      capturer_ = frame_generator_capturer;
+    }
+
+    virtual void ModifyConfigs(
+        VideoSendStream::Config* send_config,
+        std::vector<VideoReceiveStream::Config>* receive_configs,
+        std::vector<VideoStream>* video_streams) OVERRIDE {
+      (*receive_configs)[0].renderer = this;
+      // Enable the receiver side rtt calculation.
+      (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
+    }
+
+    virtual void PerformTest() OVERRIDE {
+      EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for "
+                                           "estimated capture NTP time to be "
+                                           "within bounds.";
+    }
+
+    Clock* clock_;
+    int threshold_ms_;
+    int start_time_ms_;
+    int run_time_ms_;
+    int64_t creation_time_ms_;
+    test::FrameGeneratorCapturer* capturer_;
+    bool rtp_start_timestamp_set_;
+    uint32_t rtp_start_timestamp_;
+    typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
+    FrameCaptureTimeList capture_time_list_;
+  } test(net_config, threshold_ms, start_time_ms, run_time_ms);
+
+  RunBaseTest(&test);
+}
+
+TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
+  FakeNetworkPipe::Config net_config;
+  net_config.queue_delay_ms = 100;
+  // TODO(wu): lower the threshold as the calculation/estimatation becomes more
+  // accurate.
+  const int kThresholdMs = 100;
+  const int kStartTimeMs = 10000;
+  const int kRunTimeMs = 20000;
+  TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
+}
+
+TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
+  FakeNetworkPipe::Config net_config;
+  net_config.queue_delay_ms = 100;
+  net_config.delay_standard_deviation_ms = 10;
+  // TODO(wu): lower the threshold as the calculation/estimatation becomes more
+  // accurate.
+  const int kThresholdMs = 100;
+  const int kStartTimeMs = 10000;
+  const int kRunTimeMs = 20000;
+  TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
+}
+
+TEST_F(CallPerfTest, RegisterCpuOveruseObserver) {
+  // Verifies that either a normal or overuse callback is triggered.
+  class OveruseCallbackObserver : public test::SendTest,
+                                  public webrtc::OveruseCallback {
+   public:
+    OveruseCallbackObserver() : SendTest(kLongTimeoutMs) {}
+
+    virtual void OnOveruse() OVERRIDE {
+      observation_complete_->Set();
+    }
+
+    virtual void OnNormalUse() OVERRIDE {
+      observation_complete_->Set();
+    }
+
+    virtual Call::Config GetSenderCallConfig() OVERRIDE {
+      Call::Config config(SendTransport());
+      config.overuse_callback = this;
+      return config;
+    }
+
+    virtual void PerformTest() OVERRIDE {
+      EXPECT_EQ(kEventSignaled, Wait())
+          << "Timed out before receiving an overuse callback.";
+    }
+  } test;
+
+  RunBaseTest(&test);
+}
+
+void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
+  static const int kMaxEncodeBitrateKbps = 30;
+  static const int kMinTransmitBitrateBps = 150000;
+  static const int kMinAcceptableTransmitBitrate = 130;
+  static const int kMaxAcceptableTransmitBitrate = 170;
+  static const int kNumBitrateObservationsInRange = 100;
+  class BitrateObserver : public test::EndToEndTest, public PacketReceiver {
+   public:
+    explicit BitrateObserver(bool using_min_transmit_bitrate)
+        : EndToEndTest(kLongTimeoutMs),
+          send_stream_(NULL),
+          send_transport_receiver_(NULL),
+          pad_to_min_bitrate_(using_min_transmit_bitrate),
+          num_bitrate_observations_in_range_(0) {}
+
+   private:
+    virtual void SetReceivers(PacketReceiver* send_transport_receiver,
+                              PacketReceiver* receive_transport_receiver)
+        OVERRIDE {
+      send_transport_receiver_ = send_transport_receiver;
+      test::RtpRtcpObserver::SetReceivers(this, receive_transport_receiver);
+    }
+
+    virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
+                                         size_t length) OVERRIDE {
+      VideoSendStream::Stats stats = send_stream_->GetStats();
+      if (stats.substreams.size() > 0) {
+        assert(stats.substreams.size() == 1);
+        int bitrate_kbps = stats.substreams.begin()->second.bitrate_bps / 1000;
+        if (bitrate_kbps > 0) {
+          test::PrintResult(
+              "bitrate_stats_",
+              (pad_to_min_bitrate_ ? "min_transmit_bitrate"
+                                   : "without_min_transmit_bitrate"),
+              "bitrate_kbps",
+              static_cast<size_t>(bitrate_kbps),
+              "kbps",
+              false);
+          if (pad_to_min_bitrate_) {
+            if (bitrate_kbps > kMinAcceptableTransmitBitrate &&
+                bitrate_kbps < kMaxAcceptableTransmitBitrate) {
+              ++num_bitrate_observations_in_range_;
+            }
+          } else {
+            // Expect bitrate stats to roughly match the max encode bitrate.
+            if (bitrate_kbps > kMaxEncodeBitrateKbps - 5 &&
+                bitrate_kbps < kMaxEncodeBitrateKbps + 5) {
+              ++num_bitrate_observations_in_range_;
+            }
+          }
+          if (num_bitrate_observations_in_range_ ==
+              kNumBitrateObservationsInRange)
+            observation_complete_->Set();
+        }
+      }
+      return send_transport_receiver_->DeliverPacket(packet, length);
+    }
+
+    virtual void OnStreamsCreated(
+        VideoSendStream* send_stream,
+        const std::vector<VideoReceiveStream*>& receive_streams) OVERRIDE {
+      send_stream_ = send_stream;
+    }
+
+    virtual void ModifyConfigs(
+        VideoSendStream::Config* send_config,
+        std::vector<VideoReceiveStream::Config>* receive_configs,
+        std::vector<VideoStream>* video_streams) OVERRIDE {
+      if (pad_to_min_bitrate_) {
+        send_config->rtp.min_transmit_bitrate_bps = kMinTransmitBitrateBps;
+      } else {
+        assert(send_config->rtp.min_transmit_bitrate_bps == 0);
+      }
+    }
+
+    virtual void PerformTest() OVERRIDE {
+      EXPECT_EQ(kEventSignaled, Wait())
+          << "Timeout while waiting for send-bitrate stats.";
+    }
+
+    VideoSendStream* send_stream_;
+    PacketReceiver* send_transport_receiver_;
+    const bool pad_to_min_bitrate_;
+    int num_bitrate_observations_in_range_;
+  } test(pad_to_min_bitrate);
+
+  fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
+  RunBaseTest(&test);
+}
+
+TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
+
+TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
+  TestMinTransmitBitrate(false);
+}
+
 }  // namespace webrtc