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

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: rockot@ comments 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_device_client_impl.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 unless someone
39 // notifies of changes.
40 // TODO(mcasas): monitor http://crbug.com/584817 and perhaps launch the
41 // appropriate DeviceMonitor from here.
42 base::SystemMonitor::Get()->AddDevicesChangedObserver(this);
43 }
44
45 VideoCaptureHandlerImpl::~VideoCaptureHandlerImpl() {
46 DCHECK(thread_checker_.CalledOnValidThread());
47 for (const auto& id_and_device_and_stream: device_and_streams_)
48 id_and_device_and_stream.second.first->StopAndDeAllocate();
49
50 base::SystemMonitor::Get()->RemoveDevicesChangedObserver(this);
51 }
52
53 void VideoCaptureHandlerImpl::RequestMediaDevices(
54 mojom::MediaDevicesInfoRequestPtr request,
55 const RequestMediaDevicesCallback& callback) {
56 DVLOG(1) << __FUNCTION__;
57 DCHECK(thread_checker_.CalledOnValidThread());
58
59 mojom::MediaDevicesInfoReplyPtr reply = mojom::MediaDevicesInfoReply::New();
60 reply->request_id = request->request_id;
61 if (!device_names_->empty()) {
62 for (const auto& device_name : *device_names_) {
63 mojom::WebSourceInfoPtr web_source_info = mojom::WebSourceInfo::New();
64
65 // Send a GUID of the Video Capture id to hide sensitive information.
66 const std::string guid = base::GenerateGUID();
67 public_to_private_id_map_[guid] = device_name.id();
68 web_source_info->device_id = guid;
69
70 web_source_info->label = device_name.GetNameAndModel();
71 web_source_info->kind = mojom::SourceKind::Video;
72 web_source_info->facing_mode = mojom::VideoFacingMode::None;
73
74 reply->sources.push_back(std::move(web_source_info));
75 }
76 }
77 callback.Run(std::move(reply));
78 }
79
80 void VideoCaptureHandlerImpl::RequestStream(
81 mojom::StreamOptionsPtr options,
82 const mojo::String& security_origin,
83 const RequestStreamCallback& callback) {
84 DVLOG(1) << __FUNCTION__ << " device guid: " << options->device_id;
85 DCHECK(thread_checker_.CalledOnValidThread());
86
87 const std::string& guid = options->device_id;
88 if (!ContainsKey(public_to_private_id_map_, guid)) {
89 DLOG(ERROR) << "Device identified as " << guid << " not found!";
90 return;
91 }
92
93 // Try to find |options->id| in the list of devices. If not found -> return.
94 const std::string& id = public_to_private_id_map_[guid];
95 auto name = std::find_if(device_names_->begin(), device_names_->end(),
96 [id](const media::VideoCaptureDevice::Name& name) {
97 return name.id() == id;
98 });
99 if (name == device_names_->end()) {
100 DLOG(ERROR) << "device " << options->device_id << " not found.";
101 callback.Run(nullptr);
102 return;
103 }
104
105 // TODO(mcasas): At this point we need to ask the user for permission to use
106 // this device if it hasn't been granted before or elsewhere. Soldier on for
107 // the time being and revisit this part later on.
108
109 media::VideoCaptureParams params;
110 params.requested_format.frame_size = options->capture_size.To<gfx::Size>();
111 params.requested_format.frame_rate = options->frame_rate;
112 params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
113
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 the found device and Start() with the chosen VideoCaptureParameters.
124 scoped_ptr<media::VideoCaptureDevice> video_capture_device =
125 video_capture_device_factory_->Create(*name);
126 if (!video_capture_device) {
127 DLOG(ERROR) << "device " << id << " could not be created.";
128 callback.Run(nullptr);
129 return;
130 }
131
132 // Create a VideoCaptureDeviceClientImpl using |stream_| as Delegate.
133 scoped_ptr<media::VideoCaptureDevice::Client> device_client(
134 new VideoCaptureDeviceClientImpl(stream.get()));
135 video_capture_device->AllocateAndStart(params, std::move(device_client));
136
137 device_and_streams_.insert(make_pair(
138 id, make_pair(std::move(video_capture_device), std::move(stream))));
139
140 callback.Run(std::move(stream_ptr));
141 }
142
143 void VideoCaptureHandlerImpl::OnDevicesChanged(
144 base::SystemMonitor::DeviceType device_type) {
145 DVLOG(1) << __FUNCTION__;
146 DCHECK(thread_checker_.CalledOnValidThread());
147
148 // NOTE: This method is only called in response to physical audio/video device
149 // changes (from the operating system).
150
151 if (device_type != base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE)
152 return; // Uninteresting device change.
153
154 TRACE_EVENT_ASYNC_BEGIN0("video_capture", "EnumerateDeviceNames",
155 this /* id */);
156 // Always do enumeration even though some enumeration is in progress, because
157 // those enumeration commands could be sent before these devices change.
158 video_capture_device_factory_->EnumerateDeviceNames(
159 base::Bind(&VideoCaptureHandlerImpl::OnVideoCaptureDevicesEnumerated,
160 weak_factory_.GetWeakPtr()));
161 }
162
163 void VideoCaptureHandlerImpl::OnVideoCaptureDevicesEnumerated(
164 scoped_ptr<media::VideoCaptureDevice::Names> device_names) {
165 DCHECK(thread_checker_.CalledOnValidThread());
166 TRACE_EVENT_ASYNC_END0("video_capture", "EnumerateDeviceNames", this);
167 device_names_ = std::move(device_names);
168
169 #if !defined(NDEBUG)
170 for (const auto& device_name : *device_names_) {
171 DVLOG(1) << "device: id: " << device_name.id()
172 << ", name: " << device_name.GetNameAndModel()
173 << ", api: " << device_name.GetCaptureApiTypeString();
174 }
175 #endif
176 }
177
178 void VideoCaptureHandlerImpl::OnStart(const std::string& id) {
179 DVLOG(1) << __FUNCTION__ << " " << id;
180 DCHECK(thread_checker_.CalledOnValidThread());
181 // TODO(mcasas): update some tracking saying that |id| is started ?
182 }
183
184 void VideoCaptureHandlerImpl::OnStopOrError(const std::string& id) {
185 DCHECK(thread_checker_.CalledOnValidThread());
186 const auto& entry = device_and_streams_.find(id);
187 DLOG_IF(ERROR, entry == device_and_streams_.end()) << "Device id " << id
188 << " not found!";
189 if (entry == device_and_streams_.end())
190 return;
191 entry->second.first->StopAndDeAllocate();
192 device_and_streams_.erase(entry);
193 }
194
195 } // namespace video_capture
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698