Obj-C SDK Cleanup
This CL separates the files under sdk/objc into logical directories, replacing the previous file layout under Framework/. A long term goal is to have some system set up to generate the files under sdk/objc/api (the PeerConnection API wrappers) from the C++ code. In the shorter term the goal is to abstract out shared concepts from these classes in order to make them as uniform as possible. The separation into base/, components/, and helpers/ are to differentiate between the base layer's common protocols, various utilities and the actual platform specific components. The old directory layout that resembled a framework's internal layout is not necessary, since it is generated by the framework target when building it. Bug: webrtc:9627 Change-Id: Ib084fd83f050ae980649ca99e841f4fb0580bd8f Reviewed-on: https://webrtc-review.googlesource.com/94142 Reviewed-by: Kári Helgason <kthelgason@webrtc.org> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Rasmus Brandt <brandtr@webrtc.org> Reviewed-by: Henrik Andreassson <henrika@webrtc.org> Commit-Queue: Anders Carlsson <andersc@webrtc.org> Cr-Commit-Position: refs/heads/master@{#24493}
This commit is contained in:
committed by
Commit Bot
parent
9ea5765f78
commit
7bca8ca4e2
@ -1,290 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#import "WebRTC/RTCVideoCodecH264.h"
|
||||
|
||||
#import <VideoToolbox/VideoToolbox.h>
|
||||
|
||||
#include "modules/video_coding/include/video_error_codes.h"
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/logging.h"
|
||||
#include "rtc_base/timeutils.h"
|
||||
#include "sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h"
|
||||
|
||||
#import "WebRTC/RTCVideoFrame.h"
|
||||
#import "WebRTC/RTCVideoFrameBuffer.h"
|
||||
#import "helpers.h"
|
||||
#import "scoped_cftyperef.h"
|
||||
|
||||
#if defined(WEBRTC_IOS)
|
||||
#import "Common/RTCUIApplicationStatusObserver.h"
|
||||
#import "WebRTC/UIDevice+RTCDevice.h"
|
||||
#endif
|
||||
|
||||
// Struct that we pass to the decoder per frame to decode. We receive it again
|
||||
// in the decoder callback.
|
||||
struct RTCFrameDecodeParams {
|
||||
RTCFrameDecodeParams(RTCVideoDecoderCallback cb, int64_t ts) : callback(cb), timestamp(ts) {}
|
||||
RTCVideoDecoderCallback callback;
|
||||
int64_t timestamp;
|
||||
};
|
||||
|
||||
@interface RTCVideoDecoderH264 ()
|
||||
- (void)setError:(OSStatus)error;
|
||||
@end
|
||||
|
||||
// This is the callback function that VideoToolbox calls when decode is
|
||||
// complete.
|
||||
void decompressionOutputCallback(void *decoderRef,
|
||||
void *params,
|
||||
OSStatus status,
|
||||
VTDecodeInfoFlags infoFlags,
|
||||
CVImageBufferRef imageBuffer,
|
||||
CMTime timestamp,
|
||||
CMTime duration) {
|
||||
std::unique_ptr<RTCFrameDecodeParams> decodeParams(
|
||||
reinterpret_cast<RTCFrameDecodeParams *>(params));
|
||||
if (status != noErr) {
|
||||
RTCVideoDecoderH264 *decoder = (__bridge RTCVideoDecoderH264 *)decoderRef;
|
||||
[decoder setError:status];
|
||||
RTC_LOG(LS_ERROR) << "Failed to decode frame. Status: " << status;
|
||||
return;
|
||||
}
|
||||
// TODO(tkchin): Handle CVO properly.
|
||||
RTCCVPixelBuffer *frameBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:imageBuffer];
|
||||
RTCVideoFrame *decodedFrame =
|
||||
[[RTCVideoFrame alloc] initWithBuffer:frameBuffer
|
||||
rotation:RTCVideoRotation_0
|
||||
timeStampNs:CMTimeGetSeconds(timestamp) * rtc::kNumNanosecsPerSec];
|
||||
decodedFrame.timeStamp = decodeParams->timestamp;
|
||||
decodeParams->callback(decodedFrame);
|
||||
}
|
||||
|
||||
// Decoder.
|
||||
@implementation RTCVideoDecoderH264 {
|
||||
CMVideoFormatDescriptionRef _videoFormat;
|
||||
VTDecompressionSessionRef _decompressionSession;
|
||||
RTCVideoDecoderCallback _callback;
|
||||
OSStatus _error;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
#if defined(WEBRTC_IOS) && !defined(RTC_APPRTCMOBILE_BROADCAST_EXTENSION)
|
||||
[RTCUIApplicationStatusObserver prepareForUse];
|
||||
_error = noErr;
|
||||
#endif
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self destroyDecompressionSession];
|
||||
[self setVideoFormat:nullptr];
|
||||
}
|
||||
|
||||
- (NSInteger)startDecodeWithNumberOfCores:(int)numberOfCores {
|
||||
return WEBRTC_VIDEO_CODEC_OK;
|
||||
}
|
||||
|
||||
- (NSInteger)startDecodeWithSettings:(RTCVideoEncoderSettings *)settings
|
||||
numberOfCores:(int)numberOfCores {
|
||||
return WEBRTC_VIDEO_CODEC_OK;
|
||||
}
|
||||
|
||||
- (NSInteger)decode:(RTCEncodedImage *)inputImage
|
||||
missingFrames:(BOOL)missingFrames
|
||||
codecSpecificInfo:(nullable id<RTCCodecSpecificInfo>)info
|
||||
renderTimeMs:(int64_t)renderTimeMs {
|
||||
RTC_DCHECK(inputImage.buffer);
|
||||
|
||||
if (_error != noErr) {
|
||||
RTC_LOG(LS_WARNING) << "Last frame decode failed.";
|
||||
_error = noErr;
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
|
||||
#if defined(WEBRTC_IOS) && !defined(RTC_APPRTCMOBILE_BROADCAST_EXTENSION)
|
||||
if (![[RTCUIApplicationStatusObserver sharedInstance] isApplicationActive]) {
|
||||
// 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.
|
||||
[self setVideoFormat:nullptr];
|
||||
return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
|
||||
}
|
||||
#endif
|
||||
rtc::ScopedCFTypeRef<CMVideoFormatDescriptionRef> inputFormat =
|
||||
rtc::ScopedCF(webrtc::CreateVideoFormatDescription((uint8_t *)inputImage.buffer.bytes,
|
||||
inputImage.buffer.length));
|
||||
if (inputFormat) {
|
||||
// Check if the video format has changed, and reinitialize decoder if
|
||||
// needed.
|
||||
if (!CMFormatDescriptionEqual(inputFormat.get(), _videoFormat)) {
|
||||
[self setVideoFormat:inputFormat.get()];
|
||||
int resetDecompressionSessionError = [self resetDecompressionSession];
|
||||
if (resetDecompressionSessionError != WEBRTC_VIDEO_CODEC_OK) {
|
||||
return resetDecompressionSessionError;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!_videoFormat) {
|
||||
// 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.
|
||||
RTC_LOG(LS_WARNING) << "Missing video format. Frame with sps/pps required.";
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
CMSampleBufferRef sampleBuffer = nullptr;
|
||||
if (!webrtc::H264AnnexBBufferToCMSampleBuffer((uint8_t *)inputImage.buffer.bytes,
|
||||
inputImage.buffer.length,
|
||||
_videoFormat,
|
||||
&sampleBuffer)) {
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
RTC_DCHECK(sampleBuffer);
|
||||
VTDecodeFrameFlags decodeFlags = kVTDecodeFrame_EnableAsynchronousDecompression;
|
||||
std::unique_ptr<RTCFrameDecodeParams> frameDecodeParams;
|
||||
frameDecodeParams.reset(new RTCFrameDecodeParams(_callback, inputImage.timeStamp));
|
||||
OSStatus status = VTDecompressionSessionDecodeFrame(
|
||||
_decompressionSession, sampleBuffer, decodeFlags, frameDecodeParams.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 && [self resetDecompressionSession] == WEBRTC_VIDEO_CODEC_OK) {
|
||||
frameDecodeParams.reset(new RTCFrameDecodeParams(_callback, inputImage.timeStamp));
|
||||
status = VTDecompressionSessionDecodeFrame(
|
||||
_decompressionSession, sampleBuffer, decodeFlags, frameDecodeParams.release(), nullptr);
|
||||
}
|
||||
#endif
|
||||
CFRelease(sampleBuffer);
|
||||
if (status != noErr) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to decode frame with code: " << status;
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
return WEBRTC_VIDEO_CODEC_OK;
|
||||
}
|
||||
|
||||
- (void)setCallback:(RTCVideoDecoderCallback)callback {
|
||||
_callback = callback;
|
||||
}
|
||||
|
||||
- (void)setError:(OSStatus)error {
|
||||
_error = error;
|
||||
}
|
||||
|
||||
- (NSInteger)releaseDecoder {
|
||||
// Need to invalidate the session so that callbacks no longer occur and it
|
||||
// is safe to null out the callback.
|
||||
[self destroyDecompressionSession];
|
||||
[self setVideoFormat:nullptr];
|
||||
_callback = nullptr;
|
||||
return WEBRTC_VIDEO_CODEC_OK;
|
||||
}
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
- (int)resetDecompressionSession {
|
||||
[self destroyDecompressionSession];
|
||||
|
||||
// Need to wait for the first SPS to initialize decoder.
|
||||
if (!_videoFormat) {
|
||||
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 attributesSize = 3;
|
||||
CFTypeRef keys[attributesSize] = {
|
||||
#if defined(WEBRTC_IOS)
|
||||
kCVPixelBufferOpenGLESCompatibilityKey,
|
||||
#elif defined(WEBRTC_MAC)
|
||||
kCVPixelBufferOpenGLCompatibilityKey,
|
||||
#endif
|
||||
kCVPixelBufferIOSurfacePropertiesKey,
|
||||
kCVPixelBufferPixelFormatTypeKey
|
||||
};
|
||||
CFDictionaryRef ioSurfaceValue = CreateCFTypeDictionary(nullptr, nullptr, 0);
|
||||
int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
|
||||
CFNumberRef pixelFormat = CFNumberCreate(nullptr, kCFNumberLongType, &nv12type);
|
||||
CFTypeRef values[attributesSize] = {kCFBooleanTrue, ioSurfaceValue, pixelFormat};
|
||||
CFDictionaryRef attributes = CreateCFTypeDictionary(keys, values, attributesSize);
|
||||
if (ioSurfaceValue) {
|
||||
CFRelease(ioSurfaceValue);
|
||||
ioSurfaceValue = nullptr;
|
||||
}
|
||||
if (pixelFormat) {
|
||||
CFRelease(pixelFormat);
|
||||
pixelFormat = nullptr;
|
||||
}
|
||||
VTDecompressionOutputCallbackRecord record = {
|
||||
decompressionOutputCallback, (__bridge void *)self,
|
||||
};
|
||||
OSStatus status = VTDecompressionSessionCreate(
|
||||
nullptr, _videoFormat, nullptr, attributes, &record, &_decompressionSession);
|
||||
CFRelease(attributes);
|
||||
if (status != noErr) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to create decompression session: " << status;
|
||||
[self destroyDecompressionSession];
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
[self configureDecompressionSession];
|
||||
|
||||
return WEBRTC_VIDEO_CODEC_OK;
|
||||
}
|
||||
|
||||
- (void)configureDecompressionSession {
|
||||
RTC_DCHECK(_decompressionSession);
|
||||
#if defined(WEBRTC_IOS)
|
||||
VTSessionSetProperty(_decompressionSession, kVTDecompressionPropertyKey_RealTime, kCFBooleanTrue);
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)destroyDecompressionSession {
|
||||
if (_decompressionSession) {
|
||||
#if defined(WEBRTC_IOS)
|
||||
if ([UIDevice isIOS11OrLater]) {
|
||||
VTDecompressionSessionWaitForAsynchronousFrames(_decompressionSession);
|
||||
}
|
||||
#endif
|
||||
VTDecompressionSessionInvalidate(_decompressionSession);
|
||||
CFRelease(_decompressionSession);
|
||||
_decompressionSession = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setVideoFormat:(CMVideoFormatDescriptionRef)videoFormat {
|
||||
if (_videoFormat == videoFormat) {
|
||||
return;
|
||||
}
|
||||
if (_videoFormat) {
|
||||
CFRelease(_videoFormat);
|
||||
}
|
||||
_videoFormat = videoFormat;
|
||||
if (_videoFormat) {
|
||||
CFRetain(_videoFormat);
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)implementationName {
|
||||
return @"VideoToolbox";
|
||||
}
|
||||
|
||||
@end
|
||||
@ -1,766 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#import "WebRTC/RTCVideoCodecH264.h"
|
||||
|
||||
#import <VideoToolbox/VideoToolbox.h>
|
||||
#include <vector>
|
||||
|
||||
#if defined(WEBRTC_IOS)
|
||||
#import "Common/RTCUIApplicationStatusObserver.h"
|
||||
#import "WebRTC/UIDevice+RTCDevice.h"
|
||||
#endif
|
||||
#import "PeerConnection/RTCVideoCodec+Private.h"
|
||||
#import "WebRTC/RTCVideoCodec.h"
|
||||
#import "WebRTC/RTCVideoFrame.h"
|
||||
#import "WebRTC/RTCVideoFrameBuffer.h"
|
||||
#include "common_video/h264/h264_bitstream_parser.h"
|
||||
#include "common_video/h264/profile_level_id.h"
|
||||
#include "common_video/include/bitrate_adjuster.h"
|
||||
#import "helpers.h"
|
||||
#include "modules/include/module_common_types.h"
|
||||
#include "modules/video_coding/include/video_error_codes.h"
|
||||
#include "rtc_base/buffer.h"
|
||||
#include "rtc_base/logging.h"
|
||||
#include "rtc_base/timeutils.h"
|
||||
#include "sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h"
|
||||
#include "third_party/libyuv/include/libyuv/convert_from.h"
|
||||
|
||||
@interface RTCVideoEncoderH264 ()
|
||||
|
||||
- (void)frameWasEncoded:(OSStatus)status
|
||||
flags:(VTEncodeInfoFlags)infoFlags
|
||||
sampleBuffer:(CMSampleBufferRef)sampleBuffer
|
||||
codecSpecificInfo:(id<RTCCodecSpecificInfo>)codecSpecificInfo
|
||||
width:(int32_t)width
|
||||
height:(int32_t)height
|
||||
renderTimeMs:(int64_t)renderTimeMs
|
||||
timestamp:(uint32_t)timestamp
|
||||
rotation:(RTCVideoRotation)rotation;
|
||||
|
||||
@end
|
||||
|
||||
namespace { // anonymous namespace
|
||||
|
||||
// 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;
|
||||
|
||||
const OSType kNV12PixelFormat = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
|
||||
|
||||
// Struct that we pass to the encoder per frame to encode. We receive it again
|
||||
// in the encoder callback.
|
||||
struct RTCFrameEncodeParams {
|
||||
RTCFrameEncodeParams(RTCVideoEncoderH264 *e,
|
||||
RTCCodecSpecificInfoH264 *csi,
|
||||
int32_t w,
|
||||
int32_t h,
|
||||
int64_t rtms,
|
||||
uint32_t ts,
|
||||
RTCVideoRotation r)
|
||||
: encoder(e), width(w), height(h), render_time_ms(rtms), timestamp(ts), rotation(r) {
|
||||
if (csi) {
|
||||
codecSpecificInfo = csi;
|
||||
} else {
|
||||
codecSpecificInfo = [[RTCCodecSpecificInfoH264 alloc] init];
|
||||
}
|
||||
}
|
||||
|
||||
RTCVideoEncoderH264 *encoder;
|
||||
RTCCodecSpecificInfoH264 *codecSpecificInfo;
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
int64_t render_time_ms;
|
||||
uint32_t timestamp;
|
||||
RTCVideoRotation 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 CopyVideoFrameToNV12PixelBuffer(id<RTCI420Buffer> frameBuffer, CVPixelBufferRef pixelBuffer) {
|
||||
RTC_DCHECK(pixelBuffer);
|
||||
RTC_DCHECK_EQ(CVPixelBufferGetPixelFormatType(pixelBuffer), kNV12PixelFormat);
|
||||
RTC_DCHECK_EQ(CVPixelBufferGetHeightOfPlane(pixelBuffer, 0), frameBuffer.height);
|
||||
RTC_DCHECK_EQ(CVPixelBufferGetWidthOfPlane(pixelBuffer, 0), frameBuffer.width);
|
||||
|
||||
CVReturn cvRet = CVPixelBufferLockBaseAddress(pixelBuffer, 0);
|
||||
if (cvRet != kCVReturnSuccess) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to lock base address: " << cvRet;
|
||||
return false;
|
||||
}
|
||||
uint8_t *dstY = reinterpret_cast<uint8_t *>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0));
|
||||
int dstStrideY = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
|
||||
uint8_t *dstUV = reinterpret_cast<uint8_t *>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1));
|
||||
int dstStrideUV = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1);
|
||||
// Convert I420 to NV12.
|
||||
int ret = libyuv::I420ToNV12(frameBuffer.dataY,
|
||||
frameBuffer.strideY,
|
||||
frameBuffer.dataU,
|
||||
frameBuffer.strideU,
|
||||
frameBuffer.dataV,
|
||||
frameBuffer.strideV,
|
||||
dstY,
|
||||
dstStrideY,
|
||||
dstUV,
|
||||
dstStrideUV,
|
||||
frameBuffer.width,
|
||||
frameBuffer.height);
|
||||
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
|
||||
if (ret) {
|
||||
RTC_LOG(LS_ERROR) << "Error converting I420 VideoFrame to NV12 :" << ret;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
CVPixelBufferRef CreatePixelBuffer(CVPixelBufferPoolRef pixel_buffer_pool) {
|
||||
if (!pixel_buffer_pool) {
|
||||
RTC_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) {
|
||||
RTC_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 compressionOutputCallback(void *encoder,
|
||||
void *params,
|
||||
OSStatus status,
|
||||
VTEncodeInfoFlags infoFlags,
|
||||
CMSampleBufferRef sampleBuffer) {
|
||||
if (!params) {
|
||||
// If there are pending callbacks when the encoder is destroyed, this can happen.
|
||||
return;
|
||||
}
|
||||
std::unique_ptr<RTCFrameEncodeParams> encodeParams(
|
||||
reinterpret_cast<RTCFrameEncodeParams *>(params));
|
||||
[encodeParams->encoder frameWasEncoded:status
|
||||
flags:infoFlags
|
||||
sampleBuffer:sampleBuffer
|
||||
codecSpecificInfo:encodeParams->codecSpecificInfo
|
||||
width:encodeParams->width
|
||||
height:encodeParams->height
|
||||
renderTimeMs:encodeParams->render_time_ms
|
||||
timestamp:encodeParams->timestamp
|
||||
rotation:encodeParams->rotation];
|
||||
}
|
||||
|
||||
// Extract VideoToolbox profile out of the webrtc::SdpVideoFormat. If there is
|
||||
// no specific VideoToolbox profile for the specified level, AutoLevel will be
|
||||
// returned. The user must initialize the encoder with a resolution and
|
||||
// framerate conforming to the selected H264 level regardless.
|
||||
CFStringRef ExtractProfile(webrtc::SdpVideoFormat videoFormat) {
|
||||
const absl::optional<webrtc::H264::ProfileLevelId> profile_level_id =
|
||||
webrtc::H264::ParseSdpProfileLevelId(videoFormat.parameters);
|
||||
RTC_DCHECK(profile_level_id);
|
||||
switch (profile_level_id->profile) {
|
||||
case webrtc::H264::kProfileConstrainedBaseline:
|
||||
case webrtc::H264::kProfileBaseline:
|
||||
switch (profile_level_id->level) {
|
||||
case webrtc::H264::kLevel3:
|
||||
return kVTProfileLevel_H264_Baseline_3_0;
|
||||
case webrtc::H264::kLevel3_1:
|
||||
return kVTProfileLevel_H264_Baseline_3_1;
|
||||
case webrtc::H264::kLevel3_2:
|
||||
return kVTProfileLevel_H264_Baseline_3_2;
|
||||
case webrtc::H264::kLevel4:
|
||||
return kVTProfileLevel_H264_Baseline_4_0;
|
||||
case webrtc::H264::kLevel4_1:
|
||||
return kVTProfileLevel_H264_Baseline_4_1;
|
||||
case webrtc::H264::kLevel4_2:
|
||||
return kVTProfileLevel_H264_Baseline_4_2;
|
||||
case webrtc::H264::kLevel5:
|
||||
return kVTProfileLevel_H264_Baseline_5_0;
|
||||
case webrtc::H264::kLevel5_1:
|
||||
return kVTProfileLevel_H264_Baseline_5_1;
|
||||
case webrtc::H264::kLevel5_2:
|
||||
return kVTProfileLevel_H264_Baseline_5_2;
|
||||
case webrtc::H264::kLevel1:
|
||||
case webrtc::H264::kLevel1_b:
|
||||
case webrtc::H264::kLevel1_1:
|
||||
case webrtc::H264::kLevel1_2:
|
||||
case webrtc::H264::kLevel1_3:
|
||||
case webrtc::H264::kLevel2:
|
||||
case webrtc::H264::kLevel2_1:
|
||||
case webrtc::H264::kLevel2_2:
|
||||
return kVTProfileLevel_H264_Baseline_AutoLevel;
|
||||
}
|
||||
|
||||
case webrtc::H264::kProfileMain:
|
||||
switch (profile_level_id->level) {
|
||||
case webrtc::H264::kLevel3:
|
||||
return kVTProfileLevel_H264_Main_3_0;
|
||||
case webrtc::H264::kLevel3_1:
|
||||
return kVTProfileLevel_H264_Main_3_1;
|
||||
case webrtc::H264::kLevel3_2:
|
||||
return kVTProfileLevel_H264_Main_3_2;
|
||||
case webrtc::H264::kLevel4:
|
||||
return kVTProfileLevel_H264_Main_4_0;
|
||||
case webrtc::H264::kLevel4_1:
|
||||
return kVTProfileLevel_H264_Main_4_1;
|
||||
case webrtc::H264::kLevel4_2:
|
||||
return kVTProfileLevel_H264_Main_4_2;
|
||||
case webrtc::H264::kLevel5:
|
||||
return kVTProfileLevel_H264_Main_5_0;
|
||||
case webrtc::H264::kLevel5_1:
|
||||
return kVTProfileLevel_H264_Main_5_1;
|
||||
case webrtc::H264::kLevel5_2:
|
||||
return kVTProfileLevel_H264_Main_5_2;
|
||||
case webrtc::H264::kLevel1:
|
||||
case webrtc::H264::kLevel1_b:
|
||||
case webrtc::H264::kLevel1_1:
|
||||
case webrtc::H264::kLevel1_2:
|
||||
case webrtc::H264::kLevel1_3:
|
||||
case webrtc::H264::kLevel2:
|
||||
case webrtc::H264::kLevel2_1:
|
||||
case webrtc::H264::kLevel2_2:
|
||||
return kVTProfileLevel_H264_Main_AutoLevel;
|
||||
}
|
||||
|
||||
case webrtc::H264::kProfileConstrainedHigh:
|
||||
case webrtc::H264::kProfileHigh:
|
||||
switch (profile_level_id->level) {
|
||||
case webrtc::H264::kLevel3:
|
||||
return kVTProfileLevel_H264_High_3_0;
|
||||
case webrtc::H264::kLevel3_1:
|
||||
return kVTProfileLevel_H264_High_3_1;
|
||||
case webrtc::H264::kLevel3_2:
|
||||
return kVTProfileLevel_H264_High_3_2;
|
||||
case webrtc::H264::kLevel4:
|
||||
return kVTProfileLevel_H264_High_4_0;
|
||||
case webrtc::H264::kLevel4_1:
|
||||
return kVTProfileLevel_H264_High_4_1;
|
||||
case webrtc::H264::kLevel4_2:
|
||||
return kVTProfileLevel_H264_High_4_2;
|
||||
case webrtc::H264::kLevel5:
|
||||
return kVTProfileLevel_H264_High_5_0;
|
||||
case webrtc::H264::kLevel5_1:
|
||||
return kVTProfileLevel_H264_High_5_1;
|
||||
case webrtc::H264::kLevel5_2:
|
||||
return kVTProfileLevel_H264_High_5_2;
|
||||
case webrtc::H264::kLevel1:
|
||||
case webrtc::H264::kLevel1_b:
|
||||
case webrtc::H264::kLevel1_1:
|
||||
case webrtc::H264::kLevel1_2:
|
||||
case webrtc::H264::kLevel1_3:
|
||||
case webrtc::H264::kLevel2:
|
||||
case webrtc::H264::kLevel2_1:
|
||||
case webrtc::H264::kLevel2_2:
|
||||
return kVTProfileLevel_H264_High_AutoLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@implementation RTCVideoEncoderH264 {
|
||||
RTCVideoCodecInfo *_codecInfo;
|
||||
std::unique_ptr<webrtc::BitrateAdjuster> _bitrateAdjuster;
|
||||
uint32_t _targetBitrateBps;
|
||||
uint32_t _encoderBitrateBps;
|
||||
RTCH264PacketizationMode _packetizationMode;
|
||||
CFStringRef _profile;
|
||||
RTCVideoEncoderCallback _callback;
|
||||
int32_t _width;
|
||||
int32_t _height;
|
||||
VTCompressionSessionRef _compressionSession;
|
||||
CVPixelBufferPoolRef _pixelBufferPool;
|
||||
RTCVideoCodecMode _mode;
|
||||
|
||||
webrtc::H264BitstreamParser _h264BitstreamParser;
|
||||
std::vector<uint8_t> _frameScaleBuffer;
|
||||
}
|
||||
|
||||
// .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.
|
||||
- (instancetype)initWithCodecInfo:(RTCVideoCodecInfo *)codecInfo {
|
||||
if (self = [super init]) {
|
||||
_codecInfo = codecInfo;
|
||||
_bitrateAdjuster.reset(new webrtc::BitrateAdjuster(.5, .95));
|
||||
_packetizationMode = RTCH264PacketizationModeNonInterleaved;
|
||||
_profile = ExtractProfile([codecInfo nativeSdpVideoFormat]);
|
||||
RTC_LOG(LS_INFO) << "Using profile " << CFStringToString(_profile);
|
||||
RTC_CHECK([codecInfo.name isEqualToString:kRTCVideoCodecH264Name]);
|
||||
|
||||
#if defined(WEBRTC_IOS) && !defined(RTC_APPRTCMOBILE_BROADCAST_EXTENSION)
|
||||
[RTCUIApplicationStatusObserver prepareForUse];
|
||||
#endif
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self destroyCompressionSession];
|
||||
}
|
||||
|
||||
- (NSInteger)startEncodeWithSettings:(RTCVideoEncoderSettings *)settings
|
||||
numberOfCores:(int)numberOfCores {
|
||||
RTC_DCHECK(settings);
|
||||
RTC_DCHECK([settings.name isEqualToString:kRTCVideoCodecH264Name]);
|
||||
|
||||
_width = settings.width;
|
||||
_height = settings.height;
|
||||
_mode = settings.mode;
|
||||
|
||||
// We can only set average bitrate on the HW encoder.
|
||||
_targetBitrateBps = settings.startBitrate * 1000; // startBitrate is in kbps.
|
||||
_bitrateAdjuster->SetTargetBitrateBps(_targetBitrateBps);
|
||||
|
||||
// TODO(tkchin): Try setting payload size via
|
||||
// kVTCompressionPropertyKey_MaxH264SliceBytes.
|
||||
|
||||
return [self resetCompressionSessionWithPixelFormat:kNV12PixelFormat];
|
||||
}
|
||||
|
||||
- (NSInteger)encode:(RTCVideoFrame *)frame
|
||||
codecSpecificInfo:(nullable id<RTCCodecSpecificInfo>)codecSpecificInfo
|
||||
frameTypes:(NSArray<NSNumber *> *)frameTypes {
|
||||
RTC_DCHECK_EQ(frame.width, _width);
|
||||
RTC_DCHECK_EQ(frame.height, _height);
|
||||
if (!_callback || !_compressionSession) {
|
||||
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
|
||||
}
|
||||
#if defined(WEBRTC_IOS) && !defined(RTC_APPRTCMOBILE_BROADCAST_EXTENSION)
|
||||
if (![[RTCUIApplicationStatusObserver sharedInstance] isApplicationActive]) {
|
||||
// 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 isKeyframeRequired = NO;
|
||||
|
||||
// Get a pixel buffer from the pool and copy frame data over.
|
||||
if ([self resetCompressionSessionIfNeededWithFrame:frame]) {
|
||||
isKeyframeRequired = YES;
|
||||
}
|
||||
|
||||
CVPixelBufferRef pixelBuffer = nullptr;
|
||||
if ([frame.buffer isKindOfClass:[RTCCVPixelBuffer class]]) {
|
||||
// Native frame buffer
|
||||
RTCCVPixelBuffer *rtcPixelBuffer = (RTCCVPixelBuffer *)frame.buffer;
|
||||
if (![rtcPixelBuffer 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.
|
||||
pixelBuffer = rtcPixelBuffer.pixelBuffer;
|
||||
CVBufferRetain(pixelBuffer);
|
||||
} else {
|
||||
// Cropping required, we need to crop and scale to a new pixel buffer.
|
||||
pixelBuffer = CreatePixelBuffer(_pixelBufferPool);
|
||||
if (!pixelBuffer) {
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
int dstWidth = CVPixelBufferGetWidth(pixelBuffer);
|
||||
int dstHeight = CVPixelBufferGetHeight(pixelBuffer);
|
||||
if ([rtcPixelBuffer requiresScalingToWidth:dstWidth height:dstHeight]) {
|
||||
int size =
|
||||
[rtcPixelBuffer bufferSizeForCroppingAndScalingToWidth:dstWidth height:dstHeight];
|
||||
_frameScaleBuffer.resize(size);
|
||||
} else {
|
||||
_frameScaleBuffer.clear();
|
||||
}
|
||||
_frameScaleBuffer.shrink_to_fit();
|
||||
if (![rtcPixelBuffer cropAndScaleTo:pixelBuffer withTempBuffer:_frameScaleBuffer.data()]) {
|
||||
CVBufferRelease(pixelBuffer);
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!pixelBuffer) {
|
||||
// We did not have a native frame buffer
|
||||
pixelBuffer = CreatePixelBuffer(_pixelBufferPool);
|
||||
if (!pixelBuffer) {
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
RTC_DCHECK(pixelBuffer);
|
||||
if (!CopyVideoFrameToNV12PixelBuffer([frame.buffer toI420], pixelBuffer)) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to copy frame data.";
|
||||
CVBufferRelease(pixelBuffer);
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we need a keyframe.
|
||||
if (!isKeyframeRequired && frameTypes) {
|
||||
for (NSNumber *frameType in frameTypes) {
|
||||
if ((RTCFrameType)frameType.intValue == RTCFrameTypeVideoFrameKey) {
|
||||
isKeyframeRequired = YES;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CMTime presentationTimeStamp = CMTimeMake(frame.timeStampNs / rtc::kNumNanosecsPerMillisec, 1000);
|
||||
CFDictionaryRef frameProperties = nullptr;
|
||||
if (isKeyframeRequired) {
|
||||
CFTypeRef keys[] = {kVTEncodeFrameOptionKey_ForceKeyFrame};
|
||||
CFTypeRef values[] = {kCFBooleanTrue};
|
||||
frameProperties = CreateCFTypeDictionary(keys, values, 1);
|
||||
}
|
||||
|
||||
std::unique_ptr<RTCFrameEncodeParams> encodeParams;
|
||||
encodeParams.reset(new RTCFrameEncodeParams(self,
|
||||
codecSpecificInfo,
|
||||
_width,
|
||||
_height,
|
||||
frame.timeStampNs / rtc::kNumNanosecsPerMillisec,
|
||||
frame.timeStamp,
|
||||
frame.rotation));
|
||||
encodeParams->codecSpecificInfo.packetizationMode = _packetizationMode;
|
||||
|
||||
// Update the bitrate if needed.
|
||||
[self setBitrateBps:_bitrateAdjuster->GetAdjustedBitrateBps()];
|
||||
|
||||
OSStatus status = VTCompressionSessionEncodeFrame(_compressionSession,
|
||||
pixelBuffer,
|
||||
presentationTimeStamp,
|
||||
kCMTimeInvalid,
|
||||
frameProperties,
|
||||
encodeParams.release(),
|
||||
nullptr);
|
||||
if (frameProperties) {
|
||||
CFRelease(frameProperties);
|
||||
}
|
||||
if (pixelBuffer) {
|
||||
CVBufferRelease(pixelBuffer);
|
||||
}
|
||||
|
||||
if (status == kVTInvalidSessionErr) {
|
||||
// This error occurs when entering foreground after backgrounding the app.
|
||||
RTC_LOG(LS_ERROR) << "Invalid compression session, resetting.";
|
||||
[self resetCompressionSessionWithPixelFormat:[self pixelFormatOfFrame:frame]];
|
||||
|
||||
return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
|
||||
} else if (status != noErr) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to encode frame with code: " << status;
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
return WEBRTC_VIDEO_CODEC_OK;
|
||||
}
|
||||
|
||||
- (void)setCallback:(RTCVideoEncoderCallback)callback {
|
||||
_callback = callback;
|
||||
}
|
||||
|
||||
- (int)setBitrate:(uint32_t)bitrateKbit framerate:(uint32_t)framerate {
|
||||
_targetBitrateBps = 1000 * bitrateKbit;
|
||||
_bitrateAdjuster->SetTargetBitrateBps(_targetBitrateBps);
|
||||
[self setBitrateBps:_bitrateAdjuster->GetAdjustedBitrateBps()];
|
||||
return WEBRTC_VIDEO_CODEC_OK;
|
||||
}
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
- (NSInteger)releaseEncoder {
|
||||
// Need to destroy 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.
|
||||
[self destroyCompressionSession];
|
||||
_callback = nullptr;
|
||||
return WEBRTC_VIDEO_CODEC_OK;
|
||||
}
|
||||
|
||||
- (OSType)pixelFormatOfFrame:(RTCVideoFrame *)frame {
|
||||
// Use NV12 for non-native frames.
|
||||
if ([frame.buffer isKindOfClass:[RTCCVPixelBuffer class]]) {
|
||||
RTCCVPixelBuffer *rtcPixelBuffer = (RTCCVPixelBuffer *)frame.buffer;
|
||||
return CVPixelBufferGetPixelFormatType(rtcPixelBuffer.pixelBuffer);
|
||||
}
|
||||
|
||||
return kNV12PixelFormat;
|
||||
}
|
||||
|
||||
- (BOOL)resetCompressionSessionIfNeededWithFrame:(RTCVideoFrame *)frame {
|
||||
BOOL resetCompressionSession = NO;
|
||||
|
||||
// If we're capturing native frames in another pixel format than the compression session is
|
||||
// configured with, make sure the compression session is reset using the correct pixel format.
|
||||
OSType framePixelFormat = [self pixelFormatOfFrame:frame];
|
||||
|
||||
if (_compressionSession) {
|
||||
// The pool attribute `kCVPixelBufferPixelFormatTypeKey` can contain either an array of pixel
|
||||
// formats or a single pixel format.
|
||||
NSDictionary *poolAttributes =
|
||||
(__bridge NSDictionary *)CVPixelBufferPoolGetPixelBufferAttributes(_pixelBufferPool);
|
||||
id pixelFormats =
|
||||
[poolAttributes objectForKey:(__bridge NSString *)kCVPixelBufferPixelFormatTypeKey];
|
||||
NSArray<NSNumber *> *compressionSessionPixelFormats = nil;
|
||||
if ([pixelFormats isKindOfClass:[NSArray class]]) {
|
||||
compressionSessionPixelFormats = (NSArray *)pixelFormats;
|
||||
} else if ([pixelFormats isKindOfClass:[NSNumber class]]) {
|
||||
compressionSessionPixelFormats = @[ (NSNumber *)pixelFormats ];
|
||||
}
|
||||
|
||||
if (![compressionSessionPixelFormats
|
||||
containsObject:[NSNumber numberWithLong:framePixelFormat]]) {
|
||||
resetCompressionSession = YES;
|
||||
RTC_LOG(LS_INFO) << "Resetting compression session due to non-matching pixel format.";
|
||||
}
|
||||
} else {
|
||||
resetCompressionSession = YES;
|
||||
}
|
||||
|
||||
if (resetCompressionSession) {
|
||||
[self resetCompressionSessionWithPixelFormat:framePixelFormat];
|
||||
}
|
||||
return resetCompressionSession;
|
||||
}
|
||||
|
||||
- (int)resetCompressionSessionWithPixelFormat:(OSType)framePixelFormat {
|
||||
[self destroyCompressionSession];
|
||||
|
||||
// Set source image buffer attributes. These attributes will be present on
|
||||
// buffers retrieved from the encoder's pixel buffer pool.
|
||||
const size_t attributesSize = 3;
|
||||
CFTypeRef keys[attributesSize] = {
|
||||
#if defined(WEBRTC_IOS)
|
||||
kCVPixelBufferOpenGLESCompatibilityKey,
|
||||
#elif defined(WEBRTC_MAC)
|
||||
kCVPixelBufferOpenGLCompatibilityKey,
|
||||
#endif
|
||||
kCVPixelBufferIOSurfacePropertiesKey,
|
||||
kCVPixelBufferPixelFormatTypeKey
|
||||
};
|
||||
CFDictionaryRef ioSurfaceValue = CreateCFTypeDictionary(nullptr, nullptr, 0);
|
||||
int64_t pixelFormatType = framePixelFormat;
|
||||
CFNumberRef pixelFormat = CFNumberCreate(nullptr, kCFNumberLongType, &pixelFormatType);
|
||||
CFTypeRef values[attributesSize] = {kCFBooleanTrue, ioSurfaceValue, pixelFormat};
|
||||
CFDictionaryRef sourceAttributes = CreateCFTypeDictionary(keys, values, attributesSize);
|
||||
if (ioSurfaceValue) {
|
||||
CFRelease(ioSurfaceValue);
|
||||
ioSurfaceValue = nullptr;
|
||||
}
|
||||
if (pixelFormat) {
|
||||
CFRelease(pixelFormat);
|
||||
pixelFormat = nullptr;
|
||||
}
|
||||
CFMutableDictionaryRef encoder_specs = nullptr;
|
||||
#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
|
||||
// Currently hw accl is supported above 360p on mac, below 360p
|
||||
// the compression session will be created with hw accl disabled.
|
||||
encoder_specs = CFDictionaryCreateMutable(
|
||||
nullptr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
||||
CFDictionarySetValue(encoder_specs,
|
||||
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
|
||||
kCFBooleanTrue);
|
||||
#endif
|
||||
OSStatus status =
|
||||
VTCompressionSessionCreate(nullptr, // use default allocator
|
||||
_width,
|
||||
_height,
|
||||
kCMVideoCodecType_H264,
|
||||
encoder_specs, // use hardware accelerated encoder if available
|
||||
sourceAttributes,
|
||||
nullptr, // use default compressed data allocator
|
||||
compressionOutputCallback,
|
||||
nullptr,
|
||||
&_compressionSession);
|
||||
if (sourceAttributes) {
|
||||
CFRelease(sourceAttributes);
|
||||
sourceAttributes = nullptr;
|
||||
}
|
||||
if (encoder_specs) {
|
||||
CFRelease(encoder_specs);
|
||||
encoder_specs = nullptr;
|
||||
}
|
||||
if (status != noErr) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to create compression session: " << status;
|
||||
return WEBRTC_VIDEO_CODEC_ERROR;
|
||||
}
|
||||
#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
|
||||
CFBooleanRef hwaccl_enabled = nullptr;
|
||||
status = VTSessionCopyProperty(_compressionSession,
|
||||
kVTCompressionPropertyKey_UsingHardwareAcceleratedVideoEncoder,
|
||||
nullptr,
|
||||
&hwaccl_enabled);
|
||||
if (status == noErr && (CFBooleanGetValue(hwaccl_enabled))) {
|
||||
RTC_LOG(LS_INFO) << "Compression session created with hw accl enabled";
|
||||
} else {
|
||||
RTC_LOG(LS_INFO) << "Compression session created with hw accl disabled";
|
||||
}
|
||||
#endif
|
||||
[self configureCompressionSession];
|
||||
|
||||
// The pixel buffer pool is dependent on the compression session so if the session is reset, the
|
||||
// pool should be reset as well.
|
||||
_pixelBufferPool = VTCompressionSessionGetPixelBufferPool(_compressionSession);
|
||||
|
||||
return WEBRTC_VIDEO_CODEC_OK;
|
||||
}
|
||||
|
||||
- (void)configureCompressionSession {
|
||||
RTC_DCHECK(_compressionSession);
|
||||
SetVTSessionProperty(_compressionSession, kVTCompressionPropertyKey_RealTime, true);
|
||||
SetVTSessionProperty(_compressionSession, kVTCompressionPropertyKey_ProfileLevel, _profile);
|
||||
SetVTSessionProperty(_compressionSession, kVTCompressionPropertyKey_AllowFrameReordering, false);
|
||||
[self setEncoderBitrateBps:_targetBitrateBps];
|
||||
// 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).
|
||||
SetVTSessionProperty(_compressionSession, kVTCompressionPropertyKey_MaxKeyFrameInterval, 7200);
|
||||
SetVTSessionProperty(
|
||||
_compressionSession, kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, 240);
|
||||
}
|
||||
|
||||
- (void)destroyCompressionSession {
|
||||
if (_compressionSession) {
|
||||
VTCompressionSessionInvalidate(_compressionSession);
|
||||
CFRelease(_compressionSession);
|
||||
_compressionSession = nullptr;
|
||||
_pixelBufferPool = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)implementationName {
|
||||
return @"VideoToolbox";
|
||||
}
|
||||
|
||||
- (void)setBitrateBps:(uint32_t)bitrateBps {
|
||||
if (_encoderBitrateBps != bitrateBps) {
|
||||
[self setEncoderBitrateBps:bitrateBps];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setEncoderBitrateBps:(uint32_t)bitrateBps {
|
||||
if (_compressionSession) {
|
||||
SetVTSessionProperty(_compressionSession, kVTCompressionPropertyKey_AverageBitRate, bitrateBps);
|
||||
|
||||
// TODO(tkchin): Add a helper method to set array value.
|
||||
int64_t dataLimitBytesPerSecondValue =
|
||||
static_cast<int64_t>(bitrateBps * kLimitToAverageBitRateFactor / 8);
|
||||
CFNumberRef bytesPerSecond =
|
||||
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &dataLimitBytesPerSecondValue);
|
||||
int64_t oneSecondValue = 1;
|
||||
CFNumberRef oneSecond =
|
||||
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &oneSecondValue);
|
||||
const void *nums[2] = {bytesPerSecond, oneSecond};
|
||||
CFArrayRef dataRateLimits = CFArrayCreate(nullptr, nums, 2, &kCFTypeArrayCallBacks);
|
||||
OSStatus status = VTSessionSetProperty(
|
||||
_compressionSession, kVTCompressionPropertyKey_DataRateLimits, dataRateLimits);
|
||||
if (bytesPerSecond) {
|
||||
CFRelease(bytesPerSecond);
|
||||
}
|
||||
if (oneSecond) {
|
||||
CFRelease(oneSecond);
|
||||
}
|
||||
if (dataRateLimits) {
|
||||
CFRelease(dataRateLimits);
|
||||
}
|
||||
if (status != noErr) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to set data rate limit with code: " << status;
|
||||
}
|
||||
|
||||
_encoderBitrateBps = bitrateBps;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)frameWasEncoded:(OSStatus)status
|
||||
flags:(VTEncodeInfoFlags)infoFlags
|
||||
sampleBuffer:(CMSampleBufferRef)sampleBuffer
|
||||
codecSpecificInfo:(id<RTCCodecSpecificInfo>)codecSpecificInfo
|
||||
width:(int32_t)width
|
||||
height:(int32_t)height
|
||||
renderTimeMs:(int64_t)renderTimeMs
|
||||
timestamp:(uint32_t)timestamp
|
||||
rotation:(RTCVideoRotation)rotation {
|
||||
if (status != noErr) {
|
||||
RTC_LOG(LS_ERROR) << "H264 encode failed with code: " << status;
|
||||
return;
|
||||
}
|
||||
if (infoFlags & kVTEncodeInfo_FrameDropped) {
|
||||
RTC_LOG(LS_INFO) << "H264 encode dropped frame.";
|
||||
return;
|
||||
}
|
||||
|
||||
BOOL isKeyframe = NO;
|
||||
CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, 0);
|
||||
if (attachments != nullptr && CFArrayGetCount(attachments)) {
|
||||
CFDictionaryRef attachment =
|
||||
static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(attachments, 0));
|
||||
isKeyframe = !CFDictionaryContainsKey(attachment, kCMSampleAttachmentKey_NotSync);
|
||||
}
|
||||
|
||||
if (isKeyframe) {
|
||||
RTC_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());
|
||||
RTCRtpFragmentationHeader *header;
|
||||
{
|
||||
std::unique_ptr<webrtc::RTPFragmentationHeader> header_cpp;
|
||||
bool result =
|
||||
H264CMSampleBufferToAnnexBBuffer(sampleBuffer, isKeyframe, buffer.get(), &header_cpp);
|
||||
header = [[RTCRtpFragmentationHeader alloc] initWithNativeFragmentationHeader:header_cpp.get()];
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
RTCEncodedImage *frame = [[RTCEncodedImage alloc] init];
|
||||
frame.buffer = [NSData dataWithBytesNoCopy:buffer->data() length:buffer->size() freeWhenDone:NO];
|
||||
frame.encodedWidth = width;
|
||||
frame.encodedHeight = height;
|
||||
frame.completeFrame = YES;
|
||||
frame.frameType = isKeyframe ? RTCFrameTypeVideoFrameKey : RTCFrameTypeVideoFrameDelta;
|
||||
frame.captureTimeMs = renderTimeMs;
|
||||
frame.timeStamp = timestamp;
|
||||
frame.rotation = rotation;
|
||||
frame.contentType = (_mode == RTCVideoCodecModeScreensharing) ? RTCVideoContentTypeScreenshare :
|
||||
RTCVideoContentTypeUnspecified;
|
||||
frame.flags = webrtc::VideoSendTiming::kInvalid;
|
||||
|
||||
int qp;
|
||||
_h264BitstreamParser.ParseBitstream(buffer->data(), buffer->size());
|
||||
_h264BitstreamParser.GetLastSliceQp(&qp);
|
||||
frame.qp = @(qp);
|
||||
|
||||
BOOL res = _callback(frame, codecSpecificInfo, header);
|
||||
if (!res) {
|
||||
RTC_LOG(LS_ERROR) << "Encode callback failed";
|
||||
return;
|
||||
}
|
||||
_bitrateAdjuster->Update(frame.buffer.length);
|
||||
}
|
||||
|
||||
- (RTCVideoEncoderQpThresholds *)scalingSettings {
|
||||
return [[RTCVideoEncoderQpThresholds alloc] initWithThresholdsLow:kLowH264QpThreshold
|
||||
high:kHighH264QpThreshold];
|
||||
}
|
||||
|
||||
@end
|
||||
@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017 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 "helpers.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/logging.h"
|
||||
|
||||
// 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);
|
||||
RTC_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);
|
||||
RTC_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);
|
||||
RTC_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);
|
||||
RTC_LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
|
||||
<< " to " << val_string << ": " << status;
|
||||
}
|
||||
}
|
||||
@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017 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 SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_HELPERS_H_
|
||||
#define SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_HELPERS_H_
|
||||
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <VideoToolbox/VideoToolbox.h>
|
||||
#include <string>
|
||||
|
||||
// Convenience function for creating a dictionary.
|
||||
inline CFDictionaryRef CreateCFTypeDictionary(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);
|
||||
|
||||
// Convenience function for setting a VT property.
|
||||
void SetVTSessionProperty(VTSessionRef session, CFStringRef key, int32_t value);
|
||||
|
||||
// Convenience function for setting a VT property.
|
||||
void SetVTSessionProperty(VTSessionRef session,
|
||||
CFStringRef key,
|
||||
uint32_t value);
|
||||
|
||||
// Convenience function for setting a VT property.
|
||||
void SetVTSessionProperty(VTSessionRef session, CFStringRef key, bool value);
|
||||
|
||||
// Convenience function for setting a VT property.
|
||||
void SetVTSessionProperty(VTSessionRef session,
|
||||
CFStringRef key,
|
||||
CFStringRef value);
|
||||
|
||||
#endif // SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_HELPERS_H_
|
||||
@ -1,351 +0,0 @@
|
||||
/*
|
||||
* 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 "sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h"
|
||||
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_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,
|
||||
std::unique_ptr<RTPFragmentationHeader>* out_header) {
|
||||
RTC_DCHECK(avcc_sample_buffer);
|
||||
RTC_DCHECK(out_header);
|
||||
out_header->reset(nullptr);
|
||||
|
||||
// Get format description from the sample buffer.
|
||||
CMVideoFormatDescriptionRef description =
|
||||
CMSampleBufferGetFormatDescription(avcc_sample_buffer);
|
||||
if (description == nullptr) {
|
||||
RTC_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, ¶m_set_count, &nalu_header_size);
|
||||
if (status != noErr) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to get parameter set.";
|
||||
return false;
|
||||
}
|
||||
RTC_CHECK_EQ(nalu_header_size, kAvccHeaderByteSize);
|
||||
RTC_DCHECK_EQ(param_set_count, 2);
|
||||
|
||||
// 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, ¶m_set, ¶m_set_size, nullptr, nullptr);
|
||||
if (status != noErr) {
|
||||
RTC_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) {
|
||||
RTC_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) {
|
||||
RTC_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) {
|
||||
RTC_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<RTPFragmentationHeader> header(new 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 = std::move(header);
|
||||
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 (reader.SeekToNextNaluOfType(kSps)) {
|
||||
// Buffer contains an SPS NALU - skip it and the following PPS
|
||||
const uint8_t* data;
|
||||
size_t data_len;
|
||||
if (!reader.ReadNalu(&data, &data_len)) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to read SPS";
|
||||
return false;
|
||||
}
|
||||
if (!reader.ReadNalu(&data, &data_len)) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to read PPS";
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// No SPS NALU - start reading from the first NALU in the buffer
|
||||
reader.SeekToStart();
|
||||
}
|
||||
|
||||
// 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) {
|
||||
RTC_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) {
|
||||
RTC_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) {
|
||||
RTC_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) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to create sample buffer.";
|
||||
CFRelease(contiguous_buffer);
|
||||
return false;
|
||||
}
|
||||
CFRelease(contiguous_buffer);
|
||||
return true;
|
||||
}
|
||||
|
||||
CMVideoFormatDescriptionRef CreateVideoFormatDescription(
|
||||
const uint8_t* annexb_buffer,
|
||||
size_t annexb_buffer_size) {
|
||||
const uint8_t* param_set_ptrs[2] = {};
|
||||
size_t param_set_sizes[2] = {};
|
||||
AnnexBBufferReader reader(annexb_buffer, annexb_buffer_size);
|
||||
// Skip everyting before the SPS, then read the SPS and PPS
|
||||
if (!reader.SeekToNextNaluOfType(kSps)) {
|
||||
return nullptr;
|
||||
}
|
||||
if (!reader.ReadNalu(¶m_set_ptrs[0], ¶m_set_sizes[0])) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to read SPS";
|
||||
return nullptr;
|
||||
}
|
||||
if (!reader.ReadNalu(¶m_set_ptrs[1], ¶m_set_sizes[1])) {
|
||||
RTC_LOG(LS_ERROR) << "Failed to read PPS";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Parse the SPS and PPS into a CMVideoFormatDescription.
|
||||
CMVideoFormatDescriptionRef description = nullptr;
|
||||
OSStatus status = CMVideoFormatDescriptionCreateFromH264ParameterSets(
|
||||
kCFAllocatorDefault, 2, param_set_ptrs, param_set_sizes, 4, &description);
|
||||
if (status != noErr) {
|
||||
RTC_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();
|
||||
}
|
||||
|
||||
AnnexBBufferReader::~AnnexBBufferReader() = default;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void AnnexBBufferReader::SeekToStart() {
|
||||
offset_ = offsets_.begin();
|
||||
}
|
||||
|
||||
bool AnnexBBufferReader::SeekToNextNaluOfType(NaluType type) {
|
||||
for (; offset_ != offsets_.end(); ++offset_) {
|
||||
if (offset_->payload_size < 1)
|
||||
continue;
|
||||
if (ParseNaluType(*(start_ + offset_->payload_start_offset)) == type)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
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
|
||||
@ -6,111 +6,6 @@
|
||||
* 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 SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_NALU_REWRITER_H_
|
||||
#define SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_NALU_REWRITER_H_
|
||||
|
||||
#include "modules/video_coding/codecs/h264/include/h264.h"
|
||||
|
||||
#include <CoreMedia/CoreMedia.h>
|
||||
#include <vector>
|
||||
|
||||
#include "common_video/h264/h264_common.h"
|
||||
#include "modules/include/module_common_types.h"
|
||||
#include "rtc_base/buffer.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,
|
||||
std::unique_ptr<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 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;
|
||||
|
||||
// Reset the reader to start reading from the first NALU
|
||||
void SeekToStart();
|
||||
|
||||
// Seek to the next position that holds a NALU of the desired type,
|
||||
// or the end if no such NALU is found.
|
||||
// Return true if a NALU of the desired type is found, false if we
|
||||
// reached the end instead
|
||||
bool SeekToNextNaluOfType(H264::NaluType type);
|
||||
|
||||
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 // SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_NALU_REWRITER_H_
|
||||
#import "components/video_codec/nalu_rewriter.h"
|
||||
|
||||
@ -1,233 +0,0 @@
|
||||
/*
|
||||
* 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 "common_video/h264/h264_common.h"
|
||||
#include "rtc_base/arraysize.h"
|
||||
#include "sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h"
|
||||
#include "test/gtest.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
using H264::kSps;
|
||||
|
||||
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, 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 sps_pps_not_at_start_buffer[] = {
|
||||
// Add some non-SPS/PPS NALUs at the beginning
|
||||
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x01,
|
||||
0xAB, 0x33, 0x21,
|
||||
// SPS nalu.
|
||||
0x00, 0x00, 0x01, 0x27, 0x42, 0x00, 0x1E, 0xAB, 0x40, 0xF0, 0x28, 0xD3,
|
||||
0x70, 0x20, 0x20, 0x20, 0x20,
|
||||
// PPS nalu.
|
||||
0x00, 0x00, 0x01, 0x28, 0xCE, 0x3C, 0x30};
|
||||
description = CreateVideoFormatDescription(
|
||||
sps_pps_not_at_start_buffer, arraysize(sps_pps_not_at_start_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(AnnexBBufferReaderTest, TestFindNextNaluOfType) {
|
||||
const uint8_t notSps = 0x1F;
|
||||
const uint8_t annex_b_test_data[] = {
|
||||
0x00, 0x00, 0x00, 0x01, kSps, 0x00, 0x00, 0x01, notSps,
|
||||
0x00, 0x00, 0x01, notSps, 0xDD, 0x00, 0x00, 0x01, notSps,
|
||||
0xEE, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
|
||||
0x01, 0x00, 0x00, 0x00, 0x01, kSps, 0xBB, 0x00, 0x00,
|
||||
0x01, notSps, 0x00, 0x00, 0x01, notSps, 0xDD, 0x00, 0x00,
|
||||
0x01, notSps, 0xEE, 0xFF, 0x00, 0x00, 0x00, 0x01};
|
||||
|
||||
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.FindNextNaluOfType(kSps));
|
||||
EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length));
|
||||
EXPECT_EQ(annex_b_test_data + 4, nalu);
|
||||
EXPECT_EQ(1u, nalu_length);
|
||||
|
||||
EXPECT_TRUE(reader.FindNextNaluOfType(kSps));
|
||||
EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length));
|
||||
EXPECT_EQ(annex_b_test_data + 32, nalu);
|
||||
EXPECT_EQ(2u, nalu_length);
|
||||
|
||||
EXPECT_FALSE(reader.FindNextNaluOfType(kSps));
|
||||
EXPECT_FALSE(reader.ReadNalu(&nalu, &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
|
||||
Reference in New Issue
Block a user