有奖捉虫:办公协同&微信生态&物联网文档专题 HOT
TUICallEngine 中提供了getTRTCCloudInstance() 接口,可以通过该接口调用 TRTC 的高级特性 setLocalVideoProcessDelegete:pixelFormat:bufferType: 实现接入第三方美颜。
本文将介绍如何在 TUICallKit 中接入 腾讯特效 SDK其他第三方美颜接入方法类似,请结合第三方 SDK 文档进行接入

导入组件

您可以使用 CocoaPods 导入组件,具体如下:
1. 在您的 Podfile 文件中添加以下依赖。
Objective-C
pod 'XMagic'
2. 执行以下命令,安装组件。
pod install
如果无法安装 XMagic 最新版本,执行以下命令更新本地的 CocoaPods 仓库列表。
pod repo update
3. 添加美颜资源到实际项目工程中:
3.1 下载并解压对应套餐的 SDK 和美颜资源,将 resources 文件夹下的除 LightCore.bundleLight3DPlugin.bundleLightBodyPlugin.bundleLightHandPlugin.bundleLightSegmentPlugin.bundle、audio2exp.bundle 以外的其它 bundle 资源添加到实际工程中。
3.2 在 Build Settings 中的 Other Linker Flags 添加 -ObjC
4. Bundle Identifier 修改成与申请的测试授权一致。

授权

1. 申请授权,得到 LicenseURL 和 LicenseKEY,请参见 License 指引
注意:
正常情况下,只要 App 成功联网一次,就能完成鉴权流程,因此您不需要把 License 文件放到工程的工程目录里。但是如果您的 App 在从未联网的情况下也需要使用 SDK 相关功能,那么您可以把 License 文件下载下来放到工程目录,作为保底方案,此时 License 文件名必须是 v_cube.license
2. 在相关业务模块的初始化代码中设置 URL 和 KEY,触发 license 下载,避免在使用前才临时去下载。也可以在 AppDelegate 的 didFinishLaunchingWithOptions 方法里触发下载。其中,LicenseURL 和 LicenseKey 是控制台绑定 License 时生成的授权信息。
[TELicenseCheck setTELicense:LicenseURL key:LicenseKey completion:^(NSInteger authresult, NSString * _Nonnull errorMsg) {
if (authresult == TELicenseCheckOk) {
NSLog(@"鉴权成功");
} else {
NSLog(@"鉴权失败");
}
}];

设置 SDK 素材资源路径

#import <XMagic/XMagic.h>

- (void)buildBeautySDK:(int)width and:(int)height texture:(unsigned)textureID {
NSString *beautyConfigPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
beautyConfigPath = [beautyConfigPath stringByAppendingPathComponent:@"beauty_config.json"];
NSFileManager *localFileManager=[[NSFileManager alloc] init];
BOOL isDir = YES;
NSDictionary * beautyConfigJson = @{};
if ([localFileManager fileExistsAtPath:beautyConfigPath isDirectory:&isDir] && !isDir) {
NSString *beautyConfigJsonStr = [NSString stringWithContentsOfFile:beautyConfigPath encoding:NSUTF8StringEncoding error:nil];
NSError *jsonError;
NSData *objectData = [beautyConfigJsonStr dataUsingEncoding:NSUTF8StringEncoding];
beautyConfigJson = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
}
NSDictionary *assetsDict = @{@"core_name":@"LightCore.bundle",
@"root_path":[[NSBundle mainBundle] bundlePath],
@"tnn_"
@"beauty_config":beautyConfigJson
};

// 初始化SDK:width 和 height 分别是 texture 的宽高
self.xMagicKit = [[XMagic alloc] initWithRenderSize:CGSizeMake(width, height) assetsDict:assetsDict];
}

配置美颜各种效果(详细美颜效果配置请参考 美颜参数说明

- (int)configPropertyWithType:(NSString *_Nonnull)propertyType withName:(NSString *_Nonnull)propertyName withData:(NSString*_Nonnull)propertyValue withExtraInfo:(id _Nullable)extraInfo;

进行渲染处理

TRTC SDK 设置第三方美颜的视频数据回调:设置该回调之后,TRTC SDK 会把采集到的视频帧通过您设置的 delegate 回调出来,用于第三方美颜组件进行二次处理。
#import <TUICallEngine/TUICallEngine.h>

[[[TUICallEngine createInstance] getTRTCCloudInstance] setLocalVideoProcessDelegete:self pixelFormat:TRTCVideoPixelFormat_Texture_2D bufferType:TRTCVideoBufferType_Texture];
在视频帧回调接口 onProcessVideoFrame:(TRTCVideoFrame *_Nonnull)srcFrame dstFrame:(TRTCVideoFrame *_Nonnull)dstFrame
中,构造 YTProcessInput 传入到 SDK 内做渲染处理。
#pragma mark - TRTCVideoFrameDelegate

- (uint32_t)onProcessVideoFrame:(TRTCVideoFrame *_Nonnull)srcFrame dstFrame:(TRTCVideoFrame *_Nonnull)dstFrame {
    if (!self.xMagicKit) {
        [self buildBeautySDK:srcFrame.width and:srcFrame.height texture:srcFrame.textureId];
        self.heightF = srcFrame.height;
        self.widthF = srcFrame.width;
    }
    if(self.xMagicKit!=nil && (self.heightF!=srcFrame.height || self.widthF!=srcFrame.width)){
       self.heightF = srcFrame.height;
       self.widthF = srcFrame.width;
      [self.xMagicKit setRenderSize:CGSizeMake(srcFrame.width, srcFrame.height)];
    }
   
YTProcessInput *input = [[YTProcessInput alloc] init];
    input.textureData = [[YTTextureData alloc] init];
    input.textureData.texture = srcFrame.textureId;
    input.textureData.textureWidth = srcFrame.width;
    input.textureData.textureHeight = srcFrame.height;
    input.dataType = kYTTextureData;
    YTProcessOutput *output = [self.xMagicKit process:input withOrigin:YtLightImageOriginTopLeft withOrientation:YtLightCameraRotation0];
    dstFrame.textureId = output.textureData.texture;
    return 0;
}

集成效果

下图是 TUICallKit 含 UI 组件集成腾讯特效 SDK 的效果: