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"
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
{
37 MediaComponentDeviceFeederForTest::MediaComponentDeviceFeederForTest(
38 MediaComponentDevice
*device
,
39 const BufferList
& frames
)
40 : media_component_device_(device
),
41 rendering_frame_idx_(1),
43 feeding_completed_(false) {
47 MediaComponentDeviceFeederForTest::~MediaComponentDeviceFeederForTest() {
50 void MediaComponentDeviceFeederForTest::Initialize(
51 const base::Closure
& eos_cb
) {
54 media_component_device_
->SetClient(
55 new MediaComponentDeviceClientImpl(base::Bind(
56 &MediaComponentDeviceFeederForTest::OnEos
, base::Unretained(this))));
59 media_component_device_
->SetState(MediaComponentDevice::kStateIdle
);
61 success
= media_component_device_
->SetStartPts(0);
64 media_component_device_
->SetState(MediaComponentDevice::kStatePaused
);
68 void MediaComponentDeviceFeederForTest::Feed() {
69 // Start rendering if needed.
70 if (rendering_frame_idx_
== 0) {
71 media_component_device_
->SetState(MediaComponentDevice::kStateRunning
);
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
);
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;
99 if (status
== MediaComponentDevice::kFramePending
)
102 OnFramePushed(MediaComponentDevice::kFrameSuccess
);
105 void MediaComponentDeviceFeederForTest::OnFramePushed(
106 MediaComponentDevice::FrameStatus status
) {
107 EXPECT_NE(status
, MediaComponentDevice::kFrameFailed
);
108 if (feeding_completed_
)
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()) {
130 } // namespace chromecast