Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(628)

Side by Side Diff: media/capture/video/video_capture_system.cc

Issue 2769543002: [Mojo Video Capture] Introduce abstraction VideoCaptureSystem (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "media/capture/video/video_capture_system.h"
6
7 #include "media/base/bind_to_current_loop.h"
8
9 namespace {
10
11 // Compares two VideoCaptureFormat by checking smallest frame_size area, then
12 // by _largest_ frame_rate. Used to order a VideoCaptureFormats vector so that
13 // the first entry for a given resolution has the largest frame rate, as needed
14 // by the ConsolidateCaptureFormats() method.
15 bool IsCaptureFormatSmaller(const media::VideoCaptureFormat& format1,
16 const media::VideoCaptureFormat& format2) {
17 DCHECK(format1.frame_size.GetCheckedArea().IsValid());
18 DCHECK(format2.frame_size.GetCheckedArea().IsValid());
19 if (format1.frame_size.GetCheckedArea().ValueOrDefault(0) ==
20 format2.frame_size.GetCheckedArea().ValueOrDefault(0)) {
21 return format1.frame_rate > format2.frame_rate;
22 }
23 return format1.frame_size.GetCheckedArea().ValueOrDefault(0) <
24 format2.frame_size.GetCheckedArea().ValueOrDefault(0);
25 }
26
27 bool IsCaptureFormatSizeEqual(const media::VideoCaptureFormat& format1,
28 const media::VideoCaptureFormat& format2) {
29 DCHECK(format1.frame_size.GetCheckedArea().IsValid());
30 DCHECK(format2.frame_size.GetCheckedArea().IsValid());
31 return format1.frame_size.GetCheckedArea().ValueOrDefault(0) ==
32 format2.frame_size.GetCheckedArea().ValueOrDefault(0);
33 }
34
35 // This function receives a list of capture formats, removes duplicated
36 // resolutions while keeping the highest frame rate for each, and forcing I420
37 // pixel format.
38 void ConsolidateCaptureFormats(media::VideoCaptureFormats* formats) {
39 if (formats->empty())
40 return;
41 std::sort(formats->begin(), formats->end(), IsCaptureFormatSmaller);
42 // Due to the ordering imposed, the largest frame_rate is kept while removing
43 // duplicated resolutions.
44 media::VideoCaptureFormats::iterator last =
45 std::unique(formats->begin(), formats->end(), IsCaptureFormatSizeEqual);
46 formats->erase(last, formats->end());
47 // Mark all formats as I420, since this is what the renderer side will get
48 // anyhow: the actual pixel format is decided at the device level.
49 // Don't do this for Y16 format as it is handled separatelly.
50 for (auto& format : *formats) {
51 if (format.pixel_format != media::PIXEL_FORMAT_Y16)
52 format.pixel_format = media::PIXEL_FORMAT_I420;
53 }
54 }
55
56 } // anonymous namespace
57
58 namespace media {
59
60 VideoCaptureSystem::VideoCaptureSystem(
61 std::unique_ptr<VideoCaptureDeviceFactory> factory)
62 : factory_(std::move(factory)) {}
63
64 VideoCaptureSystem::~VideoCaptureSystem() = default;
65
66 void VideoCaptureSystem::GetDeviceInfosAsync(
67 const base::Callback<void(const std::vector<VideoCaptureDeviceInfo>&)>&
68 result_callback) {
69 factory_->EnumerateDeviceDescriptors(media::BindToCurrentLoop(
70 base::Bind(&VideoCaptureSystem::OnDescriptorsReceived,
71 base::Unretained(this), result_callback)));
72 }
73
74 // Creates a VideoCaptureDevice object. Returns NULL if something goes wrong.
75 std::unique_ptr<VideoCaptureDevice> VideoCaptureSystem::CreateDevice(
76 const std::string device_id) {
77 media::VideoCaptureDeviceDescriptor descriptor;
78 if (LookupDescriptorFromId(device_id, &descriptor) == false)
79 return nullptr;
80 return factory_->CreateDevice(descriptor);
81 }
82
83 void VideoCaptureSystem::OnDescriptorsReceived(
84 const base::Callback<void(const std::vector<VideoCaptureDeviceInfo>&)>&
85 result_callback,
86 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors) {
87 // For devices for which we already have an entry in |devices_info_cache_|,
88 // we do not want to query the |factory_| for supported formats again. We
89 // simply copy them from |devices_info_cache_|.
miu 2017/03/22 22:51:23 nit: Add: "If |descriptors| does not contain a des
chfremer 2017/03/23 00:34:13 With your suggested improvement from below, I thin
90 std::vector<VideoCaptureDeviceInfo> new_devices_info_cache;
91 for (const auto& device_info : devices_info_cache_) {
miu 2017/03/22 22:51:23 On a second review of this code, it seems this who
chfremer 2017/03/23 00:34:13 Thanks. That makes things much easier to read. Don
92 for (auto it = descriptors->begin(); it != descriptors->end(); ++it) {
93 if (device_info.descriptor.device_id == it->device_id) {
94 new_devices_info_cache.push_back(device_info);
95 descriptors->erase(it);
96 break;
97 }
98 }
99 }
100
101 // For the devices for which we did not have entries in |devices_info_cache_|,
102 // we need to query for supported formats in order to create the entries.
103 for (const auto& it : *descriptors) {
104 VideoCaptureDeviceInfo device_info(it);
105 factory_->GetSupportedFormats(it, &device_info.supported_formats);
106 ConsolidateCaptureFormats(&device_info.supported_formats);
107 new_devices_info_cache.push_back(device_info);
108 }
109
110 devices_info_cache_ = new_devices_info_cache;
miu 2017/03/22 22:51:23 nit: devices_info_cache_.swap(new_devices_info_cac
chfremer 2017/03/23 00:34:13 Done.
111 result_callback.Run(new_devices_info_cache);
miu 2017/03/22 22:51:23 s/new_devices_info_cache/devices_info_cache_/ (bec
chfremer 2017/03/23 00:34:13 Done.
112 }
113
114 bool VideoCaptureSystem::LookupDescriptorFromId(
miu 2017/03/22 22:51:23 Along the lines of improvements made on prior CLs,
chfremer 2017/03/23 00:34:13 Done.
115 const std::string& device_id,
116 media::VideoCaptureDeviceDescriptor* descriptor) {
117 auto iter = std::find_if(
118 devices_info_cache_.begin(), devices_info_cache_.end(),
119 [&device_id](const media::VideoCaptureDeviceInfo& device_info) {
120 return device_info.descriptor.device_id == device_id;
121 });
122 if (iter == devices_info_cache_.end())
123 return false;
124 *descriptor = iter->descriptor;
125 return true;
126 }
127
128 } // namespace media
OLDNEW
« media/capture/video/video_capture_system.h ('K') | « media/capture/video/video_capture_system.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698