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

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

Issue 2805533002: Reland [Mojo Video Capture] Introduce abstraction VideoCaptureSystem (Closed)
Patch Set: Fix race condition in tests 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 thread_checker_.DetachFromThread();
64 }
65
66 VideoCaptureSystem::~VideoCaptureSystem() = default;
67
68 void VideoCaptureSystem::GetDeviceInfosAsync(
69 const DeviceInfoCallback& result_callback) {
70 DCHECK(thread_checker_.CalledOnValidThread());
71 // Use of Unretained() is safe assuming that |result_callback| has ownership
72 // of |this|.
73 factory_->EnumerateDeviceDescriptors(media::BindToCurrentLoop(
74 base::Bind(&VideoCaptureSystem::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> VideoCaptureSystem::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 VideoCaptureSystem::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_|,
93 // we do not want to query the |factory_| for supported formats again. We
94 // simply copy them from |devices_info_cache_|.
95 std::vector<VideoCaptureDeviceInfo> new_devices_info_cache;
96 new_devices_info_cache.reserve(descriptors->size());
97 for (const auto& descriptor : *descriptors) {
98 if (auto* cached_info = LookupDeviceInfoFromId(descriptor.device_id)) {
99 new_devices_info_cache.push_back(*cached_info);
100 } else {
101 // Query for supported formats in order to create the entry.
102 VideoCaptureDeviceInfo device_info(descriptor);
103 factory_->GetSupportedFormats(descriptor, &device_info.supported_formats);
104 ConsolidateCaptureFormats(&device_info.supported_formats);
105 new_devices_info_cache.push_back(device_info);
106 }
107 }
108
109 devices_info_cache_.swap(new_devices_info_cache);
110 result_callback.Run(devices_info_cache_);
111 }
112
113 const VideoCaptureDeviceInfo* VideoCaptureSystem::LookupDeviceInfoFromId(
114 const std::string& device_id) {
115 DCHECK(thread_checker_.CalledOnValidThread());
116 auto iter = std::find_if(
117 devices_info_cache_.begin(), devices_info_cache_.end(),
118 [&device_id](const media::VideoCaptureDeviceInfo& device_info) {
119 return device_info.descriptor.device_id == device_id;
120 });
121 if (iter == devices_info_cache_.end())
122 return nullptr;
123 return &(*iter);
124 }
125
126 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698