Delete unused downloads page asset.
[chromium-blink-merge.git] / chromecast / media / cma / test / media_component_device_feeder_for_test.cc
blob9ca180059c02eb83a9efa4624bb7a2393ff6006c
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/buffers.h"
31 #include "media/base/decoder_buffer.h"
32 #include "media/base/video_decoder_config.h"
33 #include "testing/gtest/include/gtest/gtest.h"
35 namespace chromecast {
36 namespace media {
38 MediaComponentDeviceFeederForTest::MediaComponentDeviceFeederForTest(
39 MediaComponentDevice *device,
40 const BufferList& frames)
41 : media_component_device_(device),
42 rendering_frame_idx_(1),
43 clock_frame_idx_(1),
44 feeding_completed_(false) {
45 frames_ = frames;
48 MediaComponentDeviceFeederForTest::~MediaComponentDeviceFeederForTest() {
51 void MediaComponentDeviceFeederForTest::Initialize(
52 const base::Closure& eos_cb) {
53 eos_cb_ = eos_cb;
55 media_component_device_->SetClient(
56 new MediaComponentDeviceClientImpl(base::Bind(
57 &MediaComponentDeviceFeederForTest::OnEos, base::Unretained(this))));
59 bool success =
60 media_component_device_->SetState(MediaComponentDevice::kStateIdle);
61 ASSERT_TRUE(success);
62 success = media_component_device_->SetStartPts(0);
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 = media_component_device_->PushFrame(
82 nullptr, // decrypt_context
83 new CastDecoderBufferImpl(buffer),
84 new FrameStatusCBImpl(
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