OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 if (!callback_ || !compression_session_) { | 241 if (!callback_ || !compression_session_) { |
242 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 242 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
243 } | 243 } |
244 #if defined(WEBRTC_IOS) | 244 #if defined(WEBRTC_IOS) |
245 if (!RTCIsUIApplicationActive()) { | 245 if (!RTCIsUIApplicationActive()) { |
246 // Ignore all encode requests when app isn't active. In this state, the | 246 // Ignore all encode requests when app isn't active. In this state, the |
247 // hardware encoder has been invalidated by the OS. | 247 // hardware encoder has been invalidated by the OS. |
248 return WEBRTC_VIDEO_CODEC_OK; | 248 return WEBRTC_VIDEO_CODEC_OK; |
249 } | 249 } |
250 #endif | 250 #endif |
| 251 bool is_keyframe_required = false; |
251 // Get a pixel buffer from the pool and copy frame data over. | 252 // Get a pixel buffer from the pool and copy frame data over. |
252 CVPixelBufferPoolRef pixel_buffer_pool = | 253 CVPixelBufferPoolRef pixel_buffer_pool = |
253 VTCompressionSessionGetPixelBufferPool(compression_session_); | 254 VTCompressionSessionGetPixelBufferPool(compression_session_); |
254 #if defined(WEBRTC_IOS) | 255 #if defined(WEBRTC_IOS) |
255 if (!pixel_buffer_pool) { | 256 if (!pixel_buffer_pool) { |
256 // Kind of a hack. On backgrounding, the compression session seems to get | 257 // Kind of a hack. On backgrounding, the compression session seems to get |
257 // invalidated, which causes this pool call to fail when the application | 258 // invalidated, which causes this pool call to fail when the application |
258 // is foregrounded and frames are being sent for encoding again. | 259 // is foregrounded and frames are being sent for encoding again. |
259 // Resetting the session when this happens fixes the issue. | 260 // Resetting the session when this happens fixes the issue. |
| 261 // In addition we request a keyframe so video can recover quickly. |
260 ResetCompressionSession(); | 262 ResetCompressionSession(); |
261 pixel_buffer_pool = | 263 pixel_buffer_pool = |
262 VTCompressionSessionGetPixelBufferPool(compression_session_); | 264 VTCompressionSessionGetPixelBufferPool(compression_session_); |
| 265 is_keyframe_required = true; |
263 } | 266 } |
264 #endif | 267 #endif |
265 if (!pixel_buffer_pool) { | 268 if (!pixel_buffer_pool) { |
266 LOG(LS_ERROR) << "Failed to get pixel buffer pool."; | 269 LOG(LS_ERROR) << "Failed to get pixel buffer pool."; |
267 return WEBRTC_VIDEO_CODEC_ERROR; | 270 return WEBRTC_VIDEO_CODEC_ERROR; |
268 } | 271 } |
269 CVPixelBufferRef pixel_buffer = nullptr; | 272 CVPixelBufferRef pixel_buffer = nullptr; |
270 CVReturn ret = CVPixelBufferPoolCreatePixelBuffer(nullptr, pixel_buffer_pool, | 273 CVReturn ret = CVPixelBufferPoolCreatePixelBuffer(nullptr, pixel_buffer_pool, |
271 &pixel_buffer); | 274 &pixel_buffer); |
272 if (ret != kCVReturnSuccess) { | 275 if (ret != kCVReturnSuccess) { |
273 LOG(LS_ERROR) << "Failed to create pixel buffer: " << ret; | 276 LOG(LS_ERROR) << "Failed to create pixel buffer: " << ret; |
274 // We probably want to drop frames here, since failure probably means | 277 // We probably want to drop frames here, since failure probably means |
275 // that the pool is empty. | 278 // that the pool is empty. |
276 return WEBRTC_VIDEO_CODEC_ERROR; | 279 return WEBRTC_VIDEO_CODEC_ERROR; |
277 } | 280 } |
278 RTC_DCHECK(pixel_buffer); | 281 RTC_DCHECK(pixel_buffer); |
279 if (!internal::CopyVideoFrameToPixelBuffer(input_image, pixel_buffer)) { | 282 if (!internal::CopyVideoFrameToPixelBuffer(input_image, pixel_buffer)) { |
280 LOG(LS_ERROR) << "Failed to copy frame data."; | 283 LOG(LS_ERROR) << "Failed to copy frame data."; |
281 CVBufferRelease(pixel_buffer); | 284 CVBufferRelease(pixel_buffer); |
282 return WEBRTC_VIDEO_CODEC_ERROR; | 285 return WEBRTC_VIDEO_CODEC_ERROR; |
283 } | 286 } |
284 | 287 |
285 // Check if we need a keyframe. | 288 // Check if we need a keyframe. |
286 bool is_keyframe_required = false; | 289 if (!is_keyframe_required && frame_types) { |
287 if (frame_types) { | |
288 for (auto frame_type : *frame_types) { | 290 for (auto frame_type : *frame_types) { |
289 if (frame_type == kVideoFrameKey) { | 291 if (frame_type == kVideoFrameKey) { |
290 is_keyframe_required = true; | 292 is_keyframe_required = true; |
291 break; | 293 break; |
292 } | 294 } |
293 } | 295 } |
294 } | 296 } |
295 | 297 |
296 CMTime presentation_time_stamp = | 298 CMTime presentation_time_stamp = |
297 CMTimeMake(input_image.render_time_ms(), 1000); | 299 CMTimeMake(input_image.render_time_ms(), 1000); |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 if (result != 0) { | 516 if (result != 0) { |
515 LOG(LS_ERROR) << "Encode callback failed: " << result; | 517 LOG(LS_ERROR) << "Encode callback failed: " << result; |
516 return; | 518 return; |
517 } | 519 } |
518 bitrate_adjuster_.Update(frame._size); | 520 bitrate_adjuster_.Update(frame._size); |
519 } | 521 } |
520 | 522 |
521 } // namespace webrtc | 523 } // namespace webrtc |
522 | 524 |
523 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) | 525 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
OLD | NEW |