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

Side by Side Diff: media/capture/video/fake_video_capture_device_factory.cc

Issue 2619503003: Split FakeVideoCaptureDevice into classes with single responsibility (Closed)
Patch Set: mcasas@ suggestions Created 3 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "media/capture/video/fake_video_capture_device_factory.h" 5 #include "media/capture/video/fake_video_capture_device_factory.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
10 #include "base/strings/string_tokenizer.h" 10 #include "base/strings/string_tokenizer.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "build/build_config.h" 13 #include "build/build_config.h"
14 #include "media/base/media_switches.h" 14 #include "media/base/media_switches.h"
15 15
16 namespace { 16 namespace {
17 17
18 static const size_t kDepthDeviceIndex = 1; 18 static const size_t kDepthDeviceIndex = 1;
19 static const char kDepthDeviceId[] = "/dev/video1"; 19 static const char kDepthDeviceId[] = "/dev/video1";
20 20
21 // Factory has one device by default; I420. If there are more, the second device 21 media::VideoPixelFormat GetPixelFormatFromDeviceId(
22 // is of Y16 format while the rest are I420. 22 const std::string& device_id) {
23 media::VideoPixelFormat GetPixelFormat(const std::string& device_id) {
24 return (device_id == kDepthDeviceId) ? media::PIXEL_FORMAT_Y16 23 return (device_id == kDepthDeviceId) ? media::PIXEL_FORMAT_Y16
25 : media::PIXEL_FORMAT_I420; 24 : media::PIXEL_FORMAT_I420;
26 } 25 }
27 } 26 }
28 27
29 namespace media { 28 namespace media {
30 29
31 // Cap the frame rate command line input to reasonable values. 30 // Cap the frame rate command line input to reasonable values.
32 static const float kFakeCaptureMinFrameRate = 5.0f; 31 static const float kFakeCaptureMinFrameRate = 5.0f;
33 static const float kFakeCaptureMaxFrameRate = 60.0f; 32 static const float kFakeCaptureMaxFrameRate = 60.0f;
34 // Default rate if none is specified as part of the command line. 33 // Default rate if none is specified as part of the command line.
35 static const float kFakeCaptureDefaultFrameRate = 20.0f; 34 static const float kFakeCaptureDefaultFrameRate = 20.0f;
36 // Cap the device count command line input to reasonable values. 35 // Cap the device count command line input to reasonable values.
37 static const int kFakeCaptureMinDeviceCount = 1; 36 static const int kFakeCaptureMinDeviceCount = 1;
38 static const int kFakeCaptureMaxDeviceCount = 10; 37 static const int kFakeCaptureMaxDeviceCount = 10;
39 38
40 FakeVideoCaptureDeviceFactory::FakeVideoCaptureDeviceFactory() 39 FakeVideoCaptureDeviceFactory::FakeVideoCaptureDeviceFactory()
41 : number_of_devices_(1), 40 : number_of_devices_(1),
42 fake_vcd_ownership_(FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS), 41 delivery_mode_(FakeVideoCaptureDeviceMaker::DeliveryMode::
42 USE_DEVICE_INTERNAL_BUFFERS),
43 frame_rate_(kFakeCaptureDefaultFrameRate) {} 43 frame_rate_(kFakeCaptureDefaultFrameRate) {}
44 44
45 std::unique_ptr<VideoCaptureDevice> FakeVideoCaptureDeviceFactory::CreateDevice( 45 std::unique_ptr<VideoCaptureDevice> FakeVideoCaptureDeviceFactory::CreateDevice(
46 const VideoCaptureDeviceDescriptor& device_descriptor) { 46 const VideoCaptureDeviceDescriptor& device_descriptor) {
47 DCHECK(thread_checker_.CalledOnValidThread()); 47 DCHECK(thread_checker_.CalledOnValidThread());
48 48
49 if (!command_line_parsed_) { 49 ParseCommandLine();
50 ParseCommandLine();
51 command_line_parsed_ = true;
52 }
53 50
54 for (int n = 0; n < number_of_devices_; ++n) { 51 for (int n = 0; n < number_of_devices_; ++n) {
55 std::string possible_id = base::StringPrintf("/dev/video%d", n); 52 std::string possible_id = base::StringPrintf("/dev/video%d", n);
56 if (device_descriptor.device_id.compare(possible_id) == 0) { 53 if (device_descriptor.device_id.compare(possible_id) == 0) {
57 return std::unique_ptr<VideoCaptureDevice>(new FakeVideoCaptureDevice( 54 return FakeVideoCaptureDeviceMaker::MakeInstance(
58 fake_vcd_ownership_, frame_rate_, GetPixelFormat(possible_id))); 55 GetPixelFormatFromDeviceId(possible_id), delivery_mode_, frame_rate_);
59 } 56 }
60 } 57 }
61 return std::unique_ptr<VideoCaptureDevice>(); 58 return std::unique_ptr<VideoCaptureDevice>();
62 } 59 }
63 60
64 void FakeVideoCaptureDeviceFactory::GetDeviceDescriptors( 61 void FakeVideoCaptureDeviceFactory::GetDeviceDescriptors(
65 VideoCaptureDeviceDescriptors* device_descriptors) { 62 VideoCaptureDeviceDescriptors* device_descriptors) {
66 DCHECK(thread_checker_.CalledOnValidThread()); 63 DCHECK(thread_checker_.CalledOnValidThread());
67 DCHECK(device_descriptors->empty()); 64 DCHECK(device_descriptors->empty());
68 65
69 if (!command_line_parsed_) { 66 ParseCommandLine();
70 ParseCommandLine();
71 command_line_parsed_ = true;
72 }
73 67
74 for (int n = 0; n < number_of_devices_; ++n) { 68 for (int n = 0; n < number_of_devices_; ++n) {
75 device_descriptors->emplace_back(base::StringPrintf("fake_device_%d", n), 69 device_descriptors->emplace_back(base::StringPrintf("fake_device_%d", n),
76 base::StringPrintf("/dev/video%d", n), 70 base::StringPrintf("/dev/video%d", n),
77 #if defined(OS_LINUX) 71 #if defined(OS_LINUX)
78 VideoCaptureApi::LINUX_V4L2_SINGLE_PLANE 72 VideoCaptureApi::LINUX_V4L2_SINGLE_PLANE
79 #elif defined(OS_MACOSX) 73 #elif defined(OS_MACOSX)
80 VideoCaptureApi::MACOSX_AVFOUNDATION 74 VideoCaptureApi::MACOSX_AVFOUNDATION
81 #elif defined(OS_WIN) 75 #elif defined(OS_WIN)
82 VideoCaptureApi::WIN_DIRECT_SHOW 76 VideoCaptureApi::WIN_DIRECT_SHOW
(...skipping 13 matching lines...) Expand all
96 depth_device.camera_calibration->focal_length_x = 135.0; 90 depth_device.camera_calibration->focal_length_x = 135.0;
97 depth_device.camera_calibration->focal_length_y = 135.6; 91 depth_device.camera_calibration->focal_length_y = 135.6;
98 depth_device.camera_calibration->depth_near = 0.0; 92 depth_device.camera_calibration->depth_near = 0.0;
99 depth_device.camera_calibration->depth_far = 65.535; 93 depth_device.camera_calibration->depth_far = 65.535;
100 } 94 }
101 95
102 void FakeVideoCaptureDeviceFactory::GetSupportedFormats( 96 void FakeVideoCaptureDeviceFactory::GetSupportedFormats(
103 const VideoCaptureDeviceDescriptor& device_descriptor, 97 const VideoCaptureDeviceDescriptor& device_descriptor,
104 VideoCaptureFormats* supported_formats) { 98 VideoCaptureFormats* supported_formats) {
105 DCHECK(thread_checker_.CalledOnValidThread()); 99 DCHECK(thread_checker_.CalledOnValidThread());
106 const gfx::Size supported_sizes[] = { 100
107 gfx::Size(96, 96), gfx::Size(320, 240), gfx::Size(640, 480), 101 ParseCommandLine();
108 gfx::Size(1280, 720), gfx::Size(1920, 1080)}; 102
109 supported_formats->clear(); 103 const VideoPixelFormat pixel_format =
110 for (const auto& size : supported_sizes) { 104 GetPixelFormatFromDeviceId(device_descriptor.device_id);
111 supported_formats->push_back(VideoCaptureFormat( 105 const VideoPixelStorage pixel_storage = PIXEL_STORAGE_CPU;
112 size, frame_rate_, GetPixelFormat(device_descriptor.device_id))); 106 std::vector<gfx::Size> supported_sizes;
107 FakeVideoCaptureDeviceMaker::GetSupportedSizes(&supported_sizes);
108 for (const auto& supported_size : supported_sizes) {
109 supported_formats->emplace_back(supported_size, frame_rate_, pixel_format,
110 pixel_storage);
113 } 111 }
114 } 112 }
115 113
116 // Optional comma delimited parameters to the command line can specify buffer 114 // Optional comma delimited parameters to the command line can specify buffer
117 // ownership, device count, and the fake video devices FPS. 115 // ownership, device count, and the fake video devices FPS.
118 // Examples: "ownership=client, device-count=2, fps=60" "fps=30" 116 // Examples: "ownership=client, device-count=2, fps=60" "fps=30"
119 void FakeVideoCaptureDeviceFactory::ParseCommandLine() { 117 void FakeVideoCaptureDeviceFactory::ParseCommandLine() {
118 if (command_line_parsed_)
119 return;
120 command_line_parsed_ = true;
121
120 const std::string option = 122 const std::string option =
121 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 123 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
122 switches::kUseFakeDeviceForMediaStream); 124 switches::kUseFakeDeviceForMediaStream);
123 base::StringTokenizer option_tokenizer(option, ", "); 125 base::StringTokenizer option_tokenizer(option, ", ");
124 option_tokenizer.set_quote_chars("\""); 126 option_tokenizer.set_quote_chars("\"");
125 127
126 while (option_tokenizer.GetNext()) { 128 while (option_tokenizer.GetNext()) {
127 std::vector<std::string> param = 129 std::vector<std::string> param =
128 base::SplitString(option_tokenizer.token(), "=", base::TRIM_WHITESPACE, 130 base::SplitString(option_tokenizer.token(), "=", base::TRIM_WHITESPACE,
129 base::SPLIT_WANT_NONEMPTY); 131 base::SPLIT_WANT_NONEMPTY);
130 132
131 if (param.size() != 2u) { 133 if (param.size() != 2u) {
132 LOG(WARNING) << "Forget a value '" << option << "'? Use name=value for " 134 LOG(WARNING) << "Forget a value '" << option << "'? Use name=value for "
133 << switches::kUseFakeDeviceForMediaStream << "."; 135 << switches::kUseFakeDeviceForMediaStream << ".";
134 return; 136 return;
135 } 137 }
136 138
137 if (base::EqualsCaseInsensitiveASCII(param.front(), "ownership") && 139 if (base::EqualsCaseInsensitiveASCII(param.front(), "ownership") &&
138 base::EqualsCaseInsensitiveASCII(param.back(), "client")) { 140 base::EqualsCaseInsensitiveASCII(param.back(), "client")) {
139 fake_vcd_ownership_ = 141 delivery_mode_ = FakeVideoCaptureDeviceMaker::DeliveryMode::
140 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS; 142 USE_CLIENT_PROVIDED_BUFFERS;
141 } else if (base::EqualsCaseInsensitiveASCII(param.front(), "fps")) { 143 } else if (base::EqualsCaseInsensitiveASCII(param.front(), "fps")) {
142 double fps = 0; 144 double fps = 0;
143 if (base::StringToDouble(param.back(), &fps)) { 145 if (base::StringToDouble(param.back(), &fps)) {
144 frame_rate_ = 146 frame_rate_ =
145 std::max(kFakeCaptureMinFrameRate, static_cast<float>(fps)); 147 std::max(kFakeCaptureMinFrameRate, static_cast<float>(fps));
146 frame_rate_ = std::min(kFakeCaptureMaxFrameRate, frame_rate_); 148 frame_rate_ = std::min(kFakeCaptureMaxFrameRate, frame_rate_);
147 } 149 }
148 } else if (base::EqualsCaseInsensitiveASCII(param.front(), 150 } else if (base::EqualsCaseInsensitiveASCII(param.front(),
149 "device-count")) { 151 "device-count")) {
150 unsigned int count = 0; 152 unsigned int count = 0;
151 if (base::StringToUint(param.back(), &count)) { 153 if (base::StringToUint(param.back(), &count)) {
152 number_of_devices_ = std::min( 154 number_of_devices_ = std::min(
153 kFakeCaptureMaxDeviceCount, 155 kFakeCaptureMaxDeviceCount,
154 std::max(kFakeCaptureMinDeviceCount, static_cast<int>(count))); 156 std::max(kFakeCaptureMinDeviceCount, static_cast<int>(count)));
155 } 157 }
156 } 158 }
157 } 159 }
158 } 160 }
159 161
160 } // namespace media 162 } // namespace media
OLDNEW
« no previous file with comments | « media/capture/video/fake_video_capture_device_factory.h ('k') | media/capture/video/fake_video_capture_device_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698