OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "content/renderer/media/capture_video_decoder.h" | 6 #include "content/renderer/media/capture_video_decoder.h" |
7 #include "content/renderer/media/video_capture_impl.h" | 7 #include "content/renderer/media/video_capture_impl.h" |
8 #include "content/renderer/media/video_capture_impl_manager.h" | 8 #include "content/renderer/media/video_capture_impl_manager.h" |
9 #include "media/base/filters.h" | 9 #include "media/base/filters.h" |
10 #include "media/base/limits.h" | 10 #include "media/base/limits.h" |
11 #include "media/base/mock_callback.h" | 11 #include "media/base/mock_callback.h" |
12 #include "media/base/mock_filter_host.h" | 12 #include "media/base/mock_filter_host.h" |
13 #include "media/base/mock_filters.h" | 13 #include "media/base/mock_filters.h" |
14 #include "media/base/pipeline_status.h" | 14 #include "media/base/pipeline_status.h" |
| 15 #include "media/video/capture/video_capture_types.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 using ::testing::_; | 18 using ::testing::_; |
18 using ::testing::AnyNumber; | 19 using ::testing::AnyNumber; |
19 using ::testing::Return; | 20 using ::testing::Return; |
20 using ::testing::StrictMock; | 21 using ::testing::StrictMock; |
21 | 22 |
| 23 static const int kWidth = 176; |
| 24 static const int kHeight = 144; |
| 25 static const int kFPS = 30; |
22 static const media::VideoCaptureSessionId kVideoStreamId = 1; | 26 static const media::VideoCaptureSessionId kVideoStreamId = 1; |
23 | 27 |
24 ACTION_P3(CreateDataBufferFromCapture, decoder, vc_impl, data_buffer_number) { | 28 ACTION_P3(CreateDataBufferFromCapture, decoder, vc_impl, data_buffer_number) { |
25 for (int i = 0; i < data_buffer_number; i++) { | 29 for (int i = 0; i < data_buffer_number; i++) { |
26 media::VideoCapture::VideoFrameBuffer* buffer; | 30 media::VideoCapture::VideoFrameBuffer* buffer; |
27 buffer = new media::VideoCapture::VideoFrameBuffer(); | 31 buffer = new media::VideoCapture::VideoFrameBuffer(); |
28 buffer->width = arg1.width; | 32 buffer->width = arg1.width; |
29 buffer->height = arg1.height; | 33 buffer->height = arg1.height; |
30 int length = buffer->width * buffer->height * 3 / 2; | 34 int length = buffer->width * buffer->height * 3 / 2; |
31 buffer->memory_pointer = new uint8[length]; | 35 buffer->memory_pointer = new uint8[length]; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 }; | 82 }; |
79 | 83 |
80 class CaptureVideoDecoderTest : public ::testing::Test { | 84 class CaptureVideoDecoderTest : public ::testing::Test { |
81 protected: | 85 protected: |
82 CaptureVideoDecoderTest() { | 86 CaptureVideoDecoderTest() { |
83 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 87 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
84 message_loop_proxy_ = | 88 message_loop_proxy_ = |
85 base::MessageLoopProxy::current().get(); | 89 base::MessageLoopProxy::current().get(); |
86 vc_manager_ = new MockVideoCaptureImplManager(); | 90 vc_manager_ = new MockVideoCaptureImplManager(); |
87 media::VideoCapture::VideoCaptureCapability capability; | 91 media::VideoCapture::VideoCaptureCapability capability; |
88 capability.width = 176; | 92 capability.width = kWidth; |
89 capability.height = 144; | 93 capability.height = kHeight; |
90 capability.max_fps = 30; | 94 capability.max_fps = kFPS; |
91 capability.expected_capture_delay = 0; | 95 capability.expected_capture_delay = 0; |
92 capability.raw_type = media::VideoFrame::I420; | 96 capability.raw_type = media::VideoFrame::I420; |
93 capability.interlaced = false; | 97 capability.interlaced = false; |
94 | 98 |
95 decoder_ = new CaptureVideoDecoder(message_loop_proxy_, | 99 decoder_ = new CaptureVideoDecoder(message_loop_proxy_, |
96 kVideoStreamId, vc_manager_, capability); | 100 kVideoStreamId, vc_manager_, capability); |
97 decoder_->set_host(&host_); | 101 decoder_->set_host(&host_); |
98 EXPECT_CALL(statistics_callback_object_, OnStatistics(_)) | 102 EXPECT_CALL(statistics_callback_object_, OnStatistics(_)) |
99 .Times(AnyNumber()); | 103 .Times(AnyNumber()); |
100 | 104 |
101 read_cb_ = base::Bind(&CaptureVideoDecoderTest::FrameReady, | 105 read_cb_ = base::Bind(&CaptureVideoDecoderTest::FrameReady, |
102 base::Unretained(this)); | 106 base::Unretained(this)); |
| 107 |
| 108 vc_impl_.reset(new MockVideoCaptureImpl( |
| 109 kVideoStreamId, message_loop_proxy_, new VideoCaptureMessageFilter())); |
103 } | 110 } |
104 | 111 |
105 virtual ~CaptureVideoDecoderTest() { | 112 virtual ~CaptureVideoDecoderTest() { |
106 message_loop_->RunAllPending(); | 113 message_loop_->RunAllPending(); |
107 } | 114 } |
108 | 115 |
109 media::StatisticsCallback NewStatisticsCallback() { | 116 media::StatisticsCallback NewStatisticsCallback() { |
110 return base::Bind(&media::MockStatisticsCallback::OnStatistics, | 117 return base::Bind(&media::MockStatisticsCallback::OnStatistics, |
111 base::Unretained(&statistics_callback_object_)); | 118 base::Unretained(&statistics_callback_object_)); |
112 } | 119 } |
113 | 120 |
| 121 void Initialize() { |
| 122 EXPECT_CALL(*vc_manager_, AddDevice(_, _)) |
| 123 .WillOnce(Return(vc_impl_.get())); |
| 124 decoder_->Initialize(NULL, |
| 125 media::NewExpectedClosure(), |
| 126 NewStatisticsCallback()); |
| 127 message_loop_->RunAllPending(); |
| 128 } |
| 129 |
| 130 void Start() { |
| 131 // Issue a read. |
| 132 EXPECT_CALL(*this, FrameReady(_)); |
| 133 decoder_->Read(read_cb_); |
| 134 |
| 135 // Issue a seek. |
| 136 int buffer_count = 1; |
| 137 EXPECT_CALL(*vc_impl_, StartCapture(capture_client(), _)) |
| 138 .Times(1) |
| 139 .WillOnce(CreateDataBufferFromCapture(capture_client(), |
| 140 vc_impl_.get(), |
| 141 buffer_count)); |
| 142 EXPECT_CALL(*vc_impl_, FeedBuffer(_)) |
| 143 .Times(buffer_count) |
| 144 .WillRepeatedly(DeleteDataBuffer()); |
| 145 decoder_->Seek(base::TimeDelta(), |
| 146 media::NewExpectedStatusCB(media::PIPELINE_OK)); |
| 147 message_loop_->RunAllPending(); |
| 148 } |
| 149 |
| 150 void Play() { |
| 151 decoder_->Play(media::NewExpectedClosure()); |
| 152 message_loop_->RunAllPending(); |
| 153 } |
| 154 |
| 155 void Stop() { |
| 156 EXPECT_CALL(*vc_impl_, StopCapture(capture_client())) |
| 157 .Times(1) |
| 158 .WillOnce(CaptureStopped(capture_client(), vc_impl_.get())); |
| 159 EXPECT_CALL(*vc_manager_, RemoveDevice(_, _)) |
| 160 .WillOnce(Return()); |
| 161 decoder_->Stop(media::NewExpectedClosure()); |
| 162 message_loop_->RunAllPending(); |
| 163 } |
| 164 |
| 165 media::VideoCapture::EventHandler* capture_client() { |
| 166 return static_cast<media::VideoCapture::EventHandler*>(decoder_); |
| 167 } |
| 168 |
114 MOCK_METHOD1(FrameReady, void(scoped_refptr<media::VideoFrame>)); | 169 MOCK_METHOD1(FrameReady, void(scoped_refptr<media::VideoFrame>)); |
115 | 170 |
116 // Fixture members. | 171 // Fixture members. |
117 scoped_refptr<CaptureVideoDecoder> decoder_; | 172 scoped_refptr<CaptureVideoDecoder> decoder_; |
118 scoped_refptr<MockVideoCaptureImplManager> vc_manager_; | 173 scoped_refptr<MockVideoCaptureImplManager> vc_manager_; |
| 174 scoped_ptr<MockVideoCaptureImpl> vc_impl_; |
119 media::MockStatisticsCallback statistics_callback_object_; | 175 media::MockStatisticsCallback statistics_callback_object_; |
120 StrictMock<media::MockFilterHost> host_; | 176 StrictMock<media::MockFilterHost> host_; |
121 scoped_ptr<MessageLoop> message_loop_; | 177 scoped_ptr<MessageLoop> message_loop_; |
122 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 178 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
123 media::VideoDecoder::ReadCB read_cb_; | 179 media::VideoDecoder::ReadCB read_cb_; |
124 | 180 |
125 private: | 181 private: |
126 DISALLOW_COPY_AND_ASSIGN(CaptureVideoDecoderTest); | 182 DISALLOW_COPY_AND_ASSIGN(CaptureVideoDecoderTest); |
127 }; | 183 }; |
128 | 184 |
| 185 TEST_F(CaptureVideoDecoderTest, Initialize) { |
| 186 // Test basic initialize and teardown. |
| 187 Initialize(); |
| 188 |
| 189 // Natural size should be initialized to default capability. |
| 190 EXPECT_EQ(kWidth, decoder_->natural_size().width()); |
| 191 EXPECT_EQ(kHeight, decoder_->natural_size().height()); |
| 192 |
| 193 Stop(); |
| 194 } |
| 195 |
129 TEST_F(CaptureVideoDecoderTest, Play) { | 196 TEST_F(CaptureVideoDecoderTest, Play) { |
130 int data_buffer_number = 1; | 197 // Test basic initialize, play, and teardown sequence. |
131 media::VideoCapture::EventHandler* capture_client = | 198 Initialize(); |
132 static_cast<media::VideoCapture::EventHandler*>(decoder_); | 199 Start(); |
133 scoped_ptr<MockVideoCaptureImpl> vc_impl( | 200 Play(); |
134 new MockVideoCaptureImpl(kVideoStreamId, | 201 Stop(); |
135 message_loop_proxy_, | 202 } |
136 new VideoCaptureMessageFilter())); | |
137 | 203 |
138 EXPECT_CALL(*vc_manager_, AddDevice(_, _)) | 204 TEST_F(CaptureVideoDecoderTest, OnDeviceInfoReceived) { |
139 .WillOnce(Return(vc_impl.get())); | 205 // Test that natural size gets updated as device information is sent. |
140 decoder_->Initialize(NULL, | 206 Initialize(); |
141 media::NewExpectedClosure(), | 207 Start(); |
142 NewStatisticsCallback()); | 208 |
| 209 gfx::Size expected_size(kWidth * 2, kHeight * 2); |
| 210 |
| 211 media::VideoCaptureParams params; |
| 212 params.width = expected_size.width(); |
| 213 params.height = expected_size.height(); |
| 214 params.frame_per_second = kFPS; |
| 215 params.session_id = kVideoStreamId; |
| 216 |
| 217 EXPECT_CALL(host_, SetNaturalVideoSize(expected_size)); |
| 218 decoder_->OnDeviceInfoReceived(vc_impl_.get(), params); |
143 message_loop_->RunAllPending(); | 219 message_loop_->RunAllPending(); |
144 | 220 |
145 EXPECT_CALL(*this, FrameReady(_)); | 221 EXPECT_EQ(expected_size.width(), decoder_->natural_size().width()); |
146 decoder_->Read(read_cb_); | 222 EXPECT_EQ(expected_size.height(), decoder_->natural_size().height()); |
147 | 223 |
148 EXPECT_CALL(*vc_impl, StartCapture(capture_client, _)) | 224 Stop(); |
149 .Times(1) | |
150 .WillOnce(CreateDataBufferFromCapture(capture_client, vc_impl.get(), | |
151 data_buffer_number)); | |
152 EXPECT_CALL(*vc_impl, FeedBuffer(_)) | |
153 .Times(data_buffer_number) | |
154 .WillRepeatedly(DeleteDataBuffer()); | |
155 decoder_->Seek(base::TimeDelta(), | |
156 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
157 decoder_->Play(media::NewExpectedClosure()); | |
158 message_loop_->RunAllPending(); | |
159 | |
160 EXPECT_CALL(*vc_impl, StopCapture(capture_client)) | |
161 .Times(1) | |
162 .WillOnce(CaptureStopped(capture_client, vc_impl.get())); | |
163 EXPECT_CALL(*vc_manager_, RemoveDevice(_, _)) | |
164 .WillOnce(Return()); | |
165 decoder_->Stop(media::NewExpectedClosure()); | |
166 message_loop_->RunAllPending(); | |
167 } | 225 } |
OLD | NEW |