[MediaRouter] Update MR-2-Extension's PostMessage to return boolean.
[chromium-blink-merge.git] / chromecast / media / cma / test / mock_frame_consumer.cc
blob07335e66205733cbf1bd2efd5bd4d09c42e0c167
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 "chromecast/media/cma/test/mock_frame_consumer.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/time/time.h"
12 #include "chromecast/media/cma/base/coded_frame_provider.h"
13 #include "chromecast/media/cma/base/decoder_buffer_base.h"
14 #include "chromecast/media/cma/test/frame_generator_for_test.h"
15 #include "media/base/video_decoder_config.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace chromecast {
19 namespace media {
21 MockFrameConsumer::MockFrameConsumer(
22 CodedFrameProvider* coded_frame_provider)
23 : coded_frame_provider_(coded_frame_provider),
24 pattern_idx_(0),
25 last_read_aborted_by_flush_(false) {
28 MockFrameConsumer::~MockFrameConsumer() {
31 void MockFrameConsumer::Configure(
32 const std::vector<bool>& delayed_task_pattern,
33 bool last_read_aborted_by_flush,
34 scoped_ptr<FrameGeneratorForTest> frame_generator) {
35 delayed_task_pattern_ = delayed_task_pattern;
36 last_read_aborted_by_flush_ = last_read_aborted_by_flush;
37 frame_generator_.reset(frame_generator.release());
40 void MockFrameConsumer::Start(const base::Closure& done_cb) {
41 done_cb_ = done_cb;
43 pattern_idx_ = 0;
45 base::ThreadTaskRunnerHandle::Get()->PostTask(
46 FROM_HERE,
47 base::Bind(&MockFrameConsumer::ReadFrame, base::Unretained(this)));
50 void MockFrameConsumer::ReadFrame() {
51 // Once all the frames have been read, flush the frame provider.
52 if (frame_generator_->RemainingFrameCount() == 0 &&
53 !last_read_aborted_by_flush_) {
54 coded_frame_provider_->Flush(
55 base::Bind(&MockFrameConsumer::OnFlushCompleted,
56 base::Unretained(this)));
57 return;
60 coded_frame_provider_->Read(
61 base::Bind(&MockFrameConsumer::OnNewFrame,
62 base::Unretained(this)));
64 // The last read is right away aborted by a Flush.
65 if (frame_generator_->RemainingFrameCount() == 0 &&
66 last_read_aborted_by_flush_) {
67 coded_frame_provider_->Flush(
68 base::Bind(&MockFrameConsumer::OnFlushCompleted,
69 base::Unretained(this)));
70 return;
74 void MockFrameConsumer::OnNewFrame(
75 const scoped_refptr<DecoderBufferBase>& buffer,
76 const ::media::AudioDecoderConfig& audio_config,
77 const ::media::VideoDecoderConfig& video_config) {
78 bool ref_has_config = frame_generator_->HasDecoderConfig();
79 scoped_refptr<DecoderBufferBase> ref_buffer = frame_generator_->Generate();
81 ASSERT_TRUE(buffer.get());
82 ASSERT_TRUE(ref_buffer.get());
84 EXPECT_EQ(video_config.IsValidConfig(), ref_has_config);
86 EXPECT_EQ(buffer->end_of_stream(), ref_buffer->end_of_stream());
87 if (!ref_buffer->end_of_stream()) {
88 EXPECT_EQ(buffer->timestamp(), ref_buffer->timestamp());
89 ASSERT_EQ(buffer->data_size(), ref_buffer->data_size());
90 for (size_t k = 0; k < ref_buffer->data_size(); k++)
91 EXPECT_EQ(buffer->data()[k], ref_buffer->data()[k]);
94 bool delayed = delayed_task_pattern_[pattern_idx_];
95 pattern_idx_ = (pattern_idx_ + 1) % delayed_task_pattern_.size();
97 if (delayed) {
98 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
99 FROM_HERE,
100 base::Bind(&MockFrameConsumer::ReadFrame, base::Unretained(this)),
101 base::TimeDelta::FromMilliseconds(1));
102 } else {
103 base::ThreadTaskRunnerHandle::Get()->PostTask(
104 FROM_HERE,
105 base::Bind(&MockFrameConsumer::ReadFrame, base::Unretained(this)));
109 void MockFrameConsumer::OnFlushCompleted() {
110 EXPECT_EQ(frame_generator_->RemainingFrameCount(), 0u);
111 done_cb_.Run();
114 } // namespace media
115 } // namespace chromecast