jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 27 | |
| 28 | #include "talk/media/webrtc/webrtcvideocapturer.h" |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #ifdef HAVE_WEBRTC_VIDEO |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 35 | #include "talk/media/webrtc/webrtcvideoframe.h" |
| 36 | #include "talk/media/webrtc/webrtcvideoframefactory.h" |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 37 | #include "webrtc/base/bind.h" |
| 38 | #include "webrtc/base/checks.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 39 | #include "webrtc/base/criticalsection.h" |
| 40 | #include "webrtc/base/logging.h" |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 41 | #include "webrtc/base/safe_conversions.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 42 | #include "webrtc/base/thread.h" |
| 43 | #include "webrtc/base/timeutils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 45 | #include "webrtc/base/win32.h" // Need this to #include the impl files. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | #include "webrtc/modules/video_capture/include/video_capture_factory.h" |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 47 | #include "webrtc/system_wrappers/interface/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | |
| 49 | namespace cricket { |
| 50 | |
| 51 | struct kVideoFourCCEntry { |
| 52 | uint32 fourcc; |
| 53 | webrtc::RawVideoType webrtc_type; |
| 54 | }; |
| 55 | |
| 56 | // This indicates our format preferences and defines a mapping between |
| 57 | // webrtc::RawVideoType (from video_capture_defines.h) to our FOURCCs. |
| 58 | static kVideoFourCCEntry kSupportedFourCCs[] = { |
| 59 | { FOURCC_I420, webrtc::kVideoI420 }, // 12 bpp, no conversion. |
| 60 | { FOURCC_YV12, webrtc::kVideoYV12 }, // 12 bpp, no conversion. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | { FOURCC_YUY2, webrtc::kVideoYUY2 }, // 16 bpp, fast conversion. |
| 62 | { FOURCC_UYVY, webrtc::kVideoUYVY }, // 16 bpp, fast conversion. |
buildbot@webrtc.org | fa5fcd6 | 2014-07-21 21:55:43 +0000 | [diff] [blame] | 63 | { FOURCC_NV12, webrtc::kVideoNV12 }, // 12 bpp, fast conversion. |
| 64 | { FOURCC_NV21, webrtc::kVideoNV21 }, // 12 bpp, fast conversion. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | { FOURCC_MJPG, webrtc::kVideoMJPEG }, // compressed, slow conversion. |
| 66 | { FOURCC_ARGB, webrtc::kVideoARGB }, // 32 bpp, slow conversion. |
| 67 | { FOURCC_24BG, webrtc::kVideoRGB24 }, // 24 bpp, slow conversion. |
| 68 | }; |
| 69 | |
| 70 | class WebRtcVcmFactory : public WebRtcVcmFactoryInterface { |
| 71 | public: |
| 72 | virtual webrtc::VideoCaptureModule* Create(int id, const char* device) { |
| 73 | return webrtc::VideoCaptureFactory::Create(id, device); |
| 74 | } |
| 75 | virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo(int id) { |
| 76 | return webrtc::VideoCaptureFactory::CreateDeviceInfo(id); |
| 77 | } |
| 78 | virtual void DestroyDeviceInfo(webrtc::VideoCaptureModule::DeviceInfo* info) { |
| 79 | delete info; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | static bool CapabilityToFormat(const webrtc::VideoCaptureCapability& cap, |
| 84 | VideoFormat* format) { |
| 85 | uint32 fourcc = 0; |
| 86 | for (size_t i = 0; i < ARRAY_SIZE(kSupportedFourCCs); ++i) { |
| 87 | if (kSupportedFourCCs[i].webrtc_type == cap.rawType) { |
| 88 | fourcc = kSupportedFourCCs[i].fourcc; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | if (fourcc == 0) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | format->fourcc = fourcc; |
| 97 | format->width = cap.width; |
| 98 | format->height = cap.height; |
| 99 | format->interval = VideoFormat::FpsToInterval(cap.maxFPS); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | static bool FormatToCapability(const VideoFormat& format, |
| 104 | webrtc::VideoCaptureCapability* cap) { |
| 105 | webrtc::RawVideoType webrtc_type = webrtc::kVideoUnknown; |
| 106 | for (size_t i = 0; i < ARRAY_SIZE(kSupportedFourCCs); ++i) { |
| 107 | if (kSupportedFourCCs[i].fourcc == format.fourcc) { |
| 108 | webrtc_type = kSupportedFourCCs[i].webrtc_type; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | if (webrtc_type == webrtc::kVideoUnknown) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | cap->width = format.width; |
| 117 | cap->height = format.height; |
| 118 | cap->maxFPS = VideoFormat::IntervalToFps(format.interval); |
| 119 | cap->expectedCaptureDelay = 0; |
| 120 | cap->rawType = webrtc_type; |
| 121 | cap->codecType = webrtc::kVideoCodecUnknown; |
| 122 | cap->interlaced = false; |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | /////////////////////////////////////////////////////////////////////////// |
| 127 | // Implementation of class WebRtcVideoCapturer |
| 128 | /////////////////////////////////////////////////////////////////////////// |
| 129 | |
| 130 | WebRtcVideoCapturer::WebRtcVideoCapturer() |
| 131 | : factory_(new WebRtcVcmFactory), |
| 132 | module_(NULL), |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 133 | captured_frames_(0), |
| 134 | start_thread_(nullptr) { |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 135 | set_frame_factory(new WebRtcVideoFrameFactory()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | WebRtcVideoCapturer::WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory) |
| 139 | : factory_(factory), |
| 140 | module_(NULL), |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 141 | captured_frames_(0), |
| 142 | start_thread_(nullptr) { |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 143 | set_frame_factory(new WebRtcVideoFrameFactory()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | WebRtcVideoCapturer::~WebRtcVideoCapturer() { |
| 147 | if (module_) { |
| 148 | module_->Release(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | bool WebRtcVideoCapturer::Init(const Device& device) { |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 153 | DCHECK(!start_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | if (module_) { |
| 155 | LOG(LS_ERROR) << "The capturer is already initialized"; |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | webrtc::VideoCaptureModule::DeviceInfo* info = factory_->CreateDeviceInfo(0); |
| 160 | if (!info) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | // Find the desired camera, by name. |
| 165 | // In the future, comparing IDs will be more robust. |
| 166 | // TODO(juberti): Figure what's needed to allow this. |
| 167 | int num_cams = info->NumberOfDevices(); |
| 168 | char vcm_id[256] = ""; |
| 169 | bool found = false; |
| 170 | for (int index = 0; index < num_cams; ++index) { |
| 171 | char vcm_name[256]; |
| 172 | if (info->GetDeviceName(index, vcm_name, ARRAY_SIZE(vcm_name), |
| 173 | vcm_id, ARRAY_SIZE(vcm_id)) != -1) { |
| 174 | if (device.name == reinterpret_cast<char*>(vcm_name)) { |
| 175 | found = true; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | if (!found) { |
| 181 | LOG(LS_WARNING) << "Failed to find capturer for id: " << device.id; |
| 182 | factory_->DestroyDeviceInfo(info); |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | // Enumerate the supported formats. |
| 187 | // TODO(juberti): Find out why this starts/stops the camera... |
| 188 | std::vector<VideoFormat> supported; |
| 189 | int32_t num_caps = info->NumberOfCapabilities(vcm_id); |
| 190 | for (int32_t i = 0; i < num_caps; ++i) { |
| 191 | webrtc::VideoCaptureCapability cap; |
| 192 | if (info->GetCapability(vcm_id, i, cap) != -1) { |
| 193 | VideoFormat format; |
| 194 | if (CapabilityToFormat(cap, &format)) { |
| 195 | supported.push_back(format); |
| 196 | } else { |
| 197 | LOG(LS_WARNING) << "Ignoring unsupported WebRTC capture format " |
| 198 | << cap.rawType; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | factory_->DestroyDeviceInfo(info); |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame^] | 203 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | if (supported.empty()) { |
| 205 | LOG(LS_ERROR) << "Failed to find usable formats for id: " << device.id; |
| 206 | return false; |
| 207 | } |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame^] | 208 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 209 | module_ = factory_->Create(0, vcm_id); |
| 210 | if (!module_) { |
| 211 | LOG(LS_ERROR) << "Failed to create capturer for id: " << device.id; |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | // It is safe to change member attributes now. |
| 216 | module_->AddRef(); |
| 217 | SetId(device.id); |
| 218 | SetSupportedFormats(supported); |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 219 | |
| 220 | // Ensure these 2 have the same value. |
| 221 | SetApplyRotation(module_->GetApplyRotation()); |
| 222 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | |
| 226 | bool WebRtcVideoCapturer::Init(webrtc::VideoCaptureModule* module) { |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 227 | DCHECK(!start_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 228 | if (module_) { |
| 229 | LOG(LS_ERROR) << "The capturer is already initialized"; |
| 230 | return false; |
| 231 | } |
| 232 | if (!module) { |
| 233 | LOG(LS_ERROR) << "Invalid VCM supplied"; |
| 234 | return false; |
| 235 | } |
| 236 | // TODO(juberti): Set id and formats. |
| 237 | (module_ = module)->AddRef(); |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | bool WebRtcVideoCapturer::GetBestCaptureFormat(const VideoFormat& desired, |
| 242 | VideoFormat* best_format) { |
| 243 | if (!best_format) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | if (!VideoCapturer::GetBestCaptureFormat(desired, best_format)) { |
| 248 | // We maybe using a manually injected VCM which doesn't support enum. |
| 249 | // Use the desired format as the best format. |
| 250 | best_format->width = desired.width; |
| 251 | best_format->height = desired.height; |
| 252 | best_format->fourcc = FOURCC_I420; |
| 253 | best_format->interval = desired.interval; |
| 254 | LOG(LS_INFO) << "Failed to find best capture format," |
| 255 | << " fall back to the requested format " |
| 256 | << best_format->ToString(); |
| 257 | } |
| 258 | return true; |
| 259 | } |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 260 | bool WebRtcVideoCapturer::SetApplyRotation(bool enable) { |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 261 | // Can't take lock here as this will cause deadlock with |
| 262 | // OnIncomingCapturedFrame. In fact, the whole method, including methods it |
| 263 | // calls, can't take lock. |
Fredrik Solenberg | d3ddc1b | 2015-05-07 17:07:34 +0200 | [diff] [blame] | 264 | DCHECK(module_); |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 265 | |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 266 | const std::string group_name = |
| 267 | webrtc::field_trial::FindFullName("WebRTC-CVO"); |
| 268 | |
| 269 | if (group_name == "Disabled") { |
| 270 | return true; |
| 271 | } |
| 272 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 273 | if (!VideoCapturer::SetApplyRotation(enable)) { |
| 274 | return false; |
| 275 | } |
| 276 | return module_->SetApplyRotation(enable); |
| 277 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 278 | |
| 279 | CaptureState WebRtcVideoCapturer::Start(const VideoFormat& capture_format) { |
| 280 | if (!module_) { |
| 281 | LOG(LS_ERROR) << "The capturer has not been initialized"; |
| 282 | return CS_NO_DEVICE; |
| 283 | } |
| 284 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 285 | rtc::CritScope cs(&critical_section_stopping_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 286 | if (IsRunning()) { |
| 287 | LOG(LS_ERROR) << "The capturer is already running"; |
| 288 | return CS_FAILED; |
| 289 | } |
| 290 | |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 291 | DCHECK(!start_thread_); |
| 292 | |
| 293 | start_thread_ = rtc::Thread::Current(); |
| 294 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 295 | SetCaptureFormat(&capture_format); |
| 296 | |
| 297 | webrtc::VideoCaptureCapability cap; |
| 298 | if (!FormatToCapability(capture_format, &cap)) { |
| 299 | LOG(LS_ERROR) << "Invalid capture format specified"; |
| 300 | return CS_FAILED; |
| 301 | } |
| 302 | |
| 303 | std::string camera_id(GetId()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 304 | uint32 start = rtc::Time(); |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 305 | module_->RegisterCaptureDataCallback(*this); |
| 306 | if (module_->StartCapture(cap) != 0) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 307 | LOG(LS_ERROR) << "Camera '" << camera_id << "' failed to start"; |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 308 | start_thread_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 309 | return CS_FAILED; |
| 310 | } |
| 311 | |
| 312 | LOG(LS_INFO) << "Camera '" << camera_id << "' started with format " |
| 313 | << capture_format.ToString() << ", elapsed time " |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 314 | << rtc::TimeSince(start) << " ms"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 315 | |
| 316 | captured_frames_ = 0; |
| 317 | SetCaptureState(CS_RUNNING); |
| 318 | return CS_STARTING; |
| 319 | } |
| 320 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 321 | // Critical section blocks Stop from shutting down during callbacks from capture |
| 322 | // thread to OnIncomingCapturedFrame. Note that the crit is try-locked in |
| 323 | // OnFrameCaptured, as the lock ordering between this and the system component |
| 324 | // controlling the camera is reversed: system frame -> OnIncomingCapturedFrame; |
| 325 | // Stop -> system stop camera). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 326 | void WebRtcVideoCapturer::Stop() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 327 | rtc::CritScope cs(&critical_section_stopping_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 328 | if (IsRunning()) { |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 329 | DCHECK(start_thread_); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 330 | rtc::Thread::Current()->Clear(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 331 | module_->StopCapture(); |
| 332 | module_->DeRegisterCaptureDataCallback(); |
| 333 | |
| 334 | // TODO(juberti): Determine if the VCM exposes any drop stats we can use. |
| 335 | double drop_ratio = 0.0; |
| 336 | std::string camera_id(GetId()); |
| 337 | LOG(LS_INFO) << "Camera '" << camera_id << "' stopped after capturing " |
| 338 | << captured_frames_ << " frames and dropping " |
| 339 | << drop_ratio << "%"; |
| 340 | } |
| 341 | SetCaptureFormat(NULL); |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 342 | start_thread_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | bool WebRtcVideoCapturer::IsRunning() { |
| 346 | return (module_ != NULL && module_->CaptureStarted()); |
| 347 | } |
| 348 | |
| 349 | bool WebRtcVideoCapturer::GetPreferredFourccs( |
| 350 | std::vector<uint32>* fourccs) { |
| 351 | if (!fourccs) { |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | fourccs->clear(); |
| 356 | for (size_t i = 0; i < ARRAY_SIZE(kSupportedFourCCs); ++i) { |
| 357 | fourccs->push_back(kSupportedFourCCs[i].fourcc); |
| 358 | } |
| 359 | return true; |
| 360 | } |
| 361 | |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 362 | void WebRtcVideoCapturer::OnIncomingCapturedFrame( |
| 363 | const int32_t id, |
| 364 | const webrtc::I420VideoFrame& sample) { |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 365 | // This would be a normal CritScope, except that it's possible that: |
| 366 | // (1) whatever system component producing this frame has taken a lock, and |
| 367 | // (2) Stop() probably calls back into that system component, which may take |
| 368 | // the same lock. Due to the reversed order, we have to try-lock in order to |
| 369 | // avoid a potential deadlock. Besides, if we can't enter because we're |
| 370 | // stopping, we may as well drop the frame. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 371 | rtc::TryCritScope cs(&critical_section_stopping_); |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 372 | if (!cs.locked() || !IsRunning()) { |
| 373 | // Capturer has been stopped or is in the process of stopping. |
| 374 | return; |
| 375 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 376 | |
| 377 | ++captured_frames_; |
| 378 | // Log the size and pixel aspect ratio of the first captured frame. |
| 379 | if (1 == captured_frames_) { |
| 380 | LOG(LS_INFO) << "Captured frame size " |
magjed@webrtc.org | 2056ee3 | 2015-03-16 13:46:52 +0000 | [diff] [blame] | 381 | << sample.width() << "x" << sample.height() |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 382 | << ". Expected format " << GetCaptureFormat()->ToString(); |
| 383 | } |
| 384 | |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 385 | if (start_thread_->IsCurrent()) { |
magjed@webrtc.org | 2056ee3 | 2015-03-16 13:46:52 +0000 | [diff] [blame] | 386 | SignalFrameCapturedOnStartThread(&sample); |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 387 | } else { |
| 388 | // This currently happens on with at least VideoCaptureModuleV4L2 and |
| 389 | // possibly other implementations of WebRTC's VideoCaptureModule. |
| 390 | // In order to maintain the threading contract with the upper layers and |
| 391 | // consistency with other capturers such as in Chrome, we need to do a |
| 392 | // thread hop. |
| 393 | start_thread_->Invoke<void>( |
| 394 | rtc::Bind(&WebRtcVideoCapturer::SignalFrameCapturedOnStartThread, |
magjed@webrtc.org | 2056ee3 | 2015-03-16 13:46:52 +0000 | [diff] [blame] | 395 | this, &sample)); |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 396 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void WebRtcVideoCapturer::OnCaptureDelayChanged(const int32_t id, |
| 400 | const int32_t delay) { |
| 401 | LOG(LS_INFO) << "Capture delay changed to " << delay << " ms"; |
| 402 | } |
| 403 | |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 404 | void WebRtcVideoCapturer::SignalFrameCapturedOnStartThread( |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 405 | const webrtc::I420VideoFrame* frame) { |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 406 | DCHECK(start_thread_->IsCurrent()); |
| 407 | // Signal down stream components on captured frame. |
| 408 | // The CapturedFrame class doesn't support planes. We have to ExtractBuffer |
| 409 | // to one block for it. |
| 410 | size_t length = |
| 411 | webrtc::CalcBufferSize(webrtc::kI420, frame->width(), frame->height()); |
| 412 | capture_buffer_.resize(length); |
| 413 | // TODO(magjed): Refactor the WebRtcCapturedFrame to avoid memory copy or |
| 414 | // take over ownership of the buffer held by |frame| if that's possible. |
| 415 | webrtc::ExtractBuffer(*frame, length, &capture_buffer_[0]); |
| 416 | WebRtcCapturedFrame webrtc_frame(*frame, &capture_buffer_[0], length); |
| 417 | SignalFrameCaptured(this, &webrtc_frame); |
| 418 | } |
| 419 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 420 | // WebRtcCapturedFrame |
| 421 | WebRtcCapturedFrame::WebRtcCapturedFrame(const webrtc::I420VideoFrame& sample, |
| 422 | void* buffer, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 423 | size_t length) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 424 | width = sample.width(); |
| 425 | height = sample.height(); |
| 426 | fourcc = FOURCC_I420; |
| 427 | // TODO(hellner): Support pixel aspect ratio (for OSX). |
| 428 | pixel_width = 1; |
| 429 | pixel_height = 1; |
| 430 | // Convert units from VideoFrame RenderTimeMs to CapturedFrame (nanoseconds). |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 431 | elapsed_time = sample.render_time_ms() * rtc::kNumNanosecsPerMillisec; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 432 | time_stamp = elapsed_time; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 433 | data_size = rtc::checked_cast<uint32>(length); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 434 | data = buffer; |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 435 | rotation = sample.rotation(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | } // namespace cricket |
| 439 | |
| 440 | #endif // HAVE_WEBRTC_VIDEO |