Commit e5ad0a4094a6cfa48cfb5c86b57cc0cc2860fac1
1 parent
04a4bdb5
添加滤镜,转换摄像头,去掉水印
Showing
1 changed file
with
81 additions
and
34 deletions
LiveVideoCloud/LiveVideoCloud/Classes/Sections/LiveSections/Controller/LVCLiveViewController.m
| ... | ... | @@ -9,11 +9,12 @@ |
| 9 | 9 | #import "LVCLiveViewController.h" |
| 10 | 10 | #import <CNLivePlayerSDK/CNLiveGPUStreamerKit.h> |
| 11 | 11 | #import <CNLivePlayerSDK/CNLiveWebcastManager.h> |
| 12 | +#import "FilterFunctionView.h" | |
| 12 | 13 | |
| 13 | 14 | #define KAppId @"60_irn9edco29" |
| 14 | 15 | #define KAppKey @"69fe70bda67e059286ad509d9f5377f815955645b52f94" |
| 15 | 16 | |
| 16 | -@interface LVCLiveViewController () { | |
| 17 | +@interface LVCLiveViewController ()<UIGestureRecognizerDelegate> { | |
| 17 | 18 | // 开始直播 |
| 18 | 19 | UIButton *_startLiveBtn; |
| 19 | 20 | // 缩放因子 |
| ... | ... | @@ -24,6 +25,8 @@ |
| 24 | 25 | |
| 25 | 26 | @property (nonatomic,strong) CNLiveGPUStreamerKit *kitCN; |
| 26 | 27 | |
| 28 | +@property (nonatomic,strong) FilterFunctionView *filterView; | |
| 29 | + | |
| 27 | 30 | @end |
| 28 | 31 | |
| 29 | 32 | @implementation LVCLiveViewController |
| ... | ... | @@ -69,7 +72,9 @@ |
| 69 | 72 | // _kitCN.activityName = @"iOS test"; |
| 70 | 73 | // _kitCN.coverImgUrl = @"djfiwooa"; |
| 71 | 74 | |
| 75 | + // 设置推流配置 | |
| 72 | 76 | [self setStreamerCfg]; |
| 77 | + // 添加监听 | |
| 73 | 78 | [self addObservers]; |
| 74 | 79 | |
| 75 | 80 | _startLiveBtn = [self addButton:@"开始直播" frame:CGRectMake(100, 40, 80, 40) action:@selector(startCapture:)]; |
| ... | ... | @@ -77,7 +82,7 @@ |
| 77 | 82 | [_startLiveBtn setTitle:@"停止直播" forState:UIControlStateSelected]; |
| 78 | 83 | |
| 79 | 84 | [self addButton:@"关闭预览" frame:CGRectMake(10, 40, 80, 40) action:@selector(startPreview:)]; |
| 80 | - [self addButton:@"退出" frame:CGRectMake(190, 40, 80, 40) action:@selector(quit)]; | |
| 85 | +// [self addButton:@"退出" frame:CGRectMake(190, 40, 80, 40) action:@selector(quit)]; | |
| 81 | 86 | [self addButton:@"美颜" frame:CGRectMake(10, 100, 80, 40) action:@selector(openFilter:)]; |
| 82 | 87 | [self addButton:@"摄像头" frame:CGRectMake(100, 100, 80, 40) action:@selector(changeCamera:)]; |
| 83 | 88 | [self addButton:@"闪光灯" frame:CGRectMake(190, 100, 80, 40) action:@selector(openFlash)]; |
| ... | ... | @@ -120,16 +125,16 @@ |
| 120 | 125 | [_kitCN startPreview:self.captureView]; |
| 121 | 126 | |
| 122 | 127 | //水印图片 |
| 123 | - _kitCN.watermark.logoPic = [UIImage imageNamed:@"互动电视180x180"]; | |
| 124 | - _kitCN.watermark.logoRect = CGRectMake(0.8, 0.8, 50, 50); | |
| 125 | - _kitCN.watermark.logoAlpha = 0.6; | |
| 128 | +// _kitCN.watermark.logoPic = [UIImage imageNamed:@"互动电视180x180"]; | |
| 129 | +// _kitCN.watermark.logoRect = CGRectMake(0.8, 0.8, 50, 50); | |
| 130 | +// _kitCN.watermark.logoAlpha = 0.6; | |
| 126 | 131 | |
| 127 | 132 | //水印文字 |
| 128 | - _kitCN.watermark.textLabel = [[UILabel alloc] init]; | |
| 129 | - _kitCN.watermark.textLabel.text = [NSString stringWithFormat:@"cnlive"]; | |
| 130 | - _kitCN.watermark.textLabel.font = [UIFont systemFontOfSize:8]; | |
| 131 | - _kitCN.watermark.textRect = CGRectMake(0.78, 0.88, 0.2, 0.04); | |
| 132 | - [_kitCN.watermark updateTextLabel]; | |
| 133 | +// _kitCN.watermark.textLabel = [[UILabel alloc] init]; | |
| 134 | +// _kitCN.watermark.textLabel.text = [NSString stringWithFormat:@"cnlive"]; | |
| 135 | +// _kitCN.watermark.textLabel.font = [UIFont systemFontOfSize:8]; | |
| 136 | +// _kitCN.watermark.textRect = CGRectMake(0.78, 0.88, 0.2, 0.04); | |
| 137 | +// [_kitCN.watermark updateTextLabel]; | |
| 133 | 138 | |
| 134 | 139 | } |
| 135 | 140 | |
| ... | ... | @@ -160,7 +165,7 @@ |
| 160 | 165 | _startLiveBtn.selected = NO; |
| 161 | 166 | }]; |
| 162 | 167 | }else{ |
| 163 | -// _stateLab.text = nil; | |
| 168 | + | |
| 164 | 169 | [_kitCN stopStream]; |
| 165 | 170 | } |
| 166 | 171 | } |
| ... | ... | @@ -184,6 +189,59 @@ |
| 184 | 189 | } |
| 185 | 190 | } |
| 186 | 191 | |
| 192 | +// 美颜滤镜 | |
| 193 | +- (void)openFilter:(UIButton *)button { | |
| 194 | + | |
| 195 | + button.selected = !button.selected; | |
| 196 | + | |
| 197 | + if (button.selected) { | |
| 198 | + | |
| 199 | + _filterView.hidden = NO; | |
| 200 | + | |
| 201 | + __weak LVCLiveViewController *weakselff = self; | |
| 202 | + | |
| 203 | + _filterView.filterViewBlock = ^(float grindRatio , float whitenRatio, float intensity){ | |
| 204 | + | |
| 205 | + [weakselff.kitCN.beautifyFaceFilter filterAdjustingParameterWithGrindRatio:grindRatio | |
| 206 | + whitenRatio:whitenRatio | |
| 207 | + intensity:intensity]; | |
| 208 | + }; | |
| 209 | + | |
| 210 | + _filterView.currentTypeBlock = ^(NSInteger currentType){ | |
| 211 | + [weakselff.kitCN.beautifyFaceFilter setBeautyType:currentType]; | |
| 212 | + }; | |
| 213 | + | |
| 214 | + _filterView.currentIdxBlock = ^(NSInteger currentIdx){ | |
| 215 | + weakselff.kitCN.beautifyFaceFilter.curEffectIdx = currentIdx; | |
| 216 | + }; | |
| 217 | + | |
| 218 | + } else { | |
| 219 | + | |
| 220 | + _filterView.hidden = YES; | |
| 221 | + | |
| 222 | + } | |
| 223 | +} | |
| 224 | + | |
| 225 | +// 转换前后置摄像头 | |
| 226 | +- (void)changeCamera:(UIButton *)button { | |
| 227 | + | |
| 228 | +// button.selected = !button.selected; | |
| 229 | +// if (button.selected) { | |
| 230 | +// _kitCN.cameraPosition = AVCaptureDevicePositionBack; | |
| 231 | +// }else{ | |
| 232 | +// _kitCN.cameraPosition = AVCaptureDevicePositionFront; | |
| 233 | +// } | |
| 234 | + [_kitCN switchCamera]; | |
| 235 | + | |
| 236 | +} | |
| 237 | + | |
| 238 | +// 闪光灯 | |
| 239 | +- (void)openFlash { | |
| 240 | + | |
| 241 | + [_kitCN toggleTorch]; | |
| 242 | + // [_kitCN setTorchMode:AVCaptureTorchModeOn]; | |
| 243 | +} | |
| 244 | + | |
| 187 | 245 | // 退出 |
| 188 | 246 | - (void)closeBtnClivk { |
| 189 | 247 | |
| ... | ... | @@ -200,8 +258,8 @@ |
| 200 | 258 | |
| 201 | 259 | #pragma mark - UIGestureRecognizer Action !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 202 | 260 | //对焦与曝光 |
| 203 | -- (void)onTap:(UIGestureRecognizer *)sender | |
| 204 | -{ | |
| 261 | +- (void)onTap:(UIGestureRecognizer *)sender { | |
| 262 | + | |
| 205 | 263 | CGPoint current = [sender locationInView:self.view]; |
| 206 | 264 | CGPoint point = [self convertToPointOfInterestFromViewCoordinates:current]; |
| 207 | 265 | [_kitCN exposureAtPoint:point]; |
| ... | ... | @@ -209,7 +267,8 @@ |
| 209 | 267 | } |
| 210 | 268 | |
| 211 | 269 | //缩放 |
| 212 | -- (void)pinchDetected:(UIPinchGestureRecognizer *)recognizer{ | |
| 270 | +- (void)pinchDetected:(UIPinchGestureRecognizer *)recognizer { | |
| 271 | + | |
| 213 | 272 | if (recognizer.state == UIGestureRecognizerStateBegan) { |
| 214 | 273 | _currentPinchZoomFactor = _kitCN.pinchZoomFactor; |
| 215 | 274 | } |
| ... | ... | @@ -233,40 +292,34 @@ |
| 233 | 292 | |
| 234 | 293 | if ( _kitCN.captureState == CNLiveCaptureStateIdle){ |
| 235 | 294 | //初始化时状态为空闲 |
| 236 | -// _stateLab.text = @"CNLiveCaptureStateIdle"; | |
| 237 | 295 | |
| 238 | 296 | return; |
| 239 | 297 | } |
| 240 | 298 | else if (_kitCN.captureState == CNLiveCaptureStateCapturing ) { |
| 241 | 299 | //设备工作中 |
| 242 | -// _stateLab.text = @"CNLiveCaptureStateCapturing"; | |
| 243 | 300 | |
| 244 | 301 | return; |
| 245 | 302 | } |
| 246 | 303 | else if (_kitCN.captureState == CNLiveCaptureStateClosingCapture ) { |
| 247 | 304 | //关闭采集设备中 |
| 248 | -// _stateLab.text = @"CNLiveCaptureStateClosingCapture"; | |
| 249 | 305 | |
| 250 | 306 | return; |
| 251 | 307 | } |
| 252 | 308 | else if (_kitCN.captureState == CNLiveCaptureStateDevAuthDenied ) { |
| 253 | 309 | //设备授权被拒绝 |
| 254 | 310 | NSLog(@"---------CNLiveCaptureStateDevAuthDenied"); |
| 255 | -// _stateLab.text = @"camera/mic Authorization Denied"; | |
| 256 | 311 | |
| 257 | 312 | return; |
| 258 | 313 | } |
| 259 | 314 | else if (_kitCN.captureState == CNLiveCaptureStateParameterError ) { |
| 260 | 315 | // 参数错误,无法打开 |
| 261 | 316 | NSLog(@"---------CNLiveCaptureStateParameterError"); |
| 262 | -// _stateLab.text = @"capture devices ParameterError"; | |
| 263 | 317 | |
| 264 | 318 | return; |
| 265 | 319 | } |
| 266 | 320 | else if (_kitCN.captureState == CNLiveCaptureStateDevBusy ) { |
| 267 | 321 | //设备正忙,请稍后尝试 |
| 268 | 322 | NSLog(@"---------CNLiveCaptureStateDevBusy"); |
| 269 | -// _stateLab.text = @"device busy, try later"; | |
| 270 | 323 | |
| 271 | 324 | } |
| 272 | 325 | } |
| ... | ... | @@ -276,19 +329,16 @@ |
| 276 | 329 | if (_kitCN.streamState == CNLiveStreamStateIdle) { |
| 277 | 330 | |
| 278 | 331 | NSLog(@"---------CNLiveStreamStateIdle"); |
| 279 | -// _stateLab.text = @"CNLiveStreamStateIdle"; | |
| 280 | 332 | } |
| 281 | 333 | |
| 282 | 334 | if (_kitCN.streamState == CNLiveStreamStateConnected) { |
| 283 | 335 | |
| 284 | 336 | NSLog(@"---------CNLiveStreamStateConnected"); |
| 285 | -// _stateLab.text = [NSString stringWithFormat:@"CNLiveStreamStateConnected------%@",_hostURL]; | |
| 286 | 337 | } |
| 287 | 338 | |
| 288 | 339 | if (_kitCN.streamState == CNLiveStreamStateConnecting) { |
| 289 | 340 | |
| 290 | 341 | NSLog(@"---------CNLiveStreamStateConnecting"); |
| 291 | -// _stateLab.text = @"CNLiveStreamStateConnecting"; | |
| 292 | 342 | } |
| 293 | 343 | |
| 294 | 344 | //推流出错 |
| ... | ... | @@ -302,14 +352,12 @@ |
| 302 | 352 | if ( _kitCN.streamState == CNLiveStreamStateDisconnecting) { |
| 303 | 353 | |
| 304 | 354 | NSLog(@"---------CNLiveStreamStateDisconnecting"); |
| 305 | -// _stateLab.text = @"CNLiveStreamStateDisconnecting"; | |
| 306 | 355 | } |
| 307 | 356 | |
| 308 | 357 | //结束推流成功 |
| 309 | 358 | if ( _kitCN.streamState == CNLiveStopStreamSuccess) { |
| 310 | 359 | |
| 311 | 360 | NSLog(@"---------CNLiveStopStreamSuccess"); |
| 312 | -// _stateLab.text = @"CNLiveStopStreamSuccess"; | |
| 313 | 361 | } |
| 314 | 362 | } |
| 315 | 363 | // 推流失败 |
| ... | ... | @@ -320,19 +368,19 @@ |
| 320 | 368 | NSLog(@"onErr: %lu ", (unsigned long) err); |
| 321 | 369 | |
| 322 | 370 | if ( CNLiveStreamErrorCode_CNLiveAUTHFAILED == err ) { |
| 323 | -// _stateLab.text = @"SDK auth failed, \npls check ak/sk"; | |
| 371 | + | |
| 324 | 372 | } |
| 325 | 373 | else if ( CNLiveStreamErrorCode_CODEC_OPEN_FAILED == err) { |
| 326 | -// _stateLab.text = @"Selected Codec not supported \n in this version"; | |
| 374 | + | |
| 327 | 375 | } |
| 328 | 376 | else if ( CNLiveStreamErrorCode_CONNECT_FAILED == err) { |
| 329 | -// _stateLab.text = @"Connecting error, pls check host url \nor network"; | |
| 377 | + | |
| 330 | 378 | } |
| 331 | 379 | else if ( CNLiveStreamErrorCode_CONNECT_BREAK == err) { |
| 332 | -// _stateLab.text = @"Connection break"; | |
| 380 | + | |
| 333 | 381 | } |
| 334 | 382 | else if ( CNLiveStreamErrorCode_ABNORMAL == err) { |
| 335 | -// _stateLab.text = @"CNLiveStreamErrorCode_ABNORMAL"; | |
| 383 | + | |
| 336 | 384 | [self toast:@"推流异常中断~~"]; |
| 337 | 385 | _startLiveBtn.selected = NO; |
| 338 | 386 | |
| ... | ... | @@ -376,8 +424,7 @@ |
| 376 | 424 | } |
| 377 | 425 | |
| 378 | 426 | // 提示框 |
| 379 | -- (void)toast:(NSString*)message | |
| 380 | -{ | |
| 427 | +- (void)toast:(NSString*)message { | |
| 381 | 428 | |
| 382 | 429 | UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil |
| 383 | 430 | message:message |
| ... | ... | @@ -393,8 +440,8 @@ |
| 393 | 440 | |
| 394 | 441 | |
| 395 | 442 | // 将UI的坐标转换成相机坐标 |
| 396 | -- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates | |
| 397 | -{ | |
| 443 | +- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates { | |
| 444 | + | |
| 398 | 445 | CGPoint pointOfInterest = CGPointMake(.5f, .5f); |
| 399 | 446 | CGSize frameSize = self.view.frame.size; |
| 400 | 447 | CGSize apertureSize = [_kitCN captureDimension]; | ... | ... |