@@ -16,8 +16,8 @@ @interface LFVideoCapture ()
16
16
@property (nonatomic , strong ) GPUImageVideoCamera *videoCamera;
17
17
@property (nonatomic , weak ) LFGPUImageBeautyFilter *beautyFilter;
18
18
@property (nonatomic , strong ) GPUImageOutput<GPUImageInput> *filter;
19
- @property (nonatomic , strong ) GPUImageOutput<GPUImageInput> *output;
20
19
@property (nonatomic , strong ) GPUImageCropFilter *cropfilter;
20
+ @property (nonatomic , strong ) GPUImageOutput<GPUImageInput> *output;
21
21
@property (nonatomic , strong ) GPUImageView *gpuImageView;
22
22
@property (nonatomic , strong ) LFLiveVideoConfiguration *configuration;
23
23
@@ -37,10 +37,6 @@ @implementation LFVideoCapture
37
37
- (instancetype )initWithVideoConfiguration : (LFLiveVideoConfiguration *)configuration {
38
38
if (self = [super init ]) {
39
39
_configuration = configuration;
40
- if ([self pixelBufferImageSize ].width < configuration.videoSize .width || [self pixelBufferImageSize ].height < configuration.videoSize .height ){
41
- @throw [NSException exceptionWithName: @" 当前videoSize大小出错" reason: @" LFLiveVideoConfiguration videoSize error" userInfo: nil ];
42
- return nil ;
43
- }
44
40
45
41
[[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (willEnterBackground: ) name: UIApplicationWillResignActiveNotification object: nil ];
46
42
[[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (willEnterForeground: ) name: UIApplicationDidBecomeActiveNotification object: nil ];
@@ -260,67 +256,75 @@ - (void)processVideo:(GPUImageOutput *)output {
260
256
@autoreleasepool {
261
257
GPUImageFramebuffer *imageFramebuffer = output.framebufferForOutput ;
262
258
CVPixelBufferRef pixelBuffer = [imageFramebuffer pixelBuffer ];
263
-
259
+ if (!CGSizeEqualToSize (_self.configuration .videoSize , imageFramebuffer.size )){
260
+ _self.configuration .videoSize = imageFramebuffer.size ;
261
+ }
264
262
if (pixelBuffer && _self.delegate && [_self.delegate respondsToSelector: @selector (captureOutput:pixelBuffer: )]) {
265
263
[_self.delegate captureOutput: _self pixelBuffer: pixelBuffer];
266
264
}
267
265
}
268
266
}
269
267
268
+
270
269
- (void )reloadFilter {
271
270
[self .filter removeAllTargets ];
272
- [self .cropfilter removeAllTargets ];
273
271
[self .blendFilter removeAllTargets ];
274
272
[self .uiElementInput removeAllTargets ];
275
273
[self .videoCamera removeAllTargets ];
276
-
274
+ [self .output removeAllTargets ];
275
+ [self .cropfilter removeAllTargets ];
277
276
278
277
if (self.beautyFace ) {
279
278
self.output = [[LFGPUImageEmptyFilter alloc ] init ];
280
279
self.filter = [[LFGPUImageBeautyFilter alloc ] init ];
281
280
self.beautyFilter = self.filter ;
282
- __weak typeof (self) _self = self;
283
- [self .output setFrameProcessingCompletionBlock: ^(GPUImageOutput *output, CMTime time ) {
284
- [_self processVideo: output];
285
- }];
286
281
} else {
282
+ self.output = [[LFGPUImageEmptyFilter alloc ] init ];
287
283
self.filter = [[LFGPUImageEmptyFilter alloc ] init ];
288
284
self.beautyFilter = nil ;
289
- __weak typeof (self) _self = self;
290
- [self .filter setFrameProcessingCompletionBlock: ^(GPUImageOutput *output, CMTime time ) {
291
- [_self processVideo: output];
292
- }];
293
285
}
294
286
295
- CGSize imageSize = [self pixelBufferImageSize ];
296
- CGFloat cropLeft = (imageSize.width - self.configuration .videoSize .width )/2.0 /imageSize.width ;
297
- CGFloat cropTop = (imageSize.height - self.configuration .videoSize .height )/2.0 /imageSize.height ;
298
-
299
- if (cropLeft == 0 && cropTop == 0 ){
300
- [self .videoCamera addTarget: _filter];
301
- }else {
302
- self.cropfilter = [[GPUImageCropFilter alloc ] initWithCropRegion: CGRectMake (cropLeft, cropTop, 1 - cropLeft*2 , 1 - cropTop*2 )];
287
+ // < 480*640 比例为4:3 强制转换为16:9
288
+ if ([self .configuration.avSessionPreset isEqualToString: AVCaptureSessionPreset640x480]){
289
+ CGRect cropRect = self.configuration .landscape ? CGRectMake (0 , 0.125 , 1 , 0.75 ) : CGRectMake (0.125 , 0 , 0.75 , 1 );
290
+ self.cropfilter = [[GPUImageCropFilter alloc ] initWithCropRegion: cropRect];
303
291
[self .videoCamera addTarget: self .cropfilter];
304
292
[self .cropfilter addTarget: self .filter];
293
+ }else {
294
+ [self .videoCamera addTarget: self .filter];
305
295
}
306
296
297
+ // < 添加水印
307
298
if (self.warterMarkView ){
308
299
[self .filter addTarget: self .blendFilter];
309
300
[self .uiElementInput addTarget: self .blendFilter];
310
301
[self .blendFilter addTarget: self .gpuImageView];
311
- if (self.beautyFace ){
312
- [self .filter addTarget: self .output];
313
- }
302
+ [self .filter addTarget: self .output];
314
303
[self .uiElementInput update ];
315
304
}else {
316
- if (self.beautyFace ) {
317
- [self .filter addTarget: self .output];
318
- [self .output addTarget: self .gpuImageView];
319
- } else {
320
- [self .filter addTarget: self .gpuImageView];
321
- }
305
+ [self .filter addTarget: self .output];
306
+ [self .output addTarget: self .gpuImageView];
307
+ }
308
+
309
+ // < 输出大小自适应
310
+ if (self.configuration .videoSizeRespectingAspectRatio ){
311
+ [self .filter forceProcessingAtSizeRespectingAspectRatio: self .configuration.videoSize];
312
+ [self .output forceProcessingAtSizeRespectingAspectRatio: self .configuration.videoSize];
313
+ [self .blendFilter forceProcessingAtSizeRespectingAspectRatio: self .configuration.videoSize];
314
+ [self .uiElementInput forceProcessingAtSizeRespectingAspectRatio: self .configuration.videoSize];
315
+ }else {
316
+ [self .filter forceProcessingAtSize: self .configuration.videoSize];
317
+ [self .output forceProcessingAtSize: self .configuration.videoSize];
318
+ [self .blendFilter forceProcessingAtSize: self .configuration.videoSize];
319
+ [self .uiElementInput forceProcessingAtSize: self .configuration.videoSize];
322
320
}
323
321
322
+ // < 输出数据
323
+ __weak typeof (self) _self = self;
324
+ [self .output setFrameProcessingCompletionBlock: ^(GPUImageOutput *output, CMTime time ) {
325
+ [_self processVideo: output];
326
+ }];
327
+
324
328
}
325
329
326
330
#pragma mark Notification
@@ -356,34 +360,4 @@ - (void)statusBarChanged:(NSNotification *)notification {
356
360
}
357
361
}
358
362
359
- #pragma mark --
360
- - (CGSize )pixelBufferImageSize {
361
- CGSize videoSize = CGSizeZero ;
362
- switch (self.configuration .sessionPreset ) {
363
- case LFCaptureSessionPreset360x640:
364
- {
365
- videoSize = CGSizeMake (480 , 640 );
366
- }
367
- break ;
368
- case LFCaptureSessionPreset540x960:
369
- {
370
- videoSize = CGSizeMake (540 , 960 );
371
- }
372
- break ;
373
- case LFCaptureSessionPreset720x1280:
374
- {
375
- videoSize = CGSizeMake (720 , 1280 );
376
- }
377
- break ;
378
-
379
- default :
380
- break ;
381
- }
382
-
383
- if (self.configuration .landscape ){
384
- return CGSizeMake (videoSize.height , videoSize.width );
385
- }
386
- return videoSize;
387
- }
388
-
389
363
@end
0 commit comments