#import #import #import "CaptureViewController.h" #import "LWCheckBoxView.h" #import "LWVideoCaptureSession.h" #import "LWVideoEncoder.h" #import "LWVideoOptions.h" @interface CaptureViewController () @property (strong, nonatomic) UIView* displayView; @property (strong, nonatomic) LWCheckBoxView* checkBoxView; @property (strong, nonatomic) UILabel* captureTypeLabel; @property (strong, nonatomic) UILabel* encodeBitRateLabel; @property (strong, nonatomic) UITextField* encodeBitRateField; @property (strong, nonatomic) UIButton* encodeBitRateSettedBtn; @property (strong, nonatomic) UIButton* startBtn; @property (strong, nonatomic) UIButton* stopBtn; @property (strong, nonatomic) LWVideoCaptureConfig* captureSessionConfig; @property (strong, nonatomic) LWVideoCaptureSession* captureSession; @property (strong, nonatomic) LWVideoEncoderConfig* encoderConfig; @property (strong, nonatomic) LWVideoEncoder* encoder; @property (strong, nonatomic) UIColor* hatsuneMiKuColor; @end @implementation CaptureViewController - (void) viewDidLoad { CGRect bounds = [UIScreen mainScreen].bounds; int displayViewX = 0; int displayViewY = 20; int displayViewW = [UIScreen mainScreen].bounds.size.width; int displayViewH = displayViewW / 16 * 12; self.hatsuneMiKuColor = [UIColor colorWithRed:(0x39 * 1.0 / 0xff) green:(0xc5 * 1.0 / 0xff) blue:(0xbb * 1.0 / 0xff) alpha:1.0]; self.displayView = [[UIView alloc] initWithFrame:CGRectMake(displayViewX, displayViewY, displayViewW, displayViewH)]; self.displayView.backgroundColor = [UIColor blackColor]; [self.view addSubview:self.displayView]; int checkBoxViewX = 100; int checkBoxViewY = displayViewH + displayViewY + 10; int checkBoxViewW = 270; int checkBoxViewH = 30; LWCheckBoxItem* item1 = [[LWCheckBoxItem alloc] initWithName:@"前置"]; LWCheckBoxItem* item2 = [[LWCheckBoxItem alloc] initWithName:@"后置"]; NSArray* array = [NSArray arrayWithObjects:item1, item2, nil]; 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)]; self.captureTypeLabel.text = @"摄像头选择"; self.captureTypeLabel.backgroundColor = [UIColor whiteColor]; self.captureTypeLabel.textColor = [UIColor blackColor]; [self.view addSubview:self.captureTypeLabel]; int bitrateLabelX = 0; int bitrateLabelY = checkBoxViewY + checkBoxViewH + 10; int bitrateLabelW = 60; int bitrateLabelH = 30; int bitrateBtnW = 90; self.encodeBitRateLabel = [[UILabel alloc] initWithFrame:CGRectMake(bitrateLabelX, bitrateLabelY, bitrateLabelW, bitrateLabelH)]; self.encodeBitRateLabel.text = @"码率:"; self.encodeBitRateLabel.backgroundColor = [UIColor whiteColor]; self.encodeBitRateLabel.textColor = [UIColor blackColor]; [self.view addSubview:self.encodeBitRateLabel]; self.encodeBitRateField = [[UITextField alloc] initWithFrame:CGRectMake(bitrateLabelX + bitrateLabelW, bitrateLabelY, bounds.size.width - bitrateBtnW - bitrateLabelX - bitrateLabelW, 30)]; self.encodeBitRateField.backgroundColor = [UIColor whiteColor]; self.encodeBitRateField.textColor = [UIColor blackColor]; self.encodeBitRateField.text = @"2000"; self.encodeBitRateField.delegate = self; [self.view addSubview:self.encodeBitRateField]; self.encodeBitRateSettedBtn = [[UIButton alloc] initWithFrame:CGRectMake(bounds.size.width - bitrateBtnW, bitrateLabelY, bitrateBtnW, 30)]; [self.encodeBitRateSettedBtn setTitle:@"设置" forState:UIControlStateNormal]; [self.encodeBitRateSettedBtn setBackgroundColor:[UIColor whiteColor]]; [self.encodeBitRateSettedBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.encodeBitRateSettedBtn addTarget:self action:@selector(setBitrateSender:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.encodeBitRateSettedBtn]; self.startBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, bitrateLabelY + bitrateLabelH + 10, [UIScreen mainScreen].bounds.size.width / 2, 30)]; // self.startBtn.enabled = YES; [self.startBtn setTitle:@"start" forState:UIControlStateNormal]; [self.startBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.startBtn setBackgroundColor:self.hatsuneMiKuColor]; [self.startBtn addTarget:self action:@selector(startSender:) forControlEvents:UIControlEventTouchUpInside]; [self.startBtn.layer setMasksToBounds:YES]; [self.startBtn.layer setCornerRadius:4.0]; [self.startBtn.layer setBorderWidth:1.0]; [self.startBtn.layer setBorderColor:[UIColor blackColor].CGColor]; [self.view addSubview:self.startBtn]; self.stopBtn = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2, bitrateLabelY + bitrateLabelH + 10, [UIScreen mainScreen].bounds.size.width / 2, 30)]; // self.startBtn.enabled = YES; [self.stopBtn setTitle:@"stop" forState:UIControlStateNormal]; [self.stopBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.stopBtn setBackgroundColor:self.hatsuneMiKuColor]; [self.stopBtn addTarget:self action:@selector(stopSender:) forControlEvents:UIControlEventTouchUpInside]; [self.stopBtn.layer setMasksToBounds:YES]; [self.stopBtn.layer setCornerRadius:4.0]; [self.stopBtn.layer setBorderWidth:1.0]; [self.stopBtn.layer setBorderColor:[UIColor blackColor].CGColor]; [self.view addSubview:self.stopBtn]; self.captureSessionConfig = [[LWVideoCaptureConfig alloc] init]; } - (void)viewDidDisappear:(BOOL)animated { } - (IBAction)startSender:(id)sender { self.captureSession = [[LWVideoCaptureSession alloc] initWithConfig:self.captureSessionConfig]; self.captureSession.delegate = self; [self.captureSession startCapture]; AVCaptureVideoPreviewLayer* layer = [self.captureSession layer]; layer.frame = CGRectMake(0, 0, self.displayView.frame.size.width, self.displayView.frame.size.height); layer.videoGravity = AVLayerVideoGravityResizeAspectFill; [self.displayView.layer addSublayer:layer]; self.encoderConfig = [LWVideoEncoderConfig defaultConfig]; self.encoderConfig.width = 1280; self.encoderConfig.height = 720; self.encoderConfig.framePerSecond = 30; self.encoderConfig.bitrate = 2000; self.encoder = [LWVideoEncoder newVideoEncoder:self.encoderConfig]; self.encoder.delegate = self; } - (IBAction)stopSender:(id)sender { AVCaptureVideoPreviewLayer* layer = [self.captureSession layer]; layer.frame = CGRectMake(0, 0, self.displayView.frame.size.width, self.displayView.frame.size.height); layer.videoGravity = AVLayerVideoGravityResizeAspectFill; [layer removeFromSuperlayer]; [self.captureSession stopCapture]; self.captureSession = nil; self.encoder = nil; } - (IBAction)setBitrateSender:(id)sender { NSString* bitStr = self.encodeBitRateField.text; int bitrate = [bitStr intValue]; if (self.encoder) { [self.encoder setOptionWithInt32:kLWVideoOptions_Bitrate value:bitrate]; } else { self.encoderConfig.bitrate = bitrate; } } #pragma mark LWVideoCaptureSessionDelegate - (void)onVideoWithSampleBuffer:(CMSampleBufferRef)sampleBuffer { if (self.encoder) { [self.encoder encode:sampleBuffer sourceFrameRef:nil]; } } #pragma mark LWCheckBoxViewDelegate - (void)onSelect:(int)index item:(LWCheckBoxItem*)item { if ([item.checkBoxName isEqual:@"前置"]) { self.captureSessionConfig.position = AVCaptureDevicePositionFront; } else { self.captureSessionConfig.position = AVCaptureDevicePositionBack; } } #pragma mark LWVideoEncoderDelegate - (void)onDidEncoded:(OSStatus)status sourceFrameRef:(void*)sourceFrameRef info:(VTEncodeInfoFlags)infoFlags sampleBuffer:(CMSampleBufferRef)sampleBuffer { } #pragma mark UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField *)textField{ return [self.encodeBitRateField resignFirstResponder]; } @end