blob: bc46b35364e76a078ee0d6dbe9ebb43b3a39925a [file] [log] [blame]
Zeke Chinf86edef2015-06-29 21:34:581/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 *
10 */
11
Kári Tristan Helgasonfc168052016-11-09 09:43:2612#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_encoder.h"
Zeke Chinf86edef2015-06-29 21:34:5813
kwiberg7c6513a2016-02-29 13:51:5914#include <memory>
Zeke Chinf86edef2015-06-29 21:34:5815#include <string>
16#include <vector>
17
tkchin09716bd2016-04-27 08:54:2018#if defined(WEBRTC_IOS)
tkchindc068582016-08-03 19:57:0919#import "WebRTC/UIDevice+RTCDevice.h"
tkchin09716bd2016-04-27 08:54:2020#include "RTCUIApplication.h"
21#endif
Zeke Chinf86edef2015-06-29 21:34:5822#include "libyuv/convert_from.h"
23#include "webrtc/base/checks.h"
24#include "webrtc/base/logging.h"
magjed2e3a8c62016-11-15 17:56:5525#include "webrtc/common_video/h264/profile_level_id.h"
magjed4d9524e2016-10-20 10:34:2926#include "webrtc/common_video/include/corevideo_frame_buffer.h"
Kári Tristan Helgasonfc168052016-11-09 09:43:2627#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h"
tkchin4289b602016-02-24 06:49:4228#include "webrtc/system_wrappers/include/clock.h"
Zeke Chinf86edef2015-06-29 21:34:5829
30namespace internal {
31
magjedf82b08f2016-07-26 10:10:3332// The ratio between kVTCompressionPropertyKey_DataRateLimits and
33// kVTCompressionPropertyKey_AverageBitRate. The data rate limit is set higher
34// than the average bit rate to avoid undershooting the target.
35const float kLimitToAverageBitRateFactor = 1.5f;
kthelgason77dbe492016-09-28 15:17:4336// These thresholds deviate from the default h264 QP thresholds, as they
37// have been found to work better on devices that support VideoToolbox
38const int kLowH264QpThreshold = 28;
39const int kHighH264QpThreshold = 39;
magjedf82b08f2016-07-26 10:10:3340
Zeke Chinf86edef2015-06-29 21:34:5841// Convenience function for creating a dictionary.
42inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys,
43 CFTypeRef* values,
44 size_t size) {
45 return CFDictionaryCreate(kCFAllocatorDefault, keys, values, size,
46 &kCFTypeDictionaryKeyCallBacks,
47 &kCFTypeDictionaryValueCallBacks);
48}
49
50// Copies characters from a CFStringRef into a std::string.
51std::string CFStringToString(const CFStringRef cf_string) {
henrikg5c075c82015-09-17 07:24:3452 RTC_DCHECK(cf_string);
Zeke Chinf86edef2015-06-29 21:34:5853 std::string std_string;
54 // Get the size needed for UTF8 plus terminating character.
55 size_t buffer_size =
56 CFStringGetMaximumSizeForEncoding(CFStringGetLength(cf_string),
57 kCFStringEncodingUTF8) +
58 1;
kwiberg7c6513a2016-02-29 13:51:5959 std::unique_ptr<char[]> buffer(new char[buffer_size]);
Zeke Chinf86edef2015-06-29 21:34:5860 if (CFStringGetCString(cf_string, buffer.get(), buffer_size,
61 kCFStringEncodingUTF8)) {
62 // Copy over the characters.
63 std_string.assign(buffer.get());
64 }
65 return std_string;
66}
67
68// Convenience function for setting a VT property.
69void SetVTSessionProperty(VTSessionRef session,
70 CFStringRef key,
71 int32_t value) {
72 CFNumberRef cfNum =
73 CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
74 OSStatus status = VTSessionSetProperty(session, key, cfNum);
75 CFRelease(cfNum);
76 if (status != noErr) {
77 std::string key_string = CFStringToString(key);
78 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
79 << " to " << value << ": " << status;
80 }
81}
82
83// Convenience function for setting a VT property.
tkchin4289b602016-02-24 06:49:4284void SetVTSessionProperty(VTSessionRef session,
85 CFStringRef key,
86 uint32_t value) {
87 int64_t value_64 = value;
88 CFNumberRef cfNum =
89 CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value_64);
90 OSStatus status = VTSessionSetProperty(session, key, cfNum);
91 CFRelease(cfNum);
92 if (status != noErr) {
93 std::string key_string = CFStringToString(key);
94 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
95 << " to " << value << ": " << status;
96 }
97}
98
99// Convenience function for setting a VT property.
Zeke Chinf86edef2015-06-29 21:34:58100void SetVTSessionProperty(VTSessionRef session, CFStringRef key, bool value) {
101 CFBooleanRef cf_bool = (value) ? kCFBooleanTrue : kCFBooleanFalse;
102 OSStatus status = VTSessionSetProperty(session, key, cf_bool);
103 if (status != noErr) {
104 std::string key_string = CFStringToString(key);
105 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
106 << " to " << value << ": " << status;
107 }
108}
109
110// Convenience function for setting a VT property.
111void SetVTSessionProperty(VTSessionRef session,
112 CFStringRef key,
113 CFStringRef value) {
114 OSStatus status = VTSessionSetProperty(session, key, value);
115 if (status != noErr) {
116 std::string key_string = CFStringToString(key);
117 std::string val_string = CFStringToString(value);
118 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
119 << " to " << val_string << ": " << status;
120 }
121}
122
123// Struct that we pass to the encoder per frame to encode. We receive it again
124// in the encoder callback.
125struct FrameEncodeParams {
tkchin4289b602016-02-24 06:49:42126 FrameEncodeParams(webrtc::H264VideoToolboxEncoder* e,
Zeke Chinf86edef2015-06-29 21:34:58127 const webrtc::CodecSpecificInfo* csi,
128 int32_t w,
129 int32_t h,
130 int64_t rtms,
Pereb1bef42016-04-19 13:01:23131 uint32_t ts,
132 webrtc::VideoRotation r)
133 : encoder(e),
134 width(w),
135 height(h),
136 render_time_ms(rtms),
137 timestamp(ts),
138 rotation(r) {
Zeke Chinf86edef2015-06-29 21:34:58139 if (csi) {
140 codec_specific_info = *csi;
141 } else {
142 codec_specific_info.codecType = webrtc::kVideoCodecH264;
143 }
144 }
tkchin4289b602016-02-24 06:49:42145
146 webrtc::H264VideoToolboxEncoder* encoder;
Zeke Chinf86edef2015-06-29 21:34:58147 webrtc::CodecSpecificInfo codec_specific_info;
148 int32_t width;
149 int32_t height;
150 int64_t render_time_ms;
151 uint32_t timestamp;
Pereb1bef42016-04-19 13:01:23152 webrtc::VideoRotation rotation;
Zeke Chinf86edef2015-06-29 21:34:58153};
154
155// We receive I420Frames as input, but we need to feed CVPixelBuffers into the
156// encoder. This performs the copy and format conversion.
157// TODO(tkchin): See if encoder will accept i420 frames and compare performance.
Niels Möller34d0c322016-06-13 11:06:01158bool CopyVideoFrameToPixelBuffer(
159 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& frame,
160 CVPixelBufferRef pixel_buffer) {
henrikg5c075c82015-09-17 07:24:34161 RTC_DCHECK(pixel_buffer);
magjed98c31032016-08-20 17:53:26162 RTC_DCHECK_EQ(CVPixelBufferGetPixelFormatType(pixel_buffer),
163 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange);
164 RTC_DCHECK_EQ(CVPixelBufferGetHeightOfPlane(pixel_buffer, 0),
165 static_cast<size_t>(frame->height()));
166 RTC_DCHECK_EQ(CVPixelBufferGetWidthOfPlane(pixel_buffer, 0),
167 static_cast<size_t>(frame->width()));
Zeke Chinf86edef2015-06-29 21:34:58168
169 CVReturn cvRet = CVPixelBufferLockBaseAddress(pixel_buffer, 0);
170 if (cvRet != kCVReturnSuccess) {
171 LOG(LS_ERROR) << "Failed to lock base address: " << cvRet;
172 return false;
173 }
Peter Boström07e22e62015-10-07 10:23:21174 uint8_t* dst_y = reinterpret_cast<uint8_t*>(
Zeke Chinf86edef2015-06-29 21:34:58175 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer, 0));
176 int dst_stride_y = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer, 0);
Peter Boström07e22e62015-10-07 10:23:21177 uint8_t* dst_uv = reinterpret_cast<uint8_t*>(
Zeke Chinf86edef2015-06-29 21:34:58178 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer, 1));
179 int dst_stride_uv = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer, 1);
180 // Convert I420 to NV12.
181 int ret = libyuv::I420ToNV12(
Niels Möller34d0c322016-06-13 11:06:01182 frame->DataY(), frame->StrideY(),
183 frame->DataU(), frame->StrideU(),
184 frame->DataV(), frame->StrideV(),
nisse671d8052016-05-17 11:05:47185 dst_y, dst_stride_y, dst_uv, dst_stride_uv,
Niels Möller34d0c322016-06-13 11:06:01186 frame->width(), frame->height());
Zeke Chinf86edef2015-06-29 21:34:58187 CVPixelBufferUnlockBaseAddress(pixel_buffer, 0);
188 if (ret) {
189 LOG(LS_ERROR) << "Error converting I420 VideoFrame to NV12 :" << ret;
190 return false;
191 }
192 return true;
193}
194
magjed4d9524e2016-10-20 10:34:29195CVPixelBufferRef CreatePixelBuffer(CVPixelBufferPoolRef pixel_buffer_pool) {
196 if (!pixel_buffer_pool) {
197 LOG(LS_ERROR) << "Failed to get pixel buffer pool.";
198 return nullptr;
199 }
200 CVPixelBufferRef pixel_buffer;
201 CVReturn ret = CVPixelBufferPoolCreatePixelBuffer(nullptr, pixel_buffer_pool,
202 &pixel_buffer);
203 if (ret != kCVReturnSuccess) {
204 LOG(LS_ERROR) << "Failed to create pixel buffer: " << ret;
205 // We probably want to drop frames here, since failure probably means
206 // that the pool is empty.
207 return nullptr;
208 }
209 return pixel_buffer;
210}
211
Zeke Chinf86edef2015-06-29 21:34:58212// This is the callback function that VideoToolbox calls when encode is
tkchin4289b602016-02-24 06:49:42213// complete. From inspection this happens on its own queue.
Zeke Chinf86edef2015-06-29 21:34:58214void VTCompressionOutputCallback(void* encoder,
215 void* params,
216 OSStatus status,
217 VTEncodeInfoFlags info_flags,
218 CMSampleBufferRef sample_buffer) {
kwiberg7c6513a2016-02-29 13:51:59219 std::unique_ptr<FrameEncodeParams> encode_params(
Zeke Chinf86edef2015-06-29 21:34:58220 reinterpret_cast<FrameEncodeParams*>(params));
tkchin4289b602016-02-24 06:49:42221 encode_params->encoder->OnEncodedFrame(
222 status, info_flags, sample_buffer, encode_params->codec_specific_info,
223 encode_params->width, encode_params->height,
Pereb1bef42016-04-19 13:01:23224 encode_params->render_time_ms, encode_params->timestamp,
225 encode_params->rotation);
Zeke Chinf86edef2015-06-29 21:34:58226}
227
magjed2e3a8c62016-11-15 17:56:55228// Extract VideoToolbox profile out of the cricket::VideoCodec. If there is no
229// specific VideoToolbox profile for the specified level, AutoLevel will be
230// returned. The user must initialize the encoder with a resolution and
231// framerate conforming to the selected H264 level regardless.
232CFStringRef ExtractProfile(const cricket::VideoCodec& codec) {
233 const rtc::Optional<webrtc::H264::ProfileLevelId> profile_level_id =
234 webrtc::H264::ParseSdpProfileLevelId(codec.params);
235 RTC_DCHECK(profile_level_id);
236 switch (profile_level_id->profile) {
237 case webrtc::H264::kProfileConstrainedBaseline:
238 case webrtc::H264::kProfileBaseline:
239 switch (profile_level_id->level) {
240 case webrtc::H264::kLevel3:
241 return kVTProfileLevel_H264_Baseline_3_0;
242 case webrtc::H264::kLevel3_1:
243 return kVTProfileLevel_H264_Baseline_3_1;
244 case webrtc::H264::kLevel3_2:
245 return kVTProfileLevel_H264_Baseline_3_2;
246 case webrtc::H264::kLevel4:
247 return kVTProfileLevel_H264_Baseline_4_0;
248 case webrtc::H264::kLevel4_1:
249 return kVTProfileLevel_H264_Baseline_4_1;
250 case webrtc::H264::kLevel4_2:
251 return kVTProfileLevel_H264_Baseline_4_2;
252 case webrtc::H264::kLevel5:
253 return kVTProfileLevel_H264_Baseline_5_0;
254 case webrtc::H264::kLevel5_1:
255 return kVTProfileLevel_H264_Baseline_5_1;
256 case webrtc::H264::kLevel5_2:
257 return kVTProfileLevel_H264_Baseline_5_2;
258 case webrtc::H264::kLevel1:
259 case webrtc::H264::kLevel1_b:
260 case webrtc::H264::kLevel1_1:
261 case webrtc::H264::kLevel1_2:
262 case webrtc::H264::kLevel1_3:
263 case webrtc::H264::kLevel2:
264 case webrtc::H264::kLevel2_1:
265 case webrtc::H264::kLevel2_2:
266 return kVTProfileLevel_H264_Baseline_AutoLevel;
267 }
268
269 case webrtc::H264::kProfileMain:
270 switch (profile_level_id->level) {
271 case webrtc::H264::kLevel3:
272 return kVTProfileLevel_H264_Main_3_0;
273 case webrtc::H264::kLevel3_1:
274 return kVTProfileLevel_H264_Main_3_1;
275 case webrtc::H264::kLevel3_2:
276 return kVTProfileLevel_H264_Main_3_2;
277 case webrtc::H264::kLevel4:
278 return kVTProfileLevel_H264_Main_4_0;
279 case webrtc::H264::kLevel4_1:
280 return kVTProfileLevel_H264_Main_4_1;
281 case webrtc::H264::kLevel4_2:
282 return kVTProfileLevel_H264_Main_4_2;
283 case webrtc::H264::kLevel5:
284 return kVTProfileLevel_H264_Main_5_0;
285 case webrtc::H264::kLevel5_1:
286 return kVTProfileLevel_H264_Main_5_1;
287 case webrtc::H264::kLevel5_2:
288 return kVTProfileLevel_H264_Main_5_2;
289 case webrtc::H264::kLevel1:
290 case webrtc::H264::kLevel1_b:
291 case webrtc::H264::kLevel1_1:
292 case webrtc::H264::kLevel1_2:
293 case webrtc::H264::kLevel1_3:
294 case webrtc::H264::kLevel2:
295 case webrtc::H264::kLevel2_1:
296 case webrtc::H264::kLevel2_2:
297 return kVTProfileLevel_H264_Main_AutoLevel;
298 }
299
300 case webrtc::H264::kProfileConstrainedHigh:
301 case webrtc::H264::kProfileHigh:
302 switch (profile_level_id->level) {
303 case webrtc::H264::kLevel3:
304 return kVTProfileLevel_H264_High_3_0;
305 case webrtc::H264::kLevel3_1:
306 return kVTProfileLevel_H264_High_3_1;
307 case webrtc::H264::kLevel3_2:
308 return kVTProfileLevel_H264_High_3_2;
309 case webrtc::H264::kLevel4:
310 return kVTProfileLevel_H264_High_4_0;
311 case webrtc::H264::kLevel4_1:
312 return kVTProfileLevel_H264_High_4_1;
313 case webrtc::H264::kLevel4_2:
314 return kVTProfileLevel_H264_High_4_2;
315 case webrtc::H264::kLevel5:
316 return kVTProfileLevel_H264_High_5_0;
317 case webrtc::H264::kLevel5_1:
318 return kVTProfileLevel_H264_High_5_1;
319 case webrtc::H264::kLevel5_2:
320 return kVTProfileLevel_H264_High_5_2;
321 case webrtc::H264::kLevel1:
322 case webrtc::H264::kLevel1_b:
323 case webrtc::H264::kLevel1_1:
324 case webrtc::H264::kLevel1_2:
325 case webrtc::H264::kLevel1_3:
326 case webrtc::H264::kLevel2:
327 case webrtc::H264::kLevel2_1:
328 case webrtc::H264::kLevel2_2:
329 return kVTProfileLevel_H264_High_AutoLevel;
330 }
331 }
332}
333
Zeke Chinf86edef2015-06-29 21:34:58334} // namespace internal
335
336namespace webrtc {
337
tkchin4289b602016-02-24 06:49:42338// .5 is set as a mininum to prevent overcompensating for large temporary
339// overshoots. We don't want to degrade video quality too badly.
340// .95 is set to prevent oscillations. When a lower bitrate is set on the
341// encoder than previously set, its output seems to have a brief period of
342// drastically reduced bitrate, so we want to avoid that. In steady state
343// conditions, 0.95 seems to give us better overall bitrate over long periods
344// of time.
kthelgasonb90203c2016-12-16 10:10:20345H264VideoToolboxEncoder::H264VideoToolboxEncoder(const cricket::VideoCodec& codec)
tkchin4289b602016-02-24 06:49:42346 : callback_(nullptr),
347 compression_session_(nullptr),
magjed2e3a8c62016-11-15 17:56:55348 bitrate_adjuster_(Clock::GetRealTimeClock(), .5, .95),
kthelgasonb90203c2016-12-16 10:10:20349 packetization_mode_(H264PacketizationMode::NonInterleaved),
magjed2e3a8c62016-11-15 17:56:55350 profile_(internal::ExtractProfile(codec)) {
351 LOG(LS_INFO) << "Using profile " << internal::CFStringToString(profile_);
kthelgasonb90203c2016-12-16 10:10:20352 RTC_CHECK(cricket::CodecNamesEq(codec.name, cricket::kH264CodecName));
tkchindc068582016-08-03 19:57:09353}
Zeke Chinf86edef2015-06-29 21:34:58354
355H264VideoToolboxEncoder::~H264VideoToolboxEncoder() {
356 DestroyCompressionSession();
357}
358
359int H264VideoToolboxEncoder::InitEncode(const VideoCodec* codec_settings,
360 int number_of_cores,
361 size_t max_payload_size) {
henrikg5c075c82015-09-17 07:24:34362 RTC_DCHECK(codec_settings);
363 RTC_DCHECK_EQ(codec_settings->codecType, kVideoCodecH264);
kthelgason5f55ae62016-11-29 09:44:11364
365 width_ = codec_settings->width;
366 height_ = codec_settings->height;
ilnikcb436f62017-04-11 17:34:31367 mode_ = codec_settings->mode;
Zeke Chinf86edef2015-06-29 21:34:58368 // We can only set average bitrate on the HW encoder.
tkchin4289b602016-02-24 06:49:42369 target_bitrate_bps_ = codec_settings->startBitrate;
370 bitrate_adjuster_.SetTargetBitrateBps(target_bitrate_bps_);
Zeke Chinf86edef2015-06-29 21:34:58371
372 // TODO(tkchin): Try setting payload size via
373 // kVTCompressionPropertyKey_MaxH264SliceBytes.
374
375 return ResetCompressionSession();
376}
377
378int H264VideoToolboxEncoder::Encode(
Peter Boströme2087992016-05-24 10:21:58379 const VideoFrame& frame,
Zeke Chinf86edef2015-06-29 21:34:58380 const CodecSpecificInfo* codec_specific_info,
pbosb63d8ad2015-10-19 09:39:06381 const std::vector<FrameType>* frame_types) {
kthelgason5f55ae62016-11-29 09:44:11382 // |input_frame| size should always match codec settings.
383 RTC_DCHECK_EQ(frame.width(), width_);
384 RTC_DCHECK_EQ(frame.height(), height_);
Zeke Chinf86edef2015-06-29 21:34:58385 if (!callback_ || !compression_session_) {
386 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
387 }
tkchinc6a3b2c2016-03-08 18:51:54388#if defined(WEBRTC_IOS)
389 if (!RTCIsUIApplicationActive()) {
390 // Ignore all encode requests when app isn't active. In this state, the
391 // hardware encoder has been invalidated by the OS.
392 return WEBRTC_VIDEO_CODEC_OK;
393 }
394#endif
tkchind9cd6a92016-04-11 18:40:25395 bool is_keyframe_required = false;
Peter Boströme2087992016-05-24 10:21:58396
tkchina75ed7e2016-08-09 20:02:00397 // Get a pixel buffer from the pool and copy frame data over.
398 CVPixelBufferPoolRef pixel_buffer_pool =
399 VTCompressionSessionGetPixelBufferPool(compression_session_);
400#if defined(WEBRTC_IOS)
401 if (!pixel_buffer_pool) {
402 // Kind of a hack. On backgrounding, the compression session seems to get
403 // invalidated, which causes this pool call to fail when the application
404 // is foregrounded and frames are being sent for encoding again.
405 // Resetting the session when this happens fixes the issue.
406 // In addition we request a keyframe so video can recover quickly.
407 ResetCompressionSession();
408 pixel_buffer_pool =
409 VTCompressionSessionGetPixelBufferPool(compression_session_);
410 is_keyframe_required = true;
411 LOG(LS_INFO) << "Resetting compression session due to invalid pool.";
412 }
413#endif
414
magjed98c31032016-08-20 17:53:26415 CVPixelBufferRef pixel_buffer = static_cast<CVPixelBufferRef>(
416 frame.video_frame_buffer()->native_handle());
magjed80c30502016-07-12 08:26:42417 if (pixel_buffer) {
magjed4d9524e2016-10-20 10:34:29418 // Native frame.
419 rtc::scoped_refptr<CoreVideoFrameBuffer> core_video_frame_buffer(
420 static_cast<CoreVideoFrameBuffer*>(frame.video_frame_buffer().get()));
421 if (!core_video_frame_buffer->RequiresCropping()) {
422 // This pixel buffer might have a higher resolution than what the
423 // compression session is configured to. The compression session can
424 // handle that and will output encoded frames in the configured
425 // resolution regardless of the input pixel buffer resolution.
426 CVBufferRetain(pixel_buffer);
427 } else {
428 // Cropping required, we need to crop and scale to a new pixel buffer.
429 pixel_buffer = internal::CreatePixelBuffer(pixel_buffer_pool);
430 if (!pixel_buffer) {
431 return WEBRTC_VIDEO_CODEC_ERROR;
432 }
433 if (!core_video_frame_buffer->CropAndScaleTo(&nv12_scale_buffer_,
434 pixel_buffer)) {
435 return WEBRTC_VIDEO_CODEC_ERROR;
436 }
437 }
magjed80c30502016-07-12 08:26:42438 } else {
magjed4d9524e2016-10-20 10:34:29439 pixel_buffer = internal::CreatePixelBuffer(pixel_buffer_pool);
440 if (!pixel_buffer) {
magjed80c30502016-07-12 08:26:42441 return WEBRTC_VIDEO_CODEC_ERROR;
442 }
kthelgason5f55ae62016-11-29 09:44:11443 RTC_DCHECK(pixel_buffer);
444 if (!internal::CopyVideoFrameToPixelBuffer(frame.video_frame_buffer(),
magjed98c31032016-08-20 17:53:26445 pixel_buffer)) {
magjed80c30502016-07-12 08:26:42446 LOG(LS_ERROR) << "Failed to copy frame data.";
447 CVBufferRelease(pixel_buffer);
448 return WEBRTC_VIDEO_CODEC_ERROR;
449 }
Zeke Chinf86edef2015-06-29 21:34:58450 }
451
452 // Check if we need a keyframe.
tkchind9cd6a92016-04-11 18:40:25453 if (!is_keyframe_required && frame_types) {
Zeke Chinf86edef2015-06-29 21:34:58454 for (auto frame_type : *frame_types) {
Peter Boström9511e462015-10-23 13:58:18455 if (frame_type == kVideoFrameKey) {
Zeke Chinf86edef2015-06-29 21:34:58456 is_keyframe_required = true;
457 break;
458 }
459 }
460 }
461
462 CMTime presentation_time_stamp =
Niels Möller34d0c322016-06-13 11:06:01463 CMTimeMake(frame.render_time_ms(), 1000);
Zeke Chinf86edef2015-06-29 21:34:58464 CFDictionaryRef frame_properties = nullptr;
465 if (is_keyframe_required) {
philipel20a98012015-12-21 11:04:49466 CFTypeRef keys[] = {kVTEncodeFrameOptionKey_ForceKeyFrame};
467 CFTypeRef values[] = {kCFBooleanTrue};
Zeke Chinf86edef2015-06-29 21:34:58468 frame_properties = internal::CreateCFDictionary(keys, values, 1);
469 }
kwiberg7c6513a2016-02-29 13:51:59470 std::unique_ptr<internal::FrameEncodeParams> encode_params;
Zeke Chinf86edef2015-06-29 21:34:58471 encode_params.reset(new internal::FrameEncodeParams(
Niels Möller34d0c322016-06-13 11:06:01472 this, codec_specific_info, width_, height_, frame.render_time_ms(),
473 frame.timestamp(), frame.rotation()));
tkchin4289b602016-02-24 06:49:42474
kthelgasonb90203c2016-12-16 10:10:20475 encode_params->codec_specific_info.codecSpecific.H264.packetization_mode =
476 packetization_mode_;
477
tkchin4289b602016-02-24 06:49:42478 // Update the bitrate if needed.
479 SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps());
480
tkchinc6a3b2c2016-03-08 18:51:54481 OSStatus status = VTCompressionSessionEncodeFrame(
Zeke Chinf86edef2015-06-29 21:34:58482 compression_session_, pixel_buffer, presentation_time_stamp,
483 kCMTimeInvalid, frame_properties, encode_params.release(), nullptr);
484 if (frame_properties) {
485 CFRelease(frame_properties);
486 }
487 if (pixel_buffer) {
488 CVBufferRelease(pixel_buffer);
489 }
tkchinc6a3b2c2016-03-08 18:51:54490 if (status != noErr) {
491 LOG(LS_ERROR) << "Failed to encode frame with code: " << status;
492 return WEBRTC_VIDEO_CODEC_ERROR;
493 }
Zeke Chinf86edef2015-06-29 21:34:58494 return WEBRTC_VIDEO_CODEC_OK;
495}
496
497int H264VideoToolboxEncoder::RegisterEncodeCompleteCallback(
498 EncodedImageCallback* callback) {
499 callback_ = callback;
500 return WEBRTC_VIDEO_CODEC_OK;
501}
502
503int H264VideoToolboxEncoder::SetChannelParameters(uint32_t packet_loss,
504 int64_t rtt) {
505 // Encoder doesn't know anything about packet loss or rtt so just return.
506 return WEBRTC_VIDEO_CODEC_OK;
507}
508
509int H264VideoToolboxEncoder::SetRates(uint32_t new_bitrate_kbit,
510 uint32_t frame_rate) {
tkchin4289b602016-02-24 06:49:42511 target_bitrate_bps_ = 1000 * new_bitrate_kbit;
512 bitrate_adjuster_.SetTargetBitrateBps(target_bitrate_bps_);
513 SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps());
Zeke Chinf86edef2015-06-29 21:34:58514 return WEBRTC_VIDEO_CODEC_OK;
515}
516
517int H264VideoToolboxEncoder::Release() {
tkchin4289b602016-02-24 06:49:42518 // Need to reset so that the session is invalidated and won't use the
519 // callback anymore. Do not remove callback until the session is invalidated
520 // since async encoder callbacks can occur until invalidation.
521 int ret = ResetCompressionSession();
Zeke Chinf86edef2015-06-29 21:34:58522 callback_ = nullptr;
tkchin4289b602016-02-24 06:49:42523 return ret;
Zeke Chinf86edef2015-06-29 21:34:58524}
525
526int H264VideoToolboxEncoder::ResetCompressionSession() {
527 DestroyCompressionSession();
528
529 // Set source image buffer attributes. These attributes will be present on
530 // buffers retrieved from the encoder's pixel buffer pool.
531 const size_t attributes_size = 3;
532 CFTypeRef keys[attributes_size] = {
533#if defined(WEBRTC_IOS)
534 kCVPixelBufferOpenGLESCompatibilityKey,
535#elif defined(WEBRTC_MAC)
536 kCVPixelBufferOpenGLCompatibilityKey,
537#endif
538 kCVPixelBufferIOSurfacePropertiesKey,
539 kCVPixelBufferPixelFormatTypeKey
540 };
541 CFDictionaryRef io_surface_value =
542 internal::CreateCFDictionary(nullptr, nullptr, 0);
543 int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
544 CFNumberRef pixel_format =
545 CFNumberCreate(nullptr, kCFNumberLongType, &nv12type);
philipel20a98012015-12-21 11:04:49546 CFTypeRef values[attributes_size] = {kCFBooleanTrue, io_surface_value,
547 pixel_format};
Zeke Chinf86edef2015-06-29 21:34:58548 CFDictionaryRef source_attributes =
549 internal::CreateCFDictionary(keys, values, attributes_size);
550 if (io_surface_value) {
551 CFRelease(io_surface_value);
552 io_surface_value = nullptr;
553 }
554 if (pixel_format) {
555 CFRelease(pixel_format);
556 pixel_format = nullptr;
557 }
558 OSStatus status = VTCompressionSessionCreate(
559 nullptr, // use default allocator
philipel20a98012015-12-21 11:04:49560 width_, height_, kCMVideoCodecType_H264,
Zeke Chinf86edef2015-06-29 21:34:58561 nullptr, // use default encoder
562 source_attributes,
563 nullptr, // use default compressed data allocator
philipel20a98012015-12-21 11:04:49564 internal::VTCompressionOutputCallback, this, &compression_session_);
Zeke Chinf86edef2015-06-29 21:34:58565 if (source_attributes) {
566 CFRelease(source_attributes);
567 source_attributes = nullptr;
568 }
569 if (status != noErr) {
570 LOG(LS_ERROR) << "Failed to create compression session: " << status;
571 return WEBRTC_VIDEO_CODEC_ERROR;
572 }
573 ConfigureCompressionSession();
574 return WEBRTC_VIDEO_CODEC_OK;
575}
576
577void H264VideoToolboxEncoder::ConfigureCompressionSession() {
henrikg5c075c82015-09-17 07:24:34578 RTC_DCHECK(compression_session_);
Zeke Chinf86edef2015-06-29 21:34:58579 internal::SetVTSessionProperty(compression_session_,
580 kVTCompressionPropertyKey_RealTime, true);
581 internal::SetVTSessionProperty(compression_session_,
582 kVTCompressionPropertyKey_ProfileLevel,
magjed2e3a8c62016-11-15 17:56:55583 profile_);
Zeke Chinf86edef2015-06-29 21:34:58584 internal::SetVTSessionProperty(compression_session_,
585 kVTCompressionPropertyKey_AllowFrameReordering,
586 false);
tkchin4289b602016-02-24 06:49:42587 SetEncoderBitrateBps(target_bitrate_bps_);
Zeke Chinf86edef2015-06-29 21:34:58588 // TODO(tkchin): Look at entropy mode and colorspace matrices.
589 // TODO(tkchin): Investigate to see if there's any way to make this work.
590 // May need it to interop with Android. Currently this call just fails.
591 // On inspecting encoder output on iOS8, this value is set to 6.
592 // internal::SetVTSessionProperty(compression_session_,
593 // kVTCompressionPropertyKey_MaxFrameDelayCount,
594 // 1);
Peter Bostrom79264912016-06-02 13:36:40595
596 // Set a relatively large value for keyframe emission (7200 frames or
597 // 4 minutes).
598 internal::SetVTSessionProperty(
599 compression_session_,
600 kVTCompressionPropertyKey_MaxKeyFrameInterval, 7200);
601 internal::SetVTSessionProperty(
602 compression_session_,
603 kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, 240);
Zeke Chinf86edef2015-06-29 21:34:58604}
605
606void H264VideoToolboxEncoder::DestroyCompressionSession() {
607 if (compression_session_) {
608 VTCompressionSessionInvalidate(compression_session_);
609 CFRelease(compression_session_);
610 compression_session_ = nullptr;
611 }
612}
613
Peter Boström8468abc2015-12-18 15:01:11614const char* H264VideoToolboxEncoder::ImplementationName() const {
615 return "VideoToolbox";
616}
617
magjed80c30502016-07-12 08:26:42618bool H264VideoToolboxEncoder::SupportsNativeHandle() const {
619 return true;
620}
621
tkchin4289b602016-02-24 06:49:42622void H264VideoToolboxEncoder::SetBitrateBps(uint32_t bitrate_bps) {
623 if (encoder_bitrate_bps_ != bitrate_bps) {
624 SetEncoderBitrateBps(bitrate_bps);
625 }
626}
627
628void H264VideoToolboxEncoder::SetEncoderBitrateBps(uint32_t bitrate_bps) {
629 if (compression_session_) {
630 internal::SetVTSessionProperty(compression_session_,
631 kVTCompressionPropertyKey_AverageBitRate,
632 bitrate_bps);
Peter Bostrom79264912016-06-02 13:36:40633
634 // TODO(tkchin): Add a helper method to set array value.
magjedf82b08f2016-07-26 10:10:33635 int64_t data_limit_bytes_per_second_value = static_cast<int64_t>(
636 bitrate_bps * internal::kLimitToAverageBitRateFactor / 8);
Peter Bostrom79264912016-06-02 13:36:40637 CFNumberRef bytes_per_second =
638 CFNumberCreate(kCFAllocatorDefault,
639 kCFNumberSInt64Type,
magjedf82b08f2016-07-26 10:10:33640 &data_limit_bytes_per_second_value);
Peter Bostrom79264912016-06-02 13:36:40641 int64_t one_second_value = 1;
642 CFNumberRef one_second =
643 CFNumberCreate(kCFAllocatorDefault,
644 kCFNumberSInt64Type,
645 &one_second_value);
646 const void* nums[2] = { bytes_per_second, one_second };
647 CFArrayRef data_rate_limits =
648 CFArrayCreate(nullptr, nums, 2, &kCFTypeArrayCallBacks);
649 OSStatus status =
650 VTSessionSetProperty(compression_session_,
651 kVTCompressionPropertyKey_DataRateLimits,
652 data_rate_limits);
653 if (bytes_per_second) {
654 CFRelease(bytes_per_second);
655 }
656 if (one_second) {
657 CFRelease(one_second);
658 }
659 if (data_rate_limits) {
660 CFRelease(data_rate_limits);
661 }
662 if (status != noErr) {
663 LOG(LS_ERROR) << "Failed to set data rate limit";
664 }
665
tkchin4289b602016-02-24 06:49:42666 encoder_bitrate_bps_ = bitrate_bps;
667 }
668}
669
670void H264VideoToolboxEncoder::OnEncodedFrame(
671 OSStatus status,
672 VTEncodeInfoFlags info_flags,
673 CMSampleBufferRef sample_buffer,
674 CodecSpecificInfo codec_specific_info,
675 int32_t width,
676 int32_t height,
677 int64_t render_time_ms,
Pereb1bef42016-04-19 13:01:23678 uint32_t timestamp,
679 VideoRotation rotation) {
tkchin4289b602016-02-24 06:49:42680 if (status != noErr) {
681 LOG(LS_ERROR) << "H264 encode failed.";
682 return;
683 }
684 if (info_flags & kVTEncodeInfo_FrameDropped) {
685 LOG(LS_INFO) << "H264 encode dropped frame.";
Peter Boströme2087992016-05-24 10:21:58686 return;
tkchin4289b602016-02-24 06:49:42687 }
688
689 bool is_keyframe = false;
690 CFArrayRef attachments =
691 CMSampleBufferGetSampleAttachmentsArray(sample_buffer, 0);
692 if (attachments != nullptr && CFArrayGetCount(attachments)) {
693 CFDictionaryRef attachment =
694 static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(attachments, 0));
695 is_keyframe =
696 !CFDictionaryContainsKey(attachment, kCMSampleAttachmentKey_NotSync);
697 }
698
Peter Bostrom79264912016-06-02 13:36:40699 if (is_keyframe) {
700 LOG(LS_INFO) << "Generated keyframe";
701 }
702
tkchin4289b602016-02-24 06:49:42703 // Convert the sample buffer into a buffer suitable for RTP packetization.
704 // TODO(tkchin): Allocate buffers through a pool.
kwiberg7c6513a2016-02-29 13:51:59705 std::unique_ptr<rtc::Buffer> buffer(new rtc::Buffer());
706 std::unique_ptr<webrtc::RTPFragmentationHeader> header;
707 {
708 webrtc::RTPFragmentationHeader* header_raw;
709 bool result = H264CMSampleBufferToAnnexBBuffer(sample_buffer, is_keyframe,
710 buffer.get(), &header_raw);
711 header.reset(header_raw);
712 if (!result) {
713 return;
714 }
tkchin4289b602016-02-24 06:49:42715 }
716 webrtc::EncodedImage frame(buffer->data(), buffer->size(), buffer->size());
717 frame._encodedWidth = width;
718 frame._encodedHeight = height;
719 frame._completeFrame = true;
720 frame._frameType =
721 is_keyframe ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta;
722 frame.capture_time_ms_ = render_time_ms;
723 frame._timeStamp = timestamp;
Pereb1bef42016-04-19 13:01:23724 frame.rotation_ = rotation;
tkchin4289b602016-02-24 06:49:42725
ilnikcb436f62017-04-11 17:34:31726 frame.content_type_ =
727 (mode_ == kScreensharing) ? VideoContentType::SCREENSHARE : VideoContentType::UNSPECIFIED;
728
Peter Boströme2087992016-05-24 10:21:58729 h264_bitstream_parser_.ParseBitstream(buffer->data(), buffer->size());
kthelgason5f55ae62016-11-29 09:44:11730 h264_bitstream_parser_.GetLastSliceQp(&frame.qp_);
Peter Boströme2087992016-05-24 10:21:58731
kthelgason5f55ae62016-11-29 09:44:11732 EncodedImageCallback::Result res =
sergeyubd552c82016-11-04 18:39:29733 callback_->OnEncodedImage(frame, &codec_specific_info, header.get());
kthelgason5f55ae62016-11-29 09:44:11734 if (res.error != EncodedImageCallback::Result::OK) {
735 LOG(LS_ERROR) << "Encode callback failed: " << res.error;
tkchin4289b602016-02-24 06:49:42736 return;
737 }
kthelgason821e8602017-01-10 11:02:04738 bitrate_adjuster_.Update(frame._length);
tkchin4289b602016-02-24 06:49:42739}
740
kthelgason5f55ae62016-11-29 09:44:11741VideoEncoder::ScalingSettings H264VideoToolboxEncoder::GetScalingSettings()
742 const {
743 return VideoEncoder::ScalingSettings(true, internal::kLowH264QpThreshold,
744 internal::kHighH264QpThreshold);
745}
Zeke Chinf86edef2015-06-29 21:34:58746} // namespace webrtc