| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // Implementation file of class VideoCapturer. | 11 // Implementation file of class VideoCapturer. |
| 12 | 12 |
| 13 #include "webrtc/media/base/videocapturer.h" | 13 #include "webrtc/media/base/videocapturer.h" |
| 14 | 14 |
| 15 #include <algorithm> | 15 #include <algorithm> |
| 16 | 16 |
| 17 #include "libyuv/scale_argb.h" | 17 #include "libyuv/scale_argb.h" |
| 18 #include "webrtc/base/common.h" | 18 #include "webrtc/base/common.h" |
| 19 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
| 20 #include "webrtc/base/systeminfo.h" | 20 #include "webrtc/base/systeminfo.h" |
| 21 #include "webrtc/media/base/videoframefactory.h" | 21 #include "webrtc/media/base/videoframefactory.h" |
| 22 | |
| 23 #if defined(HAVE_WEBRTC_VIDEO) | |
| 24 #include "webrtc/media/engine/webrtcvideoframe.h" | 22 #include "webrtc/media/engine/webrtcvideoframe.h" |
| 25 #include "webrtc/media/engine/webrtcvideoframefactory.h" | 23 #include "webrtc/media/engine/webrtcvideoframefactory.h" |
| 26 #endif // HAVE_WEBRTC_VIDEO | |
| 27 | 24 |
| 28 namespace cricket { | 25 namespace cricket { |
| 29 | 26 |
| 30 namespace { | 27 namespace { |
| 31 | 28 |
| 32 // TODO(thorcarpenter): This is a BIG hack to flush the system with black | |
| 33 // frames. Frontends should coordinate to update the video state of a muted | |
| 34 // user. When all frontends to this consider removing the black frame business. | |
| 35 const int kNumBlackFramesOnMute = 30; | |
| 36 | |
| 37 // MessageHandler constants. | |
| 38 enum { | |
| 39 MSG_DO_PAUSE = 0, | |
| 40 MSG_DO_UNPAUSE, | |
| 41 MSG_STATE_CHANGE | |
| 42 }; | |
| 43 | |
| 44 static const int64_t kMaxDistance = ~(static_cast<int64_t>(1) << 63); | 29 static const int64_t kMaxDistance = ~(static_cast<int64_t>(1) << 63); |
| 45 #ifdef WEBRTC_LINUX | 30 #ifdef WEBRTC_LINUX |
| 46 static const int kYU12Penalty = 16; // Needs to be higher than MJPG index. | 31 static const int kYU12Penalty = 16; // Needs to be higher than MJPG index. |
| 47 #endif | 32 #endif |
| 48 static const int kDefaultScreencastFps = 5; | 33 static const int kDefaultScreencastFps = 5; |
| 49 typedef rtc::TypedMessageData<CaptureState> StateChangeParams; | |
| 50 | 34 |
| 51 // Limit stats data collections to ~20 seconds of 30fps data before dropping | 35 // Limit stats data collections to ~20 seconds of 30fps data before dropping |
| 52 // old data in case stats aren't reset for long periods of time. | 36 // old data in case stats aren't reset for long periods of time. |
| 53 static const size_t kMaxAccumulatorSize = 600; | 37 static const size_t kMaxAccumulatorSize = 600; |
| 54 | 38 |
| 55 } // namespace | 39 } // namespace |
| 56 | 40 |
| 57 ///////////////////////////////////////////////////////////////////// | 41 ///////////////////////////////////////////////////////////////////// |
| 58 // Implementation of struct CapturedFrame | 42 // Implementation of struct CapturedFrame |
| 59 ///////////////////////////////////////////////////////////////////// | 43 ///////////////////////////////////////////////////////////////////// |
| (...skipping 14 matching lines...) Expand all Loading... |
| 74 return false; | 58 return false; |
| 75 } | 59 } |
| 76 *size = data_size; | 60 *size = data_size; |
| 77 return true; | 61 return true; |
| 78 } | 62 } |
| 79 | 63 |
| 80 ///////////////////////////////////////////////////////////////////// | 64 ///////////////////////////////////////////////////////////////////// |
| 81 // Implementation of class VideoCapturer | 65 // Implementation of class VideoCapturer |
| 82 ///////////////////////////////////////////////////////////////////// | 66 ///////////////////////////////////////////////////////////////////// |
| 83 VideoCapturer::VideoCapturer() | 67 VideoCapturer::VideoCapturer() |
| 84 : thread_(rtc::Thread::Current()), | 68 : adapt_frame_drops_data_(kMaxAccumulatorSize), |
| 85 adapt_frame_drops_data_(kMaxAccumulatorSize), | |
| 86 frame_time_data_(kMaxAccumulatorSize), | 69 frame_time_data_(kMaxAccumulatorSize), |
| 87 apply_rotation_(true) { | 70 apply_rotation_(true) { |
| 88 Construct(); | 71 thread_checker_.DetachFromThread(); |
| 89 } | |
| 90 | |
| 91 VideoCapturer::VideoCapturer(rtc::Thread* thread) | |
| 92 : thread_(thread), | |
| 93 adapt_frame_drops_data_(kMaxAccumulatorSize), | |
| 94 frame_time_data_(kMaxAccumulatorSize), | |
| 95 apply_rotation_(true) { | |
| 96 Construct(); | 72 Construct(); |
| 97 } | 73 } |
| 98 | 74 |
| 99 void VideoCapturer::Construct() { | 75 void VideoCapturer::Construct() { |
| 100 ClearAspectRatio(); | 76 ratio_w_ = 0; |
| 77 ratio_h_ = 0; |
| 101 enable_camera_list_ = false; | 78 enable_camera_list_ = false; |
| 102 square_pixel_aspect_ratio_ = false; | 79 square_pixel_aspect_ratio_ = false; |
| 103 capture_state_ = CS_STOPPED; | 80 capture_state_ = CS_STOPPED; |
| 104 SignalFrameCaptured.connect(this, &VideoCapturer::OnFrameCaptured); | 81 SignalFrameCaptured.connect(this, &VideoCapturer::OnFrameCaptured); |
| 105 // TODO(perkj) SignalVideoFrame is used directly by Chrome remoting. | 82 // TODO(perkj) SignalVideoFrame is used directly by Chrome remoting. |
| 106 // Before that is refactored, SignalVideoFrame must forward frames to the | 83 // Before that is refactored, SignalVideoFrame must forward frames to the |
| 107 // |VideoBroadcaster|; | 84 // |VideoBroadcaster|; |
| 108 SignalVideoFrame.connect(this, &VideoCapturer::OnFrame); | 85 SignalVideoFrame.connect(this, &VideoCapturer::OnFrame); |
| 109 scaled_width_ = 0; | 86 scaled_width_ = 0; |
| 110 scaled_height_ = 0; | 87 scaled_height_ = 0; |
| 111 muted_ = false; | |
| 112 black_frame_count_down_ = kNumBlackFramesOnMute; | |
| 113 enable_video_adapter_ = true; | 88 enable_video_adapter_ = true; |
| 114 adapt_frame_drops_ = 0; | 89 adapt_frame_drops_ = 0; |
| 115 previous_frame_time_ = 0.0; | 90 previous_frame_time_ = 0.0; |
| 116 #ifdef HAVE_WEBRTC_VIDEO | |
| 117 // There are lots of video capturers out there that don't call | 91 // There are lots of video capturers out there that don't call |
| 118 // set_frame_factory. We can either go change all of them, or we | 92 // set_frame_factory. We can either go change all of them, or we |
| 119 // can set this default. | 93 // can set this default. |
| 120 // TODO(pthatcher): Remove this hack and require the frame factory | 94 // TODO(pthatcher): Remove this hack and require the frame factory |
| 121 // to be passed in the constructor. | 95 // to be passed in the constructor. |
| 122 set_frame_factory(new WebRtcVideoFrameFactory()); | 96 set_frame_factory(new WebRtcVideoFrameFactory()); |
| 123 #endif | |
| 124 } | 97 } |
| 125 | 98 |
| 126 const std::vector<VideoFormat>* VideoCapturer::GetSupportedFormats() const { | 99 const std::vector<VideoFormat>* VideoCapturer::GetSupportedFormats() const { |
| 127 return &filtered_supported_formats_; | 100 return &filtered_supported_formats_; |
| 128 } | 101 } |
| 129 | 102 |
| 130 bool VideoCapturer::StartCapturing(const VideoFormat& capture_format) { | 103 bool VideoCapturer::StartCapturing(const VideoFormat& capture_format) { |
| 104 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 131 previous_frame_time_ = frame_length_time_reporter_.TimerNow(); | 105 previous_frame_time_ = frame_length_time_reporter_.TimerNow(); |
| 132 CaptureState result = Start(capture_format); | 106 CaptureState result = Start(capture_format); |
| 133 const bool success = (result == CS_RUNNING) || (result == CS_STARTING); | 107 const bool success = (result == CS_RUNNING) || (result == CS_STARTING); |
| 134 if (!success) { | 108 if (!success) { |
| 135 return false; | 109 return false; |
| 136 } | 110 } |
| 137 if (result == CS_RUNNING) { | 111 if (result == CS_RUNNING) { |
| 138 SetCaptureState(result); | 112 SetCaptureState(result); |
| 139 } | 113 } |
| 140 return true; | 114 return true; |
| 141 } | 115 } |
| 142 | 116 |
| 143 void VideoCapturer::UpdateAspectRatio(int ratio_w, int ratio_h) { | |
| 144 if (ratio_w == 0 || ratio_h == 0) { | |
| 145 LOG(LS_WARNING) << "UpdateAspectRatio ignored invalid ratio: " | |
| 146 << ratio_w << "x" << ratio_h; | |
| 147 return; | |
| 148 } | |
| 149 ratio_w_ = ratio_w; | |
| 150 ratio_h_ = ratio_h; | |
| 151 } | |
| 152 | |
| 153 void VideoCapturer::ClearAspectRatio() { | |
| 154 ratio_w_ = 0; | |
| 155 ratio_h_ = 0; | |
| 156 } | |
| 157 | |
| 158 // Override this to have more control of how your device is started/stopped. | |
| 159 bool VideoCapturer::Pause(bool pause) { | |
| 160 if (pause) { | |
| 161 if (capture_state() == CS_PAUSED) { | |
| 162 return true; | |
| 163 } | |
| 164 bool is_running = capture_state() == CS_STARTING || | |
| 165 capture_state() == CS_RUNNING; | |
| 166 if (!is_running) { | |
| 167 LOG(LS_ERROR) << "Cannot pause a stopped camera."; | |
| 168 return false; | |
| 169 } | |
| 170 LOG(LS_INFO) << "Pausing a camera."; | |
| 171 std::unique_ptr<VideoFormat> capture_format_when_paused( | |
| 172 capture_format_ ? new VideoFormat(*capture_format_) : NULL); | |
| 173 Stop(); | |
| 174 SetCaptureState(CS_PAUSED); | |
| 175 // If you override this function be sure to restore the capture format | |
| 176 // after calling Stop(). | |
| 177 SetCaptureFormat(capture_format_when_paused.get()); | |
| 178 } else { // Unpause. | |
| 179 if (capture_state() != CS_PAUSED) { | |
| 180 LOG(LS_WARNING) << "Cannot unpause a camera that hasn't been paused."; | |
| 181 return false; | |
| 182 } | |
| 183 if (!capture_format_) { | |
| 184 LOG(LS_ERROR) << "Missing capture_format_, cannot unpause a camera."; | |
| 185 return false; | |
| 186 } | |
| 187 if (muted_) { | |
| 188 LOG(LS_WARNING) << "Camera cannot be unpaused while muted."; | |
| 189 return false; | |
| 190 } | |
| 191 LOG(LS_INFO) << "Unpausing a camera."; | |
| 192 if (!Start(*capture_format_)) { | |
| 193 LOG(LS_ERROR) << "Camera failed to start when unpausing."; | |
| 194 return false; | |
| 195 } | |
| 196 } | |
| 197 return true; | |
| 198 } | |
| 199 | |
| 200 bool VideoCapturer::Restart(const VideoFormat& capture_format) { | |
| 201 if (!IsRunning()) { | |
| 202 return StartCapturing(capture_format); | |
| 203 } | |
| 204 | |
| 205 if (GetCaptureFormat() != NULL && *GetCaptureFormat() == capture_format) { | |
| 206 // The reqested format is the same; nothing to do. | |
| 207 return true; | |
| 208 } | |
| 209 | |
| 210 Stop(); | |
| 211 return StartCapturing(capture_format); | |
| 212 } | |
| 213 | |
| 214 bool VideoCapturer::MuteToBlackThenPause(bool muted) { | |
| 215 if (muted == IsMuted()) { | |
| 216 return true; | |
| 217 } | |
| 218 | |
| 219 LOG(LS_INFO) << (muted ? "Muting" : "Unmuting") << " this video capturer."; | |
| 220 muted_ = muted; // Do this before calling Pause(). | |
| 221 if (muted) { | |
| 222 // Reset black frame count down. | |
| 223 black_frame_count_down_ = kNumBlackFramesOnMute; | |
| 224 // Following frames will be overritten with black, then the camera will be | |
| 225 // paused. | |
| 226 return true; | |
| 227 } | |
| 228 // Start the camera. | |
| 229 thread_->Clear(this, MSG_DO_PAUSE); | |
| 230 return Pause(false); | |
| 231 } | |
| 232 | |
| 233 void VideoCapturer::SetSupportedFormats( | 117 void VideoCapturer::SetSupportedFormats( |
| 234 const std::vector<VideoFormat>& formats) { | 118 const std::vector<VideoFormat>& formats) { |
| 119 // This method is OK to call during initialization on a separate thread. |
| 120 RTC_DCHECK(capture_state_ == CS_STOPPED || |
| 121 thread_checker_.CalledOnValidThread()); |
| 235 supported_formats_ = formats; | 122 supported_formats_ = formats; |
| 236 UpdateFilteredSupportedFormats(); | 123 UpdateFilteredSupportedFormats(); |
| 237 } | 124 } |
| 238 | 125 |
| 239 bool VideoCapturer::GetBestCaptureFormat(const VideoFormat& format, | 126 bool VideoCapturer::GetBestCaptureFormat(const VideoFormat& format, |
| 240 VideoFormat* best_format) { | 127 VideoFormat* best_format) { |
| 128 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 241 // TODO(fbarchard): Directly support max_format. | 129 // TODO(fbarchard): Directly support max_format. |
| 242 UpdateFilteredSupportedFormats(); | 130 UpdateFilteredSupportedFormats(); |
| 243 const std::vector<VideoFormat>* supported_formats = GetSupportedFormats(); | 131 const std::vector<VideoFormat>* supported_formats = GetSupportedFormats(); |
| 244 | 132 |
| 245 if (supported_formats->empty()) { | 133 if (supported_formats->empty()) { |
| 246 return false; | 134 return false; |
| 247 } | 135 } |
| 248 LOG(LS_INFO) << " Capture Requested " << format.ToString(); | 136 LOG(LS_INFO) << " Capture Requested " << format.ToString(); |
| 249 int64_t best_distance = kMaxDistance; | 137 int64_t best_distance = kMaxDistance; |
| 250 std::vector<VideoFormat>::const_iterator best = supported_formats->end(); | 138 std::vector<VideoFormat>::const_iterator best = supported_formats->end(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 269 best_format->height = best->height; | 157 best_format->height = best->height; |
| 270 best_format->fourcc = best->fourcc; | 158 best_format->fourcc = best->fourcc; |
| 271 best_format->interval = best->interval; | 159 best_format->interval = best->interval; |
| 272 LOG(LS_INFO) << " Best " << best_format->ToString() << " Interval " | 160 LOG(LS_INFO) << " Best " << best_format->ToString() << " Interval " |
| 273 << best_format->interval << " distance " << best_distance; | 161 << best_format->interval << " distance " << best_distance; |
| 274 } | 162 } |
| 275 return true; | 163 return true; |
| 276 } | 164 } |
| 277 | 165 |
| 278 void VideoCapturer::ConstrainSupportedFormats(const VideoFormat& max_format) { | 166 void VideoCapturer::ConstrainSupportedFormats(const VideoFormat& max_format) { |
| 167 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 279 max_format_.reset(new VideoFormat(max_format)); | 168 max_format_.reset(new VideoFormat(max_format)); |
| 280 LOG(LS_VERBOSE) << " ConstrainSupportedFormats " << max_format.ToString(); | 169 LOG(LS_VERBOSE) << " ConstrainSupportedFormats " << max_format.ToString(); |
| 281 UpdateFilteredSupportedFormats(); | 170 UpdateFilteredSupportedFormats(); |
| 282 } | 171 } |
| 283 | 172 |
| 284 std::string VideoCapturer::ToString(const CapturedFrame* captured_frame) const { | 173 std::string VideoCapturer::ToString(const CapturedFrame* captured_frame) const { |
| 285 std::string fourcc_name = GetFourccName(captured_frame->fourcc) + " "; | 174 std::string fourcc_name = GetFourccName(captured_frame->fourcc) + " "; |
| 286 for (std::string::const_iterator i = fourcc_name.begin(); | 175 for (std::string::const_iterator i = fourcc_name.begin(); |
| 287 i < fourcc_name.end(); ++i) { | 176 i < fourcc_name.end(); ++i) { |
| 288 // Test character is printable; Avoid isprint() which asserts on negatives. | 177 // Test character is printable; Avoid isprint() which asserts on negatives. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 312 GetVariableSnapshot(adapt_frame_drops_data_, adapt_drops_stats); | 201 GetVariableSnapshot(adapt_frame_drops_data_, adapt_drops_stats); |
| 313 GetVariableSnapshot(frame_time_data_, frame_time_stats); | 202 GetVariableSnapshot(frame_time_data_, frame_time_stats); |
| 314 *last_captured_frame_format = last_captured_frame_format_; | 203 *last_captured_frame_format = last_captured_frame_format_; |
| 315 | 204 |
| 316 adapt_frame_drops_data_.Reset(); | 205 adapt_frame_drops_data_.Reset(); |
| 317 frame_time_data_.Reset(); | 206 frame_time_data_.Reset(); |
| 318 } | 207 } |
| 319 | 208 |
| 320 void VideoCapturer::RemoveSink( | 209 void VideoCapturer::RemoveSink( |
| 321 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) { | 210 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) { |
| 211 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 322 broadcaster_.RemoveSink(sink); | 212 broadcaster_.RemoveSink(sink); |
| 323 OnSinkWantsChanged(broadcaster_.wants()); | 213 OnSinkWantsChanged(broadcaster_.wants()); |
| 324 } | 214 } |
| 325 | 215 |
| 326 void VideoCapturer::AddOrUpdateSink( | 216 void VideoCapturer::AddOrUpdateSink( |
| 327 rtc::VideoSinkInterface<cricket::VideoFrame>* sink, | 217 rtc::VideoSinkInterface<cricket::VideoFrame>* sink, |
| 328 const rtc::VideoSinkWants& wants) { | 218 const rtc::VideoSinkWants& wants) { |
| 219 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 329 broadcaster_.AddOrUpdateSink(sink, wants); | 220 broadcaster_.AddOrUpdateSink(sink, wants); |
| 330 OnSinkWantsChanged(broadcaster_.wants()); | 221 OnSinkWantsChanged(broadcaster_.wants()); |
| 331 } | 222 } |
| 332 | 223 |
| 333 void VideoCapturer::OnSinkWantsChanged(const rtc::VideoSinkWants& wants) { | 224 void VideoCapturer::OnSinkWantsChanged(const rtc::VideoSinkWants& wants) { |
| 225 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 334 apply_rotation_ = wants.rotation_applied; | 226 apply_rotation_ = wants.rotation_applied; |
| 335 if (frame_factory_) { | 227 if (frame_factory_) { |
| 336 frame_factory_->SetApplyRotation(apply_rotation_); | 228 frame_factory_->SetApplyRotation(apply_rotation_); |
| 337 } | 229 } |
| 338 | 230 |
| 339 if (video_adapter()) { | 231 if (video_adapter()) { |
| 340 video_adapter()->OnCpuResolutionRequest(wants.max_pixel_count, | 232 video_adapter()->OnCpuResolutionRequest(wants.max_pixel_count, |
| 341 wants.max_pixel_count_step_up); | 233 wants.max_pixel_count_step_up); |
| 342 } | 234 } |
| 343 } | 235 } |
| 344 | 236 |
| 345 void VideoCapturer::OnFrameCaptured(VideoCapturer*, | 237 void VideoCapturer::OnFrameCaptured(VideoCapturer*, |
| 346 const CapturedFrame* captured_frame) { | 238 const CapturedFrame* captured_frame) { |
| 347 if (muted_) { | |
| 348 if (black_frame_count_down_ == 0) { | |
| 349 thread_->Post(this, MSG_DO_PAUSE, NULL); | |
| 350 } else { | |
| 351 --black_frame_count_down_; | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 if (!broadcaster_.frame_wanted()) { | 239 if (!broadcaster_.frame_wanted()) { |
| 356 return; | 240 return; |
| 357 } | 241 } |
| 358 | 242 |
| 359 // Use a temporary buffer to scale | 243 // Use a temporary buffer to scale |
| 360 std::unique_ptr<uint8_t[]> scale_buffer; | 244 std::unique_ptr<uint8_t[]> scale_buffer; |
| 361 | |
| 362 if (IsScreencast()) { | 245 if (IsScreencast()) { |
| 363 int scaled_width, scaled_height; | 246 int scaled_width, scaled_height; |
| 364 int desired_screencast_fps = capture_format_.get() ? | 247 int desired_screencast_fps = |
| 365 VideoFormat::IntervalToFps(capture_format_->interval) : | 248 capture_format_.get() |
| 366 kDefaultScreencastFps; | 249 ? VideoFormat::IntervalToFps(capture_format_->interval) |
| 250 : kDefaultScreencastFps; |
| 367 ComputeScale(captured_frame->width, captured_frame->height, | 251 ComputeScale(captured_frame->width, captured_frame->height, |
| 368 desired_screencast_fps, &scaled_width, &scaled_height); | 252 desired_screencast_fps, &scaled_width, &scaled_height); |
| 369 | 253 |
| 370 if (FOURCC_ARGB == captured_frame->fourcc && | 254 if (FOURCC_ARGB == captured_frame->fourcc && |
| 371 (scaled_width != captured_frame->width || | 255 (scaled_width != captured_frame->width || |
| 372 scaled_height != captured_frame->height)) { | 256 scaled_height != captured_frame->height)) { |
| 373 if (scaled_width != scaled_width_ || scaled_height != scaled_height_) { | 257 if (scaled_width != scaled_width_ || scaled_height != scaled_height_) { |
| 374 LOG(LS_INFO) << "Scaling Screencast from " | 258 LOG(LS_INFO) << "Scaling Screencast from " << captured_frame->width |
| 375 << captured_frame->width << "x" | 259 << "x" << captured_frame->height << " to " << scaled_width |
| 376 << captured_frame->height << " to " | 260 << "x" << scaled_height; |
| 377 << scaled_width << "x" << scaled_height; | |
| 378 scaled_width_ = scaled_width; | 261 scaled_width_ = scaled_width; |
| 379 scaled_height_ = scaled_height; | 262 scaled_height_ = scaled_height; |
| 380 } | 263 } |
| 381 CapturedFrame* modified_frame = | 264 CapturedFrame* modified_frame = |
| 382 const_cast<CapturedFrame*>(captured_frame); | 265 const_cast<CapturedFrame*>(captured_frame); |
| 383 const int modified_frame_size = scaled_width * scaled_height * 4; | 266 const int modified_frame_size = scaled_width * scaled_height * 4; |
| 384 scale_buffer.reset(new uint8_t[modified_frame_size]); | 267 scale_buffer.reset(new uint8_t[modified_frame_size]); |
| 385 // Compute new width such that width * height is less than maximum but | 268 // Compute new width such that width * height is less than maximum but |
| 386 // maintains original captured frame aspect ratio. | 269 // maintains original captured frame aspect ratio. |
| 387 // Round down width to multiple of 4 so odd width won't round up beyond | 270 // Round down width to multiple of 4 so odd width won't round up beyond |
| 388 // maximum, and so chroma channel is even width to simplify spatial | 271 // maximum, and so chroma channel is even width to simplify spatial |
| 389 // resampling. | 272 // resampling. |
| 390 libyuv::ARGBScale(reinterpret_cast<const uint8_t*>(captured_frame->data), | 273 libyuv::ARGBScale(reinterpret_cast<const uint8_t*>(captured_frame->data), |
| 391 captured_frame->width * 4, captured_frame->width, | 274 captured_frame->width * 4, captured_frame->width, |
| 392 captured_frame->height, scale_buffer.get(), | 275 captured_frame->height, scale_buffer.get(), |
| 393 scaled_width * 4, scaled_width, scaled_height, | 276 scaled_width * 4, scaled_width, scaled_height, |
| 394 libyuv::kFilterBilinear); | 277 libyuv::kFilterBilinear); |
| 395 modified_frame->width = scaled_width; | 278 modified_frame->width = scaled_width; |
| 396 modified_frame->height = scaled_height; | 279 modified_frame->height = scaled_height; |
| 397 modified_frame->data_size = scaled_width * 4 * scaled_height; | 280 modified_frame->data_size = scaled_width * 4 * scaled_height; |
| 398 modified_frame->data = scale_buffer.get(); | 281 modified_frame->data = scale_buffer.get(); |
| 399 } | 282 } |
| 400 } | 283 } |
| 401 | 284 |
| 402 const int kYuy2Bpp = 2; | 285 const int kYuy2Bpp = 2; |
| 403 const int kArgbBpp = 4; | 286 const int kArgbBpp = 4; |
| 404 // TODO(fbarchard): Make a helper function to adjust pixels to square. | 287 // TODO(fbarchard): Make a helper function to adjust pixels to square. |
| 405 // TODO(fbarchard): Hook up experiment to scaling. | 288 // TODO(fbarchard): Hook up experiment to scaling. |
| 406 // TODO(fbarchard): Avoid scale and convert if muted. | |
| 407 // Temporary buffer is scoped here so it will persist until i420_frame.Init() | 289 // Temporary buffer is scoped here so it will persist until i420_frame.Init() |
| 408 // makes a copy of the frame, converting to I420. | 290 // makes a copy of the frame, converting to I420. |
| 409 std::unique_ptr<uint8_t[]> temp_buffer; | 291 std::unique_ptr<uint8_t[]> temp_buffer; |
| 410 // YUY2 can be scaled vertically using an ARGB scaler. Aspect ratio is only | 292 // YUY2 can be scaled vertically using an ARGB scaler. Aspect ratio is only |
| 411 // a problem on OSX. OSX always converts webcams to YUY2 or UYVY. | 293 // a problem on OSX. OSX always converts webcams to YUY2 or UYVY. |
| 412 bool can_scale = | 294 bool can_scale = |
| 413 FOURCC_YUY2 == CanonicalFourCC(captured_frame->fourcc) || | 295 FOURCC_YUY2 == CanonicalFourCC(captured_frame->fourcc) || |
| 414 FOURCC_UYVY == CanonicalFourCC(captured_frame->fourcc); | 296 FOURCC_UYVY == CanonicalFourCC(captured_frame->fourcc); |
| 415 | 297 |
| 416 // If pixels are not square, optionally use vertical scaling to make them | 298 // If pixels are not square, optionally use vertical scaling to make them |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 adapted_width, adapted_height)); | 406 adapted_width, adapted_height)); |
| 525 | 407 |
| 526 if (!adapted_frame) { | 408 if (!adapted_frame) { |
| 527 // TODO(fbarchard): LOG more information about captured frame attributes. | 409 // TODO(fbarchard): LOG more information about captured frame attributes. |
| 528 LOG(LS_ERROR) << "Couldn't convert to I420! " | 410 LOG(LS_ERROR) << "Couldn't convert to I420! " |
| 529 << "From " << ToString(captured_frame) << " To " | 411 << "From " << ToString(captured_frame) << " To " |
| 530 << cropped_width << " x " << cropped_height; | 412 << cropped_width << " x " << cropped_height; |
| 531 return; | 413 return; |
| 532 } | 414 } |
| 533 | 415 |
| 534 if (muted_) { | |
| 535 // TODO(pthatcher): Use frame_factory_->CreateBlackFrame() instead. | |
| 536 adapted_frame->SetToBlack(); | |
| 537 } | |
| 538 SignalVideoFrame(this, adapted_frame.get()); | 416 SignalVideoFrame(this, adapted_frame.get()); |
| 539 UpdateStats(captured_frame); | 417 UpdateStats(captured_frame); |
| 540 } | 418 } |
| 541 | 419 |
| 542 void VideoCapturer::OnFrame(VideoCapturer* capturer, const VideoFrame* frame) { | 420 void VideoCapturer::OnFrame(VideoCapturer* capturer, const VideoFrame* frame) { |
| 543 broadcaster_.OnFrame(*frame); | 421 broadcaster_.OnFrame(*frame); |
| 544 } | 422 } |
| 545 | 423 |
| 546 void VideoCapturer::SetCaptureState(CaptureState state) { | 424 void VideoCapturer::SetCaptureState(CaptureState state) { |
| 425 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 547 if (state == capture_state_) { | 426 if (state == capture_state_) { |
| 548 // Don't trigger a state changed callback if the state hasn't changed. | 427 // Don't trigger a state changed callback if the state hasn't changed. |
| 549 return; | 428 return; |
| 550 } | 429 } |
| 551 StateChangeParams* state_params = new StateChangeParams(state); | |
| 552 capture_state_ = state; | 430 capture_state_ = state; |
| 553 thread_->Post(this, MSG_STATE_CHANGE, state_params); | 431 SignalStateChange(this, capture_state_); |
| 554 } | |
| 555 | |
| 556 void VideoCapturer::OnMessage(rtc::Message* message) { | |
| 557 switch (message->message_id) { | |
| 558 case MSG_STATE_CHANGE: { | |
| 559 std::unique_ptr<StateChangeParams> p( | |
| 560 static_cast<StateChangeParams*>(message->pdata)); | |
| 561 SignalStateChange(this, p->data()); | |
| 562 break; | |
| 563 } | |
| 564 case MSG_DO_PAUSE: { | |
| 565 Pause(true); | |
| 566 break; | |
| 567 } | |
| 568 case MSG_DO_UNPAUSE: { | |
| 569 Pause(false); | |
| 570 break; | |
| 571 } | |
| 572 default: { | |
| 573 ASSERT(false); | |
| 574 } | |
| 575 } | |
| 576 } | 432 } |
| 577 | 433 |
| 578 // Get the distance between the supported and desired formats. | 434 // Get the distance between the supported and desired formats. |
| 579 // Prioritization is done according to this algorithm: | 435 // Prioritization is done according to this algorithm: |
| 580 // 1) Width closeness. If not same, we prefer wider. | 436 // 1) Width closeness. If not same, we prefer wider. |
| 581 // 2) Height closeness. If not same, we prefer higher. | 437 // 2) Height closeness. If not same, we prefer higher. |
| 582 // 3) Framerate closeness. If not same, we prefer faster. | 438 // 3) Framerate closeness. If not same, we prefer faster. |
| 583 // 4) Compression. If desired format has a specific fourcc, we need exact match; | 439 // 4) Compression. If desired format has a specific fourcc, we need exact match; |
| 584 // otherwise, we use preference. | 440 // otherwise, we use preference. |
| 585 int64_t VideoCapturer::GetFormatDistance(const VideoFormat& desired, | 441 int64_t VideoCapturer::GetFormatDistance(const VideoFormat& desired, |
| 586 const VideoFormat& supported) { | 442 const VideoFormat& supported) { |
| 443 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 587 int64_t distance = kMaxDistance; | 444 int64_t distance = kMaxDistance; |
| 588 | 445 |
| 589 // Check fourcc. | 446 // Check fourcc. |
| 590 uint32_t supported_fourcc = CanonicalFourCC(supported.fourcc); | 447 uint32_t supported_fourcc = CanonicalFourCC(supported.fourcc); |
| 591 int64_t delta_fourcc = kMaxDistance; | 448 int64_t delta_fourcc = kMaxDistance; |
| 592 if (FOURCC_ANY == desired.fourcc) { | 449 if (FOURCC_ANY == desired.fourcc) { |
| 593 // Any fourcc is OK for the desired. Use preference to find best fourcc. | 450 // Any fourcc is OK for the desired. Use preference to find best fourcc. |
| 594 std::vector<uint32_t> preferred_fourccs; | 451 std::vector<uint32_t> preferred_fourccs; |
| 595 if (!GetPreferredFourccs(&preferred_fourccs)) { | 452 if (!GetPreferredFourccs(&preferred_fourccs)) { |
| 596 return distance; | 453 return distance; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 } | 544 } |
| 688 if (filtered_supported_formats_.empty()) { | 545 if (filtered_supported_formats_.empty()) { |
| 689 // The device only captures at resolutions higher than |max_format_| this | 546 // The device only captures at resolutions higher than |max_format_| this |
| 690 // indicates that |max_format_| should be ignored as it is better to capture | 547 // indicates that |max_format_| should be ignored as it is better to capture |
| 691 // at too high a resolution than to not capture at all. | 548 // at too high a resolution than to not capture at all. |
| 692 filtered_supported_formats_ = supported_formats_; | 549 filtered_supported_formats_ = supported_formats_; |
| 693 } | 550 } |
| 694 } | 551 } |
| 695 | 552 |
| 696 bool VideoCapturer::ShouldFilterFormat(const VideoFormat& format) const { | 553 bool VideoCapturer::ShouldFilterFormat(const VideoFormat& format) const { |
| 554 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 697 if (!enable_camera_list_) { | 555 if (!enable_camera_list_) { |
| 698 return false; | 556 return false; |
| 699 } | 557 } |
| 700 return format.width > max_format_->width || | 558 return format.width > max_format_->width || |
| 701 format.height > max_format_->height; | 559 format.height > max_format_->height; |
| 702 } | 560 } |
| 703 | 561 |
| 704 void VideoCapturer::UpdateStats(const CapturedFrame* captured_frame) { | 562 void VideoCapturer::UpdateStats(const CapturedFrame* captured_frame) { |
| 705 // Update stats protected from fetches from different thread. | 563 // Update stats protected from fetches from different thread. |
| 706 rtc::CritScope cs(&frame_stats_crit_); | 564 rtc::CritScope cs(&frame_stats_crit_); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 724 void VideoCapturer::GetVariableSnapshot( | 582 void VideoCapturer::GetVariableSnapshot( |
| 725 const rtc::RollingAccumulator<T>& data, | 583 const rtc::RollingAccumulator<T>& data, |
| 726 VariableInfo<T>* stats) { | 584 VariableInfo<T>* stats) { |
| 727 stats->max_val = data.ComputeMax(); | 585 stats->max_val = data.ComputeMax(); |
| 728 stats->mean = data.ComputeMean(); | 586 stats->mean = data.ComputeMean(); |
| 729 stats->min_val = data.ComputeMin(); | 587 stats->min_val = data.ComputeMin(); |
| 730 stats->variance = data.ComputeVariance(); | 588 stats->variance = data.ComputeVariance(); |
| 731 } | 589 } |
| 732 | 590 |
| 733 } // namespace cricket | 591 } // namespace cricket |
| OLD | NEW |