// // LCOnMonitorViewController.m // LCOnDemo // // Created by 陈嘉乐 on 2021/4/26. // #import "HDLLPOnMonitorViewController.h" #import #import #import "HDLLinphoneUtlis.h" #import "HDLLinphoneManager.h" #import "HDLLinPhoneCommon.h" //#import "HDLLinPhoneSDK-Swift.h" //#import "LCUtlis.h" //#import "LCApiKit.h" //#import @interface HDLLPOnMonitorViewController () @property (nonatomic,assign) BOOL playing; @property (nonatomic,assign) BOOL isInterrupt; @property (nonatomic,assign) BOOL isSpeaking; @property (nonatomic,strong) UIImage *snapImage; //截图 @property (nonatomic, strong) UIButton *backButton; // @property (nonatomic, strong) UILabel *titleUILabel; // @property (nonatomic, strong) UIView *centerView; //内容背景View @property (nonatomic, strong) UIView *videoView; @property (nonatomic, strong) UIButton *screenshotImgBtn; // @property (nonatomic, strong) UIButton *screenshotTextBtn; // @property (nonatomic, strong) UIButton *unlockImgBtn; // @property (nonatomic, strong) UIButton *unlockTextBtn; // @property (nonatomic,strong) dispatch_source_t openDoorTimer; @property (nonatomic, assign) int openDoorTimeout; @property (nonatomic, assign) BOOL isHangUpByRemote;//是否对方远程挂断 /// 播放器 //@property (nonatomic, strong) LCOpenSDK_PlayWindow *playWindow; @end @implementation HDLLPOnMonitorViewController { BOOL isBackGround; NSString * tipStr; NSString * okStr; NSString * saveToTheAlbumsStr; NSString * operationFailedStr; NSString * screenshotStr; NSString * unlockStr; NSString * endOfMonitoringStr; NSString * unlockSuccessfullyStr; NSString * LCSdkToken; NSString * LCHostApi; NSString * cancelStr; NSString * tipMsgStr; CGFloat Height66; bool isShowErrorAlert; // int _openDoorTimeout; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = UIColor.whiteColor; Height66 = GetRealWidth(66); [self initLlanguage]; [self initTopBarView]; [self initCentetView]; [self initData]; [self initESVideo]; [self StartMonitoring]; [HDLLinphoneManager.instance enableMicro:false]; // //暂时不支持开锁功能 // [self setUnlock:NO]; //注册开锁成功监听 [self addOpenSuccessAction]; // Do any additional setup after loading the view. if (self.titleName&&self.titleName.length!=0) { self.titleUILabel.text=self.titleName; } // [NSNotificationCenter.defaultCenter addObserver:self // selector:@selector(registrationUpdate:) // name:@"LinphoneRegistrationUpdate" // object:nil]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(linphoneCallUpdate:) name:@"LinphoneCallUpdate" object:nil]; } - (void)linphoneCallUpdate:(NSNotification *)notif { int state = [[notif.userInfo objectForKey:@"state"] intValue]; if ((state == LinphoneCallEnd || state == LinphoneCallError)){//挂断了 NSLog(@"挂断或出错了"); self.isHangUpByRemote=YES; [self showUIAlertViewWithBack:endOfMonitoringStr]; } } -(void)initLlanguage{ NSString *languageName = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0]; // 简体中文 if ([languageName rangeOfString:@"zh-Hans"].location != NSNotFound) { tipStr = @"提示"; okStr = @"确认"; saveToTheAlbumsStr = @"已保存至手机相册."; operationFailedStr = @"操作失败"; screenshotStr = @"截图"; unlockStr = @"开锁"; endOfMonitoringStr = @"监视结束"; unlockSuccessfullyStr = @"开锁成功"; cancelStr = @"取消"; tipMsgStr = @"确定开锁吗?"; }else{ tipStr = @"Prompt"; okStr = @"OK"; saveToTheAlbumsStr = @"Saved to the albums."; operationFailedStr = @"Operation failed."; screenshotStr = @"Screenshot"; unlockStr = @"Unlock"; endOfMonitoringStr = @"End of monitoring"; unlockSuccessfullyStr = @"Unlock successfully"; cancelStr = @"Cancel"; tipMsgStr = @"Are you sure to unlock?"; } } //顶部View - (void)initTopBarView { UIView *TopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, APP_TOP_BAR_HEIGHT)]; TopView.backgroundColor = HEXCOLORA(0xF7F7F7,1.0); [TopView addSubview:self.backButton]; [TopView addSubview:self.titleUILabel]; // [TopView addSubview:self.moreButton]; [self.view addSubview:TopView]; TopView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.25].CGColor; TopView.layer.shadowOffset = CGSizeMake(0,0.5); TopView.layer.shadowOpacity = 1; TopView.layer.shadowRadius = 0; } - (UIButton *)backButton{ if (_backButton == nil) { _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, APP_STATUS_BAR_HEIGHT, 44, 44)]; [_backButton setImage:[UIImage imageNamed:@"ic_esvideo_on_back"] forState:UIControlStateNormal]; _backButton.imageEdgeInsets = UIEdgeInsetsMake(12,12,12,12); [_backButton.imageView setContentMode:UIViewContentModeScaleAspectFit]; [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; } return _backButton; } -(void)backAction{ [self dismissViewControllerAnimated:YES completion:NULL]; } -(UILabel *)titleUILabel{ if (_titleUILabel == Nil) { _titleUILabel = [[UILabel alloc] initWithFrame:CGRectMake(90, APP_STATUS_BAR_HEIGHT, self.view.frame.size.width - 180, 44)]; _titleUILabel.font = [UIFont fontWithName:APP_UIFont_BOLD size:18.0]; _titleUILabel.textColor = TextColor; _titleUILabel.text = @""; _titleUILabel.textAlignment = NSTextAlignmentCenter; } return _titleUILabel; } - (void)initCentetView { [self.view addSubview:self.centerView]; // [self.centerView addSubview:self.collectButton]; // [self.centerView addSubview:self.homeView]; [self.centerView addSubview:self.videoView]; [self.centerView addSubview:self.screenshotImgBtn]; [self.centerView addSubview:self.screenshotTextBtn]; [self.centerView addSubview:self.unlockImgBtn]; [self.centerView addSubview:self.unlockTextBtn]; } - (UIView *)centerView{ if (_centerView == nil) { _centerView = [[UIButton alloc] initWithFrame:CGRectMake(0, APP_TOP_BAR_HEIGHT, APP_SCREEN_WIDTH, APP_VISIBLE_HEIGHT)]; _centerView.backgroundColor = UIColor.whiteColor; // [self setRadiusWithView:_centerView]; } return _centerView; } -(void)setRadiusWithView:(UIView *)mView{ //顶部圆角 UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight; if (@available(iOS 11.0, *)) { mView.layer.cornerRadius = 20; mView.layer.maskedCorners = (CACornerMask)corners; }else{ UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:mView.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(20,20)]; //创建 layer CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = mView.bounds; //赋值 maskLayer.path = maskPath.CGPath; mView.layer.mask = maskLayer; } } -(UIView *)videoView{ if (_videoView == Nil) { _videoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, GetRealWidth((210)))]; // _videoView.backgroundColor = UIColor.lightGrayColor; _videoView.backgroundColor = UIColor.blackColor; _videoView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.25].CGColor; _videoView.layer.shadowOffset = CGSizeMake(0,0.5); _videoView.layer.shadowOpacity = 1; _videoView.layer.shadowRadius = 0; _videoView.layer.masksToBounds=YES; } return _videoView; } #pragma 截图图标按钮 - (UIButton *)screenshotImgBtn{ if (_screenshotImgBtn == nil) { _screenshotImgBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Height66, Height66)]; [_screenshotImgBtn setImage:[UIImage imageNamed:@"ic_esvideo_on_takephoto_unselect"] forState:UIControlStateNormal]; // [_screenshotImgBtn setImage:[UIImage imageNamed:@"ic_esvideo_on_takephoto_select"] forState:UIControlStateSelected]; [_screenshotImgBtn.imageView setContentMode:UIViewContentModeScaleAspectFit]; [_screenshotImgBtn addTarget:self action:@selector(screenshotAction) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; [_screenshotImgBtn addTarget:self action:@selector(screenshotDownAction) forControlEvents:UIControlEventTouchDown]; _screenshotImgBtn.adjustsImageWhenHighlighted = NO; _screenshotImgBtn.center = CGPointMake(APP_SCREEN_WIDTH/4, GetRealWidth(276) + Height66/2); } return _screenshotImgBtn; } -(void)screenshotDownAction{ [_screenshotImgBtn setImage:[UIImage imageNamed:@"ic_esvideo_on_takephoto_select"] forState:UIControlStateNormal]; [_screenshotTextBtn setTitleColor:TextSelectColor forState:UIControlStateNormal]; } -(void)screenshotAction{ [_screenshotImgBtn setImage:[UIImage imageNamed:@"ic_esvideo_on_takephoto_unselect"] forState:UIControlStateNormal]; [_screenshotTextBtn setTitleColor:TextColor forState:UIControlStateNormal]; NSLog(@"截图"); //截图 [HDLLinphoneManager.instance snapShotWithView:self.videoView]; } #pragma 截图文字按钮 - (UIButton *)screenshotTextBtn{ if (_screenshotTextBtn == nil) { _screenshotTextBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, GetRealWidth(100), GetRealWidth(20))]; [_screenshotTextBtn setTitle:@"截图" forState:UIControlStateNormal]; _screenshotTextBtn.titleLabel.textAlignment = NSTextAlignmentCenter; _screenshotTextBtn.titleLabel.font = [UIFont fontWithName:APP_UIFont size:14.0]; [_screenshotTextBtn setTitleColor:TextColor forState:UIControlStateNormal]; // [_screenshotTextBtn setTitleColor:TextSelectColor forState:UIControlStateSelected]; [_screenshotTextBtn addTarget:self action:@selector(screenshotAction) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; [_screenshotTextBtn addTarget:self action:@selector(screenshotDownAction) forControlEvents:UIControlEventTouchDown]; _screenshotTextBtn.adjustsImageWhenHighlighted = NO; _screenshotTextBtn.center = CGPointMake(_screenshotImgBtn.center.x, GetRealWidth(364)); } return _screenshotTextBtn; } - (UIButton *)unlockImgBtn{ if (_unlockImgBtn == nil) { _unlockImgBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Height66, Height66)]; [_unlockImgBtn setImage:[UIImage imageNamed:@"ic_esvideo_on_unlock_unselect"] forState:UIControlStateNormal]; // [_unlockImgBtn setImage:[UIImage imageNamed:@"ic_esvideo_on_unlock_select"] forState:UIControlStateSelected]; [_unlockImgBtn.imageView setContentMode:UIViewContentModeScaleAspectFit]; [_unlockImgBtn addTarget:self action:@selector(unlockAction) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; [_unlockImgBtn addTarget:self action:@selector(unlockDownAction) forControlEvents:UIControlEventTouchDown]; _unlockImgBtn.adjustsImageWhenHighlighted = NO; _unlockImgBtn.center = CGPointMake((APP_SCREEN_WIDTH / 4) * 3, GetRealWidth(276) + Height66/2); } return _unlockImgBtn; } -(void)unlockDownAction{ [_unlockImgBtn setImage:[UIImage imageNamed:@"ic_esvideo_on_unlock_select"] forState:UIControlStateNormal]; [_unlockTextBtn setTitleColor:TextSelectColor forState:UIControlStateNormal]; } -(void)unlockAction{ [_unlockImgBtn setImage:[UIImage imageNamed:@"ic_esvideo_on_unlock_unselect"] forState:UIControlStateNormal]; [_unlockTextBtn setTitleColor:TextColor forState:UIControlStateNormal]; //开锁 if(self.hdlLinphoneCallDelegate != NULL){ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:tipStr message:tipMsgStr preferredStyle:UIAlertControllerStyleAlert]; // 添加取消按钮 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelStr style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:cancelAction]; // 添加确认按钮 WEAKSELF_AT UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:okStr style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [weakSelf_AT.hdlLinphoneCallDelegate onUnlockAction]; }]; [alertController addAction:confirmAction]; // 获取当前视图控制器 UIViewController *currentViewController = [UIApplication sharedApplication].keyWindow.rootViewController; // 显示弹窗 [self presentViewController:alertController animated:YES completion:nil]; } } - (UIButton *)unlockTextBtn{ if (_unlockTextBtn == nil) { _unlockTextBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, GetRealWidth(100), GetRealWidth(20))]; [_unlockTextBtn setTitle:@"开锁" forState:UIControlStateNormal]; _unlockTextBtn.titleLabel.font = [UIFont fontWithName:APP_UIFont size:14.0]; _unlockTextBtn.titleLabel.textAlignment = NSTextAlignmentCenter; // _unlockTextBtn.titleLabel.textColor = TextColor; [_unlockTextBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled]; [_unlockTextBtn setTitleColor:TextColor forState:UIControlStateNormal]; // [_unlockTextBtn setTitleColor:TextSelectColor forState:UIControlStateSelected]; [_unlockTextBtn addTarget:self action:@selector(unlockAction) forControlEvents:UIControlEventTouchUpInside]; [_unlockTextBtn addTarget:self action:@selector(unlockDownAction) forControlEvents:UIControlEventTouchDown]; _unlockTextBtn.adjustsImageWhenHighlighted = NO; _unlockTextBtn.center = CGPointMake(_unlockImgBtn.center.x, GetRealWidth(364)); } return _unlockTextBtn; } #pragma 开锁成功 -(void)setOpenDoorSuccess{ [self setUnlock:NO]; _openDoorTimeout = 0; [self startOpenDoorCountdown]; [self showUIAlertView:unlockSuccessfullyStr]; // // 开锁成功回调 // if(self.mESCallDelegate != NULL){ // [self.mESCallDelegate onUnlockAction]; // } } -(void)setUnlock:(BOOL)ISEnable{ [_unlockImgBtn setEnabled:ISEnable]; [_unlockTextBtn setEnabled:ISEnable]; } /** 开启倒计时 */ - (void)startOpenDoorCountdown { if (_openDoorTimeout > 10) { return; } _openDoorTimeout = 0; // GCD定时器 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); _openDoorTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(_openDoorTimer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0); //每秒执行 dispatch_source_set_event_handler(_openDoorTimer, ^{ WEAKSELF_AT if(weakSelf_AT.openDoorTimeout >= 10 ){// 计时结束 // 关闭定时器 dispatch_source_cancel(weakSelf_AT.openDoorTimer); dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf_AT setUnlock:YES]; }); }else{// 计时中 weakSelf_AT.openDoorTimeout++; } }); // 开启定时器 dispatch_resume(_openDoorTimer); } #pragma SDK可视对讲 功能部分 -(void)initESVideo{ linphone_core_set_native_video_window_id([HDLLinphoneManager getLc], (__bridge void *)(self.videoView)); // [self.centerView addSubview:[self.playWindow getWindowView]]; } -(void)initData{ [_screenshotTextBtn setTitle:screenshotStr forState:UIControlStateNormal]; [_unlockTextBtn setTitle:unlockStr forState:UIControlStateNormal]; // _titleUILabel.text = [[LCApiKit sharedInstance] currentDeviceName]; // [_collectButton setSelected:_isCollect]; } /** 暂停播放 */ - (void)stopPlay { [HDLLinphoneManager.instance endCall]; } - (void)startPlay { // [HDLLinphoneManager.instance fetchVideo]; } /** 开始监控 */ -(void)StartMonitoring{ NSLog(@"开始呼叫:%@",self.userName); [HDLLinphoneManager.instance startCall:self.userName]; } -(NSString *)getCurrentdateInterval { NSDate *datenow = [NSDate date]; NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970]*1000)]; return timeSp; } -(void)showUIAlertView:(NSString *)mes { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:tipStr message:mes preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:okStr style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; } /// -(void)showUIAlertViewWithBack:(NSString *)mes { isShowErrorAlert = true; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:tipStr message:mes preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:okStr style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self backAction]; }]]; [self presentViewController:alertController animated:YES completion:nil]; // [[HDLLinPhoneCommon rootController] presentViewController:alertController animated:YES completion:nil]; } #pragma viewDidAppear -(void)viewDidAppear:(BOOL)animated{ } //-(void)setIsInterrupt:(BOOL)isInterrupt{ // // if (_es) { // // _es.isInterrupt = isInterrupt; // // } //} - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; //1.暂停SDK相关播放 if (!self.isHangUpByRemote) { [self stopPlay]; } //2.Delegate释放 // self.mLCCallDelegate = nil; //3.定时器释放 if(_openDoorTimer){ dispatch_source_cancel(_openDoorTimer); _openDoorTimer = nil; // OK } [[NSNotificationCenter defaultCenter] removeObserver:self]; } -(void)dealloc{ NSLog(@"==============dealloc 1"); } /* 需要注意的是,有一个中断开始消息不一定会有一个中断结束消息,这就意味着中断结束的回调里的处理逻辑可能会没有被执行到。 所以需要关注当切到前台运行状态时,是不是需要重新激活你的 Audio Session。 */ - (void)willEnterForeground:(NSNotification*)notification{ NSLog(@"willEnterForeground"); } - (void)willEnterBackground:(NSNotification *)notification { isBackGround = YES; } #pragma 保存图片到相册 - (void)saveImageToPhotosAlbum:(UIImage *)savedImage { PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; if (status == PHAuthorizationStatusNotDetermined) { [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { if(status == PHAuthorizationStatusAuthorized) { UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), NULL); } }]; } else { if (status == PHAuthorizationStatusAuthorized) { UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), NULL); } } } // 指定回调方法 - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { NSString *message = nil; if (!error) { message = saveToTheAlbumsStr; } else { message = operationFailedStr; } [self showUIAlertView:message]; } /** */ - (void)saveThumbImage { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [paths objectAtIndex:0]; NSString *myDirectory = [libraryDirectory stringByAppendingPathComponent:@"ON+"]; NSString *picDirectory = [myDirectory stringByAppendingPathComponent:@"picture"]; NSDateFormatter *dataFormat = [[NSDateFormatter alloc] init]; [dataFormat setDateFormat:@"yyyyMMddHHmmss"]; NSString *strDate = [dataFormat stringFromDate:[NSDate date]]; NSString *datePath = [picDirectory stringByAppendingPathComponent:strDate]; NSString *picPath = [datePath stringByAppendingString:@".jpg"]; NSLog(@"test jpg name[%@]\n", picPath); NSFileManager *fileManage = [NSFileManager defaultManager]; NSError *pErr; BOOL isDir; if (NO == [fileManage fileExistsAtPath:myDirectory isDirectory:&isDir]) { [fileManage createDirectoryAtPath:myDirectory withIntermediateDirectories:YES attributes:nil error:&pErr]; } if (NO == [fileManage fileExistsAtPath:picDirectory isDirectory:&isDir]) { [fileManage createDirectoryAtPath:picDirectory withIntermediateDirectories:YES attributes:nil error:&pErr]; } // [self.playWindow snapShot:picPath]; UIImage *image = [UIImage imageWithContentsOfFile:picPath]; [self saveImageToPhotosAlbum:image]; } - (void)addOpenSuccessAction { [[NSNotificationCenter defaultCenter] removeObserver:self name:HDLLPCallDelegateOpenDoorSuccess object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setOpenDoorSuccess) name:HDLLPCallDelegateOpenDoorSuccess object:nil]; } @end