From 5e43be0df9748f56fe0f7a13ac2530f4995b216e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=97=B6=E6=9D=83?= Date: Mon, 23 Aug 2021 20:10:43 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E9=9C=80=E6=B1=82=E3=80=91=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8F=AF=E5=8A=A8=E6=80=81=E6=9B=B4=E6=96=B0=E7=A0=81?= =?UTF-8?q?=E7=8E=87=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AVFoundation/LWVideoEncoder.h | 4 +- .../AVFoundation/LWVideoEncoder.m | 18 ++++++ .../ViewController/CaptureViewController.m | 55 +++++++++++++++++-- 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoEncoder.h b/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoEncoder.h index 0af44c6..00eebc8 100644 --- a/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoEncoder.h +++ b/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoEncoder.h @@ -39,9 +39,9 @@ typedef NS_ENUM(NSUInteger, LWVideoEncodeStatus) { - (LWVideoEncodeStatus)reconfig:(LWVideoEncoderConfig*)config; -- (LWVideoEncodeStatus)setOptionWithInt32:(NSString*)key value:(int32_t)value; +- (LWVideoEncodeStatus)setOptionWithInt32:(const NSString*)key value:(int32_t)value; -- (LWVideoEncodeStatus)getOptionWithInt32:(NSString*)key value:(int32_t*)value; +- (LWVideoEncodeStatus)getOptionWithInt32:(const NSString*)key value:(int32_t*)value; @end diff --git a/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoEncoder.m b/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoEncoder.m index d804c16..50f06ce 100644 --- a/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoEncoder.m +++ b/project/iPhone/LWAVFoundation/LWAVFoundation/AVFoundation/LWVideoEncoder.m @@ -1,5 +1,6 @@ #import #import "LWVideoEncoder.h" +#import "LWVideoOptions.h" @interface LWVideoEncoder () { @@ -59,6 +60,23 @@ void compressSessionOutputCallback(void *opaque, return kLWVideoEncodeStatus_Success; } +- (LWVideoEncodeStatus)setOptionWithInt32:(const NSString*)key value:(int32_t)value { + LWVideoEncodeStatus status = kLWVideoEncodeStatus_Success; + // 动态更新码率 + if ([key isEqual:kLWVideoOptions_Bitrate]) { + _config.bitrate = value; + [self setBitrate:value]; + NSLog(@"%s:%d dynamic bitrate %d", __func__, __LINE__, value); + } else { + status = kLWVideoEncodeStatus_Err_NO_Support; + } + return status; +} + +- (LWVideoEncodeStatus)getOptionWithInt32:(const NSString*)key value:(int32_t*)value { + return kLWVideoEncodeStatus_Err_NO_Support; +} + - (void)dealloc { [self destroySession]; } diff --git a/project/iPhone/LWAVFoundation/LWAVFoundation/ViewController/CaptureViewController.m b/project/iPhone/LWAVFoundation/LWAVFoundation/ViewController/CaptureViewController.m index dd64947..5049fe9 100644 --- a/project/iPhone/LWAVFoundation/LWAVFoundation/ViewController/CaptureViewController.m +++ b/project/iPhone/LWAVFoundation/LWAVFoundation/ViewController/CaptureViewController.m @@ -4,11 +4,17 @@ #import "LWCheckBoxView.h" #import "LWVideoCaptureSession.h" #import "LWVideoEncoder.h" +#import "LWVideoOptions.h" -@interface CaptureViewController () +@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; @@ -24,10 +30,11 @@ @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 * 9; + 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]; @@ -52,7 +59,32 @@ self.captureTypeLabel.textColor = [UIColor blackColor]; [self.view addSubview:self.captureTypeLabel]; - self.startBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, checkBoxViewY + checkBoxViewH + 10, [UIScreen mainScreen].bounds.size.width / 2, 30)]; + 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]; @@ -64,7 +96,7 @@ [self.startBtn.layer setBorderColor:[UIColor blackColor].CGColor]; [self.view addSubview:self.startBtn]; - self.stopBtn = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2, checkBoxViewY + checkBoxViewH + 10, [UIScreen mainScreen].bounds.size.width / 2, 30)]; + 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]; @@ -114,6 +146,16 @@ 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) { @@ -137,4 +179,9 @@ sampleBuffer:(CMSampleBufferRef)sampleBuffer { } +#pragma mark UITextFieldDelegate +- (BOOL)textFieldShouldReturn:(UITextField *)textField{ + return [self.encodeBitRateField resignFirstResponder]; +} + @end -- GitLab