Skip to content

Commit 41393ff

Browse files
author
chenliming
committed
Support output arbitrary size and fix iphone4 1920*720 crash
1 parent ba48a45 commit 41393ff

File tree

7 files changed

+81
-74
lines changed

7 files changed

+81
-74
lines changed

LFLiveKit/LFLiveSession.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@
8484
/** The reconnectCount control reconnect count (重连次数) *.*/
8585
@property (nonatomic, assign) NSUInteger reconnectCount;
8686

87-
/*** The warterMarkView control whether the watermark is displayed or not ,if set ni,will remove watermark,otherwise add. set alpha represent mix *.*/
87+
/*** The warterMarkView control whether the watermark is displayed or not ,if set ni,will remove watermark,otherwise add.
88+
set alpha represent mix.Position relative to preview.
89+
*.*/
8890
@property (nonatomic, strong) UIView *warterMarkView;
8991

9092
#pragma mark - Initializer

LFLiveKit/capture/LFVideoCapture.m

+37-63
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ @interface LFVideoCapture ()
1616
@property (nonatomic, strong) GPUImageVideoCamera *videoCamera;
1717
@property (nonatomic, weak) LFGPUImageBeautyFilter *beautyFilter;
1818
@property (nonatomic, strong) GPUImageOutput<GPUImageInput> *filter;
19-
@property (nonatomic, strong) GPUImageOutput<GPUImageInput> *output;
2019
@property (nonatomic, strong) GPUImageCropFilter *cropfilter;
20+
@property (nonatomic, strong) GPUImageOutput<GPUImageInput> *output;
2121
@property (nonatomic, strong) GPUImageView *gpuImageView;
2222
@property (nonatomic, strong) LFLiveVideoConfiguration *configuration;
2323

@@ -37,10 +37,6 @@ @implementation LFVideoCapture
3737
- (instancetype)initWithVideoConfiguration:(LFLiveVideoConfiguration *)configuration {
3838
if (self = [super init]) {
3939
_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-
}
4440

4541
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterBackground:) name:UIApplicationWillResignActiveNotification object:nil];
4642
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground:) name:UIApplicationDidBecomeActiveNotification object:nil];
@@ -260,67 +256,75 @@ - (void)processVideo:(GPUImageOutput *)output {
260256
@autoreleasepool {
261257
GPUImageFramebuffer *imageFramebuffer = output.framebufferForOutput;
262258
CVPixelBufferRef pixelBuffer = [imageFramebuffer pixelBuffer];
263-
259+
if(!CGSizeEqualToSize(_self.configuration.videoSize, imageFramebuffer.size)){
260+
_self.configuration.videoSize = imageFramebuffer.size;
261+
}
264262
if (pixelBuffer && _self.delegate && [_self.delegate respondsToSelector:@selector(captureOutput:pixelBuffer:)]) {
265263
[_self.delegate captureOutput:_self pixelBuffer:pixelBuffer];
266264
}
267265
}
268266
}
269267

268+
270269
- (void)reloadFilter{
271270
[self.filter removeAllTargets];
272-
[self.cropfilter removeAllTargets];
273271
[self.blendFilter removeAllTargets];
274272
[self.uiElementInput removeAllTargets];
275273
[self.videoCamera removeAllTargets];
276-
274+
[self.output removeAllTargets];
275+
[self.cropfilter removeAllTargets];
277276

278277
if (self.beautyFace) {
279278
self.output = [[LFGPUImageEmptyFilter alloc] init];
280279
self.filter = [[LFGPUImageBeautyFilter alloc] init];
281280
self.beautyFilter = self.filter;
282-
__weak typeof(self) _self = self;
283-
[self.output setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time) {
284-
[_self processVideo:output];
285-
}];
286281
} else {
282+
self.output = [[LFGPUImageEmptyFilter alloc] init];
287283
self.filter = [[LFGPUImageEmptyFilter alloc] init];
288284
self.beautyFilter = nil;
289-
__weak typeof(self) _self = self;
290-
[self.filter setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time) {
291-
[_self processVideo:output];
292-
}];
293285
}
294286

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];
303291
[self.videoCamera addTarget:self.cropfilter];
304292
[self.cropfilter addTarget:self.filter];
293+
}else{
294+
[self.videoCamera addTarget:self.filter];
305295
}
306296

297+
//< 添加水印
307298
if(self.warterMarkView){
308299
[self.filter addTarget:self.blendFilter];
309300
[self.uiElementInput addTarget:self.blendFilter];
310301
[self.blendFilter addTarget:self.gpuImageView];
311-
if(self.beautyFace){
312-
[self.filter addTarget:self.output];
313-
}
302+
[self.filter addTarget:self.output];
314303
[self.uiElementInput update];
315304
}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];
322320
}
323321

322+
//< 输出数据
323+
__weak typeof(self) _self = self;
324+
[self.output setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time) {
325+
[_self processVideo:output];
326+
}];
327+
324328
}
325329

326330
#pragma mark Notification
@@ -356,34 +360,4 @@ - (void)statusBarChanged:(NSNotification *)notification {
356360
}
357361
}
358362

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-
389363
@end

LFLiveKit/configuration/LFLiveVideoConfiguration.h

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ typedef NS_ENUM (NSUInteger, LFLiveVideoQuality){
6060
/// 视频的分辨率,宽高务必设定为 2 的倍数,否则解码播放时可能出现绿边
6161
@property (nonatomic, assign) CGSize videoSize;
6262

63+
/// 输出图像是否等比例,默认为YES
64+
@property (nonatomic, assign) BOOL videoSizeRespectingAspectRatio;
65+
6366
/// 视频输出方向
6467
@property (nonatomic, assign) BOOL landscape;
6568

LFLiveKit/configuration/LFLiveVideoConfiguration.m

+28-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#import "LFLiveVideoConfiguration.h"
1010
#import <AVFoundation/AVFoundation.h>
1111

12+
1213
@implementation LFLiveVideoConfiguration
1314

1415
#pragma mark -- LifeCycle
16+
1517
+ (instancetype)defaultConfiguration {
1618
LFLiveVideoConfiguration *configuration = [LFLiveVideoConfiguration defaultConfigurationForQuality:LFLiveVideoQuality_Default];
1719
return configuration;
@@ -148,6 +150,13 @@ + (instancetype)defaultConfigurationForQuality:(LFLiveVideoQuality)videoQuality
148150
return configuration;
149151
}
150152

153+
- (instancetype)init{
154+
if(self = [super init]){
155+
_videoSizeRespectingAspectRatio = YES;
156+
}
157+
return self;
158+
}
159+
151160
#pragma mark -- Setter Getter
152161
- (NSString *)avSessionPreset {
153162
NSString *avSessionPreset = nil;
@@ -175,6 +184,7 @@ - (NSString *)avSessionPreset {
175184
return avSessionPreset;
176185
}
177186

187+
178188
- (void)setVideoMaxBitRate:(NSUInteger)videoMaxBitRate {
179189
if (videoMaxBitRate <= _videoBitRate) return;
180190
_videoMaxBitRate = videoMaxBitRate;
@@ -195,11 +205,28 @@ - (void)setVideoMinFrameRate:(NSUInteger)videoMinFrameRate {
195205
_videoMinFrameRate = videoMinFrameRate;
196206
}
197207

208+
- (void)setSessionPreset:(LFLiveVideoSessionPreset)sessionPreset{
209+
_sessionPreset = sessionPreset;
210+
_sessionPreset = [self supportSessionPreset:sessionPreset];
211+
}
212+
198213
#pragma mark -- Custom Method
199214
- (LFLiveVideoSessionPreset)supportSessionPreset:(LFLiveVideoSessionPreset)sessionPreset {
200215
NSString *avSessionPreset = [self avSessionPreset];
201216
AVCaptureSession *session = [[AVCaptureSession alloc] init];
202-
217+
AVCaptureDevice *inputCamera;
218+
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
219+
for (AVCaptureDevice *device in devices){
220+
if ([device position] == AVCaptureDevicePositionFront){
221+
inputCamera = device;
222+
}
223+
}
224+
AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:inputCamera error:nil];
225+
226+
if ([session canAddInput:videoInput]){
227+
[session addInput:videoInput];
228+
}
229+
203230
if (![session canSetSessionPreset:avSessionPreset]) {
204231
if (sessionPreset == LFCaptureSessionPreset720x1280) {
205232
sessionPreset = LFCaptureSessionPreset540x960;

LFLiveKitDemo/LFLiveKitDemo/LFLivePreview.m

+10-9
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ - (LFLiveSession *)session {
156156

157157

158158
/***  默认分辨率368 * 640 音频:44.1 iphone6以上48 双声道 方向竖屏 ***/
159-
_session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfigurationForQuality:LFLiveVideoQuality_Medium3 landscape:NO]];
159+
_session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfigurationForQuality:LFLiveVideoQuality_Low2 landscape:NO]];
160160

161161
/**   自己定制单声道 */
162162
/*
@@ -207,17 +207,17 @@ - (LFLiveSession *)session {
207207
audioConfiguration.audioSampleRate = LFLiveAudioSampleRate_44100Hz;
208208
209209
LFLiveVideoConfiguration *videoConfiguration = [LFLiveVideoConfiguration new];
210-
videoConfiguration.videoSize = CGSizeMake(720, 1280);
210+
videoConfiguration.videoSize = CGSizeMake(500, 700);
211211
videoConfiguration.videoBitRate = 800*1024;
212212
videoConfiguration.videoMaxBitRate = 1000*1024;
213213
videoConfiguration.videoMinBitRate = 500*1024;
214214
videoConfiguration.videoFrameRate = 15;
215215
videoConfiguration.videoMaxKeyframeInterval = 30;
216-
videoConfiguration.orientation = UIInterfaceOrientationPortrait;
216+
videoConfiguration.landscape = NO;
217217
videoConfiguration.sessionPreset = LFCaptureSessionPreset720x1280;
218218
219219
_session = [[LFLiveSession alloc] initWithAudioConfiguration:audioConfiguration videoConfiguration:videoConfiguration];
220-
*/
220+
*/
221221

222222

223223
/**   自己定制高质量音频128K 分辨率设置为720*1280 方向横屏 */
@@ -229,27 +229,28 @@ - (LFLiveSession *)session {
229229
audioConfiguration.audioSampleRate = LFLiveAudioSampleRate_44100Hz;
230230
231231
LFLiveVideoConfiguration *videoConfiguration = [LFLiveVideoConfiguration new];
232-
videoConfiguration.videoSize = CGSizeMake(1280, 720);
232+
videoConfiguration.videoSize = CGSizeMake(720, 1280);
233233
videoConfiguration.videoBitRate = 800*1024;
234234
videoConfiguration.videoMaxBitRate = 1000*1024;
235235
videoConfiguration.videoMinBitRate = 500*1024;
236236
videoConfiguration.videoFrameRate = 15;
237237
videoConfiguration.videoMaxKeyframeInterval = 30;
238-
videoConfiguration.landscape = YES;
239-
videoConfiguration.sessionPreset = LFCaptureSessionPreset720x1280;
238+
videoConfiguration.landscape = NO;
239+
videoConfiguration.sessionPreset = LFCaptureSessionPreset360x640;
240240
241241
_session = [[LFLiveSession alloc] initWithAudioConfiguration:audioConfiguration videoConfiguration:videoConfiguration];
242-
*/
242+
*/
243243

244244
_session.delegate = self;
245245
_session.showDebugInfo = NO;
246246
_session.preView = self;
247247

248248
UIImageView *imageView = [[UIImageView alloc] init];
249249
imageView.alpha = 0.8;
250-
imageView.frame = CGRectMake(100, 100, 29, 29);
250+
imageView.frame = CGRectMake(320-29, 100, 29, 29);
251251
imageView.image = [UIImage imageNamed:@"ios-29x29"];
252252
_session.warterMarkView = imageView;
253+
253254
}
254255
return _session;
255256
}

0 commit comments

Comments
 (0)