OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "media/base/filter_collection.h" | 7 #include "media/base/filter_collection.h" |
8 #include "media/base/media_log.h" | 8 #include "media/base/media_log.h" |
9 #include "media/base/message_loop_factory_impl.h" | 9 #include "media/base/message_loop_factory_impl.h" |
10 #include "media/base/pipeline.h" | 10 #include "media/base/pipeline.h" |
11 #include "media/base/test_data_util.h" | 11 #include "media/base/test_data_util.h" |
| 12 #include "media/filters/chunk_demuxer.h" |
| 13 #include "media/filters/chunk_demuxer_client.h" |
| 14 #include "media/filters/chunk_demuxer_factory.h" |
12 #include "media/filters/ffmpeg_audio_decoder.h" | 15 #include "media/filters/ffmpeg_audio_decoder.h" |
13 #include "media/filters/ffmpeg_demuxer_factory.h" | 16 #include "media/filters/ffmpeg_demuxer_factory.h" |
14 #include "media/filters/ffmpeg_video_decoder.h" | 17 #include "media/filters/ffmpeg_video_decoder.h" |
15 #include "media/filters/file_data_source.h" | 18 #include "media/filters/file_data_source.h" |
16 #include "media/filters/null_audio_renderer.h" | 19 #include "media/filters/null_audio_renderer.h" |
17 #include "media/filters/video_renderer_base.h" | 20 #include "media/filters/video_renderer_base.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
20 | 23 |
21 using ::testing::AnyNumber; | 24 using ::testing::AnyNumber; |
22 | 25 |
23 namespace media { | 26 namespace media { |
24 | 27 |
| 28 // Helper class that emulates calls made on the ChunkDemuxer by the |
| 29 // Media Source API. |
| 30 class MockMediaSource : public ChunkDemuxerClient { |
| 31 public: |
| 32 MockMediaSource(const std::string& filename, int initial_append_size) |
| 33 : url_(GetTestDataURL(filename)), |
| 34 current_position_(0), |
| 35 initial_append_size_(initial_append_size) { |
| 36 ReadTestDataFile(filename, &file_data_, &file_data_size_); |
| 37 |
| 38 DCHECK_GT(initial_append_size_, 0); |
| 39 DCHECK_LE(initial_append_size_, file_data_size_); |
| 40 } |
| 41 |
| 42 virtual ~MockMediaSource() {} |
| 43 |
| 44 const std::string& url() { return url_; } |
| 45 |
| 46 void Seek(int new_position, int seek_append_size) { |
| 47 chunk_demuxer_->FlushData(); |
| 48 |
| 49 DCHECK_GE(new_position, 0); |
| 50 DCHECK_LT(new_position, file_data_size_); |
| 51 current_position_ = new_position; |
| 52 |
| 53 AppendData(seek_append_size); |
| 54 } |
| 55 |
| 56 void AppendData(int size) { |
| 57 DCHECK(chunk_demuxer_.get()); |
| 58 DCHECK_LT(current_position_, file_data_size_); |
| 59 DCHECK_LE(current_position_ + size, file_data_size_); |
| 60 chunk_demuxer_->AppendData(file_data_.get() + current_position_, size); |
| 61 current_position_ += size; |
| 62 } |
| 63 |
| 64 void EndOfStream() { |
| 65 chunk_demuxer_->EndOfStream(PIPELINE_OK); |
| 66 } |
| 67 |
| 68 void Abort() { |
| 69 if (!chunk_demuxer_.get()) |
| 70 return; |
| 71 chunk_demuxer_->Shutdown(); |
| 72 } |
| 73 |
| 74 // ChunkDemuxerClient methods. |
| 75 virtual void DemuxerOpened(ChunkDemuxer* demuxer) { |
| 76 chunk_demuxer_ = demuxer; |
| 77 AppendData(initial_append_size_); |
| 78 } |
| 79 |
| 80 virtual void DemuxerClosed() { |
| 81 chunk_demuxer_ = NULL; |
| 82 } |
| 83 |
| 84 private: |
| 85 std::string url_; |
| 86 scoped_array<uint8> file_data_; |
| 87 int file_data_size_; |
| 88 int current_position_; |
| 89 int initial_append_size_; |
| 90 scoped_refptr<ChunkDemuxer> chunk_demuxer_; |
| 91 }; |
| 92 |
25 // Integration tests for Pipeline. Real demuxers, real decoders, and | 93 // Integration tests for Pipeline. Real demuxers, real decoders, and |
26 // base renderer implementations are used to verify pipeline functionality. The | 94 // base renderer implementations are used to verify pipeline functionality. The |
27 // renderers used in these tests rely heavily on the AudioRendererBase & | 95 // renderers used in these tests rely heavily on the AudioRendererBase & |
28 // VideoRendererBase implementations which contain a majority of the code used | 96 // VideoRendererBase implementations which contain a majority of the code used |
29 // in the real AudioRendererImpl & SkCanvasVideoRenderer implementations used in | 97 // in the real AudioRendererImpl & SkCanvasVideoRenderer implementations used in |
30 // the browser. The renderers in this test don't actually write data to a | 98 // the browser. The renderers in this test don't actually write data to a |
31 // display or audio device. Both of these devices are simulated since they have | 99 // display or audio device. Both of these devices are simulated since they have |
32 // little effect on verifying pipeline behavior and allow tests to run faster | 100 // little effect on verifying pipeline behavior and allow tests to run faster |
33 // than real-time. | 101 // than real-time. |
34 class PipelineIntegrationTest : public testing::Test { | 102 class PipelineIntegrationTest : public testing::Test { |
35 public: | 103 public: |
36 PipelineIntegrationTest() | 104 PipelineIntegrationTest() |
37 : message_loop_factory_(new MessageLoopFactoryImpl()), | 105 : message_loop_factory_(new MessageLoopFactoryImpl()), |
38 pipeline_(new Pipeline(&message_loop_, new MediaLog())), | 106 pipeline_(new Pipeline(&message_loop_, new MediaLog())), |
39 ended_(false) { | 107 ended_(false) { |
40 EXPECT_CALL(*this, OnVideoRendererPaint()).Times(AnyNumber()); | 108 EXPECT_CALL(*this, OnVideoRendererPaint()).Times(AnyNumber()); |
41 EXPECT_CALL(*this, OnSetOpaque(true)); | 109 EXPECT_CALL(*this, OnSetOpaque(true)).Times(AnyNumber()); |
42 } | 110 } |
43 | 111 |
44 virtual ~PipelineIntegrationTest() { | 112 virtual ~PipelineIntegrationTest() { |
45 if (!pipeline_->IsRunning()) | 113 if (!pipeline_->IsRunning()) |
46 return; | 114 return; |
47 | 115 |
48 Stop(); | 116 Stop(); |
49 } | 117 } |
50 | 118 |
51 void OnStatusCallback(PipelineStatus expected_status, | 119 void OnStatusCallback(PipelineStatus expected_status, |
52 PipelineStatus status) { | 120 PipelineStatus status) { |
53 DCHECK_EQ(status, expected_status); | 121 EXPECT_EQ(status, expected_status); |
54 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 122 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
55 } | 123 } |
56 | 124 |
57 PipelineStatusCB QuitOnStatusCB(PipelineStatus expected_status) { | 125 PipelineStatusCB QuitOnStatusCB(PipelineStatus expected_status) { |
58 return base::Bind(&PipelineIntegrationTest::OnStatusCallback, | 126 return base::Bind(&PipelineIntegrationTest::OnStatusCallback, |
59 base::Unretained(this), | 127 base::Unretained(this), |
60 expected_status); | 128 expected_status); |
61 } | 129 } |
62 | 130 |
63 void OnEnded(PipelineStatus status) { | 131 void OnEnded(PipelineStatus status) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 base::Bind(&PipelineIntegrationTest::QuitAfterCurrentTimeTask, | 199 base::Bind(&PipelineIntegrationTest::QuitAfterCurrentTimeTask, |
132 base::Unretained(this), | 200 base::Unretained(this), |
133 wait_time), | 201 wait_time), |
134 10); | 202 10); |
135 message_loop_.Run(); | 203 message_loop_.Run(); |
136 } | 204 } |
137 | 205 |
138 scoped_ptr<FilterCollection> CreateFilterCollection(const std::string& url) { | 206 scoped_ptr<FilterCollection> CreateFilterCollection(const std::string& url) { |
139 scoped_refptr<FileDataSource> data_source = new FileDataSource(); | 207 scoped_refptr<FileDataSource> data_source = new FileDataSource(); |
140 CHECK_EQ(PIPELINE_OK, data_source->Initialize(url)); | 208 CHECK_EQ(PIPELINE_OK, data_source->Initialize(url)); |
| 209 return CreateFilterCollection(scoped_ptr<DemuxerFactory>( |
| 210 new FFmpegDemuxerFactory(data_source, &message_loop_))); |
| 211 } |
141 | 212 |
142 scoped_ptr<FilterCollection> collection( | 213 scoped_ptr<FilterCollection> CreateFilterCollection( |
143 new FilterCollection()); | 214 ChunkDemuxerClient* client) { |
144 collection->SetDemuxerFactory(scoped_ptr<DemuxerFactory>( | 215 return CreateFilterCollection(scoped_ptr<DemuxerFactory>( |
145 new FFmpegDemuxerFactory(data_source, &message_loop_))); | 216 new ChunkDemuxerFactory(client))); |
| 217 } |
| 218 |
| 219 scoped_ptr<FilterCollection> CreateFilterCollection( |
| 220 scoped_ptr<DemuxerFactory> demuxer_factory) { |
| 221 scoped_ptr<FilterCollection> collection(new FilterCollection()); |
| 222 collection->SetDemuxerFactory(demuxer_factory.Pass()); |
146 collection->AddAudioDecoder(new FFmpegAudioDecoder( | 223 collection->AddAudioDecoder(new FFmpegAudioDecoder( |
147 message_loop_factory_->GetMessageLoop("AudioDecoderThread"))); | 224 message_loop_factory_->GetMessageLoop("AudioDecoderThread"))); |
148 collection->AddVideoDecoder(new FFmpegVideoDecoder( | 225 collection->AddVideoDecoder(new FFmpegVideoDecoder( |
149 message_loop_factory_->GetMessageLoop("VideoDecoderThread"))); | 226 message_loop_factory_->GetMessageLoop("VideoDecoderThread"))); |
150 collection->AddVideoRenderer(new VideoRendererBase( | 227 collection->AddVideoRenderer(new VideoRendererBase( |
151 base::Bind(&PipelineIntegrationTest::OnVideoRendererPaint, | 228 base::Bind(&PipelineIntegrationTest::OnVideoRendererPaint, |
152 base::Unretained(this)), | 229 base::Unretained(this)), |
153 base::Bind(&PipelineIntegrationTest::OnSetOpaque, | 230 base::Bind(&PipelineIntegrationTest::OnSetOpaque, |
154 base::Unretained(this)))); | 231 base::Unretained(this)))); |
155 collection->AddAudioRenderer(new NullAudioRenderer()); | 232 collection->AddAudioRenderer(new NullAudioRenderer()); |
156 return collection.Pass(); | 233 return collection.Pass(); |
157 } | 234 } |
158 | 235 |
| 236 // Verifies that seeking works properly for ChunkDemuxer when the |
| 237 // seek happens while there is a pending read on the ChunkDemuxer |
| 238 // and no data is available. |
| 239 void TestSeekDuringRead(const std::string& filename, |
| 240 int initial_append_size, |
| 241 base::TimeDelta start_seek_time, |
| 242 base::TimeDelta seek_time, |
| 243 int seek_file_position, |
| 244 int seek_append_size) { |
| 245 MockMediaSource source(filename, initial_append_size); |
| 246 |
| 247 pipeline_->Start(CreateFilterCollection(&source), source.url(), |
| 248 base::Bind(&PipelineIntegrationTest::OnEnded, |
| 249 base::Unretained(this)), |
| 250 base::Bind(&PipelineIntegrationTest::OnError, |
| 251 base::Unretained(this)), |
| 252 NetworkEventCB(), |
| 253 QuitOnStatusCB(PIPELINE_OK)); |
| 254 message_loop_.Run(); |
| 255 |
| 256 Play(); |
| 257 WaitUntilCurrentTimeIsAfter(start_seek_time); |
| 258 |
| 259 source.Seek(seek_file_position, seek_append_size); |
| 260 Seek(seek_time); |
| 261 |
| 262 source.EndOfStream(); |
| 263 |
| 264 source.Abort(); |
| 265 Stop(); |
| 266 } |
| 267 |
159 protected: | 268 protected: |
160 MessageLoop message_loop_; | 269 MessageLoop message_loop_; |
161 scoped_ptr<MessageLoopFactory> message_loop_factory_; | 270 scoped_ptr<MessageLoopFactory> message_loop_factory_; |
162 scoped_refptr<Pipeline> pipeline_; | 271 scoped_refptr<Pipeline> pipeline_; |
163 bool ended_; | 272 bool ended_; |
164 | 273 |
165 private: | 274 private: |
166 MOCK_METHOD0(OnVideoRendererPaint, void()); | 275 MOCK_METHOD0(OnVideoRendererPaint, void()); |
167 MOCK_METHOD1(OnSetOpaque, void(bool)); | 276 MOCK_METHOD1(OnSetOpaque, void(bool)); |
168 }; | 277 }; |
(...skipping 23 matching lines...) Expand all Loading... |
192 WaitUntilOnEnded(); | 301 WaitUntilOnEnded(); |
193 | 302 |
194 // Make sure seeking after reaching the end works as expected. | 303 // Make sure seeking after reaching the end works as expected. |
195 Pause(); | 304 Pause(); |
196 Seek(seek_time); | 305 Seek(seek_time); |
197 EXPECT_EQ(pipeline_->GetCurrentTime(), seek_time); | 306 EXPECT_EQ(pipeline_->GetCurrentTime(), seek_time); |
198 Play(); | 307 Play(); |
199 WaitUntilOnEnded(); | 308 WaitUntilOnEnded(); |
200 } | 309 } |
201 | 310 |
202 TEST_F(PipelineIntegrationTest, SeekWhilePlaying) { | 311 // TODO(acolwell): Fix flakiness http://crbug.com/109875 |
| 312 TEST_F(PipelineIntegrationTest, FLAKY_SeekWhilePlaying) { |
203 Start(GetTestDataURL("bear-320x240.webm"), PIPELINE_OK); | 313 Start(GetTestDataURL("bear-320x240.webm"), PIPELINE_OK); |
204 | 314 |
205 base::TimeDelta duration(pipeline_->GetMediaDuration()); | 315 base::TimeDelta duration(pipeline_->GetMediaDuration()); |
206 base::TimeDelta start_seek_time(duration / 4); | 316 base::TimeDelta start_seek_time(duration / 4); |
207 base::TimeDelta seek_time(duration * 3 / 4); | 317 base::TimeDelta seek_time(duration * 3 / 4); |
208 | 318 |
209 Play(); | 319 Play(); |
210 WaitUntilCurrentTimeIsAfter(start_seek_time); | 320 WaitUntilCurrentTimeIsAfter(start_seek_time); |
211 Seek(seek_time); | 321 Seek(seek_time); |
212 EXPECT_GE(pipeline_->GetCurrentTime(), seek_time); | 322 EXPECT_GE(pipeline_->GetCurrentTime(), seek_time); |
213 WaitUntilOnEnded(); | 323 WaitUntilOnEnded(); |
214 | 324 |
215 // Make sure seeking after reaching the end works as expected. | 325 // Make sure seeking after reaching the end works as expected. |
216 Seek(seek_time); | 326 Seek(seek_time); |
217 EXPECT_GE(pipeline_->GetCurrentTime(), seek_time); | 327 EXPECT_GE(pipeline_->GetCurrentTime(), seek_time); |
218 WaitUntilOnEnded(); | 328 WaitUntilOnEnded(); |
219 } | 329 } |
220 | 330 |
| 331 // Verify audio decoder & renderer can handle aborted demuxer reads. |
| 332 TEST_F(PipelineIntegrationTest, ChunkDemuxerAbortRead_AudioOnly) { |
| 333 TestSeekDuringRead("bear-320x240-audio-only.webm", 8192, |
| 334 base::TimeDelta::FromMilliseconds(477), |
| 335 base::TimeDelta::FromMilliseconds(617), |
| 336 0x10CA, 19730); |
| 337 } |
| 338 |
| 339 // Verify video decoder & renderer can handle aborted demuxer reads. |
| 340 TEST_F(PipelineIntegrationTest, ChunkDemuxerAbortRead_VideoOnly) { |
| 341 TestSeekDuringRead("bear-320x240-video-only.webm", 32768, |
| 342 base::TimeDelta::FromMilliseconds(200), |
| 343 base::TimeDelta::FromMilliseconds(1668), |
| 344 0x1C896, 65536); |
| 345 } |
| 346 |
221 } // namespace media | 347 } // namespace media |
OLD | NEW |