OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 #import "ARDCEODTURNClient.h" | 26 #import "ARDCEODTURNClient.h" |
27 #import "ARDJoinResponse.h" | 27 #import "ARDJoinResponse.h" |
28 #import "ARDMessageResponse.h" | 28 #import "ARDMessageResponse.h" |
29 #import "ARDSDPUtils.h" | 29 #import "ARDSDPUtils.h" |
30 #import "ARDSignalingMessage.h" | 30 #import "ARDSignalingMessage.h" |
31 #import "ARDUtilities.h" | 31 #import "ARDUtilities.h" |
32 #import "ARDWebSocketChannel.h" | 32 #import "ARDWebSocketChannel.h" |
33 #import "RTCICECandidate+JSON.h" | 33 #import "RTCICECandidate+JSON.h" |
34 #import "RTCSessionDescription+JSON.h" | 34 #import "RTCSessionDescription+JSON.h" |
35 | 35 |
36 | |
37 static NSString * const kARDDefaultSTUNServerUrl = | 36 static NSString * const kARDDefaultSTUNServerUrl = |
38 @"stun:stun.l.google.com:19302"; | 37 @"stun:stun.l.google.com:19302"; |
39 // TODO(tkchin): figure out a better username for CEOD statistics. | 38 // TODO(tkchin): figure out a better username for CEOD statistics. |
40 static NSString * const kARDTurnRequestUrl = | 39 static NSString * const kARDTurnRequestUrl = |
41 @"https://computeengineondemand.appspot.com" | 40 @"https://computeengineondemand.appspot.com" |
42 @"/turn?username=iapprtc&key=4080218913"; | 41 @"/turn?username=iapprtc&key=4080218913"; |
43 | 42 |
44 static NSString * const kARDAppClientErrorDomain = @"ARDAppClient"; | 43 static NSString * const kARDAppClientErrorDomain = @"ARDAppClient"; |
45 static NSInteger const kARDAppClientErrorUnknown = -1; | 44 static NSInteger const kARDAppClientErrorUnknown = -1; |
46 static NSInteger const kARDAppClientErrorRoomFull = -2; | 45 static NSInteger const kARDAppClientErrorRoomFull = -2; |
47 static NSInteger const kARDAppClientErrorCreateSDP = -3; | 46 static NSInteger const kARDAppClientErrorCreateSDP = -3; |
48 static NSInteger const kARDAppClientErrorSetSDP = -4; | 47 static NSInteger const kARDAppClientErrorSetSDP = -4; |
49 static NSInteger const kARDAppClientErrorInvalidClient = -5; | 48 static NSInteger const kARDAppClientErrorInvalidClient = -5; |
50 static NSInteger const kARDAppClientErrorInvalidRoom = -6; | 49 static NSInteger const kARDAppClientErrorInvalidRoom = -6; |
51 | 50 |
| 51 // We need a proxy to NSTimer because it causes a strong retain cycle. When |
| 52 // using the proxy, |invalidate| must be called before it properly deallocs. |
| 53 @interface ARDTimerProxy : NSObject |
| 54 |
| 55 - (instancetype)initWithInterval:(NSTimeInterval)interval |
| 56 repeats:(BOOL)repeats |
| 57 timerHandler:(void (^)(void))timerHandler; |
| 58 - (void)invalidate; |
| 59 |
| 60 @end |
| 61 |
| 62 @implementation ARDTimerProxy { |
| 63 NSTimer *_timer; |
| 64 void (^_timerHandler)(void); |
| 65 } |
| 66 |
| 67 - (instancetype)initWithInterval:(NSTimeInterval)interval |
| 68 repeats:(BOOL)repeats |
| 69 timerHandler:(void (^)(void))timerHandler { |
| 70 NSParameterAssert(timerHandler); |
| 71 if (self = [super init]) { |
| 72 _timerHandler = timerHandler; |
| 73 _timer = [NSTimer scheduledTimerWithTimeInterval:interval |
| 74 target:self |
| 75 selector:@selector(timerDidFire:) |
| 76 userInfo:nil |
| 77 repeats:repeats]; |
| 78 } |
| 79 return self; |
| 80 } |
| 81 |
| 82 - (void)invalidate { |
| 83 [_timer invalidate]; |
| 84 } |
| 85 |
| 86 - (void)timerDidFire:(NSTimer *)timer { |
| 87 _timerHandler(); |
| 88 } |
| 89 |
| 90 @end |
| 91 |
52 @implementation ARDAppClient { | 92 @implementation ARDAppClient { |
53 RTCFileLogger *_fileLogger; | 93 RTCFileLogger *_fileLogger; |
| 94 ARDTimerProxy *_statsTimer; |
54 } | 95 } |
55 | 96 |
| 97 @synthesize shouldGetStats = _shouldGetStats; |
| 98 @synthesize state = _state; |
56 @synthesize delegate = _delegate; | 99 @synthesize delegate = _delegate; |
57 @synthesize state = _state; | |
58 @synthesize roomServerClient = _roomServerClient; | 100 @synthesize roomServerClient = _roomServerClient; |
59 @synthesize channel = _channel; | 101 @synthesize channel = _channel; |
60 @synthesize turnClient = _turnClient; | 102 @synthesize turnClient = _turnClient; |
61 @synthesize peerConnection = _peerConnection; | 103 @synthesize peerConnection = _peerConnection; |
62 @synthesize factory = _factory; | 104 @synthesize factory = _factory; |
63 @synthesize messageQueue = _messageQueue; | 105 @synthesize messageQueue = _messageQueue; |
64 @synthesize isTurnComplete = _isTurnComplete; | 106 @synthesize isTurnComplete = _isTurnComplete; |
65 @synthesize hasReceivedSdp = _hasReceivedSdp; | 107 @synthesize hasReceivedSdp = _hasReceivedSdp; |
66 @synthesize roomId = _roomId; | 108 @synthesize roomId = _roomId; |
67 @synthesize clientId = _clientId; | 109 @synthesize clientId = _clientId; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 157 |
116 - (void)configure { | 158 - (void)configure { |
117 _factory = [[RTCPeerConnectionFactory alloc] init]; | 159 _factory = [[RTCPeerConnectionFactory alloc] init]; |
118 _messageQueue = [NSMutableArray array]; | 160 _messageQueue = [NSMutableArray array]; |
119 _iceServers = [NSMutableArray arrayWithObject:[self defaultSTUNServer]]; | 161 _iceServers = [NSMutableArray arrayWithObject:[self defaultSTUNServer]]; |
120 _fileLogger = [[RTCFileLogger alloc] init]; | 162 _fileLogger = [[RTCFileLogger alloc] init]; |
121 [_fileLogger start]; | 163 [_fileLogger start]; |
122 } | 164 } |
123 | 165 |
124 - (void)dealloc { | 166 - (void)dealloc { |
| 167 self.shouldGetStats = NO; |
125 [self disconnect]; | 168 [self disconnect]; |
126 } | 169 } |
127 | 170 |
| 171 - (void)setShouldGetStats:(BOOL)shouldGetStats { |
| 172 if (_shouldGetStats == shouldGetStats) { |
| 173 return; |
| 174 } |
| 175 if (shouldGetStats) { |
| 176 __weak ARDAppClient *weakSelf = self; |
| 177 _statsTimer = [[ARDTimerProxy alloc] initWithInterval:1 |
| 178 repeats:YES |
| 179 timerHandler:^{ |
| 180 ARDAppClient *strongSelf = weakSelf; |
| 181 [strongSelf.peerConnection getStatsWithDelegate:strongSelf |
| 182 mediaStreamTrack:nil |
| 183 statsOutputLevel:RTCStatsOutputLevelDebug]; |
| 184 }]; |
| 185 } else { |
| 186 [_statsTimer invalidate]; |
| 187 _statsTimer = nil; |
| 188 } |
| 189 _shouldGetStats = shouldGetStats; |
| 190 } |
| 191 |
128 - (void)setState:(ARDAppClientState)state { | 192 - (void)setState:(ARDAppClientState)state { |
129 if (_state == state) { | 193 if (_state == state) { |
130 return; | 194 return; |
131 } | 195 } |
132 _state = state; | 196 _state = state; |
133 [_delegate appClient:self didChangeState:_state]; | 197 [_delegate appClient:self didChangeState:_state]; |
134 } | 198 } |
135 | 199 |
136 - (void)connectToRoomWithId:(NSString *)roomId | 200 - (void)connectToRoomWithId:(NSString *)roomId |
137 options:(NSDictionary *)options { | 201 options:(NSDictionary *)options { |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 ARDICECandidateMessage *message = | 370 ARDICECandidateMessage *message = |
307 [[ARDICECandidateMessage alloc] initWithCandidate:candidate]; | 371 [[ARDICECandidateMessage alloc] initWithCandidate:candidate]; |
308 [self sendSignalingMessage:message]; | 372 [self sendSignalingMessage:message]; |
309 }); | 373 }); |
310 } | 374 } |
311 | 375 |
312 - (void)peerConnection:(RTCPeerConnection*)peerConnection | 376 - (void)peerConnection:(RTCPeerConnection*)peerConnection |
313 didOpenDataChannel:(RTCDataChannel*)dataChannel { | 377 didOpenDataChannel:(RTCDataChannel*)dataChannel { |
314 } | 378 } |
315 | 379 |
| 380 #pragma mark - RTCStatsDelegate |
| 381 |
| 382 - (void)peerConnection:(RTCPeerConnection*)peerConnection |
| 383 didGetStats:(NSArray*)stats { |
| 384 dispatch_async(dispatch_get_main_queue(), ^{ |
| 385 [_delegate appClient:self didGetStats:stats]; |
| 386 }); |
| 387 } |
| 388 |
316 #pragma mark - RTCSessionDescriptionDelegate | 389 #pragma mark - RTCSessionDescriptionDelegate |
317 // Callbacks for this delegate occur on non-main thread and need to be | 390 // Callbacks for this delegate occur on non-main thread and need to be |
318 // dispatched back to main queue as needed. | 391 // dispatched back to main queue as needed. |
319 | 392 |
320 - (void)peerConnection:(RTCPeerConnection *)peerConnection | 393 - (void)peerConnection:(RTCPeerConnection *)peerConnection |
321 didCreateSessionDescription:(RTCSessionDescription *)sdp | 394 didCreateSessionDescription:(RTCSessionDescription *)sdp |
322 error:(NSError *)error { | 395 error:(NSError *)error { |
323 dispatch_async(dispatch_get_main_queue(), ^{ | 396 dispatch_async(dispatch_get_main_queue(), ^{ |
324 if (error) { | 397 if (error) { |
325 RTCLogError(@"Failed to create session description. Error: %@", error); | 398 RTCLogError(@"Failed to create session description. Error: %@", error); |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 code:kARDAppClientErrorInvalidRoom | 705 code:kARDAppClientErrorInvalidRoom |
633 userInfo:@{ | 706 userInfo:@{ |
634 NSLocalizedDescriptionKey: @"Invalid room.", | 707 NSLocalizedDescriptionKey: @"Invalid room.", |
635 }]; | 708 }]; |
636 break; | 709 break; |
637 } | 710 } |
638 return error; | 711 return error; |
639 } | 712 } |
640 | 713 |
641 @end | 714 @end |
OLD | NEW |