10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2013, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#if !defined(__has_feature) || !__has_feature(objc_arc)
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#error "This file requires ARC support."
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
32333c9e9a22496185b7986a9b6c998ab162e3dc57fischman@webrtc.org#import "RTCPeerConnection+Internal.h"
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
34a00076bd69084cb5091df8e007f34236d9647b65tkchin@webrtc.org#import "RTCDataChannel+Internal.h"
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#import "RTCEnumConverter.h"
36333c9e9a22496185b7986a9b6c998ab162e3dc57fischman@webrtc.org#import "RTCICECandidate+Internal.h"
37333c9e9a22496185b7986a9b6c998ab162e3dc57fischman@webrtc.org#import "RTCICEServer+Internal.h"
38333c9e9a22496185b7986a9b6c998ab162e3dc57fischman@webrtc.org#import "RTCMediaConstraints+Internal.h"
39333c9e9a22496185b7986a9b6c998ab162e3dc57fischman@webrtc.org#import "RTCMediaStream+Internal.h"
40664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org#import "RTCMediaStreamTrack+Internal.h"
4152348f6729702ec8888955b7aa5d6b47a4727e6atkchin@webrtc.org#import "RTCPeerConnectionObserver.h"
42333c9e9a22496185b7986a9b6c998ab162e3dc57fischman@webrtc.org#import "RTCSessionDescription+Internal.h"
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#import "RTCSessionDescription.h"
44cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#import "RTCSessionDescriptionDelegate.h"
45664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org#import "RTCStatsDelegate.h"
46664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org#import "RTCStatsReport+Internal.h"
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/app/webrtc/jsep.h"
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgNSString* const kRTCSessionDescriptionDelegateErrorDomain = @"RTCSDPError";
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgint const kRTCSessionDescriptionDelegateErrorCode = -1;
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace webrtc {
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass RTCCreateSessionDescriptionObserver
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    : public CreateSessionDescriptionObserver {
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
58af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org  RTCCreateSessionDescriptionObserver(
59af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org      id<RTCSessionDescriptionDelegate> delegate,
60af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org      RTCPeerConnection* peerConnection) {
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    _delegate = delegate;
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    _peerConnection = peerConnection;
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
65bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  virtual void OnSuccess(SessionDescriptionInterface* desc) OVERRIDE {
66bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org    RTCSessionDescription* session =
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        [[RTCSessionDescription alloc] initWithSessionDescription:desc];
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    [_delegate peerConnection:_peerConnection
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        didCreateSessionDescription:session
70bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                              error:nil];
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
73bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  virtual void OnFailure(const std::string& error) OVERRIDE {
74bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org    NSString* str = @(error.c_str());
75bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org    NSError* err =
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                            code:kRTCSessionDescriptionDelegateErrorCode
78bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                        userInfo:@{@"error" : str}];
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    [_delegate peerConnection:_peerConnection
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        didCreateSessionDescription:nil
81bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                              error:err];
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
85af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org  id<RTCSessionDescriptionDelegate> _delegate;
86bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  RTCPeerConnection* _peerConnection;
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass RTCSetSessionDescriptionObserver : public SetSessionDescriptionObserver {
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
91af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org  RTCSetSessionDescriptionObserver(id<RTCSessionDescriptionDelegate> delegate,
92bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                                   RTCPeerConnection* peerConnection) {
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    _delegate = delegate;
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    _peerConnection = peerConnection;
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnSuccess() OVERRIDE {
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    [_delegate peerConnection:_peerConnection
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        didSetSessionDescriptionWithError:nil];
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
102bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  virtual void OnFailure(const std::string& error) OVERRIDE {
103bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org    NSString* str = @(error.c_str());
104bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org    NSError* err =
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                            code:kRTCSessionDescriptionDelegateErrorCode
107bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                        userInfo:@{@"error" : str}];
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    [_delegate peerConnection:_peerConnection
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        didSetSessionDescriptionWithError:err];
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
113af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org  id<RTCSessionDescriptionDelegate> _delegate;
114bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  RTCPeerConnection* _peerConnection;
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
116664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org
117664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.orgclass RTCStatsObserver : public StatsObserver {
118664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org public:
119664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org  RTCStatsObserver(id<RTCStatsDelegate> delegate,
120664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org                   RTCPeerConnection* peerConnection) {
121664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org    _delegate = delegate;
122664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org    _peerConnection = peerConnection;
123664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org  }
124664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org
125664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org  virtual void OnComplete(const std::vector<StatsReport>& reports) OVERRIDE {
126664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org    NSMutableArray* stats = [NSMutableArray arrayWithCapacity:reports.size()];
127664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org    std::vector<StatsReport>::const_iterator it = reports.begin();
128664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org    for (; it != reports.end(); ++it) {
129664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org      RTCStatsReport* statsReport =
130664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org          [[RTCStatsReport alloc] initWithStatsReport:*it];
131664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org      [stats addObject:statsReport];
132664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org    }
133664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org    [_delegate peerConnection:_peerConnection didGetStats:stats];
134664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org  }
135664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org
136664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org private:
137664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org  id<RTCStatsDelegate> _delegate;
138664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org  RTCPeerConnection* _peerConnection;
139664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org};
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org@implementation RTCPeerConnection {
143bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  NSMutableArray* _localStreams;
1442a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<webrtc::RTCPeerConnectionObserver> _observer;
1452a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection;
1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
148bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org- (BOOL)addICECandidate:(RTCICECandidate*)candidate {
1492a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<const webrtc::IceCandidateInterface> iceCandidate(
150e2d02845acc9d9799623b519d12eb120bca79eb7fischman@webrtc.org      candidate.candidate);
151e2d02845acc9d9799623b519d12eb120bca79eb7fischman@webrtc.org  return self.peerConnection->AddIceCandidate(iceCandidate.get());
1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
154bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org- (BOOL)addStream:(RTCMediaStream*)stream
155bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org      constraints:(RTCMediaConstraints*)constraints {
1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  BOOL ret = self.peerConnection->AddStream(stream.mediaStream,
1570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                            constraints.constraints);
1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (!ret) {
1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return NO;
1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  [_localStreams addObject:stream];
1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return YES;
1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
165a00076bd69084cb5091df8e007f34236d9647b65tkchin@webrtc.org- (RTCDataChannel*)createDataChannelWithLabel:(NSString*)label
166a00076bd69084cb5091df8e007f34236d9647b65tkchin@webrtc.org                                       config:(RTCDataChannelInit*)config {
167a00076bd69084cb5091df8e007f34236d9647b65tkchin@webrtc.org  std::string labelString([label UTF8String]);
1682a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<webrtc::DataChannelInterface> dataChannel =
169a00076bd69084cb5091df8e007f34236d9647b65tkchin@webrtc.org      self.peerConnection->CreateDataChannel(labelString,
170a00076bd69084cb5091df8e007f34236d9647b65tkchin@webrtc.org                                             config.dataChannelInit);
171a00076bd69084cb5091df8e007f34236d9647b65tkchin@webrtc.org  return [[RTCDataChannel alloc] initWithDataChannel:dataChannel];
172a00076bd69084cb5091df8e007f34236d9647b65tkchin@webrtc.org}
173a00076bd69084cb5091df8e007f34236d9647b65tkchin@webrtc.org
174af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org- (void)createAnswerWithDelegate:(id<RTCSessionDescriptionDelegate>)delegate
175bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                     constraints:(RTCMediaConstraints*)constraints {
1762a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<webrtc::RTCCreateSessionDescriptionObserver>
1772a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      observer(new rtc::RefCountedObject<
1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          webrtc::RTCCreateSessionDescriptionObserver>(delegate, self));
1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  self.peerConnection->CreateAnswer(observer, constraints.constraints);
1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
182af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org- (void)createOfferWithDelegate:(id<RTCSessionDescriptionDelegate>)delegate
183bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                    constraints:(RTCMediaConstraints*)constraints {
1842a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<webrtc::RTCCreateSessionDescriptionObserver>
1852a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      observer(new rtc::RefCountedObject<
1860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          webrtc::RTCCreateSessionDescriptionObserver>(delegate, self));
1870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  self.peerConnection->CreateOffer(observer, constraints.constraints);
1880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
190bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org- (void)removeStream:(RTCMediaStream*)stream {
1910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  self.peerConnection->RemoveStream(stream.mediaStream);
1920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  [_localStreams removeObject:stream];
1930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
195bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org- (void)setLocalDescriptionWithDelegate:
196af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org            (id<RTCSessionDescriptionDelegate>)delegate
197bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                     sessionDescription:(RTCSessionDescription*)sdp {
1982a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<webrtc::RTCSetSessionDescriptionObserver> observer(
1992a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      new rtc::RefCountedObject<webrtc::RTCSetSessionDescriptionObserver>(
2000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          delegate, self));
2010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  self.peerConnection->SetLocalDescription(observer, sdp.sessionDescription);
2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
204bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org- (void)setRemoteDescriptionWithDelegate:
205af6b980518ad509b79581f816cead6a7b9ade220tkchin@webrtc.org            (id<RTCSessionDescriptionDelegate>)delegate
206bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                      sessionDescription:(RTCSessionDescription*)sdp {
2072a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<webrtc::RTCSetSessionDescriptionObserver> observer(
2082a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      new rtc::RefCountedObject<webrtc::RTCSetSessionDescriptionObserver>(
2090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          delegate, self));
2100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  self.peerConnection->SetRemoteDescription(observer, sdp.sessionDescription);
2110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
213bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org- (BOOL)updateICEServers:(NSArray*)servers
214bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org             constraints:(RTCMediaConstraints*)constraints {
2150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  webrtc::PeerConnectionInterface::IceServers iceServers;
216bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  for (RTCICEServer* server in servers) {
2170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    iceServers.push_back(server.iceServer);
2180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return self.peerConnection->UpdateIce(iceServers, constraints.constraints);
2200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
222bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org- (RTCSessionDescription*)localDescription {
223bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  const webrtc::SessionDescriptionInterface* sdi =
2240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      self.peerConnection->local_description();
225bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  return sdi ? [[RTCSessionDescription alloc] initWithSessionDescription:sdi]
226bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org             : nil;
2270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
229bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org- (NSArray*)localStreams {
2300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return [_localStreams copy];
2310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
233bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org- (RTCSessionDescription*)remoteDescription {
234bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  const webrtc::SessionDescriptionInterface* sdi =
2350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      self.peerConnection->remote_description();
236bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  return sdi ? [[RTCSessionDescription alloc] initWithSessionDescription:sdi]
237bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org             : nil;
2380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org- (RTCICEConnectionState)iceConnectionState {
241bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  return [RTCEnumConverter
242bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org      convertIceConnectionStateToObjC:self.peerConnection
243bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                                          ->ice_connection_state()];
2440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org- (RTCICEGatheringState)iceGatheringState {
247bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org  return [RTCEnumConverter
248bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org      convertIceGatheringStateToObjC:self.peerConnection
249bb288156332dd2e684f0dde156bd984c73da210bfischman@webrtc.org                                         ->ice_gathering_state()];
2500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org- (RTCSignalingState)signalingState {
2530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return [RTCEnumConverter
2540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      convertSignalingStateToObjC:self.peerConnection->signaling_state()];
2550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org- (void)close {
2580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  self.peerConnection->Close();
2590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
261664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org- (BOOL)getStatsWithDelegate:(id<RTCStatsDelegate>)delegate
262664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org            mediaStreamTrack:(RTCMediaStreamTrack*)mediaStreamTrack
263664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org            statsOutputLevel:(RTCStatsOutputLevel)statsOutputLevel {
2642a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<webrtc::RTCStatsObserver> observer(
2652a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      new rtc::RefCountedObject<webrtc::RTCStatsObserver>(delegate,
266664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org                                                                self));
267664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org  webrtc::PeerConnectionInterface::StatsOutputLevel nativeOutputLevel =
268664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org      [RTCEnumConverter convertStatsOutputLevelToNative:statsOutputLevel];
269664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org  return self.peerConnection->GetStats(
270664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org      observer, mediaStreamTrack.mediaTrack, nativeOutputLevel);
271664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org}
272664ec031431b37eade6e400dadfd1709e7494fc1tkchin@webrtc.org
2730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org@end
2740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org@implementation RTCPeerConnection (Internal)
2760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
27752348f6729702ec8888955b7aa5d6b47a4727e6atkchin@webrtc.org- (instancetype)initWithFactory:(webrtc::PeerConnectionFactoryInterface*)factory
27852348f6729702ec8888955b7aa5d6b47a4727e6atkchin@webrtc.org     iceServers:(const webrtc::PeerConnectionInterface::IceServers&)iceServers
27952348f6729702ec8888955b7aa5d6b47a4727e6atkchin@webrtc.org    constraints:(const webrtc::MediaConstraintsInterface*)constraints {
28052348f6729702ec8888955b7aa5d6b47a4727e6atkchin@webrtc.org  NSParameterAssert(factory != NULL);
28152348f6729702ec8888955b7aa5d6b47a4727e6atkchin@webrtc.org  if (self = [super init]) {
28252348f6729702ec8888955b7aa5d6b47a4727e6atkchin@webrtc.org    _observer.reset(new webrtc::RTCPeerConnectionObserver(self));
28352348f6729702ec8888955b7aa5d6b47a4727e6atkchin@webrtc.org    _peerConnection = factory->CreatePeerConnection(
28452348f6729702ec8888955b7aa5d6b47a4727e6atkchin@webrtc.org        iceServers, constraints, NULL, NULL, _observer.get());
2850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    _localStreams = [[NSMutableArray alloc] init];
2860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return self;
2880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2902a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org- (rtc::scoped_refptr<webrtc::PeerConnectionInterface>)peerConnection {
2910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return _peerConnection;
2920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org@end
295