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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 *crop_y = 0; | 194 *crop_y = 0; |
253 } | 195 } |
254 | 196 |
255 if (translated_camera_time_us) { | 197 if (translated_camera_time_us) { |
256 *translated_camera_time_us = timestamp_aligner_.ClipTimestamp( | 198 *translated_camera_time_us = timestamp_aligner_.ClipTimestamp( |
257 camera_time_us + offset_us, system_time_us); | 199 camera_time_us + offset_us, system_time_us); |
258 } | 200 } |
259 return true; | 201 return true; |
260 } | 202 } |
261 | 203 |
262 void VideoCapturer::OnFrameCaptured(VideoCapturer*, | |
263 const CapturedFrame* captured_frame) { | |
264 int out_width; | |
265 int out_height; | |
266 int crop_width; | |
267 int crop_height; | |
268 int crop_x; | |
269 int crop_y; | |
270 | |
271 // TODO(nisse): We don't do timestamp translation on this input | |
272 // path. It seems straight-forward to enable translation, but that | |
273 // breaks the WebRtcVideoEngine2Test.PropagatesInputFrameTimestamp | |
274 // test. Probably not worth the effort to fix, instead, try to | |
275 // delete or refactor all code using VideoFrameFactory and | |
276 // SignalCapturedFrame. | |
277 if (!AdaptFrame(captured_frame->width, captured_frame->height, | |
278 captured_frame->time_stamp / rtc::kNumNanosecsPerMicrosec, | |
279 0, | |
280 &out_width, &out_height, | |
281 &crop_width, &crop_height, &crop_x, &crop_y, nullptr)) { | |
282 return; | |
283 } | |
284 | |
285 if (!frame_factory_) { | |
286 LOG(LS_ERROR) << "No video frame factory."; | |
287 return; | |
288 } | |
289 | |
290 // TODO(nisse): Reorganize frame factory methods. crop_x and crop_y | |
291 // are ignored for now. | |
292 std::unique_ptr<VideoFrame> adapted_frame(frame_factory_->CreateAliasedFrame( | |
293 captured_frame, crop_width, crop_height, out_width, out_height)); | |
294 | |
295 if (!adapted_frame) { | |
296 // TODO(fbarchard): LOG more information about captured frame attributes. | |
297 LOG(LS_ERROR) << "Couldn't convert to I420! " | |
298 << "From " << ToString(captured_frame) << " To " | |
299 << out_width << " x " << out_height; | |
300 return; | |
301 } | |
302 | |
303 OnFrame(*adapted_frame, captured_frame->width, captured_frame->height); | |
304 } | |
305 | |
306 void VideoCapturer::OnFrame(const VideoFrame& frame, | 204 void VideoCapturer::OnFrame(const VideoFrame& frame, |
307 int orig_width, | 205 int orig_width, |
308 int orig_height) { | 206 int orig_height) { |
309 broadcaster_.OnFrame(frame); | 207 // For a child class which implements rotation itself, we should |
| 208 // always have apply_rotation_ == false or frame.rotation() == 0. |
| 209 // Except possibly during races where apply_rotation_ is changed |
| 210 // mid-stream. |
| 211 if (apply_rotation_) { |
| 212 broadcaster_.OnFrame(WebRtcVideoFrame( |
| 213 webrtc::I420Buffer::Rotate(frame.video_frame_buffer(), |
| 214 frame.rotation()), |
| 215 webrtc::kVideoRotation_0, frame.timestamp_us())); |
| 216 } else { |
| 217 broadcaster_.OnFrame(frame); |
| 218 } |
310 UpdateInputSize(orig_width, orig_height); | 219 UpdateInputSize(orig_width, orig_height); |
311 } | 220 } |
312 | 221 |
313 void VideoCapturer::SetCaptureState(CaptureState state) { | 222 void VideoCapturer::SetCaptureState(CaptureState state) { |
314 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 223 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
315 if (state == capture_state_) { | 224 if (state == capture_state_) { |
316 // Don't trigger a state changed callback if the state hasn't changed. | 225 // Don't trigger a state changed callback if the state hasn't changed. |
317 return; | 226 return; |
318 } | 227 } |
319 capture_state_ = state; | 228 capture_state_ = state; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 void VideoCapturer::UpdateInputSize(int width, int height) { | 360 void VideoCapturer::UpdateInputSize(int width, int height) { |
452 // Update stats protected from fetches from different thread. | 361 // Update stats protected from fetches from different thread. |
453 rtc::CritScope cs(&frame_stats_crit_); | 362 rtc::CritScope cs(&frame_stats_crit_); |
454 | 363 |
455 input_size_valid_ = true; | 364 input_size_valid_ = true; |
456 input_width_ = width; | 365 input_width_ = width; |
457 input_height_ = height; | 366 input_height_ = height; |
458 } | 367 } |
459 | 368 |
460 } // namespace cricket | 369 } // namespace cricket |
OLD | NEW |