OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chromecast/media/cma/test/media_component_device_feeder_for_test.h" | |
6 | |
7 #include <list> | |
8 #include <vector> | |
9 | |
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" | |
33 | |
34 namespace chromecast { | |
35 namespace media { | |
36 | |
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; | |
45 } | |
46 | |
47 MediaComponentDeviceFeederForTest::~MediaComponentDeviceFeederForTest() { | |
48 } | |
49 | |
50 void MediaComponentDeviceFeederForTest::Initialize( | |
51 const base::Closure& eos_cb) { | |
52 eos_cb_ = eos_cb; | |
53 | |
54 media_component_device_->SetClient( | |
55 new MediaComponentDeviceClientImpl(base::Bind( | |
56 &MediaComponentDeviceFeederForTest::OnEos, base::Unretained(this)))); | |
57 | |
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); | |
66 } | |
67 | |
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_--; | |
74 } | |
75 | |
76 // Possibly feed one frame | |
77 DCHECK(!frames_.empty()); | |
78 scoped_refptr<DecoderBufferBase> buffer = frames_.front(); | |
79 | |
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(); | |
88 | |
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"; | |
93 } | |
94 | |
95 feeding_completed_ = true; | |
96 return; | |
97 } | |
98 | |
99 if (status == MediaComponentDevice::kFramePending) | |
100 return; | |
101 | |
102 OnFramePushed(MediaComponentDevice::kFrameSuccess); | |
103 } | |
104 | |
105 void MediaComponentDeviceFeederForTest::OnFramePushed( | |
106 MediaComponentDevice::FrameStatus status) { | |
107 EXPECT_NE(status, MediaComponentDevice::kFrameFailed); | |
108 if (feeding_completed_) | |
109 return; | |
110 | |
111 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
112 FROM_HERE, base::Bind(&MediaComponentDeviceFeederForTest::Feed, | |
113 base::Unretained(this))); | |
114 } | |
115 | |
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); | |
123 | |
124 if (!eos_cb_.is_null()) { | |
125 eos_cb_.Run(); | |
126 } | |
127 } | |
128 | |
129 } // namespace media | |
130 } // namespace chromecast | |
OLD | NEW |