Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromecast / media / cma / test / media_component_device_feeder_for_test.cc
blob96ec02cfec175d3e246ac92a76a8dc5b9bee5aba
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/cma/base/cast_decoder_buffer_impl.h"
20 #include "chromecast/media/cma/base/decoder_buffer_adapter.h"
21 #include "chromecast/media/cma/pipeline/frame_status_cb_impl.h"
22 #include "chromecast/media/cma/pipeline/media_component_device_client_impl.h"
23 #include "chromecast/media/cma/test/frame_segmenter_for_test.h"
24 #include "chromecast/public/media/audio_pipeline_device.h"
25 #include "chromecast/public/media/cast_decoder_buffer.h"
26 #include "chromecast/public/media/decrypt_context.h"
27 #include "chromecast/public/media/media_clock_device.h"
28 #include "chromecast/public/media/video_pipeline_device.h"
29 #include "media/base/audio_decoder_config.h"
30 #include "media/base/decoder_buffer.h"
31 #include "media/base/video_decoder_config.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 namespace chromecast {
35 namespace media {
37 MediaComponentDeviceFeederForTest::MediaComponentDeviceFeederForTest(
38 MediaComponentDevice *device,
39 const BufferList& frames)
40 : media_component_device_(device),
41 rendering_frame_idx_(1),
42 clock_frame_idx_(1),
43 feeding_completed_(false) {
44 frames_ = frames;
47 MediaComponentDeviceFeederForTest::~MediaComponentDeviceFeederForTest() {
50 void MediaComponentDeviceFeederForTest::Initialize(
51 const base::Closure& eos_cb) {
52 eos_cb_ = eos_cb;
54 media_component_device_->SetClient(
55 new MediaComponentDeviceClientImpl(base::Bind(
56 &MediaComponentDeviceFeederForTest::OnEos, base::Unretained(this))));
58 bool success =
59 media_component_device_->SetState(MediaComponentDevice::kStateIdle);
60 ASSERT_TRUE(success);
61 success = media_component_device_->SetStartPts(0);
62 ASSERT_TRUE(success);
63 success =
64 media_component_device_->SetState(MediaComponentDevice::kStatePaused);
65 ASSERT_TRUE(success);
68 void MediaComponentDeviceFeederForTest::Feed() {
69 // Start rendering if needed.
70 if (rendering_frame_idx_ == 0) {
71 media_component_device_->SetState(MediaComponentDevice::kStateRunning);
72 } else {
73 rendering_frame_idx_--;
76 // Possibly feed one frame
77 DCHECK(!frames_.empty());
78 scoped_refptr<DecoderBufferBase> buffer = frames_.front();
80 MediaComponentDevice::FrameStatus status = media_component_device_->PushFrame(
81 nullptr, // decrypt_context
82 new CastDecoderBufferImpl(buffer),
83 new FrameStatusCBImpl(
84 base::Bind(&MediaComponentDeviceFeederForTest::OnFramePushed,
85 base::Unretained(this))));
86 EXPECT_NE(status, MediaComponentDevice::kFrameFailed);
87 frames_.pop_front();
89 // Feeding is done, just wait for the end of stream callback.
90 if (buffer->end_of_stream() || frames_.empty()) {
91 if (frames_.empty() && !buffer->end_of_stream()) {
92 LOG(WARNING) << "Stream emptied without feeding EOS frame";
95 feeding_completed_ = true;
96 return;
99 if (status == MediaComponentDevice::kFramePending)
100 return;
102 OnFramePushed(MediaComponentDevice::kFrameSuccess);
105 void MediaComponentDeviceFeederForTest::OnFramePushed(
106 MediaComponentDevice::FrameStatus status) {
107 EXPECT_NE(status, MediaComponentDevice::kFrameFailed);
108 if (feeding_completed_)
109 return;
111 base::ThreadTaskRunnerHandle::Get()->PostTask(
112 FROM_HERE, base::Bind(&MediaComponentDeviceFeederForTest::Feed,
113 base::Unretained(this)));
116 void MediaComponentDeviceFeederForTest::OnEos() {
117 bool success = media_component_device_->SetState(
118 MediaComponentDevice::kStateIdle);
119 ASSERT_TRUE(success);
120 success = media_component_device_->SetState(
121 MediaComponentDevice::kStateUninitialized);
122 ASSERT_TRUE(success);
124 if (!eos_cb_.is_null()) {
125 eos_cb_.Run();
129 } // namespace media
130 } // namespace chromecast