OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "media/capture/video/video_capture_system_impl.h" | 5 #include "media/capture/video/video_capture_system_impl.h" |
6 | 6 |
7 #include "media/base/bind_to_current_loop.h" | 7 #include "media/base/bind_to_current_loop.h" |
8 | 8 |
9 namespace { | 9 namespace { |
10 | 10 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 std::unique_ptr<VideoCaptureDeviceFactory> factory) | 61 std::unique_ptr<VideoCaptureDeviceFactory> factory) |
62 : factory_(std::move(factory)) { | 62 : factory_(std::move(factory)) { |
63 thread_checker_.DetachFromThread(); | 63 thread_checker_.DetachFromThread(); |
64 } | 64 } |
65 | 65 |
66 VideoCaptureSystemImpl::~VideoCaptureSystemImpl() = default; | 66 VideoCaptureSystemImpl::~VideoCaptureSystemImpl() = default; |
67 | 67 |
68 void VideoCaptureSystemImpl::GetDeviceInfosAsync( | 68 void VideoCaptureSystemImpl::GetDeviceInfosAsync( |
69 const DeviceInfoCallback& result_callback) { | 69 const DeviceInfoCallback& result_callback) { |
70 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
71 // Use of Unretained() is safe assuming that |result_callback| has ownership | 71 VideoCaptureDeviceDescriptors descriptors; |
72 // of |this|. | 72 factory_->GetDeviceDescriptors(&descriptors); |
73 factory_->EnumerateDeviceDescriptors(media::BindToCurrentLoop( | |
74 base::Bind(&VideoCaptureSystemImpl::OnDescriptorsReceived, | |
75 base::Unretained(this), result_callback))); | |
76 } | |
77 | |
78 // Creates a VideoCaptureDevice object. Returns NULL if something goes wrong. | |
79 std::unique_ptr<VideoCaptureDevice> VideoCaptureSystemImpl::CreateDevice( | |
80 const std::string& device_id) { | |
81 DCHECK(thread_checker_.CalledOnValidThread()); | |
82 const VideoCaptureDeviceInfo* device_info = LookupDeviceInfoFromId(device_id); | |
83 if (!device_info) | |
84 return nullptr; | |
85 return factory_->CreateDevice(device_info->descriptor); | |
86 } | |
87 | |
88 void VideoCaptureSystemImpl::OnDescriptorsReceived( | |
89 const DeviceInfoCallback& result_callback, | |
90 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors) { | |
91 DCHECK(thread_checker_.CalledOnValidThread()); | |
92 // For devices for which we already have an entry in |devices_info_cache_|, | 73 // For devices for which we already have an entry in |devices_info_cache_|, |
93 // we do not want to query the |factory_| for supported formats again. We | 74 // we do not want to query the |factory_| for supported formats again. We |
94 // simply copy them from |devices_info_cache_|. | 75 // simply copy them from |devices_info_cache_|. |
95 std::vector<VideoCaptureDeviceInfo> new_devices_info_cache; | 76 std::vector<VideoCaptureDeviceInfo> new_devices_info_cache; |
96 new_devices_info_cache.reserve(descriptors->size()); | 77 new_devices_info_cache.reserve(descriptors.size()); |
97 for (const auto& descriptor : *descriptors) { | 78 for (const auto& descriptor : descriptors) { |
98 if (auto* cached_info = LookupDeviceInfoFromId(descriptor.device_id)) { | 79 if (auto* cached_info = LookupDeviceInfoFromId(descriptor.device_id)) { |
99 new_devices_info_cache.push_back(*cached_info); | 80 new_devices_info_cache.push_back(*cached_info); |
100 } else { | 81 } else { |
101 // Query for supported formats in order to create the entry. | 82 // Query for supported formats in order to create the entry. |
102 VideoCaptureDeviceInfo device_info(descriptor); | 83 VideoCaptureDeviceInfo device_info(descriptor); |
103 factory_->GetSupportedFormats(descriptor, &device_info.supported_formats); | 84 factory_->GetSupportedFormats(descriptor, &device_info.supported_formats); |
104 ConsolidateCaptureFormats(&device_info.supported_formats); | 85 ConsolidateCaptureFormats(&device_info.supported_formats); |
105 new_devices_info_cache.push_back(device_info); | 86 new_devices_info_cache.push_back(device_info); |
106 } | 87 } |
107 } | 88 } |
108 | 89 |
109 devices_info_cache_.swap(new_devices_info_cache); | 90 devices_info_cache_.swap(new_devices_info_cache); |
110 result_callback.Run(devices_info_cache_); | 91 result_callback.Run(devices_info_cache_); |
111 } | 92 } |
112 | 93 |
| 94 // Creates a VideoCaptureDevice object. Returns NULL if something goes wrong. |
| 95 std::unique_ptr<VideoCaptureDevice> VideoCaptureSystemImpl::CreateDevice( |
| 96 const std::string& device_id) { |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); |
| 98 const VideoCaptureDeviceInfo* device_info = LookupDeviceInfoFromId(device_id); |
| 99 if (!device_info) |
| 100 return nullptr; |
| 101 return factory_->CreateDevice(device_info->descriptor); |
| 102 } |
| 103 |
113 const VideoCaptureDeviceInfo* VideoCaptureSystemImpl::LookupDeviceInfoFromId( | 104 const VideoCaptureDeviceInfo* VideoCaptureSystemImpl::LookupDeviceInfoFromId( |
114 const std::string& device_id) { | 105 const std::string& device_id) { |
115 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
116 auto iter = std::find_if( | 107 auto iter = std::find_if( |
117 devices_info_cache_.begin(), devices_info_cache_.end(), | 108 devices_info_cache_.begin(), devices_info_cache_.end(), |
118 [&device_id](const media::VideoCaptureDeviceInfo& device_info) { | 109 [&device_id](const media::VideoCaptureDeviceInfo& device_info) { |
119 return device_info.descriptor.device_id == device_id; | 110 return device_info.descriptor.device_id == device_id; |
120 }); | 111 }); |
121 if (iter == devices_info_cache_.end()) | 112 if (iter == devices_info_cache_.end()) |
122 return nullptr; | 113 return nullptr; |
123 return &(*iter); | 114 return &(*iter); |
124 } | 115 } |
125 | 116 |
126 } // namespace media | 117 } // namespace media |
OLD | NEW |