Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromecast / media / cma / test / mock_frame_provider.cc
blob27649a775288925a7be7c96c2b063308eb511097
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_provider.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/decoder_buffer_base.h"
13 #include "chromecast/media/cma/test/frame_generator_for_test.h"
14 #include "media/base/audio_decoder_config.h"
15 #include "media/base/decoder_buffer.h"
16 #include "media/base/video_decoder_config.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/size.h"
21 namespace chromecast {
22 namespace media {
24 MockFrameProvider::MockFrameProvider() : delay_flush_(false) {
27 MockFrameProvider::~MockFrameProvider() {
30 void MockFrameProvider::Configure(
31 const std::vector<bool>& delayed_task_pattern,
32 scoped_ptr<FrameGeneratorForTest> frame_generator) {
33 delayed_task_pattern_ = delayed_task_pattern;
34 pattern_idx_ = 0;
36 frame_generator_.reset(frame_generator.release());
39 void MockFrameProvider::SetDelayFlush(bool delay_flush) {
40 delay_flush_ = delay_flush;
43 void MockFrameProvider::Read(const ReadCB& read_cb) {
44 bool delayed = delayed_task_pattern_[pattern_idx_];
45 pattern_idx_ = (pattern_idx_ + 1) % delayed_task_pattern_.size();
47 if (delayed) {
48 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
49 FROM_HERE,
50 base::Bind(&MockFrameProvider::DoRead, base::Unretained(this), read_cb),
51 base::TimeDelta::FromMilliseconds(1));
52 } else {
53 base::ThreadTaskRunnerHandle::Get()->PostTask(
54 FROM_HERE, base::Bind(&MockFrameProvider::DoRead,
55 base::Unretained(this), read_cb));
59 void MockFrameProvider::Flush(const base::Closure& flush_cb) {
60 if (delay_flush_) {
61 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
62 FROM_HERE, flush_cb, base::TimeDelta::FromMilliseconds(10));
63 } else {
64 flush_cb.Run();
68 void MockFrameProvider::DoRead(const ReadCB& read_cb) {
69 bool has_config = frame_generator_->HasDecoderConfig();
71 scoped_refptr<DecoderBufferBase> buffer(frame_generator_->Generate());
72 ASSERT_TRUE(buffer.get());
74 ::media::AudioDecoderConfig audio_config;
75 ::media::VideoDecoderConfig video_config;
76 if (has_config) {
77 gfx::Size coded_size(640, 480);
78 gfx::Rect visible_rect(640, 480);
79 gfx::Size natural_size(640, 480);
80 video_config = ::media::VideoDecoderConfig(
81 ::media::kCodecH264, ::media::VIDEO_CODEC_PROFILE_UNKNOWN,
82 ::media::PIXEL_FORMAT_YV12, ::media::COLOR_SPACE_UNSPECIFIED,
83 coded_size, visible_rect, natural_size, NULL, 0, false);
85 audio_config = ::media::AudioDecoderConfig(
86 ::media::kCodecAAC,
87 ::media::kSampleFormatS16,
88 ::media::CHANNEL_LAYOUT_STEREO,
89 44100,
90 NULL, 0,
91 false);
94 read_cb.Run(buffer, audio_config, video_config);
97 } // namespace media
98 } // namespace chromecast