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

Side by Side Diff: components/video_capture/video_capture_handler_impl.cc

Issue 1637793004: [WIP] Mojo Video Capture thingy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: heave!! Created 4 years, 10 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 2016 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 "components/video_capture/video_capture_handler_impl.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/guid.h"
11 #include "base/logging.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/trace_event/trace_event.h"
14 #include "components/video_capture/stream_impl.h"
15 #include "components/video_capture/video_capture_adapter.h"
16 #include "media/base/video_capture_types.h"
17 #include "media/capture/video/video_capture_device_factory.h"
18 #include "mojo/converters/geometry/geometry_type_converters.h"
19
20 namespace video_capture {
21
22 VideoCaptureHandlerImpl::VideoCaptureHandlerImpl(mojo::ApplicationImpl* app)
23 : app_(app),
24 video_capture_device_factory_(
25 media::VideoCaptureDeviceFactory::CreateFactory(
26 base::ThreadTaskRunnerHandle::Get())),
27 weak_factory_(this) {
28
29 // Enumerating devices is something that takes a while (up to hundred of ms in
30 // certain OSs).
31 // TODO(mcasas): Add a UMA like in VideoCaptureManager::EnumerateDevices().
32 TRACE_EVENT_ASYNC_BEGIN0("video_capture", "EnumerateDeviceNames",
33 this /* id */);
34 video_capture_device_factory_->EnumerateDeviceNames(
35 base::Bind(&VideoCaptureHandlerImpl::OnVideoCaptureDevicesEnumerated,
36 weak_factory_.GetWeakPtr()));
37
38 // Sadly this SystemMonitor does not do much for Video devices since both
39 // DeviceMonitorLinux and DeviceMonitorMac are in content/browser_main_loop.
40 // TODO(mcasas): Refactor them out of content/ and reuse them.
41 base::SystemMonitor::Get()->AddDevicesChangedObserver(this);
42 }
43
44 VideoCaptureHandlerImpl::~VideoCaptureHandlerImpl() {
45 DCHECK(thread_checker_.CalledOnValidThread());
46 for (const auto& id_and_device_and_stream: device_and_streams_)
47 id_and_device_and_stream.second.first->StopAndDeAllocate();
48
49 base::SystemMonitor::Get()->RemoveDevicesChangedObserver(this);
50 }
51
52 void VideoCaptureHandlerImpl::RequestMediaDevices(
53 mojom::MediaDevicesInfoRequestPtr request,
54 const RequestMediaDevicesCallback& callback) {
55 DVLOG(1) << __FUNCTION__;
56 DCHECK(thread_checker_.CalledOnValidThread());
57
58 mojom::MediaDevicesInfoReplyPtr reply = mojom::MediaDevicesInfoReply::New();
59 reply->request_id = request->request_id;
60 if (!device_names_->empty()) {
61 for (const auto& device_name : *device_names_) {
62 mojom::WebSourceInfoPtr web_source_info = mojom::WebSourceInfo::New();
63
64 // Send a GUID of the Video Capture id to hide sensitive information.
65 const std::string guid = base::GenerateGUID();
66 public_to_private_id_map_[guid] = device_name.id();
67 web_source_info->device_id = guid;
68
69 web_source_info->label = device_name.GetNameAndModel();
70 web_source_info->kind = mojom::SourceKind::Video;
71 web_source_info->facing_mode = mojom::VideoFacingMode::None;
72
73 reply->sources.push_back(std::move(web_source_info));
74 }
75 }
76 callback.Run(std::move(reply));
77 }
78
79 void VideoCaptureHandlerImpl::RequestStream(
80 mojom::StreamOptionsPtr options,
81 const mojo::String& security_origin,
82 const RequestStreamCallback& callback) {
83 DVLOG(1) << __FUNCTION__ << " device guid: " << options->device_id;
84 DCHECK(thread_checker_.CalledOnValidThread());
85
86 const std::string& guid = options->device_id;
87 if (!ContainsKey(public_to_private_id_map_, guid)) {
88 DLOG(ERROR) << "Device identified as " << guid << " not found!";
89 return;
90 }
91
92 // Try to find |options->id| in the list of devices. If not found -> return.
93 const std::string& id = public_to_private_id_map_[guid];
94 auto name = std::find_if(device_names_->begin(), device_names_->end(),
95 [id](const media::VideoCaptureDevice::Name& name) {
96 return name.id() == id;
97 });
98 if (name == device_names_->end()) {
99 DLOG(ERROR) << "device " << options->device_id << " not found.";
100 callback.Run(nullptr);
101 return;
102 }
103
104 // TODO(mcasas): At this point we need to ask the user for permission to use
105 // this device if it hasn't been granted before. Soldier on for the time being
106 // and revisit this part when a mojo permission app is available.
107
108 media::VideoCaptureParams params;
109 params.requested_format.frame_size = options->capture_size.To<gfx::Size>();
110 params.requested_format.frame_rate = options->frame_rate;
111 params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
112
113 // TODO(mcasas): hold on to |stream|, so the user can stop it.
114 mojom::StreamPtr stream_ptr;
115 scoped_ptr<StreamImpl> stream = make_scoped_ptr(new StreamImpl(
116 mojo::GetProxy(&stream_ptr), base::Bind(&VideoCaptureHandlerImpl::OnStart,
117 weak_factory_.GetWeakPtr(), id),
118 base::Bind(&VideoCaptureHandlerImpl::OnStopOrError,
119 weak_factory_.GetWeakPtr(), id),
120 base::Bind(&VideoCaptureHandlerImpl::OnStopOrError,
121 weak_factory_.GetWeakPtr(), id)));
122
123 // Create a VideoCaptureAdapter using |stream_| as Delegate.
124 scoped_ptr<media::VideoCaptureDevice::Client> device_client(
125 new VideoCaptureAdapter(stream.get()));
126
127 // Create the found device and Start() with the chosen VideoCaptureParameters.
128 scoped_ptr<media::VideoCaptureDevice> video_capture_device =
129 video_capture_device_factory_->Create(*name);
130 if (!video_capture_device) {
131 DLOG(ERROR) << "device " << id << " could not be created.";
132 callback.Run(nullptr);
133 return;
134 }
135
136 video_capture_device->AllocateAndStart(params, std::move(device_client));
137
138 device_and_streams_.insert(make_pair(
139 id, make_pair(std::move(video_capture_device), std::move(stream))));
140
141 callback.Run(std::move(stream_ptr));
142 }
143
144 void VideoCaptureHandlerImpl::OnDevicesChanged(
145 base::SystemMonitor::DeviceType device_type) {
146 DVLOG(1) << __FUNCTION__;
147 DCHECK(thread_checker_.CalledOnValidThread());
148
149 // NOTE: This method is only called in response to physical audio/video device
150 // changes (from the operating system).
151
152 if (device_type != base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE)
153 return; // Uninteresting device change.
154
155 TRACE_EVENT_ASYNC_BEGIN0("video_capture", "EnumerateDeviceNames",
156 this /* id */);
157 // Always do enumeration even though some enumeration is in progress, because
158 // those enumeration commands could be sent before these devices change.
159 video_capture_device_factory_->EnumerateDeviceNames(
160 base::Bind(&VideoCaptureHandlerImpl::OnVideoCaptureDevicesEnumerated,
161 weak_factory_.GetWeakPtr()));
162 }
163
164 void VideoCaptureHandlerImpl::OnVideoCaptureDevicesEnumerated(
165 scoped_ptr<media::VideoCaptureDevice::Names> device_names) {
166 DCHECK(thread_checker_.CalledOnValidThread());
167 TRACE_EVENT_ASYNC_END0("video_capture", "EnumerateDeviceNames", this);
168 device_names_ = std::move(device_names);
169
170 #if !defined(NDEBUG)
171 for (const auto& device_name : *device_names_) {
172 DVLOG(1) << "device: id: " << device_name.id()
173 << ", name: " << device_name.GetNameAndModel()
174 << ", api: " << device_name.GetCaptureApiTypeString();
175 }
176 #endif
177 }
178
179 void VideoCaptureHandlerImpl::OnStart(const std::string& id) {
180 DVLOG(1) << __FUNCTION__ << " " << id;
181 DCHECK(thread_checker_.CalledOnValidThread());
182 // TODO(mcasas): update some tracking saying that |id| is started.
183 }
184
185 void VideoCaptureHandlerImpl::OnStopOrError(const std::string& id) {
186 DVLOG(1) << __FUNCTION__;
187 DCHECK(thread_checker_.CalledOnValidThread());
188 const auto& entry = device_and_streams_.find(id);
189 DLOG_IF(ERROR, entry == device_and_streams_.end()) << "Device id " << id
190 << " not found!";
191 if (entry == device_and_streams_.end())
192 return;
193 entry->second.first->StopAndDeAllocate();
194 device_and_streams_.erase(entry);
195 }
196
197 } // namespace video_capture
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698