OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <cstdlib> | 5 #include <cstdlib> |
| 6 #include <vector> |
6 | 7 |
7 #include "base/bind.h" | 8 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
9 #include "base/synchronization/condition_variable.h" | 10 #include "base/synchronization/condition_variable.h" |
10 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
12 #include "media/cast/cast_config.h" | 13 #include "media/cast/cast_config.h" |
13 #include "media/cast/receiver/video_decoder.h" | 14 #include "media/cast/receiver/video_decoder.h" |
14 #include "media/cast/sender/vp8_encoder.h" | 15 #include "media/cast/sender/vp8_encoder.h" |
15 #include "media/cast/test/utility/default_config.h" | 16 #include "media/cast/test/utility/default_config.h" |
16 #include "media/cast/test/utility/standalone_cast_environment.h" | 17 #include "media/cast/test/utility/standalone_cast_environment.h" |
17 #include "media/cast/test/utility/video_utility.h" | 18 #include "media/cast/test/utility/video_utility.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 namespace cast { | 22 namespace cast { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 const int kWidth = 360; | 26 const int kStartingWidth = 360; |
26 const int kHeight = 240; | 27 const int kStartingHeight = 240; |
27 const int kFrameRate = 10; | 28 const int kFrameRate = 10; |
28 | 29 |
29 VideoSenderConfig GetVideoSenderConfigForTest() { | 30 VideoSenderConfig GetVideoSenderConfigForTest() { |
30 VideoSenderConfig config = GetDefaultVideoSenderConfig(); | 31 VideoSenderConfig config = GetDefaultVideoSenderConfig(); |
31 config.width = kWidth; | |
32 config.height = kHeight; | |
33 config.max_frame_rate = kFrameRate; | 32 config.max_frame_rate = kFrameRate; |
34 return config; | 33 return config; |
35 } | 34 } |
36 | 35 |
37 } // namespace | 36 } // namespace |
38 | 37 |
39 class VideoDecoderTest : public ::testing::TestWithParam<Codec> { | 38 class VideoDecoderTest : public ::testing::TestWithParam<Codec> { |
40 public: | 39 public: |
41 VideoDecoderTest() | 40 VideoDecoderTest() |
42 : cast_environment_(new StandaloneCastEnvironment()), | 41 : cast_environment_(new StandaloneCastEnvironment()), |
43 vp8_encoder_(GetVideoSenderConfigForTest()), | 42 vp8_encoder_(GetVideoSenderConfigForTest()), |
44 cond_(&lock_) { | 43 cond_(&lock_) { |
45 vp8_encoder_.Initialize(); | 44 vp8_encoder_.Initialize(); |
46 } | 45 } |
47 | 46 |
48 virtual ~VideoDecoderTest() { | 47 virtual ~VideoDecoderTest() { |
49 // Make sure all threads have stopped before the environment goes away. | 48 // Make sure all threads have stopped before the environment goes away. |
50 cast_environment_->Shutdown(); | 49 cast_environment_->Shutdown(); |
51 } | 50 } |
52 | 51 |
53 protected: | 52 protected: |
54 void SetUp() override { | 53 void SetUp() override { |
55 video_decoder_.reset(new VideoDecoder(cast_environment_, GetParam())); | 54 video_decoder_.reset(new VideoDecoder(cast_environment_, GetParam())); |
56 CHECK_EQ(STATUS_VIDEO_INITIALIZED, video_decoder_->InitializationResult()); | 55 CHECK_EQ(STATUS_VIDEO_INITIALIZED, video_decoder_->InitializationResult()); |
57 | 56 |
| 57 next_frame_size_ = gfx::Size(kStartingWidth, kStartingHeight); |
58 next_frame_timestamp_ = base::TimeDelta(); | 58 next_frame_timestamp_ = base::TimeDelta(); |
59 last_frame_id_ = 0; | 59 last_frame_id_ = 0; |
60 seen_a_decoded_frame_ = false; | 60 seen_a_decoded_frame_ = false; |
61 | 61 |
62 total_video_frames_feed_in_ = 0; | 62 total_video_frames_feed_in_ = 0; |
63 total_video_frames_decoded_ = 0; | 63 total_video_frames_decoded_ = 0; |
64 } | 64 } |
65 | 65 |
| 66 void SetNextFrameSize(const gfx::Size& size) { |
| 67 next_frame_size_ = size; |
| 68 } |
| 69 |
66 // Called from the unit test thread to create another EncodedFrame and push it | 70 // Called from the unit test thread to create another EncodedFrame and push it |
67 // into the decoding pipeline. | 71 // into the decoding pipeline. |
68 void FeedMoreVideo(int num_dropped_frames) { | 72 void FeedMoreVideo(int num_dropped_frames) { |
69 // Prepare a simulated EncodedFrame to feed into the VideoDecoder. | 73 // Prepare a simulated EncodedFrame to feed into the VideoDecoder. |
70 | 74 |
71 const gfx::Size frame_size(kWidth, kHeight); | |
72 const scoped_refptr<VideoFrame> video_frame = | 75 const scoped_refptr<VideoFrame> video_frame = |
73 VideoFrame::CreateFrame(VideoFrame::YV12, | 76 VideoFrame::CreateFrame(VideoFrame::YV12, |
74 frame_size, | 77 next_frame_size_, |
75 gfx::Rect(frame_size), | 78 gfx::Rect(next_frame_size_), |
76 frame_size, | 79 next_frame_size_, |
77 next_frame_timestamp_); | 80 next_frame_timestamp_); |
| 81 const base::TimeTicks reference_time = |
| 82 base::TimeTicks::UnixEpoch() + next_frame_timestamp_; |
78 next_frame_timestamp_ += base::TimeDelta::FromSeconds(1) / kFrameRate; | 83 next_frame_timestamp_ += base::TimeDelta::FromSeconds(1) / kFrameRate; |
79 PopulateVideoFrame(video_frame.get(), 0); | 84 PopulateVideoFrame(video_frame.get(), 0); |
80 | 85 |
81 // Encode |frame| into |encoded_frame->data|. | 86 // Encode |frame| into |encoded_frame->data|. |
82 scoped_ptr<EncodedFrame> encoded_frame( | 87 scoped_ptr<EncodedFrame> encoded_frame(new EncodedFrame()); |
83 new EncodedFrame()); | |
84 // Test only supports VP8, currently. | 88 // Test only supports VP8, currently. |
85 CHECK_EQ(CODEC_VIDEO_VP8, GetParam()); | 89 CHECK_EQ(CODEC_VIDEO_VP8, GetParam()); |
86 vp8_encoder_.Encode(video_frame, base::TimeTicks(), encoded_frame.get()); | 90 vp8_encoder_.Encode(video_frame, reference_time, encoded_frame.get()); |
87 // Rewrite frame IDs for testing purposes. | 91 // Rewrite frame IDs for testing purposes. |
88 encoded_frame->frame_id = last_frame_id_ + 1 + num_dropped_frames; | 92 encoded_frame->frame_id = last_frame_id_ + 1 + num_dropped_frames; |
89 if (last_frame_id_ == 0) | 93 if (encoded_frame->dependency == EncodedFrame::KEY) |
90 encoded_frame->referenced_frame_id = encoded_frame->frame_id; | 94 encoded_frame->referenced_frame_id = encoded_frame->frame_id; |
91 else | 95 else |
92 encoded_frame->referenced_frame_id = encoded_frame->frame_id - 1; | 96 encoded_frame->referenced_frame_id = encoded_frame->frame_id - 1; |
93 last_frame_id_ = encoded_frame->frame_id; | 97 last_frame_id_ = encoded_frame->frame_id; |
| 98 ASSERT_EQ(reference_time, encoded_frame->reference_time); |
94 | 99 |
95 { | 100 { |
96 base::AutoLock auto_lock(lock_); | 101 base::AutoLock auto_lock(lock_); |
97 ++total_video_frames_feed_in_; | 102 ++total_video_frames_feed_in_; |
98 } | 103 } |
99 | 104 |
100 cast_environment_->PostTask( | 105 cast_environment_->PostTask( |
101 CastEnvironment::MAIN, | 106 CastEnvironment::MAIN, |
102 FROM_HERE, | 107 FROM_HERE, |
103 base::Bind(&VideoDecoder::DecodeFrame, | 108 base::Bind(&VideoDecoder::DecodeFrame, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // TODO(miu): Once we start using VideoFrame::timestamp_, check that here. | 146 // TODO(miu): Once we start using VideoFrame::timestamp_, check that here. |
142 | 147 |
143 // Signal the main test thread that more video was decoded. | 148 // Signal the main test thread that more video was decoded. |
144 base::AutoLock auto_lock(lock_); | 149 base::AutoLock auto_lock(lock_); |
145 ++total_video_frames_decoded_; | 150 ++total_video_frames_decoded_; |
146 cond_.Signal(); | 151 cond_.Signal(); |
147 } | 152 } |
148 | 153 |
149 const scoped_refptr<StandaloneCastEnvironment> cast_environment_; | 154 const scoped_refptr<StandaloneCastEnvironment> cast_environment_; |
150 scoped_ptr<VideoDecoder> video_decoder_; | 155 scoped_ptr<VideoDecoder> video_decoder_; |
| 156 gfx::Size next_frame_size_; |
151 base::TimeDelta next_frame_timestamp_; | 157 base::TimeDelta next_frame_timestamp_; |
152 uint32 last_frame_id_; | 158 uint32 last_frame_id_; |
153 bool seen_a_decoded_frame_; | 159 bool seen_a_decoded_frame_; |
154 | 160 |
155 Vp8Encoder vp8_encoder_; | 161 Vp8Encoder vp8_encoder_; |
156 | 162 |
157 base::Lock lock_; | 163 base::Lock lock_; |
158 base::ConditionVariable cond_; | 164 base::ConditionVariable cond_; |
159 int total_video_frames_feed_in_; | 165 int total_video_frames_feed_in_; |
160 int total_video_frames_decoded_; | 166 int total_video_frames_decoded_; |
(...skipping 18 matching lines...) Expand all Loading... |
179 next_drop_at *= 2; | 185 next_drop_at *= 2; |
180 i += num_dropped; | 186 i += num_dropped; |
181 FeedMoreVideo(num_dropped); | 187 FeedMoreVideo(num_dropped); |
182 } else { | 188 } else { |
183 FeedMoreVideo(0); | 189 FeedMoreVideo(0); |
184 } | 190 } |
185 } | 191 } |
186 WaitForAllVideoToBeDecoded(); | 192 WaitForAllVideoToBeDecoded(); |
187 } | 193 } |
188 | 194 |
189 INSTANTIATE_TEST_CASE_P(VideoDecoderTestScenarios, | 195 TEST_P(VideoDecoderTest, DecodesFramesOfVaryingSizes) { |
| 196 std::vector<gfx::Size> frame_sizes; |
| 197 frame_sizes.push_back(gfx::Size(1280, 720)); |
| 198 frame_sizes.push_back(gfx::Size(640, 360)); // Shrink both dimensions. |
| 199 frame_sizes.push_back(gfx::Size(300, 200)); // Shrink both dimensions again. |
| 200 frame_sizes.push_back(gfx::Size(200, 300)); // Same area. |
| 201 frame_sizes.push_back(gfx::Size(600, 400)); // Grow both dimensions. |
| 202 frame_sizes.push_back(gfx::Size(638, 400)); // Shrink only one dimension. |
| 203 frame_sizes.push_back(gfx::Size(638, 398)); // Shrink the other dimension. |
| 204 frame_sizes.push_back(gfx::Size(320, 180)); // Shrink both dimensions again. |
| 205 frame_sizes.push_back(gfx::Size(322, 180)); // Grow only one dimension. |
| 206 frame_sizes.push_back(gfx::Size(322, 182)); // Grow the other dimension. |
| 207 frame_sizes.push_back(gfx::Size(1920, 1080)); // Grow both dimensions again. |
| 208 |
| 209 // Encode one frame at each size. |
| 210 for (const auto& frame_size : frame_sizes) { |
| 211 SetNextFrameSize(frame_size); |
| 212 FeedMoreVideo(0); |
| 213 } |
| 214 |
| 215 // Encode 10 frames at each size. |
| 216 for (const auto& frame_size : frame_sizes) { |
| 217 SetNextFrameSize(frame_size); |
| 218 const int kNumFrames = 10; |
| 219 for (int i = 0; i < kNumFrames; ++i) |
| 220 FeedMoreVideo(0); |
| 221 } |
| 222 |
| 223 WaitForAllVideoToBeDecoded(); |
| 224 } |
| 225 |
| 226 INSTANTIATE_TEST_CASE_P(, |
190 VideoDecoderTest, | 227 VideoDecoderTest, |
191 ::testing::Values(CODEC_VIDEO_VP8)); | 228 ::testing::Values(CODEC_VIDEO_VP8)); |
192 | 229 |
193 } // namespace cast | 230 } // namespace cast |
194 } // namespace media | 231 } // namespace media |
OLD | NEW |