Home | History | Annotate | Download | only in video_coding
      1 /*
      2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 #include "webrtc/modules/video_coding/media_optimization.h"
     13 #include "webrtc/system_wrappers/include/clock.h"
     14 
     15 namespace webrtc {
     16 namespace media_optimization {
     17 
     18 class TestMediaOptimization : public ::testing::Test {
     19  protected:
     20   enum {
     21     kSampleRate = 90000  // RTP timestamps per second.
     22   };
     23 
     24   // Note: simulated clock starts at 1 seconds, since parts of webrtc use 0 as
     25   // a special case (e.g. frame rate in media optimization).
     26   TestMediaOptimization()
     27       : clock_(1000),
     28         media_opt_(&clock_),
     29         frame_time_ms_(33),
     30         next_timestamp_(0) {}
     31 
     32   // This method mimics what happens in VideoSender::AddVideoFrame.
     33   void AddFrameAndAdvanceTime(uint32_t bitrate_bps, bool expect_frame_drop) {
     34     bool frame_dropped = media_opt_.DropFrame();
     35     EXPECT_EQ(expect_frame_drop, frame_dropped);
     36     if (!frame_dropped) {
     37       size_t bytes_per_frame = bitrate_bps * frame_time_ms_ / (8 * 1000);
     38       EncodedImage encoded_image;
     39       encoded_image._length = bytes_per_frame;
     40       encoded_image._timeStamp = next_timestamp_;
     41       encoded_image._frameType = kVideoFrameKey;
     42       ASSERT_EQ(VCM_OK, media_opt_.UpdateWithEncodedData(encoded_image));
     43     }
     44     next_timestamp_ += frame_time_ms_ * kSampleRate / 1000;
     45     clock_.AdvanceTimeMilliseconds(frame_time_ms_);
     46   }
     47 
     48   SimulatedClock clock_;
     49   MediaOptimization media_opt_;
     50   int frame_time_ms_;
     51   uint32_t next_timestamp_;
     52 };
     53 
     54 TEST_F(TestMediaOptimization, VerifyMuting) {
     55   // Enable video suspension with these limits.
     56   // Suspend the video when the rate is below 50 kbps and resume when it gets
     57   // above 50 + 10 kbps again.
     58   const uint32_t kThresholdBps = 50000;
     59   const uint32_t kWindowBps = 10000;
     60   media_opt_.SuspendBelowMinBitrate(kThresholdBps, kWindowBps);
     61 
     62   // The video should not be suspended from the start.
     63   EXPECT_FALSE(media_opt_.IsVideoSuspended());
     64 
     65   uint32_t target_bitrate_kbps = 100;
     66   media_opt_.SetTargetRates(target_bitrate_kbps * 1000,
     67                             0,    // Lossrate.
     68                             100,  // RTT in ms.
     69                             nullptr, nullptr);
     70   media_opt_.EnableFrameDropper(true);
     71   for (int time = 0; time < 2000; time += frame_time_ms_) {
     72     ASSERT_NO_FATAL_FAILURE(AddFrameAndAdvanceTime(target_bitrate_kbps, false));
     73   }
     74 
     75   // Set the target rate below the limit for muting.
     76   media_opt_.SetTargetRates(kThresholdBps - 1000,
     77                             0,    // Lossrate.
     78                             100,  // RTT in ms.
     79                             nullptr, nullptr);
     80   // Expect the muter to engage immediately and stay muted.
     81   // Test during 2 seconds.
     82   for (int time = 0; time < 2000; time += frame_time_ms_) {
     83     EXPECT_TRUE(media_opt_.IsVideoSuspended());
     84     ASSERT_NO_FATAL_FAILURE(AddFrameAndAdvanceTime(target_bitrate_kbps, true));
     85   }
     86 
     87   // Set the target above the limit for muting, but not above the
     88   // limit + window.
     89   media_opt_.SetTargetRates(kThresholdBps + 1000,
     90                             0,    // Lossrate.
     91                             100,  // RTT in ms.
     92                             nullptr, nullptr);
     93   // Expect the muter to stay muted.
     94   // Test during 2 seconds.
     95   for (int time = 0; time < 2000; time += frame_time_ms_) {
     96     EXPECT_TRUE(media_opt_.IsVideoSuspended());
     97     ASSERT_NO_FATAL_FAILURE(AddFrameAndAdvanceTime(target_bitrate_kbps, true));
     98   }
     99 
    100   // Set the target above limit + window.
    101   media_opt_.SetTargetRates(kThresholdBps + kWindowBps + 1000,
    102                             0,    // Lossrate.
    103                             100,  // RTT in ms.
    104                             nullptr, nullptr);
    105   // Expect the muter to disengage immediately.
    106   // Test during 2 seconds.
    107   for (int time = 0; time < 2000; time += frame_time_ms_) {
    108     EXPECT_FALSE(media_opt_.IsVideoSuspended());
    109     ASSERT_NO_FATAL_FAILURE(
    110         AddFrameAndAdvanceTime((kThresholdBps + kWindowBps) / 1000, false));
    111   }
    112 }
    113 
    114 TEST_F(TestMediaOptimization, ProtectsUsingFecBitrateAboveCodecMax) {
    115   static const int kCodecBitrateBps = 100000;
    116   static const int kMaxBitrateBps = 130000;
    117 
    118   class ProtectionCallback : public VCMProtectionCallback {
    119     int ProtectionRequest(const FecProtectionParams* delta_params,
    120                           const FecProtectionParams* key_params,
    121                           uint32_t* sent_video_rate_bps,
    122                           uint32_t* sent_nack_rate_bps,
    123                           uint32_t* sent_fec_rate_bps) override {
    124       *sent_video_rate_bps = kCodecBitrateBps;
    125       *sent_nack_rate_bps = 0;
    126       *sent_fec_rate_bps = fec_rate_bps_;
    127       return 0;
    128     }
    129 
    130    public:
    131     uint32_t fec_rate_bps_;
    132   } protection_callback;
    133 
    134   media_opt_.SetProtectionMethod(kFec);
    135   media_opt_.SetEncodingData(kVideoCodecVP8, kCodecBitrateBps, kCodecBitrateBps,
    136                              640, 480, 30, 1, 1000);
    137 
    138   // Using 10% of codec bitrate for FEC, should still be able to use all of it.
    139   protection_callback.fec_rate_bps_ = kCodecBitrateBps / 10;
    140   uint32_t target_bitrate = media_opt_.SetTargetRates(
    141       kMaxBitrateBps, 0, 0, &protection_callback, nullptr);
    142 
    143   EXPECT_EQ(kCodecBitrateBps, static_cast<int>(target_bitrate));
    144 
    145   // Using as much for codec bitrate as fec rate, new target rate should share
    146   // both equally, but only be half of max (since that ceiling should be hit).
    147   protection_callback.fec_rate_bps_ = kCodecBitrateBps;
    148   target_bitrate = media_opt_.SetTargetRates(kMaxBitrateBps, 128, 100,
    149                                              &protection_callback, nullptr);
    150   EXPECT_EQ(kMaxBitrateBps / 2, static_cast<int>(target_bitrate));
    151 }
    152 
    153 }  // namespace media_optimization
    154 }  // namespace webrtc
    155