diff --git a/src/ios/VideoCallingViewController.m b/src/ios/VideoCallingViewController.m index 636393d..d4541af 100755 --- a/src/ios/VideoCallingViewController.m +++ b/src/ios/VideoCallingViewController.m @@ -45,6 +45,7 @@ static const NSInteger maxRemoteUserNum = 7; @property (weak, nonatomic) IBOutlet UIButton *subVisibleButton; @property (weak, nonatomic) IBOutlet UIButton *viewRotateButton; @property (strong, nonatomic) IBOutletCollection(UIView) NSArray *remoteViewArr; +@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *subViewBtnArr; @property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *displayLabelArr; @property (weak, nonatomic) IBOutlet UILabel *displayLabel; @@ -54,10 +55,12 @@ static const NSInteger maxRemoteUserNum = 7; @property (strong, nonatomic) NSString* userSig; @property (strong, nonatomic) TRTCCloud *trtcCloud; @property (strong, nonatomic) NSMutableOrderedSet *remoteUidSet; -@property (strong, nonatomic) NSMutableOrderedSet *viewUserSet; -@property (strong, nonatomic) TrtcUserInfo *sharedUser; +@property (strong, nonatomic) NSMutableArray *viewUsers; @property (assign, nonatomic) BOOL isFrontCamera; +@property (assign, nonatomic) BOOL *hasShareUser; +@property (assign, nonatomic) BOOL *hiddenAllSubView; + @property (assign, nonatomic) NSInteger rotation; @end @@ -75,18 +78,18 @@ static const NSInteger maxRemoteUserNum = 7; return _trtcCloud; } -- (NSMutableOrderedSet *)remoteUidSet { +- (NSMutableOrderedSet *)remoteUidSet { if (!_remoteUidSet) { _remoteUidSet = [[NSMutableOrderedSet alloc] initWithCapacity:maxRemoteUserNum]; } return _remoteUidSet; } -- (NSMutableOrderedSet *)viewUserSet { - if (!_viewUserSet) { - _viewUserSet = [[NSMutableOrderedSet alloc] initWithCapacity:maxRemoteUserNum]; +- (NSMutableArray *)viewUsers { + if (!_viewUsers) { + _viewUsers = [[NSMutableArray alloc] initWithCapacity:maxRemoteUserNum]; } - return _viewUserSet; + return _viewUsers; } - (instancetype)initWithRoomId:(UInt32)roomId userId:(NSString *)userId appId:(UInt32)appId userSig:(NSString *)userSig{ @@ -108,7 +111,7 @@ static const NSInteger maxRemoteUserNum = 7; TrtcUserInfo *user = [[TrtcUserInfo alloc]initWithPersonid:extra[@"userId"]]; NSInteger index = [[self remoteUidSet] indexOfObject: user]; NSLog(@"TRTC - userinfo.update -- userId:%@,index:%ld",extra[@"userId"],index); - if (index != NSNotFound) { return; } + if (index == NSNotFound) { return; } TrtcUserInfo *obj = [self remoteUidSet][index]; [obj setDisplayName: extra[@"displayName"]]; dispatch_async(dispatch_get_main_queue(), ^{ @@ -129,6 +132,7 @@ static const NSInteger maxRemoteUserNum = 7; } - (void)setupDefaultUIConfig { + NSLog(@"TRTC - setupDefaultUIConfig:::::"); _displayLabel.text = @"我"; _displayLabel.hidden = NO; _rotation = 0; @@ -164,23 +168,47 @@ static const NSInteger maxRemoteUserNum = 7; [TRTCCloud destroySharedIntance]; } +- (void)changeUser:(NSInteger) a and:(NSInteger) b{ + if(a == b || a < 0 || b < 0){return;} + NSInteger x = a > b ? b : a; + NSInteger y = a > b ? a : b; + if([self remoteUidSet].count > y && x>-1){ + // 交换 + NSLog(@"TRTC - onSubViewClcik:::::Before change:%@,%@",[[self remoteUidSet][x] personid],[[self remoteUidSet][y] personid]); + TrtcUserInfo* info = [self remoteUidSet][x]; + TrtcUserInfo* info2 = [self remoteUidSet][y]; + [_remoteUidSet removeObjectAtIndex:x]; + [_remoteUidSet setObject:info atIndex: y-1]; + [_remoteUidSet insertObject:info2 atIndex:x]; + NSLog(@"TRTC - onSubViewClcik:::::after change:%@,%@",[[self remoteUidSet][x] personid],[[self remoteUidSet][y] personid]); + [self refreshRemoteVideoViews]; + NSDictionary *event = [[NSDictionary alloc]initWithObjectsAndKeys: + [[NSString alloc] initWithFormat:@"%d",_roomId],@"room",info2.personid,@"userId", nil]; + [CordovaEventKit fireEvent:@"onLayoutChangeMessage" obj:event]; + } +} + #pragma mark - IBActions - (IBAction)onSubVisibleChange:(UIButton*)sender { - NSLog(@"TRTC - onSubVisibleChange:::::"); + NSLog(@"TRTC - onSubVisibleChange:::::,before:%@",[NSNumber numberWithBool:sender.selected]); sender.selected = !sender.selected; + NSLog(@"TRTC - onSubVisibleChange:::::,after:%@",[NSNumber numberWithBool:sender.selected]); [self refreshRemoteVideoViews]; } - (IBAction)onVideoRotate:(UIButton*)sender { - NSLog(@"TRTC - onRouteChange:::::"); + NSLog(@"TRTC - onRouteChange:::::,%@,local index: %ld",[[self remoteUidSet][0] personid],[_viewUsers indexOfObject:_userId]); TRTCRenderParams *params = [[TRTCRenderParams alloc] init]; params.fillMode = TRTCVideoFillMode_Fit; - _rotation += 90; - _rotation = _rotation > 270 ? 0 : _rotation; + _rotation += 1; + _rotation = _rotation > 3 ? 0 : _rotation; params.rotation = _rotation; +// [_trtcCloud stopRemoteView:[[self viewUsers][0] personid] streamType:TRTCVideoStreamTypeBig]; [_trtcCloud setRemoteRenderParams:[[self remoteUidSet][0] personid] streamType:TRTCVideoStreamTypeBig params:params]; +// [_trtcCloud updateRemoteView:self.view streamType:TRTCVideoStreamTypeBig forUser:[[self remoteUidSet][0] personid]]; +// [_trtcCloud startRemoteView:[[self viewUsers][0] personid] streamType: TRTCVideoStreamTypeBig view:self.view]; } @@ -218,6 +246,15 @@ static const NSInteger maxRemoteUserNum = 7; } } +- (IBAction)onSubViewClcik:(UIButton*)sender { + NSLog(@"TRTC - onSubViewClcik:::::button"); + NSInteger index = [_subViewBtnArr indexOfObject:sender]; + if (index == NSNotFound) {return;} + index = index + 1; + [self changeUser:0 and:index]; +} + + #pragma mark - TRTCCloud Delegate - (void)onEnterRoom:(NSInteger) result{ @@ -237,8 +274,6 @@ static const NSInteger maxRemoteUserNum = 7; [[NSString alloc] initWithFormat:@"%d",_roomId],@"room",_userId,@"userId", nil]; [CordovaEventKit fireEvent:@"onLayoutChangeMessage" obj:event]; - - } - (void)onUserVideoAvailable:(NSString *)userId available:(BOOL)available { @@ -249,65 +284,68 @@ static const NSInteger maxRemoteUserNum = 7; if (available) { if (index != NSNotFound) { return; } if([info isShareUser]){ - _sharedUser = info; - [[self remoteUidSet] setObject:info atIndex:0]; - [_trtcCloud stopLocalPreview]; - } else{ - [[self remoteUidSet] addObject:info]; + _hasShareUser = YES; } + [[self remoteUidSet] addObject:info]; } else { - if (index != NSNotFound) { return; } + if (index == NSNotFound) { return; } if([info isShareUser]){ - _sharedUser = nil; - [_trtcCloud stopLocalPreview]; + _hasShareUser = NO; + _hiddenAllSubView = NO; } [_trtcCloud stopRemoteView:userId streamType:TRTCVideoStreamTypeSmall]; [[self remoteUidSet] removeObject:userId]; } [self refreshRemoteVideoViews]; - NSDictionary *event = [[NSDictionary alloc]initWithObjectsAndKeys: - [[NSString alloc] initWithFormat:@"%d",_roomId],@"room",userId,@"userId",available,@"available", nil]; + if(_hasShareUser){ + // 切换分享屏幕到主窗口 + [self changeUser:0 and:[[self remoteUidSet] indexOfObject:info]]; + // 隐藏所有子窗口 + _hiddenAllSubView = YES; + [self refreshRemoteVideoViews]; +// [_trtcCloud stopLocalPreview]; + } + NSDictionary *event = @{@"room": [[NSString alloc] initWithFormat:@"%d", _roomId], @"userId": userId, @"available": [NSNumber numberWithBool:available]}; [CordovaEventKit fireEvent:@"onUserVideoAvailable" obj:event]; } - (void)refreshRemoteVideoViews { - NSLog(@"TRTC - refreshRemoteVideoViews"); -// NSInteger index = 0; -// for (NSString* userId in [self remoteUidSet]) {0 -// if (index >= maxRemoteUserNum) { return; } -// [_remoteViewArr[index] setHidden:NO]; -// [_trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeSmall -// view:_remoteViewArr[index++]]; -// } + NSLog(@"TRTC - refreshRemoteVideoViews:%ld",_remoteViewArr.count); - _subVisibleButton.hidden = _sharedUser != nil; - _viewRotateButton.hidden = _sharedUser == nil; + _subVisibleButton.hidden = _hasShareUser != nil; + _viewRotateButton.hidden = _hasShareUser == nil; + _viewRotateButton.enabled = !_viewRotateButton.hidden; for (int i = 0; i <= _remoteViewArr.count; i++) { TrtcUserInfo* nUser = [self remoteUidSet].count > i ? [self remoteUidSet][i] : nil; - TrtcUserInfo* oUser = [self viewUserSet].count > i ? [self viewUserSet][i] : nil; + TrtcUserInfo* oUser = [self viewUsers].count > i ? [self viewUsers][i] : nil; UIView* view = i == 0 ? self.view : _remoteViewArr[i-1]; UILabel* label = i ==0 ? _displayLabel : _displayLabelArr[i-1]; - if((_sharedUser != nil || [_subVisibleButton isSelected]) && i!=0){ + UIButton* btn = i ==0 ? nil : _subViewBtnArr[i-1]; + NSLog(@"TRTC - refreshRemoteVideoViews - new : %@, o: %@",nUser == nil ? nil : [nUser personid],oUser == nil ? nil : [oUser personid]); + if((_hiddenAllSubView || ![_subVisibleButton isSelected]) && i!=0){ [view setHidden:YES]; [label setHidden:YES]; }else{ if(nUser != nil){ - if([nUser isEqual:oUser]){ - if([nUser displayName] == nil || [nUser displayName].length == 0) { + if([nUser isEqual:oUser] && ![view isHidden]){ + if([nUser isLocalUser]){ + [label setText:@"我"]; + } else if([nUser displayName] == nil || [nUser displayName].length == 0) { [label setText:[NSString stringWithFormat:@"%@%@",[nUser personid], [nUser isShareUser] ? @"的屏幕分享" : @""]]; } else { [label setText:[NSString stringWithFormat:@"%@%@",[nUser displayName], [nUser isShareUser] ? @"的屏幕分享" : @""]]; } - return; + [label sizeToFit]; + continue; } - TRTCRenderParams *params = [TRTCRenderParams init]; + TRTCRenderParams *params = [[TRTCRenderParams alloc] init]; params.rotation = 0; params.fillMode = [nUser isShareUser] ? TRTCVideoFillMode_Fit : TRTCVideoFillMode_Fill; if([nUser isLocalUser]){ [_trtcCloud startLocalPreview:_isFrontCamera view:view]; [_trtcCloud setLocalRenderParams:params]; - [label setText:@"我"]; + [label sizeToFit]; } else { [_trtcCloud setRemoteRenderParams:[nUser personid] streamType:i == 0 ? TRTCVideoStreamTypeBig : TRTCVideoStreamTypeSmall params:params]; [_trtcCloud startRemoteView:[nUser personid] streamType:i == 0 ? TRTCVideoStreamTypeBig : TRTCVideoStreamTypeSmall view:view]; @@ -317,18 +355,28 @@ static const NSInteger maxRemoteUserNum = 7; } else { [label setText:[NSString stringWithFormat:@"%@%@",[nUser displayName], [nUser isShareUser] ? @"的屏幕分享" : @""]]; } + [label sizeToFit]; } [label setHidden:NO]; [view setHidden:NO]; + if(btn != nil){ + [btn setHidden:NO]; + } } else { [view setHidden:YES]; [label setHidden:YES]; + if(btn != nil){ + [btn setHidden:YES]; + } } } - if( [self viewUserSet].count > i ){ - [[self viewUserSet] setObject:nUser atIndex:i]; + if(nUser != nil){ + [self viewUsers].count > i ? [self viewUsers][i] = nUser : [[self viewUsers] addObject:nUser]; + }else { + while([self viewUsers].count > i){ + [[self viewUsers] removeObjectAtIndex:i]; + } } - } } diff --git a/src/ios/VideoCallingViewController.xib b/src/ios/VideoCallingViewController.xib index 0fc2fa8..60fbb93 100755 --- a/src/ios/VideoCallingViewController.xib +++ b/src/ios/VideoCallingViewController.xib @@ -26,6 +26,12 @@ + + + + + + @@ -44,8 +50,20 @@ + + + + @@ -62,6 +80,15 @@ + @@ -80,6 +107,15 @@ + @@ -98,6 +134,15 @@ + @@ -116,6 +161,15 @@ + @@ -134,6 +188,15 @@ +