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

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

Issue 2769543002: [Mojo Video Capture] Introduce abstraction VideoCaptureSystem (Closed)
Patch Set: Fix for code that should only be run for MEDIA_DEVICE_VIDEO_CAPTURE Created 3 years, 8 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 2017 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 DeviceInfoCallback& result_callback) {
68 factory_->EnumerateDeviceDescriptors(media::BindToCurrentLoop(
emircan 2017/04/03 19:04:28 It seems like VideoCaptureDeviceFactory::Enumerate
chfremer 2017/04/04 19:05:33 Agreed that this is the case. I'd like to defer th
69 base::Bind(&VideoCaptureSystem::OnDescriptorsReceived,
70 base::Unretained(this), result_callback)));
emircan 2017/04/03 19:04:28 Can you add a comment explaining base::Unretained(
chfremer 2017/04/04 19:05:33 Done.
71 }
72
73 // Creates a VideoCaptureDevice object. Returns NULL if something goes wrong.
74 std::unique_ptr<VideoCaptureDevice> VideoCaptureSystem::CreateDevice(
75 const std::string& device_id) {
76 const VideoCaptureDeviceInfo* device_info = LookupDeviceInfoFromId(device_id);
77 if (!device_info)
78 return nullptr;
79 return factory_->CreateDevice(device_info->descriptor);
80 }
81
82 void VideoCaptureSystem::OnDescriptorsReceived(
83 const DeviceInfoCallback& result_callback,
84 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors) {
emircan 2017/04/03 19:04:28 Can you add thread_checker to this class? It would
chfremer 2017/04/04 19:05:33 Done.
85 // For devices for which we already have an entry in |devices_info_cache_|,
86 // we do not want to query the |factory_| for supported formats again. We
87 // simply copy them from |devices_info_cache_|.
88 std::vector<VideoCaptureDeviceInfo> new_devices_info_cache;
89 new_devices_info_cache.reserve(descriptors->size());
90 for (const auto& descriptor : *descriptors) {
91 if (auto* cached_info = LookupDeviceInfoFromId(descriptor.device_id)) {
92 new_devices_info_cache.push_back(*cached_info);
93 } else {
94 // Query for supported formats in order to create the entry.
95 VideoCaptureDeviceInfo device_info(descriptor);
96 factory_->GetSupportedFormats(descriptor, &device_info.supported_formats);
97 ConsolidateCaptureFormats(&device_info.supported_formats);
98 new_devices_info_cache.push_back(device_info);
99 }
100 }
101
102 devices_info_cache_.swap(new_devices_info_cache);
103 result_callback.Run(devices_info_cache_);
104 }
105
106 const VideoCaptureDeviceInfo* VideoCaptureSystem::LookupDeviceInfoFromId(
107 const std::string& device_id) {
108 auto iter = std::find_if(
109 devices_info_cache_.begin(), devices_info_cache_.end(),
110 [&device_id](const media::VideoCaptureDeviceInfo& device_info) {
111 return device_info.descriptor.device_id == device_id;
112 });
113 if (iter == devices_info_cache_.end())
114 return nullptr;
115 return &(*iter);
116 }
117
118 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698