1 /*
2  *  Copyright (c) 2014 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 #ifndef WEBRTC_VIDEO_RAMPUP_TESTS_H_
12 #define WEBRTC_VIDEO_RAMPUP_TESTS_H_
13 
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 #include "webrtc/base/scoped_ptr.h"
19 #include "webrtc/call.h"
20 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
21 #include "webrtc/system_wrappers/interface/event_wrapper.h"
22 #include "webrtc/test/call_test.h"
23 #include "webrtc/video/transport_adapter.h"
24 
25 namespace webrtc {
26 
27 static const int kTransmissionTimeOffsetExtensionId = 6;
28 static const int kAbsSendTimeExtensionId = 7;
29 static const unsigned int kSingleStreamTargetBps = 1000000;
30 
31 class Clock;
32 class CriticalSectionWrapper;
33 class ReceiveStatistics;
34 class RtpHeaderParser;
35 class RTPPayloadRegistry;
36 class RtpRtcp;
37 
38 class StreamObserver : public newapi::Transport, public RemoteBitrateObserver {
39  public:
40   typedef std::map<uint32_t, int> BytesSentMap;
41   typedef std::map<uint32_t, uint32_t> SsrcMap;
42   StreamObserver(const SsrcMap& rtx_media_ssrcs,
43                  newapi::Transport* feedback_transport,
44                  Clock* clock,
45                  RemoteBitrateEstimatorFactory* rbe_factory,
46                  RateControlType control_type);
47 
48   void set_expected_bitrate_bps(unsigned int expected_bitrate_bps);
49 
50   void set_start_bitrate_bps(unsigned int start_bitrate_bps);
51 
52   void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
53                                unsigned int bitrate) override;
54 
55   bool SendRtp(const uint8_t* packet, size_t length) override;
56 
57   bool SendRtcp(const uint8_t* packet, size_t length) override;
58 
59   EventTypeWrapper Wait();
60 
61  private:
62   void ReportResult(const std::string& measurement,
63                     size_t value,
64                     const std::string& units);
65   void TriggerTestDone() EXCLUSIVE_LOCKS_REQUIRED(crit_);
66 
67   Clock* const clock_;
68   const rtc::scoped_ptr<EventWrapper> test_done_;
69   const rtc::scoped_ptr<RtpHeaderParser> rtp_parser_;
70   rtc::scoped_ptr<RtpRtcp> rtp_rtcp_;
71   internal::TransportAdapter feedback_transport_;
72   const rtc::scoped_ptr<ReceiveStatistics> receive_stats_;
73   const rtc::scoped_ptr<RTPPayloadRegistry> payload_registry_;
74   rtc::scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
75 
76   const rtc::scoped_ptr<CriticalSectionWrapper> crit_;
77   unsigned int expected_bitrate_bps_ GUARDED_BY(crit_);
78   unsigned int start_bitrate_bps_ GUARDED_BY(crit_);
79   SsrcMap rtx_media_ssrcs_ GUARDED_BY(crit_);
80   size_t total_sent_ GUARDED_BY(crit_);
81   size_t padding_sent_ GUARDED_BY(crit_);
82   size_t rtx_media_sent_ GUARDED_BY(crit_);
83   int total_packets_sent_ GUARDED_BY(crit_);
84   int padding_packets_sent_ GUARDED_BY(crit_);
85   int rtx_media_packets_sent_ GUARDED_BY(crit_);
86   int64_t test_start_ms_ GUARDED_BY(crit_);
87   int64_t ramp_up_finished_ms_ GUARDED_BY(crit_);
88 };
89 
90 class LowRateStreamObserver : public test::DirectTransport,
91                               public RemoteBitrateObserver,
92                               public PacketReceiver {
93  public:
94   LowRateStreamObserver(newapi::Transport* feedback_transport,
95                         Clock* clock,
96                         size_t number_of_streams,
97                         bool rtx_used);
98 
99   virtual void SetSendStream(VideoSendStream* send_stream);
100 
101   virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
102                                        unsigned int bitrate);
103 
104   bool SendRtp(const uint8_t* data, size_t length) override;
105 
106   DeliveryStatus DeliverPacket(const uint8_t* packet, size_t length) override;
107 
108   bool SendRtcp(const uint8_t* packet, size_t length) override;
109 
110   // Produces a string similar to "1stream_nortx", depending on the values of
111   // number_of_streams_ and rtx_used_;
112   std::string GetModifierString();
113 
114   // This method defines the state machine for the ramp up-down-up test.
115   void EvolveTestState(unsigned int bitrate_bps);
116 
117   EventTypeWrapper Wait();
118 
119  private:
120   static const unsigned int kHighBandwidthLimitBps = 80000;
121   static const unsigned int kExpectedHighBitrateBps = 60000;
122   static const unsigned int kLowBandwidthLimitBps = 20000;
123   static const unsigned int kExpectedLowBitrateBps = 20000;
124   enum TestStates { kFirstRampup, kLowRate, kSecondRampup };
125 
126   Clock* const clock_;
127   const size_t number_of_streams_;
128   const bool rtx_used_;
129   const rtc::scoped_ptr<EventWrapper> test_done_;
130   const rtc::scoped_ptr<RtpHeaderParser> rtp_parser_;
131   rtc::scoped_ptr<RtpRtcp> rtp_rtcp_;
132   internal::TransportAdapter feedback_transport_;
133   const rtc::scoped_ptr<ReceiveStatistics> receive_stats_;
134   rtc::scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
135 
136   rtc::scoped_ptr<CriticalSectionWrapper> crit_;
137   VideoSendStream* send_stream_ GUARDED_BY(crit_);
138   FakeNetworkPipe::Config forward_transport_config_ GUARDED_BY(crit_);
139   TestStates test_state_ GUARDED_BY(crit_);
140   int64_t state_start_ms_ GUARDED_BY(crit_);
141   int64_t interval_start_ms_ GUARDED_BY(crit_);
142   unsigned int last_remb_bps_ GUARDED_BY(crit_);
143   size_t sent_bytes_ GUARDED_BY(crit_);
144   size_t total_overuse_bytes_ GUARDED_BY(crit_);
145   bool suspended_in_stats_ GUARDED_BY(crit_);
146 };
147 
148 class RampUpTest : public test::CallTest {
149  protected:
150   void RunRampUpTest(bool rtx,
151                      size_t num_streams,
152                      unsigned int start_bitrate_bps,
153                      const std::string& extension_type);
154 
155   void RunRampUpDownUpTest(size_t number_of_streams, bool rtx);
156 };
157 }  // namespace webrtc
158 #endif  // WEBRTC_VIDEO_RAMPUP_TESTS_H_
159