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

Side by Side Diff: services/video_capture/device_factory_media_to_mojo_adapter.cc

Issue 2494033004: [Mojo Video Capture] Use string keys instead of VideoCaptureDeviceDescriptor (Closed)
Patch Set: yzshen's comment Created 4 years, 1 month 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "services/video_capture/device_factory_media_to_mojo_adapter.h" 5 #include "services/video_capture/device_factory_media_to_mojo_adapter.h"
6 6
7 #include <sstream> 7 #include <sstream>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 21 matching lines...) Expand all
32 std::unique_ptr<media::VideoCaptureDeviceFactory> device_factory, 32 std::unique_ptr<media::VideoCaptureDeviceFactory> device_factory,
33 const media::VideoCaptureJpegDecoderFactoryCB& 33 const media::VideoCaptureJpegDecoderFactoryCB&
34 jpeg_decoder_factory_callback) 34 jpeg_decoder_factory_callback)
35 : device_factory_(std::move(device_factory)), 35 : device_factory_(std::move(device_factory)),
36 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {} 36 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {}
37 37
38 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; 38 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default;
39 39
40 void DeviceFactoryMediaToMojoAdapter::EnumerateDeviceDescriptors( 40 void DeviceFactoryMediaToMojoAdapter::EnumerateDeviceDescriptors(
41 const EnumerateDeviceDescriptorsCallback& callback) { 41 const EnumerateDeviceDescriptorsCallback& callback) {
42 media::VideoCaptureDeviceDescriptors descriptors; 42 std::vector<media::VideoCaptureDeviceDescriptor> descriptors;
mcasas 2016/11/14 19:02:02 media::VideoCaptureDeviceDescriptors still exists,
chfremer 2016/11/14 19:23:29 Done.
43 device_factory_->GetDeviceDescriptors(&descriptors); 43 device_factory_->GetDeviceDescriptors(&descriptors);
44 callback.Run(descriptors); 44 callback.Run(std::move(descriptors));
45 } 45 }
46 46
47 void DeviceFactoryMediaToMojoAdapter::GetSupportedFormats( 47 void DeviceFactoryMediaToMojoAdapter::GetSupportedFormats(
48 const media::VideoCaptureDeviceDescriptor& device_descriptor, 48 const std::string& device_id,
49 const GetSupportedFormatsCallback& callback) { 49 const GetSupportedFormatsCallback& callback) {
50 std::vector<media::VideoCaptureDeviceDescriptor> descriptors;
51 device_factory_->GetDeviceDescriptors(&descriptors);
52 auto descriptor_iter = std::find_if(
53 descriptors.begin(), descriptors.end(),
54 [&device_id](const media::VideoCaptureDeviceDescriptor& descriptor) {
55 return descriptor.device_id == device_id;
56 });
57 media::VideoCaptureFormats media_formats;
58 if (descriptor_iter != descriptors.end()) {
59 device_factory_->GetSupportedFormats(*descriptor_iter, &media_formats);
60 }
mcasas 2016/11/14 19:02:02 nit: no {}
chfremer 2016/11/14 19:23:29 Done.
50 std::vector<VideoCaptureFormat> result; 61 std::vector<VideoCaptureFormat> result;
51 std::vector<media::VideoCaptureFormat> media_formats;
52 device_factory_->GetSupportedFormats(device_descriptor, &media_formats);
53 for (const auto& media_format : media_formats) { 62 for (const auto& media_format : media_formats) {
54 // The Video Capture Service requires devices to deliver frames either in 63 // The Video Capture Service requires devices to deliver frames either in
55 // I420 or MJPEG formats. 64 // I420 or MJPEG formats.
56 // TODO(chfremer): Add support for Y16 format. See crbug.com/624436. 65 // TODO(chfremer): Add support for Y16 format. See crbug.com/624436.
57 if (media_format.pixel_format != media::PIXEL_FORMAT_I420 && 66 if (media_format.pixel_format != media::PIXEL_FORMAT_I420 &&
58 media_format.pixel_format != media::PIXEL_FORMAT_MJPEG) { 67 media_format.pixel_format != media::PIXEL_FORMAT_MJPEG) {
59 continue; 68 continue;
60 } 69 }
61 VideoCaptureFormat format; 70 VideoCaptureFormat format;
62 format.frame_size = media_format.frame_size; 71 format.frame_size = media_format.frame_size;
63 format.frame_rate = media_format.frame_rate; 72 format.frame_rate = media_format.frame_rate;
64 if (base::ContainsValue(result, format)) 73 if (base::ContainsValue(result, format))
65 continue; // Result already contains this format 74 continue; // Result already contains this format
66 result.push_back(format); 75 result.push_back(format);
67 } 76 }
68 callback.Run(std::move(result)); 77 callback.Run(std::move(result));
69 } 78 }
70 79
71 void DeviceFactoryMediaToMojoAdapter::CreateDeviceProxy( 80 void DeviceFactoryMediaToMojoAdapter::CreateDeviceProxy(
72 const media::VideoCaptureDeviceDescriptor& device_descriptor, 81 const std::string& device_id,
73 mojom::VideoCaptureDeviceProxyRequest proxy_request, 82 mojom::VideoCaptureDeviceProxyRequest proxy_request,
74 const CreateDeviceProxyCallback& callback) { 83 const CreateDeviceProxyCallback& callback) {
75 if (active_devices_.find(device_descriptor) != active_devices_.end()) { 84 auto active_device_iter = active_devices_by_id_.find(device_id);
85 if (active_device_iter != active_devices_by_id_.end()) {
76 // The requested device is already in use. 86 // The requested device is already in use.
77 // Revoke the access and close the device, then bind to the new request. 87 // Revoke the access and close the device, then bind to the new request.
78 ActiveDeviceEntry& device_entry = active_devices_[device_descriptor]; 88 ActiveDeviceEntry& device_entry = active_device_iter->second;
79 device_entry.binding->Unbind(); 89 device_entry.binding->Unbind();
80 device_entry.device_proxy->Stop(); 90 device_entry.device_proxy->Stop();
81 device_entry.binding->Bind(std::move(proxy_request)); 91 device_entry.binding->Bind(std::move(proxy_request));
82 device_entry.binding->set_connection_error_handler(base::Bind( 92 device_entry.binding->set_connection_error_handler(base::Bind(
83 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, 93 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose,
84 base::Unretained(this), device_descriptor)); 94 base::Unretained(this), device_id));
85 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); 95 callback.Run(mojom::DeviceAccessResultCode::SUCCESS);
86 return; 96 return;
87 } 97 }
88 98
99 // Create device
100 std::vector<media::VideoCaptureDeviceDescriptor> descriptors;
101 device_factory_->GetDeviceDescriptors(&descriptors);
102 auto descriptor_iter = std::find_if(
103 descriptors.begin(), descriptors.end(),
104 [&device_id](const media::VideoCaptureDeviceDescriptor& descriptor) {
105 return descriptor.device_id == device_id;
106 });
107 if (descriptor_iter == descriptors.end()) {
mcasas 2016/11/14 19:02:02 l.100-l.107 is the same code as l.50-l.58, is it w
chfremer 2016/11/14 19:23:29 It is! Thanks. Done.
108 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND);
109 return;
110 }
89 std::unique_ptr<media::VideoCaptureDevice> media_device = 111 std::unique_ptr<media::VideoCaptureDevice> media_device =
90 device_factory_->CreateDevice(device_descriptor); 112 device_factory_->CreateDevice(*descriptor_iter);
91 if (media_device == nullptr) { 113 if (media_device == nullptr) {
92 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); 114 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND);
93 return; 115 return;
94 } 116 }
95 117
96 // Add entry to |active_devices| to keep track of it 118 // Add entry to active_devices to keep track of it
97 ActiveDeviceEntry device_entry; 119 ActiveDeviceEntry device_entry;
98 device_entry.device_proxy = base::MakeUnique<VideoCaptureDeviceProxyImpl>( 120 device_entry.device_proxy = base::MakeUnique<VideoCaptureDeviceProxyImpl>(
99 std::move(media_device), jpeg_decoder_factory_callback_); 121 std::move(media_device), jpeg_decoder_factory_callback_);
100 device_entry.binding = 122 device_entry.binding =
101 base::MakeUnique<mojo::Binding<mojom::VideoCaptureDeviceProxy>>( 123 base::MakeUnique<mojo::Binding<mojom::VideoCaptureDeviceProxy>>(
102 device_entry.device_proxy.get(), std::move(proxy_request)); 124 device_entry.device_proxy.get(), std::move(proxy_request));
103 device_entry.binding->set_connection_error_handler(base::Bind( 125 device_entry.binding->set_connection_error_handler(base::Bind(
104 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, 126 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose,
105 base::Unretained(this), device_descriptor)); 127 base::Unretained(this), device_id));
106 active_devices_[device_descriptor] = std::move(device_entry); 128 active_devices_by_id_[device_id] = std::move(device_entry);
107 129
108 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); 130 callback.Run(mojom::DeviceAccessResultCode::SUCCESS);
109 } 131 }
110 132
111 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( 133 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose(
112 const media::VideoCaptureDeviceDescriptor& descriptor) { 134 const std::string& device_id) {
113 active_devices_[descriptor].device_proxy->Stop(); 135 active_devices_by_id_[device_id].device_proxy->Stop();
114 active_devices_.erase(descriptor); 136 active_devices_by_id_.erase(device_id);
115 } 137 }
116 138
117 } // namespace video_capture 139 } // namespace video_capture
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698