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