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