OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/media/capture/web_contents_video_capture_device.h" | 5 #include "content/browser/media/capture/web_contents_video_capture_device.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "content/public/test/test_browser_thread_bundle.h" | 30 #include "content/public/test/test_browser_thread_bundle.h" |
31 #include "content/public/test/test_utils.h" | 31 #include "content/public/test/test_utils.h" |
32 #include "content/test/test_render_frame_host_factory.h" | 32 #include "content/test/test_render_frame_host_factory.h" |
33 #include "content/test/test_render_view_host.h" | 33 #include "content/test/test_render_view_host.h" |
34 #include "content/test/test_web_contents.h" | 34 #include "content/test/test_web_contents.h" |
35 #include "media/base/video_frame.h" | 35 #include "media/base/video_frame.h" |
36 #include "media/base/video_util.h" | 36 #include "media/base/video_util.h" |
37 #include "media/base/yuv_convert.h" | 37 #include "media/base/yuv_convert.h" |
38 #include "media/capture/video/video_capture_buffer_pool_impl.h" | 38 #include "media/capture/video/video_capture_buffer_pool_impl.h" |
39 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" | 39 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" |
| 40 #include "media/capture/video/video_capture_device_client.h" |
40 #include "media/capture/video_capture_types.h" | 41 #include "media/capture/video_capture_types.h" |
41 #include "skia/ext/platform_canvas.h" | 42 #include "skia/ext/platform_canvas.h" |
42 #include "testing/gmock/include/gmock/gmock.h" | 43 #include "testing/gmock/include/gmock/gmock.h" |
43 #include "testing/gtest/include/gtest/gtest.h" | 44 #include "testing/gtest/include/gtest/gtest.h" |
44 #include "third_party/skia/include/core/SkColor.h" | 45 #include "third_party/skia/include/core/SkColor.h" |
45 #include "ui/base/layout.h" | 46 #include "ui/base/layout.h" |
46 #include "ui/display/display.h" | 47 #include "ui/display/display.h" |
47 #include "ui/display/screen.h" | 48 #include "ui/display/screen.h" |
48 #include "ui/display/test/test_screen.h" | 49 #include "ui/display/test/test_screen.h" |
49 #include "ui/gfx/geometry/dip_util.h" | 50 #include "ui/gfx/geometry/dip_util.h" |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 void(const uint8_t* data, | 237 void(const uint8_t* data, |
237 int length, | 238 int length, |
238 const media::VideoCaptureFormat& frame_format, | 239 const media::VideoCaptureFormat& frame_format, |
239 int rotation, | 240 int rotation, |
240 base::TimeTicks reference_time, | 241 base::TimeTicks reference_time, |
241 base::TimeDelta timestamp, | 242 base::TimeDelta timestamp, |
242 int frame_feedback_id)); | 243 int frame_feedback_id)); |
243 | 244 |
244 MOCK_METHOD0(DoOnIncomingCapturedBuffer, void(void)); | 245 MOCK_METHOD0(DoOnIncomingCapturedBuffer, void(void)); |
245 | 246 |
246 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> | 247 media::VideoCaptureDevice::Client::Buffer ReserveOutputBuffer( |
247 ReserveOutputBuffer(const gfx::Size& dimensions, | 248 const gfx::Size& dimensions, |
248 media::VideoPixelFormat format, | 249 media::VideoPixelFormat format, |
249 media::VideoPixelStorage storage, | 250 media::VideoPixelStorage storage, |
250 int frame_feedback_id) override { | 251 int frame_feedback_id) override { |
251 CHECK_EQ(format, media::PIXEL_FORMAT_I420); | 252 CHECK_EQ(format, media::PIXEL_FORMAT_I420); |
252 int buffer_id_to_drop = | 253 int buffer_id_to_drop = |
253 media::VideoCaptureBufferPool::kInvalidId; // Ignored. | 254 media::VideoCaptureBufferPool::kInvalidId; // Ignored. |
254 const int buffer_id = buffer_pool_->ReserveForProducer( | 255 const int buffer_id = buffer_pool_->ReserveForProducer( |
255 dimensions, format, storage, frame_feedback_id, &buffer_id_to_drop); | 256 dimensions, format, storage, frame_feedback_id, &buffer_id_to_drop); |
256 if (buffer_id == media::VideoCaptureBufferPool::kInvalidId) | 257 if (buffer_id == media::VideoCaptureBufferPool::kInvalidId) |
257 return NULL; | 258 return media::VideoCaptureDevice::Client::Buffer(); |
258 | 259 |
259 return std::unique_ptr<media::VideoCaptureDevice::Client::Buffer>( | 260 return media::VideoCaptureDeviceClient::MakeBufferStruct( |
260 new AutoReleaseBuffer(buffer_pool_, | 261 buffer_pool_, buffer_id, frame_feedback_id); |
261 buffer_pool_->GetBufferHandle(buffer_id), | |
262 buffer_id, frame_feedback_id)); | |
263 } | 262 } |
264 | 263 |
265 // Trampoline method to workaround GMOCK problems with std::unique_ptr<>. | 264 // Trampoline method to workaround GMOCK problems with std::unique_ptr<>. |
266 void OnIncomingCapturedBuffer(std::unique_ptr<Buffer> buffer, | 265 void OnIncomingCapturedBuffer(Buffer buffer, |
267 const media::VideoCaptureFormat& format, | 266 const media::VideoCaptureFormat& format, |
268 base::TimeTicks reference_time, | 267 base::TimeTicks reference_time, |
269 base::TimeDelta timestamp) override { | 268 base::TimeDelta timestamp) override { |
270 DoOnIncomingCapturedBuffer(); | 269 DoOnIncomingCapturedBuffer(); |
271 } | 270 } |
272 | 271 |
273 void OnIncomingCapturedBufferExt( | 272 void OnIncomingCapturedBufferExt( |
274 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | 273 media::VideoCaptureDevice::Client::Buffer buffer, |
275 const media::VideoCaptureFormat& format, | 274 const media::VideoCaptureFormat& format, |
276 base::TimeTicks reference_time, | 275 base::TimeTicks reference_time, |
277 base::TimeDelta timestamp, | 276 base::TimeDelta timestamp, |
278 gfx::Rect visible_rect, | 277 gfx::Rect visible_rect, |
279 const media::VideoFrameMetadata& additional_metadata) override { | 278 const media::VideoFrameMetadata& additional_metadata) override { |
280 EXPECT_FALSE(visible_rect.IsEmpty()); | 279 EXPECT_FALSE(visible_rect.IsEmpty()); |
281 EXPECT_EQ(media::PIXEL_FORMAT_I420, format.pixel_format); | 280 EXPECT_EQ(media::PIXEL_FORMAT_I420, format.pixel_format); |
282 EXPECT_EQ(kTestFramesPerSecond, format.frame_rate); | 281 EXPECT_EQ(kTestFramesPerSecond, format.frame_rate); |
283 | 282 |
284 // TODO(miu): We just look at the center pixel presently, because if the | 283 // TODO(miu): We just look at the center pixel presently, because if the |
285 // analysis is too slow, the backlog of frames will grow without bound and | 284 // analysis is too slow, the backlog of frames will grow without bound and |
286 // trouble erupts. http://crbug.com/174519 | 285 // trouble erupts. http://crbug.com/174519 |
287 using media::VideoFrame; | 286 using media::VideoFrame; |
| 287 auto buffer_access = |
| 288 buffer.handle_provider()->GetHandleForInProcessAccess(); |
288 auto frame = VideoFrame::WrapExternalSharedMemory( | 289 auto frame = VideoFrame::WrapExternalSharedMemory( |
289 media::PIXEL_FORMAT_I420, format.frame_size, visible_rect, | 290 media::PIXEL_FORMAT_I420, format.frame_size, visible_rect, |
290 format.frame_size, static_cast<uint8_t*>(buffer->data()), | 291 format.frame_size, buffer_access->data(), buffer_access->mapped_size(), |
291 buffer->mapped_size(), base::SharedMemory::NULLHandle(), 0u, | 292 base::SharedMemory::NULLHandle(), 0u, base::TimeDelta()); |
292 base::TimeDelta()); | |
293 const gfx::Point center = visible_rect.CenterPoint(); | 293 const gfx::Point center = visible_rect.CenterPoint(); |
294 const int center_offset_y = | 294 const int center_offset_y = |
295 (frame->stride(VideoFrame::kYPlane) * center.y()) + center.x(); | 295 (frame->stride(VideoFrame::kYPlane) * center.y()) + center.x(); |
296 const int center_offset_uv = | 296 const int center_offset_uv = |
297 (frame->stride(VideoFrame::kUPlane) * (center.y() / 2)) + | 297 (frame->stride(VideoFrame::kUPlane) * (center.y() / 2)) + |
298 (center.x() / 2); | 298 (center.x() / 2); |
299 report_callback_.Run( | 299 report_callback_.Run( |
300 SkColorSetRGB(frame->data(VideoFrame::kYPlane)[center_offset_y], | 300 SkColorSetRGB(frame->data(VideoFrame::kYPlane)[center_offset_y], |
301 frame->data(VideoFrame::kUPlane)[center_offset_uv], | 301 frame->data(VideoFrame::kUPlane)[center_offset_uv], |
302 frame->data(VideoFrame::kVPlane)[center_offset_uv]), | 302 frame->data(VideoFrame::kVPlane)[center_offset_uv]), |
303 frame->visible_rect().size()); | 303 frame->visible_rect().size()); |
304 } | 304 } |
305 | 305 |
306 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> | 306 media::VideoCaptureDevice::Client::Buffer ResurrectLastOutputBuffer( |
307 ResurrectLastOutputBuffer(const gfx::Size& dimensions, | 307 const gfx::Size& dimensions, |
308 media::VideoPixelFormat format, | 308 media::VideoPixelFormat format, |
309 media::VideoPixelStorage storage, | 309 media::VideoPixelStorage storage, |
310 int frame_feedback_id) override { | 310 int frame_feedback_id) override { |
311 CHECK_EQ(format, media::PIXEL_FORMAT_I420); | 311 CHECK_EQ(format, media::PIXEL_FORMAT_I420); |
312 const int buffer_id = | 312 const int buffer_id = |
313 buffer_pool_->ResurrectLastForProducer(dimensions, format, storage); | 313 buffer_pool_->ResurrectLastForProducer(dimensions, format, storage); |
314 if (buffer_id == media::VideoCaptureBufferPool::kInvalidId) | 314 if (buffer_id == media::VideoCaptureBufferPool::kInvalidId) |
315 return nullptr; | 315 return media::VideoCaptureDevice::Client::Buffer(); |
316 return std::unique_ptr<media::VideoCaptureDevice::Client::Buffer>( | 316 return media::VideoCaptureDeviceClient::MakeBufferStruct( |
317 new AutoReleaseBuffer(buffer_pool_, | 317 buffer_pool_, buffer_id, frame_feedback_id); |
318 buffer_pool_->GetBufferHandle(buffer_id), | |
319 buffer_id, frame_feedback_id)); | |
320 } | 318 } |
321 | 319 |
322 void OnError(const tracked_objects::Location& from_here, | 320 void OnError(const tracked_objects::Location& from_here, |
323 const std::string& reason) override { | 321 const std::string& reason) override { |
324 error_callback_.Run(); | 322 error_callback_.Run(); |
325 } | 323 } |
326 | 324 |
327 double GetBufferPoolUtilization() const override { return 0.0; } | 325 double GetBufferPoolUtilization() const override { return 0.0; } |
328 | 326 |
329 private: | 327 private: |
330 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer { | |
331 public: | |
332 AutoReleaseBuffer( | |
333 const scoped_refptr<media::VideoCaptureBufferPool>& pool, | |
334 std::unique_ptr<media::VideoCaptureBufferHandle> buffer_handle, | |
335 int buffer_id, | |
336 int frame_feedback_id) | |
337 : id_(buffer_id), | |
338 frame_feedback_id_(frame_feedback_id), | |
339 pool_(pool), | |
340 buffer_handle_(std::move(buffer_handle)) { | |
341 DCHECK(pool_); | |
342 } | |
343 int id() const override { return id_; } | |
344 int frame_feedback_id() const override { return frame_feedback_id_; } | |
345 gfx::Size dimensions() const override { | |
346 return buffer_handle_->dimensions(); | |
347 } | |
348 size_t mapped_size() const override { | |
349 return buffer_handle_->mapped_size(); | |
350 } | |
351 void* data(int plane) override { return buffer_handle_->data(plane); } | |
352 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
353 base::FileDescriptor AsPlatformFile() override { | |
354 return base::FileDescriptor(); | |
355 } | |
356 #endif | |
357 bool IsBackedByVideoFrame() const override { | |
358 return buffer_handle_->IsBackedByVideoFrame(); | |
359 } | |
360 scoped_refptr<media::VideoFrame> GetVideoFrame() override { | |
361 return buffer_handle_->GetVideoFrame(); | |
362 } | |
363 | |
364 private: | |
365 ~AutoReleaseBuffer() override { pool_->RelinquishProducerReservation(id_); } | |
366 | |
367 const int id_; | |
368 const int frame_feedback_id_; | |
369 const scoped_refptr<media::VideoCaptureBufferPool> pool_; | |
370 const std::unique_ptr<media::VideoCaptureBufferHandle> buffer_handle_; | |
371 }; | |
372 | |
373 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool_; | 328 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool_; |
374 base::Callback<void(SkColor, const gfx::Size&)> report_callback_; | 329 base::Callback<void(SkColor, const gfx::Size&)> report_callback_; |
375 base::Closure error_callback_; | 330 base::Closure error_callback_; |
376 | 331 |
377 DISALLOW_COPY_AND_ASSIGN(StubClient); | 332 DISALLOW_COPY_AND_ASSIGN(StubClient); |
378 }; | 333 }; |
379 | 334 |
380 class StubClientObserver { | 335 class StubClientObserver { |
381 public: | 336 public: |
382 StubClientObserver() | 337 StubClientObserver() |
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1054 for (int i = 0; i < 3; ++i) { | 1009 for (int i = 0; i < 3; ++i) { |
1055 SimulateRefreshFrameRequest(); | 1010 SimulateRefreshFrameRequest(); |
1056 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN)); | 1011 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN)); |
1057 } | 1012 } |
1058 | 1013 |
1059 device()->StopAndDeAllocate(); | 1014 device()->StopAndDeAllocate(); |
1060 } | 1015 } |
1061 | 1016 |
1062 } // namespace | 1017 } // namespace |
1063 } // namespace content | 1018 } // namespace content |
OLD | NEW |