2017-08-07 22:11:39 +03:00
|
|
|
//
|
|
|
|
// CallViewController.m
|
|
|
|
// VideoCalls
|
|
|
|
//
|
|
|
|
// Created by Ivan Sein on 31.07.17.
|
|
|
|
// Copyright © 2017 struktur AG. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "CallViewController.h"
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
#import <WebRTC/RTCCameraVideoCapturer.h>
|
|
|
|
#import <WebRTC/RTCMediaStream.h>
|
|
|
|
#import <WebRTC/RTCEAGLVideoView.h>
|
|
|
|
#import <WebRTC/RTCVideoTrack.h>
|
2017-08-07 22:11:39 +03:00
|
|
|
#import "ARDSettingsModel.h"
|
|
|
|
#import "ARDCaptureController.h"
|
2017-10-12 15:37:36 +03:00
|
|
|
#import "CallParticipantViewCell.h"
|
|
|
|
#import "NBMPeersFlowLayout.h"
|
|
|
|
#import "NCCallController.h"
|
|
|
|
|
|
|
|
typedef NS_ENUM(NSInteger, CallState) {
|
|
|
|
CallStateJoining,
|
|
|
|
CallStateWaitingParticipants,
|
|
|
|
CallStateInCall
|
|
|
|
};
|
|
|
|
|
|
|
|
@interface CallViewController () <NCCallControllerDelegate, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, RTCEAGLVideoViewDelegate>
|
|
|
|
{
|
|
|
|
CallState _callState;
|
|
|
|
NSMutableArray *_peersInCall;
|
|
|
|
NSMutableDictionary *_renderersDict;
|
|
|
|
NCCallController *_callController;
|
2017-08-07 22:11:39 +03:00
|
|
|
ARDCaptureController *_captureController;
|
|
|
|
}
|
2017-10-12 15:37:36 +03:00
|
|
|
|
|
|
|
@property (nonatomic, strong) IBOutlet UICollectionView *collectionView;
|
|
|
|
@property (nonatomic, strong) IBOutlet UICollectionViewFlowLayout *flowLayout;
|
|
|
|
|
2017-08-07 22:11:39 +03:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CallViewController
|
|
|
|
|
2017-08-28 12:55:06 +03:00
|
|
|
@synthesize delegate = _delegate;
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (instancetype)initCallInRoom:(NSString *)room asUser:(NSString*)displayName
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
_callController = [[NCCallController alloc] initWithDelegate:self];
|
|
|
|
_callController.room = room;
|
|
|
|
_callController.userDisplayName = displayName;
|
|
|
|
_peersInCall = [[NSMutableArray alloc] init];
|
|
|
|
_renderersDict = [[NSMutableDictionary alloc] init];
|
2017-08-07 22:11:39 +03:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)viewDidLoad {
|
2017-08-07 22:11:39 +03:00
|
|
|
[super viewDidLoad];
|
2017-10-12 15:37:36 +03:00
|
|
|
[self setCallState:CallStateJoining];
|
|
|
|
[_callController startCall];
|
2017-09-19 12:38:41 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
self.collectionView.delegate = self;
|
|
|
|
[self.collectionView registerNib:[UINib nibWithNibName:kCallParticipantCellNibName bundle:nil] forCellWithReuseIdentifier:kCallParticipantCellIdentifier];
|
2017-08-08 14:53:32 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
if (@available(iOS 11.0, *)) {
|
|
|
|
[self.collectionView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
|
|
|
|
}
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)didReceiveMemoryWarning {
|
2017-08-07 22:11:39 +03:00
|
|
|
[super didReceiveMemoryWarning];
|
|
|
|
// Dispose of any resources that can be recreated.
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
#pragma mark - Call State
|
2017-08-07 22:11:39 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)setCallState:(CallState)state
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
switch (state) {
|
|
|
|
case CallStateJoining:
|
|
|
|
break;
|
2017-09-22 14:52:13 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
case CallStateWaitingParticipants:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CallStateInCall:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2017-08-08 14:56:06 +03:00
|
|
|
}
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
|
|
|
|
2017-08-28 12:55:06 +03:00
|
|
|
#pragma mark - Call actions
|
|
|
|
|
2017-09-19 12:38:41 +03:00
|
|
|
- (IBAction)audioButtonPressed:(id)sender
|
|
|
|
{
|
|
|
|
UIButton *audioButton = sender;
|
2017-10-12 15:37:36 +03:00
|
|
|
if ([_callController isAudioEnabled]) {
|
|
|
|
[_callController enableAudio:NO];
|
2017-09-19 12:38:41 +03:00
|
|
|
[audioButton setImage:[UIImage imageNamed:@"audio-off"] forState:UIControlStateNormal];
|
2017-10-12 15:37:36 +03:00
|
|
|
} else {
|
|
|
|
[_callController enableAudio:YES];
|
|
|
|
[audioButton setImage:[UIImage imageNamed:@"audio"] forState:UIControlStateNormal];
|
2017-09-19 12:38:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)videoButtonPressed:(id)sender
|
|
|
|
{
|
|
|
|
UIButton *videoButton = sender;
|
2017-10-12 15:37:36 +03:00
|
|
|
if ([_callController isVideoEnabled]) {
|
|
|
|
[_callController enableVideo:NO];
|
2017-10-16 14:41:21 +03:00
|
|
|
[_captureController stopCapture];
|
2017-09-19 12:38:41 +03:00
|
|
|
[videoButton setImage:[UIImage imageNamed:@"video-off"] forState:UIControlStateNormal];
|
2017-10-12 15:37:36 +03:00
|
|
|
} else {
|
|
|
|
[_callController enableVideo:YES];
|
2017-10-16 14:41:21 +03:00
|
|
|
[_captureController startCapture];
|
2017-10-12 15:37:36 +03:00
|
|
|
[videoButton setImage:[UIImage imageNamed:@"video"] forState:UIControlStateNormal];
|
2017-09-19 12:38:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 15:15:45 +03:00
|
|
|
- (IBAction)hangupButtonPressed:(id)sender {
|
|
|
|
[self hangup];
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)hangup
|
|
|
|
{
|
|
|
|
[_localVideoView.captureSession stopRunning];
|
|
|
|
_localVideoView.captureSession = nil;
|
2017-08-28 12:55:06 +03:00
|
|
|
[_captureController stopCapture];
|
|
|
|
_captureController = nil;
|
2017-08-28 13:09:12 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
for (NCPeerConnection *peerConnection in _peersInCall) {
|
|
|
|
RTCEAGLVideoView *renderer = [_renderersDict objectForKey:peerConnection.peerId];
|
|
|
|
[[peerConnection.remoteStream.videoTracks firstObject] removeRenderer:renderer];
|
|
|
|
[_renderersDict removeObjectForKey:peerConnection.peerId];
|
|
|
|
}
|
2017-08-28 13:09:12 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
[_callController leaveCall];
|
2017-08-28 12:55:06 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)finishCall
|
2017-09-19 12:38:41 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
_callController = nil;
|
|
|
|
[self.delegate viewControllerDidFinish:self];
|
2017-09-19 12:38:41 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)dealloc
|
2017-09-19 12:38:41 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
NSLog(@"CallViewController dealloc");
|
2017-09-19 12:38:41 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
#pragma mark - UICollectionView Datasource
|
2017-08-07 22:11:39 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
return [_peersInCall count];
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
CallParticipantViewCell *cell = (CallParticipantViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCallParticipantCellIdentifier forIndexPath:indexPath];
|
|
|
|
NCPeerConnection *peerConnection = [_peersInCall objectAtIndex:indexPath.row];
|
2017-08-24 20:36:26 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
[cell setVideoView:[_renderersDict objectForKey:peerConnection.peerId]];
|
2017-10-15 13:28:33 +03:00
|
|
|
[cell setDisplayName:peerConnection.peerName];
|
|
|
|
[cell setAudioDisabled:peerConnection.isRemoteAudioDisabled];
|
2017-08-24 20:36:26 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
return cell;
|
2017-08-24 20:36:26 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
CGRect frame = [NBMPeersFlowLayout frameForWithNumberOfItems:_peersInCall.count
|
|
|
|
row:indexPath.row
|
|
|
|
contentSize:self.collectionView.frame.size];
|
|
|
|
return frame.size;
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
[self.collectionView reloadData];
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
#pragma mark - Call Controller delegate
|
|
|
|
|
|
|
|
- (void)callControllerDidJoinCall:(NCCallController *)callController
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
[self setCallState:CallStateWaitingParticipants];
|
2017-08-07 22:11:39 +03:00
|
|
|
|
|
|
|
}
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)callControllerDidEndCall:(NCCallController *)callController
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
[self finishCall];
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)callController:(NCCallController *)callController peerJoined:(NCPeerConnection *)peer
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
// Start adding cell for that peer and wait until add
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:47:16 +03:00
|
|
|
- (void)callController:(NCCallController *)callController peerLeft:(NCPeerConnection *)peer
|
|
|
|
{
|
|
|
|
RTCEAGLVideoView *renderer = [_renderersDict objectForKey:peer.peerId];
|
|
|
|
[[peer.remoteStream.videoTracks firstObject] removeRenderer:renderer];
|
|
|
|
[_renderersDict removeObjectForKey:peer.peerId];
|
|
|
|
[_peersInCall removeObject:peer];
|
|
|
|
[self.collectionView reloadData];
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)callController:(NCCallController *)callController didCreateLocalVideoCapturer:(RTCCameraVideoCapturer *)videoCapturer
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
_localVideoView.captureSession = videoCapturer.captureSession;
|
2017-08-07 22:11:39 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
|
|
|
|
_captureController = [[ARDCaptureController alloc] initWithCapturer:videoCapturer settings:settingsModel];
|
|
|
|
[_captureController startCapture];
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)callController:(NCCallController *)callController didAddLocalStream:(RTCMediaStream *)localStream
|
2017-08-24 16:38:10 +03:00
|
|
|
{
|
|
|
|
}
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)callController:(NCCallController *)callController didRemoveLocalStream:(RTCMediaStream *)localStream
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
|
|
|
}
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)callController:(NCCallController *)callController didAddStream:(RTCMediaStream *)remoteStream ofPeer:(NCPeerConnection *)remotePeer
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
RTCEAGLVideoView *renderView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero];
|
|
|
|
renderView.delegate = self;
|
|
|
|
RTCVideoTrack *remoteVideoTrack = [remotePeer.remoteStream.videoTracks firstObject];
|
|
|
|
[remoteVideoTrack addRenderer:renderView];
|
|
|
|
[_renderersDict setObject:renderView forKey:remotePeer.peerId];
|
|
|
|
[_peersInCall addObject:remotePeer];
|
|
|
|
[self.collectionView reloadData];
|
|
|
|
}
|
|
|
|
- (void)callController:(NCCallController *)callController didRemoveStream:(RTCMediaStream *)remoteStream ofPeer:(NCPeerConnection *)remotePeer
|
|
|
|
{
|
2017-10-15 22:47:16 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
}
|
|
|
|
- (void)callController:(NCCallController *)callController iceStatusChanged:(RTCIceConnectionState)state ofPeer:(NCPeerConnection *)peer
|
|
|
|
{
|
|
|
|
if (state == RTCIceConnectionStateDisconnected) {
|
|
|
|
[_peersInCall removeObject:peer];
|
|
|
|
[self.collectionView reloadData];
|
2017-08-24 15:00:50 +03:00
|
|
|
}
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)callController:(NCCallController *)callController didAddDataChannel:(RTCDataChannel *)dataChannel
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-15 13:28:33 +03:00
|
|
|
- (void)callController:(NCCallController *)callController didReceiveDataChannelMessage:(NSString *)message fromPeer:(NCPeerConnection *)peer
|
|
|
|
{
|
|
|
|
if ([message isEqualToString:@"audioOn"] || [message isEqualToString:@"audioOff"]) {
|
|
|
|
[self updatePeer:peer block:^(CallParticipantViewCell *cell) {
|
|
|
|
[cell setAudioDisabled:peer.isRemoteAudioDisabled];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
- (void)callController:(NCCallController *)callController didReceiveNick:(NSString *)nick fromPeer:(NCPeerConnection *)peer
|
|
|
|
{
|
|
|
|
[self updatePeer:peer block:^(CallParticipantViewCell *cell) {
|
|
|
|
[cell setDisplayName:nick];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
#pragma mark - RTCEAGLVideoViewDelegate
|
2017-08-07 22:11:39 +03:00
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
- (void)videoView:(RTCEAGLVideoView*)videoView didChangeVideoSize:(CGSize)size
|
2017-08-07 22:11:39 +03:00
|
|
|
{
|
2017-10-12 15:37:36 +03:00
|
|
|
for (RTCEAGLVideoView *rendererView in [_renderersDict allValues]) {
|
|
|
|
if ([videoView isEqual:rendererView]) {
|
|
|
|
rendererView.frame = CGRectMake(0, 0, size.width, size.height);
|
|
|
|
}
|
2017-08-24 15:00:50 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 15:37:36 +03:00
|
|
|
[self.collectionView reloadData];
|
2017-08-07 22:11:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-15 13:28:33 +03:00
|
|
|
#pragma mark - Cell updates
|
|
|
|
|
|
|
|
- (NSIndexPath *)indexPathOfPeer:(NCPeerConnection *)peer {
|
|
|
|
NSUInteger idx = [_peersInCall indexOfObject:peer];
|
|
|
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
|
|
|
|
|
|
|
|
return indexPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updatePeer:(NCPeerConnection *)peer block:(void(^)(CallParticipantViewCell* cell))block {
|
|
|
|
NSIndexPath *indexPath = [self indexPathOfPeer:peer];
|
|
|
|
CallParticipantViewCell *cell = (id)[self.collectionView cellForItemAtIndexPath:indexPath];
|
|
|
|
block(cell);
|
|
|
|
}
|
|
|
|
|
2017-08-07 22:11:39 +03:00
|
|
|
@end
|