2013-07-10 02:45:36 +02:00
|
|
|
/*
|
|
|
|
* libjingle
|
|
|
|
* Copyright 2013, Google Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2014-03-10 21:41:22 +01:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
#import "APPRTCAppDelegate.h"
|
|
|
|
|
|
|
|
#import "APPRTCViewController.h"
|
2013-08-01 20:29:45 +02:00
|
|
|
#import "RTCICECandidate.h"
|
|
|
|
#import "RTCICEServer.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
#import "RTCMediaConstraints.h"
|
|
|
|
#import "RTCMediaStream.h"
|
|
|
|
#import "RTCPair.h"
|
|
|
|
#import "RTCPeerConnection.h"
|
|
|
|
#import "RTCPeerConnectionDelegate.h"
|
|
|
|
#import "RTCPeerConnectionFactory.h"
|
|
|
|
#import "RTCSessionDescription.h"
|
2014-04-22 23:05:38 +02:00
|
|
|
#import "RTCStatsDelegate.h"
|
2014-03-10 21:41:22 +01:00
|
|
|
#import "RTCVideoRenderer.h"
|
|
|
|
#import "RTCVideoCapturer.h"
|
|
|
|
#import "RTCVideoTrack.h"
|
2014-03-25 01:11:56 +01:00
|
|
|
#import "APPRTCVideoView.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
@interface PCObserver : NSObject<RTCPeerConnectionDelegate>
|
|
|
|
|
|
|
|
- (id)initWithDelegate:(id<APPRTCSendMessage>)delegate;
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
@property(nonatomic, strong) APPRTCVideoView* videoView;
|
2014-03-10 21:41:22 +01:00
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation PCObserver {
|
|
|
|
id<APPRTCSendMessage> _delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithDelegate:(id<APPRTCSendMessage>)delegate {
|
|
|
|
if (self = [super init]) {
|
|
|
|
_delegate = delegate;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-04-22 23:05:38 +02:00
|
|
|
#pragma mark - RTCPeerConnectionDelegate
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnectionOnError:(RTCPeerConnection*)peerConnection {
|
2014-03-31 22:16:49 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
|
|
|
NSLog(@"PCO onError.");
|
|
|
|
NSAssert(NO, @"PeerConnection failed.");
|
|
|
|
});
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
2013-08-01 20:29:45 +02:00
|
|
|
signalingStateChanged:(RTCSignalingState)stateChanged {
|
2014-03-31 22:16:49 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
|
|
|
NSLog(@"PCO onSignalingStateChange: %d", stateChanged);
|
|
|
|
});
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
|
|
|
addedStream:(RTCMediaStream*)stream {
|
2013-07-10 02:45:36 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
2014-03-31 22:16:49 +02:00
|
|
|
NSLog(@"PCO onAddStream.");
|
2014-03-25 18:40:38 +01:00
|
|
|
NSAssert([stream.audioTracks count] <= 1,
|
|
|
|
@"Expected at most 1 audio stream");
|
2014-03-25 01:11:56 +01:00
|
|
|
NSAssert([stream.videoTracks count] <= 1,
|
|
|
|
@"Expected at most 1 video stream");
|
|
|
|
if ([stream.videoTracks count] != 0) {
|
|
|
|
[self.videoView
|
|
|
|
renderVideoTrackInterface:[stream.videoTracks objectAtIndex:0]];
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
|
|
|
removedStream:(RTCMediaStream*)stream {
|
2014-03-31 22:16:49 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(),
|
|
|
|
^(void) { NSLog(@"PCO onRemoveStream."); });
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnectionOnRenegotiationNeeded:(RTCPeerConnection*)peerConnection {
|
2014-03-31 22:16:49 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
|
|
|
NSLog(@"PCO onRenegotiationNeeded - ignoring because AppRTC has a "
|
|
|
|
"predefined negotiation strategy");
|
|
|
|
});
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
|
|
|
gotICECandidate:(RTCICECandidate*)candidate {
|
2014-03-31 22:16:49 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
|
|
|
NSLog(@"PCO onICECandidate.\n Mid[%@] Index[%d] Sdp[%@]",
|
|
|
|
candidate.sdpMid,
|
|
|
|
candidate.sdpMLineIndex,
|
|
|
|
candidate.sdp);
|
|
|
|
NSDictionary* json = @{
|
|
|
|
@"type" : @"candidate",
|
|
|
|
@"label" : [NSNumber numberWithInt:candidate.sdpMLineIndex],
|
|
|
|
@"id" : candidate.sdpMid,
|
|
|
|
@"candidate" : candidate.sdp
|
|
|
|
};
|
|
|
|
NSError* error;
|
|
|
|
NSData* data =
|
|
|
|
[NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
|
|
|
|
if (!error) {
|
|
|
|
[_delegate sendData:data];
|
|
|
|
} else {
|
|
|
|
NSAssert(NO,
|
|
|
|
@"Unable to serialize JSON object with error: %@",
|
|
|
|
error.localizedDescription);
|
|
|
|
}
|
|
|
|
});
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
2013-08-01 20:29:45 +02:00
|
|
|
iceGatheringChanged:(RTCICEGatheringState)newState {
|
2014-03-31 22:16:49 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(),
|
|
|
|
^(void) { NSLog(@"PCO onIceGatheringChange. %d", newState); });
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
2013-08-01 20:29:45 +02:00
|
|
|
iceConnectionChanged:(RTCICEConnectionState)newState {
|
2014-03-31 22:16:49 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
|
|
|
NSLog(@"PCO onIceConnectionChange. %d", newState);
|
|
|
|
if (newState == RTCICEConnectionConnected)
|
|
|
|
[self displayLogMessage:@"ICE Connection Connected."];
|
|
|
|
NSAssert(newState != RTCICEConnectionFailed, @"ICE Connection failed!");
|
|
|
|
});
|
2013-09-05 23:49:58 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 20:32:33 +02:00
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
|
|
|
didOpenDataChannel:(RTCDataChannel*)dataChannel {
|
|
|
|
NSAssert(NO, @"AppRTC doesn't use DataChannels");
|
|
|
|
}
|
|
|
|
|
2014-04-22 23:05:38 +02:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)displayLogMessage:(NSString*)message {
|
2013-09-05 23:49:58 +02:00
|
|
|
[_delegate displayLogMessage:message];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2014-04-22 23:05:38 +02:00
|
|
|
@interface APPRTCAppDelegate () <RTCStatsDelegate>
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
@property(nonatomic, strong) APPRTCAppClient* client;
|
|
|
|
@property(nonatomic, strong) PCObserver* pcObserver;
|
|
|
|
@property(nonatomic, strong) RTCPeerConnection* peerConnection;
|
|
|
|
@property(nonatomic, strong) RTCPeerConnectionFactory* peerConnectionFactory;
|
|
|
|
@property(nonatomic, strong) NSMutableArray* queuedRemoteCandidates;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2014-04-22 23:05:38 +02:00
|
|
|
@implementation APPRTCAppDelegate {
|
|
|
|
NSTimer* _statsTimer;
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
#pragma mark - UIApplicationDelegate methods
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (BOOL)application:(UIApplication*)application
|
|
|
|
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
|
2013-08-01 20:29:45 +02:00
|
|
|
[RTCPeerConnectionFactory initializeSSL];
|
2013-07-10 02:45:36 +02:00
|
|
|
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
|
|
|
self.viewController =
|
2013-08-01 20:29:45 +02:00
|
|
|
[[APPRTCViewController alloc] initWithNibName:@"APPRTCViewController"
|
2013-07-10 02:45:36 +02:00
|
|
|
bundle:nil];
|
|
|
|
self.window.rootViewController = self.viewController;
|
2014-04-22 23:05:38 +02:00
|
|
|
_statsTimer =
|
|
|
|
[NSTimer scheduledTimerWithTimeInterval:10
|
|
|
|
target:self
|
|
|
|
selector:@selector(didFireStatsTimer:)
|
|
|
|
userInfo:nil
|
|
|
|
repeats:YES];
|
2013-07-10 02:45:36 +02:00
|
|
|
[self.window makeKeyAndVisible];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)applicationWillResignActive:(UIApplication*)application {
|
2013-07-10 02:45:36 +02:00
|
|
|
[self displayLogMessage:@"Application lost focus, connection broken."];
|
2014-03-10 21:41:22 +01:00
|
|
|
[self closeVideoUI];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)applicationDidEnterBackground:(UIApplication*)application {
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)applicationWillEnterForeground:(UIApplication*)application {
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)applicationDidBecomeActive:(UIApplication*)application {
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)applicationWillTerminate:(UIApplication*)application {
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (BOOL)application:(UIApplication*)application
|
|
|
|
openURL:(NSURL*)url
|
|
|
|
sourceApplication:(NSString*)sourceApplication
|
2013-07-10 02:45:36 +02:00
|
|
|
annotation:(id)annotation {
|
|
|
|
if (self.client) {
|
|
|
|
return NO;
|
|
|
|
}
|
2014-03-25 01:11:56 +01:00
|
|
|
self.client = [[APPRTCAppClient alloc] initWithICEServerDelegate:self
|
|
|
|
messageHandler:self];
|
2013-07-10 02:45:36 +02:00
|
|
|
[self.client connectToRoom:url];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)displayLogMessage:(NSString*)message {
|
2014-03-31 22:16:49 +02:00
|
|
|
NSAssert([NSThread isMainThread], @"Called off main thread!");
|
2013-07-10 02:45:36 +02:00
|
|
|
NSLog(@"%@", message);
|
|
|
|
[self.viewController displayText:message];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - RTCSendMessage method
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)sendData:(NSData*)data {
|
2013-07-10 02:45:36 +02:00
|
|
|
[self.client sendData:data];
|
|
|
|
}
|
|
|
|
|
2013-08-01 20:29:45 +02:00
|
|
|
#pragma mark - ICEServerDelegate method
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)onICEServers:(NSArray*)servers {
|
2013-07-10 02:45:36 +02:00
|
|
|
self.queuedRemoteCandidates = [NSMutableArray array];
|
|
|
|
self.peerConnectionFactory = [[RTCPeerConnectionFactory alloc] init];
|
2014-03-25 01:11:56 +01:00
|
|
|
RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc]
|
|
|
|
initWithMandatoryConstraints:
|
|
|
|
@[
|
|
|
|
[[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" value:@"true"],
|
|
|
|
[[RTCPair alloc] initWithKey:@"OfferToReceiveVideo" value:@"true"]
|
|
|
|
]
|
|
|
|
optionalConstraints:
|
|
|
|
@[
|
|
|
|
[[RTCPair alloc] initWithKey:@"internalSctpDataChannels"
|
|
|
|
value:@"true"],
|
|
|
|
[[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement"
|
|
|
|
value:@"true"]
|
|
|
|
]];
|
2013-07-10 02:45:36 +02:00
|
|
|
self.pcObserver = [[PCObserver alloc] initWithDelegate:self];
|
|
|
|
self.peerConnection =
|
2013-08-01 20:29:45 +02:00
|
|
|
[self.peerConnectionFactory peerConnectionWithICEServers:servers
|
2013-07-10 02:45:36 +02:00
|
|
|
constraints:constraints
|
|
|
|
delegate:self.pcObserver];
|
2014-03-25 01:11:56 +01:00
|
|
|
RTCMediaStream* lms =
|
2013-07-10 02:45:36 +02:00
|
|
|
[self.peerConnectionFactory mediaStreamWithLabel:@"ARDAMS"];
|
2014-03-10 21:41:22 +01:00
|
|
|
|
2014-03-31 22:22:19 +02:00
|
|
|
// The iOS simulator doesn't provide any sort of camera capture
|
|
|
|
// support or emulation (http://goo.gl/rHAnC1) so don't bother
|
|
|
|
// trying to open a local stream.
|
|
|
|
RTCVideoTrack* localVideoTrack;
|
|
|
|
#if !TARGET_IPHONE_SIMULATOR
|
2014-03-25 01:11:56 +01:00
|
|
|
NSString* cameraID = nil;
|
|
|
|
for (AVCaptureDevice* captureDevice in
|
|
|
|
[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
|
|
|
|
if (captureDevice.position == AVCaptureDevicePositionFront) {
|
|
|
|
cameraID = [captureDevice localizedName];
|
|
|
|
break;
|
|
|
|
}
|
2014-03-10 21:41:22 +01:00
|
|
|
}
|
|
|
|
NSAssert(cameraID, @"Unable to get the front camera id");
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
RTCVideoCapturer* capturer =
|
|
|
|
[RTCVideoCapturer capturerWithDeviceName:cameraID];
|
2014-03-25 06:16:29 +01:00
|
|
|
self.videoSource = [self.peerConnectionFactory
|
2014-03-25 01:11:56 +01:00
|
|
|
videoSourceWithCapturer:capturer
|
|
|
|
constraints:self.client.videoConstraints];
|
2014-03-31 22:22:19 +02:00
|
|
|
localVideoTrack =
|
2014-03-25 01:11:56 +01:00
|
|
|
[self.peerConnectionFactory videoTrackWithID:@"ARDAMSv0"
|
2014-03-25 06:16:29 +01:00
|
|
|
source:self.videoSource];
|
2014-03-10 21:41:22 +01:00
|
|
|
if (localVideoTrack) {
|
2014-03-25 01:11:56 +01:00
|
|
|
[lms addVideoTrack:localVideoTrack];
|
2014-03-10 21:41:22 +01:00
|
|
|
}
|
2014-03-31 22:22:19 +02:00
|
|
|
#endif
|
2014-03-10 21:41:22 +01:00
|
|
|
|
|
|
|
[self.viewController.localVideoView
|
2014-03-25 01:11:56 +01:00
|
|
|
renderVideoTrackInterface:localVideoTrack];
|
2014-03-10 21:41:22 +01:00
|
|
|
|
|
|
|
self.pcObserver.videoView = self.viewController.remoteVideoView;
|
|
|
|
|
2013-08-01 20:29:45 +02:00
|
|
|
[lms addAudioTrack:[self.peerConnectionFactory audioTrackWithID:@"ARDAMSa0"]];
|
|
|
|
[self.peerConnection addStream:lms constraints:constraints];
|
2014-03-31 22:16:49 +02:00
|
|
|
[self displayLogMessage:@"onICEServers - added local stream."];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - GAEMessageHandler methods
|
|
|
|
|
|
|
|
- (void)onOpen {
|
2014-02-10 19:47:11 +01:00
|
|
|
if (!self.client.initiator) {
|
2014-03-25 01:11:56 +01:00
|
|
|
[self displayLogMessage:@"Callee; waiting for remote offer"];
|
|
|
|
return;
|
2014-02-10 19:47:11 +01:00
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
[self displayLogMessage:@"GAE onOpen - create offer."];
|
2014-03-25 01:11:56 +01:00
|
|
|
RTCPair* audio =
|
2013-07-10 02:45:36 +02:00
|
|
|
[[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" value:@"true"];
|
2014-03-25 01:11:56 +01:00
|
|
|
RTCPair* video =
|
|
|
|
[[RTCPair alloc] initWithKey:@"OfferToReceiveVideo" value:@"true"];
|
|
|
|
NSArray* mandatory = @[ audio, video ];
|
|
|
|
RTCMediaConstraints* constraints =
|
2013-07-10 02:45:36 +02:00
|
|
|
[[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatory
|
|
|
|
optionalConstraints:nil];
|
|
|
|
[self.peerConnection createOfferWithDelegate:self constraints:constraints];
|
|
|
|
[self displayLogMessage:@"PC - createOffer."];
|
|
|
|
}
|
|
|
|
|
2014-03-31 22:16:49 +02:00
|
|
|
- (void)onMessage:(NSDictionary*)messageData {
|
|
|
|
NSString* type = messageData[@"type"];
|
|
|
|
NSAssert(type, @"Missing type: %@", messageData);
|
2014-03-25 01:11:56 +01:00
|
|
|
[self displayLogMessage:[NSString stringWithFormat:@"GAE onMessage type - %@",
|
2014-03-31 22:16:49 +02:00
|
|
|
type]];
|
|
|
|
if ([type isEqualToString:@"candidate"]) {
|
|
|
|
NSString* mid = messageData[@"id"];
|
|
|
|
NSNumber* sdpLineIndex = messageData[@"label"];
|
|
|
|
NSString* sdp = messageData[@"candidate"];
|
2014-03-25 01:11:56 +01:00
|
|
|
RTCICECandidate* candidate =
|
2013-08-01 20:29:45 +02:00
|
|
|
[[RTCICECandidate alloc] initWithMid:mid
|
2013-07-10 02:45:36 +02:00
|
|
|
index:sdpLineIndex.intValue
|
|
|
|
sdp:sdp];
|
|
|
|
if (self.queuedRemoteCandidates) {
|
|
|
|
[self.queuedRemoteCandidates addObject:candidate];
|
|
|
|
} else {
|
2013-08-01 20:29:45 +02:00
|
|
|
[self.peerConnection addICECandidate:candidate];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
2014-03-31 22:16:49 +02:00
|
|
|
} else if ([type isEqualToString:@"offer"] ||
|
|
|
|
[type isEqualToString:@"answer"]) {
|
|
|
|
NSString* sdpString = messageData[@"sdp"];
|
2014-03-25 01:11:56 +01:00
|
|
|
RTCSessionDescription* sdp = [[RTCSessionDescription alloc]
|
2014-03-31 22:16:49 +02:00
|
|
|
initWithType:type
|
2014-03-25 01:11:56 +01:00
|
|
|
sdp:[APPRTCAppDelegate preferISAC:sdpString]];
|
2013-07-10 02:45:36 +02:00
|
|
|
[self.peerConnection setRemoteDescriptionWithDelegate:self
|
|
|
|
sessionDescription:sdp];
|
|
|
|
[self displayLogMessage:@"PC - setRemoteDescription."];
|
2014-03-31 22:16:49 +02:00
|
|
|
} else if ([type isEqualToString:@"bye"]) {
|
2014-03-10 21:41:22 +01:00
|
|
|
[self closeVideoUI];
|
2014-03-25 01:11:56 +01:00
|
|
|
UIAlertView* alertView =
|
|
|
|
[[UIAlertView alloc] initWithTitle:@"Remote end hung up"
|
|
|
|
message:@"dropping PeerConnection"
|
|
|
|
delegate:nil
|
|
|
|
cancelButtonTitle:@"OK"
|
|
|
|
otherButtonTitles:nil];
|
2014-03-10 21:41:22 +01:00
|
|
|
[alertView show];
|
2013-07-10 02:45:36 +02:00
|
|
|
} else {
|
2014-03-31 22:16:49 +02:00
|
|
|
NSAssert(NO, @"Invalid message: %@", messageData);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)onClose {
|
|
|
|
[self displayLogMessage:@"GAE onClose."];
|
2014-03-10 21:41:22 +01:00
|
|
|
[self closeVideoUI];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)onError:(int)code withDescription:(NSString*)description {
|
2014-03-31 22:16:49 +02:00
|
|
|
[self displayLogMessage:[NSString stringWithFormat:@"GAE onError: %d, %@",
|
|
|
|
code, description]];
|
2014-03-10 21:41:22 +01:00
|
|
|
[self closeVideoUI];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-04-21 20:47:24 +02:00
|
|
|
#pragma mark - RTCSessionDescriptionDelegate methods
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2013-09-05 23:49:58 +02:00
|
|
|
// Match |pattern| to |string| and return the first group of the first
|
|
|
|
// match, or nil if no match was found.
|
2014-03-25 01:11:56 +01:00
|
|
|
+ (NSString*)firstMatch:(NSRegularExpression*)pattern
|
|
|
|
withString:(NSString*)string {
|
2013-09-05 23:49:58 +02:00
|
|
|
NSTextCheckingResult* result =
|
2014-03-25 01:11:56 +01:00
|
|
|
[pattern firstMatchInString:string
|
|
|
|
options:0
|
|
|
|
range:NSMakeRange(0, [string length])];
|
2013-09-05 23:49:58 +02:00
|
|
|
if (!result)
|
|
|
|
return nil;
|
|
|
|
return [string substringWithRange:[result rangeAtIndex:1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mangle |origSDP| to prefer the ISAC/16k audio codec.
|
2014-03-25 01:11:56 +01:00
|
|
|
+ (NSString*)preferISAC:(NSString*)origSDP {
|
2013-09-05 23:49:58 +02:00
|
|
|
int mLineIndex = -1;
|
|
|
|
NSString* isac16kRtpMap = nil;
|
|
|
|
NSArray* lines = [origSDP componentsSeparatedByString:@"\n"];
|
|
|
|
NSRegularExpression* isac16kRegex = [NSRegularExpression
|
|
|
|
regularExpressionWithPattern:@"^a=rtpmap:(\\d+) ISAC/16000[\r]?$"
|
|
|
|
options:0
|
|
|
|
error:nil];
|
|
|
|
for (int i = 0;
|
|
|
|
(i < [lines count]) && (mLineIndex == -1 || isac16kRtpMap == nil);
|
|
|
|
++i) {
|
|
|
|
NSString* line = [lines objectAtIndex:i];
|
|
|
|
if ([line hasPrefix:@"m=audio "]) {
|
|
|
|
mLineIndex = i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
isac16kRtpMap = [self firstMatch:isac16kRegex withString:line];
|
|
|
|
}
|
|
|
|
if (mLineIndex == -1) {
|
|
|
|
NSLog(@"No m=audio line, so can't prefer iSAC");
|
|
|
|
return origSDP;
|
|
|
|
}
|
|
|
|
if (isac16kRtpMap == nil) {
|
|
|
|
NSLog(@"No ISAC/16000 line, so can't prefer iSAC");
|
|
|
|
return origSDP;
|
|
|
|
}
|
|
|
|
NSArray* origMLineParts =
|
|
|
|
[[lines objectAtIndex:mLineIndex] componentsSeparatedByString:@" "];
|
|
|
|
NSMutableArray* newMLine =
|
|
|
|
[NSMutableArray arrayWithCapacity:[origMLineParts count]];
|
|
|
|
int origPartIndex = 0;
|
|
|
|
// Format is: m=<media> <port> <proto> <fmt> ...
|
|
|
|
[newMLine addObject:[origMLineParts objectAtIndex:origPartIndex++]];
|
|
|
|
[newMLine addObject:[origMLineParts objectAtIndex:origPartIndex++]];
|
|
|
|
[newMLine addObject:[origMLineParts objectAtIndex:origPartIndex++]];
|
|
|
|
[newMLine addObject:isac16kRtpMap];
|
|
|
|
for (; origPartIndex < [origMLineParts count]; ++origPartIndex) {
|
2014-03-31 22:16:49 +02:00
|
|
|
if (![isac16kRtpMap
|
|
|
|
isEqualToString:[origMLineParts objectAtIndex:origPartIndex]]) {
|
2013-09-05 23:49:58 +02:00
|
|
|
[newMLine addObject:[origMLineParts objectAtIndex:origPartIndex]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NSMutableArray* newLines = [NSMutableArray arrayWithCapacity:[lines count]];
|
|
|
|
[newLines addObjectsFromArray:lines];
|
|
|
|
[newLines replaceObjectAtIndex:mLineIndex
|
|
|
|
withObject:[newMLine componentsJoinedByString:@" "]];
|
|
|
|
return [newLines componentsJoinedByString:@"\n"];
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
|
|
|
didCreateSessionDescription:(RTCSessionDescription*)origSdp
|
|
|
|
error:(NSError*)error {
|
2013-07-10 02:45:36 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
2014-03-31 22:16:49 +02:00
|
|
|
if (error) {
|
|
|
|
[self displayLogMessage:@"SDP onFailure."];
|
|
|
|
NSAssert(NO, error.description);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[self displayLogMessage:@"SDP onSuccess(SDP) - set local description."];
|
|
|
|
RTCSessionDescription* sdp = [[RTCSessionDescription alloc]
|
|
|
|
initWithType:origSdp.type
|
|
|
|
sdp:[APPRTCAppDelegate preferISAC:origSdp.description]];
|
|
|
|
[self.peerConnection setLocalDescriptionWithDelegate:self
|
|
|
|
sessionDescription:sdp];
|
|
|
|
|
|
|
|
[self displayLogMessage:@"PC setLocalDescription."];
|
2014-03-25 01:11:56 +01:00
|
|
|
NSDictionary* json = @{@"type" : sdp.type, @"sdp" : sdp.description};
|
|
|
|
NSError* error;
|
|
|
|
NSData* data =
|
|
|
|
[NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
|
|
|
|
NSAssert(!error,
|
|
|
|
@"%@",
|
|
|
|
[NSString stringWithFormat:@"Error: %@", error.description]);
|
|
|
|
[self sendData:data];
|
2013-07-10 02:45:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
|
|
|
didSetSessionDescriptionWithError:(NSError*)error {
|
2013-07-10 02:45:36 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
2014-03-31 22:16:49 +02:00
|
|
|
if (error) {
|
|
|
|
[self displayLogMessage:@"SDP onFailure."];
|
|
|
|
NSAssert(NO, error.description);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self displayLogMessage:@"SDP onSuccess() - possibly drain candidates"];
|
2014-03-25 01:11:56 +01:00
|
|
|
if (!self.client.initiator) {
|
|
|
|
if (self.peerConnection.remoteDescription &&
|
|
|
|
!self.peerConnection.localDescription) {
|
|
|
|
[self displayLogMessage:@"Callee, setRemoteDescription succeeded"];
|
|
|
|
RTCPair* audio = [[RTCPair alloc] initWithKey:@"OfferToReceiveAudio"
|
|
|
|
value:@"true"];
|
|
|
|
RTCPair* video = [[RTCPair alloc] initWithKey:@"OfferToReceiveVideo"
|
|
|
|
value:@"true"];
|
|
|
|
NSArray* mandatory = @[ audio, video ];
|
|
|
|
RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc]
|
|
|
|
initWithMandatoryConstraints:mandatory
|
|
|
|
optionalConstraints:nil];
|
|
|
|
[self.peerConnection createAnswerWithDelegate:self
|
|
|
|
constraints:constraints];
|
|
|
|
[self displayLogMessage:@"PC - createAnswer."];
|
2014-02-10 19:47:11 +01:00
|
|
|
} else {
|
2014-03-25 01:11:56 +01:00
|
|
|
[self displayLogMessage:@"SDP onSuccess - drain candidates"];
|
|
|
|
[self drainRemoteCandidates];
|
2014-02-10 19:47:11 +01:00
|
|
|
}
|
2014-03-25 01:11:56 +01:00
|
|
|
} else {
|
2014-02-10 19:47:11 +01:00
|
|
|
if (self.peerConnection.remoteDescription) {
|
2014-03-25 01:11:56 +01:00
|
|
|
[self displayLogMessage:@"SDP onSuccess - drain candidates"];
|
|
|
|
[self drainRemoteCandidates];
|
2014-02-10 19:47:11 +01:00
|
|
|
}
|
2014-03-25 01:11:56 +01:00
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-04-22 23:05:38 +02:00
|
|
|
#pragma mark - RTCStatsDelegate methods
|
|
|
|
|
|
|
|
- (void)peerConnection:(RTCPeerConnection*)peerConnection
|
|
|
|
didGetStats:(NSArray*)stats {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
NSString* message = [NSString stringWithFormat:@"Stats:\n %@", stats];
|
|
|
|
[self displayLogMessage:message];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
#pragma mark - internal methods
|
|
|
|
|
|
|
|
- (void)disconnect {
|
|
|
|
[self.client
|
|
|
|
sendData:[@"{\"type\": \"bye\"}" dataUsingEncoding:NSUTF8StringEncoding]];
|
2014-03-10 21:41:22 +01:00
|
|
|
[self.peerConnection close];
|
2013-07-10 02:45:36 +02:00
|
|
|
self.peerConnection = nil;
|
|
|
|
self.pcObserver = nil;
|
|
|
|
self.client = nil;
|
2014-03-25 06:16:29 +01:00
|
|
|
self.videoSource = nil;
|
|
|
|
self.peerConnectionFactory = nil;
|
2013-08-01 20:29:45 +02:00
|
|
|
[RTCPeerConnectionFactory deinitializeSSL];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drainRemoteCandidates {
|
2014-03-25 01:11:56 +01:00
|
|
|
for (RTCICECandidate* candidate in self.queuedRemoteCandidates) {
|
2013-08-01 20:29:45 +02:00
|
|
|
[self.peerConnection addICECandidate:candidate];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
self.queuedRemoteCandidates = nil;
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (NSString*)unHTMLifyString:(NSString*)base {
|
2013-07-10 02:45:36 +02:00
|
|
|
// TODO(hughv): Investigate why percent escapes are being added. Removing
|
|
|
|
// them isn't necessary on Android.
|
|
|
|
// convert HTML escaped characters to UTF8.
|
2014-03-25 01:11:56 +01:00
|
|
|
NSString* removePercent =
|
2013-07-10 02:45:36 +02:00
|
|
|
[base stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
// remove leading and trailing ".
|
|
|
|
NSRange range;
|
|
|
|
range.length = [removePercent length] - 2;
|
|
|
|
range.location = 1;
|
2014-03-25 01:11:56 +01:00
|
|
|
NSString* removeQuotes = [removePercent substringWithRange:range];
|
2013-07-10 02:45:36 +02:00
|
|
|
// convert \" to ".
|
2014-03-25 01:11:56 +01:00
|
|
|
NSString* removeEscapedQuotes =
|
2013-07-10 02:45:36 +02:00
|
|
|
[removeQuotes stringByReplacingOccurrencesOfString:@"\\\""
|
|
|
|
withString:@"\""];
|
|
|
|
// convert \\ to \.
|
2014-03-25 01:11:56 +01:00
|
|
|
NSString* removeBackslash =
|
2013-07-10 02:45:36 +02:00
|
|
|
[removeEscapedQuotes stringByReplacingOccurrencesOfString:@"\\\\"
|
|
|
|
withString:@"\\"];
|
|
|
|
return removeBackslash;
|
|
|
|
}
|
|
|
|
|
2014-04-22 23:05:38 +02:00
|
|
|
- (void)didFireStatsTimer:(NSTimer *)timer {
|
|
|
|
if (self.peerConnection) {
|
|
|
|
[self.peerConnection getStatsWithDelegate:self
|
|
|
|
mediaStreamTrack:nil
|
|
|
|
statsOutputLevel:RTCStatsOutputLevelDebug];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-10 21:41:22 +01:00
|
|
|
#pragma mark - public methods
|
|
|
|
|
|
|
|
- (void)closeVideoUI {
|
2014-03-25 01:11:56 +01:00
|
|
|
[self.viewController resetUI];
|
2014-03-25 06:16:29 +01:00
|
|
|
[self disconnect];
|
2014-03-10 21:41:22 +01:00
|
|
|
}
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
@end
|