工程中需要导入一下头文件
#import "TuSDKFramework.h"
// 相机对象
TuSDKLiveVideoCamera*_camera;
遵守代理 TuSDKLiveVideoCameraDelegate
请求摄像头权限
/**
请求摄像头的使用权
*/
- (void)requestAccessForVideo
{
__weak typeof(self) weakSeTuSDK = self;
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (status)
{
case AVAuthorizationStatusNotDetermined:
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted)
{
}
}];
break;
}
case AVAuthorizationStatusAuthorized:
{
break;
}
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
break;
default:
break;
}
}
请求音频采集权限
/**
请求音频设备的使用权
*/
- (void)requestAccessForAudio
{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (status)
{
case AVAuthorizationStatusNotDetermined:
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:nil];
break;
}
case AVAuthorizationStatusAuthorized:
{
break;
}
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
break;
default:
break;
}
}
// 滤镜列表,获取滤镜前往 TuSDK.bundle/others/lsq_tusdk_configs.json
// TuSDK 滤镜信息介绍 @see-https://tutucloud.com/docs/ios/self-customize-filter
_videoFilters = @[@"Normal",@"VideoFair", @"VideoWhiteSkin"];
// 事件处理队列
_sessionQueue = dispatch_queue_create("org.lasque.tusdkvideo", DISPATCH_QUEUE_SERIAL);
if (!_cameraView) {
_cameraView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.getSizeWidth, self.view.getSizeHeight)];
[self.view addSubview:_cameraView];
}
// 初始化摄像头
// 可以配置采集的分辨率,默认为 AVCaptureSessionPresetHigh
_camera = [TuSDKLiveVideoCamera initWithSessionPreset:AVCaptureSessionPresetHigh cameraPosition:[AVCaptureDevice lsqFirstFrontCameraPosition] cameraView:_cameraView];
// 设置委托
_camera.videoDelegate = self;
// 预先设定几组编码质量,之后可以切换
CGSize videoSize = CGSizeMake(360, 640);
/**
输出画面分辨率,默认原始采样尺寸输出。
如果设置了输出尺寸,则对画面进行等比例缩放,必要时进行裁剪,保证输出尺寸和预设尺寸一致。
*/
_camera.outputSize = _outputSize;
/**
输出 PixelBuffer 格式,可选: lsqFormatTypeBGRA | lsqFormatTypeYUV420F | lsqFormatTypeRawData
默认:lsqFormatTypeBGRA
*/
// _camera.pixelFormatType = lsqFormatTypeYUV420F;
// 禁用持续自动对焦
_camera.disableContinueFoucs = NO;
// 视频覆盖区域颜色 (默认:[UIColor blackColor])
_camera.regionViewColor = [UIColor blackColor];
// 禁用前置摄像头自动水平镜像 (默认: NO,前置摄像头拍摄结果自动进行水平镜像)
_camera.disableMirrorFrontFacing = NO;
// 点击手动聚焦 (直播相机默认关闭)
_camera.disableTapFocus = YES;
// 视频视图显示比例 (默认:0, 0 <= mRegionRatio, 当设置为0时全屏显示)
_camera.cameraViewRatio = 0;
// 视频覆盖区域颜色 (默认:[UIColor blackColor])
_camera.regionViewColor = [UIColor blackColor];
// 默认闪光灯模式
[_camera flashWithMode:AVCaptureFlashModeOff];
/**
相机采集帧率,默认 20 帧
开启智能贴纸时,帧率建议范围:12 ~ 20
关闭智能贴纸时,帧率建议范围:12 ~ 30
*/
_camera.frameRate = 30;
// 启用智能贴纸
_camera.enableFaceAutoBeauty = YES;
// 设置默认滤镜;
[_camera switchFilterWithCode:_videoFilters[1]];
// 设置是否静音
[_camera setMuted:NO];
// 启动相机
[_camera tryStartCameraCapture];
AVCaptureSessionPresetHigh
。视频数据的回调方法
#pragma mark - TuSDKLiveVideoCameraDelegate 返回视频数据
/**
获取滤镜处理后的帧数据, pixelFormatType 为 lsqFormatTypeBGRA 或 lsqFormatTypeYUV420F 时调用
@param camera 相机
@param pixelBuffer 帧数据, CVPixelBufferRef 类型, 默认为 kCVPixelFormatType_32BGRA 格式
@param frameTime 帧时间戳
*/
- (void)onVideoCamera:(TuSDKLiveVideoCamera *)camera bufferData:(CVPixelBufferRef)pixelBuffer time:(CMTime)frameTime;
{
NSLog(@"抛出来的视频数据 new buffer size: %zu - %zu", CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer));
// 将 pixelBuffer 用于编码和推流
if(_startLiving)
{
// push
}
dispatch_async(dispatch_get_main_queue(), ^{
// 打印参数信息
[self debugInfoTime];
});
}
音频数据的回调方法
#pragma mark - TuSDKLiveVideoCameraDelegate 返回音频数据
/**
音频采集数据的回调接口
@param capture 音频采集
@param audioData 音频数据
*/
- (void)audioCaptureOutput:(TuSDKLiveVideoCamera *)capture audioData:(NSData*)audioData
{
NSLog(@"抛出来的音频数据%@",audioData);
if(_startLiving)
{
// push
}
}
直播相机初始化前需要配置滤镜列表
引入头文件 #import "FilterView.h"
Demo 提供使用范例,用户可根据接口自定义修改相关使用。
// 滤镜列表,获取滤镜前往 TuSDK.bundle/others/lsq_tusdk_configs.json
// TuSDK 滤镜信息介绍 @see-https://tusdk.com/docs/ios/self-customize-filter
_videoFilters = @[@"Normal",@"VideoFair", @"VideoWhiteSkin"];
初始化滤镜栏
CGFloat filterViewHeight = self.view.getSizeHeight - self.view.getSizeWidth - 61;
_filterView = [[FilterView alloc]initWithFrame:CGRectMake(0, self.view.getSizeHeight - filterViewHeight, self.view.getSizeWidth, filterViewHeight)];
// 相机初始化加载的默认的滤镜所对应的下标保持一致
_filterView.canAdjustParameter = true;
_filterView.filterEventDelegate = self;
_filterView.currentFilterTag = 1;
[_filterView createFilterWith:_videoFilters];
[_filterView refreshAdjustParameterViewWith:_currentFilter.code filterArgs:_currentFilter.filterParameter.args];
[self.view addSubview:_filterView];
遵守代理 FilterViewEventDelegate,实现代理方法。
#pragma mark -- 滤镜栏点击代理方法 FilterEventDelegate
- (void)filterViewParamChangedWith:(TuSDKICSeekBar *)seekbar changedProgress:(CGFloat)progress{
//根据tag获得当前滤镜的对应参数,修改precent;
NSInteger index = seekbar.tag;
TuSDKFilterArg *arg = _currentFilter.filterParameter.args[index];
arg.precent = progress;
//设置滤镜参数;
[_currentFilter submitParameter];
}
切换滤镜
- (void)filterViewSwitchFilterWithCode:(NSString *)filterCode{
//切换滤镜
[_camera switchFilterWithCode:filterCode];
}
/**
* 相机滤镜改变 (如需操作UI线程, 请检查当前线程是否为主线程)
*
* @param camera 相机对象
* @param newFilter 新的滤镜对象
*/
- (void)onVideoCamera:(id<TuSDKVideoCameraInterface>)camera filterChanged:(TuSDKFilterWrap *)newFilter;
{
//赋值新滤镜 同事刷新新滤镜的参数配置;
_currentFilter = newFilter;
[_filterView refreshAdjustParameterViewWith:newFilter.code filterArgs:newFilter.filterParameter.args];
}
引入头文件 #import "StickerScrollView.h"
Demo 提供使用范例,用户可根据接口自定义修改相关使用。
使用非标准版直播服务,请跳过该部分介绍
初始化贴纸栏
CGFloat stickerViewHeight = self.view.getSizeHeight - self.view.getSizeWidth - 61;
_stickerView = [[StickerScrollView alloc]initWithFrame:CGRectMake(0, self.view.getSizeHeight - stickerViewHeight, self.view.getSizeWidth, stickerViewHeight)];
_stickerView.stickerDelegate = self;
_stickerView.backgroundColor = [UIColor colorWithRed:0.22 green:0.22 blue:0.22 alpha:0.7];
[self.view addSubview:_stickerView];
遵守代理 StickerViewClickDelegate,实现代理方法。
#pragma mark -- 贴纸栏点击代理方法 StickerViewClickDelegate
- (void)clickStickerViewWith:(TuSDKPFStickerGroup *)stickGroup{
if (!stickGroup) {
//为nil时 移除已有贴纸组;
[_camera removeAllLiveSticker];
return;
}
//展示对应贴纸组;
[_camera showGroupSticker:stickGroup];
}
©2019-2024 TUTUCLOUD. All Rights Reserved. 杭州元凡视觉智能科技有限公司 | 浙ICP备14040447号-1 | 浙公网安备33010602001649号