OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 |
(...skipping 14 matching lines...) Expand all Loading... | |
25 namespace webrtc { | 25 namespace webrtc { |
26 namespace videocapturemodule { | 26 namespace videocapturemodule { |
27 rtc::scoped_refptr<VideoCaptureModule> VideoCaptureImpl::Create( | 27 rtc::scoped_refptr<VideoCaptureModule> VideoCaptureImpl::Create( |
28 VideoCaptureExternal*& externalCapture) { | 28 VideoCaptureExternal*& externalCapture) { |
29 rtc::scoped_refptr<VideoCaptureImpl> implementation( | 29 rtc::scoped_refptr<VideoCaptureImpl> implementation( |
30 new rtc::RefCountedObject<VideoCaptureImpl>()); | 30 new rtc::RefCountedObject<VideoCaptureImpl>()); |
31 externalCapture = implementation.get(); | 31 externalCapture = implementation.get(); |
32 return implementation; | 32 return implementation; |
33 } | 33 } |
34 | 34 |
35 const char* VideoCaptureImpl::CurrentDeviceName() const | 35 const char* VideoCaptureImpl::CurrentDeviceName() const { |
36 { | 36 return _deviceUniqueId; |
37 return _deviceUniqueId; | |
38 } | 37 } |
39 | 38 |
40 // static | 39 // static |
41 int32_t VideoCaptureImpl::RotationFromDegrees(int degrees, | 40 int32_t VideoCaptureImpl::RotationFromDegrees(int degrees, |
42 VideoRotation* rotation) { | 41 VideoRotation* rotation) { |
43 switch (degrees) { | 42 switch (degrees) { |
44 case 0: | 43 case 0: |
45 *rotation = kVideoRotation_0; | 44 *rotation = kVideoRotation_0; |
46 return 0; | 45 return 0; |
47 case 90: | 46 case 90: |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 { | 128 { |
130 rtc::CritScope cs(&_apiCs); | 129 rtc::CritScope cs(&_apiCs); |
131 | 130 |
132 const int32_t width = frameInfo.width; | 131 const int32_t width = frameInfo.width; |
133 const int32_t height = frameInfo.height; | 132 const int32_t height = frameInfo.height; |
134 | 133 |
135 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime); | 134 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime); |
136 | 135 |
137 // Not encoded, convert to I420. | 136 // Not encoded, convert to I420. |
138 const VideoType commonVideoType = | 137 const VideoType commonVideoType = |
139 RawVideoTypeToCommonVideoVideoType(frameInfo.rawType); | 138 RawVideoTypeToCommonVideoVideoType(frameInfo.rawType); |
140 | 139 |
141 if (frameInfo.rawType != kVideoMJPEG && | 140 if (frameInfo.rawType != kVideoMJPEG && |
142 CalcBufferSize(commonVideoType, width, | 141 CalcBufferSize(commonVideoType, width, abs(height)) != |
143 abs(height)) != videoFrameLength) | 142 videoFrameLength) { |
144 { | 143 LOG(LS_ERROR) << "Wrong incoming frame length."; |
145 LOG(LS_ERROR) << "Wrong incoming frame length."; | 144 return -1; |
146 return -1; | |
147 } | 145 } |
148 | 146 |
149 int stride_y = width; | 147 int stride_y = width; |
150 int stride_uv = (width + 1) / 2; | 148 int stride_uv = (width + 1) / 2; |
151 int target_width = width; | 149 int target_width = width; |
152 int target_height = height; | 150 int target_height = height; |
153 | 151 |
154 // SetApplyRotation doesn't take any lock. Make a local copy here. | 152 // SetApplyRotation doesn't take any lock. Make a local copy here. |
155 bool apply_rotation = apply_rotation_; | 153 bool apply_rotation = apply_rotation_; |
156 | 154 |
(...skipping 10 matching lines...) Expand all Loading... | |
167 // In Windows, the image starts bottom left, instead of top left. | 165 // In Windows, the image starts bottom left, instead of top left. |
168 // Setting a negative source height, inverts the image (within LibYuv). | 166 // Setting a negative source height, inverts the image (within LibYuv). |
169 | 167 |
170 // TODO(nisse): Use a pool? | 168 // TODO(nisse): Use a pool? |
171 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create( | 169 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create( |
172 target_width, abs(target_height), stride_y, stride_uv, stride_uv); | 170 target_width, abs(target_height), stride_y, stride_uv, stride_uv); |
173 const int conversionResult = ConvertToI420( | 171 const int conversionResult = ConvertToI420( |
174 commonVideoType, videoFrame, 0, 0, // No cropping | 172 commonVideoType, videoFrame, 0, 0, // No cropping |
175 width, height, videoFrameLength, | 173 width, height, videoFrameLength, |
176 apply_rotation ? _rotateFrame : kVideoRotation_0, buffer.get()); | 174 apply_rotation ? _rotateFrame : kVideoRotation_0, buffer.get()); |
177 if (conversionResult < 0) | 175 if (conversionResult < 0) { |
178 { | |
179 LOG(LS_ERROR) << "Failed to convert capture frame from type " | 176 LOG(LS_ERROR) << "Failed to convert capture frame from type " |
180 << frameInfo.rawType << "to I420."; | 177 << frameInfo.rawType << "to I420."; |
181 return -1; | 178 return -1; |
182 } | 179 } |
183 | 180 |
184 VideoFrame captureFrame( | 181 VideoFrame captureFrame(buffer, 0, rtc::TimeMillis(), |
185 buffer, 0, rtc::TimeMillis(), | 182 !apply_rotation ? _rotateFrame : kVideoRotation_0); |
186 !apply_rotation ? _rotateFrame : kVideoRotation_0); | |
187 captureFrame.set_ntp_time_ms(captureTime); | 183 captureFrame.set_ntp_time_ms(captureTime); |
188 | 184 |
189 DeliverCapturedFrame(captureFrame); | 185 DeliverCapturedFrame(captureFrame); |
190 | 186 |
191 return 0; | 187 return 0; |
192 } | 188 } |
193 | 189 |
194 int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) { | 190 int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) { |
195 rtc::CritScope cs(&_apiCs); | 191 rtc::CritScope cs(&_apiCs); |
196 _rotateFrame = rotation; | 192 _rotateFrame = rotation; |
197 return 0; | 193 return 0; |
198 } | 194 } |
199 | 195 |
200 bool VideoCaptureImpl::SetApplyRotation(bool enable) { | 196 bool VideoCaptureImpl::SetApplyRotation(bool enable) { |
201 // We can't take any lock here as it'll cause deadlock with IncomingFrame. | 197 // We can't take any lock here as it'll cause deadlock with IncomingFrame. |
202 | 198 |
203 // The effect of this is the last caller wins. | 199 // The effect of this is the last caller wins. |
204 apply_rotation_ = enable; | 200 apply_rotation_ = enable; |
205 return true; | 201 return true; |
206 } | 202 } |
207 | 203 |
208 void VideoCaptureImpl::UpdateFrameCount() | 204 void VideoCaptureImpl::UpdateFrameCount() { |
209 { | 205 if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0) { |
210 if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0) | 206 // first no shift |
211 { | 207 } else { |
nisse-webrtc
2017/04/06 09:23:22
In coming cl's, try to not include unrelated forma
ilnik
2017/04/06 10:04:49
Yes, sorry about that. There were some meaningful
| |
212 // first no shift | 208 // shift |
209 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--) { | |
210 _incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i]; | |
213 } | 211 } |
214 else | 212 } |
215 { | 213 _incomingFrameTimesNanos[0] = rtc::TimeNanos(); |
216 // shift | |
217 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--) | |
218 { | |
219 _incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i]; | |
220 } | |
221 } | |
222 _incomingFrameTimesNanos[0] = rtc::TimeNanos(); | |
223 } | 214 } |
224 | 215 |
225 uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns) | 216 uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns) { |
226 { | 217 int32_t num = 0; |
227 int32_t num = 0; | 218 int32_t nrOfFrames = 0; |
228 int32_t nrOfFrames = 0; | 219 for (num = 1; num < (kFrameRateCountHistorySize - 1); num++) { |
229 for (num = 1; num < (kFrameRateCountHistorySize - 1); num++) | 220 if (_incomingFrameTimesNanos[num] <= 0 || |
230 { | 221 (now_ns - _incomingFrameTimesNanos[num]) / |
231 if (_incomingFrameTimesNanos[num] <= 0 || | 222 rtc::kNumNanosecsPerMillisec > |
232 (now_ns - _incomingFrameTimesNanos[num]) / | 223 kFrameRateHistoryWindowMs) { // don't use data older than 2sec |
233 rtc::kNumNanosecsPerMillisec > | 224 break; |
234 kFrameRateHistoryWindowMs) // don't use data older than 2sec | 225 } else { |
235 { | 226 nrOfFrames++; |
236 break; | |
237 } | |
238 else | |
239 { | |
240 nrOfFrames++; | |
241 } | |
242 } | 227 } |
243 if (num > 1) | 228 } |
244 { | 229 if (num > 1) { |
245 int64_t diff = (now_ns - _incomingFrameTimesNanos[num - 1]) / | 230 int64_t diff = (now_ns - _incomingFrameTimesNanos[num - 1]) / |
246 rtc::kNumNanosecsPerMillisec; | 231 rtc::kNumNanosecsPerMillisec; |
247 if (diff > 0) | 232 if (diff > 0) { |
248 { | 233 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f); |
249 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f); | |
250 } | |
251 } | 234 } |
235 } | |
252 | 236 |
253 return nrOfFrames; | 237 return nrOfFrames; |
254 } | 238 } |
255 } // namespace videocapturemodule | 239 } // namespace videocapturemodule |
256 } // namespace webrtc | 240 } // namespace webrtc |
OLD | NEW |