在Mac平台下,WebRTC的demo默认使用VideoToolbox进行h264硬件编码。本文档的目标是调用OpenH264进行软编码。
配置与H264相关的宏
两条命令,第一条生成xcode下的工程文件,第二条查看可用的args选项。
gn gen --args='target_cpu="x64" proprietary_codecs=true rtc_use_h264=true use_openh264=true' --ide=xcode <out_dir>
gn args --list <out_dir>
The variable "rtc_use_264" was set as a build argument
but never appeared in a declare_args() block in any buildfile.
To view all possible args, run "gn args --list <out_dir>"
The build continued as if that argument was unspecified.
修改Demo
Demo调用硬编码的代码如下:
std::unique_ptr<VideoEncoder> ObjCVideoEncoderFactory::CreateVideoEncoder(
const SdpVideoFormat &format) {
RTCVideoCodecInfo *info = [[RTCVideoCodecInfo alloc] initWithNativeSdpVideoFormat:format];
id<RTCVideoEncoder> encoder = [encoder_factory_ createEncoder:info];
if ([encoder isKindOfClass:[RTCWrappedNativeVideoEncoder class]]) {
// [read-note] 软编码实例化:vp8、vp9,还有这次加的OpenH264
return [(RTCWrappedNativeVideoEncoder *)encoder releaseWrappedEncoder];
} else {
// [read-note] 硬编码实例化:videotoolbox-h264
return std::unique_ptr<ObjCVideoEncoder>(new ObjCVideoEncoder(encoder));
}
}
如上,vp8、vp9编码时调用RTCWrappedNativeVideoEncoder类,而H264实现的RTCVideoEncoderH264调用VT进行硬编码。所以思路是再封装一个RTCWrappedNativeVideoEncoder类进行OpenH264软编码。增加两个文件:RTCVideoEncoderOpenH264.h、RTCVideoEncoderOpenH264.mm,再创建Encoder时调用RTCVideoEncoderOpenH264,如下:
- (id<RTCVideoEncoder>)createEncoder:(RTCVideoCodecInfo *)info {
if ([info.name isEqualToString:kRTCVideoCodecH264Name]) {
//return [[RTCVideoEncoderH264 alloc] initWithCodecInfo:info];
return [RTCVideoEncoderOpenH264 openH264Encoder];
} else if ([info.name isEqualToString:kRTCVideoCodecVp8Name]) {
return [RTCVideoEncoderVP8 vp8Encoder];
#if defined(RTC_ENABLE_VP9)
} else if ([info.name isEqualToString:kRTCVideoCodecVp9Name]) {
return [RTCVideoEncoderVP9 vp9Encoder];
#endif
}
return nil;
}
修改编译配置
由于Demo使用gn构建,ninja编译,所以需要修改gn脚本把刚才添加的两个文件包含到项目中。
修改sdk/build.gn
rtc_static_library("default_codec_factory_objc") {
sources = [
"objc/components/video_codec/RTCDefaultVideoDecoderFactory.h",
"objc/components/video_codec/RTCDefaultVideoDecoderFactory.m",
"objc/components/video_codec/RTCDefaultVideoEncoderFactory.h",
"objc/components/video_codec/RTCDefaultVideoEncoderFactory.m",
]
deps = [
":base_objc",
":native_video",
":videocodec_objc",
":videotoolbox_objc",
":vp8",
":OpenH264",
":vp9",
":vpx_codec_constants",
]
}
... ...
rtc_static_library("OpenH264") {
visibility = [ "*" ]
allow_poison = [ "software_video_codecs" ]
sources = [
"objc/api/video_codec/RTCVideoEncoderOpenH264.h",
"objc/api/video_codec/RTCVideoEncoderOpenH264.mm",
]
deps = [
":base_objc",
":wrapped_native_codec_objc",
"../modules/video_coding:webrtc_h264",
]
}
最终验证可以调用OpenH264编码,解码使用VT硬解码。
网友评论