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 if (!RTCIsUIApplicationActive()) { | |
246 // Ignore all encode requests when app isn't active. In this state, the | |
247 // hardware encoder has been invalidated by the OS. | |
248 return WEBRTC_VIDEO_CODEC_OK; | |
249 } | |
250 #endif | |
242 // Get a pixel buffer from the pool and copy frame data over. | 251 // Get a pixel buffer from the pool and copy frame data over. |
243 CVPixelBufferPoolRef pixel_buffer_pool = | 252 CVPixelBufferPoolRef pixel_buffer_pool = |
244 VTCompressionSessionGetPixelBufferPool(compression_session_); | 253 VTCompressionSessionGetPixelBufferPool(compression_session_); |
254 if (!pixel_buffer_pool) { | |
255 // Kind of a hack. On backgrounding, the compression session seems to get | |
256 // invalidated, which causes this pool call to fail when the application | |
257 // is foregrounded and frames are being sent for encoding again. | |
258 // Resetting the session when this happens fixes the issue. | |
stefan-webrtc
2016/03/07 15:00:02
Based on this comment it sounds like this should b
tkchin_webrtc
2016/03/08 04:51:41
Done.
| |
259 ResetCompressionSession(); | |
260 pixel_buffer_pool = | |
261 VTCompressionSessionGetPixelBufferPool(compression_session_); | |
262 } | |
263 if (!pixel_buffer_pool) { | |
264 LOG(LS_ERROR) << "Failed to get pixel buffer pool."; | |
265 return WEBRTC_VIDEO_CODEC_ERROR; | |
266 } | |
245 CVPixelBufferRef pixel_buffer = nullptr; | 267 CVPixelBufferRef pixel_buffer = nullptr; |
246 CVReturn ret = CVPixelBufferPoolCreatePixelBuffer(nullptr, pixel_buffer_pool, | 268 CVReturn ret = CVPixelBufferPoolCreatePixelBuffer(nullptr, pixel_buffer_pool, |
247 &pixel_buffer); | 269 &pixel_buffer); |
248 if (ret != kCVReturnSuccess) { | 270 if (ret != kCVReturnSuccess) { |
249 LOG(LS_ERROR) << "Failed to create pixel buffer: " << ret; | 271 LOG(LS_ERROR) << "Failed to create pixel buffer: " << ret; |
250 // We probably want to drop frames here, since failure probably means | 272 // We probably want to drop frames here, since failure probably means |
251 // that the pool is empty. | 273 // that the pool is empty. |
252 return WEBRTC_VIDEO_CODEC_ERROR; | 274 return WEBRTC_VIDEO_CODEC_ERROR; |
253 } | 275 } |
254 RTC_DCHECK(pixel_buffer); | 276 RTC_DCHECK(pixel_buffer); |
(...skipping 23 matching lines...) Expand all Loading... | |
278 frame_properties = internal::CreateCFDictionary(keys, values, 1); | 300 frame_properties = internal::CreateCFDictionary(keys, values, 1); |
279 } | 301 } |
280 std::unique_ptr<internal::FrameEncodeParams> encode_params; | 302 std::unique_ptr<internal::FrameEncodeParams> encode_params; |
281 encode_params.reset(new internal::FrameEncodeParams( | 303 encode_params.reset(new internal::FrameEncodeParams( |
282 this, codec_specific_info, width_, height_, input_image.render_time_ms(), | 304 this, codec_specific_info, width_, height_, input_image.render_time_ms(), |
283 input_image.timestamp())); | 305 input_image.timestamp())); |
284 | 306 |
285 // Update the bitrate if needed. | 307 // Update the bitrate if needed. |
286 SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps()); | 308 SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps()); |
287 | 309 |
288 VTCompressionSessionEncodeFrame( | 310 OSStatus status = VTCompressionSessionEncodeFrame( |
289 compression_session_, pixel_buffer, presentation_time_stamp, | 311 compression_session_, pixel_buffer, presentation_time_stamp, |
290 kCMTimeInvalid, frame_properties, encode_params.release(), nullptr); | 312 kCMTimeInvalid, frame_properties, encode_params.release(), nullptr); |
291 if (frame_properties) { | 313 if (frame_properties) { |
292 CFRelease(frame_properties); | 314 CFRelease(frame_properties); |
293 } | 315 } |
294 if (pixel_buffer) { | 316 if (pixel_buffer) { |
295 CVBufferRelease(pixel_buffer); | 317 CVBufferRelease(pixel_buffer); |
296 } | 318 } |
319 if (status != noErr) { | |
320 LOG(LS_ERROR) << "Failed to encode frame with code: " << status; | |
321 return WEBRTC_VIDEO_CODEC_ERROR; | |
322 } | |
297 return WEBRTC_VIDEO_CODEC_OK; | 323 return WEBRTC_VIDEO_CODEC_OK; |
298 } | 324 } |
299 | 325 |
300 int H264VideoToolboxEncoder::RegisterEncodeCompleteCallback( | 326 int H264VideoToolboxEncoder::RegisterEncodeCompleteCallback( |
301 EncodedImageCallback* callback) { | 327 EncodedImageCallback* callback) { |
302 callback_ = callback; | 328 callback_ = callback; |
303 return WEBRTC_VIDEO_CODEC_OK; | 329 return WEBRTC_VIDEO_CODEC_OK; |
304 } | 330 } |
305 | 331 |
306 int H264VideoToolboxEncoder::SetChannelParameters(uint32_t packet_loss, | 332 int H264VideoToolboxEncoder::SetChannelParameters(uint32_t packet_loss, |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
486 if (result != 0) { | 512 if (result != 0) { |
487 LOG(LS_ERROR) << "Encode callback failed: " << result; | 513 LOG(LS_ERROR) << "Encode callback failed: " << result; |
488 return; | 514 return; |
489 } | 515 } |
490 bitrate_adjuster_.Update(frame._size); | 516 bitrate_adjuster_.Update(frame._size); |
491 } | 517 } |
492 | 518 |
493 } // namespace webrtc | 519 } // namespace webrtc |
494 | 520 |
495 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) | 521 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
OLD | NEW |