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 */ |
11 | 11 |
12 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_encoder.h" | 12 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_encoder.h" |
13 | 13 |
14 #if defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) | 14 #if defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
15 | 15 |
16 #include <memory> | 16 #include <memory> |
17 #include <string> | 17 #include <string> |
18 #include <vector> | 18 #include <vector> |
19 | 19 |
20 #include "libyuv/convert_from.h" | 20 #include "libyuv/convert_from.h" |
21 #include "webrtc/base/checks.h" | 21 #include "webrtc/base/checks.h" |
22 #include "webrtc/base/logging.h" | 22 #include "webrtc/base/logging.h" |
| 23 #if defined(WEBRTC_IOS) |
| 24 #include "webrtc/base/objc/RTCUIApplication.h" |
| 25 #endif |
| 26 #include "webrtc/base/scoped_ptr.h" |
23 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.h" | 27 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.h" |
24 #include "webrtc/system_wrappers/include/clock.h" | 28 #include "webrtc/system_wrappers/include/clock.h" |
25 | 29 |
26 namespace internal { | 30 namespace internal { |
27 | 31 |
28 // Convenience function for creating a dictionary. | 32 // Convenience function for creating a dictionary. |
29 inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys, | 33 inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys, |
30 CFTypeRef* values, | 34 CFTypeRef* values, |
31 size_t size) { | 35 size_t size) { |
32 return CFDictionaryCreate(kCFAllocatorDefault, keys, values, size, | 36 return CFDictionaryCreate(kCFAllocatorDefault, keys, values, size, |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 if (input_image.IsZeroSize()) { | 235 if (input_image.IsZeroSize()) { |
232 // It's possible to get zero sizes as a signal to produce keyframes (this | 236 // It's possible to get zero sizes as a signal to produce keyframes (this |
233 // happens for internal sources). But this shouldn't happen in | 237 // happens for internal sources). But this shouldn't happen in |
234 // webrtcvideoengine2. | 238 // webrtcvideoengine2. |
235 RTC_NOTREACHED(); | 239 RTC_NOTREACHED(); |
236 return WEBRTC_VIDEO_CODEC_OK; | 240 return WEBRTC_VIDEO_CODEC_OK; |
237 } | 241 } |
238 if (!callback_ || !compression_session_) { | 242 if (!callback_ || !compression_session_) { |
239 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 243 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
240 } | 244 } |
241 | 245 #if defined(WEBRTC_IOS) |
| 246 bool is_app_active = RTCIsUIApplicationActive(); |
| 247 if (!is_app_active) { |
| 248 // Ignore all encode requests when app isn't active. In this state, the |
| 249 // hardware encoder has been invalidated by the OS. |
| 250 return WEBRTC_VIDEO_CODEC_OK; |
| 251 } |
| 252 #endif |
242 // Get a pixel buffer from the pool and copy frame data over. | 253 // Get a pixel buffer from the pool and copy frame data over. |
243 CVPixelBufferPoolRef pixel_buffer_pool = | 254 CVPixelBufferPoolRef pixel_buffer_pool = |
244 VTCompressionSessionGetPixelBufferPool(compression_session_); | 255 VTCompressionSessionGetPixelBufferPool(compression_session_); |
| 256 if (!pixel_buffer_pool) { |
| 257 // Kind of a hack. On backgrounding, the compression session seems to get |
| 258 // invalidated, which causes this pool call to fail when the application |
| 259 // is foregrounded and frames are being sent for encoding again. |
| 260 // Resetting the session when this happens fixes the issue. |
| 261 ResetCompressionSession(); |
| 262 pixel_buffer_pool = |
| 263 VTCompressionSessionGetPixelBufferPool(compression_session_); |
| 264 } |
| 265 if (!pixel_buffer_pool) { |
| 266 LOG(LS_ERROR) << "Failed to get pixel buffer pool."; |
| 267 return WEBRTC_VIDEO_CODEC_ERROR; |
| 268 } |
245 CVPixelBufferRef pixel_buffer = nullptr; | 269 CVPixelBufferRef pixel_buffer = nullptr; |
246 CVReturn ret = CVPixelBufferPoolCreatePixelBuffer(nullptr, pixel_buffer_pool, | 270 CVReturn ret = CVPixelBufferPoolCreatePixelBuffer(nullptr, pixel_buffer_pool, |
247 &pixel_buffer); | 271 &pixel_buffer); |
248 if (ret != kCVReturnSuccess) { | 272 if (ret != kCVReturnSuccess) { |
249 LOG(LS_ERROR) << "Failed to create pixel buffer: " << ret; | 273 LOG(LS_ERROR) << "Failed to create pixel buffer: " << ret; |
250 // We probably want to drop frames here, since failure probably means | 274 // We probably want to drop frames here, since failure probably means |
251 // that the pool is empty. | 275 // that the pool is empty. |
252 return WEBRTC_VIDEO_CODEC_ERROR; | 276 return WEBRTC_VIDEO_CODEC_ERROR; |
253 } | 277 } |
254 RTC_DCHECK(pixel_buffer); | 278 RTC_DCHECK(pixel_buffer); |
(...skipping 23 matching lines...) Expand all Loading... |
278 frame_properties = internal::CreateCFDictionary(keys, values, 1); | 302 frame_properties = internal::CreateCFDictionary(keys, values, 1); |
279 } | 303 } |
280 std::unique_ptr<internal::FrameEncodeParams> encode_params; | 304 std::unique_ptr<internal::FrameEncodeParams> encode_params; |
281 encode_params.reset(new internal::FrameEncodeParams( | 305 encode_params.reset(new internal::FrameEncodeParams( |
282 this, codec_specific_info, width_, height_, input_image.render_time_ms(), | 306 this, codec_specific_info, width_, height_, input_image.render_time_ms(), |
283 input_image.timestamp())); | 307 input_image.timestamp())); |
284 | 308 |
285 // Update the bitrate if needed. | 309 // Update the bitrate if needed. |
286 SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps()); | 310 SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps()); |
287 | 311 |
288 VTCompressionSessionEncodeFrame( | 312 OSStatus status = VTCompressionSessionEncodeFrame( |
289 compression_session_, pixel_buffer, presentation_time_stamp, | 313 compression_session_, pixel_buffer, presentation_time_stamp, |
290 kCMTimeInvalid, frame_properties, encode_params.release(), nullptr); | 314 kCMTimeInvalid, frame_properties, encode_params.release(), nullptr); |
291 if (frame_properties) { | 315 if (frame_properties) { |
292 CFRelease(frame_properties); | 316 CFRelease(frame_properties); |
293 } | 317 } |
294 if (pixel_buffer) { | 318 if (pixel_buffer) { |
295 CVBufferRelease(pixel_buffer); | 319 CVBufferRelease(pixel_buffer); |
296 } | 320 } |
| 321 if (status != noErr) { |
| 322 LOG(LS_ERROR) << "Failed to encode frame with code: " << status; |
| 323 return WEBRTC_VIDEO_CODEC_ERROR; |
| 324 } |
297 return WEBRTC_VIDEO_CODEC_OK; | 325 return WEBRTC_VIDEO_CODEC_OK; |
298 } | 326 } |
299 | 327 |
300 int H264VideoToolboxEncoder::RegisterEncodeCompleteCallback( | 328 int H264VideoToolboxEncoder::RegisterEncodeCompleteCallback( |
301 EncodedImageCallback* callback) { | 329 EncodedImageCallback* callback) { |
302 callback_ = callback; | 330 callback_ = callback; |
303 return WEBRTC_VIDEO_CODEC_OK; | 331 return WEBRTC_VIDEO_CODEC_OK; |
304 } | 332 } |
305 | 333 |
306 int H264VideoToolboxEncoder::SetChannelParameters(uint32_t packet_loss, | 334 int H264VideoToolboxEncoder::SetChannelParameters(uint32_t packet_loss, |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 if (result != 0) { | 514 if (result != 0) { |
487 LOG(LS_ERROR) << "Encode callback failed: " << result; | 515 LOG(LS_ERROR) << "Encode callback failed: " << result; |
488 return; | 516 return; |
489 } | 517 } |
490 bitrate_adjuster_.Update(frame._size); | 518 bitrate_adjuster_.Update(frame._size); |
491 } | 519 } |
492 | 520 |
493 } // namespace webrtc | 521 } // namespace webrtc |
494 | 522 |
495 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) | 523 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
OLD | NEW |