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

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 comments & rebase Created 3 years, 11 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 // Factory has one device by default; I420. If there are more, the second device 18 media::VideoPixelFormat GetPixelFormatFromDeviceId(
19 // is of Y16 format while the rest are I420. 19 const std::string& device_id) {
20 media::VideoPixelFormat GetPixelFormat(const std::string& device_id) {
21 return (device_id == "/dev/video1") ? media::PIXEL_FORMAT_Y16 20 return (device_id == "/dev/video1") ? media::PIXEL_FORMAT_Y16
22 : media::PIXEL_FORMAT_I420; 21 : media::PIXEL_FORMAT_I420;
23 } 22 }
24 } 23 }
25 24
26 namespace media { 25 namespace media {
27 26
28 // Cap the frame rate command line input to reasonable values. 27 // Cap the frame rate command line input to reasonable values.
29 static const float kFakeCaptureMinFrameRate = 5.0f; 28 static const float kFakeCaptureMinFrameRate = 5.0f;
30 static const float kFakeCaptureMaxFrameRate = 60.0f; 29 static const float kFakeCaptureMaxFrameRate = 60.0f;
31 // Default rate if none is specified as part of the command line. 30 // Default rate if none is specified as part of the command line.
32 static const float kFakeCaptureDefaultFrameRate = 20.0f; 31 static const float kFakeCaptureDefaultFrameRate = 20.0f;
33 // Cap the device count command line input to reasonable values. 32 // Cap the device count command line input to reasonable values.
34 static const int kFakeCaptureMinDeviceCount = 1; 33 static const int kFakeCaptureMinDeviceCount = 1;
35 static const int kFakeCaptureMaxDeviceCount = 10; 34 static const int kFakeCaptureMaxDeviceCount = 10;
36 35
37 FakeVideoCaptureDeviceFactory::FakeVideoCaptureDeviceFactory() 36 FakeVideoCaptureDeviceFactory::FakeVideoCaptureDeviceFactory()
38 : number_of_devices_(1), 37 : number_of_devices_(1),
39 fake_vcd_ownership_(FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS), 38 delivery_mode_(
39 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_OWN_BUFFERS),
40 frame_rate_(kFakeCaptureDefaultFrameRate) {} 40 frame_rate_(kFakeCaptureDefaultFrameRate) {}
41 41
42 std::unique_ptr<VideoCaptureDevice> FakeVideoCaptureDeviceFactory::CreateDevice( 42 std::unique_ptr<VideoCaptureDevice> FakeVideoCaptureDeviceFactory::CreateDevice(
43 const VideoCaptureDeviceDescriptor& device_descriptor) { 43 const VideoCaptureDeviceDescriptor& device_descriptor) {
44 DCHECK(thread_checker_.CalledOnValidThread()); 44 DCHECK(thread_checker_.CalledOnValidThread());
45 45
46 if (!command_line_parsed_) { 46 ParseCommandLine();
47 ParseCommandLine();
48 command_line_parsed_ = true;
49 }
50 47
51 for (int n = 0; n < number_of_devices_; ++n) { 48 for (int n = 0; n < number_of_devices_; ++n) {
52 std::string possible_id = base::StringPrintf("/dev/video%d", n); 49 std::string possible_id = base::StringPrintf("/dev/video%d", n);
53 if (device_descriptor.device_id.compare(possible_id) == 0) { 50 if (device_descriptor.device_id.compare(possible_id) == 0) {
54 return std::unique_ptr<VideoCaptureDevice>(new FakeVideoCaptureDevice( 51 return FakeVideoCaptureDeviceMaker::MakeInstance(
55 fake_vcd_ownership_, frame_rate_, GetPixelFormat(possible_id))); 52 GetPixelFormatFromDeviceId(possible_id), delivery_mode_, frame_rate_);
56 } 53 }
57 } 54 }
58 return std::unique_ptr<VideoCaptureDevice>(); 55 return std::unique_ptr<VideoCaptureDevice>();
59 } 56 }
60 57
61 void FakeVideoCaptureDeviceFactory::GetDeviceDescriptors( 58 void FakeVideoCaptureDeviceFactory::GetDeviceDescriptors(
62 VideoCaptureDeviceDescriptors* device_descriptors) { 59 VideoCaptureDeviceDescriptors* device_descriptors) {
63 DCHECK(thread_checker_.CalledOnValidThread()); 60 DCHECK(thread_checker_.CalledOnValidThread());
64 DCHECK(device_descriptors->empty()); 61 DCHECK(device_descriptors->empty());
65 62
66 if (!command_line_parsed_) { 63 ParseCommandLine();
67 ParseCommandLine();
68 command_line_parsed_ = true;
69 }
70 64
71 for (int n = 0; n < number_of_devices_; ++n) { 65 for (int n = 0; n < number_of_devices_; ++n) {
72 device_descriptors->emplace_back(base::StringPrintf("fake_device_%d", n), 66 device_descriptors->emplace_back(base::StringPrintf("fake_device_%d", n),
73 base::StringPrintf("/dev/video%d", n), 67 base::StringPrintf("/dev/video%d", n),
74 #if defined(OS_LINUX) 68 #if defined(OS_LINUX)
75 VideoCaptureApi::LINUX_V4L2_SINGLE_PLANE 69 VideoCaptureApi::LINUX_V4L2_SINGLE_PLANE
76 #elif defined(OS_MACOSX) 70 #elif defined(OS_MACOSX)
77 VideoCaptureApi::MACOSX_AVFOUNDATION 71 VideoCaptureApi::MACOSX_AVFOUNDATION
78 #elif defined(OS_WIN) 72 #elif defined(OS_WIN)
79 VideoCaptureApi::WIN_DIRECT_SHOW 73 VideoCaptureApi::WIN_DIRECT_SHOW
80 #elif defined(OS_ANDROID) 74 #elif defined(OS_ANDROID)
81 VideoCaptureApi::ANDROID_API2_LEGACY 75 VideoCaptureApi::ANDROID_API2_LEGACY
82 #endif 76 #endif
83 ); 77 );
84 } 78 }
85 } 79 }
86 80
87 void FakeVideoCaptureDeviceFactory::GetSupportedFormats( 81 void FakeVideoCaptureDeviceFactory::GetSupportedFormats(
88 const VideoCaptureDeviceDescriptor& device_descriptor, 82 const VideoCaptureDeviceDescriptor& device_descriptor,
89 VideoCaptureFormats* supported_formats) { 83 VideoCaptureFormats* supported_formats) {
90 DCHECK(thread_checker_.CalledOnValidThread()); 84 DCHECK(thread_checker_.CalledOnValidThread());
91 const gfx::Size supported_sizes[] = { 85
92 gfx::Size(96, 96), gfx::Size(320, 240), gfx::Size(640, 480), 86 ParseCommandLine();
93 gfx::Size(1280, 720), gfx::Size(1920, 1080)}; 87
94 supported_formats->clear(); 88 const VideoPixelFormat pixel_format =
95 for (const auto& size : supported_sizes) { 89 GetPixelFormatFromDeviceId(device_descriptor.device_id);
96 supported_formats->push_back(VideoCaptureFormat( 90 const VideoPixelStorage pixel_storage = PIXEL_STORAGE_CPU;
97 size, frame_rate_, GetPixelFormat(device_descriptor.device_id))); 91 std::vector<gfx::Size> supported_sizes;
92 FakeVideoCaptureDeviceMaker::GetSupportedSizes(&supported_sizes);
93 for (const auto& supported_size : supported_sizes) {
94 supported_formats->emplace_back(supported_size, frame_rate_, pixel_format,
95 pixel_storage);
98 } 96 }
99 } 97 }
100 98
101 // Optional comma delimited parameters to the command line can specify buffer 99 // Optional comma delimited parameters to the command line can specify buffer
102 // ownership, device count, and the fake video devices FPS. 100 // ownership, device count, and the fake video devices FPS.
103 // Examples: "ownership=client, device-count=2, fps=60" "fps=30" 101 // Examples: "ownership=client, device-count=2, fps=60" "fps=30"
104 void FakeVideoCaptureDeviceFactory::ParseCommandLine() { 102 void FakeVideoCaptureDeviceFactory::ParseCommandLine() {
103 if (command_line_parsed_)
104 return;
105 command_line_parsed_ = true;
106
105 const std::string option = 107 const std::string option =
106 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 108 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
107 switches::kUseFakeDeviceForMediaStream); 109 switches::kUseFakeDeviceForMediaStream);
108 base::StringTokenizer option_tokenizer(option, ", "); 110 base::StringTokenizer option_tokenizer(option, ", ");
109 option_tokenizer.set_quote_chars("\""); 111 option_tokenizer.set_quote_chars("\"");
110 112
111 while (option_tokenizer.GetNext()) { 113 while (option_tokenizer.GetNext()) {
112 std::vector<std::string> param = 114 std::vector<std::string> param =
113 base::SplitString(option_tokenizer.token(), "=", base::TRIM_WHITESPACE, 115 base::SplitString(option_tokenizer.token(), "=", base::TRIM_WHITESPACE,
114 base::SPLIT_WANT_NONEMPTY); 116 base::SPLIT_WANT_NONEMPTY);
115 117
116 if (param.size() != 2u) { 118 if (param.size() != 2u) {
117 LOG(WARNING) << "Forget a value '" << option << "'? Use name=value for " 119 LOG(WARNING) << "Forget a value '" << option << "'? Use name=value for "
118 << switches::kUseFakeDeviceForMediaStream << "."; 120 << switches::kUseFakeDeviceForMediaStream << ".";
119 return; 121 return;
120 } 122 }
121 123
122 if (base::EqualsCaseInsensitiveASCII(param.front(), "ownership") && 124 if (base::EqualsCaseInsensitiveASCII(param.front(), "ownership") &&
123 base::EqualsCaseInsensitiveASCII(param.back(), "client")) { 125 base::EqualsCaseInsensitiveASCII(param.back(), "client")) {
124 fake_vcd_ownership_ = 126 delivery_mode_ =
125 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS; 127 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_CLIENT_BUFFERS;
126 } else if (base::EqualsCaseInsensitiveASCII(param.front(), "fps")) { 128 } else if (base::EqualsCaseInsensitiveASCII(param.front(), "fps")) {
127 double fps = 0; 129 double fps = 0;
128 if (base::StringToDouble(param.back(), &fps)) { 130 if (base::StringToDouble(param.back(), &fps)) {
129 frame_rate_ = 131 frame_rate_ =
130 std::max(kFakeCaptureMinFrameRate, static_cast<float>(fps)); 132 std::max(kFakeCaptureMinFrameRate, static_cast<float>(fps));
131 frame_rate_ = std::min(kFakeCaptureMaxFrameRate, frame_rate_); 133 frame_rate_ = std::min(kFakeCaptureMaxFrameRate, frame_rate_);
132 } 134 }
133 } else if (base::EqualsCaseInsensitiveASCII(param.front(), 135 } else if (base::EqualsCaseInsensitiveASCII(param.front(),
134 "device-count")) { 136 "device-count")) {
135 unsigned int count = 0; 137 unsigned int count = 0;
136 if (base::StringToUint(param.back(), &count)) { 138 if (base::StringToUint(param.back(), &count)) {
137 number_of_devices_ = std::min( 139 number_of_devices_ = std::min(
138 kFakeCaptureMaxDeviceCount, 140 kFakeCaptureMaxDeviceCount,
139 std::max(kFakeCaptureMinDeviceCount, static_cast<int>(count))); 141 std::max(kFakeCaptureMinDeviceCount, static_cast<int>(count)));
140 } 142 }
141 } 143 }
142 } 144 }
143 } 145 }
144 146
145 } // namespace media 147 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698