Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
Fix some issue to enable HEVC on iOS.
Browse files Browse the repository at this point in the history
- Add some missing classes.
- Define OWT_USE_H265 when rtc_use_h265 is true.
  • Loading branch information
jianjunz committed Oct 15, 2019
1 parent 4729846 commit f65f295
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 24 deletions.
2 changes: 2 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ config("common_inherited_config") {

if (!rtc_use_h265) {
defines += [ "DISABLE_H265" ]
} else {
defines += [ "OWT_USE_H265" ]
}
}

Expand Down
2 changes: 2 additions & 0 deletions modules/rtp_rtcp/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ rtc_static_library("rtp_rtcp") {

if (!rtc_use_h265) {
defines += ["DISABLE_H265"]
} else {
defines += [ "OWT_USE_H265" ]
}

deps = [
Expand Down
23 changes: 18 additions & 5 deletions sdk/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,16 @@ if (is_ios || is_mac) {
"objc/components/video_codec/RTCH264ProfileLevelId.h",
"objc/components/video_codec/RTCH264ProfileLevelId.mm",
]
if (rtc_use_h265) {
sources += [
# TODO: Split this file.
"objc/Framework/Headers/WebRTC/RTCVideoCodecH265.h",
"objc/components/video_codec/RTCCodecSpecificInfoH265+Private.h",
"objc/components/video_codec/RTCCodecSpecificInfoH265.h",
"objc/components/video_codec/RTCCodecSpecificInfoH265.mm",
"objc/components/video_codec/RTCVideoCodecH265.mm",
]
}
if (is_ios) {
sources += [
"objc/components/video_codec/UIDevice+H264Profile.h",
Expand Down Expand Up @@ -1631,13 +1641,16 @@ if (is_ios || is_mac) {
"objc/components/video_codec/RTCVideoEncoderFactoryH264.m",
"objc/components/video_codec/RTCVideoEncoderH264.h",
"objc/components/video_codec/RTCVideoEncoderH264.mm",
"objc/Framework/Headers/WebRTC/RTCVideoCodecH265.h",
"objc/components/video_codec/RTCVideoEncoderH265.mm",
"objc/components/video_codec/RTCVideoCodecH265.mm",
"objc/components/video_codec/RTCVideoDecoderH265.h",
"objc/components/video_codec/RTCVideoDecoderH265.mm",
]

if (rtc_use_h265) {
sources += [
"objc/components/video_codec/RTCVideoDecoderH265.h",
"objc/components/video_codec/RTCVideoDecoderH265.mm",
"objc/components/video_codec/RTCVideoEncoderH265.mm",
]
}

configs += [
"..:common_objc",
":used_from_extension",
Expand Down
5 changes: 1 addition & 4 deletions sdk/objc/Framework/Headers/WebRTC/RTCVideoCodecH265.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
#import <WebRTC/RTCMacros.h>
#import <WebRTC/RTCVideoCodecFactory.h>

RTC_OBJC_EXPORT
API_AVAILABLE(ios(11.0))
@interface RTCCodecSpecificInfoH265 : NSObject <RTCCodecSpecificInfo>
@end
RTC_OBJC_EXPORT extern NSString *const kRTCVideoCodecH265Name;

/** Encoder. */
RTC_OBJC_EXPORT
Expand Down
25 changes: 25 additions & 0 deletions sdk/objc/components/video_codec/RTCCodecSpecificInfoH265+Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 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.
*/
/* This file is borrowed from sdk/objc/components/video_codec/RTCCodecSpecificInfoH264+Private.h */

#import "RTCCodecSpecificInfoH265.h"

#include "modules/video_coding/include/video_codec_interface.h"

NS_ASSUME_NONNULL_BEGIN

/* Interfaces for converting to/from internal C++ formats. */
@interface RTCCodecSpecificInfoH265 ()

- (webrtc::CodecSpecificInfo)nativeCodecSpecificInfo;

@end

NS_ASSUME_NONNULL_END
28 changes: 28 additions & 0 deletions sdk/objc/components/video_codec/RTCCodecSpecificInfoH265.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 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.
*/
/* This file is borrowed from sdk/objc/components/video_codec/RTCCodecSpecificInfoH264.h. */

#import <Foundation/Foundation.h>

#import "RTCCodecSpecificInfo.h"
#import "RTCMacros.h"

/** Class for H265 specific config. */
typedef NS_ENUM(NSUInteger, RTCH265PacketizationMode) {
RTCH265PacketizationModeNonInterleaved = 0, // Mode 1 - STAP-A, FU-A is allowed
RTCH265PacketizationModeSingleNalUnit // Mode 0 - only single NALU allowed
};

RTC_OBJC_EXPORT
@interface RTCCodecSpecificInfoH265 : NSObject <RTCCodecSpecificInfo>

@property(nonatomic, assign) RTCH265PacketizationMode packetizationMode;

@end
28 changes: 28 additions & 0 deletions sdk/objc/components/video_codec/RTCCodecSpecificInfoH265.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 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.
*/
/* This file is borrowed from sdk/objc/components/video_codec/RTCCodecSpecificInfoH264.mm */

#import "RTCCodecSpecificInfoH265+Private.h"

// H265 specific settings.
@implementation RTCCodecSpecificInfoH265

@synthesize packetizationMode = _packetizationMode;

- (webrtc::CodecSpecificInfo)nativeCodecSpecificInfo {
webrtc::CodecSpecificInfo codecSpecificInfo;
codecSpecificInfo.codecType = webrtc::kVideoCodecH265;
codecSpecificInfo.codecSpecific.H265.packetization_mode =
(webrtc::H265PacketizationMode)_packetizationMode;

return codecSpecificInfo;
}

@end
10 changes: 4 additions & 6 deletions sdk/objc/components/video_codec/RTCVideoCodecH265.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@
/* This file is borrowed from
* webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCVideoCodecH265.mm */

#import "WebRTC/RTCVideoCodec.h"
#import "WebRTC/RTCVideoCodecH265.h"

#include <vector>

#import "WebRTC/RTCVideoCodec.h"

#include "media/base/media_constants.h"
#include "rtc_base/time_utils.h"
#include "system_wrappers/include/field_trial.h"

NSString *const kRTCVideoCodecH265Name = @(cricket::kH265CodecName);

static NSString* kH265CodecName = @"H265";
// TODO(jianjunz): This is value is not correct.
static NSString* kLevel31Main = @"4d001f";

@implementation RTCCodecSpecificInfoH265
@end

// Encoder factory.
@implementation RTCVideoEncoderFactoryH265

Expand Down
18 changes: 9 additions & 9 deletions sdk/objc/components/video_codec/RTCVideoEncoderH265.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
*
*/

#import "WebRTC/RTCVideoCodecH265.h"

#import <VideoToolbox/VideoToolbox.h>
#include <vector>

#if defined(WEBRTC_IOS)
#import "helpers/UIDevice+RTCDevice.h"
#endif
#import "RTCCodecSpecificInfoH265.h"
#import "WebRTC/RTCVideoCodec.h"
#import "WebRTC/RTCVideoCodecH265.h"
#import "WebRTC/RTCVideoFrame.h"
#import "WebRTC/RTCVideoFrameBuffer.h"
#import "helpers.h"
#import "sdk/objc/Framework/Classes/PeerConnection/RTCVideoCodec+Private.h"
#if defined(WEBRTC_IOS)
#import "helpers/UIDevice+RTCDevice.h"
#endif

#include <vector>
#include "common_video/h264/profile_level_id.h"
#include "common_video/include/bitrate_adjuster.h"
#include "libyuv/convert_from.h"
Expand All @@ -28,8 +30,6 @@
#include "rtc_base/buffer.h"
#include "rtc_base/logging.h"
#include "rtc_base/time_utils.h"
#import "sdk/objc/Framework/Classes/PeerConnection/RTCVideoCodec+Private.h"
#import "helpers.h"
#include "sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h"
#include "system_wrappers/include/clock.h"

Expand Down
6 changes: 6 additions & 0 deletions sdk/objc/components/video_codec/nalu_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
return true;
}

#ifdef OWT_USE_H265
bool H265CMSampleBufferToAnnexBBuffer(
CMSampleBufferRef hvcc_sample_buffer,
bool is_keyframe,
Expand Down Expand Up @@ -474,6 +475,7 @@ bool H265AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
CFRelease(contiguous_buffer);
return true;
}
#endif

CMVideoFormatDescriptionRef CreateVideoFormatDescription(
const uint8_t* annexb_buffer,
Expand Down Expand Up @@ -505,6 +507,7 @@ CMVideoFormatDescriptionRef CreateVideoFormatDescription(
return description;
}

#ifdef OWT_USE_H265
CMVideoFormatDescriptionRef CreateH265VideoFormatDescription(
const uint8_t* annexb_buffer,
size_t annexb_buffer_size) {
Expand Down Expand Up @@ -539,6 +542,7 @@ CMVideoFormatDescriptionRef CreateH265VideoFormatDescription(
}
return description;
}
#endif

AnnexBBufferReader::AnnexBBufferReader(const uint8_t* annexb_buffer,
size_t length)
Expand Down Expand Up @@ -587,6 +591,7 @@ bool AnnexBBufferReader::SeekToNextNaluOfType(NaluType type) {
return false;
}

#ifdef OWT_USE_H265
bool AnnexBBufferReader::SeekToNextNaluOfType(H265::NaluType type) {
for (; offset_ != offsets_.end(); ++offset_) {
if (offset_->payload_size < 1)
Expand All @@ -596,6 +601,7 @@ bool AnnexBBufferReader::SeekToNextNaluOfType(H265::NaluType type) {
}
return false;
}
#endif

AvccBufferWriter::AvccBufferWriter(uint8_t* const avcc_buffer, size_t length)
: start_(avcc_buffer), offset_(0), length_(length) {
Expand Down
8 changes: 8 additions & 0 deletions sdk/objc/components/video_codec/nalu_rewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include <vector>

#include "common_video/h264/h264_common.h"
#ifdef OWT_USE_H265
#include "common_video/h265/h265_common.h"
#endif
#include "modules/include/module_common_types.h"
#include "rtc_base/buffer.h"

Expand Down Expand Up @@ -48,6 +50,7 @@ bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
CMSampleBufferRef* out_sample_buffer,
CMMemoryPoolRef memory_pool);

#ifdef OWT_USE_H265
// Converts a sample buffer emitted from the VideoToolbox encoder into a buffer
// suitable for RTP. The sample buffer is in hvcc format whereas the rtp buffer
// needs to be in Annex B format. Data is written directly to |annexb_buffer|
Expand All @@ -70,6 +73,7 @@ bool H265AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
CMVideoFormatDescriptionRef video_format,
CMSampleBufferRef* out_sample_buffer)
__OSX_AVAILABLE_STARTING(__MAC_10_12, __IPHONE_11_0);
#endif

// 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.
Expand All @@ -78,10 +82,12 @@ CMVideoFormatDescriptionRef CreateVideoFormatDescription(
const uint8_t* annexb_buffer,
size_t annexb_buffer_size);

#ifdef OWT_USE_H265
CMVideoFormatDescriptionRef CreateH265VideoFormatDescription(
const uint8_t* annexb_buffer,
size_t annexb_buffer_size)
__OSX_AVAILABLE_STARTING(__MAC_10_12, __IPHONE_11_0);
#endif

// Helper class for reading NALUs from an RTP Annex B buffer.
class AnnexBBufferReader final {
Expand All @@ -107,7 +113,9 @@ class AnnexBBufferReader final {
// Return true if a NALU of the desired type is found, false if we
// reached the end instead
bool SeekToNextNaluOfType(H264::NaluType type);
#ifdef OWT_USE_H265
bool SeekToNextNaluOfType(H265::NaluType type);
#endif

private:
// Returns the the next offset that contains NALU data.
Expand Down
8 changes: 8 additions & 0 deletions sdk/objc/native/src/objc_video_encoder_factory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#import "base/RTCVideoEncoder.h"
#import "base/RTCVideoEncoderFactory.h"
#import "components/video_codec/RTCCodecSpecificInfoH264+Private.h"
#ifdef OWT_USE_H265
#import "components/video_codec/RTCCodecSpecificInfoH265+Private.h"
#endif
#import "sdk/objc/api/peerconnection/RTCEncodedImage+Private.h"
#import "sdk/objc/api/peerconnection/RTCRtpFragmentationHeader+Private.h"
#import "sdk/objc/api/peerconnection/RTCVideoCodecInfo+Private.h"
Expand Down Expand Up @@ -62,6 +65,11 @@ int32_t RegisterEncodeCompleteCallback(EncodedImageCallback *callback) override
if ([NSStringFromClass([info class]) isEqual:@"RTCCodecSpecificInfoH264"]) {
// if ([info isKindOfClass:[RTCCodecSpecificInfoH264 class]]) {
codecSpecificInfo = [(RTCCodecSpecificInfoH264 *)info nativeCodecSpecificInfo];
#ifdef OWT_USE_H265
} else if ([NSStringFromClass([info class]) isEqual:@"RTCCodecSpecificInfoH265"]) {
// if ([info isKindOfClass:[RTCCodecSpecificInfoH265 class]]) {
codecSpecificInfo = [(RTCCodecSpecificInfoH265 *)info nativeCodecSpecificInfo];
#endif
}

std::unique_ptr<RTPFragmentationHeader> fragmentationHeader =
Expand Down

0 comments on commit f65f295

Please sign in to comment.