diff --git a/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoCaptureSession.m b/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoCaptureSession.m index 4f49dcae3d3df1ba223d402f604c2ecabdd31029..af1b48d463b635b6460e1de285ee616f7a786710 100644 --- a/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoCaptureSession.m +++ b/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoCaptureSession.m @@ -23,8 +23,10 @@ #pragma mark private - (void)initAVCaptureSession { + NSError* error = nil; self.session = [[AVCaptureSession alloc] init]; + // 设置视频分辨率 if (![self.session canSetSessionPreset:self.config.preset]) { if (![self.session canSetSessionPreset:AVCaptureSessionPresetiFrame960x540]) { if (![self.session canSetSessionPreset:AVCaptureSessionPreset640x480]) { @@ -40,7 +42,7 @@ } [self.session setSessionPreset:self.config.preset]; - [self.session beginConfiguration]; + // 获取系统设备信息 AVCaptureDeviceDiscoverySession* deviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:self.config.position]; NSArray* devices = deviceDiscoverySession.devices; for (AVCaptureDevice* device in devices) { @@ -50,17 +52,38 @@ } } - NSError* error = nil; + // 设置帧率 + BOOL frameRateSupport = NO; + NSArray* ranges = [self.device.activeFormat videoSupportedFrameRateRanges]; + for (AVFrameRateRange* range in ranges) { + if (CMTIME_COMPARE_INLINE(self.config.duration, >=, range.minFrameDuration) && + CMTIME_COMPARE_INLINE(self.config.duration, <=, range.maxFrameDuration)) { + frameRateSupport = YES; + } + } + + if (frameRateSupport && [self.device lockForConfiguration:&error]) { + [self.device setActiveVideoMaxFrameDuration:self.config.duration]; + [self.device setActiveVideoMinFrameDuration:self.config.duration]; + [self.device unlockForConfiguration]; + } + + // 开始配置参数 + [self.session beginConfiguration]; + + // 从设备中创建输入,之后需要设置到session self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:&error]; if (error) { NSLog(@"%s:%d init input error!!!", __func__, __LINE__); return; } + // 设置session的输入 if ([self.session canAddInput:self.videoInput]) { [self.session addInput:self.videoInput]; } + // 配置session的输出 self.videoOutput = [[AVCaptureVideoDataOutput alloc] init]; self.videoOutput.alwaysDiscardsLateVideoFrames = NO; [self.videoOutput setVideoSettings:@{ @@ -73,12 +96,16 @@ [self.session addOutput:self.videoOutput]; } + // 设置连接器 AVCaptureConnection* connect = [self.videoOutput connectionWithMediaType:AVMediaTypeVideo]; + // 设置图片的位置 connect.videoOrientation = self.config.orientation; if ([connect isVideoStabilizationSupported]) { connect.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto; } connect.videoScaleAndCropFactor = connect.videoMaxScaleAndCropFactor; + + // 提交采集相关配置 [self.session commitConfiguration]; self.captureLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; diff --git a/project/iPhone/LWAVFoundation/LWAVFoundation/Model/LWVideoCaptureConfig.h b/project/iPhone/LWAVFoundation/LWAVFoundation/Model/LWVideoCaptureConfig.h index 1cc10d0dada1248f055ab784e837f726140e8539..36adfcd61ff448e3767305347ddd09496a835253 100644 --- a/project/iPhone/LWAVFoundation/LWAVFoundation/Model/LWVideoCaptureConfig.h +++ b/project/iPhone/LWAVFoundation/LWAVFoundation/Model/LWVideoCaptureConfig.h @@ -5,6 +5,8 @@ typedef NS_ENUM(NSUInteger, AVCaptureOutputPixelBufferType) { kAVCaptureOutputPixelBufferType_420v = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, + kAVCaptureOutputPixelBufferType_420f = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, + kAVCaptureOutputPixelBufferType_bgra = kCVPixelFormatType_32BGRA, }; @interface LWVideoCaptureConfig : NSObject @@ -21,6 +23,9 @@ typedef NS_ENUM(NSUInteger, AVCaptureOutputPixelBufferType) { // 视频的方向 @property (assign, nonatomic) AVCaptureVideoOrientation orientation; +// 设置每一帧的时间间隔,其的分子为时间,单位秒,分母为帧率。 +@property (assign, nonatomic) CMTime duration; + - (instancetype) init; @end diff --git a/project/iPhone/LWAVFoundation/LWAVFoundation/Model/LWVideoCaptureConfig.m b/project/iPhone/LWAVFoundation/LWAVFoundation/Model/LWVideoCaptureConfig.m index 6496252160abe1f8047b04ad6d3de7084174aee2..7ac572c1881bc085c91f4905ffdc7664e357b0de 100644 --- a/project/iPhone/LWAVFoundation/LWAVFoundation/Model/LWVideoCaptureConfig.m +++ b/project/iPhone/LWAVFoundation/LWAVFoundation/Model/LWVideoCaptureConfig.m @@ -13,6 +13,7 @@ self.preset = AVCaptureSessionPreset1280x720; self.pixelBufferType = kAVCaptureOutputPixelBufferType_420v; self.orientation = AVCaptureVideoOrientationPortrait; + self.duration = CMTimeMake(1, 30); } return self; } diff --git a/project/iPhone/LWAVFoundation/LWAVFoundation/View/LWCheckBoxView.m b/project/iPhone/LWAVFoundation/LWAVFoundation/View/LWCheckBoxView.m index 7c4bd0b5d71e53085a305866df16693d5f0a980a..c9307dc1c160e14a0733364508aa027faefd2849 100644 --- a/project/iPhone/LWAVFoundation/LWAVFoundation/View/LWCheckBoxView.m +++ b/project/iPhone/LWAVFoundation/LWAVFoundation/View/LWCheckBoxView.m @@ -56,11 +56,11 @@ [self.btnItems addObject:[[LWCheckBoxButton alloc] init]]; self.btnItems[i].contentMode = UIViewContentModeScaleAspectFit; self.btnItems[i].delegate = self; + self.btnItems[i].tag = i; [self.labelItems addObject:[[UILabel alloc] init]]; } - self.btnItems[i].tag = i; self.btnItems[i].image = [self.btnNoSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; self.btnItems[i].frame = CGRectMake(checkBoxItemX, checkBoxItemY, 30, 30); @@ -93,7 +93,7 @@ self.selectItem = self.items[index]; if (self.delegate && [self.delegate respondsToSelector:@selector(onSelect:item:)]) { - [self.delegate onSelect:index item:self.items[index]]; + [self.delegate onSelect:index item:self.selectItem]; } } diff --git a/project/iPhone/LWAVFoundation/LWAVFoundation/ViewController/CaptureViewController.m b/project/iPhone/LWAVFoundation/LWAVFoundation/ViewController/CaptureViewController.m index e10b1337682e009b93c7337f51eb7a21813a1dbf..b5b942047ad8e8fc5351fd0faeb04a3398873b8a 100644 --- a/project/iPhone/LWAVFoundation/LWAVFoundation/ViewController/CaptureViewController.m +++ b/project/iPhone/LWAVFoundation/LWAVFoundation/ViewController/CaptureViewController.m @@ -36,6 +36,7 @@ self.checkBoxView = [[LWCheckBoxView alloc] initWithItems:array]; self.checkBoxView.frame = CGRectMake(checkBoxViewX, checkBoxViewY, checkBoxViewW, checkBoxViewH); self.checkBoxView.displayType = kLWCheckBoxViewDisplayType_LR; + self.checkBoxView.delegate = self; [self.view addSubview:self.checkBoxView]; self.captureTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, checkBoxViewY, 100, 30)]; @@ -59,9 +60,6 @@ [self.stopBtn setBackgroundColor:[UIColor whiteColor]]; [self.stopBtn addTarget:self action:@selector(stopSender:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.stopBtn]; - - self.captureSessionConfig = [[LWVideoCaptureConfig alloc] init]; - self.captureSession = [[LWVideoCaptureSession alloc] initWithConfig:self.captureSessionConfig]; } - (void)viewDidDisappear:(BOOL)animated { @@ -69,6 +67,9 @@ } - (IBAction)startSender:(id)sender { + self.captureSessionConfig = [[LWVideoCaptureConfig alloc] init]; + self.captureSession = [[LWVideoCaptureSession alloc] initWithConfig:self.captureSessionConfig]; + [self.captureSession startCapture]; AVCaptureVideoPreviewLayer* layer = [self.captureSession layer]; @@ -84,6 +85,7 @@ [layer removeFromSuperlayer]; [self.captureSession stopCapture]; + self.captureSession = nil; } #pragma mark LWVideoCaptureSessionDelegate