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 */ |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 OSStatus status = VTSessionSetProperty(session, key, cfNum); | 60 OSStatus status = VTSessionSetProperty(session, key, cfNum); |
61 CFRelease(cfNum); | 61 CFRelease(cfNum); |
62 if (status != noErr) { | 62 if (status != noErr) { |
63 std::string key_string = CFStringToString(key); | 63 std::string key_string = CFStringToString(key); |
64 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string | 64 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string |
65 << " to " << value << ": " << status; | 65 << " to " << value << ": " << status; |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 // Convenience function for setting a VT property. | 69 // Convenience function for setting a VT property. |
70 void SetVTSessionProperty(VTSessionRef session, | |
71 CFStringRef key, | |
72 uint32_t value) { | |
73 int64_t value_64 = value; | |
74 CFNumberRef cfNum = | |
75 CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value_64); | |
76 OSStatus status = VTSessionSetProperty(session, key, cfNum); | |
77 CFRelease(cfNum); | |
78 if (status != noErr) { | |
79 std::string key_string = CFStringToString(key); | |
80 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string | |
81 << " to " << value << ": " << status; | |
82 } | |
83 } | |
84 | |
85 // Convenience function for setting a VT property. | |
70 void SetVTSessionProperty(VTSessionRef session, CFStringRef key, bool value) { | 86 void SetVTSessionProperty(VTSessionRef session, CFStringRef key, bool value) { |
71 CFBooleanRef cf_bool = (value) ? kCFBooleanTrue : kCFBooleanFalse; | 87 CFBooleanRef cf_bool = (value) ? kCFBooleanTrue : kCFBooleanFalse; |
72 OSStatus status = VTSessionSetProperty(session, key, cf_bool); | 88 OSStatus status = VTSessionSetProperty(session, key, cf_bool); |
73 if (status != noErr) { | 89 if (status != noErr) { |
74 std::string key_string = CFStringToString(key); | 90 std::string key_string = CFStringToString(key); |
75 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string | 91 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string |
76 << " to " << value << ": " << status; | 92 << " to " << value << ": " << status; |
77 } | 93 } |
78 } | 94 } |
79 | 95 |
80 // Convenience function for setting a VT property. | 96 // Convenience function for setting a VT property. |
81 void SetVTSessionProperty(VTSessionRef session, | 97 void SetVTSessionProperty(VTSessionRef session, |
82 CFStringRef key, | 98 CFStringRef key, |
83 CFStringRef value) { | 99 CFStringRef value) { |
84 OSStatus status = VTSessionSetProperty(session, key, value); | 100 OSStatus status = VTSessionSetProperty(session, key, value); |
85 if (status != noErr) { | 101 if (status != noErr) { |
86 std::string key_string = CFStringToString(key); | 102 std::string key_string = CFStringToString(key); |
87 std::string val_string = CFStringToString(value); | 103 std::string val_string = CFStringToString(value); |
88 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string | 104 LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string |
89 << " to " << val_string << ": " << status; | 105 << " to " << val_string << ": " << status; |
90 } | 106 } |
91 } | 107 } |
92 | 108 |
93 // Struct that we pass to the encoder per frame to encode. We receive it again | 109 // Struct that we pass to the encoder per frame to encode. We receive it again |
94 // in the encoder callback. | 110 // in the encoder callback. |
95 struct FrameEncodeParams { | 111 struct FrameEncodeParams { |
96 FrameEncodeParams(webrtc::EncodedImageCallback* cb, | 112 FrameEncodeParams(webrtc::H264VideoToolboxEncoder* e, |
97 const webrtc::CodecSpecificInfo* csi, | 113 const webrtc::CodecSpecificInfo* csi, |
98 int32_t w, | 114 int32_t w, |
99 int32_t h, | 115 int32_t h, |
100 int64_t rtms, | 116 int64_t rtms, |
101 uint32_t ts) | 117 uint32_t ts) |
102 : callback(cb), width(w), height(h), render_time_ms(rtms), timestamp(ts) { | 118 : encoder(e), width(w), height(h), render_time_ms(rtms), timestamp(ts) { |
103 if (csi) { | 119 if (csi) { |
104 codec_specific_info = *csi; | 120 codec_specific_info = *csi; |
105 } else { | 121 } else { |
106 codec_specific_info.codecType = webrtc::kVideoCodecH264; | 122 codec_specific_info.codecType = webrtc::kVideoCodecH264; |
107 } | 123 } |
108 } | 124 } |
109 webrtc::EncodedImageCallback* callback; | 125 |
126 webrtc::H264VideoToolboxEncoder* encoder; | |
110 webrtc::CodecSpecificInfo codec_specific_info; | 127 webrtc::CodecSpecificInfo codec_specific_info; |
111 int32_t width; | 128 int32_t width; |
112 int32_t height; | 129 int32_t height; |
113 int64_t render_time_ms; | 130 int64_t render_time_ms; |
114 uint32_t timestamp; | 131 uint32_t timestamp; |
115 }; | 132 }; |
116 | 133 |
117 // We receive I420Frames as input, but we need to feed CVPixelBuffers into the | 134 // We receive I420Frames as input, but we need to feed CVPixelBuffers into the |
118 // encoder. This performs the copy and format conversion. | 135 // encoder. This performs the copy and format conversion. |
119 // TODO(tkchin): See if encoder will accept i420 frames and compare performance. | 136 // TODO(tkchin): See if encoder will accept i420 frames and compare performance. |
(...skipping 26 matching lines...) Expand all Loading... | |
146 dst_stride_y, dst_uv, dst_stride_uv, frame.width(), frame.height()); | 163 dst_stride_y, dst_uv, dst_stride_uv, frame.width(), frame.height()); |
147 CVPixelBufferUnlockBaseAddress(pixel_buffer, 0); | 164 CVPixelBufferUnlockBaseAddress(pixel_buffer, 0); |
148 if (ret) { | 165 if (ret) { |
149 LOG(LS_ERROR) << "Error converting I420 VideoFrame to NV12 :" << ret; | 166 LOG(LS_ERROR) << "Error converting I420 VideoFrame to NV12 :" << ret; |
150 return false; | 167 return false; |
151 } | 168 } |
152 return true; | 169 return true; |
153 } | 170 } |
154 | 171 |
155 // This is the callback function that VideoToolbox calls when encode is | 172 // This is the callback function that VideoToolbox calls when encode is |
156 // complete. | 173 // complete. From inspection this happens on its own queue. |
157 void VTCompressionOutputCallback(void* encoder, | 174 void VTCompressionOutputCallback(void* encoder, |
158 void* params, | 175 void* params, |
159 OSStatus status, | 176 OSStatus status, |
160 VTEncodeInfoFlags info_flags, | 177 VTEncodeInfoFlags info_flags, |
161 CMSampleBufferRef sample_buffer) { | 178 CMSampleBufferRef sample_buffer) { |
162 rtc::scoped_ptr<FrameEncodeParams> encode_params( | 179 rtc::scoped_ptr<FrameEncodeParams> encode_params( |
163 reinterpret_cast<FrameEncodeParams*>(params)); | 180 reinterpret_cast<FrameEncodeParams*>(params)); |
164 if (status != noErr) { | 181 encode_params->encoder->OnEncodedFrame( |
165 LOG(LS_ERROR) << "H264 encoding failed."; | 182 status, info_flags, sample_buffer, encode_params->codec_specific_info, |
166 return; | 183 encode_params->width, encode_params->height, |
167 } | 184 encode_params->render_time_ms, encode_params->timestamp); |
168 if (info_flags & kVTEncodeInfo_FrameDropped) { | |
169 LOG(LS_INFO) << "H264 encode dropped frame."; | |
170 } | |
171 | |
172 bool is_keyframe = false; | |
173 CFArrayRef attachments = | |
174 CMSampleBufferGetSampleAttachmentsArray(sample_buffer, 0); | |
175 if (attachments != nullptr && CFArrayGetCount(attachments)) { | |
176 CFDictionaryRef attachment = | |
177 static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(attachments, 0)); | |
178 is_keyframe = | |
179 !CFDictionaryContainsKey(attachment, kCMSampleAttachmentKey_NotSync); | |
180 } | |
181 | |
182 // Convert the sample buffer into a buffer suitable for RTP packetization. | |
183 // TODO(tkchin): Allocate buffers through a pool. | |
184 rtc::scoped_ptr<rtc::Buffer> buffer(new rtc::Buffer()); | |
185 rtc::scoped_ptr<webrtc::RTPFragmentationHeader> header; | |
186 if (!H264CMSampleBufferToAnnexBBuffer(sample_buffer, is_keyframe, | |
187 buffer.get(), header.accept())) { | |
188 return; | |
189 } | |
190 webrtc::EncodedImage frame(buffer->data(), buffer->size(), buffer->size()); | |
191 frame._encodedWidth = encode_params->width; | |
192 frame._encodedHeight = encode_params->height; | |
193 frame._completeFrame = true; | |
194 frame._frameType = | |
195 is_keyframe ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta; | |
196 frame.capture_time_ms_ = encode_params->render_time_ms; | |
197 frame._timeStamp = encode_params->timestamp; | |
198 | |
199 int result = encode_params->callback->Encoded( | |
200 frame, &(encode_params->codec_specific_info), header.get()); | |
201 if (result != 0) { | |
202 LOG(LS_ERROR) << "Encoded callback failed: " << result; | |
203 } | |
204 } | 185 } |
205 | 186 |
206 } // namespace internal | 187 } // namespace internal |
207 | 188 |
208 namespace webrtc { | 189 namespace webrtc { |
209 | 190 |
210 H264VideoToolboxEncoder::H264VideoToolboxEncoder() | 191 H264VideoToolboxEncoder::H264VideoToolboxEncoder() |
211 : callback_(nullptr), compression_session_(nullptr) {} | 192 : callback_(nullptr), |
193 compression_session_(nullptr), | |
194 bitrate_adjuster_(this) {} | |
212 | 195 |
213 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() { | 196 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() { |
214 DestroyCompressionSession(); | 197 DestroyCompressionSession(); |
215 } | 198 } |
216 | 199 |
217 int H264VideoToolboxEncoder::InitEncode(const VideoCodec* codec_settings, | 200 int H264VideoToolboxEncoder::InitEncode(const VideoCodec* codec_settings, |
218 int number_of_cores, | 201 int number_of_cores, |
219 size_t max_payload_size) { | 202 size_t max_payload_size) { |
220 RTC_DCHECK(codec_settings); | 203 RTC_DCHECK(codec_settings); |
221 RTC_DCHECK_EQ(codec_settings->codecType, kVideoCodecH264); | 204 RTC_DCHECK_EQ(codec_settings->codecType, kVideoCodecH264); |
222 // TODO(tkchin): We may need to enforce width/height dimension restrictions | 205 // TODO(tkchin): We may need to enforce width/height dimension restrictions |
223 // to match what the encoder supports. | 206 // to match what the encoder supports. |
224 width_ = codec_settings->width; | 207 width_ = codec_settings->width; |
225 height_ = codec_settings->height; | 208 height_ = codec_settings->height; |
226 // We can only set average bitrate on the HW encoder. | 209 // We can only set average bitrate on the HW encoder. |
227 bitrate_ = codec_settings->startBitrate * 1000; | 210 bitrate_kbit_ = codec_settings->startBitrate; |
211 bitrate_adjuster_.SetTargetBitrate(bitrate_kbit_); | |
228 | 212 |
229 // TODO(tkchin): Try setting payload size via | 213 // TODO(tkchin): Try setting payload size via |
230 // kVTCompressionPropertyKey_MaxH264SliceBytes. | 214 // kVTCompressionPropertyKey_MaxH264SliceBytes. |
231 | 215 |
232 return ResetCompressionSession(); | 216 return ResetCompressionSession(); |
233 } | 217 } |
234 | 218 |
235 int H264VideoToolboxEncoder::Encode( | 219 int H264VideoToolboxEncoder::Encode( |
236 const VideoFrame& input_image, | 220 const VideoFrame& input_image, |
237 const CodecSpecificInfo* codec_specific_info, | 221 const CodecSpecificInfo* codec_specific_info, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 CMTime presentation_time_stamp = | 264 CMTime presentation_time_stamp = |
281 CMTimeMake(input_image.render_time_ms(), 1000); | 265 CMTimeMake(input_image.render_time_ms(), 1000); |
282 CFDictionaryRef frame_properties = nullptr; | 266 CFDictionaryRef frame_properties = nullptr; |
283 if (is_keyframe_required) { | 267 if (is_keyframe_required) { |
284 CFTypeRef keys[] = {kVTEncodeFrameOptionKey_ForceKeyFrame}; | 268 CFTypeRef keys[] = {kVTEncodeFrameOptionKey_ForceKeyFrame}; |
285 CFTypeRef values[] = {kCFBooleanTrue}; | 269 CFTypeRef values[] = {kCFBooleanTrue}; |
286 frame_properties = internal::CreateCFDictionary(keys, values, 1); | 270 frame_properties = internal::CreateCFDictionary(keys, values, 1); |
287 } | 271 } |
288 rtc::scoped_ptr<internal::FrameEncodeParams> encode_params; | 272 rtc::scoped_ptr<internal::FrameEncodeParams> encode_params; |
289 encode_params.reset(new internal::FrameEncodeParams( | 273 encode_params.reset(new internal::FrameEncodeParams( |
290 callback_, codec_specific_info, width_, height_, | 274 this, codec_specific_info, width_, height_, input_image.render_time_ms(), |
291 input_image.render_time_ms(), input_image.timestamp())); | 275 input_image.timestamp())); |
292 VTCompressionSessionEncodeFrame( | 276 VTCompressionSessionEncodeFrame( |
293 compression_session_, pixel_buffer, presentation_time_stamp, | 277 compression_session_, pixel_buffer, presentation_time_stamp, |
294 kCMTimeInvalid, frame_properties, encode_params.release(), nullptr); | 278 kCMTimeInvalid, frame_properties, encode_params.release(), nullptr); |
295 if (frame_properties) { | 279 if (frame_properties) { |
296 CFRelease(frame_properties); | 280 CFRelease(frame_properties); |
297 } | 281 } |
298 if (pixel_buffer) { | 282 if (pixel_buffer) { |
299 CVBufferRelease(pixel_buffer); | 283 CVBufferRelease(pixel_buffer); |
300 } | 284 } |
301 return WEBRTC_VIDEO_CODEC_OK; | 285 return WEBRTC_VIDEO_CODEC_OK; |
302 } | 286 } |
303 | 287 |
304 int H264VideoToolboxEncoder::RegisterEncodeCompleteCallback( | 288 int H264VideoToolboxEncoder::RegisterEncodeCompleteCallback( |
305 EncodedImageCallback* callback) { | 289 EncodedImageCallback* callback) { |
306 callback_ = callback; | 290 callback_ = callback; |
307 return WEBRTC_VIDEO_CODEC_OK; | 291 return WEBRTC_VIDEO_CODEC_OK; |
308 } | 292 } |
309 | 293 |
310 int H264VideoToolboxEncoder::SetChannelParameters(uint32_t packet_loss, | 294 int H264VideoToolboxEncoder::SetChannelParameters(uint32_t packet_loss, |
311 int64_t rtt) { | 295 int64_t rtt) { |
312 // Encoder doesn't know anything about packet loss or rtt so just return. | 296 // Encoder doesn't know anything about packet loss or rtt so just return. |
313 return WEBRTC_VIDEO_CODEC_OK; | 297 return WEBRTC_VIDEO_CODEC_OK; |
314 } | 298 } |
315 | 299 |
316 int H264VideoToolboxEncoder::SetRates(uint32_t new_bitrate_kbit, | 300 int H264VideoToolboxEncoder::SetRates(uint32_t new_bitrate_kbit, |
317 uint32_t frame_rate) { | 301 uint32_t frame_rate) { |
318 bitrate_ = new_bitrate_kbit * 1000; | 302 bitrate_kbit_ = new_bitrate_kbit; |
319 if (compression_session_) { | 303 bitrate_adjuster_.SetTargetBitrate(new_bitrate_kbit); |
320 internal::SetVTSessionProperty(compression_session_, | 304 SetBitrate(bitrate_kbit_); |
321 kVTCompressionPropertyKey_AverageBitRate, | 305 |
322 bitrate_); | |
323 } | |
324 return WEBRTC_VIDEO_CODEC_OK; | 306 return WEBRTC_VIDEO_CODEC_OK; |
325 } | 307 } |
326 | 308 |
327 int H264VideoToolboxEncoder::Release() { | 309 int H264VideoToolboxEncoder::Release() { |
310 // Need to reset to that the session is invalidated and won't use the | |
sprang
2016/02/09 10:46:26
s/to that/so that
tkchin_webrtc
2016/02/11 00:03:21
Done.
| |
311 // callback anymore. Frames may continue to vend until it is torn down | |
sprang
2016/02/09 10:46:26
s/vend/send
tkchin_webrtc
2016/02/11 00:03:21
Done.
| |
312 // so we remove the callback after. | |
313 int ret = ResetCompressionSession(); | |
328 callback_ = nullptr; | 314 callback_ = nullptr; |
329 // Need to reset to that the session is invalidated and won't use the | 315 return ret; |
330 // callback anymore. | |
331 return ResetCompressionSession(); | |
332 } | 316 } |
333 | 317 |
334 int H264VideoToolboxEncoder::ResetCompressionSession() { | 318 int H264VideoToolboxEncoder::ResetCompressionSession() { |
335 DestroyCompressionSession(); | 319 DestroyCompressionSession(); |
336 | 320 |
337 // Set source image buffer attributes. These attributes will be present on | 321 // Set source image buffer attributes. These attributes will be present on |
338 // buffers retrieved from the encoder's pixel buffer pool. | 322 // buffers retrieved from the encoder's pixel buffer pool. |
339 const size_t attributes_size = 3; | 323 const size_t attributes_size = 3; |
340 CFTypeRef keys[attributes_size] = { | 324 CFTypeRef keys[attributes_size] = { |
341 #if defined(WEBRTC_IOS) | 325 #if defined(WEBRTC_IOS) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
382 return WEBRTC_VIDEO_CODEC_OK; | 366 return WEBRTC_VIDEO_CODEC_OK; |
383 } | 367 } |
384 | 368 |
385 void H264VideoToolboxEncoder::ConfigureCompressionSession() { | 369 void H264VideoToolboxEncoder::ConfigureCompressionSession() { |
386 RTC_DCHECK(compression_session_); | 370 RTC_DCHECK(compression_session_); |
387 internal::SetVTSessionProperty(compression_session_, | 371 internal::SetVTSessionProperty(compression_session_, |
388 kVTCompressionPropertyKey_RealTime, true); | 372 kVTCompressionPropertyKey_RealTime, true); |
389 internal::SetVTSessionProperty(compression_session_, | 373 internal::SetVTSessionProperty(compression_session_, |
390 kVTCompressionPropertyKey_ProfileLevel, | 374 kVTCompressionPropertyKey_ProfileLevel, |
391 kVTProfileLevel_H264_Baseline_AutoLevel); | 375 kVTProfileLevel_H264_Baseline_AutoLevel); |
392 internal::SetVTSessionProperty( | |
393 compression_session_, kVTCompressionPropertyKey_AverageBitRate, bitrate_); | |
394 internal::SetVTSessionProperty(compression_session_, | 376 internal::SetVTSessionProperty(compression_session_, |
395 kVTCompressionPropertyKey_AllowFrameReordering, | 377 kVTCompressionPropertyKey_AllowFrameReordering, |
396 false); | 378 false); |
379 SetBitrate(bitrate_kbit_); | |
397 // TODO(tkchin): Look at entropy mode and colorspace matrices. | 380 // TODO(tkchin): Look at entropy mode and colorspace matrices. |
398 // TODO(tkchin): Investigate to see if there's any way to make this work. | 381 // TODO(tkchin): Investigate to see if there's any way to make this work. |
399 // May need it to interop with Android. Currently this call just fails. | 382 // May need it to interop with Android. Currently this call just fails. |
400 // On inspecting encoder output on iOS8, this value is set to 6. | 383 // On inspecting encoder output on iOS8, this value is set to 6. |
401 // internal::SetVTSessionProperty(compression_session_, | 384 // internal::SetVTSessionProperty(compression_session_, |
402 // kVTCompressionPropertyKey_MaxFrameDelayCount, | 385 // kVTCompressionPropertyKey_MaxFrameDelayCount, |
403 // 1); | 386 // 1); |
404 // TODO(tkchin): See if enforcing keyframe frequency is beneficial in any | 387 // TODO(tkchin): See if enforcing keyframe frequency is beneficial in any |
405 // way. | 388 // way. |
406 // internal::SetVTSessionProperty( | 389 // internal::SetVTSessionProperty( |
407 // compression_session_, | 390 // compression_session_, |
408 // kVTCompressionPropertyKey_MaxKeyFrameInterval, 240); | 391 // kVTCompressionPropertyKey_MaxKeyFrameInterval, 240); |
409 // internal::SetVTSessionProperty( | 392 // internal::SetVTSessionProperty( |
410 // compression_session_, | 393 // compression_session_, |
411 // kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, 240); | 394 // kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, 240); |
412 } | 395 } |
413 | 396 |
414 void H264VideoToolboxEncoder::DestroyCompressionSession() { | 397 void H264VideoToolboxEncoder::DestroyCompressionSession() { |
415 if (compression_session_) { | 398 if (compression_session_) { |
416 VTCompressionSessionInvalidate(compression_session_); | 399 VTCompressionSessionInvalidate(compression_session_); |
417 CFRelease(compression_session_); | 400 CFRelease(compression_session_); |
418 compression_session_ = nullptr; | 401 compression_session_ = nullptr; |
419 } | 402 } |
420 } | 403 } |
421 | 404 |
422 const char* H264VideoToolboxEncoder::ImplementationName() const { | 405 const char* H264VideoToolboxEncoder::ImplementationName() const { |
423 return "VideoToolbox"; | 406 return "VideoToolbox"; |
424 } | 407 } |
425 | 408 |
409 void H264VideoToolboxEncoder::OnBitrateAdjusted( | |
410 uint32_t adjusted_bitrate_kbit) { | |
411 SetBitrate(adjusted_bitrate_kbit); | |
412 } | |
413 | |
414 void H264VideoToolboxEncoder::SetBitrate(uint32_t bitrate_kbit) { | |
415 if (compression_session_) { | |
416 internal::SetVTSessionProperty(compression_session_, | |
417 kVTCompressionPropertyKey_AverageBitRate, | |
418 bitrate_kbit * 1000); | |
419 } | |
420 } | |
421 | |
422 void H264VideoToolboxEncoder::OnEncodedFrame( | |
423 OSStatus status, | |
424 VTEncodeInfoFlags info_flags, | |
425 CMSampleBufferRef sample_buffer, | |
426 CodecSpecificInfo codec_specific_info, | |
427 int32_t width, | |
428 int32_t height, | |
429 int64_t render_time_ms, | |
430 uint32_t timestamp) { | |
431 if (status != noErr) { | |
432 LOG(LS_ERROR) << "H264 encode failed."; | |
433 return; | |
434 } | |
435 if (info_flags & kVTEncodeInfo_FrameDropped) { | |
436 LOG(LS_INFO) << "H264 encode dropped frame."; | |
437 } | |
438 | |
439 bool is_keyframe = false; | |
440 CFArrayRef attachments = | |
441 CMSampleBufferGetSampleAttachmentsArray(sample_buffer, 0); | |
442 if (attachments != nullptr && CFArrayGetCount(attachments)) { | |
443 CFDictionaryRef attachment = | |
444 static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(attachments, 0)); | |
445 is_keyframe = | |
446 !CFDictionaryContainsKey(attachment, kCMSampleAttachmentKey_NotSync); | |
447 } | |
448 | |
449 // Convert the sample buffer into a buffer suitable for RTP packetization. | |
450 // TODO(tkchin): Allocate buffers through a pool. | |
451 rtc::scoped_ptr<rtc::Buffer> buffer(new rtc::Buffer()); | |
452 rtc::scoped_ptr<webrtc::RTPFragmentationHeader> header; | |
453 if (!H264CMSampleBufferToAnnexBBuffer(sample_buffer, is_keyframe, | |
454 buffer.get(), header.accept())) { | |
455 return; | |
456 } | |
457 webrtc::EncodedImage frame(buffer->data(), buffer->size(), buffer->size()); | |
458 frame._encodedWidth = width; | |
459 frame._encodedHeight = height; | |
460 frame._completeFrame = true; | |
461 frame._frameType = | |
462 is_keyframe ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta; | |
463 frame.capture_time_ms_ = render_time_ms; | |
464 frame._timeStamp = timestamp; | |
465 | |
466 int result = callback_->Encoded(frame, &(codec_specific_info), header.get()); | |
467 if (result != 0) { | |
468 LOG(LS_ERROR) << "Encode callback failed: " << result; | |
469 return; | |
470 } | |
471 bitrate_adjuster_.Update(frame._size); | |
472 } | |
473 | |
426 } // namespace webrtc | 474 } // namespace webrtc |
427 | 475 |
428 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) | 476 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
OLD | NEW |