OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "services/video_capture/device_media_to_mojo_adapter.h" | 5 #include "services/video_capture/device_media_to_mojo_adapter.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "media/capture/video/video_capture_buffer_pool_impl.h" | 9 #include "media/capture/video/video_capture_buffer_pool_impl.h" |
10 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" | 10 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" |
11 #include "media/capture/video/video_capture_jpeg_decoder.h" | 11 #include "media/capture/video/video_capture_jpeg_decoder.h" |
12 #include "services/video_capture/receiver_mojo_to_media_adapter.h" | 12 #include "services/video_capture/receiver_mojo_to_media_adapter.h" |
13 | 13 |
| 14 namespace { |
| 15 |
| 16 // The maximum number of video frame buffers in-flight at any one time. |
| 17 // If all buffers are still in use by consumers when new frames are produced |
| 18 // those frames get dropped. |
| 19 static const int kMaxBufferCount = 3; |
| 20 } |
| 21 |
14 namespace video_capture { | 22 namespace video_capture { |
15 | 23 |
16 DeviceMediaToMojoAdapter::DeviceMediaToMojoAdapter( | 24 DeviceMediaToMojoAdapter::DeviceMediaToMojoAdapter( |
17 std::unique_ptr<media::VideoCaptureDevice> device, | 25 std::unique_ptr<media::VideoCaptureDevice> device, |
18 const media::VideoCaptureJpegDecoderFactoryCB& | 26 const media::VideoCaptureJpegDecoderFactoryCB& |
19 jpeg_decoder_factory_callback) | 27 jpeg_decoder_factory_callback) |
20 : device_(std::move(device)), | 28 : device_(std::move(device)), |
21 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback), | 29 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback), |
22 device_running_(false) {} | 30 device_running_(false) {} |
23 | 31 |
24 DeviceMediaToMojoAdapter::~DeviceMediaToMojoAdapter() { | 32 DeviceMediaToMojoAdapter::~DeviceMediaToMojoAdapter() { |
25 Stop(); | 33 if (device_running_) |
| 34 device_->StopAndDeAllocate(); |
26 } | 35 } |
27 | 36 |
28 void DeviceMediaToMojoAdapter::Start( | 37 void DeviceMediaToMojoAdapter::Start( |
29 const media::VideoCaptureParams& requested_settings, | 38 const media::VideoCaptureParams& requested_settings, |
30 mojom::ReceiverPtr receiver) { | 39 mojom::ReceiverPtr receiver) { |
31 receiver.set_connection_error_handler( | 40 receiver.set_connection_error_handler( |
32 base::Bind(&DeviceMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 41 base::Bind(&DeviceMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
33 base::Unretained(this))); | 42 base::Unretained(this))); |
34 | 43 |
35 auto media_receiver = | 44 auto media_receiver = |
36 base::MakeUnique<ReceiverMojoToMediaAdapter>(std::move(receiver)); | 45 base::MakeUnique<ReceiverMojoToMediaAdapter>(std::move(receiver)); |
37 | 46 |
38 // Create a dedicated buffer pool for the device usage session. | 47 // Create a dedicated buffer pool for the device usage session. |
39 const int kMaxBufferCount = 2; | |
40 auto buffer_tracker_factory = | 48 auto buffer_tracker_factory = |
41 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(); | 49 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(); |
42 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool( | 50 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool( |
43 new media::VideoCaptureBufferPoolImpl(std::move(buffer_tracker_factory), | 51 new media::VideoCaptureBufferPoolImpl(std::move(buffer_tracker_factory), |
44 kMaxBufferCount)); | 52 max_buffer_pool_buffer_count())); |
45 | 53 |
46 auto device_client = base::MakeUnique<media::VideoCaptureDeviceClient>( | 54 auto device_client = base::MakeUnique<media::VideoCaptureDeviceClient>( |
47 std::move(media_receiver), buffer_pool, jpeg_decoder_factory_callback_); | 55 std::move(media_receiver), buffer_pool, jpeg_decoder_factory_callback_); |
48 | 56 |
49 device_->AllocateAndStart(requested_settings, std::move(device_client)); | 57 device_->AllocateAndStart(requested_settings, std::move(device_client)); |
50 device_running_ = true; | 58 device_running_ = true; |
51 } | 59 } |
52 | 60 |
| 61 void DeviceMediaToMojoAdapter::OnReceiverReportingUtilization( |
| 62 int32_t frame_feedback_id, |
| 63 double utilization) { |
| 64 device_->OnUtilizationReport(frame_feedback_id, utilization); |
| 65 } |
| 66 |
53 void DeviceMediaToMojoAdapter::Stop() { | 67 void DeviceMediaToMojoAdapter::Stop() { |
54 if (device_running_ == false) | 68 if (device_running_ == false) |
55 return; | 69 return; |
| 70 device_running_ = false; |
56 device_->StopAndDeAllocate(); | 71 device_->StopAndDeAllocate(); |
57 device_running_ = false; | |
58 } | 72 } |
59 | 73 |
60 void DeviceMediaToMojoAdapter::OnClientConnectionErrorOrClose() { | 74 void DeviceMediaToMojoAdapter::OnClientConnectionErrorOrClose() { |
61 Stop(); | 75 Stop(); |
62 } | 76 } |
63 | 77 |
| 78 // static |
| 79 int DeviceMediaToMojoAdapter::max_buffer_pool_buffer_count() { |
| 80 return kMaxBufferCount; |
| 81 } |
| 82 |
64 } // namespace video_capture | 83 } // namespace video_capture |
OLD | NEW |