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

Side by Side Diff: media/capture/service/video_capture_handler_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698