// // RootViewController.m // AnyChatCallCenter // // Created by alex on 14-1-27. // Copyright (c) 2014年 GuangZhou BaiRui NetWork Technology Co.,Ltd. All rights reserved. // #import "VideoViewController.h" #import "BRUIImageView.h" #import #import "BRManager.h" #import "UIView+XHRemoteImage.h" #import "UIView+AdaptScreen.h" #define kDegreesToRadian(degrees) (M_PI * (degrees) / 180.0) #define kLayer_Z_Axis_3DRotation(degrees) CATransform3DMakeRotation(kDegreesToRadian(degrees), 0.0, 0.0, 1.0) #define kLocalVideo_Width AdaptW(121.0f) #define kLocalVideo_Height AdaptW(162.0f) @interface VideoViewController () @property (strong, nonatomic) AVCaptureVideoPreviewLayer *localVideoSurface; @property (strong, nonatomic) NSMutableArray *theOnlineUserList; @property (strong, nonatomic) IBOutlet BRUIImageView *theUIImageView; @property (strong, nonatomic) IBOutlet UIView *theLocalView; @property (weak, nonatomic) IBOutlet UIButton *voiceBtn; @property (weak, nonatomic) IBOutlet UIButton *cameraBtn; @property (strong, nonatomic) MZTimerLabel *theVideoMZTimer; @property (nonatomic, assign) int iRemoteUserId; @property (weak, nonatomic) IBOutlet UIImageView *headImage; @property (weak, nonatomic) IBOutlet UILabel *voiceLabel; @property (weak, nonatomic) IBOutlet UILabel *videoLabel; /// 是否未打开麦克风 @property (nonatomic) BOOL isUNAuthorizationTypeAudio; /// 是否未打开相机 @property (nonatomic) BOOL isUNAuthorizationTypeCamera; @end @implementation VideoViewController #pragma mark - Life Cycle - (void)viewDidLoad { [super viewDidLoad]; kWeakSelf(self) self.view.backgroundColor = [UIColor blackColor]; self.theLocalView.backgroundColor = [UIColor blackColor]; JLAuthorizationManager *jlManager = [JLAuthorizationManager defaultManager]; [jlManager JL_requestAuthorizationWithAuthorizationType:JLAuthorizationTypeCamera authorizedHandler:nil unAuthorizedHandler:^{ weakself.isUNAuthorizationTypeAudio = YES; }]; [jlManager JL_requestAuthorizationWithAuthorizationType:JLAuthorizationTypeCamera authorizedHandler:nil unAuthorizedHandler:^{ weakself.isUNAuthorizationTypeCamera = YES; }]; self.theUserEntity = [UserEntity new]; self.theUserEntity.theEntityRemoteID = [_brModel.deviceUserName intValue]; self.theUserEntity.theEntityRemoteName = _device.name;//_brModel.deviceName; [self addDeviceRotateObserver]; [self StartVideoChat:self.theUserEntity.theEntityRemoteID]; [self setTheTimer]; [self p_configNavItem]; self.title = F(GJText(@"与%@的对话"),self.theUserEntity.theEntityRemoteName); [self.view adaptScreenWidthWithType:AdaptScreenWidthTypeAll exceptViews:nil]; // 视频 if (_callType == 0) { self.headImage.hidden = YES; // 语音 } else { [self.headImage setImageWithURL:[NSURL URLWithString:_device.image] placeholer:[UIImage imageNamed:@"icon_girl_head_portrait_1"]]; self.view.backgroundColor = KKMainColor; self.videoLabel.hidden = YES; self.voiceLabel.hidden = YES; self.voiceBtn.hidden = YES; self.cameraBtn.hidden = YES; self.theLocalView.hidden = YES; self.theUIImageView.hidden = YES; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OnAnyChatUserEnterRoom:) name:@"ANY_CHAT_USER_ENTER_ROOM" object:nil]; } - (void)judgeAuthorization{ if(self.isUNAuthorizationTypeCamera == YES){ [self showAlertAuthorizationTypeCamera]; if(self.isReceiveCall){ //[self activeHangUpBtnAction:nil]; }else{ //[self hangUpButton:nil]; } }else if(self.isUNAuthorizationTypeAudio == YES){ [self showAlertAuthorizationTypeVideo]; if(self.isReceiveCall){ //[self activeHangUpBtnAction:nil]; }else{ //[self hangUpButton:nil]; } } } -(void)OnAnyChatUserEnterRoom:(NSNotification *)notification { NSNumber *dwUserId = notification.userInfo[@"dwUserId"]; if (dwUserId) { NSLog(@"======VC OnAnyChatUserEnterRoom========%d", dwUserId.intValue); NSMutableArray *arr = [AnyChatPlatform GetOnlineUser]; NSLog(@"Get Online User = %@", arr); #pragma mark 当用户不在房间里时打开用户音视频 if(!arr || arr.count==0) { [self openRemoteView]; } } } - (void)showAlertAuthorizationTypeVideo { EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:GJText(@"请在iPhone的\"设置-隐私-麦克风\"选项中,允许智慧校园访问你的麦克风") subtitle:nil AlertViewType:AlertViewTypeSystemAlert config:nil]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"确定") type:AlertItemTypeSystemDefault callback:^(EasyAlertView *showview, long index) { //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]; }]; [alertV showAlertView]; } - (void)showAlertAuthorizationTypeCamera { EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:GJText(@"在iPhone的\"设置-隐私-相机\"选项中,允许智慧校园访问你的相机") subtitle:nil AlertViewType:AlertViewTypeSystemAlert config:nil]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"确定") type:AlertItemTypeSystemDefault callback:^(EasyAlertView *showview, long index) { //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]; }]; [alertV showAlertView]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; //强制亮屏 [[UIApplication sharedApplication] setIdleTimerDisabled:YES]; if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive){ [self judgeAuthorization]; } [self setUIControls]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:YES]; //The Timer Pause [self.theVideoMZTimer pause]; } -(void)navLeftClick { [self FinishVideoChatBtnClicked:nil]; } -(BOOL)navBarTranslucent { return YES; } - (void)p_configNavItem { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(OnSwitchCameraBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; [button setImage:[UIImage imageNamed:@"video_switch"] forState:UIControlStateNormal]; [button sizeToFit]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.rightBarButtonItem = rightItem; } #pragma mark - Action Method - (void) StartVideoChat:(int) userid { [AnyChatPlatform SetSDKOptionInt:BRAC_SO_LOCALVIDEO_AUTOROTATION :0]; [AnyChatPlatform SetSDKOptionInt:BRAC_SO_LOCALVIDEO_CLIPMODE :0]; [AnyChatPlatform SetSDKOptionInt:BRAC_SO_LOCALVIDEO_APPLYPARAM :1]; //Get a camera, Must be in the real machine. NSMutableArray* cameraDeviceArray = [AnyChatPlatform EnumVideoCapture]; if (cameraDeviceArray.count > 0) { if(cameraDeviceArray.count >= 2) [AnyChatPlatform SelectVideoCapture:[cameraDeviceArray objectAtIndex:1]]; else [AnyChatPlatform SelectVideoCapture:[cameraDeviceArray objectAtIndex:0]]; } // open local video [AnyChatPlatform SetSDKOptionInt:BRAC_SO_LOCALVIDEO_OVERLAY :1]; [AnyChatPlatform UserSpeakControl:-1:YES]; if (_callType == 0) { [AnyChatPlatform SetVideoPos:-1 :self :0 :0 :0 :0]; [AnyChatPlatform UserCameraControl:-1 :YES]; } // request other user video // 拉伸全屏 //[self.theUIImageView setContentMode:UIViewContentModeScaleToFill]; // 裁剪全屏 //[self.theUIImageView setContentMode:UIViewContentModeScaleAspectFill]; [AnyChatPlatform UserSpeakControl:userid :YES]; if (_callType == 0) { [AnyChatPlatform SetVideoPos:userid :self.theUIImageView :0 :0 :0 :0]; [AnyChatPlatform UserCameraControl:userid :YES]; } self.iRemoteUserId = userid; [AnyChatPlatform SetSDKOptionInt:BRAC_SO_LOCALVIDEO_ORIENTATION : (int)[UIApplication sharedApplication].statusBarOrientation]; NSLog(@"================userid = %d", userid); } //打开对方的视频 -(void)openRemoteView { if (_callType == 0) { [AnyChatPlatform UserSpeakControl:self.theUserEntity.theEntityRemoteID :YES]; [AnyChatPlatform UserCameraControl:self.theUserEntity.theEntityRemoteID :YES]; [AnyChatPlatform SetVideoPos:self.theUserEntity.theEntityRemoteID :self.theUIImageView :0:0:0:0]; } } - (void) FinishVideoChat { [AnyChatPlatform UserSpeakControl: -1 :NO]; if (_callType == 0) { [AnyChatPlatform UserCameraControl: -1 :NO]; } [AnyChatPlatform UserSpeakControl: self.iRemoteUserId :NO]; if (_callType == 0) { [AnyChatPlatform UserCameraControl: self.iRemoteUserId :NO]; } self.iRemoteUserId = -1; if (g_isEnterRoom) { g_isEnterRoom = NO; [AnyChatPlatform LeaveRoom:-1]; NSLog(@"=====VideoVC LeaveRoom"); } } - (IBAction)FinishVideoChatBtnClicked:(id)sender { //[ACVideoAlertView showAlertViewByTitle:@"是否想要结束当前通话?" clickBlock:^(NSInteger index) { // if(index == 0) { [AnyChatPlatform VideoCallControl:BRAC_VIDEOCALL_EVENT_FINISH :self.iRemoteUserId :0 :0 :0 :nil]; [self FinishVideoChat]; //[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]; [self.navigationController popViewControllerAnimated:YES]; // } //}]; } - (IBAction)OnCloseVoiceBtnClicked:(id)sender { if (self.voiceBtn.selected == NO) { [AnyChatPlatform UserSpeakControl:-1 :NO]; self.voiceBtn.selected = YES; } else { [AnyChatPlatform UserSpeakControl: -1:YES]; self.voiceBtn.selected = NO; } } - (IBAction)OnCloseCameraBtnClicked:(id)sender { if ([AnyChatPlatform GetCameraState:-1] == 1) { //open local Camera [AnyChatPlatform SetVideoPos:-1 :self :0 :0 :0 :0]; [AnyChatPlatform UserCameraControl:-1 : YES]; self.theLocalView.hidden = NO; self.cameraBtn.selected = NO; } if ([AnyChatPlatform GetCameraState:-1] == 2) { //close local Camera [AnyChatPlatform UserCameraControl:-1 :NO]; self.theLocalView.hidden = YES; self.cameraBtn.selected = YES; } } - (void) btnSelectedOnClicked:(UIButton*)button { if (button.selected) { button.selected = NO; } else { button.selected = YES; } } #pragma mark - Local Video - (void) OnSwitchCameraBtnClicked:(id)sender { static int CurrentCameraDevice = 1; NSMutableArray* cameraDeviceArray = [AnyChatPlatform EnumVideoCapture]; if(cameraDeviceArray.count == 2) { CurrentCameraDevice = (CurrentCameraDevice+1) % 2; [AnyChatPlatform SelectVideoCapture:[cameraDeviceArray objectAtIndex:CurrentCameraDevice]]; } [self btnSelectedOnClicked:sender]; } - (void) OnLocalVideoRelease:(id)sender { if(self.localVideoSurface) { self.localVideoSurface = nil; } } - (void) OnLocalVideoInit:(id)session { self.localVideoSurface = [AVCaptureVideoPreviewLayer layerWithSession: (AVCaptureSession*)session]; self.localVideoSurface.frame = CGRectMake(0, 0, kLocalVideo_Width, kLocalVideo_Height); self.localVideoSurface.videoGravity = AVLayerVideoGravityResizeAspectFill; [self.theLocalView.layer addSublayer:self.localVideoSurface]; } #pragma mark - 自动旋转 - (BOOL)shouldAutorotate { return YES; } //返回直接支持的方向 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; } //返回最优先显示的屏幕方向 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } - (void)addDeviceRotateObserver { [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotateViews:) name:UIDeviceOrientationDidChangeNotification object:nil]; } - (void) removeDeviceRotateObserver { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ANY_CHAT_USER_ENTER_ROOM" object:nil]; } - (void)rotateViews:(NSObject *)sender { UIDevice* device = [sender valueForKey:@"object"]; switch (device.orientation) { case UIDeviceOrientationUnknown: { break; } case UIDeviceOrientationPortrait: { self.localVideoSurface.transform = kLayer_Z_Axis_3DRotation(0); self.theUIImageView.layer.transform = kLayer_Z_Axis_3DRotation(0); break; } case UIDeviceOrientationPortraitUpsideDown: { break; } case UIDeviceOrientationLandscapeLeft: { self.localVideoSurface.transform = kLayer_Z_Axis_3DRotation(-90.0); self.theUIImageView.layer.transform = kLayer_Z_Axis_3DRotation(-90.0); break; } case UIDeviceOrientationLandscapeRight: { self.localVideoSurface.transform = kLayer_Z_Axis_3DRotation(90.0); self.theUIImageView.layer.transform = kLayer_Z_Axis_3DRotation(90.0); break; } case UIDeviceOrientationFaceUp: { break; } case UIDeviceOrientationFaceDown: { break; } } } #pragma mark - UI - (void)setUIControls { //Local View line self.theLocalView.layer.borderColor = [[UIColor whiteColor] CGColor]; self.theLocalView.layer.borderWidth = 1.0f; //Rounded corners self.theLocalView.layer.cornerRadius = 4; self.theLocalView.layer.masksToBounds = YES; [self.theVideoMZTimer start]; } - (void)setTheTimer { //The Timer Init self.theVideoMZTimer = [[MZTimerLabel alloc]initWithLabel:self.theVideoTimeLab]; self.theVideoMZTimer.timeFormat = @"HH:mm:ss"; [self.theVideoMZTimer start]; } -(void)dealloc { NSLog(@"销毁了"); [self FinishVideoChat]; [self removeDeviceRotateObserver]; } @end