Implement mac version of AppRTCDemo.
- Refactored and moved AppRTCDemo to support sharing AppRTC connection code between iOS and mac counterparts.
- Refactored OpenGL rendering code to be shared between iOS and mac counterparts.
- iOS AppRTCDemo now respects video aspect ratio.
BUG=2168
R=fischman@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/17589004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6291 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCAppDelegate.h b/talk/examples/ios/AppRTCDemo/APPRTCAppDelegate.h
deleted file mode 100644
index dd810f7..0000000
--- a/talk/examples/ios/AppRTCDemo/APPRTCAppDelegate.h
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * 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.
- */
-
-#import <UIKit/UIKit.h>
-
-#import "GAEChannelClient.h"
-#import "APPRTCAppClient.h"
-#import "RTCSessionDescriptionDelegate.h"
-#import "RTCVideoSource.h"
-// Used to send a message to an apprtc.appspot.com "room".
-@protocol APPRTCSendMessage<NSObject>
-
-- (void)sendData:(NSData*)data;
-// Logging helper.
-- (void)displayLogMessage:(NSString*)message;
-@end
-
-@class APPRTCViewController;
-@class RTCVideoTrack;
-
-// The main application class of the AppRTCDemo iOS app demonstrating
-// interoperability between the Objective C implementation of PeerConnection
-// and the apprtc.appspot.com demo webapp.
-@interface APPRTCAppDelegate : UIResponder<ICEServerDelegate,
- GAEMessageHandler,
- APPRTCSendMessage,
- RTCSessionDescriptionDelegate,
- UIApplicationDelegate>
-
-@property(strong, nonatomic) UIWindow* window;
-@property(strong, nonatomic) APPRTCViewController* viewController;
-@property (strong, nonatomic) RTCVideoSource* videoSource;
-
-- (void)closeVideoUI;
-
-@end
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCAppDelegate.m b/talk/examples/ios/AppRTCDemo/APPRTCAppDelegate.m
deleted file mode 100644
index 87d1f53..0000000
--- a/talk/examples/ios/AppRTCDemo/APPRTCAppDelegate.m
+++ /dev/null
@@ -1,575 +0,0 @@
-/*
- * 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.
- */
-
-#import <AVFoundation/AVFoundation.h>
-
-#import "APPRTCAppDelegate.h"
-
-#import "APPRTCViewController.h"
-#import "RTCEAGLVideoView.h"
-#import "RTCICECandidate.h"
-#import "RTCICEServer.h"
-#import "RTCMediaConstraints.h"
-#import "RTCMediaStream.h"
-#import "RTCPair.h"
-#import "RTCPeerConnection.h"
-#import "RTCPeerConnectionDelegate.h"
-#import "RTCPeerConnectionFactory.h"
-#import "RTCSessionDescription.h"
-#import "RTCStatsDelegate.h"
-#import "RTCVideoRenderer.h"
-#import "RTCVideoCapturer.h"
-#import "RTCVideoTrack.h"
-
-@interface PCObserver : NSObject<RTCPeerConnectionDelegate>
-
-- (id)initWithDelegate:(id<APPRTCSendMessage>)delegate;
-
-@property(nonatomic, strong) RTCEAGLVideoView* videoView;
-
-@end
-
-@implementation PCObserver {
- id<APPRTCSendMessage> _delegate;
-}
-
-- (id)initWithDelegate:(id<APPRTCSendMessage>)delegate {
- if (self = [super init]) {
- _delegate = delegate;
- }
- return self;
-}
-
-#pragma mark - RTCPeerConnectionDelegate
-
-- (void)peerConnectionOnError:(RTCPeerConnection*)peerConnection {
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- NSLog(@"PCO onError.");
- NSAssert(NO, @"PeerConnection failed.");
- });
-}
-
-- (void)peerConnection:(RTCPeerConnection*)peerConnection
- signalingStateChanged:(RTCSignalingState)stateChanged {
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- NSLog(@"PCO onSignalingStateChange: %d", stateChanged);
- });
-}
-
-- (void)peerConnection:(RTCPeerConnection*)peerConnection
- addedStream:(RTCMediaStream*)stream {
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- NSLog(@"PCO onAddStream.");
- NSAssert([stream.audioTracks count] <= 1,
- @"Expected at most 1 audio stream");
- NSAssert([stream.videoTracks count] <= 1,
- @"Expected at most 1 video stream");
- if ([stream.videoTracks count] != 0) {
- self.videoView.videoTrack = stream.videoTracks[0];
- }
- });
-}
-
-- (void)peerConnection:(RTCPeerConnection*)peerConnection
- removedStream:(RTCMediaStream*)stream {
- dispatch_async(dispatch_get_main_queue(),
- ^(void) { NSLog(@"PCO onRemoveStream."); });
-}
-
-- (void)peerConnectionOnRenegotiationNeeded:(RTCPeerConnection*)peerConnection {
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- NSLog(@"PCO onRenegotiationNeeded - ignoring because AppRTC has a "
- "predefined negotiation strategy");
- });
-}
-
-- (void)peerConnection:(RTCPeerConnection*)peerConnection
- gotICECandidate:(RTCICECandidate*)candidate {
- 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);
- }
- });
-}
-
-- (void)peerConnection:(RTCPeerConnection*)peerConnection
- iceGatheringChanged:(RTCICEGatheringState)newState {
- dispatch_async(dispatch_get_main_queue(),
- ^(void) { NSLog(@"PCO onIceGatheringChange. %d", newState); });
-}
-
-- (void)peerConnection:(RTCPeerConnection*)peerConnection
- iceConnectionChanged:(RTCICEConnectionState)newState {
- 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!");
- });
-}
-
-- (void)peerConnection:(RTCPeerConnection*)peerConnection
- didOpenDataChannel:(RTCDataChannel*)dataChannel {
- NSAssert(NO, @"AppRTC doesn't use DataChannels");
-}
-
-#pragma mark - Private
-
-- (void)displayLogMessage:(NSString*)message {
- [_delegate displayLogMessage:message];
-}
-
-@end
-
-@interface APPRTCAppDelegate () <RTCStatsDelegate>
-
-@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;
-
-@end
-
-@implementation APPRTCAppDelegate {
- NSTimer* _statsTimer;
-}
-
-#pragma mark - UIApplicationDelegate methods
-
-- (BOOL)application:(UIApplication*)application
- didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
- [RTCPeerConnectionFactory initializeSSL];
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- self.viewController =
- [[APPRTCViewController alloc] initWithNibName:@"APPRTCViewController"
- bundle:nil];
- self.window.rootViewController = self.viewController;
- _statsTimer =
- [NSTimer scheduledTimerWithTimeInterval:10
- target:self
- selector:@selector(didFireStatsTimer:)
- userInfo:nil
- repeats:YES];
- [self.window makeKeyAndVisible];
- return YES;
-}
-
-- (void)applicationWillResignActive:(UIApplication*)application {
- [self displayLogMessage:@"Application lost focus, connection broken."];
- [self closeVideoUI];
-}
-
-- (void)applicationDidEnterBackground:(UIApplication*)application {
-}
-
-- (void)applicationWillEnterForeground:(UIApplication*)application {
-}
-
-- (void)applicationDidBecomeActive:(UIApplication*)application {
-}
-
-- (void)applicationWillTerminate:(UIApplication*)application {
-}
-
-- (BOOL)application:(UIApplication*)application
- openURL:(NSURL*)url
- sourceApplication:(NSString*)sourceApplication
- annotation:(id)annotation {
- if (self.client) {
- return NO;
- }
- self.client = [[APPRTCAppClient alloc] initWithICEServerDelegate:self
- messageHandler:self];
- [self.client connectToRoom:url];
- return YES;
-}
-
-- (void)displayLogMessage:(NSString*)message {
- NSAssert([NSThread isMainThread], @"Called off main thread!");
- NSLog(@"%@", message);
- [self.viewController displayText:message];
-}
-
-#pragma mark - RTCSendMessage method
-
-- (void)sendData:(NSData*)data {
- [self.client sendData:data];
-}
-
-#pragma mark - ICEServerDelegate method
-
-- (void)onICEServers:(NSArray*)servers {
- self.queuedRemoteCandidates = [NSMutableArray array];
- self.peerConnectionFactory = [[RTCPeerConnectionFactory alloc] init];
- 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"]
- ]];
- self.pcObserver = [[PCObserver alloc] initWithDelegate:self];
- self.peerConnection =
- [self.peerConnectionFactory peerConnectionWithICEServers:servers
- constraints:constraints
- delegate:self.pcObserver];
- RTCMediaStream* lms =
- [self.peerConnectionFactory mediaStreamWithLabel:@"ARDAMS"];
-
- // 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
- NSString* cameraID = nil;
- for (AVCaptureDevice* captureDevice in
- [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
- if (captureDevice.position == AVCaptureDevicePositionFront) {
- cameraID = [captureDevice localizedName];
- break;
- }
- }
- NSAssert(cameraID, @"Unable to get the front camera id");
-
- RTCVideoCapturer* capturer =
- [RTCVideoCapturer capturerWithDeviceName:cameraID];
- self.videoSource = [self.peerConnectionFactory
- videoSourceWithCapturer:capturer
- constraints:self.client.videoConstraints];
- localVideoTrack =
- [self.peerConnectionFactory videoTrackWithID:@"ARDAMSv0"
- source:self.videoSource];
- if (localVideoTrack) {
- [lms addVideoTrack:localVideoTrack];
- }
- self.viewController.localVideoView.videoTrack = localVideoTrack;
-#else
- self.viewController.localVideoView.hidden = YES;
-#endif
-
- self.pcObserver.videoView = self.viewController.remoteVideoView;
- [lms addAudioTrack:[self.peerConnectionFactory audioTrackWithID:@"ARDAMSa0"]];
- [self.peerConnection addStream:lms constraints:constraints];
- [self displayLogMessage:@"onICEServers - added local stream."];
-}
-
-#pragma mark - GAEMessageHandler methods
-
-- (void)onOpen {
- if (!self.client.initiator) {
- [self displayLogMessage:@"Callee; waiting for remote offer"];
- return;
- }
- [self displayLogMessage:@"GAE onOpen - create offer."];
- 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 createOfferWithDelegate:self constraints:constraints];
- [self displayLogMessage:@"PC - createOffer."];
-}
-
-- (void)onMessage:(NSDictionary*)messageData {
- NSString* type = messageData[@"type"];
- NSAssert(type, @"Missing type: %@", messageData);
- [self displayLogMessage:[NSString stringWithFormat:@"GAE onMessage type - %@",
- type]];
- if ([type isEqualToString:@"candidate"]) {
- NSString* mid = messageData[@"id"];
- NSNumber* sdpLineIndex = messageData[@"label"];
- NSString* sdp = messageData[@"candidate"];
- RTCICECandidate* candidate =
- [[RTCICECandidate alloc] initWithMid:mid
- index:sdpLineIndex.intValue
- sdp:sdp];
- if (self.queuedRemoteCandidates) {
- [self.queuedRemoteCandidates addObject:candidate];
- } else {
- [self.peerConnection addICECandidate:candidate];
- }
- } else if ([type isEqualToString:@"offer"] ||
- [type isEqualToString:@"answer"]) {
- NSString* sdpString = messageData[@"sdp"];
- RTCSessionDescription* sdp = [[RTCSessionDescription alloc]
- initWithType:type
- sdp:[APPRTCAppDelegate preferISAC:sdpString]];
- [self.peerConnection setRemoteDescriptionWithDelegate:self
- sessionDescription:sdp];
- [self displayLogMessage:@"PC - setRemoteDescription."];
- } else if ([type isEqualToString:@"bye"]) {
- [self closeVideoUI];
- UIAlertView* alertView =
- [[UIAlertView alloc] initWithTitle:@"Remote end hung up"
- message:@"dropping PeerConnection"
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- } else {
- NSAssert(NO, @"Invalid message: %@", messageData);
- }
-}
-
-- (void)onClose {
- [self displayLogMessage:@"GAE onClose."];
- [self closeVideoUI];
-}
-
-- (void)onError:(int)code withDescription:(NSString*)description {
- [self displayLogMessage:[NSString stringWithFormat:@"GAE onError: %d, %@",
- code, description]];
- [self closeVideoUI];
-}
-
-#pragma mark - RTCSessionDescriptionDelegate methods
-
-// Match |pattern| to |string| and return the first group of the first
-// match, or nil if no match was found.
-+ (NSString*)firstMatch:(NSRegularExpression*)pattern
- withString:(NSString*)string {
- NSTextCheckingResult* result =
- [pattern firstMatchInString:string
- options:0
- range:NSMakeRange(0, [string length])];
- if (!result)
- return nil;
- return [string substringWithRange:[result rangeAtIndex:1]];
-}
-
-// Mangle |origSDP| to prefer the ISAC/16k audio codec.
-+ (NSString*)preferISAC:(NSString*)origSDP {
- 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) {
- if (![isac16kRtpMap
- isEqualToString:[origMLineParts objectAtIndex:origPartIndex]]) {
- [newMLine addObject:[origMLineParts objectAtIndex:origPartIndex]];
- }
- }
- NSMutableArray* newLines = [NSMutableArray arrayWithCapacity:[lines count]];
- [newLines addObjectsFromArray:lines];
- [newLines replaceObjectAtIndex:mLineIndex
- withObject:[newMLine componentsJoinedByString:@" "]];
- return [newLines componentsJoinedByString:@"\n"];
-}
-
-- (void)peerConnection:(RTCPeerConnection*)peerConnection
- didCreateSessionDescription:(RTCSessionDescription*)origSdp
- error:(NSError*)error {
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- 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."];
- 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];
- });
-}
-
-- (void)peerConnection:(RTCPeerConnection*)peerConnection
- didSetSessionDescriptionWithError:(NSError*)error {
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- if (error) {
- [self displayLogMessage:@"SDP onFailure."];
- NSAssert(NO, error.description);
- return;
- }
-
- [self displayLogMessage:@"SDP onSuccess() - possibly drain candidates"];
- 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."];
- } else {
- [self displayLogMessage:@"SDP onSuccess - drain candidates"];
- [self drainRemoteCandidates];
- }
- } else {
- if (self.peerConnection.remoteDescription) {
- [self displayLogMessage:@"SDP onSuccess - drain candidates"];
- [self drainRemoteCandidates];
- }
- }
- });
-}
-
-#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];
- });
-}
-
-#pragma mark - internal methods
-
-- (void)disconnect {
- [self.client
- sendData:[@"{\"type\": \"bye\"}" dataUsingEncoding:NSUTF8StringEncoding]];
- [self.peerConnection close];
- self.peerConnection = nil;
- self.pcObserver = nil;
- self.client = nil;
- self.videoSource = nil;
- self.peerConnectionFactory = nil;
- [RTCPeerConnectionFactory deinitializeSSL];
-}
-
-- (void)drainRemoteCandidates {
- for (RTCICECandidate* candidate in self.queuedRemoteCandidates) {
- [self.peerConnection addICECandidate:candidate];
- }
- self.queuedRemoteCandidates = nil;
-}
-
-- (NSString*)unHTMLifyString:(NSString*)base {
- // TODO(hughv): Investigate why percent escapes are being added. Removing
- // them isn't necessary on Android.
- // convert HTML escaped characters to UTF8.
- NSString* removePercent =
- [base stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- // remove leading and trailing ".
- NSRange range;
- range.length = [removePercent length] - 2;
- range.location = 1;
- NSString* removeQuotes = [removePercent substringWithRange:range];
- // convert \" to ".
- NSString* removeEscapedQuotes =
- [removeQuotes stringByReplacingOccurrencesOfString:@"\\\""
- withString:@"\""];
- // convert \\ to \.
- NSString* removeBackslash =
- [removeEscapedQuotes stringByReplacingOccurrencesOfString:@"\\\\"
- withString:@"\\"];
- return removeBackslash;
-}
-
-- (void)didFireStatsTimer:(NSTimer *)timer {
- if (self.peerConnection) {
- [self.peerConnection getStatsWithDelegate:self
- mediaStreamTrack:nil
- statsOutputLevel:RTCStatsOutputLevelDebug];
- }
-}
-
-#pragma mark - public methods
-
-- (void)closeVideoUI {
- [self.viewController resetUI];
- [self disconnect];
-}
-
-@end
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCViewController.m b/talk/examples/ios/AppRTCDemo/APPRTCViewController.m
deleted file mode 100644
index bdd8b50..0000000
--- a/talk/examples/ios/AppRTCDemo/APPRTCViewController.m
+++ /dev/null
@@ -1,150 +0,0 @@
-/*
- * 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.
- */
-
-#import "APPRTCViewController.h"
-
-#import <AVFoundation/AVFoundation.h>
-#import "RTCEAGLVideoView.h"
-
-@interface APPRTCViewController ()
-@property(nonatomic, assign) UIInterfaceOrientation statusBarOrientation;
-@end
-
-@implementation APPRTCViewController
-
-- (void)viewDidLoad {
- [super viewDidLoad];
- self.statusBarOrientation =
- [UIApplication sharedApplication].statusBarOrientation;
- self.roomInput.delegate = self;
- [self.roomInput becomeFirstResponder];
-}
-
-- (void)viewDidLayoutSubviews {
- if (self.statusBarOrientation !=
- [UIApplication sharedApplication].statusBarOrientation) {
- self.statusBarOrientation =
- [UIApplication sharedApplication].statusBarOrientation;
- [[NSNotificationCenter defaultCenter]
- postNotificationName:@"StatusBarOrientationDidChange"
- object:nil];
- }
-}
-
-- (void)displayText:(NSString*)text {
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- NSString* output =
- [NSString stringWithFormat:@"%@\n%@", self.logView.text, text];
- self.logView.text = output;
- [self.logView
- scrollRangeToVisible:NSMakeRange([self.logView.text length], 0)];
- });
-}
-
-- (void)resetUI {
- [self.roomInput resignFirstResponder];
- self.roomInput.text = nil;
- self.roomInput.hidden = NO;
- self.instructionsView.hidden = NO;
- self.logView.hidden = YES;
- self.logView.text = nil;
- self.blackView.hidden = YES;
-
- [self.remoteVideoView removeFromSuperview];
- self.remoteVideoView = nil;
-
- [self.localVideoView removeFromSuperview];
- self.localVideoView = nil;
-}
-
-// TODO(fischman): Use video dimensions from the incoming video stream
-// and resize the Video View accordingly w.r.t. aspect ratio.
-enum {
- // Remote video view dimensions.
- kRemoteVideoWidth = 640,
- kRemoteVideoHeight = 480,
- // Padding space for local video view with its parent.
- kLocalViewPadding = 20
-};
-
-- (void)setupCaptureSession {
- self.blackView.hidden = NO;
-
- CGSize videoSize =
- CGSizeMake(kRemoteVideoWidth, kRemoteVideoHeight);
- CGRect remoteVideoFrame =
- AVMakeRectWithAspectRatioInsideRect(videoSize,
- self.blackView.bounds);
- CGRect localVideoFrame = remoteVideoFrame;
- // TODO(tkchin): use video dimensions from incoming video stream
- // and handle rotation.
- localVideoFrame.size.width = remoteVideoFrame.size.height / 4;
- localVideoFrame.size.height = remoteVideoFrame.size.width / 4;
- localVideoFrame.origin.x = CGRectGetMaxX(remoteVideoFrame)
- - localVideoFrame.size.width - kLocalViewPadding;
- localVideoFrame.origin.y = CGRectGetMaxY(remoteVideoFrame)
- - localVideoFrame.size.height - kLocalViewPadding;
-
- self.remoteVideoView =
- [[RTCEAGLVideoView alloc] initWithFrame:remoteVideoFrame];
- [self.blackView addSubview:self.remoteVideoView];
- self.remoteVideoView.transform = CGAffineTransformMakeScale(-1, 1);
-
- self.localVideoView =
- [[RTCEAGLVideoView alloc] initWithFrame:localVideoFrame];
- [self.blackView addSubview:self.localVideoView];
-}
-
-#pragma mark - UITextFieldDelegate
-
-- (void)textFieldDidEndEditing:(UITextField*)textField {
- NSString* room = textField.text;
- if ([room length] == 0) {
- return;
- }
- textField.hidden = YES;
- self.instructionsView.hidden = YES;
- self.logView.hidden = NO;
- // TODO(hughv): Instead of launching a URL with apprtc scheme, change to
- // prepopulating the textField with a valid URL missing the room. This allows
- // the user to have the simplicity of just entering the room or the ability to
- // override to a custom appspot instance. Remove apprtc:// when this is done.
- NSString* url =
- [NSString stringWithFormat:@"apprtc://apprtc.appspot.com/?r=%@", room];
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
-
- dispatch_async(dispatch_get_main_queue(), ^{ [self setupCaptureSession]; });
-}
-
-- (BOOL)textFieldShouldReturn:(UITextField*)textField {
- // There is no other control that can take focus, so manually resign focus
- // when return (Join) is pressed to trigger |textFieldDidEndEditing|.
- [textField resignFirstResponder];
- return YES;
-}
-
-@end
diff --git a/talk/examples/ios/README b/talk/examples/ios/README
deleted file mode 100644
index 9c0d134..0000000
--- a/talk/examples/ios/README
+++ /dev/null
@@ -1,3 +0,0 @@
-This directory contains an example iOS client for http://apprtc.appspot.com
-
-See ../../app/webrtc/objc/README for information on how to use it.
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCAppClient.h b/talk/examples/objc/AppRTCDemo/APPRTCAppClient.h
similarity index 84%
rename from talk/examples/ios/AppRTCDemo/APPRTCAppClient.h
rename to talk/examples/objc/AppRTCDemo/APPRTCAppClient.h
index 41a795e..b69b517 100644
--- a/talk/examples/ios/AppRTCDemo/APPRTCAppClient.h
+++ b/talk/examples/objc/AppRTCDemo/APPRTCAppClient.h
@@ -29,10 +29,13 @@
#import "GAEChannelClient.h"
-// Called when there are RTCICEServers.
-@protocol ICEServerDelegate<NSObject>
+@class APPRTCAppClient;
+@protocol APPRTCAppClientDelegate
-- (void)onICEServers:(NSArray*)servers;
+- (void)appClient:(APPRTCAppClient*)appClient
+ didErrorWithMessage:(NSString*)message;
+- (void)appClient:(APPRTCAppClient*)appClient
+ didReceiveICEServers:(NSArray*)servers;
@end
@@ -47,13 +50,12 @@
// for the registered handler to be called with received messages.
@interface APPRTCAppClient : NSObject<NSURLConnectionDataDelegate>
-@property(nonatomic, weak, readonly) id<ICEServerDelegate> ICEServerDelegate;
-@property(nonatomic, weak, readonly) id<GAEMessageHandler> messageHandler;
-@property(nonatomic, assign) BOOL initiator;
+@property(nonatomic) BOOL initiator;
@property(nonatomic, copy, readonly) RTCMediaConstraints* videoConstraints;
+@property(nonatomic, weak) id<APPRTCAppClientDelegate> delegate;
-- (id)initWithICEServerDelegate:(id<ICEServerDelegate>)delegate
- messageHandler:(id<GAEMessageHandler>)handler;
+- (id)initWithDelegate:(id<APPRTCAppClientDelegate>)delegate
+ messageHandler:(id<GAEMessageHandler>)handler;
- (void)connectToRoom:(NSURL*)room;
- (void)sendData:(NSData*)data;
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCAppClient.m b/talk/examples/objc/AppRTCDemo/APPRTCAppClient.m
similarity index 83%
rename from talk/examples/ios/AppRTCDemo/APPRTCAppClient.m
rename to talk/examples/objc/AppRTCDemo/APPRTCAppClient.m
index 9ef0a7a..c412aef 100644
--- a/talk/examples/ios/AppRTCDemo/APPRTCAppClient.m
+++ b/talk/examples/objc/AppRTCDemo/APPRTCAppClient.m
@@ -25,18 +25,22 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+
#import "APPRTCAppClient.h"
#import <dispatch/dispatch.h>
#import "GAEChannelClient.h"
#import "RTCICEServer.h"
-#import "APPRTCAppDelegate.h"
#import "RTCMediaConstraints.h"
+#import "RTCPair.h"
@interface APPRTCAppClient ()
-@property(nonatomic, strong) dispatch_queue_t backgroundQueue;
+@property(nonatomic, weak, readonly) id<GAEMessageHandler> messageHandler;
@property(nonatomic, copy) NSString* baseURL;
@property(nonatomic, strong) GAEChannelClient* gaeChannel;
@property(nonatomic, copy) NSString* postMessageUrl;
@@ -49,12 +53,14 @@
@end
-@implementation APPRTCAppClient
+@implementation APPRTCAppClient {
+ dispatch_queue_t _backgroundQueue;
+}
-- (id)initWithICEServerDelegate:(id<ICEServerDelegate>)delegate
- messageHandler:(id<GAEMessageHandler>)handler {
+- (id)initWithDelegate:(id<APPRTCAppClientDelegate>)delegate
+ messageHandler:(id<GAEMessageHandler>)handler {
if (self = [super init]) {
- _ICEServerDelegate = delegate;
+ _delegate = delegate;
_messageHandler = handler;
_backgroundQueue = dispatch_queue_create("RTCBackgroundQueue",
DISPATCH_QUEUE_SERIAL);
@@ -68,14 +74,15 @@
#pragma mark - Public methods
- (void)connectToRoom:(NSURL*)url {
- NSURLRequest* request = [self getRequestFromUrl:url];
+ self.roomHtml = [NSMutableString stringWithCapacity:20000];
+ NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)sendData:(NSData*)data {
[self maybeLogMessage:@"Send message"];
- dispatch_async(self.backgroundQueue, ^{
+ dispatch_async(_backgroundQueue, ^{
[self.sendQueue addObject:[data copy]];
if ([self.postMessageUrl length] < 1) {
@@ -109,10 +116,10 @@
NSArray* matches =
[regexp matchesInString:self.roomHtml options:0 range:fullRange];
if ([matches count] != 1) {
- [self showMessage:[NSString stringWithFormat:@"%d matches for %@ in %@",
- [matches count],
- name,
- self.roomHtml]];
+ NSString* format = @"%lu matches for %@ in %@";
+ NSString* message = [NSString stringWithFormat:format,
+ (unsigned long)[matches count], name, self.roomHtml];
+ [self.delegate appClient:self didErrorWithMessage:message];
return nil;
}
NSRange matchRange = [matches[0] rangeAtIndex:1];
@@ -130,15 +137,6 @@
return value;
}
-- (NSURLRequest*)getRequestFromUrl:(NSURL*)url {
- self.roomHtml = [NSMutableString stringWithCapacity:20000];
- NSString* path =
- [NSString stringWithFormat:@"https:%@", [url resourceSpecifier]];
- NSURLRequest* request =
- [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
- return request;
-}
-
- (void)maybeLogMessage:(NSString*)message {
if (self.verboseLogging) {
NSLog(@"%@", message);
@@ -164,23 +162,13 @@
[NSString stringWithUTF8String:[responseData bytes]]);
}
-- (void)showMessage:(NSString*)message {
- NSLog(@"%@", message);
- UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Unable to join"
- message:message
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
-}
-
- (void)updateICEServers:(NSMutableArray*)ICEServers
withTurnServer:(NSString*)turnServerUrl {
if ([turnServerUrl length] < 1) {
- [self.ICEServerDelegate onICEServers:ICEServers];
+ [self.delegate appClient:self didReceiveICEServers:ICEServers];
return;
}
- dispatch_async(self.backgroundQueue, ^(void) {
+ dispatch_async(_backgroundQueue, ^(void) {
NSMutableURLRequest* request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:turnServerUrl]];
[request addValue:@"Mozilla/5.0" forHTTPHeaderField:@"user-agent"];
@@ -214,7 +202,7 @@
}
dispatch_async(dispatch_get_main_queue(), ^(void) {
- [self.ICEServerDelegate onICEServers:ICEServers];
+ [self.delegate appClient:self didReceiveICEServers:ICEServers];
});
});
}
@@ -223,8 +211,10 @@
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
NSString* roomHtml = [NSString stringWithUTF8String:[data bytes]];
- [self maybeLogMessage:[NSString stringWithFormat:@"Received %d chars",
- [roomHtml length]]];
+ NSString* message =
+ [NSString stringWithFormat:@"Received %lu chars",
+ (unsigned long)[roomHtml length]];
+ [self maybeLogMessage:message];
[self.roomHtml appendString:roomHtml];
}
@@ -243,8 +233,10 @@
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
- [self maybeLogMessage:[NSString stringWithFormat:@"finished loading %d chars",
- [self.roomHtml length]]];
+ NSString* message =
+ [NSString stringWithFormat:@"finished loading %lu chars",
+ (unsigned long)[self.roomHtml length]];
+ [self maybeLogMessage:message];
NSRegularExpression* fullRegex =
[NSRegularExpression regularExpressionWithPattern:@"room is full"
options:0
@@ -253,10 +245,8 @@
numberOfMatchesInString:self.roomHtml
options:0
range:NSMakeRange(0, [self.roomHtml length])]) {
- [self showMessage:@"Room full"];
- APPRTCAppDelegate* ad =
- (APPRTCAppDelegate*)[[UIApplication sharedApplication] delegate];
- [ad closeVideoUI];
+ NSString* message = @"Room full, dropping peerconnection.";
+ [self.delegate appClient:self didErrorWithMessage:message];
return;
}
@@ -331,7 +321,22 @@
json =
[NSJSONSerialization JSONObjectWithData:mcData options:0 error:&error];
NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
- if ([[json objectForKey:@"video"] boolValue]) {
+ id video = json[@"video"];
+ if ([video isKindOfClass:[NSDictionary class]]) {
+ NSDictionary* mandatory = video[@"mandatory"];
+ NSMutableArray* mandatoryContraints =
+ [NSMutableArray arrayWithCapacity:[mandatory count]];
+ [mandatory enumerateKeysAndObjectsUsingBlock:^(
+ id key, id obj, BOOL* stop) {
+ [mandatoryContraints addObject:[[RTCPair alloc] initWithKey:key
+ value:obj]];
+ }];
+ // TODO(tkchin): figure out json formats for optional constraints.
+ _videoConstraints =
+ [[RTCMediaConstraints alloc]
+ initWithMandatoryConstraints:mandatoryContraints
+ optionalConstraints:nil];
+ } else if ([video isKindOfClass:[NSNumber class]] && [video boolValue]) {
_videoConstraints = [[RTCMediaConstraints alloc] init];
}
}
diff --git a/talk/examples/objc/AppRTCDemo/APPRTCConnectionManager.h b/talk/examples/objc/AppRTCDemo/APPRTCConnectionManager.h
new file mode 100644
index 0000000..98fe755
--- /dev/null
+++ b/talk/examples/objc/AppRTCDemo/APPRTCConnectionManager.h
@@ -0,0 +1,66 @@
+/*
+ * libjingle
+ * Copyright 2014, 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.
+ */
+
+#import <Foundation/Foundation.h>
+
+// Used to log messages to destination like UI.
+@protocol APPRTCLogger<NSObject>
+- (void)logMessage:(NSString*)message;
+@end
+
+@class RTCVideoTrack;
+@class APPRTCConnectionManager;
+
+// Used to provide AppRTC connection information.
+@protocol APPRTCConnectionManagerDelegate<NSObject>
+
+- (void)connectionManager:(APPRTCConnectionManager*)manager
+ didReceiveLocalVideoTrack:(RTCVideoTrack*)localVideoTrack;
+
+- (void)connectionManager:(APPRTCConnectionManager*)manager
+ didReceiveRemoteVideoTrack:(RTCVideoTrack*)remoteVideoTrack;
+
+- (void)connectionManagerDidReceiveHangup:(APPRTCConnectionManager*)manager;
+
+- (void)connectionManager:(APPRTCConnectionManager*)manager
+ didErrorWithMessage:(NSString*)errorMessage;
+
+@end
+
+// Abstracts the network connection aspect of AppRTC. The delegate will receive
+// information about connection status as changes occur.
+@interface APPRTCConnectionManager : NSObject
+
+@property(nonatomic, weak) id<APPRTCConnectionManagerDelegate> delegate;
+@property(nonatomic, weak) id<APPRTCLogger> logger;
+
+- (instancetype)initWithDelegate:(id<APPRTCConnectionManagerDelegate>)delegate
+ logger:(id<APPRTCLogger>)logger;
+- (BOOL)connectToRoomWithURL:(NSURL*)url;
+- (void)disconnect;
+
+@end
diff --git a/talk/examples/objc/AppRTCDemo/APPRTCConnectionManager.m b/talk/examples/objc/AppRTCDemo/APPRTCConnectionManager.m
new file mode 100644
index 0000000..6d0a5a2
--- /dev/null
+++ b/talk/examples/objc/AppRTCDemo/APPRTCConnectionManager.m
@@ -0,0 +1,494 @@
+/*
+ * libjingle
+ * Copyright 2014, 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.
+ */
+
+#import "APPRTCConnectionManager.h"
+
+#import <AVFoundation/AVFoundation.h>
+#import "APPRTCAppClient.h"
+#import "GAEChannelClient.h"
+#import "RTCICECandidate.h"
+#import "RTCMediaConstraints.h"
+#import "RTCMediaStream.h"
+#import "RTCPair.h"
+#import "RTCPeerConnection.h"
+#import "RTCPeerConnectionDelegate.h"
+#import "RTCPeerConnectionFactory.h"
+#import "RTCSessionDescription.h"
+#import "RTCSessionDescriptionDelegate.h"
+#import "RTCStatsDelegate.h"
+#import "RTCVideoCapturer.h"
+#import "RTCVideoSource.h"
+
+@interface APPRTCConnectionManager ()
+ <APPRTCAppClientDelegate, GAEMessageHandler, RTCPeerConnectionDelegate,
+ RTCSessionDescriptionDelegate, RTCStatsDelegate>
+
+@property(nonatomic, strong) APPRTCAppClient* client;
+@property(nonatomic, strong) RTCPeerConnection* peerConnection;
+@property(nonatomic, strong) RTCPeerConnectionFactory* peerConnectionFactory;
+@property(nonatomic, strong) RTCVideoSource* videoSource;
+@property(nonatomic, strong) NSMutableArray* queuedRemoteCandidates;
+
+@end
+
+@implementation APPRTCConnectionManager {
+ NSTimer* _statsTimer;
+}
+
+- (instancetype)initWithDelegate:(id<APPRTCConnectionManagerDelegate>)delegate
+ logger:(id<APPRTCLogger>)logger {
+ if (self = [super init]) {
+ self.delegate = delegate;
+ self.logger = logger;
+ self.peerConnectionFactory = [[RTCPeerConnectionFactory alloc] init];
+ // TODO(tkchin): turn this into a button.
+ // Uncomment for stat logs.
+ // _statsTimer =
+ // [NSTimer scheduledTimerWithTimeInterval:10
+ // target:self
+ // selector:@selector(didFireStatsTimer:)
+ // userInfo:nil
+ // repeats:YES];
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [self disconnect];
+}
+
+- (BOOL)connectToRoomWithURL:(NSURL*)url {
+ if (self.client) {
+ // Already have a connection.
+ return NO;
+ }
+ self.client = [[APPRTCAppClient alloc] initWithDelegate:self
+ messageHandler:self];
+ [self.client connectToRoom:url];
+ return YES;
+}
+
+- (void)disconnect {
+ if (!self.client) {
+ return;
+ }
+ [self.client
+ sendData:[@"{\"type\": \"bye\"}" dataUsingEncoding:NSUTF8StringEncoding]];
+ [self.peerConnection close];
+ self.peerConnection = nil;
+ self.client = nil;
+ self.queuedRemoteCandidates = nil;
+}
+
+#pragma mark - APPRTCAppClientDelegate
+
+- (void)appClient:(APPRTCAppClient*)appClient
+ didErrorWithMessage:(NSString*)message {
+ [self.delegate connectionManager:self
+ didErrorWithMessage:message];
+}
+
+- (void)appClient:(APPRTCAppClient*)appClient
+ didReceiveICEServers:(NSArray*)servers {
+ self.queuedRemoteCandidates = [NSMutableArray array];
+ 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"]
+ ]];
+ self.peerConnection =
+ [self.peerConnectionFactory peerConnectionWithICEServers:servers
+ constraints:constraints
+ delegate:self];
+ RTCMediaStream* lms =
+ [self.peerConnectionFactory mediaStreamWithLabel:@"ARDAMS"];
+
+ // 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;
+
+ // TODO(tkchin): local video capture for OSX. See
+ // https://code.google.com/p/webrtc/issues/detail?id=3417.
+#if !TARGET_IPHONE_SIMULATOR && TARGET_OS_IPHONE
+ NSString* cameraID = nil;
+ for (AVCaptureDevice* captureDevice in
+ [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
+ if (captureDevice.position == AVCaptureDevicePositionFront) {
+ cameraID = [captureDevice localizedName];
+ break;
+ }
+ }
+ NSAssert(cameraID, @"Unable to get the front camera id");
+
+ RTCVideoCapturer* capturer =
+ [RTCVideoCapturer capturerWithDeviceName:cameraID];
+ self.videoSource = [self.peerConnectionFactory
+ videoSourceWithCapturer:capturer
+ constraints:self.client.videoConstraints];
+ localVideoTrack =
+ [self.peerConnectionFactory videoTrackWithID:@"ARDAMSv0"
+ source:self.videoSource];
+ if (localVideoTrack) {
+ [lms addVideoTrack:localVideoTrack];
+ }
+ [self.delegate connectionManager:self
+ didReceiveLocalVideoTrack:localVideoTrack];
+#endif
+
+ [lms addAudioTrack:[self.peerConnectionFactory audioTrackWithID:@"ARDAMSa0"]];
+ [self.peerConnection addStream:lms constraints:constraints];
+ [self.logger logMessage:@"onICEServers - added local stream."];
+}
+
+#pragma mark - GAEMessageHandler methods
+
+- (void)onOpen {
+ if (!self.client.initiator) {
+ [self.logger logMessage:@"Callee; waiting for remote offer"];
+ return;
+ }
+ [self.logger logMessage:@"GAE onOpen - create offer."];
+ 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 createOfferWithDelegate:self constraints:constraints];
+ [self.logger logMessage:@"PC - createOffer."];
+}
+
+- (void)onMessage:(NSDictionary*)messageData {
+ NSString* type = messageData[@"type"];
+ NSAssert(type, @"Missing type: %@", messageData);
+ [self.logger logMessage:[NSString stringWithFormat:@"GAE onMessage type - %@",
+ type]];
+ if ([type isEqualToString:@"candidate"]) {
+ NSString* mid = messageData[@"id"];
+ NSNumber* sdpLineIndex = messageData[@"label"];
+ NSString* sdp = messageData[@"candidate"];
+ RTCICECandidate* candidate =
+ [[RTCICECandidate alloc] initWithMid:mid
+ index:sdpLineIndex.intValue
+ sdp:sdp];
+ if (self.queuedRemoteCandidates) {
+ [self.queuedRemoteCandidates addObject:candidate];
+ } else {
+ [self.peerConnection addICECandidate:candidate];
+ }
+ } else if ([type isEqualToString:@"offer"] ||
+ [type isEqualToString:@"answer"]) {
+ NSString* sdpString = messageData[@"sdp"];
+ RTCSessionDescription* sdp = [[RTCSessionDescription alloc]
+ initWithType:type
+ sdp:[[self class] preferISAC:sdpString]];
+ [self.peerConnection setRemoteDescriptionWithDelegate:self
+ sessionDescription:sdp];
+ [self.logger logMessage:@"PC - setRemoteDescription."];
+ } else if ([type isEqualToString:@"bye"]) {
+ [self.delegate connectionManagerDidReceiveHangup:self];
+ } else {
+ NSAssert(NO, @"Invalid message: %@", messageData);
+ }
+}
+
+- (void)onClose {
+ [self.logger logMessage:@"GAE onClose."];
+ [self.delegate connectionManagerDidReceiveHangup:self];
+}
+
+- (void)onError:(int)code withDescription:(NSString*)description {
+ NSString* message = [NSString stringWithFormat:@"GAE onError: %d, %@",
+ code, description];
+ [self.logger logMessage:message];
+ [self.delegate connectionManager:self
+ didErrorWithMessage:message];
+}
+
+#pragma mark - RTCPeerConnectionDelegate
+
+- (void)peerConnectionOnError:(RTCPeerConnection*)peerConnection {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSString* message = @"PeerConnection error";
+ NSLog(@"%@", message);
+ NSAssert(NO, @"PeerConnection failed.");
+ [self.delegate connectionManager:self
+ didErrorWithMessage:message];
+ });
+}
+
+- (void)peerConnection:(RTCPeerConnection*)peerConnection
+ signalingStateChanged:(RTCSignalingState)stateChanged {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSLog(@"PCO onSignalingStateChange: %d", stateChanged);
+ });
+}
+
+- (void)peerConnection:(RTCPeerConnection*)peerConnection
+ addedStream:(RTCMediaStream*)stream {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSLog(@"PCO onAddStream.");
+ NSAssert([stream.audioTracks count] == 1 || [stream.videoTracks count] == 1,
+ @"Expected audio or video track");
+ NSAssert([stream.audioTracks count] <= 1,
+ @"Expected at most 1 audio stream");
+ NSAssert([stream.videoTracks count] <= 1,
+ @"Expected at most 1 video stream");
+ if ([stream.videoTracks count] != 0) {
+ [self.delegate connectionManager:self
+ didReceiveRemoteVideoTrack:stream.videoTracks[0]];
+ }
+ });
+}
+
+- (void)peerConnection:(RTCPeerConnection*)peerConnection
+ removedStream:(RTCMediaStream*)stream {
+ dispatch_async(dispatch_get_main_queue(),
+ ^{ NSLog(@"PCO onRemoveStream."); });
+}
+
+- (void)peerConnectionOnRenegotiationNeeded:(RTCPeerConnection*)peerConnection {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSLog(@"PCO onRenegotiationNeeded - ignoring because AppRTC has a "
+ "predefined negotiation strategy");
+ });
+}
+
+- (void)peerConnection:(RTCPeerConnection*)peerConnection
+ gotICECandidate:(RTCICECandidate*)candidate {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSLog(@"PCO onICECandidate.\n Mid[%@] Index[%li] Sdp[%@]",
+ candidate.sdpMid,
+ (long)candidate.sdpMLineIndex,
+ candidate.sdp);
+ NSDictionary* json = @{
+ @"type" : @"candidate",
+ @"label" : @(candidate.sdpMLineIndex),
+ @"id" : candidate.sdpMid,
+ @"candidate" : candidate.sdp
+ };
+ NSError* error;
+ NSData* data =
+ [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
+ if (!error) {
+ [self.client sendData:data];
+ } else {
+ NSAssert(NO,
+ @"Unable to serialize JSON object with error: %@",
+ error.localizedDescription);
+ }
+ });
+}
+
+- (void)peerConnection:(RTCPeerConnection*)peerConnection
+ iceGatheringChanged:(RTCICEGatheringState)newState {
+ dispatch_async(dispatch_get_main_queue(),
+ ^{ NSLog(@"PCO onIceGatheringChange. %d", newState); });
+}
+
+- (void)peerConnection:(RTCPeerConnection*)peerConnection
+ iceConnectionChanged:(RTCICEConnectionState)newState {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSLog(@"PCO onIceConnectionChange. %d", newState);
+ if (newState == RTCICEConnectionConnected)
+ [self.logger logMessage:@"ICE Connection Connected."];
+ NSAssert(newState != RTCICEConnectionFailed, @"ICE Connection failed!");
+ });
+}
+
+- (void)peerConnection:(RTCPeerConnection*)peerConnection
+ didOpenDataChannel:(RTCDataChannel*)dataChannel {
+ NSAssert(NO, @"AppRTC doesn't use DataChannels");
+}
+
+#pragma mark - RTCSessionDescriptionDelegate
+
+- (void)peerConnection:(RTCPeerConnection*)peerConnection
+ didCreateSessionDescription:(RTCSessionDescription*)origSdp
+ error:(NSError*)error {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (error) {
+ [self.logger logMessage:@"SDP onFailure."];
+ NSAssert(NO, error.description);
+ return;
+ }
+ [self.logger logMessage:@"SDP onSuccess(SDP) - set local description."];
+ RTCSessionDescription* sdp = [[RTCSessionDescription alloc]
+ initWithType:origSdp.type
+ sdp:[[self class] preferISAC:origSdp.description]];
+ [self.peerConnection setLocalDescriptionWithDelegate:self
+ sessionDescription:sdp];
+ [self.logger logMessage:@"PC setLocalDescription."];
+ NSDictionary* json = @{@"type" : sdp.type, @"sdp" : sdp.description};
+ NSError* jsonError;
+ NSData* data = [NSJSONSerialization dataWithJSONObject:json
+ options:0
+ error:&jsonError];
+ NSAssert(!jsonError, @"Error: %@", jsonError.description);
+ [self.client sendData:data];
+ });
+}
+
+- (void)peerConnection:(RTCPeerConnection*)peerConnection
+ didSetSessionDescriptionWithError:(NSError*)error {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (error) {
+ [self.logger logMessage:@"SDP onFailure."];
+ NSAssert(NO, error.description);
+ return;
+ }
+ [self.logger logMessage:@"SDP onSuccess() - possibly drain candidates"];
+ if (!self.client.initiator) {
+ if (self.peerConnection.remoteDescription &&
+ !self.peerConnection.localDescription) {
+ [self.logger logMessage:@"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.logger logMessage:@"PC - createAnswer."];
+ } else {
+ [self.logger logMessage:@"SDP onSuccess - drain candidates"];
+ [self drainRemoteCandidates];
+ }
+ } else {
+ if (self.peerConnection.remoteDescription) {
+ [self.logger logMessage:@"SDP onSuccess - drain candidates"];
+ [self drainRemoteCandidates];
+ }
+ }
+ });
+}
+
+#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.logger logMessage:message];
+ });
+}
+
+#pragma mark - Private
+
+// Match |pattern| to |string| and return the first group of the first
+// match, or nil if no match was found.
++ (NSString*)firstMatch:(NSRegularExpression*)pattern
+ withString:(NSString*)string {
+ NSTextCheckingResult* result =
+ [pattern firstMatchInString:string
+ options:0
+ range:NSMakeRange(0, [string length])];
+ if (!result)
+ return nil;
+ return [string substringWithRange:[result rangeAtIndex:1]];
+}
+
+// Mangle |origSDP| to prefer the ISAC/16k audio codec.
++ (NSString*)preferISAC:(NSString*)origSDP {
+ 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) {
+ if (![isac16kRtpMap
+ isEqualToString:[origMLineParts objectAtIndex:origPartIndex]]) {
+ [newMLine addObject:[origMLineParts objectAtIndex:origPartIndex]];
+ }
+ }
+ NSMutableArray* newLines = [NSMutableArray arrayWithCapacity:[lines count]];
+ [newLines addObjectsFromArray:lines];
+ [newLines replaceObjectAtIndex:mLineIndex
+ withObject:[newMLine componentsJoinedByString:@" "]];
+ return [newLines componentsJoinedByString:@"\n"];
+}
+
+- (void)drainRemoteCandidates {
+ for (RTCICECandidate* candidate in self.queuedRemoteCandidates) {
+ [self.peerConnection addICECandidate:candidate];
+ }
+ self.queuedRemoteCandidates = nil;
+}
+
+- (void)didFireStatsTimer:(NSTimer*)timer {
+ if (self.peerConnection) {
+ [self.peerConnection getStatsWithDelegate:self
+ mediaStreamTrack:nil
+ statsOutputLevel:RTCStatsOutputLevelDebug];
+ }
+}
+
+@end
diff --git a/talk/examples/ios/AppRTCDemo/GAEChannelClient.h b/talk/examples/objc/AppRTCDemo/GAEChannelClient.h
similarity index 89%
rename from talk/examples/ios/AppRTCDemo/GAEChannelClient.h
rename to talk/examples/objc/AppRTCDemo/GAEChannelClient.h
index 8c7d5d3..2eac214 100644
--- a/talk/examples/ios/AppRTCDemo/GAEChannelClient.h
+++ b/talk/examples/objc/AppRTCDemo/GAEChannelClient.h
@@ -25,7 +25,7 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#import <UIKit/UIKit.h>
+#import <Foundation/Foundation.h>
// These methods will be called by the AppEngine chanel. The documentation
// for these methods is found here. (Yes, it is a JS API.)
@@ -35,15 +35,15 @@
- (void)onOpen;
- (void)onMessage:(NSDictionary*)data;
- (void)onClose;
-- (void)onError:(int)code withDescription:(NSString *)description;
+- (void)onError:(int)code withDescription:(NSString*)description;
@end
// Initialize with a token for an AppRTC data channel. This will load
// ios_channel.html and use the token to establish a data channel between the
// application and AppEngine.
-@interface GAEChannelClient : NSObject<UIWebViewDelegate>
+@interface GAEChannelClient : NSObject
-- (id)initWithToken:(NSString *)token delegate:(id<GAEMessageHandler>)delegate;
+- (id)initWithToken:(NSString*)token delegate:(id<GAEMessageHandler>)delegate;
@end
diff --git a/talk/examples/ios/AppRTCDemo/GAEChannelClient.m b/talk/examples/objc/AppRTCDemo/GAEChannelClient.m
similarity index 78%
rename from talk/examples/ios/AppRTCDemo/GAEChannelClient.m
rename to talk/examples/objc/AppRTCDemo/GAEChannelClient.m
index fcd0787..6146043 100644
--- a/talk/examples/ios/AppRTCDemo/GAEChannelClient.m
+++ b/talk/examples/objc/AppRTCDemo/GAEChannelClient.m
@@ -29,10 +29,25 @@
#import "RTCPeerConnectionFactory.h"
+#if TARGET_OS_IPHONE
+
+#import <UIKit/UIKit.h>
+
+@interface GAEChannelClient () <UIWebViewDelegate>
+
+@property(nonatomic, strong) UIWebView* webView;
+
+#else
+
+#import <WebKit/WebKit.h>
+
@interface GAEChannelClient ()
+@property(nonatomic, strong) WebView* webView;
+
+#endif
+
@property(nonatomic, assign) id<GAEMessageHandler> delegate;
-@property(nonatomic, strong) UIWebView* webView;
@end
@@ -41,47 +56,67 @@
- (id)initWithToken:(NSString*)token delegate:(id<GAEMessageHandler>)delegate {
self = [super init];
if (self) {
+#if TARGET_OS_IPHONE
_webView = [[UIWebView alloc] init];
_webView.delegate = self;
+#else
+ _webView = [[WebView alloc] init];
+ _webView.policyDelegate = self;
+#endif
_delegate = delegate;
NSString* htmlPath =
- [[NSBundle mainBundle] pathForResource:@"ios_channel" ofType:@"html"];
+ [[NSBundle mainBundle] pathForResource:@"channel" ofType:@"html"];
NSURL* htmlUrl = [NSURL fileURLWithPath:htmlPath];
NSString* path = [NSString
stringWithFormat:@"%@?token=%@", [htmlUrl absoluteString], token];
+#if TARGET_OS_IPHONE
[_webView
+#else
+ [[_webView mainFrame]
+#endif
loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]];
}
return self;
}
- (void)dealloc {
+#if TARGET_OS_IPHONE
_webView.delegate = nil;
[_webView stopLoading];
+#else
+ _webView.policyDelegate = nil;
+ [[_webView mainFrame] stopLoading];
+#endif
}
-#pragma mark - UIWebViewDelegate method
-
-+ (NSDictionary*)jsonStringToDictionary:(NSString*)str {
- NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
- NSError* error;
- NSDictionary* dict =
- [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
- NSAssert(!error, @"Invalid JSON? %@", str);
- return dict;
-}
+#if TARGET_OS_IPHONE
+#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView*)webView
shouldStartLoadWithRequest:(NSURLRequest*)request
navigationType:(UIWebViewNavigationType)navigationType {
+#else
+// WebPolicyDelegate is an informal delegate.
+#pragma mark - WebPolicyDelegate
+
+- (void)webView:(WebView*)webView
+ decidePolicyForNavigationAction:(NSDictionary*)actionInformation
+ request:(NSURLRequest*)request
+ frame:(WebFrame*)frame
+ decisionListener:(id<WebPolicyDecisionListener>)listener {
+#endif
NSString* scheme = [request.URL scheme];
NSAssert(scheme, @"scheme is nil: %@", request);
if (![scheme isEqualToString:@"js-frame"]) {
+#if TARGET_OS_IPHONE
return YES;
+#else
+ [listener use];
+ return;
+#endif
}
-
- dispatch_async(dispatch_get_main_queue(), ^(void) {
+ dispatch_async(dispatch_get_main_queue(), ^{
NSString* queuedMessage = [webView
stringByEvaluatingJavaScriptFromString:@"popQueuedMessage();"];
NSAssert([queuedMessage length], @"Empty queued message from JS");
@@ -110,7 +145,23 @@
NSAssert(NO, @"Invalid message sent from UIWebView: %@", queuedMessage);
}
});
+#if TARGET_OS_IPHONE
return NO;
+#else
+ [listener ignore];
+ return;
+#endif
+}
+
+#pragma mark - Private
+
++ (NSDictionary*)jsonStringToDictionary:(NSString*)str {
+ NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
+ NSError* error;
+ NSDictionary* dict =
+ [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
+ NSAssert(!error, @"Invalid JSON? %@", str);
+ return dict;
}
@end
diff --git a/talk/examples/ios/AppRTCDemo/ios_channel.html b/talk/examples/objc/AppRTCDemo/channel.html
similarity index 100%
rename from talk/examples/ios/AppRTCDemo/ios_channel.html
rename to talk/examples/objc/AppRTCDemo/channel.html
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h b/talk/examples/objc/AppRTCDemo/ios/APPRTCAppDelegate.h
similarity index 71%
copy from talk/examples/ios/AppRTCDemo/APPRTCViewController.h
copy to talk/examples/objc/AppRTCDemo/ios/APPRTCAppDelegate.h
index 1737a13..196b39f 100644
--- a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h
+++ b/talk/examples/objc/AppRTCDemo/ios/APPRTCAppDelegate.h
@@ -27,20 +27,8 @@
#import <UIKit/UIKit.h>
-@class RTCEAGLVideoView;
-
-// The view controller that is displayed when AppRTCDemo is loaded.
-@interface APPRTCViewController : UIViewController<UITextFieldDelegate>
-
-@property(weak, nonatomic) IBOutlet UITextField* roomInput;
-@property(weak, nonatomic) IBOutlet UITextView* instructionsView;
-@property(weak, nonatomic) IBOutlet UITextView* logView;
-@property(weak, nonatomic) IBOutlet UIView* blackView;
-
-@property(nonatomic, strong) RTCEAGLVideoView* localVideoView;
-@property(nonatomic, strong) RTCEAGLVideoView* remoteVideoView;
-
-- (void)displayText:(NSString*)text;
-- (void)resetUI;
-
+// The main application class of the AppRTCDemo iOS app demonstrating
+// interoperability between the Objective C implementation of PeerConnection
+// and the apprtc.appspot.com demo webapp.
+@interface APPRTCAppDelegate : NSObject<UIApplicationDelegate>
@end
diff --git a/talk/examples/objc/AppRTCDemo/ios/APPRTCAppDelegate.m b/talk/examples/objc/AppRTCDemo/ios/APPRTCAppDelegate.m
new file mode 100644
index 0000000..58963d5
--- /dev/null
+++ b/talk/examples/objc/AppRTCDemo/ios/APPRTCAppDelegate.m
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+#import "APPRTCAppDelegate.h"
+
+#import "APPRTCViewController.h"
+#import "RTCPeerConnectionFactory.h"
+
+@implementation APPRTCAppDelegate {
+ UIWindow* _window;
+}
+
+#pragma mark - UIApplicationDelegate methods
+
+- (BOOL)application:(UIApplication*)application
+ didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
+ [RTCPeerConnectionFactory initializeSSL];
+ _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
+ APPRTCViewController* viewController =
+ [[APPRTCViewController alloc] initWithNibName:@"APPRTCViewController"
+ bundle:nil];
+ _window.rootViewController = viewController;
+ [_window makeKeyAndVisible];
+ return YES;
+}
+
+- (void)applicationWillResignActive:(UIApplication*)application {
+ [[self appRTCViewController] applicationWillResignActive:application];
+}
+
+- (void)applicationWillTerminate:(UIApplication*)application {
+ [RTCPeerConnectionFactory deinitializeSSL];
+}
+
+#pragma mark - Private
+
+- (APPRTCViewController*)appRTCViewController {
+ return (APPRTCViewController*)_window.rootViewController;
+}
+
+@end
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h b/talk/examples/objc/AppRTCDemo/ios/APPRTCViewController.h
similarity index 89%
rename from talk/examples/ios/AppRTCDemo/APPRTCViewController.h
rename to talk/examples/objc/AppRTCDemo/ios/APPRTCViewController.h
index 1737a13..5b10199 100644
--- a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h
+++ b/talk/examples/objc/AppRTCDemo/ios/APPRTCViewController.h
@@ -27,8 +27,6 @@
#import <UIKit/UIKit.h>
-@class RTCEAGLVideoView;
-
// The view controller that is displayed when AppRTCDemo is loaded.
@interface APPRTCViewController : UIViewController<UITextFieldDelegate>
@@ -37,10 +35,6 @@
@property(weak, nonatomic) IBOutlet UITextView* logView;
@property(weak, nonatomic) IBOutlet UIView* blackView;
-@property(nonatomic, strong) RTCEAGLVideoView* localVideoView;
-@property(nonatomic, strong) RTCEAGLVideoView* remoteVideoView;
-
-- (void)displayText:(NSString*)text;
-- (void)resetUI;
+- (void)applicationWillResignActive:(UIApplication*)application;
@end
diff --git a/talk/examples/objc/AppRTCDemo/ios/APPRTCViewController.m b/talk/examples/objc/AppRTCDemo/ios/APPRTCViewController.m
new file mode 100644
index 0000000..a4a0bd3
--- /dev/null
+++ b/talk/examples/objc/AppRTCDemo/ios/APPRTCViewController.m
@@ -0,0 +1,231 @@
+/*
+ * 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.
+ */
+
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+
+#import "APPRTCViewController.h"
+
+#import <AVFoundation/AVFoundation.h>
+#import "APPRTCConnectionManager.h"
+#import "RTCEAGLVideoView.h"
+
+// Padding space for local video view with its parent.
+static CGFloat const kLocalViewPadding = 20;
+
+@interface APPRTCViewController ()
+<APPRTCConnectionManagerDelegate, APPRTCLogger, RTCEAGLVideoViewDelegate>
+@property(nonatomic, assign) UIInterfaceOrientation statusBarOrientation;
+@property(nonatomic, strong) RTCEAGLVideoView* localVideoView;
+@property(nonatomic, strong) RTCEAGLVideoView* remoteVideoView;
+@end
+
+@implementation APPRTCViewController {
+ APPRTCConnectionManager* _connectionManager;
+ CGSize _localVideoSize;
+ CGSize _remoteVideoSize;
+}
+
+- (instancetype)initWithNibName:(NSString*)nibName
+ bundle:(NSBundle*)bundle {
+ if (self = [super initWithNibName:nibName bundle:bundle]) {
+ _connectionManager =
+ [[APPRTCConnectionManager alloc] initWithDelegate:self
+ logger:self];
+ }
+ return self;
+}
+
+- (void)viewDidLoad {
+ [super viewDidLoad];
+ self.statusBarOrientation =
+ [UIApplication sharedApplication].statusBarOrientation;
+ self.roomInput.delegate = self;
+ [self.roomInput becomeFirstResponder];
+}
+
+- (void)viewDidLayoutSubviews {
+ if (self.statusBarOrientation !=
+ [UIApplication sharedApplication].statusBarOrientation) {
+ self.statusBarOrientation =
+ [UIApplication sharedApplication].statusBarOrientation;
+ [[NSNotificationCenter defaultCenter]
+ postNotificationName:@"StatusBarOrientationDidChange"
+ object:nil];
+ }
+}
+
+- (void)applicationWillResignActive:(UIApplication*)application {
+ [self logMessage:@"Application lost focus, connection broken."];
+ [self disconnect];
+}
+
+#pragma mark - APPRTCConnectionManagerDelegate
+
+- (void)connectionManager:(APPRTCConnectionManager*)manager
+ didReceiveLocalVideoTrack:(RTCVideoTrack*)localVideoTrack {
+ self.localVideoView.hidden = NO;
+ self.localVideoView.videoTrack = localVideoTrack;
+}
+
+- (void)connectionManager:(APPRTCConnectionManager*)manager
+ didReceiveRemoteVideoTrack:(RTCVideoTrack*)remoteVideoTrack {
+ self.remoteVideoView.videoTrack = remoteVideoTrack;
+}
+
+- (void)connectionManagerDidReceiveHangup:(APPRTCConnectionManager*)manager {
+ [self showAlertWithMessage:@"Remote hung up."];
+ [self disconnect];
+}
+
+- (void)connectionManager:(APPRTCConnectionManager*)manager
+ didErrorWithMessage:(NSString*)message {
+ [self showAlertWithMessage:message];
+ [self disconnect];
+}
+
+#pragma mark - APPRTCLogger
+
+- (void)logMessage:(NSString*)message {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSString* output =
+ [NSString stringWithFormat:@"%@\n%@", self.logView.text, message];
+ self.logView.text = output;
+ [self.logView
+ scrollRangeToVisible:NSMakeRange([self.logView.text length], 0)];
+ });
+}
+
+#pragma mark - RTCEAGLVideoViewDelegate
+
+- (void)videoView:(RTCEAGLVideoView*)videoView
+ didChangeVideoSize:(CGSize)size {
+ if (videoView == self.localVideoView) {
+ _localVideoSize = size;
+ } else if (videoView == self.remoteVideoView) {
+ _remoteVideoSize = size;
+ } else {
+ NSParameterAssert(NO);
+ }
+ [self updateVideoViewLayout];
+}
+
+#pragma mark - UITextFieldDelegate
+
+- (void)textFieldDidEndEditing:(UITextField*)textField {
+ NSString* room = textField.text;
+ if ([room length] == 0) {
+ return;
+ }
+ textField.hidden = YES;
+ self.instructionsView.hidden = YES;
+ self.logView.hidden = NO;
+ NSString* url =
+ [NSString stringWithFormat:@"https://apprtc.appspot.com/?r=%@", room];
+ [_connectionManager connectToRoomWithURL:[NSURL URLWithString:url]];
+ [self setupCaptureSession];
+}
+
+- (BOOL)textFieldShouldReturn:(UITextField*)textField {
+ // There is no other control that can take focus, so manually resign focus
+ // when return (Join) is pressed to trigger |textFieldDidEndEditing|.
+ [textField resignFirstResponder];
+ return YES;
+}
+
+#pragma mark - Private
+
+- (void)disconnect {
+ [self resetUI];
+ [_connectionManager disconnect];
+}
+
+- (void)showAlertWithMessage:(NSString*)message {
+ UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
+ message:message
+ delegate:nil
+ cancelButtonTitle:@"OK"
+ otherButtonTitles:nil];
+ [alertView show];
+}
+
+- (void)resetUI {
+ [self.roomInput resignFirstResponder];
+ self.roomInput.text = nil;
+ self.roomInput.hidden = NO;
+ self.instructionsView.hidden = NO;
+ self.logView.hidden = YES;
+ self.logView.text = nil;
+ self.blackView.hidden = YES;
+ [self.remoteVideoView removeFromSuperview];
+ self.remoteVideoView = nil;
+ [self.localVideoView removeFromSuperview];
+ self.localVideoView = nil;
+}
+
+- (void)setupCaptureSession {
+ self.blackView.hidden = NO;
+ self.remoteVideoView =
+ [[RTCEAGLVideoView alloc] initWithFrame:self.blackView.bounds];
+ self.remoteVideoView.delegate = self;
+ self.remoteVideoView.transform = CGAffineTransformMakeScale(-1, 1);
+ [self.blackView addSubview:self.remoteVideoView];
+
+ self.localVideoView =
+ [[RTCEAGLVideoView alloc] initWithFrame:self.blackView.bounds];
+ self.localVideoView.delegate = self;
+ [self.blackView addSubview:self.localVideoView];
+ [self updateVideoViewLayout];
+}
+
+- (void)updateVideoViewLayout {
+ // TODO(tkchin): handle rotation.
+ CGSize defaultAspectRatio = CGSizeMake(4, 3);
+ CGSize localAspectRatio = CGSizeEqualToSize(_localVideoSize, CGSizeZero) ?
+ defaultAspectRatio : _localVideoSize;
+ CGSize remoteAspectRatio = CGSizeEqualToSize(_remoteVideoSize, CGSizeZero) ?
+ defaultAspectRatio : _remoteVideoSize;
+
+ CGRect remoteVideoFrame =
+ AVMakeRectWithAspectRatioInsideRect(remoteAspectRatio,
+ self.blackView.bounds);
+ self.remoteVideoView.frame = remoteVideoFrame;
+
+ CGRect localVideoFrame =
+ AVMakeRectWithAspectRatioInsideRect(localAspectRatio,
+ self.blackView.bounds);
+ localVideoFrame.size.width = localVideoFrame.size.width / 3;
+ localVideoFrame.size.height = localVideoFrame.size.height / 3;
+ localVideoFrame.origin.x = CGRectGetMaxX(self.blackView.bounds)
+ - localVideoFrame.size.width - kLocalViewPadding;
+ localVideoFrame.origin.y = CGRectGetMaxY(self.blackView.bounds)
+ - localVideoFrame.size.height - kLocalViewPadding;
+ self.localVideoView.frame = localVideoFrame;
+}
+
+@end
diff --git a/talk/examples/ios/AppRTCDemo/AppRTCDemo-Prefix.pch b/talk/examples/objc/AppRTCDemo/ios/AppRTCDemo-Prefix.pch
similarity index 100%
rename from talk/examples/ios/AppRTCDemo/AppRTCDemo-Prefix.pch
rename to talk/examples/objc/AppRTCDemo/ios/AppRTCDemo-Prefix.pch
diff --git a/talk/examples/ios/AppRTCDemo/Default.png b/talk/examples/objc/AppRTCDemo/ios/Default.png
similarity index 100%
rename from talk/examples/ios/AppRTCDemo/Default.png
rename to talk/examples/objc/AppRTCDemo/ios/Default.png
Binary files differ
diff --git a/talk/examples/ios/AppRTCDemo/Info.plist b/talk/examples/objc/AppRTCDemo/ios/Info.plist
similarity index 80%
rename from talk/examples/ios/AppRTCDemo/Info.plist
rename to talk/examples/objc/AppRTCDemo/ios/Info.plist
index a32be86..b616480 100644
--- a/talk/examples/ios/AppRTCDemo/Info.plist
+++ b/talk/examples/objc/AppRTCDemo/ios/Info.plist
@@ -38,19 +38,6 @@
<array>
<string>iPhoneOS</string>
</array>
- <key>CFBundleURLTypes</key>
- <array>
- <dict>
- <key>CFBundleTypeRole</key>
- <string>Editor</string>
- <key>CFBundleURLName</key>
- <string>com.google.apprtcdemo</string>
- <key>CFBundleURLSchemes</key>
- <array>
- <string>apprtc</string>
- </array>
- </dict>
- </array>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>UIRequiredDeviceCapabilities</key>
diff --git a/talk/examples/ios/AppRTCDemo/ResourceRules.plist b/talk/examples/objc/AppRTCDemo/ios/ResourceRules.plist
similarity index 100%
rename from talk/examples/ios/AppRTCDemo/ResourceRules.plist
rename to talk/examples/objc/AppRTCDemo/ios/ResourceRules.plist
diff --git a/talk/examples/ios/AppRTCDemo/en.lproj/APPRTCViewController.xib b/talk/examples/objc/AppRTCDemo/ios/en.lproj/APPRTCViewController.xib
similarity index 98%
rename from talk/examples/ios/AppRTCDemo/en.lproj/APPRTCViewController.xib
rename to talk/examples/objc/AppRTCDemo/ios/en.lproj/APPRTCViewController.xib
index 62807fe..cb2dc83 100644
--- a/talk/examples/ios/AppRTCDemo/en.lproj/APPRTCViewController.xib
+++ b/talk/examples/objc/AppRTCDemo/ios/en.lproj/APPRTCViewController.xib
@@ -52,7 +52,7 @@
<bool key="IBUIClipsSubviews">YES</bool>
<bool key="IBUIUserInteractionEnabled">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
- <string key="IBUIText">Use Safari and open a URL with a scheme of apprtc to load the test app and connect. i.e. apprtc://apprtc.appspot.com/?r=12345678 Or just enter the room below to connect to apprtc.</string>
+ <string key="IBUIText">Enter the room below to connect to apprtc.</string>
<object class="IBUITextInputTraits" key="IBUITextInputTraits">
<int key="IBUIAutocapitalizationType">2</int>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
diff --git a/talk/examples/ios/AppRTCDemo/main.m b/talk/examples/objc/AppRTCDemo/ios/main.m
similarity index 100%
rename from talk/examples/ios/AppRTCDemo/main.m
rename to talk/examples/objc/AppRTCDemo/ios/main.m
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h b/talk/examples/objc/AppRTCDemo/mac/APPRTCAppDelegate.h
similarity index 68%
copy from talk/examples/ios/AppRTCDemo/APPRTCViewController.h
copy to talk/examples/objc/AppRTCDemo/mac/APPRTCAppDelegate.h
index 1737a13..77011f1 100644
--- a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h
+++ b/talk/examples/objc/AppRTCDemo/mac/APPRTCAppDelegate.h
@@ -1,6 +1,6 @@
/*
* libjingle
- * Copyright 2013, Google Inc.
+ * Copyright 2014, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -25,22 +25,7 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#import <UIKit/UIKit.h>
+#import <Cocoa/Cocoa.h>
-@class RTCEAGLVideoView;
-
-// The view controller that is displayed when AppRTCDemo is loaded.
-@interface APPRTCViewController : UIViewController<UITextFieldDelegate>
-
-@property(weak, nonatomic) IBOutlet UITextField* roomInput;
-@property(weak, nonatomic) IBOutlet UITextView* instructionsView;
-@property(weak, nonatomic) IBOutlet UITextView* logView;
-@property(weak, nonatomic) IBOutlet UIView* blackView;
-
-@property(nonatomic, strong) RTCEAGLVideoView* localVideoView;
-@property(nonatomic, strong) RTCEAGLVideoView* remoteVideoView;
-
-- (void)displayText:(NSString*)text;
-- (void)resetUI;
-
+@interface APPRTCAppDelegate : NSObject<NSApplicationDelegate>
@end
diff --git a/talk/examples/objc/AppRTCDemo/mac/APPRTCAppDelegate.m b/talk/examples/objc/AppRTCDemo/mac/APPRTCAppDelegate.m
new file mode 100644
index 0000000..d6bd4fc
--- /dev/null
+++ b/talk/examples/objc/AppRTCDemo/mac/APPRTCAppDelegate.m
@@ -0,0 +1,77 @@
+/*
+ * libjingle
+ * Copyright 2014, 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.
+ */
+
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+
+#import "APPRTCAppDelegate.h"
+
+#import "APPRTCViewController.h"
+#import "RTCPeerConnectionFactory.h"
+
+@interface APPRTCAppDelegate () <NSWindowDelegate>
+@end
+
+@implementation APPRTCAppDelegate {
+ APPRTCViewController* _viewController;
+ NSWindow* _window;
+}
+
+#pragma mark - NSApplicationDelegate
+
+- (void)applicationDidFinishLaunching:(NSNotification*)notification {
+ [RTCPeerConnectionFactory initializeSSL];
+ NSScreen* screen = [NSScreen mainScreen];
+ NSRect visibleRect = [screen visibleFrame];
+ NSRect windowRect = NSMakeRect(NSMidX(visibleRect),
+ NSMidY(visibleRect),
+ 1320,
+ 1140);
+ NSUInteger styleMask = NSTitledWindowMask | NSClosableWindowMask;
+ _window = [[NSWindow alloc] initWithContentRect:windowRect
+ styleMask:styleMask
+ backing:NSBackingStoreBuffered
+ defer:NO];
+ _window.delegate = self;
+ [_window makeKeyAndOrderFront:self];
+ [_window makeMainWindow];
+ _viewController = [[APPRTCViewController alloc] initWithNibName:nil
+ bundle:nil];
+ [_window setContentView:[_viewController view]];
+}
+
+#pragma mark - NSWindow
+
+- (void)windowWillClose:(NSNotification*)notification {
+ [_viewController windowWillClose:notification];
+ [RTCPeerConnectionFactory deinitializeSSL];
+ [NSApp terminate:self];
+}
+
+@end
+
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h b/talk/examples/objc/AppRTCDemo/mac/APPRTCViewController.h
similarity index 69%
copy from talk/examples/ios/AppRTCDemo/APPRTCViewController.h
copy to talk/examples/objc/AppRTCDemo/mac/APPRTCViewController.h
index 1737a13..3ef058c 100644
--- a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h
+++ b/talk/examples/objc/AppRTCDemo/mac/APPRTCViewController.h
@@ -1,6 +1,6 @@
/*
* libjingle
- * Copyright 2013, Google Inc.
+ * Copyright 2014, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -25,22 +25,10 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#import <UIKit/UIKit.h>
+#import <Cocoa/Cocoa.h>
-@class RTCEAGLVideoView;
+@interface APPRTCViewController : NSViewController
-// The view controller that is displayed when AppRTCDemo is loaded.
-@interface APPRTCViewController : UIViewController<UITextFieldDelegate>
-
-@property(weak, nonatomic) IBOutlet UITextField* roomInput;
-@property(weak, nonatomic) IBOutlet UITextView* instructionsView;
-@property(weak, nonatomic) IBOutlet UITextView* logView;
-@property(weak, nonatomic) IBOutlet UIView* blackView;
-
-@property(nonatomic, strong) RTCEAGLVideoView* localVideoView;
-@property(nonatomic, strong) RTCEAGLVideoView* remoteVideoView;
-
-- (void)displayText:(NSString*)text;
-- (void)resetUI;
+- (void)windowWillClose:(NSNotification*)notification;
@end
diff --git a/talk/examples/objc/AppRTCDemo/mac/APPRTCViewController.m b/talk/examples/objc/AppRTCDemo/mac/APPRTCViewController.m
new file mode 100644
index 0000000..cf5b836
--- /dev/null
+++ b/talk/examples/objc/AppRTCDemo/mac/APPRTCViewController.m
@@ -0,0 +1,312 @@
+/*
+ * libjingle
+ * Copyright 2014, 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.
+ */
+
+#import "APPRTCViewController.h"
+
+#import <AVFoundation/AVFoundation.h>
+#import "APPRTCConnectionManager.h"
+#import "RTCNSGLVideoView.h"
+
+static NSUInteger const kContentWidth = 1280;
+static NSUInteger const kContentHeight = 720;
+static NSUInteger const kRoomFieldWidth = 80;
+static NSUInteger const kLogViewHeight = 280;
+
+@class APPRTCMainView;
+@protocol APPRTCMainViewDelegate
+
+- (void)appRTCMainView:(APPRTCMainView*)mainView
+ didEnterRoomId:(NSString*)roomId;
+
+@end
+
+@interface APPRTCMainView : NSView
+
+@property(nonatomic, weak) id<APPRTCMainViewDelegate> delegate;
+@property(nonatomic, readonly) RTCNSGLVideoView* localVideoView;
+@property(nonatomic, readonly) RTCNSGLVideoView* remoteVideoView;
+
+- (void)displayLogMessage:(NSString*)message;
+
+@end
+
+@interface APPRTCMainView () <NSTextFieldDelegate, RTCNSGLVideoViewDelegate>
+@end
+@implementation APPRTCMainView {
+ NSScrollView* _scrollView;
+ NSTextField* _roomLabel;
+ NSTextField* _roomField;
+ NSTextView* _logView;
+ RTCNSGLVideoView* _localVideoView;
+ RTCNSGLVideoView* _remoteVideoView;
+ CGSize _localVideoSize;
+ CGSize _remoteVideoSize;
+}
+
++ (BOOL)requiresConstraintBasedLayout {
+ return YES;
+}
+
+- (instancetype)initWithFrame:(NSRect)frame {
+ if (self = [super initWithFrame:frame]) {
+ [self setupViews];
+ }
+ return self;
+}
+
+- (void)updateConstraints {
+ NSParameterAssert(
+ _roomField != nil && _scrollView != nil && _remoteVideoView != nil);
+ [self removeConstraints:[self constraints]];
+ NSDictionary* viewsDictionary =
+ NSDictionaryOfVariableBindings(_roomLabel,
+ _roomField,
+ _scrollView,
+ _remoteVideoView);
+
+ NSSize remoteViewSize = [self remoteVideoViewSize];
+ NSDictionary* metrics = @{
+ @"kLogViewHeight" : @(kLogViewHeight),
+ @"kRoomFieldWidth" : @(kRoomFieldWidth),
+ @"remoteViewWidth" : @(remoteViewSize.width),
+ @"remoteViewHeight" : @(remoteViewSize.height),
+ };
+ // Declare this separately to avoid compiler warning about splitting string
+ // within an NSArray expression.
+ NSString* verticalConstraint =
+ @"V:|-[_roomLabel]-[_roomField]-[_scrollView(kLogViewHeight)]"
+ "-[_remoteVideoView(remoteViewHeight)]-|";
+ NSArray* constraintFormats = @[
+ verticalConstraint,
+ @"|-[_roomLabel]",
+ @"|-[_roomField(kRoomFieldWidth)]",
+ @"|-[_scrollView(remoteViewWidth)]-|",
+ @"|-[_remoteVideoView(remoteViewWidth)]-|",
+ ];
+ for (NSString* constraintFormat in constraintFormats) {
+ NSArray* constraints =
+ [NSLayoutConstraint constraintsWithVisualFormat:constraintFormat
+ options:0
+ metrics:metrics
+ views:viewsDictionary];
+ for (NSLayoutConstraint* constraint in constraints) {
+ [self addConstraint:constraint];
+ }
+ }
+ [super updateConstraints];
+}
+
+- (void)displayLogMessage:(NSString*)message {
+ _logView.string =
+ [NSString stringWithFormat:@"%@%@\n", _logView.string, message];
+ NSRange range = NSMakeRange([_logView.string length], 0);
+ [_logView scrollRangeToVisible:range];
+}
+
+#pragma mark - NSControl delegate
+
+- (void)controlTextDidEndEditing:(NSNotification*)notification {
+ NSDictionary* userInfo = [notification userInfo];
+ NSInteger textMovement = [userInfo[@"NSTextMovement"] intValue];
+ if (textMovement == NSReturnTextMovement) {
+ [self.delegate appRTCMainView:self didEnterRoomId:_roomField.stringValue];
+ }
+}
+
+#pragma mark - RTCNSGLVideoViewDelegate
+
+- (void)videoView:(RTCNSGLVideoView*)videoView
+ didChangeVideoSize:(NSSize)size {
+ if (videoView == _remoteVideoView) {
+ _remoteVideoSize = size;
+ } else if (videoView == _localVideoView) {
+ _localVideoSize = size;
+ } else {
+ return;
+ }
+ [self setNeedsUpdateConstraints:YES];
+}
+
+#pragma mark - Private
+
+- (void)setupViews {
+ NSParameterAssert([[self subviews] count] == 0);
+
+ _roomLabel = [[NSTextField alloc] initWithFrame:NSZeroRect];
+ [_roomLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
+ [_roomLabel setBezeled:NO];
+ [_roomLabel setDrawsBackground:NO];
+ [_roomLabel setEditable:NO];
+ [_roomLabel setStringValue:@"Enter AppRTC room id:"];
+ [self addSubview:_roomLabel];
+
+ _roomField = [[NSTextField alloc] initWithFrame:NSZeroRect];
+ [_roomField setTranslatesAutoresizingMaskIntoConstraints:NO];
+ [self addSubview:_roomField];
+ [_roomField setEditable:YES];
+ [_roomField setDelegate:self];
+
+ _logView = [[NSTextView alloc] initWithFrame:NSZeroRect];
+ [_logView setMinSize:NSMakeSize(0, kLogViewHeight)];
+ [_logView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
+ [_logView setVerticallyResizable:YES];
+ [_logView setAutoresizingMask:NSViewWidthSizable];
+ NSTextContainer* textContainer = [_logView textContainer];
+ NSSize containerSize = NSMakeSize(kContentWidth, FLT_MAX);
+ [textContainer setContainerSize:containerSize];
+ [textContainer setWidthTracksTextView:YES];
+ [_logView setEditable:NO];
+
+ _scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
+ [_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
+ [_scrollView setHasVerticalScroller:YES];
+ [_scrollView setDocumentView:_logView];
+ [self addSubview:_scrollView];
+
+ NSOpenGLPixelFormatAttribute attributes[] = {
+ NSOpenGLPFADoubleBuffer,
+ NSOpenGLPFADepthSize, 24,
+ NSOpenGLPFAOpenGLProfile,
+ NSOpenGLProfileVersion3_2Core,
+ 0
+ };
+ NSOpenGLPixelFormat* pixelFormat =
+ [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
+ _remoteVideoView = [[RTCNSGLVideoView alloc] initWithFrame:NSZeroRect
+ pixelFormat:pixelFormat];
+ [_remoteVideoView setTranslatesAutoresizingMaskIntoConstraints:NO];
+ _remoteVideoView.delegate = self;
+ [self addSubview:_remoteVideoView];
+
+ // TODO(tkchin): create local video view.
+ // https://code.google.com/p/webrtc/issues/detail?id=3417.
+}
+
+- (NSSize)remoteVideoViewSize {
+ if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) {
+ return _remoteVideoSize;
+ } else {
+ return NSMakeSize(kContentWidth, kContentHeight);
+ }
+}
+
+- (NSSize)localVideoViewSize {
+ return NSZeroSize;
+}
+
+@end
+
+@interface APPRTCViewController ()
+ <APPRTCConnectionManagerDelegate, APPRTCMainViewDelegate, APPRTCLogger>
+@property(nonatomic, readonly) APPRTCMainView* mainView;
+@end
+
+@implementation APPRTCViewController {
+ APPRTCConnectionManager* _connectionManager;
+}
+
+- (instancetype)initWithNibName:(NSString*)nibName
+ bundle:(NSBundle*)bundle {
+ if (self = [super initWithNibName:nibName bundle:bundle]) {
+ _connectionManager =
+ [[APPRTCConnectionManager alloc] initWithDelegate:self
+ logger:self];
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [self disconnect];
+}
+
+- (void)loadView {
+ APPRTCMainView* view = [[APPRTCMainView alloc] initWithFrame:NSZeroRect];
+ [view setTranslatesAutoresizingMaskIntoConstraints:NO];
+ view.delegate = self;
+ self.view = view;
+}
+
+- (void)windowWillClose:(NSNotification*)notification {
+ [self disconnect];
+}
+
+#pragma mark - APPRTCConnectionManagerDelegate
+
+- (void)connectionManager:(APPRTCConnectionManager*)manager
+ didReceiveLocalVideoTrack:(RTCVideoTrack*)localVideoTrack {
+ self.mainView.localVideoView.videoTrack = localVideoTrack;
+}
+
+- (void)connectionManager:(APPRTCConnectionManager*)manager
+ didReceiveRemoteVideoTrack:(RTCVideoTrack*)remoteVideoTrack {
+ self.mainView.remoteVideoView.videoTrack = remoteVideoTrack;
+}
+
+- (void)connectionManagerDidReceiveHangup:(APPRTCConnectionManager*)manager {
+ [self showAlertWithMessage:@"Remote closed connection"];
+ [self disconnect];
+}
+
+- (void)connectionManager:(APPRTCConnectionManager*)manager
+ didErrorWithMessage:(NSString*)message {
+ [self showAlertWithMessage:message];
+ [self disconnect];
+}
+
+#pragma mark - APPRTCLogger
+
+- (void)logMessage:(NSString*)message {
+ [self.mainView displayLogMessage:message];
+}
+
+#pragma mark - APPRTCMainViewDelegate
+
+- (void)appRTCMainView:(APPRTCMainView*)mainView
+ didEnterRoomId:(NSString*)roomId {
+ NSString* urlString =
+ [NSString stringWithFormat:@"https://apprtc.appspot.com/?r=%@", roomId];
+ [_connectionManager connectToRoomWithURL:[NSURL URLWithString:urlString]];
+}
+
+#pragma mark - Private
+
+- (APPRTCMainView*)mainView {
+ return (APPRTCMainView*)self.view;
+}
+
+- (void)showAlertWithMessage:(NSString*)message {
+ NSAlert* alert = [[NSAlert alloc] init];
+ [alert setMessageText:message];
+ [alert runModal];
+}
+
+- (void)disconnect {
+ self.mainView.remoteVideoView.videoTrack = nil;
+ [_connectionManager disconnect];
+}
+
+@end
diff --git a/talk/examples/objc/AppRTCDemo/mac/Info.plist b/talk/examples/objc/AppRTCDemo/mac/Info.plist
new file mode 100644
index 0000000..4dcb240
--- /dev/null
+++ b/talk/examples/objc/AppRTCDemo/mac/Info.plist
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>en</string>
+ <key>CFBundleDisplayName</key>
+ <string>${PRODUCT_NAME}</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIdentifier</key>
+ <string>com.Google.${PRODUCT_NAME:rfc1034identifier}</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundleName</key>
+ <string>${PRODUCT_NAME}</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleShortVersionString</key>
+ <string>1.0</string>
+ <key>CFBundleVersion</key>
+ <string>1.0</string>
+ <key>LSMinimumSystemVersion</key>
+ <string>${MACOSX_DEPLOYMENT_TARGET}</string>
+ <key>NSPrincipalClass</key>
+ <string>NSApplication</string>
+</dict>
+</plist>
\ No newline at end of file
diff --git a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h b/talk/examples/objc/AppRTCDemo/mac/main.m
similarity index 68%
copy from talk/examples/ios/AppRTCDemo/APPRTCViewController.h
copy to talk/examples/objc/AppRTCDemo/mac/main.m
index 1737a13..9ce3de2 100644
--- a/talk/examples/ios/AppRTCDemo/APPRTCViewController.h
+++ b/talk/examples/objc/AppRTCDemo/mac/main.m
@@ -1,6 +1,6 @@
/*
* libjingle
- * Copyright 2013, Google Inc.
+ * Copyright 2014, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -25,22 +25,15 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#import <UIKit/UIKit.h>
+#import <Cocoa/Cocoa.h>
-@class RTCEAGLVideoView;
+#import "APPRTCAppDelegate.h"
-// The view controller that is displayed when AppRTCDemo is loaded.
-@interface APPRTCViewController : UIViewController<UITextFieldDelegate>
-
-@property(weak, nonatomic) IBOutlet UITextField* roomInput;
-@property(weak, nonatomic) IBOutlet UITextView* instructionsView;
-@property(weak, nonatomic) IBOutlet UITextView* logView;
-@property(weak, nonatomic) IBOutlet UIView* blackView;
-
-@property(nonatomic, strong) RTCEAGLVideoView* localVideoView;
-@property(nonatomic, strong) RTCEAGLVideoView* remoteVideoView;
-
-- (void)displayText:(NSString*)text;
-- (void)resetUI;
-
-@end
+int main(int argc, char* argv[]) {
+ @autoreleasepool {
+ [NSApplication sharedApplication];
+ APPRTCAppDelegate* delegate = [[APPRTCAppDelegate alloc] init];
+ [NSApp setDelegate:delegate];
+ [NSApp run];
+ }
+}
diff --git a/talk/examples/ios/Icon.png b/talk/examples/objc/Icon.png
similarity index 100%
rename from talk/examples/ios/Icon.png
rename to talk/examples/objc/Icon.png
Binary files differ
diff --git a/talk/examples/objc/README b/talk/examples/objc/README
new file mode 100644
index 0000000..bfe18b3
--- /dev/null
+++ b/talk/examples/objc/README
@@ -0,0 +1,3 @@
+This directory contains sample iOS and mac clients for http://apprtc.appspot.com
+
+See ../../app/webrtc/objc/README for information on how to use it.