3 * Copyright 2013, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #import <AVFoundation/AVFoundation.h>
30 #import "APPRTCAppDelegate.h"
32 #import "APPRTCViewController.h"
33 #import "RTCICECandidate.h"
34 #import "RTCICEServer.h"
35 #import "RTCMediaConstraints.h"
36 #import "RTCMediaStream.h"
38 #import "RTCPeerConnection.h"
39 #import "RTCPeerConnectionDelegate.h"
40 #import "RTCPeerConnectionFactory.h"
41 #import "RTCSessionDescription.h"
42 #import "RTCStatsDelegate.h"
43 #import "RTCVideoRenderer.h"
44 #import "RTCVideoCapturer.h"
45 #import "RTCVideoTrack.h"
46 #import "APPRTCVideoView.h"
48 @interface PCObserver : NSObject<RTCPeerConnectionDelegate>
50 - (id)initWithDelegate:(id<APPRTCSendMessage>)delegate;
52 @property(nonatomic, strong) APPRTCVideoView* videoView;
56 @implementation PCObserver {
57 id<APPRTCSendMessage> _delegate;
60 - (id)initWithDelegate:(id<APPRTCSendMessage>)delegate {
61 if (self = [super init]) {
67 #pragma mark - RTCPeerConnectionDelegate
69 - (void)peerConnectionOnError:(RTCPeerConnection*)peerConnection {
70 dispatch_async(dispatch_get_main_queue(), ^(void) {
71 NSLog(@"PCO onError.");
72 NSAssert(NO, @"PeerConnection failed.");
76 - (void)peerConnection:(RTCPeerConnection*)peerConnection
77 signalingStateChanged:(RTCSignalingState)stateChanged {
78 dispatch_async(dispatch_get_main_queue(), ^(void) {
79 NSLog(@"PCO onSignalingStateChange: %d", stateChanged);
83 - (void)peerConnection:(RTCPeerConnection*)peerConnection
84 addedStream:(RTCMediaStream*)stream {
85 dispatch_async(dispatch_get_main_queue(), ^(void) {
86 NSLog(@"PCO onAddStream.");
87 NSAssert([stream.audioTracks count] <= 1,
88 @"Expected at most 1 audio stream");
89 NSAssert([stream.videoTracks count] <= 1,
90 @"Expected at most 1 video stream");
91 if ([stream.videoTracks count] != 0) {
93 renderVideoTrackInterface:[stream.videoTracks objectAtIndex:0]];
98 - (void)peerConnection:(RTCPeerConnection*)peerConnection
99 removedStream:(RTCMediaStream*)stream {
100 dispatch_async(dispatch_get_main_queue(),
101 ^(void) { NSLog(@"PCO onRemoveStream."); });
104 - (void)peerConnectionOnRenegotiationNeeded:(RTCPeerConnection*)peerConnection {
105 dispatch_async(dispatch_get_main_queue(), ^(void) {
106 NSLog(@"PCO onRenegotiationNeeded - ignoring because AppRTC has a "
107 "predefined negotiation strategy");
111 - (void)peerConnection:(RTCPeerConnection*)peerConnection
112 gotICECandidate:(RTCICECandidate*)candidate {
113 dispatch_async(dispatch_get_main_queue(), ^(void) {
114 NSLog(@"PCO onICECandidate.\n Mid[%@] Index[%d] Sdp[%@]",
116 candidate.sdpMLineIndex,
118 NSDictionary* json = @{
119 @"type" : @"candidate",
120 @"label" : [NSNumber numberWithInt:candidate.sdpMLineIndex],
121 @"id" : candidate.sdpMid,
122 @"candidate" : candidate.sdp
126 [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
128 [_delegate sendData:data];
131 @"Unable to serialize JSON object with error: %@",
132 error.localizedDescription);
137 - (void)peerConnection:(RTCPeerConnection*)peerConnection
138 iceGatheringChanged:(RTCICEGatheringState)newState {
139 dispatch_async(dispatch_get_main_queue(),
140 ^(void) { NSLog(@"PCO onIceGatheringChange. %d", newState); });
143 - (void)peerConnection:(RTCPeerConnection*)peerConnection
144 iceConnectionChanged:(RTCICEConnectionState)newState {
145 dispatch_async(dispatch_get_main_queue(), ^(void) {
146 NSLog(@"PCO onIceConnectionChange. %d", newState);
147 if (newState == RTCICEConnectionConnected)
148 [self displayLogMessage:@"ICE Connection Connected."];
149 NSAssert(newState != RTCICEConnectionFailed, @"ICE Connection failed!");
153 - (void)peerConnection:(RTCPeerConnection*)peerConnection
154 didOpenDataChannel:(RTCDataChannel*)dataChannel {
155 NSAssert(NO, @"AppRTC doesn't use DataChannels");
158 #pragma mark - Private
160 - (void)displayLogMessage:(NSString*)message {
161 [_delegate displayLogMessage:message];
166 @interface APPRTCAppDelegate () <RTCStatsDelegate>
168 @property(nonatomic, strong) APPRTCAppClient* client;
169 @property(nonatomic, strong) PCObserver* pcObserver;
170 @property(nonatomic, strong) RTCPeerConnection* peerConnection;
171 @property(nonatomic, strong) RTCPeerConnectionFactory* peerConnectionFactory;
172 @property(nonatomic, strong) NSMutableArray* queuedRemoteCandidates;
176 @implementation APPRTCAppDelegate {
177 NSTimer* _statsTimer;
180 #pragma mark - UIApplicationDelegate methods
182 - (BOOL)application:(UIApplication*)application
183 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
184 [RTCPeerConnectionFactory initializeSSL];
185 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
186 self.viewController =
187 [[APPRTCViewController alloc] initWithNibName:@"APPRTCViewController"
189 self.window.rootViewController = self.viewController;
191 [NSTimer scheduledTimerWithTimeInterval:10
193 selector:@selector(didFireStatsTimer:)
196 [self.window makeKeyAndVisible];
200 - (void)applicationWillResignActive:(UIApplication*)application {
201 [self displayLogMessage:@"Application lost focus, connection broken."];
205 - (void)applicationDidEnterBackground:(UIApplication*)application {
208 - (void)applicationWillEnterForeground:(UIApplication*)application {
211 - (void)applicationDidBecomeActive:(UIApplication*)application {
214 - (void)applicationWillTerminate:(UIApplication*)application {
217 - (BOOL)application:(UIApplication*)application
219 sourceApplication:(NSString*)sourceApplication
220 annotation:(id)annotation {
224 self.client = [[APPRTCAppClient alloc] initWithICEServerDelegate:self
225 messageHandler:self];
226 [self.client connectToRoom:url];
230 - (void)displayLogMessage:(NSString*)message {
231 NSAssert([NSThread isMainThread], @"Called off main thread!");
232 NSLog(@"%@", message);
233 [self.viewController displayText:message];
236 #pragma mark - RTCSendMessage method
238 - (void)sendData:(NSData*)data {
239 [self.client sendData:data];
242 #pragma mark - ICEServerDelegate method
244 - (void)onICEServers:(NSArray*)servers {
245 self.queuedRemoteCandidates = [NSMutableArray array];
246 self.peerConnectionFactory = [[RTCPeerConnectionFactory alloc] init];
247 RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc]
248 initWithMandatoryConstraints:
250 [[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" value:@"true"],
251 [[RTCPair alloc] initWithKey:@"OfferToReceiveVideo" value:@"true"]
255 [[RTCPair alloc] initWithKey:@"internalSctpDataChannels"
257 [[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement"
260 self.pcObserver = [[PCObserver alloc] initWithDelegate:self];
261 self.peerConnection =
262 [self.peerConnectionFactory peerConnectionWithICEServers:servers
263 constraints:constraints
264 delegate:self.pcObserver];
265 RTCMediaStream* lms =
266 [self.peerConnectionFactory mediaStreamWithLabel:@"ARDAMS"];
268 // The iOS simulator doesn't provide any sort of camera capture
269 // support or emulation (http://goo.gl/rHAnC1) so don't bother
270 // trying to open a local stream.
271 RTCVideoTrack* localVideoTrack;
272 #if !TARGET_IPHONE_SIMULATOR
273 NSString* cameraID = nil;
274 for (AVCaptureDevice* captureDevice in
275 [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
276 if (captureDevice.position == AVCaptureDevicePositionFront) {
277 cameraID = [captureDevice localizedName];
281 NSAssert(cameraID, @"Unable to get the front camera id");
283 RTCVideoCapturer* capturer =
284 [RTCVideoCapturer capturerWithDeviceName:cameraID];
285 self.videoSource = [self.peerConnectionFactory
286 videoSourceWithCapturer:capturer
287 constraints:self.client.videoConstraints];
289 [self.peerConnectionFactory videoTrackWithID:@"ARDAMSv0"
290 source:self.videoSource];
291 if (localVideoTrack) {
292 [lms addVideoTrack:localVideoTrack];
296 [self.viewController.localVideoView
297 renderVideoTrackInterface:localVideoTrack];
299 self.pcObserver.videoView = self.viewController.remoteVideoView;
301 [lms addAudioTrack:[self.peerConnectionFactory audioTrackWithID:@"ARDAMSa0"]];
302 [self.peerConnection addStream:lms constraints:constraints];
303 [self displayLogMessage:@"onICEServers - added local stream."];
306 #pragma mark - GAEMessageHandler methods
309 if (!self.client.initiator) {
310 [self displayLogMessage:@"Callee; waiting for remote offer"];
313 [self displayLogMessage:@"GAE onOpen - create offer."];
315 [[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" value:@"true"];
317 [[RTCPair alloc] initWithKey:@"OfferToReceiveVideo" value:@"true"];
318 NSArray* mandatory = @[ audio, video ];
319 RTCMediaConstraints* constraints =
320 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatory
321 optionalConstraints:nil];
322 [self.peerConnection createOfferWithDelegate:self constraints:constraints];
323 [self displayLogMessage:@"PC - createOffer."];
326 - (void)onMessage:(NSDictionary*)messageData {
327 NSString* type = messageData[@"type"];
328 NSAssert(type, @"Missing type: %@", messageData);
329 [self displayLogMessage:[NSString stringWithFormat:@"GAE onMessage type - %@",
331 if ([type isEqualToString:@"candidate"]) {
332 NSString* mid = messageData[@"id"];
333 NSNumber* sdpLineIndex = messageData[@"label"];
334 NSString* sdp = messageData[@"candidate"];
335 RTCICECandidate* candidate =
336 [[RTCICECandidate alloc] initWithMid:mid
337 index:sdpLineIndex.intValue
339 if (self.queuedRemoteCandidates) {
340 [self.queuedRemoteCandidates addObject:candidate];
342 [self.peerConnection addICECandidate:candidate];
344 } else if ([type isEqualToString:@"offer"] ||
345 [type isEqualToString:@"answer"]) {
346 NSString* sdpString = messageData[@"sdp"];
347 RTCSessionDescription* sdp = [[RTCSessionDescription alloc]
349 sdp:[APPRTCAppDelegate preferISAC:sdpString]];
350 [self.peerConnection setRemoteDescriptionWithDelegate:self
351 sessionDescription:sdp];
352 [self displayLogMessage:@"PC - setRemoteDescription."];
353 } else if ([type isEqualToString:@"bye"]) {
355 UIAlertView* alertView =
356 [[UIAlertView alloc] initWithTitle:@"Remote end hung up"
357 message:@"dropping PeerConnection"
359 cancelButtonTitle:@"OK"
360 otherButtonTitles:nil];
363 NSAssert(NO, @"Invalid message: %@", messageData);
368 [self displayLogMessage:@"GAE onClose."];
372 - (void)onError:(int)code withDescription:(NSString*)description {
373 [self displayLogMessage:[NSString stringWithFormat:@"GAE onError: %d, %@",
378 #pragma mark - RTCSessionDescriptionDelegate methods
380 // Match |pattern| to |string| and return the first group of the first
381 // match, or nil if no match was found.
382 + (NSString*)firstMatch:(NSRegularExpression*)pattern
383 withString:(NSString*)string {
384 NSTextCheckingResult* result =
385 [pattern firstMatchInString:string
387 range:NSMakeRange(0, [string length])];
390 return [string substringWithRange:[result rangeAtIndex:1]];
393 // Mangle |origSDP| to prefer the ISAC/16k audio codec.
394 + (NSString*)preferISAC:(NSString*)origSDP {
396 NSString* isac16kRtpMap = nil;
397 NSArray* lines = [origSDP componentsSeparatedByString:@"\n"];
398 NSRegularExpression* isac16kRegex = [NSRegularExpression
399 regularExpressionWithPattern:@"^a=rtpmap:(\\d+) ISAC/16000[\r]?$"
403 (i < [lines count]) && (mLineIndex == -1 || isac16kRtpMap == nil);
405 NSString* line = [lines objectAtIndex:i];
406 if ([line hasPrefix:@"m=audio "]) {
410 isac16kRtpMap = [self firstMatch:isac16kRegex withString:line];
412 if (mLineIndex == -1) {
413 NSLog(@"No m=audio line, so can't prefer iSAC");
416 if (isac16kRtpMap == nil) {
417 NSLog(@"No ISAC/16000 line, so can't prefer iSAC");
420 NSArray* origMLineParts =
421 [[lines objectAtIndex:mLineIndex] componentsSeparatedByString:@" "];
422 NSMutableArray* newMLine =
423 [NSMutableArray arrayWithCapacity:[origMLineParts count]];
424 int origPartIndex = 0;
425 // Format is: m=<media> <port> <proto> <fmt> ...
426 [newMLine addObject:[origMLineParts objectAtIndex:origPartIndex++]];
427 [newMLine addObject:[origMLineParts objectAtIndex:origPartIndex++]];
428 [newMLine addObject:[origMLineParts objectAtIndex:origPartIndex++]];
429 [newMLine addObject:isac16kRtpMap];
430 for (; origPartIndex < [origMLineParts count]; ++origPartIndex) {
432 isEqualToString:[origMLineParts objectAtIndex:origPartIndex]]) {
433 [newMLine addObject:[origMLineParts objectAtIndex:origPartIndex]];
436 NSMutableArray* newLines = [NSMutableArray arrayWithCapacity:[lines count]];
437 [newLines addObjectsFromArray:lines];
438 [newLines replaceObjectAtIndex:mLineIndex
439 withObject:[newMLine componentsJoinedByString:@" "]];
440 return [newLines componentsJoinedByString:@"\n"];
443 - (void)peerConnection:(RTCPeerConnection*)peerConnection
444 didCreateSessionDescription:(RTCSessionDescription*)origSdp
445 error:(NSError*)error {
446 dispatch_async(dispatch_get_main_queue(), ^(void) {
448 [self displayLogMessage:@"SDP onFailure."];
449 NSAssert(NO, error.description);
452 [self displayLogMessage:@"SDP onSuccess(SDP) - set local description."];
453 RTCSessionDescription* sdp = [[RTCSessionDescription alloc]
454 initWithType:origSdp.type
455 sdp:[APPRTCAppDelegate preferISAC:origSdp.description]];
456 [self.peerConnection setLocalDescriptionWithDelegate:self
457 sessionDescription:sdp];
459 [self displayLogMessage:@"PC setLocalDescription."];
460 NSDictionary* json = @{@"type" : sdp.type, @"sdp" : sdp.description};
463 [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
466 [NSString stringWithFormat:@"Error: %@", error.description]);
467 [self sendData:data];
471 - (void)peerConnection:(RTCPeerConnection*)peerConnection
472 didSetSessionDescriptionWithError:(NSError*)error {
473 dispatch_async(dispatch_get_main_queue(), ^(void) {
475 [self displayLogMessage:@"SDP onFailure."];
476 NSAssert(NO, error.description);
480 [self displayLogMessage:@"SDP onSuccess() - possibly drain candidates"];
481 if (!self.client.initiator) {
482 if (self.peerConnection.remoteDescription &&
483 !self.peerConnection.localDescription) {
484 [self displayLogMessage:@"Callee, setRemoteDescription succeeded"];
485 RTCPair* audio = [[RTCPair alloc] initWithKey:@"OfferToReceiveAudio"
487 RTCPair* video = [[RTCPair alloc] initWithKey:@"OfferToReceiveVideo"
489 NSArray* mandatory = @[ audio, video ];
490 RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc]
491 initWithMandatoryConstraints:mandatory
492 optionalConstraints:nil];
493 [self.peerConnection createAnswerWithDelegate:self
494 constraints:constraints];
495 [self displayLogMessage:@"PC - createAnswer."];
497 [self displayLogMessage:@"SDP onSuccess - drain candidates"];
498 [self drainRemoteCandidates];
501 if (self.peerConnection.remoteDescription) {
502 [self displayLogMessage:@"SDP onSuccess - drain candidates"];
503 [self drainRemoteCandidates];
509 #pragma mark - RTCStatsDelegate methods
511 - (void)peerConnection:(RTCPeerConnection*)peerConnection
512 didGetStats:(NSArray*)stats {
513 dispatch_async(dispatch_get_main_queue(), ^{
514 NSString* message = [NSString stringWithFormat:@"Stats:\n %@", stats];
515 [self displayLogMessage:message];
519 #pragma mark - internal methods
523 sendData:[@"{\"type\": \"bye\"}" dataUsingEncoding:NSUTF8StringEncoding]];
524 [self.peerConnection close];
525 self.peerConnection = nil;
526 self.pcObserver = nil;
528 self.videoSource = nil;
529 self.peerConnectionFactory = nil;
530 [RTCPeerConnectionFactory deinitializeSSL];
533 - (void)drainRemoteCandidates {
534 for (RTCICECandidate* candidate in self.queuedRemoteCandidates) {
535 [self.peerConnection addICECandidate:candidate];
537 self.queuedRemoteCandidates = nil;
540 - (NSString*)unHTMLifyString:(NSString*)base {
541 // TODO(hughv): Investigate why percent escapes are being added. Removing
542 // them isn't necessary on Android.
543 // convert HTML escaped characters to UTF8.
544 NSString* removePercent =
545 [base stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
546 // remove leading and trailing ".
548 range.length = [removePercent length] - 2;
550 NSString* removeQuotes = [removePercent substringWithRange:range];
552 NSString* removeEscapedQuotes =
553 [removeQuotes stringByReplacingOccurrencesOfString:@"\\\""
556 NSString* removeBackslash =
557 [removeEscapedQuotes stringByReplacingOccurrencesOfString:@"\\\\"
559 return removeBackslash;
562 - (void)didFireStatsTimer:(NSTimer *)timer {
563 if (self.peerConnection) {
564 [self.peerConnection getStatsWithDelegate:self
566 statsOutputLevel:RTCStatsOutputLevelDebug];
570 #pragma mark - public methods
572 - (void)closeVideoUI {
573 [self.viewController resetUI];