Add a webrtc{en,de}coderfactory implementation for VideoToolbox

This CL removes the coupling of the VideoToolbox h264 implementation
to the generic h264 code. The files have been moved into sdb/obj/Framework
and all dependency on them has been removed from the rest of WebRTC.
We now add it as an external encoder via a factory supplied to the
CreatePeerConnectionFactory call. This also brings the iOS implementation
closer to what we do on Android for MediaCodec.

BUG=webrtc:6619

Review-Url: https://codereview.webrtc.org/2463313002
Cr-Commit-Position: refs/heads/master@{#14953}
This commit is contained in:
kthelgason
2016-11-07 07:26:00 -08:00
committed by Commit bot
parent 3babb99039
commit 6a5047dad3
17 changed files with 239 additions and 212 deletions

View File

@ -21,6 +21,8 @@
#import "RTCVideoTrack+Private.h"
#import "WebRTC/RTCLogging.h"
#include "videotoolboxvideocodecfactory.h"
@implementation RTCPeerConnectionFactory {
std::unique_ptr<rtc::Thread> _networkThread;
std::unique_ptr<rtc::Thread> _workerThread;
@ -44,9 +46,14 @@
result = _signalingThread->Start();
NSAssert(result, @"Failed to start signaling thread.");
const auto encoder_factory = new webrtc::VideoToolboxVideoEncoderFactory();
const auto decoder_factory = new webrtc::VideoToolboxVideoDecoderFactory();
// Ownership of encoder/decoder factories is passed on to the
// peerconnectionfactory, that handles deleting them.
_nativeFactory = webrtc::CreatePeerConnectionFactory(
_networkThread.get(), _workerThread.get(), _signalingThread.get(),
nullptr, nullptr, nullptr);
nullptr, encoder_factory, decoder_factory);
NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
}
return self;

View File

@ -0,0 +1,277 @@
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*
*/
#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_decoder.h"
#include <memory>
#if defined(WEBRTC_IOS)
#include "RTCUIApplication.h"
#endif
#include "libyuv/convert.h"
#include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
#include "webrtc/common_video/include/corevideo_frame_buffer.h"
#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h"
#include "webrtc/video_frame.h"
namespace internal {
static const int64_t kMsPerSec = 1000;
// Convenience function for creating a dictionary.
inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys,
CFTypeRef* values,
size_t size) {
return CFDictionaryCreate(nullptr, keys, values, size,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
}
// Struct that we pass to the decoder per frame to decode. We receive it again
// in the decoder callback.
struct FrameDecodeParams {
FrameDecodeParams(webrtc::DecodedImageCallback* cb, int64_t ts)
: callback(cb), timestamp(ts) {}
webrtc::DecodedImageCallback* callback;
int64_t timestamp;
};
// This is the callback function that VideoToolbox calls when decode is
// complete.
void VTDecompressionOutputCallback(void* decoder,
void* params,
OSStatus status,
VTDecodeInfoFlags info_flags,
CVImageBufferRef image_buffer,
CMTime timestamp,
CMTime duration) {
std::unique_ptr<FrameDecodeParams> decode_params(
reinterpret_cast<FrameDecodeParams*>(params));
if (status != noErr) {
LOG(LS_ERROR) << "Failed to decode frame. Status: " << status;
return;
}
// TODO(tkchin): Handle CVO properly.
rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
new rtc::RefCountedObject<webrtc::CoreVideoFrameBuffer>(image_buffer);
webrtc::VideoFrame decoded_frame(buffer, decode_params->timestamp,
CMTimeGetSeconds(timestamp) * kMsPerSec,
webrtc::kVideoRotation_0);
decode_params->callback->Decoded(decoded_frame);
}
} // namespace internal
namespace webrtc {
H264VideoToolboxDecoder::H264VideoToolboxDecoder()
: callback_(nullptr),
video_format_(nullptr),
decompression_session_(nullptr) {}
H264VideoToolboxDecoder::~H264VideoToolboxDecoder() {
DestroyDecompressionSession();
SetVideoFormat(nullptr);
}
int H264VideoToolboxDecoder::InitDecode(const VideoCodec* video_codec,
int number_of_cores) {
return WEBRTC_VIDEO_CODEC_OK;
}
int H264VideoToolboxDecoder::Decode(
const EncodedImage& input_image,
bool missing_frames,
const RTPFragmentationHeader* fragmentation,
const CodecSpecificInfo* codec_specific_info,
int64_t render_time_ms) {
RTC_DCHECK(input_image._buffer);
#if defined(WEBRTC_IOS)
if (!RTCIsUIApplicationActive()) {
// Ignore all decode requests when app isn't active. In this state, the
// hardware decoder has been invalidated by the OS.
// Reset video format so that we won't process frames until the next
// keyframe.
SetVideoFormat(nullptr);
return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
}
#endif
CMVideoFormatDescriptionRef input_format = nullptr;
if (H264AnnexBBufferHasVideoFormatDescription(input_image._buffer,
input_image._length)) {
input_format = CreateVideoFormatDescription(input_image._buffer,
input_image._length);
if (input_format) {
// Check if the video format has changed, and reinitialize decoder if
// needed.
if (!CMFormatDescriptionEqual(input_format, video_format_)) {
SetVideoFormat(input_format);
ResetDecompressionSession();
}
CFRelease(input_format);
}
}
if (!video_format_) {
// We received a frame but we don't have format information so we can't
// decode it.
// This can happen after backgrounding. We need to wait for the next
// sps/pps before we can resume so we request a keyframe by returning an
// error.
LOG(LS_WARNING) << "Missing video format. Frame with sps/pps required.";
return WEBRTC_VIDEO_CODEC_ERROR;
}
CMSampleBufferRef sample_buffer = nullptr;
if (!H264AnnexBBufferToCMSampleBuffer(input_image._buffer,
input_image._length, video_format_,
&sample_buffer)) {
return WEBRTC_VIDEO_CODEC_ERROR;
}
RTC_DCHECK(sample_buffer);
VTDecodeFrameFlags decode_flags =
kVTDecodeFrame_EnableAsynchronousDecompression;
std::unique_ptr<internal::FrameDecodeParams> frame_decode_params;
frame_decode_params.reset(
new internal::FrameDecodeParams(callback_, input_image._timeStamp));
OSStatus status = VTDecompressionSessionDecodeFrame(
decompression_session_, sample_buffer, decode_flags,
frame_decode_params.release(), nullptr);
#if defined(WEBRTC_IOS)
// Re-initialize the decoder if we have an invalid session while the app is
// active and retry the decode request.
if (status == kVTInvalidSessionErr &&
ResetDecompressionSession() == WEBRTC_VIDEO_CODEC_OK) {
frame_decode_params.reset(
new internal::FrameDecodeParams(callback_, input_image._timeStamp));
status = VTDecompressionSessionDecodeFrame(
decompression_session_, sample_buffer, decode_flags,
frame_decode_params.release(), nullptr);
}
#endif
CFRelease(sample_buffer);
if (status != noErr) {
LOG(LS_ERROR) << "Failed to decode frame with code: " << status;
return WEBRTC_VIDEO_CODEC_ERROR;
}
return WEBRTC_VIDEO_CODEC_OK;
}
int H264VideoToolboxDecoder::RegisterDecodeCompleteCallback(
DecodedImageCallback* callback) {
RTC_DCHECK(!callback_);
callback_ = callback;
return WEBRTC_VIDEO_CODEC_OK;
}
int H264VideoToolboxDecoder::Release() {
// Need to invalidate the session so that callbacks no longer occur and it
// is safe to null out the callback.
DestroyDecompressionSession();
SetVideoFormat(nullptr);
callback_ = nullptr;
return WEBRTC_VIDEO_CODEC_OK;
}
int H264VideoToolboxDecoder::ResetDecompressionSession() {
DestroyDecompressionSession();
// Need to wait for the first SPS to initialize decoder.
if (!video_format_) {
return WEBRTC_VIDEO_CODEC_OK;
}
// Set keys for OpenGL and IOSurface compatibilty, which makes the encoder
// create pixel buffers with GPU backed memory. The intent here is to pass
// the pixel buffers directly so we avoid a texture upload later during
// rendering. This currently is moot because we are converting back to an
// I420 frame after decode, but eventually we will be able to plumb
// CVPixelBuffers directly to the renderer.
// TODO(tkchin): Maybe only set OpenGL/IOSurface keys if we know that that
// we can pass CVPixelBuffers as native handles in decoder output.
static size_t const attributes_size = 3;
CFTypeRef keys[attributes_size] = {
#if defined(WEBRTC_IOS)
kCVPixelBufferOpenGLESCompatibilityKey,
#elif defined(WEBRTC_MAC)
kCVPixelBufferOpenGLCompatibilityKey,
#endif
kCVPixelBufferIOSurfacePropertiesKey,
kCVPixelBufferPixelFormatTypeKey
};
CFDictionaryRef io_surface_value =
internal::CreateCFDictionary(nullptr, nullptr, 0);
int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
CFNumberRef pixel_format =
CFNumberCreate(nullptr, kCFNumberLongType, &nv12type);
CFTypeRef values[attributes_size] = {kCFBooleanTrue, io_surface_value,
pixel_format};
CFDictionaryRef attributes =
internal::CreateCFDictionary(keys, values, attributes_size);
if (io_surface_value) {
CFRelease(io_surface_value);
io_surface_value = nullptr;
}
if (pixel_format) {
CFRelease(pixel_format);
pixel_format = nullptr;
}
VTDecompressionOutputCallbackRecord record = {
internal::VTDecompressionOutputCallback, this,
};
OSStatus status =
VTDecompressionSessionCreate(nullptr, video_format_, nullptr, attributes,
&record, &decompression_session_);
CFRelease(attributes);
if (status != noErr) {
DestroyDecompressionSession();
return WEBRTC_VIDEO_CODEC_ERROR;
}
ConfigureDecompressionSession();
return WEBRTC_VIDEO_CODEC_OK;
}
void H264VideoToolboxDecoder::ConfigureDecompressionSession() {
RTC_DCHECK(decompression_session_);
#if defined(WEBRTC_IOS)
VTSessionSetProperty(decompression_session_,
kVTDecompressionPropertyKey_RealTime, kCFBooleanTrue);
#endif
}
void H264VideoToolboxDecoder::DestroyDecompressionSession() {
if (decompression_session_) {
VTDecompressionSessionInvalidate(decompression_session_);
CFRelease(decompression_session_);
decompression_session_ = nullptr;
}
}
void H264VideoToolboxDecoder::SetVideoFormat(
CMVideoFormatDescriptionRef video_format) {
if (video_format_ == video_format) {
return;
}
if (video_format_) {
CFRelease(video_format_);
}
video_format_ = video_format;
if (video_format_) {
CFRetain(video_format_);
}
}
const char* H264VideoToolboxDecoder::ImplementationName() const {
return "VideoToolbox";
}
} // namespace webrtc

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*
*/
#ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_DECODER_H_
#define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_DECODER_H_
#include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
#include <VideoToolbox/VideoToolbox.h>
// This file provides a H264 encoder implementation using the VideoToolbox
// APIs. Since documentation is almost non-existent, this is largely based on
// the information in the VideoToolbox header files, a talk from WWDC 2014 and
// experimentation.
namespace webrtc {
class H264VideoToolboxDecoder : public H264Decoder {
public:
H264VideoToolboxDecoder();
~H264VideoToolboxDecoder() override;
int InitDecode(const VideoCodec* video_codec, int number_of_cores) override;
int Decode(const EncodedImage& input_image,
bool missing_frames,
const RTPFragmentationHeader* fragmentation,
const CodecSpecificInfo* codec_specific_info,
int64_t render_time_ms) override;
int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override;
int Release() override;
const char* ImplementationName() const override;
private:
int ResetDecompressionSession();
void ConfigureDecompressionSession();
void DestroyDecompressionSession();
void SetVideoFormat(CMVideoFormatDescriptionRef video_format);
DecodedImageCallback* callback_;
CMVideoFormatDescriptionRef video_format_;
VTDecompressionSessionRef decompression_session_;
}; // H264VideoToolboxDecoder
} // namespace webrtc
#endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_DECODER_H_

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*
*/
#ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_ENCODER_H_
#define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_ENCODER_H_
#include "webrtc/base/criticalsection.h"
#include "webrtc/common_video/h264/h264_bitstream_parser.h"
#include "webrtc/common_video/include/bitrate_adjuster.h"
#include "webrtc/common_video/rotation.h"
#include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
#include "webrtc/modules/video_coding/utility/quality_scaler.h"
#include <VideoToolbox/VideoToolbox.h>
#include <vector>
// This file provides a H264 encoder implementation using the VideoToolbox
// APIs. Since documentation is almost non-existent, this is largely based on
// the information in the VideoToolbox header files, a talk from WWDC 2014 and
// experimentation.
namespace webrtc {
class H264VideoToolboxEncoder : public H264Encoder {
public:
H264VideoToolboxEncoder();
~H264VideoToolboxEncoder() override;
int InitEncode(const VideoCodec* codec_settings,
int number_of_cores,
size_t max_payload_size) override;
int Encode(const VideoFrame& input_image,
const CodecSpecificInfo* codec_specific_info,
const std::vector<FrameType>* frame_types) override;
int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
void OnDroppedFrame() override;
int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
int SetRates(uint32_t new_bitrate_kbit, uint32_t frame_rate) override;
int Release() override;
const char* ImplementationName() const override;
bool SupportsNativeHandle() const override;
void OnEncodedFrame(OSStatus status,
VTEncodeInfoFlags info_flags,
CMSampleBufferRef sample_buffer,
CodecSpecificInfo codec_specific_info,
int32_t width,
int32_t height,
int64_t render_time_ms,
uint32_t timestamp,
VideoRotation rotation);
private:
int ResetCompressionSession();
void ConfigureCompressionSession();
void DestroyCompressionSession();
rtc::scoped_refptr<VideoFrameBuffer> GetScaledBufferOnEncode(
const rtc::scoped_refptr<VideoFrameBuffer>& frame);
void SetBitrateBps(uint32_t bitrate_bps);
void SetEncoderBitrateBps(uint32_t bitrate_bps);
EncodedImageCallback* callback_;
VTCompressionSessionRef compression_session_;
BitrateAdjuster bitrate_adjuster_;
uint32_t target_bitrate_bps_;
uint32_t encoder_bitrate_bps_;
int32_t width_;
int32_t height_;
rtc::CriticalSection quality_scaler_crit_;
QualityScaler quality_scaler_ GUARDED_BY(quality_scaler_crit_);
H264BitstreamParser h264_bitstream_parser_;
bool enable_scaling_;
std::vector<uint8_t> nv12_scale_buffer_;
}; // H264VideoToolboxEncoder
} // namespace webrtc
#endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_ENCODER_H_

View File

@ -0,0 +1,660 @@
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*
*/
#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_encoder.h"
#include <memory>
#include <string>
#include <vector>
#if defined(WEBRTC_IOS)
#import "WebRTC/UIDevice+RTCDevice.h"
#include "RTCUIApplication.h"
#endif
#include "libyuv/convert_from.h"
#include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
#include "webrtc/common_video/include/corevideo_frame_buffer.h"
#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h"
#include "webrtc/system_wrappers/include/clock.h"
namespace internal {
// The ratio between kVTCompressionPropertyKey_DataRateLimits and
// kVTCompressionPropertyKey_AverageBitRate. The data rate limit is set higher
// than the average bit rate to avoid undershooting the target.
const float kLimitToAverageBitRateFactor = 1.5f;
// These thresholds deviate from the default h264 QP thresholds, as they
// have been found to work better on devices that support VideoToolbox
const int kLowH264QpThreshold = 28;
const int kHighH264QpThreshold = 39;
// Convenience function for creating a dictionary.
inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys,
CFTypeRef* values,
size_t size) {
return CFDictionaryCreate(kCFAllocatorDefault, keys, values, size,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
}
// Copies characters from a CFStringRef into a std::string.
std::string CFStringToString(const CFStringRef cf_string) {
RTC_DCHECK(cf_string);
std::string std_string;
// Get the size needed for UTF8 plus terminating character.
size_t buffer_size =
CFStringGetMaximumSizeForEncoding(CFStringGetLength(cf_string),
kCFStringEncodingUTF8) +
1;
std::unique_ptr<char[]> buffer(new char[buffer_size]);
if (CFStringGetCString(cf_string, buffer.get(), buffer_size,
kCFStringEncodingUTF8)) {
// Copy over the characters.
std_string.assign(buffer.get());
}
return std_string;
}
// Convenience function for setting a VT property.
void SetVTSessionProperty(VTSessionRef session,
CFStringRef key,
int32_t value) {
CFNumberRef cfNum =
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
OSStatus status = VTSessionSetProperty(session, key, cfNum);
CFRelease(cfNum);
if (status != noErr) {
std::string key_string = CFStringToString(key);
LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
<< " to " << value << ": " << status;
}
}
// Convenience function for setting a VT property.
void SetVTSessionProperty(VTSessionRef session,
CFStringRef key,
uint32_t value) {
int64_t value_64 = value;
CFNumberRef cfNum =
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value_64);
OSStatus status = VTSessionSetProperty(session, key, cfNum);
CFRelease(cfNum);
if (status != noErr) {
std::string key_string = CFStringToString(key);
LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
<< " to " << value << ": " << status;
}
}
// Convenience function for setting a VT property.
void SetVTSessionProperty(VTSessionRef session, CFStringRef key, bool value) {
CFBooleanRef cf_bool = (value) ? kCFBooleanTrue : kCFBooleanFalse;
OSStatus status = VTSessionSetProperty(session, key, cf_bool);
if (status != noErr) {
std::string key_string = CFStringToString(key);
LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
<< " to " << value << ": " << status;
}
}
// Convenience function for setting a VT property.
void SetVTSessionProperty(VTSessionRef session,
CFStringRef key,
CFStringRef value) {
OSStatus status = VTSessionSetProperty(session, key, value);
if (status != noErr) {
std::string key_string = CFStringToString(key);
std::string val_string = CFStringToString(value);
LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
<< " to " << val_string << ": " << status;
}
}
// Struct that we pass to the encoder per frame to encode. We receive it again
// in the encoder callback.
struct FrameEncodeParams {
FrameEncodeParams(webrtc::H264VideoToolboxEncoder* e,
const webrtc::CodecSpecificInfo* csi,
int32_t w,
int32_t h,
int64_t rtms,
uint32_t ts,
webrtc::VideoRotation r)
: encoder(e),
width(w),
height(h),
render_time_ms(rtms),
timestamp(ts),
rotation(r) {
if (csi) {
codec_specific_info = *csi;
} else {
codec_specific_info.codecType = webrtc::kVideoCodecH264;
}
}
webrtc::H264VideoToolboxEncoder* encoder;
webrtc::CodecSpecificInfo codec_specific_info;
int32_t width;
int32_t height;
int64_t render_time_ms;
uint32_t timestamp;
webrtc::VideoRotation rotation;
};
// We receive I420Frames as input, but we need to feed CVPixelBuffers into the
// encoder. This performs the copy and format conversion.
// TODO(tkchin): See if encoder will accept i420 frames and compare performance.
bool CopyVideoFrameToPixelBuffer(
const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& frame,
CVPixelBufferRef pixel_buffer) {
RTC_DCHECK(pixel_buffer);
RTC_DCHECK_EQ(CVPixelBufferGetPixelFormatType(pixel_buffer),
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange);
RTC_DCHECK_EQ(CVPixelBufferGetHeightOfPlane(pixel_buffer, 0),
static_cast<size_t>(frame->height()));
RTC_DCHECK_EQ(CVPixelBufferGetWidthOfPlane(pixel_buffer, 0),
static_cast<size_t>(frame->width()));
CVReturn cvRet = CVPixelBufferLockBaseAddress(pixel_buffer, 0);
if (cvRet != kCVReturnSuccess) {
LOG(LS_ERROR) << "Failed to lock base address: " << cvRet;
return false;
}
uint8_t* dst_y = reinterpret_cast<uint8_t*>(
CVPixelBufferGetBaseAddressOfPlane(pixel_buffer, 0));
int dst_stride_y = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer, 0);
uint8_t* dst_uv = reinterpret_cast<uint8_t*>(
CVPixelBufferGetBaseAddressOfPlane(pixel_buffer, 1));
int dst_stride_uv = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer, 1);
// Convert I420 to NV12.
int ret = libyuv::I420ToNV12(
frame->DataY(), frame->StrideY(),
frame->DataU(), frame->StrideU(),
frame->DataV(), frame->StrideV(),
dst_y, dst_stride_y, dst_uv, dst_stride_uv,
frame->width(), frame->height());
CVPixelBufferUnlockBaseAddress(pixel_buffer, 0);
if (ret) {
LOG(LS_ERROR) << "Error converting I420 VideoFrame to NV12 :" << ret;
return false;
}
return true;
}
CVPixelBufferRef CreatePixelBuffer(CVPixelBufferPoolRef pixel_buffer_pool) {
if (!pixel_buffer_pool) {
LOG(LS_ERROR) << "Failed to get pixel buffer pool.";
return nullptr;
}
CVPixelBufferRef pixel_buffer;
CVReturn ret = CVPixelBufferPoolCreatePixelBuffer(nullptr, pixel_buffer_pool,
&pixel_buffer);
if (ret != kCVReturnSuccess) {
LOG(LS_ERROR) << "Failed to create pixel buffer: " << ret;
// We probably want to drop frames here, since failure probably means
// that the pool is empty.
return nullptr;
}
return pixel_buffer;
}
// This is the callback function that VideoToolbox calls when encode is
// complete. From inspection this happens on its own queue.
void VTCompressionOutputCallback(void* encoder,
void* params,
OSStatus status,
VTEncodeInfoFlags info_flags,
CMSampleBufferRef sample_buffer) {
std::unique_ptr<FrameEncodeParams> encode_params(
reinterpret_cast<FrameEncodeParams*>(params));
encode_params->encoder->OnEncodedFrame(
status, info_flags, sample_buffer, encode_params->codec_specific_info,
encode_params->width, encode_params->height,
encode_params->render_time_ms, encode_params->timestamp,
encode_params->rotation);
}
} // namespace internal
namespace webrtc {
// .5 is set as a mininum to prevent overcompensating for large temporary
// overshoots. We don't want to degrade video quality too badly.
// .95 is set to prevent oscillations. When a lower bitrate is set on the
// encoder than previously set, its output seems to have a brief period of
// drastically reduced bitrate, so we want to avoid that. In steady state
// conditions, 0.95 seems to give us better overall bitrate over long periods
// of time.
H264VideoToolboxEncoder::H264VideoToolboxEncoder()
: callback_(nullptr),
compression_session_(nullptr),
bitrate_adjuster_(Clock::GetRealTimeClock(), .5, .95) {
}
H264VideoToolboxEncoder::~H264VideoToolboxEncoder() {
DestroyCompressionSession();
}
int H264VideoToolboxEncoder::InitEncode(const VideoCodec* codec_settings,
int number_of_cores,
size_t max_payload_size) {
RTC_DCHECK(codec_settings);
RTC_DCHECK_EQ(codec_settings->codecType, kVideoCodecH264);
{
rtc::CritScope lock(&quality_scaler_crit_);
quality_scaler_.Init(internal::kLowH264QpThreshold,
internal::kHighH264QpThreshold,
codec_settings->startBitrate, codec_settings->width,
codec_settings->height, codec_settings->maxFramerate);
QualityScaler::Resolution res = quality_scaler_.GetScaledResolution();
// TODO(tkchin): We may need to enforce width/height dimension restrictions
// to match what the encoder supports.
width_ = res.width;
height_ = res.height;
}
// We can only set average bitrate on the HW encoder.
target_bitrate_bps_ = codec_settings->startBitrate;
bitrate_adjuster_.SetTargetBitrateBps(target_bitrate_bps_);
// TODO(tkchin): Try setting payload size via
// kVTCompressionPropertyKey_MaxH264SliceBytes.
return ResetCompressionSession();
}
int H264VideoToolboxEncoder::Encode(
const VideoFrame& frame,
const CodecSpecificInfo* codec_specific_info,
const std::vector<FrameType>* frame_types) {
RTC_DCHECK(!frame.IsZeroSize());
if (!callback_ || !compression_session_) {
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
#if defined(WEBRTC_IOS)
if (!RTCIsUIApplicationActive()) {
// Ignore all encode requests when app isn't active. In this state, the
// hardware encoder has been invalidated by the OS.
return WEBRTC_VIDEO_CODEC_OK;
}
#endif
bool is_keyframe_required = false;
quality_scaler_.OnEncodeFrame(frame.width(), frame.height());
const QualityScaler::Resolution scaled_res =
quality_scaler_.GetScaledResolution();
if (scaled_res.width != width_ || scaled_res.height != height_) {
width_ = scaled_res.width;
height_ = scaled_res.height;
int ret = ResetCompressionSession();
if (ret < 0)
return ret;
}
// Get a pixel buffer from the pool and copy frame data over.
CVPixelBufferPoolRef pixel_buffer_pool =
VTCompressionSessionGetPixelBufferPool(compression_session_);
#if defined(WEBRTC_IOS)
if (!pixel_buffer_pool) {
// Kind of a hack. On backgrounding, the compression session seems to get
// invalidated, which causes this pool call to fail when the application
// is foregrounded and frames are being sent for encoding again.
// Resetting the session when this happens fixes the issue.
// In addition we request a keyframe so video can recover quickly.
ResetCompressionSession();
pixel_buffer_pool =
VTCompressionSessionGetPixelBufferPool(compression_session_);
is_keyframe_required = true;
LOG(LS_INFO) << "Resetting compression session due to invalid pool.";
}
#endif
CVPixelBufferRef pixel_buffer = static_cast<CVPixelBufferRef>(
frame.video_frame_buffer()->native_handle());
if (pixel_buffer) {
// Native frame.
rtc::scoped_refptr<CoreVideoFrameBuffer> core_video_frame_buffer(
static_cast<CoreVideoFrameBuffer*>(frame.video_frame_buffer().get()));
if (!core_video_frame_buffer->RequiresCropping()) {
// This pixel buffer might have a higher resolution than what the
// compression session is configured to. The compression session can
// handle that and will output encoded frames in the configured
// resolution regardless of the input pixel buffer resolution.
CVBufferRetain(pixel_buffer);
} else {
// Cropping required, we need to crop and scale to a new pixel buffer.
pixel_buffer = internal::CreatePixelBuffer(pixel_buffer_pool);
if (!pixel_buffer) {
return WEBRTC_VIDEO_CODEC_ERROR;
}
if (!core_video_frame_buffer->CropAndScaleTo(&nv12_scale_buffer_,
pixel_buffer)) {
return WEBRTC_VIDEO_CODEC_ERROR;
}
}
} else {
pixel_buffer = internal::CreatePixelBuffer(pixel_buffer_pool);
if (!pixel_buffer) {
return WEBRTC_VIDEO_CODEC_ERROR;
}
// TODO(magjed): Optimize by merging scaling and NV12 pixel buffer
// conversion once libyuv::MergeUVPlanes is available.
rtc::scoped_refptr<VideoFrameBuffer> scaled_i420_buffer =
quality_scaler_.GetScaledBuffer(frame.video_frame_buffer());
if (!internal::CopyVideoFrameToPixelBuffer(scaled_i420_buffer,
pixel_buffer)) {
LOG(LS_ERROR) << "Failed to copy frame data.";
CVBufferRelease(pixel_buffer);
return WEBRTC_VIDEO_CODEC_ERROR;
}
}
// Check if we need a keyframe.
if (!is_keyframe_required && frame_types) {
for (auto frame_type : *frame_types) {
if (frame_type == kVideoFrameKey) {
is_keyframe_required = true;
break;
}
}
}
CMTime presentation_time_stamp =
CMTimeMake(frame.render_time_ms(), 1000);
CFDictionaryRef frame_properties = nullptr;
if (is_keyframe_required) {
CFTypeRef keys[] = {kVTEncodeFrameOptionKey_ForceKeyFrame};
CFTypeRef values[] = {kCFBooleanTrue};
frame_properties = internal::CreateCFDictionary(keys, values, 1);
}
std::unique_ptr<internal::FrameEncodeParams> encode_params;
encode_params.reset(new internal::FrameEncodeParams(
this, codec_specific_info, width_, height_, frame.render_time_ms(),
frame.timestamp(), frame.rotation()));
// Update the bitrate if needed.
SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps());
OSStatus status = VTCompressionSessionEncodeFrame(
compression_session_, pixel_buffer, presentation_time_stamp,
kCMTimeInvalid, frame_properties, encode_params.release(), nullptr);
if (frame_properties) {
CFRelease(frame_properties);
}
if (pixel_buffer) {
CVBufferRelease(pixel_buffer);
}
if (status != noErr) {
LOG(LS_ERROR) << "Failed to encode frame with code: " << status;
return WEBRTC_VIDEO_CODEC_ERROR;
}
return WEBRTC_VIDEO_CODEC_OK;
}
int H264VideoToolboxEncoder::RegisterEncodeCompleteCallback(
EncodedImageCallback* callback) {
callback_ = callback;
return WEBRTC_VIDEO_CODEC_OK;
}
void H264VideoToolboxEncoder::OnDroppedFrame() {
rtc::CritScope lock(&quality_scaler_crit_);
quality_scaler_.ReportDroppedFrame();
}
int H264VideoToolboxEncoder::SetChannelParameters(uint32_t packet_loss,
int64_t rtt) {
// Encoder doesn't know anything about packet loss or rtt so just return.
return WEBRTC_VIDEO_CODEC_OK;
}
int H264VideoToolboxEncoder::SetRates(uint32_t new_bitrate_kbit,
uint32_t frame_rate) {
target_bitrate_bps_ = 1000 * new_bitrate_kbit;
bitrate_adjuster_.SetTargetBitrateBps(target_bitrate_bps_);
SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps());
rtc::CritScope lock(&quality_scaler_crit_);
quality_scaler_.ReportFramerate(frame_rate);
return WEBRTC_VIDEO_CODEC_OK;
}
int H264VideoToolboxEncoder::Release() {
// Need to reset so that the session is invalidated and won't use the
// callback anymore. Do not remove callback until the session is invalidated
// since async encoder callbacks can occur until invalidation.
int ret = ResetCompressionSession();
callback_ = nullptr;
return ret;
}
int H264VideoToolboxEncoder::ResetCompressionSession() {
DestroyCompressionSession();
// Set source image buffer attributes. These attributes will be present on
// buffers retrieved from the encoder's pixel buffer pool.
const size_t attributes_size = 3;
CFTypeRef keys[attributes_size] = {
#if defined(WEBRTC_IOS)
kCVPixelBufferOpenGLESCompatibilityKey,
#elif defined(WEBRTC_MAC)
kCVPixelBufferOpenGLCompatibilityKey,
#endif
kCVPixelBufferIOSurfacePropertiesKey,
kCVPixelBufferPixelFormatTypeKey
};
CFDictionaryRef io_surface_value =
internal::CreateCFDictionary(nullptr, nullptr, 0);
int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
CFNumberRef pixel_format =
CFNumberCreate(nullptr, kCFNumberLongType, &nv12type);
CFTypeRef values[attributes_size] = {kCFBooleanTrue, io_surface_value,
pixel_format};
CFDictionaryRef source_attributes =
internal::CreateCFDictionary(keys, values, attributes_size);
if (io_surface_value) {
CFRelease(io_surface_value);
io_surface_value = nullptr;
}
if (pixel_format) {
CFRelease(pixel_format);
pixel_format = nullptr;
}
OSStatus status = VTCompressionSessionCreate(
nullptr, // use default allocator
width_, height_, kCMVideoCodecType_H264,
nullptr, // use default encoder
source_attributes,
nullptr, // use default compressed data allocator
internal::VTCompressionOutputCallback, this, &compression_session_);
if (source_attributes) {
CFRelease(source_attributes);
source_attributes = nullptr;
}
if (status != noErr) {
LOG(LS_ERROR) << "Failed to create compression session: " << status;
return WEBRTC_VIDEO_CODEC_ERROR;
}
ConfigureCompressionSession();
return WEBRTC_VIDEO_CODEC_OK;
}
void H264VideoToolboxEncoder::ConfigureCompressionSession() {
RTC_DCHECK(compression_session_);
internal::SetVTSessionProperty(compression_session_,
kVTCompressionPropertyKey_RealTime, true);
internal::SetVTSessionProperty(compression_session_,
kVTCompressionPropertyKey_ProfileLevel,
kVTProfileLevel_H264_Baseline_AutoLevel);
internal::SetVTSessionProperty(compression_session_,
kVTCompressionPropertyKey_AllowFrameReordering,
false);
SetEncoderBitrateBps(target_bitrate_bps_);
// TODO(tkchin): Look at entropy mode and colorspace matrices.
// TODO(tkchin): Investigate to see if there's any way to make this work.
// May need it to interop with Android. Currently this call just fails.
// On inspecting encoder output on iOS8, this value is set to 6.
// internal::SetVTSessionProperty(compression_session_,
// kVTCompressionPropertyKey_MaxFrameDelayCount,
// 1);
// Set a relatively large value for keyframe emission (7200 frames or
// 4 minutes).
internal::SetVTSessionProperty(
compression_session_,
kVTCompressionPropertyKey_MaxKeyFrameInterval, 7200);
internal::SetVTSessionProperty(
compression_session_,
kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, 240);
}
void H264VideoToolboxEncoder::DestroyCompressionSession() {
if (compression_session_) {
VTCompressionSessionInvalidate(compression_session_);
CFRelease(compression_session_);
compression_session_ = nullptr;
}
}
const char* H264VideoToolboxEncoder::ImplementationName() const {
return "VideoToolbox";
}
bool H264VideoToolboxEncoder::SupportsNativeHandle() const {
return true;
}
void H264VideoToolboxEncoder::SetBitrateBps(uint32_t bitrate_bps) {
if (encoder_bitrate_bps_ != bitrate_bps) {
SetEncoderBitrateBps(bitrate_bps);
}
}
void H264VideoToolboxEncoder::SetEncoderBitrateBps(uint32_t bitrate_bps) {
if (compression_session_) {
internal::SetVTSessionProperty(compression_session_,
kVTCompressionPropertyKey_AverageBitRate,
bitrate_bps);
// TODO(tkchin): Add a helper method to set array value.
int64_t data_limit_bytes_per_second_value = static_cast<int64_t>(
bitrate_bps * internal::kLimitToAverageBitRateFactor / 8);
CFNumberRef bytes_per_second =
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt64Type,
&data_limit_bytes_per_second_value);
int64_t one_second_value = 1;
CFNumberRef one_second =
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt64Type,
&one_second_value);
const void* nums[2] = { bytes_per_second, one_second };
CFArrayRef data_rate_limits =
CFArrayCreate(nullptr, nums, 2, &kCFTypeArrayCallBacks);
OSStatus status =
VTSessionSetProperty(compression_session_,
kVTCompressionPropertyKey_DataRateLimits,
data_rate_limits);
if (bytes_per_second) {
CFRelease(bytes_per_second);
}
if (one_second) {
CFRelease(one_second);
}
if (data_rate_limits) {
CFRelease(data_rate_limits);
}
if (status != noErr) {
LOG(LS_ERROR) << "Failed to set data rate limit";
}
encoder_bitrate_bps_ = bitrate_bps;
}
}
void H264VideoToolboxEncoder::OnEncodedFrame(
OSStatus status,
VTEncodeInfoFlags info_flags,
CMSampleBufferRef sample_buffer,
CodecSpecificInfo codec_specific_info,
int32_t width,
int32_t height,
int64_t render_time_ms,
uint32_t timestamp,
VideoRotation rotation) {
if (status != noErr) {
LOG(LS_ERROR) << "H264 encode failed.";
return;
}
if (info_flags & kVTEncodeInfo_FrameDropped) {
LOG(LS_INFO) << "H264 encode dropped frame.";
rtc::CritScope lock(&quality_scaler_crit_);
quality_scaler_.ReportDroppedFrame();
return;
}
bool is_keyframe = false;
CFArrayRef attachments =
CMSampleBufferGetSampleAttachmentsArray(sample_buffer, 0);
if (attachments != nullptr && CFArrayGetCount(attachments)) {
CFDictionaryRef attachment =
static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(attachments, 0));
is_keyframe =
!CFDictionaryContainsKey(attachment, kCMSampleAttachmentKey_NotSync);
}
if (is_keyframe) {
LOG(LS_INFO) << "Generated keyframe";
}
// Convert the sample buffer into a buffer suitable for RTP packetization.
// TODO(tkchin): Allocate buffers through a pool.
std::unique_ptr<rtc::Buffer> buffer(new rtc::Buffer());
std::unique_ptr<webrtc::RTPFragmentationHeader> header;
{
webrtc::RTPFragmentationHeader* header_raw;
bool result = H264CMSampleBufferToAnnexBBuffer(sample_buffer, is_keyframe,
buffer.get(), &header_raw);
header.reset(header_raw);
if (!result) {
return;
}
}
webrtc::EncodedImage frame(buffer->data(), buffer->size(), buffer->size());
frame._encodedWidth = width;
frame._encodedHeight = height;
frame._completeFrame = true;
frame._frameType =
is_keyframe ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta;
frame.capture_time_ms_ = render_time_ms;
frame._timeStamp = timestamp;
frame.rotation_ = rotation;
h264_bitstream_parser_.ParseBitstream(buffer->data(), buffer->size());
int qp;
if (h264_bitstream_parser_.GetLastSliceQp(&qp)) {
rtc::CritScope lock(&quality_scaler_crit_);
quality_scaler_.ReportQP(qp);
}
EncodedImageCallback::Result result =
callback_->OnEncodedImage(frame, &codec_specific_info, header.get());
if (result.error != EncodedImageCallback::Result::OK) {
LOG(LS_ERROR) << "Encode callback failed: " << result.error;
return;
}
bitrate_adjuster_.Update(frame._size);
}
} // namespace webrtc

View File

@ -0,0 +1,365 @@
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*
*/
#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h"
#include <CoreFoundation/CoreFoundation.h>
#include <memory>
#include <vector>
#include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
namespace webrtc {
using H264::kAud;
using H264::kSps;
using H264::NaluIndex;
using H264::NaluType;
using H264::ParseNaluType;
const char kAnnexBHeaderBytes[4] = {0, 0, 0, 1};
const size_t kAvccHeaderByteSize = sizeof(uint32_t);
bool H264CMSampleBufferToAnnexBBuffer(
CMSampleBufferRef avcc_sample_buffer,
bool is_keyframe,
rtc::Buffer* annexb_buffer,
webrtc::RTPFragmentationHeader** out_header) {
RTC_DCHECK(avcc_sample_buffer);
RTC_DCHECK(out_header);
*out_header = nullptr;
// Get format description from the sample buffer.
CMVideoFormatDescriptionRef description =
CMSampleBufferGetFormatDescription(avcc_sample_buffer);
if (description == nullptr) {
LOG(LS_ERROR) << "Failed to get sample buffer's description.";
return false;
}
// Get parameter set information.
int nalu_header_size = 0;
size_t param_set_count = 0;
OSStatus status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
description, 0, nullptr, nullptr, &param_set_count, &nalu_header_size);
if (status != noErr) {
LOG(LS_ERROR) << "Failed to get parameter set.";
return false;
}
RTC_CHECK_EQ(nalu_header_size, kAvccHeaderByteSize);
RTC_DCHECK_EQ(param_set_count, 2u);
// Truncate any previous data in the buffer without changing its capacity.
annexb_buffer->SetSize(0);
size_t nalu_offset = 0;
std::vector<size_t> frag_offsets;
std::vector<size_t> frag_lengths;
// Place all parameter sets at the front of buffer.
if (is_keyframe) {
size_t param_set_size = 0;
const uint8_t* param_set = nullptr;
for (size_t i = 0; i < param_set_count; ++i) {
status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
description, i, &param_set, &param_set_size, nullptr, nullptr);
if (status != noErr) {
LOG(LS_ERROR) << "Failed to get parameter set.";
return false;
}
// Update buffer.
annexb_buffer->AppendData(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes));
annexb_buffer->AppendData(reinterpret_cast<const char*>(param_set),
param_set_size);
// Update fragmentation.
frag_offsets.push_back(nalu_offset + sizeof(kAnnexBHeaderBytes));
frag_lengths.push_back(param_set_size);
nalu_offset += sizeof(kAnnexBHeaderBytes) + param_set_size;
}
}
// Get block buffer from the sample buffer.
CMBlockBufferRef block_buffer =
CMSampleBufferGetDataBuffer(avcc_sample_buffer);
if (block_buffer == nullptr) {
LOG(LS_ERROR) << "Failed to get sample buffer's block buffer.";
return false;
}
CMBlockBufferRef contiguous_buffer = nullptr;
// Make sure block buffer is contiguous.
if (!CMBlockBufferIsRangeContiguous(block_buffer, 0, 0)) {
status = CMBlockBufferCreateContiguous(
nullptr, block_buffer, nullptr, nullptr, 0, 0, 0, &contiguous_buffer);
if (status != noErr) {
LOG(LS_ERROR) << "Failed to flatten non-contiguous block buffer: "
<< status;
return false;
}
} else {
contiguous_buffer = block_buffer;
// Retain to make cleanup easier.
CFRetain(contiguous_buffer);
block_buffer = nullptr;
}
// Now copy the actual data.
char* data_ptr = nullptr;
size_t block_buffer_size = CMBlockBufferGetDataLength(contiguous_buffer);
status = CMBlockBufferGetDataPointer(contiguous_buffer, 0, nullptr, nullptr,
&data_ptr);
if (status != noErr) {
LOG(LS_ERROR) << "Failed to get block buffer data.";
CFRelease(contiguous_buffer);
return false;
}
size_t bytes_remaining = block_buffer_size;
while (bytes_remaining > 0) {
// The size type here must match |nalu_header_size|, we expect 4 bytes.
// Read the length of the next packet of data. Must convert from big endian
// to host endian.
RTC_DCHECK_GE(bytes_remaining, (size_t)nalu_header_size);
uint32_t* uint32_data_ptr = reinterpret_cast<uint32_t*>(data_ptr);
uint32_t packet_size = CFSwapInt32BigToHost(*uint32_data_ptr);
// Update buffer.
annexb_buffer->AppendData(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes));
annexb_buffer->AppendData(data_ptr + nalu_header_size, packet_size);
// Update fragmentation.
frag_offsets.push_back(nalu_offset + sizeof(kAnnexBHeaderBytes));
frag_lengths.push_back(packet_size);
nalu_offset += sizeof(kAnnexBHeaderBytes) + packet_size;
size_t bytes_written = packet_size + sizeof(kAnnexBHeaderBytes);
bytes_remaining -= bytes_written;
data_ptr += bytes_written;
}
RTC_DCHECK_EQ(bytes_remaining, (size_t)0);
std::unique_ptr<webrtc::RTPFragmentationHeader> header;
header.reset(new webrtc::RTPFragmentationHeader());
header->VerifyAndAllocateFragmentationHeader(frag_offsets.size());
RTC_DCHECK_EQ(frag_lengths.size(), frag_offsets.size());
for (size_t i = 0; i < frag_offsets.size(); ++i) {
header->fragmentationOffset[i] = frag_offsets[i];
header->fragmentationLength[i] = frag_lengths[i];
header->fragmentationPlType[i] = 0;
header->fragmentationTimeDiff[i] = 0;
}
*out_header = header.release();
CFRelease(contiguous_buffer);
return true;
}
bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
size_t annexb_buffer_size,
CMVideoFormatDescriptionRef video_format,
CMSampleBufferRef* out_sample_buffer) {
RTC_DCHECK(annexb_buffer);
RTC_DCHECK(out_sample_buffer);
RTC_DCHECK(video_format);
*out_sample_buffer = nullptr;
AnnexBBufferReader reader(annexb_buffer, annexb_buffer_size);
if (H264AnnexBBufferHasVideoFormatDescription(annexb_buffer,
annexb_buffer_size)) {
// Advance past the SPS and PPS.
const uint8_t* data = nullptr;
size_t data_len = 0;
if (!reader.ReadNalu(&data, &data_len)) {
LOG(LS_ERROR) << "Failed to read SPS";
return false;
}
if (!reader.ReadNalu(&data, &data_len)) {
LOG(LS_ERROR) << "Failed to read PPS";
return false;
}
}
// Allocate memory as a block buffer.
// TODO(tkchin): figure out how to use a pool.
CMBlockBufferRef block_buffer = nullptr;
OSStatus status = CMBlockBufferCreateWithMemoryBlock(
nullptr, nullptr, reader.BytesRemaining(), nullptr, nullptr, 0,
reader.BytesRemaining(), kCMBlockBufferAssureMemoryNowFlag,
&block_buffer);
if (status != kCMBlockBufferNoErr) {
LOG(LS_ERROR) << "Failed to create block buffer.";
return false;
}
// Make sure block buffer is contiguous.
CMBlockBufferRef contiguous_buffer = nullptr;
if (!CMBlockBufferIsRangeContiguous(block_buffer, 0, 0)) {
status = CMBlockBufferCreateContiguous(
nullptr, block_buffer, nullptr, nullptr, 0, 0, 0, &contiguous_buffer);
if (status != noErr) {
LOG(LS_ERROR) << "Failed to flatten non-contiguous block buffer: "
<< status;
CFRelease(block_buffer);
return false;
}
} else {
contiguous_buffer = block_buffer;
block_buffer = nullptr;
}
// Get a raw pointer into allocated memory.
size_t block_buffer_size = 0;
char* data_ptr = nullptr;
status = CMBlockBufferGetDataPointer(contiguous_buffer, 0, nullptr,
&block_buffer_size, &data_ptr);
if (status != kCMBlockBufferNoErr) {
LOG(LS_ERROR) << "Failed to get block buffer data pointer.";
CFRelease(contiguous_buffer);
return false;
}
RTC_DCHECK(block_buffer_size == reader.BytesRemaining());
// Write Avcc NALUs into block buffer memory.
AvccBufferWriter writer(reinterpret_cast<uint8_t*>(data_ptr),
block_buffer_size);
while (reader.BytesRemaining() > 0) {
const uint8_t* nalu_data_ptr = nullptr;
size_t nalu_data_size = 0;
if (reader.ReadNalu(&nalu_data_ptr, &nalu_data_size)) {
writer.WriteNalu(nalu_data_ptr, nalu_data_size);
}
}
// Create sample buffer.
status = CMSampleBufferCreate(nullptr, contiguous_buffer, true, nullptr,
nullptr, video_format, 1, 0, nullptr, 0,
nullptr, out_sample_buffer);
if (status != noErr) {
LOG(LS_ERROR) << "Failed to create sample buffer.";
CFRelease(contiguous_buffer);
return false;
}
CFRelease(contiguous_buffer);
return true;
}
bool H264AnnexBBufferHasVideoFormatDescription(const uint8_t* annexb_buffer,
size_t annexb_buffer_size) {
RTC_DCHECK(annexb_buffer);
RTC_DCHECK_GT(annexb_buffer_size, 4u);
// The buffer we receive via RTP has 00 00 00 01 start code artifically
// embedded by the RTP depacketizer. Extract NALU information.
// TODO(tkchin): handle potential case where sps and pps are delivered
// separately.
NaluType first_nalu_type = ParseNaluType(annexb_buffer[4]);
bool is_first_nalu_type_sps = first_nalu_type == kSps;
if (is_first_nalu_type_sps)
return true;
bool is_first_nalu_type_aud = first_nalu_type == kAud;
// Start code + access unit delimiter + start code = 4 + 2 + 4 = 10.
if (!is_first_nalu_type_aud || annexb_buffer_size <= 10u)
return false;
NaluType second_nalu_type = ParseNaluType(annexb_buffer[10]);
bool is_second_nalu_type_sps = second_nalu_type == kSps;
return is_second_nalu_type_sps;
}
CMVideoFormatDescriptionRef CreateVideoFormatDescription(
const uint8_t* annexb_buffer,
size_t annexb_buffer_size) {
if (!H264AnnexBBufferHasVideoFormatDescription(annexb_buffer,
annexb_buffer_size)) {
return nullptr;
}
AnnexBBufferReader reader(annexb_buffer, annexb_buffer_size);
CMVideoFormatDescriptionRef description = nullptr;
OSStatus status = noErr;
// Parse the SPS and PPS into a CMVideoFormatDescription.
const uint8_t* param_set_ptrs[2] = {};
size_t param_set_sizes[2] = {};
// Skip AUD.
if (ParseNaluType(annexb_buffer[4]) == kAud) {
if (!reader.ReadNalu(&param_set_ptrs[0], &param_set_sizes[0])) {
LOG(LS_ERROR) << "Failed to read AUD";
return nullptr;
}
}
if (!reader.ReadNalu(&param_set_ptrs[0], &param_set_sizes[0])) {
LOG(LS_ERROR) << "Failed to read SPS";
return nullptr;
}
if (!reader.ReadNalu(&param_set_ptrs[1], &param_set_sizes[1])) {
LOG(LS_ERROR) << "Failed to read PPS";
return nullptr;
}
status = CMVideoFormatDescriptionCreateFromH264ParameterSets(
kCFAllocatorDefault, 2, param_set_ptrs, param_set_sizes, 4,
&description);
if (status != noErr) {
LOG(LS_ERROR) << "Failed to create video format description.";
return nullptr;
}
return description;
}
AnnexBBufferReader::AnnexBBufferReader(const uint8_t* annexb_buffer,
size_t length)
: start_(annexb_buffer), length_(length) {
RTC_DCHECK(annexb_buffer);
offsets_ = H264::FindNaluIndices(annexb_buffer, length);
offset_ = offsets_.begin();
}
bool AnnexBBufferReader::ReadNalu(const uint8_t** out_nalu,
size_t* out_length) {
RTC_DCHECK(out_nalu);
RTC_DCHECK(out_length);
*out_nalu = nullptr;
*out_length = 0;
if (offset_ == offsets_.end()) {
return false;
}
*out_nalu = start_ + offset_->payload_start_offset;
*out_length = offset_->payload_size;
++offset_;
return true;
}
size_t AnnexBBufferReader::BytesRemaining() const {
if (offset_ == offsets_.end()) {
return 0;
}
return length_ - offset_->start_offset;
}
AvccBufferWriter::AvccBufferWriter(uint8_t* const avcc_buffer, size_t length)
: start_(avcc_buffer), offset_(0), length_(length) {
RTC_DCHECK(avcc_buffer);
}
bool AvccBufferWriter::WriteNalu(const uint8_t* data, size_t data_size) {
// Check if we can write this length of data.
if (data_size + kAvccHeaderByteSize > BytesRemaining()) {
return false;
}
// Write length header, which needs to be big endian.
uint32_t big_endian_length = CFSwapInt32HostToBig(data_size);
memcpy(start_ + offset_, &big_endian_length, sizeof(big_endian_length));
offset_ += sizeof(big_endian_length);
// Write data.
memcpy(start_ + offset_, data, data_size);
offset_ += data_size;
return true;
}
size_t AvccBufferWriter::BytesRemaining() const {
return length_ - offset_;
}
} // namespace webrtc

View File

@ -0,0 +1,112 @@
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*
*/
#ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_NALU_H_
#define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_NALU_H_
#include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
#include <CoreMedia/CoreMedia.h>
#include <vector>
#include "webrtc/base/buffer.h"
#include "webrtc/common_video/h264/h264_common.h"
#include "webrtc/modules/include/module_common_types.h"
using webrtc::H264::NaluIndex;
namespace webrtc {
// Converts a sample buffer emitted from the VideoToolbox encoder into a buffer
// suitable for RTP. The sample buffer is in avcc format whereas the rtp buffer
// needs to be in Annex B format. Data is written directly to |annexb_buffer|
// and a new RTPFragmentationHeader is returned in |out_header|.
bool H264CMSampleBufferToAnnexBBuffer(
CMSampleBufferRef avcc_sample_buffer,
bool is_keyframe,
rtc::Buffer* annexb_buffer,
webrtc::RTPFragmentationHeader** out_header);
// Converts a buffer received from RTP into a sample buffer suitable for the
// VideoToolbox decoder. The RTP buffer is in annex b format whereas the sample
// buffer is in avcc format.
// If |is_keyframe| is true then |video_format| is ignored since the format will
// be read from the buffer. Otherwise |video_format| must be provided.
// Caller is responsible for releasing the created sample buffer.
bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
size_t annexb_buffer_size,
CMVideoFormatDescriptionRef video_format,
CMSampleBufferRef* out_sample_buffer);
// Returns true if the type of the first NALU in the supplied Annex B buffer is
// the SPS type.
bool H264AnnexBBufferHasVideoFormatDescription(const uint8_t* annexb_buffer,
size_t annexb_buffer_size);
// Returns a video format description created from the sps/pps information in
// the Annex B buffer. If there is no such information, nullptr is returned.
// The caller is responsible for releasing the description.
CMVideoFormatDescriptionRef CreateVideoFormatDescription(
const uint8_t* annexb_buffer,
size_t annexb_buffer_size);
// Helper class for reading NALUs from an RTP Annex B buffer.
class AnnexBBufferReader final {
public:
AnnexBBufferReader(const uint8_t* annexb_buffer, size_t length);
~AnnexBBufferReader() {}
AnnexBBufferReader(const AnnexBBufferReader& other) = delete;
void operator=(const AnnexBBufferReader& other) = delete;
// Returns a pointer to the beginning of the next NALU slice without the
// header bytes and its length. Returns false if no more slices remain.
bool ReadNalu(const uint8_t** out_nalu, size_t* out_length);
// Returns the number of unread NALU bytes, including the size of the header.
// If the buffer has no remaining NALUs this will return zero.
size_t BytesRemaining() const;
private:
// Returns the the next offset that contains NALU data.
size_t FindNextNaluHeader(const uint8_t* start,
size_t length,
size_t offset) const;
const uint8_t* const start_;
std::vector<NaluIndex> offsets_;
std::vector<NaluIndex>::iterator offset_;
const size_t length_;
};
// Helper class for writing NALUs using avcc format into a buffer.
class AvccBufferWriter final {
public:
AvccBufferWriter(uint8_t* const avcc_buffer, size_t length);
~AvccBufferWriter() {}
AvccBufferWriter(const AvccBufferWriter& other) = delete;
void operator=(const AvccBufferWriter& other) = delete;
// Writes the data slice into the buffer. Returns false if there isn't
// enough space left.
bool WriteNalu(const uint8_t* data, size_t data_size);
// Returns the unused bytes in the buffer.
size_t BytesRemaining() const;
private:
uint8_t* const start_;
size_t offset_;
const size_t length_;
};
} // namespace webrtc
#endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_NALU_H_

View File

@ -0,0 +1,204 @@
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*
*/
#include <memory>
#include "webrtc/base/arraysize.h"
#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h"
#include "webrtc/test/gtest.h"
namespace webrtc {
static const uint8_t NALU_TEST_DATA_0[] = {0xAA, 0xBB, 0xCC};
static const uint8_t NALU_TEST_DATA_1[] = {0xDE, 0xAD, 0xBE, 0xEF};
TEST(H264VideoToolboxNaluTest, TestHasVideoFormatDescription) {
const uint8_t sps_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x27};
EXPECT_TRUE(H264AnnexBBufferHasVideoFormatDescription(sps_buffer,
arraysize(sps_buffer)));
const uint8_t aud_sps_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x29, 0x10,
0x00, 0x00, 0x00, 0x01, 0x27, 0xFF};
EXPECT_TRUE(H264AnnexBBufferHasVideoFormatDescription(
aud_sps_buffer, arraysize(aud_sps_buffer)));
const uint8_t other_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x28};
EXPECT_FALSE(H264AnnexBBufferHasVideoFormatDescription(
other_buffer, arraysize(other_buffer)));
const uint8_t aud_other_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x29,
0x00, 0x00, 0x00, 0x01, 0x28};
EXPECT_FALSE(H264AnnexBBufferHasVideoFormatDescription(
aud_other_buffer, arraysize(aud_other_buffer)));
}
TEST(H264VideoToolboxNaluTest, TestCreateVideoFormatDescription) {
const uint8_t sps_pps_buffer[] = {
// SPS nalu.
0x00, 0x00, 0x00, 0x01,
0x27, 0x42, 0x00, 0x1E, 0xAB, 0x40, 0xF0, 0x28, 0xD3, 0x70, 0x20, 0x20,
0x20, 0x20,
// PPS nalu.
0x00, 0x00, 0x00, 0x01,
0x28, 0xCE, 0x3C, 0x30
};
CMVideoFormatDescriptionRef description =
CreateVideoFormatDescription(sps_pps_buffer, arraysize(sps_pps_buffer));
EXPECT_TRUE(description);
if (description) {
CFRelease(description);
description = nullptr;
}
const uint8_t other_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x28};
EXPECT_FALSE(CreateVideoFormatDescription(other_buffer,
arraysize(other_buffer)));
}
TEST(AnnexBBufferReaderTest, TestReadEmptyInput) {
const uint8_t annex_b_test_data[] = {0x00};
AnnexBBufferReader reader(annex_b_test_data, 0);
const uint8_t* nalu = nullptr;
size_t nalu_length = 0;
EXPECT_EQ(0u, reader.BytesRemaining());
EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length));
EXPECT_EQ(nullptr, nalu);
EXPECT_EQ(0u, nalu_length);
}
TEST(AnnexBBufferReaderTest, TestReadSingleNalu) {
const uint8_t annex_b_test_data[] = {0x00, 0x00, 0x00, 0x01, 0xAA};
AnnexBBufferReader reader(annex_b_test_data, arraysize(annex_b_test_data));
const uint8_t* nalu = nullptr;
size_t nalu_length = 0;
EXPECT_EQ(arraysize(annex_b_test_data), reader.BytesRemaining());
EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length));
EXPECT_EQ(annex_b_test_data + 4, nalu);
EXPECT_EQ(1u, nalu_length);
EXPECT_EQ(0u, reader.BytesRemaining());
EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length));
EXPECT_EQ(nullptr, nalu);
EXPECT_EQ(0u, nalu_length);
}
TEST(AnnexBBufferReaderTest, TestReadSingleNalu3ByteHeader) {
const uint8_t annex_b_test_data[] = {0x00, 0x00, 0x01, 0xAA};
AnnexBBufferReader reader(annex_b_test_data, arraysize(annex_b_test_data));
const uint8_t* nalu = nullptr;
size_t nalu_length = 0;
EXPECT_EQ(arraysize(annex_b_test_data), reader.BytesRemaining());
EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length));
EXPECT_EQ(annex_b_test_data + 3, nalu);
EXPECT_EQ(1u, nalu_length);
EXPECT_EQ(0u, reader.BytesRemaining());
EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length));
EXPECT_EQ(nullptr, nalu);
EXPECT_EQ(0u, nalu_length);
}
TEST(AnnexBBufferReaderTest, TestReadMissingNalu) {
// clang-format off
const uint8_t annex_b_test_data[] = {0x01,
0x00, 0x01,
0x00, 0x00, 0x00, 0xFF};
// clang-format on
AnnexBBufferReader reader(annex_b_test_data, arraysize(annex_b_test_data));
const uint8_t* nalu = nullptr;
size_t nalu_length = 0;
EXPECT_EQ(0u, reader.BytesRemaining());
EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length));
EXPECT_EQ(nullptr, nalu);
EXPECT_EQ(0u, nalu_length);
}
TEST(AnnexBBufferReaderTest, TestReadMultipleNalus) {
// clang-format off
const uint8_t annex_b_test_data[] = {0x00, 0x00, 0x00, 0x01, 0xFF,
0x01,
0x00, 0x01,
0x00, 0x00, 0x00, 0xFF,
0x00, 0x00, 0x01, 0xAA, 0xBB};
// clang-format on
AnnexBBufferReader reader(annex_b_test_data, arraysize(annex_b_test_data));
const uint8_t* nalu = nullptr;
size_t nalu_length = 0;
EXPECT_EQ(arraysize(annex_b_test_data), reader.BytesRemaining());
EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length));
EXPECT_EQ(annex_b_test_data + 4, nalu);
EXPECT_EQ(8u, nalu_length);
EXPECT_EQ(6u, reader.BytesRemaining());
EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length));
EXPECT_EQ(annex_b_test_data + 16, nalu);
EXPECT_EQ(2u, nalu_length);
EXPECT_EQ(0u, reader.BytesRemaining());
EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length));
EXPECT_EQ(nullptr, nalu);
EXPECT_EQ(0u, nalu_length);
}
TEST(AvccBufferWriterTest, TestEmptyOutputBuffer) {
const uint8_t expected_buffer[] = {0x00};
const size_t buffer_size = 1;
std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
memset(buffer.get(), 0, buffer_size);
AvccBufferWriter writer(buffer.get(), 0);
EXPECT_EQ(0u, writer.BytesRemaining());
EXPECT_FALSE(writer.WriteNalu(NALU_TEST_DATA_0, arraysize(NALU_TEST_DATA_0)));
EXPECT_EQ(0,
memcmp(expected_buffer, buffer.get(), arraysize(expected_buffer)));
}
TEST(AvccBufferWriterTest, TestWriteSingleNalu) {
const uint8_t expected_buffer[] = {
0x00, 0x00, 0x00, 0x03, 0xAA, 0xBB, 0xCC,
};
const size_t buffer_size = arraysize(NALU_TEST_DATA_0) + 4;
std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
AvccBufferWriter writer(buffer.get(), buffer_size);
EXPECT_EQ(buffer_size, writer.BytesRemaining());
EXPECT_TRUE(writer.WriteNalu(NALU_TEST_DATA_0, arraysize(NALU_TEST_DATA_0)));
EXPECT_EQ(0u, writer.BytesRemaining());
EXPECT_FALSE(writer.WriteNalu(NALU_TEST_DATA_1, arraysize(NALU_TEST_DATA_1)));
EXPECT_EQ(0,
memcmp(expected_buffer, buffer.get(), arraysize(expected_buffer)));
}
TEST(AvccBufferWriterTest, TestWriteMultipleNalus) {
// clang-format off
const uint8_t expected_buffer[] = {
0x00, 0x00, 0x00, 0x03, 0xAA, 0xBB, 0xCC,
0x00, 0x00, 0x00, 0x04, 0xDE, 0xAD, 0xBE, 0xEF
};
// clang-format on
const size_t buffer_size =
arraysize(NALU_TEST_DATA_0) + arraysize(NALU_TEST_DATA_1) + 8;
std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
AvccBufferWriter writer(buffer.get(), buffer_size);
EXPECT_EQ(buffer_size, writer.BytesRemaining());
EXPECT_TRUE(writer.WriteNalu(NALU_TEST_DATA_0, arraysize(NALU_TEST_DATA_0)));
EXPECT_EQ(buffer_size - (arraysize(NALU_TEST_DATA_0) + 4),
writer.BytesRemaining());
EXPECT_TRUE(writer.WriteNalu(NALU_TEST_DATA_1, arraysize(NALU_TEST_DATA_1)));
EXPECT_EQ(0u, writer.BytesRemaining());
EXPECT_EQ(0,
memcmp(expected_buffer, buffer.get(), arraysize(expected_buffer)));
}
TEST(AvccBufferWriterTest, TestOverflow) {
const uint8_t expected_buffer[] = {0x00, 0x00, 0x00};
const size_t buffer_size = arraysize(NALU_TEST_DATA_0);
std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
memset(buffer.get(), 0, buffer_size);
AvccBufferWriter writer(buffer.get(), buffer_size);
EXPECT_EQ(buffer_size, writer.BytesRemaining());
EXPECT_FALSE(writer.WriteNalu(NALU_TEST_DATA_0, arraysize(NALU_TEST_DATA_0)));
EXPECT_EQ(buffer_size, writer.BytesRemaining());
EXPECT_EQ(0,
memcmp(expected_buffer, buffer.get(), arraysize(expected_buffer)));
}
} // namespace webrtc

View File

@ -0,0 +1,103 @@
/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/sdk/objc/Framework/Classes/videotoolboxvideocodecfactory.h"
#include "webrtc/base/logging.h"
#include "webrtc/media/base/codec.h"
#if defined(WEBRTC_IOS)
#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_encoder.h"
#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_decoder.h"
#endif
// TODO(kthelgason): delete this when CreateVideoDecoder takes
// a cricket::VideoCodec instead of webrtc::VideoCodecType.
static const char* NameFromCodecType(webrtc::VideoCodecType type) {
switch (type) {
case webrtc::kVideoCodecVP8:
return cricket::kVp8CodecName;
case webrtc::kVideoCodecVP9:
return cricket::kVp9CodecName;
case webrtc::kVideoCodecH264:
return cricket::kH264CodecName;
default:
return "Unknown codec";
}
}
namespace webrtc {
// VideoToolboxVideoEncoderFactory
VideoToolboxVideoEncoderFactory::VideoToolboxVideoEncoderFactory() {
// Hardware H264 encoding only supported on iOS for now.
#if defined(WEBRTC_IOS)
supported_codecs_.push_back(cricket::VideoCodec(cricket::kH264CodecName));
#endif
}
VideoToolboxVideoEncoderFactory::~VideoToolboxVideoEncoderFactory() {}
VideoEncoder* VideoToolboxVideoEncoderFactory::CreateVideoEncoder(
const cricket::VideoCodec& codec) {
#if defined(WEBRTC_IOS)
if (IsCodecSupported(supported_codecs_, codec)) {
LOG(LS_INFO) << "Creating HW encoder for " << codec.name;
return new H264VideoToolboxEncoder();
}
#endif
LOG(LS_INFO) << "No HW encoder found for codec " << codec.name;
return nullptr;
}
void VideoToolboxVideoEncoderFactory::DestroyVideoEncoder(
VideoEncoder* encoder) {
#if defined(WEBRTC_IOS)
delete encoder;
encoder = nullptr;
#endif
}
const std::vector<cricket::VideoCodec>&
VideoToolboxVideoEncoderFactory::supported_codecs() const {
return supported_codecs_;
}
// VideoToolboxVideoDecoderFactory
VideoToolboxVideoDecoderFactory::VideoToolboxVideoDecoderFactory() {
#if defined(WEBRTC_IOS)
supported_codecs_.push_back(cricket::VideoCodec("H264"));
#endif
}
VideoToolboxVideoDecoderFactory::~VideoToolboxVideoDecoderFactory() {}
VideoDecoder* VideoToolboxVideoDecoderFactory::CreateVideoDecoder(
VideoCodecType type) {
const auto codec = cricket::VideoCodec(NameFromCodecType(type));
#if defined(WEBRTC_IOS)
if (IsCodecSupported(supported_codecs_, codec)) {
LOG(LS_INFO) << "Creating HW decoder for " << codec.name;
return new H264VideoToolboxDecoder();
}
#endif
LOG(LS_INFO) << "No HW decoder found for codec " << codec.name;
return nullptr;
}
void VideoToolboxVideoDecoderFactory::DestroyVideoDecoder(
VideoDecoder* decoder) {
#if defined(WEBRTC_IOS)
delete decoder;
decoder = nullptr;
#endif
}
} // namespace webrtc

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*
*/
#ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOXVIDEOCODECFACTORY_H_
#define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOXVIDEOCODECFACTORY_H_
#include "webrtc/media/engine/webrtcvideoencoderfactory.h"
#include "webrtc/media/engine/webrtcvideodecoderfactory.h"
namespace webrtc {
class VideoToolboxVideoEncoderFactory
: public cricket::WebRtcVideoEncoderFactory {
public:
VideoToolboxVideoEncoderFactory();
~VideoToolboxVideoEncoderFactory();
// WebRtcVideoEncoderFactory implementation.
VideoEncoder* CreateVideoEncoder(const cricket::VideoCodec& codec) override;
void DestroyVideoEncoder(VideoEncoder* encoder) override;
const std::vector<cricket::VideoCodec>& supported_codecs() const override;
private:
std::vector<cricket::VideoCodec> supported_codecs_;
};
class VideoToolboxVideoDecoderFactory
: public cricket::WebRtcVideoDecoderFactory {
public:
VideoToolboxVideoDecoderFactory();
~VideoToolboxVideoDecoderFactory();
// WebRtcVideoDecoderFactory implementation.
VideoDecoder* CreateVideoDecoder(VideoCodecType type) override;
void DestroyVideoDecoder(VideoDecoder* decoder) override;
private:
std::vector<cricket::VideoCodec> supported_codecs_;
};
} // namespace webrtc
#endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOXVIDEOCODECFACTORY_H_