Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / examples / ios / AppRTCDemo / APPRTCAppDelegate.m
1 /*
2  * libjingle
3  * Copyright 2013, Google Inc.
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 <AVFoundation/AVFoundation.h>
29
30 #import "APPRTCAppDelegate.h"
31
32 #import "APPRTCViewController.h"
33 #import "RTCICECandidate.h"
34 #import "RTCICEServer.h"
35 #import "RTCMediaConstraints.h"
36 #import "RTCMediaStream.h"
37 #import "RTCPair.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"
47
48 @interface PCObserver : NSObject<RTCPeerConnectionDelegate>
49
50 - (id)initWithDelegate:(id<APPRTCSendMessage>)delegate;
51
52 @property(nonatomic, strong) APPRTCVideoView* videoView;
53
54 @end
55
56 @implementation PCObserver {
57   id<APPRTCSendMessage> _delegate;
58 }
59
60 - (id)initWithDelegate:(id<APPRTCSendMessage>)delegate {
61   if (self = [super init]) {
62     _delegate = delegate;
63   }
64   return self;
65 }
66
67 #pragma mark - RTCPeerConnectionDelegate
68
69 - (void)peerConnectionOnError:(RTCPeerConnection*)peerConnection {
70   dispatch_async(dispatch_get_main_queue(), ^(void) {
71       NSLog(@"PCO onError.");
72       NSAssert(NO, @"PeerConnection failed.");
73   });
74 }
75
76 - (void)peerConnection:(RTCPeerConnection*)peerConnection
77     signalingStateChanged:(RTCSignalingState)stateChanged {
78   dispatch_async(dispatch_get_main_queue(), ^(void) {
79       NSLog(@"PCO onSignalingStateChange: %d", stateChanged);
80   });
81 }
82
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) {
92         [self.videoView
93             renderVideoTrackInterface:[stream.videoTracks objectAtIndex:0]];
94       }
95   });
96 }
97
98 - (void)peerConnection:(RTCPeerConnection*)peerConnection
99          removedStream:(RTCMediaStream*)stream {
100   dispatch_async(dispatch_get_main_queue(),
101                  ^(void) { NSLog(@"PCO onRemoveStream."); });
102 }
103
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");
108   });
109 }
110
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[%@]",
115             candidate.sdpMid,
116             candidate.sdpMLineIndex,
117             candidate.sdp);
118       NSDictionary* json = @{
119         @"type" : @"candidate",
120         @"label" : [NSNumber numberWithInt:candidate.sdpMLineIndex],
121         @"id" : candidate.sdpMid,
122         @"candidate" : candidate.sdp
123       };
124       NSError* error;
125       NSData* data =
126           [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
127       if (!error) {
128         [_delegate sendData:data];
129       } else {
130         NSAssert(NO,
131                  @"Unable to serialize JSON object with error: %@",
132                  error.localizedDescription);
133       }
134   });
135 }
136
137 - (void)peerConnection:(RTCPeerConnection*)peerConnection
138     iceGatheringChanged:(RTCICEGatheringState)newState {
139   dispatch_async(dispatch_get_main_queue(),
140                  ^(void) { NSLog(@"PCO onIceGatheringChange. %d", newState); });
141 }
142
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!");
150   });
151 }
152
153 - (void)peerConnection:(RTCPeerConnection*)peerConnection
154     didOpenDataChannel:(RTCDataChannel*)dataChannel {
155   NSAssert(NO, @"AppRTC doesn't use DataChannels");
156 }
157
158 #pragma mark - Private
159
160 - (void)displayLogMessage:(NSString*)message {
161   [_delegate displayLogMessage:message];
162 }
163
164 @end
165
166 @interface APPRTCAppDelegate () <RTCStatsDelegate>
167
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;
173
174 @end
175
176 @implementation APPRTCAppDelegate {
177   NSTimer* _statsTimer;
178 }
179
180 #pragma mark - UIApplicationDelegate methods
181
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"
188                                              bundle:nil];
189   self.window.rootViewController = self.viewController;
190   _statsTimer =
191       [NSTimer scheduledTimerWithTimeInterval:10
192                                        target:self
193                                      selector:@selector(didFireStatsTimer:)
194                                      userInfo:nil
195                                       repeats:YES];
196   [self.window makeKeyAndVisible];
197   return YES;
198 }
199
200 - (void)applicationWillResignActive:(UIApplication*)application {
201   [self displayLogMessage:@"Application lost focus, connection broken."];
202   [self closeVideoUI];
203 }
204
205 - (void)applicationDidEnterBackground:(UIApplication*)application {
206 }
207
208 - (void)applicationWillEnterForeground:(UIApplication*)application {
209 }
210
211 - (void)applicationDidBecomeActive:(UIApplication*)application {
212 }
213
214 - (void)applicationWillTerminate:(UIApplication*)application {
215 }
216
217 - (BOOL)application:(UIApplication*)application
218               openURL:(NSURL*)url
219     sourceApplication:(NSString*)sourceApplication
220            annotation:(id)annotation {
221   if (self.client) {
222     return NO;
223   }
224   self.client = [[APPRTCAppClient alloc] initWithICEServerDelegate:self
225                                                     messageHandler:self];
226   [self.client connectToRoom:url];
227   return YES;
228 }
229
230 - (void)displayLogMessage:(NSString*)message {
231   NSAssert([NSThread isMainThread], @"Called off main thread!");
232   NSLog(@"%@", message);
233   [self.viewController displayText:message];
234 }
235
236 #pragma mark - RTCSendMessage method
237
238 - (void)sendData:(NSData*)data {
239   [self.client sendData:data];
240 }
241
242 #pragma mark - ICEServerDelegate method
243
244 - (void)onICEServers:(NSArray*)servers {
245   self.queuedRemoteCandidates = [NSMutableArray array];
246   self.peerConnectionFactory = [[RTCPeerConnectionFactory alloc] init];
247   RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc]
248       initWithMandatoryConstraints:
249           @[
250             [[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" value:@"true"],
251             [[RTCPair alloc] initWithKey:@"OfferToReceiveVideo" value:@"true"]
252           ]
253                optionalConstraints:
254                    @[
255                      [[RTCPair alloc] initWithKey:@"internalSctpDataChannels"
256                                             value:@"true"],
257                      [[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement"
258                                             value:@"true"]
259                    ]];
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"];
267
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];
278       break;
279     }
280   }
281   NSAssert(cameraID, @"Unable to get the front camera id");
282
283   RTCVideoCapturer* capturer =
284       [RTCVideoCapturer capturerWithDeviceName:cameraID];
285   self.videoSource = [self.peerConnectionFactory
286       videoSourceWithCapturer:capturer
287                   constraints:self.client.videoConstraints];
288   localVideoTrack =
289       [self.peerConnectionFactory videoTrackWithID:@"ARDAMSv0"
290                                             source:self.videoSource];
291   if (localVideoTrack) {
292     [lms addVideoTrack:localVideoTrack];
293   }
294 #endif
295
296   [self.viewController.localVideoView
297       renderVideoTrackInterface:localVideoTrack];
298
299   self.pcObserver.videoView = self.viewController.remoteVideoView;
300
301   [lms addAudioTrack:[self.peerConnectionFactory audioTrackWithID:@"ARDAMSa0"]];
302   [self.peerConnection addStream:lms constraints:constraints];
303   [self displayLogMessage:@"onICEServers - added local stream."];
304 }
305
306 #pragma mark - GAEMessageHandler methods
307
308 - (void)onOpen {
309   if (!self.client.initiator) {
310     [self displayLogMessage:@"Callee; waiting for remote offer"];
311     return;
312   }
313   [self displayLogMessage:@"GAE onOpen - create offer."];
314   RTCPair* audio =
315       [[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" value:@"true"];
316   RTCPair* video =
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."];
324 }
325
326 - (void)onMessage:(NSDictionary*)messageData {
327   NSString* type = messageData[@"type"];
328   NSAssert(type, @"Missing type: %@", messageData);
329   [self displayLogMessage:[NSString stringWithFormat:@"GAE onMessage type - %@",
330                                                      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
338                                          sdp:sdp];
339     if (self.queuedRemoteCandidates) {
340       [self.queuedRemoteCandidates addObject:candidate];
341     } else {
342       [self.peerConnection addICECandidate:candidate];
343     }
344   } else if ([type isEqualToString:@"offer"] ||
345              [type isEqualToString:@"answer"]) {
346     NSString* sdpString = messageData[@"sdp"];
347     RTCSessionDescription* sdp = [[RTCSessionDescription alloc]
348         initWithType:type
349                  sdp:[APPRTCAppDelegate preferISAC:sdpString]];
350     [self.peerConnection setRemoteDescriptionWithDelegate:self
351                                        sessionDescription:sdp];
352     [self displayLogMessage:@"PC - setRemoteDescription."];
353   } else if ([type isEqualToString:@"bye"]) {
354     [self closeVideoUI];
355     UIAlertView* alertView =
356         [[UIAlertView alloc] initWithTitle:@"Remote end hung up"
357                                    message:@"dropping PeerConnection"
358                                   delegate:nil
359                          cancelButtonTitle:@"OK"
360                          otherButtonTitles:nil];
361     [alertView show];
362   } else {
363     NSAssert(NO, @"Invalid message: %@", messageData);
364   }
365 }
366
367 - (void)onClose {
368   [self displayLogMessage:@"GAE onClose."];
369   [self closeVideoUI];
370 }
371
372 - (void)onError:(int)code withDescription:(NSString*)description {
373   [self displayLogMessage:[NSString stringWithFormat:@"GAE onError: %d, %@",
374                                     code, description]];
375   [self closeVideoUI];
376 }
377
378 #pragma mark - RTCSessionDescriptionDelegate methods
379
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
386                           options:0
387                             range:NSMakeRange(0, [string length])];
388   if (!result)
389     return nil;
390   return [string substringWithRange:[result rangeAtIndex:1]];
391 }
392
393 // Mangle |origSDP| to prefer the ISAC/16k audio codec.
394 + (NSString*)preferISAC:(NSString*)origSDP {
395   int mLineIndex = -1;
396   NSString* isac16kRtpMap = nil;
397   NSArray* lines = [origSDP componentsSeparatedByString:@"\n"];
398   NSRegularExpression* isac16kRegex = [NSRegularExpression
399       regularExpressionWithPattern:@"^a=rtpmap:(\\d+) ISAC/16000[\r]?$"
400                            options:0
401                              error:nil];
402   for (int i = 0;
403        (i < [lines count]) && (mLineIndex == -1 || isac16kRtpMap == nil);
404        ++i) {
405     NSString* line = [lines objectAtIndex:i];
406     if ([line hasPrefix:@"m=audio "]) {
407       mLineIndex = i;
408       continue;
409     }
410     isac16kRtpMap = [self firstMatch:isac16kRegex withString:line];
411   }
412   if (mLineIndex == -1) {
413     NSLog(@"No m=audio line, so can't prefer iSAC");
414     return origSDP;
415   }
416   if (isac16kRtpMap == nil) {
417     NSLog(@"No ISAC/16000 line, so can't prefer iSAC");
418     return origSDP;
419   }
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) {
431     if (![isac16kRtpMap
432             isEqualToString:[origMLineParts objectAtIndex:origPartIndex]]) {
433       [newMLine addObject:[origMLineParts objectAtIndex:origPartIndex]];
434     }
435   }
436   NSMutableArray* newLines = [NSMutableArray arrayWithCapacity:[lines count]];
437   [newLines addObjectsFromArray:lines];
438   [newLines replaceObjectAtIndex:mLineIndex
439                       withObject:[newMLine componentsJoinedByString:@" "]];
440   return [newLines componentsJoinedByString:@"\n"];
441 }
442
443 - (void)peerConnection:(RTCPeerConnection*)peerConnection
444     didCreateSessionDescription:(RTCSessionDescription*)origSdp
445                           error:(NSError*)error {
446   dispatch_async(dispatch_get_main_queue(), ^(void) {
447       if (error) {
448         [self displayLogMessage:@"SDP onFailure."];
449         NSAssert(NO, error.description);
450         return;
451       }
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];
458
459       [self displayLogMessage:@"PC setLocalDescription."];
460       NSDictionary* json = @{@"type" : sdp.type, @"sdp" : sdp.description};
461       NSError* error;
462       NSData* data =
463           [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
464       NSAssert(!error,
465                @"%@",
466                [NSString stringWithFormat:@"Error: %@", error.description]);
467       [self sendData:data];
468   });
469 }
470
471 - (void)peerConnection:(RTCPeerConnection*)peerConnection
472     didSetSessionDescriptionWithError:(NSError*)error {
473   dispatch_async(dispatch_get_main_queue(), ^(void) {
474       if (error) {
475         [self displayLogMessage:@"SDP onFailure."];
476         NSAssert(NO, error.description);
477         return;
478       }
479
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"
486                                                   value:@"true"];
487           RTCPair* video = [[RTCPair alloc] initWithKey:@"OfferToReceiveVideo"
488                                                   value:@"true"];
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."];
496         } else {
497           [self displayLogMessage:@"SDP onSuccess - drain candidates"];
498           [self drainRemoteCandidates];
499         }
500       } else {
501         if (self.peerConnection.remoteDescription) {
502           [self displayLogMessage:@"SDP onSuccess - drain candidates"];
503           [self drainRemoteCandidates];
504         }
505       }
506   });
507 }
508
509 #pragma mark - RTCStatsDelegate methods
510
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];
516   });
517 }
518
519 #pragma mark - internal methods
520
521 - (void)disconnect {
522   [self.client
523       sendData:[@"{\"type\": \"bye\"}" dataUsingEncoding:NSUTF8StringEncoding]];
524   [self.peerConnection close];
525   self.peerConnection = nil;
526   self.pcObserver = nil;
527   self.client = nil;
528   self.videoSource = nil;
529   self.peerConnectionFactory = nil;
530   [RTCPeerConnectionFactory deinitializeSSL];
531 }
532
533 - (void)drainRemoteCandidates {
534   for (RTCICECandidate* candidate in self.queuedRemoteCandidates) {
535     [self.peerConnection addICECandidate:candidate];
536   }
537   self.queuedRemoteCandidates = nil;
538 }
539
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 ".
547   NSRange range;
548   range.length = [removePercent length] - 2;
549   range.location = 1;
550   NSString* removeQuotes = [removePercent substringWithRange:range];
551   // convert \" to ".
552   NSString* removeEscapedQuotes =
553       [removeQuotes stringByReplacingOccurrencesOfString:@"\\\""
554                                               withString:@"\""];
555   // convert \\ to \.
556   NSString* removeBackslash =
557       [removeEscapedQuotes stringByReplacingOccurrencesOfString:@"\\\\"
558                                                      withString:@"\\"];
559   return removeBackslash;
560 }
561
562 - (void)didFireStatsTimer:(NSTimer *)timer {
563   if (self.peerConnection) {
564     [self.peerConnection getStatsWithDelegate:self
565                              mediaStreamTrack:nil
566                              statsOutputLevel:RTCStatsOutputLevelDebug];
567   }
568 }
569
570 #pragma mark - public methods
571
572 - (void)closeVideoUI {
573   [self.viewController resetUI];
574   [self disconnect];
575 }
576
577 @end