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

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

Powered by Google App Engine
This is Rietveld 408576698