tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2015 Google Inc. |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #import "ARDVideoCallViewController.h" |
| 29 | |
| 30 | #import "ARDAppClient.h" |
| 31 | #import "ARDVideoCallView.h" |
| 32 | |
| 33 | @interface ARDVideoCallViewController () <ARDAppClientDelegate, |
| 34 | ARDVideoCallViewDelegate> |
| 35 | @property(nonatomic, readonly) ARDVideoCallView *videoCallView; |
| 36 | @end |
| 37 | |
| 38 | @implementation ARDVideoCallViewController { |
| 39 | ARDAppClient *_client; |
| 40 | RTCVideoTrack *_remoteVideoTrack; |
| 41 | RTCVideoTrack *_localVideoTrack; |
| 42 | } |
| 43 | |
| 44 | @synthesize videoCallView = _videoCallView; |
| 45 | |
| 46 | - (instancetype)initForRoom:(NSString *)room { |
| 47 | if (self = [super init]) { |
| 48 | _client = [[ARDAppClient alloc] initWithDelegate:self]; |
| 49 | [_client connectToRoomWithId:room options:nil]; |
| 50 | } |
| 51 | return self; |
| 52 | } |
| 53 | |
| 54 | - (void)loadView { |
| 55 | _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero]; |
| 56 | _videoCallView.delegate = self; |
| 57 | _videoCallView.statusLabel.text = |
| 58 | [self statusTextForState:RTCICEConnectionNew]; |
| 59 | self.view = _videoCallView; |
| 60 | } |
| 61 | |
| 62 | #pragma mark - ARDAppClientDelegate |
| 63 | |
| 64 | - (void)appClient:(ARDAppClient *)client |
| 65 | didChangeState:(ARDAppClientState)state { |
| 66 | switch (state) { |
| 67 | case kARDAppClientStateConnected: |
| 68 | NSLog(@"Client connected."); |
| 69 | break; |
| 70 | case kARDAppClientStateConnecting: |
| 71 | NSLog(@"Client connecting."); |
| 72 | break; |
| 73 | case kARDAppClientStateDisconnected: |
| 74 | NSLog(@"Client disconnected."); |
| 75 | [self hangup]; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | - (void)appClient:(ARDAppClient *)client |
| 81 | didChangeConnectionState:(RTCICEConnectionState)state { |
| 82 | NSLog(@"ICE state changed: %d", state); |
| 83 | __weak ARDVideoCallViewController *weakSelf = self; |
| 84 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 85 | ARDVideoCallViewController *strongSelf = weakSelf; |
| 86 | strongSelf.videoCallView.statusLabel.text = |
| 87 | [strongSelf statusTextForState:state]; |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | - (void)appClient:(ARDAppClient *)client |
| 92 | didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
| 93 | if (!_localVideoTrack) { |
| 94 | _localVideoTrack = localVideoTrack; |
| 95 | [_localVideoTrack addRenderer:_videoCallView.localVideoView]; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | - (void)appClient:(ARDAppClient *)client |
| 100 | didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
| 101 | if (!_remoteVideoTrack) { |
| 102 | _remoteVideoTrack = remoteVideoTrack; |
| 103 | [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView]; |
| 104 | _videoCallView.statusLabel.hidden = YES; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | - (void)appClient:(ARDAppClient *)client |
| 109 | didError:(NSError *)error { |
| 110 | NSString *message = |
| 111 | [NSString stringWithFormat:@"%@", error.localizedDescription]; |
| 112 | [self showAlertWithMessage:message]; |
| 113 | [self hangup]; |
| 114 | } |
| 115 | |
| 116 | #pragma mark - ARDVideoCallViewDelegate |
| 117 | |
| 118 | - (void)videoCallViewDidHangup:(ARDVideoCallView *)view { |
| 119 | [self hangup]; |
| 120 | } |
| 121 | |
| 122 | #pragma mark - Private |
| 123 | |
| 124 | - (void)hangup { |
| 125 | if (_remoteVideoTrack) { |
| 126 | [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView]; |
| 127 | _remoteVideoTrack = nil; |
| 128 | [_videoCallView.remoteVideoView renderFrame:nil]; |
| 129 | } |
| 130 | if (_localVideoTrack) { |
| 131 | [_localVideoTrack removeRenderer:_videoCallView.localVideoView]; |
| 132 | _localVideoTrack = nil; |
| 133 | [_videoCallView.localVideoView renderFrame:nil]; |
| 134 | } |
| 135 | [_client disconnect]; |
| 136 | [self.presentingViewController dismissViewControllerAnimated:YES |
| 137 | completion:nil]; |
| 138 | } |
| 139 | |
| 140 | - (NSString *)statusTextForState:(RTCICEConnectionState)state { |
| 141 | switch (state) { |
| 142 | case RTCICEConnectionNew: |
| 143 | case RTCICEConnectionChecking: |
| 144 | return @"Connecting..."; |
| 145 | case RTCICEConnectionConnected: |
| 146 | case RTCICEConnectionCompleted: |
| 147 | case RTCICEConnectionFailed: |
| 148 | case RTCICEConnectionDisconnected: |
| 149 | case RTCICEConnectionClosed: |
| 150 | return nil; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | - (void)showAlertWithMessage:(NSString*)message { |
| 155 | UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil |
| 156 | message:message |
| 157 | delegate:nil |
| 158 | cancelButtonTitle:@"OK" |
| 159 | otherButtonTitles:nil]; |
| 160 | [alertView show]; |
| 161 | } |
| 162 | |
| 163 | @end |