Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / media / cast / sender / congestion_control_unittest.cc
blob5293b58c901e96af3c31a2a5a8caf80aa45b7684
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <stdint.h>
7 #include "base/bind.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "media/cast/cast_defines.h"
10 #include "media/cast/sender/congestion_control.h"
11 #include "media/cast/test/fake_single_thread_task_runner.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace media {
15 namespace cast {
17 static const int kMaxBitrateConfigured = 5000000;
18 static const int kMinBitrateConfigured = 500000;
19 static const int64 kFrameDelayMs = 33;
20 static const double kMaxFrameRate = 1000.0 / kFrameDelayMs;
21 static const int64 kStartMillisecond = INT64_C(12345678900000);
22 static const double kTargetEmptyBufferFraction = 0.9;
24 class CongestionControlTest : public ::testing::Test {
25 protected:
26 CongestionControlTest()
27 : task_runner_(new test::FakeSingleThreadTaskRunner(&testing_clock_)) {
28 testing_clock_.Advance(
29 base::TimeDelta::FromMilliseconds(kStartMillisecond));
30 congestion_control_.reset(NewAdaptiveCongestionControl(
31 &testing_clock_, kMaxBitrateConfigured, kMinBitrateConfigured,
32 kMaxFrameRate));
33 const int max_unacked_frames = 10;
34 const base::TimeDelta target_playout_delay =
35 (max_unacked_frames - 1) * base::TimeDelta::FromSeconds(1) /
36 kMaxFrameRate;
37 congestion_control_->UpdateTargetPlayoutDelay(target_playout_delay);
40 void AckFrame(uint32 frame_id) {
41 congestion_control_->AckFrame(frame_id, testing_clock_.NowTicks());
44 void Run(uint32 frames,
45 size_t frame_size,
46 base::TimeDelta rtt,
47 base::TimeDelta frame_delay,
48 base::TimeDelta ack_time) {
49 for (frame_id_ = 0; frame_id_ < frames; frame_id_++) {
50 congestion_control_->UpdateRtt(rtt);
51 congestion_control_->SendFrameToTransport(
52 frame_id_, frame_size, testing_clock_.NowTicks());
53 task_runner_->PostDelayedTask(FROM_HERE,
54 base::Bind(&CongestionControlTest::AckFrame,
55 base::Unretained(this),
56 frame_id_),
57 ack_time);
58 task_runner_->Sleep(frame_delay);
62 base::SimpleTestTickClock testing_clock_;
63 scoped_ptr<CongestionControl> congestion_control_;
64 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
65 uint32 frame_id_;
67 DISALLOW_COPY_AND_ASSIGN(CongestionControlTest);
70 // Tests that AdaptiveCongestionControl returns reasonable bitrates based on
71 // estimations of network bandwidth and how much is in-flight (i.e, using the
72 // "target buffer fill" model).
73 TEST_F(CongestionControlTest, SimpleRun) {
74 uint32 frame_size = 10000 * 8;
75 Run(500,
76 frame_size,
77 base::TimeDelta::FromMilliseconds(10),
78 base::TimeDelta::FromMilliseconds(kFrameDelayMs),
79 base::TimeDelta::FromMilliseconds(45));
80 // Empty the buffer.
81 task_runner_->Sleep(base::TimeDelta::FromMilliseconds(100));
83 // Use a soft maximum bitrate limit so large it will not bound the results of
84 // the underlying computations.
85 const int soft_max_bitrate = std::numeric_limits<int>::max();
87 uint32 safe_bitrate = frame_size * 1000 / kFrameDelayMs;
88 uint32 bitrate = congestion_control_->GetBitrate(
89 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(300),
90 base::TimeDelta::FromMilliseconds(300),
91 soft_max_bitrate);
92 EXPECT_NEAR(
93 safe_bitrate / kTargetEmptyBufferFraction, bitrate, safe_bitrate * 0.05);
95 bitrate = congestion_control_->GetBitrate(
96 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(200),
97 base::TimeDelta::FromMilliseconds(300),
98 soft_max_bitrate);
99 EXPECT_NEAR(safe_bitrate / kTargetEmptyBufferFraction * 2 / 3,
100 bitrate,
101 safe_bitrate * 0.05);
103 bitrate = congestion_control_->GetBitrate(
104 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(100),
105 base::TimeDelta::FromMilliseconds(300),
106 soft_max_bitrate);
107 EXPECT_NEAR(safe_bitrate / kTargetEmptyBufferFraction * 1 / 3,
108 bitrate,
109 safe_bitrate * 0.05);
111 // Add a large (100ms) frame.
112 congestion_control_->SendFrameToTransport(
113 frame_id_++, safe_bitrate * 100 / 1000, testing_clock_.NowTicks());
115 // Results should show that we have ~200ms to send.
116 bitrate = congestion_control_->GetBitrate(
117 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(300),
118 base::TimeDelta::FromMilliseconds(300),
119 soft_max_bitrate);
120 EXPECT_NEAR(safe_bitrate / kTargetEmptyBufferFraction * 2 / 3,
121 bitrate,
122 safe_bitrate * 0.05);
124 // Add another large (100ms) frame.
125 congestion_control_->SendFrameToTransport(
126 frame_id_++, safe_bitrate * 100 / 1000, testing_clock_.NowTicks());
128 // Results should show that we have ~100ms to send.
129 bitrate = congestion_control_->GetBitrate(
130 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(300),
131 base::TimeDelta::FromMilliseconds(300),
132 soft_max_bitrate);
133 EXPECT_NEAR(safe_bitrate / kTargetEmptyBufferFraction * 1 / 3,
134 bitrate,
135 safe_bitrate * 0.05);
138 } // namespace cast
139 } // namespace media