[MediaRouter] Update MR-2-Extension's PostMessage to return boolean.
[chromium-blink-merge.git] / chromecast / media / cma / test / media_component_device_feeder_for_test.cc
blob84cc3da2b0762928ae1d54d5c052ea709b00deed
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/media_component_device_feeder_for_test.h"
7 #include <list>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "base/time/time.h"
19 #include "chromecast/media/base/decrypt_context.h"
20 #include "chromecast/media/cma/backend/audio_pipeline_device.h"
21 #include "chromecast/media/cma/backend/media_clock_device.h"
22 #include "chromecast/media/cma/backend/media_pipeline_device.h"
23 #include "chromecast/media/cma/backend/video_pipeline_device.h"
24 #include "chromecast/media/cma/base/decoder_buffer_adapter.h"
25 #include "chromecast/media/cma/base/decoder_buffer_base.h"
26 #include "chromecast/media/cma/test/frame_segmenter_for_test.h"
27 #include "media/base/audio_decoder_config.h"
28 #include "media/base/buffers.h"
29 #include "media/base/decoder_buffer.h"
30 #include "media/base/video_decoder_config.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 namespace chromecast {
34 namespace media {
36 MediaComponentDeviceFeederForTest::MediaComponentDeviceFeederForTest(
37 MediaComponentDevice *device,
38 const BufferList& frames)
39 : media_component_device_(device),
40 rendering_frame_idx_(1),
41 clock_frame_idx_(1),
42 feeding_completed_(false) {
43 frames_ = frames;
46 MediaComponentDeviceFeederForTest::~MediaComponentDeviceFeederForTest() {
49 void MediaComponentDeviceFeederForTest::Initialize(
50 const base::Closure& eos_cb) {
51 eos_cb_ = eos_cb;
53 MediaComponentDevice::Client client;
54 client.eos_cb =
55 base::Bind(&MediaComponentDeviceFeederForTest::OnEos,
56 base::Unretained(this));
57 media_component_device_->SetClient(client);
59 bool success =
60 media_component_device_->SetState(MediaComponentDevice::kStateIdle);
61 ASSERT_TRUE(success);
62 success = media_component_device_->SetStartPts(base::TimeDelta());
63 ASSERT_TRUE(success);
64 success =
65 media_component_device_->SetState(MediaComponentDevice::kStatePaused);
66 ASSERT_TRUE(success);
69 void MediaComponentDeviceFeederForTest::Feed() {
70 // Start rendering if needed.
71 if (rendering_frame_idx_ == 0) {
72 media_component_device_->SetState(MediaComponentDevice::kStateRunning);
73 } else {
74 rendering_frame_idx_--;
77 // Possibly feed one frame
78 DCHECK(!frames_.empty());
79 scoped_refptr<DecoderBufferBase> buffer = frames_.front();
81 MediaComponentDevice::FrameStatus status =
82 media_component_device_->PushFrame(
83 scoped_refptr<DecryptContext>(),
84 buffer,
85 base::Bind(&MediaComponentDeviceFeederForTest::OnFramePushed,
86 base::Unretained(this)));
87 EXPECT_NE(status, MediaComponentDevice::kFrameFailed);
88 frames_.pop_front();
90 // Feeding is done, just wait for the end of stream callback.
91 if (buffer->end_of_stream() || frames_.empty()) {
92 if (frames_.empty() && !buffer->end_of_stream()) {
93 LOG(WARNING) << "Stream emptied without feeding EOS frame";
96 feeding_completed_ = true;
97 return;
100 if (status == MediaComponentDevice::kFramePending)
101 return;
103 OnFramePushed(MediaComponentDevice::kFrameSuccess);
106 void MediaComponentDeviceFeederForTest::OnFramePushed(
107 MediaComponentDevice::FrameStatus status) {
108 EXPECT_NE(status, MediaComponentDevice::kFrameFailed);
109 if (feeding_completed_)
110 return;
112 base::ThreadTaskRunnerHandle::Get()->PostTask(
113 FROM_HERE, base::Bind(&MediaComponentDeviceFeederForTest::Feed,
114 base::Unretained(this)));
117 void MediaComponentDeviceFeederForTest::OnEos() {
118 bool success = media_component_device_->SetState(
119 MediaComponentDevice::kStateIdle);
120 ASSERT_TRUE(success);
121 success = media_component_device_->SetState(
122 MediaComponentDevice::kStateUninitialized);
123 ASSERT_TRUE(success);
125 if (!eos_cb_.is_null()) {
126 eos_cb_.Run();
130 } // namespace media
131 } // namespace chromecast