roll skia to 4057
[chromium-blink-merge.git] / media / filters / pipeline_integration_test.cc
bloba9347efd3e160df0e9ce1d890e9882a294bae485
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
3 // found in the LICENSE file.
5 #include "media/filters/pipeline_integration_test_base.h"
7 #include "base/bind.h"
8 #include "media/base/test_data_util.h"
9 #include "media/filters/chunk_demuxer_client.h"
11 namespace media {
13 // Key ID of the video track in test file "bear-320x240-encrypted.webm".
14 static const unsigned char kKeyId[] =
15 "\x11\xa5\x18\x37\xc4\x73\x84\x03\xe5\xe6\x57\xed\x8e\x06\xd9\x7c";
17 static const char* kSourceId = "SourceId";
19 // Helper class that emulates calls made on the ChunkDemuxer by the
20 // Media Source API.
21 class MockMediaSource : public ChunkDemuxerClient {
22 public:
23 MockMediaSource(const std::string& filename, int initial_append_size)
24 : url_(GetTestDataURL(filename)),
25 current_position_(0),
26 initial_append_size_(initial_append_size) {
27 ReadTestDataFile(filename, &file_data_, &file_data_size_);
29 DCHECK_GT(initial_append_size_, 0);
30 DCHECK_LE(initial_append_size_, file_data_size_);
33 virtual ~MockMediaSource() {}
35 void set_decryptor(AesDecryptor* decryptor) {
36 decryptor_ = decryptor;
38 AesDecryptor* decryptor() const {
39 return decryptor_;
42 const std::string& url() const { return url_; }
44 void Seek(int new_position, int seek_append_size) {
45 chunk_demuxer_->FlushData();
47 DCHECK_GE(new_position, 0);
48 DCHECK_LT(new_position, file_data_size_);
49 current_position_ = new_position;
51 AppendData(seek_append_size);
54 void AppendData(int size) {
55 DCHECK(chunk_demuxer_.get());
56 DCHECK_LT(current_position_, file_data_size_);
57 DCHECK_LE(current_position_ + size, file_data_size_);
58 CHECK(chunk_demuxer_->AppendData(kSourceId,
59 file_data_.get() + current_position_,
60 size));
61 current_position_ += size;
64 void EndOfStream() {
65 chunk_demuxer_->EndOfStream(PIPELINE_OK);
68 void Abort() {
69 if (!chunk_demuxer_.get())
70 return;
71 chunk_demuxer_->Shutdown();
74 // ChunkDemuxerClient methods.
75 virtual void DemuxerOpened(ChunkDemuxer* demuxer) {
76 chunk_demuxer_ = demuxer;
78 std::vector<std::string> codecs(2);
79 codecs[0] = "vp8";
80 codecs[1] = "vorbis";
81 chunk_demuxer_->AddId(kSourceId, "video/webm", codecs);
82 AppendData(initial_append_size_);
85 virtual void DemuxerClosed() {
86 chunk_demuxer_ = NULL;
89 virtual void KeyNeeded(scoped_array<uint8> init_data, int init_data_size) {
90 DCHECK(init_data.get());
91 DCHECK_EQ(init_data_size, 16);
92 DCHECK(decryptor());
93 // In test file bear-320x240-encrypted.webm, the decryption key is equal to
94 // |init_data|.
95 decryptor()->AddKey(init_data.get(), init_data_size,
96 init_data.get(), init_data_size);
99 private:
100 std::string url_;
101 scoped_array<uint8> file_data_;
102 int file_data_size_;
103 int current_position_;
104 int initial_append_size_;
105 scoped_refptr<ChunkDemuxer> chunk_demuxer_;
106 AesDecryptor* decryptor_;
109 class PipelineIntegrationTest
110 : public testing::Test,
111 public PipelineIntegrationTestBase {
112 public:
113 void StartPipelineWithMediaSource(MockMediaSource& source) {
114 pipeline_->Start(
115 CreateFilterCollection(&source),
116 base::Bind(&PipelineIntegrationTest::OnEnded, base::Unretained(this)),
117 base::Bind(&PipelineIntegrationTest::OnError, base::Unretained(this)),
118 NetworkEventCB(), QuitOnStatusCB(PIPELINE_OK));
120 ASSERT_TRUE(decoder_.get());
121 source.set_decryptor(decoder_->decryptor());
123 message_loop_.Run();
126 // Verifies that seeking works properly for ChunkDemuxer when the
127 // seek happens while there is a pending read on the ChunkDemuxer
128 // and no data is available.
129 bool TestSeekDuringRead(const std::string& filename,
130 int initial_append_size,
131 base::TimeDelta start_seek_time,
132 base::TimeDelta seek_time,
133 int seek_file_position,
134 int seek_append_size) {
135 MockMediaSource source(filename, initial_append_size);
136 StartPipelineWithMediaSource(source);
138 if (pipeline_status_ != PIPELINE_OK)
139 return false;
141 Play();
142 if (!WaitUntilCurrentTimeIsAfter(start_seek_time))
143 return false;
145 source.Seek(seek_file_position, seek_append_size);
146 if (!Seek(seek_time))
147 return false;
149 source.EndOfStream();
151 source.Abort();
152 Stop();
153 return true;
158 TEST_F(PipelineIntegrationTest, BasicPlayback) {
159 ASSERT_TRUE(Start(GetTestDataURL("bear-320x240.webm"), PIPELINE_OK));
161 Play();
163 ASSERT_TRUE(WaitUntilOnEnded());
165 ASSERT_EQ(GetVideoHash(), "f0be120a90a811506777c99a2cdf7cc1");
168 TEST_F(PipelineIntegrationTest, EncryptedPlayback) {
169 MockMediaSource source("bear-320x240-encrypted.webm", 219726);
170 StartPipelineWithMediaSource(source);
172 source.EndOfStream();
173 ASSERT_EQ(PIPELINE_OK, pipeline_status_);
175 Play();
177 ASSERT_TRUE(WaitUntilOnEnded());
178 source.Abort();
179 Stop();
182 // TODO(acolwell): Fix flakiness http://crbug.com/117921
183 TEST_F(PipelineIntegrationTest, DISABLED_SeekWhilePaused) {
184 ASSERT_TRUE(Start(GetTestDataURL("bear-320x240.webm"), PIPELINE_OK));
186 base::TimeDelta duration(pipeline_->GetMediaDuration());
187 base::TimeDelta start_seek_time(duration / 4);
188 base::TimeDelta seek_time(duration * 3 / 4);
190 Play();
191 ASSERT_TRUE(WaitUntilCurrentTimeIsAfter(start_seek_time));
192 Pause();
193 ASSERT_TRUE(Seek(seek_time));
194 EXPECT_EQ(pipeline_->GetCurrentTime(), seek_time);
195 Play();
196 ASSERT_TRUE(WaitUntilOnEnded());
198 // Make sure seeking after reaching the end works as expected.
199 Pause();
200 ASSERT_TRUE(Seek(seek_time));
201 EXPECT_EQ(pipeline_->GetCurrentTime(), seek_time);
202 Play();
203 ASSERT_TRUE(WaitUntilOnEnded());
206 // TODO(acolwell): Fix flakiness http://crbug.com/117921
207 TEST_F(PipelineIntegrationTest, DISABLED_SeekWhilePlaying) {
208 ASSERT_TRUE(Start(GetTestDataURL("bear-320x240.webm"), PIPELINE_OK));
210 base::TimeDelta duration(pipeline_->GetMediaDuration());
211 base::TimeDelta start_seek_time(duration / 4);
212 base::TimeDelta seek_time(duration * 3 / 4);
214 Play();
215 ASSERT_TRUE(WaitUntilCurrentTimeIsAfter(start_seek_time));
216 ASSERT_TRUE(Seek(seek_time));
217 EXPECT_GE(pipeline_->GetCurrentTime(), seek_time);
218 ASSERT_TRUE(WaitUntilOnEnded());
220 // Make sure seeking after reaching the end works as expected.
221 ASSERT_TRUE(Seek(seek_time));
222 EXPECT_GE(pipeline_->GetCurrentTime(), seek_time);
223 ASSERT_TRUE(WaitUntilOnEnded());
226 // Verify audio decoder & renderer can handle aborted demuxer reads.
227 TEST_F(PipelineIntegrationTest, ChunkDemuxerAbortRead_AudioOnly) {
228 ASSERT_TRUE(TestSeekDuringRead("bear-320x240-audio-only.webm", 8192,
229 base::TimeDelta::FromMilliseconds(464),
230 base::TimeDelta::FromMilliseconds(617),
231 0x10CA, 19730));
234 // Verify video decoder & renderer can handle aborted demuxer reads.
235 TEST_F(PipelineIntegrationTest, ChunkDemuxerAbortRead_VideoOnly) {
236 ASSERT_TRUE(TestSeekDuringRead("bear-320x240-video-only.webm", 32768,
237 base::TimeDelta::FromMilliseconds(200),
238 base::TimeDelta::FromMilliseconds(1668),
239 0x1C896, 65536));
242 } // namespace media