jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 9 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 10 | |
kjellander@webrtc.org | 5ad1297 | 2016-02-12 06:39:40 +0100 | [diff] [blame] | 11 | #include "webrtc/media/engine/webrtcvideocapturer.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 13 | #include "webrtc/base/arraysize.h" |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 14 | #include "webrtc/base/bind.h" |
| 15 | #include "webrtc/base/checks.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 16 | #include "webrtc/base/criticalsection.h" |
| 17 | #include "webrtc/base/logging.h" |
Tommi | d44c077 | 2016-03-11 17:12:32 -0800 | [diff] [blame] | 18 | #include "webrtc/base/safe_conversions.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 19 | #include "webrtc/base/thread.h" |
| 20 | #include "webrtc/base/timeutils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 22 | #include "webrtc/base/win32.h" // Need this to #include the impl files. |
Henrik Kjellander | 5dda80a | 2015-11-12 12:46:09 +0100 | [diff] [blame] | 23 | #include "webrtc/modules/video_capture/video_capture_factory.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/include/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | |
| 26 | namespace cricket { |
| 27 | |
| 28 | struct kVideoFourCCEntry { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 29 | uint32_t fourcc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | webrtc::RawVideoType webrtc_type; |
| 31 | }; |
| 32 | |
| 33 | // This indicates our format preferences and defines a mapping between |
| 34 | // webrtc::RawVideoType (from video_capture_defines.h) to our FOURCCs. |
| 35 | static kVideoFourCCEntry kSupportedFourCCs[] = { |
| 36 | { FOURCC_I420, webrtc::kVideoI420 }, // 12 bpp, no conversion. |
| 37 | { FOURCC_YV12, webrtc::kVideoYV12 }, // 12 bpp, no conversion. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | { FOURCC_YUY2, webrtc::kVideoYUY2 }, // 16 bpp, fast conversion. |
| 39 | { FOURCC_UYVY, webrtc::kVideoUYVY }, // 16 bpp, fast conversion. |
buildbot@webrtc.org | fa5fcd6 | 2014-07-21 21:55:43 +0000 | [diff] [blame] | 40 | { FOURCC_NV12, webrtc::kVideoNV12 }, // 12 bpp, fast conversion. |
| 41 | { FOURCC_NV21, webrtc::kVideoNV21 }, // 12 bpp, fast conversion. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | { FOURCC_MJPG, webrtc::kVideoMJPEG }, // compressed, slow conversion. |
| 43 | { FOURCC_ARGB, webrtc::kVideoARGB }, // 32 bpp, slow conversion. |
| 44 | { FOURCC_24BG, webrtc::kVideoRGB24 }, // 24 bpp, slow conversion. |
| 45 | }; |
| 46 | |
| 47 | class WebRtcVcmFactory : public WebRtcVcmFactoryInterface { |
| 48 | public: |
Peter Boström | 09c3a1e | 2016-03-22 17:17:39 +0100 | [diff] [blame] | 49 | virtual rtc::scoped_refptr<webrtc::VideoCaptureModule> Create( |
Peter Boström | 09c3a1e | 2016-03-22 17:17:39 +0100 | [diff] [blame] | 50 | const char* device) { |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 51 | return webrtc::VideoCaptureFactory::Create(device); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | } |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 53 | virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo() { |
| 54 | return webrtc::VideoCaptureFactory::CreateDeviceInfo(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | } |
| 56 | virtual void DestroyDeviceInfo(webrtc::VideoCaptureModule::DeviceInfo* info) { |
| 57 | delete info; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | static bool CapabilityToFormat(const webrtc::VideoCaptureCapability& cap, |
| 62 | VideoFormat* format) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 63 | uint32_t fourcc = 0; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 64 | for (size_t i = 0; i < arraysize(kSupportedFourCCs); ++i) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | if (kSupportedFourCCs[i].webrtc_type == cap.rawType) { |
| 66 | fourcc = kSupportedFourCCs[i].fourcc; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | if (fourcc == 0) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | format->fourcc = fourcc; |
| 75 | format->width = cap.width; |
| 76 | format->height = cap.height; |
| 77 | format->interval = VideoFormat::FpsToInterval(cap.maxFPS); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | static bool FormatToCapability(const VideoFormat& format, |
| 82 | webrtc::VideoCaptureCapability* cap) { |
| 83 | webrtc::RawVideoType webrtc_type = webrtc::kVideoUnknown; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 84 | for (size_t i = 0; i < arraysize(kSupportedFourCCs); ++i) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | if (kSupportedFourCCs[i].fourcc == format.fourcc) { |
| 86 | webrtc_type = kSupportedFourCCs[i].webrtc_type; |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | if (webrtc_type == webrtc::kVideoUnknown) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | cap->width = format.width; |
| 95 | cap->height = format.height; |
| 96 | cap->maxFPS = VideoFormat::IntervalToFps(format.interval); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | cap->rawType = webrtc_type; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | cap->interlaced = false; |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | /////////////////////////////////////////////////////////////////////////// |
| 103 | // Implementation of class WebRtcVideoCapturer |
| 104 | /////////////////////////////////////////////////////////////////////////// |
| 105 | |
| 106 | WebRtcVideoCapturer::WebRtcVideoCapturer() |
| 107 | : factory_(new WebRtcVcmFactory), |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 108 | module_(nullptr), |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 109 | captured_frames_(0), |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 110 | start_thread_(nullptr), |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 111 | async_invoker_(nullptr) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | |
| 113 | WebRtcVideoCapturer::WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory) |
| 114 | : factory_(factory), |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 115 | module_(nullptr), |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 116 | captured_frames_(0), |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 117 | start_thread_(nullptr), |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 118 | async_invoker_(nullptr) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | |
Peter Boström | 09c3a1e | 2016-03-22 17:17:39 +0100 | [diff] [blame] | 120 | WebRtcVideoCapturer::~WebRtcVideoCapturer() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | |
| 122 | bool WebRtcVideoCapturer::Init(const Device& device) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 123 | RTC_DCHECK(!start_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | if (module_) { |
| 125 | LOG(LS_ERROR) << "The capturer is already initialized"; |
| 126 | return false; |
| 127 | } |
| 128 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 129 | webrtc::VideoCaptureModule::DeviceInfo* info = factory_->CreateDeviceInfo(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 130 | if (!info) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // Find the desired camera, by name. |
| 135 | // In the future, comparing IDs will be more robust. |
| 136 | // TODO(juberti): Figure what's needed to allow this. |
| 137 | int num_cams = info->NumberOfDevices(); |
| 138 | char vcm_id[256] = ""; |
| 139 | bool found = false; |
| 140 | for (int index = 0; index < num_cams; ++index) { |
| 141 | char vcm_name[256]; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 142 | if (info->GetDeviceName(index, vcm_name, arraysize(vcm_name), vcm_id, |
| 143 | arraysize(vcm_id)) != -1) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | if (device.name == reinterpret_cast<char*>(vcm_name)) { |
| 145 | found = true; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | if (!found) { |
| 151 | LOG(LS_WARNING) << "Failed to find capturer for id: " << device.id; |
| 152 | factory_->DestroyDeviceInfo(info); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | // Enumerate the supported formats. |
| 157 | // TODO(juberti): Find out why this starts/stops the camera... |
| 158 | std::vector<VideoFormat> supported; |
| 159 | int32_t num_caps = info->NumberOfCapabilities(vcm_id); |
| 160 | for (int32_t i = 0; i < num_caps; ++i) { |
| 161 | webrtc::VideoCaptureCapability cap; |
| 162 | if (info->GetCapability(vcm_id, i, cap) != -1) { |
| 163 | VideoFormat format; |
| 164 | if (CapabilityToFormat(cap, &format)) { |
| 165 | supported.push_back(format); |
| 166 | } else { |
| 167 | LOG(LS_WARNING) << "Ignoring unsupported WebRTC capture format " |
| 168 | << cap.rawType; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | factory_->DestroyDeviceInfo(info); |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 173 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 174 | if (supported.empty()) { |
| 175 | LOG(LS_ERROR) << "Failed to find usable formats for id: " << device.id; |
| 176 | return false; |
| 177 | } |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 178 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 179 | module_ = factory_->Create(vcm_id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 180 | if (!module_) { |
| 181 | LOG(LS_ERROR) << "Failed to create capturer for id: " << device.id; |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | // It is safe to change member attributes now. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 186 | SetId(device.id); |
| 187 | SetSupportedFormats(supported); |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 188 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
Peter Boström | 09c3a1e | 2016-03-22 17:17:39 +0100 | [diff] [blame] | 192 | bool WebRtcVideoCapturer::Init( |
| 193 | const rtc::scoped_refptr<webrtc::VideoCaptureModule>& module) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 194 | RTC_DCHECK(!start_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | if (module_) { |
| 196 | LOG(LS_ERROR) << "The capturer is already initialized"; |
| 197 | return false; |
| 198 | } |
| 199 | if (!module) { |
| 200 | LOG(LS_ERROR) << "Invalid VCM supplied"; |
| 201 | return false; |
| 202 | } |
| 203 | // TODO(juberti): Set id and formats. |
Peter Boström | 09c3a1e | 2016-03-22 17:17:39 +0100 | [diff] [blame] | 204 | module_ = module; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
| 208 | bool WebRtcVideoCapturer::GetBestCaptureFormat(const VideoFormat& desired, |
| 209 | VideoFormat* best_format) { |
| 210 | if (!best_format) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | if (!VideoCapturer::GetBestCaptureFormat(desired, best_format)) { |
| 215 | // We maybe using a manually injected VCM which doesn't support enum. |
| 216 | // Use the desired format as the best format. |
| 217 | best_format->width = desired.width; |
| 218 | best_format->height = desired.height; |
| 219 | best_format->fourcc = FOURCC_I420; |
| 220 | best_format->interval = desired.interval; |
| 221 | LOG(LS_INFO) << "Failed to find best capture format," |
| 222 | << " fall back to the requested format " |
| 223 | << best_format->ToString(); |
| 224 | } |
| 225 | return true; |
| 226 | } |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 227 | void WebRtcVideoCapturer::OnSinkWantsChanged(const rtc::VideoSinkWants& wants) { |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 228 | // Can't take lock here as this will cause deadlock with |
| 229 | // OnIncomingCapturedFrame. In fact, the whole method, including methods it |
| 230 | // calls, can't take lock. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 231 | RTC_DCHECK(module_); |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 232 | |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame^] | 233 | if (webrtc::field_trial::FindFullName("WebRTC-CVO").find("Disabled") == 0) |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 234 | return; |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 235 | |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 236 | VideoCapturer::OnSinkWantsChanged(wants); |
| 237 | bool result = module_->SetApplyRotation(wants.rotation_applied); |
| 238 | RTC_CHECK(result); |
| 239 | |
| 240 | return; |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 241 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 242 | |
| 243 | CaptureState WebRtcVideoCapturer::Start(const VideoFormat& capture_format) { |
| 244 | if (!module_) { |
| 245 | LOG(LS_ERROR) << "The capturer has not been initialized"; |
Per | fb45d17 | 2016-02-29 12:07:35 +0100 | [diff] [blame] | 246 | return CS_FAILED; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | } |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 248 | if (start_thread_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 249 | LOG(LS_ERROR) << "The capturer is already running"; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 250 | RTC_DCHECK(start_thread_->IsCurrent()) |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 251 | << "Trying to start capturer on different threads"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 252 | return CS_FAILED; |
| 253 | } |
| 254 | |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 255 | start_thread_ = rtc::Thread::Current(); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 256 | RTC_DCHECK(!async_invoker_); |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 257 | async_invoker_.reset(new rtc::AsyncInvoker()); |
| 258 | captured_frames_ = 0; |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 259 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 260 | SetCaptureFormat(&capture_format); |
| 261 | |
| 262 | webrtc::VideoCaptureCapability cap; |
| 263 | if (!FormatToCapability(capture_format, &cap)) { |
| 264 | LOG(LS_ERROR) << "Invalid capture format specified"; |
| 265 | return CS_FAILED; |
| 266 | } |
| 267 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 268 | int64_t start = rtc::TimeMillis(); |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 269 | module_->RegisterCaptureDataCallback(this); |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 270 | if (module_->StartCapture(cap) != 0) { |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 271 | LOG(LS_ERROR) << "Camera '" << GetId() << "' failed to start"; |
| 272 | module_->DeRegisterCaptureDataCallback(); |
| 273 | async_invoker_.reset(); |
| 274 | SetCaptureFormat(nullptr); |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 275 | start_thread_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 276 | return CS_FAILED; |
| 277 | } |
| 278 | |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 279 | LOG(LS_INFO) << "Camera '" << GetId() << "' started with format " |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 280 | << capture_format.ToString() << ", elapsed time " |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 281 | << rtc::TimeSince(start) << " ms"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 282 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 283 | SetCaptureState(CS_RUNNING); |
| 284 | return CS_STARTING; |
| 285 | } |
| 286 | |
| 287 | void WebRtcVideoCapturer::Stop() { |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 288 | if (!start_thread_) { |
| 289 | LOG(LS_ERROR) << "The capturer is already stopped"; |
| 290 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 291 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 292 | RTC_DCHECK(start_thread_); |
| 293 | RTC_DCHECK(start_thread_->IsCurrent()); |
| 294 | RTC_DCHECK(async_invoker_); |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 295 | if (IsRunning()) { |
| 296 | // The module is responsible for OnIncomingCapturedFrame being called, if |
| 297 | // we stop it we will get no further callbacks. |
| 298 | module_->StopCapture(); |
| 299 | } |
| 300 | module_->DeRegisterCaptureDataCallback(); |
| 301 | |
| 302 | // TODO(juberti): Determine if the VCM exposes any drop stats we can use. |
| 303 | double drop_ratio = 0.0; |
| 304 | LOG(LS_INFO) << "Camera '" << GetId() << "' stopped after capturing " |
| 305 | << captured_frames_ << " frames and dropping " |
| 306 | << drop_ratio << "%"; |
| 307 | |
| 308 | // Clear any pending async invokes (that OnIncomingCapturedFrame may have |
| 309 | // caused). |
| 310 | async_invoker_.reset(); |
| 311 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 312 | SetCaptureFormat(NULL); |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 313 | start_thread_ = nullptr; |
hbos | 0de97f1 | 2015-12-01 02:13:33 -0800 | [diff] [blame] | 314 | SetCaptureState(CS_STOPPED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | bool WebRtcVideoCapturer::IsRunning() { |
| 318 | return (module_ != NULL && module_->CaptureStarted()); |
| 319 | } |
| 320 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 321 | bool WebRtcVideoCapturer::GetPreferredFourccs(std::vector<uint32_t>* fourccs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 322 | if (!fourccs) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | fourccs->clear(); |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 327 | for (size_t i = 0; i < arraysize(kSupportedFourCCs); ++i) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 328 | fourccs->push_back(kSupportedFourCCs[i].fourcc); |
| 329 | } |
| 330 | return true; |
| 331 | } |
| 332 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 333 | void WebRtcVideoCapturer::OnFrame( |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 334 | const webrtc::VideoFrame& sample) { |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 335 | // This can only happen between Start() and Stop(). |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 336 | RTC_DCHECK(start_thread_); |
| 337 | RTC_DCHECK(async_invoker_); |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 338 | |
| 339 | ++captured_frames_; |
| 340 | // Log the size and pixel aspect ratio of the first captured frame. |
| 341 | if (1 == captured_frames_) { |
| 342 | LOG(LS_INFO) << "Captured frame size " |
nisse | 7924697 | 2016-08-23 05:50:09 -0700 | [diff] [blame] | 343 | << sample.width() << "x" << sample.height() |
Henrik Boström | cbe408a | 2015-05-27 10:11:34 +0200 | [diff] [blame] | 344 | << ". Expected format " << GetCaptureFormat()->ToString(); |
| 345 | } |
| 346 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 347 | VideoCapturer::OnFrame(sample, sample.width(), sample.height()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | } // namespace cricket |