| 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" | |
| 22 #include "webrtc/media/engine/webrtcvideoframe.h" | 21 #include "webrtc/media/engine/webrtcvideoframe.h" |
| 23 #include "webrtc/media/engine/webrtcvideoframefactory.h" | |
| 24 | 22 |
| 25 namespace cricket { | 23 namespace cricket { |
| 26 | 24 |
| 27 namespace { | 25 namespace { |
| 28 | 26 |
| 29 static const int64_t kMaxDistance = ~(static_cast<int64_t>(1) << 63); | 27 static const int64_t kMaxDistance = ~(static_cast<int64_t>(1) << 63); |
| 30 #ifdef WEBRTC_LINUX | 28 #ifdef WEBRTC_LINUX |
| 31 static const int kYU12Penalty = 16; // Needs to be higher than MJPG index. | 29 static const int kYU12Penalty = 16; // Needs to be higher than MJPG index. |
| 32 #endif | 30 #endif |
| 33 | 31 |
| 34 } // namespace | 32 } // namespace |
| 35 | 33 |
| 36 ///////////////////////////////////////////////////////////////////// | 34 ///////////////////////////////////////////////////////////////////// |
| 37 // Implementation of struct CapturedFrame | |
| 38 ///////////////////////////////////////////////////////////////////// | |
| 39 CapturedFrame::CapturedFrame() | |
| 40 : width(0), | |
| 41 height(0), | |
| 42 fourcc(0), | |
| 43 pixel_width(0), | |
| 44 pixel_height(0), | |
| 45 time_stamp(0), | |
| 46 data_size(0), | |
| 47 rotation(webrtc::kVideoRotation_0), | |
| 48 data(NULL) {} | |
| 49 | |
| 50 // TODO(fbarchard): Remove this function once lmimediaengine stops using it. | |
| 51 bool CapturedFrame::GetDataSize(uint32_t* size) const { | |
| 52 if (!size || data_size == CapturedFrame::kUnknownDataSize) { | |
| 53 return false; | |
| 54 } | |
| 55 *size = data_size; | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 ///////////////////////////////////////////////////////////////////// | |
| 60 // Implementation of class VideoCapturer | 35 // Implementation of class VideoCapturer |
| 61 ///////////////////////////////////////////////////////////////////// | 36 ///////////////////////////////////////////////////////////////////// |
| 62 VideoCapturer::VideoCapturer() : apply_rotation_(false) { | 37 VideoCapturer::VideoCapturer() : apply_rotation_(false) { |
| 63 thread_checker_.DetachFromThread(); | 38 thread_checker_.DetachFromThread(); |
| 64 Construct(); | 39 Construct(); |
| 65 } | 40 } |
| 66 | 41 |
| 67 void VideoCapturer::Construct() { | 42 void VideoCapturer::Construct() { |
| 68 enable_camera_list_ = false; | 43 enable_camera_list_ = false; |
| 69 capture_state_ = CS_STOPPED; | 44 capture_state_ = CS_STOPPED; |
| 70 SignalFrameCaptured.connect(this, &VideoCapturer::OnFrameCaptured); | |
| 71 scaled_width_ = 0; | 45 scaled_width_ = 0; |
| 72 scaled_height_ = 0; | 46 scaled_height_ = 0; |
| 73 enable_video_adapter_ = true; | 47 enable_video_adapter_ = true; |
| 74 // There are lots of video capturers out there that don't call | |
| 75 // set_frame_factory. We can either go change all of them, or we | |
| 76 // can set this default. | |
| 77 // TODO(pthatcher): Remove this hack and require the frame factory | |
| 78 // to be passed in the constructor. | |
| 79 set_frame_factory(new WebRtcVideoFrameFactory()); | |
| 80 } | 48 } |
| 81 | 49 |
| 82 const std::vector<VideoFormat>* VideoCapturer::GetSupportedFormats() const { | 50 const std::vector<VideoFormat>* VideoCapturer::GetSupportedFormats() const { |
| 83 return &filtered_supported_formats_; | 51 return &filtered_supported_formats_; |
| 84 } | 52 } |
| 85 | 53 |
| 86 bool VideoCapturer::StartCapturing(const VideoFormat& capture_format) { | 54 bool VideoCapturer::StartCapturing(const VideoFormat& capture_format) { |
| 87 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 55 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 88 CaptureState result = Start(capture_format); | 56 CaptureState result = Start(capture_format); |
| 89 const bool success = (result == CS_RUNNING) || (result == CS_STARTING); | 57 const bool success = (result == CS_RUNNING) || (result == CS_STARTING); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 return true; | 113 return true; |
| 146 } | 114 } |
| 147 | 115 |
| 148 void VideoCapturer::ConstrainSupportedFormats(const VideoFormat& max_format) { | 116 void VideoCapturer::ConstrainSupportedFormats(const VideoFormat& max_format) { |
| 149 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 117 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 150 max_format_.reset(new VideoFormat(max_format)); | 118 max_format_.reset(new VideoFormat(max_format)); |
| 151 LOG(LS_VERBOSE) << " ConstrainSupportedFormats " << max_format.ToString(); | 119 LOG(LS_VERBOSE) << " ConstrainSupportedFormats " << max_format.ToString(); |
| 152 UpdateFilteredSupportedFormats(); | 120 UpdateFilteredSupportedFormats(); |
| 153 } | 121 } |
| 154 | 122 |
| 155 std::string VideoCapturer::ToString(const CapturedFrame* captured_frame) const { | |
| 156 std::string fourcc_name = GetFourccName(captured_frame->fourcc) + " "; | |
| 157 for (std::string::const_iterator i = fourcc_name.begin(); | |
| 158 i < fourcc_name.end(); ++i) { | |
| 159 // Test character is printable; Avoid isprint() which asserts on negatives. | |
| 160 if (*i < 32 || *i >= 127) { | |
| 161 fourcc_name = ""; | |
| 162 break; | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 std::ostringstream ss; | |
| 167 ss << fourcc_name << captured_frame->width << "x" << captured_frame->height; | |
| 168 return ss.str(); | |
| 169 } | |
| 170 | |
| 171 void VideoCapturer::set_frame_factory(VideoFrameFactory* frame_factory) { | |
| 172 frame_factory_.reset(frame_factory); | |
| 173 if (frame_factory) { | |
| 174 frame_factory->SetApplyRotation(apply_rotation_); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 bool VideoCapturer::GetInputSize(int* width, int* height) { | 123 bool VideoCapturer::GetInputSize(int* width, int* height) { |
| 179 rtc::CritScope cs(&frame_stats_crit_); | 124 rtc::CritScope cs(&frame_stats_crit_); |
| 180 if (!input_size_valid_) { | 125 if (!input_size_valid_) { |
| 181 return false; | 126 return false; |
| 182 } | 127 } |
| 183 *width = input_width_; | 128 *width = input_width_; |
| 184 *height = input_height_; | 129 *height = input_height_; |
| 185 | 130 |
| 186 return true; | 131 return true; |
| 187 } | 132 } |
| 188 | 133 |
| 189 void VideoCapturer::RemoveSink( | 134 void VideoCapturer::RemoveSink( |
| 190 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) { | 135 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) { |
| 191 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 136 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 192 broadcaster_.RemoveSink(sink); | 137 broadcaster_.RemoveSink(sink); |
| 193 OnSinkWantsChanged(broadcaster_.wants()); | 138 OnSinkWantsChanged(broadcaster_.wants()); |
| 194 } | 139 } |
| 195 | 140 |
| 196 void VideoCapturer::AddOrUpdateSink( | 141 void VideoCapturer::AddOrUpdateSink( |
| 197 rtc::VideoSinkInterface<cricket::VideoFrame>* sink, | 142 rtc::VideoSinkInterface<cricket::VideoFrame>* sink, |
| 198 const rtc::VideoSinkWants& wants) { | 143 const rtc::VideoSinkWants& wants) { |
| 199 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 144 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 200 broadcaster_.AddOrUpdateSink(sink, wants); | 145 broadcaster_.AddOrUpdateSink(sink, wants); |
| 201 OnSinkWantsChanged(broadcaster_.wants()); | 146 OnSinkWantsChanged(broadcaster_.wants()); |
| 202 } | 147 } |
| 203 | 148 |
| 204 void VideoCapturer::OnSinkWantsChanged(const rtc::VideoSinkWants& wants) { | 149 void VideoCapturer::OnSinkWantsChanged(const rtc::VideoSinkWants& wants) { |
| 205 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 150 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 206 apply_rotation_ = wants.rotation_applied; | 151 apply_rotation_ = wants.rotation_applied; |
| 207 if (frame_factory_) { | |
| 208 frame_factory_->SetApplyRotation(apply_rotation_); | |
| 209 } | |
| 210 | 152 |
| 211 if (video_adapter()) { | 153 if (video_adapter()) { |
| 212 video_adapter()->OnResolutionRequest(wants.max_pixel_count, | 154 video_adapter()->OnResolutionRequest(wants.max_pixel_count, |
| 213 wants.max_pixel_count_step_up); | 155 wants.max_pixel_count_step_up); |
| 214 } | 156 } |
| 215 } | 157 } |
| 216 | 158 |
| 217 bool VideoCapturer::AdaptFrame(int width, | 159 bool VideoCapturer::AdaptFrame(int width, |
| 218 int height, | 160 int height, |
| 219 int64_t camera_time_us, | 161 int64_t camera_time_us, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 247 *out_height = height; | 189 *out_height = height; |
| 248 *crop_width = width; | 190 *crop_width = width; |
| 249 *crop_height = height; | 191 *crop_height = height; |
| 250 *crop_x = 0; | 192 *crop_x = 0; |
| 251 *crop_y = 0; | 193 *crop_y = 0; |
| 252 } | 194 } |
| 253 | 195 |
| 254 return true; | 196 return true; |
| 255 } | 197 } |
| 256 | 198 |
| 257 void VideoCapturer::OnFrameCaptured(VideoCapturer*, | |
| 258 const CapturedFrame* captured_frame) { | |
| 259 int out_width; | |
| 260 int out_height; | |
| 261 int crop_width; | |
| 262 int crop_height; | |
| 263 int crop_x; | |
| 264 int crop_y; | |
| 265 | |
| 266 // TODO(nisse): We don't do timestamp translation on this input | |
| 267 // path. It seems straight-forward to enable translation, but that | |
| 268 // breaks the WebRtcVideoEngine2Test.PropagatesInputFrameTimestamp | |
| 269 // test. Probably not worth the effort to fix, instead, try to | |
| 270 // delete or refactor all code using VideoFrameFactory and | |
| 271 // SignalCapturedFrame. | |
| 272 if (!AdaptFrame(captured_frame->width, captured_frame->height, | |
| 273 captured_frame->time_stamp / rtc::kNumNanosecsPerMicrosec, | |
| 274 0, | |
| 275 &out_width, &out_height, | |
| 276 &crop_width, &crop_height, &crop_x, &crop_y, nullptr)) { | |
| 277 return; | |
| 278 } | |
| 279 | |
| 280 if (!frame_factory_) { | |
| 281 LOG(LS_ERROR) << "No video frame factory."; | |
| 282 return; | |
| 283 } | |
| 284 | |
| 285 // TODO(nisse): Reorganize frame factory methods. crop_x and crop_y | |
| 286 // are ignored for now. | |
| 287 std::unique_ptr<VideoFrame> adapted_frame(frame_factory_->CreateAliasedFrame( | |
| 288 captured_frame, crop_width, crop_height, out_width, out_height)); | |
| 289 | |
| 290 if (!adapted_frame) { | |
| 291 // TODO(fbarchard): LOG more information about captured frame attributes. | |
| 292 LOG(LS_ERROR) << "Couldn't convert to I420! " | |
| 293 << "From " << ToString(captured_frame) << " To " | |
| 294 << out_width << " x " << out_height; | |
| 295 return; | |
| 296 } | |
| 297 | |
| 298 OnFrame(*adapted_frame, captured_frame->width, captured_frame->height); | |
| 299 } | |
| 300 | |
| 301 void VideoCapturer::OnFrame(const VideoFrame& frame, | 199 void VideoCapturer::OnFrame(const VideoFrame& frame, |
| 302 int orig_width, | 200 int orig_width, |
| 303 int orig_height) { | 201 int orig_height) { |
| 304 broadcaster_.OnFrame(frame); | 202 // For a child class which implements rotation itself, we should |
| 203 // always have apply_rotation_ == false or frame.rotation() == 0. |
| 204 // Except possibly during races where apply_rotation_ is changed |
| 205 // mid-stream. |
| 206 if (apply_rotation_ && frame.rotation() != webrtc::kVideoRotation_0) { |
| 207 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer( |
| 208 frame.video_frame_buffer()); |
| 209 if (buffer->native_handle()) { |
| 210 // Sources producing native frames must handle apply_rotation |
| 211 // themselves. But even if they do, we may occasionally end up |
| 212 // in this case, for frames in flight at the time |
| 213 // applied_rotation is set to true. In that case, we just drop |
| 214 // the frame. |
| 215 LOG(LS_WARNING) << "Native frame requiring rotation. Discarding."; |
| 216 return; |
| 217 } |
| 218 broadcaster_.OnFrame(WebRtcVideoFrame( |
| 219 webrtc::I420Buffer::Rotate(buffer, frame.rotation()), |
| 220 webrtc::kVideoRotation_0, frame.timestamp_us())); |
| 221 } else { |
| 222 broadcaster_.OnFrame(frame); |
| 223 } |
| 305 UpdateInputSize(orig_width, orig_height); | 224 UpdateInputSize(orig_width, orig_height); |
| 306 } | 225 } |
| 307 | 226 |
| 308 void VideoCapturer::SetCaptureState(CaptureState state) { | 227 void VideoCapturer::SetCaptureState(CaptureState state) { |
| 309 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 228 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 310 if (state == capture_state_) { | 229 if (state == capture_state_) { |
| 311 // Don't trigger a state changed callback if the state hasn't changed. | 230 // Don't trigger a state changed callback if the state hasn't changed. |
| 312 return; | 231 return; |
| 313 } | 232 } |
| 314 capture_state_ = state; | 233 capture_state_ = state; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 void VideoCapturer::UpdateInputSize(int width, int height) { | 365 void VideoCapturer::UpdateInputSize(int width, int height) { |
| 447 // Update stats protected from fetches from different thread. | 366 // Update stats protected from fetches from different thread. |
| 448 rtc::CritScope cs(&frame_stats_crit_); | 367 rtc::CritScope cs(&frame_stats_crit_); |
| 449 | 368 |
| 450 input_size_valid_ = true; | 369 input_size_valid_ = true; |
| 451 input_width_ = width; | 370 input_width_ = width; |
| 452 input_height_ = height; | 371 input_height_ = height; |
| 453 } | 372 } |
| 454 | 373 |
| 455 } // namespace cricket | 374 } // namespace cricket |
| OLD | NEW |