WFCUMultiVideoViewController.m 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. //
  2. // ViewController.m
  3. // WFDemo
  4. //
  5. // Created by heavyrain on 17/9/27.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUMultiVideoViewController.h"
  9. #import <AVFoundation/AVFoundation.h>
  10. #import <AVKit/AVKit.h>
  11. #if WFCU_SUPPORT_VOIP
  12. #import <WebRTC/WebRTC.h>
  13. #import <WFAVEngineKit/WFAVEngineKit.h>
  14. #import "WFCUFloatingWindow.h"
  15. #import "WFCUParticipantCollectionViewCell.h"
  16. #endif
  17. #import <SDWebImage/SDWebImage.h>
  18. #import <WFChatClient/WFCCConversation.h>
  19. #import "WFCUPortraitCollectionViewCell.h"
  20. #import "WFCUParticipantCollectionViewLayout.h"
  21. #import "WFCUSeletedUserViewController.h"
  22. #import "UIView+Toast.h"
  23. #import "WFCUConfigManager.h"
  24. #import "WFCUImage.h"
  25. #import "WFZConferenceInfo.h"
  26. #import "WFCUUtilities.h"
  27. @interface WFCUMultiVideoViewController () <UITextFieldDelegate
  28. #if WFCU_SUPPORT_VOIP
  29. ,WFAVCallSessionDelegate
  30. #endif
  31. ,UICollectionViewDataSource
  32. ,UICollectionViewDelegate
  33. >
  34. #if WFCU_SUPPORT_VOIP
  35. @property (nonatomic, strong) UIView *bigVideoView;
  36. @property (nonatomic, strong) UICollectionView *smallCollectionView;
  37. @property (nonatomic, strong) UICollectionView *portraitCollectionView;
  38. @property (nonatomic, strong) UIButton *hangupButton;
  39. @property (nonatomic, strong) UIButton *answerButton;
  40. @property (nonatomic, strong) UIButton *switchCameraButton;
  41. @property (nonatomic, strong) UIButton *audioButton;
  42. @property (nonatomic, strong) UIButton *speakerButton;
  43. @property (nonatomic, strong) UIButton *videoButton;
  44. @property (nonatomic, strong) UIButton *scalingButton;
  45. @property (nonatomic, strong) UIButton *minimizeButton;
  46. @property (nonatomic, strong) UIButton *addParticipantButton;
  47. @property (nonatomic, strong) UIImageView *portraitView;
  48. @property (nonatomic, strong) UILabel *userNameLabel;
  49. @property (nonatomic, strong) UILabel *stateLabel;
  50. @property (nonatomic, strong) UILabel *connectTimeLabel;
  51. @property (nonatomic, strong) WFAVCallSession *currentSession;
  52. @property (nonatomic, assign) WFAVVideoScalingType smallScalingType;
  53. @property (nonatomic, assign) WFAVVideoScalingType bigScalingType;
  54. @property (nonatomic, assign) CGPoint panStartPoint;
  55. @property (nonatomic, assign) CGRect panStartVideoFrame;
  56. @property (nonatomic, strong) NSTimer *connectedTimer;
  57. @property (nonatomic, strong) NSMutableArray<NSString *> *participants;
  58. @property(nonatomic, strong)NSTimer *broadcastOngoingTimer;
  59. #endif
  60. @end
  61. #define ButtonSize 60
  62. #define BottomPadding 36
  63. #define SmallVideoView 120
  64. #define OperationTitleFont 10
  65. #define OperationButtonSize 50
  66. #define PortraitItemSize 100
  67. @implementation WFCUMultiVideoViewController
  68. #if !WFCU_SUPPORT_VOIP
  69. - (instancetype)initWithSession:(WFAVCallSession *)session {
  70. self = [super init];
  71. return self;
  72. }
  73. - (instancetype)initWithTargets:(NSArray<NSString *> *)targetIds conversation:(WFCCConversation *)conversation audioOnly:(BOOL)audioOnly {
  74. self = [super init];
  75. return self;
  76. }
  77. #else
  78. - (instancetype)initWithSession:(WFAVCallSession *)session {
  79. self = [super init];
  80. if (self) {
  81. self.currentSession = session;
  82. self.currentSession.delegate = self;
  83. [self didChangeState:kWFAVEngineStateIncomming];
  84. [self rearrangeParticipants];
  85. }
  86. return self;
  87. }
  88. - (instancetype)initWithTargets:(NSArray<NSString *> *)targetIds conversation:(WFCCConversation *)conversation audioOnly:(BOOL)audioOnly {
  89. self = [super init];
  90. if (self) {
  91. WFAVCallSession *session = [[WFAVEngineKit sharedEngineKit] startCall:targetIds
  92. audioOnly:audioOnly
  93. callExtra:nil
  94. conversation:conversation
  95. sessionDelegate:self];
  96. self.currentSession = session;
  97. [self rearrangeParticipants];
  98. }
  99. return self;
  100. }
  101. /*
  102. session的participantIds是除了自己外的所有成员。这里把自己也加入列表,然后把发起者放到最后面。
  103. */
  104. - (void)rearrangeParticipants {
  105. self.participants = [[NSMutableArray alloc] init];
  106. [self.participants addObjectsFromArray:self.currentSession.participantIds];
  107. if ([self.currentSession.initiator isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  108. [self.participants addObject:[WFCCNetworkService sharedInstance].userId];
  109. } else {
  110. if ([self.participants containsObject:self.currentSession.initiator]) {
  111. [self.participants insertObject:[WFCCNetworkService sharedInstance].userId atIndex:[self.participants indexOfObject:self.currentSession.initiator]];
  112. [self.participants removeObject:self.currentSession.initiator];
  113. [self.participants addObject:self.currentSession.initiator];
  114. } else {
  115. [self.participants addObject:[WFCCNetworkService sharedInstance].userId];
  116. }
  117. }
  118. }
  119. - (void)viewDidLoad {
  120. [super viewDidLoad];
  121. [self.view setBackgroundColor:[UIColor blackColor]];
  122. if(self.currentSession.state == kWFAVEngineStateIdle) {
  123. [self didCallEndWithReason:self.currentSession.endReason];
  124. return;
  125. }
  126. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveMessages:) name:kReceiveMessages object:nil];
  127. self.smallScalingType = kWFAVVideoScalingTypeAspectFit;
  128. self.bigScalingType = kWFAVVideoScalingTypeAspectBalanced;
  129. self.bigVideoView = [[UIView alloc] initWithFrame:self.view.bounds];
  130. UITapGestureRecognizer *tapBigVideo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickedBigVideoView:)];
  131. [self.bigVideoView addGestureRecognizer:tapBigVideo];
  132. self.bigVideoView.layer.borderWidth = 1;
  133. self.bigVideoView.layer.borderColor = [UIColor clearColor].CGColor;
  134. self.bigVideoView.layer.masksToBounds = YES;
  135. [self.view addSubview:self.bigVideoView];
  136. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  137. CGFloat itemWidth = (self.view.frame.size.width + layout.minimumLineSpacing)/3 - layout.minimumLineSpacing;
  138. layout.itemSize = CGSizeMake(itemWidth, itemWidth);
  139. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  140. int lines = (int)([self.currentSession participantIds].count + 2) /3;
  141. self.smallCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, [WFCUUtilities wf_navigationFullHeight], self.view.frame.size.width, itemWidth*lines + layout.minimumLineSpacing * (lines - 1)) collectionViewLayout:layout];
  142. self.smallCollectionView.dataSource = self;
  143. self.smallCollectionView.delegate = self;
  144. [self.smallCollectionView registerClass:[WFCUParticipantCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
  145. self.smallCollectionView.backgroundColor = [UIColor clearColor];
  146. [self.smallCollectionView addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onSmallVideoPan:)]];
  147. if (self.currentSession.audioOnly) {
  148. self.smallCollectionView.hidden = YES;
  149. }
  150. [self.view addSubview:self.smallCollectionView];
  151. WFCUParticipantCollectionViewLayout *layout2 = [[WFCUParticipantCollectionViewLayout alloc] init];
  152. CGFloat itemHeight = MIN(PortraitItemSize, (self.view.frame.size.width - 32 - 2*layout.minimumLineSpacing)/3);
  153. layout2.itemHeight = itemHeight;
  154. layout2.itemWidth = itemHeight;
  155. layout2.lineSpace = 6;
  156. layout2.itemSpace = 6;
  157. self.portraitCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(16, self.view.frame.size.height - BottomPadding - ButtonSize - (itemHeight)*3, self.view.frame.size.width - 32, (itemHeight)*3+2*layout.minimumLineSpacing) collectionViewLayout:layout2];
  158. self.portraitCollectionView.dataSource = self;
  159. self.portraitCollectionView.delegate = self;
  160. [self.portraitCollectionView registerClass:[WFCUPortraitCollectionViewCell class] forCellWithReuseIdentifier:@"cell2"];
  161. self.portraitCollectionView.backgroundColor = [UIColor clearColor];
  162. [self.view addSubview:self.portraitCollectionView];
  163. if(self.currentSession.state == kWFAVEngineStateOutgoing && !self.currentSession.isAudioOnly) {
  164. [[WFAVEngineKit sharedEngineKit] startVideoPreview];
  165. }
  166. WFCCUserInfo *user = [[WFCCIMService sharedWFCIMService] getUserInfo:self.currentSession.inviter inGroup:self.currentSession.conversation.type == Group_Type ? self.currentSession.conversation.target : nil refresh:NO];
  167. self.portraitView = [[UIImageView alloc] init];
  168. [self.portraitView sd_setImageWithURL:[NSURL URLWithString:[user.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"]];
  169. self.portraitView.layer.masksToBounds = YES;
  170. self.portraitView.layer.cornerRadius = 8.f;
  171. [self.view addSubview:self.portraitView];
  172. self.userNameLabel = [[UILabel alloc] init];
  173. self.userNameLabel.font = [UIFont systemFontOfSize:26];
  174. self.userNameLabel.text = user.displayName;
  175. self.userNameLabel.textColor = [UIColor whiteColor];
  176. [self.view addSubview:self.userNameLabel];
  177. self.stateLabel = [[UILabel alloc] init];
  178. self.stateLabel.font = [UIFont systemFontOfSize:16];
  179. self.stateLabel.textColor = [UIColor whiteColor];
  180. [self.view addSubview:self.stateLabel];
  181. self.connectTimeLabel = [[UILabel alloc] init];
  182. self.connectTimeLabel.font = [UIFont systemFontOfSize:16];
  183. self.connectTimeLabel.textColor = [UIColor whiteColor];
  184. [self.view addSubview:self.connectTimeLabel];
  185. [self updateTopViewFrame];
  186. [self didChangeState:self.currentSession.state];//update ui
  187. [[NSNotificationCenter defaultCenter] addObserver:self
  188. selector:@selector(onDeviceOrientationDidChange)
  189. name:UIDeviceOrientationDidChangeNotification
  190. object:nil];
  191. [self onDeviceOrientationDidChange];
  192. [self reloadVideoUI];
  193. }
  194. - (void)onReceiveMessages:(NSNotification *)notification {
  195. NSArray<WFCCMessage *> *messages = notification.object;
  196. NSMutableArray<WFCCMultiCallOngoingMessageContent *> *ongoingCalls = [[NSMutableArray alloc] init];
  197. for (WFCCMessage *msg in messages) {
  198. if([msg.content isKindOfClass:WFCCMultiCallOngoingMessageContent.class]) {
  199. WFCCMultiCallOngoingMessageContent *ongoing = (WFCCMultiCallOngoingMessageContent *)msg.content;
  200. [ongoingCalls addObject:ongoing];
  201. } else if([msg.content isKindOfClass:WFCCJoinCallRequestMessageContent.class]) {
  202. WFCCJoinCallRequestMessageContent *join = (WFCCJoinCallRequestMessageContent *)msg.content;
  203. if([self.currentSession.callId isEqualToString:join.callId] && self.currentSession.state == kWFAVEngineStateConnected && [self.currentSession.initiator isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  204. [self.currentSession inviteNewParticipants:@[msg.fromUser] targetClientId:join.clientId autoAnswer:YES];
  205. }
  206. }
  207. }
  208. }
  209. - (UIButton *)hangupButton {
  210. if (!_hangupButton) {
  211. _hangupButton = [[UIButton alloc] init];
  212. [_hangupButton setImage:[WFCUImage imageNamed:@"hangup"] forState:UIControlStateNormal];
  213. [_hangupButton setImage:[WFCUImage imageNamed:@"hangup_hover"] forState:UIControlStateHighlighted];
  214. [_hangupButton setImage:[WFCUImage imageNamed:@"hangup_hover"] forState:UIControlStateSelected];
  215. _hangupButton.backgroundColor = [UIColor clearColor];
  216. [_hangupButton addTarget:self action:@selector(hanupButtonDidTap:) forControlEvents:UIControlEventTouchDown];
  217. _hangupButton.hidden = YES;
  218. [self.view addSubview:_hangupButton];
  219. }
  220. return _hangupButton;
  221. }
  222. - (UIButton *)answerButton {
  223. if (!_answerButton) {
  224. _answerButton = [[UIButton alloc] init];
  225. if (self.currentSession.audioOnly) {
  226. [_answerButton setImage:[WFCUImage imageNamed:@"answer"] forState:UIControlStateNormal];
  227. [_answerButton setImage:[WFCUImage imageNamed:@"answer_hover"] forState:UIControlStateHighlighted];
  228. [_answerButton setImage:[WFCUImage imageNamed:@"answer_hover"] forState:UIControlStateSelected];
  229. } else {
  230. [_answerButton setImage:[WFCUImage imageNamed:@"video_answer"] forState:UIControlStateNormal];
  231. [_answerButton setImage:[WFCUImage imageNamed:@"video_answer_hover"] forState:UIControlStateHighlighted];
  232. [_answerButton setImage:[WFCUImage imageNamed:@"video_answer_hover"] forState:UIControlStateSelected];
  233. }
  234. _answerButton.backgroundColor = [UIColor clearColor];
  235. [_answerButton addTarget:self action:@selector(answerButtonDidTap:) forControlEvents:UIControlEventTouchDown];
  236. _answerButton.hidden = YES;
  237. [self.view addSubview:_answerButton];
  238. }
  239. return _answerButton;
  240. }
  241. - (UIButton *)minimizeButton {
  242. if (!_minimizeButton) {
  243. _minimizeButton = [[UIButton alloc] initWithFrame:CGRectMake(16, 26, 30, 30)];
  244. [_minimizeButton setImage:[WFCUImage imageNamed:@"minimize"] forState:UIControlStateNormal];
  245. [_minimizeButton setImage:[WFCUImage imageNamed:@"minimize_hover"] forState:UIControlStateHighlighted];
  246. [_minimizeButton setImage:[WFCUImage imageNamed:@"minimize_hover"] forState:UIControlStateSelected];
  247. _minimizeButton.backgroundColor = [UIColor clearColor];
  248. [_minimizeButton addTarget:self action:@selector(minimizeButtonDidTap:) forControlEvents:UIControlEventTouchDown];
  249. _minimizeButton.hidden = NO;
  250. [self.view addSubview:_minimizeButton];
  251. }
  252. return _minimizeButton;
  253. }
  254. - (UIButton *)addParticipantButton {
  255. if (!_addParticipantButton) {
  256. _addParticipantButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 16 - 30, 26, 30, 30)];
  257. [_addParticipantButton setImage:[WFCUImage imageNamed:@"plus-circle"] forState:UIControlStateNormal];
  258. [_addParticipantButton setImage:[WFCUImage imageNamed:@"plus-circle"] forState:UIControlStateHighlighted];
  259. [_addParticipantButton setImage:[WFCUImage imageNamed:@"plus-circle"] forState:UIControlStateSelected];
  260. _addParticipantButton.backgroundColor = [UIColor clearColor];
  261. [_addParticipantButton addTarget:self action:@selector(addParticipantButtonDidTap:) forControlEvents:UIControlEventTouchDown];
  262. _addParticipantButton.hidden = YES;
  263. [self.view addSubview:_addParticipantButton];
  264. }
  265. return _addParticipantButton;
  266. }
  267. - (UIButton *)switchCameraButton {
  268. if (!_switchCameraButton) {
  269. _switchCameraButton = [[UIButton alloc] init];
  270. [_switchCameraButton setImage:[WFCUImage imageNamed:@"switchcamera"] forState:UIControlStateNormal];
  271. [_switchCameraButton setImage:[WFCUImage imageNamed:@"switchcamera_hover"] forState:UIControlStateHighlighted];
  272. [_switchCameraButton setImage:[WFCUImage imageNamed:@"switchcamera_hover"] forState:UIControlStateSelected];
  273. _switchCameraButton.backgroundColor = [UIColor clearColor];
  274. [_switchCameraButton addTarget:self action:@selector(switchCameraButtonDidTap:) forControlEvents:UIControlEventTouchDown];
  275. _switchCameraButton.hidden = YES;
  276. [self.view addSubview:_switchCameraButton];
  277. }
  278. return _switchCameraButton;
  279. }
  280. - (UIButton *)audioButton {
  281. if (!_audioButton) {
  282. _audioButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-ButtonSize/2, self.view.frame.size.height-10-ButtonSize, ButtonSize, ButtonSize)];
  283. [_audioButton setImage:[WFCUImage imageNamed:@"mute"] forState:UIControlStateNormal];
  284. [_audioButton setImage:[WFCUImage imageNamed:@"mute_hover"] forState:UIControlStateHighlighted];
  285. [_audioButton setImage:[WFCUImage imageNamed:@"mute_hover"] forState:UIControlStateSelected];
  286. _audioButton.backgroundColor = [UIColor clearColor];
  287. [_audioButton addTarget:self action:@selector(audioButtonDidTap:) forControlEvents:UIControlEventTouchDown];
  288. _audioButton.hidden = YES;
  289. [self updateAudioButton];
  290. [self.view addSubview:_audioButton];
  291. }
  292. return _audioButton;
  293. }
  294. - (UIButton *)speakerButton {
  295. if (!_speakerButton) {
  296. _speakerButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-ButtonSize/2, self.view.frame.size.height-10-ButtonSize, ButtonSize, ButtonSize)];
  297. [_speakerButton setImage:[WFCUImage imageNamed:@"speaker"] forState:UIControlStateNormal];
  298. [_speakerButton setImage:[WFCUImage imageNamed:@"speaker_hover"] forState:UIControlStateHighlighted];
  299. [_speakerButton setImage:[WFCUImage imageNamed:@"speaker_hover"] forState:UIControlStateSelected];
  300. _speakerButton.backgroundColor = [UIColor clearColor];
  301. [_speakerButton addTarget:self action:@selector(speakerButtonDidTap:) forControlEvents:UIControlEventTouchDown];
  302. _speakerButton.hidden = YES;
  303. [self.view addSubview:_speakerButton];
  304. }
  305. return _speakerButton;
  306. }
  307. - (UIButton *)videoButton {
  308. if (!_videoButton) {
  309. _videoButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width*3/4-ButtonSize/4, self.view.frame.size.height-45-ButtonSize-ButtonSize/2-2, ButtonSize/2, ButtonSize/2)];
  310. [_videoButton setImage:[WFCUImage imageNamed:@"enable_video"] forState:UIControlStateNormal];
  311. _videoButton.backgroundColor = [UIColor clearColor];
  312. [_videoButton addTarget:self action:@selector(videoButtonDidTap:) forControlEvents:UIControlEventTouchDown];
  313. _videoButton.hidden = YES;
  314. [self updateVideoButton];
  315. [self.view addSubview:_videoButton];
  316. }
  317. return _videoButton;
  318. }
  319. - (UIButton *)scalingButton {
  320. if (!_scalingButton) {
  321. _scalingButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-ButtonSize/2, self.view.frame.size.height-10-ButtonSize, ButtonSize, ButtonSize)];
  322. [_scalingButton setTitle:WFCString(@"Scale") forState:UIControlStateNormal];
  323. _scalingButton.backgroundColor = [UIColor greenColor];
  324. [_scalingButton addTarget:self action:@selector(scalingButtonDidTap:) forControlEvents:UIControlEventTouchDown];
  325. _scalingButton.hidden = YES;
  326. [self.view addSubview:_scalingButton];
  327. }
  328. return _scalingButton;
  329. }
  330. - (void)startConnectedTimer {
  331. [self stopConnectedTimer];
  332. self.connectedTimer = [NSTimer scheduledTimerWithTimeInterval:1
  333. target:self
  334. selector:@selector(updateConnectedTimeLabel)
  335. userInfo:nil
  336. repeats:YES];
  337. [self.connectedTimer fire];
  338. }
  339. - (void)stopConnectedTimer {
  340. if (self.connectedTimer) {
  341. [self.connectedTimer invalidate];
  342. self.connectedTimer = nil;
  343. }
  344. }
  345. - (void)setFocusUser:(NSString *)userId {
  346. if (userId) {
  347. [self.participants removeObject:userId];
  348. [self.participants addObject:userId];
  349. [self reloadVideoUI];
  350. }
  351. }
  352. - (void)updateConnectedTimeLabel {
  353. long sec = [[NSDate date] timeIntervalSince1970] - self.currentSession.connectedTime / 1000;
  354. if (sec < 60 * 60) {
  355. self.connectTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", sec / 60, sec % 60];
  356. } else {
  357. self.connectTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", sec / 60 / 60, (sec / 60) % 60, sec % 60];
  358. }
  359. }
  360. - (void)hanupButtonDidTap:(UIButton *)button {
  361. if(self.currentSession.state != kWFAVEngineStateIdle) {
  362. [self.currentSession endCall];
  363. }
  364. }
  365. - (void)answerButtonDidTap:(UIButton *)button {
  366. [WFCUUtilities checkRecordOrCameraPermission:YES complete:^(BOOL granted) {
  367. if(granted) {
  368. if(self.currentSession.audioOnly) {
  369. if (self.currentSession.state == kWFAVEngineStateIncomming) {
  370. [self.currentSession answerCall:NO callExtra:nil];
  371. }
  372. } else {
  373. [WFCUUtilities checkRecordOrCameraPermission:NO complete:^(BOOL granted) {
  374. if(granted) {
  375. if (self.currentSession.state == kWFAVEngineStateIncomming) {
  376. [self.currentSession answerCall:NO callExtra:nil];
  377. }
  378. }
  379. } viewController:self];
  380. }
  381. }
  382. } viewController:self];
  383. }
  384. - (void)minimizeButtonDidTap:(UIButton *)button {
  385. __block NSString *focusUser = [self.participants lastObject];
  386. [WFCUFloatingWindow startCallFloatingWindow:self.currentSession focusUser:focusUser withTouchedBlock:^(WFAVCallSession *callSession, WFZConferenceInfo *conferenceInfo) {
  387. WFCUMultiVideoViewController *vc = [[WFCUMultiVideoViewController alloc] initWithSession:callSession];
  388. [vc setFocusUser:focusUser];
  389. [[WFAVEngineKit sharedEngineKit] presentViewController:vc];
  390. }];
  391. [[WFAVEngineKit sharedEngineKit] dismissViewController:self];
  392. }
  393. - (void)didMoveToParentViewController:(UIViewController *)parent {
  394. [self startBroadcastCallOngoing:NO];
  395. }
  396. - (void)addParticipantButtonDidTap:(UIButton *)button {
  397. WFCUSeletedUserViewController *pvc = [[WFCUSeletedUserViewController alloc] init];
  398. NSMutableArray *disabledUser = [[NSMutableArray alloc] init];
  399. [disabledUser addObjectsFromArray:self.participants];
  400. pvc.disableUserIds = disabledUser;
  401. pvc.maxSelectCount = self.currentSession.audioOnly ? [WFAVEngineKit sharedEngineKit].maxAudioCallCount : [WFAVEngineKit sharedEngineKit].maxVideoCallCount;
  402. pvc.groupId = self.currentSession.conversation.target;
  403. NSMutableArray *candidateUser = [[NSMutableArray alloc] init];
  404. NSArray<WFCCGroupMember *> *members = [[WFCCIMService sharedWFCIMService] getGroupMembers:self.currentSession.conversation.target forceUpdate:NO];
  405. for (WFCCGroupMember *member in members) {
  406. [candidateUser addObject:member.memberId];
  407. }
  408. pvc.candidateUsers = candidateUser;
  409. pvc.type = Vertical;
  410. __weak typeof(self)ws = self;
  411. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  412. if (contacts.count) {
  413. [ws.currentSession inviteNewParticipants:contacts];
  414. }
  415. };
  416. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:pvc];
  417. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  418. dispatch_async(dispatch_get_main_queue(), ^{
  419. [ws presentViewController:navi animated:YES completion:nil];
  420. });
  421. }
  422. - (void)switchCameraButtonDidTap:(UIButton *)button {
  423. if (self.currentSession.state != kWFAVEngineStateIdle) {
  424. [self.currentSession switchCamera];
  425. }
  426. }
  427. - (void)audioButtonDidTap:(UIButton *)button {
  428. if (self.currentSession.state != kWFAVEngineStateIdle) {
  429. [self.currentSession muteAudio:!self.currentSession.audioMuted];
  430. [self updateAudioButton];
  431. }
  432. }
  433. - (void)updateAudioButton {
  434. if (self.currentSession.audioMuted) {
  435. [self.audioButton setImage:[WFCUImage imageNamed:@"mute_hover"] forState:UIControlStateNormal];
  436. } else {
  437. [self.audioButton setImage:[WFCUImage imageNamed:@"mute"] forState:UIControlStateNormal];
  438. }
  439. }
  440. - (void)speakerButtonDidTap:(UIButton *)button {
  441. if (self.currentSession.state != kWFAVEngineStateIdle) {
  442. [self.currentSession enableSpeaker:!self.currentSession.isSpeaker];
  443. [self updateSpeakerButton];
  444. }
  445. }
  446. - (void)updateSpeakerButton {
  447. if (!self.currentSession.isSpeaker) {
  448. if([self.currentSession isHeadsetPluggedIn]) {
  449. [self.speakerButton setImage:[WFCUImage imageNamed:@"speaker_headset"] forState:UIControlStateNormal];
  450. } else if([self.currentSession isBluetoothSpeaker]) {
  451. [self.speakerButton setImage:[WFCUImage imageNamed:@"speaker_bluetooth"] forState:UIControlStateNormal];
  452. } else {
  453. [self.speakerButton setImage:[WFCUImage imageNamed:@"speaker"] forState:UIControlStateNormal];
  454. }
  455. } else {
  456. [self.speakerButton setImage:[WFCUImage imageNamed:@"speaker_hover"] forState:UIControlStateNormal];
  457. }
  458. }
  459. - (void)updateVideoButton {
  460. if (self.currentSession.videoMuted) {
  461. [self.videoButton setImage:[WFCUImage imageNamed:@"disable_video"] forState:UIControlStateNormal];
  462. } else {
  463. [self.videoButton setImage:[WFCUImage imageNamed:@"enable_video"] forState:UIControlStateNormal];
  464. }
  465. }
  466. //1.决定当前界面是否开启自动转屏,如果返回NO,后面两个方法也不会被调用,只是会支持默认的方向
  467. - (BOOL)shouldAutorotate {
  468. return YES;
  469. }
  470. //2.返回支持的旋转方向
  471. //iPad设备上,默认返回值UIInterfaceOrientationMaskAllButUpSideDwon
  472. //iPad设备上,默认返回值是UIInterfaceOrientationMaskAll
  473. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
  474. return UIDeviceOrientationLandscapeLeft | UIDeviceOrientationLandscapeRight | UIDeviceOrientationPortrait;
  475. }
  476. //3.返回进入界面默认显示方向
  477. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  478. return UIInterfaceOrientationPortrait;
  479. }
  480. - (BOOL)onDeviceOrientationDidChange{
  481. if (self.currentSession.state == kWFAVEngineStateIdle) {
  482. return YES;
  483. }
  484. //获取当前设备Device
  485. UIDevice *device = [UIDevice currentDevice] ;
  486. NSString *lastUser = nil;
  487. switch (device.orientation) {
  488. case UIDeviceOrientationFaceUp:
  489. break;
  490. case UIDeviceOrientationFaceDown:
  491. break;
  492. case UIDeviceOrientationUnknown:
  493. //系统当前无法识别设备朝向,可能是倾斜
  494. break;
  495. case UIDeviceOrientationLandscapeLeft:
  496. self.bigVideoView.transform = CGAffineTransformMakeRotation(M_PI_2);
  497. self.bigVideoView.frame = self.view.bounds;
  498. lastUser = [self.participants lastObject];
  499. if ([lastUser isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  500. [self.currentSession setupLocalVideoView:self.bigVideoView scalingType:self.bigScalingType];
  501. } else {
  502. [self.currentSession setupRemoteVideoView:self.bigVideoView scalingType:self.bigScalingType forUser:lastUser screenSharing:NO];
  503. }
  504. break;
  505. case UIDeviceOrientationLandscapeRight:
  506. self.bigVideoView.transform = CGAffineTransformMakeRotation(-M_PI_2);
  507. self.bigVideoView.frame = self.view.bounds;
  508. lastUser = [self.participants lastObject];
  509. if ([lastUser isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  510. [self.currentSession setupLocalVideoView:self.bigVideoView scalingType:self.bigScalingType];
  511. } else {
  512. [self.currentSession setupRemoteVideoView:self.bigVideoView scalingType:self.bigScalingType forUser:lastUser screenSharing:NO];
  513. }
  514. break;
  515. case UIDeviceOrientationPortrait:
  516. self.bigVideoView.transform = CGAffineTransformMakeRotation(0);
  517. self.bigVideoView.frame = self.view.bounds;
  518. lastUser = [self.participants lastObject];
  519. if ([lastUser isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  520. [self.currentSession setupLocalVideoView:self.bigVideoView scalingType:self.bigScalingType];
  521. } else {
  522. [self.currentSession setupRemoteVideoView:self.bigVideoView scalingType:self.bigScalingType forUser:lastUser screenSharing:NO];
  523. }
  524. break;
  525. case UIDeviceOrientationPortraitUpsideDown:
  526. break;
  527. default:
  528. NSLog(@"無法识别");
  529. break;
  530. }
  531. if (!self.smallCollectionView.hidden) {
  532. [self.smallCollectionView reloadData];
  533. }
  534. return YES;
  535. }
  536. - (void)viewWillAppear:(BOOL)animated {
  537. [super viewWillAppear:animated];
  538. if (_currentSession.state == kWFAVEngineStateConnected) {
  539. [self updateConnectedTimeLabel];
  540. [self startConnectedTimer];
  541. }
  542. }
  543. - (void)viewWillDisappear:(BOOL)animated {
  544. [super viewWillDisappear:animated];
  545. [self stopConnectedTimer];
  546. }
  547. - (void)setPanStartPoint:(CGPoint)panStartPoint {
  548. _panStartPoint = panStartPoint;
  549. _panStartVideoFrame = self.smallCollectionView.frame;
  550. }
  551. - (void)moveToPanPoint:(CGPoint)panPoint {
  552. CGRect frame = self.panStartVideoFrame;
  553. CGSize moveSize = CGSizeMake(panPoint.x - self.panStartPoint.x, panPoint.y - self.panStartPoint.y);
  554. frame.origin.x += moveSize.width;
  555. frame.origin.y += moveSize.height;
  556. self.smallCollectionView.frame = frame;
  557. }
  558. - (void)onSmallVideoPan:(UIPanGestureRecognizer *)recognize {
  559. switch (recognize.state) {
  560. case UIGestureRecognizerStateBegan:
  561. self.panStartPoint = [recognize translationInView:self.view];
  562. break;
  563. case UIGestureRecognizerStateChanged: {
  564. CGPoint currentPoint = [recognize translationInView:self.view];
  565. [self moveToPanPoint:currentPoint];
  566. break;
  567. }
  568. case UIGestureRecognizerStateEnded: {
  569. CGPoint endPoint = [recognize translationInView:self.view];
  570. [self moveToPanPoint:endPoint];
  571. break;
  572. }
  573. case UIGestureRecognizerStateCancelled:
  574. case UIGestureRecognizerStateFailed:
  575. default:
  576. break;
  577. }
  578. }
  579. - (void)videoButtonDidTap:(UIButton *)button {
  580. if (self.currentSession.state != kWFAVEngineStateIdle) {
  581. [self.currentSession muteVideo:!self.currentSession.isVideoMuted];
  582. [self updateVideoButton];
  583. }
  584. }
  585. - (void)scalingButtonDidTap:(UIButton *)button {
  586. // if (self.currentSession.state != kWFAVEngineStateIdle) {
  587. // if (self.scalingType < kWFAVVideoScalingTypeAspectBalanced) {
  588. // self.scalingType++;
  589. // } else {
  590. // self.scalingType = kWFAVVideoScalingTypeAspectFit;
  591. // }
  592. //
  593. //// [self.currentSession setupLocalVideoView:self.smallVideoView scalingType:self.scalingType];
  594. //// [self.currentSession setupRemoteVideoView:self.bigVideoView scalingType:self.scalingType forUser:self.currentSession.participants[0]];
  595. // }
  596. }
  597. - (CGRect)getButtomCenterButtonFrame {
  598. return CGRectMake(self.view.frame.size.width/2-ButtonSize/2, self.view.frame.size.height-BottomPadding-ButtonSize, ButtonSize, ButtonSize);
  599. }
  600. - (CGRect)getButtomLeftButtonFrame {
  601. return CGRectMake(self.view.frame.size.width/4-ButtonSize/2, self.view.frame.size.height-BottomPadding-ButtonSize, ButtonSize, ButtonSize);
  602. }
  603. - (CGRect)getButtomRightButtonFrame {
  604. return CGRectMake(self.view.frame.size.width*3/4-ButtonSize/2, self.view.frame.size.height-BottomPadding-ButtonSize, ButtonSize, ButtonSize);
  605. }
  606. - (CGRect)getToAudioButtonFrame {
  607. return CGRectMake(self.view.frame.size.width*3/4-ButtonSize/2, self.view.frame.size.height-BottomPadding-ButtonSize-ButtonSize-2, ButtonSize, ButtonSize);
  608. }
  609. - (void)updateTopViewFrame {
  610. // if (self.currentSession.isAudioOnly) {
  611. CGFloat containerWidth = self.view.bounds.size.width;
  612. self.portraitView.frame = CGRectMake((containerWidth-64)/2, [WFCUUtilities wf_navigationFullHeight], 64, 64);;
  613. self.userNameLabel.frame = CGRectMake((containerWidth - 240)/2, [WFCUUtilities wf_navigationFullHeight] + 64 + 8, 240, 26);
  614. self.userNameLabel.textAlignment = NSTextAlignmentCenter;
  615. self.connectTimeLabel.textAlignment = NSTextAlignmentCenter;
  616. self.stateLabel.textAlignment = NSTextAlignmentCenter;
  617. if (self.currentSession.isAudioOnly) {
  618. self.connectTimeLabel.frame = CGRectMake((containerWidth - 240)/2, self.portraitCollectionView.frame.origin.y - 40, 240, 16);
  619. self.stateLabel.frame = CGRectMake((containerWidth - 240)/2, self.portraitCollectionView.frame.origin.y -40, 240, 16);
  620. } else {
  621. self.connectTimeLabel.frame = CGRectMake((containerWidth - 240)/2, self.smallCollectionView.frame.origin.y + self.smallCollectionView.frame.size.height + 8, 240, 16);
  622. self.stateLabel.frame = CGRectMake((containerWidth - 240)/2, self.smallCollectionView.frame.origin.y + self.smallCollectionView.frame.size.height + 30, 240, 16);
  623. }
  624. // } else {
  625. // self.portraitView.frame = CGRectMake(16, [WFCUUtilities wf_navigationFullHeight], 64, 64);
  626. // self.userNameLabel.frame = CGRectMake(96, [WFCUUtilities wf_navigationFullHeight] + 8, 240, 26);
  627. // if(![NSThread isMainThread]) {
  628. // NSLog(@"error not main thread");
  629. // }
  630. // self.userNameLabel.textAlignment = NSTextAlignmentLeft;
  631. // if(self.currentSession.state == kWFAVEngineStateConnected) {
  632. // self.stateLabel.frame = CGRectMake(54, 30, 240, 20);
  633. // } else {
  634. // self.stateLabel.frame = CGRectMake(96, [WFCUUtilities wf_navigationFullHeight] + 26 + 14, 240, 16);
  635. // }
  636. // self.stateLabel.textAlignment = NSTextAlignmentLeft;
  637. // }
  638. }
  639. - (void)onClickedBigVideoView:(id)sender {
  640. if (self.currentSession.state != kWFAVEngineStateConnected) {
  641. return;
  642. }
  643. if (self.currentSession.audioOnly) {
  644. return;
  645. }
  646. if (self.smallCollectionView.hidden) {
  647. if (self.hangupButton.hidden) {
  648. self.hangupButton.hidden = NO;
  649. self.audioButton.hidden = NO;
  650. if (self.currentSession.audioOnly) {
  651. self.videoButton.hidden = YES;
  652. } else {
  653. self.videoButton.hidden = NO;
  654. }
  655. self.switchCameraButton.hidden = NO;
  656. self.smallCollectionView.hidden = NO;
  657. self.minimizeButton.hidden = NO;
  658. self.addParticipantButton.hidden = NO;
  659. } else {
  660. self.hangupButton.hidden = YES;
  661. self.audioButton.hidden = YES;
  662. self.videoButton.hidden = YES;
  663. self.switchCameraButton.hidden = YES;
  664. self.minimizeButton.hidden = YES;
  665. self.addParticipantButton.hidden = YES;
  666. }
  667. } else {
  668. self.smallCollectionView.hidden = YES;
  669. }
  670. }
  671. #pragma mark - WFAVEngineDelegate
  672. - (void)didChangeState:(WFAVEngineState)state {
  673. if (!self.viewLoaded) {
  674. return;
  675. }
  676. switch (state) {
  677. case kWFAVEngineStateIdle:
  678. self.answerButton.hidden = YES;
  679. self.hangupButton.hidden = YES;
  680. self.switchCameraButton.hidden = YES;
  681. self.audioButton.hidden = YES;
  682. self.videoButton.hidden = YES;
  683. self.scalingButton.hidden = YES;
  684. [self stopConnectedTimer];
  685. self.userNameLabel.hidden = YES;
  686. self.portraitView.hidden = YES;
  687. self.stateLabel.text = WFCString(@"CallEnded");
  688. self.smallCollectionView.hidden = YES;
  689. self.portraitCollectionView.hidden = YES;
  690. self.bigVideoView.hidden = YES;
  691. self.minimizeButton.hidden = YES;
  692. self.speakerButton.hidden = YES;
  693. self.addParticipantButton.hidden = YES;
  694. [self updateTopViewFrame];
  695. break;
  696. case kWFAVEngineStateOutgoing:
  697. self.answerButton.hidden = YES;
  698. self.connectTimeLabel.hidden = YES;
  699. self.hangupButton.hidden = NO;
  700. self.hangupButton.frame = [self getButtomCenterButtonFrame];
  701. self.audioButton.frame = [self getButtomLeftButtonFrame];
  702. self.audioButton.hidden = NO;
  703. if (self.currentSession.isAudioOnly) {
  704. self.speakerButton.hidden = NO;
  705. self.switchCameraButton.hidden = YES;
  706. [self updateSpeakerButton];
  707. self.speakerButton.frame = [self getButtomRightButtonFrame];
  708. } else {
  709. self.speakerButton.hidden = YES;
  710. self.switchCameraButton.hidden = NO;
  711. self.switchCameraButton.frame = [self getButtomRightButtonFrame];
  712. }
  713. self.videoButton.hidden = YES;
  714. self.scalingButton.hidden = YES;
  715. [self.currentSession setupLocalVideoView:self.bigVideoView scalingType:self.bigScalingType];
  716. self.stateLabel.text = WFCString(@"WaitingAccept");
  717. self.smallCollectionView.hidden = YES;
  718. self.portraitCollectionView.hidden = NO;
  719. [self.portraitCollectionView reloadData];
  720. self.userNameLabel.hidden = YES;
  721. self.portraitView.hidden = YES;
  722. self.minimizeButton.hidden = NO;
  723. [self updateTopViewFrame];
  724. break;
  725. case kWFAVEngineStateConnecting:
  726. self.answerButton.hidden = YES;
  727. self.hangupButton.hidden = NO;
  728. self.hangupButton.frame = [self getButtomCenterButtonFrame];
  729. self.videoButton.hidden = YES;
  730. self.scalingButton.hidden = YES;
  731. self.audioButton.frame = [self getButtomLeftButtonFrame];
  732. self.audioButton.hidden = NO;
  733. self.audioButton.enabled = NO;
  734. if (self.currentSession.isAudioOnly) {
  735. self.speakerButton.hidden = NO;
  736. self.speakerButton.enabled = NO;
  737. self.switchCameraButton.hidden = YES;
  738. self.speakerButton.frame = [self getButtomRightButtonFrame];
  739. } else {
  740. self.speakerButton.hidden = YES;
  741. self.switchCameraButton.hidden = NO;
  742. self.switchCameraButton.enabled = NO;
  743. self.switchCameraButton.frame = [self getButtomRightButtonFrame];
  744. }
  745. [self.currentSession setupLocalVideoView:self.bigVideoView scalingType:self.bigScalingType];
  746. if (self.currentSession.audioOnly) {
  747. self.smallCollectionView.hidden = YES;
  748. self.portraitCollectionView.hidden = NO;
  749. [self.portraitCollectionView reloadData];
  750. self.portraitCollectionView.center = self.view.center;
  751. } else {
  752. self.smallCollectionView.hidden = NO;
  753. [self.smallCollectionView reloadData];
  754. self.portraitCollectionView.hidden = YES;
  755. }
  756. self.stateLabel.text = WFCString(@"CallConnecting");
  757. self.portraitView.hidden = YES;
  758. self.userNameLabel.hidden = YES;
  759. break;
  760. case kWFAVEngineStateConnected:
  761. self.answerButton.hidden = YES;
  762. self.hangupButton.hidden = NO;
  763. self.connectTimeLabel.hidden = NO;
  764. self.stateLabel.hidden = YES;
  765. self.hangupButton.frame = [self getButtomCenterButtonFrame];
  766. self.audioButton.hidden = NO;
  767. self.audioButton.enabled = YES;
  768. self.audioButton.frame = [self getButtomLeftButtonFrame];
  769. if([self.currentSession isHeadsetPluggedIn] || [self.currentSession isBluetoothSpeaker]) {
  770. [self.currentSession enableSpeaker:NO];
  771. } else {
  772. [self.currentSession enableSpeaker:YES];
  773. }
  774. if (self.currentSession.isAudioOnly) {
  775. self.speakerButton.hidden = NO;
  776. self.speakerButton.enabled = YES;
  777. self.speakerButton.frame = [self getButtomRightButtonFrame];
  778. [self updateSpeakerButton];
  779. self.switchCameraButton.hidden = YES;
  780. self.videoButton.hidden = YES;
  781. } else {
  782. self.speakerButton.hidden = YES;
  783. self.switchCameraButton.hidden = NO;
  784. self.switchCameraButton.enabled = YES;
  785. self.switchCameraButton.frame = [self getButtomRightButtonFrame];
  786. self.videoButton.hidden = NO;
  787. }
  788. self.scalingButton.hidden = YES;
  789. self.minimizeButton.hidden = NO;
  790. self.addParticipantButton.hidden = NO;
  791. if (self.currentSession.isAudioOnly) {
  792. [self.currentSession setupLocalVideoView:nil scalingType:self.bigScalingType];
  793. self.smallCollectionView.hidden = YES;
  794. self.bigVideoView.hidden = YES;
  795. self.portraitCollectionView.hidden = NO;
  796. [self.portraitCollectionView reloadData];
  797. } else {
  798. NSString *lastUser = [self.participants lastObject];
  799. if ([lastUser isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  800. [self.currentSession setupLocalVideoView:self.bigVideoView scalingType:self.bigScalingType];
  801. } else {
  802. [self.currentSession setupRemoteVideoView:self.bigVideoView scalingType:self.bigScalingType forUser:lastUser screenSharing:NO];
  803. }
  804. self.smallCollectionView.hidden = NO;
  805. [self.smallCollectionView reloadData];
  806. self.bigVideoView.hidden = NO;
  807. self.portraitCollectionView.hidden = YES;
  808. }
  809. // if (!_currentSession.isAudioOnly) {
  810. self.userNameLabel.hidden = YES;
  811. self.portraitView.hidden = YES;
  812. // } else {
  813. // self.userNameLabel.hidden = NO;
  814. // self.portraitView.hidden = NO;
  815. // }
  816. [self updateConnectedTimeLabel];
  817. [self startConnectedTimer];
  818. [self updateTopViewFrame];
  819. break;
  820. case kWFAVEngineStateIncomming:
  821. self.connectTimeLabel.hidden = YES;
  822. self.answerButton.hidden = NO;
  823. self.answerButton.frame = [self getButtomRightButtonFrame];
  824. self.hangupButton.hidden = NO;
  825. self.hangupButton.frame = [self getButtomLeftButtonFrame];
  826. self.switchCameraButton.hidden = YES;
  827. self.audioButton.hidden = YES;
  828. self.videoButton.hidden = YES;
  829. self.scalingButton.hidden = YES;
  830. self.minimizeButton.hidden = NO;
  831. [self.currentSession setupLocalVideoView:self.bigVideoView scalingType:self.bigScalingType];
  832. self.stateLabel.text = WFCString(@"InvitingYou");
  833. self.smallCollectionView.hidden = YES;
  834. self.portraitCollectionView.hidden = NO;
  835. [self.portraitCollectionView reloadData];
  836. break;
  837. default:
  838. break;
  839. }
  840. if(state == kWFAVEngineStateConnected && [self.currentSession.initiator isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  841. [self startBroadcastCallOngoing:YES];
  842. } else {
  843. [self startBroadcastCallOngoing:NO];
  844. }
  845. }
  846. - (void)didCreateLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
  847. }
  848. - (void)didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack fromUser:(NSString *)userId screenSharing:(BOOL)screenSharing {
  849. }
  850. - (void)didVideoMuted:(BOOL)videoMuted fromUser:(NSString *)userId {
  851. if ([self.participants.lastObject isEqualToString:userId]) {
  852. for (int i = 0; i < self.participants.count-1; i++) {
  853. NSString *pid = [self.participants objectAtIndex:i];
  854. if ([pid isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  855. if (!self.currentSession.myProfile.videoMuted) {
  856. [self switchVideoView:i];
  857. return;
  858. }
  859. continue;
  860. }
  861. for (WFAVParticipantProfile *p in self.currentSession.participants) {
  862. if ([p.userId isEqualToString:pid]) {
  863. if (!p.videoMuted && p.state == kWFAVEngineStateConnected) {
  864. [self switchVideoView:i];
  865. return;
  866. }
  867. break;
  868. }
  869. }
  870. }
  871. [self reloadVideoUI];
  872. } else {
  873. [self reloadVideoUI];
  874. }
  875. }
  876. - (void)didReportAudioVolume:(NSInteger)volume ofUser:(NSString *)userId {
  877. if([WFCUConfigManager globalManager].displaySpeakingInMultiCall) {
  878. [[NSNotificationCenter defaultCenter] postNotificationName:@"wfavVolumeUpdated" object:userId userInfo:@{@"volume":@(volume)}];
  879. if (!self.currentSession.audioOnly && [userId isEqualToString:self.participants.lastObject]) {
  880. if (volume > 1000) {
  881. self.bigVideoView.layer.borderColor = [UIColor greenColor].CGColor;
  882. } else {
  883. self.bigVideoView.layer.borderColor = [UIColor clearColor].CGColor;
  884. }
  885. }
  886. }
  887. }
  888. - (void)didCallEndWithReason:(WFAVCallEndReason)reason {
  889. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  890. [[WFAVEngineKit sharedEngineKit] dismissViewController:self];
  891. });
  892. }
  893. - (void)didParticipantJoined:(NSString *)userId screenSharing:(BOOL)screenSharing {
  894. if ([self.participants containsObject:userId] || [userId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  895. return;
  896. }
  897. [self.participants insertObject:userId atIndex:0];
  898. [self reloadVideoUI];
  899. }
  900. - (void)didParticipantConnected:(NSString *)userId screenSharing:(BOOL)screenSharing {
  901. [self reloadVideoUI];
  902. }
  903. - (void)didParticipantLeft:(NSString *)userId screenSharing:(BOOL)screenSharing withReason:(WFAVCallEndReason)reason {
  904. [self.participants removeObject:userId];
  905. [self reloadVideoUI];
  906. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:userId inGroup:self.currentSession.conversation.type == Group_Type ? self.currentSession.conversation.target : nil refresh:NO];
  907. NSString *reasonStr;
  908. if (reason == kWFAVCallEndReasonTimeout) {
  909. reasonStr = @"未接听";
  910. } else if(reason == kWFAVCallEndReasonBusy) {
  911. reasonStr = @"网络忙";
  912. } else if(reason == kWFAVCallEndReasonInterrupted) {
  913. reasonStr = @"通话中断";
  914. } else if(reason == kWFAVCallEndReasonRemoteInterrupted) {
  915. reasonStr = @"对方通话中断";
  916. } else if(reason == kWFAVCallEndReasonRemoteHangup) {
  917. reasonStr = @"离开会议";
  918. } else {
  919. reasonStr = @"离开会议"; //"网络错误";
  920. }
  921. [self.view makeToast:[NSString stringWithFormat:@"%@ %@", userInfo.displayName, reasonStr] duration:1 position:CSToastPositionCenter];
  922. }
  923. - (void)didChangeMode:(BOOL)isAudioOnly {
  924. [self didChangeState:self.currentSession.state];
  925. }
  926. - (void)didError:(NSError *)error {
  927. }
  928. - (void)didGetStats:(NSArray *)stats {
  929. }
  930. - (void)didChangeAudioRoute {
  931. [self updateSpeakerButton];
  932. }
  933. - (void)startBroadcastCallOngoing:(BOOL)start {
  934. if([WFCUConfigManager globalManager].enableMultiCallAutoJoin) {
  935. if(start && !self.broadcastOngoingTimer) {
  936. __weak typeof(self)ws = self;
  937. WFCCMultiCallOngoingMessageContent *ongoing = [[WFCCMultiCallOngoingMessageContent alloc] init];
  938. ongoing.callId = self.currentSession.callId;
  939. ongoing.audioOnly = self.currentSession.audioOnly;
  940. ongoing.initiator = self.currentSession.initiator;
  941. ongoing.targetIds = self.currentSession.participantIds;
  942. if (@available(iOS 10.0, *)) {
  943. self.broadcastOngoingTimer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
  944. typeof(self) strongSelf = ws;
  945. if(strongSelf.currentSession.state == kWFAVEngineStateConnected) {
  946. [[WFCCIMService sharedWFCIMService] send:strongSelf.currentSession.conversation content:ongoing success:nil error:nil];
  947. }
  948. }];
  949. } else {
  950. // Fallback on earlier versions
  951. }
  952. } else if(!start && self.broadcastOngoingTimer) {
  953. [self.broadcastOngoingTimer invalidate];
  954. self.broadcastOngoingTimer = nil;
  955. }
  956. }
  957. }
  958. - (void)reloadVideoUI {
  959. if (!self.currentSession.audioOnly) {
  960. NSString *userId = [self.participants lastObject];
  961. if (self.currentSession.state == kWFAVEngineStateConnecting || self.currentSession.state == kWFAVEngineStateConnected) {
  962. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  963. CGFloat itemWidth = (self.view.frame.size.width + layout.minimumLineSpacing)/3 - layout.minimumLineSpacing;
  964. if (self.participants.count - 1 > 3) {
  965. self.smallCollectionView.frame = CGRectMake(0, [WFCUUtilities wf_navigationFullHeight], self.view.frame.size.width, itemWidth * 2 + layout.minimumLineSpacing);
  966. } else {
  967. self.smallCollectionView.frame = CGRectMake(0, [WFCUUtilities wf_navigationFullHeight], self.view.frame.size.width, itemWidth);
  968. }
  969. if ([userId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  970. if (self.currentSession.myProfile.videoMuted) {
  971. [self.currentSession setupLocalVideoView:nil scalingType:self.bigScalingType];
  972. self.stateLabel.text = WFCString(@"VideoClosed");
  973. self.stateLabel.hidden = NO;
  974. } else {
  975. [self.currentSession setupLocalVideoView:self.bigVideoView scalingType:self.bigScalingType];
  976. self.stateLabel.text = nil;
  977. self.stateLabel.hidden = YES;
  978. }
  979. } else {
  980. for (WFAVParticipantProfile *profile in self.currentSession.participants) {
  981. if ([profile.userId isEqualToString:userId]) {
  982. if (profile.videoMuted) {
  983. [self.currentSession setupRemoteVideoView:nil scalingType:self.bigScalingType forUser:userId screenSharing:profile.screeSharing];
  984. self.stateLabel.text = WFCString(@"VideoClosed");
  985. self.stateLabel.hidden = NO;
  986. } else {
  987. [self.currentSession setupRemoteVideoView:self.bigVideoView scalingType:self.bigScalingType forUser:userId screenSharing:profile.screeSharing];
  988. self.stateLabel.text = nil;
  989. self.stateLabel.hidden = YES;
  990. }
  991. break;
  992. }
  993. }
  994. }
  995. [self.smallCollectionView reloadData];
  996. } else {
  997. [self.portraitCollectionView reloadData];
  998. }
  999. } else {
  1000. [self.portraitCollectionView reloadData];
  1001. }
  1002. }
  1003. - (BOOL)switchVideoView:(NSUInteger)index {
  1004. NSString *userId = self.participants[index];
  1005. BOOL canSwitch = NO;
  1006. for (WFAVParticipantProfile *profile in self.currentSession.participants) {
  1007. if ([profile.userId isEqualToString:userId]) {
  1008. if (profile.state == kWFAVEngineStateConnected) {
  1009. canSwitch = YES;
  1010. }
  1011. break;
  1012. }
  1013. }
  1014. if ([userId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  1015. if (self.currentSession.state == kWFAVEngineStateConnected) {
  1016. canSwitch = YES;
  1017. }
  1018. }
  1019. if (canSwitch) {
  1020. NSString *lastId = [self.participants lastObject];
  1021. [self.participants removeLastObject];
  1022. [self.participants insertObject:lastId atIndex:index];
  1023. [self.participants removeObject:userId];
  1024. [self.participants addObject:userId];
  1025. }
  1026. [self reloadVideoUI];
  1027. return canSwitch;
  1028. }
  1029. - (void)didChangeInitiator:(NSString *_Nullable)initiator {
  1030. [self startBroadcastCallOngoing:[initiator isEqualToString:[WFCCNetworkService sharedInstance].userId]];
  1031. }
  1032. - (void)didMedia:(NSString *_Nullable)media lostPackage:(int)lostPackage screenSharing:(BOOL)screenSharing {
  1033. //发送方丢包超过6为网络不好
  1034. if(lostPackage > 6) {
  1035. [self.view makeToast:@"您的网络不好" duration:3 position:CSToastPositionCenter];
  1036. }
  1037. }
  1038. - (void)didMedia:(NSString *)media lostPackage:(int)lostPackage uplink:(BOOL)uplink ofUser:(NSString *)userId screenSharing:(BOOL)screenSharing {
  1039. //如果uplink ture对方网络不好,false您的网络不好
  1040. //接受方丢包超过10为网络不好
  1041. if(lostPackage > 10) {
  1042. if(uplink) {
  1043. NSLog(@"对方的网络不好");
  1044. [self.view makeToast:@"对方的网络不好" duration:3 position:CSToastPositionCenter];
  1045. } else {
  1046. NSLog(@"您的网络不好");
  1047. [self.view makeToast:@"您的网络不好" duration:3 position:CSToastPositionCenter];
  1048. }
  1049. }
  1050. }
  1051. #pragma mark - UICollectionViewDataSource
  1052. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  1053. if (collectionView == self.portraitCollectionView) {
  1054. if (self.currentSession.audioOnly && (self.currentSession.state == kWFAVEngineStateConnecting || self.currentSession.state == kWFAVEngineStateConnected)) {
  1055. return self.participants.count;
  1056. }
  1057. }
  1058. return self.participants.count - 1;
  1059. }
  1060. // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
  1061. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  1062. NSString *userId = self.participants[indexPath.row];
  1063. if (collectionView == self.smallCollectionView) {
  1064. WFCUParticipantCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  1065. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:userId inGroup:self.currentSession.conversation.type == Group_Type ? self.currentSession.conversation.target : nil refresh:NO];
  1066. UIDevice *device = [UIDevice currentDevice] ;
  1067. if (device.orientation == UIDeviceOrientationLandscapeLeft) {
  1068. cell.transform = CGAffineTransformMakeRotation(M_PI_2);
  1069. } else if (device.orientation == UIDeviceOrientationLandscapeRight) {
  1070. cell.transform = CGAffineTransformMakeRotation(-M_PI_2);
  1071. } else {
  1072. cell.transform = CGAffineTransformMakeRotation(0);
  1073. }
  1074. if ([userId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  1075. WFAVParticipantProfile *profile = self.currentSession.myProfile;
  1076. [cell setUserInfo:userInfo callProfile:profile];
  1077. if (profile.videoMuted) {
  1078. [self.currentSession setupLocalVideoView:nil scalingType:self.smallScalingType];
  1079. } else {
  1080. [self.currentSession setupLocalVideoView:cell scalingType:self.smallScalingType];
  1081. }
  1082. } else {
  1083. for (WFAVParticipantProfile *profile in self.currentSession.participants) {
  1084. if ([profile.userId isEqualToString:userId]) {
  1085. [cell setUserInfo:userInfo callProfile:profile];
  1086. if (profile.videoMuted) {
  1087. [self.currentSession setupRemoteVideoView:nil scalingType:self.smallScalingType forUser:userId screenSharing:profile.screeSharing];
  1088. } else {
  1089. [self.currentSession setupRemoteVideoView:cell scalingType:self.smallScalingType forUser:userId screenSharing:profile.screeSharing];
  1090. }
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. return cell;
  1096. } else {
  1097. WFCUPortraitCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
  1098. CGFloat itemHeight = MIN(PortraitItemSize, (self.view.frame.size.width - 32 - 2*10)/3);
  1099. cell.itemSize = itemHeight;
  1100. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:userId inGroup:self.currentSession.conversation.type == Group_Type ? self.currentSession.conversation.target : nil refresh:NO];
  1101. cell.userInfo = userInfo;
  1102. if ([userId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  1103. cell.profile = self.currentSession.myProfile;
  1104. } else {
  1105. for (WFAVParticipantProfile *profile in self.currentSession.participants) {
  1106. if ([profile.userId isEqualToString:userId]) {
  1107. cell.profile = profile;
  1108. break;
  1109. }
  1110. }
  1111. }
  1112. return cell;
  1113. }
  1114. }
  1115. #pragma mark - UICollectionViewDelegate
  1116. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  1117. if (collectionView == self.smallCollectionView) {
  1118. [self switchVideoView:indexPath.row];
  1119. }
  1120. }
  1121. - (void)dealloc {
  1122. [[NSNotificationCenter defaultCenter] removeObserver:self];
  1123. }
  1124. #endif
  1125. @end