WFCUConferenceViewController.m 51 KB

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