WFCUMultiVideoViewController.m 50 KB

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