| 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 <string> | 17 #include <string> |
| 17 #include <vector> | 18 #include <vector> |
| 18 | 19 |
| 19 #include "libyuv/convert_from.h" | 20 #include "libyuv/convert_from.h" |
| 20 #include "webrtc/base/checks.h" | 21 #include "webrtc/base/checks.h" |
| 21 #include "webrtc/base/logging.h" | 22 #include "webrtc/base/logging.h" |
| 22 #include "webrtc/base/scoped_ptr.h" | |
| 23 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.h" | 23 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.h" |
| 24 #include "webrtc/system_wrappers/include/clock.h" | 24 #include "webrtc/system_wrappers/include/clock.h" |
| 25 | 25 |
| 26 namespace internal { | 26 namespace internal { |
| 27 | 27 |
| 28 // Convenience function for creating a dictionary. | 28 // Convenience function for creating a dictionary. |
| 29 inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys, | 29 inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys, |
| 30 CFTypeRef* values, | 30 CFTypeRef* values, |
| 31 size_t size) { | 31 size_t size) { |
| 32 return CFDictionaryCreate(kCFAllocatorDefault, keys, values, size, | 32 return CFDictionaryCreate(kCFAllocatorDefault, keys, values, size, |
| 33 &kCFTypeDictionaryKeyCallBacks, | 33 &kCFTypeDictionaryKeyCallBacks, |
| 34 &kCFTypeDictionaryValueCallBacks); | 34 &kCFTypeDictionaryValueCallBacks); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // Copies characters from a CFStringRef into a std::string. | 37 // Copies characters from a CFStringRef into a std::string. |
| 38 std::string CFStringToString(const CFStringRef cf_string) { | 38 std::string CFStringToString(const CFStringRef cf_string) { |
| 39 RTC_DCHECK(cf_string); | 39 RTC_DCHECK(cf_string); |
| 40 std::string std_string; | 40 std::string std_string; |
| 41 // Get the size needed for UTF8 plus terminating character. | 41 // Get the size needed for UTF8 plus terminating character. |
| 42 size_t buffer_size = | 42 size_t buffer_size = |
| 43 CFStringGetMaximumSizeForEncoding(CFStringGetLength(cf_string), | 43 CFStringGetMaximumSizeForEncoding(CFStringGetLength(cf_string), |
| 44 kCFStringEncodingUTF8) + | 44 kCFStringEncodingUTF8) + |
| 45 1; | 45 1; |
| 46 rtc::scoped_ptr<char[]> buffer(new char[buffer_size]); | 46 std::unique_ptr<char[]> buffer(new char[buffer_size]); |
| 47 if (CFStringGetCString(cf_string, buffer.get(), buffer_size, | 47 if (CFStringGetCString(cf_string, buffer.get(), buffer_size, |
| 48 kCFStringEncodingUTF8)) { | 48 kCFStringEncodingUTF8)) { |
| 49 // Copy over the characters. | 49 // Copy over the characters. |
| 50 std_string.assign(buffer.get()); | 50 std_string.assign(buffer.get()); |
| 51 } | 51 } |
| 52 return std_string; | 52 return std_string; |
| 53 } | 53 } |
| 54 | 54 |
| 55 // Convenience function for setting a VT property. | 55 // Convenience function for setting a VT property. |
| 56 void SetVTSessionProperty(VTSessionRef session, | 56 void SetVTSessionProperty(VTSessionRef session, |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 return true; | 170 return true; |
| 171 } | 171 } |
| 172 | 172 |
| 173 // This is the callback function that VideoToolbox calls when encode is | 173 // This is the callback function that VideoToolbox calls when encode is |
| 174 // complete. From inspection this happens on its own queue. | 174 // complete. From inspection this happens on its own queue. |
| 175 void VTCompressionOutputCallback(void* encoder, | 175 void VTCompressionOutputCallback(void* encoder, |
| 176 void* params, | 176 void* params, |
| 177 OSStatus status, | 177 OSStatus status, |
| 178 VTEncodeInfoFlags info_flags, | 178 VTEncodeInfoFlags info_flags, |
| 179 CMSampleBufferRef sample_buffer) { | 179 CMSampleBufferRef sample_buffer) { |
| 180 rtc::scoped_ptr<FrameEncodeParams> encode_params( | 180 std::unique_ptr<FrameEncodeParams> encode_params( |
| 181 reinterpret_cast<FrameEncodeParams*>(params)); | 181 reinterpret_cast<FrameEncodeParams*>(params)); |
| 182 encode_params->encoder->OnEncodedFrame( | 182 encode_params->encoder->OnEncodedFrame( |
| 183 status, info_flags, sample_buffer, encode_params->codec_specific_info, | 183 status, info_flags, sample_buffer, encode_params->codec_specific_info, |
| 184 encode_params->width, encode_params->height, | 184 encode_params->width, encode_params->height, |
| 185 encode_params->render_time_ms, encode_params->timestamp); | 185 encode_params->render_time_ms, encode_params->timestamp); |
| 186 } | 186 } |
| 187 | 187 |
| 188 } // namespace internal | 188 } // namespace internal |
| 189 | 189 |
| 190 namespace webrtc { | 190 namespace webrtc { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 } | 270 } |
| 271 | 271 |
| 272 CMTime presentation_time_stamp = | 272 CMTime presentation_time_stamp = |
| 273 CMTimeMake(input_image.render_time_ms(), 1000); | 273 CMTimeMake(input_image.render_time_ms(), 1000); |
| 274 CFDictionaryRef frame_properties = nullptr; | 274 CFDictionaryRef frame_properties = nullptr; |
| 275 if (is_keyframe_required) { | 275 if (is_keyframe_required) { |
| 276 CFTypeRef keys[] = {kVTEncodeFrameOptionKey_ForceKeyFrame}; | 276 CFTypeRef keys[] = {kVTEncodeFrameOptionKey_ForceKeyFrame}; |
| 277 CFTypeRef values[] = {kCFBooleanTrue}; | 277 CFTypeRef values[] = {kCFBooleanTrue}; |
| 278 frame_properties = internal::CreateCFDictionary(keys, values, 1); | 278 frame_properties = internal::CreateCFDictionary(keys, values, 1); |
| 279 } | 279 } |
| 280 rtc::scoped_ptr<internal::FrameEncodeParams> encode_params; | 280 std::unique_ptr<internal::FrameEncodeParams> encode_params; |
| 281 encode_params.reset(new internal::FrameEncodeParams( | 281 encode_params.reset(new internal::FrameEncodeParams( |
| 282 this, codec_specific_info, width_, height_, input_image.render_time_ms(), | 282 this, codec_specific_info, width_, height_, input_image.render_time_ms(), |
| 283 input_image.timestamp())); | 283 input_image.timestamp())); |
| 284 | 284 |
| 285 // Update the bitrate if needed. | 285 // Update the bitrate if needed. |
| 286 SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps()); | 286 SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps()); |
| 287 | 287 |
| 288 VTCompressionSessionEncodeFrame( | 288 VTCompressionSessionEncodeFrame( |
| 289 compression_session_, pixel_buffer, presentation_time_stamp, | 289 compression_session_, pixel_buffer, presentation_time_stamp, |
| 290 kCMTimeInvalid, frame_properties, encode_params.release(), nullptr); | 290 kCMTimeInvalid, frame_properties, encode_params.release(), nullptr); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 CMSampleBufferGetSampleAttachmentsArray(sample_buffer, 0); | 455 CMSampleBufferGetSampleAttachmentsArray(sample_buffer, 0); |
| 456 if (attachments != nullptr && CFArrayGetCount(attachments)) { | 456 if (attachments != nullptr && CFArrayGetCount(attachments)) { |
| 457 CFDictionaryRef attachment = | 457 CFDictionaryRef attachment = |
| 458 static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(attachments, 0)); | 458 static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(attachments, 0)); |
| 459 is_keyframe = | 459 is_keyframe = |
| 460 !CFDictionaryContainsKey(attachment, kCMSampleAttachmentKey_NotSync); | 460 !CFDictionaryContainsKey(attachment, kCMSampleAttachmentKey_NotSync); |
| 461 } | 461 } |
| 462 | 462 |
| 463 // Convert the sample buffer into a buffer suitable for RTP packetization. | 463 // Convert the sample buffer into a buffer suitable for RTP packetization. |
| 464 // TODO(tkchin): Allocate buffers through a pool. | 464 // TODO(tkchin): Allocate buffers through a pool. |
| 465 rtc::scoped_ptr<rtc::Buffer> buffer(new rtc::Buffer()); | 465 std::unique_ptr<rtc::Buffer> buffer(new rtc::Buffer()); |
| 466 rtc::scoped_ptr<webrtc::RTPFragmentationHeader> header; | 466 std::unique_ptr<webrtc::RTPFragmentationHeader> header; |
| 467 if (!H264CMSampleBufferToAnnexBBuffer(sample_buffer, is_keyframe, | 467 { |
| 468 buffer.get(), header.accept())) { | 468 webrtc::RTPFragmentationHeader* header_raw; |
| 469 return; | 469 bool result = H264CMSampleBufferToAnnexBBuffer(sample_buffer, is_keyframe, |
| 470 buffer.get(), &header_raw); |
| 471 header.reset(header_raw); |
| 472 if (!result) { |
| 473 return; |
| 474 } |
| 470 } | 475 } |
| 471 webrtc::EncodedImage frame(buffer->data(), buffer->size(), buffer->size()); | 476 webrtc::EncodedImage frame(buffer->data(), buffer->size(), buffer->size()); |
| 472 frame._encodedWidth = width; | 477 frame._encodedWidth = width; |
| 473 frame._encodedHeight = height; | 478 frame._encodedHeight = height; |
| 474 frame._completeFrame = true; | 479 frame._completeFrame = true; |
| 475 frame._frameType = | 480 frame._frameType = |
| 476 is_keyframe ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta; | 481 is_keyframe ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta; |
| 477 frame.capture_time_ms_ = render_time_ms; | 482 frame.capture_time_ms_ = render_time_ms; |
| 478 frame._timeStamp = timestamp; | 483 frame._timeStamp = timestamp; |
| 479 | 484 |
| 480 int result = callback_->Encoded(frame, &codec_specific_info, header.get()); | 485 int result = callback_->Encoded(frame, &codec_specific_info, header.get()); |
| 481 if (result != 0) { | 486 if (result != 0) { |
| 482 LOG(LS_ERROR) << "Encode callback failed: " << result; | 487 LOG(LS_ERROR) << "Encode callback failed: " << result; |
| 483 return; | 488 return; |
| 484 } | 489 } |
| 485 bitrate_adjuster_.Update(frame._size); | 490 bitrate_adjuster_.Update(frame._size); |
| 486 } | 491 } |
| 487 | 492 |
| 488 } // namespace webrtc | 493 } // namespace webrtc |
| 489 | 494 |
| 490 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) | 495 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
| OLD | NEW |