1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/download_rate_monitor.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::Mock;
namespace media {
class DownloadRateMonitorTest : public ::testing::Test {
public:
DownloadRateMonitorTest() {
monitor_.set_total_bytes(kMediaSizeInBytes);
}
virtual ~DownloadRateMonitorTest() { }
MOCK_METHOD0(CanPlayThrough, void());
protected:
static const int kMediaSizeInBytes = 20 * 1024 * 1024;
// Simulates downloading of the media file. Packets are timed evenly in
// |ms_between_packets| intervals, starting at |starting_time|, which is
// number of seconds since unix epoch (Jan 1, 1970).
// Returns the number of bytes buffered in the media file after the
// network activity.
int SimulateNetwork(double starting_time,
int starting_bytes,
int bytes_per_packet,
int ms_between_packets,
int number_of_packets) {
int bytes_buffered = starting_bytes;
base::Time packet_time = base::Time::FromDoubleT(starting_time);
monitor_.SetNetworkActivity(true);
// Loop executes (number_of_packets + 1) times because a packet needs a
// starting and end point.
for (int i = 0; i < number_of_packets + 1; ++i) {
monitor_.SetBufferedBytes(bytes_buffered, packet_time);
packet_time += base::TimeDelta::FromMilliseconds(ms_between_packets);
bytes_buffered += bytes_per_packet;
}
monitor_.SetNetworkActivity(false);
return bytes_buffered;
}
void StartMonitor(int bitrate) {
StartMonitor(bitrate, false, false);
}
void StartMonitor(int bitrate, bool streaming, bool local_source) {
monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
base::Unretained(this)), bitrate, streaming, local_source);
}
DownloadRateMonitor monitor_;
private:
DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitorTest);
};
TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate) {
static const int media_bitrate = 1024 * 1024 * 8;
// Simulate downloading at double the media's bitrate.
StartMonitor(media_bitrate);
EXPECT_CALL(*this, CanPlayThrough());
SimulateNetwork(1, 0, 2 * media_bitrate / 8, 1000, 10);
}
// If the user pauses and the pipeline stops downloading data, make sure the
// DownloadRateMonitor understands that the download is not stalling.
TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_Pause) {
static const int media_bitrate = 1024 * 1024 * 8;
static const int download_byte_rate = 1.1 * media_bitrate / 8;
// Start downloading faster than the media's bitrate.
StartMonitor(media_bitrate);
EXPECT_CALL(*this, CanPlayThrough());
int buffered = SimulateNetwork(1, 0, download_byte_rate, 1000, 2);
// Then "pause" for 3 minutes and continue downloading at same rate.
SimulateNetwork(60 * 3, buffered, download_byte_rate, 1000, 4);
}
TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekForward) {
static const int media_bitrate = 1024 * 1024 * 8;
static const int download_byte_rate = 1.1 * media_bitrate / 8;
// Start downloading faster than the media's bitrate.
EXPECT_CALL(*this, CanPlayThrough());
StartMonitor(media_bitrate);
SimulateNetwork(1, 0, download_byte_rate, 1000, 2);
// Then seek forward mid-file and continue downloading at same rate.
SimulateNetwork(4, kMediaSizeInBytes / 2, download_byte_rate, 1000, 4);
}
TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekBackward) {
static const int media_bitrate = 1024 * 1024 * 8;
static const int download_byte_rate = 1.1 * media_bitrate / 8;
// Start downloading faster than the media's bitrate, in middle of file.
StartMonitor(media_bitrate);
SimulateNetwork(1, kMediaSizeInBytes / 2, download_byte_rate, 1000, 2);
// Then seek back to beginning and continue downloading at same rate.
EXPECT_CALL(*this, CanPlayThrough());
SimulateNetwork(4, 0, download_byte_rate, 1000, 4);
}
TEST_F(DownloadRateMonitorTest, DownloadRateLessThanBitrate) {
static const int media_bitrate = 1024 * 1024 * 8;
// Simulate downloading at half the media's bitrate.
EXPECT_CALL(*this, CanPlayThrough())
.Times(0);
StartMonitor(media_bitrate);
SimulateNetwork(1, 0, media_bitrate / 8 / 2, 1000, 10);
}
TEST_F(DownloadRateMonitorTest, MediaSourceIsLocal) {
static const int media_bitrate = 1024 * 1024 * 8;
// Simulate no data downloaded.
EXPECT_CALL(*this, CanPlayThrough());
StartMonitor(media_bitrate, false, true);
}
TEST_F(DownloadRateMonitorTest, MediaSourceIsStreaming) {
static const int media_bitrate = 1024 * 1024 * 8;
// Simulate downloading at the media's bitrate while streaming.
EXPECT_CALL(*this, CanPlayThrough());
StartMonitor(media_bitrate, true, false);
SimulateNetwork(1, 0, media_bitrate / 8, 1000, 10);
}
TEST_F(DownloadRateMonitorTest, VeryFastDownloadRate) {
static const int media_bitrate = 1024 * 1024 * 8;
// Simulate downloading half the video very quickly in one chunk.
StartMonitor(media_bitrate);
EXPECT_CALL(*this, CanPlayThrough());
SimulateNetwork(1, 0, kMediaSizeInBytes / 2, 10, 1);
}
TEST_F(DownloadRateMonitorTest, DownloadEntireVideo) {
static const int seconds_of_data = 20;
static const int media_bitrate = kMediaSizeInBytes * 8 / seconds_of_data;
// Simulate downloading entire video at half the bitrate of the video.
StartMonitor(media_bitrate);
EXPECT_CALL(*this, CanPlayThrough());
SimulateNetwork(1, 0, media_bitrate / 8 / 2, 1000, seconds_of_data * 2);
}
} // namespace media
|