Commit 7b964133f99cb6315840d3461f91f28b59ae7d56

Authored by 王军波
1 parent 0a30c700

添加消息ID

LiveVideoCloud/LiveVideoCloud/Classes/Sections/LiveSections/Controller/LVCLiveViewController.m
@@ -28,8 +28,8 @@ @@ -28,8 +28,8 @@
28 28
29 @implementation LVCLiveViewController 29 @implementation LVCLiveViewController
30 30
31 -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
32 -{ 31 +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  32 +
33 if (_kitCN) { 33 if (_kitCN) {
34 return UIInterfaceOrientationMaskAllButUpsideDown; 34 return UIInterfaceOrientationMaskAllButUpsideDown;
35 } else { 35 } else {
@@ -49,9 +49,11 @@ @@ -49,9 +49,11 @@
49 49
50 - (void)viewDidLoad { 50 - (void)viewDidLoad {
51 [super viewDidLoad]; 51 [super viewDidLoad];
52 - // Do any additional setup after loading the view. 52 +
53 self.view.backgroundColor = [UIColor blueColor]; 53 self.view.backgroundColor = [UIColor blueColor];
54 54
  55 + [self.view addSubview:self.captureView];
  56 +
55 UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 50, 30, 50, 30)]; 57 UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 50, 30, 50, 30)];
56 [closeBtn setTitle:@"关闭" forState:UIControlStateNormal]; 58 [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
57 [closeBtn addTarget:self action:@selector(closeBtnClivk) forControlEvents:UIControlEventTouchUpInside]; 59 [closeBtn addTarget:self action:@selector(closeBtnClivk) forControlEvents:UIControlEventTouchUpInside];
@@ -69,6 +71,23 @@ @@ -69,6 +71,23 @@
69 71
70 [self setStreamerCfg]; 72 [self setStreamerCfg];
71 [self addObservers]; 73 [self addObservers];
  74 +
  75 + _startLiveBtn = [self addButton:@"开始直播" frame:CGRectMake(100, 40, 80, 40) action:@selector(startCapture:)];
  76 + [_startLiveBtn setTitle:@"开始直播" forState:UIControlStateNormal];
  77 + [_startLiveBtn setTitle:@"停止直播" forState:UIControlStateSelected];
  78 +
  79 + [self addButton:@"关闭预览" frame:CGRectMake(10, 40, 80, 40) action:@selector(startPreview:)];
  80 + [self addButton:@"退出" frame:CGRectMake(190, 40, 80, 40) action:@selector(quit)];
  81 + [self addButton:@"美颜" frame:CGRectMake(10, 100, 80, 40) action:@selector(openFilter:)];
  82 + [self addButton:@"摄像头" frame:CGRectMake(100, 100, 80, 40) action:@selector(changeCamera:)];
  83 + [self addButton:@"闪光灯" frame:CGRectMake(190, 100, 80, 40) action:@selector(openFlash)];
  84 +
  85 + UITapGestureRecognizer * singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
  86 + singleRecognizer.delegate = self;
  87 + [self.captureView addGestureRecognizer:singleRecognizer];
  88 +
  89 + UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchDetected:)];
  90 + [self.view addGestureRecognizer:pinch];
72 } 91 }
73 92
74 #pragma mark - initialization Streamer 93 #pragma mark - initialization Streamer
@@ -114,46 +133,102 @@ @@ -114,46 +133,102 @@
114 133
115 } 134 }
116 135
117 -#pragma mark - Action !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
118 -  
119 -- (void)addObservers { 136 +#pragma mark - GetMethod !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  137 +// 拍摄视图
  138 +- (UIView *)captureView {
120 139
121 - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureStateChange) name:CNLiveCaptureStateDidChangeNotification object:nil];  
122 -  
123 - [[NSNotificationCenter defaultCenter] addObserver:self  
124 - selector:@selector(streamStateChange)  
125 - name:CNLiveStreamStateDidChangeNotification  
126 - object:nil]; 140 + if (!_captureView) {
  141 + _captureView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
  142 + _captureView.backgroundColor = [UIColor blackColor];
  143 + }
  144 + return _captureView;
  145 +}
  146 +
  147 +
  148 +#pragma mark - UIButton Action !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  149 +
  150 +// 开始推流
  151 +- (void)startCapture:(UIButton *)button {
127 152
128 - [[NSNotificationCenter defaultCenter] addObserver:self  
129 - selector:@selector(enterBg)  
130 - name:UIApplicationDidEnterBackgroundNotification  
131 - object:nil]; 153 + button.selected = !button.selected;
132 154
133 - [[NSNotificationCenter defaultCenter] addObserver:self  
134 - selector:@selector(becameActive)  
135 - name:UIApplicationDidBecomeActiveNotification  
136 - object:nil]; 155 + if (button.selected) {
  156 + [_kitCN startStream:^{
  157 + NSLog(@"开启推流成功 ------- success");
  158 + } failure:^(NSDictionary *errorDic) {
  159 + [self toast:errorDic[@"errorMessage"]];
  160 + _startLiveBtn.selected = NO;
  161 + }];
  162 + }else{
  163 +// _stateLab.text = nil;
  164 + [_kitCN stopStream];
  165 + }
  166 +}
  167 +
  168 +// 开始 / 关闭 预览
  169 +- (void)startPreview:(UIButton *)button {
  170 +
  171 + button.selected = !button.selected;
  172 + if (button.selected) {
  173 + _startLiveBtn.enabled = NO;
  174 + [button setTitle:@"开启预览" forState:UIControlStateNormal];
  175 + if (_kitCN.streamState == CNLiveStreamStateConnected) {
  176 + [_kitCN stopStream];
  177 + _startLiveBtn.selected = NO;
  178 + }
  179 + [_kitCN stopPreview];
  180 + }else{
  181 + [_kitCN startPreview:self.captureView];
  182 + [button setTitle:@"关闭预览" forState:UIControlStateNormal];
  183 + _startLiveBtn.enabled = YES;
  184 + }
137 } 185 }
138 186
139 // 退出 187 // 退出
140 - (void)closeBtnClivk { 188 - (void)closeBtnClivk {
141 189
  190 + if (_kitCN.streamState == CNLiveStreamStateConnected) {
  191 + [_kitCN stopStream];
  192 + }
  193 + if (_kitCN.captureState == CNLiveCaptureStateCapturing) {
  194 + [_kitCN stopPreview];
  195 + }
  196 +
142 [self dismissViewControllerAnimated:YES completion:nil]; 197 [self dismissViewControllerAnimated:YES completion:nil];
143 } 198 }
144 199
145 -#pragma mark - notification  
146 200
  201 +#pragma mark - UIGestureRecognizer Action !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  202 +//对焦与曝光
  203 +- (void)onTap:(UIGestureRecognizer *)sender
  204 +{
  205 + CGPoint current = [sender locationInView:self.view];
  206 + CGPoint point = [self convertToPointOfInterestFromViewCoordinates:current];
  207 + [_kitCN exposureAtPoint:point];
  208 + [_kitCN focusAtPoint:point];
  209 +}
  210 +
  211 +//缩放
  212 +- (void)pinchDetected:(UIPinchGestureRecognizer *)recognizer{
  213 + if (recognizer.state == UIGestureRecognizerStateBegan) {
  214 + _currentPinchZoomFactor = _kitCN.pinchZoomFactor;
  215 + }
  216 + CGFloat zoomFactor = _currentPinchZoomFactor * recognizer.scale;//当前触摸缩放因子*坐标比例
  217 + [_kitCN setPinchZoomFactor:zoomFactor];
  218 +}
  219 +
  220 +#pragma mark - notification !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  221 +// 进入后台
147 - (void)enterBg { 222 - (void)enterBg {
148 223
149 [_kitCN appEnterBackground]; 224 [_kitCN appEnterBackground];
150 } 225 }
151 - 226 +// 回到前台
152 - (void)becameActive { 227 - (void)becameActive {
153 228
154 [_kitCN appBecomeActive]; 229 [_kitCN appBecomeActive];
155 } 230 }
156 - 231 +// 拍摄状态改变
157 - (void)captureStateChange { 232 - (void)captureStateChange {
158 233
159 if ( _kitCN.captureState == CNLiveCaptureStateIdle){ 234 if ( _kitCN.captureState == CNLiveCaptureStateIdle){
@@ -195,7 +270,7 @@ @@ -195,7 +270,7 @@
195 270
196 } 271 }
197 } 272 }
198 - 273 +// 推流状态改变
199 - (void)streamStateChange { 274 - (void)streamStateChange {
200 275
201 if (_kitCN.streamState == CNLiveStreamStateIdle) { 276 if (_kitCN.streamState == CNLiveStreamStateIdle) {
@@ -237,7 +312,7 @@ @@ -237,7 +312,7 @@
237 // _stateLab.text = @"CNLiveStopStreamSuccess"; 312 // _stateLab.text = @"CNLiveStopStreamSuccess";
238 } 313 }
239 } 314 }
240 - 315 +// 推流失败
241 - (void)onStreamError { 316 - (void)onStreamError {
242 317
243 CNLiveStreamErrorCode err = _kitCN.streamErrorCode; 318 CNLiveStreamErrorCode err = _kitCN.streamErrorCode;
@@ -263,6 +338,7 @@ @@ -263,6 +338,7 @@
263 338
264 } else if (err == CNLiveStreamErrorCode_CODEC_OPEN_FAILED) { 339 } else if (err == CNLiveStreamErrorCode_CODEC_OPEN_FAILED) {
265 NSLog(@"video codec open failed, try software codec"); 340 NSLog(@"video codec open failed, try software codec");
  341 + // 换个编码类型重新尝试
266 _kitCN.streamerBase.videoCodec = CNLiveVideoCodec_VT264; 342 _kitCN.streamerBase.videoCodec = CNLiveVideoCodec_VT264;
267 [self tryReconnect]; 343 [self tryReconnect];
268 } 344 }
@@ -277,6 +353,7 @@ @@ -277,6 +353,7 @@
277 353
278 } 354 }
279 355
  356 +// 重试
280 - (void) tryReconnect { 357 - (void) tryReconnect {
281 358
282 dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)); 359 dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC));
@@ -298,7 +375,7 @@ @@ -298,7 +375,7 @@
298 }); 375 });
299 } 376 }
300 377
301 -// 378 +// 提示框
302 - (void)toast:(NSString*)message 379 - (void)toast:(NSString*)message
303 { 380 {
304 381
@@ -350,6 +427,47 @@ @@ -350,6 +427,47 @@
350 return pointOfInterest; 427 return pointOfInterest;
351 } 428 }
352 429
  430 +#pragma mark - Action !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  431 +
  432 +- (void)addObservers {
  433 +
  434 + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureStateChange) name:CNLiveCaptureStateDidChangeNotification object:nil];
  435 +
  436 + [[NSNotificationCenter defaultCenter] addObserver:self
  437 + selector:@selector(streamStateChange)
  438 + name:CNLiveStreamStateDidChangeNotification
  439 + object:nil];
  440 +
  441 + [[NSNotificationCenter defaultCenter] addObserver:self
  442 + selector:@selector(enterBg)
  443 + name:UIApplicationDidEnterBackgroundNotification
  444 + object:nil];
  445 +
  446 + [[NSNotificationCenter defaultCenter] addObserver:self
  447 + selector:@selector(becameActive)
  448 + name:UIApplicationDidBecomeActiveNotification
  449 + object:nil];
  450 +}
  451 +
  452 +- (UIButton *)addButton:(NSString*)title
  453 + frame:(CGRect)frame
  454 + action:(SEL)action {
  455 + UIButton * button;
  456 + button = [UIButton buttonWithType:UIButtonTypeCustom];
  457 + button.frame = frame;
  458 + [button setTitle: title forState: UIControlStateNormal];
  459 + [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
  460 + button.titleLabel.font = [UIFont systemFontOfSize:15];
  461 + [button setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
  462 + button.layer.cornerRadius = 10;
  463 + button.layer.masksToBounds = YES;
  464 + button.layer.borderColor = [UIColor blackColor].CGColor;
  465 + button.layer.borderWidth = 1;
  466 + [self.captureView addSubview:button];
  467 + return button;
  468 +
  469 +}
  470 +
353 - (void)didReceiveMemoryWarning { 471 - (void)didReceiveMemoryWarning {
354 [super didReceiveMemoryWarning]; 472 [super didReceiveMemoryWarning];
355 // Dispose of any resources that can be recreated. 473 // Dispose of any resources that can be recreated.