Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_( |
| 40 frame_rate_(kFakeCaptureDefaultFrameRate) {} | 39 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_OWN_BUFFERS), |
| 40 output_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_, |
| 53 output_frame_rate_); | |
| 56 } | 54 } |
| 57 } | 55 } |
| 58 return std::unique_ptr<VideoCaptureDevice>(); | 56 return std::unique_ptr<VideoCaptureDevice>(); |
| 59 } | 57 } |
| 60 | 58 |
| 61 void FakeVideoCaptureDeviceFactory::GetDeviceDescriptors( | 59 void FakeVideoCaptureDeviceFactory::GetDeviceDescriptors( |
| 62 VideoCaptureDeviceDescriptors* device_descriptors) { | 60 VideoCaptureDeviceDescriptors* device_descriptors) { |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); | 61 DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 DCHECK(device_descriptors->empty()); | 62 DCHECK(device_descriptors->empty()); |
| 65 | 63 |
| 66 if (!command_line_parsed_) { | 64 ParseCommandLine(); |
| 67 ParseCommandLine(); | |
| 68 command_line_parsed_ = true; | |
| 69 } | |
| 70 | 65 |
| 71 for (int n = 0; n < number_of_devices_; ++n) { | 66 for (int n = 0; n < number_of_devices_; ++n) { |
| 72 device_descriptors->emplace_back(base::StringPrintf("fake_device_%d", n), | 67 device_descriptors->emplace_back(base::StringPrintf("fake_device_%d", n), |
| 73 base::StringPrintf("/dev/video%d", n), | 68 base::StringPrintf("/dev/video%d", n), |
| 74 #if defined(OS_LINUX) | 69 #if defined(OS_LINUX) |
| 75 VideoCaptureApi::LINUX_V4L2_SINGLE_PLANE | 70 VideoCaptureApi::LINUX_V4L2_SINGLE_PLANE |
| 76 #elif defined(OS_MACOSX) | 71 #elif defined(OS_MACOSX) |
| 77 VideoCaptureApi::MACOSX_AVFOUNDATION | 72 VideoCaptureApi::MACOSX_AVFOUNDATION |
| 78 #elif defined(OS_WIN) | 73 #elif defined(OS_WIN) |
| 79 VideoCaptureApi::WIN_DIRECT_SHOW | 74 VideoCaptureApi::WIN_DIRECT_SHOW |
| 80 #elif defined(OS_ANDROID) | 75 #elif defined(OS_ANDROID) |
| 81 VideoCaptureApi::ANDROID_API2_LEGACY | 76 VideoCaptureApi::ANDROID_API2_LEGACY |
| 82 #endif | 77 #endif |
| 83 ); | 78 ); |
| 84 } | 79 } |
| 85 } | 80 } |
| 86 | 81 |
| 87 void FakeVideoCaptureDeviceFactory::GetSupportedFormats( | 82 void FakeVideoCaptureDeviceFactory::GetSupportedFormats( |
| 88 const VideoCaptureDeviceDescriptor& device_descriptor, | 83 const VideoCaptureDeviceDescriptor& device_descriptor, |
| 89 VideoCaptureFormats* supported_formats) { | 84 VideoCaptureFormats* supported_formats) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 const gfx::Size supported_sizes[] = { | 86 |
| 92 gfx::Size(96, 96), gfx::Size(320, 240), gfx::Size(640, 480), | 87 ParseCommandLine(); |
| 93 gfx::Size(1280, 720), gfx::Size(1920, 1080)}; | 88 |
| 94 supported_formats->clear(); | 89 const VideoPixelFormat pixel_format = |
| 95 for (const auto& size : supported_sizes) { | 90 GetPixelFormatFromDeviceId(device_descriptor.device_id); |
| 96 supported_formats->push_back(VideoCaptureFormat( | 91 const VideoPixelStorage pixel_storage = PIXEL_STORAGE_CPU; |
| 97 size, frame_rate_, GetPixelFormat(device_descriptor.device_id))); | 92 std::vector<gfx::Size> supported_sizes; |
| 93 FakeVideoCaptureDevice::GetSupportedSizes(&supported_sizes); | |
| 94 for (const auto& supported_size : supported_sizes) { | |
| 95 supported_formats->emplace_back(supported_size, output_frame_rate_, | |
| 96 pixel_format, pixel_storage); | |
| 98 } | 97 } |
| 99 } | 98 } |
| 100 | 99 |
| 101 // Optional comma delimited parameters to the command line can specify buffer | 100 // Optional comma delimited parameters to the command line can specify buffer |
| 102 // ownership, device count, and the fake video devices FPS. | 101 // ownership, device count, and the fake video devices FPS. |
| 103 // Examples: "ownership=client, device-count=2, fps=60" "fps=30" | 102 // Examples: "ownership=client, device-count=2, fps=60" "fps=30" |
| 104 void FakeVideoCaptureDeviceFactory::ParseCommandLine() { | 103 void FakeVideoCaptureDeviceFactory::ParseCommandLine() { |
| 104 if (command_line_parsed_) { | |
| 105 return; | |
| 106 } | |
|
mcasas
2017/01/12 03:19:16
nit: no {} for one-line bodies.
chfremer
2017/01/18 01:29:52
Done.
| |
| 107 command_line_parsed_ = true; | |
| 108 | |
| 105 const std::string option = | 109 const std::string option = |
| 106 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 110 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 107 switches::kUseFakeDeviceForMediaStream); | 111 switches::kUseFakeDeviceForMediaStream); |
| 108 base::StringTokenizer option_tokenizer(option, ", "); | 112 base::StringTokenizer option_tokenizer(option, ", "); |
| 109 option_tokenizer.set_quote_chars("\""); | 113 option_tokenizer.set_quote_chars("\""); |
| 110 | 114 |
| 111 while (option_tokenizer.GetNext()) { | 115 while (option_tokenizer.GetNext()) { |
| 112 std::vector<std::string> param = | 116 std::vector<std::string> param = |
| 113 base::SplitString(option_tokenizer.token(), "=", base::TRIM_WHITESPACE, | 117 base::SplitString(option_tokenizer.token(), "=", base::TRIM_WHITESPACE, |
| 114 base::SPLIT_WANT_NONEMPTY); | 118 base::SPLIT_WANT_NONEMPTY); |
| 115 | 119 |
| 116 if (param.size() != 2u) { | 120 if (param.size() != 2u) { |
| 117 LOG(WARNING) << "Forget a value '" << option << "'? Use name=value for " | 121 LOG(WARNING) << "Forget a value '" << option << "'? Use name=value for " |
| 118 << switches::kUseFakeDeviceForMediaStream << "."; | 122 << switches::kUseFakeDeviceForMediaStream << "."; |
| 119 return; | 123 return; |
| 120 } | 124 } |
| 121 | 125 |
| 122 if (base::EqualsCaseInsensitiveASCII(param.front(), "ownership") && | 126 if (base::EqualsCaseInsensitiveASCII(param.front(), "ownership") && |
| 123 base::EqualsCaseInsensitiveASCII(param.back(), "client")) { | 127 base::EqualsCaseInsensitiveASCII(param.back(), "client")) { |
| 124 fake_vcd_ownership_ = | 128 delivery_mode_ = |
| 125 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS; | 129 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_CLIENT_BUFFERS; |
| 126 } else if (base::EqualsCaseInsensitiveASCII(param.front(), "fps")) { | 130 } else if (base::EqualsCaseInsensitiveASCII(param.front(), "fps")) { |
| 127 double fps = 0; | 131 double fps = 0; |
| 128 if (base::StringToDouble(param.back(), &fps)) { | 132 if (base::StringToDouble(param.back(), &fps)) { |
| 129 frame_rate_ = | 133 output_frame_rate_ = |
| 130 std::max(kFakeCaptureMinFrameRate, static_cast<float>(fps)); | 134 std::max(kFakeCaptureMinFrameRate, static_cast<float>(fps)); |
| 131 frame_rate_ = std::min(kFakeCaptureMaxFrameRate, frame_rate_); | 135 output_frame_rate_ = |
| 136 std::min(kFakeCaptureMaxFrameRate, output_frame_rate_); | |
| 132 } | 137 } |
| 133 } else if (base::EqualsCaseInsensitiveASCII(param.front(), | 138 } else if (base::EqualsCaseInsensitiveASCII(param.front(), |
| 134 "device-count")) { | 139 "device-count")) { |
| 135 unsigned int count = 0; | 140 unsigned int count = 0; |
| 136 if (base::StringToUint(param.back(), &count)) { | 141 if (base::StringToUint(param.back(), &count)) { |
| 137 number_of_devices_ = std::min( | 142 number_of_devices_ = std::min( |
| 138 kFakeCaptureMaxDeviceCount, | 143 kFakeCaptureMaxDeviceCount, |
| 139 std::max(kFakeCaptureMinDeviceCount, static_cast<int>(count))); | 144 std::max(kFakeCaptureMinDeviceCount, static_cast<int>(count))); |
| 140 } | 145 } |
| 141 } | 146 } |
| 142 } | 147 } |
| 143 } | 148 } |
| 144 | 149 |
| 145 } // namespace media | 150 } // namespace media |
| OLD | NEW |